Skip to content

compress.brotli #

Description

compress.brotli compresses and decompresses binary data using Brotli.

The module loads the system libbrotlienc and libbrotlidec shared libraries at runtime. Set V_BROTLI_ENC_LIB or V_BROTLI_DEC_LIB to point to custom library locations when they are not in the platform loader path.

Examples

import compress.brotli

fn main() {
    uncompressed := 'Hello world!'
    compressed := brotli.compress(uncompressed.bytes(), mode: .text)!
    decompressed := brotli.decompress(compressed)!
    assert decompressed == uncompressed.bytes()
}

Library loading uses V's dl module and requires no bundled native loader header. MemorySanitizer builds link directly to the sanitizer runtime helper to mark bytes written by the external Brotli libraries as initialized, including when the runtime is linked statically.

fn compress #

fn compress(data []u8, params CompressParams) ![]u8

compress compresses data as a Brotli stream and returns the compressed bytes.

fn decompress #

fn decompress(data []u8, _ DecompressParams) ![]u8

decompress decompresses a Brotli stream and returns the decompressed bytes.

fn decompress_with_callback #

fn decompress_with_callback(data []u8, cb DecompressCallback, userdata voidptr) !int

decompress_with_callback decompresses a Brotli stream and delivers output chunks to cb. Returning 0 from cb stops decoding before the remaining input is validated.

fn is_available #

fn is_available() bool

is_available reports whether both Brotli encoder and decoder libraries can be loaded.

fn Mode.from #

fn Mode.from[W](input W) !Mode

from returns the Mode value of an integer or of a field name, or an error if there is none.

type DecompressCallback #

type DecompressCallback = fn (chunk []u8, userdata voidptr) int

DecompressCallback receives decoded chunks and returns the chunk length to continue or 0 to stop.

enum Mode #

enum Mode {
	generic = 0
	text    = 1
	font    = 2
}

Mode tunes the Brotli encoder for generic, text, or font data.

struct CompressParams #

@[params]
struct CompressParams {
pub:
	quality int  = 11
	lgwin   int  = 22
	mode    Mode = .generic
}

CompressParams controls Brotli compression.

struct DecompressParams #

@[params]
struct DecompressParams {}

DecompressParams controls Brotli decompression.