1 ! Copyright (C) 2006 Doug Coleman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: checksums kernel math sequences sequences.private ;
6 CONSTANT: crc32-polynomial 0xedb88320
8 CONSTANT: crc32-table V{ }
12 [ 2/ ] [ even? ] bi [ crc32-polynomial bitxor ] unless
14 ] map 0 crc32-table copy
16 : (crc32) ( crc ch -- crc )
18 0xff bitand crc32-table nth-unsafe
19 swap -8 shift bitxor ; inline
23 INSTANCE: crc32 checksum
25 : init-crc32 ( input checksum -- x y input )
26 drop [ 0xffffffff dup ] dip ; inline
29 : 4>be ( n -- byte-array ) ! duplicated from io.binary, but in core
30 { -24 -16 -8 0 } [ shift 0xff bitand ] with B{ } map-as ;
33 : finish-crc32 ( x y -- bytes )
36 M: crc32 checksum-bytes
41 M: crc32 checksum-lines
43 [ [ (crc32) ] each CHAR: \n (crc32) ] each