int ns_write_chksum_flush(NetStream_t *ns) { char chksum_value[CHKSUM_MAX_SIZE]; int err, n; tbuffer_t buf; log_printf(15, "ns_write_chksum_flush: injecting chksum! ns=%d type=%d bytesleft=" I64T " bsize=" I64T "\n", ns_getid(ns), chksum_type(&(ns->write_chksum.chksum)), ns->write_chksum.bytesleft, ns->write_chksum.blocksize); flush_log(); if (ns_write_chksum_state(ns) == 0) return(0); if (ns->write_chksum.bytesleft == ns->write_chksum.blocksize) return(0); //** Nothing to do n = chksum_size(&(ns->write_chksum.chksum), CHKSUM_DIGEST_HEX); chksum_get(&(ns->write_chksum.chksum), CHKSUM_DIGEST_HEX, chksum_value); ns->write_chksum.is_running = 0; //** Don't want to get in an endless loop tbuffer_single(&buf, n, chksum_value); err = _write_netstream_block(ns, apr_time_now() + apr_time_make(5,0), &buf, 0, n, 0); ns->write_chksum.is_running = 1; if (err != 0) { log_printf(10, "ns_write_chksum_flush: ns=%d Error writing chksum! error=%d\n", ns_getid(ns), err); return(err); } chksum_value[n] = '\0'; log_printf(15, "ns_write_chksum_flush: ns=%d chksum_value=%s\n", ns_getid(ns), chksum_value); log_printf(15, "ns_write_chksum_flush: end of routine! ns=%d\n err=%d", ns_getid(ns), err); flush_log(); return(err); }
int ns_read_chksum_flush(NetStream_t *ns) { char ns_value[CHKSUM_MAX_SIZE], chksum_value[CHKSUM_MAX_SIZE]; int err, n; tbuffer_t buf; log_printf(15, "ns_read_chksum_flush: Reading chksum! ns=%d type=%d bleft=" I64T " bsize=" I64T " state=%d\n", ns_getid(ns), chksum_type(&(ns->read_chksum.chksum)), ns->read_chksum.bytesleft, ns->read_chksum.blocksize, ns_read_chksum_state(ns)); flush_log(); if (ns_read_chksum_state(ns) == 0) return(0); if (ns->read_chksum.bytesleft == ns->read_chksum.blocksize) return(0); //** Nothing to do n = chksum_size(&(ns->read_chksum.chksum), CHKSUM_DIGEST_HEX); ns->read_chksum.is_running = 0; //** Don't want to get in an endless loop tbuffer_single(&buf, n, ns_value); err = _read_netstream_block(ns, apr_time_now() + apr_time_make(5,0), &buf, 0, n, 0); ns_value[n] = '\0'; ns->read_chksum.is_running = 1; log_printf(15, "ns_read_chksum_flush: Finished reading chksum! ns=%d\n", ns_getid(ns)); flush_log(); if (err != 0) { log_printf(10, "ns_read_chksum_flush: ns=%d Error reading chksum! error=%d\n", ns_getid(ns), err); return(err); } chksum_get(&(ns->read_chksum.chksum), CHKSUM_DIGEST_HEX, chksum_value); log_printf(15, "ns_read_chksum_flush: after chksum_get! ns=%d\n", ns_getid(ns)); flush_log(); err = (strncmp(chksum_value, ns_value, n) == 0) ? 0 : 1; log_printf(15, "ns_read_chksum_flush: ns=%d ns_value=%s cmp=%d\n", ns_getid(ns), ns_value, err); log_printf(15, "ns_read_chksum_flush: ns=%d chksum_value=%s\n", ns_getid(ns), chksum_value); if (err != 0) { log_printf(1, "ns_read_chksum_flush: ns=%d chksum error!\n", ns_getid(ns)); log_printf(1, "ns_read_chksum_flush: ns=%d ns_value=%s cmp=%d\n", ns_getid(ns), ns_value, err); log_printf(1, "ns_read_chksum_flush: ns=%d chksum_value=%s\n", ns_getid(ns), chksum_value); } log_printf(15, "ns_read_chksum_flush: end of routine! ns=%d\n err=%d", ns_getid(ns), err); flush_log(); return(err); }
int main(int argc, char **argv) { char sig[CHKSUM_MAX_SIZE]; char *data; int n, i, repcount; chksum_t cs; if (argc < 4) { printf("chksum_test type data rep_count\n"); return(0); } i=1; if (strcmp(argv[i], "SHA1") == 0) { chksum_set(&cs, CHKSUM_SHA1); } else if (strcmp(argv[i], "SHA256") == 0) { chksum_set(&cs, CHKSUM_SHA256); } else if (strcmp(argv[i], "SHA512") == 0) { chksum_set(&cs, CHKSUM_SHA512); } else if (strcmp(argv[i], "MD5") == 0) { chksum_set(&cs, CHKSUM_MD5); } else { printf("Invalid chksum type. Got %s should be SHA1, SHA256, SHA512, or MD5\n", argv[i]); abort(); } i++; data = argv[i]; i++; repcount = atoi(argv[i]); i++; n = strlen(data); for (i=0; i<repcount; i++) { chksum_add(&cs, n, data); } chksum_get(&cs, CHKSUM_DIGEST_HEX, sig); printf("Data: \"%s\" RepCount: %d\n", data, repcount); printf("Signature: %s\n", sig); return(0); }