-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
344 lines (313 loc) · 7.22 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package goheatshrink
import (
"io"
"log"
)
// ReadResetter groups an io.Reader with a Reset method, which can switch to a new underlying io.Reader.
// This permits reusing a io.Reader instead of allocating a new one.
type ReadResetter interface {
io.Reader
// Reset discards any buffered data and resets the Resetter as if it was
// newly initialized with the given reader.
Reset(r io.Reader)
}
type reader struct {
*config
inner io.Reader
inputBuffer []byte
inputSize int
inputIndex int
headIndex int
state decodeState
current byte
bitIndex byte
buffer []byte
windowBuffer []byte
bufferSize int
outputCount int
outputBackRefIndex int
}
type decodeState int
const (
decodeStateTagBit decodeState = iota
decodeStateYieldLiteral
decodeStateBackRefIndexMSB
decodeStateBackRefIndexLSB
decodeStateBackRefCountMSB
decodeStateBackRefCountLSB
decodeStateYieldBackRef
)
// NewReaderConfig creates a new ReadResetter reading the given io.Reader.
//
// options modifies the default configuration values to use when decompressing
func NewReader(r io.Reader, options ...func(*config)) ReadResetter {
hr := &reader{
config: &config{window:defaultWindow, lookahead:defaultLookahead},
inner: r,
state: decodeStateTagBit,
}
for _, option := range options {
option(hr.config)
}
hr.windowBuffer = make([]byte, 1<<hr.window)
hr.inputBuffer = make([]byte, 1<<hr.window)
return hr;
}
func (r *reader) Read(out []byte) (int, error) {
if len(out) == 0 {
return 0, nil
}
var in []byte
if r.inputSize > 0 {
// Still have leftover bytes from last read
in = r.inputBuffer[r.inputSize:]
} else {
in = r.inputBuffer
}
count, err := r.inner.Read(in)
r.inputSize += count
if r.inputSize > 0 {
return r.decodeRead(r.inputBuffer[:r.inputSize], out)
} else if err != nil {
if err == io.EOF {
if r.finish() {
return 0, io.EOF
}
return 0, ErrTruncated
}
return 0, err
}
return 0, nil
}
// Reset clears the state of the Reader r such that it is equivalent to its initial state
func (r *reader) Reset(new io.Reader) {
for i := range r.buffer {
r.buffer[i] = 0
}
for i := range r.windowBuffer {
r.windowBuffer[i] = 0
}
for i := range r.inputBuffer {
r.inputBuffer[i] = 0
}
r.state = decodeStateTagBit
r.inputIndex = 0
r.inputSize = 0
r.bitIndex = 0
r.current = 0x0
r.outputCount = 0
r.outputBackRefIndex = 0
r.headIndex = 0
r.inner = new
}
type output struct {
buf []byte
size int
index int
}
func (o *output) push(b byte) {
o.buf[o.index] = b
o.index++
}
func (r *reader) decodeRead(in []byte, out []byte) (int, error) {
totalout := 0
r.buffer = in
r.inputSize = len(in)
o := &output{
buf: out,
size: len(out),
index: 0,
}
for {
outputSize, err := r.poll(o)
totalout += outputSize
if err == errOutputBufferFull {
return totalout, nil
} else if err != nil {
return totalout, err
}
if r.finish() {
return totalout, nil
}
}
return totalout, ErrTruncated
}
func (r *reader) poll(o *output) (int, error) {
o.index = 0
for {
state := r.state
switch state {
case decodeStateTagBit:
r.state = r.stateTagBit()
case decodeStateYieldLiteral:
r.state = r.stateYieldLiteral(o)
case decodeStateBackRefIndexMSB:
r.state = r.stateBackRefIndexMSB()
case decodeStateBackRefIndexLSB:
r.state = r.stateBackRefIndexLSB()
case decodeStateBackRefCountMSB:
r.state = r.stateBackRefCountMSB()
case decodeStateBackRefCountLSB:
r.state = r.stateBackRefCountLSB()
case decodeStateYieldBackRef:
r.state = r.stateYieldBackRef(o)
default:
log.Fatal("Unknown state: %v", state)
}
if r.state == state {
if o.index == cap(o.buf) {
return o.index, errOutputBufferFull
}
return o.index, nil
}
}
}
func (r *reader) stateTagBit() decodeState {
bits, err := r.getBits(1)
if err == errNoBitsAvailable {
return decodeStateTagBit
}
if bits > 0 {
return decodeStateYieldLiteral
}
if r.window > 8 {
return decodeStateBackRefIndexMSB
}
r.outputBackRefIndex = 0
return decodeStateBackRefIndexLSB
}
func (r *reader) stateYieldLiteral(o *output) decodeState {
if o.index < o.size {
bits, err := r.getBits(8)
if err == errNoBitsAvailable {
return decodeStateYieldLiteral
}
var mask uint16 = (1 << r.window) - 1
c := byte(bits & 0xFF)
r.windowBuffer[uint16(r.headIndex)&mask] = c
r.headIndex++
o.push(c)
return decodeStateTagBit
}
return decodeStateYieldLiteral
}
func (r *reader) stateBackRefIndexMSB() decodeState {
bitCount := r.window
bits, err := r.getBits(bitCount - 8)
if err == errNoBitsAvailable {
return decodeStateBackRefIndexMSB
}
r.outputBackRefIndex = int(bits) << 8
return decodeStateBackRefIndexLSB
}
func (r *reader) stateBackRefIndexLSB() decodeState {
bitCount := r.window
var bits uint16
var err error
if bitCount < 8 {
bits, err = r.getBits(bitCount)
} else {
bits, err = r.getBits(8)
}
if err == errNoBitsAvailable {
return decodeStateBackRefIndexLSB
}
r.outputBackRefIndex |= int(bits)
r.outputBackRefIndex++
backRefBitCount := r.lookahead
r.outputCount = 0
if backRefBitCount > 8 {
return decodeStateBackRefCountMSB
}
return decodeStateBackRefCountLSB
}
func (r *reader) stateBackRefCountMSB() decodeState {
backRefBitCount := r.lookahead
bits, err := r.getBits(backRefBitCount - 8)
if err == errNoBitsAvailable {
return decodeStateBackRefCountMSB
}
r.outputCount = int(bits) << 8
return decodeStateBackRefCountLSB
}
func (r *reader) stateBackRefCountLSB() decodeState {
backRefBitCount := r.lookahead
var bits uint16
var err error
if backRefBitCount < 8 {
bits, err = r.getBits(backRefBitCount)
} else {
bits, err = r.getBits(8)
}
if err == errNoBitsAvailable {
return decodeStateBackRefCountLSB
}
r.outputCount |= int(bits)
r.outputCount++
return decodeStateYieldBackRef
}
func (r *reader) stateYieldBackRef(o *output) decodeState {
count := o.size - o.index
if count > 0 {
if r.outputCount < count {
count = r.outputCount
}
mask := (1 << r.window) - 1
negOffset := r.outputBackRefIndex
for i := 0; i < count; i++ {
c := r.windowBuffer[(r.headIndex-negOffset)&mask]
o.push(c)
r.windowBuffer[r.headIndex&mask] = c
r.headIndex++
}
r.outputCount -= count
if r.outputCount == 0 {
return decodeStateTagBit
}
}
return decodeStateYieldBackRef
}
func (r *reader) getBits(count uint8) (uint16, error) {
var accumulator uint16
if count > 15 {
return 0, errNoBitsAvailable
}
if r.inputSize == 0 {
if r.bitIndex < (1 << (count - 1)) {
return 0, errNoBitsAvailable
}
}
var i uint8
for i = 0; i < count; i++ {
if r.bitIndex == 0x0 {
if r.inputSize == 0 {
return 0, errNoBitsAvailable
}
r.current = r.buffer[r.inputIndex]
r.inputIndex++
if r.inputIndex == r.inputSize {
r.inputIndex = 0
r.inputSize = 0
}
r.bitIndex = 0x80
}
accumulator <<= 1
if r.current&r.bitIndex > 0 {
accumulator |= 0x01
}
r.bitIndex >>= 1
}
return accumulator, nil
}
func (r *reader) finish() bool {
switch r.state {
case decodeStateTagBit, decodeStateBackRefIndexLSB, decodeStateBackRefIndexMSB, decodeStateBackRefCountLSB, decodeStateBackRefCountMSB, decodeStateYieldLiteral:
if r.inputSize == 0 {
return true
}
return false
case decodeStateYieldBackRef:
return false
}
return false
}