Recent

Author Topic: Accessing multiple bytes inside bytearray?  (Read 2393 times)

Sestus

  • New Member
  • *
  • Posts: 14
Accessing multiple bytes inside bytearray?
« on: September 23, 2021, 02:15:30 pm »
Hello,
i need to store a FileHandle that is of the type THandle inside a specific position in my ByteArray. The number stored inside the handle normally is about 49 000 000. When i access the Array via
Code: Pascal  [Select][+][-]
  1. //Exportieren der Datei
  2.                  hOpenResult := XWF_OpenItem(CurrentVolume, nItemID, $01); //Öffnen des Files, returned Handle, bei 0 unsuccessful
  3.  
  4. //Erzeugung des Hashs in XWF
  5.                  HashTypeBuf[0] := $11;
  6.                  HashTypeBuf[1] := hOpenResult;
  7.  
the second number stored inside the array is NOT the number inside the FileHandle as i want it. I thought the problem may be that on byte isn't sufficient for that huge number so i think 4 bytes to store the number would do the trick. My problem now is that i don't know how to access multiple bytes in the byte array so i can store the huge number inside for example the four bytes starting at HashTypeBuf[1].
Would be much appreciated if someone can help. :)

Bart

  • Hero Member
  • *****
  • Posts: 4472
    • Bart en Mariska's Webstek
Re: Accessing multiple bytes inside bytearray?
« Reply #1 on: September 23, 2021, 02:26:06 pm »
You can cast a THandle to array[0..SizeOf(THandle)-1] bytes and then access and store the individual bytes.
Code: Pascal  [Select][+][-]
  1. type
  2.   THandleAsBytes = array[0..SizeOf(THandle)-1] byte;
  3. ...
  4.   HashTypeBuf[1] := THandleAsBytes(Handle)[0];
  5.   ... etc.
You might get into trouble if Endianness changes.

Bart

Sestus

  • New Member
  • *
  • Posts: 14
Re: Accessing multiple bytes inside bytearray?
« Reply #2 on: September 23, 2021, 02:47:23 pm »
So when i do it like this
Code: Pascal  [Select][+][-]
  1. var
  2. THandleAsBytes             :   array[0..SizeOf(THandle)-1] of Byte;
  3. ...
  4. hOpenResult := XWF_OpenItem(CurrentVolume, nItemID, $01); //Öffnen des Files, returned Handle, bei 0 unsuccessful
  5. //Erzeugung des Hashs in XWF
  6. HashTypeBuf[0] := $10;
  7. HashTypeBuf[1] := THandleAsBytes(hOpenResult)[0];
  8.  
it doesn't compile but maybe i understood something wrong I'm still kinda new to FPC sry :)

Zvoni

  • Hero Member
  • *****
  • Posts: 739
Re: Accessing multiple bytes inside bytearray?
« Reply #3 on: September 23, 2021, 04:15:54 pm »
How "big" is the Array HashTypeBuf?
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Sestus

  • New Member
  • *
  • Posts: 14
Re: Accessing multiple bytes inside bytearray?
« Reply #4 on: September 23, 2021, 05:10:14 pm »
I have it just declared like that
Code: Pascal  [Select][+][-]
  1. HashTypeBuf                :   array of Byte;
and after it like that
Code: Pascal  [Select][+][-]
  1. SetLength(HashTypeBuf,                                     16);
« Last Edit: September 23, 2021, 05:28:03 pm by Sestus »

Warfley

  • Hero Member
  • *****
  • Posts: 554
Re: Accessing multiple bytes inside bytearray?
« Reply #5 on: September 23, 2021, 05:52:50 pm »
You can access any type as any other type through pointers, and Pointers function like arrays.
Accessing the bytes of a handle
Code: Pascal  [Select][+][-]
  1. PByte(@Handle)[0]; // PByte is byte pointer
  2. PByte(@Handle)[1];
  3. ...
And to access a byte array as handle:
Code: Pascal  [Select][+][-]
  1. PHandle(Pointer(ByteArray))^;
« Last Edit: September 23, 2021, 06:05:18 pm by Warfley »

Zvoni

  • Hero Member
  • *****
  • Posts: 739
Re: Accessing multiple bytes inside bytearray?
« Reply #6 on: September 23, 2021, 05:53:19 pm »
Should be enough.
Have you tried to „Move“ (memcopy) the array?
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

engkin

  • Hero Member
  • *****
  • Posts: 2958
Re: Accessing multiple bytes inside bytearray?
« Reply #7 on: September 23, 2021, 05:58:24 pm »
Code: Pascal  [Select][+][-]
  1.  type
  2.   PHandle=^THandle;
  3. ...
  4.   PHandle(@HashTypeBuf[1])^:=hOpenResule;

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1008
    • Lebeau Software
