checksums.common: simplify add-checksum-bytes.

char-rename
John Benediktsson 2016-07-09 20:58:14 -07:00
parent 217b7f2b0c
commit 6b9d6c00c0
1 changed files with 19 additions and 23 deletions

View File

@ -41,29 +41,25 @@ GENERIC: get-checksum ( checksum-state -- value )
: next-level ( n size -- n' ) : next-level ( n size -- n' )
2dup mod [ + ] [ - + ] if-zero ; inline 2dup mod [ + ] [ - + ] if-zero ; inline
! Update the bytes-read before calculating checksum in case checksum uses ! Update the bytes-read before calculating checksum in case
! this in the calculation. ! checksum uses this in the calculation.
:: add-checksum-bytes ( state data -- state' ) :: add-checksum-bytes ( checksum-state data -- checksum-state' )
state block-size>> :> block-size checksum-state block-size>> :> block-size
state bytes>> length :> initial-len checksum-state bytes>> length :> initial-len
data length :> data-len initial-len data length + block-size /mod :> ( n extra )
initial-len data-len + :> total-len data checksum-state bytes>> [ push-all ] keep :> all-bytes
total-len block-size /mod :> ( n extra ) all-bytes block-size <groups>
data state bytes>> [ push-all ] keep :> all-bytes extra zero? [ f ] [ unclip-last-slice ] if :> ( blocks remain )
n zero? [
state [ data-len + ] change-bytes-read drop checksum-state [ initial-len - ] change-bytes-read drop
] [
all-bytes block-size <groups> [ length 64 = ] partition [ blocks [
[ state [ block-size next-level ] change-bytes-read drop state checksum-block ] each checksum-state [ block-size + ] change-bytes-read
BV{ } clone state bytes<< checksum-block
] [ ] each
[
first checksum-state [ extra + ] change-bytes-read
[ length state [ + ] change-bytes-read drop ] remain [ >byte-vector ] [ BV{ } clone ] if* >>bytes ;
[ >byte-vector state bytes<< ] bi
] unless-empty
] bi*
] if state ;
: add-checksum-stream ( checksum-state stream -- checksum-state ) : add-checksum-stream ( checksum-state stream -- checksum-state )
[ [ add-checksum-bytes ] each-block ] with-input-stream ; [ [ add-checksum-bytes ] each-block ] with-input-stream ;