lib/protocol/hpack/huffman.rb in protocol-hpack-1.4.1 vs lib/protocol/hpack/huffman.rb in protocol-hpack-1.4.2

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com> # Copyrigh, 2013, by Ilya Grigorik. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -22,13 +24,11 @@ require_relative 'huffman/machine' require_relative 'error' module Protocol module HPACK - # Implementation of huffman encoding for HPACK - # - # - http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-10 + # Implementation of huffman encoding for HPACK. class Huffman BITS_AT_ONCE = 4 EOS = 256 # Encodes provided value via huffman encoding. @@ -45,38 +45,39 @@ # Decodes provided Huffman coded string. # # @param buf [Buffer] # @return [String] binary string # @raise [CompressionError] when Huffman coded string is malformed - def decode(buf) - emit = '' + def decode(buffer) + emit = String.new.b state = 0 # start state mask = (1 << BITS_AT_ONCE) - 1 - buf.each_byte do |chr| + buffer.each_byte do |c| (8 / BITS_AT_ONCE - 1).downto(0) do |shift| - branch = (chr >> (shift * BITS_AT_ONCE)) & mask + branch = (c >> (shift * BITS_AT_ONCE)) & mask + # MACHINE[state] = [final, [transitions]] # [final] unfinished bits so far are prefix of the EOS code. # Each transition is [emit, next] # [emit] character to be emitted on this transition, empty string, or EOS. # [next] next state number. - trans = MACHINE[state][branch] - raise CompressionError, 'Huffman decode error (EOS found)' if trans.first == EOS - emit << trans.first.chr if trans.first - state = trans.last + value, state = MACHINE[state][branch] + + raise CompressionError, 'Huffman decode error (EOS found)' if value == EOS + + emit << value.chr if value end end # Check whether partial input is correctly filled unless state <= MAX_FINAL_STATE raise CompressionError, 'Huffman decode error (EOS invalid)' end emit.force_encoding(Encoding::BINARY) end - # Huffman table as specified in - # - http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-10#appendix-B + # Huffman table as specified in https://tools.ietf.org/html/rfc7541#appendix-B CODES = [ [0x1ff8, 13], [0x7fffd8, 23], [0xfffffe2, 28], [0xfffffe3, 28], @@ -106,100 +107,100 @@ [0xffffff7, 28], [0xffffff8, 28], [0xffffff9, 28], [0xffffffa, 28], [0xffffffb, 28], - [0x14, 6], + [0x14, 6], [0x3f8, 10], [0x3f9, 10], [0xffa, 12], [0x1ff9, 13], - [0x15, 6], - [0xf8, 8], + [0x15, 6], + [0xf8, 8], [0x7fa, 11], [0x3fa, 10], [0x3fb, 10], - [0xf9, 8], + [0xf9, 8], [0x7fb, 11], - [0xfa, 8], - [0x16, 6], - [0x17, 6], - [0x18, 6], - [0x0, 5], - [0x1, 5], - [0x2, 5], - [0x19, 6], - [0x1a, 6], - [0x1b, 6], - [0x1c, 6], - [0x1d, 6], - [0x1e, 6], - [0x1f, 6], - [0x5c, 7], - [0xfb, 8], + [0xfa, 8], + [0x16, 6], + [0x17, 6], + [0x18, 6], + [0x0, 5], + [0x1, 5], + [0x2, 5], + [0x19, 6], + [0x1a, 6], + [0x1b, 6], + [0x1c, 6], + [0x1d, 6], + [0x1e, 6], + [0x1f, 6], + [0x5c, 7], + [0xfb, 8], [0x7ffc, 15], - [0x20, 6], + [0x20, 6], [0xffb, 12], [0x3fc, 10], [0x1ffa, 13], - [0x21, 6], - [0x5d, 7], - [0x5e, 7], - [0x5f, 7], - [0x60, 7], - [0x61, 7], - [0x62, 7], - [0x63, 7], - [0x64, 7], - [0x65, 7], - [0x66, 7], - [0x67, 7], - [0x68, 7], - [0x69, 7], - [0x6a, 7], - [0x6b, 7], - [0x6c, 7], - [0x6d, 7], - [0x6e, 7], - [0x6f, 7], - [0x70, 7], - [0x71, 7], - [0x72, 7], - [0xfc, 8], - [0x73, 7], - [0xfd, 8], + [0x21, 6], + [0x5d, 7], + [0x5e, 7], + [0x5f, 7], + [0x60, 7], + [0x61, 7], + [0x62, 7], + [0x63, 7], + [0x64, 7], + [0x65, 7], + [0x66, 7], + [0x67, 7], + [0x68, 7], + [0x69, 7], + [0x6a, 7], + [0x6b, 7], + [0x6c, 7], + [0x6d, 7], + [0x6e, 7], + [0x6f, 7], + [0x70, 7], + [0x71, 7], + [0x72, 7], + [0xfc, 8], + [0x73, 7], + [0xfd, 8], [0x1ffb, 13], [0x7fff0, 19], [0x1ffc, 13], [0x3ffc, 14], - [0x22, 6], + [0x22, 6], [0x7ffd, 15], - [0x3, 5], - [0x23, 6], - [0x4, 5], - [0x24, 6], - [0x5, 5], - [0x25, 6], - [0x26, 6], - [0x27, 6], - [0x6, 5], - [0x74, 7], - [0x75, 7], - [0x28, 6], - [0x29, 6], - [0x2a, 6], - [0x7, 5], - [0x2b, 6], - [0x76, 7], - [0x2c, 6], - [0x8, 5], - [0x9, 5], - [0x2d, 6], - [0x77, 7], - [0x78, 7], - [0x79, 7], - [0x7a, 7], - [0x7b, 7], + [0x3, 5], + [0x23, 6], + [0x4, 5], + [0x24, 6], + [0x5, 5], + [0x25, 6], + [0x26, 6], + [0x27, 6], + [0x6, 5], + [0x74, 7], + [0x75, 7], + [0x28, 6], + [0x29, 6], + [0x2a, 6], + [0x7, 5], + [0x2b, 6], + [0x76, 7], + [0x2c, 6], + [0x8, 5], + [0x9, 5], + [0x2d, 6], + [0x77, 7], + [0x78, 7], + [0x79, 7], + [0x7a, 7], + [0x7b, 7], [0x7ffe, 15], [0x7fc, 11], [0x3ffd, 14], [0x1ffd, 13], [0xffffffc, 28],