module documentation

Read and write compressedbuf files, used in NWSync and campaign databases.

Mainly used for NWSync file storage; but also used in sqlite files such as nwsync storage and string/blob compression in campaign sqlite databases.

Binary Format

Field Size (bytes) Description
Magic 4 FileMagic
Header Version 4 Format version (currently always 3)
Algorithm 4 Algorithm
Uncompressed Size 4 Size of the uncompressed data
Algorithm Version 4 (if ZLIB/ZSTD) Version of the compression algorithm (currently always 1)
Dictionary 4 (if ZSTD) Dictionary ID (currently always 0)
Compressed Data Variable The compressed data
Class Algorithm No class docstring; 1/3 constant documented
Function compress Compresses the given data using the specified algorithm and returns the compressed data as bytes.
Function decompress Decompresses the given data using the specified algorithm and returns the decompressed data as bytes.
Function read Decompresses the given binary file using the specified algorithm.
Function write Compresses the given data and writes it to the specified file using the specified algorithm.
Constant _VERSION Undocumented
Constant _ZLIB_VERSION Undocumented
Constant _ZSTD_VERSION Undocumented
def compress(data: bytes, file_magic: FileMagic, alg=Algorithm.ZSTD) -> bytes: (source)

Compresses the given data using the specified algorithm and returns the compressed data as bytes.

Parameters
data:bytesThe data to be compressed.
file_magic:FileMagicThe file magic to use.
alg:Algorithm, optionalThe compression algorithm to use. Defaults to Algorithm.ZSTD.
Returns
bytesThe compressed data as bytes.
def decompress(data: bytes, expect_magic: FileMagic | None = None) -> bytes: (source)

Decompresses the given data using the specified algorithm and returns the decompressed data as bytes.

Parameters
data:bytesThe data to be decompressed.
expect_magic:FileMagic | NoneThe expected file magic. If provided, the file magic read from the data must match this value.
Returns
bytesThe decompressed data as bytes.
def read(file: BinaryIO, expect_magic: FileMagic | None = None) -> tuple[bytes, FileMagic, Algorithm]: (source)

Decompresses the given binary file using the specified algorithm.

Parameters
file:BinaryIOThe binary file to decompress.
expect_magic:FileMagic | NoneThe expected file magic. If provided, the file magic read from the file must match this value.
Returns
tuple[bytes, FileMagic, Algorithm]A tuple containing the decompressed data, the file magic, and the algorithm used.
Raises
ValueErrorIf the stream is invalid, or the magic number does not match the expected value, or if an unsupported algorithm is encountered.
def write(file: BinaryIO, magic: FileMagic, data: bytes, alg=Algorithm.ZSTD): (source)

Compresses the given data and writes it to the specified file using the specified algorithm.

Parameters
file:BinaryIOThe file object to write the compressed data to.
magic:FileMagicThe file magic to write.
data:bytesThe data to be compressed.
alg:Algorithm, optionalThe compression algorithm to use. Defaults to Algorithm.ZSTD.
Raises
ValueErrorIf an unsupported algorithm is specified.
_VERSION: int = (source)

Undocumented

Value
3
_ZLIB_VERSION: int = (source)

Undocumented

Value
1
_ZSTD_VERSION: int = (source)

Undocumented

Value
1