]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/checksums/common/common.factor
factor: trim using lists
[factor.git] / basis / checksums / common / common.factor
index 69d6c3ed9def0e54a0b1d4eada26afc86796bd32..d313b238d70f8f3267ef096e5278b81d3bdafc15 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2006, 2008 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: byte-arrays grouping io.binary kernel make math ;
+USING: accessors byte-arrays byte-vectors checksums endian
+grouping kernel make math sequences ;
 IN: checksums.common
 
 : calculate-pad-length ( length -- length' )
@@ -12,3 +13,39 @@ IN: checksums.common
         [ 0x3f bitand calculate-pad-length <byte-array> % ]
         [ 3 shift 8 rot [ >be ] [ >le ] if % ] bi
     ] B{ } make 64 group ;
+
+MIXIN: block-checksum
+
+INSTANCE: block-checksum checksum
+
+TUPLE: block-checksum-state < checksum-state
+    { bytes-read integer }
+    { block-size integer } ;
+
+GENERIC: checksum-block ( bytes checksum-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
+    initial-len data length + block-size /mod :> ( n extra )
+    data state bytes>> [ push-all ] keep :> all-bytes
+    all-bytes block-size <groups>
+    extra zero? [ f ] [ unclip-last-slice ] if :> ( blocks remain )
+
+    state [ initial-len - ] change-bytes-read drop
+
+    blocks [
+        state [ block-size + ] change-bytes-read
+        checksum-block
+    ] each
+
+    state [ extra + ] change-bytes-read
+    remain [ >byte-vector ] [ BV{ } clone ] if* >>bytes ;
+
+M: block-checksum checksum-bytes
+    [ swap add-checksum-bytes get-checksum ] with-checksum-state ;
+
+M: block-checksum checksum-stream
+    [ swap add-checksum-stream get-checksum ] with-checksum-state ;