Advertisement
Toxotsist

Task 11 py

May 21st, 2022
775
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pprint import pprint
  2. import struct
  3.  
  4. # H - uint16    2
  5. # h - int16     2
  6. # I - uint32    4
  7. # i - int32     4
  8. # b - int8      1
  9. # B - uint8     1
  10. # q - int64     8
  11. # Q - uint64    8
  12. # f - float     4
  13. # c - char      1
  14. # d - double    8
  15.  
  16. pointer = 4
  17.  
  18.  
  19. def structA(data):
  20.     A = {}
  21.     global pointer
  22.     A['A1'] = struct.unpack('>H', data[pointer:pointer + 2])[0]
  23.     pointer += 2
  24.     # print(A['A1'])
  25.     A['A2'] = []
  26.     size = struct.unpack('>H', data[pointer:pointer + 2])[0]
  27.     pointer += 2
  28.     address = struct.unpack('>H', data[pointer:pointer + 2])[0]
  29.     pointer += 2
  30.     for i in range(size):
  31.         b = struct.unpack('>I', data[address + i * 4:address + 4 + i * 4])[0]
  32.         A['A2'].append(b)
  33.     for i in range(size):
  34.         A['A2'][i] = structB(data, A['A2'][i])
  35.     address = struct.unpack('>H', data[pointer:pointer + 2])[0]
  36.     pointer += 2
  37.     A['A3'] = structC(data, address)
  38.     A['A4'] = struct.unpack('>i', data[pointer:pointer + 4])[0]
  39.     pointer += 4
  40.     return A
  41.  
  42.  
  43. def structB(data, address):
  44.     B = {}
  45.     B['B1'] = struct.unpack('>b', data[address:address + 1])[0]
  46.     address += 1
  47.     B['B2'] = struct.unpack('>H', data[address:address + 2])[0]
  48.     address += 2
  49.     return B
  50.  
  51.  
  52. def structC(data, address):
  53.     C = {}
  54.     C['C1'] = []
  55.     for i in range(4):
  56.         C['C1'].append(structD(data, address))
  57.         address += 1
  58.         address += 8
  59.     C['C2'] = struct.unpack('>b', data[address:address + 1])[0]
  60.     address += 1
  61.     address_2 = struct.unpack('>H', data[address:address + 2])[0]
  62.     address += 2
  63.     C['C3'] = structE(data, address_2)
  64.     C['C4'] = struct.unpack('>d', data[address:address + 8])[0]
  65.     address += 8
  66.     address_3 = struct.unpack('>H', data[address:address + 2])[0]
  67.     address += 2
  68.     C['C5'] = structF(data, address_3)
  69.     return C
  70.  
  71.  
  72. def structD(data, address):
  73.     D = {}
  74.     D['D1'] = struct.unpack('>B', data[address:address + 1])[0]
  75.     address += 1
  76.     D['D2'] = struct.unpack('>d', data[address:address + 8])[0]
  77.     address += 8
  78.     return D
  79.  
  80.  
  81. def structE(data, address):
  82.     E = {}
  83.     size = struct.unpack('>I', data[address:address + 4])[0]
  84.     address += 4
  85.     address_2 = struct.unpack('>I', data[address:address + 4])[0]
  86.     address += 4
  87.     E['E1'] = []
  88.     for i in range(size):
  89.         b = struct.unpack('>i', data[address_2 + i * 4:address_2 + i * 4 + 4])
  90.         E['E1'].append(b[0])
  91.     address_2 += 4 * size
  92.     size = struct.unpack('>I', data[address:address + 4])[0]
  93.     address += 4
  94.     address_2 = struct.unpack('>H', data[address:address + 2])[0]
  95.     address += 2
  96.     E['E2'] = []
  97.     for i in range(size):
  98.         b = struct.unpack('>b', data[address_2 + i:address_2 + i + 1])
  99.         E['E2'].append(b[0])
  100.     E['E3'] = struct.unpack('>I', data[address:address + 4])[0]
  101.     address += 4
  102.     E['E4'] = struct.unpack('>i', data[address:address + 4])[0]
  103.     address += 4
  104.     return E
  105.  
  106.  
  107. def structF(data, address):
  108.     F = {}
  109.     F['F1'] = struct.unpack('>f', data[address:address + 4])[0]
  110.     address += 4
  111.     F['F2'] = struct.unpack('>f', data[address:address + 4])[0]
  112.     address += 4
  113.     F['F3'] = struct.unpack('>h', data[address:address + 2])[0]
  114.     address += 2
  115.     F['F4'] = []
  116.     for i in range(5):
  117.         F['F4'].append(struct.unpack('>B', data[address:address + 1])[0])
  118.         address += 1
  119.     return F
  120.  
  121.  
  122. # d = (b'YXXBI\xfa\x00\x02\x00\x16\x00]S#\xa0\xf1\xab/;\xbc\xaf\xe2\x00\x00'
  123. #      b'\x00\x10\x00\x00\x00\x13\x03\x89\x92\xa0\xec\xb6\x86\x8aS\xba\xc2\xc4\xe35'
  124. #      b'\xf9\x88\xbd\x9c\x07\xcd\xfeX\x1c1\xe1\xe1\x00\x00\x00\x06\x00\x00\x00\x1e'
  125. #      b'\x00\x00\x00\x02\x006\xf1\x08?\x10s\xaa\x85W\xbe\xbe\xc4\xc4?3\xfb.\xac\x91'
  126. #      b'\x93k)/\xcc\xfe?\xda5\xd1\x80\xc5\xc60\x8b?\xe9T\x1c\x11Xu\x04!\xbf\xcazw'
  127. #      b'6\xe7\xb4\xf0H\xbf\xe6\xa0\xd5\xe6\xacRl\x19\x008\xbf\xe8\xd2\xa0\xf2n\xe5Z'
  128. #      b'\x00N')
  129.  
  130.  
  131. def main(d):
  132.     return structA(d)
  133.  
Advertisement
RAW Paste Data Copied
Advertisement