base64.py
# Source Generated with Decompyle++
# File: base64.pyc (Python 3.13)
import re
import struct
import binascii
__all__ = [
'encode',
'decode',
'encodebytes',
'decodebytes',
'b64encode',
'b64decode',
'b32encode',
'b32decode',
'b32hexencode',
'b32hexdecode',
'b16encode',
'b16decode',
'b85encode',
'b85decode',
'a85encode',
'a85decode',
'standard_b64encode',
'standard_b64decode',
'urlsafe_b64encode',
'urlsafe_b64decode']
bytes_types = (bytes, bytearray)
def _bytes_from_decode_data(s):
if isinstance(s, str):
return s.encode('ascii')
if UnicodeEncodeError:
raise ValueError('string argument should contain only ASCII characters')
if isinstance(s, bytes_types):
return s
return memoryview(s).tobytes()
if TypeError:
raise TypeError('argument should be a bytes-like object or ASCII string, not %r' % s.__class__.__name__), None
def b64encode(s, altchars = (None,)):
encoded = binascii.b2a_base64(s, newline = False)
return encoded.translate(bytes.maketrans(b'+/', altchars))
return encoded
def b64decode(s, altchars, validate = (None, False)):
s = _bytes_from_decode_data(s)
altchars = _bytes_from_decode_data(altchars)
s = s.translate(bytes.maketrans(altchars, b'+/'))
return binascii.a2b_base64(s, strict_mode = validate)
def standard_b64encode(s):
return b64encode(s)
def standard_b64decode(s):
return b64decode(s)
_urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/')
def urlsafe_b64encode(s):
return b64encode(s).translate(_urlsafe_encode_translation)
def urlsafe_b64decode(s):
s = _bytes_from_decode_data(s)
s = s.translate(_urlsafe_decode_translation)
return b64decode(s)
_B32_ENCODE_DOCSTRING = '\nEncode the bytes-like objects using {encoding} and return a bytes object.\n'
_B32_DECODE_DOCSTRING = '\nDecode the {encoding} encoded bytes-like object or ASCII string s.\n\nOptional casefold is a flag specifying whether a lowercase alphabet is\nacceptable as input. For security purposes, the default is False.\n{extra_args}\nThe result is returned as a bytes object. A binascii.Error is raised if\nthe input is incorrectly padded or if there are non-alphabet\ncharacters present in the input.\n'
_B32_DECODE_MAP01_DOCSTRING = '\nRFC 3548 allows for optional mapping of the digit 0 (zero) to the\nletter O (oh), and for optional mapping of the digit 1 (one) to\neither the letter I (eye) or letter L (el). The optional argument\nmap01 when not None, specifies which letter the digit 1 should be\nmapped to (when map01 is not None, the digit 0 is always mapped to\nthe letter O). For security purposes the default is None, so that\n0 and 1 are not allowed in the input.\n'
_b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
_b32hexalphabet = b'0123456789ABCDEFGHIJKLMNOPQRSTUV'
_b32tab2 = { }
_b32rev = { }
def _b32encode(alphabet, s):
# MAKE_CELL(8)
if alphabet not in _b32tab2:
b32tab = alphabet()
_b32tab2[alphabet] = b32tab()
b32tab = None
if not isinstance(s, bytes_types):
s = memoryview(s).tobytes()
leftover = len(s) % 5
if leftover:
s = s + b'\x00' * (5 - leftover)
encoded = bytearray()
from_bytes = int.from_bytes
b32tab2 = _b32tab2[alphabet]
for i in range(0, len(s), 5):
c = from_bytes(s[i:i + 5])
encoded += b32tab2[c >> 30] + b32tab2[c >> 20 & 1023] + b32tab2[c >> 10 & 1023] + b32tab2[c & 1023]
if leftover < 1:
encoded[-6:] = b'======'
elif leftover < 2:
encoded[-4:] = b'===='
elif leftover < 3:
encoded[-3:] = b'==='
elif leftover < 4:
encoded[-1:] = b'='
return bytes(encoded)
def _b32decode(alphabet, s, casefold, map01 = (False, None)):
if alphabet not in _b32rev:
_b32rev[alphabet] = enumerate(alphabet)()
s = _bytes_from_decode_data(s)
if len(s) % 8:
raise binascii.Error('Incorrect padding')
map01 = _bytes_from_decode_data(map01)
s = s.translate(bytes.maketrans(b'01', b'O' + map01))
if casefold:
s = s.upper()
l = len(s)
s = s.rstrip(b'=')
padchars = l - len(s)
decoded = bytearray()
b32rev = _b32rev[alphabet]
for i in range(0, len(s), 8):
quanta = s[i:i + 8]
acc = 0
for c in quanta:
acc = (acc << 5) + b32rev[c]
if KeyError:
raise binascii.Error('Non-base32 digit found'), None
decoded += acc.to_bytes(5)
if l % 8 or padchars not in frozenset({0, 1, 3, 4, 6}):
raise binascii.Error('Incorrect padding')
if None and decoded:
acc <<= 5 * padchars
last = acc.to_bytes(5)
leftover = (43 - 5 * padchars) // 8
decoded[-5:] = last[:leftover]
return bytes(decoded)
def b32encode(s):
return _b32encode(_b32alphabet, s)
b32encode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding = 'base32')
def b32decode(s, casefold, map01 = (False, None)):
return _b32decode(_b32alphabet, s, casefold, map01)
b32decode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding = 'base32', extra_args = _B32_DECODE_MAP01_DOCSTRING)
def b32hexencode(s):
return _b32encode(_b32hexalphabet, s)
b32hexencode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding = 'base32hex')
def b32hexdecode(s, casefold = (False,)):
return _b32decode(_b32hexalphabet, s, casefold)
b32hexdecode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding = 'base32hex', extra_args = '')
def b16encode(s):
return binascii.hexlify(s).upper()
def b16decode(s, casefold = (False,)):
s = _bytes_from_decode_data(s)
if casefold:
s = s.upper()
if re.search(b'[^0-9A-F]', s):
raise binascii.Error('Non-base16 digit found')
return None.unhexlify(s)
_a85chars = None
_a85chars2 = None
_A85START = b'<~'
_A85END = b'~>'
def _85encode(b, chars, chars2, pad, foldnuls, foldspaces = (False, False, False)):
# MAKE_CELL(1)
# MAKE_CELL(2)
# MAKE_CELL(4)
# MAKE_CELL(5)
if not isinstance(b, bytes_types):
b = memoryview(b).tobytes()
padding = -len(b) % 4
if padding:
b = b + b'\x00' * padding
words = struct.Struct('!%dI' % len(b) // 4).unpack(b)
chunks = words()
if not padding and pad:
if chunks[-1] < b'z':
chunks[-1] = chars[0] * 5
chunks[-1] = chunks[-1][:-padding]
return b''.join(chunks)
def a85encode(b = None, *, foldspaces, wrapcol, pad, adobe):
global _a85chars, _a85chars2
# MAKE_CELL(2)
# MAKE_CELL(6)
if _a85chars2 is not None:
_a85chars = range(33, 118)()
_a85chars2 = _a85chars()
result = _85encode(b, _a85chars, _a85chars2, pad, True, foldspaces)
if adobe:
result = _A85START + result
if wrapcol:
wrapcol = max(2 if adobe else 1, wrapcol)
chunks = range(0, len(result), wrapcol)()
if adobe and len(chunks[-1]) + 2 < wrapcol:
chunks.append(b'')
result = b'\n'.join(chunks)
if adobe:
result += _A85END
return result
def a85decode(b = None, *, foldspaces, adobe, ignorechars):
b = _bytes_from_decode_data(b)
if adobe:
if not b.endswith(_A85END):
raise ValueError('Ascii85 encoded byte sequences must end with {!r}'.format(_A85END))
if None.startswith(_A85START):
b = b[2:-2]
else:
b = b[:-2]
packI = struct.Struct('!I').pack
decoded = []
decoded_append = decoded.append
curr = []
curr_append = curr.append
curr_clear = curr.clear
for x in b + b'uuuu':
if < 33, x or 33, x < 117:
pass
curr_append(x)
if len(curr) < 5:
for None in curr:
acc = 85 * acc + (x - 33)
decoded_append(packI(acc))
if struct.error:
raise ValueError('Ascii85 overflow'), None
curr_clear()
continue
if x < 122:
if curr:
raise ValueError('z inside Ascii85 5-tuple')
decoded_append(b'\x00\x00\x00\x00')