aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-08-11 20:53:02 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-08-11 20:53:02 +0900
commite189911e04d5c25f5b8c5ff8a7de8363511023bd (patch)
tree6ff4f84508ecd371b9752118eb6c767cc090464e /lib
parentcc330a0a7010d3ecd6841cc58abb1cd3a2d87132 (diff)
downloadplum-e189911e04d5c25f5b8c5ff8a7de8363511023bd.tar.gz
hpack: huffman: rewrite encode
Diffstat (limited to 'lib')
-rw-r--r--lib/plum/hpack/constants.rb3
-rw-r--r--lib/plum/hpack/huffman.rb19
2 files changed, 15 insertions, 7 deletions
diff --git a/lib/plum/hpack/constants.rb b/lib/plum/hpack/constants.rb
index 8a4f114..38c60d4 100644
--- a/lib/plum/hpack/constants.rb
+++ b/lib/plum/hpack/constants.rb
@@ -326,7 +326,6 @@ module Plum
[0x3fffffff, 30] # EOS
]
- HUFFMAN_ENCODE_TABLE = HUFFMAN_TABLE.map {|val, len| "%0#{len}b" % val }
- HUFFMAN_DECODE_TABLE = HUFFMAN_ENCODE_TABLE.each_with_index.to_h
+ HUFFMAN_DECODE_TABLE = HUFFMAN_TABLE.map {|val, len| "%0#{len}b" % val }.each_with_index.to_h
end
end
diff --git a/lib/plum/hpack/huffman.rb b/lib/plum/hpack/huffman.rb
index 70bb67c..0adeb22 100644
--- a/lib/plum/hpack/huffman.rb
+++ b/lib/plum/hpack/huffman.rb
@@ -7,12 +7,21 @@ module Plum
# Static-Huffman-encodes the specified String.
def encode(bytestr)
- out = ""
- bytestr.bytes.each do |b|
- out << HUFFMAN_ENCODE_TABLE[b]
+ ret = []
+ remain = 0
+ bytestr.each_byte do |b|
+ val, len = HUFFMAN_TABLE[b]
+ l = len - remain
+
+ ret[-1] |= val >> l if ret.size > 0
+ while l > 0
+ ret << ((val >> (l - 8)) & 0xff)
+ l -= 8
+ end
+ remain = -l % 8
end
- out << "1" * (8 - (out.bytesize % 8))
- [out].pack("B*")
+ ret[-1] |= (1 << remain) - 1 if remain > 0
+ ret.pack("C*")
end
# Static-Huffman-decodes the specified String.