| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [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 ( |
| 7 | "bytes" |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 8 | "errors" |
| 9 | "log" |
| 10 | "math" |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 11 | |
| 12 | "github.com/klauspost/compress/internal/le" |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | // enable debug printing |
| 16 | const debug = false |
| 17 | |
| Akash Reddy Kankanala | cf04537 | 2025-06-10 14:11:24 +0530 | [diff] [blame] | 18 | // enable encoding debug printing |
| 19 | const debugEncoder = debug |
| 20 | |
| 21 | // enable decoding debug printing |
| 22 | const debugDecoder = debug |
| 23 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 24 | // Enable extra assertions. |
| 25 | const debugAsserts = debug || false |
| 26 | |
| 27 | // print sequence details |
| 28 | const debugSequences = false |
| 29 | |
| 30 | // print detailed matching information |
| 31 | const debugMatches = false |
| 32 | |
| 33 | // force encoder to use predefined tables. |
| 34 | const forcePreDef = false |
| 35 | |
| 36 | // zstdMinMatch is the minimum zstd match length. |
| 37 | const zstdMinMatch = 3 |
| 38 | |
| Akash Reddy Kankanala | cf04537 | 2025-06-10 14:11:24 +0530 | [diff] [blame] | 39 | // fcsUnknown is used for unknown frame content size. |
| 40 | const fcsUnknown = math.MaxUint64 |
| 41 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 42 | var ( |
| 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 Kankanala | cf04537 | 2025-06-10 14:11:24 +0530 | [diff] [blame] | 55 | // ErrUnexpectedBlockSize is returned when a block has unexpected size. |
| 56 | // Typically returned on invalid input. |
| 57 | ErrUnexpectedBlockSize = errors.New("unexpected block size") |
| 58 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 59 | // 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. |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 75 | 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 Kankanala | cf04537 | 2025-06-10 14:11:24 +0530 | [diff] [blame] | 81 | // 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 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 85 | // 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 Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 92 | // ErrEncoderClosed will be returned if the Encoder was used after |
| 93 | // Close has been called. |
| 94 | ErrEncoderClosed = errors.New("encoder used after Close") |
| 95 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 96 | // 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 | |
| 101 | func println(a ...interface{}) { |
| Akash Reddy Kankanala | cf04537 | 2025-06-10 14:11:24 +0530 | [diff] [blame] | 102 | if debug || debugDecoder || debugEncoder { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 103 | log.Println(a...) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func printf(format string, a ...interface{}) { |
| Akash Reddy Kankanala | cf04537 | 2025-06-10 14:11:24 +0530 | [diff] [blame] | 108 | if debug || debugDecoder || debugEncoder { |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 109 | log.Printf(format, a...) |
| 110 | } |
| 111 | } |
| 112 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 113 | func load3232(b []byte, i int32) uint32 { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 114 | return le.Load32(b, i) |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | func load6432(b []byte, i int32) uint64 { |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 118 | return le.Load64(b, i) |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | type byter interface { |
| 122 | Bytes() []byte |
| 123 | Len() int |
| 124 | } |
| 125 | |
| 126 | var _ byter = &bytes.Buffer{} |