+++ /dev/null
-# Copyright (C) 2002-2006 Python Software Foundation\r
-# Author: Ben Gertzfield\r
-# Contact: email-sig@python.org\r
-\r
-"""Base64 content transfer encoding per RFCs 2045-2047.\r
-\r
-This module handles the content transfer encoding method defined in RFC 2045\r
-to encode arbitrary 8-bit data using the three 8-bit bytes in four 7-bit\r
-characters encoding known as Base64.\r
-\r
-It is used in the MIME standards for email to attach images, audio, and text\r
-using some 8-bit character sets to messages.\r
-\r
-This module provides an interface to encode and decode both headers and bodies\r
-with Base64 encoding.\r
-\r
-RFC 2045 defines a method for including character set information in an\r
-`encoded-word' in a header. This method is commonly used for 8-bit real names\r
-in To:, From:, Cc:, etc. fields, as well as Subject: lines.\r
-\r
-This module does not do the line wrapping or end-of-line character conversion\r
-necessary for proper internationalized headers; it only does dumb encoding and\r
-decoding. To deal with the various line wrapping issues, use the email.header\r
-module.\r
-"""\r
-\r
-__all__ = [\r
- 'base64_len',\r
- 'body_decode',\r
- 'body_encode',\r
- 'decode',\r
- 'decodestring',\r
- 'encode',\r
- 'encodestring',\r
- 'header_encode',\r
- ]\r
-\r
-\r
-from binascii import b2a_base64, a2b_base64\r
-from email.utils import fix_eols\r
-\r
-CRLF = '\r\n'\r
-NL = '\n'\r
-EMPTYSTRING = ''\r
-\r
-# See also Charset.py\r
-MISC_LEN = 7\r
-\r
-\r
-\f\r
-# Helpers\r
-def base64_len(s):\r
- """Return the length of s when it is encoded with base64."""\r
- groups_of_3, leftover = divmod(len(s), 3)\r
- # 4 bytes out for each 3 bytes (or nonzero fraction thereof) in.\r
- # Thanks, Tim!\r
- n = groups_of_3 * 4\r
- if leftover:\r
- n += 4\r
- return n\r
-\r
-\r
-\f\r
-def header_encode(header, charset='iso-8859-1', keep_eols=False,\r
- maxlinelen=76, eol=NL):\r
- """Encode a single header line with Base64 encoding in a given charset.\r
-\r
- Defined in RFC 2045, this Base64 encoding is identical to normal Base64\r
- encoding, except that each line must be intelligently wrapped (respecting\r
- the Base64 encoding), and subsequent lines must start with a space.\r
-\r
- charset names the character set to use to encode the header. It defaults\r
- to iso-8859-1.\r
-\r
- End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted\r
- to the canonical email line separator \\r\\n unless the keep_eols\r
- parameter is True (the default is False).\r
-\r
- Each line of the header will be terminated in the value of eol, which\r
- defaults to "\\n". Set this to "\\r\\n" if you are using the result of\r
- this function directly in email.\r
-\r
- The resulting string will be in the form:\r
-\r
- "=?charset?b?WW/5ciBtYXp66XLrIHf8eiBhIGhhbXBzdGHuciBBIFlv+XIgbWF6euly?=\\n\r
- =?charset?b?6yB3/HogYSBoYW1wc3Rh7nIgQkMgWW/5ciBtYXp66XLrIHf8eiBhIGhh?="\r
-\r
- with each line wrapped at, at most, maxlinelen characters (defaults to 76\r
- characters).\r
- """\r
- # Return empty headers unchanged\r
- if not header:\r
- return header\r
-\r
- if not keep_eols:\r
- header = fix_eols(header)\r
-\r
- # Base64 encode each line, in encoded chunks no greater than maxlinelen in\r
- # length, after the RFC chrome is added in.\r
- base64ed = []\r
- max_encoded = maxlinelen - len(charset) - MISC_LEN\r
- max_unencoded = max_encoded * 3 // 4\r
-\r
- for i in range(0, len(header), max_unencoded):\r
- base64ed.append(b2a_base64(header[i:i+max_unencoded]))\r
-\r
- # Now add the RFC chrome to each encoded chunk\r
- lines = []\r
- for line in base64ed:\r
- # Ignore the last character of each line if it is a newline\r
- if line.endswith(NL):\r
- line = line[:-1]\r
- # Add the chrome\r
- lines.append('=?%s?b?%s?=' % (charset, line))\r
- # Glue the lines together and return it. BAW: should we be able to\r
- # specify the leading whitespace in the joiner?\r
- joiner = eol + ' '\r
- return joiner.join(lines)\r
-\r
-\r
-\f\r
-def encode(s, binary=True, maxlinelen=76, eol=NL):\r
- """Encode a string with base64.\r
-\r
- Each line will be wrapped at, at most, maxlinelen characters (defaults to\r
- 76 characters).\r
-\r
- If binary is False, end-of-line characters will be converted to the\r
- canonical email end-of-line sequence \\r\\n. Otherwise they will be left\r
- verbatim (this is the default).\r
-\r
- Each line of encoded text will end with eol, which defaults to "\\n". Set\r
- this to "\r\n" if you will be using the result of this function directly\r
- in an email.\r
- """\r
- if not s:\r
- return s\r
-\r
- if not binary:\r
- s = fix_eols(s)\r
-\r
- encvec = []\r
- max_unencoded = maxlinelen * 3 // 4\r
- for i in range(0, len(s), max_unencoded):\r
- # BAW: should encode() inherit b2a_base64()'s dubious behavior in\r
- # adding a newline to the encoded string?\r
- enc = b2a_base64(s[i:i + max_unencoded])\r
- if enc.endswith(NL) and eol != NL:\r
- enc = enc[:-1] + eol\r
- encvec.append(enc)\r
- return EMPTYSTRING.join(encvec)\r
-\r
-\r
-# For convenience and backwards compatibility w/ standard base64 module\r
-body_encode = encode\r
-encodestring = encode\r
-\r
-\r
-\f\r
-def decode(s, convert_eols=None):\r
- """Decode a raw base64 string.\r
-\r
- If convert_eols is set to a string value, all canonical email linefeeds,\r
- e.g. "\\r\\n", in the decoded text will be converted to the value of\r
- convert_eols. os.linesep is a good choice for convert_eols if you are\r
- decoding a text attachment.\r
-\r
- This function does not parse a full MIME header value encoded with\r
- base64 (like =?iso-8895-1?b?bmloISBuaWgh?=) -- please use the high\r
- level email.header class for that functionality.\r
- """\r
- if not s:\r
- return s\r
-\r
- dec = a2b_base64(s)\r
- if convert_eols:\r
- return dec.replace(CRLF, convert_eols)\r
- return dec\r
-\r
-\r
-# For convenience and backwards compatibility w/ standard base64 module\r
-body_decode = decode\r
-decodestring = decode\r