1 ! Copyright (C) 2016 Alexander Ilin.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: checksums io.binary kernel math sequences
7 CONSTANT: crc16-polynomial 0xa001
9 CONSTANT: crc16-table V{ }
13 [ 2/ ] [ even? ] bi [ crc16-polynomial bitxor ] unless
15 ] map 0 crc16-table copy
17 : (crc16) ( crc ch -- crc )
19 mask-byte crc16-table nth-unsafe
20 swap -8 shift bitxor ; inline
24 INSTANCE: crc16 checksum
26 : init-crc16 ( input checksum -- x input )
27 drop [ 0xffff ] dip ; inline
29 : finish-crc16 ( x -- bytes )
32 M: crc16 checksum-bytes
37 M: crc16 checksum-lines
39 [ [ (crc16) ] each CHAR: \n (crc16) ] each