blob: 70bb67cadea276e3f0d58a1ea78e6a37e8b878f8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
using Plum::BinaryString
module Plum
module HPACK
module Huffman
extend self
# Static-Huffman-encodes the specified String.
def encode(bytestr)
out = ""
bytestr.bytes.each do |b|
out << HUFFMAN_ENCODE_TABLE[b]
end
out << "1" * (8 - (out.bytesize % 8))
[out].pack("B*")
end
# Static-Huffman-decodes the specified String.
def decode(encoded)
bits = encoded.unpack("B*")[0]
buf = ""
outl = []
while (n = bits.byteshift(1)).bytesize > 0
if c = HUFFMAN_DECODE_TABLE[buf << n]
buf = ""
outl << c
end
end
if buf.bytesize > 7
raise HPACKError.new("huffman: padding is too large (> 7 bits)")
elsif buf != "1" * buf.bytesize
raise HPACKError.new("huffman: unknown suffix: #{buf}")
else
outl.pack("C*")
end
end
end
end
end
|