On Mon, 10 Aug 2009, Szak�ts Viktor wrote:
Hi,
> Yes, the CRC16 is different indeed, but the CRC32 was the same, in
> fact (reading the old comments) the CRC32 code was taken from a generic
> unicode library, so it's nothing tpathy specific, before deletion
> I've verified the code table and formula to be the same as in core.
Any CRC tables can be easy generated using generic hb_crc() and hb_crcct()
functions I added to hbrtl, i.e. this code generates CRC tables for
HB_CRC32(), HB_CRC16() and TP_CRC16():
proc main()
local i, n
set alternate to crctabs.c
set alternate on
? "static const ULONG crc32_tab[ 256 ] = {"
for i := 0 to 255
if i % 5 == 0
? " "
endif
n := hb_bitxor( hb_crc( chr( i ), 0xFFFFFFFF, 0x104C11DB7 ), ;
0xFFFFFFFF )
?? " 0x" + hb_numtohex( n, 8 ) + "L" + iif( i < 255, ",", "" )
next
? "};"
?
? "static const ULONG crc16_tab[] ="
for i := 0 to 255
if i % 8 == 0
? " "
endif
n := hb_bitxor( hb_crc( chr( i ), 0xFFFF, 0x18005 ), 0xFFFF )
?? " 0x" + hb_numtohex( n, 4 ) + iif( i < 255, ",", "" )
next
? "};"
?
? "static const ULONG crctp16_tab[] ="
for i := 0 to 255
if i % 8 == 0
? " "
endif
n := hb_crcct( chr( i ),, 0x11021 )
?? " 0x" + hb_numtohex( n, 4 ) + iif( i < 255, ",", "" )
next
? "};"
return
> I've now checked original Telepath(y) libs and it uses "std" CRC32
> code with the same precalculated table. It's uses it for ZMODEM which
> needs std CRC32.
> Actually the correct name for CRC16 included in Telepath(y) lib should
> be CRC16-CCITT (or CRC-CCITT, or CRC16 X.25), which is a variation of
> CRC16 using different polynomial. It's used in XMODEM protocol.
It's not only different polynomial but also a little bit modified
algorithm used to calculate CRC so the results of HB_CRC() and HB_CRCCT()
are not equal even for the same polynomial.
> There is an HB_CRC() function in core which is supposed to be
> compatible with it, but I cannot get the same result.
> ---
> ? HB_CRC( "hello world" ) // -> 44550
> ? HB_CRCCT( "hello world" ) // -> 15332
> ? HB_CRC16( "hello world" ) // -> 8760
> ? TP_CRC16( "hello world" ) // -> 58427
> ? "Clipper original", 58427
> ---
You should use hb_crcct() with default 0x11021 polynomial
( 0x11021 => P(X) = X^16 + X^15 + X^5 + X^0 ) and you should
swap bytes in calculated result, i.e.:
func tp_crc16( s )
local n := hb_crcct( s )
return int( n / 256 ) + int( n % 256 * 256 )
best regards,
Przemek
_______________________________________________
Harbour mailing list
[email protected]
http://lists.harbour-project.org/mailman/listinfo/harbour