[fpc-pascal] does the following translation of a c struct to FPC is correct ?

Jonas Maebe jonas.maebe at elis.ugent.be
Sun Feb 26 14:08:21 CET 2012


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.


Jonas


More information about the fpc-pascal mailing list