# Function Reference

# _WinAPI_CompressBuffer

Compresses a buffer with specified compression format and engine type

#include <WinAPISys.au3>
_WinAPI_CompressBuffer ( $pUncompressedBuffer, $iUncompressedSize, $pCompressedBuffer, $iCompressedSize [, $iFormatAndEngine = $COMPRESSION_FORMAT_LZNT1] )

## Parameters

$pUncompressedBuffer | A pointer to a caller-allocated buffer that contains the data to be compressed.
$iUncompressedSize | The size of the uncompressed buffer, in bytes.
$pCompressedBuffer | A pointer to a caller-allocated buffer that receives the compressed data.
$iCompressedSize | The size of the compressed buffer, in bytes.
$iFormatAndEngine | [optional] A bitmask that specifies the compression format and engine type. This parameter must be set to a valid bitwise OR combination of one format type and one engine type. $COMPRESSION_FORMAT_LZNT1 (Default) $COMPRESSION_FORMAT_XPRESS $COMPRESSION_FORMAT_XPRESS_HUFF $COMPRESSION_ENGINE_STANDARD (Default) $COMPRESSION_ENGINE_MAXIMUM

## Return Value

Success: | The size of the compressed data stored in compressed buffer, in bytes.
Failure: | 0 and sets the @error flag to non-zero, @extended flag may contain the NTSTATUS error code.

## Remarks

The _WinAPI_CompressBuffer() function takes as input an uncompressed buffer and produces its compressed equivalent provided that the compressed data fits within the specified destination buffer.
To decompress a compressed buffer, use the _WinAPI_DecompressBuffer() function.

## Related

_WinAPI_DecompressBuffer

## See Also

Search RtlCompressBuffer in MSDN Library.

## Example

    #include <WinAPIMem.au3>
    #include <WinAPISys.au3>

    ; Create compressed and uncompressed buffers
    Local $a_pBuffer[2]
    For $i = 0 To 1
        $a_pBuffer[$i] = _WinAPI_CreateBuffer(1024)
    Next

    Local $iCompressFormat = $COMPRESSION_FORMAT_XPRESS_HUFF
    ;~ Local $iCompressFormat = $COMPRESSION_FORMAT_XPRESS
    ;~ Local $iCompressFormat = $COMPRESSION_FORMAT_LZNT1

    ; Compress binary data
    Local $dData = Binary('0x00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF')

    If $iCompressFormat = $COMPRESSION_FORMAT_XPRESS_HUFF Then
        $dData &= $dData ;XPRESS_HUFF looks to struggle with short data
        $dData &= $dData
        $dData &= $dData
        $dData &= $dData
    EndIf

    Local $iSize = BinaryLen($dData)
    DllStructSetData(DllStructCreate('byte[' & $iSize & ']', $a_pBuffer[0]), 1, $dData)
    ConsoleWrite('Initial:      $iSize = ' & $iSize & @CRLF)

    $iSize = _WinAPI_CompressBuffer($a_pBuffer[0], $iSize, $a_pBuffer[1], 1024, BitOR($iCompressFormat, $COMPRESSION_ENGINE_MAXIMUM))
    If Not @error Then
        ConsoleWrite('Compressed:   $iSize = ' & $iSize & ' : ' & DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $a_pBuffer[1]), 1) & @CRLF)
    Else
        ConsoleWrite('Uncompressed: $iSize = ' & $iSize & ' : ' & ' Error code: ' & @error & '    Extended code: ' & @extended & ' (0x' & Hex(@extended) & ')' & @CRLF)
    EndIf

    ; Decompress data
    $iSize = _WinAPI_DecompressBuffer($a_pBuffer[0], 1024, $a_pBuffer[1], $iSize, BitOR($iCompressFormat, $COMPRESSION_ENGINE_MAXIMUM))
    If Not @error Then
        ConsoleWrite('Uncompressed: $iSize = ' & $iSize & ' : ' & DllStructGetData(DllStructCreate('byte[' & $iSize & ']', $a_pBuffer[0]), 1) & @CRLF)
    Else
        ConsoleWrite('Uncompressed: $iSize = ' & $iSize & ' : ' & ' Error code: ' & @error & '    Extended code: ' & @extended & ' (0x' & Hex(@extended) & ')' & @CRLF)
    EndIf

    ; Free memory
    For $i = 0 To 1
        _WinAPI_FreeMemory($a_pBuffer[$i])
    Next