Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../email
File: base64mime.py
# Copyright (C) 2002-2006 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
'base64_len',
[27] Fix | Delete
'body_decode',
[28] Fix | Delete
'body_encode',
[29] Fix | Delete
'decode',
[30] Fix | Delete
'decodestring',
[31] Fix | Delete
'encode',
[32] Fix | Delete
'encodestring',
[33] Fix | Delete
'header_encode',
[34] Fix | Delete
]
[35] Fix | Delete
[36] Fix | Delete
[37] Fix | Delete
from binascii import b2a_base64, a2b_base64
[38] Fix | Delete
from email.utils import fix_eols
[39] Fix | Delete
[40] Fix | Delete
CRLF = '\r\n'
[41] Fix | Delete
NL = '\n'
[42] Fix | Delete
EMPTYSTRING = ''
[43] Fix | Delete
[44] Fix | Delete
# See also Charset.py
[45] Fix | Delete
MISC_LEN = 7
[46] Fix | Delete
[47] Fix | Delete
[48] Fix | Delete
[49] Fix | Delete
# Helpers
[50] Fix | Delete
def base64_len(s):
[51] Fix | Delete
"""Return the length of s when it is encoded with base64."""
[52] Fix | Delete
groups_of_3, leftover = divmod(len(s), 3)
[53] Fix | Delete
# 4 bytes out for each 3 bytes (or nonzero fraction thereof) in.
[54] Fix | Delete
# Thanks, Tim!
[55] Fix | Delete
n = groups_of_3 * 4
[56] Fix | Delete
if leftover:
[57] Fix | Delete
n += 4
[58] Fix | Delete
return n
[59] Fix | Delete
[60] Fix | Delete
[61] Fix | Delete
[62] Fix | Delete
def header_encode(header, charset='iso-8859-1', keep_eols=False,
[63] Fix | Delete
maxlinelen=76, eol=NL):
[64] Fix | Delete
"""Encode a single header line with Base64 encoding in a given charset.
[65] Fix | Delete
[66] Fix | Delete
Defined in RFC 2045, this Base64 encoding is identical to normal Base64
[67] Fix | Delete
encoding, except that each line must be intelligently wrapped (respecting
[68] Fix | Delete
the Base64 encoding), and subsequent lines must start with a space.
[69] Fix | Delete
[70] Fix | Delete
charset names the character set to use to encode the header. It defaults
[71] Fix | Delete
to iso-8859-1.
[72] Fix | Delete
[73] Fix | Delete
End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted
[74] Fix | Delete
to the canonical email line separator \\r\\n unless the keep_eols
[75] Fix | Delete
parameter is True (the default is False).
[76] Fix | Delete
[77] Fix | Delete
Each line of the header will be terminated in the value of eol, which
[78] Fix | Delete
defaults to "\\n". Set this to "\\r\\n" if you are using the result of
[79] Fix | Delete
this function directly in email.
[80] Fix | Delete
[81] Fix | Delete
The resulting string will be in the form:
[82] Fix | Delete
[83] Fix | Delete
"=?charset?b?WW/5ciBtYXp66XLrIHf8eiBhIGhhbXBzdGHuciBBIFlv+XIgbWF6euly?=\\n
[84] Fix | Delete
=?charset?b?6yB3/HogYSBoYW1wc3Rh7nIgQkMgWW/5ciBtYXp66XLrIHf8eiBhIGhh?="
[85] Fix | Delete
[86] Fix | Delete
with each line wrapped at, at most, maxlinelen characters (defaults to 76
[87] Fix | Delete
characters).
[88] Fix | Delete
"""
[89] Fix | Delete
# Return empty headers unchanged
[90] Fix | Delete
if not header:
[91] Fix | Delete
return header
[92] Fix | Delete
[93] Fix | Delete
if not keep_eols:
[94] Fix | Delete
header = fix_eols(header)
[95] Fix | Delete
[96] Fix | Delete
# Base64 encode each line, in encoded chunks no greater than maxlinelen in
[97] Fix | Delete
# length, after the RFC chrome is added in.
[98] Fix | Delete
base64ed = []
[99] Fix | Delete
max_encoded = maxlinelen - len(charset) - MISC_LEN
[100] Fix | Delete
max_unencoded = max_encoded * 3 // 4
[101] Fix | Delete
[102] Fix | Delete
for i in range(0, len(header), max_unencoded):
[103] Fix | Delete
base64ed.append(b2a_base64(header[i:i+max_unencoded]))
[104] Fix | Delete
[105] Fix | Delete
# Now add the RFC chrome to each encoded chunk
[106] Fix | Delete
lines = []
[107] Fix | Delete
for line in base64ed:
[108] Fix | Delete
# Ignore the last character of each line if it is a newline
[109] Fix | Delete
if line.endswith(NL):
[110] Fix | Delete
line = line[:-1]
[111] Fix | Delete
# Add the chrome
[112] Fix | Delete
lines.append('=?%s?b?%s?=' % (charset, line))
[113] Fix | Delete
# Glue the lines together and return it. BAW: should we be able to
[114] Fix | Delete
# specify the leading whitespace in the joiner?
[115] Fix | Delete
joiner = eol + ' '
[116] Fix | Delete
return joiner.join(lines)
[117] Fix | Delete
[118] Fix | Delete
[119] Fix | Delete
[120] Fix | Delete
def encode(s, binary=True, maxlinelen=76, eol=NL):
[121] Fix | Delete
"""Encode a string with base64.
[122] Fix | Delete
[123] Fix | Delete
Each line will be wrapped at, at most, maxlinelen characters (defaults to
[124] Fix | Delete
76 characters).
[125] Fix | Delete
[126] Fix | Delete
If binary is False, end-of-line characters will be converted to the
[127] Fix | Delete
canonical email end-of-line sequence \\r\\n. Otherwise they will be left
[128] Fix | Delete
verbatim (this is the default).
[129] Fix | Delete
[130] Fix | Delete
Each line of encoded text will end with eol, which defaults to "\\n". Set
[131] Fix | Delete
this to "\\r\\n" if you will be using the result of this function directly
[132] Fix | Delete
in an email.
[133] Fix | Delete
"""
[134] Fix | Delete
if not s:
[135] Fix | Delete
return s
[136] Fix | Delete
[137] Fix | Delete
if not binary:
[138] Fix | Delete
s = fix_eols(s)
[139] Fix | Delete
[140] Fix | Delete
encvec = []
[141] Fix | Delete
max_unencoded = maxlinelen * 3 // 4
[142] Fix | Delete
for i in range(0, len(s), max_unencoded):
[143] Fix | Delete
# BAW: should encode() inherit b2a_base64()'s dubious behavior in
[144] Fix | Delete
# adding a newline to the encoded string?
[145] Fix | Delete
enc = b2a_base64(s[i:i + max_unencoded])
[146] Fix | Delete
if enc.endswith(NL) and eol != NL:
[147] Fix | Delete
enc = enc[:-1] + eol
[148] Fix | Delete
encvec.append(enc)
[149] Fix | Delete
return EMPTYSTRING.join(encvec)
[150] Fix | Delete
[151] Fix | Delete
[152] Fix | Delete
# For convenience and backwards compatibility w/ standard base64 module
[153] Fix | Delete
body_encode = encode
[154] Fix | Delete
encodestring = encode
[155] Fix | Delete
[156] Fix | Delete
[157] Fix | Delete
[158] Fix | Delete
def decode(s, convert_eols=None):
[159] Fix | Delete
"""Decode a raw base64 string.
[160] Fix | Delete
[161] Fix | Delete
If convert_eols is set to a string value, all canonical email linefeeds,
[162] Fix | Delete
e.g. "\\r\\n", in the decoded text will be converted to the value of
[163] Fix | Delete
convert_eols. os.linesep is a good choice for convert_eols if you are
[164] Fix | Delete
decoding a text attachment.
[165] Fix | Delete
[166] Fix | Delete
This function does not parse a full MIME header value encoded with
[167] Fix | Delete
base64 (like =?iso-8859-1?b?bmloISBuaWgh?=) -- please use the high
[168] Fix | Delete
level email.header class for that functionality.
[169] Fix | Delete
"""
[170] Fix | Delete
if not s:
[171] Fix | Delete
return s
[172] Fix | Delete
[173] Fix | Delete
dec = a2b_base64(s)
[174] Fix | Delete
if convert_eols:
[175] Fix | Delete
return dec.replace(CRLF, convert_eols)
[176] Fix | Delete
return dec
[177] Fix | Delete
[178] Fix | Delete
[179] Fix | Delete
# For convenience and backwards compatibility w/ standard base64 module
[180] Fix | Delete
body_decode = decode
[181] Fix | Delete
decodestring = decode
[182] Fix | Delete
[183] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function