Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 1 | // 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 |
| 4 | package zstd |
| 5 | |
| 6 | import ( |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 7 | "bytes" |
| 8 | "encoding/binary" |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 9 | "errors" |
| 10 | "log" |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 11 | "math" |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 12 | "math/bits" |
| 13 | ) |
| 14 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 15 | // enable debug printing |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 16 | const debug = false |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 17 | |
| 18 | // Enable extra assertions. |
| 19 | const debugAsserts = debug || false |
| 20 | |
| 21 | // print sequence details |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 22 | const debugSequences = false |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 23 | |
| 24 | // print detailed matching information |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 25 | const debugMatches = false |
| 26 | |
| 27 | // force encoder to use predefined tables. |
| 28 | const forcePreDef = false |
| 29 | |
| 30 | // zstdMinMatch is the minimum zstd match length. |
| 31 | const zstdMinMatch = 3 |
| 32 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 33 | // Reset the buffer offset when reaching this. |
| 34 | const bufferReset = math.MaxInt32 - MaxWindowSize |
| 35 | |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 36 | var ( |
| 37 | // ErrReservedBlockType is returned when a reserved block type is found. |
| 38 | // Typically this indicates wrong or corrupted input. |
| 39 | ErrReservedBlockType = errors.New("invalid input: reserved block type encountered") |
| 40 | |
| 41 | // ErrCompressedSizeTooBig is returned when a block is bigger than allowed. |
| 42 | // Typically this indicates wrong or corrupted input. |
| 43 | ErrCompressedSizeTooBig = errors.New("invalid input: compressed size too big") |
| 44 | |
| 45 | // ErrBlockTooSmall is returned when a block is too small to be decoded. |
| 46 | // Typically returned on invalid input. |
| 47 | ErrBlockTooSmall = errors.New("block too small") |
| 48 | |
| 49 | // ErrMagicMismatch is returned when a "magic" number isn't what is expected. |
| 50 | // Typically this indicates wrong or corrupted input. |
| 51 | ErrMagicMismatch = errors.New("invalid input: magic number mismatch") |
| 52 | |
| 53 | // ErrWindowSizeExceeded is returned when a reference exceeds the valid window size. |
| 54 | // Typically this indicates wrong or corrupted input. |
| 55 | ErrWindowSizeExceeded = errors.New("window size exceeded") |
| 56 | |
| 57 | // ErrWindowSizeTooSmall is returned when no window size is specified. |
| 58 | // Typically this indicates wrong or corrupted input. |
| 59 | ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small") |
| 60 | |
| 61 | // ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit. |
| 62 | ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit") |
| 63 | |
| 64 | // ErrUnknownDictionary is returned if the dictionary ID is unknown. |
| 65 | // For the time being dictionaries are not supported. |
| 66 | ErrUnknownDictionary = errors.New("unknown dictionary") |
| 67 | |
| 68 | // ErrFrameSizeExceeded is returned if the stated frame size is exceeded. |
| 69 | // This is only returned if SingleSegment is specified on the frame. |
| 70 | ErrFrameSizeExceeded = errors.New("frame size exceeded") |
| 71 | |
| 72 | // ErrCRCMismatch is returned if CRC mismatches. |
| 73 | ErrCRCMismatch = errors.New("CRC check failed") |
| 74 | |
| 75 | // ErrDecoderClosed will be returned if the Decoder was used after |
| 76 | // Close has been called. |
| 77 | ErrDecoderClosed = errors.New("decoder used after Close") |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 78 | |
| 79 | // ErrDecoderNilInput is returned when a nil Reader was provided |
| 80 | // and an operation other than Reset/DecodeAll/Close was attempted. |
| 81 | ErrDecoderNilInput = errors.New("nil input provided as reader") |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 82 | ) |
| 83 | |
| 84 | func println(a ...interface{}) { |
| 85 | if debug { |
| 86 | log.Println(a...) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func printf(format string, a ...interface{}) { |
| 91 | if debug { |
| 92 | log.Printf(format, a...) |
| 93 | } |
| 94 | } |
| 95 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 96 | // matchLenFast does matching, but will not match the last up to 7 bytes. |
| 97 | func matchLenFast(a, b []byte) int { |
| 98 | endI := len(a) & (math.MaxInt32 - 7) |
| 99 | for i := 0; i < endI; i += 8 { |
| 100 | if diff := load64(a, i) ^ load64(b, i); diff != 0 { |
| 101 | return i + bits.TrailingZeros64(diff)>>3 |
| 102 | } |
| 103 | } |
| 104 | return endI |
| 105 | } |
| 106 | |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 107 | // matchLen returns the maximum length. |
| 108 | // a must be the shortest of the two. |
| 109 | // The function also returns whether all bytes matched. |
| 110 | func matchLen(a, b []byte) int { |
| 111 | b = b[:len(a)] |
| 112 | for i := 0; i < len(a)-7; i += 8 { |
| 113 | if diff := load64(a, i) ^ load64(b, i); diff != 0 { |
| 114 | return i + (bits.TrailingZeros64(diff) >> 3) |
| 115 | } |
| 116 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 117 | |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 118 | checked := (len(a) >> 3) << 3 |
| 119 | a = a[checked:] |
| 120 | b = b[checked:] |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 121 | for i := range a { |
| 122 | if a[i] != b[i] { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 123 | return i + checked |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | return len(a) + checked |
| 127 | } |
| 128 | |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 129 | func load3232(b []byte, i int32) uint32 { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 130 | return binary.LittleEndian.Uint32(b[i:]) |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | func load6432(b []byte, i int32) uint64 { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 134 | return binary.LittleEndian.Uint64(b[i:]) |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | func load64(b []byte, i int) uint64 { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 138 | return binary.LittleEndian.Uint64(b[i:]) |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 139 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 140 | |
| 141 | type byter interface { |
| 142 | Bytes() []byte |
| 143 | Len() int |
| 144 | } |
| 145 | |
| 146 | var _ byter = &bytes.Buffer{} |