huffman.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.
  4. package hpack
  5. import (
  6. "bytes"
  7. "errors"
  8. "io"
  9. "sync"
  10. )
  11. var bufPool = sync.Pool{
  12. New: func() interface{} { return new(bytes.Buffer) },
  13. }
  14. // HuffmanDecode decodes the string in v and writes the expanded
  15. // result to w, returning the number of bytes written to w and the
  16. // Write call's return value. At most one Write call is made.
  17. func HuffmanDecode(w io.Writer, v []byte) (int, error) {
  18. buf := bufPool.Get().(*bytes.Buffer)
  19. buf.Reset()
  20. defer bufPool.Put(buf)
  21. if err := huffmanDecode(buf, 0, v); err != nil {
  22. return 0, err
  23. }
  24. return w.Write(buf.Bytes())
  25. }
  26. // HuffmanDecodeToString decodes the string in v.
  27. func HuffmanDecodeToString(v []byte) (string, error) {
  28. buf := bufPool.Get().(*bytes.Buffer)
  29. buf.Reset()
  30. defer bufPool.Put(buf)
  31. if err := huffmanDecode(buf, 0, v); err != nil {
  32. return "", err
  33. }
  34. return buf.String(), nil
  35. }
  36. // ErrInvalidHuffman is returned for errors found decoding
  37. // Huffman-encoded strings.
  38. var ErrInvalidHuffman = errors.New("hpack: invalid Huffman-encoded data")
  39. // huffmanDecode decodes v to buf.
  40. // If maxLen is greater than 0, attempts to write more to buf than
  41. // maxLen bytes will return ErrStringLength.
  42. func huffmanDecode(buf *bytes.Buffer, maxLen int, v []byte) error {
  43. n := rootHuffmanNode
  44. cur, nbits := uint(0), uint8(0)
  45. for _, b := range v {
  46. cur = cur<<8 | uint(b)
  47. nbits += 8
  48. for nbits >= 8 {
  49. idx := byte(cur >> (nbits - 8))
  50. n = n.children[idx]
  51. if n == nil {
  52. return ErrInvalidHuffman
  53. }
  54. if n.children == nil {
  55. if maxLen != 0 && buf.Len() == maxLen {
  56. return ErrStringLength
  57. }
  58. buf.WriteByte(n.sym)
  59. nbits -= n.codeLen
  60. n = rootHuffmanNode
  61. } else {
  62. nbits -= 8
  63. }
  64. }
  65. }
  66. for nbits > 0 {
  67. n = n.children[byte(cur<<(8-nbits))]
  68. if n.children != nil || n.codeLen > nbits {
  69. break
  70. }
  71. buf.WriteByte(n.sym)
  72. nbits -= n.codeLen
  73. n = rootHuffmanNode
  74. }
  75. return nil
  76. }
  77. type node struct {
  78. // children is non-nil for internal nodes
  79. children []*node
  80. // The following are only valid if children is nil:
  81. codeLen uint8 // number of bits that led to the output of sym
  82. sym byte // output symbol
  83. }
  84. func newInternalNode() *node {
  85. return &node{children: make([]*node, 256)}
  86. }
  87. var rootHuffmanNode = newInternalNode()
  88. func init() {
  89. if len(huffmanCodes) != 256 {
  90. panic("unexpected size")
  91. }
  92. for i, code := range huffmanCodes {
  93. addDecoderNode(byte(i), code, huffmanCodeLen[i])
  94. }
  95. }
  96. func addDecoderNode(sym byte, code uint32, codeLen uint8) {
  97. cur := rootHuffmanNode
  98. for codeLen > 8 {
  99. codeLen -= 8
  100. i := uint8(code >> codeLen)
  101. if cur.children[i] == nil {
  102. cur.children[i] = newInternalNode()
  103. }
  104. cur = cur.children[i]
  105. }
  106. shift := 8 - codeLen
  107. start, end := int(uint8(code<<shift)), int(1<<shift)
  108. for i := start; i < start+end; i++ {
  109. cur.children[i] = &node{sym: sym, codeLen: codeLen}
  110. }
  111. }
  112. // AppendHuffmanString appends s, as encoded in Huffman codes, to dst
  113. // and returns the extended buffer.
  114. func AppendHuffmanString(dst []byte, s string) []byte {
  115. rembits := uint8(8)
  116. for i := 0; i < len(s); i++ {
  117. if rembits == 8 {
  118. dst = append(dst, 0)
  119. }
  120. dst, rembits = appendByteToHuffmanCode(dst, rembits, s[i])
  121. }
  122. if rembits < 8 {
  123. // special EOS symbol
  124. code := uint32(0x3fffffff)
  125. nbits := uint8(30)
  126. t := uint8(code >> (nbits - rembits))
  127. dst[len(dst)-1] |= t
  128. }
  129. return dst
  130. }
  131. // HuffmanEncodeLength returns the number of bytes required to encode
  132. // s in Huffman codes. The result is round up to byte boundary.
  133. func HuffmanEncodeLength(s string) uint64 {
  134. n := uint64(0)
  135. for i := 0; i < len(s); i++ {
  136. n += uint64(huffmanCodeLen[s[i]])
  137. }
  138. return (n + 7) / 8
  139. }
  140. // appendByteToHuffmanCode appends Huffman code for c to dst and
  141. // returns the extended buffer and the remaining bits in the last
  142. // element. The appending is not byte aligned and the remaining bits
  143. // in the last element of dst is given in rembits.
  144. func appendByteToHuffmanCode(dst []byte, rembits uint8, c byte) ([]byte, uint8) {
  145. code := huffmanCodes[c]
  146. nbits := huffmanCodeLen[c]
  147. for {
  148. if rembits > nbits {
  149. t := uint8(code << (rembits - nbits))
  150. dst[len(dst)-1] |= t
  151. rembits -= nbits
  152. break
  153. }
  154. t := uint8(code >> (nbits - rembits))
  155. dst[len(dst)-1] |= t
  156. nbits -= rembits
  157. rembits = 8
  158. if nbits == 0 {
  159. break
  160. }
  161. dst = append(dst, 0)
  162. }
  163. return dst, rembits
  164. }