Jump to content

Recommended Posts

Posted (edited)

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

.

Edited by Trong

Enjoy my work? Buy me a 🍻 or tip via ❤️ PayPal

Posted (edited)

EG:

#include "LZNT_Base64.au3"

Global $g_iTestTotal = 0
Global $g_iTestPassed = 0
Global $g_iTestFailed = 0

; Helper function to check test result
Func CheckTest($sTestName, $bCondition)
    $g_iTestTotal += 1
    If $bCondition Then
        $g_iTestPassed += 1
        ConsoleWrite('-'&$sTestName & ": PASS" & @CRLF)
        Return True
    Else
        $g_iTestFailed += 1
        ConsoleWrite('!'&$sTestName & ": FAIL" & @CRLF)
        Return False
    EndIf
EndFunc

; Test _Base64Encode
Func Test_Base64Encode()
    ConsoleWrite("+=== Test _Base64Encode ===" & @CRLF)

    Local $result = _Base64Encode("")
    CheckTest("Test 1 (Empty)", $result == "")

    Local $bData = StringToBinary("Hello World")
    $result = _Base64Encode($bData)
    CheckTest("Test 2 (Hello World)", $result == "SGVsbG8gV29ybGQ=")

    $bData = Binary("0x010203040506")
    $result = _Base64Encode($bData)
    CheckTest("Test 3 (Binary)", $result == "AQIDBAUG")

    $bData = StringToBinary("This is a longer test string")
    $result = _Base64Encode($bData, 10)
    CheckTest("Test 4 (Line wrap)", StringInStr($result, @CRLF) > 0)

    $result = _Base64Encode($bData, 0)
    CheckTest("Test 5 (No wrap)", StringInStr($result, @CRLF) == 0)

    ConsoleWrite(@CRLF)
EndFunc

; Test _Base64Decode
Func Test_Base64Decode()
    ConsoleWrite("+=== Test _Base64Decode ===" & @CRLF)

    Local $result = _Base64Decode("")
    CheckTest("Test 1 (Empty error)", @error == 1)

    $result = _Base64Decode("SGVsbG8gV29ybGQ=", 0)
    Local $expected = StringToBinary("Hello World")
    CheckTest("Test 2 (Binary)", $result == $expected)

    $result = _Base64Decode("SGVsbG8gV29ybGQ=", 4)
    CheckTest("Test 3 (UTF8)", $result == "Hello World")

    $result = _Base64Decode("SGVs" & @CRLF & "bG8g" & " " & "V29ybGQ=", 4)
    CheckTest("Test 4 (With whitespace)", $result == "Hello World")

    $result = _Base64Decode("AQIDBAUG", 0)
    $expected = Binary("0x010203040506")
    CheckTest("Test 5 (Binary data)", $result == $expected)

    $result = _Base64Decode("Invalid@#$", 0)
    CheckTest("Test 6 (Invalid error)", @error > 0)

    ConsoleWrite(@CRLF)
EndFunc

; Test _Base64EncodeStr
Func Test_Base64EncodeStr()
    ConsoleWrite("+=== Test _Base64EncodeStr ===" & @CRLF)

    Local $result = _Base64EncodeStr("Hello World", 0, 4)
    CheckTest("Test 1 (UTF8)", $result == "SGVsbG8gV29ybGQ=")

    $result = _Base64EncodeStr("", 0, 4)
    CheckTest("Test 2 (Empty)", $result == "")

    Local $encoded = _Base64EncodeStr("Xin chào", 0, 4)
    Local $decoded = _Base64DecodeStr($encoded, 4)
    CheckTest("Test 3 (Unicode roundtrip)", $decoded == "Xin chào")

    $result = _Base64EncodeStr("This is a test string for wrapping", 15, 4)
    CheckTest("Test 4 (Line wrap)", StringInStr($result, @CRLF) > 0)

    $encoded = _Base64EncodeStr("Test", 0, 1)
    $decoded = _Base64DecodeStr($encoded, 1)
    CheckTest("Test 5 (ANSI roundtrip)", $decoded == "Test")

    ConsoleWrite(@CRLF)
EndFunc

