forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/net/http2/hpack/huffman.go

398 lines
6.0 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2014 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
package hpack
import (
"bytes"
"errors"
"io"
"sync"
)
var bufPool = sync.Pool{
2024-02-18 10:42:21 +00:00
New: func() interface{} { return new(bytes.Buffer) },
}
// HuffmanDecode decodes the string in v and writes the expanded
2024-02-18 10:42:21 +00:00
// result to w, returning the number of bytes written to w and the
2024-02-18 10:42:21 +00:00
// Write call's return value. At most one Write call is made.
2024-02-18 10:42:21 +00:00
func HuffmanDecode(w io.Writer, v []byte) (int, error) {
2024-02-18 10:42:21 +00:00
buf := bufPool.Get().(*bytes.Buffer)
2024-02-18 10:42:21 +00:00
buf.Reset()
2024-02-18 10:42:21 +00:00
defer bufPool.Put(buf)
2024-02-18 10:42:21 +00:00
if err := huffmanDecode(buf, 0, v); err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return w.Write(buf.Bytes())
2024-02-18 10:42:21 +00:00
}
// HuffmanDecodeToString decodes the string in v.
2024-02-18 10:42:21 +00:00
func HuffmanDecodeToString(v []byte) (string, error) {
2024-02-18 10:42:21 +00:00
buf := bufPool.Get().(*bytes.Buffer)
2024-02-18 10:42:21 +00:00
buf.Reset()
2024-02-18 10:42:21 +00:00
defer bufPool.Put(buf)
2024-02-18 10:42:21 +00:00
if err := huffmanDecode(buf, 0, v); err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return buf.String(), nil
2024-02-18 10:42:21 +00:00
}
// ErrInvalidHuffman is returned for errors found decoding
2024-02-18 10:42:21 +00:00
// Huffman-encoded strings.
2024-02-18 10:42:21 +00:00
var ErrInvalidHuffman = errors.New("hpack: invalid Huffman-encoded data")
// huffmanDecode decodes v to buf.
2024-02-18 10:42:21 +00:00
// If maxLen is greater than 0, attempts to write more to buf than
2024-02-18 10:42:21 +00:00
// maxLen bytes will return ErrStringLength.
2024-02-18 10:42:21 +00:00
func huffmanDecode(buf *bytes.Buffer, maxLen int, v []byte) error {
2024-02-18 10:42:21 +00:00
rootHuffmanNode := getRootHuffmanNode()
2024-02-18 10:42:21 +00:00
n := rootHuffmanNode
2024-02-18 10:42:21 +00:00
// cur is the bit buffer that has not been fed into n.
2024-02-18 10:42:21 +00:00
// cbits is the number of low order bits in cur that are valid.
2024-02-18 10:42:21 +00:00
// sbits is the number of bits of the symbol prefix being decoded.
2024-02-18 10:42:21 +00:00
cur, cbits, sbits := uint(0), uint8(0), uint8(0)
2024-02-18 10:42:21 +00:00
for _, b := range v {
2024-02-18 10:42:21 +00:00
cur = cur<<8 | uint(b)
2024-02-18 10:42:21 +00:00
cbits += 8
2024-02-18 10:42:21 +00:00
sbits += 8
2024-02-18 10:42:21 +00:00
for cbits >= 8 {
2024-02-18 10:42:21 +00:00
idx := byte(cur >> (cbits - 8))
2024-02-18 10:42:21 +00:00
n = n.children[idx]
2024-02-18 10:42:21 +00:00
if n == nil {
2024-02-18 10:42:21 +00:00
return ErrInvalidHuffman
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n.children == nil {
2024-02-18 10:42:21 +00:00
if maxLen != 0 && buf.Len() == maxLen {
2024-02-18 10:42:21 +00:00
return ErrStringLength
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf.WriteByte(n.sym)
2024-02-18 10:42:21 +00:00
cbits -= n.codeLen
2024-02-18 10:42:21 +00:00
n = rootHuffmanNode
2024-02-18 10:42:21 +00:00
sbits = cbits
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
cbits -= 8
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for cbits > 0 {
2024-02-18 10:42:21 +00:00
n = n.children[byte(cur<<(8-cbits))]
2024-02-18 10:42:21 +00:00
if n == nil {
2024-02-18 10:42:21 +00:00
return ErrInvalidHuffman
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n.children != nil || n.codeLen > cbits {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if maxLen != 0 && buf.Len() == maxLen {
2024-02-18 10:42:21 +00:00
return ErrStringLength
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf.WriteByte(n.sym)
2024-02-18 10:42:21 +00:00
cbits -= n.codeLen
2024-02-18 10:42:21 +00:00
n = rootHuffmanNode
2024-02-18 10:42:21 +00:00
sbits = cbits
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sbits > 7 {
2024-02-18 10:42:21 +00:00
// Either there was an incomplete symbol, or overlong padding.
2024-02-18 10:42:21 +00:00
// Both are decoding errors per RFC 7541 section 5.2.
2024-02-18 10:42:21 +00:00
return ErrInvalidHuffman
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if mask := uint(1<<cbits - 1); cur&mask != mask {
2024-02-18 10:42:21 +00:00
// Trailing bits must be a prefix of EOS per RFC 7541 section 5.2.
2024-02-18 10:42:21 +00:00
return ErrInvalidHuffman
2024-02-18 10:42:21 +00:00
}
return nil
2024-02-18 10:42:21 +00:00
}
// incomparable is a zero-width, non-comparable type. Adding it to a struct
2024-02-18 10:42:21 +00:00
// makes that struct also non-comparable, and generally doesn't add
2024-02-18 10:42:21 +00:00
// any size (as long as it's first).
2024-02-18 10:42:21 +00:00
type incomparable [0]func()
type node struct {
_ incomparable
// children is non-nil for internal nodes
2024-02-18 10:42:21 +00:00
children *[256]*node
// The following are only valid if children is nil:
2024-02-18 10:42:21 +00:00
codeLen uint8 // number of bits that led to the output of sym
sym byte // output symbol
2024-02-18 10:42:21 +00:00
}
func newInternalNode() *node {
2024-02-18 10:42:21 +00:00
return &node{children: new([256]*node)}
2024-02-18 10:42:21 +00:00
}
var (
buildRootOnce sync.Once
2024-02-18 10:42:21 +00:00
lazyRootHuffmanNode *node
)
func getRootHuffmanNode() *node {
2024-02-18 10:42:21 +00:00
buildRootOnce.Do(buildRootHuffmanNode)
2024-02-18 10:42:21 +00:00
return lazyRootHuffmanNode
2024-02-18 10:42:21 +00:00
}
func buildRootHuffmanNode() {
2024-02-18 10:42:21 +00:00
if len(huffmanCodes) != 256 {
2024-02-18 10:42:21 +00:00
panic("unexpected size")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
lazyRootHuffmanNode = newInternalNode()
2024-02-18 10:42:21 +00:00
// allocate a leaf node for each of the 256 symbols
2024-02-18 10:42:21 +00:00
leaves := new([256]node)
for sym, code := range huffmanCodes {
2024-02-18 10:42:21 +00:00
codeLen := huffmanCodeLen[sym]
cur := lazyRootHuffmanNode
2024-02-18 10:42:21 +00:00
for codeLen > 8 {
2024-02-18 10:42:21 +00:00
codeLen -= 8
2024-02-18 10:42:21 +00:00
i := uint8(code >> codeLen)
2024-02-18 10:42:21 +00:00
if cur.children[i] == nil {
2024-02-18 10:42:21 +00:00
cur.children[i] = newInternalNode()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cur = cur.children[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
shift := 8 - codeLen
2024-02-18 10:42:21 +00:00
start, end := int(uint8(code<<shift)), int(1<<shift)
leaves[sym].sym = byte(sym)
2024-02-18 10:42:21 +00:00
leaves[sym].codeLen = codeLen
2024-02-18 10:42:21 +00:00
for i := start; i < start+end; i++ {
2024-02-18 10:42:21 +00:00
cur.children[i] = &leaves[sym]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// AppendHuffmanString appends s, as encoded in Huffman codes, to dst
2024-02-18 10:42:21 +00:00
// and returns the extended buffer.
2024-02-18 10:42:21 +00:00
func AppendHuffmanString(dst []byte, s string) []byte {
2024-02-18 10:42:21 +00:00
// This relies on the maximum huffman code length being 30 (See tables.go huffmanCodeLen array)
2024-02-18 10:42:21 +00:00
// So if a uint64 buffer has less than 32 valid bits can always accommodate another huffmanCode.
2024-02-18 10:42:21 +00:00
var (
x uint64 // buffer
n uint // number valid of bits present in x
2024-02-18 10:42:21 +00:00
)
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); i++ {
2024-02-18 10:42:21 +00:00
c := s[i]
2024-02-18 10:42:21 +00:00
n += uint(huffmanCodeLen[c])
2024-02-18 10:42:21 +00:00
x <<= huffmanCodeLen[c] % 64
2024-02-18 10:42:21 +00:00
x |= uint64(huffmanCodes[c])
2024-02-18 10:42:21 +00:00
if n >= 32 {
n %= 32 // Normally would be -= 32 but %= 32 informs compiler 0 <= n <= 31 for upcoming shift
2024-02-18 10:42:21 +00:00
y := uint32(x >> n) // Compiler doesn't combine memory writes if y isn't uint32
2024-02-18 10:42:21 +00:00
dst = append(dst, byte(y>>24), byte(y>>16), byte(y>>8), byte(y))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Add padding bits if necessary
2024-02-18 10:42:21 +00:00
if over := n % 8; over > 0 {
2024-02-18 10:42:21 +00:00
const (
eosCode = 0x3fffffff
eosNBits = 30
2024-02-18 10:42:21 +00:00
eosPadByte = eosCode >> (eosNBits - 8)
)
2024-02-18 10:42:21 +00:00
pad := 8 - over
2024-02-18 10:42:21 +00:00
x = (x << pad) | (eosPadByte >> over)
2024-02-18 10:42:21 +00:00
n += pad // 8 now divides into n exactly
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// n in (0, 8, 16, 24, 32)
2024-02-18 10:42:21 +00:00
switch n / 8 {
2024-02-18 10:42:21 +00:00
case 0:
2024-02-18 10:42:21 +00:00
return dst
2024-02-18 10:42:21 +00:00
case 1:
2024-02-18 10:42:21 +00:00
return append(dst, byte(x))
2024-02-18 10:42:21 +00:00
case 2:
2024-02-18 10:42:21 +00:00
y := uint16(x)
2024-02-18 10:42:21 +00:00
return append(dst, byte(y>>8), byte(y))
2024-02-18 10:42:21 +00:00
case 3:
2024-02-18 10:42:21 +00:00
y := uint16(x >> 8)
2024-02-18 10:42:21 +00:00
return append(dst, byte(y>>8), byte(y), byte(x))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// case 4:
2024-02-18 10:42:21 +00:00
y := uint32(x)
2024-02-18 10:42:21 +00:00
return append(dst, byte(y>>24), byte(y>>16), byte(y>>8), byte(y))
2024-02-18 10:42:21 +00:00
}
// HuffmanEncodeLength returns the number of bytes required to encode
2024-02-18 10:42:21 +00:00
// s in Huffman codes. The result is round up to byte boundary.
2024-02-18 10:42:21 +00:00
func HuffmanEncodeLength(s string) uint64 {
2024-02-18 10:42:21 +00:00
n := uint64(0)
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); i++ {
2024-02-18 10:42:21 +00:00
n += uint64(huffmanCodeLen[s[i]])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return (n + 7) / 8
2024-02-18 10:42:21 +00:00
}