-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder_lzss.py
61 lines (47 loc) · 1.62 KB
/
decoder_lzss.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
"""
Name: Hew Ye Zea
Student ID: 29035546
Date Created : 16/6/2020
Date Last Edited : 26/6/2020
"""
import sys
from huffman import *
from elias import *
from bitarray import bitarray
def decode(filename):
"""
Decode a file
:param filename: file to be decoded
:return: a file that contains the decoded string
"""
bitcode = bitarray()
file = open(filename, 'rb')
bitcode.fromfile(file)
# build a huffman tree to decode characters
huffman_decoder = HuffmanTree()
start_index = huffman_decoder.buildTree(bitcode)
# decode the number of formats
num_of_format, start_index = eliasDecode(bitcode,start_index)
retval = ''
# decode all formats
for i in range(num_of_format):
start_index += 1
# match length > 3 : format starts with "0"
if not bitcode[start_index - 1]:
offset, start_index = eliasDecode(bitcode,start_index) # decode the offset
matched_length, start_index = eliasDecode(bitcode,start_index) # decode match length
offset_start = len(retval) - offset
for j in range(matched_length):
retval += retval[j + offset_start] # copy the characters
# match length < 3 , format starts with "1"
else:
char_ascii,start_index = huffman_decoder.decodeHuffman(bitcode,start_index)
retval += chr(char_ascii) # decode the character
output = open('output_decoder_lzss.txt', 'w')
output.write(retval)
output.close()
file.close()
if __name__ == '__main__':
_, filename = sys.argv
decode(filename)
#decode('output_encoder_lzss.bin')