; Test _Base64DecodeStr
Func Test_Base64DecodeStr()
    ConsoleWrite("+=== Test _Base64DecodeStr ===" & @CRLF)

    Local $result = _Base64DecodeStr("SGVsbG8gV29ybGQ=", 4)
    CheckTest("Test 1 (UTF8)", $result == "Hello World")

    $result = _Base64DecodeStr("", 4)
    CheckTest("Test 2 (Empty error)", @error == 1)

    Local $encoded = _Base64EncodeStr("Xin chào", 0, 4)
    $result = _Base64DecodeStr($encoded, 4)
    CheckTest("Test 3 (Unicode)", $result == "Xin chào")

    $result = _Base64DecodeStr("SGVs" & @CRLF & "bG8g" & " " & "V29ybGQ=", 4)
    CheckTest("Test 4 (With whitespace)", $result == "Hello World")

    $encoded = _Base64EncodeStr("Test", 0, 1)
    $result = _Base64DecodeStr($encoded, 1)
    CheckTest("Test 5 (ANSI)", $result == "Test")

    $encoded = _Base64EncodeStr("!@#$%^&*()", 0, 4)
    $result = _Base64DecodeStr($encoded, 4)
    CheckTest("Test 6 (Special chars)", $result == "!@#$%^&*()")

    ConsoleWrite(@CRLF)
EndFunc

; Test _LZNT_Compress
Func Test_LZNT_Compress()
    ConsoleWrite("+=== Test _LZNT_Compress ===" & @CRLF)

    Local $bData = StringToBinary("Hello World")
    Local $result = _LZNT_Compress($bData, 2, 258, False)
    CheckTest("Test 1 (Simple compress)", IsBinary($result) And BinaryLen($result) > 0)

    Local $sText = "AAAAAAAAAA"
    $bData = StringToBinary($sText)
    $result = _LZNT_Compress($bData, 2, 258, False)
    CheckTest("Test 2 (Repetitive data)", BinaryLen($result) < BinaryLen($bData))

    $result = _LZNT_Compress($bData, 2, 258, True, 64)
    CheckTest("Test 3 (Base64 output)", IsString($result) And StringLen($result) > 0)

    $sText = ""
    For $i = 1 To 100
        $sText &= "Test string number " & $i & @CRLF
    Next
    $bData = StringToBinary($sText)
    $result = _LZNT_Compress($bData, 2, 258, False)
    CheckTest("Test 4 (Large data)", BinaryLen($result) < BinaryLen($bData))

    $result = _LZNT_Compress("", 2, 258, False)
    CheckTest("Test 5 (Empty data)", $result == "")

    ConsoleWrite(@CRLF)
EndFunc

; Test _LZNT_Decompress
Func Test_LZNT_Decompress()
    ConsoleWrite("+=== Test _LZNT_Decompress ===" & @CRLF)

    Local $bData = StringToBinary("Hello World")
    Local $compressed = _LZNT_Compress($bData, 2, 258, False)
    Local $result = _LZNT_Decompress($compressed, 2, 258, False)
    CheckTest("Test 1 (Simple roundtrip)", $result == $bData)

    Local $sText = "AAAAAAAAAA"
    $bData = StringToBinary($sText)
    $compressed = _LZNT_Compress($bData, 2, 258, False)
    $result = _LZNT_Decompress($compressed, 2, 258, False)
    CheckTest("Test 2 (Repetitive roundtrip)", $result == $bData)

    $compressed = _LZNT_Compress($bData, 2, 258, True)
    $result = _LZNT_Decompress($compressed, 2, 258, True)
    CheckTest("Test 3 (Base64 roundtrip)", $result == $bData)

    $sText = ""
    For $i = 1 To 1000
        $sText &= "Test string number " & $i & @CRLF
    Next
    $bData = StringToBinary($sText)
    $compressed = _LZNT_Compress($bData, 8, 258, False)
    $result = _LZNT_Decompress($compressed, 8, 258, False)
    CheckTest("Test 4 (Large data roundtrip)", $result == $bData)

    $result = _LZNT_Decompress(Binary("0xFF"), 2, 258, False)
    CheckTest("Test 5 (Invalid data error)", @error > 0)

    ConsoleWrite(@CRLF)
EndFunc

