blob: 57a8a2f5bb82e7e2ef8052fb9c43d6988ea50d65 [file] [log] [blame]
Scott Bakered4efab2020-01-13 19:12:25 -08001// 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 "errors"
8 "log"
9 "math/bits"
10)
11
12const debug = false
13const debugSequences = false
14const debugMatches = false
15
16// force encoder to use predefined tables.
17const forcePreDef = false
18
19// zstdMinMatch is the minimum zstd match length.
20const zstdMinMatch = 3
21
22var (
23 // ErrReservedBlockType is returned when a reserved block type is found.
24 // Typically this indicates wrong or corrupted input.
25 ErrReservedBlockType = errors.New("invalid input: reserved block type encountered")
26
27 // ErrCompressedSizeTooBig is returned when a block is bigger than allowed.
28 // Typically this indicates wrong or corrupted input.
29 ErrCompressedSizeTooBig = errors.New("invalid input: compressed size too big")
30
31 // ErrBlockTooSmall is returned when a block is too small to be decoded.
32 // Typically returned on invalid input.
33 ErrBlockTooSmall = errors.New("block too small")
34
35 // ErrMagicMismatch is returned when a "magic" number isn't what is expected.
36 // Typically this indicates wrong or corrupted input.
37 ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
38
39 // ErrWindowSizeExceeded is returned when a reference exceeds the valid window size.
40 // Typically this indicates wrong or corrupted input.
41 ErrWindowSizeExceeded = errors.New("window size exceeded")
42
43 // ErrWindowSizeTooSmall is returned when no window size is specified.
44 // Typically this indicates wrong or corrupted input.
45 ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small")
46
47 // ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit.
48 ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit")
49
50 // ErrUnknownDictionary is returned if the dictionary ID is unknown.
51 // For the time being dictionaries are not supported.
52 ErrUnknownDictionary = errors.New("unknown dictionary")
53
54 // ErrFrameSizeExceeded is returned if the stated frame size is exceeded.
55 // This is only returned if SingleSegment is specified on the frame.
56 ErrFrameSizeExceeded = errors.New("frame size exceeded")
57
58 // ErrCRCMismatch is returned if CRC mismatches.
59 ErrCRCMismatch = errors.New("CRC check failed")
60
61 // ErrDecoderClosed will be returned if the Decoder was used after
62 // Close has been called.
63 ErrDecoderClosed = errors.New("decoder used after Close")
64)
65
66func println(a ...interface{}) {
67 if debug {
68 log.Println(a...)
69 }
70}
71
72func printf(format string, a ...interface{}) {
73 if debug {
74 log.Printf(format, a...)
75 }
76}
77
78// matchLen returns the maximum length.
79// a must be the shortest of the two.
80// The function also returns whether all bytes matched.
81func matchLen(a, b []byte) int {
82 b = b[:len(a)]
83 for i := 0; i < len(a)-7; i += 8 {
84 if diff := load64(a, i) ^ load64(b, i); diff != 0 {
85 return i + (bits.TrailingZeros64(diff) >> 3)
86 }
87 }
88 checked := (len(a) >> 3) << 3
89 a = a[checked:]
90 b = b[checked:]
91 // TODO: We could do a 4 check.
92 for i := range a {
93 if a[i] != b[i] {
94 return int(i) + checked
95 }
96 }
97 return len(a) + checked
98}
99
100// matchLen returns a match length in src between index s and t
101func matchLenIn(src []byte, s, t int32) int32 {
102 s1 := len(src)
103 b := src[t:]
104 a := src[s:s1]
105 b = b[:len(a)]
106 // Extend the match to be as long as possible.
107 for i := range a {
108 if a[i] != b[i] {
109 return int32(i)
110 }
111 }
112 return int32(len(a))
113}
114
115func load3232(b []byte, i int32) uint32 {
116 // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
117 b = b[i:]
118 b = b[:4]
119 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
120}
121
122func load6432(b []byte, i int32) uint64 {
123 // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
124 b = b[i:]
125 b = b[:8]
126 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
127 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
128}
129
130func load64(b []byte, i int) uint64 {
131 // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
132 b = b[i:]
133 b = b[:8]
134 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
135 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
136}