From: Serhiy Storchaka Date: Tue, 28 May 2013 12:30:38 +0000 (+0300) Subject: Issue #18011: base64.b32decode() now raises a binascii.Error if there are X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=77a3ad743f604c11c23ec6c0100360675b5676f8;p=python Issue #18011: base64.b32decode() now raises a binascii.Error if there are non-alphabet characters present in the input string to conform a docstring. Updated the module documentation. --- 77a3ad743f604c11c23ec6c0100360675b5676f8 diff --cc Lib/base64.py index 0f731fa043,b6e82b69bd..f923244104 --- a/Lib/base64.py +++ b/Lib/base64.py @@@ -209,36 -232,42 +209,36 @@@ def b32decode(s, casefold=False, map01= # Strip off pad characters from the right. We need to count the pad # characters because this will tell us how many null bytes to remove from # the end of the decoded string. - padchars = 0 - mo = re.search(b'(?P[=]*)$', s) - if mo: - padchars = len(mo.group('pad')) - if padchars > 0: - s = s[:-padchars] + l = len(s) + s = s.rstrip(b'=') + padchars = l - len(s) # Now decode the full quanta - parts = [] - acc = 0 - shift = 35 - for c in s: - val = _b32rev.get(c) - if val is None: + decoded = bytearray() + b32rev = _b32rev + for i in range(0, len(s), 8): + quanta = s[i: i + 8] + acc = 0 + try: + for c in quanta: + acc = (acc << 5) + b32rev[c] + except KeyError: - raise TypeError('Non-base32 digit found') + raise binascii.Error('Non-base32 digit found') - acc += _b32rev[c] << shift - shift -= 5 - if shift < 0: - parts.append(binascii.unhexlify(bytes('%010x' % acc, "ascii"))) - acc = 0 - shift = 35 + decoded += acc.to_bytes(5, 'big') # Process the last, partial quanta - last = binascii.unhexlify(bytes('%010x' % acc, "ascii")) - if padchars == 0: - last = b'' # No characters - elif padchars == 1: - last = last[:-1] - elif padchars == 3: - last = last[:-2] - elif padchars == 4: - last = last[:-3] - elif padchars == 6: - last = last[:-4] - else: - raise binascii.Error('Incorrect padding') - parts.append(last) - return b''.join(parts) + if padchars: + acc <<= 5 * padchars + last = acc.to_bytes(5, 'big') + if padchars == 1: + decoded[-5:] = last[:-1] + elif padchars == 3: + decoded[-5:] = last[:-2] + elif padchars == 4: + decoded[-5:] = last[:-3] + elif padchars == 6: + decoded[-5:] = last[:-4] + else: + raise binascii.Error('Incorrect padding') + return bytes(decoded) diff --cc Misc/NEWS index fd2978f360,41bc85645a..04e9d15470 --- a/Misc/NEWS +++ b/Misc/NEWS @@@ -103,47 -127,6 +103,51 @@@ Core and Builtin Library ------- ++- Issue #18011: base64.b32decode() now raises a binascii.Error if there are ++ non-alphabet characters present in the input string to conform a docstring. ++ Updated the module documentation. ++ +- Issue #18072: Implement importlib.abc.InspectLoader.get_code() and + importlib.abc.ExecutionLoader.get_code(). + +- Issue #8240: Set the SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER flag on SSL + sockets. + +- Issue #17269: Workaround for socket.getaddrinfo crash on MacOS X + with port None or "0" and flags AI_NUMERICSERV. + +- Issue #16986: ElementTree now correctly parses a string input not only when + an internal XML encoding is UTF-8 or US-ASCII. + +- Issue #17996: socket module now exposes AF_LINK constant on BSD and OSX. + +- Issue #17900: Allowed pickling of recursive OrderedDicts. Decreased pickled + size and pickling time. + +- Issue #17914: Add os.cpu_count(). Patch by Yogesh Chaudhari, based on an + initial patch by Trent Nelson. + +- Issue #17812: Fixed quadratic complexity of base64.b32encode(). + Optimize base64.b32encode() and base64.b32decode() (speed up to 3x). + +- Issue #17980: Fix possible abuse of ssl.match_hostname() for denial of + service using certificates with many wildcards (CVE-2013-2099). + +- Issue #15758: Fix FileIO.readall() so it no longer has O(n**2) complexity. + +- Issue #14596: The struct.Struct() objects now use more compact implementation. + +- Issue #17981: Closed socket on error in SysLogHandler. + +- Issue #17964: Fix os.sysconf(): the return type of the C sysconf() function + is long, not int. + +- Fix typos in the multiprocessing module. + +- Issue #17754: Make ctypes.util.find_library() independent of the locale. + +- Issue #17968: Fix memory leak in os.listxattr(). + - Issue #17606: Fixed support of encoded byte strings in the XMLGenerator characters() and ignorableWhitespace() methods. Original patch by Sebastian Ortiz Vasquez.