Skip to content

Make b64decode with validate=True faster by compiling regex #11634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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 number Diff line number Diff 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 Down Expand 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