[pypy-svn] r60948 - in pypy/trunk/pypy/lib: . app_test

fijal at codespeak.net fijal at codespeak.net
Wed Jan 14 13:24:25 CET 2009


Author: fijal
Date: Wed Jan 14 13:24:21 2009
New Revision: 60948

Modified:
   pypy/trunk/pypy/lib/app_test/test_binascii.py
   pypy/trunk/pypy/lib/binascii.py
Log:
* make a2b_base64 not O(n^2)
* try to repeat when to report an incorrect padding


Modified: pypy/trunk/pypy/lib/app_test/test_binascii.py
==============================================================================
--- pypy/trunk/pypy/lib/app_test/test_binascii.py	(original)
+++ pypy/trunk/pypy/lib/app_test/test_binascii.py	Wed Jan 14 13:24:21 2009
@@ -148,6 +148,5 @@
     binascii.crc_hqx('', 0)
 
 def test_wrong_padding():
-    skip("fails")
     s = 'CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3IQ'
     raises(binascii.Error, binascii.a2b_base64, s)

Modified: pypy/trunk/pypy/lib/binascii.py
==============================================================================
--- pypy/trunk/pypy/lib/binascii.py	(original)
+++ pypy/trunk/pypy/lib/binascii.py	Wed Jan 14 13:24:21 2009
@@ -136,22 +136,36 @@
 def a2b_base64(s):
     s = s.rstrip()
     # clean out all invalid characters, this also strips the final '=' padding
+    # check for correct padding
+    if not s or s.startswith('='):
+        count = 0
+    else:
+        count = 0
+        while s[len(s)-count-1] == '=':
+            count += 1
     clean_s = []
     for item in s:
         if item in table_a2b_base64:
             clean_s.append(item)
+    if len(clean_s) % 4 == 1:
+        if count != 2:
+            raise Error("incorrect padding")
+    if len(clean_s) % 4 == 2:
+        if count != 1:
+            raise Error("incorrect padding")
     s = ''.join(clean_s)
-    # Add '=' padding back into the string
     if len(s) % 4:
         s = s + ('=' * (4 - len(s) % 4))
+
+    # Add '=' padding back into the string
      
     def quadruplets_gen(s):
-        while s:
+        l = [s[i:i+4] for i in range(0, len(s), 4)]
+        for s in l:
             yield (table_a2b_base64[s[0]],
                    table_a2b_base64[s[1]],
                    table_a2b_base64[s[2]],
                    table_a2b_base64[s[3]])
-            s = s[4:]
 
     result = [
         chr(A << 2 | ((B >> 4) & 0x3)) + 



More information about the Pypy-commit mailing list