@@ -209,51 +209,8 @@ def urlsafe_b64decode(s):
209209the letter O). For security purposes the default is None, so that
2102100 and 1 are not allowed in the input.
211211'''
212- _b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
213- _b32hexalphabet = b'0123456789ABCDEFGHIJKLMNOPQRSTUV'
214- _b32tab2 = {}
215- _b32rev = {}
216-
217- def _b32encode (alphabet , s ):
218- # Delay the initialization of the table to not waste memory
219- # if the function is never called
220- if alphabet not in _b32tab2 :
221- b32tab = [bytes ((i ,)) for i in alphabet ]
222- _b32tab2 [alphabet ] = [a + b for a in b32tab for b in b32tab ]
223- b32tab = None
224-
225- if not isinstance (s , bytes_types ):
226- s = memoryview (s ).tobytes ()
227- leftover = len (s ) % 5
228- # Pad the last quantum with zero bits if necessary
229- if leftover :
230- s = s + b'\0 ' * (5 - leftover ) # Don't use += !
231- encoded = bytearray ()
232- from_bytes = int .from_bytes
233- b32tab2 = _b32tab2 [alphabet ]
234- for i in range (0 , len (s ), 5 ):
235- c = from_bytes (s [i : i + 5 ]) # big endian
236- encoded += (b32tab2 [c >> 30 ] + # bits 1 - 10
237- b32tab2 [(c >> 20 ) & 0x3ff ] + # bits 11 - 20
238- b32tab2 [(c >> 10 ) & 0x3ff ] + # bits 21 - 30
239- b32tab2 [c & 0x3ff ] # bits 31 - 40
240- )
241- # Adjust for any leftover partial quanta
242- if leftover == 1 :
243- encoded [- 6 :] = b'======'
244- elif leftover == 2 :
245- encoded [- 4 :] = b'===='
246- elif leftover == 3 :
247- encoded [- 3 :] = b'==='
248- elif leftover == 4 :
249- encoded [- 1 :] = b'='
250- return encoded .take_bytes ()
251-
252- def _b32decode (alphabet , s , casefold = False , map01 = None ):
253- # Delay the initialization of the table to not waste memory
254- # if the function is never called
255- if alphabet not in _b32rev :
256- _b32rev [alphabet ] = {v : k for k , v in enumerate (alphabet )}
212+
213+ def _b32decode_prepare (s , casefold = False , map01 = None ):
257214 s = _bytes_from_decode_data (s )
258215 if len (s ) % 8 :
259216 raise binascii .Error ('Incorrect padding' )
@@ -266,51 +223,27 @@ def _b32decode(alphabet, s, casefold=False, map01=None):
266223 s = s .translate (bytes .maketrans (b'01' , b'O' + map01 ))
267224 if casefold :
268225 s = s .upper ()
269- # Strip off pad characters from the right. We need to count the pad
270- # characters because this will tell us how many null bytes to remove from
271- # the end of the decoded string.
272- l = len (s )
273- s = s .rstrip (b'=' )
274- padchars = l - len (s )
275- # Now decode the full quanta
276- decoded = bytearray ()
277- b32rev = _b32rev [alphabet ]
278- for i in range (0 , len (s ), 8 ):
279- quanta = s [i : i + 8 ]
280- acc = 0
281- try :
282- for c in quanta :
283- acc = (acc << 5 ) + b32rev [c ]
284- except KeyError :
285- raise binascii .Error ('Non-base32 digit found' ) from None
286- decoded += acc .to_bytes (5 ) # big endian
287- # Process the last, partial quanta
288- if l % 8 or padchars not in {0 , 1 , 3 , 4 , 6 }:
289- raise binascii .Error ('Incorrect padding' )
290- if padchars and decoded :
291- acc <<= 5 * padchars
292- last = acc .to_bytes (5 ) # big endian
293- leftover = (43 - 5 * padchars ) // 8 # 1: 4, 3: 3, 4: 2, 6: 1
294- decoded [- 5 :] = last [:leftover ]
295- return decoded .take_bytes ()
226+ return s
296227
297228
298229def b32encode (s ):
299- return _b32encode ( _b32alphabet , s )
230+ return binascii . b2a_base32 ( s )
300231b32encode .__doc__ = _B32_ENCODE_DOCSTRING .format (encoding = 'base32' )
301232
302233def b32decode (s , casefold = False , map01 = None ):
303- return _b32decode (_b32alphabet , s , casefold , map01 )
234+ s = _b32decode_prepare (s , casefold , map01 )
235+ return binascii .a2b_base32 (s )
304236b32decode .__doc__ = _B32_DECODE_DOCSTRING .format (encoding = 'base32' ,
305237 extra_args = _B32_DECODE_MAP01_DOCSTRING )
306238
307239def b32hexencode (s ):
308- return _b32encode ( _b32hexalphabet , s )
240+ return binascii . b2a_base32hex ( s )
309241b32hexencode .__doc__ = _B32_ENCODE_DOCSTRING .format (encoding = 'base32hex' )
310242
311243def b32hexdecode (s , casefold = False ):
312244 # base32hex does not have the 01 mapping
313- return _b32decode (_b32hexalphabet , s , casefold )
245+ s = _b32decode_prepare (s , casefold )
246+ return binascii .a2b_base32hex (s )
314247b32hexdecode .__doc__ = _B32_DECODE_DOCSTRING .format (encoding = 'base32hex' ,
315248 extra_args = '' )
316249
0 commit comments