]> gitweb.factorcode.org Git - factor.git/blob - core/checksums/crc32/crc32.factor
f6f6870041649d54e588e64c2c9aa19c5b0ff881
[factor.git] / core / checksums / crc32 / crc32.factor
1 ! Copyright (C) 2006 Doug Coleman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: checksums io.binary kernel math sequences
4 sequences.private ;
5 IN: checksums.crc32
6
7 CONSTANT: crc32-polynomial 0xedb88320
8
9 CONSTANT: crc32-table V{ }
10
11 256 <iota> [
12     8 [
13         [ 2/ ] [ even? ] bi [ crc32-polynomial bitxor ] unless
14     ] times
15 ] map 0 crc32-table copy
16
17 : (crc32) ( crc ch -- crc )
18     dupd bitxor
19     mask-byte crc32-table nth-unsafe
20     swap -8 shift bitxor ; inline
21
22 SINGLETON: crc32
23
24 INSTANCE: crc32 checksum
25
26 : init-crc32 ( input checksum -- x y input )
27     drop [ 0xffffffff dup ] dip ; inline
28
29 : finish-crc32 ( x y -- bytes )
30     bitxor 4 >be ; inline
31
32 M: crc32 checksum-bytes
33     init-crc32
34     [ (crc32) ] each
35     finish-crc32 ; inline
36
37 M: crc32 checksum-lines
38     init-crc32
39     [ [ (crc32) ] each CHAR: \n (crc32) ] each
40     finish-crc32 ; inline