deflate

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2026 License: Apache-2.0, BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package deflate is a frozen copy of the Go standard library's DEFLATE encoder.

Why this exists

A DevProof subject digest is computed over the compressed layer, so the exact bytes a compressor emits are part of artifact identity (DP-002, DP-015). Neither the Go standard library nor any third-party compressor promises byte-stable output across releases; they are free to improve their match-finding at any time, and doing so is not considered a breaking change by anyone but us.

Compiling against compress/flate would therefore mean that upgrading Go silently reissues every artifact DevProof has ever produced under a new identity, with nothing to detect it but a golden-vector test failing long after the upgrade shipped. Freezing the encoder here removes that coupling entirely: the toolchain moves, the bytes do not.

What was frozen

Copied from Go 1.27.1, compress/flate, at the revision recorded in SOURCE-VERSION. Only the encoder is present. Decompression is deliberately left to the standard library, because inflating a valid DEFLATE stream is unambiguous — any correct implementation yields the same bytes — so there is nothing to pin.

The only edits are the package clause and the extraction of InternalError, which the encoder references from the decoder's file. No logic is changed.

Rules for this directory

Do not reformat, relint, refactor, or "modernize" anything here. The files are excluded from the formatters and most linters in .golangci.yaml for that reason, and some declarations are unused because their callers live in the decoder that was not copied. Any of those changes risks perturbing output for no benefit.

Updating to a newer upstream encoder is a bundle format version decision, not a dependency bump. It requires new golden vectors under a new format version, and the old encoder must be retained for as long as v1 artifacts are produced.

Provenance and license are recorded in the repository NOTICE and in the LICENSE file in this directory.

Index

Constants

View Source
const (
	NoCompression      = 0
	BestSpeed          = 1
	BestCompression    = 9
	DefaultCompression = -1

	// HuffmanOnly disables Lempel-Ziv match searching and only performs Huffman
	// entropy encoding. This mode is useful in compressing data that has
	// already been compressed with an LZ style algorithm (e.g. Snappy or LZ4)
	// that lacks an entropy encoder. Compression gains are achieved when
	// certain bytes in the input stream occur more frequently than others.
	//
	// Note that HuffmanOnly produces a compressed output that is
	// RFC 1951 compliant. That is, any valid DEFLATE decompressor will
	// continue to be able to decompress this output.
	HuffmanOnly = -2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type InternalError

type InternalError string

An InternalError reports an error in the flate code itself.

func (InternalError) Error

func (e InternalError) Error() string

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

A Writer takes data written to it and writes the compressed form of that data to an underlying writer (see NewWriter).

func NewWriter

func NewWriter(w io.Writer, level int) (*Writer, error)

NewWriter returns a new Writer compressing data at the given level. Following zlib, levels range from 1 (BestSpeed) to 9 (BestCompression); higher levels typically run slower but compress more. Level 0 (NoCompression) does not attempt any compression; it only adds the necessary DEFLATE framing. Level -1 (DefaultCompression) uses the default compression level. Level -2 (HuffmanOnly) will use Huffman compression only, giving a very fast compression for all types of input, but sacrificing considerable compression efficiency.

If level is in the range [-2, 9] then the error returned will be nil. Otherwise the error returned will be non-nil.

Note that the exact bytes written to w are not covered by the Go 1 compatibility promise. Callers, including tests, should not depend on the exact written bytes.

func NewWriterDict

func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, error)

NewWriterDict is like NewWriter but initializes the new Writer with a preset dictionary. The returned Writer behaves as if the dictionary had been written to it without producing any compressed output. The compressed data written to w can only be decompressed by a reader initialized with the same dictionary (see [NewReaderDict]).

Note that the exact bytes written to w are not covered by the Go 1 compatibility promise. Callers, including tests, should not depend on the exact written bytes.

func (*Writer) Close

func (w *Writer) Close() error

Close flushes and closes the writer.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush flushes any pending data to the underlying writer. It is useful mainly in compressed network protocols, to ensure that a remote reader has enough data to reconstruct a packet. Flush does not return until the data has been written. Calling Flush when there is no pending data still causes the Writer to emit a sync marker of at least 4 bytes. If the underlying writer returns an error, Flush returns that error.

In the terminology of the zlib library, Flush is equivalent to Z_SYNC_FLUSH.

func (*Writer) Reset

func (w *Writer) Reset(dst io.Writer)

Reset discards the writer's state and makes it equivalent to the result of NewWriter or NewWriterDict called with dst and w's level and dictionary.

func (*Writer) Write

func (w *Writer) Write(data []byte) (n int, err error)

Write writes data to w, which will eventually write the compressed form of data to its underlying writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL