1
2
3
4
5
6 package flate
7
8 import (
9 "encoding/binary"
10 "errors"
11 "fmt"
12 "io"
13 "math"
14 )
15
16 const (
17 NoCompression = 0
18 BestSpeed = 1
19 BestCompression = 9
20 DefaultCompression = -1
21
22
23
24
25
26
27
28
29
30
31 HuffmanOnly = -2
32 ConstantCompression = HuffmanOnly
33
34 logWindowSize = 15
35 windowSize = 1 << logWindowSize
36 windowMask = windowSize - 1
37 logMaxOffsetSize = 15
38 minMatchLength = 4
39 maxMatchLength = 258
40 minOffsetSize = 1
41
42
43
44
45
46 maxFlateBlockTokens = 1 << 15
47 maxStoreBlockSize = 65535
48 hashBits = 17
49 hashSize = 1 << hashBits
50 hashMask = (1 << hashBits) - 1
51 hashShift = (hashBits + minMatchLength - 1) / minMatchLength
52 maxHashOffset = 1 << 28
53
54 skipNever = math.MaxInt32
55
56 debugDeflate = false
57 )
58
59 type compressionLevel struct {
60 good, lazy, nice, chain, fastSkipHashing, level int
61 }
62
63
64
65
66 var levels = []compressionLevel{
67 {},
68
69 {0, 0, 0, 0, 0, 1},
70 {0, 0, 0, 0, 0, 2},
71 {0, 0, 0, 0, 0, 3},
72 {0, 0, 0, 0, 0, 4},
73 {0, 0, 0, 0, 0, 5},
74 {0, 0, 0, 0, 0, 6},
75
76
77 {8, 12, 16, 24, skipNever, 7},
78 {16, 30, 40, 64, skipNever, 8},
79 {32, 258, 258, 1024, skipNever, 9},
80 }
81
82
83 type advancedState struct {
84
85 length int
86 offset int
87 maxInsertIndex int
88 chainHead int
89 hashOffset int
90
91 ii uint16
92
93
94 index int
95 hashMatch [maxMatchLength + minMatchLength]uint32
96
97
98
99
100
101
102 hashHead [hashSize]uint32
103 hashPrev [windowSize]uint32
104 }
105
106 type compressor struct {
107 compressionLevel
108
109 h *huffmanEncoder
110 w *huffmanBitWriter
111
112
113 fill func(*compressor, []byte) int
114 step func(*compressor)
115
116 window []byte
117 windowEnd int
118 blockStart int
119 err error
120
121
122 tokens tokens
123 fast fastEnc
124 state *advancedState
125
126 sync bool
127 byteAvailable bool
128 }
129
130 func (d *compressor) fillDeflate(b []byte) int {
131 s := d.state
132 if s.index >= 2*windowSize-(minMatchLength+maxMatchLength) {
133
134
135 *(*[windowSize]byte)(d.window) = *(*[windowSize]byte)(d.window[windowSize:])
136 s.index -= windowSize
137 d.windowEnd -= windowSize
138 if d.blockStart >= windowSize {
139 d.blockStart -= windowSize
140 } else {
141 d.blockStart = math.MaxInt32
142 }
143 s.hashOffset += windowSize
144 if s.hashOffset > maxHashOffset {
145 delta := s.hashOffset - 1
146 s.hashOffset -= delta
147 s.chainHead -= delta
148
149
150 for i, v := range s.hashPrev[:] {
151 if int(v) > delta {
152 s.hashPrev[i] = uint32(int(v) - delta)
153 } else {
154 s.hashPrev[i] = 0
155 }
156 }
157 for i, v := range s.hashHead[:] {
158 if int(v) > delta {
159 s.hashHead[i] = uint32(int(v) - delta)
160 } else {
161 s.hashHead[i] = 0
162 }
163 }
164 }
165 }
166 n := copy(d.window[d.windowEnd:], b)
167 d.windowEnd += n
168 return n
169 }
170
171 func (d *compressor) writeBlock(tok *tokens, index int, eof bool) error {
172 if index > 0 || eof {
173 var window []byte
174 if d.blockStart <= index {
175 window = d.window[d.blockStart:index]
176 }
177 d.blockStart = index
178
179 d.w.writeBlockDynamic(tok, eof, window, d.sync)
180 return d.w.err
181 }
182 return nil
183 }
184
185
186
187
188 func (d *compressor) writeBlockSkip(tok *tokens, index int, eof bool) error {
189 if index > 0 || eof {
190 if d.blockStart <= index {
191 window := d.window[d.blockStart:index]
192
193
194 if int(tok.n) > len(window)-int(tok.n>>6) {
195 d.w.writeBlockHuff(eof, window, d.sync)
196 } else {
197
198 d.w.writeBlockDynamic(tok, eof, window, d.sync)
199 }
200 } else {
201 d.w.writeBlock(tok, eof, nil)
202 }
203 d.blockStart = index
204 return d.w.err
205 }
206 return nil
207 }
208
209
210
211
212
213 func (d *compressor) fillWindow(b []byte) {
214
215 if d.level <= 0 && d.level > -MinCustomWindowSize {
216 return
217 }
218 if d.fast != nil {
219
220 if len(b) > maxMatchOffset {
221 b = b[len(b)-maxMatchOffset:]
222 }
223 d.fast.Encode(&d.tokens, b)
224 d.tokens.Reset()
225 return
226 }
227 s := d.state
228
229 if len(b) > windowSize {
230 b = b[len(b)-windowSize:]
231 }
232
233 n := copy(d.window[d.windowEnd:], b)
234
235
236 loops := (n + 256 - minMatchLength) / 256
237 for j := 0; j < loops; j++ {
238 startindex := j * 256
239 end := startindex + 256 + minMatchLength - 1
240 if end > n {
241 end = n
242 }
243 tocheck := d.window[startindex:end]
244 dstSize := len(tocheck) - minMatchLength + 1
245
246 if dstSize <= 0 {
247 continue
248 }
249
250 dst := s.hashMatch[:dstSize]
251 bulkHash4(tocheck, dst)
252 var newH uint32
253 for i, val := range dst {
254 di := i + startindex
255 newH = val & hashMask
256
257
258 s.hashPrev[di&windowMask] = s.hashHead[newH]
259
260 s.hashHead[newH] = uint32(di + s.hashOffset)
261 }
262 }
263
264 d.windowEnd += n
265 s.index = n
266 }
267
268
269
270
271 func (d *compressor) findMatch(pos int, prevHead int, lookahead int) (length, offset int, ok bool) {
272 minMatchLook := maxMatchLength
273 if lookahead < minMatchLook {
274 minMatchLook = lookahead
275 }
276
277 win := d.window[0 : pos+minMatchLook]
278
279
280 nice := len(win) - pos
281 if d.nice < nice {
282 nice = d.nice
283 }
284
285
286 tries := d.chain
287 length = minMatchLength - 1
288
289 wEnd := win[pos+length]
290 wPos := win[pos:]
291 minIndex := pos - windowSize
292 if minIndex < 0 {
293 minIndex = 0
294 }
295 offset = 0
296
297 if d.chain < 100 {
298 for i := prevHead; tries > 0; tries-- {
299 if wEnd == win[i+length] {
300 n := matchLen(win[i:i+minMatchLook], wPos)
301 if n > length {
302 length = n
303 offset = pos - i
304 ok = true
305 if n >= nice {
306
307 break
308 }
309 wEnd = win[pos+n]
310 }
311 }
312 if i <= minIndex {
313
314 break
315 }
316 i = int(d.state.hashPrev[i&windowMask]) - d.state.hashOffset
317 if i < minIndex {
318 break
319 }
320 }
321 return
322 }
323
324
325 cGain := 4
326
327
328 const baseCost = 3
329
330
331
332 for i := prevHead; tries > 0; tries-- {
333 if wEnd == win[i+length] {
334 n := matchLen(win[i:i+minMatchLook], wPos)
335 if n > length {
336
337 newGain := d.h.bitLengthRaw(wPos[:n]) - int(offsetExtraBits[offsetCode(uint32(pos-i))]) - baseCost - int(lengthExtraBits[lengthCodes[(n-3)&255]])
338
339
340 if newGain > cGain {
341 length = n
342 offset = pos - i
343 cGain = newGain
344 ok = true
345 if n >= nice {
346
347 break
348 }
349 wEnd = win[pos+n]
350 }
351 }
352 }
353 if i <= minIndex {
354
355 break
356 }
357 i = int(d.state.hashPrev[i&windowMask]) - d.state.hashOffset
358 if i < minIndex {
359 break
360 }
361 }
362 return
363 }
364
365 func (d *compressor) writeStoredBlock(buf []byte) error {
366 if d.w.writeStoredHeader(len(buf), false); d.w.err != nil {
367 return d.w.err
368 }
369 d.w.writeBytes(buf)
370 return d.w.err
371 }
372
373
374
375
376 func hash4(b []byte) uint32 {
377 return hash4u(binary.LittleEndian.Uint32(b), hashBits)
378 }
379
380
381
382 func hash4u(u uint32, h uint8) uint32 {
383 return (u * prime4bytes) >> (32 - h)
384 }
385
386
387
388 func bulkHash4(b []byte, dst []uint32) {
389 if len(b) < 4 {
390 return
391 }
392 hb := binary.LittleEndian.Uint32(b)
393
394 dst[0] = hash4u(hb, hashBits)
395 end := len(b) - 4 + 1
396 for i := 1; i < end; i++ {
397 hb = (hb >> 8) | uint32(b[i+3])<<24
398 dst[i] = hash4u(hb, hashBits)
399 }
400 }
401
402 func (d *compressor) initDeflate() {
403 d.window = make([]byte, 2*windowSize)
404 d.byteAvailable = false
405 d.err = nil
406 if d.state == nil {
407 return
408 }
409 s := d.state
410 s.index = 0
411 s.hashOffset = 1
412 s.length = minMatchLength - 1
413 s.offset = 0
414 s.chainHead = -1
415 }
416
417
418
419 func (d *compressor) deflateLazy() {
420 s := d.state
421
422
423
424 const sanity = debugDeflate
425
426 if d.windowEnd-s.index < minMatchLength+maxMatchLength && !d.sync {
427 return
428 }
429 if d.windowEnd != s.index && d.chain > 100 {
430
431 if d.h == nil {
432 d.h = newHuffmanEncoder(maxFlateBlockTokens)
433 }
434 var tmp [256]uint16
435 for _, v := range d.window[s.index:d.windowEnd] {
436 tmp[v]++
437 }
438 d.h.generate(tmp[:], 15)
439 }
440
441 s.maxInsertIndex = d.windowEnd - (minMatchLength - 1)
442
443 for {
444 if sanity && s.index > d.windowEnd {
445 panic("index > windowEnd")
446 }
447 lookahead := d.windowEnd - s.index
448 if lookahead < minMatchLength+maxMatchLength {
449 if !d.sync {
450 return
451 }
452 if sanity && s.index > d.windowEnd {
453 panic("index > windowEnd")
454 }
455 if lookahead == 0 {
456
457 if d.byteAvailable {
458
459 d.tokens.AddLiteral(d.window[s.index-1])
460 d.byteAvailable = false
461 }
462 if d.tokens.n > 0 {
463 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
464 return
465 }
466 d.tokens.Reset()
467 }
468 return
469 }
470 }
471 if s.index < s.maxInsertIndex {
472
473 hash := hash4(d.window[s.index:])
474 ch := s.hashHead[hash]
475 s.chainHead = int(ch)
476 s.hashPrev[s.index&windowMask] = ch
477 s.hashHead[hash] = uint32(s.index + s.hashOffset)
478 }
479 prevLength := s.length
480 prevOffset := s.offset
481 s.length = minMatchLength - 1
482 s.offset = 0
483 minIndex := s.index - windowSize
484 if minIndex < 0 {
485 minIndex = 0
486 }
487
488 if s.chainHead-s.hashOffset >= minIndex && lookahead > prevLength && prevLength < d.lazy {
489 if newLength, newOffset, ok := d.findMatch(s.index, s.chainHead-s.hashOffset, lookahead); ok {
490 s.length = newLength
491 s.offset = newOffset
492 }
493 }
494
495 if prevLength >= minMatchLength && s.length <= prevLength {
496
497
498
499
500 const checkOff = 2
501
502
503 if prevLength < maxMatchLength-checkOff {
504 prevIndex := s.index - 1
505 if prevIndex+prevLength < s.maxInsertIndex {
506 end := lookahead
507 if lookahead > maxMatchLength+checkOff {
508 end = maxMatchLength + checkOff
509 }
510 end += prevIndex
511
512
513 h := hash4(d.window[prevIndex+prevLength:])
514 ch2 := int(s.hashHead[h]) - s.hashOffset - prevLength
515 if prevIndex-ch2 != prevOffset && ch2 > minIndex+checkOff {
516 length := matchLen(d.window[prevIndex+checkOff:end], d.window[ch2+checkOff:])
517
518 if length > prevLength {
519 prevLength = length
520 prevOffset = prevIndex - ch2
521
522
523 for i := checkOff - 1; i >= 0; i-- {
524 if prevLength >= maxMatchLength || d.window[prevIndex+i] != d.window[ch2+i] {
525
526 for j := 0; j <= i; j++ {
527 d.tokens.AddLiteral(d.window[prevIndex+j])
528 if d.tokens.n == maxFlateBlockTokens {
529
530 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
531 return
532 }
533 d.tokens.Reset()
534 }
535 s.index++
536 if s.index < s.maxInsertIndex {
537 h := hash4(d.window[s.index:])
538 ch := s.hashHead[h]
539 s.chainHead = int(ch)
540 s.hashPrev[s.index&windowMask] = ch
541 s.hashHead[h] = uint32(s.index + s.hashOffset)
542 }
543 }
544 break
545 } else {
546 prevLength++
547 }
548 }
549 } else if false {
550
551
552 prevIndex++
553 h := hash4(d.window[prevIndex+prevLength:])
554 ch2 := int(s.hashHead[h]) - s.hashOffset - prevLength
555 if prevIndex-ch2 != prevOffset && ch2 > minIndex+checkOff {
556 length := matchLen(d.window[prevIndex+checkOff:end], d.window[ch2+checkOff:])
557
558 if length > prevLength+checkOff {
559 prevLength = length
560 prevOffset = prevIndex - ch2
561 prevIndex--
562
563
564 for i := checkOff; i >= 0; i-- {
565 if prevLength >= maxMatchLength || d.window[prevIndex+i] != d.window[ch2+i-1] {
566
567 for j := 0; j <= i; j++ {
568 d.tokens.AddLiteral(d.window[prevIndex+j])
569 if d.tokens.n == maxFlateBlockTokens {
570
571 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
572 return
573 }
574 d.tokens.Reset()
575 }
576 s.index++
577 if s.index < s.maxInsertIndex {
578 h := hash4(d.window[s.index:])
579 ch := s.hashHead[h]
580 s.chainHead = int(ch)
581 s.hashPrev[s.index&windowMask] = ch
582 s.hashHead[h] = uint32(s.index + s.hashOffset)
583 }
584 }
585 break
586 } else {
587 prevLength++
588 }
589 }
590 }
591 }
592 }
593 }
594 }
595 }
596
597
598 d.tokens.AddMatch(uint32(prevLength-3), uint32(prevOffset-minOffsetSize))
599
600
601
602
603
604 newIndex := s.index + prevLength - 1
605
606 end := newIndex
607 if end > s.maxInsertIndex {
608 end = s.maxInsertIndex
609 }
610 end += minMatchLength - 1
611 startindex := s.index + 1
612 if startindex > s.maxInsertIndex {
613 startindex = s.maxInsertIndex
614 }
615 tocheck := d.window[startindex:end]
616 dstSize := len(tocheck) - minMatchLength + 1
617 if dstSize > 0 {
618 dst := s.hashMatch[:dstSize]
619 bulkHash4(tocheck, dst)
620 var newH uint32
621 for i, val := range dst {
622 di := i + startindex
623 newH = val & hashMask
624
625
626 s.hashPrev[di&windowMask] = s.hashHead[newH]
627
628 s.hashHead[newH] = uint32(di + s.hashOffset)
629 }
630 }
631
632 s.index = newIndex
633 d.byteAvailable = false
634 s.length = minMatchLength - 1
635 if d.tokens.n == maxFlateBlockTokens {
636
637 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
638 return
639 }
640 d.tokens.Reset()
641 }
642 s.ii = 0
643 } else {
644
645 if s.length >= minMatchLength {
646 s.ii = 0
647 }
648
649 if d.byteAvailable {
650 s.ii++
651 d.tokens.AddLiteral(d.window[s.index-1])
652 if d.tokens.n == maxFlateBlockTokens {
653 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
654 return
655 }
656 d.tokens.Reset()
657 }
658 s.index++
659
660
661
662 if n := int(s.ii) - d.chain; n > 0 {
663 n = 1 + int(n>>6)
664 for j := 0; j < n; j++ {
665 if s.index >= d.windowEnd-1 {
666 break
667 }
668 d.tokens.AddLiteral(d.window[s.index-1])
669 if d.tokens.n == maxFlateBlockTokens {
670 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
671 return
672 }
673 d.tokens.Reset()
674 }
675
676 if s.index < s.maxInsertIndex {
677 h := hash4(d.window[s.index:])
678 ch := s.hashHead[h]
679 s.chainHead = int(ch)
680 s.hashPrev[s.index&windowMask] = ch
681 s.hashHead[h] = uint32(s.index + s.hashOffset)
682 }
683 s.index++
684 }
685
686 d.tokens.AddLiteral(d.window[s.index-1])
687 d.byteAvailable = false
688
689 if d.tokens.n == maxFlateBlockTokens {
690 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
691 return
692 }
693 d.tokens.Reset()
694 }
695 }
696 } else {
697 s.index++
698 d.byteAvailable = true
699 }
700 }
701 }
702 }
703
704 func (d *compressor) store() {
705 if d.windowEnd > 0 && (d.windowEnd == maxStoreBlockSize || d.sync) {
706 d.err = d.writeStoredBlock(d.window[:d.windowEnd])
707 d.windowEnd = 0
708 }
709 }
710
711
712
713 func (d *compressor) fillBlock(b []byte) int {
714 n := copy(d.window[d.windowEnd:], b)
715 d.windowEnd += n
716 return n
717 }
718
719
720
721
722 func (d *compressor) storeHuff() {
723 if d.windowEnd < len(d.window) && !d.sync || d.windowEnd == 0 {
724 return
725 }
726 d.w.writeBlockHuff(false, d.window[:d.windowEnd], d.sync)
727 d.err = d.w.err
728 d.windowEnd = 0
729 }
730
731
732
733
734 func (d *compressor) storeFast() {
735
736 if d.windowEnd < len(d.window) {
737 if !d.sync {
738 return
739 }
740
741 if d.windowEnd < 128 {
742 if d.windowEnd == 0 {
743 return
744 }
745 if d.windowEnd <= 32 {
746 d.err = d.writeStoredBlock(d.window[:d.windowEnd])
747 } else {
748 d.w.writeBlockHuff(false, d.window[:d.windowEnd], true)
749 d.err = d.w.err
750 }
751 d.tokens.Reset()
752 d.windowEnd = 0
753 d.fast.Reset()
754 return
755 }
756 }
757
758 d.fast.Encode(&d.tokens, d.window[:d.windowEnd])
759
760 if d.tokens.n == 0 {
761 d.err = d.writeStoredBlock(d.window[:d.windowEnd])
762
763 } else if int(d.tokens.n) > d.windowEnd-(d.windowEnd>>4) {
764 d.w.writeBlockHuff(false, d.window[:d.windowEnd], d.sync)
765 d.err = d.w.err
766 } else {
767 d.w.writeBlockDynamic(&d.tokens, false, d.window[:d.windowEnd], d.sync)
768 d.err = d.w.err
769 }
770 d.tokens.Reset()
771 d.windowEnd = 0
772 }
773
774
775
776 func (d *compressor) write(b []byte) (n int, err error) {
777 if d.err != nil {
778 return 0, d.err
779 }
780 n = len(b)
781 for len(b) > 0 {
782 if d.windowEnd == len(d.window) || d.sync {
783 d.step(d)
784 }
785 b = b[d.fill(d, b):]
786 if d.err != nil {
787 return 0, d.err
788 }
789 }
790 return n, d.err
791 }
792
793 func (d *compressor) syncFlush() error {
794 d.sync = true
795 if d.err != nil {
796 return d.err
797 }
798 d.step(d)
799 if d.err == nil {
800 d.w.writeStoredHeader(0, false)
801 d.w.flush()
802 d.err = d.w.err
803 }
804 d.sync = false
805 return d.err
806 }
807
808 func (d *compressor) init(w io.Writer, level int) (err error) {
809 d.w = newHuffmanBitWriter(w)
810
811 switch {
812 case level == NoCompression:
813 d.window = make([]byte, maxStoreBlockSize)
814 d.fill = (*compressor).fillBlock
815 d.step = (*compressor).store
816 case level == ConstantCompression:
817 d.w.logNewTablePenalty = 10
818 d.window = make([]byte, 32<<10)
819 d.fill = (*compressor).fillBlock
820 d.step = (*compressor).storeHuff
821 case level == DefaultCompression:
822 level = 5
823 fallthrough
824 case level >= 1 && level <= 6:
825 d.w.logNewTablePenalty = 7
826 d.fast = newFastEnc(level)
827 d.window = make([]byte, maxStoreBlockSize)
828 d.fill = (*compressor).fillBlock
829 d.step = (*compressor).storeFast
830 case 7 <= level && level <= 9:
831 d.w.logNewTablePenalty = 8
832 d.state = &advancedState{}
833 d.compressionLevel = levels[level]
834 d.initDeflate()
835 d.fill = (*compressor).fillDeflate
836 d.step = (*compressor).deflateLazy
837 case -level >= MinCustomWindowSize && -level <= MaxCustomWindowSize:
838 d.w.logNewTablePenalty = 7
839 d.fast = &fastEncL5Window{maxOffset: int32(-level), cur: maxStoreBlockSize}
840 d.window = make([]byte, maxStoreBlockSize)
841 d.fill = (*compressor).fillBlock
842 d.step = (*compressor).storeFast
843 default:
844 return fmt.Errorf("flate: invalid compression level %d: want value in range [-2, 9]", level)
845 }
846 d.level = level
847 return nil
848 }
849
850
851 func (d *compressor) reset(w io.Writer) {
852 d.w.reset(w)
853 d.sync = false
854 d.err = nil
855
856 if d.fast != nil {
857 d.fast.Reset()
858 d.windowEnd = 0
859 d.tokens.Reset()
860 return
861 }
862 switch d.compressionLevel.chain {
863 case 0:
864
865 d.windowEnd = 0
866 default:
867 s := d.state
868 s.chainHead = -1
869 for i := range s.hashHead {
870 s.hashHead[i] = 0
871 }
872 for i := range s.hashPrev {
873 s.hashPrev[i] = 0
874 }
875 s.hashOffset = 1
876 s.index, d.windowEnd = 0, 0
877 d.blockStart, d.byteAvailable = 0, false
878 d.tokens.Reset()
879 s.length = minMatchLength - 1
880 s.offset = 0
881 s.ii = 0
882 s.maxInsertIndex = 0
883 }
884 }
885
886 func (d *compressor) close() error {
887 if d.err != nil {
888 return d.err
889 }
890 d.sync = true
891 d.step(d)
892 if d.err != nil {
893 return d.err
894 }
895 if d.w.writeStoredHeader(0, true); d.w.err != nil {
896 return d.w.err
897 }
898 d.w.flush()
899 d.w.reset(nil)
900 return d.w.err
901 }
902
903
904
905
906
907
908
909
910
911
912
913
914
915 func NewWriter(w io.Writer, level int) (*Writer, error) {
916 var dw Writer
917 if err := dw.d.init(w, level); err != nil {
918 return nil, err
919 }
920 return &dw, nil
921 }
922
923
924
925
926
927
928
929 func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, error) {
930 zw, err := NewWriter(w, level)
931 if err != nil {
932 return nil, err
933 }
934 zw.d.fillWindow(dict)
935 zw.dict = append(zw.dict, dict...)
936 return zw, err
937 }
938
939
940 const MinCustomWindowSize = 32
941
942
943 const MaxCustomWindowSize = windowSize
944
945
946
947 func NewWriterWindow(w io.Writer, windowSize int) (*Writer, error) {
948 if windowSize < MinCustomWindowSize {
949 return nil, errors.New("flate: requested window size less than MinWindowSize")
950 }
951 if windowSize > MaxCustomWindowSize {
952 return nil, errors.New("flate: requested window size bigger than MaxCustomWindowSize")
953 }
954 var dw Writer
955 if err := dw.d.init(w, -windowSize); err != nil {
956 return nil, err
957 }
958 return &dw, nil
959 }
960
961
962
963 type Writer struct {
964 d compressor
965 dict []byte
966 }
967
968
969
970 func (w *Writer) Write(data []byte) (n int, err error) {
971 return w.d.write(data)
972 }
973
974
975
976
977
978
979
980
981
982
983 func (w *Writer) Flush() error {
984
985
986 return w.d.syncFlush()
987 }
988
989
990 func (w *Writer) Close() error {
991 return w.d.close()
992 }
993
994
995
996
997 func (w *Writer) Reset(dst io.Writer) {
998 if len(w.dict) > 0 {
999
1000 w.d.reset(dst)
1001 if dst != nil {
1002 w.d.fillWindow(w.dict)
1003 }
1004 } else {
1005
1006 w.d.reset(dst)
1007 }
1008 }
1009
1010
1011
1012
1013 func (w *Writer) ResetDict(dst io.Writer, dict []byte) {
1014 w.dict = dict
1015 w.d.reset(dst)
1016 w.d.fillWindow(w.dict)
1017 }
1018
View as plain text