From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: AI12-0218: What is the portable representation clause for processing IETF packets on little-endian machines?
Date: Fri, 11 May 2018 21:08:38 +0200
Date: 2018-05-11T21:08:38+02:00 [thread overview]
Message-ID: <pd4pnn$iai$1@dont-email.me> (raw)
In-Reply-To: <ly4ljejfys.fsf@pushface.org>
On 05/11/2018 06:48 PM, Simon Wright wrote:
> "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> writes:
>
>> On 05/11/2018 03:49 PM, Simon Wright wrote:
>>> Lucretia <laguest9000@googlemail.com> writes:
>>>
>>>> On Friday, 11 May 2018 08:55:28 UTC+1, Simon Wright wrote:
>>>>> In the body,
>>>>>
>>>>> Big_Endian : constant Boolean
>>>>> := System."=" (System.Default_Bit_Order, System.High_Order_First);
>>>>>
>>>>
>>>> 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;
function To_LE is new Unchecked_Conversion (Source => I16, Target => LE16);
function To_I16 is new Unchecked_Conversion (Source => BE16, Target => I16);
A : I16 := 299;
B : I16 := To_I16 (BE16 (To_LE (A) ) ); -- Bytes swapped
begin -- Byte_Swap
Text_IO.Put_Line (Item => I16'Image (B) );
end Byte_Swap;
Because BE16 is derived from LE16, there are conversions between them that do a
change of representation; in this case, byte swapping.
$ gnatmake -gnat83 -gnatan -gnato2 -O2 -fstack-check byte_swap.adb
$ ./byte_swap
11009
--
Jeff Carter
"Since I strongly believe that overpopulation is by
far the greatest problem in the world, this [Soylent
Green] would be my only message movie."
Charleton Heston
123
next prev parent reply other threads:[~2018-05-11 19:08 UTC|newest]
Thread overview: 63+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-05-10 17:45 AI12-0218: What is the portable representation clause for processing IETF packets on little-endian machines? Dan'l Miller
2018-05-10 19:24 ` Dan'l Miller
2018-05-10 20:32 ` Paul Rubin
2018-05-10 22:24 ` Dan'l Miller
2018-05-10 22:44 ` Niklas Holsti
2018-05-10 23:14 ` Paul Rubin
2018-05-11 2:38 ` Dan'l Miller
2018-05-11 7:55 ` Simon Wright
2018-05-11 12:11 ` Lucretia
2018-05-11 13:49 ` Simon Wright
2018-05-11 16:11 ` Jeffrey R. Carter
2018-05-11 16:48 ` Simon Wright
2018-05-11 19:08 ` Jeffrey R. Carter [this message]
2018-05-11 21:39 ` Simon Wright
2018-05-11 21:56 ` Jeffrey R. Carter
2018-05-12 7:08 ` Simon Wright
2018-05-12 7:53 ` Jeffrey R. Carter
2018-05-14 22:43 ` Randy Brukardt
2018-05-11 13:46 ` Simon Wright
2018-05-11 22:12 ` Randy Brukardt
2018-05-12 10:33 ` Björn Lundin
2018-05-12 13:08 ` Simon Wright
2018-05-12 14:21 ` Björn Lundin
2018-05-10 23:07 ` Paul Rubin
2018-05-11 0:14 ` Dan'l Miller
2018-05-11 0:30 ` Paul Rubin
2018-05-11 0:50 ` Dan'l Miller
2018-05-11 1:34 ` Paul Rubin
2018-05-11 2:11 ` Dan'l Miller
2018-05-11 22:32 ` Randy Brukardt
2018-05-11 8:02 ` Simon Wright
2018-05-11 22:14 ` Randy Brukardt
2018-05-10 19:28 ` Simon Wright
2018-05-10 22:40 ` Randy Brukardt
2018-05-10 22:50 ` Dan'l Miller
2018-05-11 22:00 ` Randy Brukardt
2018-05-12 1:15 ` Paul Rubin
2018-05-14 22:54 ` Randy Brukardt
2018-05-15 0:43 ` Paul Rubin
2018-05-15 21:39 ` Randy Brukardt
2018-05-15 0:44 ` Dennis Lee Bieber
2018-05-11 8:09 ` Simon Wright
2018-05-10 19:34 ` Dmitry A. Kazakov
2018-05-10 20:06 ` Dan'l Miller
2018-05-10 22:44 ` Paul Rubin
2018-05-10 22:50 ` Randy Brukardt
2018-05-11 9:40 ` Niklas Holsti
2018-05-11 11:40 ` Dan'l Miller
2018-05-11 20:16 ` Niklas Holsti
2018-05-11 9:40 ` Dmitry A. Kazakov
2018-05-11 14:21 ` AdaMagica
2018-05-26 16:15 ` Dan'l Miller
2018-05-26 19:02 ` AdaMagica
2018-05-26 21:01 ` Dan'l Miller
2018-05-27 14:58 ` AdaMagica
2018-05-27 18:03 ` Simon Wright
2018-05-29 22:17 ` Randy Brukardt
2018-05-30 6:39 ` Simon Wright
2018-05-30 7:25 ` Dmitry A. Kazakov
2018-05-30 15:01 ` Simon Wright
2018-05-30 15:59 ` Dan'l Miller
2018-05-30 19:38 ` Randy Brukardt
2018-05-27 18:04 ` Dan'l Miller
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox