/** * \brief Get a block of data if possible, and see if it matches. * * On each call, we try to process all of the input data available on the * scoop and input buffer. */ static rs_result rs_delta_s_scan(rs_job_t *job) { rs_long_t match_pos; size_t match_len; rs_result result; Rollsum test; rs_job_check(job); /* read the input into the scoop */ rs_getinput(job); /* output any pending output from the tube */ result=rs_tube_catchup(job); /* while output is not blocked and there is a block of data */ while ((result==RS_DONE) && ((job->scoop_pos + job->block_len) < job->scoop_avail)) { /* check if this block matches */ if (rs_findmatch(job,&match_pos,&match_len)) { /* append the match and reset the weak_sum */ result=rs_appendmatch(job,match_pos,match_len); RollsumInit(&job->weak_sum); } else { /* rotate the weak_sum and append the miss byte */ RollsumRotate(&job->weak_sum,job->scoop_next[job->scoop_pos], job->scoop_next[job->scoop_pos+job->block_len]); result=rs_appendmiss(job,1); if (rs_roll_paranoia) { RollsumInit(&test); RollsumUpdate(&test,job->scoop_next+job->scoop_pos, job->block_len); if (RollsumDigest(&test) != RollsumDigest(&job->weak_sum)) { rs_fatal("mismatch between rolled sum %#x and check %#x", (int)RollsumDigest(&job->weak_sum), (int)RollsumDigest(&test)); } } } } /* if we completed OK */ if (result==RS_DONE) { /* if we reached eof, we can flush the last fragment */ if (job->stream->eof_in) { job->statefn=rs_delta_s_flush; return RS_RUNNING; } else { /* we are blocked waiting for more data */ return RS_BLOCKED; } } return result; }
static rs_result rs_delta_s_flush(rs_job_t *job) { rs_long_t match_pos; size_t match_len; rs_result result; rs_job_check(job); /* read the input into the scoop */ rs_getinput(job); /* output any pending output */ result=rs_tube_catchup(job); /* while output is not blocked and there is any remaining data */ while ((result==RS_DONE) && (job->scoop_pos < job->scoop_avail)) { /* check if this block matches */ if (rs_findmatch(job,&match_pos,&match_len)) { /* append the match and reset the weak_sum */ result=rs_appendmatch(job,match_pos,match_len); RollsumInit(&job->weak_sum); } else { /* rollout from weak_sum and append the miss byte */ RollsumRollout(&job->weak_sum,job->scoop_next[job->scoop_pos]); rs_trace("block reduced to %d", (int)job->weak_sum.count); result=rs_appendmiss(job,1); } } /* if we are not blocked, flush and set end statefn. */ if (result==RS_DONE) { result=rs_appendflush(job); job->statefn=rs_delta_s_end; } if (result==RS_DONE) { return RS_RUNNING; } return result; }
/** * Prepare to compute a streaming delta. */ rs_job_t *rs_delta_begin(rs_signature_t *sig) { rs_job_t *job; job = rs_job_new("delta", rs_delta_s_header); job->signature = sig; RollsumInit(&job->weak_sum); if ((job->block_len = sig->block_len) < 0) { rs_log(RS_LOG_ERR, "unreasonable block_len %d in signature", job->block_len); return NULL; } job->strong_sum_len = sig->strong_sum_len; if (job->strong_sum_len < 0 || job->strong_sum_len > RS_STRONG_SUM_LEN) { rs_log(RS_LOG_ERR, "unreasonable strong_sum_len %d in signature", job->strong_sum_len); return NULL; } return job; }
int main(int argc, char *argv[]) { int i; Rollsum sum; char c,data[BLOCKSIZE]; unsigned long digest; if (argc != 1) { usage(); } for (i=0;i<BLOCKSIZE;i++) { data[i]=(char)(i & 0xff); } RollsumInit(&sum); RollsumUpdate(&sum,data,BLOCKSIZE); for (i=0;i<(COUNT*BLOCKSIZE);i++) { c=data[i % BLOCKSIZE]; RollsumRotate(&sum,c,c); digest=RollsumDigest(&sum); } printf("rollsum: %i x %i rotate gives %X\n",COUNT,BLOCKSIZE,digest); }