NAME: MIME Base64
CREATOR: The MIME group
PB AUTHOR: Dave Navarro
DESCRIPTION: Base64 is related to UUencoding in that it uses the same mechanism of distributing the bits in 3 bytes into 4 bytes, but it uses a different table to map the resulting data into printable characters.
NOTES: Base64 encoded data takes one-third more space than the data before the conversion.
VARIATIONS: Ed Turner Don Dickinson
SOURCE: http://www.powerbasic.com/support/forums/Forum7/HTML/000274.html
Viewing source from mimebase64.bas   1718 bytes   Last modified Wed, 20 September 2006

'----------------------------------------------------------------------------
' Convert a file to MIME text (Base64 Encoded) for PB/DLL or PB/CC
' by Dave Navarro (dave@powerbasic.com)
'
FUNCTION FileToMIME(InFile AS ASCIIZ, OutFile AS ASCIIZ) AS LONG
  LOCAL Enc     AS STRING * 64
  LOCAL b       AS ASCIIZ * 4
  LOCAL InBuff  AS STRING
  LOCAL OutBuff AS STRING
  LOCAL i       AS LONG

  Enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

  OPEN InFile FOR BINARY AS #1
  OPEN OutFile FOR OUTPUT AS #2

    PRINT# 2, "MIME-Version: 1.0"
    PRINT# 2, "Content-Type: application/octet-stream; name=" + InFile
    PRINT# 2, "Content-transfer-encoding: base64"
    PRINT# 2, ""

    WHILE NOT EOF(1)
      GET$ 1, 57, InBuff
      OutBuff = ""
      WHILE LEN(InBuff)
        b = LEFT$(InBuff, 3)
        ! mov AL, b[0]
        ! shr AL, 2
        ! movzx i, AL
        OutBuff = OutBuff + MID$(Enc, i+1, 1)
        ! mov AL, b[1]
        ! mov AH, b[0]
        ! shr AX, 4
        ! and AL, &H3F
        ! movzx i, AL
        OutBuff = OutBuff + MID$(Enc, i+1, 1)
        IF LEN(InBuff) = 1 THEN
          OutBuff = OutBuff + "=="
          EXIT DO
        END IF
        ! mov AL, b[2]
        ! mov AH, b[1]
        ! shr AX, 6
        ! and AL, &H3F
        ! movzx i, AL
        OutBuff = OutBuff + MID$(Enc, i+1, 1)
        IF LEN(InBuff) = 2 THEN
          OutBuff = OutBuff + "="
          EXIT DO
        END IF
        ! mov AL, b[2]
        ! and AL, &H3F
        ! movzx i, AL
        OutBuff = OutBuff + MID$(Enc, i+1, 1)
        InBuff = MID$(InBuff, 4)
      WEND
      PRINT#2, OutBuff
    WEND
  CLOSE

END FUNCTION

Back to The Archives