Snappy's raw and framing formats: a decoder and an encoder, streaming for the framing format, with CRC-32C checks.
moon add gaato/snappy///|
test "compress and decompress" {
let text = b"hello, hello, hello, snappy"
// The framing format (.sz files, `snzip`).
let framed = @snappy.compress(text)
assert_eq(@snappy.decompress(framed, max_output=16 * 1024 * 1024), text)
// The raw format (LevelDB, Parquet), with its length first.
let raw = @snappy.compress_raw(text)
assert_eq(@snappy.decompress_raw(raw, max_output=16 * 1024 * 1024), text)
}
///|
test "decode what snappy wrote" {
let framed = b"\xff\x06\x00\x00sNaPpY\x01\x09\x00\x00\xbb\x1f\x1c\x19hello"
assert_eq(@snappy.decompress(framed), b"hello")
assert_eq(@snappy.decompress_raw(b"\x05\x10hello"), b"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 = @snappy.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 decoded = Buffer()
decode_all(
pieces_of([
b"\xff\x06\x00\x00sNa", b"PpY\x01\x09\x00\x00\xbb\x1f", b"\x1c\x19hello",
]),
bytes => decoded.write_bytes(bytes),
)
assert_eq(decoded.to_bytes(), b"hello")
}| Option | Default | Notes |
|---|---|---|
| verify_checksums (Decoder) | true | false skips the CRC-32C check. |
| until (Decoder) | EndOfInput | |
| max_output (decompress, decompress_raw) | none | Raises LimitExceeded with resource: Output past it. |
| Kind | When |
|---|---|
| Truncated | The input ends inside a chunk, or inside a raw stream's length. |
| Corrupt | Anything else wrong in the stream. |
| ChecksumMismatch | A chunk's CRC-32C fails. |
| LimitExceeded | A compressed chunk over 76,494 bytes (resource: Memory), or more than max_output (resource: 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) -> Statusfn compress(input : BytesView) -> Bytesfn compress_bound(n : Int64) -> Int64fn compress_raw(input : BytesView) -> Bytesfn decoder_memory_bound() -> Int64fn encoder_memory_bound() -> Int64fn raw_compress_bound(n : Int64) -> Int64Install
Download zipSnappy's raw and framing formats: a decoder and an encoder, streaming for the framing format, with CRC-32C checks.