1
// Package zstd provides decompression of zstandard files.
3
// For advanced usage and examples, go to the README: https://github.com/klauspost/compress/tree/master/zstd#zstd
15
// enable debug printing
18
// enable encoding debug printing
19
const debugEncoder = debug
21
// enable decoding debug printing
22
const debugDecoder = debug
24
// Enable extra assertions.
25
const debugAsserts = debug || false
27
// print sequence details
28
const debugSequences = false
30
// print detailed matching information
31
const debugMatches = false
33
// force encoder to use predefined tables.
34
const forcePreDef = false
36
// zstdMinMatch is the minimum zstd match length.
39
// Reset the buffer offset when reaching this.
40
const bufferReset = math.MaxInt32 - MaxWindowSize
43
// ErrReservedBlockType is returned when a reserved block type is found.
44
// Typically this indicates wrong or corrupted input.
45
ErrReservedBlockType = errors.New("invalid input: reserved block type encountered")
47
// ErrCompressedSizeTooBig is returned when a block is bigger than allowed.
48
// Typically this indicates wrong or corrupted input.
49
ErrCompressedSizeTooBig = errors.New("invalid input: compressed size too big")
51
// ErrBlockTooSmall is returned when a block is too small to be decoded.
52
// Typically returned on invalid input.
53
ErrBlockTooSmall = errors.New("block too small")
55
// ErrMagicMismatch is returned when a "magic" number isn't what is expected.
56
// Typically this indicates wrong or corrupted input.
57
ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
59
// ErrWindowSizeExceeded is returned when a reference exceeds the valid window size.
60
// Typically this indicates wrong or corrupted input.
61
ErrWindowSizeExceeded = errors.New("window size exceeded")
63
// ErrWindowSizeTooSmall is returned when no window size is specified.
64
// Typically this indicates wrong or corrupted input.
65
ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small")
67
// ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit.
68
ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit")
70
// ErrUnknownDictionary is returned if the dictionary ID is unknown.
71
// For the time being dictionaries are not supported.
72
ErrUnknownDictionary = errors.New("unknown dictionary")
74
// ErrFrameSizeExceeded is returned if the stated frame size is exceeded.
75
// This is only returned if SingleSegment is specified on the frame.
76
ErrFrameSizeExceeded = errors.New("frame size exceeded")
78
// ErrFrameSizeMismatch is returned if the stated frame size does not match the expected size.
79
// This is only returned if SingleSegment is specified on the frame.
80
ErrFrameSizeMismatch = errors.New("frame size does not match size on stream")
82
// ErrCRCMismatch is returned if CRC mismatches.
83
ErrCRCMismatch = errors.New("CRC check failed")
85
// ErrDecoderClosed will be returned if the Decoder was used after
86
// Close has been called.
87
ErrDecoderClosed = errors.New("decoder used after Close")
89
// ErrDecoderNilInput is returned when a nil Reader was provided
90
// and an operation other than Reset/DecodeAll/Close was attempted.
91
ErrDecoderNilInput = errors.New("nil input provided as reader")
94
func println(a ...interface{}) {
95
if debug || debugDecoder || debugEncoder {
100
func printf(format string, a ...interface{}) {
101
if debug || debugDecoder || debugEncoder {
102
log.Printf(format, a...)
106
// matchLenFast does matching, but will not match the last up to 7 bytes.
107
func matchLenFast(a, b []byte) int {
108
endI := len(a) & (math.MaxInt32 - 7)
109
for i := 0; i < endI; i += 8 {
110
if diff := load64(a, i) ^ load64(b, i); diff != 0 {
111
return i + bits.TrailingZeros64(diff)>>3
117
// matchLen returns the maximum length.
118
// a must be the shortest of the two.
119
// The function also returns whether all bytes matched.
120
func matchLen(a, b []byte) int {
122
for i := 0; i < len(a)-7; i += 8 {
123
if diff := load64(a, i) ^ load64(b, i); diff != 0 {
124
return i + (bits.TrailingZeros64(diff) >> 3)
128
checked := (len(a) >> 3) << 3
136
return len(a) + checked
139
func load3232(b []byte, i int32) uint32 {
140
return binary.LittleEndian.Uint32(b[i:])
143
func load6432(b []byte, i int32) uint64 {
144
return binary.LittleEndian.Uint64(b[i:])
147
func load64(b []byte, i int) uint64 {
148
return binary.LittleEndian.Uint64(b[i:])
151
type byter interface {
156
var _ byter = &bytes.Buffer{}