Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from struct import unpack_from, calcsize
- 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, "int64")
- d2_size, offs = parse(buf, offs, 'uint32')
- d2_offset, offs = parse(buf, offs, 'uint16')
- d2 = []
- for _ in range(d2_size):
- val, d2_offset = parse(buf, d2_offset, "uint32")
- d2.append(val)
- d3, offs = parse(buf, offs, "uint8")
- d4, offs = parse(buf, offs, "int8")
- d5_size, offs = parse(buf, offs, 'uint32')
- d5_offset, offs = parse(buf, offs, 'uint32')
- d5 = []
- for _ in range(d5_size):
- val, d5_offset = parse(buf, d5_offset, "int64")
- d5.append(val)
- return dict(D1=d1, D2=d2, D3=d3, D4=d4, D5=d5), offs
- def parse_c(buf, offs):
- c1, offs = parse(buf, offs, 'float')
- c2, offs = parse(buf, offs, 'float')
- return dict(C1=c1, C2=c2), offs
- def parse_b(buf, offs):
- b1_size, offs = parse(buf, offs, 'uint32')
- b1_offset, offs = parse(buf, offs, 'uint16')
- b1 = []
- for _ in range(b1_size):
- val, b1_offset = parse_c(buf, b1_offset)
- b1.append(val)
- b2 = []
- for _ in range(5):
- val, offs = parse(buf, offs, "uint16")
- b2.append(val)
- b3, offs = parse(buf, offs, "uint32")
- b4, offs = parse(buf, offs, "uint64")
- b5, offs = parse(buf, offs, "uint16")
- b6, _ = parse_d(buf, offs)
- return dict(B1=b1, B2=b2, B3=b3, B4=b4, B5=b5, B6=b6, ), offs
- def parse_a(buf, offs):
- a1, offs = parse(buf, offs, 'uint64')
- a2, offs = parse(buf, offs, 'double')
- a3, offs = parse(buf, offs, 'uint8')
- a4, offs = parse(buf, offs, 'uint8')
- a5, offs = parse(buf, offs, 'int32')
- a6_offset, offs = parse(buf, offs, 'uint32')
- a6, _ = parse_b(buf, a6_offset)
- return dict(A1=a1, A2=a2, A3=a3, A4=a4, A5=a5, A6=a6), offs
- def main(stream):
- return parse_a(stream, 5)[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement