# LzMaximumCompressBuffer

<pre><code><strong>ULONG LzMaximumCompressBuffer(_In_ PBYTE UncompressedBuffer, _In_ ULONG SizeOfUncompressedBufferInBytes, _Inout_ PBYTE CompressedBuffer, _In_ ULONG CompressedBufferSizeInBytes)
</strong>{
	typedef NTSTATUS(NTAPI* RTLCOMPRESSBUFFER)(USHORT, PUCHAR, ULONG, PUCHAR, ULONG, ULONG, PULONG, PVOID);
	typedef NTSTATUS(NTAPI* RTLGETCOMPRESSIONWORKSPACESIZE)(USHORT, PULONG, PULONG);
	RTLCOMPRESSBUFFER RtlCompressBuffer = NULL;
	RTLGETCOMPRESSIONWORKSPACESIZE RtlGetCompressionWorkSpaceSize = NULL;
	HMODULE hMod = NULL;
	NTSTATUS Status = STATUS_SUCCESS;
	USHORT CompressionFormatAndEngine = COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM;
	ULONG CompressBufferWorkSpaceSize = 0;
	ULONG CompressFragmentWorkSpaceSize = 0;
	PVOID Workspace = NULL;
	ULONG FinalCompressedSize = 0;

	hMod = GetModuleHandleW(L"ntdll.dll");
	if (hMod == NULL)
		goto EXIT_ROUTINE;

	RtlCompressBuffer = (RTLCOMPRESSBUFFER)GetProcAddress(hMod, "RtlCompressBuffer");
	RtlGetCompressionWorkSpaceSize = (RTLGETCOMPRESSIONWORKSPACESIZE)GetProcAddress(hMod, "RtlGetCompressionWorkSpaceSize");

	if (!RtlCompressBuffer || !RtlGetCompressionWorkSpaceSize)
		goto EXIT_ROUTINE;

	Status = RtlGetCompressionWorkSpaceSize(CompressionFormatAndEngine, &#x26;CompressBufferWorkSpaceSize, &#x26;CompressFragmentWorkSpaceSize);
	if (Status != STATUS_SUCCESS)
		goto EXIT_ROUTINE;

	if (CompressBufferWorkSpaceSize == 0)
		goto EXIT_ROUTINE;

	Workspace = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, CompressBufferWorkSpaceSize);
	if (Workspace == NULL)
		goto EXIT_ROUTINE;

	Status = RtlCompressBuffer(CompressionFormatAndEngine, UncompressedBuffer, SizeOfUncompressedBufferInBytes, CompressedBuffer, CompressedBufferSizeInBytes, 4096, &#x26;FinalCompressedSize, Workspace);
	if (Status != STATUS_SUCCESS)
		goto EXIT_ROUTINE;

EXIT_ROUTINE:

	if (Workspace)
		HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, Workspace);

	return FinalCompressedSize;
}
</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://malwaresourcecode.com/home/compression/lempel-ziv/lzmaximumcompressbuffer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
