-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
177 lines (151 loc) · 4.17 KB
/
convert.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
import re
import random
import rs
skip = [0b101010, 0b010101, 0b000000, 0b111111];
values = ["{:06b}".format(x) for x in range(0, 2**6) if not x in skip]
lowChars = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./ "
highChars = '~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>? '
SHIFT_VAL = values[-1]
SENTINEL = "00010101010101000"
rsEncoder = rs.RSCoder(len(values),25)
def changeListBase(arr, oldbase, newbase):
temp = reduce(lambda a, val: a*oldbase + val, arr, 0)
res = []
while temp > 0:
res.append(int(temp % newbase))
temp /= newbase
res.reverse()
return res
def encode(s):
rsenc = rsEncoder.encode(s)
indices = changeListBase(rsenc, 256, len(values))
return ''.join(values[i] for i in indices)
# for c in s:
# if c in lowChars:
# idx = lowChars.index(c)
# res.append(values[idx])
# elif c in highChars:
# idx = highChars.index(c)
# res.append(SHIFT_VAL)
# res.append(values[idx])
# return ''.join(res)
def decode(bits):
parts = re.findall(".{6}",bits)
indices = [values.index(seq) for seq in parts]
rsenc = changeListBase(indices, len(values), 256)
result = rsEncoder.decode(rsenc)
return result
# res = []
# wasShift = False
# for b in re.findall(".{6}",bits):
# try:
# if b == SHIFT_VAL:
# wasShift = True
# else:
# idx = values.index(b)
# if wasShift:
# res.append(highChars[idx])
# else:
# res.append(lowChars[idx])
# wasShift = False
# except:
# pass
# return ''.join(res)
# def checksum(bits):
# sum = 0
# for b in re.findall(".{6}",bits):
# sum += int(b,2)
# csidx = sum % len(values)
# return values[csidx]
def makeMessage(type,body):
t = values[lowChars.index(type)]
encoded = encode(body)
datasize = len(encoded)/6
if(datasize >= len(values)):
raise Exception("Message is too long.")
# cs = checksum(encoded)
print t,datasize
# sizeBit1 = datasize / len(values)
# sizeBit2 = datasize % len(values)
msg = [SENTINEL]
msg.append(t)
msg.append(values[datasize])
# msg.append(cs)
msg.append(encoded)
#print msg
return ''.join(msg)
class MessageDecoder:
state = "scanning"
bits = ""
message = {}
counter = 0
data = ""
def __init__(self):
pass
def reset(self):
self.state = "scanning"
self.bits = ""
self.message = {}
self.counter = 0
self.data = ""
def consume(self, bit):
try:
self.bits += "1" if bit else "0"
#print self.state, self.bits
if self.state == "scanning":
if self.bits == SENTINEL:
print "FOUND SENTINEL!"
self.state = "header_type"
self.bits = ""
while not SENTINEL.startswith(self.bits):
self.bits = self.bits[1:]
elif len(self.bits) == 6:
if self.state == "header_type":
self.message['type'] = lowChars[values.index(self.bits)]
self.state = "header_size"
print "Got header type {}".format(self.message['type'])
elif self.state == "header_size":
if self.counter == 0:
self.message['size'] = values.index(self.bits)
self.state = "body"
print "Got header size {}".format(self.message['size'])
# elif self.state == "header_checksum":
# self.message['checksum'] = self.bits
# self.state = "body"
# print "Got header checksum {}".format(self.message['checksum'])
elif self.state == "body":
self.data += self.bits
self.counter += 1
if self.counter >= self.message['size']:
self.message['body'] = decode(self.data)
result = self.message
print "Got message body! {}".format(self.message['body'])
self.reset()
return result
self.bits = ""
return None
except ValueError, e:
print "VALUE ERROR"
print e
self.reset()
return None
# def decodeMessage():
# decoder = MessageDecoder();
# while True:
# bit = yield None
# val = decoder.consume(bit)
# if val != None:
# yield val
# raise StopIteration()
if __name__ == '__main__':
e = '{:b}'.format(random.randint(0,2**random.randint(3,20))) \
+ makeMessage('m',"Testing") \
+ '{:b}'.format(random.randint(0,2**random.randint(3,20))) \
+ makeMessage('m',"Test") \
+ '{:b}'.format(random.randint(0,2**random.randint(3,20)))
print(e)
decoder = MessageDecoder();
for bit in e:
value = decoder.consume(bit == '1')
if value != None:
print value