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_d(buf, offs):
- d1, offs = parse(buf, offs, 'int8')
- d2 = []
- for _ in range(4):
- val, offs = parse(buf, offs, 'uint32')
- d2.append(val)
- d3, offs = parse(buf, offs, 'uint8')
- d4, offs = parse(buf, offs, 'int32')
- return dict(D1=d1, D2=d2, D3=d3, D4=d4), offs
- def parse_c(buf, offs):
- c1, offs = parse(buf, offs, 'uint16')
- c2 = []
- for _ in range(8):
- val, offs = parse(buf, offs, 'float')
- c2.append(val)
- return dict(C1=c1, C2=c2), offs
- def parse_b(buf, offs):
- b1, offs = parse(buf, offs, 'uint16')
- b2size, offs = parse(buf, offs, 'uint32')
- b2offs, offs = parse(buf, offs, 'uint32')
- b2 = ''
- for _ in range(b2size):
- val, b2offs = parse(buf, b2offs, "char")
- b2 += val.decode()
- return dict(B1=b1, B2=b2), offs
- def parse_a(buf, offs):
- a1 = []
- for _ in range(2):
- val, offs = parse_b(buf, offs)
- a1.append(val)
- a2, offs = parse_c(buf, offs)
- a3, offs = parse(buf, offs, 'uint8')
- a4, offs = parse(buf, offs, 'uint8')
- a5, offs = parse(buf, offs, 'int8')
- a6offs, offs = parse(buf, offs, 'uint32')
- a6, offs = parse_d(buf, a6offs)
- return dict(A1=a1, A2=a2, A3=a3, A4=a4, A5=a5, A6=a6), offs
- def main(stream):
- res, _ = parse_a(stream, 5)
- return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement