source: code/trunk/vendor/github.com/klauspost/compress/flate/deflate.go@ 822

Last change on this file since 822 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 22.2 KB
Line 
1// Copyright 2009 The Go Authors. All rights reserved.
2// Copyright (c) 2015 Klaus Post
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6package flate
7
8import (
9 "fmt"
10 "io"
11 "math"
12)
13
14const (
15 NoCompression = 0
16 BestSpeed = 1
17 BestCompression = 9
18 DefaultCompression = -1
19
20 // HuffmanOnly disables Lempel-Ziv match searching and only performs Huffman
21 // entropy encoding. This mode is useful in compressing data that has
22 // already been compressed with an LZ style algorithm (e.g. Snappy or LZ4)
23 // that lacks an entropy encoder. Compression gains are achieved when
24 // certain bytes in the input stream occur more frequently than others.
25 //
26 // Note that HuffmanOnly produces a compressed output that is
27 // RFC 1951 compliant. That is, any valid DEFLATE decompressor will
28 // continue to be able to decompress this output.
29 HuffmanOnly = -2
30 ConstantCompression = HuffmanOnly // compatibility alias.
31
32 logWindowSize = 15
33 windowSize = 1 << logWindowSize
34 windowMask = windowSize - 1
35 logMaxOffsetSize = 15 // Standard DEFLATE
36 minMatchLength = 4 // The smallest match that the compressor looks for
37 maxMatchLength = 258 // The longest match for the compressor
38 minOffsetSize = 1 // The shortest offset that makes any sense
39
40 // The maximum number of tokens we put into a single flat block, just too
41 // stop things from getting too large.
42 maxFlateBlockTokens = 1 << 14
43 maxStoreBlockSize = 65535
44 hashBits = 17 // After 17 performance degrades
45 hashSize = 1 << hashBits
46 hashMask = (1 << hashBits) - 1
47 hashShift = (hashBits + minMatchLength - 1) / minMatchLength
48 maxHashOffset = 1 << 24
49
50 skipNever = math.MaxInt32
51
52 debugDeflate = false
53)
54
55type compressionLevel struct {
56 good, lazy, nice, chain, fastSkipHashing, level int
57}
58
59// Compression levels have been rebalanced from zlib deflate defaults
60// to give a bigger spread in speed and compression.
61// See https://blog.klauspost.com/rebalancing-deflate-compression-levels/
62var levels = []compressionLevel{
63 {}, // 0
64 // Level 1-6 uses specialized algorithm - values not used
65 {0, 0, 0, 0, 0, 1},
66 {0, 0, 0, 0, 0, 2},
67 {0, 0, 0, 0, 0, 3},
68 {0, 0, 0, 0, 0, 4},
69 {0, 0, 0, 0, 0, 5},
70 {0, 0, 0, 0, 0, 6},
71 // Levels 7-9 use increasingly more lazy matching
72 // and increasingly stringent conditions for "good enough".
73 {8, 8, 24, 16, skipNever, 7},
74 {10, 16, 24, 64, skipNever, 8},
75 {32, 258, 258, 4096, skipNever, 9},
76}
77
78// advancedState contains state for the advanced levels, with bigger hash tables, etc.
79type advancedState struct {
80 // deflate state
81 length int
82 offset int
83 hash uint32
84 maxInsertIndex int
85 ii uint16 // position of last match, intended to overflow to reset.
86
87 // Input hash chains
88 // hashHead[hashValue] contains the largest inputIndex with the specified hash value
89 // If hashHead[hashValue] is within the current window, then
90 // hashPrev[hashHead[hashValue] & windowMask] contains the previous index
91 // with the same hash value.
92 chainHead int
93 hashHead [hashSize]uint32
94 hashPrev [windowSize]uint32
95 hashOffset int
96
97 // input window: unprocessed data is window[index:windowEnd]
98 index int
99 hashMatch [maxMatchLength + minMatchLength]uint32
100}
101
102type compressor struct {
103 compressionLevel
104
105 w *huffmanBitWriter
106
107 // compression algorithm
108 fill func(*compressor, []byte) int // copy data to window
109 step func(*compressor) // process window
110 sync bool // requesting flush
111
112 window []byte
113 windowEnd int
114 blockStart int // window index where current tokens start
115 byteAvailable bool // if true, still need to process window[index-1].
116 err error
117
118 // queued output tokens
119 tokens tokens
120 fast fastEnc
121 state *advancedState
122}
123
124func (d *compressor) fillDeflate(b []byte) int {
125 s := d.state
126 if s.index >= 2*windowSize-(minMatchLength+maxMatchLength) {
127 // shift the window by windowSize
128 copy(d.window[:], d.window[windowSize:2*windowSize])
129 s.index -= windowSize
130 d.windowEnd -= windowSize
131 if d.blockStart >= windowSize {
132 d.blockStart -= windowSize
133 } else {
134 d.blockStart = math.MaxInt32
135 }
136 s.hashOffset += windowSize
137 if s.hashOffset > maxHashOffset {
138 delta := s.hashOffset - 1
139 s.hashOffset -= delta
140 s.chainHead -= delta
141 // Iterate over slices instead of arrays to avoid copying
142 // the entire table onto the stack (Issue #18625).
143 for i, v := range s.hashPrev[:] {
144 if int(v) > delta {
145 s.hashPrev[i] = uint32(int(v) - delta)
146 } else {
147 s.hashPrev[i] = 0
148 }
149 }
150 for i, v := range s.hashHead[:] {
151 if int(v) > delta {
152 s.hashHead[i] = uint32(int(v) - delta)
153 } else {
154 s.hashHead[i] = 0
155 }
156 }
157 }
158 }
159 n := copy(d.window[d.windowEnd:], b)
160 d.windowEnd += n
161 return n
162}
163
164func (d *compressor) writeBlock(tok *tokens, index int, eof bool) error {
165 if index > 0 || eof {
166 var window []byte
167 if d.blockStart <= index {
168 window = d.window[d.blockStart:index]
169 }
170 d.blockStart = index
171 d.w.writeBlock(tok, eof, window)
172 return d.w.err
173 }
174 return nil
175}
176
177// writeBlockSkip writes the current block and uses the number of tokens
178// to determine if the block should be stored on no matches, or
179// only huffman encoded.
180func (d *compressor) writeBlockSkip(tok *tokens, index int, eof bool) error {
181 if index > 0 || eof {
182 if d.blockStart <= index {
183 window := d.window[d.blockStart:index]
184 // If we removed less than a 64th of all literals
185 // we huffman compress the block.
186 if int(tok.n) > len(window)-int(tok.n>>6) {
187 d.w.writeBlockHuff(eof, window, d.sync)
188 } else {
189 // Write a dynamic huffman block.
190 d.w.writeBlockDynamic(tok, eof, window, d.sync)
191 }
192 } else {
193 d.w.writeBlock(tok, eof, nil)
194 }
195 d.blockStart = index
196 return d.w.err
197 }
198 return nil
199}
200
201// fillWindow will fill the current window with the supplied
202// dictionary and calculate all hashes.
203// This is much faster than doing a full encode.
204// Should only be used after a start/reset.
205func (d *compressor) fillWindow(b []byte) {
206 // Do not fill window if we are in store-only or huffman mode.
207 if d.level <= 0 {
208 return
209 }
210 if d.fast != nil {
211 // encode the last data, but discard the result
212 if len(b) > maxMatchOffset {
213 b = b[len(b)-maxMatchOffset:]
214 }
215 d.fast.Encode(&d.tokens, b)
216 d.tokens.Reset()
217 return
218 }
219 s := d.state
220 // If we are given too much, cut it.
221 if len(b) > windowSize {
222 b = b[len(b)-windowSize:]
223 }
224 // Add all to window.
225 n := copy(d.window[d.windowEnd:], b)
226
227 // Calculate 256 hashes at the time (more L1 cache hits)
228 loops := (n + 256 - minMatchLength) / 256
229 for j := 0; j < loops; j++ {
230 startindex := j * 256
231 end := startindex + 256 + minMatchLength - 1
232 if end > n {
233 end = n
234 }
235 tocheck := d.window[startindex:end]
236 dstSize := len(tocheck) - minMatchLength + 1
237
238 if dstSize <= 0 {
239 continue
240 }
241
242 dst := s.hashMatch[:dstSize]
243 bulkHash4(tocheck, dst)
244 var newH uint32
245 for i, val := range dst {
246 di := i + startindex
247 newH = val & hashMask
248 // Get previous value with the same hash.
249 // Our chain should point to the previous value.
250 s.hashPrev[di&windowMask] = s.hashHead[newH]
251 // Set the head of the hash chain to us.
252 s.hashHead[newH] = uint32(di + s.hashOffset)
253 }
254 s.hash = newH
255 }
256 // Update window information.
257 d.windowEnd += n
258 s.index = n
259}
260
261// Try to find a match starting at index whose length is greater than prevSize.
262// We only look at chainCount possibilities before giving up.
263// pos = s.index, prevHead = s.chainHead-s.hashOffset, prevLength=minMatchLength-1, lookahead
264func (d *compressor) findMatch(pos int, prevHead int, prevLength int, lookahead int) (length, offset int, ok bool) {
265 minMatchLook := maxMatchLength
266 if lookahead < minMatchLook {
267 minMatchLook = lookahead
268 }
269
270 win := d.window[0 : pos+minMatchLook]
271
272 // We quit when we get a match that's at least nice long
273 nice := len(win) - pos
274 if d.nice < nice {
275 nice = d.nice
276 }
277
278 // If we've got a match that's good enough, only look in 1/4 the chain.
279 tries := d.chain
280 length = prevLength
281 if length >= d.good {
282 tries >>= 2
283 }
284
285 wEnd := win[pos+length]
286 wPos := win[pos:]
287 minIndex := pos - windowSize
288
289 for i := prevHead; tries > 0; tries-- {
290 if wEnd == win[i+length] {
291 n := matchLen(win[i:i+minMatchLook], wPos)
292
293 if n > length && (n > minMatchLength || pos-i <= 4096) {
294 length = n
295 offset = pos - i
296 ok = true
297 if n >= nice {
298 // The match is good enough that we don't try to find a better one.
299 break
300 }
301 wEnd = win[pos+n]
302 }
303 }
304 if i == minIndex {
305 // hashPrev[i & windowMask] has already been overwritten, so stop now.
306 break
307 }
308 i = int(d.state.hashPrev[i&windowMask]) - d.state.hashOffset
309 if i < minIndex || i < 0 {
310 break
311 }
312 }
313 return
314}
315
316func (d *compressor) writeStoredBlock(buf []byte) error {
317 if d.w.writeStoredHeader(len(buf), false); d.w.err != nil {
318 return d.w.err
319 }
320 d.w.writeBytes(buf)
321 return d.w.err
322}
323
324// hash4 returns a hash representation of the first 4 bytes
325// of the supplied slice.
326// The caller must ensure that len(b) >= 4.
327func hash4(b []byte) uint32 {
328 b = b[:4]
329 return hash4u(uint32(b[3])|uint32(b[2])<<8|uint32(b[1])<<16|uint32(b[0])<<24, hashBits)
330}
331
332// bulkHash4 will compute hashes using the same
333// algorithm as hash4
334func bulkHash4(b []byte, dst []uint32) {
335 if len(b) < 4 {
336 return
337 }
338 hb := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
339 dst[0] = hash4u(hb, hashBits)
340 end := len(b) - 4 + 1
341 for i := 1; i < end; i++ {
342 hb = (hb << 8) | uint32(b[i+3])
343 dst[i] = hash4u(hb, hashBits)
344 }
345}
346
347func (d *compressor) initDeflate() {
348 d.window = make([]byte, 2*windowSize)
349 d.byteAvailable = false
350 d.err = nil
351 if d.state == nil {
352 return
353 }
354 s := d.state
355 s.index = 0
356 s.hashOffset = 1
357 s.length = minMatchLength - 1
358 s.offset = 0
359 s.hash = 0
360 s.chainHead = -1
361}
362
363// deflateLazy is the same as deflate, but with d.fastSkipHashing == skipNever,
364// meaning it always has lazy matching on.
365func (d *compressor) deflateLazy() {
366 s := d.state
367 // Sanity enables additional runtime tests.
368 // It's intended to be used during development
369 // to supplement the currently ad-hoc unit tests.
370 const sanity = debugDeflate
371
372 if d.windowEnd-s.index < minMatchLength+maxMatchLength && !d.sync {
373 return
374 }
375
376 s.maxInsertIndex = d.windowEnd - (minMatchLength - 1)
377 if s.index < s.maxInsertIndex {
378 s.hash = hash4(d.window[s.index : s.index+minMatchLength])
379 }
380
381 for {
382 if sanity && s.index > d.windowEnd {
383 panic("index > windowEnd")
384 }
385 lookahead := d.windowEnd - s.index
386 if lookahead < minMatchLength+maxMatchLength {
387 if !d.sync {
388 return
389 }
390 if sanity && s.index > d.windowEnd {
391 panic("index > windowEnd")
392 }
393 if lookahead == 0 {
394 // Flush current output block if any.
395 if d.byteAvailable {
396 // There is still one pending token that needs to be flushed
397 d.tokens.AddLiteral(d.window[s.index-1])
398 d.byteAvailable = false
399 }
400 if d.tokens.n > 0 {
401 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
402 return
403 }
404 d.tokens.Reset()
405 }
406 return
407 }
408 }
409 if s.index < s.maxInsertIndex {
410 // Update the hash
411 s.hash = hash4(d.window[s.index : s.index+minMatchLength])
412 ch := s.hashHead[s.hash&hashMask]
413 s.chainHead = int(ch)
414 s.hashPrev[s.index&windowMask] = ch
415 s.hashHead[s.hash&hashMask] = uint32(s.index + s.hashOffset)
416 }
417 prevLength := s.length
418 prevOffset := s.offset
419 s.length = minMatchLength - 1
420 s.offset = 0
421 minIndex := s.index - windowSize
422 if minIndex < 0 {
423 minIndex = 0
424 }
425
426 if s.chainHead-s.hashOffset >= minIndex && lookahead > prevLength && prevLength < d.lazy {
427 if newLength, newOffset, ok := d.findMatch(s.index, s.chainHead-s.hashOffset, minMatchLength-1, lookahead); ok {
428 s.length = newLength
429 s.offset = newOffset
430 }
431 }
432 if prevLength >= minMatchLength && s.length <= prevLength {
433 // There was a match at the previous step, and the current match is
434 // not better. Output the previous match.
435 d.tokens.AddMatch(uint32(prevLength-3), uint32(prevOffset-minOffsetSize))
436
437 // Insert in the hash table all strings up to the end of the match.
438 // index and index-1 are already inserted. If there is not enough
439 // lookahead, the last two strings are not inserted into the hash
440 // table.
441 var newIndex int
442 newIndex = s.index + prevLength - 1
443 // Calculate missing hashes
444 end := newIndex
445 if end > s.maxInsertIndex {
446 end = s.maxInsertIndex
447 }
448 end += minMatchLength - 1
449 startindex := s.index + 1
450 if startindex > s.maxInsertIndex {
451 startindex = s.maxInsertIndex
452 }
453 tocheck := d.window[startindex:end]
454 dstSize := len(tocheck) - minMatchLength + 1
455 if dstSize > 0 {
456 dst := s.hashMatch[:dstSize]
457 bulkHash4(tocheck, dst)
458 var newH uint32
459 for i, val := range dst {
460 di := i + startindex
461 newH = val & hashMask
462 // Get previous value with the same hash.
463 // Our chain should point to the previous value.
464 s.hashPrev[di&windowMask] = s.hashHead[newH]
465 // Set the head of the hash chain to us.
466 s.hashHead[newH] = uint32(di + s.hashOffset)
467 }
468 s.hash = newH
469 }
470
471 s.index = newIndex
472 d.byteAvailable = false
473 s.length = minMatchLength - 1
474 if d.tokens.n == maxFlateBlockTokens {
475 // The block includes the current character
476 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
477 return
478 }
479 d.tokens.Reset()
480 }
481 } else {
482 // Reset, if we got a match this run.
483 if s.length >= minMatchLength {
484 s.ii = 0
485 }
486 // We have a byte waiting. Emit it.
487 if d.byteAvailable {
488 s.ii++
489 d.tokens.AddLiteral(d.window[s.index-1])
490 if d.tokens.n == maxFlateBlockTokens {
491 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
492 return
493 }
494 d.tokens.Reset()
495 }
496 s.index++
497
498 // If we have a long run of no matches, skip additional bytes
499 // Resets when s.ii overflows after 64KB.
500 if s.ii > 31 {
501 n := int(s.ii >> 5)
502 for j := 0; j < n; j++ {
503 if s.index >= d.windowEnd-1 {
504 break
505 }
506
507 d.tokens.AddLiteral(d.window[s.index-1])
508 if d.tokens.n == maxFlateBlockTokens {
509 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
510 return
511 }
512 d.tokens.Reset()
513 }
514 s.index++
515 }
516 // Flush last byte
517 d.tokens.AddLiteral(d.window[s.index-1])
518 d.byteAvailable = false
519 // s.length = minMatchLength - 1 // not needed, since s.ii is reset above, so it should never be > minMatchLength
520 if d.tokens.n == maxFlateBlockTokens {
521 if d.err = d.writeBlock(&d.tokens, s.index, false); d.err != nil {
522 return
523 }
524 d.tokens.Reset()
525 }
526 }
527 } else {
528 s.index++
529 d.byteAvailable = true
530 }
531 }
532 }
533}
534
535func (d *compressor) store() {
536 if d.windowEnd > 0 && (d.windowEnd == maxStoreBlockSize || d.sync) {
537 d.err = d.writeStoredBlock(d.window[:d.windowEnd])
538 d.windowEnd = 0
539 }
540}
541
542// fillWindow will fill the buffer with data for huffman-only compression.
543// The number of bytes copied is returned.
544func (d *compressor) fillBlock(b []byte) int {
545 n := copy(d.window[d.windowEnd:], b)
546 d.windowEnd += n
547 return n
548}
549
550// storeHuff will compress and store the currently added data,
551// if enough has been accumulated or we at the end of the stream.
552// Any error that occurred will be in d.err
553func (d *compressor) storeHuff() {
554 if d.windowEnd < len(d.window) && !d.sync || d.windowEnd == 0 {
555 return
556 }
557 d.w.writeBlockHuff(false, d.window[:d.windowEnd], d.sync)
558 d.err = d.w.err
559 d.windowEnd = 0
560}
561
562// storeFast will compress and store the currently added data,
563// if enough has been accumulated or we at the end of the stream.
564// Any error that occurred will be in d.err
565func (d *compressor) storeFast() {
566 // We only compress if we have maxStoreBlockSize.
567 if d.windowEnd < len(d.window) {
568 if !d.sync {
569 return
570 }
571 // Handle extremely small sizes.
572 if d.windowEnd < 128 {
573 if d.windowEnd == 0 {
574 return
575 }
576 if d.windowEnd <= 32 {
577 d.err = d.writeStoredBlock(d.window[:d.windowEnd])
578 } else {
579 d.w.writeBlockHuff(false, d.window[:d.windowEnd], true)
580 d.err = d.w.err
581 }
582 d.tokens.Reset()
583 d.windowEnd = 0
584 d.fast.Reset()
585 return
586 }
587 }
588
589 d.fast.Encode(&d.tokens, d.window[:d.windowEnd])
590 // If we made zero matches, store the block as is.
591 if d.tokens.n == 0 {
592 d.err = d.writeStoredBlock(d.window[:d.windowEnd])
593 // If we removed less than 1/16th, huffman compress the block.
594 } else if int(d.tokens.n) > d.windowEnd-(d.windowEnd>>4) {
595 d.w.writeBlockHuff(false, d.window[:d.windowEnd], d.sync)
596 d.err = d.w.err
597 } else {
598 d.w.writeBlockDynamic(&d.tokens, false, d.window[:d.windowEnd], d.sync)
599 d.err = d.w.err
600 }
601 d.tokens.Reset()
602 d.windowEnd = 0
603}
604
605// write will add input byte to the stream.
606// Unless an error occurs all bytes will be consumed.
607func (d *compressor) write(b []byte) (n int, err error) {
608 if d.err != nil {
609 return 0, d.err
610 }
611 n = len(b)
612 for len(b) > 0 {
613 d.step(d)
614 b = b[d.fill(d, b):]
615 if d.err != nil {
616 return 0, d.err
617 }
618 }
619 return n, d.err
620}
621
622func (d *compressor) syncFlush() error {
623 d.sync = true
624 if d.err != nil {
625 return d.err
626 }
627 d.step(d)
628 if d.err == nil {
629 d.w.writeStoredHeader(0, false)
630 d.w.flush()
631 d.err = d.w.err
632 }
633 d.sync = false
634 return d.err
635}
636
637func (d *compressor) init(w io.Writer, level int) (err error) {
638 d.w = newHuffmanBitWriter(w)
639
640 switch {
641 case level == NoCompression:
642 d.window = make([]byte, maxStoreBlockSize)
643 d.fill = (*compressor).fillBlock
644 d.step = (*compressor).store
645 case level == ConstantCompression:
646 d.w.logNewTablePenalty = 4
647 d.window = make([]byte, maxStoreBlockSize)
648 d.fill = (*compressor).fillBlock
649 d.step = (*compressor).storeHuff
650 case level == DefaultCompression:
651 level = 5
652 fallthrough
653 case level >= 1 && level <= 6:
654 d.w.logNewTablePenalty = 6
655 d.fast = newFastEnc(level)
656 d.window = make([]byte, maxStoreBlockSize)
657 d.fill = (*compressor).fillBlock
658 d.step = (*compressor).storeFast
659 case 7 <= level && level <= 9:
660 d.w.logNewTablePenalty = 10
661 d.state = &advancedState{}
662 d.compressionLevel = levels[level]
663 d.initDeflate()
664 d.fill = (*compressor).fillDeflate
665 d.step = (*compressor).deflateLazy
666 default:
667 return fmt.Errorf("flate: invalid compression level %d: want value in range [-2, 9]", level)
668 }
669 d.level = level
670 return nil
671}
672
673// reset the state of the compressor.
674func (d *compressor) reset(w io.Writer) {
675 d.w.reset(w)
676 d.sync = false
677 d.err = nil
678 // We only need to reset a few things for Snappy.
679 if d.fast != nil {
680 d.fast.Reset()
681 d.windowEnd = 0
682 d.tokens.Reset()
683 return
684 }
685 switch d.compressionLevel.chain {
686 case 0:
687 // level was NoCompression or ConstantCompresssion.
688 d.windowEnd = 0
689 default:
690 s := d.state
691 s.chainHead = -1
692 for i := range s.hashHead {
693 s.hashHead[i] = 0
694 }
695 for i := range s.hashPrev {
696 s.hashPrev[i] = 0
697 }
698 s.hashOffset = 1
699 s.index, d.windowEnd = 0, 0
700 d.blockStart, d.byteAvailable = 0, false
701 d.tokens.Reset()
702 s.length = minMatchLength - 1
703 s.offset = 0
704 s.hash = 0
705 s.ii = 0
706 s.maxInsertIndex = 0
707 }
708}
709
710func (d *compressor) close() error {
711 if d.err != nil {
712 return d.err
713 }
714 d.sync = true
715 d.step(d)
716 if d.err != nil {
717 return d.err
718 }
719 if d.w.writeStoredHeader(0, true); d.w.err != nil {
720 return d.w.err
721 }
722 d.w.flush()
723 d.w.reset(nil)
724 return d.w.err
725}
726
727// NewWriter returns a new Writer compressing data at the given level.
728// Following zlib, levels range from 1 (BestSpeed) to 9 (BestCompression);
729// higher levels typically run slower but compress more.
730// Level 0 (NoCompression) does not attempt any compression; it only adds the
731// necessary DEFLATE framing.
732// Level -1 (DefaultCompression) uses the default compression level.
733// Level -2 (ConstantCompression) will use Huffman compression only, giving
734// a very fast compression for all types of input, but sacrificing considerable
735// compression efficiency.
736//
737// If level is in the range [-2, 9] then the error returned will be nil.
738// Otherwise the error returned will be non-nil.
739func NewWriter(w io.Writer, level int) (*Writer, error) {
740 var dw Writer
741 if err := dw.d.init(w, level); err != nil {
742 return nil, err
743 }
744 return &dw, nil
745}
746
747// NewWriterDict is like NewWriter but initializes the new
748// Writer with a preset dictionary. The returned Writer behaves
749// as if the dictionary had been written to it without producing
750// any compressed output. The compressed data written to w
751// can only be decompressed by a Reader initialized with the
752// same dictionary.
753func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, error) {
754 zw, err := NewWriter(w, level)
755 if err != nil {
756 return nil, err
757 }
758 zw.d.fillWindow(dict)
759 zw.dict = append(zw.dict, dict...) // duplicate dictionary for Reset method.
760 return zw, err
761}
762
763// A Writer takes data written to it and writes the compressed
764// form of that data to an underlying writer (see NewWriter).
765type Writer struct {
766 d compressor
767 dict []byte
768}
769
770// Write writes data to w, which will eventually write the
771// compressed form of data to its underlying writer.
772func (w *Writer) Write(data []byte) (n int, err error) {
773 return w.d.write(data)
774}
775
776// Flush flushes any pending data to the underlying writer.
777// It is useful mainly in compressed network protocols, to ensure that
778// a remote reader has enough data to reconstruct a packet.
779// Flush does not return until the data has been written.
780// Calling Flush when there is no pending data still causes the Writer
781// to emit a sync marker of at least 4 bytes.
782// If the underlying writer returns an error, Flush returns that error.
783//
784// In the terminology of the zlib library, Flush is equivalent to Z_SYNC_FLUSH.
785func (w *Writer) Flush() error {
786 // For more about flushing:
787 // http://www.bolet.org/~pornin/deflate-flush.html
788 return w.d.syncFlush()
789}
790
791// Close flushes and closes the writer.
792func (w *Writer) Close() error {
793 return w.d.close()
794}
795
796// Reset discards the writer's state and makes it equivalent to
797// the result of NewWriter or NewWriterDict called with dst
798// and w's level and dictionary.
799func (w *Writer) Reset(dst io.Writer) {
800 if len(w.dict) > 0 {
801 // w was created with NewWriterDict
802 w.d.reset(dst)
803 if dst != nil {
804 w.d.fillWindow(w.dict)
805 }
806 } else {
807 // w was created with NewWriter
808 w.d.reset(dst)
809 }
810}
811
812// ResetDict discards the writer's state and makes it equivalent to
813// the result of NewWriter or NewWriterDict called with dst
814// and w's level, but sets a specific dictionary.
815func (w *Writer) ResetDict(dst io.Writer, dict []byte) {
816 w.dict = dict
817 w.d.reset(dst)
818 w.d.fillWindow(w.dict)
819}
Note: See TracBrowser for help on using the repository browser.