; Test _LZNT_CompressStr
Func Test_LZNT_CompressStr()
    ConsoleWrite("+=== Test _LZNT_CompressStr ===" & @CRLF)

    Local $result = _LZNT_CompressStr("Hello World", 0, 4, 2, False)
    CheckTest("Test 1 (Simple string)", IsBinary($result) And BinaryLen($result) > 0)

    $result = _LZNT_CompressStr("AAAAAAAAAA", 0, 4, 2, False)
    Local $bOriginal = StringToBinary("AAAAAAAAAA", 4)
    CheckTest("Test 2 (Compression ratio)", BinaryLen($result) < BinaryLen($bOriginal))

    $result = _LZNT_CompressStr("Hello World", 0, 4, 2, True, 64)
    CheckTest("Test 3 (Base64 string output)", IsString($result) And StringLen($result) > 0)

    $result = _LZNT_CompressStr("Xin chào Việt Nam", 0, 4, 2, False)
    CheckTest("Test 4 (Unicode string)", IsBinary($result) And BinaryLen($result) > 0)

    $result = _LZNT_CompressStr("", 0, 4, 2, False)
    CheckTest("Test 5 (Empty string)", $result == "")

    Local $sLong = ""
    For $i = 1 To 1000
        $sLong &= "Lorem ipsum dolor sit amet. "
    Next
    $result = _LZNT_CompressStr($sLong, 0, 4, 2, False)
    $bOriginal = StringToBinary($sLong, 4)
    Local $fRatio = BinaryLen($result) / BinaryLen($bOriginal)
    CheckTest("Test 6 (Long text ratio < 0.5)", $fRatio < 0.5)

    ConsoleWrite(@CRLF)
EndFunc

; Test _LZNT_DecompressStr
Func Test_LZNT_DecompressStr()
    ConsoleWrite("+=== Test _LZNT_DecompressStr ===" & @CRLF)

    Local $compressed = _LZNT_CompressStr("Hello World", 0, 4, 2, False)
    Local $result = _LZNT_DecompressStr($compressed, 0, 4, 2, False)
    CheckTest("Test 1 (Simple roundtrip)", $result == "Hello World")

    $compressed = _LZNT_CompressStr("AAAAAAAAAA", 0, 4, 2, False)
    $result = _LZNT_DecompressStr($compressed, 0, 4, 2, False)
    CheckTest("Test 2 (Repetitive roundtrip)", $result == "AAAAAAAAAA")

    $compressed = _LZNT_CompressStr("Hello World", 0, 4, 2, True)
    $result = _LZNT_DecompressStr($compressed, 0, 4, 2, True)
    CheckTest("Test 3 (Base64 roundtrip)", $result == "Hello World")

    $compressed = _LZNT_CompressStr("Xin chào Việt Nam", 0, 4, 2, False)
    $result = _LZNT_DecompressStr($compressed, 0, 4, 2, False)
    CheckTest("Test 4 (Unicode roundtrip)", $result == "Xin chào Việt Nam")

    Local $sLong = ""
    For $i = 1 To 1000
        $sLong &= "Lorem ipsum dolor sit amet. "
    Next
    $compressed = _LZNT_CompressStr($sLong, 0, 4, 2, False)
    $result = _LZNT_DecompressStr($compressed, 0, 4, 2, False)
    CheckTest("Test 5 (Long text roundtrip)", $result == $sLong)

    $result = _LZNT_DecompressStr(Binary("0xFF"), 0, 4, 2, False)
    CheckTest("Test 6 (Invalid data error)", @error > 0)

    Local $sOriginal = "!@#$%^&*()_+-=[]{}|;:',.<>?/~`"
    $compressed = _LZNT_CompressStr($sOriginal, 0, 4, 2, False)
    $result = _LZNT_DecompressStr($compressed, 0, 4, 2, False)
    CheckTest("Test 7 (Special chars roundtrip)", $result == $sOriginal)

    ConsoleWrite(@CRLF)
EndFunc

