-
Notifications
You must be signed in to change notification settings - Fork 11
/
compression_test2.py
45 lines (28 loc) · 1 KB
/
compression_test2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# Author Dario Clavijo 2016
import zlib
import bz2
import fileinput
import struct
zlib_magic = "ZLIB"
def ser_uint(i):
return hex(i).replace("0x", "").replace("L", "").zfill(8).decode("hex")
def deser_uint(s):
return int(s.encode("hex"), 16)
def test(msg_data):
print("Criginal data:", msg_data)
msg_data = zlib.compress(msg_data, 1)
print("Compressed data(hex): " + msg_data.encode("hex"))
msg_data = struct.pack(
">4si{0:d}s".format(len(msg_data)), zlib_magic, len(msg_data), msg_data
)
# msg_data = "ZLIB%s%s" % (ser_uint(len(msg_data)),msg_data)
print("Packed data: " + msg_data.encode("hex"))
if msg_data[:4] == zlib_magic:
msg_len = deser_uint(msg_data[4:8])
print("msg_len: {0!s}".format(msg_len))
a, b, c = struct.unpack(">4si{0:d}s".format(msg_len), msg_data)
print("Pack:", a, b, c.encode("hex"))
new_data = zlib.decompress(c)
print(f"Decompressed data: {new_data}")
test("dario")