| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package flate |
| 6 |
|
| 7 |
// dictDecoder implements the LZ77 sliding dictionary as used in decompression. |
| 8 |
// LZ77 decompresses data through sequences of two forms of commands: |
| 9 |
// |
| 10 |
// - Literal insertions: Runs of one or more symbols are inserted into the data |
| 11 |
// stream as is. This is accomplished through the writeByte method for a |
| 12 |
// single symbol, or combinations of writeSlice/writeMark for multiple symbols. |
| 13 |
// Any valid stream must start with a literal insertion if no preset dictionary |
| 14 |
// is used. |
| 15 |
// |
| 16 |
// - Backward copies: Runs of one or more symbols are copied from previously |
| 17 |
// emitted data. Backward copies come as the tuple (dist, length) where dist |
| 18 |
// determines how far back in the stream to copy from and length determines how |
| 19 |
// many bytes to copy. Note that it is valid for the length to be greater than |
| 20 |
// the distance. Since LZ77 uses forward copies, that situation is used to |
| 21 |
// perform a form of run-length encoding on repeated runs of symbols. |
| 22 |
// The writeCopy and tryWriteCopy are used to implement this command. |
| 23 |
// |
| 24 |
// For performance reasons, this implementation performs little to no sanity |
| 25 |
// checks about the arguments. As such, the invariants documented for each |
| 26 |
// method call must be respected. |
| 27 |
type dictDecoder struct { |
| 28 |
hist []byte // Sliding window history |
| 29 |
|
| 30 |
// Invariant: 0 <= rdPos <= wrPos <= len(hist) |
| 31 |
wrPos int // Current output position in buffer |
| 32 |
rdPos int // Have emitted hist[:rdPos] already |
| 33 |
flushed int64 // Total bytes returned by readFlush since init |
| 34 |
full bool // Has a full window length been written yet? |
| 35 |
} |
| 36 |
|
| 37 |
// init initializes dictDecoder to have a sliding window dictionary of the given |
| 38 |
// size. If a preset dict is provided, it will initialize the dictionary with |
| 39 |
// the contents of dict. |
| 40 |
func (dd *dictDecoder) init(size int, dict []byte) { |
| 41 |
*dd = dictDecoder{hist: dd.hist} |
| 42 |
|
| 43 |
if cap(dd.hist) < size { |
| 44 |
dd.hist = make([]byte, size) |
| 45 |
} |
| 46 |
dd.hist = dd.hist[:size] |
| 47 |
|
| 48 |
if len(dict) > len(dd.hist) { |
| 49 |
dict = dict[len(dict)-len(dd.hist):] |
| 50 |
} |
| 51 |
dd.wrPos = copy(dd.hist, dict) |
| 52 |
if dd.wrPos == len(dd.hist) { |
| 53 |
dd.wrPos = 0 |
| 54 |
dd.full = true |
| 55 |
} |
| 56 |
dd.rdPos = dd.wrPos |
| 57 |
} |
| 58 |
|
| 59 |
// histSize reports the total amount of historical data in the dictionary. |
| 60 |
func (dd *dictDecoder) histSize() int { |
| 61 |
if dd.full { |
| 62 |
return len(dd.hist) |
| 63 |
} |
| 64 |
return dd.wrPos |
| 65 |
} |
| 66 |
|
| 67 |
// availRead reports the number of bytes that can be flushed by readFlush. |
| 68 |
func (dd *dictDecoder) availRead() int { |
| 69 |
return dd.wrPos - dd.rdPos |
| 70 |
} |
| 71 |
|
| 72 |
// availWrite reports the available amount of output buffer space. |
| 73 |
func (dd *dictDecoder) availWrite() int { |
| 74 |
return len(dd.hist) - dd.wrPos |
| 75 |
} |
| 76 |
|
| 77 |
// writeSlice returns a slice of the available buffer to write data to. |
| 78 |
// |
| 79 |
// This invariant will be kept: len(s) <= availWrite() |
| 80 |
func (dd *dictDecoder) writeSlice() []byte { |
| 81 |
return dd.hist[dd.wrPos:] |
| 82 |
} |
| 83 |
|
| 84 |
// writeMark advances the writer pointer by cnt. |
| 85 |
// |
| 86 |
// This invariant must be kept: 0 <= cnt <= availWrite() |
| 87 |
func (dd *dictDecoder) writeMark(cnt int) { |
| 88 |
dd.wrPos += cnt |
| 89 |
} |
| 90 |
|
| 91 |
// writeByte writes a single byte to the dictionary. |
| 92 |
// |
| 93 |
// This invariant must be kept: 0 < availWrite() |
| 94 |
func (dd *dictDecoder) writeByte(c byte) { |
| 95 |
dd.hist[dd.wrPos] = c |
| 96 |
dd.wrPos++ |
| 97 |
} |
| 98 |
|
| 99 |
// writeCopy copies a string at a given (dist, length) to the output. |
| 100 |
// This returns the number of bytes copied and may be less than the requested |
| 101 |
// length if the available space in the output buffer is too small. |
| 102 |
// |
| 103 |
// This invariant must be kept: 0 < dist <= histSize() |
| 104 |
func (dd *dictDecoder) writeCopy(dist, length int) int { |
| 105 |
dstBase := dd.wrPos |
| 106 |
dstPos := dstBase |
| 107 |
srcPos := dstPos - dist |
| 108 |
endPos := min(dstPos+length, len(dd.hist)) |
| 109 |
|
| 110 |
// Copy non-overlapping section after destination position. |
| 111 |
// |
| 112 |
// This section is non-overlapping in that the copy length for this section |
| 113 |
// is always less than or equal to the backwards distance. This can occur |
| 114 |
// if a distance refers to data that wraps-around in the buffer. |
| 115 |
// Thus, a backwards copy is performed here; that is, the exact bytes in |
| 116 |
// the source prior to the copy is placed in the destination. |
| 117 |
if srcPos < 0 { |
| 118 |
srcPos += len(dd.hist) |
| 119 |
dstPos += copy(dd.hist[dstPos:endPos], dd.hist[srcPos:]) |
| 120 |
srcPos = 0 |
| 121 |
} |
| 122 |
|
| 123 |
// Copy possibly overlapping section before destination position. |
| 124 |
// |
| 125 |
// This section can overlap if the copy length for this section is larger |
| 126 |
// than the backwards distance. This is allowed by LZ77 so that repeated |
| 127 |
// strings can be succinctly represented using (dist, length) pairs. |
| 128 |
// Thus, a forwards copy is performed here; that is, the bytes copied is |
| 129 |
// possibly dependent on the resulting bytes in the destination as the copy |
| 130 |
// progresses along. This is functionally equivalent to the following: |
| 131 |
// |
| 132 |
// for i := 0; i < endPos-dstPos; i++ { |
| 133 |
// dd.hist[dstPos+i] = dd.hist[srcPos+i] |
| 134 |
// } |
| 135 |
// dstPos = endPos |
| 136 |
// |
| 137 |
for dstPos < endPos { |
| 138 |
dstPos += copy(dd.hist[dstPos:endPos], dd.hist[srcPos:dstPos]) |
| 139 |
} |
| 140 |
|
| 141 |
dd.wrPos = dstPos |
| 142 |
return dstPos - dstBase |
| 143 |
} |
| 144 |
|
| 145 |
// tryWriteCopy tries to copy a string at a given (distance, length) to the |
| 146 |
// output. This specialized version is optimized for short distances. |
| 147 |
// |
| 148 |
// This method is designed to be inlined for performance reasons. |
| 149 |
// |
| 150 |
// This invariant must be kept: 0 < dist <= histSize() |
| 151 |
func (dd *dictDecoder) tryWriteCopy(dist, length int) int { |
| 152 |
dstPos := dd.wrPos |
| 153 |
endPos := dstPos + length |
| 154 |
if dstPos < dist || endPos > len(dd.hist) { |
| 155 |
return 0 |
| 156 |
} |
| 157 |
dstBase := dstPos |
| 158 |
srcPos := dstPos - dist |
| 159 |
|
| 160 |
// Copy possibly overlapping section before destination position. |
| 161 |
loop: |
| 162 |
dstPos += copy(dd.hist[dstPos:endPos], dd.hist[srcPos:dstPos]) |
| 163 |
if dstPos < endPos { |
| 164 |
goto loop // Avoid for-loop so that this function can be inlined |
| 165 |
} |
| 166 |
|
| 167 |
dd.wrPos = dstPos |
| 168 |
return dstPos - dstBase |
| 169 |
} |
| 170 |
|
| 171 |
// appendWindow appends the current sliding window (up to len(hist) most recent |
| 172 |
// bytes, oldest first) to dst. |
| 173 |
func (dd *dictDecoder) appendWindow(dst []byte) []byte { |
| 174 |
if dd.full { |
| 175 |
dst = append(dst, dd.hist[dd.wrPos:]...) |
| 176 |
return append(dst, dd.hist[:dd.wrPos]...) |
| 177 |
} |
| 178 |
return append(dst, dd.hist[:dd.wrPos]...) |
| 179 |
} |
| 180 |
|
| 181 |
// readFlush returns a slice of the historical buffer that is ready to be |
| 182 |
// emitted to the user. The data returned by readFlush must be fully consumed |
| 183 |
// before calling any other dictDecoder methods. |
| 184 |
func (dd *dictDecoder) readFlush() []byte { |
| 185 |
toRead := dd.hist[dd.rdPos:dd.wrPos] |
| 186 |
dd.flushed += int64(len(toRead)) |
| 187 |
dd.rdPos = dd.wrPos |
| 188 |
if dd.wrPos == len(dd.hist) { |
| 189 |
dd.wrPos, dd.rdPos = 0, 0 |
| 190 |
dd.full = true |
| 191 |
} |
| 192 |
return toRead |
| 193 |
} |
| 194 |
|
| 195 |
// decoded reports the total number of bytes written into the dictionary since |
| 196 |
// init (i.e. excluding any preset dict bytes). |
| 197 |
func (dd *dictDecoder) decoded() int64 { |
| 198 |
return dd.flushed + int64(dd.wrPos-dd.rdPos) |
| 199 |
} |