| 1 |
//go:build generate |
| 2 |
// +build generate |
| 3 |
|
| 4 |
//go:generate go run $GOFILE |
| 5 |
//go:generate go fmt ../inflate_gen.go |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
func main() { |
| 15 |
f, err := os.Create("../inflate_gen.go") |
| 16 |
if err != nil { |
| 17 |
panic(err) |
| 18 |
} |
| 19 |
defer f.Close() |
| 20 |
types := []string{"*bytes.Buffer", "*bytes.Reader", "*bufio.Reader", "*strings.Reader", "Reader"} |
| 21 |
names := []string{"BytesBuffer", "BytesReader", "BufioReader", "StringsReader", "GenericReader"} |
| 22 |
imports := []string{"bytes", "bufio", "fmt", "strings", "math/bits", "", "github.com/klauspost/compress/internal/regmask"} |
| 23 |
f.WriteString(`// Code generated by go generate gen_inflate.go. DO NOT EDIT. |
| 24 |
|
| 25 |
package flate |
| 26 |
|
| 27 |
import ( |
| 28 |
`) |
| 29 |
|
| 30 |
for _, imp := range imports { |
| 31 |
if imp == "" { |
| 32 |
f.WriteString("\n") |
| 33 |
continue |
| 34 |
} |
| 35 |
f.WriteString("\t\"" + imp + "\"\n") |
| 36 |
} |
| 37 |
f.WriteString(")\n\n") |
| 38 |
|
| 39 |
template := ` |
| 40 |
|
| 41 |
// Decode a single Huffman block from f. |
| 42 |
// hl and hd are the Huffman states for the lit/length values |
| 43 |
// and the distance values, respectively. If hd == nil, using the |
| 44 |
// fixed distance encoding associated with fixed Huffman blocks. |
| 45 |
func (f *decompressor) $FUNCNAME$() { |
| 46 |
const ( |
| 47 |
stateInit = iota // Zero value must be stateInit |
| 48 |
stateDict |
| 49 |
) |
| 50 |
fr := f.r.($TYPE$) |
| 51 |
|
| 52 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 53 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 54 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 55 |
fnb, fb, dict := f.nb, f.b, &f.dict |
| 56 |
$PEEKTOP$ |
| 57 |
switch f.stepState { |
| 58 |
case stateInit: |
| 59 |
goto readLiteral |
| 60 |
case stateDict: |
| 61 |
goto copyHistory |
| 62 |
} |
| 63 |
|
| 64 |
readLiteral: |
| 65 |
// Read literal and/or (length, distance) according to RFC section 3.2.3. |
| 66 |
{ |
| 67 |
var v int |
| 68 |
{ |
| 69 |
// Inlined v, err := f.huffSym(f.hl) |
| 70 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 71 |
// with single element, huffSym must error on these two edge cases. In both |
| 72 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 73 |
// satisfy the n == 0 check below. |
| 74 |
n := uint(f.hl.maxRead) |
| 75 |
for { |
| 76 |
for fnb < n { |
| 77 |
$GETBYTE_S$ |
| 78 |
f.err = noEOF(err) |
| 79 |
$GETBYTE_E$ |
| 80 |
f.roffset++ |
| 81 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 82 |
fnb += 8 |
| 83 |
} |
| 84 |
chunk := f.hl.chunks[fb&(huffmanNumChunks-1)] |
| 85 |
n = uint(chunk & huffmanCountMask) |
| 86 |
if n > huffmanChunkBits { |
| 87 |
chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask] |
| 88 |
n = uint(chunk & huffmanCountMask) |
| 89 |
} |
| 90 |
if n <= fnb { |
| 91 |
if n == 0 { |
| 92 |
$DISCARD$ f.b, f.nb = fb, fnb |
| 93 |
if debugDecode { |
| 94 |
fmt.Println("huffsym: n==0") |
| 95 |
} |
| 96 |
f.err = CorruptInputError(f.roffset) |
| 97 |
return |
| 98 |
} |
| 99 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 100 |
fnb = fnb - n |
| 101 |
v = int(chunk >> huffmanValueShift) |
| 102 |
break |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
var length int |
| 108 |
switch { |
| 109 |
case v < 256: |
| 110 |
dict.writeByte(byte(v)) |
| 111 |
if dict.availWrite() == 0 { |
| 112 |
$DISCARD$ f.toRead = dict.readFlush() |
| 113 |
f.step = $FUNCNAME$ |
| 114 |
f.stepState = stateInit |
| 115 |
f.b, f.nb = fb, fnb |
| 116 |
return |
| 117 |
} |
| 118 |
goto readLiteral |
| 119 |
case v == 256: |
| 120 |
$DISCARD$ f.b, f.nb = fb, fnb |
| 121 |
f.finishBlock() |
| 122 |
return |
| 123 |
// otherwise, reference to older data |
| 124 |
case v < 265: |
| 125 |
length = v - (257 - 3) |
| 126 |
case v < maxNumLit: |
| 127 |
val := decCodeToLen[(v - 257)] |
| 128 |
length = int(val.length) + 3 |
| 129 |
n := uint(val.extra) |
| 130 |
for fnb < n { |
| 131 |
$GETBYTE_S$ |
| 132 |
if debugDecode { |
| 133 |
fmt.Println("morebits n>0:", err) |
| 134 |
} |
| 135 |
f.err = err |
| 136 |
$GETBYTE_E$ |
| 137 |
f.roffset++ |
| 138 |
fb |= uint32(c) << (fnb®mask.Shift32ByUint) |
| 139 |
fnb += 8 |
| 140 |
} |
| 141 |
length += int(fb & bitMask32[n]) |
| 142 |
fb >>= n & regmask.Shift32ByUint |
| 143 |
fnb -= n |
| 144 |
default: |
| 145 |
$DISCARD$ if debugDecode { |
| 146 |
fmt.Println(v, ">= maxNumLit") |
| 147 |
} |
| 148 |
f.err = CorruptInputError(f.roffset) |
| 149 |
f.b, f.nb = fb, fnb |
| 150 |
return |
| 151 |
} |
| 152 |
|
| 153 |
var dist uint32 |
| 154 |
if f.hd == nil { |
| 155 |
for fnb < 5 { |
| 156 |
$GETBYTE_S$ |
| 157 |
if debugDecode { |
| 158 |
fmt.Println("morebits f.nb<5:", err) |
| 159 |
} |
| 160 |
f.err = err |
| 161 |
$GETBYTE_E$ |
| 162 |
f.roffset++ |
| 163 |
fb |= uint32(c) << (fnb®mask.Shift32ByUint) |
| 164 |
fnb += 8 |
| 165 |
} |
| 166 |
dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3))) |
| 167 |
fb >>= 5 |
| 168 |
fnb -= 5 |
| 169 |
} else { |
| 170 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 171 |
// with single element, huffSym must error on these two edge cases. In both |
| 172 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 173 |
// satisfy the n == 0 check below. |
| 174 |
n := uint(f.hd.maxRead) |
| 175 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 176 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 177 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 178 |
for { |
| 179 |
for fnb < n { |
| 180 |
$GETBYTE_S$ |
| 181 |
f.err = noEOF(err) |
| 182 |
$GETBYTE_E$ |
| 183 |
f.roffset++ |
| 184 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 185 |
fnb += 8 |
| 186 |
} |
| 187 |
chunk := f.hd.chunks[fb&(huffmanNumChunks-1)] |
| 188 |
n = uint(chunk & huffmanCountMask) |
| 189 |
if n > huffmanChunkBits { |
| 190 |
chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask] |
| 191 |
n = uint(chunk & huffmanCountMask) |
| 192 |
} |
| 193 |
if n <= fnb { |
| 194 |
if n == 0 { |
| 195 |
$DISCARD$ f.b, f.nb = fb, fnb |
| 196 |
if debugDecode { |
| 197 |
fmt.Println("huffsym: n==0") |
| 198 |
} |
| 199 |
f.err = CorruptInputError(f.roffset) |
| 200 |
return |
| 201 |
} |
| 202 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 203 |
fnb = fnb - n |
| 204 |
dist = uint32(chunk >> huffmanValueShift) |
| 205 |
break |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
switch { |
| 211 |
case dist < 4: |
| 212 |
dist++ |
| 213 |
case dist < maxNumDist: |
| 214 |
nb := uint(dist-2) >> 1 |
| 215 |
// have 1 bit in bottom of dist, need nb more. |
| 216 |
extra := (dist & 1) << (nb & regmask.Shift32ByUint) |
| 217 |
for fnb < nb { |
| 218 |
$GETBYTE_S$ |
| 219 |
if debugDecode { |
| 220 |
fmt.Println("morebits f.nb<nb:", err) |
| 221 |
} |
| 222 |
f.err = err |
| 223 |
$GETBYTE_E$ |
| 224 |
f.roffset++ |
| 225 |
fb |= uint32(c) << (fnb®mask.Shift32ByUint) |
| 226 |
fnb += 8 |
| 227 |
} |
| 228 |
extra |= fb & bitMask32[nb] |
| 229 |
fb >>= nb & regmask.Shift32ByUint |
| 230 |
fnb -= nb |
| 231 |
dist = 1<<((nb+1)®mask.Shift32ByUint) + 1 + extra |
| 232 |
// slower: dist = bitMask32[nb+1] + 2 + extra |
| 233 |
default: |
| 234 |
$DISCARD$ f.b, f.nb = fb, fnb |
| 235 |
if debugDecode { |
| 236 |
fmt.Println("dist too big:", dist, maxNumDist) |
| 237 |
} |
| 238 |
f.err = CorruptInputError(f.roffset) |
| 239 |
return |
| 240 |
} |
| 241 |
|
| 242 |
// No check on length; encoding can be prescient. |
| 243 |
if dist > uint32(dict.histSize()) { |
| 244 |
$DISCARD$ f.b, f.nb = fb, fnb |
| 245 |
if debugDecode { |
| 246 |
fmt.Println("dist > dict.histSize():", dist, dict.histSize()) |
| 247 |
} |
| 248 |
f.err = CorruptInputError(f.roffset) |
| 249 |
return |
| 250 |
} |
| 251 |
|
| 252 |
f.copyLen, f.copyDist = length, int(dist) |
| 253 |
goto copyHistory |
| 254 |
} |
| 255 |
|
| 256 |
copyHistory: |
| 257 |
// Perform a backwards copy according to RFC section 3.2.3. |
| 258 |
{ |
| 259 |
cnt := dict.tryWriteCopy(f.copyDist, f.copyLen) |
| 260 |
if cnt == 0 { |
| 261 |
cnt = dict.writeCopy(f.copyDist, f.copyLen) |
| 262 |
} |
| 263 |
f.copyLen -= cnt |
| 264 |
|
| 265 |
if dict.availWrite() == 0 || f.copyLen > 0 { |
| 266 |
$DISCARD$ f.toRead = dict.readFlush() |
| 267 |
f.step = $FUNCNAME$ // We need to continue this work |
| 268 |
f.stepState = stateDict |
| 269 |
f.b, f.nb = fb, fnb |
| 270 |
return |
| 271 |
} |
| 272 |
goto readLiteral |
| 273 |
} |
| 274 |
// Not reached |
| 275 |
} |
| 276 |
|
| 277 |
` |
| 278 |
|
| 279 |
f.WriteString(` |
| 280 |
func peekBufio(fr *bufio.Reader) ([]byte, error) { |
| 281 |
if fr.Buffered() == 0 { |
| 282 |
if _, err := fr.Peek(1); err != nil && fr.Buffered() == 0 { |
| 283 |
return nil, err |
| 284 |
} |
| 285 |
} |
| 286 |
return fr.Peek(fr.Buffered()) |
| 287 |
} |
| 288 |
|
| 289 |
`) |
| 290 |
// Each variant pulls the next input byte differently. The default reads via |
| 291 |
// fr.ReadByte(). The *bufio.Reader variant reads from a Peek window so the |
| 292 |
// per-byte fill inlines as a slice index; since Peek only views the buffer and |
| 293 |
// Discard advances it, it must Discard consumed bytes before every return. |
| 294 |
// getS/getE wrap each read site around that site's own EOF handling: |
| 295 |
// <getS> <site-specific error body> <getE> |
| 296 |
// (output indentation is normalized by the go fmt step, so these need not be |
| 297 |
// pre-indented). |
| 298 |
type readImpl struct{ top, getS, getE, discard string } |
| 299 |
|
| 300 |
readByte := readImpl{ |
| 301 |
getS: `c, err := fr.ReadByte() |
| 302 |
if err != nil { |
| 303 |
f.b, f.nb = fb, fnb`, |
| 304 |
getE: `return |
| 305 |
}`, |
| 306 |
} |
| 307 |
peek := readImpl{ |
| 308 |
top: "pbuf, _ := fr.Peek(fr.Buffered())\npos := 0\n", |
| 309 |
getS: `if pos >= len(pbuf) { |
| 310 |
fr.Discard(pos) |
| 311 |
var err error |
| 312 |
pbuf, err = peekBufio(fr) |
| 313 |
pos = 0 |
| 314 |
if len(pbuf) == 0 { |
| 315 |
f.b, f.nb = fb, fnb`, |
| 316 |
getE: `return |
| 317 |
} |
| 318 |
} |
| 319 |
c := pbuf[pos] |
| 320 |
pos++`, |
| 321 |
discard: "fr.Discard(pos)\n", |
| 322 |
} |
| 323 |
|
| 324 |
for i, t := range types { |
| 325 |
r := readByte |
| 326 |
if t == "*bufio.Reader" { |
| 327 |
r = peek |
| 328 |
} |
| 329 |
f.WriteString(strings.NewReplacer( |
| 330 |
"$FUNCNAME$", "huffman"+names[i], |
| 331 |
"$TYPE$", t, |
| 332 |
"$PEEKTOP$", r.top, |
| 333 |
"$GETBYTE_S$", r.getS, |
| 334 |
"$GETBYTE_E$", r.getE, |
| 335 |
"$DISCARD$", r.discard, |
| 336 |
).Replace(template)) |
| 337 |
} |
| 338 |
|
| 339 |
f.WriteString("func (f *decompressor) huffmanBlockDecoder() {\n") |
| 340 |
f.WriteString("\tswitch f.r.(type) {\n") |
| 341 |
for i, t := range types { |
| 342 |
f.WriteString("\t\tcase " + t + ":\n") |
| 343 |
f.WriteString("\t\t\tf.huffman" + names[i] + "()\n") |
| 344 |
} |
| 345 |
f.WriteString("\t\tdefault:\n") |
| 346 |
f.WriteString("\t\t\tf.huffmanGenericReader()\n") |
| 347 |
f.WriteString("\t}\n}\n") |
| 348 |
} |