udfFileToBase64String
udfFileFromBase64String
;------------------------------------------------------------------------------------------------------------------------------------------
; strBase64 = udfFileToBase64String (strFileName)
; intFileSize = udfFileFromBase64String (strFileName, strBase64)
;
; Detlev Dalitz.20111203.
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileToBase64String (strFileName)
intFileSize = FileSize (strFileName, 1)
If 0 == intFileSize Then Return ""

hdlBBFile = BinaryAlloc (intFileSize)
If intFileSize != BinaryRead (hdlBBFile, strFileName)
   hdlBBFile = BinaryFree (hdlBBFile)
   Return ""
EndIf
intBBFileAddr = BinaryBufInfo (hdlBBFile, 1)

CRYPT_STRING_BASE64 = 1
strCryptDll = DirWindows (1) : "Crypt32.dll"

; Get size of Base64 string.
hdlBBSize = BinaryAlloc (4)
blnResult = DllCall (strCryptDll, long : "CryptBinaryToStringA", long : intBBFileAddr, long : intFileSize, long : CRYPT_STRING_BASE64, lpnull, lpbinary : hdlBBSize)
If !blnResult
   hdlBBSize = BinaryFree (hdlBBSize)
   hdlBBFile = BinaryFree (hdlBBFile)
   Return ""
EndIf
intBBStringSize = BinaryPeek4 (hdlBBSize, 0)

; Get Base64 string.
hdlBBString = BinaryAlloc (intBBStringSize)
blnResult = DllCall (strCryptDll, long : "CryptBinaryToStringA", long : intBBFileAddr, long : intFileSize, long : CRYPT_STRING_BASE64, lpbinary : hdlBBString, lpbinary : hdlBBSize)
If !blnResult
   hdlBBString = BinaryFree (hdlBBString)
   hdlBBSize = BinaryFree (hdlBBSize)
   hdlBBFile = BinaryFree (hdlBBFile)
   Return ""
EndIf
intStringSize = BinaryPeek4 (hdlBBSize, 0)

BinaryEodSet (hdlBBString, intStringSize)
strBase64 = BinaryPeekStr (hdlBBString, 0, intStringSize)

hdlBBString = BinaryFree (hdlBBString)
hdlBBSize = BinaryFree (hdlBBSize)
hdlBBFile = BinaryFree (hdlBBFile)

Return strBase64 ; Return string.
;..........................................................................................................................................
; This UDF "udfFileToBase64String" converts the contents from any file to a string using the Base 64 encrypting algorithm.
;
; (c)Detlev Dalitz.20111203.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileFromBase64String (strFileName, strBase64)
CRYPT_STRING_BASE64 = 1
strCryptDll = DirWindows (1) : "Crypt32.dll"

; Get size of output buffer.
hdlBBFileSize = BinaryAlloc (4)
blnResult = DllCall (strCryptDll, long : "CryptStringToBinaryA", lpstr : strBase64, long : 0, long : CRYPT_STRING_BASE64, lpnull, lpbinary : hdlBBFileSize, lpnull, lpnull)
If !blnResult
   hdlBBFileSize = BinaryFree (hdlBBFileSize)
   Return ""
EndIf
intBBFileSize = BinaryPeek4 (hdlBBFileSize, 0)

; Convert the Base64 string to binary data.
hdlBBFile = BinaryAlloc (intBBFileSize)
blnResult = DllCall (strCryptDll, long : "CryptStringToBinaryA", lpstr : strBase64, long : 0, long : CRYPT_STRING_BASE64, lpbinary : hdlBBFile, lpbinary : hdlBBFileSize, lpnull, lpnull)
If !blnResult
   hdlBBFile = BinaryFree (hdlBBFile)
   hdlBBFileSize = BinaryFree (hdlBBFileSize)
   Return ""
EndIf
hdlBBFileSize = BinaryFree (hdlBBFileSize)

BinaryEodSet (hdlBBFile, intBBFileSize)
intFileSize = BinaryWrite (hdlBBFile, strFileName)
hdlBBFile = BinaryFree (hdlBBFile)

; Return (intFileSize == intBBFileSize) && (FileExist (strFileName) != 0) ; Return boolean value.
Return intFileSize ; Return FileSize.
;..........................................................................................................................................
; This UDF "udfFileFromBase64String" converts a Base64 encoded string to a file using the Base 64 decrypting algorithm.
;
; (c)Detlev Dalitz.20111203.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

DirChange (DirScript ())
strFileThis = IntControl (1004, 0, 0, 0, 0)
strFileOutTxt = ItemReplace ("out.txt", -1, strFileThis, ".") ; Step 2.
strFileOutBmp = ItemReplace ("out.bmp", -1, strFileThis, ".") ; Step 3.
strFileBmp1 = "Image.1.bmp" ; Step 4.
strFileBmp2 = "Image.2.bmp" ; Step 5.


; Get existing image file from WinBatch system folder.
Pause ("Demo Crypt/Decrypt Base64|Step 1", "Display image file as is.")
strFileName = DirHome () : "WBOwl.bmp"
ShellExecute (strFileName, "", "", @NORMAL, "")


; Crypt image file to Base64 string.
Pause ("Demo Crypt/Decrypt Base64|Step 2", "Crypt image file to Base64 string.")
strBase64 = udfFileToBase64String (strFileName)
FilePut (strFileOutTxt, strBase64)
ShellExecute (strFileOutTxt, "", "", @NORMAL, "")


; Decrypt Base64 string to image file.
Pause ("Demo Crypt/Decrypt Base64|Step 3", "Decrypt Base64 string to image file.")
intFileSize = udfFileFromBase64String (strFileOutBmp, strBase64)
ShellExecute (strFileOutBmp, "", "", @NORMAL, "")
Drop (strBase64, strFileOutBmp)


; Get Base64 string from text file and decrypt string to image file.
Pause ("Demo Crypt/Decrypt Base64|Step 4", "Get Base64 string from text file.")
strBase64 = FileGet (strFileOutTxt)
intFileSize = udfFileFromBase64String (strFileBmp1, strBase64)
ShellExecute (strFileBmp1, "", "", @NORMAL, "")


; Get Base64 string from a variable within a WinBatch call file and decrypt string to image file.
Pause ("Demo Crypt/Decrypt Base64|Step 5", "Write out the Base64 string as a WB variable into a Winbatch call file" : @LF : "and decrypt string to image file.")
;   ; Remove trailing blank lines.
;   intLen = StrLen (strBase64) - 1
;   While intLen == StrIndex (strBase64, @CRLF, intLen, @FWDSCAN)
;      intLen = intLen - 2
;   EndWhile
;   strBase64 = StrSub (strBase64, 1, intLen + 1)

; Assign Base64 string to variable and store it as code script into a WinBatch wbt file.
strText = `strImgB64 = "` : strBase64 : `"`
strText = StrReplace (strText, @CR, `"` : @CR)
strText = StrReplace (strText, @LF, @LF : `strImgB64 = strImgB64 : "`)
FilePut ("LoadImgB64.wbt", strText)
Drop (strText)

Call ("LoadImgB64.wbt", "") ; The called script publishes the variable strImgB64.

intFileSize = udfFileFromBase64String (strFileBmp2, strImgB64) ; Use variable from called file.
ShellExecute (strFileBmp2, "", "", @NORMAL, "")

:CANCEL
Exit