Class: Protocol::HPACK::Huffman::Generator::Node
- Inherits:
-
Object
- Object
- Protocol::HPACK::Huffman::Generator::Node
- Defined in:
- lib/protocol/hpack/huffman/generator.rb
Defined Under Namespace
Classes: Transition
Constant Summary collapse
- MACHINE_PATH =
File.("machine.rb", __dir__)
- @@id =
0
Class Attribute Summary collapse
-
.root ⇒ Object
readonly
Returns the value of attribute root.
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#emit ⇒ Object
Returns the value of attribute emit.
-
#final ⇒ Object
Returns the value of attribute final.
-
#id ⇒ Object
Returns the value of attribute id.
-
#next ⇒ Object
Returns the value of attribute next.
-
#transitions ⇒ Object
Returns the value of attribute transitions.
Class Method Summary collapse
-
.decode(input) ⇒ Object
Test decoder.
- .generate_machine ⇒ Object
- .generate_state_table(output_path = MACHINE_PATH) ⇒ Object
- .generate_tree ⇒ Object
Instance Method Summary collapse
- #add(code, len, chr) ⇒ Object
-
#initialize(depth) ⇒ Node
constructor
A new instance of Node.
Constructor Details
#initialize(depth) ⇒ Node
27 28 29 30 31 32 33 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 27 def initialize(depth) @next = [nil, nil] @id = @@id @@id += 1 @final = false @depth = depth end |
Class Attribute Details
.root ⇒ Object (readonly)
Returns the value of attribute root.
163 164 165 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 163 def root @root end |
Instance Attribute Details
#depth ⇒ Object
Returns the value of attribute depth.
21 22 23 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 21 def depth @depth end |
#emit ⇒ Object
Returns the value of attribute emit.
21 22 23 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 21 def emit @emit end |
#final ⇒ Object
Returns the value of attribute final.
21 22 23 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 21 def final @final end |
#id ⇒ Object
Returns the value of attribute id.
23 24 25 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 23 def id @id end |
#next ⇒ Object
Returns the value of attribute next.
21 22 23 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 21 def next @next end |
#transitions ⇒ Object
Returns the value of attribute transitions.
22 23 24 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 22 def transitions @transitions end |
Class Method Details
.decode(input) ⇒ Object
Test decoder
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 167 def self.decode(input) emit = "" n = root nibbles = input.unpack("C*").flat_map {|b| [((b & 0xf0) >> 4), b & 0xf]} until nibbles.empty? nb = nibbles.shift t = n.transitions[nb] emit << t.emit n = t.node end unless n.final && nibbles.all? {|x| x == 0xf} puts "len = #{emit.size} n.final = #{n.final} nibbles = #{nibbles}" end emit end |
.generate_machine ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 64 def self.generate_machine generate_tree # Using un-ordered sets (potentially) produces non-deterministic results: togo = Set[@root] @states = Set[@root] until togo.empty? node = togo.first togo.delete(node) next if node.transitions node.transitions = Array[1 << BITS_AT_ONCE] (1 << BITS_AT_ONCE).times do |input| n = node emit = +"" (BITS_AT_ONCE - 1).downto(0) do |i| bit = (input & (1 << i)).zero? ? 0 : 1 n = n.next[bit] next unless n.emit if n.emit == EOS emit = EOS # cause error on decoding else emit << n.emit.chr(Encoding::BINARY) unless emit == EOS end n = @root end node.transitions[input] = Transition.new(emit, n) togo << n @states << n end end puts "#{@states.size} states" @root end |
.generate_state_table(output_path = MACHINE_PATH) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 103 def self.generate_state_table(output_path = MACHINE_PATH) generate_machine state_id = {} id_state = {} state_id[@root] = 0 id_state[0] = @root max_final = 0 id = 1 (@states - [@root]).sort_by {|s| s.final ? 0 : 1}.each do |s| state_id[s] = id id_state[id] = s max_final = id if s.final id += 1 end File.open(output_path, "w") do |file| file.print " # frozen_string_literal: true\n\n # Released under the MIT License.\n # Copyright, 2018-2024, by Samuel Williams.\n\n # Machine generated Huffman decoder state machine.\n # DO NOT EDIT THIS FILE.\n\n module Protocol\n module HPACK\n class Huffman\n # :nodoc:\n MAX_FINAL_STATE = \#{max_final}\n MACHINE = [\n HEADER\n \n id.times do |i|\n n = id_state[i]\n file.print \"\\t\\t\\t\\t[\"\n string = (1 << BITS_AT_ONCE).times.map do |t|\n transition = n.transitions.fetch(t)\n emit = transition.emit\n unless emit == EOS\n bytes = emit.bytes\n fail ArgumentError if bytes.size > 1\n emit = bytes.first\n end\n \"[\#{emit.inspect}, \#{state_id.fetch(transition.node)}]\"\n end.join(\", \")\n file.print(string)\n file.print \"],\\n\"\n end\n \n file.print <<~FOOTER\n ].each {|arr| arr.each {|subarr| subarr.each(&:freeze)}.freeze}.freeze\n end\n end\n end\n FOOTER\n end\nend\n" |
.generate_tree ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 54 def self.generate_tree @root = new(0) Protocol::HPACK::Huffman::CODES.each_with_index do |c, chr| code, len = c @root.add(code, len, chr) end puts "#{@@id} nodes" @root end |
Instance Method Details
#add(code, len, chr) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/protocol/hpack/huffman/generator.rb', line 35 def add(code, len, chr) self.final = true if chr == EOS && @depth <= 7 if len.zero? @emit = chr else bit = (code & (1 << (len - 1))).zero? ? 0 : 1 node = @next[bit] ||= Node.new(@depth + 1) node.add(code, len - 1, chr) end end |