github.com/bir3/gocompiler@v0.9.2202/extra/compress/zstd/zstd.go (about) 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" 8 "encoding/binary" 9 "errors" 10 "log" 11 "math" 12 "math/bits" 13 ) 14 15 // enable debug printing 16 const debug = false 17 18 // enable encoding debug printing 19 const debugEncoder = debug 20 21 // enable decoding debug printing 22 const debugDecoder = debug 23 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 39 // fcsUnknown is used for unknown frame content size. 40 const fcsUnknown = math.MaxUint64 41 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 55 // ErrUnexpectedBlockSize is returned when a block has unexpected size. 56 // Typically returned on invalid input. 57 ErrUnexpectedBlockSize = errors.New("unexpected block size") 58 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. 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 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 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 92 // ErrDecoderNilInput is returned when a nil Reader was provided 93 // and an operation other than Reset/DecodeAll/Close was attempted. 94 ErrDecoderNilInput = errors.New("nil input provided as reader") 95 ) 96 97 func println(a ...interface{}) { 98 if debug || debugDecoder || debugEncoder { 99 log.Println(a...) 100 } 101 } 102 103 func printf(format string, a ...interface{}) { 104 if debug || debugDecoder || debugEncoder { 105 log.Printf(format, a...) 106 } 107 } 108 109 // matchLen returns the maximum common prefix length of a and b. 110 // a must be the shortest of the two. 111 func matchLen(a, b []byte) (n int) { 112 for ; len(a) >= 8 && len(b) >= 8; a, b = a[8:], b[8:] { 113 diff := binary.LittleEndian.Uint64(a) ^ binary.LittleEndian.Uint64(b) 114 if diff != 0 { 115 return n + bits.TrailingZeros64(diff)>>3 116 } 117 n += 8 118 } 119 120 for i := range a { 121 if a[i] != b[i] { 122 break 123 } 124 n++ 125 } 126 return n 127 128 } 129 130 func load3232(b []byte, i int32) uint32 { 131 return binary.LittleEndian.Uint32(b[:len(b):len(b)][i:]) 132 } 133 134 func load6432(b []byte, i int32) uint64 { 135 return binary.LittleEndian.Uint64(b[:len(b):len(b)][i:]) 136 } 137 138 type byter interface { 139 Bytes() []byte 140 Len() int 141 } 142 143 var _ byter = &bytes.Buffer{}