Documentation
¶
Overview ¶
Package lz supports encoding and decoding of LZ77 sequences. A sequence, as described in the Zstandard specification, consists of a literal copy command followed by a match copy command. The literal copy command is described by the length in literal bytes to be copied, and the match command consists of the distance of the match to copy and the length of the match in bytes.
A Parser converts a byte stream into blocks of sequences. The Decoder converts the block of sequences into the original decompressed byte stream.
The module provides multiple parser implementations that offer different combinations of encoding speed and compression ratios. Usually, a slower parser will generate a better compression ratio.
Parsers may use different matchers to provide their functionality. One Example is [greedyParser] which can use multiple Matcher implementations.
The library supports the implementation of parsers outside of this package.
Index ¶
- Variables
- type Block
- type Buffer
- func (b *Buffer) ByteAt(off int64) (c byte, err error)
- func (b *Buffer) Init(size, retentionSize int, shift ShiftFunc) error
- func (b *Buffer) Parsable() int
- func (b *Buffer) ReadAt(p []byte, off int64) (n int, err error)
- func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
- func (b *Buffer) Reset(data []byte) error
- func (b *Buffer) Write(p []byte) (n int, err error)
- type Configurator
- type Decoder
- func (d *Decoder) ByteAtEnd(off int) byte
- func (d *Decoder) Init(opts DecoderOptions) error
- func (d *Decoder) Read(p []byte) (n int, err error)
- func (d *Decoder) Reset()
- func (d *Decoder) Write(p []byte) (n int, err error)
- func (d *Decoder) WriteBlock(blk *Block) (n int, err error)
- func (d *Decoder) WriteByte(c byte) error
- func (d *Decoder) WriteMatch(mu, ou uint32) (n int, err error)
- func (d *Decoder) WriteTo(w io.Writer) (n int64, err error)
- type DecoderOptions
- type Entry
- type GenericMatcherOptions
- type GreedyParserOptions
- type HashOptions
- type Mapper
- type MapperConfigurator
- type Matcher
- type MatcherConfigurator
- type Parser
- type ParserFlags
- type Seq
- type ShiftFunc
Constants ¶
This section is empty.
Variables ¶
var ErrEndOfBuffer = errors.New("lz: end of buffer")
ErrEndOfBuffer is returned at the end of the buffer.
var ErrFullBuffer = errors.New("lz: full buffer")
ErrFullBuffer is returned when the buffer is full and no more data can be written to it.
var ErrOutOfBuffer = errors.New("lz: offset outside of buffer")
ErrOutOfBuffer is returned when the offset is outside of the buffer.
var ErrStartOfBuffer = errors.New("lz: start of buffer")
ErrStartOfBuffer is returned at the start of the buffer.
Functions ¶
This section is empty.
Types ¶
type Block ¶
Block stores sequences and literals. Note that the sequences stored in the Sequences slice might not consume the entire Literals slice. The remaining literal bytes must be added to the decoded text after all sequences have been decoded.
type Buffer ¶
type Buffer struct {
Data []byte
// Window end index
W int
// maximum buffer size
Size int
// RetentionSize is the number of bytes to keep when pruning the buffer.
RetentionSize int
// offset of Data
Off int64
// Shift will be called when the buffer is pruned to inform other
// components about the number of bytes removed from the start of
// the buffer.
Shift ShiftFunc
}
Buffer is the buffer used for LZ parsing.
The Off field describes the offset of Data[0] in the original stream. The W points to the end of sliding window used for copying matches.
Data is not fully allocated at the beginning. It grows with the usage. There must be always 7 extra bytes allocated at the end of Data to allow easy reads of data from the buffer.
func (*Buffer) ByteAt ¶ added in v0.5.0
ByteAt returns the byte at offset off. If off is outside of the buffer, ErrOutOfBuffer is returned.
func (*Buffer) Init ¶
Init initializes the buffer. The old data slice is reused and the capacity might be larger than the new buffer size.
func (*Buffer) Parsable ¶ added in v0.6.11
Parsable returns the number of bytes that can be parsed from the buffer.
func (*Buffer) ReadAt ¶ added in v0.5.0
ReadAt reads len(p) bytes from the buffer starting at byte offset off. It returns the number of bytes read and any error encountered. If off is outside of the buffer, ErrOutOfBuffer is returned.
func (*Buffer) ReadFrom ¶ added in v0.5.0
ReadFrom reads data from r until EOF or error. It returns the number of bytes read and any error encountered.
func (*Buffer) Reset ¶
Reset resets the buffer with the provided data slice. If the data slice is larger than the buffer size, the buffer size will be updated. Note that the data slice should have 7 extra bytes, len(data)+7 <= cap(data). Otherwise the old slice will be used or a new one need to be allocated.
type Configurator ¶
type Configurator interface {
NewParser(windowSize, retentionSize, bufferSize int) (Parser, error)
}
Configurator creates a parser. Usually an Options type implements the interface.
func ParseJSON ¶ added in v0.2.1
func ParseJSON(data []byte) (Configurator, error)
ParseJSON unmarshals parser options from JSON data.
type Decoder ¶
type Decoder struct {
// Data is the actual buffer. The end of the slice is also the head of
// the dictionary window.
Data []byte
// R tracks the position of the reads from the buffer and must be less
// or equal to the length of the Data slice.
R int
// Off records the total offset and marks the end of the Data slice,
// which is also the end of the dictionary window.
Off int64
// DecoderOptions provides the configuration parameters WindowSize and
// BufferSize.
DecoderOptions
}
Decoder provides a simple buffer for decoding LZ77 sequences. Data is the actual buffer. The end of the slice is also the head of the dictionary window. R tracks the read position in the buffer and must be less than or equal to the length of the Data slice. Off records the total offset and marks the end of the Data slice, which is also the end of the dictionary window. DecoderConfig provides the configuration parameters WindowSize and BufferSize.
func (*Decoder) Init ¶
func (d *Decoder) Init(opts DecoderOptions) error
Init initializes the DecoderBuffer.
func (*Decoder) Reset ¶
func (d *Decoder) Reset()
Reset returns the DecoderBuffer to its initialized state.
func (*Decoder) Write ¶
Write inserts the slice into the buffer. The method will write the entire slice or return 0 and ErrFullBuffer.
func (*Decoder) WriteBlock ¶
WriteBlock writes sequences from the block into the buffer. Each sequence is written atomically, as the block value is not modified. If there is not enough space in the buffer, ErrFullBuffer will be returned. All written sequences and literals will be removed from the block.
The capacity of the block slices will not be maintained. You have to keep a copy of the block to achieve that.
The growth of the array is limited to BufferSize.
The function returns the number of bytes written.
func (*Decoder) WriteMatch ¶
WriteMatch appends the match to the end of the buffer. The match will be written completely, or n=0 and ErrFullBuffer will be returned.
type DecoderOptions ¶ added in v0.6.0
type DecoderOptions struct {
// Size of the sliding dictionary window in bytes.
WindowSize int
// Maximum size of the buffer in bytes.
BufferSize int
}
DecoderOptions contains the parameters for the DecoderBuffer and decoder types. WindowSize must be smaller than BufferSize. It is recommended to set BufferSize to twice the WindowSize.
func (DecoderOptions) NewDecoder ¶ added in v0.6.6
func (opts DecoderOptions) NewDecoder() (*Decoder, error)
type Entry ¶ added in v0.6.3
type Entry struct {
// contains filtered or unexported fields
}
Entry is returned by a Mapper for a found match.
type GenericMatcherOptions ¶ added in v0.6.6
type GenericMatcherOptions struct {
MinMatchLen int
MaxMatchLen int
MapperOptions MapperConfigurator
}
GenericMatcherOptions provide the options for a generic matcher.
func (*GenericMatcherOptions) MarshalJSON ¶ added in v0.6.6
func (opts *GenericMatcherOptions) MarshalJSON() ([]byte, error)
MarshalJSON marshals the matcher options into JSON and adds the MatcherType field.
func (*GenericMatcherOptions) NewMatcher ¶ added in v0.6.6
func (opts *GenericMatcherOptions) NewMatcher(windowSize, retentionSize, bufferSize int) (Matcher, error)
NewMatcher creates a new generic matcher using the generic matcher options.
func (*GenericMatcherOptions) UnmarshalJSON ¶ added in v0.6.6
func (opts *GenericMatcherOptions) UnmarshalJSON(data []byte) error
type GreedyParserOptions ¶ added in v0.6.0
type GreedyParserOptions struct {
MatcherOptions MatcherConfigurator
}
GreedyParserOptions defines the configuration options for a greedy parser.
func (*GreedyParserOptions) MarshalJSON ¶ added in v0.6.7
func (gpo *GreedyParserOptions) MarshalJSON() ([]byte, error)
MarshalJSON provides a custom JSON marshaller that adds a type field to the JSON structure.
func (*GreedyParserOptions) NewParser ¶ added in v0.6.2
func (gpo *GreedyParserOptions) NewParser(windowSize, retentionSize, bufferSize int) (Parser, error)
NewParser creates a new greedy parser using the greedy parser options.
func (*GreedyParserOptions) UnmarshalJSON ¶ added in v0.6.7
func (gpo *GreedyParserOptions) UnmarshalJSON(data []byte) error
UnmarshalJSON provides a custom JSON unmarshaler that parses the type field from the JSON structure.
type HashOptions ¶ added in v0.6.0
HashOptions provides the parameters for the Hash Mapper.
func (*HashOptions) GetInputLen ¶ added in v0.6.6
func (hopts *HashOptions) GetInputLen() int
GetInputLen returns the input length.
func (*HashOptions) MarshalJSON ¶ added in v0.6.6
func (hopts *HashOptions) MarshalJSON() ([]byte, error)
MarshalJSON generates the JSON representation of HashOptions by adding the Mapper field and set it to "hash".
func (*HashOptions) NewMapper ¶ added in v0.6.6
func (hopts *HashOptions) NewMapper() (Mapper, error)
NewMapper creates the hash mapper.
type Mapper ¶ added in v0.6.3
type Mapper interface {
InputLen() int
Reset()
// Shift is called by the number of bytes pruned from the buffer.
Shift(delta int)
Put(a, w int, p []byte) int
// Get returns all candidate entries for the provided hash value. The
// entry value v contains the all 4 bytes stored a position i.
Get(v uint64) []Entry
}
Mapper will be typically implemented by hash tables.
The Put method returns the number of trailing bytes that could not be hashed. Shift is called, when n bytes have been pruned from the buffer.
type MapperConfigurator ¶ added in v0.6.6
MapperConfigurator creates a mapper, usually an Options type implements this function.
func UnmarshalJSONMapperOptions ¶ added in v0.6.6
func UnmarshalJSONMapperOptions(data []byte) (MapperConfigurator, error)
UnmarshalJSONMapperOptions unmarshals mapper options from JSON data. The function looks first for the MapperType field to determine the type of mapper to create.
type Matcher ¶ added in v0.6.0
type Matcher interface {
Edges(n int) []Seq
Skip(n int) (skipped int, err error)
Write(p []byte) (n int, err error)
ReadFrom(r io.Reader) (n int64, err error)
ReadAt(p []byte, off int64) (n int, err error)
ByteAt(off int64) (c byte, err error)
Reset(data []byte) error
Buf() *Buffer
WindowSize() int
Options() MatcherConfigurator
}
Matcher is responsible to find matches or Literal bytes in the byte stream.
type MatcherConfigurator ¶ added in v0.6.6
type MatcherConfigurator interface {
NewMatcher(windowSize, retentionSize, bufferSize int) (Matcher, error)
}
MatcherConfigurator creates a matcher, usually an Options type implements the interface.
func UnmarshalJSONMatcherOptions ¶ added in v0.6.6
func UnmarshalJSONMatcherOptions(data []byte) (MatcherConfigurator, error)
UnmarshalJSONMatcherOptions unmarshals matcher options from JSON data. The function looks first for the MatcherType field to determine the type of matcher to create.
type Parser ¶ added in v0.3.0
type Parser interface {
// Parse up to block size bytes from the internal buffer and provides
// the sequences in the block structure. While slices will be reused,
// not old information will be maintained.
Parse(blk *Block, n int, flags ParserFlags) (parsed int, err error)
// Write writes data into the internal buffer.
Write(p []byte) (n int, err error)
// ReadFrom reads data from the provided reader into the internal
// buffer.
ReadFrom(r io.Reader) (n int64, err error)
// ReadAt reads len(p) bytes from the internal buffer at offset off.
ReadAt(p []byte, off int64) (n int, err error)
// ByteAt returns the byte at offset off in the internal buffer.
ByteAt(off int64) (c byte, err error)
// Reset resets the internal buffer to the provided data.
Reset(data []byte) error
// WindowSize returns the window size used by the parser.
WindowSize() int
// Options returns the options used to create the parser.
Options() Configurator
}
Parser provides the possibility to parse a byte stream into LZ77 sequences.
type ParserFlags ¶ added in v0.6.0
type ParserFlags int
ParserFlags define optional parser behavior.
const ( // NoTrailingLiterals indicates that the parser should not generate // trailing literal bytes in the output. NoTrailingLiterals ParserFlags = 1 << iota )