Skip to content

Instantly share code, notes, and snippets.

@mikeb01
Created July 22, 2015 11:30
Show Gist options
  • Save mikeb01/08c096864dd415e4dc95 to your computer and use it in GitHub Desktop.
Save mikeb01/08c096864dd415e4dc95 to your computer and use it in GitHub Desktop.
from ctypes import *
import base64
import gzip, zlib
class HDR_COMPRESSED(BigEndianStructure):
_pack_ = 1
_fields_ = [
("cookie", c_int),
("length", c_int)
]
class HDR_ENCODED(BigEndianStructure):
_pack_ = 1
_fields_ = [
("cookie", c_int),
("significant_figures", c_int),
("lowest_trackable_value", c_longlong),
("highest_trackable_value", c_longlong),
("total_count", c_longlong)
]
class COUNT(BigEndianStructure):
_fields_ = [("value", c_longlong)]
def main():
# Base64 decode the string and store the compressed binary value in a bytestring
compressed = bytearray(base64.decodestring('HISTiQAAAGd42pNpmdzBwMDAxAAGfgoMDMxuBjsWQLgMTG+hDDYGVMCExmdGo1nQ1LGg0VxQmhNKi0NpISgtDKUtoLQflPaG0v1Quh1Kp6GZww+lWXG4n1jAyDAKRsNtFIzG82g8j4IRCABtRQfZ'))
# Create a ctypes struct over the top of the bytearray
hdr_compressed = HDR_COMPRESSED.from_buffer(compressed)
# Get the head fields which is followed by the compressed data
print("compressed.cookie: {:#x}".format(hdr_compressed.cookie))
print("compressed.length: {:d}".format(hdr_compressed.length))
# Decompress the remaining data.
encoded = bytearray(zlib.decompress(bytes(compressed[8:hdr_compressed.length + 8])))
# Create a ctypes flyweight over the uncompressed data
hdr_encode = HDR_ENCODED.from_buffer(encoded)
# Read out the header fields.
# Note this is the current jhiccup format which uses the older V0 format.
# To be correct the cookie should be checked.
print("encoded.cookie: {:#x}".format(hdr_encode.cookie))
print("encoded.significant_figures: {:d}".format(hdr_encode.significant_figures))
print("encoded.lowest_trackable_value: {:#d}".format(hdr_encode.lowest_trackable_value))
print("encoded.highest_trackable_value: {:#d}".format(hdr_encode.highest_trackable_value))
print("encoded.total_count: {:d}".format(hdr_encode.total_count))
uncompressed_header_len = 32;
word_size = 8
# This is a hack, the histogram should be created from the above fields and figure out
# the required data length.
data_len = (len(encoded) - uncompressed_header_len) / word_size
# Create an array over the remaining data
CountsArrayType = COUNT * data_len;
counts_array = CountsArrayType.from_buffer(encoded[uncompressed_header_len:len(encoded) + uncompressed_header_len])
for i in counts_array:
print i.value
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment