int _write_netstream(NetStream_t *ns, tbuffer_t *buffer, int boff, int bsize, Net_timeout_t timeout, int dolock) { int total_bytes, i; if (dolock == 1) lock_write_ns(ns); if (ns->sock_status(ns->sock) != 1) { log_printf(15, "write_netstream: connection closed! ns=%d\n", ns->id); if (dolock == 1) unlock_write_ns(ns); return(-1); } if (bsize == 0) { if (dolock == 1) unlock_write_ns(ns); return(0); } if (ns_write_chksum_state(ns) == 1) { //** We have chksumming enabled if (bsize > ns->write_chksum.bytesleft) { bsize = ns->write_chksum.bytesleft; //** Truncate at the block } } total_bytes = ns->write(ns->sock, buffer, boff, bsize, timeout); if (total_bytes == -1) { log_printf(10, "write_netstream: Dead connection! ns=%d\n", ns_getid(ns)); } ns->last_write = apr_time_now(); if ((ns_write_chksum_state(ns) == 1) && (total_bytes > 0)) { //** We have chksumming enabled chksum_add(&(ns->write_chksum.chksum), total_bytes, buffer, boff); //** Chksum it ns->write_chksum.bytesleft -= total_bytes; if (ns->write_chksum.bytesleft <= 0) { //** Reached the block size so inject the chksum i = ns_write_chksum_flush(ns); if (i != 0) total_bytes = NS_CHKSUM; //** Reset the chksum ns->write_chksum.bytesleft = ns->write_chksum.blocksize; chksum_reset(&(ns->write_chksum.chksum)); } } if (dolock == 1) unlock_write_ns(ns); return(total_bytes); }
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); }