Реализовать разбор двоичного формата данных.
Данные начинаются с сигнатуры 0x31 0x56 0x4f 0x5a
, за которой следует структура A. Порядок байт: от младшего к старшему. Адреса указаны в виде смещений от начала данных. В решении предлагается использовать модуль struct.
from struct import *
FMT = dict(
char=‘c’,
int8=‘b’,
uint8=‘B’,
int16=‘h’,
uint16=‘H’,
int32=‘i’,
uint32=‘I’,
int64=‘q’,
uint64=‘Q’,
double=‘d’,
float=‘f’,
)
def parse(buf, offs, ty):
return unpack_from(FMT[ty], buf, offs)[0], offs + calcsize(FMT[ty])
def parse_a(buf, offs):
a1, offs = parse(buf, offs, ‘uint8’)
a2, offs = parse(buf, offs, ‘uint16’)
a3_offs, offs = parse(buf, offs, ‘uint32’)
a3, offs = parse_b(buf, offs)
a4_size, offs = parse(buf, offs, ‘uint16’)
a4_offs, offs = parse(buf, offs, ‘uint16’)
a4 =
for _ in range(a4_size):
val, a4_offs = parse(buf, a4_offs, ‘char’)
a4.append(val.decode())
a5, offs = parse(buf, offs, ‘double’)
a6_offs, offs = parse(buf, offs, ‘uint16’)
a6, offs = parse_d(buf, offs)
a7 =
for _ in range(7):
val, offs = parse(buf, offs, ‘int16’)
a7.append(val)
return dict(A1=a1, A2=a2, A3=a3, A4=‘’.join(a4), A5=a5, A6=a6, A7=a7), offs
def parse_b(buf, offs):
b1, offs = parse(buf, offs, ‘int64’)
b2_size, offs = parse(buf, offs, ‘uint16’)
b2_offs, offs = parse(buf, offs, ‘uint32’)
b2 =
for _ in range(b2_size):
val, b2_offs = parse(buf, b2_offs, ‘uint16’)
val, b2_offs = parse_c(buf, b2_offs)
b2.append(val)
b3, offs = parse(buf, offs, ‘uint16’)
return dict(B1=b1, B2=b2, B3=b3), offs
def parse_c(buf, offs):
c1, offs = parse(buf, offs, ‘int8’)
c2_size, offs = parse(buf, offs, ‘uint32’)
c2_offs, offs = parse(buf, offs, ‘uint16’)
c2 =
for _ in range(c2_size):
val, c2_offs = parse(buf, c2_offs, ‘float’)
c2.append(val.decode())
c3, offs = parse(buf, offs, ‘uint32’)
return dict(C1=c1, C2=‘’.join(c2), C3=c3), offs
def parse_d(buf, offs):
d1, offs = parse(buf, offs, ‘uint32’)
d2, offs = parse(buf, offs, ‘uint16’)
return dict(D1=d1, D2=d2), offs
def main(buf):
return parse_a(buf, 4)[0]
print(main(b’1VOZ\x11\x00\x0b\xa5\x00\x00\x00\x03\x00\xb5\x00\x8e6{\x9e\xef)\xeb?\xb8’
b’\x00\xf8+\xbb=\xda\x8eg\xb6\x04\x1fL\xe3S\x9d\x94g+?\t3C\xbf\xeb’
b"\x02\x00\x00\x00’\x00!\x80\x99\xa0\xceH:?\x14\x1b\x99\xbe\xe5\x02"
b’\x00\x00\x00:\x00\xd0\xaa\xae\xf3\xec\x96d\xbf!\x9bG?V\x02\x00\x00\x00M\x00’
b’\xc1J\r+\xf1vM?\xd5]5?\xcc\x02\x00\x00\x00\x00\xd4\xa9\x83\x176' b'\xf8\x88>\xee\xa4\xba\xbe\x1c\x02\x00\x00\x00s\x00\xfd\xfd\xcf\xf7\xd3\x82' b"'?\xdal\x1f=\xc2\x02\x00\x00\x00\x86\x00\x9f\x02
$/\x00B\x00U\x00h"
b’\x00{\x00\x8e\x00\xcf%\x85\xad\xfb\xc5\x9bP\x06\x00\x99\x00\x00\x00\xd7>ahz’
b’\x8fp\xa8^\x90r’))