]> gitweb.factorcode.org Git - factor.git/commitdiff
checksums.common: simplify add-checksum-bytes.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 10 Jul 2016 03:58:14 +0000 (20:58 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 10 Jul 2016 03:58:14 +0000 (20:58 -0700)
basis/checksums/common/common.factor

index d1cf83723f482594c36474038337a0721f85c24f..687a09386ebfaacf8d29e8de84b4b716c881235f 100644 (file)
@@ -41,29 +41,25 @@ 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' )
-    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 )
-    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 ;
+! 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
+    initial-len data length + block-size /mod :> ( n extra )
+    data checksum-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
+
+    blocks [
+        checksum-state [ block-size + ] change-bytes-read
+        checksum-block
+    ] each
+
+    checksum-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 ;