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