Module: Quicsilver::Protocol::Qpack::Decoder
- Included in:
- HeaderBlockDecoder
- Defined in:
- lib/quicsilver/protocol/qpack/decoder.rb
Constant Summary collapse
- DQS_CACHE =
Cache for decode_qpack_string
{}
- DQS_OID_CACHE =
array-content → [str, consumed]
{}
- DQS_CACHE_MAX =
object_id|offset → [str, consumed]
128- DQS_LAST_A =
2-slot last-result cache for decode_qpack_string
[nil, nil, nil]
- DQS_LAST_B =
[nil, nil, nil]
Instance Method Summary collapse
-
#decode_prefix_integer(bytes, offset, prefix_bits, pattern_mask) ⇒ Object
RFC 7541 prefix integer decoding Returns [value, bytes_consumed].
-
#decode_prefix_integer_str(data, offset, prefix_bits, pattern_mask) ⇒ Object
String-based prefix integer decoding.
- #decode_qpack_string(bytes, offset) ⇒ Object
-
#decode_qpack_string_from_str(data, offset) ⇒ Object
Decode a QPACK string literal (RFC 9204 Section 4.1.2) Returns [string, bytes_consumed] String-based variant: accepts a binary String instead of byte array.
Instance Method Details
#decode_prefix_integer(bytes, offset, prefix_bits, pattern_mask) ⇒ Object
RFC 7541 prefix integer decoding Returns [value, bytes_consumed]
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/quicsilver/protocol/qpack/decoder.rb', line 138 def decode_prefix_integer(bytes, offset, prefix_bits, pattern_mask) max_prefix = (1 << prefix_bits) - 1 first_byte = bytes[offset] value = first_byte & max_prefix bytes_consumed = 1 if value == max_prefix multiplier = 1 loop do return [value, bytes_consumed] if offset + bytes_consumed >= bytes.size next_byte = bytes[offset + bytes_consumed] bytes_consumed += 1 value += (next_byte & 0x7F) * multiplier break if (next_byte & 0x80) == 0 multiplier *= 128 end end [value, bytes_consumed] end |
#decode_prefix_integer_str(data, offset, prefix_bits, pattern_mask) ⇒ Object
String-based prefix integer decoding
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/quicsilver/protocol/qpack/decoder.rb', line 115 def decode_prefix_integer_str(data, offset, prefix_bits, pattern_mask) max_prefix = (1 << prefix_bits) - 1 first_byte = data.getbyte(offset) value = first_byte & max_prefix bytes_consumed = 1 if value == max_prefix multiplier = 1 loop do return [value, bytes_consumed] if offset + bytes_consumed >= data.bytesize next_byte = data.getbyte(offset + bytes_consumed) bytes_consumed += 1 value += (next_byte & 0x7F) * multiplier break if (next_byte & 0x80) == 0 multiplier *= 128 end end [value, bytes_consumed] end |
#decode_qpack_string(bytes, offset) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/quicsilver/protocol/qpack/decoder.rb', line 50 def decode_qpack_string(bytes, offset) # 2-slot equal? fast path (covers alternating-object patterns) return DQS_LAST_A[2] if bytes.equal?(DQS_LAST_A[0]) && offset == DQS_LAST_A[1] return DQS_LAST_B[2] if bytes.equal?(DQS_LAST_B[0]) && offset == DQS_LAST_B[1] # Object-id cache oid_key = (bytes.object_id << 16) | offset cached = DQS_OID_CACHE[oid_key] if cached # Rotate 2-slot cache DQS_LAST_B[0], DQS_LAST_B[1], DQS_LAST_B[2] = DQS_LAST_A[0], DQS_LAST_A[1], DQS_LAST_A[2] DQS_LAST_A[0], DQS_LAST_A[1], DQS_LAST_A[2] = bytes, offset, cached return cached end # Dispatch to string variant if given a String return decode_qpack_string_from_str(bytes, offset) if bytes.is_a?(String) # Content-based cache for offset=0 if offset == 0 cached = DQS_CACHE[bytes] if cached DQS_OID_CACHE[oid_key] = cached return cached end end first = bytes[offset] huffman = (first & 0x80) != 0 # Inline 7-bit prefix integer decode to avoid method call length = first & 0x7F len_bytes = 1 if length == 0x7F multiplier = 1 while offset + len_bytes < bytes.size next_byte = bytes[offset + len_bytes] len_bytes += 1 length += (next_byte & 0x7F) * multiplier break if (next_byte & 0x80) == 0 multiplier *= 128 end end data_offset = offset + len_bytes raw = bytes[data_offset, length].pack("C*") str = if huffman Huffman.decode(raw) || raw else raw end result = [str, len_bytes + length].freeze # Cache for offset=0 (common case: standalone decode) if offset == 0 && DQS_CACHE.size < DQS_CACHE_MAX DQS_CACHE[bytes.frozen? ? bytes : bytes.dup.freeze] = result end DQS_OID_CACHE[oid_key] = result if DQS_OID_CACHE.size < DQS_CACHE_MAX result end |
#decode_qpack_string_from_str(data, offset) ⇒ Object
Decode a QPACK string literal (RFC 9204 Section 4.1.2) Returns [string, bytes_consumed] String-based variant: accepts a binary String instead of byte array
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/quicsilver/protocol/qpack/decoder.rb', line 12 def decode_qpack_string_from_str(data, offset) first = data.getbyte(offset) huffman = (first & 0x80) != 0 length = first & 0x7F len_bytes = 1 if length == 0x7F multiplier = 1 while offset + len_bytes < data.bytesize next_byte = data.getbyte(offset + len_bytes) len_bytes += 1 length += (next_byte & 0x7F) * multiplier break if (next_byte & 0x80) == 0 multiplier *= 128 end end data_offset = offset + len_bytes raw = data.byteslice(data_offset, length) str = if huffman Huffman.decode(raw) || raw else raw end [str, len_bytes + length] end |