module ObjectFramework::HTTP2::HPACK::Huffman
Constants
- TREE
Public Class Methods
Source
# File lib/object_framework/http2/hpack.rb, line 53 def self.decode(bytes, limit: 32 * 1024) output, node, tail, tail_size = +"".b, TREE, 0, 0 bytes.each_byte do |byte| 7.downto(0) do |index| bit = (byte >> index) & 1 node = node[bit] || raise(CompressionError) tail, tail_size = (tail << 1) | bit, tail_size + 1 if (symbol = node[:symbol]) raise CompressionError if symbol == 256 || output.bytesize >= limit output << symbol node, tail, tail_size = TREE, 0, 0 end end end raise CompressionError if tail_size > 7 || tail != (1 << tail_size) - 1 output end
Source
# File lib/object_framework/http2/hpack.rb, line 38 def self.encode(bytes) output, bits, length = +"".b, 0, 0 bytes.each_byte do |byte| code, size = HUFFMAN_CODES.fetch(byte) bits, length = (bits << size) | code, length + size while length >= 8 length -= 8 output << ((bits >> length) & 255) end bits &= (1 << length) - 1 end output << ((bits << (8 - length)) | ((1 << (8 - length)) - 1)) if length.positive? output end