]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/checksums/common/common.factor
factor: trim using lists
[factor.git] / basis / checksums / common / common.factor
index 2f24b3a54ac16814bd5a7184a1e3a5f707f04f48..d313b238d70f8f3267ef096e5278b81d3bdafc15 100644 (file)
@@ -1,8 +1,7 @@
 ! Copyright (C) 2006, 2008 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors byte-arrays byte-vectors checksums grouping io
-io.backend io.binary io.encodings.binary io.files kernel make
-math sequences locals ;
+USING: accessors byte-arrays byte-vectors checksums endian
+grouping kernel make math sequences ;
 IN: checksums.common
 
 : calculate-pad-length ( length -- length' )
@@ -19,55 +18,34 @@ MIXIN: block-checksum
 
 INSTANCE: block-checksum checksum
 
-TUPLE: checksum-state
-{ bytes-read integer }
-{ block-size integer }
-{ bytes byte-vector } ;
-
-: new-checksum-state ( class -- checksum-state )
-    new
-        BV{ } clone >>bytes ; inline
-
-M: checksum-state clone
-    call-next-method
-    [ clone ] change-bytes ;
-
-GENERIC: initialize-checksum-state ( checksum -- checksum-state )
+TUPLE: block-checksum-state < checksum-state
+    { bytes-read integer }
+    { block-size integer } ;
 
 GENERIC: checksum-block ( bytes checksum-state -- )
 
-GENERIC: get-checksum ( checksum-state -- value )
-
 ! Update the bytes-read before calculating checksum in case
 ! checksum uses this in the calculation.
-:: add-checksum-bytes ( checksum-state data -- checksum-state' )
-    checksum-state block-size>> :> block-size
-    checksum-state bytes>> length :> initial-len
+M:: block-checksum-state add-checksum-bytes ( state data -- state )
+    state block-size>> :> block-size
+    state bytes>> length :> initial-len
     initial-len data length + block-size /mod :> ( n extra )
-    data checksum-state bytes>> [ push-all ] keep :> all-bytes
+    data state bytes>> [ push-all ] keep :> all-bytes
     all-bytes block-size <groups>
     extra zero? [ f ] [ unclip-last-slice ] if :> ( blocks remain )
 
-    checksum-state [ initial-len - ] change-bytes-read drop
+    state [ initial-len - ] change-bytes-read drop
 
     blocks [
-        checksum-state [ block-size + ] change-bytes-read
+        state [ block-size + ] change-bytes-read
         checksum-block
     ] each
 
-    checksum-state [ extra + ] change-bytes-read
+    state [ extra + ] change-bytes-read
     remain [ >byte-vector ] [ BV{ } clone ] if* >>bytes ;
 
-: add-checksum-stream ( checksum-state stream -- checksum-state )
-    [ [ add-checksum-bytes ] each-block ] with-input-stream ;
-
-: add-checksum-file ( checksum-state path -- checksum-state )
-    binary <file-reader> add-checksum-stream ;
-
 M: block-checksum checksum-bytes
-    initialize-checksum-state
-    swap add-checksum-bytes get-checksum ;
+    [ swap add-checksum-bytes get-checksum ] with-checksum-state ;
 
 M: block-checksum checksum-stream
-    initialize-checksum-state
-    swap add-checksum-stream get-checksum ;
+    [ swap add-checksum-stream get-checksum ] with-checksum-state ;