Search the Community
Showing results for tags 'base64encode'.
-
UDF: #include-once ; =============================================================================================================================== ; Title .........: LZNT_Base64 UDF ; Author.........: Dao Van Trong - TRONG.PRO ; Version........: 3.01 ; Description....: Provides functions for LZNT compression/decompression and Base64 encoding/decoding ; by calling Windows API functions from ntdll.dll and Crypt32.dll. ; Notes: ; - LZNT Engine options: 2 (MSZIP) or 258 (LZNT1). Default is 258. ; - Encoding options (for String functions): 1=ANSI, 2=UTF16LE, 3=UTF16BE, 4=UTF8. Default is 4 (UTF8). ; =============================================================================================================================== ; ============================================================================= ; LZNT Compression/Decompression Functions ; ============================================================================= ; =============================================================================================================================== ; Public Function: _LZNT_Compress ; =============================================================================================================================== ; Description: Compresses binary data using the LZNT algorithm (RtlCompressBuffer). ; Parameters: ; $bInput - The binary data to compress. ; $iOrigSize - An internal factor for pre-allocating the buffer (default 16). ; $iEngine - The compression engine type (2 for MSZIP, 258 for LZNT1 - Default: 258). ; $bBase64 - Flag to return the compressed data as a Base64 encoded string (Default: False). ; $iLineLen - If $bBase64 is True, specifies the line length for Base64 output (Default: 1024). ; Return Value: Returns the compressed binary data or Base64 string on success. ; On Failure: Returns an empty string (""), @error is set to: ; -1 = Invalid input (Binary length < 1). ; 1 = Failed to get workspace size (DllCall RtlGetCompressionWorkSpaceSize error). ; 2 = Compression failed (DllCall RtlCompressBuffer error). ; @extended is set to 0 (or original size if successful). ; Author: Dao Van Trong - TRONG.PRO ; =============================================================================================================================== Func _LZNT_Compress($bInput, $iOrigSize = 16, $iEngine = 258, $bBase64 = False, $iLineLen = 1024) $bInput = Binary($bInput) If (BinaryLen($bInput) < 1) Then Return SetError(-1, 0, "") If $iEngine <> 2 And $iEngine <> 258 Then $iEngine = 258 Local $tInput = DllStructCreate("byte[" & BinaryLen($bInput) & "]") DllStructSetData($tInput, 1, $bInput) Local $aCall = DllCall("ntdll.dll", "int", "RtlGetCompressionWorkSpaceSize", _ "ushort", $iEngine, "dword*", 0, "dword*", 0) If @error Or $aCall[0] Then Return SetError(1, 0, "") Local $tWork = DllStructCreate("byte[" & $aCall[2] & "]") If ($iOrigSize < 1) Then $iOrigSize = 16 Local $iLen = $iOrigSize * DllStructGetSize($tInput) Local $tBuffer = DllStructCreate("byte[" & $iLen & "]") $aCall = DllCall("ntdll.dll", "int", "RtlCompressBuffer", _ "ushort", $iEngine, _ "ptr", DllStructGetPtr($tInput), "dword", DllStructGetSize($tInput), _ "ptr", DllStructGetPtr($tBuffer), "dword", DllStructGetSize($tBuffer), _ "dword", 4096, "dword*", 0, "ptr", DllStructGetPtr($tWork)) If @error Or $aCall[0] Then Return SetError(2, 0, "") Local $tOut = DllStructCreate("byte[" & $aCall[7] & "]", DllStructGetPtr($tBuffer)) Local $bOut = DllStructGetData($tOut, 1) If $bBase64 Then Return SetError(0, DllStructGetSize($tInput), _Base64Encode($bOut, $iLineLen)) EndIf Return SetError(0, DllStructGetSize($tInput), $bOut) EndFunc ;==>_LZNT_Compress ; =============================================================================================================================== ; Public Function: _LZNT_Decompress ; =============================================================================================================================== ; Description: Decompresses LZNT compressed binary data (RtlDecompressBuffer). ; Parameters: ; $vInput - The compressed binary data (or Base64 string if $bBase64 is True). ; $iOrigSize - An internal factor for pre-allocating the output buffer (default 16). ; $iEngine - The compression engine type used for compression (Default: 258). ; $bBase64 - Flag indicating if $vInput is a Base64 encoded string (Default: False). ; Return Value: Returns the decompressed binary data on success. ; On Failure: Returns an empty binary (""), @error is set to: ; -1 = Invalid input (Binary length < 1). ; 1 = Decompression failed (DllCall RtlDecompressBuffer error). ; @extended is set to 0 (or original size if successful). ; Author: Dao Van Trong - TRONG.PRO ; =============================================================================================================================== Func _LZNT_Decompress($vInput, $iOrigSize = 16, $iEngine = 258, $bBase64 = False) Local $bInput If $bBase64 Then $bInput = _Base64Decode($vInput) Else $bInput = $vInput EndIf $bInput = Binary($bInput) If (BinaryLen($bInput) < 1) Then Return SetError(-1, 0, "") If $iEngine <> 2 And $iEngine <> 258 Then $iEngine = 258 Local $tInput = DllStructCreate("byte[" & BinaryLen($bInput) & "]") DllStructSetData($tInput, 1, $bInput) If ($iOrigSize < 1) Then $iOrigSize = 16 Local $iLen = $iOrigSize * DllStructGetSize($tInput) Local $tBuffer = DllStructCreate("byte[" & $iLen & "]") Local $aCall = DllCall("ntdll.dll", "int", "RtlDecompressBuffer", _ "ushort", $iEngine, _ "ptr", DllStructGetPtr($tBuffer), "dword", DllStructGetSize($tBuffer), _ "ptr", DllStructGetPtr($tInput), "dword", DllStructGetSize($tInput), _ "dword*", 0) If @error Or $aCall[0] Then Return SetError(1, 0, "") Local $tOut = DllStructCreate("byte[" & $aCall[6] & "]", DllStructGetPtr($tBuffer)) Return SetError(0, $aCall[6], DllStructGetData($tOut, 1)) EndFunc ;==>_LZNT_Decompress ; =============================================================================================================================== ; Public Function: _LZNT_CompressStr ; =============================================================================================================================== ; Description: Converts a string to binary, compresses it, and returns the result (Binary or Base64). ; Parameters: ; $sInput - The string data to compress. ; $iOrigSize - An internal factor for pre-allocating the buffer (default 16). ; $iEncoding - The encoding to convert the string to binary. (Default: 4 - UTF8). ; $iEngine - The compression engine type (Default: 258 - LZNT1). ; $bBase64 - Flag to return the compressed data as a Base64 encoded string (Default: False). ; $iLineLen - If $bBase64 is True, specifies the line length for Base64 output (Default: 1024). ; Return Value: Returns the compressed data (Binary or Base64 string). ; On Failure: Returns an empty string (""), @error is set by _LZNT_Compress. ; Author: Dao Van Trong - TRONG.PRO ; =============================================================================================================================== Func _LZNT_CompressStr($sInput, $iOrigSize = 16, $iEncoding = 4, $iEngine = 258, $bBase64 = False, $iLineLen = 1024) Local $bData = StringToBinary($sInput, $iEncoding) Local $sReturn = _LZNT_Compress($bData, $iOrigSize, $iEngine, $bBase64, $iLineLen) Return SetError(@error, @extended, $sReturn) EndFunc ;==>_LZNT_CompressStr ; =============================================================================================================================== ; Public Function: _LZNT_DecompressStr ; =============================================================================================================================== ; Description: Decompresses LZNT data and converts the resulting binary data to a string. ; Parameters: ; $vInput - The compressed data (Binary or Base64 string). ; $iOrigSize - An internal factor for pre-allocating the output buffer (default 16). ; $iEncoding - The encoding to convert the resulting binary data to a string. (Default: 4 - UTF8). ; $iEngine - The compression engine type used for compression (Default: 258 - LZNT1). ; $bBase64 - Flag indicating if $vInput is a Base64 encoded string (Default: False). ; Return Value: Returns the decompressed string on success. ; On Failure: Returns an empty string (""), @error is set by _LZNT_Decompress (usually 1). ; Author: Dao Van Trong - TRONG.PRO ; =============================================================================================================================== Func _LZNT_DecompressStr($vInput, $iOrigSize = 16, $iEncoding = 4, $iEngine = 258, $bBase64 = False) Local $bData = _LZNT_Decompress($vInput, $iOrigSize, $iEngine, $bBase64) If @error Then Return SetError(1, 0, "") Return BinaryToString($bData, $iEncoding) EndFunc ;==>_LZNT_DecompressStr ; ============================================================================= ; Base64 Encoding/Decoding Functions ; ============================================================================= ; =============================================================================================================================== ; Public Function: _Base64Encode ; =============================================================================================================================== ; Description: Encodes binary data to a Base64 string using CryptBinaryToStringW. ; Parameters: ; $bData - The binary data to encode. ; $iLineLen - Line length for wrapping the Base64 output (0 for no wrapping, default 0). ; Return Value: Returns the Base64 encoded string on success. ; On Failure: Returns an empty string (""), @error is set to: ; 1 = Failed to get required buffer size. ; 2 = Encoding failed. ; @extended is set to 0. ; Author: Dao Van Trong - TRONG.PRO ; =============================================================================================================================== Func _Base64Encode($bData, $iLineLen = 0) If ($iLineLen < 1) Then $iLineLen = 0 $bData = Binary($bData) Local $tInput = DllStructCreate("byte[" & BinaryLen($bData) & "]") DllStructSetData($tInput, 1, $bData) Local $iFlags = 0x40000001 ; CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF ; 1. Get required buffer size Local $aResult = DllCall("Crypt32.dll", "bool", "CryptBinaryToStringW", _ "struct*", $tInput, _ "dword", DllStructGetSize($tInput), _ "dword", $iFlags, _ "ptr", 0, _ "dword*", 0) If @error Or Not $aResult[0] Then Return SetError(1, 0, "") Local $iSize = $aResult[5] Local $tOutput = DllStructCreate("wchar[" & $iSize & "]") ; 2. Perform encoding $aResult = DllCall("Crypt32.dll", "bool", "CryptBinaryToStringW", _ "struct*", $tInput, _ "dword", DllStructGetSize($tInput), _ "dword", $iFlags, _ "struct*", $tOutput, _ "dword*", $iSize) If @error Or Not $aResult[0] Then Return SetError(2, 0, "") Local $sOut = DllStructGetData($tOutput, 1) ; 3. Manual wrap if requested If ($iLineLen > 0) And (StringLen($sOut) > $iLineLen) Then Local $sWrapped = "" For $i = 1 To StringLen($sOut) Step $iLineLen $sWrapped &= StringMid($sOut, $i, $iLineLen) & @CRLF Next $sOut = StringTrimRight($sWrapped, 2) ; Remove trailing @CRLF EndIf Return $sOut EndFunc ;==>_Base64Encode ; =============================================================================================================================== ; Public Function: _Base64EncodeStr ; =============================================================================================================================== ; Description: Encodes a string to a Base64 string. ; Parameters: ; $sInput - The string data to encode. ; $iLineLen - Line length for wrapping the Base64 output (0 for no wrapping, default 0). ; $iEncodeType - The encoding to convert the string to binary before encoding (Default: 4 - UTF8). ; Return Value: Returns the Base64 encoded string on success. ; On Failure: Returns an empty string (""), @error is set by _Base64Encode. ; Author: Dao Van Trong - TRONG.PRO ; =============================================================================================================================== Func _Base64EncodeStr($sInput, $iLineLen = 0, $iEncodeType = 4) If ($iLineLen < 1) Then $iLineLen = 0 If ($iEncodeType > 4) Or ($iEncodeType < 1) Then $iEncodeType = 4 Local $bBinary = StringToBinary($sInput, $iEncodeType) Local $sReturn = _Base64Encode($bBinary, $iLineLen) Return SetError(@error, @extended, $sReturn) EndFunc ;==>_Base64EncodeStr ; =============================================================================================================================== ; Public Function: _Base64Decode ; =============================================================================================================================== ; Description: Decodes a Base64 string to binary data using CryptStringToBinaryW. ; Parameters: ; $sInput - The Base64 encoded string. Whitespace is removed before decoding. ; $bReturnType - Return type: 0 for Binary (Default), 1-4 for String with specified encoding. ; Return Value: Returns the decoded data (Binary or String) on success. ; On Failure: Returns an empty binary (Binary("")) or empty string (""), @error is set to: ; 1 = Invalid input (Empty string after whitespace removal). ; 2 = Failed to get required buffer size. ; 3 = Decoding failed. ; @extended is set to 0. ; Author: Dao Van Trong - TRONG.PRO ; =============================================================================================================================== Func _Base64Decode($sInput, $bReturnType = 0) If ($bReturnType > 4) Or ($bReturnType < 1) Then $bReturnType = 0 ; Remove all whitespace and CRLF $sInput = StringRegExpReplace($sInput, "\s", "") If $sInput = "" Then Return SetError(1, 0, Binary("")) Local $iFlags = 0x1 ; CRYPT_STRING_BASE64 ; 1. Get required buffer size Local $aResult = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryW", _ "wstr", $sInput, _ "dword", StringLen($sInput), _ "dword", $iFlags, _ "ptr", 0, _ "dword*", 0, _ "ptr", 0, _ "ptr", 0) If @error Or Not $aResult[0] Then Return SetError(2, 0, Binary("")) Local $iSize = $aResult[5] Local $tOutput = DllStructCreate("byte[" & $iSize & "]") ; 2. Perform decoding $aResult = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryW", _ "wstr", $sInput, _ "dword", StringLen($sInput), _ "dword", $iFlags, _ "struct*", $tOutput, _ "dword*", $iSize, _ "ptr", 0, _ "ptr", 0) If @error Or Not $aResult[0] Then Return SetError(3, 0, Binary("")) Local $bBinary = DllStructGetData($tOutput, 1) If ($bReturnType = 0) Then Return $bBinary Else ; Convert binary to string with specified encoding Return BinaryToString($bBinary, $bReturnType) EndIf EndFunc ;==>_Base64Decode ; =============================================================================================================================== ; Public Function: _Base64DecodeStr ; =============================================================================================================================== ; Description: Decodes a Base64 string and converts the result to a string. ; Parameters: ; $sInput - The Base64 encoded string. ; $iDecodeType - The encoding to convert the resulting binary data to a string (Default: 4 - UTF8). ; Return Value: Returns the decoded string on success. ; On Failure: Returns an empty string (""), @error is set by _Base64Decode. ; Author: Dao Van Trong - TRONG.PRO ; =============================================================================================================================== Func _Base64DecodeStr($sInput, $iDecodeType = 4) Local $sReturn = _Base64Decode($sInput, $iDecodeType) Return SetError(@error, @extended, $sReturn) EndFunc ;==>_Base64DecodeStr .
- 1 reply
-
- _lznt_compress
- _lznt_decompress
- (and 3 more)