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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a883dc07df0d6bb1 X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: Decoding an octet stream Date: 1999/11/29 Message-ID: <81u247$kc3$1@hobbes2.crc.com>#1/1 X-Deja-AN: 554431532 References: <877lj2q36g.fsf@deneb.cygnus.argh.org> X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Complaints-To: abuse@crc.com X-Trace: hobbes2.crc.com 943885255 20867 198.175.145.56 (29 Nov 1999 14:20:55 GMT) Organization: CRC: A wholly owned subsidiary of Thermo Electron NNTP-Posting-Date: 29 Nov 1999 14:20:55 GMT Newsgroups: comp.lang.ada Date: 1999-11-29T14:20:55+00:00 List-Id: Florian Weimer <" "@deneb.cygnus.argh.org> wrote in message news:877lj2q36g.fsf@deneb.cygnus.argh.org... > What is the best way to decode an octet stream (i.e. a sequence of > unsigned eight-bit values) with Ada? The octet stream consists of > packet markes (single octets), two-octet integers (stored with the > most significant octet first), strings (sequences of octets of a given > length) and so on. I don't want to use Ada.Sequential_IO, because it > would mean that only one octet can be read at a time, which seems to > be quite inefficient. > > Streams seem to be nice, though. If I specify the necessary > representation clauses, I think I'll only have to write a few 'Read > and 'Write operations, and composed types will be handled correctly > almost automatically. But is this really portable? Are there any > targets (except supercomputers, mainframes, and embedded systems) > where Stream_Element'Size isn't equal to 8? > > BTW: I'm currently reading the two-octet integers using a construct like > `First_Octet * 2**8 + Second_Octet'. Is it possible to denote the octet > ordering using a representation clause? For a target with a matching > octet order, more efficient code could be generated. Here's a package I use for just that purpose. All two- and four-byte scalar types used in network packets have their 'read and 'write attributes overridden by instances of these generic procedures. ------------------------------------------------------------------------ -- Byte_Ordering -- Purpose: -- Instantiations of the generic procedures provided by This package -- are Read and Write procedures fully-conformant with the 'Read and -- 'Write attributes of the type with which the procedure was -- instantiated. -- The procedures transform the stream to network order, according to -- the endianness of the target host. -- Thus, instantiations of these procedures may be used to override -- the default stream-oriented attributes of scalar types, so that -- their stream reads and writes are done in network order, -- irrespective of host ordering (endianness). ------------------------------------------------------------------------ with Ada.Streams; package Byte_Ordering is generic type Item_Type is private; procedure Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Item_type); generic type Item_Type is private; procedure Write (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : Item_type); end Byte_Ordering; with System; package body Byte_Ordering is type U8 is mod 2 ** 8; for U8'Size use 8; type U8_Array is array (Integer range <>) of U8; --======================= -- Private subprograms == --======================= ---------- -- Swap -- ---------- procedure Swap (The_Item : in out U8_Array) is The_Bytes : array (1 .. The_Item'Size / U8'Size) of U8; for The_Bytes'Address use The_Item'Address; Temp : U8; begin for B in 1 .. The_Bytes'Last / 2 loop Temp := The_Bytes (B); The_Bytes (B) := The_Bytes (The_Bytes'Last - B + 1); The_Bytes (The_Bytes'Last - B + 1) := temp; end loop; end Swap; --====================== -- Public subprograms == --====================== ---------- -- Read -- ---------- procedure Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Item_type) is The_Bytes : U8_Array (1 .. Item'Size / U8'Size); for The_Bytes'Address use Item'Address; use type System.Bit_Order; begin U8_Array'Read (Stream, The_Bytes); if System.Default_Bit_Order = System.Low_Order_First then Swap (The_Bytes); end if; end Read; ----------- -- Write -- ----------- procedure Write (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : Item_Type) is Item_Copy : Item_type := Item; The_Bytes : U8_Array (1 .. Item_Copy'Size / U8'Size); for The_Bytes'Address use Item'Address; use type System.Bit_Order; begin if System.Default_Bit_Order = System.Low_Order_First then Swap (The_Bytes); end if; U8_Array'Write (Stream, The_Bytes); end Write; end Byte_Ordering;