comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <jrcarter@acm.org>
Subject: Re: Ada records and byte order
Date: Sat, 23 Jun 2001 16:35:28 GMT
Date: 2001-06-23T16:35:28+00:00	[thread overview]
Message-ID: <3B34C543.2EDC59C9@acm.org> (raw)
In-Reply-To: e7ebd224.0106230215.3f684ac6@posting.google.com

Karl Ran wrote:
> 
> Hi there,
> 
> I've a question about Ada records:
> 
> I have to read a binary file from disk which was written by another program.
> 
> The record size is fixed: 3 bytes
> The file structure looks like this:
> 
> 
> |byte 1 | |byte 2 | |byte 3 |   |byte 4...
> aaaa.aaaa.bbbb.bbbb.bbbb.cccc | aaaa.a.....
> M       L M            L M  L
> S       S S            S S  S
> B       B B            B B  B
> 
> a:  8 bits
> b: 12 bits
> c:  4 bits
> 
> Here is the code ...
> 
> procedure test is
> 
>    subtype BYTE     is Integer range 0 .. 2 **   8 - 1;
>    subtype WORD12   is integer range 0 .. 2 **  12 - 1;
>    subtype WORD4    is integer range 0 .. 2 **   4 - 1;
> 
>    type My_rec is
>       record
>          A : BYTE;
>          B : WORD12;
>          C : WORD4;
>       end record;
> 
>    for My_rec use
>       record
>          A at 0 range 0 ..  7;
>          B at 1 range 0 .. 11;
>          C at 2 range 0 ..  3;
>       end record;
> 
>    Abc : My_Rec;
> 
> begin
>    Abc.b := 16#123#;
>    ...
> end test;
> 
> ... which fails to compile whith GNAT on a (low-endian) i386:
> test.adb:31:10: component "C" overlaps "B" at line 30
> 
> Is there an Ada like (TM) solution for this kind of (byte order) problem?

On a little-endian machine, you are placing B in all of byte 3 and the 4
LSBs of byte 2. Since you are also placing C in the 4 LSBs of byte 3,
you are attempting to overlap these components.

The basic problem is that B is "at 1 range 4 .. 15" on a little-endian
machine. 15 is the MSB, and 0 the LSB, of a 16-bit word, on a
little-endian machine. Your numbering would be appropriate on a
big-endian machine. You can use System.Default_Bit_Order to obtain
endian-independent bit numbers, since it is now required to be static.

You might also want to use modular types rather than subtypes of
Integer, such as

subtype Byte is Interfaces.Unsigned_8;
type Word_12 is mod 2 ** 12;
type Nibble  is mod 2 **  4;

You might also want to include 'Size clauses on your types. The objects
(variables) that you use to read these values from the file should be
explicitly set to 24 bits to help ensure that you don't read 32 bits and
throw away 8.

-- 
Jeff Carter
"Perfidious English mouse-dropping hoarders."
Monty Python & the Holy Grail



  parent reply	other threads:[~2001-06-23 16:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-23 10:15 Ada records and byte order Karl Ran
2001-06-23 11:18 ` Carbonne Damien
2001-06-23 16:35 ` Jeffrey Carter [this message]
2001-06-23 19:30 ` David C. Hoos, Sr.
2001-06-23 22:34   ` Jeffrey Carter
2001-06-25 10:19   ` Karl Ran
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox