update: macaron cores,gzip,session (#10522)
Co-authored-by: zeripath <art27@cantab.net>tokarchuk/v1.17
parent
694f44660f
commit
8d2059a201
@ -0,0 +1,24 @@ |
||||
kind: pipeline |
||||
name: go1-1-2 |
||||
|
||||
steps: |
||||
- name: test |
||||
image: golang:1.12 |
||||
environment: |
||||
GOPROXY: https://goproxy.cn |
||||
commands: |
||||
- go build -v |
||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic |
||||
|
||||
--- |
||||
kind: pipeline |
||||
name: go1-1-3 |
||||
|
||||
steps: |
||||
- name: test |
||||
image: golang:1.13 |
||||
environment: |
||||
GOPROXY: https://goproxy.cn |
||||
commands: |
||||
- go build -v |
||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic |
@ -0,0 +1,19 @@ |
||||
# gzip |
||||
|
||||
Middleware gzip provides gzip comparess middleware for [Macaron](https://gitea.com/macaron/macaron). |
||||
|
||||
### Installation |
||||
|
||||
go get gitea.com/macaron/gzip |
||||
|
||||
## Getting Help |
||||
|
||||
- [API Reference](https://godoc.org/gitea.com/macaron/gzip) |
||||
|
||||
## Credits |
||||
|
||||
This package is a modified version of [go-macaron gzip](github.com/go-macaron/gzip). |
||||
|
||||
## License |
||||
|
||||
This project is under the Apache License, Version 2.0. See the [LICENSE](LICENSE) file for the full license text. |
@ -0,0 +1,274 @@ |
||||
// +build generate
|
||||
|
||||
//go:generate go run $GOFILE && gofmt -w inflate_gen.go
|
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"os" |
||||
"strings" |
||||
) |
||||
|
||||
func main() { |
||||
f, err := os.Create("inflate_gen.go") |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
defer f.Close() |
||||
types := []string{"*bytes.Buffer", "*bytes.Reader", "*bufio.Reader", "*strings.Reader"} |
||||
names := []string{"BytesBuffer", "BytesReader", "BufioReader", "StringsReader"} |
||||
imports := []string{"bytes", "bufio", "io", "strings", "math/bits"} |
||||
f.WriteString(`// Code generated by go generate gen_inflate.go. DO NOT EDIT.
|
||||
|
||||
package flate |
||||
|
||||
import ( |
||||
`) |
||||
|
||||
for _, imp := range imports { |
||||
f.WriteString("\t\"" + imp + "\"\n") |
||||
} |
||||
f.WriteString(")\n\n") |
||||
|
||||
template := ` |
||||
|
||||
// Decode a single Huffman block from f.
|
||||
// hl and hd are the Huffman states for the lit/length values
|
||||
// and the distance values, respectively. If hd == nil, using the
|
||||
// fixed distance encoding associated with fixed Huffman blocks.
|
||||
func (f *decompressor) $FUNCNAME$() { |
||||
const ( |
||||
stateInit = iota // Zero value must be stateInit
|
||||
stateDict |
||||
) |
||||
fr := f.r.($TYPE$) |
||||
moreBits := func() error { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
return noEOF(err) |
||||
} |
||||
f.roffset++ |
||||
f.b |= uint32(c) << f.nb |
||||
f.nb += 8 |
||||
return nil |
||||
} |
||||
|
||||
switch f.stepState { |
||||
case stateInit: |
||||
goto readLiteral |
||||
case stateDict: |
||||
goto copyHistory |
||||
} |
||||
|
||||
readLiteral: |
||||
// Read literal and/or (length, distance) according to RFC section 3.2.3.
|
||||
{ |
||||
var v int |
||||
{ |
||||
// Inlined v, err := f.huffSym(f.hl)
|
||||
// Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
||||
// with single element, huffSym must error on these two edge cases. In both
|
||||
// cases, the chunks slice will be 0 for the invalid sequence, leading it
|
||||
// satisfy the n == 0 check below.
|
||||
n := uint(f.hl.maxRead) |
||||
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
||||
// but is smart enough to keep local variables in registers, so use nb and b,
|
||||
// inline call to moreBits and reassign b,nb back to f on return.
|
||||
nb, b := f.nb, f.b |
||||
for { |
||||
for nb < n { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
f.b = b |
||||
f.nb = nb |
||||
f.err = noEOF(err) |
||||
return |
||||
} |
||||
f.roffset++ |
||||
b |= uint32(c) << (nb & 31) |
||||
nb += 8 |
||||
} |
||||
chunk := f.hl.chunks[b&(huffmanNumChunks-1)] |
||||
n = uint(chunk & huffmanCountMask) |
||||
if n > huffmanChunkBits { |
||||
chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask] |
||||
n = uint(chunk & huffmanCountMask) |
||||
} |
||||
if n <= nb { |
||||
if n == 0 { |
||||
f.b = b |
||||
f.nb = nb |
||||
if debugDecode { |
||||
fmt.Println("huffsym: n==0") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
f.b = b >> (n & 31) |
||||
f.nb = nb - n |
||||
v = int(chunk >> huffmanValueShift) |
||||
break |
||||
} |
||||
} |
||||
} |
||||
|
||||
var n uint // number of bits extra
|
||||
var length int |
||||
var err error |
||||
switch { |
||||
case v < 256: |
||||
f.dict.writeByte(byte(v)) |
||||
if f.dict.availWrite() == 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).$FUNCNAME$ |
||||
f.stepState = stateInit |
||||
return |
||||
} |
||||
goto readLiteral |
||||
case v == 256: |
||||
f.finishBlock() |
||||
return |
||||
// otherwise, reference to older data
|
||||
case v < 265: |
||||
length = v - (257 - 3) |
||||
n = 0 |
||||
case v < 269: |
||||
length = v*2 - (265*2 - 11) |
||||
n = 1 |
||||
case v < 273: |
||||
length = v*4 - (269*4 - 19) |
||||
n = 2 |
||||
case v < 277: |
||||
length = v*8 - (273*8 - 35) |
||||
n = 3 |
||||
case v < 281: |
||||
length = v*16 - (277*16 - 67) |
||||
n = 4 |
||||
case v < 285: |
||||
length = v*32 - (281*32 - 131) |
||||
n = 5 |
||||
case v < maxNumLit: |
||||
length = 258 |
||||
n = 0 |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println(v, ">= maxNumLit") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
if n > 0 { |
||||
for f.nb < n { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits n>0:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
length += int(f.b & uint32(1<<n-1)) |
||||
f.b >>= n |
||||
f.nb -= n |
||||
} |
||||
|
||||
var dist int |
||||
if f.hd == nil { |
||||
for f.nb < 5 { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<5:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3))) |
||||
f.b >>= 5 |
||||
f.nb -= 5 |
||||
} else { |
||||
if dist, err = f.huffSym(f.hd); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("huffsym:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
|
||||
switch { |
||||
case dist < 4: |
||||
dist++ |
||||
case dist < maxNumDist: |
||||
nb := uint(dist-2) >> 1 |
||||
// have 1 bit in bottom of dist, need nb more.
|
||||
extra := (dist & 1) << nb |
||||
for f.nb < nb { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<nb:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
extra |= int(f.b & uint32(1<<nb-1)) |
||||
f.b >>= nb |
||||
f.nb -= nb |
||||
dist = 1<<(nb+1) + 1 + extra |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println("dist too big:", dist, maxNumDist) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
// No check on length; encoding can be prescient.
|
||||
if dist > f.dict.histSize() { |
||||
if debugDecode { |
||||
fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize()) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
f.copyLen, f.copyDist = length, dist |
||||
goto copyHistory |
||||
} |
||||
|
||||
copyHistory: |
||||
// Perform a backwards copy according to RFC section 3.2.3.
|
||||
{ |
||||
cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen) |
||||
if cnt == 0 { |
||||
cnt = f.dict.writeCopy(f.copyDist, f.copyLen) |
||||
} |
||||
f.copyLen -= cnt |
||||
|
||||
if f.dict.availWrite() == 0 || f.copyLen > 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).$FUNCNAME$ // We need to continue this work
|
||||
f.stepState = stateDict |
||||
return |
||||
} |
||||
goto readLiteral |
||||
} |
||||
} |
||||
|
||||
` |
||||
for i, t := range types { |
||||
s := strings.Replace(template, "$FUNCNAME$", "huffman"+names[i], -1) |
||||
s = strings.Replace(s, "$TYPE$", t, -1) |
||||
f.WriteString(s) |
||||
} |
||||
f.WriteString("func (f *decompressor) huffmanBlockDecoder() func() {\n") |
||||
f.WriteString("\tswitch f.r.(type) {\n") |
||||
for i, t := range types { |
||||
f.WriteString("\t\tcase " + t + ":\n") |
||||
f.WriteString("\t\t\treturn f.huffman" + names[i] + "\n") |
||||
} |
||||
f.WriteString("\t\tdefault:\n") |
||||
f.WriteString("\t\t\treturn f.huffmanBlockGeneric") |
||||
f.WriteString("\t}\n}\n") |
||||
} |
@ -0,0 +1,178 @@ |
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package flate |
||||
|
||||
// Sort sorts data.
|
||||
// It makes one call to data.Len to determine n, and O(n*log(n)) calls to
|
||||
// data.Less and data.Swap. The sort is not guaranteed to be stable.
|
||||
func sortByFreq(data []literalNode) { |
||||
n := len(data) |
||||
quickSortByFreq(data, 0, n, maxDepth(n)) |
||||
} |
||||
|
||||
func quickSortByFreq(data []literalNode, a, b, maxDepth int) { |
||||
for b-a > 12 { // Use ShellSort for slices <= 12 elements
|
||||
if maxDepth == 0 { |
||||
heapSort(data, a, b) |
||||
return |
||||
} |
||||
maxDepth-- |
||||
mlo, mhi := doPivotByFreq(data, a, b) |
||||
// Avoiding recursion on the larger subproblem guarantees
|
||||
// a stack depth of at most lg(b-a).
|
||||
if mlo-a < b-mhi { |
||||
quickSortByFreq(data, a, mlo, maxDepth) |
||||
a = mhi // i.e., quickSortByFreq(data, mhi, b)
|
||||
} else { |
||||
quickSortByFreq(data, mhi, b, maxDepth) |
||||
b = mlo // i.e., quickSortByFreq(data, a, mlo)
|
||||
} |
||||
} |
||||
if b-a > 1 { |
||||
// Do ShellSort pass with gap 6
|
||||
// It could be written in this simplified form cause b-a <= 12
|
||||
for i := a + 6; i < b; i++ { |
||||
if data[i].freq == data[i-6].freq && data[i].literal < data[i-6].literal || data[i].freq < data[i-6].freq { |
||||
data[i], data[i-6] = data[i-6], data[i] |
||||
} |
||||
} |
||||
insertionSortByFreq(data, a, b) |
||||
} |
||||
} |
||||
|
||||
// siftDownByFreq implements the heap property on data[lo, hi).
|
||||
// first is an offset into the array where the root of the heap lies.
|
||||
func siftDownByFreq(data []literalNode, lo, hi, first int) { |
||||
root := lo |
||||
for { |
||||
child := 2*root + 1 |
||||
if child >= hi { |
||||
break |
||||
} |
||||
if child+1 < hi && (data[first+child].freq == data[first+child+1].freq && data[first+child].literal < data[first+child+1].literal || data[first+child].freq < data[first+child+1].freq) { |
||||
child++ |
||||
} |
||||
if data[first+root].freq == data[first+child].freq && data[first+root].literal > data[first+child].literal || data[first+root].freq > data[first+child].freq { |
||||
return |
||||
} |
||||
data[first+root], data[first+child] = data[first+child], data[first+root] |
||||
root = child |
||||
} |
||||
} |
||||
func doPivotByFreq(data []literalNode, lo, hi int) (midlo, midhi int) { |
||||
m := int(uint(lo+hi) >> 1) // Written like this to avoid integer overflow.
|
||||
if hi-lo > 40 { |
||||
// Tukey's ``Ninther,'' median of three medians of three.
|
||||
s := (hi - lo) / 8 |
||||
medianOfThreeSortByFreq(data, lo, lo+s, lo+2*s) |
||||
medianOfThreeSortByFreq(data, m, m-s, m+s) |
||||
medianOfThreeSortByFreq(data, hi-1, hi-1-s, hi-1-2*s) |
||||
} |
||||
medianOfThreeSortByFreq(data, lo, m, hi-1) |
||||
|
||||
// Invariants are:
|
||||
// data[lo] = pivot (set up by ChoosePivot)
|
||||
// data[lo < i < a] < pivot
|
||||
// data[a <= i < b] <= pivot
|
||||
// data[b <= i < c] unexamined
|
||||
// data[c <= i < hi-1] > pivot
|
||||
// data[hi-1] >= pivot
|
||||
pivot := lo |
||||
a, c := lo+1, hi-1 |
||||
|
||||
for ; a < c && (data[a].freq == data[pivot].freq && data[a].literal < data[pivot].literal || data[a].freq < data[pivot].freq); a++ { |
||||
} |
||||
b := a |
||||
for { |
||||
for ; b < c && (data[pivot].freq == data[b].freq && data[pivot].literal > data[b].literal || data[pivot].freq > data[b].freq); b++ { // data[b] <= pivot
|
||||
} |
||||
for ; b < c && (data[pivot].freq == data[c-1].freq && data[pivot].literal < data[c-1].literal || data[pivot].freq < data[c-1].freq); c-- { // data[c-1] > pivot
|
||||
} |
||||
if b >= c { |
||||
break |
||||
} |
||||
// data[b] > pivot; data[c-1] <= pivot
|
||||
data[b], data[c-1] = data[c-1], data[b] |
||||
b++ |
||||
c-- |
||||
} |
||||
// If hi-c<3 then there are duplicates (by property of median of nine).
|
||||
// Let's be a bit more conservative, and set border to 5.
|
||||
protect := hi-c < 5 |
||||
if !protect && hi-c < (hi-lo)/4 { |
||||
// Lets test some points for equality to pivot
|
||||
dups := 0 |
||||
if data[pivot].freq == data[hi-1].freq && data[pivot].literal > data[hi-1].literal || data[pivot].freq > data[hi-1].freq { // data[hi-1] = pivot
|
||||
data[c], data[hi-1] = data[hi-1], data[c] |
||||
c++ |
||||
dups++ |
||||
} |
||||
if data[b-1].freq == data[pivot].freq && data[b-1].literal > data[pivot].literal || data[b-1].freq > data[pivot].freq { // data[b-1] = pivot
|
||||
b-- |
||||
dups++ |
||||
} |
||||
// m-lo = (hi-lo)/2 > 6
|
||||
// b-lo > (hi-lo)*3/4-1 > 8
|
||||
// ==> m < b ==> data[m] <= pivot
|
||||
if data[m].freq == data[pivot].freq && data[m].literal > data[pivot].literal || data[m].freq > data[pivot].freq { // data[m] = pivot
|
||||
data[m], data[b-1] = data[b-1], data[m] |
||||
b-- |
||||
dups++ |
||||
} |
||||
// if at least 2 points are equal to pivot, assume skewed distribution
|
||||
protect = dups > 1 |
||||
} |
||||
if protect { |
||||
// Protect against a lot of duplicates
|
||||
// Add invariant:
|
||||
// data[a <= i < b] unexamined
|
||||
// data[b <= i < c] = pivot
|
||||
for { |
||||
for ; a < b && (data[b-1].freq == data[pivot].freq && data[b-1].literal > data[pivot].literal || data[b-1].freq > data[pivot].freq); b-- { // data[b] == pivot
|
||||
} |
||||
for ; a < b && (data[a].freq == data[pivot].freq && data[a].literal < data[pivot].literal || data[a].freq < data[pivot].freq); a++ { // data[a] < pivot
|
||||
} |
||||
if a >= b { |
||||
break |
||||
} |
||||
// data[a] == pivot; data[b-1] < pivot
|
||||
data[a], data[b-1] = data[b-1], data[a] |
||||
a++ |
||||
b-- |
||||
} |
||||
} |
||||
// Swap pivot into middle
|
||||
data[pivot], data[b-1] = data[b-1], data[pivot] |
||||
return b - 1, c |
||||
} |
||||
|
||||
// Insertion sort
|
||||
func insertionSortByFreq(data []literalNode, a, b int) { |
||||
for i := a + 1; i < b; i++ { |
||||
for j := i; j > a && (data[j].freq == data[j-1].freq && data[j].literal < data[j-1].literal || data[j].freq < data[j-1].freq); j-- { |
||||
data[j], data[j-1] = data[j-1], data[j] |
||||
} |
||||
} |
||||
} |
||||
|
||||
// quickSortByFreq, loosely following Bentley and McIlroy,
|
||||
// ``Engineering a Sort Function,'' SP&E November 1993.
|
||||
|
||||
// medianOfThreeSortByFreq moves the median of the three values data[m0], data[m1], data[m2] into data[m1].
|
||||
func medianOfThreeSortByFreq(data []literalNode, m1, m0, m2 int) { |
||||
// sort 3 elements
|
||||
if data[m1].freq == data[m0].freq && data[m1].literal < data[m0].literal || data[m1].freq < data[m0].freq { |
||||
data[m1], data[m0] = data[m0], data[m1] |
||||
} |
||||
// data[m0] <= data[m1]
|
||||
if data[m2].freq == data[m1].freq && data[m2].literal < data[m1].literal || data[m2].freq < data[m1].freq { |
||||
data[m2], data[m1] = data[m1], data[m2] |
||||
// data[m0] <= data[m2] && data[m1] < data[m2]
|
||||
if data[m1].freq == data[m0].freq && data[m1].literal < data[m0].literal || data[m1].freq < data[m0].freq { |
||||
data[m1], data[m0] = data[m0], data[m1] |
||||
} |
||||
} |
||||
// now data[m0] <= data[m1] <= data[m2]
|
||||
} |
@ -0,0 +1,201 @@ |
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package flate |
||||
|
||||
// Sort sorts data.
|
||||
// It makes one call to data.Len to determine n, and O(n*log(n)) calls to
|
||||
// data.Less and data.Swap. The sort is not guaranteed to be stable.
|
||||
func sortByLiteral(data []literalNode) { |
||||
n := len(data) |
||||
quickSort(data, 0, n, maxDepth(n)) |
||||
} |
||||
|
||||
func quickSort(data []literalNode, a, b, maxDepth int) { |
||||
for b-a > 12 { // Use ShellSort for slices <= 12 elements
|
||||
if maxDepth == 0 { |
||||
heapSort(data, a, b) |
||||
return |
||||
} |
||||
maxDepth-- |
||||
mlo, mhi := doPivot(data, a, b) |
||||
// Avoiding recursion on the larger subproblem guarantees
|
||||
// a stack depth of at most lg(b-a).
|
||||
if mlo-a < b-mhi { |
||||
quickSort(data, a, mlo, maxDepth) |
||||
a = mhi // i.e., quickSort(data, mhi, b)
|
||||
} else { |
||||
quickSort(data, mhi, b, maxDepth) |
||||
b = mlo // i.e., quickSort(data, a, mlo)
|
||||
} |
||||
} |
||||
if b-a > 1 { |
||||
// Do ShellSort pass with gap 6
|
||||
// It could be written in this simplified form cause b-a <= 12
|
||||
for i := a + 6; i < b; i++ { |
||||
if data[i].literal < data[i-6].literal { |
||||
data[i], data[i-6] = data[i-6], data[i] |
||||
} |
||||
} |
||||
insertionSort(data, a, b) |
||||
} |
||||
} |
||||
func heapSort(data []literalNode, a, b int) { |
||||
first := a |
||||
lo := 0 |
||||
hi := b - a |
||||
|
||||
// Build heap with greatest element at top.
|
||||
for i := (hi - 1) / 2; i >= 0; i-- { |
||||
siftDown(data, i, hi, first) |
||||
} |
||||
|
||||
// Pop elements, largest first, into end of data.
|
||||
for i := hi - 1; i >= 0; i-- { |
||||
data[first], data[first+i] = data[first+i], data[first] |
||||
siftDown(data, lo, i, first) |
||||
} |
||||
} |
||||
|
||||
// siftDown implements the heap property on data[lo, hi).
|
||||
// first is an offset into the array where the root of the heap lies.
|
||||
func siftDown(data []literalNode, lo, hi, first int) { |
||||
root := lo |
||||
for { |
||||
child := 2*root + 1 |
||||
if child >= hi { |
||||
break |
||||
} |
||||
if child+1 < hi && data[first+child].literal < data[first+child+1].literal { |
||||
child++ |
||||
} |
||||
if data[first+root].literal > data[first+child].literal { |
||||
return |
||||
} |
||||
data[first+root], data[first+child] = data[first+child], data[first+root] |
||||
root = child |
||||
} |
||||
} |
||||
func doPivot(data []literalNode, lo, hi int) (midlo, midhi int) { |
||||
m := int(uint(lo+hi) >> 1) // Written like this to avoid integer overflow.
|
||||
if hi-lo > 40 { |
||||
// Tukey's ``Ninther,'' median of three medians of three.
|
||||
s := (hi - lo) / 8 |
||||
medianOfThree(data, lo, lo+s, lo+2*s) |
||||
medianOfThree(data, m, m-s, m+s) |
||||
medianOfThree(data, hi-1, hi-1-s, hi-1-2*s) |
||||
} |
||||
medianOfThree(data, lo, m, hi-1) |
||||
|
||||
// Invariants are:
|
||||
// data[lo] = pivot (set up by ChoosePivot)
|
||||
// data[lo < i < a] < pivot
|
||||
// data[a <= i < b] <= pivot
|
||||
// data[b <= i < c] unexamined
|
||||
// data[c <= i < hi-1] > pivot
|
||||
// data[hi-1] >= pivot
|
||||
pivot := lo |
||||
a, c := lo+1, hi-1 |
||||
|
||||
for ; a < c && data[a].literal < data[pivot].literal; a++ { |
||||
} |
||||
b := a |
||||
for { |
||||
for ; b < c && data[pivot].literal > data[b].literal; b++ { // data[b] <= pivot
|
||||
} |
||||
for ; b < c && data[pivot].literal < data[c-1].literal; c-- { // data[c-1] > pivot
|
||||
} |
||||
if b >= c { |
||||
break |
||||
} |
||||
// data[b] > pivot; data[c-1] <= pivot
|
||||
data[b], data[c-1] = data[c-1], data[b] |
||||
b++ |
||||
c-- |
||||
} |
||||
// If hi-c<3 then there are duplicates (by property of median of nine).
|
||||
// Let's be a bit more conservative, and set border to 5.
|
||||
protect := hi-c < 5 |
||||
if !protect && hi-c < (hi-lo)/4 { |
||||
// Lets test some points for equality to pivot
|
||||
dups := 0 |
||||
if data[pivot].literal > data[hi-1].literal { // data[hi-1] = pivot
|
||||
data[c], data[hi-1] = data[hi-1], data[c] |
||||
c++ |
||||
dups++ |
||||
} |
||||
if data[b-1].literal > data[pivot].literal { // data[b-1] = pivot
|
||||
b-- |
||||
dups++ |
||||
} |
||||
// m-lo = (hi-lo)/2 > 6
|
||||
// b-lo > (hi-lo)*3/4-1 > 8
|
||||
// ==> m < b ==> data[m] <= pivot
|
||||
if data[m].literal > data[pivot].literal { // data[m] = pivot
|
||||
data[m], data[b-1] = data[b-1], data[m] |
||||
b-- |
||||
dups++ |
||||
} |
||||
// if at least 2 points are equal to pivot, assume skewed distribution
|
||||
protect = dups > 1 |
||||
} |
||||
if protect { |
||||
// Protect against a lot of duplicates
|
||||
// Add invariant:
|
||||
// data[a <= i < b] unexamined
|
||||
// data[b <= i < c] = pivot
|
||||
for { |
||||
for ; a < b && data[b-1].literal > data[pivot].literal; b-- { // data[b] == pivot
|
||||
} |
||||
for ; a < b && data[a].literal < data[pivot].literal; a++ { // data[a] < pivot
|
||||
} |
||||
if a >= b { |
||||
break |
||||
} |
||||
// data[a] == pivot; data[b-1] < pivot
|
||||
data[a], data[b-1] = data[b-1], data[a] |
||||
a++ |
||||
b-- |
||||
} |
||||
} |
||||
// Swap pivot into middle
|
||||
data[pivot], data[b-1] = data[b-1], data[pivot] |
||||
return b - 1, c |
||||
} |
||||
|
||||
// Insertion sort
|
||||
func insertionSort(data []literalNode, a, b int) { |
||||
for i := a + 1; i < b; i++ { |
||||
for j := i; j > a && data[j].literal < data[j-1].literal; j-- { |
||||
data[j], data[j-1] = data[j-1], data[j] |
||||
} |
||||
} |
||||
} |
||||
|
||||
// maxDepth returns a threshold at which quicksort should switch
|
||||
// to heapsort. It returns 2*ceil(lg(n+1)).
|
||||
func maxDepth(n int) int { |
||||
var depth int |
||||
for i := n; i > 0; i >>= 1 { |
||||
depth++ |
||||
} |
||||
return depth * 2 |
||||
} |
||||
|
||||
// medianOfThree moves the median of the three values data[m0], data[m1], data[m2] into data[m1].
|
||||
func medianOfThree(data []literalNode, m1, m0, m2 int) { |
||||
// sort 3 elements
|
||||
if data[m1].literal < data[m0].literal { |
||||
data[m1], data[m0] = data[m0], data[m1] |
||||
} |
||||
// data[m0] <= data[m1]
|
||||
if data[m2].literal < data[m1].literal { |
||||
data[m2], data[m1] = data[m1], data[m2] |
||||
// data[m0] <= data[m2] && data[m1] < data[m2]
|
||||
if data[m1].literal < data[m0].literal { |
||||
data[m1], data[m0] = data[m0], data[m1] |
||||
} |
||||
} |
||||
// now data[m0] <= data[m1] <= data[m2]
|
||||
} |
@ -0,0 +1,922 @@ |
||||
// Code generated by go generate gen_inflate.go. DO NOT EDIT.
|
||||
|
||||
package flate |
||||
|
||||
import ( |
||||
"bufio" |
||||
"bytes" |
||||
"fmt" |
||||
"math/bits" |
||||
"strings" |
||||
) |
||||
|
||||
// Decode a single Huffman block from f.
|
||||
// hl and hd are the Huffman states for the lit/length values
|
||||
// and the distance values, respectively. If hd == nil, using the
|
||||
// fixed distance encoding associated with fixed Huffman blocks.
|
||||
func (f *decompressor) huffmanBytesBuffer() { |
||||
const ( |
||||
stateInit = iota // Zero value must be stateInit
|
||||
stateDict |
||||
) |
||||
fr := f.r.(*bytes.Buffer) |
||||
moreBits := func() error { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
return noEOF(err) |
||||
} |
||||
f.roffset++ |
||||
f.b |= uint32(c) << f.nb |
||||
f.nb += 8 |
||||
return nil |
||||
} |
||||
|
||||
switch f.stepState { |
||||
case stateInit: |
||||
goto readLiteral |
||||
case stateDict: |
||||
goto copyHistory |
||||
} |
||||
|
||||
readLiteral: |
||||
// Read literal and/or (length, distance) according to RFC section 3.2.3.
|
||||
{ |
||||
var v int |
||||
{ |
||||
// Inlined v, err := f.huffSym(f.hl)
|
||||
// Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
||||
// with single element, huffSym must error on these two edge cases. In both
|
||||
// cases, the chunks slice will be 0 for the invalid sequence, leading it
|
||||
// satisfy the n == 0 check below.
|
||||
n := uint(f.hl.maxRead) |
||||
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
||||
// but is smart enough to keep local variables in registers, so use nb and b,
|
||||
// inline call to moreBits and reassign b,nb back to f on return.
|
||||
nb, b := f.nb, f.b |
||||
for { |
||||
for nb < n { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
f.b = b |
||||
f.nb = nb |
||||
f.err = noEOF(err) |
||||
return |
||||
} |
||||
f.roffset++ |
||||
b |= uint32(c) << (nb & 31) |
||||
nb += 8 |
||||
} |
||||
chunk := f.hl.chunks[b&(huffmanNumChunks-1)] |
||||
n = uint(chunk & huffmanCountMask) |
||||
if n > huffmanChunkBits { |
||||
chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask] |
||||
n = uint(chunk & huffmanCountMask) |
||||
} |
||||
if n <= nb { |
||||
if n == 0 { |
||||
f.b = b |
||||
f.nb = nb |
||||
if debugDecode { |
||||
fmt.Println("huffsym: n==0") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
f.b = b >> (n & 31) |
||||
f.nb = nb - n |
||||
v = int(chunk >> huffmanValueShift) |
||||
break |
||||
} |
||||
} |
||||
} |
||||
|
||||
var n uint // number of bits extra
|
||||
var length int |
||||
var err error |
||||
switch { |
||||
case v < 256: |
||||
f.dict.writeByte(byte(v)) |
||||
if f.dict.availWrite() == 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).huffmanBytesBuffer |
||||
f.stepState = stateInit |
||||
return |
||||
} |
||||
goto readLiteral |
||||
case v == 256: |
||||
f.finishBlock() |
||||
return |
||||
// otherwise, reference to older data
|
||||
case v < 265: |
||||
length = v - (257 - 3) |
||||
n = 0 |
||||
case v < 269: |
||||
length = v*2 - (265*2 - 11) |
||||
n = 1 |
||||
case v < 273: |
||||
length = v*4 - (269*4 - 19) |
||||
n = 2 |
||||
case v < 277: |
||||
length = v*8 - (273*8 - 35) |
||||
n = 3 |
||||
case v < 281: |
||||
length = v*16 - (277*16 - 67) |
||||
n = 4 |
||||
case v < 285: |
||||
length = v*32 - (281*32 - 131) |
||||
n = 5 |
||||
case v < maxNumLit: |
||||
length = 258 |
||||
n = 0 |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println(v, ">= maxNumLit") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
if n > 0 { |
||||
for f.nb < n { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits n>0:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
length += int(f.b & uint32(1<<n-1)) |
||||
f.b >>= n |
||||
f.nb -= n |
||||
} |
||||
|
||||
var dist int |
||||
if f.hd == nil { |
||||
for f.nb < 5 { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<5:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3))) |
||||
f.b >>= 5 |
||||
f.nb -= 5 |
||||
} else { |
||||
if dist, err = f.huffSym(f.hd); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("huffsym:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
|
||||
switch { |
||||
case dist < 4: |
||||
dist++ |
||||
case dist < maxNumDist: |
||||
nb := uint(dist-2) >> 1 |
||||
// have 1 bit in bottom of dist, need nb more.
|
||||
extra := (dist & 1) << nb |
||||
for f.nb < nb { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<nb:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
extra |= int(f.b & uint32(1<<nb-1)) |
||||
f.b >>= nb |
||||
f.nb -= nb |
||||
dist = 1<<(nb+1) + 1 + extra |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println("dist too big:", dist, maxNumDist) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
// No check on length; encoding can be prescient.
|
||||
if dist > f.dict.histSize() { |
||||
if debugDecode { |
||||
fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize()) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
f.copyLen, f.copyDist = length, dist |
||||
goto copyHistory |
||||
} |
||||
|
||||
copyHistory: |
||||
// Perform a backwards copy according to RFC section 3.2.3.
|
||||
{ |
||||
cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen) |
||||
if cnt == 0 { |
||||
cnt = f.dict.writeCopy(f.copyDist, f.copyLen) |
||||
} |
||||
f.copyLen -= cnt |
||||
|
||||
if f.dict.availWrite() == 0 || f.copyLen > 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).huffmanBytesBuffer // We need to continue this work
|
||||
f.stepState = stateDict |
||||
return |
||||
} |
||||
goto readLiteral |
||||
} |
||||
} |
||||
|
||||
// Decode a single Huffman block from f.
|
||||
// hl and hd are the Huffman states for the lit/length values
|
||||
// and the distance values, respectively. If hd == nil, using the
|
||||
// fixed distance encoding associated with fixed Huffman blocks.
|
||||
func (f *decompressor) huffmanBytesReader() { |
||||
const ( |
||||
stateInit = iota // Zero value must be stateInit
|
||||
stateDict |
||||
) |
||||
fr := f.r.(*bytes.Reader) |
||||
moreBits := func() error { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
return noEOF(err) |
||||
} |
||||
f.roffset++ |
||||
f.b |= uint32(c) << f.nb |
||||
f.nb += 8 |
||||
return nil |
||||
} |
||||
|
||||
switch f.stepState { |
||||
case stateInit: |
||||
goto readLiteral |
||||
case stateDict: |
||||
goto copyHistory |
||||
} |
||||
|
||||
readLiteral: |
||||
// Read literal and/or (length, distance) according to RFC section 3.2.3.
|
||||
{ |
||||
var v int |
||||
{ |
||||
// Inlined v, err := f.huffSym(f.hl)
|
||||
// Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
||||
// with single element, huffSym must error on these two edge cases. In both
|
||||
// cases, the chunks slice will be 0 for the invalid sequence, leading it
|
||||
// satisfy the n == 0 check below.
|
||||
n := uint(f.hl.maxRead) |
||||
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
||||
// but is smart enough to keep local variables in registers, so use nb and b,
|
||||
// inline call to moreBits and reassign b,nb back to f on return.
|
||||
nb, b := f.nb, f.b |
||||
for { |
||||
for nb < n { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
f.b = b |
||||
f.nb = nb |
||||
f.err = noEOF(err) |
||||
return |
||||
} |
||||
f.roffset++ |
||||
b |= uint32(c) << (nb & 31) |
||||
nb += 8 |
||||
} |
||||
chunk := f.hl.chunks[b&(huffmanNumChunks-1)] |
||||
n = uint(chunk & huffmanCountMask) |
||||
if n > huffmanChunkBits { |
||||
chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask] |
||||
n = uint(chunk & huffmanCountMask) |
||||
} |
||||
if n <= nb { |
||||
if n == 0 { |
||||
f.b = b |
||||
f.nb = nb |
||||
if debugDecode { |
||||
fmt.Println("huffsym: n==0") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
f.b = b >> (n & 31) |
||||
f.nb = nb - n |
||||
v = int(chunk >> huffmanValueShift) |
||||
break |
||||
} |
||||
} |
||||
} |
||||
|
||||
var n uint // number of bits extra
|
||||
var length int |
||||
var err error |
||||
switch { |
||||
case v < 256: |
||||
f.dict.writeByte(byte(v)) |
||||
if f.dict.availWrite() == 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).huffmanBytesReader |
||||
f.stepState = stateInit |
||||
return |
||||
} |
||||
goto readLiteral |
||||
case v == 256: |
||||
f.finishBlock() |
||||
return |
||||
// otherwise, reference to older data
|
||||
case v < 265: |
||||
length = v - (257 - 3) |
||||
n = 0 |
||||
case v < 269: |
||||
length = v*2 - (265*2 - 11) |
||||
n = 1 |
||||
case v < 273: |
||||
length = v*4 - (269*4 - 19) |
||||
n = 2 |
||||
case v < 277: |
||||
length = v*8 - (273*8 - 35) |
||||
n = 3 |
||||
case v < 281: |
||||
length = v*16 - (277*16 - 67) |
||||
n = 4 |
||||
case v < 285: |
||||
length = v*32 - (281*32 - 131) |
||||
n = 5 |
||||
case v < maxNumLit: |
||||
length = 258 |
||||
n = 0 |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println(v, ">= maxNumLit") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
if n > 0 { |
||||
for f.nb < n { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits n>0:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
length += int(f.b & uint32(1<<n-1)) |
||||
f.b >>= n |
||||
f.nb -= n |
||||
} |
||||
|
||||
var dist int |
||||
if f.hd == nil { |
||||
for f.nb < 5 { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<5:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3))) |
||||
f.b >>= 5 |
||||
f.nb -= 5 |
||||
} else { |
||||
if dist, err = f.huffSym(f.hd); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("huffsym:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
|
||||
switch { |
||||
case dist < 4: |
||||
dist++ |
||||
case dist < maxNumDist: |
||||
nb := uint(dist-2) >> 1 |
||||
// have 1 bit in bottom of dist, need nb more.
|
||||
extra := (dist & 1) << nb |
||||
for f.nb < nb { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<nb:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
extra |= int(f.b & uint32(1<<nb-1)) |
||||
f.b >>= nb |
||||
f.nb -= nb |
||||
dist = 1<<(nb+1) + 1 + extra |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println("dist too big:", dist, maxNumDist) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
// No check on length; encoding can be prescient.
|
||||
if dist > f.dict.histSize() { |
||||
if debugDecode { |
||||
fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize()) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
f.copyLen, f.copyDist = length, dist |
||||
goto copyHistory |
||||
} |
||||
|
||||
copyHistory: |
||||
// Perform a backwards copy according to RFC section 3.2.3.
|
||||
{ |
||||
cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen) |
||||
if cnt == 0 { |
||||
cnt = f.dict.writeCopy(f.copyDist, f.copyLen) |
||||
} |
||||
f.copyLen -= cnt |
||||
|
||||
if f.dict.availWrite() == 0 || f.copyLen > 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).huffmanBytesReader // We need to continue this work
|
||||
f.stepState = stateDict |
||||
return |
||||
} |
||||
goto readLiteral |
||||
} |
||||
} |
||||
|
||||
// Decode a single Huffman block from f.
|
||||
// hl and hd are the Huffman states for the lit/length values
|
||||
// and the distance values, respectively. If hd == nil, using the
|
||||
// fixed distance encoding associated with fixed Huffman blocks.
|
||||
func (f *decompressor) huffmanBufioReader() { |
||||
const ( |
||||
stateInit = iota // Zero value must be stateInit
|
||||
stateDict |
||||
) |
||||
fr := f.r.(*bufio.Reader) |
||||
moreBits := func() error { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
return noEOF(err) |
||||
} |
||||
f.roffset++ |
||||
f.b |= uint32(c) << f.nb |
||||
f.nb += 8 |
||||
return nil |
||||
} |
||||
|
||||
switch f.stepState { |
||||
case stateInit: |
||||
goto readLiteral |
||||
case stateDict: |
||||
goto copyHistory |
||||
} |
||||
|
||||
readLiteral: |
||||
// Read literal and/or (length, distance) according to RFC section 3.2.3.
|
||||
{ |
||||
var v int |
||||
{ |
||||
// Inlined v, err := f.huffSym(f.hl)
|
||||
// Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
||||
// with single element, huffSym must error on these two edge cases. In both
|
||||
// cases, the chunks slice will be 0 for the invalid sequence, leading it
|
||||
// satisfy the n == 0 check below.
|
||||
n := uint(f.hl.maxRead) |
||||
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
||||
// but is smart enough to keep local variables in registers, so use nb and b,
|
||||
// inline call to moreBits and reassign b,nb back to f on return.
|
||||
nb, b := f.nb, f.b |
||||
for { |
||||
for nb < n { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
f.b = b |
||||
f.nb = nb |
||||
f.err = noEOF(err) |
||||
return |
||||
} |
||||
f.roffset++ |
||||
b |= uint32(c) << (nb & 31) |
||||
nb += 8 |
||||
} |
||||
chunk := f.hl.chunks[b&(huffmanNumChunks-1)] |
||||
n = uint(chunk & huffmanCountMask) |
||||
if n > huffmanChunkBits { |
||||
chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask] |
||||
n = uint(chunk & huffmanCountMask) |
||||
} |
||||
if n <= nb { |
||||
if n == 0 { |
||||
f.b = b |
||||
f.nb = nb |
||||
if debugDecode { |
||||
fmt.Println("huffsym: n==0") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
f.b = b >> (n & 31) |
||||
f.nb = nb - n |
||||
v = int(chunk >> huffmanValueShift) |
||||
break |
||||
} |
||||
} |
||||
} |
||||
|
||||
var n uint // number of bits extra
|
||||
var length int |
||||
var err error |
||||
switch { |
||||
case v < 256: |
||||
f.dict.writeByte(byte(v)) |
||||
if f.dict.availWrite() == 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).huffmanBufioReader |
||||
f.stepState = stateInit |
||||
return |
||||
} |
||||
goto readLiteral |
||||
case v == 256: |
||||
f.finishBlock() |
||||
return |
||||
// otherwise, reference to older data
|
||||
case v < 265: |
||||
length = v - (257 - 3) |
||||
n = 0 |
||||
case v < 269: |
||||
length = v*2 - (265*2 - 11) |
||||
n = 1 |
||||
case v < 273: |
||||
length = v*4 - (269*4 - 19) |
||||
n = 2 |
||||
case v < 277: |
||||
length = v*8 - (273*8 - 35) |
||||
n = 3 |
||||
case v < 281: |
||||
length = v*16 - (277*16 - 67) |
||||
n = 4 |
||||
case v < 285: |
||||
length = v*32 - (281*32 - 131) |
||||
n = 5 |
||||
case v < maxNumLit: |
||||
length = 258 |
||||
n = 0 |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println(v, ">= maxNumLit") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
if n > 0 { |
||||
for f.nb < n { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits n>0:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
length += int(f.b & uint32(1<<n-1)) |
||||
f.b >>= n |
||||
f.nb -= n |
||||
} |
||||
|
||||
var dist int |
||||
if f.hd == nil { |
||||
for f.nb < 5 { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<5:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3))) |
||||
f.b >>= 5 |
||||
f.nb -= 5 |
||||
} else { |
||||
if dist, err = f.huffSym(f.hd); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("huffsym:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
|
||||
switch { |
||||
case dist < 4: |
||||
dist++ |
||||
case dist < maxNumDist: |
||||
nb := uint(dist-2) >> 1 |
||||
// have 1 bit in bottom of dist, need nb more.
|
||||
extra := (dist & 1) << nb |
||||
for f.nb < nb { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<nb:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
extra |= int(f.b & uint32(1<<nb-1)) |
||||
f.b >>= nb |
||||
f.nb -= nb |
||||
dist = 1<<(nb+1) + 1 + extra |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println("dist too big:", dist, maxNumDist) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
// No check on length; encoding can be prescient.
|
||||
if dist > f.dict.histSize() { |
||||
if debugDecode { |
||||
fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize()) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
f.copyLen, f.copyDist = length, dist |
||||
goto copyHistory |
||||
} |
||||
|
||||
copyHistory: |
||||
// Perform a backwards copy according to RFC section 3.2.3.
|
||||
{ |
||||
cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen) |
||||
if cnt == 0 { |
||||
cnt = f.dict.writeCopy(f.copyDist, f.copyLen) |
||||
} |
||||
f.copyLen -= cnt |
||||
|
||||
if f.dict.availWrite() == 0 || f.copyLen > 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).huffmanBufioReader // We need to continue this work
|
||||
f.stepState = stateDict |
||||
return |
||||
} |
||||
goto readLiteral |
||||
} |
||||
} |
||||
|
||||
// Decode a single Huffman block from f.
|
||||
// hl and hd are the Huffman states for the lit/length values
|
||||
// and the distance values, respectively. If hd == nil, using the
|
||||
// fixed distance encoding associated with fixed Huffman blocks.
|
||||
func (f *decompressor) huffmanStringsReader() { |
||||
const ( |
||||
stateInit = iota // Zero value must be stateInit
|
||||
stateDict |
||||
) |
||||
fr := f.r.(*strings.Reader) |
||||
moreBits := func() error { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
return noEOF(err) |
||||
} |
||||
f.roffset++ |
||||
f.b |= uint32(c) << f.nb |
||||
f.nb += 8 |
||||
return nil |
||||
} |
||||
|
||||
switch f.stepState { |
||||
case stateInit: |
||||
goto readLiteral |
||||
case stateDict: |
||||
goto copyHistory |
||||
} |
||||
|
||||
readLiteral: |
||||
// Read literal and/or (length, distance) according to RFC section 3.2.3.
|
||||
{ |
||||
var v int |
||||
{ |
||||
// Inlined v, err := f.huffSym(f.hl)
|
||||
// Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
||||
// with single element, huffSym must error on these two edge cases. In both
|
||||
// cases, the chunks slice will be 0 for the invalid sequence, leading it
|
||||
// satisfy the n == 0 check below.
|
||||
n := uint(f.hl.maxRead) |
||||
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
||||
// but is smart enough to keep local variables in registers, so use nb and b,
|
||||
// inline call to moreBits and reassign b,nb back to f on return.
|
||||
nb, b := f.nb, f.b |
||||
for { |
||||
for nb < n { |
||||
c, err := fr.ReadByte() |
||||
if err != nil { |
||||
f.b = b |
||||
f.nb = nb |
||||
f.err = noEOF(err) |
||||
return |
||||
} |
||||
f.roffset++ |
||||
b |= uint32(c) << (nb & 31) |
||||
nb += 8 |
||||
} |
||||
chunk := f.hl.chunks[b&(huffmanNumChunks-1)] |
||||
n = uint(chunk & huffmanCountMask) |
||||
if n > huffmanChunkBits { |
||||
chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask] |
||||
n = uint(chunk & huffmanCountMask) |
||||
} |
||||
if n <= nb { |
||||
if n == 0 { |
||||
f.b = b |
||||
f.nb = nb |
||||
if debugDecode { |
||||
fmt.Println("huffsym: n==0") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
f.b = b >> (n & 31) |
||||
f.nb = nb - n |
||||
v = int(chunk >> huffmanValueShift) |
||||
break |
||||
} |
||||
} |
||||
} |
||||
|
||||
var n uint // number of bits extra
|
||||
var length int |
||||
var err error |
||||
switch { |
||||
case v < 256: |
||||
f.dict.writeByte(byte(v)) |
||||
if f.dict.availWrite() == 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).huffmanStringsReader |
||||
f.stepState = stateInit |
||||
return |
||||
} |
||||
goto readLiteral |
||||
case v == 256: |
||||
f.finishBlock() |
||||
return |
||||
// otherwise, reference to older data
|
||||
case v < 265: |
||||
length = v - (257 - 3) |
||||
n = 0 |
||||
case v < 269: |
||||
length = v*2 - (265*2 - 11) |
||||
n = 1 |
||||
case v < 273: |
||||
length = v*4 - (269*4 - 19) |
||||
n = 2 |
||||
case v < 277: |
||||
length = v*8 - (273*8 - 35) |
||||
n = 3 |
||||
case v < 281: |
||||
length = v*16 - (277*16 - 67) |
||||
n = 4 |
||||
case v < 285: |
||||
length = v*32 - (281*32 - 131) |
||||
n = 5 |
||||
case v < maxNumLit: |
||||
length = 258 |
||||
n = 0 |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println(v, ">= maxNumLit") |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
if n > 0 { |
||||
for f.nb < n { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits n>0:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
length += int(f.b & uint32(1<<n-1)) |
||||
f.b >>= n |
||||
f.nb -= n |
||||
} |
||||
|
||||
var dist int |
||||
if f.hd == nil { |
||||
for f.nb < 5 { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<5:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3))) |
||||
f.b >>= 5 |
||||
f.nb -= 5 |
||||
} else { |
||||
if dist, err = f.huffSym(f.hd); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("huffsym:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
|
||||
switch { |
||||
case dist < 4: |
||||
dist++ |
||||
case dist < maxNumDist: |
||||
nb := uint(dist-2) >> 1 |
||||
// have 1 bit in bottom of dist, need nb more.
|
||||
extra := (dist & 1) << nb |
||||
for f.nb < nb { |
||||
if err = moreBits(); err != nil { |
||||
if debugDecode { |
||||
fmt.Println("morebits f.nb<nb:", err) |
||||
} |
||||
f.err = err |
||||
return |
||||
} |
||||
} |
||||
extra |= int(f.b & uint32(1<<nb-1)) |
||||
f.b >>= nb |
||||
f.nb -= nb |
||||
dist = 1<<(nb+1) + 1 + extra |
||||
default: |
||||
if debugDecode { |
||||
fmt.Println("dist too big:", dist, maxNumDist) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
// No check on length; encoding can be prescient.
|
||||
if dist > f.dict.histSize() { |
||||
if debugDecode { |
||||
fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize()) |
||||
} |
||||
f.err = CorruptInputError(f.roffset) |
||||
return |
||||
} |
||||
|
||||
f.copyLen, f.copyDist = length, dist |
||||
goto copyHistory |
||||
} |
||||
|
||||
copyHistory: |
||||
// Perform a backwards copy according to RFC section 3.2.3.
|
||||
{ |
||||
cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen) |
||||
if cnt == 0 { |
||||
cnt = f.dict.writeCopy(f.copyDist, f.copyLen) |
||||
} |
||||
f.copyLen -= cnt |
||||
|
||||
if f.dict.availWrite() == 0 || f.copyLen > 0 { |
||||
f.toRead = f.dict.readFlush() |
||||
f.step = (*decompressor).huffmanStringsReader // We need to continue this work
|
||||
f.stepState = stateDict |
||||
return |
||||
} |
||||
goto readLiteral |
||||
} |
||||
} |
||||
|
||||
func (f *decompressor) huffmanBlockDecoder() func() { |
||||
switch f.r.(type) { |
||||
case *bytes.Buffer: |
||||
return f.huffmanBytesBuffer |
||||
case *bytes.Reader: |
||||
return f.huffmanBytesReader |
||||
case *bufio.Reader: |
||||
return f.huffmanBufioReader |
||||
case *strings.Reader: |
||||
return f.huffmanStringsReader |
||||
default: |
||||
return f.huffmanBlockGeneric |
||||
} |
||||
} |
Loading…
Reference in new issue