Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library · std.bytes

    Bytes.deflateCompress

    Compresses this byte sequence using DEFLATE.

    deflateCompress() -> Result<Bytes, string>
    Module
    std.bytes
    Receiver
    Bytes
    Minimum arity
    0
    Maximum arity
    0

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    Import std.bytes in language mode 5.21.

    The current compiler selects std/bytes.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Source evidence

    Receiver

    Bytes

    Parameter constraints

    Bytes.deflateCompress returns Ok with one complete raw RFC 1951 DEFLATE stream. It adds no zlib or gzip header or checksum.

    Bytes.deflateFixedCompress returns Ok with one final fixed Huffman block. Empty input is valid and the final unused bits are zero.

    Fixed Huffman compression accepts at most 268435456 input bytes. Larger inputs return Err with a string before producing output.

    Bytes.zlibFixedCompress applies the same fixed Huffman compression and input bound, with an RFC 1950 zlib header and Adler-32 trailer.

    The zlib stream declares a 32768-byte window and no preset dictionary. Its header bytes are 0x78 and 0x01.

    Bytes.deflateDecompress accepts raw streams containing stored, fixed Huffman, and dynamic Huffman blocks. It reconstructs the original bytes in Ok.

    The decoder consumes one complete stream. Malformed or truncated blocks, invalid back-references, and complete trailing bytes return Err with a string.

    Final unused padding bits are not decoded as data. A zlib or gzip wrapper is not a raw DEFLATE stream.

    Bytes.deflateDecompress reverses either raw compressor for every supported input. Bytes.zlibFixedCompress requires a zlib decoder because its wrapper is part of the result.

    Each receiver evaluates once and remains unchanged. The operations perform no I/O and errors expose no partially decoded Bytes.

    Compression does not promise a particular size reduction. Except for the fixed block and wrapper requirements, encoded byte choices may change while preserving the stream format.

    Result

    The declared result type is Result<Bytes, string>.

    Effects

    Failures

    Result and effects

    Link

    Return one raw RFC 1951 DEFLATE stream in Ok without zlib or gzip framing.

    Resolved contracts

    Link

    Related entries

    Link

    std.bytes

    Distinguish raw DEFLATE from zlib framing

    Link
    Ruletopaz.stdlib.contract.bytes-deflate.rule

    Distinguish raw DEFLATE from zlib framing

    Bytes.deflateCompress returns Ok with one complete raw RFC 1951 DEFLATE stream. It adds no zlib or gzip header or checksum.

    Bytes.deflateFixedCompress returns Ok with one final fixed Huffman block. Empty input is valid and the final unused bits are zero.

    Fixed Huffman compression accepts at most 268435456 input bytes. Larger inputs return Err with a string before producing output.

    Bytes.zlibFixedCompress applies the same fixed Huffman compression and input bound, with an RFC 1950 zlib header and Adler-32 trailer.

    The zlib stream declares a 32768-byte window and no preset dictionary. Its header bytes are 0x78 and 0x01.

    Bytes.deflateDecompress accepts raw streams containing stored, fixed Huffman, and dynamic Huffman blocks. It reconstructs the original bytes in Ok.

    The decoder consumes one complete stream. Malformed or truncated blocks, invalid back-references, and complete trailing bytes return Err with a string.

    Final unused padding bits are not decoded as data. A zlib or gzip wrapper is not a raw DEFLATE stream.

    Bytes.deflateDecompress reverses either raw compressor for every supported input. Bytes.zlibFixedCompress requires a zlib decoder because its wrapper is part of the result.

    Each receiver evaluates once and remains unchanged. The operations perform no I/O and errors expose no partially decoded Bytes.

    Compression does not promise a particular size reduction. Except for the fixed block and wrapper requirements, encoded byte choices may change while preserving the stream format.

    Why

    Raw DEFLATE and zlib are different wire formats. The fixed Huffman operation additionally specifies block coding, so it serves a distinct format requirement rather than another name for a general compressor.

    Source evidence

    Recover bytes from a raw stream

    Link
    main.tpzRecover bytes from a raw streamOpen in Playground
    1. import std.io
    2. import std.test
    3. let encoded = test.assertOk("hello".encodeUtf8().deflateCompress())
    4. io.writeLine(test.assertOk(encoded.deflateDecompress()).toHex())
    5. io.writeLine(test.assertOk("".encodeUtf8().deflateFixedCompress()).toHex())
    6. io.writeLine(test.assertOk("".encodeUtf8().zlibFixedCompress()).toHex())
    Expected outputexit 0
    68656c6c6f
    0300
    7801030000000001
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 68656c6c6f
    2. 0300
    3. 7801030000000001
    Source evidence

    Decode a valid fixed Huffman stream

    Link
    main.tpzDecode a valid fixed Huffman streamOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. let encoded = test.assertOk(Bytes.fromHex("cb48cdc9c90700"))
    5. match (encoded.deflateDecompress()) {
    6. case Ok(bytes) => io.writeLine(bytes.toHex())
    7. case Err(_) => io.writeLine("rejected")
    8. }
    Expected outputexit 0
    68656c6c6f
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 68656c6c6f
    Source evidence

    Decode short nonfinal stored blocks

    Link
    main.tpzDecode short nonfinal stored blocksOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. let encoded = test.assertOk(Bytes.fromHex("000100feff41010100feff42"))
    5. match (encoded.deflateDecompress()) {
    6. case Ok(bytes) => io.writeLine(bytes.toHex())
    7. case Err(_) => io.writeLine("rejected")
    8. }
    Expected outputexit 0
    4142
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 4142
    Source evidence

    Ignore stored block alignment bits

    Link
    main.tpzIgnore stored block alignment bitsOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. let encoded = test.assertOk(Bytes.fromHex("f90100feff41"))
    5. match (encoded.deflateDecompress()) {
    6. case Ok(bytes) => io.writeLine(bytes.toHex())
    7. case Err(_) => io.writeLine("rejected")
    8. }
    Expected outputexit 0
    41
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 41
    Source evidence

    Reject truncated streams and trailing bytes

    Link
    main.tpzReject truncated streams and trailing bytesOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. for hex in (["", "010100feff", "010000ffff00", "070000"]) {
    5. match (test.assertOk(Bytes.fromHex(hex)).deflateDecompress()) {
    6. case Err(_) => io.writeLine("invalid DEFLATE")
    7. case Ok(_) => io.writeLine("accepted")
    8. }
    9. }
    Expected outputexit 0
    invalid DEFLATE
    invalid DEFLATE
    invalid DEFLATE
    invalid DEFLATE
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. invalid DEFLATE
    2. invalid DEFLATE
    3. invalid DEFLATE
    4. invalid DEFLATE
    Source evidence

    Menu

    Download