Last active
July 8, 2018 21:08
-
-
Save spotco/3d38e1eee4185864415177c218ea93f5 to your computer and use it in GitHub Desktop.
bindecompress.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import struct | |
| import os | |
| import binascii | |
| _fh = open(sys.argv[1]) | |
| def bytearray_to_int(input): | |
| return int(input.encode('hex'), 16) | |
| def float_to_bytes(val): | |
| return struct.unpack("f",val)[0] | |
| def reverse_read_to_bytearray(size): | |
| global _fh | |
| _fh.seek(-size,os.SEEK_CUR) | |
| rtv = _fh.read(size) | |
| _fh.seek(-size,os.SEEK_CUR) | |
| return rtv | |
| COLUMN_SYMBOL = "symbol" | |
| COLUMN_SYM3 = "sym3" | |
| COLUMN_TIMESTAMP_DELTA = "timestamp_delta" | |
| COLUMN_ENDINT = "endint" | |
| _type_states = {} | |
| def read_def_table(type): | |
| global _fh | |
| global _type_states | |
| type_table = {} | |
| table_size = bytearray_to_int(reverse_read_to_bytearray(2)) | |
| for i in range(0,table_size): | |
| val = bytearray_to_int(reverse_read_to_bytearray(2)) | |
| strlen = bytearray_to_int(reverse_read_to_bytearray(1)) | |
| key = reverse_read_to_bytearray(strlen) | |
| type_table[val] = key | |
| _type_states[type] = type_table | |
| _fh.seek(0,os.SEEK_END) | |
| _initial_timestamp = bytearray_to_int(reverse_read_to_bytearray(4)) | |
| read_def_table(COLUMN_ENDINT) | |
| read_def_table(COLUMN_TIMESTAMP_DELTA) | |
| read_def_table(COLUMN_SYM3) | |
| read_def_table(COLUMN_SYMBOL) | |
| BYTES_PER_LINE = 12 | |
| num_lines = _fh.tell() / BYTES_PER_LINE | |
| def type_state_lookup(val, type): | |
| global _type_states | |
| return _type_states[type][val] | |
| _fh.seek(0,os.SEEK_SET) | |
| cur_timestamp = _initial_timestamp | |
| for i in range(0,num_lines): | |
| symbol = type_state_lookup(bytearray_to_int(_fh.read(2)),COLUMN_SYMBOL) | |
| sym3 = type_state_lookup(bytearray_to_int(_fh.read(2)),COLUMN_SYM3) | |
| timestamp_delta = int(type_state_lookup(bytearray_to_int(_fh.read(2)),COLUMN_TIMESTAMP_DELTA)) | |
| float_bytes = _fh.read(4) | |
| endint = int(type_state_lookup(bytearray_to_int(_fh.read(2)),COLUMN_ENDINT)) | |
| sym0 = sym3[0] | |
| sym1 = sym3[1] | |
| sym2 = sym3[2] | |
| cur_timestamp = cur_timestamp + timestamp_delta | |
| floatval = float_to_bytes(float_bytes) | |
| print("%s,%s,%s,%s,%d,%d,%.2f,%d"%(symbol,sym0,sym1,sym2,cur_timestamp,cur_timestamp,floatval,endint)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment