Jump to content

Structure alignment


trancexx
 Share

Recommended Posts

Here's the thing. I know what to do but I don't know why.

It's freaking me out!

Will show you on one small example:

$sResFile = FileOpenDialog("Choose bitmap", "", "(*.bmp)")
If @error Then Exit


$structure_Binary_No1 = DllStructCreate("byte[" & FileGetSize($sResFile) & "]")
DllStructSetData($structure_Binary_No1, 1, FileRead($sResFile))

$structure_Resource_No1 = DllStructCreate("byte Identifier[2];" & _
        "byte BitmapSize[4];" & _
        "byte[2];" & _
        "byte[2];" & _
        "byte BitmapOffset[4];" & _
        "byte Body[" & DllStructGetSize($structure_Binary_No1) - 14 & "]", _
        DllStructGetPtr($structure_Binary_No1))



$structure_Resource_No2 = DllStructCreate("align 2;byte Identifier[2];" & _
        "dword BitmapSize;" & _
        "short;" & _
        "short;" & _
        "dword BitmapOffset;" & _
        "byte Body[" & DllStructGetSize($structure_Binary_No1) - 14 & "]", _
        DllStructGetPtr($structure_Binary_No1))


ConsoleWrite(DllStructGetData($structure_Resource_No1, "Identifier") & @CRLF)
ConsoleWrite(DllStructGetData($structure_Resource_No1, "BitmapSize") & @CRLF)
ConsoleWrite(DllStructGetData($structure_Resource_No1, 3) & @CRLF)
ConsoleWrite(DllStructGetData($structure_Resource_No1, 3) & @CRLF)
ConsoleWrite(DllStructGetData($structure_Resource_No1, "BitmapOffset") & @CRLF)
ConsoleWrite(BinaryMid(DllStructGetData($structure_Resource_No1, "Body"), 1, 10) & @CRLF); just first 10 bytes 

ConsoleWrite(@CRLF)

ConsoleWrite(DllStructGetData($structure_Resource_No2, "Identifier") & @CRLF)
ConsoleWrite(DllStructGetData($structure_Resource_No2, "BitmapSize") & @CRLF)
ConsoleWrite(DllStructGetData($structure_Resource_No2, 3) & @CRLF)
ConsoleWrite(DllStructGetData($structure_Resource_No2, 3) & @CRLF)
ConsoleWrite(DllStructGetData($structure_Resource_No2, "BitmapOffset") & @CRLF)
ConsoleWrite(BinaryMid(DllStructGetData($structure_Resource_No2, "Body"), 1, 10) & @CRLF); just first 10 bytes

First structure is all about bytes, second one is more advanced (almost certainly there is some more appropriate word).

Problem is that I have to align that one in order to get the data right.

Why, ooooo why??? So, the problem is align 2. Again, why? :)

btw, it will ask you to choose some .bmp file, and will show you it's header and first 10 bytes of actual picture.

Please help me undersand this one.

(And I think it's kind of cool that I'm reading to this kind of structures using built-in functions ...thank you ...thank you)

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I understand that question may seem abstract or whatever but it's not.

It's even not like some questions that you need to get that much into the posted code.

It's actually very simple question.

Why would some structure need additional alignment to make it right? And some other structures not.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Will bump this once more and drop it then.

I guess the answer is either "Because it's not aligned, d'oh!" or "You made a terrible and totally stupid mistake creating that other structure. Shame on you!"

Will try Chinese though:

Yes, my Chinese suck too!

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Hmm I first wouldn't want to reply since I practically have no experience with alignments but I believe it's a compiler specific thing which you can control it yourself by padding the struct with single byte types.

If you haven't already read the wiki page

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Hmm I first wouldn't want to reply since I practically have no experience with alignments but I believe it's a compiler specific thing which you can control it yourself by padding the struct with single byte types.

If you haven't already read the wiki page

Thanks for replying.

Yes, I've read everyting that I could find on this.

This has something to do with "dword". If structure has "dword" then "align" is often required.

I find interesting that "align 2" cannot make any damage to any structure, at least I culdn't find one.

I believe this is a bug. Something is not right.

$structure_Binary = DllStructCreate("byte[6]")
DllStructSetData($structure_Binary, 1, "0x3378BB453D11"); that is 0x3378 and 0xBB453D11 

$struct =  DllStructCreate("align 1;byte[2];dword;", DllStructGetPtr($structure_Binary))

ConsoleWrite(DllStructGetData($struct, 1) & @CRLF)
ConsoleWrite(DllStructGetData($struct, 2) & @CRLF)

Try without "align" part

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I think I got it.

It's a bug. "dword" is unsigned integer and in some situations AutoIt is handling it wrong. - like it's signed and will for some reason skipp MSB and move to left one byte inside dword (to right generally).

Like I said, I think.

edit;

$structure_Binary = DllStructCreate("byte[7]")
DllStructSetData($structure_Binary, 1, "0x3378FF453D1133"); that is 0x3378 and 0xBB453D11, last byte 0x33 is just for testing 

$struct_1 =  DllStructCreate("short;dword;", DllStructGetPtr($structure_Binary))
ConsoleWrite("Before align:" & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct_1, 1)) & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct_1, 2)) & " <-- 0xFF is missing and 0x33 is there" & @CRLF)

ConsoleWrite(@CRLF)

$struct =  DllStructCreate("align 1;short;dword;", DllStructGetPtr($structure_Binary))
ConsoleWrite("After:" & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct, 1)) & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct, 2)) & " <-- this is ok" & @CRLF)

Like "align 4" is in front of every structure.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

You think wrong. From my comment here:

The code is correct. The helpfile almost explains why with this line:

DllStructCreate("short;int") ; structure is 8 bytes, the "int" is at offset 4

The key there is, the int (dword in your example) starts at byte 4 (0-index) and not at byte 2 like you are thinking.

So, we have the data 0x3378FF453D1133. The 0x3378FF45 portion is mapped to the short but only the first 2 bytes are accessible (0x3378). The 0xFF45 portion is mapped to the 2 bytes of padding that appears between the short and the dword. That means that the dword is 0x3D113300. Visually it looks like:

3378FF453D113300 (Extra 00's added to pad length for clarity).
SSSSPPPPDDDDDDDD (Where S = short, P = padding and D = dword).

When you start changing the alignment, you remove the padding and the structures align correctly.

This is not a bug.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...