Advertisement
ludaludaed

Untitled

May 30th, 2023
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. from struct import *
  2.  
  3. FMT = dict(
  4.     char='c',
  5.     int8='b',
  6.     uint8='B',
  7.     int16='h',
  8.     uint16='H',
  9.     int32='i',
  10.     uint32='I',
  11.     int64='q',
  12.     uint64='Q',
  13.     float='f',
  14.     double='d'
  15. )
  16.  
  17.  
  18. def parse(buf, offs, ty, order='>'):
  19.     pattern = FMT[ty]
  20.     size = calcsize(pattern)
  21.     value = unpack_from(order + pattern, buf, offs)[0]
  22.     return value, offs + size
  23.  
  24.  
  25. def parse_e(buf, offs):
  26.     e1, offs = parse(buf, offs, 'int8')
  27.     e2, offs = parse(buf, offs, 'uint16')
  28.     return dict(E1=e1, E2=e2), offs
  29.  
  30.  
  31. def parse_d(buf, offs):
  32.     d1, offs = parse(buf, offs, 'uint8')
  33.     d2, offs = parse(buf, offs, 'int64')
  34.     return dict(D1=d1, D2=d2), offs
  35.  
  36.  
  37. def parse_c(buf, offs):
  38.     c1, offs = parse(buf, offs, 'int64')
  39.     c2, offs = parse(buf, offs, 'int32')
  40.     c3, offs = parse(buf, offs, 'uint8')
  41.     c4, offs = parse(buf, offs, 'float')
  42.     return dict(C1=c1, C2=c2, C3=c3, C4=c4), offs
  43.  
  44.  
  45. def parse_b(buf, offs):
  46.     b1 = []
  47.     for _ in range(3):
  48.         val, offs = parse_c(buf, offs)
  49.         b1.append(val)
  50.     b2 = parse(buf, offs, 'double')
  51.     b3 = parse(buf, offs, 'double')
  52.     b4_size, offs = parse(buf, offs, 'uint16')
  53.     b4_offs, offs = parse(buf, offs, 'uint32')
  54.     b4 = []
  55.     for i in range(b4_size):
  56.         val, b4_offs = parse(buf, b4_offs, 'int32')
  57.         b4.append(val)
  58.     return dict(B1=b1, B2=b2, B3=b3, B4=b4), offs
  59.  
  60.  
  61. def parse_a(buf, offs):
  62.     a1, offs = parse_b(buf, offs)
  63.     a2, offs = parse(buf, offs, 'uint16')
  64.     a3, offs = parse(buf, offs, 'uint16')
  65.     a4_size, offs = parse(buf, offs, 'uint16')
  66.     a4_offs, offs = parse(buf, offs, 'uint32')
  67.     a4 = []
  68.     for _ in range(a4_size):
  69.         val, a4_offs = parse(buf, a4_offs, 'char')
  70.         a4.append(val)
  71.     a4 = b''.join(a4).decode('utf-8')
  72.     d_offs, offs = parse(buf, offs, 'uint16')
  73.     a5, _ = parse_d(buf, d_offs)
  74.     a6 = []
  75.     for _ in range(3):
  76.         val, offs = parse(buf, offs, 'double')
  77.         a6.append(val)
  78.     a7, offs = parse_e(buf, offs)
  79.     return dict(A1=a1, A2=a2, A3=a3, A4=a4, A5=a5, A6=a6, A7=a7), offs
  80.  
  81.  
  82. def main(stream):
  83.     return parse_a(stream, 4)[0]
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement