c 20022007 python software foundation ben gertzfield contact emailsigpython org base64 content transfer encoding per rfcs 20452047 this module handles the content transfer encoding method defined in rfc 2045 to encode arbitrary 8bit data using the three 8bit bytes in four 7bit characters encoding known as base64 it is used in the mime standards for email to attach images audio and text using some 8bit character sets to messages this module provides an interface to encode and decode both headers and bodies with base64 encoding rfc 2045 defines a method for including character set information in an encodedword in a header this method is commonly used for 8bit real names in to from cc etc fields as well as subject lines this module does not do the line wrapping or endofline character conversion necessary for proper internationalized headers it only does dumb encoding and decoding to deal with the various line wrapping issues use the email header module see also charset py helpers return the length of s when it is encoded with base64 groupsof3 leftover divmodlenbytearray 3 4 bytes out for each 3 bytes or nonzero fraction thereof in n groupsof3 4 if leftover n 4 return n def headerencodeheaderbytes charset iso88591 if not headerbytes return if isinstanceheaderbytes str headerbytes headerbytes encodecharset encoded b64encodeheaderbytes decodeascii return s b s charset encoded def bodyencodes maxlinelen76 eolnl rencode a string with base64 each line will be wrapped at at most maxlinelen characters defaults to 76 characters each line of encoded text will end with eol which defaults to n set this to rn if you will be using the result of this function directly in an email baw should encode inherit b2abase64 s dubious behavior in adding a newline to the encoded string decode a raw base64 string returning a bytes object this function does not parse a full mime header value encoded with base64 like iso88591 b bmloisbuawgh please use the high level email header class for that functionality for convenience and backwards compatibility w standard base64 module c 2002 2007 python software foundation ben gertzfield contact email sig python org base64 content transfer encoding per rfcs 2045 2047 this module handles the content transfer encoding method defined in rfc 2045 to encode arbitrary 8 bit data using the three 8 bit bytes in four 7 bit characters encoding known as base64 it is used in the mime standards for email to attach images audio and text using some 8 bit character sets to messages this module provides an interface to encode and decode both headers and bodies with base64 encoding rfc 2045 defines a method for including character set information in an encoded word in a header this method is commonly used for 8 bit real names in to from cc etc fields as well as subject lines this module does not do the line wrapping or end of line character conversion necessary for proper internationalized headers it only does dumb encoding and decoding to deal with the various line wrapping issues use the email header module see also charset py helpers return the length of s when it is encoded with base64 4 bytes out for each 3 bytes or nonzero fraction thereof in encode a single header line with base64 encoding in a given charset charset names the character set to use to encode the header it defaults to iso 8859 1 base64 encoding is defined in rfc 2045 encode a string with base64 each line will be wrapped at at most maxlinelen characters defaults to 76 characters each line of encoded text will end with eol which defaults to n set this to r n if you will be using the result of this function directly in an email baw should encode inherit b2a_base64 s dubious behavior in adding a newline to the encoded string decode a raw base64 string returning a bytes object this function does not parse a full mime header value encoded with base64 like iso 8859 1 b bmloisbuawgh please use the high level email header class for that functionality for convenience and backwards compatibility w standard base64 module
__all__ = [
    'body_decode',
    'body_encode',
    'decode',
    'decodestring',
    'header_encode',
    'header_length',
    ]


from base64 import b64encode
from binascii import b2a_base64, a2b_base64

CRLF = '\r\n'
NL = '\n'
EMPTYSTRING = ''


MISC_LEN = 7



def header_length(bytearray):
    
    groups_of_3, leftover = divmod(len(bytearray), 3)
    
    n = groups_of_3 * 4
    if leftover:
        n += 4
    return n


def header_encode(header_bytes, charset='iso-8859-1'):
    
    if not header_bytes:
        return ""
    if isinstance(header_bytes, str):
        header_bytes = header_bytes.encode(charset)
    encoded = b64encode(header_bytes).decode("ascii")
    return '=?%s?b?%s?=' % (charset, encoded)


def body_encode(s, maxlinelen=76, eol=NL):
    r
    if not s:
        return ""

    encvec = []
    max_unencoded = maxlinelen * 3 // 4
    for i in range(0, len(s), max_unencoded):
        
        
        enc = b2a_base64(s[i:i + max_unencoded]).decode("ascii")
        if enc.endswith(NL) and eol != NL:
            enc = enc[:-1] + eol
        encvec.append(enc)
    return EMPTYSTRING.join(encvec)


def decode(string):
    
    if not string:
        return bytes()
    elif isinstance(string, str):
        return a2b_base64(string.encode('raw-unicode-escape'))
    else:
        return a2b_base64(string)



body_decode = decode
decodestring = decode