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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e710f7d3f890e76b X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wn14feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada From: anon@anon.org (anon) Subject: Re: Base64-Encoding Reply-To: anon@anon.org (anon) References: <20071015161229.3f439230@cube.tz.axivion.com> X-Newsreader: IBM NewsReader/2 2.0 Message-ID: Date: Fri, 19 Oct 2007 04:33:24 GMT NNTP-Posting-Host: 12.65.54.225 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1192768404 12.65.54.225 (Fri, 19 Oct 2007 04:33:24 GMT) NNTP-Posting-Date: Fri, 19 Oct 2007 04:33:24 GMT Organization: AT&T Worldnet Xref: g2news2.google.com comp.lang.ada:2492 Date: 2007-10-19T04:33:24+00:00 List-Id: that should say type Six_Bits is new Integer mod 2**6 ; -- or use range 0 .. 63 ; In , anon@anon.org (anon) writes: >with Interfaces ; >use Interfaces ; >with System ; >use System ; >with Ada.Unchecked_Conversion ; > >package b64 is > > -- > -- Uses the system default on endian > -- > > type Six_Bits is new Integer range mod 2**6 ; -- or use 0 .. 63 ; > > > -- > -- base64_descriptor is a record that insures the endian is set > -- by the program ( in this case "little-endian format" ) in > -- architectures that have switchable endianness. > -- Such as ARM, PowerPC (but not the PPC970/G5), DEC Alpha, SPARC V9, > -- MIPS, PA-RISC and IA64 > -- > type base64_descriptor is record > Unused : Unsigned_8 range 0..3 ; > Bit_5 : Unsigned_8 range 0..1 ; > Bit_4 : Unsigned_8 range 0..1 ; > Bit_3 : Unsigned_8 range 0..1 ; > Bit_2 : Unsigned_8 range 0..1 ; > Bit_1 : Unsigned_8 range 0..1 ; > Bit_0 : Unsigned_8 range 0..1 ; > end record ; > -- > -- set bit order > -- > for base64_descriptor use record > Unused at 0 range 6..7 ; > Bit_5 at 0 range 5..5 ; > Bit_4 at 0 range 4..4 ; > Bit_3 at 0 range 3..3 ; > Bit_2 at 0 range 2..2 ; > Bit_1 at 0 range 1..1 ; > Bit_0 at 0 range 0..0 ; > end record ; > > -- > -- or you could use the following statement which > -- forces endian to little-endian format > -- > for base64_descriptor'Bit_Order use Low_Order_First ; > > -- > -- insure 1 byte usage for base64_descriptor ; > -- > pragma pack ( base64_descriptor ) ; > > > > function To_Base64 is new Ada.Unchecked_Conversion > ( Source => Unsigned_8, > Target => base64_descriptor ) ; > > function From_Base64 is new Ada.Unchecked_Conversion > ( Target => Unsigned_8, > Source => base64_descriptor ) ; > > >end ; > >In 2001 Tom Moran created a Ada BASE64 package which is archived at > >http://www.adapower.com/index.php?Command=Class&ClassID=Algorithms&CID=257 > >have a look at it. I a quick look it kind of suggest that the endian for >Six_Bits is not important. > > >In <20071015161229.3f439230@cube.tz.axivion.com>, Stefan Bellon writes: >>Hi all, >> >>I've been looking through the previous postings of the group and found >>two major threads where this topic has already been discussed. But the >>proposed solutions were all different to what I was thinking about. I >>have thought about the following: >> >> >>package body Base64 is >> >> type Six_Bits is mod 2**6; >> for Six_Bits'Size use 6; >> >> type Six_Bits_Array is array (Natural range <>) of Six_Bits; >> for Six_Bits_Array'Alignment use 1; -- To overlay over String type. >> for Six_Bits_Array'Component_Size use Six_Bits'Size; >> pragma Pack (Six_Bits_Array); >> >> Base64_Chars : constant array (Six_Bits) of Character := >> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; >> >> function Encode >> (Data : in String) >> return String >> is >> Padded_Length : constant Natural := ((Data'Length + 2) / 3) * 3; >> -- Pad input data to 3-Byte boundary. >> >> Base64_Length : constant Natural := Padded_Length / 3 * 4; >> -- Number of six-bit tokens necessary (including padding). >> >> Six_Bits_Length : constant Natural := (Data'Length * 4 + 2) / 3; >> -- Number of six-bit tokens necessary (without padding). >> >> Padded_Data : String (1 .. Padded_Length) := (others => ASCII.NUL); >> -- Padded input data. >> >> Base64_Data : Six_Bits_Array (1 .. Base64_Length); >> for Base64_Data'Address use Padded_Data'Address; >> -- Overlay array of six-bit tokens over the padded input data. >> >> Result : String (1 .. Base64_Length) := (others => '='); >> -- Output buffer, initialized with '=' tokens for unfilled >> -- end-markers. >> begin >> Padded_Data (1 .. Data'Length) := Data; >> -- Initialize data into padded-data (can't be done with aggregate >> -- in elaboration part, sadly). >> >> -- Do the actual encoding ... >> for I in 1 .. Six_Bits_Length loop >> Result (I) := Base64_Chars (Base64_Data (I)); >> end loop; >> >> return Result; >> end Encode; >> >>end Base64; >> >> >>However it looks like this solution has a problem with endianness, in a >>way that the wrong 6 bits of the Bytes are used in the conversion. >> >>Is there an easy way to fix this (as I think the rest would be pretty >>neat) or is this way of trying to do it, doomed to fail anyway? >> >>-- >>Stefan Bellon >