##// END OF EJS Templates
py3: converts bytes to pycompat.bytestr to get bytechrs while enumerating...
Pulkit Goyal -
r36209:80301c90 default
parent child Browse files
Show More
@@ -1,79 +1,80 b''
1 # base85.py: pure python base85 codec
1 # base85.py: pure python base85 codec
2 #
2 #
3 # Copyright (C) 2009 Brendan Cully <brendan@kublai.com>
3 # Copyright (C) 2009 Brendan Cully <brendan@kublai.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import struct
10 import struct
11
11
12 from .. import pycompat
12 from .. import pycompat
13
13
14 _b85chars = pycompat.bytestr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
14 _b85chars = pycompat.bytestr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
15 "ghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~")
15 "ghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~")
16 _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
16 _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
17 _b85dec = {}
17 _b85dec = {}
18
18
19 def _mkb85dec():
19 def _mkb85dec():
20 for i, c in enumerate(_b85chars):
20 for i, c in enumerate(_b85chars):
21 _b85dec[c] = i
21 _b85dec[c] = i
22
22
23 def b85encode(text, pad=False):
23 def b85encode(text, pad=False):
24 """encode text in base85 format"""
24 """encode text in base85 format"""
25 l = len(text)
25 l = len(text)
26 r = l % 4
26 r = l % 4
27 if r:
27 if r:
28 text += '\0' * (4 - r)
28 text += '\0' * (4 - r)
29 longs = len(text) >> 2
29 longs = len(text) >> 2
30 words = struct.unpack('>%dL' % (longs), text)
30 words = struct.unpack('>%dL' % (longs), text)
31
31
32 out = ''.join(_b85chars[(word // 52200625) % 85] +
32 out = ''.join(_b85chars[(word // 52200625) % 85] +
33 _b85chars2[(word // 7225) % 7225] +
33 _b85chars2[(word // 7225) % 7225] +
34 _b85chars2[word % 7225]
34 _b85chars2[word % 7225]
35 for word in words)
35 for word in words)
36
36
37 if pad:
37 if pad:
38 return out
38 return out
39
39
40 # Trim padding
40 # Trim padding
41 olen = l % 4
41 olen = l % 4
42 if olen:
42 if olen:
43 olen += 1
43 olen += 1
44 olen += l // 4 * 5
44 olen += l // 4 * 5
45 return out[:olen]
45 return out[:olen]
46
46
47 def b85decode(text):
47 def b85decode(text):
48 """decode base85-encoded text"""
48 """decode base85-encoded text"""
49 if not _b85dec:
49 if not _b85dec:
50 _mkb85dec()
50 _mkb85dec()
51
51
52 l = len(text)
52 l = len(text)
53 out = []
53 out = []
54 for i in range(0, len(text), 5):
54 for i in range(0, len(text), 5):
55 chunk = text[i:i + 5]
55 chunk = text[i:i + 5]
56 chunk = pycompat.bytestr(chunk)
56 acc = 0
57 acc = 0
57 for j, c in enumerate(chunk):
58 for j, c in enumerate(chunk):
58 try:
59 try:
59 acc = acc * 85 + _b85dec[c]
60 acc = acc * 85 + _b85dec[c]
60 except KeyError:
61 except KeyError:
61 raise ValueError('bad base85 character at position %d'
62 raise ValueError('bad base85 character at position %d'
62 % (i + j))
63 % (i + j))
63 if acc > 4294967295:
64 if acc > 4294967295:
64 raise ValueError('Base85 overflow in hunk starting at byte %d' % i)
65 raise ValueError('Base85 overflow in hunk starting at byte %d' % i)
65 out.append(acc)
66 out.append(acc)
66
67
67 # Pad final chunk if necessary
68 # Pad final chunk if necessary
68 cl = l % 5
69 cl = l % 5
69 if cl:
70 if cl:
70 acc *= 85 ** (5 - cl)
71 acc *= 85 ** (5 - cl)
71 if cl > 1:
72 if cl > 1:
72 acc += 0xffffff >> (cl - 2) * 8
73 acc += 0xffffff >> (cl - 2) * 8
73 out[-1] = acc
74 out[-1] = acc
74
75
75 out = struct.pack('>%dL' % (len(out)), *out)
76 out = struct.pack('>%dL' % (len(out)), *out)
76 if cl:
77 if cl:
77 out = out[:-(5 - cl)]
78 out = out[:-(5 - cl)]
78
79
79 return out
80 return out
General Comments 0
You need to be logged in to leave comments. Login now