Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Lib/base64.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,9 +28,17 @@
'urlsafe_b64encode', 'urlsafe_b64decode',
]


VALID_BASE64_REGEX = None
bytes_types = (bytes, bytearray) # Types acceptable as binary data

def _get_valid_base64_regex():
global VALID_BASE64_REGEX
if VALID_BASE64_REGEX:
return VALID_BASE64_REGEX

VALID_BASE64_REGEX = re.compile(b'^[A-Za-z0-9+/]*={0,2}$')
return VALID_BASE64_REGEX

def _bytes_from_decode_data(s):
if isinstance(s, str):
try:
Expand DownExpand Up@@ -82,7 +90,7 @@ def b64decode(s, altchars=None, validate=False):
altchars = _bytes_from_decode_data(altchars)
assert len(altchars) == 2, repr(altchars)
s = s.translate(bytes.maketrans(altchars, b'+/'))
if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s):
if validate and not _get_valid_base64_regex().match(s):
raise binascii.Error('Non-base64 digit found')
return binascii.a2b_base64(s)

Expand Down