Re: Accessing multiple bytes inside bytearray?
« Reply #8 on: September 23, 2021, 06:16:50 pm »
Code: Pascal  [Select][+][-]
  1. Move(hOpenResult, HashTypeBuf[1], SizeOf(hOpenResult));
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Sestus

  • New Member
  • *
  • Posts: 14
Re: Accessing multiple bytes inside bytearray?
« Reply #9 on: September 23, 2021, 06:44:15 pm »
With Move(hOpenResult, HashTypeBuf[1], SizeOf(hOpenResult)); the array fills. Before the second Byte only was filles with a 160, with the new command it's 160, 67, 226, 2. The original value of hOpenResult was hOpenResult = 48382880 though. Do i have to declare the values before to be hex so in the array its right?
I will put the explanation of the original function from the api doc here so maybe if I'm doing something complety wrong here point me to the right spot.

"x10: flag to make this function compute the requested value(s) during the call if the hash value is not stored in the volume snapshot yet, which requires v19.7 or later, and a handle to the file (hItem) must be stored in the buffer at offset 4, i.e. directly after the DWORD value"... hItem im my case should be hOpenResult. The DWORD value is $10.

My current code:
Code: Pascal  [Select][+][-]
  1.                  hOpenResult := XWF_OpenItem(CurrentVolume, nItemID, $01); //Öffnen des Files, returned Handle, bei 0 unsuccessful
  2.  
  3.                  //Erzeugung des Hashs in XWF
  4.                  HashTypeBuf[0] := $10;
  5.                  Move(hOpenResult, HashTypeBuf[1], SizeOf(hOpenResult));
  6.                  GetHashOK := XWF_GetHashValue(nItemID, @HashTypeBuf[0]); //Hash Value mithilfe XWF Funktion berechnen
   

ASerge

  • Hero Member
  • *****
  • Posts: 1869
Re: Accessing multiple bytes inside bytearray?
« Reply #10 on: September 24, 2021, 03:49:41 am »
You might get into trouble if Endianness changes.
There are conversion functions like NtoBE. Example:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. procedure Test;
  5. type
  6.   THandleAsBytes = array[0..SizeOf(THandle)-1] of Byte;
  7. var
  8.   hOpenResult: THandle;
  9.   hOpenResultAsBytes: THandleAsBytes absolute hOpenResult;
  10.   HashTypeBuf: array of Byte;
  11. begin
  12.   SetLength(HashTypeBuf, 100);
  13.   hOpenResult := NtoBE($12345678); // A function that returns a handle $12345678
  14.   HashTypeBuf[1] := hOpenResultAsBytes[0];
  15.   Writeln(HashTypeBuf[1]);  // 18 (=$12 even for little-endian, x86 processor)
  16. end;
  17.  
  18. begin
  19.   Test;
  20.   Readln;
  21. end.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3406
  • Compiler Developer
Re: Accessing multiple bytes inside bytearray?
« Reply #11 on: September 24, 2021, 08:59:30 am »
I thought the problem may be that on byte isn't sufficient for that huge number so i think 4 bytes to store the number would do the trick.

Please note that the size of a handle depends on the platform, it might be 2, 4 or 8 Byte.

Also it might be a bad idea to use the handle as a hash as the value contained in the handle might change between runs. Where did you get the handle from and what are you trying to achieve with your hash?

Sestus

  • New Member
  • *
  • Posts: 14
Re: Accessing multiple bytes inside bytearray?
« Reply #12 on: September 24, 2021, 01:20:20 pm »
The XWF GetHashValue function writes the Hash value into the HashTypeBuf Buffer. The Handle has to be stored in the buffer before because the GetHashValue function needs the handle at offset 4 to work correctly. After the hash is in the buffer i want to write it into a string to later compare it with another string which is also a hash to see if its one and the same file.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3406
  • Compiler Developer
Re: Accessing multiple bytes inside bytearray?
« Reply #13 on: September 24, 2021, 01:25:53 pm »
But that's my point: a hash of the handle is nothing unique. If you open a file again it might get a different handle also if you open a file twice it will be the same file but different handles.

Sestus

  • New Member
  • *
  • Posts: 14
Re: Accessing multiple bytes inside bytearray?
« Reply #14 on: September 24, 2021, 01:33:42 pm »
The function does NOT compute a Hash for the handle. The handle is just for the function to work correctly. Normally the function will create a Hash for a file inside the program in which i want to implement my programm. I only need the handle because the documentary states that to work it needs this at offset 4. So as i understood it i get a hash inside the declared buffer which overrides the values that where previously there such as $10 and the handle to the file.
The problem now is that the numbers that get written inside the buffer are not the result of hOpenResult. So i think there is some kind of conversion with hex numbers or something going on but i dont know how to fix that.
« Last Edit: September 24, 2021, 01:55:51 pm by Sestus »

 

TinyPortal © 2005-2018