crc32 bug on newlisp 32-bit

Started by kosh, July 19, 2014, 11:17:33 PM

Previous topic - Next topic

kosh

crc32 function may return wrong value on 32-bit machine.


> (crc32 "hello")
907060870        ; OK
> (crc32 "HELLO")
-1052482506      ; NG [0xffffffffc1446436]
> (& (crc32 "HELLO") 0x00000000ffffffff)
3242484790       ; OK [0x00000000c144643]


This problem occurs because of difference between internal value and CELL value.



- upadte_crc function (in nl-math.c) returns unsigned 32-bit integer.

- But newlisp's printCell function treats as signed integer.



This solves the crc32 values are always treated as an 64-bit integer.


--- nl-math.c~ 2014-06-04 11:44:59 +0900
+++ nl-math.c 2014-06-20 22:31:07 +0900
@@ -2354,9 +2354,12 @@
 {
 char * data;
 size_t len;
+unsigned int crc;
 
 params = getStringSize(params, &data, &len, TRUE);
-return(stuffInteger(update_crc(0xffffffffL, (unsigned char *)data, (int)len) ^ 0xffffffffL));
+crc = update_crc(0xffffffffL, (unsigned char *)data, (int)len) ^ 0xffffffffL;
+return(stuffInteger64(crc));
 }

Lutz

#1
Thanks Kosh, for this discovery and the fix. Online here:



http://www.newlisp.org/downloads/development/inprogress/">http://www.newlisp.org/downloads/develo ... nprogress/">http://www.newlisp.org/downloads/development/inprogress/