Skip to content

Commit

Permalink
FEAT: added functional /PART and /SEEK refinements for WRITE action o…
Browse files Browse the repository at this point in the history
…n CHECKSUM port

Possible usage:

data: #{0bad}
port: open checksum://
sum1: read write port bin
sum2: read write/part open port tail bin -2 ;port was restarted using OPEN
sum3: read write/seek/part open port bin 2 -2
all [sum1 = sum2 sum1 = sum3] ;== true
  • Loading branch information
Oldes committed Sep 30, 2018
1 parent 9285dfd commit 5149e47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/core/p-checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
REBVAL *method;
REBREQ *req;
REBVAL *arg;
REBCNT args = 0;
REBVAL *data;
REBVAL *ctx;
REBYTE *temp;
Expand Down Expand Up @@ -128,16 +129,38 @@
Checksum_Open(port, method, ctx_size);
SET_OPEN(req);
}
args = Find_Refines(ds, ALL_WRITE_REFS);
arg = D_ARG(2);
REBYTE *bin = VAL_BIN_DATA(arg);
REBI64 pos = (REBI64)VAL_INDEX(arg);
if (args & AM_WRITE_SEEK) {
pos += Int64(D_ARG(ARG_WRITE_INDEX));
if(pos < 0) pos = 0;
else if (pos > VAL_TAIL(arg)) pos = VAL_TAIL(arg);
}
REBI64 part = (REBI64)VAL_TAIL(arg) - pos;
if (args & AM_WRITE_PART) {
REBI64 cnt = Int64(D_ARG(ARG_WRITE_LENGTH));
if(cnt < 0) {
cnt = -cnt;
pos -= cnt;
if (pos < 0) {
cnt += pos;
pos = 0;
}
part = cnt;
} else if (cnt < part) part = cnt;
}
if(part <= 0) return R_RET;
switch (VAL_WORD_CANON(method)) {
case SYM_MD5:
MD5_Update((MD5_CTX*)VAL_BIN(ctx), VAL_BIN_DATA(arg), VAL_TAIL(arg) - VAL_INDEX(arg));
MD5_Update((MD5_CTX*)VAL_BIN(ctx), VAL_BIN_SKIP(arg, pos), part);
break;
case SYM_SHA1:
SHA1_Update((SHA_CTX*)VAL_BIN(ctx), VAL_BIN_DATA(arg), VAL_TAIL(arg) - VAL_INDEX(arg));
SHA1_Update((SHA_CTX*)VAL_BIN(ctx), VAL_BIN_SKIP(arg, pos), part);
break;
case SYM_SHA256:
SHA256_Update((SHA256_CTX*)VAL_BIN(ctx), VAL_BIN_DATA(arg), VAL_TAIL(arg) - VAL_INDEX(arg));
SHA256_Update((SHA256_CTX*)VAL_BIN(ctx), VAL_BIN_SKIP(arg, pos), part);
break;
}
break;
Expand Down Expand Up @@ -176,7 +199,6 @@
return R_RET;

case A_OPEN:
if (IS_OPEN(req)) return R_RET;
if (Checksum_Open(port, method, ctx_size)) {
SET_OPEN(req);
} else {
Expand Down
19 changes: 19 additions & 0 deletions src/tests/units/checksum-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ Rebol [
--assert sum1 = port/data
close port

--test-- "checksum-write-refinements"
port: open checksum://
write/part port bin 1
write/part port next bin 1
sum1: checksum/method bin port/spec/method
sum2: checksum/method join bin bin port/spec/method
--assert sum1 = read port
--assert sum1 = read write/part port bin 0
--assert sum1 = read write/part port bin -1
--assert sum2 = read write/part port tail bin -2
port: open checksum://
--assert sum1 = read write/seek/part port #{cafe0bad} 2 2
;opening already opened port restarts computation
--assert sum1 = read write/seek/part open port #{cafe0bad} 2 2
--assert sum1 = read write/seek/part open port tail #{cafe0bad} -2 2




===end-group===

~~~end-file~~~

0 comments on commit 5149e47

Please sign in to comment.