open-vault/vendor/github.com/nwaples/rardecode/huffman.go
Calvin Leung Huang d2dbb8c963
Vault Debug (#7375)
* cli: initial work on debug; server-status target

* debug: add metrics capture target (#7376)

* check against DR secondary

* debug: add compression

* refactor check into preflight func

* debug: set short test time on tests, fix exit code bug

* debug: use temp dir for output on tests

* debug: use mholt/archiver for compression

* first pass on adding pprof

* use logger for output

* refactor polling target capture logic

* debug: poll and collect replication status

* debug: poll and collect host-info; rename output files and collection refactor

* fix comments

* add archive test; fix bugs found

* rename flag name to singular target

* add target output test; scaffold other tests cases

* debug/test: add pprof and index file tests

* debug/test: add min timing check tests

* debug: fix index gen race and collection goroutine race

* debug: extend archive tests, handle race between program exit and polling goroutines

* update docstring

* debug: correctly add to pollingWg

* debug: add config target support

* debug: don't wait on interrupt shutdown; add file exists unit tests

* move pprof bits into its goroutine

* debug: skip empty metrics and some pprof file creation if permission denied, add matching unit test

* address comments and feedback

* Vault debug using run.Group (#7658)

* debug: switch to use oklog/run.Group

* debug: use context to cancel requests and interrupt rungroups.

* debug: trigger the first interval properly

* debug: metrics collection should use metrics interval

* debug: add missing continue on metrics error

* debug: remove the use of buffered chan to trigger first interval

* debug: don't shadow BaseCommand's client, properly block on interval capture failures

* debug: actually use c.cachedClient everywhere

* go mod vendor

* debug: run all pprof in goroutines; bump pprof timings in tests to reduce flakiness

* debug: update help text
2019-10-15 15:39:19 -07:00

209 lines
3.9 KiB
Go

package rardecode
import (
"errors"
"io"
)
const (
maxCodeLength = 15 // maximum code length in bits
maxQuickBits = 10
maxQuickSize = 1 << maxQuickBits
)
var (
errHuffDecodeFailed = errors.New("rardecode: huffman decode failed")
errInvalidLengthTable = errors.New("rardecode: invalid huffman code length table")
)
type huffmanDecoder struct {
limit [maxCodeLength + 1]int
pos [maxCodeLength + 1]int
symbol []int
min uint
quickbits uint
quicklen [maxQuickSize]uint
quicksym [maxQuickSize]int
}
func (h *huffmanDecoder) init(codeLengths []byte) {
var count [maxCodeLength + 1]int
for _, n := range codeLengths {
if n == 0 {
continue
}
count[n]++
}
h.pos[0] = 0
h.limit[0] = 0
h.min = 0
for i := uint(1); i <= maxCodeLength; i++ {
h.limit[i] = h.limit[i-1] + count[i]<<(maxCodeLength-i)
h.pos[i] = h.pos[i-1] + count[i-1]
if h.min == 0 && h.limit[i] > 0 {
h.min = i
}
}
if cap(h.symbol) >= len(codeLengths) {
h.symbol = h.symbol[:len(codeLengths)]
for i := range h.symbol {
h.symbol[i] = 0
}
} else {
h.symbol = make([]int, len(codeLengths))
}
copy(count[:], h.pos[:])
for i, n := range codeLengths {
if n != 0 {
h.symbol[count[n]] = i
count[n]++
}
}
if len(codeLengths) >= 298 {
h.quickbits = maxQuickBits
} else {
h.quickbits = maxQuickBits - 3
}
bits := uint(1)
for i := 0; i < 1<<h.quickbits; i++ {
v := i << (maxCodeLength - h.quickbits)
for v >= h.limit[bits] && bits < maxCodeLength {
bits++
}
h.quicklen[i] = bits
dist := v - h.limit[bits-1]
dist >>= (maxCodeLength - bits)
pos := h.pos[bits] + dist
if pos < len(h.symbol) {
h.quicksym[i] = h.symbol[pos]
} else {
h.quicksym[i] = 0
}
}
}
func (h *huffmanDecoder) readSym(r bitReader) (int, error) {
bits := uint(maxCodeLength)
v, err := r.readBits(maxCodeLength)
if err != nil {
if err != io.EOF {
return 0, err
}
// fall back to 1 bit at a time if we read past EOF
for i := uint(1); i <= maxCodeLength; i++ {
b, err := r.readBits(1)
if err != nil {
return 0, err // not enough bits return error
}
v |= b << (maxCodeLength - i)
if v < h.limit[i] {
bits = i
break
}
}
} else {
if v < h.limit[h.quickbits] {
i := v >> (maxCodeLength - h.quickbits)
r.unreadBits(maxCodeLength - h.quicklen[i])
return h.quicksym[i], nil
}
for i, n := range h.limit[h.min:] {
if v < n {
bits = h.min + uint(i)
r.unreadBits(maxCodeLength - bits)
break
}
}
}
dist := v - h.limit[bits-1]
dist >>= maxCodeLength - bits
pos := h.pos[bits] + dist
if pos > len(h.symbol) {
return 0, errHuffDecodeFailed
}
return h.symbol[pos], nil
}
// readCodeLengthTable reads a new code length table into codeLength from br.
// If addOld is set the old table is added to the new one.
func readCodeLengthTable(br bitReader, codeLength []byte, addOld bool) error {
var bitlength [20]byte
for i := 0; i < len(bitlength); i++ {
n, err := br.readBits(4)
if err != nil {
return err
}
if n == 0xf {
cnt, err := br.readBits(4)
if err != nil {
return err
}
if cnt > 0 {
// array already zero'd dont need to explicitly set
i += cnt + 1
continue
}
}
bitlength[i] = byte(n)
}
var bl huffmanDecoder
bl.init(bitlength[:])
for i := 0; i < len(codeLength); i++ {
l, err := bl.readSym(br)
if err != nil {
return err
}
if l < 16 {
if addOld {
codeLength[i] = (codeLength[i] + byte(l)) & 0xf
} else {
codeLength[i] = byte(l)
}
continue
}
var count int
var value byte
switch l {
case 16, 18:
count, err = br.readBits(3)
count += 3
default:
count, err = br.readBits(7)
count += 11
}
if err != nil {
return err
}
if l < 18 {
if i == 0 {
return errInvalidLengthTable
}
value = codeLength[i-1]
}
for ; count > 0 && i < len(codeLength); i++ {
codeLength[i] = value
count--
}
i--
}
return nil
}