forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase85.py
More file actions
185 lines (167 loc) · 8.03 KB
/
Copy pathbase85.py
File metadata and controls
185 lines (167 loc) · 8.03 KB
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
# -*- coding: UTF-8 -*-
"""Base85 Codec - base85 content encoding.
This is a simple wrapper for adding base64.b85**code to the codecs.
This codec:
- en/decodes strings from str to str
- en/decodes strings from bytes to bytes
- decodes file content to str (read)
- encodes file content from str to bytes (write)
"""
import base64
from ._base import _get_charset, digits, lower, main, upper
from ..__common__ import *
__examples__ = {
'enc-dec(base85|z85|base85-ipv6)': ["@random{512,1024,2048}"],
'enc-dec(base85-btoa|base85-xbtoa)': ["@random{512,1024,2048}"],
'enc(base85|ascii85)': {'this is a test': "FD,B0+DGm>@3BZ'F*%"},
'enc(base85-adobe)': {'this is a test': "<~FD,B0+DGm>@3BZ'F*%~>",
'this is a test\0\0\0\0\0\0': "<~FD,B0+DGm>@3BZ'F*%B^z~>"},
'enc(z85|base85-z)': {'this is a test': "BzbxfazC)tvixV6B94"},
'enc(base85-ipv6|base85_rfc1924)': {'this is a test': "bZBXFAZc?TVIXv6b94"},
'enc(base85_btoa)': {'this is a test': "FD,B0+DGm>@3BZ'F*%B^"},
'enc(base85_btoa)': {'this\0\0\0\0test': "FD,B0+DGm>@3BZ'F*%B^"},
'enc(base85_btoa)': {'this is a test\0\0\0\0': "FD,B0+DGm>y@3BZ'F*%B^z"},
'enc(base85-xbtoa)': {'this is a test': "xbtoa Begin\nFD,B0+DGm>@3BZ'F*%B^\nxbtoa End N 14 e E 4b" \
" S 523 R 1b132e"},
'dec(base85-xbtoa)': {'xbtoa Begin\nFD,B0+DGm>@3BZ\'F*%B^\nxbtoa End': None,
'xbtoa Begin\nFD,B0+DGm>@3BZ\'F*%B^\nxbtoa End N 14 e E 4b S 523 R 000bad':
None},
'enc(base85-xml)': {'this is a test': "bZBXFAZc@TVIXv6b94"},
'enc(base85|ascii85)': {'this\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0test': "FD,B0zzz!!!\"@ATMq"},
}
__guess__ = ["ascii85", "z85", "base85-ipv6", "base85-xml", "base85-adobe", "base85-xbtoa"]
B85 = {
r'(base[-_]?85([-_]ascii)?|ascii85)$': "!\"#$%&'()*+,-./" + digits + ":;<=>?@" + upper + "[\\]^_`" + lower[:21],
r'(z85|base[-_]?85[-_]z(eromq)?)$': digits + lower + upper + ".-:+=^!/*?&<>()[]{}@%$#",
r'base[-_]?85[-_](rfc1924|ipv6)$': digits + upper + lower + "!#$%&()*+-;<=>?@^_`{|}~",
r'base[-_]?85[-_]xml$': digits + upper + lower[:-1] + "!#$()*+,-./:;=?@^`{|}~z_",
}
B85[r'(base[-_]?85[-_]adobe)$'] = B85[r'(base[-_]?85[-_]x?btoa)$'] = B85[r'(base[-_]?85([-_]ascii)?|ascii85)$']
POW85 = [85 ** i for i in range(5)]
def __format(text, mode, decode=False, **kwargs):
if "adobe" in mode:
if decode:
if text.startswith("<~") and text.endswith("~>"):
text = text[2:-2]
else:
text = "<~" + text + "~>"
elif "xbtoa" in mode:
sp, ep = "xbtoa [bB]egin\n", "xbtoa [eE]nd"
if decode:
if re.match(r"^xbtoa\s+[bB]egin\n", text) and \
re.search(r"\nxbtoa\s+[eE]nd N \d+{h} E{h} S{h} R{h}\s*$".format(h=" [0-9a-fA-F]+"), text):
text = "".join(text.split("\n")[1:-1]).replace(" ", "")
elif not decode:
l, t = kwargs['length'], "\n".join(text[i:i+78] for i in range(0, len(text), 78))
text = "xbtoa Begin\n%s\nxbtoa End N %d %x E %x S %x R %x" % \
(t, l, l, kwargs['c_xor'], kwargs['c_sum'], kwargs['c_rot'])
return text
def __xbtoa_values(text):
try:
hr = "[0-9a-fA-F]+"
return re.search(r"\nxbtoa\s+[eE]nd N (\d+) ({h}) E ({h}) S ({h}) R ({h})\s*$".format(h=hr), text).groups()
except:
raise Base85DecodeError("Bad or missing xbtoa parameters")
def base85_encode(mode):
b85 = _get_charset(B85, mode)
def encode(input, errors="strict"):
r, l, kw = "", len(input), {}
if l == 0:
return input, 0
if "xbtoa" in mode:
kw['length'] = l
kw['c_xor'], kw['c_sum'], kw['c_rot'] = 0, 0, 0
n_pad = (4 - l % 4) % 4
for i in range(0, l, 4):
block = input[i:i+4]
if block == "\0\0\0\0" and b85[-3:] == "stu":
r += "z"
if block == "\x20\x20\x20\x20" and "btoa" in mode:
r += "y"
if "xbtoa" in mode:
for c in block:
k = ord(c)
kw['c_xor'] ^= k
kw['c_sum'] += k + 1
kw['c_rot'] <<= 1
if kw['c_rot'] & 0x80000000:
kw['c_rot'] += 1
kw['c_rot'] += k
if block == "\0\0\0\0" and b85[-3:] == "stu" or block == "\x20\x20\x20\x20" and "btoa" in mode:
continue
if len(block) < 4:
block += n_pad * "\0"
n, bl = s2i(block), ""
for _ in range(5):
n, k = divmod(n, 85)
bl = b85[k] + bl
r += bl
if "btoa" not in mode and n_pad:
r = r[:-n_pad]
if b85[-3:] == "stu" and r[-5:] == "!!!!!":
r = r[:-5] + "z"
return __format(r, mode, **kw), l
return encode
def base85_decode(mode):
b85 = _get_charset(B85, mode)
def decode(input, errors="strict"):
r, l, i, n_pad = "", len(input), 0, 0
if l == 0:
return input, 0
if "xbtoa" in mode:
v = __xbtoa_values(input)
n_last = int(v[0]) % 4
c_xor, c_sum, c_rot = 0, 0, 0
input = __format(input, mode, True)
ehandler = handle_error("base85", errors, decode=True)
if b85[-3:] == "stu" and input[-1] == "z":
input = input[:-1] + "!!!!!"
l = len(input)
while i < l:
n, incr = 0, 5
if input[i] == "z" and b85[-3:] == "stu":
bl, incr = "\0\0\0\0", 1
elif input[i] == "y" and "btoa" in mode:
bl, incr = "\x20\x20\x20\x20", 1
else:
block = input[i:i+5]
if len(block) < 5:
n_pad = 5 - len(block) % 5
block += n_pad * "\0"
for k, c in enumerate(block[::-1]):
try:
n += (b85.index(c) if c != "\0" else 255) * POW85[k]
except ValueError:
r += ehandler(c, i + k, r)
bl = codecs.decode("{:0>8}".format(hex(n & 0xffffffff)[2:]), "hex")
if "xbtoa" in mode:
if i + 5 == l and n_last > 0:
bl = bl[:n_last]
for c in bl:
k = ord(c)
c_xor ^= k
c_sum += k + 1
c_rot <<= 1
if c_rot & 0x80000000:
c_rot += 1
c_rot += k
r += bl
i += incr
if n_pad > 0:
r = r[:-n_pad]
if "xbtoa" in mode:
chkv = ["%d" % len(r), "%x" % len(r), "%x" % c_xor, "%x" % c_sum, "%x" % c_rot]
if any(v1 != v2 for v1, v2 in zip(v, chkv)) and errors == "strict":
raise Base85ValueError("A check value does not match (%s != %s)" % (str(list(v)).replace("'", ""),
str(chkv).replace("'", "")))
return r, l
return decode
add("base85", base85_encode, base85_decode, expansion_factor=lambda f, ename: f if "xbtoa" in ename else 1.25,
pattern=r"^(base[-_]?85(?:|[-_](?:adobe|x?btoa|ipv6|rfc1924|xml|z(?:eromq)?))|z85|ascii85)$",
extra_exceptions=["Base85ValueError"])
main85 = main(85, None)
main85adobe = main(85, None, "adobe")
main85xbtoa = main(85, None, "xbtoa", wrap=False)
main85rfc1924 = main(85, "RFC 1924", "ipv6")
main85xml = main(85, "<https://datatracker.ietf.org/doc/html/draft-kwiatkowski-base85-for-xml-00>", "xml")
main85zeromq = main(85, "<https://rfc.zeromq.org/spec/32/>", "zeromq")