zstdframe

package
v1.104.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: BSD-3-Clause Imports: 8 Imported by: 2

Documentation

Overview

Package zstdframe provides functionality for encoding and decoding independently compressed zstandard frames.

Index

Constants

View Source
const (
	FastestCompression = encoderLevel(zstd.SpeedFastest)
	DefaultCompression = encoderLevel(zstd.SpeedDefault)
	BetterCompression  = encoderLevel(zstd.SpeedBetterCompression)
	BestCompression    = encoderLevel(zstd.SpeedBestCompression)
)

Constants that implement Option and can be passed to AppendEncode.

Variables

This section is empty.

Functions

func AppendDecode

func AppendDecode(dst, src []byte, opts ...Option) ([]byte, error)

AppendDecode appends the zstandard decoded content of src to dst. The input may consist of zero or more frames. Any call that handles untrusted input should specify MaxDecodedSize.

func AppendEncode

func AppendEncode(dst, src []byte, opts ...Option) []byte

AppendEncode appends the zstandard encoded content of src to dst. It emits exactly one frame as a single segment.

func GetDecoder added in v1.104.0

func GetDecoder(opts ...Option) (dec *zstd.Decoder, put func())

GetDecoder returns a decoder from the shared decoder pool for streaming (io.Reader-style) use, configured with opts. The caller must call dec.Reset on it before first use and whenever its underlying reader changes. When done with the decoder, the caller must call the returned put function exactly once to return it to the pool. The decoder must not be used after put is called, and must not be used concurrently.

func GetStreamingEncoder added in v1.104.0

func GetStreamingEncoder(opts ...Option) (enc *zstd.Encoder, put func())

GetStreamingEncoder returns an encoder from the shared pool, configured for streaming (stateful) use: it maintains compression context across Write calls, and Close finishes the frame. The caller must call enc.Reset with the destination writer before first use, and enc.Close when done writing. Call the returned put function exactly once afterwards to return the encoder to the pool; the encoder must not be used after that, and must not be used concurrently.

func NextSize

func NextSize(b []byte) (n int, err error)

NextSize parses the next frame (regardless of whether it is a data frame or a metadata frame) and returns the total size of the frame. The frame can be skipped by slicing n bytes from b (e.g., b[n:]). It report io.ErrUnexpectedEOF if the frame is incomplete.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is an option that can be passed to AppendEncode or AppendDecode.

func EncoderLevel

func EncoderLevel(level zstd.EncoderLevel) Option

EncoderLevel specifies the compression level when encoding.

This exists for compatibility with zstd.EncoderLevel values. Most usages should directly use one of the following constants:

By default, DefaultCompression is chosen. This option is ignored when decoding.

func LowMemory

func LowMemory(low bool) Option

LowMemory specifies that the encoder and decoder should aim to use lower amounts of memory at the cost of speed. By default, more memory used for better speed.

func MaxDecodedSize

func MaxDecodedSize(maxSize uint64) Option

MaxDecodedSize specifies the maximum decoded size and is used to protect against hostile content. By default, there is no limit. This option is ignored when encoding.

func MaxWindowSize

func MaxWindowSize(maxSize uint64) Option

MaxWindowSize specifies the maximum window size, which must be a power-of-two and be in the range of [zstd.MinWindowSize, zstd.MaxWindowSize].

The compression or decompression algorithm will use a LZ77 rolling window no larger than the specified size. The compression ratio will be adversely affected, but memory requirements will be lower. When decompressing, an error is reported if a LZ77 back reference exceeds the specified maximum window size.

For decompression, MaxDecodedSize is generally more useful.

func WithChecksum

func WithChecksum(check bool) Option

WithChecksum specifies whether to produce a checksum when encoding, or whether to verify the checksum when decoding. By default, checksums are produced and verified.

Jump to

Keyboard shortcuts

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