-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
451 lines (407 loc) · 9.17 KB
/
writer.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package goheatshrink
import (
"bufio"
"errors"
"io"
"log"
)
type writer struct {
*config
inputSize int
matchScanIndex int
matchLength int
matchPosition int
outgoingBits int
outgoingBitsCount uint8
flags encodeFlags
state encodeState
current byte
bitIndex uint8
buffer []byte
index []int16
inner inner
outputTotal int
}
type inner interface {
io.ByteWriter
Flush() error
}
type encodeFlags int
const (
encodeFlagsNone encodeFlags = 0
encodeFlagsFinishing = 1
)
type encodeState int
const (
encodeStateNotFull encodeState = iota
encodeStateFilled
encodeStateSearch
encodeStateYieldTagBit
encodeStateYieldLiteral
encodeStateYieldBackRefIndex
encodeStateYieldBackRefLength
encodeStateSaveBacklog
encodeStateFlushBits
encodeStateDone
encodeStateInvalid
)
const heatshrinkLiteralMarker byte = 0x01
const heatshrinkBackrefMarker byte = 0x00
// NewWriterConfig creates a new io.WriteCloser. Writes to the returned io.WriteCloser are compressed and written to w.
//
// config specifies the configuration values to use when compressing
//
// It is the caller's responsibility to call Close on the io.WriteCloser when done. Writes may be buffered and not flushed until Close.
func NewWriter(w io.Writer, options ...func(*config)) io.WriteCloser {
hw := &writer{
config: &config{window:defaultWindow, lookahead:defaultLookahead},
state: encodeStateNotFull,
bitIndex: 0x80,
}
for _, option := range options {
option(hw.config)
}
bufSize := 2 << hw.window
bw, ok := w.(inner)
if !ok {
bw = bufio.NewWriter(w)
}
hw.inner = bw
hw.buffer = make([]byte, bufSize)
hw.index = make([]int16, bufSize)
return hw
}
func (w *writer) Write(p []byte) (n int, err error) {
var done int
total := len(p)
for {
if len(p) > 0 {
inputSize, err := w.sink(p)
done += int(inputSize)
if err != nil {
return done, err
}
p = p[inputSize:]
}
outputSize, err := w.poll()
if err != nil {
return done, err
}
if done >= total {
break
}
if w.state == encodeStateNotFull {
continue
}
if outputSize == 0 {
if w.finish() {
return done, nil
}
}
}
return done, nil
}
func (w *writer) Close() error {
var err error
if w.finish() {
err = w.inner.Flush()
if err != nil {
return err
}
return nil
}
_, err = w.poll()
if err != nil {
return err
}
if w.finish() {
err = w.inner.Flush()
if err != nil {
return err
}
return nil
}
return ErrBadStateOnClose
}
func (w *writer) sink(in []byte) (int, error) {
if w.isFinishing() {
return 0, errors.New("sinking while finishing")
}
if w.state != encodeStateNotFull {
return 0, errors.New("sinking while processing")
}
offset := w.getInputBufferSize() + w.inputSize
ibs := w.getInputBufferSize()
remaining := ibs - w.inputSize
var copySize int
if remaining < len(in) {
copySize = remaining
} else {
copySize = len(in)
}
copy(w.buffer[offset:], in[:copySize])
w.inputSize += copySize
if copySize == remaining {
w.state = encodeStateFilled
}
return copySize, nil
}
func (w *writer) poll() (int, error) {
w.outputTotal = 0
var err error
for {
state := w.state
switch state {
case encodeStateNotFull:
return w.outputTotal, nil
case encodeStateFilled:
w.doIndexing()
w.state = encodeStateSearch
case encodeStateSearch:
w.state = w.stateStepSearch()
case encodeStateYieldTagBit:
w.state, err = w.stateYieldTagBit()
case encodeStateYieldLiteral:
w.state, err = w.stateYieldLiteral()
case encodeStateYieldBackRefIndex:
w.state, err = w.stateYieldBackRefIndex()
case encodeStateYieldBackRefLength:
w.state, err = w.stateYieldBackRefLength()
case encodeStateSaveBacklog:
w.state = w.stateSaveBacklog()
case encodeStateFlushBits:
w.state, err = w.stateFlushBitBuffer()
case encodeStateDone:
return w.outputTotal, nil
case encodeStateInvalid:
log.Fatal("Invalid state: %v", state)
default:
log.Fatal("Unknown state: %v", state)
}
if err != nil {
return w.outputTotal, err
}
}
}
func (w *writer) finish() bool {
w.flags |= encodeFlagsFinishing
if w.state == encodeStateNotFull {
w.state = encodeStateFilled
}
return w.state == encodeStateDone
}
func (w *writer) stateStepSearch() encodeState {
windowLength := 1 << w.window
lookaheadLength := 1 << w.lookahead
msi := w.matchScanIndex
fin := w.isFinishing()
var lookaheadCompare int
if fin {
lookaheadCompare = 1
} else {
lookaheadCompare = lookaheadLength
}
if msi > w.inputSize-lookaheadCompare {
if fin {
return encodeStateFlushBits
}
return encodeStateSaveBacklog
}
ibs := w.getInputBufferSize()
end := ibs + msi
start := end - windowLength
maxPossible := lookaheadLength
if w.inputSize-msi < lookaheadLength {
maxPossible = w.inputSize - msi
}
matchPos, matchLength := w.findLongestMatch(start, end, maxPossible)
if matchPos == matchNotFound {
w.matchScanIndex++
w.matchLength = 0
return encodeStateYieldTagBit
}
w.matchPosition = matchPos
w.matchLength = matchLength
if matchPos < (1 << w.window) {
}
return encodeStateYieldTagBit
}
func (w *writer) stateYieldTagBit() (encodeState, error) {
if w.matchLength == 0 {
err := w.addTagBit(heatshrinkLiteralMarker)
if err != nil {
return encodeStateInvalid, err
}
return encodeStateYieldLiteral, nil
}
err := w.addTagBit(heatshrinkBackrefMarker)
if err != nil {
return encodeStateInvalid, err
}
w.outgoingBits = w.matchPosition - 1
w.outgoingBitsCount = w.window
return encodeStateYieldBackRefIndex, nil
}
func (w *writer) stateYieldLiteral() (encodeState, error) {
err := w.pushLiteralByte()
if err != nil {
return encodeStateInvalid, err
}
return encodeStateSearch, nil
}
func (w *writer) stateYieldBackRefIndex() (encodeState, error) {
count, err := w.pushOutgoingBits()
if err != nil {
return encodeStateInvalid, err
}
if count > 0 {
return encodeStateYieldBackRefIndex, nil
}
w.outgoingBits = w.matchLength - 1
w.outgoingBitsCount = w.lookahead
return encodeStateYieldBackRefLength, nil
}
func (w *writer) stateYieldBackRefLength() (encodeState, error) {
count, err := w.pushOutgoingBits()
if err != nil {
return encodeStateInvalid, err
}
if count > 0 {
return encodeStateYieldBackRefLength, nil
}
w.matchScanIndex += w.matchLength
w.matchLength = 0
return encodeStateSearch, nil
}
func (w *writer) stateSaveBacklog() encodeState {
w.saveBacklog()
return encodeStateNotFull
}
func (w *writer) stateFlushBitBuffer() (encodeState, error) {
if w.bitIndex == 0x80 {
return encodeStateDone, nil
}
err := w.inner.WriteByte(w.current)
if err != nil {
return encodeStateInvalid, err
}
w.outputTotal++
return encodeStateDone, nil
}
const matchNotFound = ^int(0)
func (w *writer) findLongestMatch(start int, end int, max int) (int, int) {
var matchMaxLength int
var matchIndex = matchNotFound
var len int
needlepoint := w.buffer[end:]
pos := w.index[end]
for pos-int16(start) >= 0 {
pospoint := w.buffer[pos:]
len = 0
if pospoint[matchMaxLength] != needlepoint[matchMaxLength] {
pos = w.index[pos]
continue
}
for len = 1; len < max; len++ {
if pospoint[len] != needlepoint[len] {
break
}
}
if len > matchMaxLength {
matchMaxLength = len
matchIndex = int(pos)
if len == max {
break
}
}
pos = w.index[pos]
}
breakEven := 1 + w.window + w.lookahead
if 8*uint(matchMaxLength) > uint(breakEven) {
return end - matchIndex, matchMaxLength
}
return matchNotFound, 0
}
func (w *writer) pushLiteralByte() error {
processedOffset := w.matchScanIndex - 1
inputOffset := w.getInputBufferSize() + processedOffset
b := w.buffer[inputOffset]
return w.pushBits(8, b)
}
func (w *writer) pushBits(count uint8, bits byte) error {
if count == 8 && w.bitIndex == 0x80 {
err := w.inner.WriteByte(bits)
if err == nil {
w.outputTotal++
}
return err
}
var i int16
var err error
for i = int16(count) - 1; i >= 0; i-- {
bit := bits & (1 << uint16(i))
if bit > 0 {
w.current |= w.bitIndex
}
w.bitIndex >>= 1
if w.bitIndex == 0 {
w.bitIndex = 0x80
err = w.inner.WriteByte(w.current)
if err != nil {
return err
}
w.outputTotal++
w.current = 0x0
}
}
return nil
}
func (w *writer) pushOutgoingBits() (uint8, error) {
var count uint8
var bits byte
if w.outgoingBitsCount > 8 {
count = 8
bits = byte(w.outgoingBits >> (w.outgoingBitsCount - 8))
} else {
count = w.outgoingBitsCount
bits = byte(w.outgoingBits)
}
if count > 0 {
err := w.pushBits(count, bits)
if err != nil {
return 0, err
}
w.outgoingBitsCount -= count
}
return count, nil
}
func (w *writer) addTagBit(tag byte) error {
return w.pushBits(1, tag)
}
func (w *writer) saveBacklog() {
msi := w.matchScanIndex
copy(w.buffer, w.buffer[msi:])
w.matchScanIndex = 0
w.inputSize -= msi
}
func (w *writer) getInputBufferSize() int {
return 1 << w.window
}
func (w *writer) doIndexing() {
var last [256]int16
for i := range last {
last[i] = -1
}
ibs := w.getInputBufferSize()
end := ibs + w.inputSize
var i int
for i = 0; i < end; i++ {
v := w.buffer[i]
lv := last[v]
w.index[i] = lv
last[v] = int16(i)
}
}
func (w *writer) isFinishing() bool {
return w.flags&encodeFlagsFinishing == encodeFlagsFinishing
}