Commits

Armin Rigo committed 496810c

Share the code rather than duplicate it, for the crc32.

Comments (0)

Files changed (2)

pypy/module/binascii/interp_crc32.py

 from pypy.interpreter.gateway import unwrap_spec
+from rpython.rtyper.lltypesystem import rffi
 from rpython.rlib.rarithmetic import r_uint, intmask
-from rpython.rtyper.lltypesystem import rffi
-from rpython.rlib.rzipfile import crc_32_tab
+from rpython.rlib import rzipfile
 
 @unwrap_spec(data='bufferstr', oldcrc='truncatedint_w')
 def crc32(space, data, oldcrc=0):
     "Compute the CRC-32 incrementally."
 
-    crc = r_uint(rffi.cast(rffi.UINT, ~oldcrc))   # signed => 32-bit unsigned
-
-    # in the following loop, we have always 0 <= crc < 2**32
-    for c in data:
-        crc = crc_32_tab[(crc & 0xff) ^ ord(c)] ^ (crc >> 8)
-
-    crc = ~intmask(rffi.cast(rffi.INT, crc))   # unsigned => 32-bit signed
-    return space.wrap(crc)
+    crc = rzipfile.crc32(data, r_uint(oldcrc))
+    crc = rffi.cast(rffi.INT, crc)    # unsigned => 32-bit signed
+    return space.wrap(intmask(crc))

rpython/rlib/rzipfile.py

 ]
 crc_32_tab = map(r_uint, crc_32_tab)
 
-# XXX hack to get crc32 to work
-
-rcrc_32_tab = [r_uint(i) for i in crc_32_tab]
-
-def crc32(s, crc=0):
-    result = 0
-    crc = ~r_uint(crc) & r_uint(0xffffffffL)
+def crc32(s, crc=r_uint(0)):
+    crc = ~crc & r_uint(0xffffffffL)
     for c in s:
-        crc = rcrc_32_tab[(crc ^ r_uint(ord(c))) & 0xffL] ^ (crc >> 8)
+        crc = crc_32_tab[(crc ^ r_uint(ord(c))) & 0xffL] ^ (crc >> 8)
         #/* Note:  (crc >> 8) MUST zero fill on left
-
-        result = crc ^ r_uint(0xffffffffL)
-
-    return result
+    return crc ^ r_uint(0xffffffffL)
 
 # parts copied from zipfile library implementation
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.