1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 var bufPool = sync.Pool{
15 New: func() interface{} { return new(bytes.Buffer) },
18 // HuffmanDecode decodes the string in v and writes the expanded
19 // result to w, returning the number of bytes written to w and the
20 // Write call's return value. At most one Write call is made.
21 func HuffmanDecode(w io.Writer, v []byte) (int, error) {
22 buf := bufPool.Get().(*bytes.Buffer)
24 defer bufPool.Put(buf)
25 if err := huffmanDecode(buf, 0, v); err != nil {
28 return w.Write(buf.Bytes())
31 // HuffmanDecodeToString decodes the string in v.
32 func HuffmanDecodeToString(v []byte) (string, error) {
33 buf := bufPool.Get().(*bytes.Buffer)
35 defer bufPool.Put(buf)
36 if err := huffmanDecode(buf, 0, v); err != nil {
39 return buf.String(), nil
42 // ErrInvalidHuffman is returned for errors found decoding
43 // Huffman-encoded strings.
44 var ErrInvalidHuffman = errors.New("hpack: invalid Huffman-encoded data")
46 // huffmanDecode decodes v to buf.
47 // If maxLen is greater than 0, attempts to write more to buf than
48 // maxLen bytes will return ErrStringLength.
49 func huffmanDecode(buf *bytes.Buffer, maxLen int, v []byte) error {
51 // cur is the bit buffer that has not been fed into n.
52 // cbits is the number of low order bits in cur that are valid.
53 // sbits is the number of bits of the symbol prefix being decoded.
54 cur, cbits, sbits := uint(0), uint8(0), uint8(0)
56 cur = cur<<8 | uint(b)
60 idx := byte(cur >> (cbits - 8))
63 return ErrInvalidHuffman
65 if n.children == nil {
66 if maxLen != 0 && buf.Len() == maxLen {
67 return ErrStringLength
79 n = n.children[byte(cur<<(8-cbits))]
81 return ErrInvalidHuffman
83 if n.children != nil || n.codeLen > cbits {
86 if maxLen != 0 && buf.Len() == maxLen {
87 return ErrStringLength
95 // Either there was an incomplete symbol, or overlong padding.
96 // Both are decoding errors per RFC 7541 section 5.2.
97 return ErrInvalidHuffman
99 if mask := uint(1<<cbits - 1); cur&mask != mask {
100 // Trailing bits must be a prefix of EOS per RFC 7541 section 5.2.
101 return ErrInvalidHuffman
108 // children is non-nil for internal nodes
111 // The following are only valid if children is nil:
112 codeLen uint8 // number of bits that led to the output of sym
113 sym byte // output symbol
116 func newInternalNode() *node {
117 return &node{children: make([]*node, 256)}
120 var rootHuffmanNode = newInternalNode()
123 if len(huffmanCodes) != 256 {
124 panic("unexpected size")
126 for i, code := range huffmanCodes {
127 addDecoderNode(byte(i), code, huffmanCodeLen[i])
131 func addDecoderNode(sym byte, code uint32, codeLen uint8) {
132 cur := rootHuffmanNode
135 i := uint8(code >> codeLen)
136 if cur.children[i] == nil {
137 cur.children[i] = newInternalNode()
139 cur = cur.children[i]
142 start, end := int(uint8(code<<shift)), int(1<<shift)
143 for i := start; i < start+end; i++ {
144 cur.children[i] = &node{sym: sym, codeLen: codeLen}
148 // AppendHuffmanString appends s, as encoded in Huffman codes, to dst
149 // and returns the extended buffer.
150 func AppendHuffmanString(dst []byte, s string) []byte {
153 for i := 0; i < len(s); i++ {
157 dst, rembits = appendByteToHuffmanCode(dst, rembits, s[i])
161 // special EOS symbol
162 code := uint32(0x3fffffff)
165 t := uint8(code >> (nbits - rembits))
172 // HuffmanEncodeLength returns the number of bytes required to encode
173 // s in Huffman codes. The result is round up to byte boundary.
174 func HuffmanEncodeLength(s string) uint64 {
176 for i := 0; i < len(s); i++ {
177 n += uint64(huffmanCodeLen[s[i]])
182 // appendByteToHuffmanCode appends Huffman code for c to dst and
183 // returns the extended buffer and the remaining bits in the last
184 // element. The appending is not byte aligned and the remaining bits
185 // in the last element of dst is given in rembits.
186 func appendByteToHuffmanCode(dst []byte, rembits uint8, c byte) ([]byte, uint8) {
187 code := huffmanCodes[c]
188 nbits := huffmanCodeLen[c]
192 t := uint8(code << (rembits - nbits))
198 t := uint8(code >> (nbits - rembits))