compress.gzip #
Description
compress.gzip
is a module that assists in the compression and decompression of binary data using gzip
compression
Examples:
import compress.gzip
fn main() {
uncompressed := 'Hello world!'
compressed := gzip.compress(uncompressed.bytes())!
decompressed := gzip.decompress(compressed)!
assert decompressed == uncompressed.bytes()
}
Constants #
const reserved_bits = 00b11100000
const ftext = 00b00000001
const fextra = 00b00000100
const fname = 00b00001000
const fcomment = 00b00010000
const fhcrc = 00b00000010
fn compress #
fn compress(data []u8, params CompressParams) ![]u8
Note: compression_level 0~4095
Example
compressed := gzip.compress(b, compression_level:4095)!
fn decompress #
fn decompress(data []u8, params DecompressParams) ![]u8
decompress an array of bytes using zlib and returns the decompressed bytes in a new array.
Example
decompressed := gzip.decompress(b)!
fn decompress_with_callback #
fn decompress_with_callback(data []u8, cb compr.ChunkCallback, userdata voidptr, params DecompressParams) !int
decompress_with_callback decompresses the given data
, using zlib. It calls cb
with each chunk of decompressed bytes. A chunk is usually 32 KB or less. Note: the chunk data received by cb
should be cloned, if you need to store it for later, and not process it right away. The callback function should return the chunk length, if it wants to continue decompressing, or 0, if it wants to abort the decompression early. See also compress.ChunkCallback for more details.
fn validate #
fn validate(data []u8, params DecompressParams) !GzipHeader
validate validates the header and returns its details if valid
enum CompressFlags #
enum CompressFlags {
// The low 12 bits will be overwritten by `compression_level`
compression_level_overwrite_flag01
compression_level_overwrite_flag02
compression_level_overwrite_flag03
compression_level_overwrite_flag04
compression_level_overwrite_flag05
compression_level_overwrite_flag06
compression_level_overwrite_flag07
compression_level_overwrite_flag08
compression_level_overwrite_flag09
compression_level_overwrite_flag10
compression_level_overwrite_flag11
compression_level_overwrite_flag12
// If set, the compressor outputs a zlib header before the deflate data, and the Adler-32 of the source data at the end. Otherwise, you'll get raw deflate data.
write_zlib_header //= 0x01000
// Always compute the adler-32 of the input data (even when not writing zlib headers).
compute_adler32 //= 0x02000
// Set to use faster greedy parsing, instead of more efficient lazy parsing.
greedy_parsing_flag //= 0x04000
// Enable to decrease the compressor's initialization time to the minimum, but the output may vary from run to run given the same input (depending on the contents of memory).
nondeterministic_parsing_flag //= 0x08000
// Only look for RLE matches (matches with a distance of 1)
rle_matches //= 0x10000
// Discards matches <= 5 chars if enabled.
filter_matches //= 0x20000
// Disable usage of optimized Huffman tables.
force_all_static_blocks //= 0x40000
// Only use raw (uncompressed) deflate blocks.
force_all_raw_blocks //= 0x80000
}
CompressFlags
Todo: These flags have no use now
enum DecompressFlags #
enum DecompressFlags {
// If set, the input has a valid zlib header and ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the input is a raw deflate stream.
parse_zlib_header
// If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input.
has_more_input
// If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB).
using_non_wrapping_output_buf
// Force adler-32 checksum computation of the decompressed bytes.
compute_adler32
}
DecompressFlags
Todo: These flags have no use now
struct CompressParams #
struct CompressParams {
pub:
compression_level int = 128 // 0~4095
flags CompressFlags
}
CompressParams set compression_level for compression: 0: Huffman only; 1: Huffman+LZ (fastest/crap compression); 128: default_max_probes; 4095: Huffman+LZ (slowest/best compression)
struct DecompressParams #
struct DecompressParams {
pub:
verify_header_checksum bool = true
verify_length bool = true
verify_checksum bool = true
flags DecompressFlags
}
DecompressParams set flags for decompression:
struct GzipHeader #
struct GzipHeader {
pub mut:
length int = 10
extra []u8
filename []u8
comment []u8
modification_time u32
operating_system u8
}