Lua API:Bz2

From The Powder Toy
Revision as of 03:04, 13 March 2024 by jacob1 (talk | contribs) (98.0 update: Replace with LBPHacker's version)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The bz2 API provides access to the bzip2 library TPT bundles. It can be used for compressing and decompressing blocks of data, such as TPT saves.

Only one-shot functionality is exposed because streaming functionality is only useful for tasks that are beyond the scope of this API.

Unless stated otherwise, all functions raise errors if supplied with parameters that disagree with their descriptions.

Methods

bz2.compress

Compress data with bzip2 at compression level 9.

compressedData, errCode, errStr = bz2.compress(data, [maxSize])
  • data: string, the data to be compressed
  • maxSize: number, upper limit on the length of compressedData; defaults to 0, which means no limit
  • compressedData: string, the compressed data, or nil on error
  • errCode: nil, or one of the following values on error:
    • bz2.COMPRESS_NOMEM: out of memory, TPT will probably crash soon
    • bz2.COMPRESS_LIMIT: the length of compressedData would exceed maxSize
  • errStr: nil, or a human-friendly string that explains the error

bz2.decompress

Decompress bzip2-compressed data.

data, errCode, errStr = bz2.decompress(compressedData, [maxSize])
  • compressedData: string, the compressed data
  • maxSize: number, upper limit on the length of data; defaults to 0, which means no limit
  • data: string, the original data, or nil on error
  • errCode: nil, or one of the following values on error:
    • bz2.DECOMPRESS_NOMEM: out of memory, TPT will probably crash soon
    • bz2.DECOMPRESS_LIMIT: the length of data would exceed maxSize
    • bz2.DECOMPRESS_TYPE: compressedData is not bzip2-compressed data
    • bz2.DECOMPRESS_BAD: compressedData is otherwise corrupt
    • bz2.DECOMPRESS_EOF: compressedData contains data beyond the end of the bzip2 stream
  • errStr: nil, or a human-friendly string that explains the error