Base64 (again) 
Author Message
 Base64 (again)

I recently needed Base64 encode/decode routines for an embedded email
application.  I found source for quite a few variations but they all looked
like a bad translation of C code or something; lacking much insight  into
the "Basic" problem.

Or maybe I just suffer from the "not invented here" syndrone.

In any case, you be the judge.  Here is my PB interpretation of what "Basic"
Base64 encode/decode rouitnes should look like.  IMO, these are more
compact, cleaner and probably faster (not tested) than most others I found.
They've also been battle tested with hundreds of emails sent and received.

$B64_Alpha =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

FUNCTION StringToMime(InBuff AS STRING) AS STRING

   '--- Base64 Encode

   LOCAL OutBuff AS STRING, T AS STRING
   LOCAL I AS LONG, J AS LONG, K AS LONG
   LOCAL tt AS LONG     '4 byte work buffer

   T = SPACE$(4)                    'octet work string
   J=0                              'input char. pointer
   DO WHILE J<LEN(InBuff)
      tt=0                          'clear work buffer
      K=1                           'output counter
      For I=1 to 3                  'load up 3 ASCII char.
         Shift Left tt,8            'make room for it in buffer
         if J<LEN(InBuff) then      'any remaining?
            Incr J                  'next char.
            Incr K                  'count the required output
            tt = tt OR ASC(InBuff,J)'add into buffer
         end if
      NEXT
      Rotate Left tt,14          'roll around to start of buffer
      MID$(T,3)="=="             'pre-pad the octet string in case K<4
      For I=1 to K               'build the octet
         ASC(T,I) = ASC($B64_Alpha, (tt AND 63&)+1&) 'convert 6 bits to B64
alphabet
         Rotate Left tt,6        'do next one
      next
      OutBuff=OutBuff+T          'build output one octet at a time
   LOOP
   FUNCTION = OutBuff            'return the encoded string

END FUNCTION

FUNCTION MimeToString(InBuff AS STRING) AS STRING

   '--- Base64 decode

   LOCAL t as long, tt as long, I AS LONG, J AS LONG, K AS LONG
   LOCAL OutBuff AS STRING

   I=1
   DO WHILE I<LEN(InBuff)
      K=3                   'output counter
      tt=0                  'clear work buffer
      '--- decode octet
      for J=1 to 4          'load 4 Mime characters
         t=ASC(InBuff,I)    'convert B64 alphabet to bit pattern
         if t>96 then
            if t<123 then t=t-71 else t=0
         elseif t>64 then
            if t<91 then t=t-65 else t=0
         elseif t>47 then
            if t<58 then t=t+4 else t=0
         elseif t=43 Then
            t=62
         end if
         if t=0 then
            Decr K          'non-alphabet or padding, reduce output count
            t=63
         end if
         Shift Left tt,6    'make room for new bits
         tt=tt OR t         'add bit pattern to buffer
         Incr I             'do next one
      next
      Rotate Left tt,16     'roll around to start
      '--- output up to 3, 8-bit characters
      DO UNTIL K=0
         OutBuff=OutBuff+CHR$(LOBYT(tt))
         Rotate Left tt,8
         Decr K
      LOOP
   LOOP

   FUNCTION = OutBuff       'return the decoded string

END FUNCTION



Thu, 04 Aug 2005 01:42:13 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Scheme vs ML again and again and again and again

2. Scheme vs ML again and again and again and again

3. APL and J (again and again) (long)

4. APL and J (again and again)

5. 'SQL Server Login': Again, again, ...

6. static vs. dynamic typing (again, again)

7. Scheme vs ML again and again

8. All in EXE file AGAIN AGAIN

9. trees again and again

10. Re-inventing hot water again and again

11. tcltest processes subdirectory again and again - Why?

12. Newsgroup access again, good to see you all again

 

 
Powered by phpBB® Forum Software