-
Notifications
You must be signed in to change notification settings - Fork 1
/
base41.py
60 lines (47 loc) · 1.5 KB
/
base41.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
#! /usr/bin/python2.7
"""Base 41 Data encoding"""
def base41_decode(input):
"""Decode a Base41 string.
input is the string to decode.
The decoded data is returned. A TypeError is raised if input
is not valid (odd number of chars, non-alphabet character present,
invalid word).
"""
rslt = bytearray()
i = 0
while i + 2 < len(input):
x = (ord(input[i]) - 41) + 41 * (ord(input[i+1]) - 41) + 1681*(ord(input[i+2]) - 41)
rslt.extend([x % 256, x // 256])
i += 3
if i != len(input):
raise TypeError("Invalid Base41 string")
return rslt
def base41_encode(input):
"""Encode an array of bytes to a Base41 string.
input is the array of bytes to encode.
The encoded string is returned.
If input has an odd number of bytes, a TypeError is raised.
"""
rslt = ""
i = 0
while i + 1 < len(input):
x = input[i] + 256 * input[i+1]
rslt += chr((x % 41) + 41)
x //= 41
rslt += chr((x % 41) + 41) + chr((x // 41) + 41)
i += 2
if i != len(input):
raise TypeError("Invalid input length for Base41 encoding")
return rslt
def test():
"""Test the Base41 codec"""
data = bytearray([26,168,48,178,7,162,52,188,199,36])
s = "BABA.DEDA.DECA."
decoded = base41_decode(s)
if data != decoded:
raise RuntimeError("bad decoding")
encoded = base41_encode(data)
if s != encoded:
raise RuntimeError("bad decoding")
if __name__ == '__main__':
test()