Module: Biryani::HPACK::Huffman
- Defined in:
- lib/biryani/hpack/huffman.rb
Overview
rubocop: disable Metrics/ModuleLength
Class Method Summary collapse
Class Method Details
.decode(io, cursor, length) ⇒ String
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'lib/biryani/hpack/huffman.rb', line 553 def self.decode(io, cursor, length) res = ''.b bits = 0 bits_len = 0 bytes = io.get_values([:U8] * length, cursor) (length * 8).times do |i| bits_len += 1 bits += 1 if (bytes[i / 8] & (1 << 7 - (i % 8))).positive? raise Error::HuffmanDecodeError if bits == EOS if (chr = DECODE_TABLE[[bits, bits_len]]) res << chr bits = 0 bits_len = 0 else bits <<= 1 end end raise Error::HuffmanDecodeError if bits_len.positive? && bits != (2 << bits_len) - 2 || bits_len >= 8 res end |
.encode(s) ⇒ String
533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/biryani/hpack/huffman.rb', line 533 def self.encode(s) bits = s.bytes.map { |sym| ENCODE_TABLE[sym] } acc = IO::Buffer.new(bits.sum(&:bytesize)) offset = 0 bits.each do |s| acc.set_string(s, offset) offset += s.bytesize end [acc.get_string.ljust((acc.size + 7) & ~7, '1')].pack('B*') end |