1 ! Copyright (C) 2016 Alexander Ilin.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: checksums endian kernel math sequences sequences.private ;
6 CONSTANT: crc16-polynomial 0xa001
8 CONSTANT: crc16-table V{ }
12 [ 2/ ] [ even? ] bi [ crc16-polynomial bitxor ] unless
14 ] map 0 crc16-table copy
16 : (crc16) ( crc ch -- crc )
18 0xff bitand crc16-table nth-unsafe
19 swap -8 shift bitxor ; inline
23 INSTANCE: crc16 checksum
25 : init-crc16 ( input checksum -- x input )
26 drop [ 0xffff ] dip ; inline
28 : finish-crc16 ( x -- bytes )
31 M: crc16 checksum-bytes
36 M: crc16 checksum-lines
38 [ [ (crc16) ] each CHAR: \n (crc16) ] each