Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2006 Python Software Foundation |
| 2 | # Author: Barry Warsaw |
| 3 | # Contact: email-sig@python.org |
| 4 | |
| 5 | """Encodings and related functions.""" |
| 6 | |
| 7 | __all__ = [ |
| 8 | 'encode_7or8bit', |
| 9 | 'encode_base64', |
| 10 | 'encode_noop', |
| 11 | 'encode_quopri', |
| 12 | ] |
| 13 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 14 | |
R David Murray | 6d94bd4 | 2011-03-16 15:52:22 -0400 | [diff] [blame] | 15 | from base64 import encodebytes as _bencode |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 16 | from quopri import encodestring as _encodestring |
| 17 | |
| 18 | |
| 19 | |
| 20 | def _qencode(s): |
| 21 | enc = _encodestring(s, quotetabs=True) |
| 22 | # Must encode spaces, which quopri.encodestring() doesn't do |
R David Murray | f6069f9 | 2013-06-27 18:37:00 -0400 | [diff] [blame] | 23 | return enc.replace(b' ', b'=20') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 24 | |
| 25 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 26 | def encode_base64(msg): |
| 27 | """Encode the message's payload in Base64. |
| 28 | |
| 29 | Also, add an appropriate Content-Transfer-Encoding header. |
| 30 | """ |
| 31 | orig = msg.get_payload() |
R. David Murray | 7da8f06 | 2010-06-04 16:11:08 +0000 | [diff] [blame] | 32 | encdata = str(_bencode(orig), 'ascii') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 33 | msg.set_payload(encdata) |
| 34 | msg['Content-Transfer-Encoding'] = 'base64' |
| 35 | |
| 36 | |
| 37 | |
| 38 | def encode_quopri(msg): |
| 39 | """Encode the message's payload in quoted-printable. |
| 40 | |
| 41 | Also, add an appropriate Content-Transfer-Encoding header. |
| 42 | """ |
| 43 | orig = msg.get_payload() |
R David Murray | f6069f9 | 2013-06-27 18:37:00 -0400 | [diff] [blame] | 44 | if isinstance(orig, str): |
| 45 | # If it is a string, the model data may have binary data encoded in via |
| 46 | # surrogateescape. Convert back to bytes so we can CTE encode it. |
| 47 | orig = orig.encode('ascii', 'surrogateescape') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 48 | encdata = _qencode(orig) |
R David Murray | f6069f9 | 2013-06-27 18:37:00 -0400 | [diff] [blame] | 49 | msg.set_payload(encdata.decode('ascii', 'surrogateescape')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 50 | msg['Content-Transfer-Encoding'] = 'quoted-printable' |
| 51 | |
| 52 | |
| 53 | |
| 54 | def encode_7or8bit(msg): |
| 55 | """Set the Content-Transfer-Encoding header to 7bit or 8bit.""" |
| 56 | orig = msg.get_payload() |
| 57 | if orig is None: |
| 58 | # There's no payload. For backwards compatibility we use 7bit |
| 59 | msg['Content-Transfer-Encoding'] = '7bit' |
| 60 | return |
R David Murray | 56a9d7e | 2011-03-15 12:20:02 -0400 | [diff] [blame] | 61 | # We play a trick to make this go fast. If encoding/decode to ASCII |
| 62 | # succeeds, we know the data must be 7bit, otherwise treat it as 8bit. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 63 | try: |
R David Murray | 56a9d7e | 2011-03-15 12:20:02 -0400 | [diff] [blame] | 64 | if isinstance(orig, str): |
| 65 | orig.encode('ascii') |
| 66 | else: |
| 67 | orig.decode('ascii') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 68 | except UnicodeError: |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 69 | charset = msg.get_charset() |
| 70 | output_cset = charset and charset.output_charset |
R David Murray | ec317a8 | 2013-02-11 10:51:28 -0500 | [diff] [blame] | 71 | # iso-2022-* is non-ASCII but encodes to a 7-bit representation |
R. David Murray | ef3d6bd | 2010-04-12 14:48:58 +0000 | [diff] [blame] | 72 | if output_cset and output_cset.lower().startswith('iso-2022-'): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 73 | msg['Content-Transfer-Encoding'] = '7bit' |
| 74 | else: |
| 75 | msg['Content-Transfer-Encoding'] = '8bit' |
| 76 | else: |
| 77 | msg['Content-Transfer-Encoding'] = '7bit' |
R David Murray | ec317a8 | 2013-02-11 10:51:28 -0500 | [diff] [blame] | 78 | if not isinstance(orig, str): |
| 79 | msg.set_payload(orig.decode('ascii', 'surrogateescape')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | |
| 83 | def encode_noop(msg): |
| 84 | """Do nothing.""" |
R David Murray | ceaa8b1 | 2013-02-09 13:02:58 -0500 | [diff] [blame] | 85 | # Well, not quite *nothing*: in Python3 we have to turn bytes into a string |
| 86 | # in our internal surrogateescaped form in order to keep the model |
| 87 | # consistent. |
| 88 | orig = msg.get_payload() |
| 89 | if not isinstance(orig, str): |
| 90 | msg.set_payload(orig.decode('ascii', 'surrogateescape')) |