-
Notifications
You must be signed in to change notification settings - Fork 1
/
kolmoblock-generate.py
executable file
·171 lines (143 loc) · 5.32 KB
/
kolmoblock-generate.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
import argparse
import queue
import heapq
import struct
import kolmo
FREQUENCY_THRESHOLD = 1
TMP_FILE = 'out/cur'
def get_counts(symbol_size, target):
counts = {}
while True:
cur = target.read(symbol_size)
if len(cur)<symbol_size:
break
counts[cur] = counts.get(cur, 0) + 1
return counts
def print_counts():
sorted_cs = sorted(cs.items(), key=lambda x:-x[1])
for symbol, count in sorted_cs:
if count < FREQUENCY_THRESHOLD:
break
print("{} [{}]: {:,}".format(repr(symbol), symbol.encode('hex'), count))
def build_huffman(counts, token_size):
p = []
all_else = 0
for sym, count in cs.items():
if count < FREQUENCY_THRESHOLD:
all_else += count
continue
heapq.heappush(p,(count, sym))
heapq.heappush(p,(all_else, b"\a" * token_size ))
while len(p) > 1:
l = heapq.heappop(p)
r = heapq.heappop(p)
heapq.heappush(p, (l[0]+r[0]- 0.0017,(l,r)))
return heapq.heappop(p)
def generate_encoding(tree):
codes = {}
stack = [(tree, "")]
while len(stack) > 0:
(_, node), code = stack.pop()
if isinstance(node, bytes):
codes[node] = code
continue
left, right = node
stack.append((left, code + "0"))
stack.append((right, code + "1"))
return codes
def save_encoding_table(codes):
sorted_table = sorted(codes.items(), key=lambda x:len(x[1]))
with open(TMP_FILE, 'w') as et_file:
for symbol,representation in sorted_table:
et_file.write("{}|{}\n".format(representation, repr(symbol)))
return kolmo.name_by_content(TMP_FILE, {
"MIME": "text/plain",
"tag": "huffman_encoding_table",
}
)
def save_huffman_tree(tree, token_size):
with open(TMP_FILE, 'wb') as ht_file:
stack = [tree]
while len(stack) > 0:
(_, node) = stack.pop()
if isinstance(node, bytes):
ht_file.write(node)
continue
ht_file.write(str.encode("\0"*token_size))
stack.append(node[1])
stack.append(node[0])
return kolmo.name_by_content(TMP_FILE, {
"MIME": "application/octet-stream",
"tag": "huffman_encoding_table binary serialized",
"token_size": token_size,
}
)
def load_huffman_tree(serialized_file, token_size):
with open(serialized_file, 'rb') as s_file:
def allocateNode():
next_cell = s_file.read(token_size)
if next_cell != b'\0'*token_size:
return (10, next_cell)
left = allocateNode()
right = allocateNode()
return (10, (left, right))
root = allocateNode()
return root
def save_compressed_data(symbol_size, codes, target_file):
current = ""
with open(TMP_FILE, 'w') as txt_cd_file:
while True:
cur = target_file.read(symbol_size)
if len(cur)< symbol_size:
break
if cur in codes:
representation = codes[cur]
else:
literal = ""
for each in cur:
prefix = bin(each)[2:]
zero_padding = "0"*(8-len(prefix))
literal += zero_padding + prefix
representation = codes[b'\a'* symbol_size] + literal
txt_cd_file.write(representation)
current += representation
compressed_data_humans = kolmo.name_by_content(TMP_FILE, {
"MIME": "text/plain",
"tag": "huffman_encoded data human-readable",
"token_size": symbol_size,
}
)
num_of_bytes = len(current) // (8)
with open(TMP_FILE, 'wb') as cd_file:
for i in range(num_of_bytes):
bytes = current[8*i:8*(i+1)]
int_value = int(bytes, base=2)
cd_file.write(int_value.to_bytes(1,'little'))
compressed_data_binary = kolmo.name_by_content(TMP_FILE, {
"MIME": "application/octet-stream",
"tag": "huffman_encoded data binary",
"token_size": symbol_size,
}
)
return compressed_data_humans, compressed_data_binary
parser = argparse.ArgumentParser()
parser.add_argument('--token_size', dest='token_size',default=1, type=int)
parser.add_argument('--target', dest='target', type=str)
parser.add_argument('--huffmantree', dest='huffmantree', default="", type=str)
args = parser.parse_args()
with open(args.target,'rb') as input_file:
cs = get_counts(args.token_size, input_file)
if args.huffmantree == "":
huffman_tree = build_huffman(cs, args.token_size)
htree_serialized_hash = save_huffman_tree(huffman_tree, args.token_size)
else:
huffman_tree = load_huffman_tree("out/raw/" + args.huffmantree, args.token_size)
htree_serialized_hash = args.huffmantree
encoding_table = generate_encoding(huffman_tree)
huffman_tree_humans = save_encoding_table(encoding_table)
with open(args.target,'rb') as input_file:
encoded_data_hash_humans, encoded_data_hash = save_compressed_data(args.token_size, encoding_table, input_file)
kolmo.generate_huffman_manifest(args.target, args.token_size, htree_serialized_hash, encoded_data_hash)
print("human readable encoded data: %s" % encoded_data_hash_humans)
print("human readable huffman tree %s" % huffman_tree_humans)