zstd

    compressions
    decompression
    zstd
    Download zip
    Author
    Version
    0.1.3
    License
    Apache-2.0
    Last updated
    50 minutes ago
    Downloads
    3K

    #Milky2018/zstd

    Pure MoonBit Zstandard library (incremental implementation).

    #Import

    // moon.pkg
    import "Milky2018/zstd" @zstd

    #Quick Start

    let input = b"hello zstd"
    let compressed = @zstd.compress(input)
    let restored = @zstd.decompress(compressed)
    assert_eq(restored, input)

    #API

    • compress(src : Bytes, level? : Int = 3) -> Bytes raise ZstdError
    • compress(src : Bytes, level? : Int = 3, checksum? : Bool = false) -> Bytes raise ZstdError
    • compress_with_dictionary(src : Bytes, dictionary : Bytes, level? : Int = 3, checksum? : Bool = false) -> Bytes raise ZstdError
    • compress_with_options(src : Bytes, options : CompressOptions) -> Bytes raise ZstdError
    • compress_with_dictionary_and_options(src : Bytes, dictionary : Bytes, options : CompressOptions) -> Bytes raise ZstdError
    • default_compress_options(level? : Int = 3, checksum? : Bool = false) -> CompressOptions
    • decompress(src : Bytes) -> Bytes raise ZstdError
    • decompress_with_dictionary(src : Bytes, dictionary : Bytes) -> Bytes raise ZstdError
    • new_compressor(level? : Int = 3) -> Compressor
    • new_compressor_with_dictionary(dictionary : Bytes, level? : Int = 3) -> Compressor
    • new_compressor_with_options(options : CompressOptions) -> Compressor
    • new_compressor_with_dictionary_and_options(dictionary : Bytes, options : CompressOptions) -> Compressor
    • Compressor::push(self, chunk : Bytes) -> Unit
    • Compressor::pull(self) -> Bytes raise ZstdError
    • Compressor::finish(self) -> Bytes raise ZstdError
    • Compressor::pending_input(self) -> Int
    • new_decompressor() -> Decompressor
    • new_decompressor_with_dictionary(dictionary : Bytes) -> Decompressor
    • Decompressor::push(self, chunk : Bytes) -> Unit
    • Decompressor::pull(self) -> Bytes raise ZstdError
    • Decompressor::finish(self) -> Bytes raise ZstdError
    • Decompressor::pending_input(self) -> Int
    • compress_bound_macro(src_size : UInt64) -> UInt64
    • compress_bound(src_size : UInt64) -> UInt64 raise ZstdError
    • decompress_bound(src : Bytes) -> UInt64 raise ZstdError

    #Current Support

    • Compression:
      • emits valid zstd frames using raw/rle blocks
      • at higher levels (level >= 10), can emit compressed blocks for repeated-window payloads with short periods (2..64), including literal prefixes and tails (pure MoonBit subset)
      • at very high levels (level >= 18), may emit multi-sequence variants (2..4 sequences) for this periodic-window subset
      • for prefixed periodic windows, multi-sequence encoding is attempted when symbols are representable; otherwise it falls back automatically
      • includes a generic non-periodic single-match compressed-block path (selected when periodic encoding is unavailable or not preferred)
      • supports dictionary-aware compression entrypoints:
        • compress_with_dictionary / new_compressor_with_dictionary
        • emits frame dictID for standard zstd dictionaries
        • can use dictionary history for first-block single-match encoding
        • single-match offset symbol encoding is repcode-aware (rep1/rep2 when applicable)
        • predefined multi-sequence offset path is repcode-aware (including rep3 and ll=0 combinations)
        • seeds first-block entropy state from standard dictionary tables:
          • sequence-table headers can be reused as repeat-mode sources
          • literal Huffman tree can be reused as treeless literals source
          • mode selection can prefer candidates with smaller seeded-rewrite size (repeat/treeless aware)
      • supports options-based tuning via CompressOptions:
        • enable_long_distance_matching increases single-match search range
          • long-distance search window now scales with block length (instead of fixed 32768)
        • target_compressed_block_size bounds emitted compressed-block payload size (falls back to raw when exceeded)
        • single_segment can be disabled to emit a window descriptor
        • write_content_size controls FCS presence for non-single-segment frames
        • compact_frame_header enables compact FCS encodings (1/2-byte when representable)
        • window_log controls target window size for non-single-segment frames
    • Decompression:
      • supports raw blocks and rle blocks
      • supports compressed blocks where:
        • literals section:
          • raw/rle
          • compressed literals with direct Huffman weights
          • compressed literals with FSE-compressed Huffman weights
          • treeless literals reusing previous Huffman tree
          • both single-stream and 4-stream Huffman literals
        • sequence modes are RLE_Mode, Predefined_Mode, FSE_Compressed_Mode, and Repeat_Mode
        • repeat mode reuses previously established RLE/predefined/FSE sequence tables
        • sequence bitstream is parsed in reverse for extra bits
        • offset code <= 31
        • literal length code <= 35, match length code <= 52
    • Frame handling: supports concatenated frames and skippable frames.
    • Frame checksum: validates XXH64-based 32-bit checksum when present.
    • Streaming encode: stateful Compressor supports chunked input.
    • Streaming decode: stateful Decompressor supports chunked input.
    • Dictionary-backed decode:
      • raw dictionary/prefix history
      • standard zstd dictionary blobs (magic + dictID + entropy prelude + rep offsets + content)
      • verified against real zstd CLI dictionary-compressed vectors
      • verified across multiple compression modes (-1/-9/-19, check/no-check, long mode)

    #Current Limitations

    • Full upstream dictionary compatibility is still incremental; some complex dictionary-compressed frames may still be rejected.

    #Error Handling

    All fallible APIs raise ZstdError:

    • SrcSizeTooLarge(UInt64)
    • SrcSizeWrong
    • CorruptionDetected
    • BoundOverflow
    • UnsupportedFeature(String)

    ZstdError

    pub suberror ZstdError {
    SrcSizeTooLarge(UInt64)
    SrcSizeWrong
    CorruptionDetected
    BoundOverflow
    DictionaryRequired(UInt)
    UnsupportedFeature(String)
    }

    CompressOptions

    pub struct CompressOptions {
    level : Int
    checksum : Bool
    single_segment : Bool
    write_content_size : Bool
    compact_frame_header : Bool
    window_log : Int
    enable_long_distance_matching : Bool
    target_compressed_block_size : Int
    }

    CompressOptions::with_checksum

    fn CompressOptions::with_checksum(self : CompressOptions, checksum : Bool) -> CompressOptions

    CompressOptions::with_compact_frame_header

    fn CompressOptions::with_compact_frame_header(self : CompressOptions, enabled : Bool) -> CompressOptions

    CompressOptions::with_content_size

    fn CompressOptions::with_content_size(self : CompressOptions, enabled : Bool) -> CompressOptions

    CompressOptions::with_level

    fn CompressOptions::with_level(self : CompressOptions, level : Int) -> CompressOptions

    CompressOptions::with_long_distance_matching

    fn CompressOptions::with_long_distance_matching(self : CompressOptions, enabled : Bool) -> CompressOptions

    CompressOptions::with_single_segment

    fn CompressOptions::with_single_segment(self : CompressOptions, enabled : Bool) -> CompressOptions

    CompressOptions::with_target_compressed_block_size

    fn CompressOptions::with_target_compressed_block_size(self : CompressOptions, size : Int) -> CompressOptions

    CompressOptions::with_window_log

    fn CompressOptions::with_window_log(self : CompressOptions, log : Int) -> CompressOptions

    Compressor

    pub struct Compressor {
    input_buffer : Array[Byte]
    options : CompressOptions
    dictionary : Bytes
    }

    Compressor::finish

    fn Compressor::finish(self : Compressor) -> Bytes raise ZstdError

    Compressor::pending_input

    fn Compressor::pending_input(self : Compressor) -> Int

    Compressor::pull

    fn Compressor::pull(self : Compressor) -> Bytes raise ZstdError

    Compressor::push

    fn Compressor::push(self : Compressor, chunk : Bytes) -> Unit

    Decompressor

    pub struct Decompressor {
    input_buffer : Array[Byte]
    dictionary : Bytes
    }

    Decompressor::finish

    fn Decompressor::finish(self : Decompressor) -> Bytes raise ZstdError

    Decompressor::pending_input

    fn Decompressor::pending_input(self : Decompressor) -> Int

    Decompressor::pull

    fn Decompressor::pull(self : Decompressor) -> Bytes raise ZstdError

    Decompressor::push

    fn Decompressor::push(self : Decompressor, chunk : Bytes) -> Unit

    compress

    fn compress(src : Bytes, level? : Int, checksum? : Bool) -> Bytes raise ZstdError

    Pure MoonBit one-shot compressor. Current subset: emits valid zstd frames with raw/rle blocks and a minimal compressed-block path for periodic payloads at higher levels.

    compress_bound

    fn compress_bound(src_size : UInt64) -> UInt64 raise ZstdError

    MoonBit equivalent of ZSTD_compressBound(srcSize). Upstream C returns an error code for srcSize >= ZSTD_MAX_INPUT_SIZE. This port raises a typed error instead.

    compress_bound_macro

    fn compress_bound_macro(src_size : UInt64) -> UInt64

    MoonBit equivalent of ZSTD_COMPRESSBOUND(srcSize) in upstream zstd.h.

    compress_with_dictionary

    fn compress_with_dictionary(src : Bytes, dictionary : Bytes, level? : Int, checksum? : Bool) -> Bytes raise ZstdError

    Pure MoonBit one-shot compressor with optional dictionary history. If dictionary is a standard zstd dictionary blob, frame header dictID is emitted from dictionary metadata. Raw prefix dictionaries emit no dictID.

    compress_with_dictionary_and_options

    fn compress_with_dictionary_and_options(src : Bytes, dictionary : Bytes, options : CompressOptions) -> Bytes raise ZstdError

    Pure MoonBit one-shot compressor with dictionary history and options.

    compress_with_options

    fn compress_with_options(src : Bytes, options : CompressOptions) -> Bytes raise ZstdError

    Pure MoonBit one-shot compressor with structured encoding options.

    decompress

    fn decompress(src : Bytes) -> Bytes raise ZstdError

    Pure MoonBit one-shot decompressor. Supports concatenated frames and skippable frames.

    decompress_bound

    fn decompress_bound(src : Bytes) -> UInt64 raise ZstdError

    MoonBit equivalent of ZSTD_decompressBound(src, srcSize). Supports concatenated zstd frames and skippable frames.

    decompress_with_dictionary

    fn decompress_with_dictionary(src : Bytes, dictionary : Bytes) -> Bytes raise ZstdError

    Pure MoonBit one-shot decompressor with raw dictionary/prefix history.

    default_compress_options

    fn default_compress_options(level? : Int, checksum? : Bool) -> CompressOptions

    new_compressor

    fn new_compressor(level? : Int) -> Compressor

    new_compressor_with_dictionary

    fn new_compressor_with_dictionary(dictionary : Bytes, level? : Int) -> Compressor

    new_compressor_with_dictionary_and_options

    fn new_compressor_with_dictionary_and_options(dictionary : Bytes, options : CompressOptions) -> Compressor

    new_compressor_with_options

    fn new_compressor_with_options(options : CompressOptions) -> Compressor

    new_decompressor

    fn new_decompressor() -> Decompressor

    new_decompressor_with_dictionary

    fn new_decompressor_with_dictionary(dictionary : Bytes) -> Decompressor

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io