forked from AdrainYe/CTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LZ-78.py
196 lines (88 loc) · 2.77 KB
/
LZ-78.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class LZEncode:
def __init__(self,inputText):
self.inputText = inputText
self.segment = []
self.codeWord = []
self.code = []
self.length = 0
def FillBin(self,bin):
need = self.length - len(bin)
if need != 0:
bin = '0'*need + bin
return bin
def CreateSegment(self):
n = 0
while 2**n < len(self.codeWord)+1:
n = n + 1
self.length = n
self.segment = [self.FillBin(bin(i+1)[2:]) for i in range(len(self.codeWord))]
def CreateWord(self):
flag = ''
for i in self.inputText:
flag += i
if self.codeWord.count(flag) == 0:
self.codeWord.append(flag)
flag = ''
def ToGetCode(self,codeWord,num):
if num == 1:
return self.length*'0' + codeWord
else:
getIndex = self.codeWord.index(codeWord[0:-1])
return self.segment[getIndex]+codeWord[-1]
def Code(self):
self.code = [self.ToGetCode(i,len(i)) for i in self.codeWord]
def Encode(self):
if ''.join(self.codeWord) != self.inputText:
theLast = self.inputText[len(''.join(self.codeWord)):]
getIndex = self.codeWord.index(theLast)
return ''.join(self.code + [self.code[getIndex]]),len(self.codeWord),self.length
else:
return ''.join(self.code),len(self.codeWord)-1,self.length
def Control(self):
self.CreateWord()
self.CreateSegment()
self.Code()
return self.Encode()
class LZDecode:
def __init__(self,encodeText,length,codeLength):
self.encodeText = encodeText
self.length = length
self.segment = []
self.codeWord = []
self.code = []
self.length = length
self.codeLength = codeLength
def FillBin(self,bin,n):
need = n - len(bin)
if need != 0:
bin = '0'*need + bin
return bin
def CreateSegment(self):
n = 0
while 2**n < self.length+1:
n = n + 1
self.segment = [self.FillBin(bin(i+1)[2:],n) for i in range(self.length)]
def CreateDict(self):
self.CreateSegment()
for i in range(len(self.encodeText)/(self.codeLength+1)):
getOne = self.encodeText[i*(self.codeLength+1):(i+1)*(self.codeLength+1)]
if getOne[0:self.codeLength] == '0'*self.codeLength:
self.codeWord.append(getOne[self.codeLength])
else:
getSegmentIndex = self.segment.index(getOne[0:self.codeLength])
self.codeWord.append(self.codeWord[getSegmentIndex]+getOne[self.codeLength])
return ''.join(self.codeWord)
def main():
#inputText = '00100101110110011'
inputText = raw_input('Please input the bin you want you encode and decode:')
lzEn = LZEncode(inputText)
encode,length,codeLength = lzEn.Control()
lzDe = LZDecode(encode,length,codeLength)
decode = lzDe.CreateDict()
print 'The source text is:' + inputText
print 'The encode text is:' + encode
print 'The decode text is:' + decode
if __name__ == '__main__':
main()