Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Binary file to Base64 encoded MIME

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binary file to Base64 encoded MIME

    Code:
    '----------------------------------------------------------------------------
    ' Convert a file to MIME text (Base64 Encoded) for PB/DLL or PB/CC
    ' by Dave Navarro ([email protected])
    '
    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
    Home of the BASIC Gurus
    www.basicguru.com

  • #2
    I wrote the following functions a year or
    so ago to encode and decode mime64. They are
    included in a Terminal emulator and have
    worked fine. Don't know how they will compare
    to yours Dave... probably not as good but at
    least everyone can get the idea of what is
    involved in decoding as well as encoding.

    Code:
    FUNCTION mimeEncode(BYVAL strng AS STRING) AS STRING
    	DIM temp?, first?, second?, third?, fourth?
    	DIM Firstletter$, Secondletter$, Thirdletter$, rtn$
    	rtn = nll
    	n& = Len(strng)
    	For i& = 1 to n& step 3
    		Firstletter = Mid$(strng, i&, 1)
    		If i& = n&  Then
    			Secondletter = Chr$(0)
    			Thirdletter = Chr$(0)
    		else
    			Secondletter = Mid$(strng, i& + 1, 1)
    			If i& + 1 = n& Then
    				Thirdletter = Chr$(0)
    			Else
    				Thirdletter = Mid$(strng, i& + 2, 1)
    			End If
    		End if
    		first = ASC(Firstletter)
    		SHIFT RIGHT first, 2
    		second = ASC(Firstletter)
    		SHIFT LEFT second, 4
    		second = second AND	&H30
    		temp = ASC(Secondletter)
    		SHIFT RIGHT temp, 4
    		second = second OR temp
    		If i& = n& Then
    			rtn = rtn & Nvt$(first) & Nvt$(second) & "=="
    		Else
    			third = ASC(Secondletter)
    			SHIFT LEFT third, 2
    			third = third AND &H3C
    			temp = ASC(Thirdletter)
    			SHIFT RIGHT temp, 6
    			third = third OR temp
    			If i& + 1 = n& Then
    				rtn = rtn & Nvt$(first) & Nvt$(second) & Nvt$(third) & "="
    			Else
    				fourth = ASC(Thirdletter)
    				fourth = fourth AND	&H3F
    				rtn = rtn & Nvt$(first) & Nvt$(second) & Nvt$(third) & Nvt$(fourth)
    			End if
    		End If
    	Next i&
    	FUNCTION = rtn
    END FUNCTION
    
    
    FUNCTION ConvertToNvt(t AS Byte) As Byte
    	IF t > 64 and t < 91 Then
    		Function = t - 65
    	Elseif t > 96 and t < 123 Then
    		Function = t - 71
    	Elseif t > 47 and t < 58 Then
    		Function = t + 4
    	Elseif t = 43 Then
    		Function = 62
    	Else
    		Function = 63
    	End If
    END FUNCTION
    
    
    FUNCTION mimeDecode(BYVAL strng AS STRING) AS STRING
    	DIM temp?, first?, second?, third?
    	DIM Firstletter$, Secondletter$, Thirdletter$, Fourthletter$, rtn$
    	rtn = nll
    	n& = Len(strng)
    	For i& = 1 to n& step 4
    		Firstletter = Mid$(strng, i&, 1)
    		Secondletter = Mid$(strng, i& + 1, 1)
    		Thirdletter = Mid$(strng, i& + 2, 1)
    		Fourthletter = Mid$(strng, i& + 3, 1)
    		first = ConvertToNvt(ASC(Firstletter))
    		SHIFT LEFT first, 2
    		temp = ConvertToNvt(ASC(Secondletter))
    		SHIFT RIGHT temp, 4
    		first = first OR temp
    		If Thirdletter = "=" Then
    			rtn = rtn & Chr$(first)
    		Else
    			second = ConvertToNvt(ASC(Secondletter))
    			SHIFT LEFT second, 4
    			temp = ConvertToNvt(ASC(Thirdletter))
    			SHIFT RIGHT temp, 2
    			second = second OR temp
    			If Fourthletter = "=" Then
    				rtn = rtn & Chr$(first) & Chr$(second)
    			Else
    				third = ConvertToNvt(ASC(Thirdletter))
    				SHIFT LEFT third, 6
    				temp = ConvertToNvt(ASC(Fourthletter))
    				third = third OR temp
    				rtn = rtn & Chr$(first) & Chr$(second) & Chr$(third)
    			End If
    		End If
    	Next i&
    	FUNCTION = rtn
    END FUNCTION
    
    
    SUB SetNvt()
    	S$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    	For i& = 1 to 64
    		Nvt(i& - 1) = Mid$(S$, i&, 1)
    	Next i&
    END SUB
    By the way, you will need to set Nvt$ as
    Global or else just include it in the
    MimeEncode Function.
    Cheers.


    [This message has been edited by Ed Turner (edited 07-11-99).]

    Comment


    • #3
      Hi, I'm writing a small mail-application in PC/CC v2.00.

      Everything works fine (Host, From, To, CC, Subject) but I can't combine a message with an attachment.

      Separated a message works and separated a file-attached to the mail works to, but I can't let the combination work.

      Anyone an idea how to do this ?

      Thanks,

      Guy

      ------------------
      Regards,
      Guy

      Comment


      • #4
        Search the BBS for "attachment" and you should find some clues and possibly you may find some code too.

        Also, please ask your questions to the appropriate forum - the Source Code forum is not open for general discussions.

        Thanks!


        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Here is an inefficient implementation of a base64 encoder/decoder.
          I wanted to write my own...

          Code:
          FUNCTION Base64Encode(InputStream AS STRING) AS STRING
          
          LOCAL Base64Alphabet AS STRING
           Base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
          
          LOCAL OutputStream AS STRING
           LOCAL ByteDecVal AS INTEGER
           LOCAL BitBinVal AS INTEGER
           LOCAL ByteGroup AS STRING
           LOCAL ByteGroupBinStream AS STRING
           LOCAL Padding AS STRING
          
          LOCAL idx AS INTEGER
          
          DO WHILE InputStream <> ""
              ByteGroup = LEFT$(InputStream, 3)
              DO WHILE ByteGroup <> ""
                  FOR idx = 7 TO 0 STEP -1
                      ByteDecVal = ASC(LEFT$(ByteGroup, 1))
                      BitBinVal = BIT(ByteDecVal, idx)
                      ByteGroupBinStream = ByteGroupBinStream & TRIM$(STR$(BitBinVal))
                  NEXT idx
                  ByteGroup = REMAIN$(ByteGroup, LEFT$(ByteGroup, 1))
              LOOP
              IF LEN(ByteGroupBinStream) < 24 THEN
                  Padding = STRING$((((LEN(ByteGroupBinStream) / 8) -3) * -1), "=")
                  ByteGroupBinStream = ByteGroupBinStream & STRING$(INT(24 / LEN(ByteGroupBinStream)) + 1, "0")
              END IF
              DO WHILE ByteGroupBinStream <> ""
                  OutputStream = OutputStream & MID$(Base64Alphabet, VAL("&B00" & LEFT$(ByteGroupBinStream, 6)) + 1, 1)
                  ByteGroupBinStream = REMAIN$(ByteGroupBinStream, LEFT$(ByteGroupBinStream, 6))
              LOOP
              OutputStream = OutputStream & Padding
              ByteGroupBinStream = ""
              InputStream = REMAIN$(InputStream, LEFT$(InputStream, 3))
          LOOP
          FUNCTION = OutputStream
          END FUNCTION
          
          FUNCTION Base64Decode(InputStream AS STRING) AS STRING
          LOCAL Base64Alphabet AS STRING
           Base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
          
          LOCAL OutputStream AS STRING
           LOCAL ByteDecVal AS INTEGER
           LOCAL BitBinVal AS INTEGER
           LOCAL ByteGroup AS STRING
           LOCAL ByteGroupBinStream AS STRING
           LOCAL Padding AS STRING
          
          LOCAL idx AS INTEGER
          
          Base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
          InputStream = REMOVE$(InputStream, ANY REMOVE$(InputStream, ANY Base64Alphabet))
          DO WHILE InputStream <> ""
              ByteGroup = LEFT$(InputStream, 4)
              DO WHILE ByteGroup <> ""
                  FOR idx = 5 TO 0 STEP -1
                          ByteDecVal = INSTR(Base64Alphabet, LEFT$(ByteGroup, 1)) - 1
                          BitBinVal = BIT(ByteDecVal, idx)
                          ByteGroupBinStream = ByteGroupBinStream & TRIM$(STR$(BitBinVal))
                  NEXT idx
                  ByteGroup = REMAIN$(ByteGroup, LEFT$(ByteGroup, 1))
              LOOP
              DO WHILE ByteGroupBinStream <> ""
                  OutputStream = OutputStream & CHR$(VAL("&B" & LEFT$(ByteGroupBinStream, 8)))
                  ByteGroupBinStream = REMAIN$(ByteGroupBinStream, LEFT$(ByteGroupBinStream, 8))
              LOOP
              InputStream = REMAIN$(InputStream, LEFT$(InputStream, 4))
          LOOP
          FUNCTION = OutputStream
          
          END FUNCTION
          
          FUNCTION PBMAIN()
              STDOUT Base64Encode("ABC")
              STDOUT Base64Decode("QUJD")
          END FUNCTION

          ------------------

          Comment

          Working...
          X