hexdump snaps
This commit is contained in:
parent
616e14be1f
commit
59c2886046
|
@ -5,6 +5,9 @@
|
|||
# AND DUUUUH .scan() IS BASICALLY ALREADY
|
||||
# .groups_of()
|
||||
# TODO: get rid of it?!
|
||||
# update: no
|
||||
# .scan(/../) works on strings and groups_of works on arrays
|
||||
# so it has its purpose
|
||||
class Array
|
||||
def groups_of(max_size)
|
||||
return [] if max_size < 1
|
||||
|
|
32
lib/bytes.rb
32
lib/bytes.rb
|
@ -1,10 +1,42 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'array'
|
||||
|
||||
# turn byte array into hex string
|
||||
def str_hex(data)
|
||||
data.unpack1('H*').scan(/../).join(' ').upcase
|
||||
end
|
||||
|
||||
def data_to_ascii(data)
|
||||
ascii = ''
|
||||
data.unpack('C*').each do |c|
|
||||
ascii += c < 32 || c > 126 ? '.' : c.chr
|
||||
end
|
||||
ascii
|
||||
end
|
||||
|
||||
def hexdump_lines(data, width = 2)
|
||||
byte_groups = data.unpack1('H*').scan(/../).groups_of(4)
|
||||
lines = []
|
||||
hex = ''
|
||||
ascii = ''
|
||||
w = 0
|
||||
byte_groups.each do |byte_group|
|
||||
hex += ' ' unless hex.empty?
|
||||
hex += byte_group.join(' ')
|
||||
ascii += data_to_ascii(str_bytes(byte_group.join).pack('C*'))
|
||||
w += 1
|
||||
next unless w >= width
|
||||
|
||||
w = 0
|
||||
lines.push("#{hex} #{ascii}")
|
||||
hex = ''
|
||||
ascii = ''
|
||||
end
|
||||
lines.push("#{hex} #{ascii}") unless hex.empty?
|
||||
lines
|
||||
end
|
||||
|
||||
# turn hex string to byte array
|
||||
def str_bytes(str)
|
||||
str.scan(/../).map { |b| b.to_i(16) }
|
||||
|
|
|
@ -182,8 +182,9 @@ class GameClient
|
|||
# TODO: add get_raw(size)
|
||||
data = u.get_raw
|
||||
puts 'snap:'
|
||||
p str_hex(data.pack('C*'))
|
||||
p data
|
||||
hexdump_lines(data.pack('C*')).each do |hex|
|
||||
puts hex
|
||||
end
|
||||
|
||||
# ack every snapshot no matter how broken
|
||||
@ack_game_tick = game_tick
|
||||
|
|
|
@ -3,6 +3,25 @@
|
|||
require_relative '../lib/bytes'
|
||||
|
||||
describe 'Bytes', :bytes do
|
||||
context '#data_to_ascii' do
|
||||
it 'Should encode printable and non printable chars"' do
|
||||
expect(data_to_ascii("\x01\x41\x41\x01\x41\x42")).to eq('.AA.AB')
|
||||
end
|
||||
end
|
||||
context '#hexdump_lines' do
|
||||
it 'Should work with default width"' do
|
||||
ex = ['01 41 02 03 03 03 03 03 .A......', '03 03 03 03 03 ef ......']
|
||||
expect(hexdump_lines("\x01\x41\x02\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\xef")).to eq(ex)
|
||||
end
|
||||
it 'Should work with width 1"' do
|
||||
ex = ['01 41 02 03 .A..', '03 03 03 03 ....', '03 03 03 03 ....', '03 ef ..']
|
||||
expect(hexdump_lines("\x01\x41\x02\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\xef", 1)).to eq(ex)
|
||||
end
|
||||
it 'Should work with width 40"' do
|
||||
ex = ['01 41 02 03 03 03 03 03 03 03 03 03 03 ef .A............']
|
||||
expect(hexdump_lines("\x01\x41\x02\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\xef", 40)).to eq(ex)
|
||||
end
|
||||
end
|
||||
context '#str_hex' do
|
||||
it 'Should format "\xFF" to "FF"' do
|
||||
expect(str_hex("\xFF")).to eq('FF')
|
||||
|
|
Loading…
Reference in a new issue