ยปCore Development>Code coverage>Lib/email/base64mime.py

Python code coverage for Lib/email/base64mime.py

#countcontent
1n/a# Copyright (C) 2002-2007 Python Software Foundation
2n/a# Author: Ben Gertzfield
3n/a# Contact: email-sig@python.org
4n/a
5n/a"""Base64 content transfer encoding per RFCs 2045-2047.
6n/a
7n/aThis module handles the content transfer encoding method defined in RFC 2045
8n/ato encode arbitrary 8-bit data using the three 8-bit bytes in four 7-bit
9n/acharacters encoding known as Base64.
10n/a
11n/aIt is used in the MIME standards for email to attach images, audio, and text
12n/ausing some 8-bit character sets to messages.
13n/a
14n/aThis module provides an interface to encode and decode both headers and bodies
15n/awith Base64 encoding.
16n/a
17n/aRFC 2045 defines a method for including character set information in an
18n/a`encoded-word' in a header. This method is commonly used for 8-bit real names
19n/ain To:, From:, Cc:, etc. fields, as well as Subject: lines.
20n/a
21n/aThis module does not do the line wrapping or end-of-line character conversion
22n/anecessary for proper internationalized headers; it only does dumb encoding and
23n/adecoding. To deal with the various line wrapping issues, use the email.header
24n/amodule.
25n/a"""
26n/a
27n/a__all__ = [
28n/a 'body_decode',
29n/a 'body_encode',
30n/a 'decode',
31n/a 'decodestring',
32n/a 'header_encode',
33n/a 'header_length',
34n/a ]
35n/a
36n/a
37n/afrom base64 import b64encode
38n/afrom binascii import b2a_base64, a2b_base64
39n/a
40n/aCRLF = '\r\n'
41n/aNL = '\n'
42n/aEMPTYSTRING = ''
43n/a
44n/a# See also Charset.py
45n/aMISC_LEN = 7
46n/a
47n/a
48n/a
49n/a# Helpers
50n/adef header_length(bytearray):
51n/a """Return the length of s when it is encoded with base64."""
52n/a groups_of_3, leftover = divmod(len(bytearray), 3)
53n/a # 4 bytes out for each 3 bytes (or nonzero fraction thereof) in.
54n/a n = groups_of_3 * 4
55n/a if leftover:
56n/a n += 4
57n/a return n
58n/a
59n/a
60n/a
61n/adef header_encode(header_bytes, charset='iso-8859-1'):
62n/a """Encode a single header line with Base64 encoding in a given charset.
63n/a
64n/a charset names the character set to use to encode the header. It defaults
65n/a to iso-8859-1. Base64 encoding is defined in RFC 2045.
66n/a """
67n/a if not header_bytes:
68n/a return ""
69n/a if isinstance(header_bytes, str):
70n/a header_bytes = header_bytes.encode(charset)
71n/a encoded = b64encode(header_bytes).decode("ascii")
72n/a return '=?%s?b?%s?=' % (charset, encoded)
73n/a
74n/a
75n/a
76n/adef body_encode(s, maxlinelen=76, eol=NL):
77n/a r"""Encode a string with base64.
78n/a
79n/a Each line will be wrapped at, at most, maxlinelen characters (defaults to
80n/a 76 characters).
81n/a
82n/a Each line of encoded text will end with eol, which defaults to "\n". Set
83n/a this to "\r\n" if you will be using the result of this function directly
84n/a in an email.
85n/a """
86n/a if not s:
87n/a return s
88n/a
89n/a encvec = []
90n/a max_unencoded = maxlinelen * 3 // 4
91n/a for i in range(0, len(s), max_unencoded):
92n/a # BAW: should encode() inherit b2a_base64()'s dubious behavior in
93n/a # adding a newline to the encoded string?
94n/a enc = b2a_base64(s[i:i + max_unencoded]).decode("ascii")
95n/a if enc.endswith(NL) and eol != NL:
96n/a enc = enc[:-1] + eol
97n/a encvec.append(enc)
98n/a return EMPTYSTRING.join(encvec)
99n/a
100n/a
101n/a
102n/adef decode(string):
103n/a """Decode a raw base64 string, returning a bytes object.
104n/a
105n/a This function does not parse a full MIME header value encoded with
106n/a base64 (like =?iso-8859-1?b?bmloISBuaWgh?=) -- please use the high
107n/a level email.header class for that functionality.
108n/a """
109n/a if not string:
110n/a return bytes()
111n/a elif isinstance(string, str):
112n/a return a2b_base64(string.encode('raw-unicode-escape'))
113n/a else:
114n/a return a2b_base64(string)
115n/a
116n/a
117n/a# For convenience and backwards compatibility w/ standard base64 module
118n/abody_decode = decode
119n/adecodestring = decode