Last active
June 22, 2016 14:31
-
-
Save killswitch-GUI/9cd738d9f3efccd19ed4ac4f10e6e46e to your computer and use it in GitHub Desktop.
This file contains 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 zlib | |
import struct | |
import sys | |
# 4 byte header | |
# crc32 uLong | |
# | |
# 0 1 | |
# +---+---+ | |
# |CMF|FLG| | |
# +---+---+ | |
# bits 0 to 3 CM Compression method | |
# bits 4 to 7 CINFO Compression info | |
def comp_data(data, cvalue=9): | |
''' | |
Takes in a string and computes | |
the comp obj. | |
data = string wanting compression | |
cvalue = 0-9 comp value (default 6) | |
''' | |
cdata = zlib.compress(data,cvalue) | |
return cdata | |
def crc32_data(data): | |
''' | |
Takes in a string and computes crc32 value. | |
data = string before compression | |
returns: | |
HEX bytes of data | |
''' | |
crc = zlib.crc32(data) & 0xFFFFFFFF | |
return crc | |
def build_header(data, crc): | |
''' | |
Takes comp data, org crc32 value, | |
and adds self header. | |
data = comp data | |
crc = crc32 value | |
''' | |
header = struct.pack("!I",crc) | |
built_data = header + data | |
return built_data | |
def dec_data(data): | |
''' | |
Takes: | |
Custom / standard header data | |
data = comp data with zlib header | |
returns: | |
dict with crc32 cheack and dec data string | |
ex. {"crc32" : true, "dec_data" : "-SNIP-"} | |
''' | |
comp_crc32 = struct.unpack("!I", data[:4])[0] | |
dec_data = zlib.decompress(data[4:]) | |
dec_crc32 = zlib.crc32(dec_data) & 0xFFFFFFFF | |
if comp_crc32 == dec_crc32: | |
crc32 = True | |
else: | |
crc32 = False | |
return { "header_crc32" : comp_crc32, "dec_crc32" : dec_crc32, "crc32_check" : crc32, "data" : dec_data } | |
data = "Killswitch-GUI is a sick handle" | |
start_crc32 = crc32_data(data) | |
print "-Starting str: ", data | |
print "-un-compressed data size: ", str(sys.getsizeof(data)) | |
print "-Start crc32 value: " + str(start_crc32) | |
comp_data = comp_data(data) | |
print "-Compression data size: " + str(sys.getsizeof(comp_data)) | |
final_comp_data = build_header(comp_data, start_crc32) | |
print "-Compression data with CRC header built size: " + str(sys.getsizeof(final_comp_data)) | |
print "-Starting decompress of data!" | |
dec_data = dec_data(final_comp_data) | |
print "-Final return data dict: ", dec_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment