1 ! Copyright (C) 2006 Doug Coleman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: checksums io.binary kernel math sequences
7 CONSTANT: crc32-polynomial 0xedb88320
9 CONSTANT: crc32-table V{ }
13 [ 2/ ] [ even? ] bi [ crc32-polynomial bitxor ] unless
15 ] map 0 crc32-table copy
17 : (crc32) ( crc ch -- crc )
19 mask-byte crc32-table nth-unsafe
20 swap -8 shift bitxor ; inline
24 INSTANCE: crc32 checksum
26 : init-crc32 ( input checksum -- x y input )
27 drop [ 0xffffffff dup ] dip ; inline
29 : finish-crc32 ( x y -- bytes )
32 M: crc32 checksum-bytes
37 M: crc32 checksum-lines
39 [ [ (crc32) ] each CHAR: \n (crc32) ] each