annotate Lib/base64.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 42366e293b7b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
4f251b8e328f Add #! line.
Guido van Rossum <guido@python.org>
parents: 3145
diff changeset
2
87228
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
3 """Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 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
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
87228
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
12 import itertools
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
14
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
15 __all__ = [
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
16 # 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
17 '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
18 # 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
19 '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
20 'b16encode', 'b16decode',
87228
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
21 # Base85 and Ascii85 encodings
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
22 'b85encode', 'b85decode', 'a85encode', 'a85decode',
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
23 # Standard Base64 encoding
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
24 '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
25 # 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
26 # starting at:
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 # 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
29 '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
30 ]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
31
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
32
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
33 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
34
75058
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
35 def _bytes_from_decode_data(s):
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
36 if isinstance(s, str):
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
37 try:
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
38 return s.encode('ascii')
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
39 except UnicodeEncodeError:
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
40 raise ValueError('string argument should contain only ASCII characters')
85930
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
41 if isinstance(s, bytes_types):
75058
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
42 return s
85930
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
43 try:
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
44 return memoryview(s).tobytes()
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
45 except TypeError:
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
46 raise TypeError("argument should be a bytes-like object or ASCII "
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
47 "string, not %r" % s.__class__.__name__) from None
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
48
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
49
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
50 # 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
51
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
52 def b64encode(s, altchars=None):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
53 """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
54
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
55 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
56 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
57 '+' and '/' characters. This allows an application to
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
58 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
59
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
60 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
61 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
62 # 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
63 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
64 if altchars is not None:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
65 assert len(altchars) == 2, repr(altchars)
77592
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
66 return encoded.translate(bytes.maketrans(b'+/', altchars))
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
67 return encoded
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
68
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
69
66152
857d9fe60169 #1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents: 65331
diff changeset
70 def b64decode(s, altchars=None, validate=False):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
71 """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
72
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
73 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
74 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
75 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
76
66152
857d9fe60169 #1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents: 65331
diff changeset
77 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
78 incorrectly padded.
857d9fe60169 #1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents: 65331
diff changeset
79
857d9fe60169 #1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents: 65331
diff changeset
80 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
81 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
82 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
83 """
75058
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
84 s = _bytes_from_decode_data(s)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
85 if altchars is not None:
75058
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
86 altchars = _bytes_from_decode_data(altchars)
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
87 assert len(altchars) == 2, repr(altchars)
77592
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
88 s = s.translate(bytes.maketrans(altchars, b'+/'))
66152
857d9fe60169 #1466065: add validate option to base64.b64decode
R. David Murray <rdmurray@bitdance.com>
parents: 65331
diff changeset
89 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
90 raise binascii.Error('Non-base64 digit found')
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
91 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
92
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 def standard_b64encode(s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
95 """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
96
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
97 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
98 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
99 return b64encode(s)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
100
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
101 def standard_b64decode(s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
102 """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
103
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
104 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
105 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
106 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
107 input.
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
108 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
109 return b64decode(s)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
110
77592
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
111
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
112 _urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
113 _urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/')
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
114
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
115 def urlsafe_b64encode(s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
116 """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
117
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
118 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
119 returned. The alphabet uses '-' instead of '+' and '_' instead of
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
120 '/'.
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
121 """
77592
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
122 return b64encode(s).translate(_urlsafe_encode_translation)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
123
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
124 def urlsafe_b64decode(s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
125 """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
126
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
127 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
128 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
129 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
130 input.
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
131
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
132 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
133 """
77592
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
134 s = _bytes_from_decode_data(s)
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
135 s = s.translate(_urlsafe_decode_translation)
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
136 return b64decode(s)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
137
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
138
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
139
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
140 # Base32 encoding/decoding must be done in Python
83837
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
141 _b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
142 _b32tab = [bytes([i]) for i in _b32alphabet]
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
143 _b32tab2 = [a + b for a in _b32tab for b in _b32tab]
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
144 _b32rev = {v: k for k, v in enumerate(_b32alphabet)}
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
145
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
146 def b32encode(s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
147 """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
148
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
149 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
150 """
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
151 if not isinstance(s, bytes_types):
85930
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
152 s = memoryview(s).tobytes()
83837
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
153 leftover = len(s) % 5
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
154 # 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
155 if leftover:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
156 s = s + bytes(5 - leftover) # Don't use += !
83836
4b5467d997f1 Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 77592
diff changeset
157 encoded = bytearray()
83837
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
158 from_bytes = int.from_bytes
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
159 b32tab2 = _b32tab2
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
160 for i in range(0, len(s), 5):
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
161 c = from_bytes(s[i: i + 5], 'big')
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
162 encoded += (b32tab2[c >> 30] + # bits 1 - 10
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
163 b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
164 b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
165 b32tab2[c & 0x3ff] # bits 31 - 40
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
166 )
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
167 # 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
168 if leftover == 1:
83836
4b5467d997f1 Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 77592
diff changeset
169 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
170 elif leftover == 2:
83836
4b5467d997f1 Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 77592
diff changeset
171 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
172 elif leftover == 3:
83836
4b5467d997f1 Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 77592
diff changeset
173 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
174 elif leftover == 4:
83836
4b5467d997f1 Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 77592
diff changeset
175 encoded[-1:] = b'='
4b5467d997f1 Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 77592
diff changeset
176 return bytes(encoded)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
177
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
178 def b32decode(s, casefold=False, map01=None):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
179 """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
180
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
181 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
182 specifying whether a lowercase alphabet is acceptable as input.
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
183 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
184
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
185 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
186 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
187 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
188 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
189 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
190 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
191 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
192
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
193 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
194 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
195 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
196 """
75058
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
197 s = _bytes_from_decode_data(s)
83837
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
198 if len(s) % 8:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
199 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
200 # 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
201 # 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
202 # 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
203 if map01 is not None:
75058
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
204 map01 = _bytes_from_decode_data(map01)
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
205 assert len(map01) == 1, repr(map01)
77592
2cbbc015d8a2 Speed up base64.urlsafe_{en,de}code().
Guido van Rossum <guido@python.org>
parents: 75058
diff changeset
206 s = s.translate(bytes.maketrans(b'01', b'O' + map01))
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
207 if casefold:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 42966
diff changeset
208 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
209 # 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
210 # 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
211 # the end of the decoded string.
83837
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
212 l = len(s)
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
213 s = s.rstrip(b'=')
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
214 padchars = l - len(s)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
215 # Now decode the full quanta
83837
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
216 decoded = bytearray()
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
217 b32rev = _b32rev
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
218 for i in range(0, len(s), 8):
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
219 quanta = s[i: i + 8]
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
220 acc = 0
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
221 try:
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
222 for c in quanta:
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
223 acc = (acc << 5) + b32rev[c]
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
224 except KeyError:
83952
29a823f31465 Issue #18011: Silence an unrelated noise introduced in changeset 1b5ef05d6ced.
Serhiy Storchaka <storchaka@gmail.com>
parents: 83950
diff changeset
225 raise binascii.Error('Non-base32 digit found') from None
83837
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
226 decoded += acc.to_bytes(5, 'big')
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
227 # Process the last, partial quanta
83837
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
228 if padchars:
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
229 acc <<= 5 * padchars
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
230 last = acc.to_bytes(5, 'big')
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
231 if padchars == 1:
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
232 decoded[-5:] = last[:-1]
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
233 elif padchars == 3:
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
234 decoded[-5:] = last[:-2]
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
235 elif padchars == 4:
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
236 decoded[-5:] = last[:-3]
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
237 elif padchars == 6:
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
238 decoded[-5:] = last[:-4]
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
239 else:
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
240 raise binascii.Error('Incorrect padding')
1b5ef05d6ced Issue #17812: Fixed quadratic complexity of base64.b32encode().
Serhiy Storchaka <storchaka@gmail.com>
parents: 83836
diff changeset
241 return bytes(decoded)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
242
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
243
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
244
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
245 # 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
246 # 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
247 # insensitively.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
248 def b16encode(s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
249 """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
250
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
251 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
252 """
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 42966
diff changeset
253 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
254
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
255
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
256 def b16decode(s, casefold=False):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
257 """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
258
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
259 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
260 specifying whether a lowercase alphabet is acceptable as input.
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
261 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
262
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
263 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
264 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
265 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
266 """
75058
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
267 s = _bytes_from_decode_data(s)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
268 if casefold:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 42966
diff changeset
269 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
270 if re.search(b'[^0-9A-F]', s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
271 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
272 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
273
87228
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
274 #
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
275 # Ascii85 encoding/decoding
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
276 #
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
277
87228
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
278 def _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
279 # Helper function for a85encode and b85encode
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
280 if not isinstance(b, bytes_types):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
281 b = memoryview(b).tobytes()
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
282
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
283 padding = (-len(b)) % 4
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
284 if padding:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
285 b = b + b'\0' * padding
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
286 words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
287
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
288 a85chars2 = _a85chars2
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
289 a85chars = _a85chars
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
290 chunks = [b'z' if foldnuls and not word else
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
291 b'y' if foldspaces and word == 0x20202020 else
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
292 (chars2[word // 614125] +
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
293 chars2[word // 85 % 7225] +
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
294 chars[word % 85])
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
295 for word in words]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
296
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
297 if padding and not pad:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
298 if chunks[-1] == b'z':
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
299 chunks[-1] = chars[0] * 5
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
300 chunks[-1] = chunks[-1][:-padding]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
301
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
302 return b''.join(chunks)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
303
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
304 _A85START = b"<~"
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
305 _A85END = b"~>"
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
306 _a85chars = [bytes([i]) for i in range(33, 118)]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
307 _a85chars2 = [(a + b) for a in _a85chars for b in _a85chars]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
308
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
309 def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
310 """Encode a byte string using Ascii85.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
311
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
312 b is the byte string to encode. The encoded byte string is returned.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
313
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
314 foldspaces is an optional flag that uses the special short sequence 'y'
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
315 instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
316 feature is not supported by the "standard" Adobe encoding.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
317
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
318 wrapcol controls whether the output should have newline ('\n') characters
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
319 added to it. If this is non-zero, each output line will be at most this
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
320 many characters long.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
321
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
322 pad controls whether the input string is padded to a multiple of 4 before
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
323 encoding. Note that the btoa implementation always pads.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
324
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
325 adobe controls whether the encoded byte sequence is framed with <~ and ~>,
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
326 which is used by the Adobe implementation.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
327 """
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
328 result = _85encode(b, _a85chars, _a85chars2, pad, True, foldspaces)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
329
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
330 if adobe:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
331 result = _A85START + result
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
332 if wrapcol:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
333 wrapcol = max(2 if adobe else 1, wrapcol)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
334 chunks = [result[i: i + wrapcol]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
335 for i in range(0, len(result), wrapcol)]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
336 if adobe:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
337 if len(chunks[-1]) + 2 > wrapcol:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
338 chunks.append(b'')
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
339 result = b'\n'.join(chunks)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
340 if adobe:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
341 result += _A85END
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
342
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
343 return result
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
344
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
345 def a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v'):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
346 """Decode an Ascii85 encoded byte string.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
347
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
348 s is the byte string to decode.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
349
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
350 foldspaces is a flag that specifies whether the 'y' short sequence should be
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
351 accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
352 not supported by the "standard" Adobe encoding.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
353
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
354 adobe controls whether the input sequence is in Adobe Ascii85 format (i.e.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
355 is framed with <~ and ~>).
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
356
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
357 ignorechars should be a byte string containing characters to ignore from the
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
358 input. This should only contain whitespace characters, and by default
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
359 contains all whitespace characters in ASCII.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
360 """
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
361 b = _bytes_from_decode_data(b)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
362 if adobe:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
363 if not (b.startswith(_A85START) and b.endswith(_A85END)):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
364 raise ValueError("Ascii85 encoded byte sequences must be bracketed "
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
365 "by {} and {}".format(_A85START, _A85END))
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
366 b = b[2:-2] # Strip off start/end markers
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
367 #
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
368 # We have to go through this stepwise, so as to ignore spaces and handle
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
369 # special short sequences
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
370 #
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
371 packI = struct.Struct('!I').pack
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
372 decoded = []
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
373 decoded_append = decoded.append
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
374 curr = []
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
375 curr_append = curr.append
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
376 curr_clear = curr.clear
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
377 for x in b + b'u' * 4:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
378 if b'!'[0] <= x <= b'u'[0]:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
379 curr_append(x)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
380 if len(curr) == 5:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
381 acc = 0
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
382 for x in curr:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
383 acc = 85 * acc + (x - 33)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
384 try:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
385 decoded_append(packI(acc))
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
386 except struct.error:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
387 raise ValueError('Ascii85 overflow') from None
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
388 curr_clear()
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
389 elif x == b'z'[0]:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
390 if curr:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
391 raise ValueError('z inside Ascii85 5-tuple')
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
392 decoded_append(b'\0\0\0\0')
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
393 elif foldspaces and x == b'y'[0]:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
394 if curr:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
395 raise ValueError('y inside Ascii85 5-tuple')
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
396 decoded_append(b'\x20\x20\x20\x20')
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
397 elif x in ignorechars:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
398 # Skip whitespace
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
399 continue
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
400 else:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
401 raise ValueError('Non-Ascii85 digit found: %c' % x)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
402
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
403 result = b''.join(decoded)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
404 padding = 4 - len(curr)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
405 if padding:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
406 # Throw away the extra padding
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
407 result = result[:-padding]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
408 return result
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
409
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
410 # The following code is originally taken (with permission) from Mercurial
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
411
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
412 _b85chars = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
413 b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
414 _b85chars = [bytes([i]) for i in _b85chars]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
415 _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
416 _b85dec = None
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
417
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
418 def b85encode(b, pad=False):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
419 """Encode an ASCII-encoded byte array in base85 format.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
420
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
421 If pad is true, the input is padded with "\0" so its length is a multiple of
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
422 4 characters before encoding.
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
423 """
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
424 return _85encode(b, _b85chars, _b85chars2, pad)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
425
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
426 def b85decode(b):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
427 """Decode base85-encoded byte array"""
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
428 b = _bytes_from_decode_data(b)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
429 global _b85dec
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
430 if _b85dec is None:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
431 _b85dec = [None] * 256
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
432 for i, c in enumerate(_b85chars):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
433 _b85dec[c[0]] = i
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
434
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
435 padding = (-len(b)) % 5
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
436 b = b + b'~' * padding
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
437 out = []
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
438 packI = struct.Struct('!I').pack
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
439 for i in range(0, len(b), 5):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
440 chunk = b[i:i + 5]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
441 acc = 0
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
442 try:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
443 for c in chunk:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
444 acc = acc * 85 + _b85dec[c]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
445 except TypeError:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
446 for j, c in enumerate(chunk):
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
447 if _b85dec[c] is None:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
448 raise ValueError('bad base85 character at position %d'
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
449 % (i + j)) from None
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
450 raise
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
451 try:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
452 out.append(packI(acc))
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
453 except struct.error:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
454 raise ValueError('base85 overflow in hunk starting at byte %d'
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
455 % i) from None
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
456
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
457 result = b''.join(out)
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
458 if padding:
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
459 result = result[:-padding]
42366e293b7b Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85930
diff changeset
460 return result
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
461
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
462 # 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
463 # 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
464 # 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
465
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
466 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
467 MAXBINSIZE = (MAXLINESIZE//4)*3
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
468
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 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
470 """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
471 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
472 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
473 if not s:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
474 break
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
475 while len(s) < MAXBINSIZE:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
476 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
477 if not ns:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
478 break
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
479 s += ns
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
480 line = binascii.b2a_base64(s)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
481 output.write(line)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
483
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 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
485 """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
486 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
487 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
488 if not line:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
489 break
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
490 s = binascii.a2b_base64(line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
491 output.write(s)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
85930
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
493 def _input_type_check(s):
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
494 try:
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
495 m = memoryview(s)
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
496 except TypeError as err:
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
497 msg = "expected bytes-like object, not %s" % s.__class__.__name__
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
498 raise TypeError(msg) from err
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
499 if m.format not in ('c', 'b', 'B'):
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
500 msg = ("expected single byte elements, not %r from %s" %
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
501 (m.format, s.__class__.__name__))
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
502 raise TypeError(msg)
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
503 if m.ndim != 1:
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
504 msg = ("expected 1-D data, not %d-D data from %s" %
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
505 (m.ndim, s.__class__.__name__))
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
506 raise TypeError(msg)
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
507
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
508
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
509 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
510 """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
511 of base-64 data."""
85930
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
512 _input_type_check(s)
18053
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
513 pieces = []
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
514 for i in range(0, len(s), MAXBINSIZE):
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
515 chunk = s[i : i + MAXBINSIZE]
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
516 pieces.append(binascii.b2a_base64(chunk))
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
517 return b"".join(pieces)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
518
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
519 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
520 """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
521 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
522 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
523 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
524 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
525
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
526
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
527 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
528 """Decode a bytestring of base-64 data into a bytestring."""
85930
d90f25e1a705 Close #17839: support bytes-like objects in base64 module
Nick Coghlan <ncoghlan@gmail.com>
parents: 83952
diff changeset
529 _input_type_check(s)
18053
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
530 return binascii.a2b_base64(s)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
531
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
532 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
533 """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
534 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
535 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
536 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
537 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
538
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
539
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
540 # Usable as a script...
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
541 def main():
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
542 """Small main program"""
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
543 import sys, getopt
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
544 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
545 opts, args = getopt.getopt(sys.argv[1:], 'deut')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40204
diff changeset
546 except getopt.error as msg:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
547 sys.stdout = sys.stderr
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
548 print(msg)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
549 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
550 -d, -u: decode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
551 -e: encode (default)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
552 -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
553 sys.exit(2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
554 func = encode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
555 for o, a in opts:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
556 if o == '-e': func = encode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
557 if o == '-d': func = decode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
558 if o == '-u': func = decode
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
559 if o == '-t': test(); return
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
560 if args and args[0] != '-':
65332
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61917
diff changeset
561 with open(args[0], 'rb') as f:
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61917
diff changeset
562 func(f, sys.stdout.buffer)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
563 else:
61917
93c5e6241309 Merged revisions 81533 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55589
diff changeset
564 func(sys.stdin.buffer, sys.stdout.buffer)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
565
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
566
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
567 def test():
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
568 s0 = b"Aladdin:open sesame"
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
569 print(repr(s0))
54568
031b963f2bc2 More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents: 54564
diff changeset
570 s1 = encodebytes(s0)
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
571 print(repr(s1))
54568
031b963f2bc2 More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents: 54564
diff changeset
572 s2 = decodebytes(s1)
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
573 print(repr(s2))
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
574 assert s0 == s2
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
575
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
576
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 if __name__ == '__main__':
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
578 main()