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.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,d778a4f52acd9d43 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 d10mr7839741pbw.6.1324556680008; Thu, 22 Dec 2011 04:24:40 -0800 (PST) Path: lh20ni51563pbb.0!nntp.google.com!news2.google.com!volia.net!news2.volia.net!feed-A.news.volia.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Representation clauses for base-64 encoding Date: Thu, 22 Dec 2011 14:24:38 +0200 Organization: Tidorum Ltd Message-ID: <9lgls6FticU1@mid.individual.net> References: <4ef31672$0$6574$9b4e6d93@newsspool3.arcor-online.net> Mime-Version: 1.0 X-Trace: individual.net 1UTMM5Yf/Uz0ua1W39goOwBuz5mxItWhlrfMSRGgak/lqk7MZ1 Cancel-Lock: sha1:XIzrvxBODkqTkrsajfjcUtbpED4= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <4ef31672$0$6574$9b4e6d93@newsspool3.arcor-online.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Date: 2011-12-22T14:24:38+02:00 List-Id: On 11-12-22 13:37 , Georg Bauhaus wrote: > On 22.12.11 10:41, Natasha Kerensikova wrote: >> 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; > > Here I would stop. > > The RFC says that a value from the range 0 .. 63 is associated with > a character from a specific set of characters, for encoding it: > 'A' .. 'Z', 'a' .. 'z', '+', '/'. > And there is a "pad", '='. > Since the characters shall stand for 0 .. 25, 26 .. 51, 52 .. 63, > this specifies a range, actually. > In Ada, the 1:1 translation into a type can be: > > type Repertoire is ( > 'A','B','C','D','E','F','G','H','I','J','K','L','M', > 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', > 'a','b','c','d','e','f','g','h','i','j','k','l','m', > 'n','o','p','q','r','s','t','u','v','w','x','y','z', > '0','1','2','3','4','5','6','7','8','9', > '+','/','='); > subtype Base_64_Character is Repertoire range 'A' .. '/'; > subtype Padding is Repertoire range '=' .. '='; This defines a nice Ada representation of the six-bit codes. But this was not Natasha's question; the question was about the slicing of a sequence of bits, composed from 8-bit groups, into a sequence of 6-bit groups. > Let the compiler choose the best representation for Repertoire > subtypes when encoding. The point of base-64 encoding is to emit the six-bit groups as ordinary Characters (using whatever character encoding is standard, for example Latin-1). The compiler's internal representation of Repertoire elements is not suitable; the Repertoire literal 'A' should be emitted as the Character 'A', not as six zero bits. Natasha did not mention that, of course, since the focus was on the mapping between 8-bit and 6-bit slices of the bit-string. > Only if you need some representation in memory or other storage > that has 'Size /= Character'Size, or 'Size /= Repertoire'Size > etc, derive new types as needed, and add representation clauses: That was the point, but the problem is the difficulty of making representation clauses portable. > > http://www.adacore.com/2008/03/03/gem-27/ I am surprised and disappointed that there is *no* mention of portability problems in that "gem". This is marketing hype for GNAT, not sound programming advice. > For streaming encoded text over the wire, a subtype of String should > serve the job just fine, convert as necessary. Huh? It is not possible to convert between Repertoire and Character, except through Unchecked_Conversion, which does not work as desired in this case. The best method would be an array indexed by Repertoire and containing Character elements, mapping the Repertoire literal 'A' to the Character 'A', etc. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .