Compression Utilities

These functions provide compression and decompression throughout libredstone, through a variety of different algorithms. There is also a function to guess what compression algorithm a given set of data was compressed with.

Enums

enum RSCompressionType

Supported compression methods.

This enumeration is used all over libredstone whenever a compression type is required. If you don’t know the type, use RS_AUTO_COMPRESSION and the type will be guessed automatically.

Values:

enumerator RS_AUTO_COMPRESSION

Use this for almost everything —

it means “figure it out based

on the data given”, and works very well. Receiving functions determine type via rs_get_compression_type().

enumerator RS_GZIP

RFC 1952 (for standalone NBT)

enumerator RS_ZLIB

RFC 1950 (for region file NBT)

enumerator RS_UNKNOWN_COMPRESSION

Used by rs_get_compression_type() to indicate unknown type.

Functions

void rs_decompress(RSCompressionType enc, uint8_t *gzdata, size_t gzdatalen, uint8_t **outdata, size_t *outdatalen)

Decompress the given data.

Use this to conveniently decompress the given data. Decompressed data will be written to a newly-allocated buffer in *outdata, and the length of this data will be written to *outdatalen. The buffer stored in *outdata MUST be freed!

In the event of an error, *outdata will be set to NULL.

See also

rs_compress, rs_free

Parameters:
  • enc – the type of encoding to decompress with

  • gzdata – the data to decompress

  • gzdatalen – the length of gzdata

  • outdata – where to store the output buffer

  • outdatalen – where to store the output buffer length

void rs_compress(RSCompressionType enc, uint8_t *rawdata, size_t rawdatalen, uint8_t **gzdata, size_t *gzdatalen)

Compress the given data.

Use this to conveniently compress the given data. Compressed data will be written to a newly-allocated buffer in *gzdata, and the length of this data will be written to *gzdatalen. The buffer stored in *gzdata MUST be freed!

In the event of an error, *gzdata will be set to NULL.

See also

rs_decompress, rs_free

Parameters:
  • enc – the type of encoding to compress with

  • rawdata – the data to compress

  • rawdatalen – the length of rawdata

  • gzdata – where to store the compressed output buffer

  • gzdatalen – where to store the compressed output buffer length

RSCompressionType rs_get_compression_type(void *data, size_t len)

Use this to intelligently guess compression type.

The data you pass in must be at least a few bytes long. If the compression type cannot be guessed, it will return RS_UNKNOWN_COMPRESSION.

Parameters:
  • data – the data to guess for

  • len – the length of the data

Returns:

the guessed compression type