; Run all tests
Func RunAllTests()
    ConsoleWrite("========================================" & @CRLF)
    ConsoleWrite("  LZNT & Base64 Test Suite" & @CRLF)
    ConsoleWrite("========================================" & @CRLF & @CRLF)

    Test_Base64Encode()
    Test_Base64Decode()
    Test_Base64EncodeStr()
    Test_Base64DecodeStr()
    Test_LZNT_Compress()
    Test_LZNT_Decompress()
    Test_LZNT_CompressStr()
    Test_LZNT_DecompressStr()

    ; Print summary report
    ConsoleWrite("========================================" & @CRLF)
    ConsoleWrite("  TEST SUMMARY REPORT" & @CRLF)
    ConsoleWrite("========================================" & @CRLF)
    ConsoleWrite("Total Tests:  " & $g_iTestTotal & @CRLF)
    ConsoleWrite("Passed:       " & $g_iTestPassed & " (" & Round($g_iTestPassed / $g_iTestTotal * 100, 2) & "%)" & @CRLF)
    ConsoleWrite("Failed:       " & $g_iTestFailed & " (" & Round($g_iTestFailed / $g_iTestTotal * 100, 2) & "%)" & @CRLF)
    ConsoleWrite("========================================" & @CRLF)

    If $g_iTestFailed = 0 Then
        ConsoleWrite("Result: ALL TESTS PASSED!" & @CRLF)
    Else
        ConsoleWrite("Result: SOME TESTS FAILED!" & @CRLF)
    EndIf
    ConsoleWrite("========================================" & @CRLF)
EndFunc

; Execute all tests
RunAllTests()

 

LogTest:

========================================
  LZNT & Base64 Test Suite
========================================

+=== Test _Base64Encode ===
-Test 1 (Empty): PASS
-Test 2 (Hello World): PASS
-Test 3 (Binary): PASS
-Test 4 (Line wrap): PASS
-Test 5 (No wrap): PASS

+=== Test _Base64Decode ===
-Test 1 (Empty error): PASS
-Test 2 (Binary): PASS
-Test 3 (UTF8): PASS
-Test 4 (With whitespace): PASS
-Test 5 (Binary data): PASS
-Test 6 (Invalid error): PASS

+=== Test _Base64EncodeStr ===
-Test 1 (UTF8): PASS
-Test 2 (Empty): PASS
-Test 3 (Unicode roundtrip): PASS
-Test 4 (Line wrap): PASS
-Test 5 (ANSI roundtrip): PASS

+=== Test _Base64DecodeStr ===
-Test 1 (UTF8): PASS
-Test 2 (Empty error): PASS
-Test 3 (Unicode): PASS
-Test 4 (With whitespace): PASS
-Test 5 (ANSI): PASS
-Test 6 (Special chars): PASS

+=== Test _LZNT_Compress ===
-Test 1 (Simple compress): PASS
-Test 2 (Repetitive data): PASS
-Test 3 (Base64 output): PASS
-Test 4 (Large data): PASS
-Test 5 (Empty data): PASS

+=== Test _LZNT_Decompress ===
-Test 1 (Simple roundtrip): PASS
-Test 2 (Repetitive roundtrip): PASS
-Test 3 (Base64 roundtrip): PASS
-Test 4 (Large data roundtrip): PASS
-Test 5 (Invalid data error): PASS

+=== Test _LZNT_CompressStr ===
-Test 1 (Simple string): PASS
-Test 2 (Compression ratio): PASS
-Test 3 (Base64 string output): PASS
-Test 4 (Unicode string): PASS
-Test 5 (Empty string): PASS
-Test 6 (Long text ratio < 0.5): PASS

+=== Test _LZNT_DecompressStr ===
-Test 1 (Simple roundtrip): PASS
-Test 2 (Repetitive roundtrip): PASS
-Test 3 (Base64 roundtrip): PASS
-Test 4 (Unicode roundtrip): PASS
-Test 5 (Long text roundtrip): PASS
-Test 6 (Invalid data error): PASS
-Test 7 (Special chars roundtrip): PASS

========================================
  TEST SUMMARY REPORT
========================================
Total Tests:  45
Passed:       45 (100%)
Failed:       0 (0%)
========================================
Result: ALL TESTS PASSED!
========================================

 

Edited by Trong

Enjoy my work? Buy me a 🍻 or tip via ❤️ PayPal

  • Trong changed the title to LZNT compression/decompression and Base64 encoding/decoding

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...