]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/checksums/common/common.factor
factor: trim using lists
[factor.git] / basis / checksums / common / common.factor
index d1cf83723f482594c36474038337a0721f85c24f..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,62 +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 )
-
-: next-level ( n size -- n' )
-    2dup mod [ + ] [ - + ] if-zero ; inline
-
-! Update the bytes-read before calculating checksum in case checksum uses
-! this in the calculation.
-:: add-checksum-bytes ( state data -- state' )
+! Update the bytes-read before calculating checksum in case
+! checksum uses this in the calculation.
+M:: block-checksum-state add-checksum-bytes ( state data -- state )
     state block-size>> :> block-size
     state bytes>> length :> initial-len
-    data length :> data-len
-    initial-len data-len + :> total-len
-    total-len block-size /mod :> ( n extra )
+    initial-len data length + block-size /mod :> ( n extra )
     data state bytes>> [ push-all ] keep :> all-bytes
-    n zero? [
-        state [ data-len + ] change-bytes-read drop
-    ] [
-        all-bytes block-size <groups> [ length 64 = ] partition [
-            [ state [ block-size next-level ] change-bytes-read drop state checksum-block ] each
-            BV{ } clone state bytes<<
-        ] [
-            [
-                first
-                [ length state [ + ] change-bytes-read drop ]
-                [ >byte-vector state bytes<< ] bi
-            ] unless-empty
-        ] bi*
-    ] if state ;
+    all-bytes block-size <groups>
+    extra zero? [ f ] [ unclip-last-slice ] if :> ( blocks remain )
+
+    state [ initial-len - ] change-bytes-read drop
 
-: add-checksum-stream ( checksum-state stream -- checksum-state )
-    [ [ add-checksum-bytes ] each-block ] with-input-stream ;
+    blocks [
+        state [ block-size + ] change-bytes-read
+        checksum-block
+    ] each
 
-: add-checksum-file ( checksum-state path -- checksum-state )
-    binary <file-reader> add-checksum-stream ;
+    state [ extra + ] change-bytes-read
+    remain [ >byte-vector ] [ BV{ } clone ] if* >>bytes ;
 
 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 ;