Documentation
¶
Overview ¶
Package zlib is a pure-Go (no cgo) reimplementation of the public surface of Ruby's `zlib` standard library (the MRI 4.0.5 `Zlib` module). The DEFLATE engine is github.com/klauspost/compress (its drop-in compress/flate, compress/zlib and compress/gzip packages) — a pure-Go, CGO=0, build-from-source dependency that is substantially faster than the standard library's flate. The checksums are SIMD-accelerated: CRC-32 folds with the host's carryless-multiply unit (go-simd/crc32, PCLMULQDQ/PMULL) and Adler-32 uses go-simd/adler32, each a bit-exact drop-in for the standard library's kernel. So a host such as go-embedded-ruby can offer `require "zlib"` with no C extension and a static, CGO=0 binary.
The checksums (Crc32 / Adler32 and their combine forms) are byte-exact with MRI: a value computed here equals the value MRI prints for the same input. The compressors round-trip and interoperate with MRI (Inflate(MRIdeflate) and MRI-inflate of Deflate output both succeed), but the exact deflate byte stream is implementation-defined and need not equal MRI's — zlib never promises a canonical encoding, and Go's flate writer differs from zlib's. For gzip the same holds, plus the header carries an mtime/OS byte; compare the decompressed payload (and its CRC), not the raw gzip bytes.
Index ¶
- Constants
- Variables
- func Adler32(data []byte, seed uint32) uint32
- func Adler32Combine(adler1, adler2 uint32, len2 int64) uint32
- func Crc32(data []byte, seed uint32) uint32
- func Crc32Combine(crc1, crc2 uint32, len2 int64) uint32
- func Crc32Table() *crc32.Table
- func Deflate(data []byte, level int) ([]byte, error)
- func GzipCompress(data []byte, level int) ([]byte, error)
- func GzipDecompress(data []byte) ([]byte, error)
- func Inflate(data []byte) ([]byte, error)
- type Deflater
- type Error
- type Inflater
Constants ¶
const ( NoCompression = flate.NoCompression // 0 BestSpeed = flate.BestSpeed // 1 BestCompression = flate.BestCompression // 9 DefaultCompression = flate.DefaultCompression // -1 )
Compression levels — the values MRI exposes as Zlib::NO_COMPRESSION etc. They coincide with the compress/flate constants.
const ( DefaultStrategy = 0 // Zlib::DEFAULT_STRATEGY Filtered = 1 // Zlib::FILTERED HuffmanOnly = 2 // Zlib::HUFFMAN_ONLY RLE = 3 // Zlib::RLE Fixed = 4 // Zlib::FIXED )
Compression strategies (Zlib::*_STRATEGY / Zlib::FILTERED …). Go's flate does not act on a strategy, so these are accepted for parity and validation but do not change the output; DefaultStrategy is the only one that compresses normally, the others are tolerated.
const ( NoFlush = 0 // Zlib::NO_FLUSH SyncFlush = 2 // Zlib::SYNC_FLUSH FullFlush = 3 // Zlib::FULL_FLUSH Finish = 4 // Zlib::FINISH )
Flush modes for streaming Deflate/Inflate (Zlib::NO_FLUSH …). The numeric values match MRI (and zlib): NO_FLUSH=0, SYNC_FLUSH=2, FULL_FLUSH=3, FINISH=4.
const ( Version = "3.2.3" // Zlib::VERSION (Ruby binding) ZlibVersion = "1.2.12" // Zlib::ZLIB_VERSION (representative C-lib version) )
Version strings a host surfaces as Zlib::VERSION / Zlib::ZLIB_VERSION. VERSION is the Ruby zlib binding version (stable across MRI 4.0 builds). ZlibVersion stands in for the linked C zlib library version, which in MRI varies by host build (e.g. "1.2.12" or "1.3"); this port has no C zlib, so it reports a representative constant rather than a build-specific one.
Variables ¶
var ( // ErrStream is Zlib::StreamError — an invalid argument such as a bad // compression level. ErrStream = &Error{Class: "Zlib::StreamError", Msg: "stream error"} // ErrBuf is Zlib::BufError — a buffer/flush condition (e.g. a stream that // produced no progress). ErrBuf = &Error{Class: "Zlib::BufError", Msg: "buffer error"} // ErrData is Zlib::DataError — corrupt or invalid compressed input. ErrData = &Error{Class: "Zlib::DataError", Msg: "incorrect header check"} // ErrGzipFile is Zlib::GzipFile::Error — a malformed gzip stream. ErrGzipFile = &Error{Class: "Zlib::GzipFile::Error", Msg: "not in gzip format"} )
The sentinel leaf errors. They carry MRI's class name and default message; the wrap* helpers below clone them with the underlying library error attached so errors.Is still matches while errors.Unwrap reaches the cause.
Functions ¶
func Adler32 ¶
Adler32 returns the Adler-32 checksum of data continued from seed, mirroring Zlib.adler32(data, seed). The MRI default seed is 1 (the Adler-32 identity); callers wanting MRI's zero-argument behaviour pass 1. The value is byte-exact with MRI. From the identity seed it delegates to hash/adler32; from any other running value it continues the sum directly, since the standard library exposes only the from-scratch form.
func Adler32Combine ¶
Adler32Combine combines two Adler-32 checksums as if the two byte runs had been checksummed as one, mirroring Zlib.adler32_combine(adler1, adler2, len2).
func Crc32 ¶
Crc32 returns the CRC-32 (IEEE) checksum of data continued from seed, mirroring Zlib.crc32(data, seed). With seed 0 and the default arguments it is the plain checksum; passing a running value lets a caller checksum a stream in pieces. The value is byte-exact with MRI.
func Crc32Combine ¶
Crc32Combine combines two CRC-32 checksums as if the two byte runs had been checksummed as one, mirroring Zlib.crc32_combine(crc1, crc2, len2). len2 is the byte length of the second run.
func Crc32Table ¶
Crc32Table exposes the IEEE polynomial table for callers that need it (analogous to giving access to MRI's internal table); it is the same table hash/crc32 uses.
func Deflate ¶
Deflate compresses data into a zlib stream at the given level, mirroring Zlib::Deflate.deflate(data, level). level is DefaultCompression or 0..9; an out-of-range level returns ErrStream (MRI's Zlib::StreamError). The output is a valid zlib stream that Inflate and MRI both decode, though its exact bytes are not guaranteed to equal MRI's.
func GzipCompress ¶
GzipCompress compresses data into a gzip stream at the given level, mirroring a Zlib::GzipWriter round trip. The gzip header's mtime field is fixed at zero so the output is deterministic across runs (MRI defaults it to the current time); an out-of-range level returns ErrStream. The exact bytes still differ from MRI's (header OS byte and flate encoding); decode and compare the payload.
func GzipDecompress ¶
GzipDecompress decompresses a gzip stream, mirroring a Zlib::GzipReader read of the whole file. A bad gzip header or corrupt body (including a checksum mismatch) returns ErrGzipFile (MRI's Zlib::GzipFile::Error family).
Types ¶
type Deflater ¶
type Deflater struct {
// contains filtered or unexported fields
}
Deflater is the streaming compressor, mirroring a Zlib::Deflate instance:
z := zlib.NewDeflater(zlib.BestCompression)
a, _ := z.Deflate([]byte("hello "), zlib.SyncFlush)
b, _ := z.Deflate([]byte("world"), zlib.NoFlush)
tail, _ := z.Finish()
stream := append(append(a, b...), tail...) // a valid zlib stream
Bytes produced are accumulated internally; each Deflate / Finish call returns only the bytes that became available since the previous call (as MRI's Zlib::Deflate#deflate does). TotalIn / TotalOut / Adler / Finished track the stream as MRI's accessors do.
func NewDeflater ¶
NewDeflater creates a streaming compressor at the given level, falling back to DefaultCompression when level is out of range. Use NewDeflaterLevel to detect an invalid level (which MRI reports as Zlib::StreamError) instead of defaulting it.
func NewDeflaterLevel ¶
NewDeflaterLevel creates a streaming compressor, returning ErrStream for an out-of-range level (MRI raises Zlib::StreamError from Zlib::Deflate.new).
func (*Deflater) Deflate ¶
Deflate feeds data into the stream and returns the compressed bytes that became available, mirroring Zlib::Deflate#deflate(data, flush). flush is one of NoFlush / SyncFlush / FullFlush / Finish. With Finish the stream is closed (as Zlib::Deflate#deflate(data, Zlib::FINISH) does) and Finished becomes true.
func (*Deflater) Finish ¶
Finish closes the stream and returns any remaining compressed bytes, mirroring Zlib::Deflate#finish. After Finish, Finished reports true and further Deflate calls return ErrStream. Calling Finish again on an already-finished Deflater is tolerated and returns an empty slice (no error), matching MRI, whose Zlib::Deflate#finish returns "" when re-invoked rather than raising.
func (*Deflater) Finished ¶
Finished reports whether the stream has been finished (Zlib::Deflate#finished?).
type Error ¶
type Error struct {
// Class is the MRI exception class name a host should raise, e.g.
// "Zlib::DataError"; it lets the binding pick the exact Ruby class.
Class string
// Msg is the message MRI uses for the condition.
Msg string
// contains filtered or unexported fields
}
Error is the base of the Zlib error family, mirroring Zlib::Error (a StandardError subclass in MRI). Every error this package returns wraps to Error via errors.Is, so a host can map the whole family to Zlib::Error and the leaf classes (StreamError / BufError / DataError / GzipFile::Error) to the matching Ruby exceptions.
type Inflater ¶
type Inflater struct {
// contains filtered or unexported fields
}
Inflater is the streaming decompressor, mirroring a Zlib::Inflate instance. inflate accumulates the compressed input and decodes as much as it can; the MRI streaming idiom Zlib::Inflate.new.inflate(stream) is supported by feeding a complete stream in one call.
func NewInflater ¶
func NewInflater() *Inflater
NewInflater creates a streaming decompressor (Zlib::Inflate.new).
func (*Inflater) Finish ¶
Finish completes decompression, mirroring Zlib::Inflate#finish; for this one-shot streaming model it is a no-op that marks the inflater finished and returns no further bytes.
func (*Inflater) Finished ¶
Finished reports whether the stream has been fully inflated (Zlib::Inflate#finished?).
func (*Inflater) Inflate ¶
Inflate feeds compressed data and returns the decompressed bytes decoded so far, mirroring Zlib::Inflate#inflate(data). The accumulated input must form a complete zlib stream by the time output is required; a complete stream (the common one-shot streaming use) decodes fully and marks the inflater finished. Corrupt input returns ErrData.
Calling Inflate on an already-finished Inflater is tolerated and returns an empty slice (no error), matching MRI: once the stream end has been reached, Zlib::Inflate#inflate returns "" for any further input (including non-empty data) rather than raising.
