'Author: Dave Navarro, Don Dickinson 'Source: http://www.powerbasic.com/support/forums/Forum5/HTML/000675.html ' ' Convert a file to MIME text (Base64 Encoded) for PB/DLL or PB/CC ' by Dave Navarro (dave@powerbasic.com) ' ' Modified by Don Dickinson (ddickinson@basicguru.com) to add more ' error checking, a definable multi-part boundary, and allowing ' for a destination file name that's different from the source name. ' #If Not %Def(%PB_MIME_BAS) %PB_MIME_BAS = 1 Function pbFileToMIME(InFile As Asciiz, OutFile As Asciiz, zOutFileName As Asciiz, zBoundary As Asciiz) As Long Register i As Long Dim Enc As String * 64 Dim b As Asciiz * 4 Dim InBuff As String Dim OutBuff As String Dim InFileName As String Dim iIn As Long Dim iOut As Long Dim iNameField As Long Dim iCount As Long Enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" '- Open the files On Error Resume Next iIn = FreeFile Open InFile For Binary As #iIn If Err Then Function = %False Exit Function End If iOut = FreeFile Open OutFile For Output As #iOut If Err Then Close #iIn Function = %False Exit Function End If '- What is the name of our file. If Trim$(zOutFileName) = "" Then iNameField = ParseCount(inFile, Any ":\") InFileName = Parse$(inFile, Any ":\", iNameField) Else InFileName = Trim$(zOutFileName) End If Print #iOut, "Content-Type: application/octet-stream; name=" + Chr$(34) + InFileName + Chr$(34) Print #iOut, "Content-Transfer-Encoding: base64" Print #iOut, "Content-Disposition: attachment; filename=" + Chr$(34) + InFileName + Chr$(34) Print #iOut, "" While Not Eof(iIn) iCount = iCount + 1 If iCount Mod 20 = 0 Then Dialog DoEvents If gAbort Then GoTo Abort_MimeEncode End If End If Get$ iIn, 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) If Err Then Exit Do Wend Print #iOut, OutBuff Wend Print #iOut, "" Print #iOut, zBoundary Function = %True Abort_MimeEncode: Close #iIn Close #iOut End Function #EndIf