Advertisement
malixds_

Untitled

May 28th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. from struct import unpack_from, calcsize
  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_d(buf, offs):
  26. d1, offs = parse(buf, offs, "int64")
  27.  
  28. d2_size, offs = parse(buf, offs, 'uint32')
  29. d2_offset, offs = parse(buf, offs, 'uint16')
  30. d2 = []
  31.  
  32. for _ in range(d2_size):
  33. val, d2_offset = parse(buf, d2_offset, "uint32")
  34. d2.append(val)
  35.  
  36. d3, offs = parse(buf, offs, "uint8")
  37. d4, offs = parse(buf, offs, "int8")
  38.  
  39. d5_size, offs = parse(buf, offs, 'uint32')
  40. d5_offset, offs = parse(buf, offs, 'uint32')
  41. d5 = []
  42.  
  43. for _ in range(d5_size):
  44. val, d5_offset = parse(buf, d5_offset, "int64")
  45. d5.append(val)
  46.  
  47. return dict(D1=d1, D2=d2, D3=d3, D4=d4, D5=d5), offs
  48.  
  49.  
  50. def parse_c(buf, offs):
  51. c1, offs = parse(buf, offs, 'float')
  52. c2, offs = parse(buf, offs, 'float')
  53. return dict(C1=c1, C2=c2), offs
  54.  
  55.  
  56. def parse_b(buf, offs):
  57. b1_size, offs = parse(buf, offs, 'uint32')
  58. b1_offset, offs = parse(buf, offs, 'uint16')
  59. b1 = []
  60. for _ in range(b1_size):
  61. val, b1_offset = parse_c(buf, b1_offset)
  62. b1.append(val)
  63.  
  64. b2 = []
  65. for _ in range(5):
  66. val, offs = parse(buf, offs, "uint16")
  67. b2.append(val)
  68.  
  69. b3, offs = parse(buf, offs, "uint32")
  70. b4, offs = parse(buf, offs, "uint64")
  71. b5, offs = parse(buf, offs, "uint16")
  72.  
  73. b6, _ = parse_d(buf, offs)
  74.  
  75. return dict(B1=b1, B2=b2, B3=b3, B4=b4, B5=b5, B6=b6, ), offs
  76.  
  77.  
  78. def parse_a(buf, offs):
  79. a1, offs = parse(buf, offs, 'uint64')
  80. a2, offs = parse(buf, offs, 'double')
  81. a3, offs = parse(buf, offs, 'uint8')
  82. a4, offs = parse(buf, offs, 'uint8')
  83. a5, offs = parse(buf, offs, 'int32')
  84.  
  85. a6_offset, offs = parse(buf, offs, 'uint32')
  86. a6, _ = parse_b(buf, a6_offset)
  87. return dict(A1=a1, A2=a2, A3=a3, A4=a4, A5=a5, A6=a6), offs
  88.  
  89.  
  90. def main(stream):
  91. return parse_a(stream, 5)[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement