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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,d778a4f52acd9d43,start X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.75.170 with SMTP id d10mr7473409pbw.6.1324546890929; Thu, 22 Dec 2011 01:41:30 -0800 (PST) Path: lh20ni51148pbb.0!nntp.google.com!news1.google.com!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Natasha Kerensikova Newsgroups: comp.lang.ada Subject: Representation clauses for base-64 encoding Date: Thu, 22 Dec 2011 09:41:30 +0000 (UTC) Organization: A noiseless patient Spider Message-ID: Mime-Version: 1.0 Injection-Date: Thu, 22 Dec 2011 09:41:30 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="7fyd7lCvGVUn5eibBXudkw"; logging-data="14644"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cyhfGZOz2bOiWS/X/aEp/" User-Agent: slrn/0.9.9p1 (FreeBSD) Cancel-Lock: sha1:MsEaaCp5zNqNP14m+RENtAkTjZY= Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: 2011-12-22T09:41:30+00:00 List-Id: Hello, the recent discussion about representation clauses vs explicit shifting made me wonder about what is the Right Way of performing base-64 encoding (rfc 1421). My first thoughts were along the following lines: type Octet is mod 256; -- or Character or Storage_Element or Stream_Element -- or whatever 8-bit type relevant for the appliication for Octet'Size use 8; for Octet'Component_Size use 8; for Octet'Bit_Order use System.Low_Order_First; type Base_64_Digit is mod 64; for Base_64_Digit'Size use 6; for Base_64_Digit'Component_Size use 6; for Base_64_Digit'Bit_Order use System.Low_Order_First; type Octet_Block is array (1 .. 3) of Octet; pragma Pack (Octet_Block); type Base_64_Block is array (1 .. 4) of Base_64_Digit; pragma Pack (Base_64_Block); function Split_Base_64 is new Ada.Unchecked_Conversion (Source => Octet_Block, Target => Base_64_Block); function Merge_Base_64 is new Ada.Unchecked_Conversion (Source => Base_64_Block, Target => Octet_Block); However, if I understand 13.3(73) correctly, conforming compilers don't have to support such arrays (unless 6 and 8 are both factor or multiple of word size, but I guess there are not many 2-bit or 24-bit platforms around). It seems a more portable but uglier way of doing it is using record: instead of arrays: type Octet_Block is record P, Q, R : Octet; end record; for Octet_Block use record P at 0 range 0 .. 7; Q at 0 range 8 .. 15; R at 0 range 16 .. 23; end record; type Base_64_Block is record A, B, C, D : Base_64_Digit; end record; for Base_64_Block use record A at 0 range 0 .. 5; B at 0 range 6 .. 11; C at 0 range 12 .. 17; D at 0 range 18 .. 23; end record; Though I guess it might not work so well in 16-bit platforms. So is there a better way of doing it? Is it acceptable to handle portability with different bodies for a spec that only contains the Split_Base_64 and Merge_Base_64 functions? Or is there some things I'm missing that makes even that non-portable or even incorrect? Thanks in advance for sharing your wisdom, Natasha