annotate Lib/base64.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 2cbbc015d8a2
children 4b5467d997f1
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
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
3 """RFC 3548: Base16, Base32, Base64 Data Encodings"""
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
4
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
5 # Modified 04-Oct-1995 by Jack Jansen to use binascii module
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
6 # Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
7 # Modified 22-May-2007 by Guido van Rossum to use bytes everywhere
2742
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
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
13
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
14 __all__ = [
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
15 # Legacy interface exports traditional RFC 1521 Base64 encodings
54564
18d823e8b602 #3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
Georg Brandl <georg@python.org>
parents: 48769
diff changeset
16 'encode', 'decode', 'encodebytes', 'decodebytes',
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
17 # Generalized interface for other encodings
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
18 'b64encode', 'b64decode', 'b32encode', 'b32decode',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
19 'b16encode', 'b16decode',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
20 # Standard Base64 encoding
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
21 'standard_b64encode', 'standard_b64decode',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
22 # Some common Base64 alternatives. As referenced by RFC 3458, see thread
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
23 # starting at:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
24 #
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
25 # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
26 'urlsafe_b64encode', 'urlsafe_b64decode',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
27 ]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
28
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
29
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
30 bytes_types = (bytes, bytearray) # Types acceptable as binary data
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 42966
diff changeset
31
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
32 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
33 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
34 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
35 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
36 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
37 raise ValueError('string argument should contain only ASCII characters')
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 elif isinstance(s, bytes_types):
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 return 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
40 else:
c760bd844222 Issue #13641: Decoding functions in the base64 module now accept ASCII-only unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
41 raise TypeError("argument should be bytes or ASCII string, not %s" % s.__class__.__name__)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 42966
diff changeset
42
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
43
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
44
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
45 # 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
46
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
47 def b64encode(s, altchars=None):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
48 """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
49
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
50 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
51 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
52 '+' and '/' characters. This allows an application to
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
53 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
54
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
55 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
56 """
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
57 if not isinstance(s, bytes_types):
46638
0fdef14981a8 Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43947
diff changeset
58 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
59 # 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
60 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
61 if altchars is not None:
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
62 if not isinstance(altchars, bytes_types):
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54568
diff changeset
63 raise TypeError("expected bytes, not %s"
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54568
diff changeset
64 % altchars.__class__.__name__)
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
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
141 _b32alphabet = {
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
142 0: b'A', 9: b'J', 18: b'S', 27: b'3',
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
143 1: b'B', 10: b'K', 19: b'T', 28: b'4',
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
144 2: b'C', 11: b'L', 20: b'U', 29: b'5',
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
145 3: b'D', 12: b'M', 21: b'V', 30: b'6',
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
146 4: b'E', 13: b'N', 22: b'W', 31: b'7',
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
147 5: b'F', 14: b'O', 23: b'X',
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
148 6: b'G', 15: b'P', 24: b'Y',
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
149 7: b'H', 16: b'Q', 25: b'Z',
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
150 8: b'I', 17: b'R', 26: b'2',
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
151 }
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
152
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
153 _b32tab = [v[0] for k, v in sorted(_b32alphabet.items())]
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
154 _b32rev = dict([(v[0], k) for k, v in _b32alphabet.items()])
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
155
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
156
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
157 def b32encode(s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
158 """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
159
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
160 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
161 """
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
162 if not isinstance(s, bytes_types):
46638
0fdef14981a8 Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43947
diff changeset
163 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
164 quanta, leftover = divmod(len(s), 5)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
165 # 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
166 if leftover:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
167 s = s + bytes(5 - leftover) # Don't use += !
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
168 quanta += 1
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
169 encoded = bytes()
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
170 for i in range(quanta):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
171 # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
172 # code is to process the 40 bits in units of 5 bits. So we take the 1
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
173 # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
174 # bits of c2 and tack them onto c3. The shifts and masks are intended
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
175 # to give us values of exactly 5 bits in width.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
176 c1, c2, c3 = struct.unpack('!HHB', s[i*5:(i+1)*5])
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
177 c2 += (c1 & 1) << 16 # 17 bits wide
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
178 c3 += (c2 & 3) << 8 # 10 bits wide
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
179 encoded += bytes([_b32tab[c1 >> 11], # bits 1 - 5
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
180 _b32tab[(c1 >> 6) & 0x1f], # bits 6 - 10
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
181 _b32tab[(c1 >> 1) & 0x1f], # bits 11 - 15
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
182 _b32tab[c2 >> 12], # bits 16 - 20 (1 - 5)
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
183 _b32tab[(c2 >> 7) & 0x1f], # bits 21 - 25 (6 - 10)
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
184 _b32tab[(c2 >> 2) & 0x1f], # bits 26 - 30 (11 - 15)
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
185 _b32tab[c3 >> 5], # bits 31 - 35 (1 - 5)
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
186 _b32tab[c3 & 0x1f], # bits 36 - 40 (1 - 5)
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
187 ])
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
188 # 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
189 if leftover == 1:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
190 return encoded[:-6] + b'======'
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
191 elif leftover == 2:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
192 return encoded[:-4] + b'===='
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
193 elif leftover == 3:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
194 return encoded[:-3] + b'==='
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
195 elif leftover == 4:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
196 return encoded[:-1] + b'='
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
197 return encoded
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
198
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
199
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
200 def b32decode(s, casefold=False, map01=None):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
201 """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
202
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
203 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
204 specifying whether a lowercase alphabet is acceptable as input.
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
205 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
206
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
207 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
208 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
209 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
210 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
211 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
212 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
213 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
214
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
215 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
216 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
217 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
218 """
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
219 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
220 quanta, leftover = divmod(len(s), 8)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
221 if leftover:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
222 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
223 # 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
224 # 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
225 # 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
226 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
227 map01 = _bytes_from_decode_data(map01)
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
228 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
229 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
230 if casefold:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 42966
diff changeset
231 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
232 # 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
233 # 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
234 # the end of the decoded string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
235 padchars = 0
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
236 mo = re.search(b'(?P<pad>[=]*)$', s)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
237 if mo:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
238 padchars = len(mo.group('pad'))
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
239 if padchars > 0:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
240 s = s[:-padchars]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
241 # Now decode the full quanta
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
242 parts = []
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
243 acc = 0
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
244 shift = 35
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
245 for c in s:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
246 val = _b32rev.get(c)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
247 if val is None:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
248 raise TypeError('Non-base32 digit found')
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
249 acc += _b32rev[c] << shift
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
250 shift -= 5
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
251 if shift < 0:
63258
5edce14b6287 Fix failure introduced in r83182.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61916
diff changeset
252 parts.append(binascii.unhexlify(bytes('%010x' % acc, "ascii")))
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
253 acc = 0
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
254 shift = 35
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
255 # Process the last, partial quanta
42911
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
256 last = binascii.unhexlify(bytes('%010x' % acc, "ascii"))
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
257 if padchars == 0:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
258 last = b'' # No characters
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
259 elif padchars == 1:
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
260 last = last[:-1]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
261 elif padchars == 3:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
262 last = last[:-2]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
263 elif padchars == 4:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
264 last = last[:-3]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
265 elif padchars == 6:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
266 last = last[:-4]
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
267 else:
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
268 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
269 parts.append(last)
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
270 return b''.join(parts)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
271
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
272
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
273
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
274 # 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
275 # 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
276 # insensitively.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
277 def b16encode(s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
278 """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
279
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
280 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
281 """
46638
0fdef14981a8 Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43947
diff changeset
282 if not isinstance(s, bytes_types):
0fdef14981a8 Removed implicit convertions of str object to bytes from base64.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 43947
diff changeset
283 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 42966
diff changeset
284 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
285
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
286
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
287 def b16decode(s, casefold=False):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
288 """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
289
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
290 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
291 specifying whether a lowercase alphabet is acceptable as input.
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
292 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
293
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
294 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
295 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
296 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
297 """
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
298 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
299 if casefold:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 42966
diff changeset
300 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
301 if re.search(b'[^0-9A-F]', s):
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
302 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
303 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
304
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
305
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
306
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
307 # 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
308 # 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
309 # 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
310
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
311 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
312 MAXBINSIZE = (MAXLINESIZE//4)*3
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
313
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 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
315 """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
316 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
317 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
318 if not s:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
319 break
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
320 while len(s) < MAXBINSIZE:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
321 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
322 if not ns:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
323 break
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
324 s += ns
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
325 line = binascii.b2a_base64(s)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
326 output.write(line)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
328
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 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
330 """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
331 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
332 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
333 if not line:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
334 break
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
335 s = binascii.a2b_base64(line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
336 output.write(s)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
338
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
339 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
340 """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
341 of base-64 data."""
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
342 if not isinstance(s, bytes_types):
42915
3f0f1c775d9d Fix some tests I broke. (More to follow.)
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
343 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
18053
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
344 pieces = []
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
345 for i in range(0, len(s), MAXBINSIZE):
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
346 chunk = s[i : i + MAXBINSIZE]
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
347 pieces.append(binascii.b2a_base64(chunk))
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
348 return b"".join(pieces)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
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
350 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
351 """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
352 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
353 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
354 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
355 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
356
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
357
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
358 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
359 """Decode a bytestring of base-64 data into a bytestring."""
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
360 if not isinstance(s, bytes_types):
42915
3f0f1c775d9d Fix some tests I broke. (More to follow.)
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
361 raise TypeError("expected bytes, not %s" % s.__class__.__name__)
18053
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
362 return binascii.a2b_base64(s)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
363
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
364 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
365 """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
366 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
367 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
368 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
369 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
370
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 46638
diff changeset
371
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
372 # Usable as a script...
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
373 def main():
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
374 """Small main program"""
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
375 import sys, getopt
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
376 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
377 opts, args = getopt.getopt(sys.argv[1:], 'deut')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
378 except getopt.error as msg:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
379 sys.stdout = sys.stderr
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
380 print(msg)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
381 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
382 -d, -u: decode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
383 -e: encode (default)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
384 -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
385 sys.exit(2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
386 func = encode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
387 for o, a in opts:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
388 if o == '-e': func = encode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
389 if o == '-d': func = decode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
390 if o == '-u': func = decode
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
391 if o == '-t': test(); return
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
392 if args and args[0] != '-':
65332
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61917
diff changeset
393 with open(args[0], 'rb') as f:
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61917
diff changeset
394 func(f, sys.stdout.buffer)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
395 else:
61917
93c5e6241309 Merged revisions 81533 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55589
diff changeset
396 func(sys.stdin.buffer, sys.stdout.buffer)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
397
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
398
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
399 def test():
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
400 s0 = b"Aladdin:open sesame"
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
401 print(repr(s0))
54568
031b963f2bc2 More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents: 54564
diff changeset
402 s1 = encodebytes(s0)
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
403 print(repr(s1))
54568
031b963f2bc2 More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents: 54564
diff changeset
404 s2 = decodebytes(s1)
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
405 print(repr(s2))
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
406 assert s0 == s2
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
408
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 if __name__ == '__main__':
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
410 main()