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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e710f7d3f890e76b,start X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!usenet-fr.net!news.buerger.net!news.uni-stuttgart.de!not-for-mail From: Stefan Bellon Newsgroups: comp.lang.ada Subject: Base64-Encoding Date: Mon, 15 Oct 2007 16:12:29 +0200 Organization: Comp.Center (RUS), U of Stuttgart, FRG Message-ID: <20071015161229.3f439230@cube.tz.axivion.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: infosun2.rus.uni-stuttgart.de 1192457558 30177 129.69.226.23 (15 Oct 2007 14:12:38 GMT) X-Complaints-To: news@news.uni-stuttgart.de NNTP-Posting-Date: Mon, 15 Oct 2007 14:12:38 +0000 (UTC) X-Newsreader: Sylpheed-Claws 2.6.0 (GTK+ 2.8.20; i486-pc-linux-gnu) X-URL: http://www.axivion.com/ Xref: g2news2.google.com comp.lang.ada:2450 Date: 2007-10-15T16:12:29+02:00 List-Id: 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