From 6b9d6c00c0574f7ae767ffa217b4832a3f9f5f2c Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sat, 9 Jul 2016 20:58:14 -0700 Subject: [PATCH] checksums.common: simplify add-checksum-bytes. --- basis/checksums/common/common.factor | 42 +++++++++++++--------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/basis/checksums/common/common.factor b/basis/checksums/common/common.factor index d1cf83723f..687a09386e 100644 --- a/basis/checksums/common/common.factor +++ b/basis/checksums/common/common.factor @@ -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 [ 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 + 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 ;