Mercurial > cpython-lru_cache-weakref
annotate Lib/base64.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 3c9232a684ae |
| children | c760bd844222 |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
55589
diff
changeset
|
1 #! /usr/bin/env python3 |
| 5243 | 2 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
3 """RFC 3548: Base16, Base32, Base64 Data Encodings""" |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
4 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
5 # Modified 04-Oct-1995 by Jack Jansen to use binascii module |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
6 # Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
7 # Modified 22-May-2007 by Guido van Rossum to use bytes everywhere |
| 2742 | 8 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
9 import re |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
10 import struct |
|
3145
43f8db23ef15
Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents:
3104
diff
changeset
|
11 import binascii |
| 2742 | 12 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
13 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
14 __all__ = [ |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
15 # Legacy interface exports traditional RFC 1521 Base64 encodings |
|
54564
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
16 'encode', 'decode', 'encodebytes', 'decodebytes', |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
17 # Generalized interface for other encodings |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
18 'b64encode', 'b64decode', 'b32encode', 'b32decode', |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
19 'b16encode', 'b16decode', |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
20 # Standard Base64 encoding |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
21 'standard_b64encode', 'standard_b64decode', |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
22 # Some common Base64 alternatives. As referenced by RFC 3458, see thread |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
23 # starting at: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
24 # |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
25 # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
26 'urlsafe_b64encode', 'urlsafe_b64decode', |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
27 ] |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
28 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
29 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
30 bytes_types = (bytes, bytearray) # Types acceptable as binary data |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
42966
diff
changeset
|
31 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
42966
diff
changeset
|
32 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
33 def _translate(s, altchars): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
34 if not isinstance(s, bytes_types): |
|
42915
3f0f1c775d9d
Fix some tests I broke. (More to follow.)
Guido van Rossum <guido@python.org>
parents:
42911
diff
changeset
|
35 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
36 translation = bytearray(range(256)) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
37 for k, v in altchars.items(): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
38 translation[ord(k)] = v[0] |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
39 return s.translate(translation) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
40 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
41 |
|
48769
83b2d07b475b
#2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents:
46638
diff
changeset
|
42 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
43 # Base64 encoding/decoding uses binascii |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
44 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
45 def b64encode(s, altchars=None): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
46 """Encode a byte string using Base64. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
47 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
48 s is the byte string to encode. Optional altchars must be a byte |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
49 string of length 2 which specifies an alternative alphabet for the |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
50 '+' and '/' characters. This allows an application to |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
51 e.g. generate url or filesystem safe Base64 strings. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
52 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
53 The encoded byte string is returned. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
54 """ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
55 if not isinstance(s, bytes_types): |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
56 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
57 # Strip off the trailing newline |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
58 encoded = binascii.b2a_base64(s)[:-1] |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
59 if altchars is not None: |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
60 if not isinstance(altchars, bytes_types): |
|
55589
546aa134d773
Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54568
diff
changeset
|
61 raise TypeError("expected bytes, not %s" |
|
546aa134d773
Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54568
diff
changeset
|
62 % altchars.__class__.__name__) |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
63 assert len(altchars) == 2, repr(altchars) |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
64 return _translate(encoded, {'+': altchars[0:1], '/': altchars[1:2]}) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
65 return encoded |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
66 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
67 |
|
66152
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
68 def b64decode(s, altchars=None, validate=False): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
69 """Decode a Base64 encoded byte string. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
70 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
71 s is the byte string to decode. Optional altchars must be a |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
72 string of length 2 which specifies the alternative alphabet used |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
73 instead of the '+' and '/' characters. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
74 |
|
66152
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
75 The decoded string is returned. A binascii.Error is raised if s is |
|
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
76 incorrectly padded. |
|
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
77 |
|
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
78 If validate is False (the default), non-base64-alphabet characters are |
|
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
79 discarded prior to the padding check. If validate is True, |
|
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
80 non-base64-alphabet characters in the input result in a binascii.Error. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
81 """ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
82 if not isinstance(s, bytes_types): |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
83 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
84 if altchars is not None: |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
85 if not isinstance(altchars, bytes_types): |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
86 raise TypeError("expected bytes, not %s" |
|
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
87 % altchars.__class__.__name__) |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
88 assert len(altchars) == 2, repr(altchars) |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
89 s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'}) |
|
66152
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
90 if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s): |
|
857d9fe60169
#1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents:
65331
diff
changeset
|
91 raise binascii.Error('Non-base64 digit found') |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
92 return binascii.a2b_base64(s) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
93 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
94 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
95 def standard_b64encode(s): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
96 """Encode a byte string using the standard Base64 alphabet. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
97 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
98 s is the byte string to encode. The encoded byte string is returned. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
99 """ |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
100 return b64encode(s) |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
101 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
102 def standard_b64decode(s): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
103 """Decode a byte string encoded with the standard Base64 alphabet. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
104 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
105 s is the byte string to decode. The decoded byte string is |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
106 returned. binascii.Error is raised if the input is incorrectly |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
107 padded or if there are non-alphabet characters present in the |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
108 input. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
109 """ |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
110 return b64decode(s) |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
111 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
112 def urlsafe_b64encode(s): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
113 """Encode a byte string using a url-safe Base64 alphabet. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
114 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
115 s is the byte string to encode. The encoded byte string is |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
116 returned. The alphabet uses '-' instead of '+' and '_' instead of |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
117 '/'. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
118 """ |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
119 return b64encode(s, b'-_') |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
120 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
121 def urlsafe_b64decode(s): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
122 """Decode a byte string encoded with the standard Base64 alphabet. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
123 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
124 s is the byte string to decode. The decoded byte string is |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
125 returned. binascii.Error is raised if the input is incorrectly |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
126 padded or if there are non-alphabet characters present in the |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
127 input. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
128 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
129 The alphabet uses '-' instead of '+' and '_' instead of '/'. |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
130 """ |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
131 return b64decode(s, b'-_') |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
132 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
133 |
|
48769
83b2d07b475b
#2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents:
46638
diff
changeset
|
134 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
135 # Base32 encoding/decoding must be done in Python |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
136 _b32alphabet = { |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
137 0: b'A', 9: b'J', 18: b'S', 27: b'3', |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
138 1: b'B', 10: b'K', 19: b'T', 28: b'4', |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
139 2: b'C', 11: b'L', 20: b'U', 29: b'5', |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
140 3: b'D', 12: b'M', 21: b'V', 30: b'6', |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
141 4: b'E', 13: b'N', 22: b'W', 31: b'7', |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
142 5: b'F', 14: b'O', 23: b'X', |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
143 6: b'G', 15: b'P', 24: b'Y', |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
144 7: b'H', 16: b'Q', 25: b'Z', |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
145 8: b'I', 17: b'R', 26: b'2', |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
146 } |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
147 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
148 _b32tab = [v[0] for k, v in sorted(_b32alphabet.items())] |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
149 _b32rev = dict([(v[0], k) for k, v in _b32alphabet.items()]) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
150 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
151 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
152 def b32encode(s): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
153 """Encode a byte string using Base32. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
154 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
155 s is the byte string to encode. The encoded byte string is returned. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
156 """ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
157 if not isinstance(s, bytes_types): |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
158 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
159 quanta, leftover = divmod(len(s), 5) |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
160 # Pad the last quantum with zero bits if necessary |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
161 if leftover: |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
162 s = s + bytes(5 - leftover) # Don't use += ! |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
163 quanta += 1 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
164 encoded = bytes() |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
165 for i in range(quanta): |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
166 # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
167 # code is to process the 40 bits in units of 5 bits. So we take the 1 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
168 # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
169 # bits of c2 and tack them onto c3. The shifts and masks are intended |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
170 # to give us values of exactly 5 bits in width. |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
171 c1, c2, c3 = struct.unpack('!HHB', s[i*5:(i+1)*5]) |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
172 c2 += (c1 & 1) << 16 # 17 bits wide |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
173 c3 += (c2 & 3) << 8 # 10 bits wide |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
174 encoded += bytes([_b32tab[c1 >> 11], # bits 1 - 5 |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
175 _b32tab[(c1 >> 6) & 0x1f], # bits 6 - 10 |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
176 _b32tab[(c1 >> 1) & 0x1f], # bits 11 - 15 |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
177 _b32tab[c2 >> 12], # bits 16 - 20 (1 - 5) |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
178 _b32tab[(c2 >> 7) & 0x1f], # bits 21 - 25 (6 - 10) |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
179 _b32tab[(c2 >> 2) & 0x1f], # bits 26 - 30 (11 - 15) |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
180 _b32tab[c3 >> 5], # bits 31 - 35 (1 - 5) |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
181 _b32tab[c3 & 0x1f], # bits 36 - 40 (1 - 5) |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
182 ]) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
183 # Adjust for any leftover partial quanta |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
184 if leftover == 1: |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
185 return encoded[:-6] + b'======' |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
186 elif leftover == 2: |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
187 return encoded[:-4] + b'====' |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
188 elif leftover == 3: |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
189 return encoded[:-3] + b'===' |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
190 elif leftover == 4: |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
191 return encoded[:-1] + b'=' |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
192 return encoded |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
193 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
194 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
195 def b32decode(s, casefold=False, map01=None): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
196 """Decode a Base32 encoded byte string. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
197 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
198 s is the byte string to decode. Optional casefold is a flag |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
199 specifying whether a lowercase alphabet is acceptable as input. |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
200 For security purposes, the default is False. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
201 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
202 RFC 3548 allows for optional mapping of the digit 0 (zero) to the |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
203 letter O (oh), and for optional mapping of the digit 1 (one) to |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
204 either the letter I (eye) or letter L (el). The optional argument |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
205 map01 when not None, specifies which letter the digit 1 should be |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
206 mapped to (when map01 is not None, the digit 0 is always mapped to |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
207 the letter O). For security purposes the default is None, so that |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
208 0 and 1 are not allowed in the input. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
209 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
210 The decoded byte string is returned. binascii.Error is raised if |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
211 the input is incorrectly padded or if there are non-alphabet |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
212 characters present in the input. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
213 """ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
214 if not isinstance(s, bytes_types): |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
215 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
216 quanta, leftover = divmod(len(s), 8) |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
217 if leftover: |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
218 raise binascii.Error('Incorrect padding') |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
219 # Handle section 2.4 zero and one mapping. The flag map01 will be either |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
220 # False, or the character to map the digit 1 (one) to. It should be |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
221 # either L (el) or I (eye). |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
222 if map01 is not None: |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
223 if not isinstance(map01, bytes_types): |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
224 raise TypeError("expected bytes, not %s" % map01.__class__.__name__) |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
225 assert len(map01) == 1, repr(map01) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
42966
diff
changeset
|
226 s = _translate(s, {b'0': b'O', b'1': map01}) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
227 if casefold: |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
42966
diff
changeset
|
228 s = s.upper() |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
229 # Strip off pad characters from the right. We need to count the pad |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
230 # characters because this will tell us how many null bytes to remove from |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
231 # the end of the decoded string. |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
232 padchars = 0 |
|
48769
83b2d07b475b
#2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents:
46638
diff
changeset
|
233 mo = re.search(b'(?P<pad>[=]*)$', s) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
234 if mo: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
235 padchars = len(mo.group('pad')) |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
236 if padchars > 0: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
237 s = s[:-padchars] |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
238 # Now decode the full quanta |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
239 parts = [] |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
240 acc = 0 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
241 shift = 35 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
242 for c in s: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
243 val = _b32rev.get(c) |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
244 if val is None: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
245 raise TypeError('Non-base32 digit found') |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
246 acc += _b32rev[c] << shift |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
247 shift -= 5 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
248 if shift < 0: |
|
63258
5edce14b6287
Fix failure introduced in r83182.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61916
diff
changeset
|
249 parts.append(binascii.unhexlify(bytes('%010x' % acc, "ascii"))) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
250 acc = 0 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
251 shift = 35 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
252 # Process the last, partial quanta |
|
42911
cb28e14ee808
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
41827
diff
changeset
|
253 last = binascii.unhexlify(bytes('%010x' % acc, "ascii")) |
|
35047
662488d4187d
[Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents:
31515
diff
changeset
|
254 if padchars == 0: |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
255 last = b'' # No characters |
|
35047
662488d4187d
[Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents:
31515
diff
changeset
|
256 elif padchars == 1: |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
257 last = last[:-1] |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
258 elif padchars == 3: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
259 last = last[:-2] |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
260 elif padchars == 4: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
261 last = last[:-3] |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
262 elif padchars == 6: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
263 last = last[:-4] |
|
35047
662488d4187d
[Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents:
31515
diff
changeset
|
264 else: |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
265 raise binascii.Error('Incorrect padding') |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
266 parts.append(last) |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
267 return b''.join(parts) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
268 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
269 |
|
48769
83b2d07b475b
#2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents:
46638
diff
changeset
|
270 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
271 # RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
272 # lowercase. The RFC also recommends against accepting input case |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
273 # insensitively. |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
274 def b16encode(s): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
275 """Encode a byte string using Base16. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
276 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
277 s is the byte string to encode. The encoded byte string is returned. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
278 """ |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
279 if not isinstance(s, bytes_types): |
|
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
280 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
42966
diff
changeset
|
281 return binascii.hexlify(s).upper() |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
282 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
283 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
284 def b16decode(s, casefold=False): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
285 """Decode a Base16 encoded byte string. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
286 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
287 s is the byte string to decode. Optional casefold is a flag |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
288 specifying whether a lowercase alphabet is acceptable as input. |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
289 For security purposes, the default is False. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
290 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
291 The decoded byte string is returned. binascii.Error is raised if |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
292 s were incorrectly padded or if there are non-alphabet characters |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
293 present in the string. |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
294 """ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
295 if not isinstance(s, bytes_types): |
|
46638
0fdef14981a8
Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43947
diff
changeset
|
296 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
297 if casefold: |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
42966
diff
changeset
|
298 s = s.upper() |
|
48769
83b2d07b475b
#2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents:
46638
diff
changeset
|
299 if re.search(b'[^0-9A-F]', s): |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
300 raise binascii.Error('Non-base16 digit found') |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
301 return binascii.unhexlify(s) |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
302 |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
303 |
|
48769
83b2d07b475b
#2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents:
46638
diff
changeset
|
304 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
305 # Legacy interface. This code could be cleaned up since I don't believe |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
306 # binascii has any line length limitations. It just doesn't seem worth it |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
307 # though. The files should be opened in binary mode. |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
13710
diff
changeset
|
308 |
|
3145
43f8db23ef15
Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents:
3104
diff
changeset
|
309 MAXLINESIZE = 76 # Excluding the CRLF |
|
19694
3716b116fb31
The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents:
18053
diff
changeset
|
310 MAXBINSIZE = (MAXLINESIZE//4)*3 |
| 2742 | 311 |
| 312 def encode(input, output): | |
|
42916
9fb8331cec2c
Force test_xmlrpc to pass. I'm not happy with how I did this, but I don't
Guido van Rossum <guido@python.org>
parents:
42915
diff
changeset
|
313 """Encode a file; input and output are binary files.""" |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
314 while True: |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
315 s = input.read(MAXBINSIZE) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
316 if not s: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
317 break |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
318 while len(s) < MAXBINSIZE: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
319 ns = input.read(MAXBINSIZE-len(s)) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
320 if not ns: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
321 break |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
322 s += ns |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
323 line = binascii.b2a_base64(s) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
324 output.write(line) |
| 2742 | 325 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
326 |
| 2742 | 327 def decode(input, output): |
|
42916
9fb8331cec2c
Force test_xmlrpc to pass. I'm not happy with how I did this, but I don't
Guido van Rossum <guido@python.org>
parents:
42915
diff
changeset
|
328 """Decode a file; input and output are binary files.""" |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
329 while True: |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
330 line = input.readline() |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
331 if not line: |
|
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
332 break |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
333 s = binascii.a2b_base64(line) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
334 output.write(s) |
| 2742 | 335 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
336 |
|
54564
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
337 def encodebytes(s): |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
338 """Encode a bytestring into a bytestring containing multiple lines |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
339 of base-64 data.""" |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
340 if not isinstance(s, bytes_types): |
|
42915
3f0f1c775d9d
Fix some tests I broke. (More to follow.)
Guido van Rossum <guido@python.org>
parents:
42911
diff
changeset
|
341 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
18053
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
342 pieces = [] |
|
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
343 for i in range(0, len(s), MAXBINSIZE): |
|
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
344 chunk = s[i : i + MAXBINSIZE] |
|
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
345 pieces.append(binascii.b2a_base64(chunk)) |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
346 return b"".join(pieces) |
| 2742 | 347 |
|
54564
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
348 def encodestring(s): |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
349 """Legacy alias of encodebytes().""" |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
350 import warnings |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
351 warnings.warn("encodestring() is a deprecated alias, use encodebytes()", |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
352 DeprecationWarning, 2) |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
353 return encodebytes(s) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
354 |
|
42916
9fb8331cec2c
Force test_xmlrpc to pass. I'm not happy with how I did this, but I don't
Guido van Rossum <guido@python.org>
parents:
42915
diff
changeset
|
355 |
|
54564
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
356 def decodebytes(s): |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
357 """Decode a bytestring of base-64 data into a bytestring.""" |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
358 if not isinstance(s, bytes_types): |
|
42915
3f0f1c775d9d
Fix some tests I broke. (More to follow.)
Guido van Rossum <guido@python.org>
parents:
42911
diff
changeset
|
359 raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
|
18053
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
360 return binascii.a2b_base64(s) |
| 2742 | 361 |
|
54564
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
362 def decodestring(s): |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
363 """Legacy alias of decodebytes().""" |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
364 import warnings |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
365 warnings.warn("decodestring() is a deprecated alias, use decodebytes()", |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
366 DeprecationWarning, 2) |
|
18d823e8b602
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents:
48769
diff
changeset
|
367 return decodebytes(s) |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
368 |
|
48769
83b2d07b475b
#2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents:
46638
diff
changeset
|
369 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
370 # Usable as a script... |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
371 def main(): |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
372 """Small main program""" |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
373 import sys, getopt |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
374 try: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
375 opts, args = getopt.getopt(sys.argv[1:], 'deut') |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
376 except getopt.error as msg: |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
377 sys.stdout = sys.stderr |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
378 print(msg) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
379 print("""usage: %s [-d|-e|-u|-t] [file|-] |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
380 -d, -u: decode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
381 -e: encode (default) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
382 -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]) |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
383 sys.exit(2) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
384 func = encode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
385 for o, a in opts: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
386 if o == '-e': func = encode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
387 if o == '-d': func = decode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
388 if o == '-u': func = decode |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
389 if o == '-t': test(); return |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
390 if args and args[0] != '-': |
|
65332
6296632ad2cb
Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61917
diff
changeset
|
391 with open(args[0], 'rb') as f: |
|
6296632ad2cb
Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61917
diff
changeset
|
392 func(f, sys.stdout.buffer) |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
393 else: |
|
61917
93c5e6241309
Merged revisions 81533 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
55589
diff
changeset
|
394 func(sys.stdin.buffer, sys.stdout.buffer) |
| 2742 | 395 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
396 |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
397 def test(): |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
398 s0 = b"Aladdin:open sesame" |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
399 print(repr(s0)) |
|
54568
031b963f2bc2
More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents:
54564
diff
changeset
|
400 s1 = encodebytes(s0) |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
401 print(repr(s1)) |
|
54568
031b963f2bc2
More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents:
54564
diff
changeset
|
402 s2 = decodebytes(s1) |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
403 print(repr(s2)) |
|
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
404 assert s0 == s2 |
| 2742 | 405 |
|
31326
3cc0d8fd4e2b
Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents:
19694
diff
changeset
|
406 |
| 2742 | 407 if __name__ == '__main__': |
|
41827
eb45f85c4c79
Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
408 main() |
