teeworlds_network/lib/packer.rb

195 lines
4.7 KiB
Ruby
Raw Normal View History

2022-11-05 16:26:08 +00:00
# frozen_string_literal: true
require_relative 'array'
SANITIZE = 1
SANITIZE_CC = 2
SKIP_START_WHITESPACES = 4
class Packer
# Format: ESDDDDDD EDDDDDDD EDD... Extended, Data, Sign
2024-02-21 06:18:01 +00:00
# @return [Array<int>]
def self.pack_int(num)
# the first byte can fit 6 bits
# because the first two bits are extended and sign
# so if you have a number bigger than 63
# it needs two bytes
# which are also least significant byte first etc
# so we do not support that YET
#
# the first too big number 64 is represented as those bits
# 10000000 00000001
2022-11-06 11:11:54 +00:00
# ^^^ ^ ^^ ^
# ||\ / | \ /
# || \ / not \ /
# || \ extended
# || \ /
# || \ /
# || \ /
# || \/
# || /\
# || / \
# || / \
2022-11-06 11:11:54 +00:00
# || 0000001 000000
# || |
# || v
# || 64
# ||
# |positive
# extended
sign = '0'
if num.negative?
sign = '1'
num += 1
end
num = num.abs
2022-11-05 16:19:05 +00:00
return pack_big_int(sign, num) if num > 63 || num < -63
ext = '0'
bits = ext + sign + num.to_s(2).rjust(6, '0')
[bits.to_i(2)]
end
def self.pack_big_int(sign, num)
num_bits = num.to_s(2)
2022-11-05 16:26:08 +00:00
first = "1#{sign}#{num_bits[-6..]}"
num_bits = num_bits[0..-7]
2024-06-24 03:45:45 +00:00
bytes = num_bits.chars.groups_of(7).map do |seven_bits|
# mark all as extended
2024-06-24 03:45:45 +00:00
"1#{seven_bits.join.rjust(7, '0')}"
end
# least significant first
bytes = bytes.reverse
# mark last byte as unextended
bytes[-1][0] = '0'
([first] + bytes).map { |b| b.to_i(2) }
end
def self.pack_str(str)
str.chars.map(&:ord) + [0x00]
end
2022-11-04 14:21:48 +00:00
end
2022-11-04 12:58:19 +00:00
2022-11-04 14:21:48 +00:00
class Unpacker
2022-11-16 14:45:04 +00:00
attr_reader :prev, :data, :parsed
2022-11-04 12:58:19 +00:00
def initialize(data)
@data = data
2022-11-16 14:45:04 +00:00
@prev = [] # all the data already unpacked
@parsed = []
2022-11-17 13:57:39 +00:00
# TODO: delete parsed or document it with examples
# @parsed.push({ type: 'string', value: str, raw:, len:, pos: })
2022-11-16 14:45:04 +00:00
@red_bytes = 0
# { type: 'int', value: 1, raw: "\x01", len: 1, pos: 0 }
2022-11-05 16:19:05 +00:00
if data.instance_of?(String)
@data = data.unpack('C*')
elsif data.instance_of?(Array)
2022-11-04 15:26:24 +00:00
@data = data
else
2022-11-05 16:19:05 +00:00
raise 'Error: Unpacker expects array of integers or byte string'
2022-11-04 15:26:24 +00:00
end
2022-11-04 12:58:19 +00:00
end
def str_sanitize(str)
letters = str.chars
letters.map! do |c|
c.ord < 32 && c != "\r" && c != "\n" && c != "\t" ? ' ' : c
end
letters.join
end
2022-11-15 17:26:49 +00:00
def str_sanitize_cc(str)
letters = str.chars
letters.map! do |c|
c.ord < 32 ? ' ' : c
end
letters.join
end
def get_string(sanitize = SANITIZE)
2022-11-04 12:58:19 +00:00
return nil if @data.nil?
str = ''
2022-11-16 14:45:04 +00:00
raw = []
len = 0
pos = @red_bytes
2022-11-04 12:58:19 +00:00
@data.each_with_index do |byte, index|
2022-11-16 14:45:04 +00:00
@prev.push(byte)
raw.push(raw)
@red_bytes += 1
len += 1
2022-11-05 16:26:08 +00:00
if byte.zero?
@data = index == @data.length - 1 ? nil : @data[(index + 1)..]
str = str_sanitize(str) unless (sanitize & SANITIZE).zero?
2022-11-15 17:26:49 +00:00
str = str_sanitize_cc(str) unless (sanitize & SANITIZE_CC).zero?
2022-11-16 14:45:04 +00:00
@parsed.push({ type: 'string', value: str, raw:, len:, pos: })
2022-11-04 12:58:19 +00:00
return str
end
str += byte.chr
end
# raise "get_string() failed to find null terminator"
# return empty string in case of error
''
end
2022-11-05 16:19:05 +00:00
def get_int
2022-11-04 12:58:19 +00:00
return nil if @data.nil?
return nil if @data.empty?
2022-11-04 12:58:19 +00:00
2022-11-05 16:19:05 +00:00
# TODO: make this more performant
2022-11-04 12:58:19 +00:00
# it should not read in ALL bytes
# of the WHOLE packed data
# it should be max 4 bytes
# because bigger ints are not sent anyways
2022-11-04 14:21:48 +00:00
bytes = @data.map { |byte| byte.to_s(2).rjust(8, '0') }
2022-11-04 12:58:19 +00:00
first = bytes[0]
sign = first[1]
2022-11-04 14:21:48 +00:00
bits = []
2022-11-16 14:45:04 +00:00
parsed = { type: 'int' }
2022-11-04 12:58:19 +00:00
# extended
if first[0] == '1'
2022-11-04 14:21:48 +00:00
bits << first[2..]
bytes = bytes[1..]
2022-11-04 14:35:54 +00:00
consumed = 1
2022-11-04 12:58:19 +00:00
bytes.each do |eigth_bits|
2022-11-04 14:21:48 +00:00
bits << eigth_bits[1..]
2022-11-04 14:35:54 +00:00
consumed += 1
2022-11-04 14:21:48 +00:00
break if eigth_bits[0] == '0'
2022-11-04 12:58:19 +00:00
end
2022-11-04 14:21:48 +00:00
bits = bits.reverse
2022-11-16 14:45:04 +00:00
@prev += @data[0...consumed]
parsed[:raw] = @data[0...consumed]
parsed[:len] = consumed
parsed[:pos] = @red_bytes
@red_bytes += consumed
2022-11-04 14:35:54 +00:00
@data = @data[consumed..]
2022-11-04 12:58:19 +00:00
else # single byte
2022-11-04 14:21:48 +00:00
bits = [first[2..]]
2022-11-16 14:45:04 +00:00
@prev.push(@data[0])
parsed[:raw] = [@data[0]]
parsed[:len] = 1
parsed[:pos] = @red_bytes
@red_bytes += 1
2022-11-04 14:21:48 +00:00
@data = @data[1..]
2022-11-04 12:58:19 +00:00
end
num = bits.join.to_i(2)
2022-11-16 14:45:04 +00:00
parsed[:value] = sign == '1' ? -(num + 1) : num
@parsed.push(parsed)
parsed[:value]
2022-11-04 12:58:19 +00:00
end
2022-11-08 15:20:46 +00:00
def get_raw(size = -1)
2022-11-16 14:45:04 +00:00
len = size == -1 ? @data.size : size
@prev += @data[...len]
pos = @red_bytes
@red_bytes += len
@parsed.push({ type: 'raw', value: @data[...len], raw: @data[...len], len:, pos: })
2022-11-08 15:20:46 +00:00
# TODO: error if size exceeds @data.size
2022-11-16 14:45:04 +00:00
@data.shift(len)
2022-11-08 15:20:46 +00:00
end
end