blob: 6252b46ae6fd3f27a1e63fcacfe86f0f6cecc272 [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001// Package zstd provides decompression of zstandard files.
2//
3// For advanced usage and examples, go to the README: https://github.com/klauspost/compress/tree/master/zstd#zstd
4package zstd
5
6import (
7 "bytes"
khenaidood948f772021-08-11 17:49:24 -04008 "errors"
9 "log"
10 "math"
Abhay Kumara2ae5992025-11-10 14:02:24 +000011
12 "github.com/klauspost/compress/internal/le"
khenaidood948f772021-08-11 17:49:24 -040013)
14
15// enable debug printing
16const debug = false
17
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053018// enable encoding debug printing
19const debugEncoder = debug
20
21// enable decoding debug printing
22const debugDecoder = debug
23
khenaidood948f772021-08-11 17:49:24 -040024// Enable extra assertions.
25const debugAsserts = debug || false
26
27// print sequence details
28const debugSequences = false
29
30// print detailed matching information
31const debugMatches = false
32
33// force encoder to use predefined tables.
34const forcePreDef = false
35
36// zstdMinMatch is the minimum zstd match length.
37const zstdMinMatch = 3
38
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053039// fcsUnknown is used for unknown frame content size.
40const fcsUnknown = math.MaxUint64
41
khenaidood948f772021-08-11 17:49:24 -040042var (
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")
46
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")
50
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")
54
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053055 // ErrUnexpectedBlockSize is returned when a block has unexpected size.
56 // Typically returned on invalid input.
57 ErrUnexpectedBlockSize = errors.New("unexpected block size")
58
khenaidood948f772021-08-11 17:49:24 -040059 // ErrMagicMismatch is returned when a "magic" number isn't what is expected.
60 // Typically this indicates wrong or corrupted input.
61 ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
62
63 // ErrWindowSizeExceeded is returned when a reference exceeds the valid window size.
64 // Typically this indicates wrong or corrupted input.
65 ErrWindowSizeExceeded = errors.New("window size exceeded")
66
67 // ErrWindowSizeTooSmall is returned when no window size is specified.
68 // Typically this indicates wrong or corrupted input.
69 ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small")
70
71 // ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit.
72 ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit")
73
74 // ErrUnknownDictionary is returned if the dictionary ID is unknown.
khenaidood948f772021-08-11 17:49:24 -040075 ErrUnknownDictionary = errors.New("unknown dictionary")
76
77 // ErrFrameSizeExceeded is returned if the stated frame size is exceeded.
78 // This is only returned if SingleSegment is specified on the frame.
79 ErrFrameSizeExceeded = errors.New("frame size exceeded")
80
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053081 // ErrFrameSizeMismatch is returned if the stated frame size does not match the expected size.
82 // This is only returned if SingleSegment is specified on the frame.
83 ErrFrameSizeMismatch = errors.New("frame size does not match size on stream")
84
khenaidood948f772021-08-11 17:49:24 -040085 // ErrCRCMismatch is returned if CRC mismatches.
86 ErrCRCMismatch = errors.New("CRC check failed")
87
88 // ErrDecoderClosed will be returned if the Decoder was used after
89 // Close has been called.
90 ErrDecoderClosed = errors.New("decoder used after Close")
91
Abhay Kumara2ae5992025-11-10 14:02:24 +000092 // ErrEncoderClosed will be returned if the Encoder was used after
93 // Close has been called.
94 ErrEncoderClosed = errors.New("encoder used after Close")
95
khenaidood948f772021-08-11 17:49:24 -040096 // ErrDecoderNilInput is returned when a nil Reader was provided
97 // and an operation other than Reset/DecodeAll/Close was attempted.
98 ErrDecoderNilInput = errors.New("nil input provided as reader")
99)
100
101func println(a ...interface{}) {
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +0530102 if debug || debugDecoder || debugEncoder {
khenaidood948f772021-08-11 17:49:24 -0400103 log.Println(a...)
104 }
105}
106
107func printf(format string, a ...interface{}) {
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +0530108 if debug || debugDecoder || debugEncoder {
khenaidood948f772021-08-11 17:49:24 -0400109 log.Printf(format, a...)
110 }
111}
112
khenaidood948f772021-08-11 17:49:24 -0400113func load3232(b []byte, i int32) uint32 {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000114 return le.Load32(b, i)
khenaidood948f772021-08-11 17:49:24 -0400115}
116
117func load6432(b []byte, i int32) uint64 {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000118 return le.Load64(b, i)
khenaidood948f772021-08-11 17:49:24 -0400119}
120
121type byter interface {
122 Bytes() []byte
123 Len() int
124}
125
126var _ byter = &bytes.Buffer{}