module Plum::HPACK::Huffman

Public Instance Methods

decode(encoded) click to toggle source

Static-Huffman-decodes the specified String.

# File lib/plum/hpack/huffman.rb, line 19
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) click to toggle source

Static-Huffman-encodes the specified String.

# File lib/plum/hpack/huffman.rb, line 9
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