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