LZ4's frame and block formats: a streaming decoder and encoder for frames, with XXH32 checks, dictionaries, skippable and legacy frames.
moon add gaato/lz4///|
test "compress and decompress" {
let text = b"hello, hello, hello, lz4"
let frame = @lz4.compress(text)
assert_eq(@lz4.decompress(frame, max_output=16 * 1024 * 1024), text)
// A block on its own: the caller keeps its size, or a bound of it.
let block = @lz4.compress_block(text)
assert_eq(@lz4.decompress_block(block, max_output=text.length()), text)
}
///|
test "decode what lz4 wrote" {
// `lz4` of "hello hello hello hello hello".
let frame = b"\x04\x22\x4d\x18\x64\x40\xa7\x0f\x00\x00\x00\x6ehello \x06\x00\x50hello\x00\x00\x00\x00\x79\xb1\xff\xf9"
assert_eq(@lz4.decompress(frame), b"hello hello hello hello hello")
}///|
/// Decodes what `read` hands over, `None` once it has no more, passing the
/// output to `write` as it is decoded.
fn decode_all(read : () -> Bytes?, write : (Bytes) -> Unit) -> Unit raise {
let decoder = @lz4.Decoder()
let out = FixedArray::make(64 * 1024, b'\x00')
for ;; {
let piece = read()
let end = piece is None
let mut input = piece.unwrap_or(b"")[:]
for ;; {
let status = decoder.step(input, out.mut_view(), end~)
input = input[decoder.last_consumed():]
write(Bytes::from_array(out[:decoder.last_produced()]))
match status {
Done => return
NeedMoreInput => break
NeedMoreOutput => ()
}
}
}
}
///|
/// A source that hands over `pieces` one by one, then `None`.
fn pieces_of(pieces : Array[Bytes]) -> () -> Bytes? {
let mut next = 0
() => {
if next < pieces.length() {
next += 1
Some(pieces[next - 1])
} else {
None
}
}
}
///|
test "decode a stream chunk by chunk" {
let frame = @lz4.compress(b"one frame, then the next")
let decoded = Buffer()
let pieces = [frame[:9].to_owned(), frame[9:].to_owned(), frame]
decode_all(pieces_of(pieces), bytes => decoded.write_bytes(bytes))
assert_eq(
decoded.to_bytes(),
b"one frame, then the nextone frame, then the next",
)
}| Option | Default | Notes |
|---|---|---|
| acceleration | 1 | Higher is faster and larger. lz4 --fast=N is N + 1. |
| block_size | Max64KiB | The lz4 command uses Max4MiB with linked=false. |
| linked | true | Blocks may refer to the previous 64 KiB. |
| checksum | true | Content checksum. |
| block_checksum | false | |
| content_size | none | Written in the header when given, and the input is held to it. |
| max_output (decompress) | none | Raises LimitExceeded past it. |
| max_output (decompress_block) | required | A block does not say its size, so the caller gives its decoded size or a bound. |
| Kind | When |
|---|---|
| Truncated | The input ends inside a frame. |
| Corrupt | The input is not valid LZ4. |
| ChecksumMismatch | A header, block or content checksum fails, or the content size does not match. |
| Unsupported | A version, block size or reserved bit the format does not define, or a legacy frame under until=EndOfMember. |
| DictionaryRequired | The frame names a dictionary and none was given. |
| TrailingData | Bytes after the last frame start no frame. |
| LimitExceeded | More than max_output. |
pub struct Decoder {
// private fields
}fn step(self : Decoder, input : BytesView, output : MutArrayView[Byte], end~ : Bool) -> Status raise CodecErrorfn Decoder::step(self : Decoder, input : BytesView, output : MutArrayView[Byte], end~ : Bool) -> Status raise CodecErrorpub struct Encoder {
// private fields
}fn step(self : Encoder, input : BytesView, output : MutArrayView[Byte], action~ : EncodeAction) -> Status raise CodecErrorfn Encoder::step(self : Encoder, input : BytesView, output : MutArrayView[Byte], action~ : EncodeAction) -> Status raise CodecErrorfn block_compress_bound(n : Int64) -> Int64fn compress(input : BytesView, acceleration? : Int, block_size? : BlockSize, linked? : Bool, checksum? : Bool, block_checksum? : Bool, dictionary? : BytesView, dictionary_id? : UInt) -> Bytesfn compress_block(input : BytesView, acceleration? : Int, dictionary? : BytesView) -> Bytesfn decoder_memory_bound() -> Int64fn decompress(input : BytesView, max_output? : Int, dictionary? : BytesView) -> Bytes raise CodecErrorfn decompress_block(input : BytesView, max_output~ : Int, dictionary? : BytesView) -> Bytes raise CodecErrorfn skippable_frame(variant : Int, data : BytesView) -> BytesInstall
Download zipLZ4's frame and block formats: a streaming decoder and encoder for frames, with XXH32 checks, dictionaries, skippable and legacy frames.