Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from struct import *
- FMT = dict(
- char='c',
- int8='b',
- uint8='B',
- int16='h',
- uint16='H',
- int32='i',
- uint32='I',
- int64='q',
- uint64='Q',
- float='f',
- double='d'
- )
- def parse(buf, offs, ty, order='>'):
- pattern = FMT[ty]
- size = calcsize(pattern)
- value = unpack_from(order + pattern, buf, offs)[0]
- return value, offs + size
- def parse_e(buf, offs):
- e1, offs = parse(buf, offs, 'int8')
- e2, offs = parse(buf, offs, 'uint16')
- return dict(E1=e1, E2=e2), offs
- def parse_d(buf, offs):
- d1, offs = parse(buf, offs, 'uint8')
- d2, offs = parse(buf, offs, 'int64')
- return dict(D1=d1, D2=d2), offs
- def parse_c(buf, offs):
- c1, offs = parse(buf, offs, 'int64')
- c2, offs = parse(buf, offs, 'int32')
- c3, offs = parse(buf, offs, 'uint8')
- c4, offs = parse(buf, offs, 'float')
- return dict(C1=c1, C2=c2, C3=c3, C4=c4), offs
- def parse_b(buf, offs):
- b1 = []
- for _ in range(3):
- val, offs = parse_c(buf, offs)
- b1.append(val)
- b2 = parse(buf, offs, 'double')
- b3 = parse(buf, offs, 'double')
- b4_size, offs = parse(buf, offs, 'uint16')
- b4_offs, offs = parse(buf, offs, 'uint32')
- b4 = []
- for i in range(b4_size):
- val, b4_offs = parse(buf, b4_offs, 'int32')
- b4.append(val)
- return dict(B1=b1, B2=b2, B3=b3, B4=b4), offs
- def parse_a(buf, offs):
- a1, offs = parse_b(buf, offs)
- a2, offs = parse(buf, offs, 'uint16')
- a3, offs = parse(buf, offs, 'uint16')
- a4_size, offs = parse(buf, offs, 'uint16')
- a4_offs, offs = parse(buf, offs, 'uint32')
- a4 = []
- for _ in range(a4_size):
- val, a4_offs = parse(buf, a4_offs, 'char')
- a4.append(val)
- a4 = b''.join(a4).decode('utf-8')
- d_offs, offs = parse(buf, offs, 'uint16')
- a5, _ = parse_d(buf, d_offs)
- a6 = []
- for _ in range(3):
- val, offs = parse(buf, offs, 'double')
- a6.append(val)
- a7, offs = parse_e(buf, offs)
- return dict(A1=a1, A2=a2, A3=a3, A4=a4, A5=a5, A6=a6, A7=a7), offs
- def main(stream):
- return parse_a(stream, 4)[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement