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