ยปCore Development>Code coverage>Lib/base64.py

Python code coverage for Lib/base64.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a"""Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodings"""
4n/a
5n/a# Modified 04-Oct-1995 by Jack Jansen to use binascii module
6n/a# Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
7n/a# Modified 22-May-2007 by Guido van Rossum to use bytes everywhere
8n/a
9n/aimport re
10n/aimport struct
11n/aimport binascii
12n/a
13n/a
14n/a__all__ = [
15n/a # Legacy interface exports traditional RFC 2045 Base64 encodings
16n/a 'encode', 'decode', 'encodebytes', 'decodebytes',
17n/a # Generalized interface for other encodings
18n/a 'b64encode', 'b64decode', 'b32encode', 'b32decode',
19n/a 'b16encode', 'b16decode',
20n/a # Base85 and Ascii85 encodings
21n/a 'b85encode', 'b85decode', 'a85encode', 'a85decode',
22n/a # Standard Base64 encoding
23n/a 'standard_b64encode', 'standard_b64decode',
24n/a # Some common Base64 alternatives. As referenced by RFC 3458, see thread
25n/a # starting at:
26n/a #
27n/a # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
28n/a 'urlsafe_b64encode', 'urlsafe_b64decode',
29n/a ]
30n/a
31n/a
32n/abytes_types = (bytes, bytearray) # Types acceptable as binary data
33n/a
34n/adef _bytes_from_decode_data(s):
35n/a if isinstance(s, str):
36n/a try:
37n/a return s.encode('ascii')
38n/a except UnicodeEncodeError:
39n/a raise ValueError('string argument should contain only ASCII characters')
40n/a if isinstance(s, bytes_types):
41n/a return s
42n/a try:
43n/a return memoryview(s).tobytes()
44n/a except TypeError:
45n/a raise TypeError("argument should be a bytes-like object or ASCII "
46n/a "string, not %r" % s.__class__.__name__) from None
47n/a
48n/a
49n/a# Base64 encoding/decoding uses binascii
50n/a
51n/adef b64encode(s, altchars=None):
52n/a """Encode the bytes-like object s using Base64 and return a bytes object.
53n/a
54n/a Optional altchars should be a byte string of length 2 which specifies an
55n/a alternative alphabet for the '+' and '/' characters. This allows an
56n/a application to e.g. generate url or filesystem safe Base64 strings.
57n/a """
58n/a encoded = binascii.b2a_base64(s, newline=False)
59n/a if altchars is not None:
60n/a assert len(altchars) == 2, repr(altchars)
61n/a return encoded.translate(bytes.maketrans(b'+/', altchars))
62n/a return encoded
63n/a
64n/a
65n/adef b64decode(s, altchars=None, validate=False):
66n/a """Decode the Base64 encoded bytes-like object or ASCII string s.
67n/a
68n/a Optional altchars must be a bytes-like object or ASCII string of length 2
69n/a which specifies the alternative alphabet used instead of the '+' and '/'
70n/a characters.
71n/a
72n/a The result is returned as a bytes object. A binascii.Error is raised if
73n/a s is incorrectly padded.
74n/a
75n/a If validate is False (the default), characters that are neither in the
76n/a normal base-64 alphabet nor the alternative alphabet are discarded prior
77n/a to the padding check. If validate is True, these non-alphabet characters
78n/a in the input result in a binascii.Error.
79n/a """
80n/a s = _bytes_from_decode_data(s)
81n/a if altchars is not None:
82n/a altchars = _bytes_from_decode_data(altchars)
83n/a assert len(altchars) == 2, repr(altchars)
84n/a s = s.translate(bytes.maketrans(altchars, b'+/'))
85n/a if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s):
86n/a raise binascii.Error('Non-base64 digit found')
87n/a return binascii.a2b_base64(s)
88n/a
89n/a
90n/adef standard_b64encode(s):
91n/a """Encode bytes-like object s using the standard Base64 alphabet.
92n/a
93n/a The result is returned as a bytes object.
94n/a """
95n/a return b64encode(s)
96n/a
97n/adef standard_b64decode(s):
98n/a """Decode bytes encoded with the standard Base64 alphabet.
99n/a
100n/a Argument s is a bytes-like object or ASCII string to decode. The result
101n/a is returned as a bytes object. A binascii.Error is raised if the input
102n/a is incorrectly padded. Characters that are not in the standard alphabet
103n/a are discarded prior to the padding check.
104n/a """
105n/a return b64decode(s)
106n/a
107n/a
108n/a_urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
109n/a_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/')
110n/a
111n/adef urlsafe_b64encode(s):
112n/a """Encode bytes using the URL- and filesystem-safe Base64 alphabet.
113n/a
114n/a Argument s is a bytes-like object to encode. The result is returned as a
115n/a bytes object. The alphabet uses '-' instead of '+' and '_' instead of
116n/a '/'.
117n/a """
118n/a return b64encode(s).translate(_urlsafe_encode_translation)
119n/a
120n/adef urlsafe_b64decode(s):
121n/a """Decode bytes using the URL- and filesystem-safe Base64 alphabet.
122n/a
123n/a Argument s is a bytes-like object or ASCII string to decode. The result
124n/a is returned as a bytes object. A binascii.Error is raised if the input
125n/a is incorrectly padded. Characters that are not in the URL-safe base-64
126n/a alphabet, and are not a plus '+' or slash '/', are discarded prior to the
127n/a padding check.
128n/a
129n/a The alphabet uses '-' instead of '+' and '_' instead of '/'.
130n/a """
131n/a s = _bytes_from_decode_data(s)
132n/a s = s.translate(_urlsafe_decode_translation)
133n/a return b64decode(s)
134n/a
135n/a
136n/a
137n/a# Base32 encoding/decoding must be done in Python
138n/a_b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
139n/a_b32tab2 = None
140n/a_b32rev = None
141n/a
142n/adef b32encode(s):
143n/a """Encode the bytes-like object s using Base32 and return a bytes object.
144n/a """
145n/a global _b32tab2
146n/a # Delay the initialization of the table to not waste memory
147n/a # if the function is never called
148n/a if _b32tab2 is None:
149n/a b32tab = [bytes((i,)) for i in _b32alphabet]
150n/a _b32tab2 = [a + b for a in b32tab for b in b32tab]
151n/a b32tab = None
152n/a
153n/a if not isinstance(s, bytes_types):
154n/a s = memoryview(s).tobytes()
155n/a leftover = len(s) % 5
156n/a # Pad the last quantum with zero bits if necessary
157n/a if leftover:
158n/a s = s + b'\0' * (5 - leftover) # Don't use += !
159n/a encoded = bytearray()
160n/a from_bytes = int.from_bytes
161n/a b32tab2 = _b32tab2
162n/a for i in range(0, len(s), 5):
163n/a c = from_bytes(s[i: i + 5], 'big')
164n/a encoded += (b32tab2[c >> 30] + # bits 1 - 10
165n/a b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
166n/a b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
167n/a b32tab2[c & 0x3ff] # bits 31 - 40
168n/a )
169n/a # Adjust for any leftover partial quanta
170n/a if leftover == 1:
171n/a encoded[-6:] = b'======'
172n/a elif leftover == 2:
173n/a encoded[-4:] = b'===='
174n/a elif leftover == 3:
175n/a encoded[-3:] = b'==='
176n/a elif leftover == 4:
177n/a encoded[-1:] = b'='
178n/a return bytes(encoded)
179n/a
180n/adef b32decode(s, casefold=False, map01=None):
181n/a """Decode the Base32 encoded bytes-like object or ASCII string s.
182n/a
183n/a Optional casefold is a flag specifying whether a lowercase alphabet is
184n/a acceptable as input. For security purposes, the default is False.
185n/a
186n/a RFC 3548 allows for optional mapping of the digit 0 (zero) to the
187n/a letter O (oh), and for optional mapping of the digit 1 (one) to
188n/a either the letter I (eye) or letter L (el). The optional argument
189n/a map01 when not None, specifies which letter the digit 1 should be
190n/a mapped to (when map01 is not None, the digit 0 is always mapped to
191n/a the letter O). For security purposes the default is None, so that
192n/a 0 and 1 are not allowed in the input.
193n/a
194n/a The result is returned as a bytes object. A binascii.Error is raised if
195n/a the input is incorrectly padded or if there are non-alphabet
196n/a characters present in the input.
197n/a """
198n/a global _b32rev
199n/a # Delay the initialization of the table to not waste memory
200n/a # if the function is never called
201n/a if _b32rev is None:
202n/a _b32rev = {v: k for k, v in enumerate(_b32alphabet)}
203n/a s = _bytes_from_decode_data(s)
204n/a if len(s) % 8:
205n/a raise binascii.Error('Incorrect padding')
206n/a # Handle section 2.4 zero and one mapping. The flag map01 will be either
207n/a # False, or the character to map the digit 1 (one) to. It should be
208n/a # either L (el) or I (eye).
209n/a if map01 is not None:
210n/a map01 = _bytes_from_decode_data(map01)
211n/a assert len(map01) == 1, repr(map01)
212n/a s = s.translate(bytes.maketrans(b'01', b'O' + map01))
213n/a if casefold:
214n/a s = s.upper()
215n/a # Strip off pad characters from the right. We need to count the pad
216n/a # characters because this will tell us how many null bytes to remove from
217n/a # the end of the decoded string.
218n/a l = len(s)
219n/a s = s.rstrip(b'=')
220n/a padchars = l - len(s)
221n/a # Now decode the full quanta
222n/a decoded = bytearray()
223n/a b32rev = _b32rev
224n/a for i in range(0, len(s), 8):
225n/a quanta = s[i: i + 8]
226n/a acc = 0
227n/a try:
228n/a for c in quanta:
229n/a acc = (acc << 5) + b32rev[c]
230n/a except KeyError:
231n/a raise binascii.Error('Non-base32 digit found') from None
232n/a decoded += acc.to_bytes(5, 'big')
233n/a # Process the last, partial quanta
234n/a if padchars:
235n/a acc <<= 5 * padchars
236n/a last = acc.to_bytes(5, 'big')
237n/a if padchars == 1:
238n/a decoded[-5:] = last[:-1]
239n/a elif padchars == 3:
240n/a decoded[-5:] = last[:-2]
241n/a elif padchars == 4:
242n/a decoded[-5:] = last[:-3]
243n/a elif padchars == 6:
244n/a decoded[-5:] = last[:-4]
245n/a else:
246n/a raise binascii.Error('Incorrect padding')
247n/a return bytes(decoded)
248n/a
249n/a
250n/a
251n/a# RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
252n/a# lowercase. The RFC also recommends against accepting input case
253n/a# insensitively.
254n/adef b16encode(s):
255n/a """Encode the bytes-like object s using Base16 and return a bytes object.
256n/a """
257n/a return binascii.hexlify(s).upper()
258n/a
259n/a
260n/adef b16decode(s, casefold=False):
261n/a """Decode the Base16 encoded bytes-like object or ASCII string s.
262n/a
263n/a Optional casefold is a flag specifying whether a lowercase alphabet is
264n/a acceptable as input. For security purposes, the default is False.
265n/a
266n/a The result is returned as a bytes object. A binascii.Error is raised if
267n/a s is incorrectly padded or if there are non-alphabet characters present
268n/a in the input.
269n/a """
270n/a s = _bytes_from_decode_data(s)
271n/a if casefold:
272n/a s = s.upper()
273n/a if re.search(b'[^0-9A-F]', s):
274n/a raise binascii.Error('Non-base16 digit found')
275n/a return binascii.unhexlify(s)
276n/a
277n/a#
278n/a# Ascii85 encoding/decoding
279n/a#
280n/a
281n/a_a85chars = None
282n/a_a85chars2 = None
283n/a_A85START = b"<~"
284n/a_A85END = b"~>"
285n/a
286n/adef _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False):
287n/a # Helper function for a85encode and b85encode
288n/a if not isinstance(b, bytes_types):
289n/a b = memoryview(b).tobytes()
290n/a
291n/a padding = (-len(b)) % 4
292n/a if padding:
293n/a b = b + b'\0' * padding
294n/a words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b)
295n/a
296n/a chunks = [b'z' if foldnuls and not word else
297n/a b'y' if foldspaces and word == 0x20202020 else
298n/a (chars2[word // 614125] +
299n/a chars2[word // 85 % 7225] +
300n/a chars[word % 85])
301n/a for word in words]
302n/a
303n/a if padding and not pad:
304n/a if chunks[-1] == b'z':
305n/a chunks[-1] = chars[0] * 5
306n/a chunks[-1] = chunks[-1][:-padding]
307n/a
308n/a return b''.join(chunks)
309n/a
310n/adef a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False):
311n/a """Encode bytes-like object b using Ascii85 and return a bytes object.
312n/a
313n/a foldspaces is an optional flag that uses the special short sequence 'y'
314n/a instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
315n/a feature is not supported by the "standard" Adobe encoding.
316n/a
317n/a wrapcol controls whether the output should have newline (b'\\n') characters
318n/a added to it. If this is non-zero, each output line will be at most this
319n/a many characters long.
320n/a
321n/a pad controls whether the input is padded to a multiple of 4 before
322n/a encoding. Note that the btoa implementation always pads.
323n/a
324n/a adobe controls whether the encoded byte sequence is framed with <~ and ~>,
325n/a which is used by the Adobe implementation.
326n/a """
327n/a global _a85chars, _a85chars2
328n/a # Delay the initialization of tables to not waste memory
329n/a # if the function is never called
330n/a if _a85chars is None:
331n/a _a85chars = [bytes((i,)) for i in range(33, 118)]
332n/a _a85chars2 = [(a + b) for a in _a85chars for b in _a85chars]
333n/a
334n/a result = _85encode(b, _a85chars, _a85chars2, pad, True, foldspaces)
335n/a
336n/a if adobe:
337n/a result = _A85START + result
338n/a if wrapcol:
339n/a wrapcol = max(2 if adobe else 1, wrapcol)
340n/a chunks = [result[i: i + wrapcol]
341n/a for i in range(0, len(result), wrapcol)]
342n/a if adobe:
343n/a if len(chunks[-1]) + 2 > wrapcol:
344n/a chunks.append(b'')
345n/a result = b'\n'.join(chunks)
346n/a if adobe:
347n/a result += _A85END
348n/a
349n/a return result
350n/a
351n/adef a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v'):
352n/a """Decode the Ascii85 encoded bytes-like object or ASCII string b.
353n/a
354n/a foldspaces is a flag that specifies whether the 'y' short sequence should be
355n/a accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is
356n/a not supported by the "standard" Adobe encoding.
357n/a
358n/a adobe controls whether the input sequence is in Adobe Ascii85 format (i.e.
359n/a is framed with <~ and ~>).
360n/a
361n/a ignorechars should be a byte string containing characters to ignore from the
362n/a input. This should only contain whitespace characters, and by default
363n/a contains all whitespace characters in ASCII.
364n/a
365n/a The result is returned as a bytes object.
366n/a """
367n/a b = _bytes_from_decode_data(b)
368n/a if adobe:
369n/a if not b.endswith(_A85END):
370n/a raise ValueError(
371n/a "Ascii85 encoded byte sequences must end "
372n/a "with {!r}".format(_A85END)
373n/a )
374n/a if b.startswith(_A85START):
375n/a b = b[2:-2] # Strip off start/end markers
376n/a else:
377n/a b = b[:-2]
378n/a #
379n/a # We have to go through this stepwise, so as to ignore spaces and handle
380n/a # special short sequences
381n/a #
382n/a packI = struct.Struct('!I').pack
383n/a decoded = []
384n/a decoded_append = decoded.append
385n/a curr = []
386n/a curr_append = curr.append
387n/a curr_clear = curr.clear
388n/a for x in b + b'u' * 4:
389n/a if b'!'[0] <= x <= b'u'[0]:
390n/a curr_append(x)
391n/a if len(curr) == 5:
392n/a acc = 0
393n/a for x in curr:
394n/a acc = 85 * acc + (x - 33)
395n/a try:
396n/a decoded_append(packI(acc))
397n/a except struct.error:
398n/a raise ValueError('Ascii85 overflow') from None
399n/a curr_clear()
400n/a elif x == b'z'[0]:
401n/a if curr:
402n/a raise ValueError('z inside Ascii85 5-tuple')
403n/a decoded_append(b'\0\0\0\0')
404n/a elif foldspaces and x == b'y'[0]:
405n/a if curr:
406n/a raise ValueError('y inside Ascii85 5-tuple')
407n/a decoded_append(b'\x20\x20\x20\x20')
408n/a elif x in ignorechars:
409n/a # Skip whitespace
410n/a continue
411n/a else:
412n/a raise ValueError('Non-Ascii85 digit found: %c' % x)
413n/a
414n/a result = b''.join(decoded)
415n/a padding = 4 - len(curr)
416n/a if padding:
417n/a # Throw away the extra padding
418n/a result = result[:-padding]
419n/a return result
420n/a
421n/a# The following code is originally taken (with permission) from Mercurial
422n/a
423n/a_b85alphabet = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
424n/a b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~")
425n/a_b85chars = None
426n/a_b85chars2 = None
427n/a_b85dec = None
428n/a
429n/adef b85encode(b, pad=False):
430n/a """Encode bytes-like object b in base85 format and return a bytes object.
431n/a
432n/a If pad is true, the input is padded with b'\\0' so its length is a multiple of
433n/a 4 bytes before encoding.
434n/a """
435n/a global _b85chars, _b85chars2
436n/a # Delay the initialization of tables to not waste memory
437n/a # if the function is never called
438n/a if _b85chars is None:
439n/a _b85chars = [bytes((i,)) for i in _b85alphabet]
440n/a _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
441n/a return _85encode(b, _b85chars, _b85chars2, pad)
442n/a
443n/adef b85decode(b):
444n/a """Decode the base85-encoded bytes-like object or ASCII string b
445n/a
446n/a The result is returned as a bytes object.
447n/a """
448n/a global _b85dec
449n/a # Delay the initialization of tables to not waste memory
450n/a # if the function is never called
451n/a if _b85dec is None:
452n/a _b85dec = [None] * 256
453n/a for i, c in enumerate(_b85alphabet):
454n/a _b85dec[c] = i
455n/a
456n/a b = _bytes_from_decode_data(b)
457n/a padding = (-len(b)) % 5
458n/a b = b + b'~' * padding
459n/a out = []
460n/a packI = struct.Struct('!I').pack
461n/a for i in range(0, len(b), 5):
462n/a chunk = b[i:i + 5]
463n/a acc = 0
464n/a try:
465n/a for c in chunk:
466n/a acc = acc * 85 + _b85dec[c]
467n/a except TypeError:
468n/a for j, c in enumerate(chunk):
469n/a if _b85dec[c] is None:
470n/a raise ValueError('bad base85 character at position %d'
471n/a % (i + j)) from None
472n/a raise
473n/a try:
474n/a out.append(packI(acc))
475n/a except struct.error:
476n/a raise ValueError('base85 overflow in hunk starting at byte %d'
477n/a % i) from None
478n/a
479n/a result = b''.join(out)
480n/a if padding:
481n/a result = result[:-padding]
482n/a return result
483n/a
484n/a# Legacy interface. This code could be cleaned up since I don't believe
485n/a# binascii has any line length limitations. It just doesn't seem worth it
486n/a# though. The files should be opened in binary mode.
487n/a
488n/aMAXLINESIZE = 76 # Excluding the CRLF
489n/aMAXBINSIZE = (MAXLINESIZE//4)*3
490n/a
491n/adef encode(input, output):
492n/a """Encode a file; input and output are binary files."""
493n/a while True:
494n/a s = input.read(MAXBINSIZE)
495n/a if not s:
496n/a break
497n/a while len(s) < MAXBINSIZE:
498n/a ns = input.read(MAXBINSIZE-len(s))
499n/a if not ns:
500n/a break
501n/a s += ns
502n/a line = binascii.b2a_base64(s)
503n/a output.write(line)
504n/a
505n/a
506n/adef decode(input, output):
507n/a """Decode a file; input and output are binary files."""
508n/a while True:
509n/a line = input.readline()
510n/a if not line:
511n/a break
512n/a s = binascii.a2b_base64(line)
513n/a output.write(s)
514n/a
515n/adef _input_type_check(s):
516n/a try:
517n/a m = memoryview(s)
518n/a except TypeError as err:
519n/a msg = "expected bytes-like object, not %s" % s.__class__.__name__
520n/a raise TypeError(msg) from err
521n/a if m.format not in ('c', 'b', 'B'):
522n/a msg = ("expected single byte elements, not %r from %s" %
523n/a (m.format, s.__class__.__name__))
524n/a raise TypeError(msg)
525n/a if m.ndim != 1:
526n/a msg = ("expected 1-D data, not %d-D data from %s" %
527n/a (m.ndim, s.__class__.__name__))
528n/a raise TypeError(msg)
529n/a
530n/a
531n/adef encodebytes(s):
532n/a """Encode a bytestring into a bytes object containing multiple lines
533n/a of base-64 data."""
534n/a _input_type_check(s)
535n/a pieces = []
536n/a for i in range(0, len(s), MAXBINSIZE):
537n/a chunk = s[i : i + MAXBINSIZE]
538n/a pieces.append(binascii.b2a_base64(chunk))
539n/a return b"".join(pieces)
540n/a
541n/adef encodestring(s):
542n/a """Legacy alias of encodebytes()."""
543n/a import warnings
544n/a warnings.warn("encodestring() is a deprecated alias, use encodebytes()",
545n/a DeprecationWarning, 2)
546n/a return encodebytes(s)
547n/a
548n/a
549n/adef decodebytes(s):
550n/a """Decode a bytestring of base-64 data into a bytes object."""
551n/a _input_type_check(s)
552n/a return binascii.a2b_base64(s)
553n/a
554n/adef decodestring(s):
555n/a """Legacy alias of decodebytes()."""
556n/a import warnings
557n/a warnings.warn("decodestring() is a deprecated alias, use decodebytes()",
558n/a DeprecationWarning, 2)
559n/a return decodebytes(s)
560n/a
561n/a
562n/a# Usable as a script...
563n/adef main():
564n/a """Small main program"""
565n/a import sys, getopt
566n/a try:
567n/a opts, args = getopt.getopt(sys.argv[1:], 'deut')
568n/a except getopt.error as msg:
569n/a sys.stdout = sys.stderr
570n/a print(msg)
571n/a print("""usage: %s [-d|-e|-u|-t] [file|-]
572n/a -d, -u: decode
573n/a -e: encode (default)
574n/a -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0])
575n/a sys.exit(2)
576n/a func = encode
577n/a for o, a in opts:
578n/a if o == '-e': func = encode
579n/a if o == '-d': func = decode
580n/a if o == '-u': func = decode
581n/a if o == '-t': test(); return
582n/a if args and args[0] != '-':
583n/a with open(args[0], 'rb') as f:
584n/a func(f, sys.stdout.buffer)
585n/a else:
586n/a func(sys.stdin.buffer, sys.stdout.buffer)
587n/a
588n/a
589n/adef test():
590n/a s0 = b"Aladdin:open sesame"
591n/a print(repr(s0))
592n/a s1 = encodebytes(s0)
593n/a print(repr(s1))
594n/a s2 = decodebytes(s1)
595n/a print(repr(s2))
596n/a assert s0 == s2
597n/a
598n/a
599n/aif __name__ == '__main__':
600n/a main()