dreamworkAPI
Ctrl K
GitHub
libraryDocs / lzma

lzma

librarysharedmenusource

The lzma library, compresses and decompresses strings in the format used by util.Compress.

The format is the 5 bytes of lzma properties, the decompressed size as 8 bytes in little endian and the raw lzma stream.

When HolyLib is installed and the call is made from a coroutine, the work is done on a separate thread with util.AsyncCompress / util.AsyncDecompress and the coroutine waits for the result, otherwise the call is synchronous.

Fields

NameTypeDescription
PROPS_SIZEintegerThe size of the lzma properties in bytes. = 5

Functions

lzma.compressCompresses a string into lzma data.
lzma.decompressDecompresses lzma data.
lzma.sizeReturns the decompressed size stored in the header of the lzma data.
lzma.validateChecks if the string starts with a valid lzma header, the same check HolyLib and the engine do before decompressing.

#lzma.compress

sharedmenuasyncnodiscardsource
async function lzma.compress(data: string, level?: integer, dictionary_size?: integer): string

Compresses a string into lzma data.

Runs on a separate thread when HolyLib is installed and the call is made from a coroutine, synchronously with util.Compress otherwise.

Throws an error if the compression fails.

Parameters
NameTypeDescription
datastringThe string to compress.
leveloptionalintegerThe compression level from 0 to 9, 5 by default, only used by HolyLib.
dictionary_sizeoptionalintegerThe dictionary size in bytes, 65536 by default, only used by HolyLib.
Returns
TypeNameDescription
stringcompressed_dataThe compressed data.

#lzma.decompress

sharedmenuasyncnodiscardsource
async function lzma.decompress(compressed_data: string, ratio?: number): string

Decompresses lzma data.

Runs on a separate thread when HolyLib is installed and the call is made from a coroutine, synchronously with util.Decompress otherwise.

ratio protects against decompression bombs, the data is rejected if decompressed_size // compressed_size is greater than 1 / ( 1 - ratio ), 0.98 allows less than 50 times the compressed size ( floating point makes the limit 49.99... ), nil or 1 and more disables the check.

Throws an error on an invalid header ( see lzma.validate ), malformed or truncated data, a ratio of 0 or less, or if the ratio is exceeded; call via pcall() when processing untrusted input.

Parameters
NameTypeDescription
compressed_datastringThe lzma data to decompress.
ratiooptionalnumberThe maximum allowed compression ratio, no limit by default.
Returns
TypeNameDescription
stringdecompressed_dataThe decompressed string.

#lzma.size

sharedmenunodiscardsource
function lzma.size(str: string): integer

Returns the decompressed size stored in the header of the lzma data.

Only the low 32 bits of the 64-bit size field are read.

Throws an error if the data is shorter than 9 bytes.

Parameters
NameTypeDescription
strstringThe lzma data.
Returns
TypeNameDescription
integersizeThe decompressed size in bytes.

#lzma.validate

sharedmenunodiscardsource
function lzma.validate(compressed_data: string): boolean, nil | string

Checks if the string starts with a valid lzma header, the same check HolyLib and the engine do before decompressing.

The data must be longer than the 13 bytes of the header, the properties byte must be below 225 ( ( pb * 5 + lp ) * 9 + lc ) and the dictionary size must be at least 4096 bytes.

The stream itself is not checked, so lzma.decompress can still fail on a valid header.

Parameters
NameTypeDescription
compressed_datastringThe lzma data.
Returns
TypeNameDescription
booleanis_validtrue if the header is valid, false otherwise.
nil | stringerr_msgThe reason why the header is invalid.
Generated from the dreamwork meta branch.