mirror of
https://github.com/go-gitea/gitea
synced 2024-11-17 23:54:25 +00:00
12a1f914f4
* update github.com/alecthomas/chroma v0.8.0 -> v0.8.1 * github.com/blevesearch/bleve v1.0.10 -> v1.0.12 * editorconfig-core-go v2.1.1 -> v2.3.7 * github.com/gliderlabs/ssh v0.2.2 -> v0.3.1 * migrate editorconfig.ParseBytes to Parse * github.com/shurcooL/vfsgen to 0d455de96546 * github.com/go-git/go-git/v5 v5.1.0 -> v5.2.0 * github.com/google/uuid v1.1.1 -> v1.1.2 * github.com/huandu/xstrings v1.3.0 -> v1.3.2 * github.com/klauspost/compress v1.10.11 -> v1.11.1 * github.com/markbates/goth v1.61.2 -> v1.65.0 * github.com/mattn/go-sqlite3 v1.14.0 -> v1.14.4 * github.com/mholt/archiver v3.3.0 -> v3.3.2 * github.com/microcosm-cc/bluemonday 4f7140c49acb -> v1.0.4 * github.com/minio/minio-go v7.0.4 -> v7.0.5 * github.com/olivere/elastic v7.0.9 -> v7.0.20 * github.com/urfave/cli v1.20.0 -> v1.22.4 * github.com/prometheus/client_golang v1.1.0 -> v1.8.0 * github.com/xanzy/go-gitlab v0.37.0 -> v0.38.1 * mvdan.cc/xurls v2.1.0 -> v2.2.0 Co-authored-by: Lauris BH <lauris@nix.lv>
209 lines
3.9 KiB
Go
Vendored
209 lines
3.9 KiB
Go
Vendored
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
|
|
}
|