##// END OF EJS Templates
pure: use string for another exception in the pure version of base85...
marmoute -
r44082:4cd91104 stable
parent child Browse files
Show More
@@ -1,88 +1,88 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(
14 _b85chars = pycompat.bytestr(
15 b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
15 b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
16 b"ghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
16 b"ghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
17 )
17 )
18 _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
18 _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
19 _b85dec = {}
19 _b85dec = {}
20
20
21
21
22 def _mkb85dec():
22 def _mkb85dec():
23 for i, c in enumerate(_b85chars):
23 for i, c in enumerate(_b85chars):
24 _b85dec[c] = i
24 _b85dec[c] = i
25
25
26
26
27 def b85encode(text, pad=False):
27 def b85encode(text, pad=False):
28 """encode text in base85 format"""
28 """encode text in base85 format"""
29 l = len(text)
29 l = len(text)
30 r = l % 4
30 r = l % 4
31 if r:
31 if r:
32 text += b'\0' * (4 - r)
32 text += b'\0' * (4 - r)
33 longs = len(text) >> 2
33 longs = len(text) >> 2
34 words = struct.unpack(b'>%dL' % longs, text)
34 words = struct.unpack(b'>%dL' % longs, text)
35
35
36 out = b''.join(
36 out = b''.join(
37 _b85chars[(word // 52200625) % 85]
37 _b85chars[(word // 52200625) % 85]
38 + _b85chars2[(word // 7225) % 7225]
38 + _b85chars2[(word // 7225) % 7225]
39 + _b85chars2[word % 7225]
39 + _b85chars2[word % 7225]
40 for word in words
40 for word in words
41 )
41 )
42
42
43 if pad:
43 if pad:
44 return out
44 return out
45
45
46 # Trim padding
46 # Trim padding
47 olen = l % 4
47 olen = l % 4
48 if olen:
48 if olen:
49 olen += 1
49 olen += 1
50 olen += l // 4 * 5
50 olen += l // 4 * 5
51 return out[:olen]
51 return out[:olen]
52
52
53
53
54 def b85decode(text):
54 def b85decode(text):
55 """decode base85-encoded text"""
55 """decode base85-encoded text"""
56 if not _b85dec:
56 if not _b85dec:
57 _mkb85dec()
57 _mkb85dec()
58
58
59 l = len(text)
59 l = len(text)
60 out = []
60 out = []
61 for i in range(0, len(text), 5):
61 for i in range(0, len(text), 5):
62 chunk = text[i : i + 5]
62 chunk = text[i : i + 5]
63 chunk = pycompat.bytestr(chunk)
63 chunk = pycompat.bytestr(chunk)
64 acc = 0
64 acc = 0
65 for j, c in enumerate(chunk):
65 for j, c in enumerate(chunk):
66 try:
66 try:
67 acc = acc * 85 + _b85dec[c]
67 acc = acc * 85 + _b85dec[c]
68 except KeyError:
68 except KeyError:
69 raise ValueError(
69 raise ValueError(
70 'bad base85 character at position %d' % (i + j)
70 'bad base85 character at position %d' % (i + j)
71 )
71 )
72 if acc > 4294967295:
72 if acc > 4294967295:
73 raise ValueError(b'Base85 overflow in hunk starting at byte %d' % i)
73 raise ValueError('Base85 overflow in hunk starting at byte %d' % i)
74 out.append(acc)
74 out.append(acc)
75
75
76 # Pad final chunk if necessary
76 # Pad final chunk if necessary
77 cl = l % 5
77 cl = l % 5
78 if cl:
78 if cl:
79 acc *= 85 ** (5 - cl)
79 acc *= 85 ** (5 - cl)
80 if cl > 1:
80 if cl > 1:
81 acc += 0xFFFFFF >> (cl - 2) * 8
81 acc += 0xFFFFFF >> (cl - 2) * 8
82 out[-1] = acc
82 out[-1] = acc
83
83
84 out = struct.pack(b'>%dL' % (len(out)), *out)
84 out = struct.pack(b'>%dL' % (len(out)), *out)
85 if cl:
85 if cl:
86 out = out[: -(5 - cl)]
86 out = out[: -(5 - cl)]
87
87
88 return out
88 return out
General Comments 0
You need to be logged in to leave comments. Login now