1 ! Copyright (C) 2016 Alexander Ilin.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: checksums endian kernel literals math sequences sequences.private ;
7 CONSTANT: crc16-polynomial 0xa001
10 CONSTANT: crc16-table $[
13 [ 2/ ] [ even? ] bi [ crc16-polynomial bitxor ] unless
18 : (crc16) ( crc ch -- crc )
20 0xff bitand crc16-table nth-unsafe
21 swap -8 shift bitxor ; inline
25 INSTANCE: crc16 checksum
27 : init-crc16 ( input checksum -- x input )
28 drop [ 0xffff ] dip ; inline
30 : finish-crc16 ( x -- bytes )
33 M: crc16 checksum-bytes
38 M: crc16 checksum-lines
40 [ [ (crc16) ] each CHAR: \n (crc16) ] each