Info-ZIPのCRC32とzlibのcrc32でなぜか戻り値が違う。
printf("%x", CRC32(305419896L, 'x')); //=> 123456 printf("%x", crc32(305419896L, "x", 1)); //=> d210dbdb
で、zlibのcrc32のソースを読む。
/* ========================================================================= */ unsigned long ZEXPORT crc32(crc, buf, len) unsigned long crc; const unsigned char FAR *buf; unsigned len; { if (buf == Z_NULL) return 0UL; #ifdef DYNAMIC_CRC_TABLE if (crc_table_empty) make_crc_table(); #endif /* DYNAMIC_CRC_TABLE */ #ifdef BYFOUR if (sizeof(void *) == sizeof(ptrdiff_t)) { u4 endian; endian = 1; if (*((unsigned char *)(&endian))) return crc32_little(crc, buf, len); else return crc32_big(crc, buf, len); } #endif /* BYFOUR */ crc = crc ^ 0xffffffffUL; while (len >= 8) { DO8; len -= 8; } if (len) do { DO1; } while (--len); return crc ^ 0xffffffffUL; }
crc_tableテーブルは同じだけど、0xffffffffULでXORしてるのが原因みたい。
次のようにしたら、同じ戻り値になった。
printf("%x", CRC32(305419896L, 'x')); //=> 123456 printf("%x", crc32(305419896L ^ 0xffffffffUL, "x", 1) ^ 0xffffffffUL); //=> 123456
0xffffffffULはなんなんだろう?
・・・というかZipのcrc32はなんなんだって考えるべきなんだろうなー。