From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,60c0f30dbde782f6 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.68.224.130 with SMTP id rc2mr14960600pbc.2.1358137538944; Sun, 13 Jan 2013 20:25:38 -0800 (PST) Path: 6ni105439pbd.1!nntp.google.com!npeer03.iad.highwinds-media.com!peer01.am1!peering.am1!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!backlog2.nntp.ams.giganews.com!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.straub-nv.de!zen.net.uk!dedekind.zen.co.uk!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Record Elements and Endian Conversion Date: Thu, 10 Jan 2013 19:32:19 +0100 Organization: cbb software GmbH Message-ID: <18uvrk2updt1l.1h7clt4i3bi9k.dlg@40tude.net> References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: cDN0fd8KlIeJLyErIrSf0A.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 X-Original-Bytes: 3066 X-Received-Bytes: 3432 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Date: 2013-01-10T19:32:19+01:00 List-Id: On Thu, 10 Jan 2013 09:31:25 -0800 (PST), awdorrin wrote: > Is there any mechanism within Ada that would provide a way to iterate > across all components of a record, perhaps providing their data types? Yes, http://ada-programming.blogspot.de/2012/12/type-introspection-in-ada.html though that unlikely would solve your problem. > Problem I have is that this record is used (via a MEMCPY call) to populate > a buffer used to transmit an message via Ethernet. While I have adjusted > the record's representation clause to put the data fields of the record > into the correct bit location, those fields larger than a byte are of the > wrong Endianness within the buffer. One should never program network protocols this way. Here are short guide lines [unless you enjoy the problems you have]: 1. There should be a specification the packet encoding. 2. No layout representation clauses shall be used. 3. No compiler generated stream attributes shall be used, except maybe for Unsigned_8 or Character, e.g. a data type directly corresponding to the octet. A short example, getting big endian 32-bit unsigned from stream elements array: pragma Assert (Stream_Element'Size = 8); procedure Get ( Data : in out Stream_Element_Array; Pointer : in out Stream_Element_Offset; Value : out Unsigned_32 ) is begin if ( Pointer < Data'First or else ( Pointer > Data'Last and then Pointer > Data'Last + 1 ) ) then Raise_Exception ( Layout_Error'Identity, "Pointer is out of bounds" ); elsif Pointer + 3 > Data'Last then Raise_Exception (End_Error'Identity, "End of data"); end if; Value := ( Shift_Left (Unsigned_32 (Data (Pointer )), 24) or Shift_Left (Unsigned_32 (Data (Pointer + 1)), 16) or Shift_Left (Unsigned_32 (Data (Pointer + 2)), 8) or Unsigned_32 (Data (Pointer + 3)) ); Pointer := Pointer + 4; end Get; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de