Module: Plum::HPACK::Huffman

Extended by:
Huffman
Included in:
Huffman
Defined in:
lib/plum/hpack/huffman.rb

Instance Method Summary collapse

Instance Method Details

#decode(encoded) ⇒ Object

Static-Huffman-decodes the specified String.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/plum/hpack/huffman.rb', line 20

def decode(encoded)
  bits = encoded.unpack("B*")[0]
  out = []
  buf = String.new
  bits.each_char do |cb|
    buf << cb
    if c = HUFFMAN_TABLE_INVERSED[buf]
      raise HPACKError.new("huffman: EOS detected") if c == 256
      out << c
      buf.clear
    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
    out.pack("C*")
  end
end

#encode(bytestr) ⇒ Object

Static-Huffman-encodes the specified String.


10
11
12
13
14
15
16
17
# File 'lib/plum/hpack/huffman.rb', line 10

def encode(bytestr)
  out = String.new
  bytestr.each_byte do |b|
    out << HUFFMAN_TABLE[b]
  end
  out << "1" * ((8 - out.bytesize) % 8)
  [out].pack("B*")
end