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
|
# File 'lib/mtproto/type/gzip_packed.rb', line 13
def unpack(data)
offset = 4
length_byte = data[offset].ord
offset += 1
puts " [GZIP] Length byte: #{length_byte} (0x#{length_byte.to_s(16)})" if $DEBUG
if length_byte == 254
length_bytes = data[offset, 3]
length = (length_bytes + "\x00").unpack1('L<')
offset += 3
puts " [GZIP] Extended length: #{length} bytes" if $DEBUG
elsif length_byte == 255
raise "Invalid TL string length: 255"
else
length = length_byte
puts " [GZIP] Short length: #{length} bytes" if $DEBUG
end
raise "Invalid length: #{length.inspect}" unless length.is_a?(Integer) && length > 0
compressed_data = data[offset, length]
raise "Not enough data: expected #{length}, got #{compressed_data&.bytesize}" if compressed_data.nil? || compressed_data.bytesize < length
Zlib::GzipReader.new(StringIO.new(compressed_data)).read.force_encoding(Encoding::BINARY)
end
|