2008-04-30 17:11:55 -04:00
|
|
|
! Copyright (c) 2008 Slava Pestov
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2014-10-27 16:39:08 -04:00
|
|
|
USING: accessors byte-arrays byte-vectors io io.backend io.files
|
|
|
|
kernel math math.parser sequences ;
|
2008-04-30 17:11:55 -04:00
|
|
|
IN: checksums
|
|
|
|
|
|
|
|
MIXIN: checksum
|
|
|
|
|
2009-05-18 01:24:24 -04:00
|
|
|
TUPLE: checksum-state
|
2014-02-17 21:25:51 -05:00
|
|
|
{ bytes-read integer }
|
|
|
|
{ block-size integer }
|
|
|
|
{ bytes byte-vector } ;
|
2009-05-16 14:03:09 -04:00
|
|
|
|
2009-05-17 13:45:20 -04:00
|
|
|
: new-checksum-state ( class -- checksum-state )
|
2009-05-16 14:03:09 -04:00
|
|
|
new
|
2009-05-18 01:24:24 -04:00
|
|
|
BV{ } clone >>bytes ; inline
|
2009-05-16 14:03:09 -04:00
|
|
|
|
2009-05-16 19:00:56 -04:00
|
|
|
M: checksum-state clone
|
|
|
|
call-next-method
|
|
|
|
[ clone ] change-bytes ;
|
|
|
|
|
2014-02-17 21:25:51 -05:00
|
|
|
GENERIC: initialize-checksum-state ( checksum -- checksum-state )
|
2009-05-17 13:45:20 -04:00
|
|
|
|
2015-12-30 12:40:58 -05:00
|
|
|
GENERIC: checksum-block ( bytes checksum-state -- )
|
2009-05-16 14:03:09 -04:00
|
|
|
|
2015-12-30 12:40:58 -05:00
|
|
|
GENERIC: get-checksum ( checksum-state -- value )
|
2009-05-16 14:03:09 -04:00
|
|
|
|
|
|
|
: add-checksum-bytes ( checksum-state data -- checksum-state )
|
|
|
|
over bytes>> [ push-all ] keep
|
|
|
|
[ dup length pick block-size>> >= ]
|
|
|
|
[
|
2014-02-17 21:25:51 -05:00
|
|
|
over block-size>> cut-slice [
|
2015-12-30 12:40:58 -05:00
|
|
|
over checksum-block
|
2014-02-17 21:25:51 -05:00
|
|
|
[ block-size>> ] keep [ + ] change-bytes-read
|
2009-05-16 14:03:09 -04:00
|
|
|
] dip
|
2009-05-18 01:24:24 -04:00
|
|
|
] while
|
|
|
|
>byte-vector
|
|
|
|
[ >>bytes ] [ length [ + ] curry change-bytes-read ] bi ;
|
2009-05-16 14:03:09 -04:00
|
|
|
|
|
|
|
: add-checksum-stream ( checksum-state stream -- checksum-state )
|
2015-12-30 12:40:58 -05:00
|
|
|
[ [ add-checksum-bytes ] each-block ] with-input-stream ;
|
2009-05-16 14:03:09 -04:00
|
|
|
|
|
|
|
: add-checksum-file ( checksum-state path -- checksum-state )
|
2009-05-17 19:05:46 -04:00
|
|
|
normalize-path (file-reader) add-checksum-stream ;
|
2009-05-16 14:03:09 -04:00
|
|
|
|
2008-04-30 17:11:55 -04:00
|
|
|
GENERIC: checksum-bytes ( bytes checksum -- value )
|
|
|
|
|
|
|
|
GENERIC: checksum-stream ( stream checksum -- value )
|
|
|
|
|
|
|
|
GENERIC: checksum-lines ( lines checksum -- value )
|
|
|
|
|
2008-11-23 03:44:56 -05:00
|
|
|
M: checksum checksum-stream
|
2009-05-01 11:41:27 -04:00
|
|
|
[ stream-contents ] dip checksum-bytes ;
|
2008-04-30 17:11:55 -04:00
|
|
|
|
2008-11-23 03:44:56 -05:00
|
|
|
M: checksum checksum-lines
|
|
|
|
[ B{ CHAR: \n } join ] dip checksum-bytes ;
|
2008-04-30 17:11:55 -04:00
|
|
|
|
2008-05-01 21:02:34 -04:00
|
|
|
: checksum-file ( path checksum -- value )
|
2015-09-08 19:15:10 -04:00
|
|
|
! normalize-path (file-reader) is equivalent to
|
|
|
|
! binary <file-reader>. We use the lower-level form
|
|
|
|
! so that we can move io.encodings.binary to basis/.
|
2008-12-14 21:03:00 -05:00
|
|
|
[ normalize-path (file-reader) ] dip checksum-stream ;
|
2008-04-30 17:11:55 -04:00
|
|
|
|
|
|
|
: hex-string ( seq -- str )
|
2009-01-29 23:19:07 -05:00
|
|
|
[ >hex 2 CHAR: 0 pad-head ] { } map-as concat ;
|