factor/core/checksums/crc32/crc32.factor

42 lines
917 B
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2006 Doug Coleman
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences sequences.private namespaces
words io io.binary io.files quotations
2008-04-30 17:11:55 -04:00
definitions checksums ;
IN: checksums.crc32
2007-09-20 18:09:08 -04:00
2009-02-22 20:09:49 -05:00
CONSTANT: crc32-polynomial HEX: edb88320
2007-09-20 18:09:08 -04:00
2009-02-22 20:09:49 -05:00
CONSTANT: crc32-table V{ }
2007-09-20 18:09:08 -04:00
256 iota [
2007-09-20 18:09:08 -04:00
8 [
[ 2/ ] [ even? ] bi [ crc32-polynomial bitxor ] unless
] times
] map 0 crc32-table copy
2007-09-20 18:09:08 -04:00
: (crc32) ( crc ch -- crc )
dupd bitxor
mask-byte crc32-table nth-unsafe
2007-09-20 18:09:08 -04:00
swap -8 shift bitxor ; inline
2008-04-30 17:11:55 -04:00
SINGLETON: crc32
2007-09-20 18:09:08 -04:00
2008-04-30 17:11:55 -04:00
INSTANCE: crc32 checksum
: init-crc32 ( input checksum -- x y input )
drop [ HEX: ffffffff dup ] dip ; inline
2008-04-30 17:11:55 -04:00
: finish-crc32 ( x y -- bytes )
bitxor 4 >be ; inline
2008-04-30 17:11:55 -04:00
M: crc32 checksum-bytes
init-crc32
[ (crc32) ] each
finish-crc32 ;
M: crc32 checksum-lines
init-crc32
[ [ (crc32) ] each CHAR: \n (crc32) ] each
finish-crc32 ;