cubefs

Форк
0
156 строк · 4.6 Кб
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
// Reset the buffer offset when reaching this.
40
const bufferReset = math.MaxInt32 - MaxWindowSize
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
	// ErrMagicMismatch is returned when a "magic" number isn't what is expected.
56
	// Typically this indicates wrong or corrupted input.
57
	ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
58

59
	// ErrWindowSizeExceeded is returned when a reference exceeds the valid window size.
60
	// Typically this indicates wrong or corrupted input.
61
	ErrWindowSizeExceeded = errors.New("window size exceeded")
62

63
	// ErrWindowSizeTooSmall is returned when no window size is specified.
64
	// Typically this indicates wrong or corrupted input.
65
	ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small")
66

67
	// ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit.
68
	ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit")
69

70
	// ErrUnknownDictionary is returned if the dictionary ID is unknown.
71
	// For the time being dictionaries are not supported.
72
	ErrUnknownDictionary = errors.New("unknown dictionary")
73

74
	// ErrFrameSizeExceeded is returned if the stated frame size is exceeded.
75
	// This is only returned if SingleSegment is specified on the frame.
76
	ErrFrameSizeExceeded = errors.New("frame size exceeded")
77

78
	// ErrFrameSizeMismatch is returned if the stated frame size does not match the expected size.
79
	// This is only returned if SingleSegment is specified on the frame.
80
	ErrFrameSizeMismatch = errors.New("frame size does not match size on stream")
81

82
	// ErrCRCMismatch is returned if CRC mismatches.
83
	ErrCRCMismatch = errors.New("CRC check failed")
84

85
	// ErrDecoderClosed will be returned if the Decoder was used after
86
	// Close has been called.
87
	ErrDecoderClosed = errors.New("decoder used after Close")
88

89
	// ErrDecoderNilInput is returned when a nil Reader was provided
90
	// and an operation other than Reset/DecodeAll/Close was attempted.
91
	ErrDecoderNilInput = errors.New("nil input provided as reader")
92
)
93

94
func println(a ...interface{}) {
95
	if debug || debugDecoder || debugEncoder {
96
		log.Println(a...)
97
	}
98
}
99

100
func printf(format string, a ...interface{}) {
101
	if debug || debugDecoder || debugEncoder {
102
		log.Printf(format, a...)
103
	}
104
}
105

106
// matchLenFast does matching, but will not match the last up to 7 bytes.
107
func matchLenFast(a, b []byte) int {
108
	endI := len(a) & (math.MaxInt32 - 7)
109
	for i := 0; i < endI; i += 8 {
110
		if diff := load64(a, i) ^ load64(b, i); diff != 0 {
111
			return i + bits.TrailingZeros64(diff)>>3
112
		}
113
	}
114
	return endI
115
}
116

117
// matchLen returns the maximum length.
118
// a must be the shortest of the two.
119
// The function also returns whether all bytes matched.
120
func matchLen(a, b []byte) int {
121
	b = b[:len(a)]
122
	for i := 0; i < len(a)-7; i += 8 {
123
		if diff := load64(a, i) ^ load64(b, i); diff != 0 {
124
			return i + (bits.TrailingZeros64(diff) >> 3)
125
		}
126
	}
127

128
	checked := (len(a) >> 3) << 3
129
	a = a[checked:]
130
	b = b[checked:]
131
	for i := range a {
132
		if a[i] != b[i] {
133
			return i + checked
134
		}
135
	}
136
	return len(a) + checked
137
}
138

139
func load3232(b []byte, i int32) uint32 {
140
	return binary.LittleEndian.Uint32(b[i:])
141
}
142

143
func load6432(b []byte, i int32) uint64 {
144
	return binary.LittleEndian.Uint64(b[i:])
145
}
146

147
func load64(b []byte, i int) uint64 {
148
	return binary.LittleEndian.Uint64(b[i:])
149
}
150

151
type byter interface {
152
	Bytes() []byte
153
	Len() int
154
}
155

156
var _ byter = &bytes.Buffer{}
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.