Advertisement
yasi04

Maks_12

Jun 3rd, 2024 (edited)
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. from struct import *
  2.  
  3.  
  4. FMT = dict(
  5.     char='c',
  6.     int8='b',
  7.     uint8='B',
  8.     int16='h',
  9.     uint16='H',
  10.     int32='i',
  11.     uint32='I',
  12.     int64='q',
  13.     uint64='Q',
  14.     float='f',
  15.     double='d'
  16. )
  17.  
  18.  
  19. def parse(buf, offs, ty, order='>'):
  20.     pattern = FMT[ty]
  21.     size = calcsize(pattern)
  22.     value = unpack_from(order + pattern, buf, offs)[0]
  23.     return value, offs + size
  24.  
  25.  
  26. def parse_d(buf, offs):
  27.     d1, offs = parse(buf, offs, 'int8')
  28.     d2 = []
  29.     for _ in range(4):
  30.         val, offs = parse(buf, offs, 'uint32')
  31.         d2.append(val)
  32.     d3, offs = parse(buf, offs, 'uint8')
  33.     d4, offs = parse(buf, offs, 'int32')
  34.     return dict(D1=d1, D2=d2, D3=d3, D4=d4), offs
  35.  
  36.  
  37. def parse_c(buf, offs):
  38.     c1, offs = parse(buf, offs, 'uint16')
  39.     c2 = []
  40.     for _ in range(8):
  41.         val, offs = parse(buf, offs, 'float')
  42.         c2.append(val)
  43.     return dict(C1=c1, C2=c2), offs
  44.  
  45.  
  46. def parse_b(buf, offs):
  47.     b1, offs = parse(buf, offs, 'uint16')
  48.     b2size, offs = parse(buf, offs, 'uint32')
  49.     b2offs, offs = parse(buf, offs, 'uint32')
  50.     b2 = ''
  51.     for _ in range(b2size):
  52.         val, b2offs = parse(buf, b2offs, "char")
  53.         b2 += val.decode()
  54.     return dict(B1=b1, B2=b2), offs
  55.  
  56.  
  57. def parse_a(buf, offs):
  58.     a1 = []
  59.     for _ in range(2):
  60.         val, offs = parse_b(buf, offs)
  61.         a1.append(val)
  62.     a2, offs = parse_c(buf, offs)
  63.     a3, offs = parse(buf, offs, 'uint8')
  64.     a4, offs = parse(buf, offs, 'uint8')
  65.     a5, offs = parse(buf, offs, 'int8')
  66.     a6offs, offs = parse(buf, offs, 'uint32')
  67.     a6, offs = parse_d(buf, a6offs)
  68.     return dict(A1=a1, A2=a2, A3=a3, A4=a4, A5=a5, A6=a6), offs
  69.  
  70.  
  71. def main(stream):
  72.     res, _ = parse_a(stream, 5)
  73.     return res
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement