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=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: AI12-0218: What is the portable representation clause for processing IETF packets on little-endian machines? Date: Fri, 11 May 2018 22:39:22 +0100 Organization: A noiseless patient Spider Message-ID: References: <9af47760-e731-4cb5-a1a0-d63e31019ce5@googlegroups.com> <877eob1cc6.fsf@nightsong.com> <5c9b9f90-884f-4de7-8663-d39a67949f4f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="906cab77493fe040d97f450db9e7653d"; logging-data="11412"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19A4128M0P/mzinqG/kenol1KroeQjaINU=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (darwin) Cancel-Lock: sha1:qEkPQyT3DXldx+68/BOLCxEI8uE= sha1:OavYg0beIKKkCdxmDjCvXDrekR8= Xref: reader02.eternal-september.org comp.lang.ada:52295 Date: 2018-05-11T22:39:22+01:00 List-Id: "Jeffrey R. Carter" writes: > On 05/11/2018 06:48 PM, Simon Wright wrote: >> "Jeffrey R. Carter" writes: >> >>> On 05/11/2018 03:49 PM, Simon Wright wrote: >>>> Lucretia writes: >>>> >>>>> Is it not possible to use derived types with different rep clauses >>>>> to implement byte swapping on assignment, like the pack/unpack >>>>> trick can? >>>> >>>> Maybe nowadays; not when that code was written. >>> >>> You can, and have been able to since Ada 83. >> >> I must be missing something. >> >> type T is record >> J : Integer; >> end T; >> >> type BE_T is new T; >> for BE_T use record >> ????? >> end record; > > In Ada 83, it would have been > > with Text_IO; > with Unchecked_Conversion; > procedure Byte_Swap is > type I16 is range -(2 ** 15) .. 2 ** 15 - 1; > for I16'Size use 16; > > type Byte is range -128 .. 127; > for Byte'Size use 8; > > type LE16 is record -- We'll assume this is the native order > LSB : Byte; > MSB : Byte; > end record; > for LE16'Size use 16; > for LE16 use record > LSB at 0 range 0 .. 7; > MSB at 1 range 0 .. 7; > end record; > > type BE16 is new LE16; > for BE16'Size use 16; > for BE16 use record > LSB at 1 range 0 .. 7; > MSB at 0 range 0 .. 7; > end record; I had somewhere got the idea that the parent type couldn't have representation clauses. But I see that Ada83 LRM 13.6 says "Hence, if an alternative representation is needed, it is necessary to declare a second type, derived from the first, and to specify a different representation for the second type." whereas Ada2012 says "To convert a record from one representation to another, two record types with a common ancestor type need to be declared, with no inherited subprograms." which seems to require _three_ types. But in any case your code doesn't answer my question: what representation trick could convert between type T is record J : Integer; end T; and type BE_T is new T; for BE_T use record ????? end record; One approach (the C one) would be to use htonl(), of course.