]> gitweb.factorcode.org Git - factor.git/blob - core/checksums/crc32/crc32.factor
use radix literals
[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: kernel math sequences sequences.private namespaces
4 words io io.binary io.files quotations
5 definitions checksums ;
6 IN: checksums.crc32
7
8 CONSTANT: crc32-polynomial 0xedb88320
9
10 CONSTANT: crc32-table V{ }
11
12 256 iota [
13     8 [
14         [ 2/ ] [ even? ] bi [ crc32-polynomial bitxor ] unless
15     ] times
16 ] map 0 crc32-table copy
17
18 : (crc32) ( crc ch -- crc )
19     dupd bitxor
20     mask-byte crc32-table nth-unsafe
21     swap -8 shift bitxor ; inline
22
23 SINGLETON: crc32
24
25 INSTANCE: crc32 checksum
26
27 : init-crc32 ( input checksum -- x y input )
28     drop [ 0xffffffff dup ] dip ; inline
29
30 : finish-crc32 ( x y -- bytes )
31     bitxor 4 >be ; inline
32
33 M: crc32 checksum-bytes
34     init-crc32
35     [ (crc32) ] each
36     finish-crc32 ; inline
37
38 M: crc32 checksum-lines
39     init-crc32
40     [ [ (crc32) ] each CHAR: \n (crc32) ] each
41     finish-crc32 ; inline