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