! Copyright (C) 2006, 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors byte-arrays byte-vectors checksums destructors
-grouping io io.backend io.binary io.encodings.binary io.files
-kernel make math sequences locals ;
+USING: accessors byte-arrays byte-vectors checksums grouping
+io.binary kernel locals make math sequences ;
IN: checksums.common
: calculate-pad-length ( length -- length' )
{ bytes-read integer }
{ block-size integer } ;
-M: block-checksum-state dispose drop ;
-
GENERIC: checksum-block ( bytes checksum-state -- )
! Update the bytes-read before calculating checksum in case
remain [ >byte-vector ] [ BV{ } clone ] if* >>bytes ;
M: block-checksum checksum-bytes
- initialize-checksum-state [
- swap add-checksum-bytes get-checksum
- ] with-disposal ;
+ [ swap add-checksum-bytes get-checksum ] with-checksum-state ;
M: block-checksum checksum-stream
- initialize-checksum-state [
- swap add-checksum-stream get-checksum
- ] with-disposal ;
+ [ swap add-checksum-stream get-checksum ] with-checksum-state ;
! Copyright (c) 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors byte-vectors io io.backend io.encodings.binary
-io.files io.streams.byte-array kernel sequences ;
+USING: accessors byte-vectors destructors io io.backend
+io.encodings.binary io.files io.streams.byte-array kernel
+sequences ;
IN: checksums
MIXIN: checksum
TUPLE: checksum-state checksum { bytes byte-vector } ;
+M: checksum-state dispose drop ;
+
M: checksum-state clone
call-next-method
[ clone ] change-bytes ;
GENERIC# add-checksum-bytes 1 ( checksum-state data -- checksum-state )
GENERIC: get-checksum ( checksum-state -- value )
+: with-checksum-state ( ... checksum quot: ( ... checksum-state -- ..b ) -- ..b value )
+ [ initialize-checksum-state ] dip with-disposal ; inline
+
: add-checksum-stream ( checksum-state stream -- checksum-state )
[ [ add-checksum-bytes ] each-block ] with-input-stream ;