1 ! Copyright (C) 2006 Doug Coleman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences sequences.private namespaces
4 words io io.binary io.files io.streams.string quotations
5 definitions checksums ;
8 : crc32-polynomial HEX: edb88320 ; inline
10 : crc32-table V{ } ; inline
14 [ 2/ ] [ even? ] bi [ crc32-polynomial bitxor ] unless
16 ] map 0 crc32-table copy
18 : (crc32) ( crc ch -- crc )
20 mask-byte crc32-table nth-unsafe >bignum
21 swap -8 shift bitxor ; inline
25 INSTANCE: crc32 checksum
27 : init-crc32 ( input checksum -- x y input )
28 drop [ HEX: ffffffff dup ] dip ; inline
30 : finish-crc32 ( x y -- bytes )
33 M: crc32 checksum-bytes
38 M: crc32 checksum-lines
40 [ [ (crc32) ] each CHAR: \n (crc32) ] each