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 08:55:25 +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="19700"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+znER2CRw84/E0Lc/gh4aKBHRgxQEN40I=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (darwin) Cancel-Lock: sha1:JKMOl4pvgkw9C5qeVQDm+2AUca4= sha1:Bhz76e91MJS2MWf95fxwbPWqWlc= Xref: reader02.eternal-september.org comp.lang.ada:52248 Date: 2018-05-11T08:55:25+01:00 List-Id: "Dan'l Miller" writes: > Still, standard Ada has no good Ada-esque solution to heterogenous > endianness at the perimeter of a system, other than writing C-esque > pointer-arithmetic code with the various unchecked_ constructs (which > even C programmers don't generally do; they utilize > conditionally-compiled macros that correctly type-cast > meticulously*-laid-out structs-of-bitfields onto that packet's header > or IC register's word. I've used (effectively) conditionally-compiled sections of code for this. >From an SNTP implementation: in BE, the first 32 bits of a packet are -- 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 -- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -- |LI | VN |Mode | Stratum | Poll | Precision | -- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ and looking just at the top (first) byte, the package spec contains type Leap_Indicator is (No_Warning, Last_Minute_Has_61_Seconds, Last_Minute_Has_59_Seconds, Alarm_Condition); type Version is range 3 .. 4; type Mode is (Reserved, Symmetric_Active, Symmetric_Passive, Client, Server, Broadcast, Reserved_For_NTP_Control_Message, Reserved_For_Private_Use); type Status is record LI : Leap_Indicator; VN : Version; M : Mode; end record; in native layout (nowadays I'd be a lot more specific about sizes). Conversions: function To_Stream_Element (S : Status) return Ada.Streams.Stream_Element; function To_Status (S : Ada.Streams.Stream_Element) return Status; In the body, Big_Endian : constant Boolean := System."=" (System.Default_Bit_Order, System.High_Order_First); and function To_Status (S : Ada.Streams.Stream_Element) return Status is begin -- these two sections are conditionally compiled (by GNAT, -- anyway) because Big_Endian is constant. if Big_Endian then declare -- create a BE type with BE representation type Host_Status is record LI : Leap_Indicator; VN : Version; M : Mode; end record; for Host_Status use record LI at 0 range 0 .. 1; VN at 0 range 2 .. 4; M at 0 range 5 .. 7; end record; for Host_Status'Size use 8; function Convert is new Ada.Unchecked_Conversion (Ada.Streams.Stream_Element, Host_Status); V : constant Host_Status := Convert (S); begin -- let the compiler convert from BE representation to host return (LI => V.LI, VN => V.VN, M => V.M); end; else declare -- create an LE type with LE representation type Host_Status is record LI : Leap_Indicator; VN : Version; M : Mode; end record; for Host_Status use record LI at 0 range 6 .. 7; VN at 0 range 3 .. 5; M at 0 range 0 .. 2; end record; for Host_Status'Size use 8; function Convert is new Ada.Unchecked_Conversion (Ada.Streams.Stream_Element, Host_Status); V : constant Host_Status := Convert (S); begin -- let the compiler convert from LE representation to host return (LI => V.LI, VN => V.VN, M => V.M); end; end if; end To_Status; No denying it's a lot of work. Much easier for 2-, 4-, 8-byte objects: type SNTP_Timestamp is delta 2.0 ** (-32) range -2.0 ** 31 .. 2.0 ** 31; for SNTP_Timestamp'Size use 64; subtype Timestamp_Slice is Ada.Streams.Stream_Element_Array (1 .. 8); function To_SNTP_Timestamp (T : Timestamp_Slice) return SNTP_Timestamp is function Convert is new Ada.Unchecked_Conversion (Timestamp_Slice, SNTP_Timestamp); begin if Big_Endian then return Convert (T); else return Convert ((1 => T (8), 2 => T (7), 3 => T (6), 4 => T (5), 5 => T (4), 6 => T (3), 7 => T (2), 8 => T (1))); end if; end To_SNTP_Timestamp;