[fpc-pascal] does the following translation of a c struct to FPC is correct ?
ik
idokan at gmail.com
Sun Feb 26 14:16:09 CET 2012
On Sun, Feb 26, 2012 at 15:08, Jonas Maebe <jonas.maebe at elis.ugent.be> wrote:
>
> On 26 Feb 2012, at 13:54, ik wrote:
>
>> to the following Pascal record:
>>
>>
>> iphdr = record
>> {$IFDEF ENDIAN_LITTLE}
>> jhl : Cardinal; // __u8 ihl:4,
>> version : Cardinal; // version:4
>
> The ":4" in C means "4 bits", not 4 bytes. The correct translation in the general case for bitfields is hard, because FPC does not support C-compatible bitpacking (it depends on the type of the bitfield, e.g. "int a:4;" and "char a:4" have different alignment rules). In this case, this should match the C layout:
>
> {$packrecords c}
> iphdr = record
> bitfields = bitpacked record
> {$IFDEF ENDIAN_LITTLE}
> jhl: 0..(1 shl 4)-1;
> version: 0..(1 shl 4)-1;
> {$ELSE}
> jhl: 0..(1 shl 4)-1;
> version: 0..(1 shl 4)-1;
> {$ENDIF}
> end;
> tos : cuint8;
> tot_len : cint16;
> id : cint16;
> frag_off : cint16;
> ttl : cint8;
> protocol : cint8;
> check : cuint16;
> saddr : cint32;
> daddr : cint32;
> end;
>
> You will then of course have to use iphdrvar.bitfields.jhl instead of iphdrvar.jhl.
I'ved replaced it to
iphdr = bitpacked record
{$IFDEF ENDIAN_LITTLE}
jhl : 0..(1 shl 4)-1; // __u8 ihl:4,
version : 0..(1 shl 4)-1; // version:4
{$ELSE}
version : 0..(1 shl 4)-1; // __u8 version:4,
jhl : 0..(1 shl 4)-1; // version:4
{$ENDIF}
tos : cuint8;
tot_len : cint16;
id : cint16;
frag_off : cint16;
ttl : cint8;
protocol : cint8;
check : cuint16;
saddr : cint32;
daddr : cint32;
end;
And now all of the problems that I continue to find gone away.
Thank you
>
>
> Jonas_______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
More information about the fpc-pascal
mailing list