From 53e12b0eeee50fab5087b841f8f1ff54b43e7efb Mon Sep 17 00:00:00 2001 From: Jonathan Dieter Date: Tue, 5 Jun 2018 21:22:50 +0300 Subject: [PATCH] All non-public functions shouldn't start with zck_* Signed-off-by: Jonathan Dieter --- src/lib/comp/comp.c | 74 ++++++------ src/lib/comp/nocomp/nocomp.c | 4 +- src/lib/comp/nocomp/nocomp.h | 2 +- src/lib/comp/zstd/zstd.c | 4 +- src/lib/comp/zstd/zstd.h | 2 +- src/lib/dl/dl.c | 219 +++++++++++++++-------------------- src/lib/dl/multipart.c | 6 +- src/lib/dl/range.c | 69 ++++------- src/lib/hash/hash.c | 114 +++++++++--------- src/lib/header.c | 26 ++--- src/lib/index/index_common.c | 21 +++- src/lib/index/index_create.c | 26 ++--- src/lib/index/index_read.c | 2 +- src/lib/log.c | 2 +- src/lib/zck.c | 75 ++++++------ src/lib/zck_private.h | 77 ++++++------ src/zck_dl.c | 5 +- test/lib/util.c | 8 +- 18 files changed, 340 insertions(+), 396 deletions(-) diff --git a/src/lib/comp/comp.c b/src/lib/comp/comp.c index 1277bb1..00fee99 100644 --- a/src/lib/comp/comp.c +++ b/src/lib/comp/comp.c @@ -85,7 +85,7 @@ const static char *COMP_NAME[] = { "zstd" }; -int zck_comp_init(zckCtx *zck) { +int comp_init(zckCtx *zck) { VALIDATE(zck); zckComp *comp = &(zck->comp); @@ -115,7 +115,7 @@ int zck_comp_init(zckCtx *zck) { free(dst); return False; } - if(!zck_index_add_to_chunk(zck, dst, dst_size, + if(!index_add_to_chunk(zck, dst, dst_size, zck->comp.dict_size)) { free(dst); return False; @@ -130,14 +130,14 @@ int zck_comp_init(zckCtx *zck) { free(dst); return False; } - if(!zck_index_add_to_chunk(zck, dst, dst_size, 0) || - !zck_index_finish_chunk(zck)) { + if(!index_add_to_chunk(zck, dst, dst_size, 0) || + !index_finish_chunk(zck)) { free(dst); return False; } free(dst); } else { - if(!zck_index_finish_chunk(zck)) + if(!index_finish_chunk(zck)) return False; } } @@ -148,7 +148,7 @@ int zck_comp_init(zckCtx *zck) { return True; } -int zck_comp_reset(zckCtx *zck) { +int comp_reset(zckCtx *zck) { VALIDATE(zck); zck->comp.started = 0; @@ -163,7 +163,7 @@ int zck_comp_reset(zckCtx *zck) { return zck->comp.close(&(zck->comp)); } -int zck_comp_close(zckCtx *zck) { +int comp_close(zckCtx *zck) { VALIDATE(zck); zck_log(ZCK_LOG_DEBUG, "Closing compression\n"); if(zck->comp.data) { @@ -173,7 +173,7 @@ int zck_comp_close(zckCtx *zck) { zck->comp.data_loc = 0; zck->comp.data_idx = NULL; } - return zck_comp_reset(zck); + return comp_reset(zck); } static int set_comp_type(zckCtx *zck, ssize_t type) { @@ -200,10 +200,10 @@ static int set_comp_type(zckCtx *zck, ssize_t type) { zck_log(ZCK_LOG_DEBUG, "Setting compression to %s\n", zck_comp_name_from_type(type)); if(type == ZCK_COMP_NONE) { - return zck_nocomp_setup(comp); + return nocomp_setup(comp); #ifdef ZCHUNK_ZSTD } else if(type == ZCK_COMP_ZSTD) { - return zck_zstd_setup(comp); + return zstd_setup(comp); #endif } else { zck_log(ZCK_LOG_ERROR, "Unsupported compression type: %s\n", @@ -274,7 +274,7 @@ const char PUBLIC *zck_comp_name_from_type(int comp_type) { return COMP_NAME[comp_type]; } -size_t zck_comp_read_from_dc(zckComp *comp, char *dst, size_t dst_size) { +size_t comp_read_from_dc(zckComp *comp, char *dst, size_t dst_size) { VALIDATE_SIZE(comp); VALIDATE_SIZE(dst); @@ -289,7 +289,7 @@ size_t zck_comp_read_from_dc(zckComp *comp, char *dst, size_t dst_size) { return dl_size; } -int zck_comp_add_to_dc(zckComp *comp, const char *src, size_t src_size) { +int comp_add_to_dc(zckComp *comp, const char *src, size_t src_size) { VALIDATE(comp); VALIDATE(src); @@ -318,7 +318,7 @@ int zck_comp_add_to_dc(zckComp *comp, const char *src, size_t src_size) { return True; } -int zck_comp_add_to_data(zckComp *comp, const char *src, size_t src_size) { +int comp_add_to_data(zckComp *comp, const char *src, size_t src_size) { VALIDATE(comp); VALIDATE(src); comp->data = realloc(comp->data, comp->data_size + src_size); @@ -338,7 +338,7 @@ int zck_comp_add_to_data(zckComp *comp, const char *src, size_t src_size) { ssize_t PUBLIC zck_write(zckCtx *zck, const char *src, const size_t src_size) { VALIDATE_WRITE_SIZE(zck); - if(!zck->comp.started && !zck_comp_init(zck)) + if(!zck->comp.started && !comp_init(zck)) return -1; if(src_size == 0) @@ -352,7 +352,7 @@ ssize_t PUBLIC zck_write(zckCtx *zck, const char *src, const size_t src_size) { free(dst); return -1; } - if(!zck_index_add_to_chunk(zck, dst, dst_size, src_size)) { + if(!index_add_to_chunk(zck, dst, dst_size, src_size)) { free(dst); return -1; } @@ -363,7 +363,7 @@ ssize_t PUBLIC zck_write(zckCtx *zck, const char *src, const size_t src_size) { ssize_t PUBLIC zck_end_chunk(zckCtx *zck) { VALIDATE_WRITE_SIZE(zck); - if(!zck->comp.started && !zck_comp_init(zck)) + if(!zck->comp.started && !comp_init(zck)) return -1; /* No point in compressing empty data */ @@ -379,11 +379,11 @@ ssize_t PUBLIC zck_end_chunk(zckCtx *zck) { free(dst); return -1; } - if(!zck_index_add_to_chunk(zck, dst, dst_size, 0)) { + if(!index_add_to_chunk(zck, dst, dst_size, 0)) { free(dst); return -1; } - if(!zck_index_finish_chunk(zck)) { + if(!index_finish_chunk(zck)) { free(dst); return -1; } @@ -393,11 +393,11 @@ ssize_t PUBLIC zck_end_chunk(zckCtx *zck) { ssize_t comp_end_dchunk(zckCtx *zck, int use_dict, size_t fd_size) { ssize_t rb = zck->comp.end_dchunk(&(zck->comp), use_dict, fd_size); - if(zck_validate_current_chunk(zck) < 1) + if(validate_current_chunk(zck) < 1) return -1; zck->comp.data_loc = 0; zck->comp.data_idx = zck->comp.data_idx->next; - if(!zck_hash_init(&(zck->check_chunk_hash), &(zck->chunk_hash_type))) + if(!hash_init(&(zck->check_chunk_hash), &(zck->chunk_hash_type))) return -1; return rb; } @@ -415,7 +415,7 @@ ssize_t comp_read(zckCtx *zck, char *dst, size_t dst_size, int use_dict) { /* Read dictionary if it exists and hasn't been read yet */ if(use_dict && !zck->comp.data_eof && zck->comp.data_idx == NULL && - zck->index.first->length > 0 && !zck_import_dict(zck)) + zck->index.first->length > 0 && !import_dict(zck)) return -1; size_t dc = 0; @@ -429,9 +429,9 @@ ssize_t comp_read(zckCtx *zck, char *dst, size_t dst_size, int use_dict) { zck_log(ZCK_LOG_DEBUG, "Trying to read %lu bytes\n", dst_size); while(dc < dst_size) { /* Get bytes from decompressed buffer */ - ssize_t rb = zck_comp_read_from_dc(&(zck->comp), dst+dc, dst_size-dc); + ssize_t rb = comp_read_from_dc(&(zck->comp), dst+dc, dst_size-dc); if(rb < 0) - goto zck_read_error; + goto read_error; dc += rb; if(dc == dst_size) break; @@ -444,7 +444,7 @@ ssize_t comp_read(zckCtx *zck, char *dst, size_t dst_size, int use_dict) { size_t dc_data_size = zck->comp.dc_data_size; size_t dc_data_loc = zck->comp.dc_data_loc; if(!zck->comp.decompress(&(zck->comp), use_dict)) - goto zck_read_error; + goto read_error; /* Check whether we decompressed more data */ if(zck->comp.dc_data_size != dc_data_size || @@ -457,15 +457,15 @@ ssize_t comp_read(zckCtx *zck, char *dst, size_t dst_size, int use_dict) { /* Skip first chunk if it's an empty dict */ if(zck->comp.data_idx->comp_length == 0) zck->comp.data_idx = zck->comp.data_idx->next; - if(!zck_hash_init(&(zck->check_chunk_hash), &(zck->chunk_hash_type))) - goto zck_hash_error; + if(!hash_init(&(zck->check_chunk_hash), &(zck->chunk_hash_type))) + goto hash_error; if(zck->comp.data_loc > 0) { - if(!zck_hash_update(&(zck->check_full_hash), zck->comp.data, + if(!hash_update(&(zck->check_full_hash), zck->comp.data, zck->comp.data_loc)) - goto zck_hash_error; - if(!zck_hash_update(&(zck->check_chunk_hash), zck->comp.data, + goto hash_error; + if(!hash_update(&(zck->check_chunk_hash), zck->comp.data, zck->comp.data_loc)) - goto zck_hash_error; + goto hash_error; } if(zck->comp.data_idx == NULL) { free(src); @@ -496,22 +496,22 @@ ssize_t comp_read(zckCtx *zck, char *dst, size_t dst_size, int use_dict) { * compressed buffer */ rb = read_data(zck->fd, src, rs); if(rb < 0) - goto zck_read_error; + goto read_error; if(rb < rs) { zck_log(ZCK_LOG_DEBUG, "EOF\n"); finished_rd = True; } - if(!zck_hash_update(&(zck->check_full_hash), src, rb) || - !zck_hash_update(&(zck->check_chunk_hash), src, rb) || - !zck_comp_add_to_data(&(zck->comp), src, rb)) - goto zck_read_error; + if(!hash_update(&(zck->check_full_hash), src, rb) || + !hash_update(&(zck->check_chunk_hash), src, rb) || + !comp_add_to_data(&(zck->comp), src, rb)) + goto read_error; } free(src); return dc; -zck_read_error: +read_error: free(src); return -1; -zck_hash_error: +hash_error: free(src); return -2; } diff --git a/src/lib/comp/nocomp/nocomp.c b/src/lib/comp/nocomp/nocomp.c index dbc5b49..890a857 100644 --- a/src/lib/comp/nocomp/nocomp.c +++ b/src/lib/comp/nocomp/nocomp.c @@ -63,7 +63,7 @@ static int decompress(zckComp *comp, const int use_dict) { char src_size = comp->data_size; comp->data = NULL; comp->data_size = 0; - if(!zck_comp_add_to_dc(comp, src, src_size)) { + if(!comp_add_to_dc(comp, src, src_size)) { free(src); return False; } @@ -91,7 +91,7 @@ static int set_default_parameters(zckComp *comp) { return True; } -int zck_nocomp_setup(zckComp *comp) { +int nocomp_setup(zckComp *comp) { comp->init = init; comp->set_parameter = set_parameter; comp->compress = compress; diff --git a/src/lib/comp/nocomp/nocomp.h b/src/lib/comp/nocomp/nocomp.h index 327a300..31953cf 100644 --- a/src/lib/comp/nocomp/nocomp.h +++ b/src/lib/comp/nocomp/nocomp.h @@ -1,6 +1,6 @@ #ifndef ZCHUNK_COMPRESSION_NOCOMP_H #define ZCHUNK_COMPRESSION_NOCOMP_H -int zck_nocomp_setup(zckComp *comp); +int nocomp_setup(zckComp *comp); #endif diff --git a/src/lib/comp/zstd/zstd.c b/src/lib/comp/zstd/zstd.c index 0f88610..0f59726 100644 --- a/src/lib/comp/zstd/zstd.c +++ b/src/lib/comp/zstd/zstd.c @@ -145,7 +145,7 @@ static int end_dchunk(zckComp *comp, const int use_dict, const size_t fd_size) { ZSTD_getErrorName(retval)); goto decomp_error_2; } - if(!zck_comp_add_to_dc(comp, dst, fd_size)) + if(!comp_add_to_dc(comp, dst, fd_size)) goto decomp_error_2; free(dst); free(src); @@ -194,7 +194,7 @@ static int set_default_parameters(zckComp *comp) { return set_parameter(comp, ZCK_ZSTD_COMP_LEVEL, &level); } -int zck_zstd_setup(zckComp *comp) { +int zstd_setup(zckComp *comp) { comp->init = init; comp->set_parameter = set_parameter; comp->compress = compress; diff --git a/src/lib/comp/zstd/zstd.h b/src/lib/comp/zstd/zstd.h index 73b151d..c3380ca 100644 --- a/src/lib/comp/zstd/zstd.h +++ b/src/lib/comp/zstd/zstd.h @@ -1,6 +1,6 @@ #ifndef ZCHUNK_COMPRESSION_ZSTD_H #define ZCHUNK_COMPRESSION_ZSTD_H -int zck_zstd_setup(zckComp *comp); +int zstd_setup(zckComp *comp); #endif diff --git a/src/lib/dl/dl.c b/src/lib/dl/dl.c index bb9c716..2bec9c2 100644 --- a/src/lib/dl/dl.c +++ b/src/lib/dl/dl.c @@ -40,8 +40,8 @@ return False; \ } -/* Free zckDL regex's used for downloading ranges */ -static void free_dl_regex(zckDL *dl) { +/* Free zckDL header regex used for downloading ranges */ +void clear_dl_regex(zckDL *dl) { if(dl == NULL || dl->priv == NULL) return; @@ -79,26 +79,7 @@ static int zero_chunk(zckCtx *tgt, zckIndexItem *tgt_idx) { return True; } -static int dl_write(zckDL *dl, const char *at, size_t length) { - VALIDATE(dl); - VALIDATE(dl->priv); - int wb = 0; - if(dl->priv->write_in_chunk > 0) { - if(dl->priv->write_in_chunk < length) - wb = dl->priv->write_in_chunk; - else - wb = length; - if(!write_data(dl->zck->fd, at, wb)) - return -1; - dl->priv->write_in_chunk -= wb; - if(!zck_hash_update(dl->priv->chunk_hash, at, wb)) - return -1; - zck_log(ZCK_LOG_DEBUG, "Writing %lu bytes\n", wb); - dl->priv->dl_chunk_data += wb; - } - return wb; -} - +/* Check whether last downloaded chunk is valid and zero it out if it isn't */ static int validate_chunk(zckDL *dl) { VALIDATE(dl); VALIDATE(dl->priv); @@ -106,7 +87,7 @@ static int validate_chunk(zckDL *dl) { zck_log(ZCK_LOG_ERROR, "Chunk hash not initialized\n"); return False; } - char *digest = zck_hash_finalize(dl->priv->chunk_hash); + char *digest = hash_finalize(dl->priv->chunk_hash); free(dl->priv->chunk_hash); if(memcmp(digest, dl->priv->tgt_check->digest, dl->priv->tgt_check->digest_size) != 0) { @@ -124,7 +105,29 @@ static int validate_chunk(zckDL *dl) { return True; } -int zck_dl_write_range(zckDL *dl, const char *at, size_t length) { +/* Write length or to end of current chunk, whichever comes first */ +static int dl_write(zckDL *dl, const char *at, size_t length) { + VALIDATE(dl); + VALIDATE(dl->priv); + int wb = 0; + if(dl->priv->write_in_chunk > 0) { + if(dl->priv->write_in_chunk < length) + wb = dl->priv->write_in_chunk; + else + wb = length; + if(!write_data(dl->zck->fd, at, wb)) + return -1; + dl->priv->write_in_chunk -= wb; + if(!hash_update(dl->priv->chunk_hash, at, wb)) + return -1; + zck_log(ZCK_LOG_DEBUG, "Writing %lu bytes\n", wb); + dl->priv->dl_chunk_data += wb; + } + return wb; +} + +/* Split current read into the appropriate chunks and write appropriately */ +int dl_write_range(zckDL *dl, const char *at, size_t length) { VALIDATE(dl); VALIDATE(dl->priv); if(dl->range == NULL) { @@ -161,7 +164,7 @@ int zck_dl_write_range(zckDL *dl, const char *at, size_t length) { idx->digest_size) == 0) { dl->priv->tgt_check = tgt_idx; dl->priv->chunk_hash = zmalloc(sizeof(zckHash)); - if(!zck_hash_init(dl->priv->chunk_hash, + if(!hash_init(dl->priv->chunk_hash, &(dl->zck->chunk_hash_type))) return 0; dl->priv->write_in_chunk = idx->comp_length; @@ -181,52 +184,16 @@ int zck_dl_write_range(zckDL *dl, const char *at, size_t length) { } } int wb2 = 0; + /* We've still got data, call recursively */ if(dl->priv->write_in_chunk > 0 && wb < length) { - wb2 = zck_dl_write_range(dl, at+wb, length-wb); + wb2 = dl_write_range(dl, at+wb, length-wb); if(wb2 == 0) return 0; } return wb + wb2; } -size_t PUBLIC zck_write_zck_header_cb(void *ptr, size_t l, size_t c, void *dl_v) { - if(dl_v == NULL) - return 0; - zckDL *dl = (zckDL*)dl_v; - size_t wb = 0; - dl->dl += l*c; - size_t loc = tell_data(dl->zck->fd); - zck_log(ZCK_LOG_DEBUG, "Downloading %lu bytes to position %lu\n", l*c, loc); - wb = write(dl->zck->fd, ptr, l*c); - if(dl->write_cb) - return dl->write_cb(ptr, l, c, dl->wdata); - return wb; -} - -size_t PUBLIC zck_write_chunk_cb(void *ptr, size_t l, size_t c, void *dl_v) { - if(dl_v == NULL) - return 0; - zckDL *dl = (zckDL*)dl_v; - size_t wb = 0; - dl->dl += l*c; - if(dl->priv->boundary != NULL) { - int retval = zck_multipart_extract(dl, ptr, l*c); - if(retval == 0) - wb = 0; - else - wb = l*c; - } else { - int retval = zck_dl_write_range(dl, ptr, l*c); - if(retval == 0) - wb = 0; - else - wb = l*c; - } - if(dl->write_cb) - return dl->write_cb(ptr, l, c, dl->wdata); - return wb; -} - +/* Copy chunk identified by src_idx into location specified by tgt_idx */ int write_and_verify_chunk(zckCtx *src, zckCtx *tgt, zckIndexItem *src_idx, zckIndexItem *tgt_idx) { static char buf[BUF_SIZE] = {0}; @@ -237,7 +204,7 @@ int write_and_verify_chunk(zckCtx *src, zckCtx *tgt, zckIndexItem *src_idx, if(!seek_data(tgt->fd, tgt->data_offset + tgt_idx->start, SEEK_SET)) return False; zckHash check_hash = {0}; - if(!zck_hash_init(&check_hash, &(src->chunk_hash_type))) + if(!hash_init(&check_hash, &(src->chunk_hash_type))) return False; while(to_read > 0) { int rb = BUF_SIZE; @@ -245,13 +212,13 @@ int write_and_verify_chunk(zckCtx *src, zckCtx *tgt, zckIndexItem *src_idx, rb = to_read; if(!read_data(src->fd, buf, rb)) return False; - if(!zck_hash_update(&check_hash, buf, rb)) + if(!hash_update(&check_hash, buf, rb)) return False; if(!write_data(tgt->fd, buf, rb)) return False; to_read -= rb; } - char *digest = zck_hash_finalize(&check_hash); + char *digest = hash_finalize(&check_hash); /* If chunk is invalid, overwrite with zeros and add to download range */ if(memcmp(digest, src_idx->digest, src_idx->digest_size) != 0) { char *pdigest = zck_get_chunk_digest(src_idx); @@ -306,52 +273,6 @@ int PUBLIC zck_copy_chunks(zckCtx *src, zckCtx *tgt) { return True; } -size_t PUBLIC zck_header_cb(char *b, size_t l, size_t c, void *dl_v) { - if(dl_v == NULL) - return 0; - zckDL *dl = (zckDL*)dl_v; - - if(zck_multipart_get_boundary(dl, b, c*l) == 0) - zck_log(ZCK_LOG_DEBUG, "No boundary detected"); - - if(dl->header_cb) - return dl->header_cb(b, l, c, dl->hdrdata); - return c*l; -} - - -int zck_zero_bytes(zckDL *dl, size_t bytes, size_t start, size_t *buffer_len) { - char buf[BUF_SIZE] = {0}; - if(start + bytes > *buffer_len) { - zck_log(ZCK_LOG_DEBUG, "Seeking to end of temporary file\n"); - if(lseek(dl->zck->fd, 0, SEEK_END) == -1) { - zck_log(ZCK_LOG_ERROR, "Seek to end of temporary file failed: %s\n", - strerror(errno)); - return False; - } - size_t write = *buffer_len; - while(write < start + bytes) { - size_t wb = BUF_SIZE; - if(write + wb > start + bytes) - wb = (start + bytes) - write; - if(!write_data(dl->zck->fd, buf, wb)) - return False; - write += wb; - } - zck_log(ZCK_LOG_DEBUG, "Wrote %lu zeros at position %lu\n", start+bytes-*buffer_len, *buffer_len); - *buffer_len = start+bytes; - zck_log(ZCK_LOG_DEBUG, "Seeking to position %lu\n", start); - if(lseek(dl->zck->fd, start, SEEK_SET) == -1) { - zck_log(ZCK_LOG_ERROR, - "Seek to byte %lu of temporary file failed: %s\n", start, - strerror(errno)); - return False; - } - } - return True; -} - - size_t PUBLIC zck_dl_get_bytes_downloaded(zckDL *dl) { VALIDATE(dl); return dl->dl; @@ -362,19 +283,6 @@ size_t PUBLIC zck_dl_get_bytes_uploaded(zckDL *dl) { return dl->ul; } -/* Free zckDL header regex used for downloading ranges */ -void zck_dl_clear_regex(zckDL *dl) { - if(dl == NULL || dl->priv == NULL) - return; - - free_dl_regex(dl); - if(dl->priv->hdr_regex) { - regfree(dl->priv->hdr_regex); - free(dl->priv->hdr_regex); - dl->priv->hdr_regex = NULL; - } -} - /* Initialize zckDL. When finished, zckDL *must* be freed by zck_dl_free() */ zckDL PUBLIC *zck_dl_init(zckCtx *zck) { zckDL *dl = zmalloc(sizeof(zckDL)); @@ -400,7 +308,7 @@ zckDL PUBLIC *zck_dl_init(zckCtx *zck) { return dl; } -/* Free zckDL and set pointer to NULL */ +/* Reset dl while maintaining download statistics and private information */ void PUBLIC zck_dl_reset(zckDL *dl) { if(!dl) return; @@ -411,7 +319,7 @@ void PUBLIC zck_dl_reset(zckDL *dl) { memset(dl->priv->mp, 0, sizeof(zckMP)); } dl->priv->dl_chunk_data = 0; - zck_dl_clear_regex(dl); + clear_dl_regex(dl); if(dl->priv->boundary) free(dl->priv->boundary); dl->priv->boundary = NULL; @@ -440,3 +348,58 @@ void PUBLIC zck_dl_free(zckDL **dl) { free(*dl); *dl = NULL; } + +/******************************************************************* + * Callbacks + *******************************************************************/ + +size_t PUBLIC zck_header_cb(char *b, size_t l, size_t c, void *dl_v) { + if(dl_v == NULL) + return 0; + zckDL *dl = (zckDL*)dl_v; + + if(multipart_get_boundary(dl, b, c*l) == 0) + zck_log(ZCK_LOG_DEBUG, "No boundary detected"); + + if(dl->header_cb) + return dl->header_cb(b, l, c, dl->hdrdata); + return c*l; +} + +size_t PUBLIC zck_write_zck_header_cb(void *ptr, size_t l, size_t c, void *dl_v) { + if(dl_v == NULL) + return 0; + zckDL *dl = (zckDL*)dl_v; + size_t wb = 0; + dl->dl += l*c; + size_t loc = tell_data(dl->zck->fd); + zck_log(ZCK_LOG_DEBUG, "Downloading %lu bytes to position %lu\n", l*c, loc); + wb = write(dl->zck->fd, ptr, l*c); + if(dl->write_cb) + return dl->write_cb(ptr, l, c, dl->wdata); + return wb; +} + +size_t PUBLIC zck_write_chunk_cb(void *ptr, size_t l, size_t c, void *dl_v) { + if(dl_v == NULL) + return 0; + zckDL *dl = (zckDL*)dl_v; + size_t wb = 0; + dl->dl += l*c; + if(dl->priv->boundary != NULL) { + int retval = multipart_extract(dl, ptr, l*c); + if(retval == 0) + wb = 0; + else + wb = l*c; + } else { + int retval = dl_write_range(dl, ptr, l*c); + if(retval == 0) + wb = 0; + else + wb = l*c; + } + if(dl->write_cb) + return dl->write_cb(ptr, l, c, dl->wdata); + return wb; +} diff --git a/src/lib/dl/multipart.c b/src/lib/dl/multipart.c index 8776906..506c9bd 100644 --- a/src/lib/dl/multipart.c +++ b/src/lib/dl/multipart.c @@ -94,7 +94,7 @@ static int gen_regex(zckDL *dl) { return True; } -size_t zck_multipart_extract(zckDL *dl, char *b, size_t l) { +size_t multipart_extract(zckDL *dl, char *b, size_t l) { VALIDATE(dl); if(dl->priv == NULL || dl->priv->mp == NULL) return 0; @@ -139,7 +139,7 @@ size_t zck_multipart_extract(zckDL *dl, char *b, size_t l) { } else { mp->length -= size; } - if(zck_dl_write_range(dl, i, size) != size) + if(dl_write_range(dl, i, size) != size) return 0; i += size; continue; @@ -207,7 +207,7 @@ static void reset_mp(zckMP *mp) { memset(mp, 0, sizeof(zckMP)); } -size_t zck_multipart_get_boundary(zckDL *dl, char *b, size_t size) { +size_t multipart_get_boundary(zckDL *dl, char *b, size_t size) { VALIDATE(dl); if(dl->priv == NULL) return 0; diff --git a/src/lib/dl/range.c b/src/lib/dl/range.c index 8a88626..1ecd685 100644 --- a/src/lib/dl/range.c +++ b/src/lib/dl/range.c @@ -34,14 +34,6 @@ #include "zck_private.h" -void zck_range_remove(zckRangeItem *range) { - if(range->prev) - range->prev->next = range->next; - if(range->next) - range->next->prev = range->prev; - free(range); -} - void PUBLIC zck_range_free(zckRange **info) { zckRangeItem *next = (*info)->first; while(next) { @@ -49,12 +41,12 @@ void PUBLIC zck_range_free(zckRange **info) { next = next->next; free(tmp); } - zck_index_clean(&((*info)->index)); + index_clean(&((*info)->index)); free(*info); *info = NULL; } -zckRangeItem *zck_range_insert_new(zckRangeItem *prev, zckRangeItem *next, uint64_t start, +zckRangeItem *range_insert_new(zckRangeItem *prev, zckRangeItem *next, uint64_t start, uint64_t end, zckRange *info, zckIndexItem *idx, int add_index) { zckRangeItem *new = zmalloc(sizeof(zckRangeItem)); @@ -74,7 +66,7 @@ zckRangeItem *zck_range_insert_new(zckRangeItem *prev, zckRangeItem *next, uint6 next->prev = new; } if(add_index) - if(!zck_index_new_chunk(&(info->index), idx->digest, idx->digest_size, + if(!index_new_chunk(&(info->index), idx->digest, idx->digest_size, end-start+1, end-start+1, False)) { free(new); return NULL; @@ -82,7 +74,15 @@ zckRangeItem *zck_range_insert_new(zckRangeItem *prev, zckRangeItem *next, uint6 return new; } -void zck_range_merge_combined(zckRange *info) { +void range_remove(zckRangeItem *range) { + if(range->prev) + range->prev->next = range->next; + if(range->next) + range->next->prev = range->prev; + free(range); +} + +void range_merge_combined(zckRange *info) { if(!info) { zck_log(ZCK_LOG_ERROR, "zckRange not allocated\n"); return; @@ -91,7 +91,7 @@ void zck_range_merge_combined(zckRange *info) { if(ptr->next && ptr->end >= ptr->next->start-1) { if(ptr->end < ptr->next->end) ptr->end = ptr->next->end; - zck_range_remove(ptr->next); + range_remove(ptr->next); info->count -= 1; } else { ptr = ptr->next; @@ -99,7 +99,7 @@ void zck_range_merge_combined(zckRange *info) { } } -int zck_range_add(zckRange *info, zckIndexItem *idx, zckCtx *zck) { +int range_add(zckRange *info, zckIndexItem *idx, zckCtx *zck) { if(info == NULL || idx == NULL) { zck_log(ZCK_LOG_ERROR, "zckRange or zckIndexItem not allocated\n"); return False; @@ -121,35 +121,37 @@ int zck_range_add(zckRange *info, zckIndexItem *idx, zckCtx *zck) { continue; } else if(start < ptr->start) { - if(zck_range_insert_new(ptr->prev, ptr, start, end, info, idx, add_index) == NULL) + if(range_insert_new(ptr->prev, ptr, start, end, info, idx, + add_index) == NULL) return False; if(info->first == ptr) { info->first = ptr->prev; } info->count += 1; - zck_range_merge_combined(info); + range_merge_combined(info); return True; } else { // start == ptr->start if(end > ptr->end) ptr->end = end; info->count += 1; - zck_range_merge_combined(info); + range_merge_combined(info); return True; } } /* We've only reached here if we should be last item */ - zckRangeItem *new = zck_range_insert_new(prev, NULL, start, end, info, idx, add_index); + zckRangeItem *new = range_insert_new(prev, NULL, start, end, info, idx, + add_index); if(new == NULL) return False; if(info->first == NULL) info->first = new; info->count += 1; - zck_range_merge_combined(info); + range_merge_combined(info); return True; } char PUBLIC *zck_get_range_char(zckRange *range) { - int buf_size=32768; + int buf_size=BUF_SIZE; char *output=malloc(buf_size); if(!output) { zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n", buf_size); @@ -207,7 +209,7 @@ zckRange PUBLIC *zck_get_dl_range(zckCtx *zck, int max_ranges) { idx = idx->next; continue; } - if(!zck_range_add(range, idx, zck)) { + if(!range_add(range, idx, zck)) { zck_range_free(&range); return NULL; } @@ -218,31 +220,6 @@ zckRange PUBLIC *zck_get_dl_range(zckCtx *zck, int max_ranges) { return range; } -int zck_range_get_need_dl(zckRange *info, zckCtx *zck_src, zckCtx *zck_tgt) { - zckIndex *tgt_info = zck_get_index(zck_tgt); - zckIndex *src_info = zck_get_index(zck_src); - zckIndexItem *tgt_idx = tgt_info->first; - zckIndexItem *src_idx = src_info->first; - while(tgt_idx) { - int found = False; - src_idx = src_info->first; - - while(src_idx) { - if(memcmp(tgt_idx->digest, src_idx->digest, tgt_idx->digest_size) == 0) { - found = True; - break; - } - src_idx = src_idx->next; - } - if(!found) - if(!zck_range_add(info, tgt_idx, zck_tgt)) - return False; - - tgt_idx = tgt_idx->next; - } - return True; -} - char PUBLIC *zck_get_range(size_t start, size_t end) { zckRange range = {0}; zckRangeItem ri = {0}; diff --git a/src/lib/hash/hash.c b/src/lib/hash/hash.c index dfa9d80..d078fb5 100644 --- a/src/lib/hash/hash.c +++ b/src/lib/hash/hash.c @@ -52,7 +52,7 @@ int get_max_hash_size() { return SHA256_DIGEST_SIZE; } -int zck_hash_setup(zckHashType *ht, int h) { +int hash_setup(zckHashType *ht, int h) { if(ht) { if(h == ZCK_HASH_SHA1) { memset(ht, 0, sizeof(zckHashType)); @@ -73,7 +73,19 @@ int zck_hash_setup(zckHashType *ht, int h) { return False; } -int zck_hash_init(zckHash *hash, zckHashType *hash_type) { +void hash_close(zckHash *hash) { + if(!hash) + return; + + if(hash->ctx) { + free(hash->ctx); + hash->ctx = NULL; + } + hash->type = NULL; + return; +} + +int hash_init(zckHash *hash, zckHashType *hash_type) { if(hash && hash_type) { if(hash_type->type == ZCK_HASH_SHA1) { zck_log(ZCK_LOG_DEBUG, "Initializing SHA-1 hash\n"); @@ -99,7 +111,7 @@ int zck_hash_init(zckHash *hash, zckHashType *hash_type) { return False; } -int zck_hash_update(zckHash *hash, const char *message, const size_t size) { +int hash_update(zckHash *hash, const char *message, const size_t size) { if(message == NULL && size == 0) return True; if(message == NULL) { @@ -127,56 +139,17 @@ int zck_hash_update(zckHash *hash, const char *message, const size_t size) { return False; } -void zck_hash_close(zckHash *hash) { - if(!hash) - return; - - if(hash->ctx) { - free(hash->ctx); - hash->ctx = NULL; - } - hash->type = NULL; - return; -} - -/* Returns 1 if data hash matches, 0 if it doesn't and -1 if failure */ -int PUBLIC zck_validate_data_checksum(zckCtx *zck) { - zck_hash_close(&(zck->check_full_hash)); - if(!seek_data(zck->fd, zck->data_offset, SEEK_SET)) - return -1; - if(!zck_hash_init(&(zck->check_full_hash), &(zck->hash_type))) - return -1; - char buf[BUF_SIZE] = {0}; - zckIndexItem *idx = zck->index.first; - zck_log(ZCK_LOG_INFO, "Checking full hash\n"); - while(idx) { - size_t to_read = idx->comp_length; - while(to_read > 0) { - size_t rb = BUF_SIZE; - if(rb > to_read) - rb = to_read; - if(!read_data(zck->fd, buf, rb)) - return -1; - if(!zck_hash_update(&(zck->check_full_hash), buf, rb)) - return -1; - to_read -= rb; - } - idx = idx->next; - } - return zck_validate_file(zck); -} - -char *zck_hash_finalize(zckHash *hash) { +char *hash_finalize(zckHash *hash) { if(hash && hash->ctx && hash->type) { if(hash->type->type == ZCK_HASH_SHA1) { unsigned char *digest = zmalloc(hash->type->digest_size); SHA1_Final((sha1_byte*)digest, (SHA_CTX *)hash->ctx); - zck_hash_close(hash); + hash_close(hash); return (char *)digest; } else if(hash->type->type == ZCK_HASH_SHA256) { unsigned char *digest = zmalloc(hash->type->digest_size); sha256_final((sha256_ctx *)hash->ctx, digest); - zck_hash_close(hash); + hash_close(hash); return (char *)digest; } zck_log(ZCK_LOG_ERROR, "Unsupported hash type: %i\n", hash->type); @@ -186,25 +159,17 @@ char *zck_hash_finalize(zckHash *hash) { return NULL; } -const char PUBLIC *zck_hash_name_from_type(int hash_type) { - if(hash_type > 1) { - snprintf(unknown+8, 21, "%i)", hash_type); - return unknown; - } - return HASH_NAME[hash_type]; -} - int set_full_hash_type(zckCtx *zck, int hash_type) { VALIDATE(zck); zck_log(ZCK_LOG_INFO, "Setting full hash to %s\n", zck_hash_name_from_type(hash_type)); - if(!zck_hash_setup(&(zck->hash_type), hash_type)) { + if(!hash_setup(&(zck->hash_type), hash_type)) { zck_log(ZCK_LOG_ERROR, "Unable to set full hash to %s\n", zck_hash_name_from_type(hash_type)); return False; } - zck_hash_close(&(zck->full_hash)); - if(!zck_hash_init(&(zck->full_hash), &(zck->hash_type))) { + hash_close(&(zck->full_hash)); + if(!hash_init(&(zck->full_hash), &(zck->hash_type))) { zck_log(ZCK_LOG_ERROR, "Unable initialize full hash\n"); return False; } @@ -216,7 +181,7 @@ int set_chunk_hash_type(zckCtx *zck, int hash_type) { memset(&(zck->chunk_hash_type), 0, sizeof(zckHashType)); zck_log(ZCK_LOG_INFO, "Setting chunk hash to %s\n", zck_hash_name_from_type(hash_type)); - if(!zck_hash_setup(&(zck->chunk_hash_type), hash_type)) { + if(!hash_setup(&(zck->chunk_hash_type), hash_type)) { zck_log(ZCK_LOG_ERROR, "Unable to set chunk hash to %s\n", zck_hash_name_from_type(hash_type)); return False; @@ -225,3 +190,38 @@ int set_chunk_hash_type(zckCtx *zck, int hash_type) { zck->index.digest_size = zck->chunk_hash_type.digest_size; return True; } + +/* Returns 1 if data hash matches, 0 if it doesn't and -1 if failure */ +int PUBLIC zck_validate_data_checksum(zckCtx *zck) { + hash_close(&(zck->check_full_hash)); + if(!seek_data(zck->fd, zck->data_offset, SEEK_SET)) + return -1; + if(!hash_init(&(zck->check_full_hash), &(zck->hash_type))) + return -1; + char buf[BUF_SIZE] = {0}; + zckIndexItem *idx = zck->index.first; + zck_log(ZCK_LOG_INFO, "Checking full hash\n"); + while(idx) { + size_t to_read = idx->comp_length; + while(to_read > 0) { + size_t rb = BUF_SIZE; + if(rb > to_read) + rb = to_read; + if(!read_data(zck->fd, buf, rb)) + return -1; + if(!hash_update(&(zck->check_full_hash), buf, rb)) + return -1; + to_read -= rb; + } + idx = idx->next; + } + return validate_file(zck); +} + +const char PUBLIC *zck_hash_name_from_type(int hash_type) { + if(hash_type > 1) { + snprintf(unknown+8, 21, "%i)", hash_type); + return unknown; + } + return HASH_NAME[hash_type]; +} diff --git a/src/lib/header.c b/src/lib/header.c index afb33de..2ce9ea7 100644 --- a/src/lib/header.c +++ b/src/lib/header.c @@ -104,7 +104,7 @@ int PUBLIC zck_read_lead(zckCtx *zck) { "(%i)\n", hash_type, zck->prep_hash_type); return False; } - if(!zck_hash_setup(&(zck->hash_type), hash_type)) + if(!hash_setup(&(zck->hash_type), hash_type)) return False; zck_log(ZCK_LOG_DEBUG, "Setting header and full digest hash type to %s\n", zck_hash_name_from_type(hash_type)); @@ -201,14 +201,14 @@ int read_header_from_file(zckCtx *zck) { zck->header_size = zck->lead_size + zck->header_length; } - if(!zck_hash_init(&(zck->check_full_hash), &(zck->hash_type))) + if(!hash_init(&(zck->check_full_hash), &(zck->hash_type))) return False; - if(!zck_hash_update(&(zck->check_full_hash), zck->header, + if(!hash_update(&(zck->check_full_hash), zck->header, zck->hdr_digest_loc)) return False; - if(!zck_hash_update(&(zck->check_full_hash), header, zck->header_length)) + if(!hash_update(&(zck->check_full_hash), header, zck->header_length)) return False; - if(!zck_validate_header(zck)) + if(!validate_header(zck)) return False; return True; } @@ -254,7 +254,7 @@ int read_preface(zckCtx *zck) { return False; if(!comp_ioption(zck, ZCK_COMP_TYPE, tmp)) return False; - if(!zck_comp_init(zck)) + if(!comp_init(zck)) return False; /* Read and initialize index size */ @@ -285,7 +285,7 @@ int read_index(zckCtx *zck) { } header = zck->header + zck->lead_size + zck->preface_size; int max_length = zck->header_size - (zck->lead_size + zck->preface_size); - if(!zck_index_read(zck, header, zck->index_size, max_length)) + if(!index_read(zck, header, zck->index_size, max_length)) return False; zck->index_string = header; @@ -441,7 +441,7 @@ int lead_create(zckCtx *zck) { return True; } -int zck_header_create(zckCtx *zck) { +int header_create(zckCtx *zck) { /* Rebuild header without header hash */ if(zck->header_digest) { free(zck->header_digest); @@ -498,18 +498,18 @@ int zck_header_create(zckCtx *zck) { zckHash header_hash = {0}; /* Calculate hash of header */ - if(!zck_hash_init(&header_hash, &(zck->hash_type))) + if(!hash_init(&header_hash, &(zck->hash_type))) return False; zck_log(ZCK_LOG_DEBUG, "Hashing lead\n"); /* Hash lead up to header digest */ - if(!zck_hash_update(&header_hash, zck->lead_string, + if(!hash_update(&header_hash, zck->lead_string, zck->hdr_digest_loc)) return False; zck_log(ZCK_LOG_DEBUG, "Hashing the rest\n"); /* Hash rest of header */ - if(!zck_hash_update(&header_hash, zck->preface_string, zck->header_length)) + if(!hash_update(&header_hash, zck->preface_string, zck->header_length)) return False; - zck->header_digest = zck_hash_finalize(&header_hash); + zck->header_digest = hash_finalize(&header_hash); if(zck->header_digest == NULL) { zck_log(ZCK_LOG_ERROR, "Unable to calculate %s checksum for index\n", @@ -523,7 +523,7 @@ int zck_header_create(zckCtx *zck) { return True; } -int zck_write_header(zckCtx *zck) { +int write_header(zckCtx *zck) { VALIDATE_WRITE(zck); zck_log(ZCK_LOG_DEBUG, "Writing header: %lu bytes\n", diff --git a/src/lib/index/index_common.c b/src/lib/index/index_common.c index 0484cd9..59b105f 100644 --- a/src/lib/index/index_common.c +++ b/src/lib/index/index_common.c @@ -31,7 +31,7 @@ #include "zck_private.h" -zckIndexItem *zck_get_index_of_loc(zckIndex *index, size_t loc) { +zckIndexItem *get_index_of_loc(zckIndex *index, size_t loc) { zckIndexItem *idx = index->first; while(idx != NULL) { if(loc >= idx->start && loc < idx->start + idx->comp_length) @@ -43,7 +43,7 @@ zckIndexItem *zck_get_index_of_loc(zckIndex *index, size_t loc) { return NULL; } -void zck_index_free_item(zckIndexItem **item) { +void index_free_item(zckIndexItem **item) { if(*item == NULL) return; @@ -54,7 +54,7 @@ void zck_index_free_item(zckIndexItem **item) { return; } -void zck_index_clean(zckIndex *index) { +void index_clean(zckIndex *index) { if(index == NULL) return; @@ -63,15 +63,15 @@ void zck_index_clean(zckIndex *index) { zckIndexItem *tmp=index->first; while(tmp != NULL) { next = tmp->next; - zck_index_free_item(&tmp); + index_free_item(&tmp); tmp = next; } } memset(index, 0, sizeof(zckIndex)); } -void zck_index_free(zckCtx *zck) { - zck_index_clean(&(zck->index)); +void index_free(zckCtx *zck) { + index_clean(&(zck->index)); if(zck->full_hash_digest) { free(zck->full_hash_digest); zck->full_hash_digest = NULL; @@ -93,3 +93,12 @@ void zck_index_free(zckCtx *zck) { zck->header_digest = NULL; } } + +void clear_work_index(zckCtx *zck) { + if(zck == NULL) + return; + + hash_close(&(zck->work_index_hash)); + if(zck->work_index_item) + index_free_item(&(zck->work_index_item)); +} diff --git a/src/lib/index/index_create.c b/src/lib/index/index_create.c index ce721e6..ef80179 100644 --- a/src/lib/index/index_create.c +++ b/src/lib/index/index_create.c @@ -44,7 +44,7 @@ int index_create(zckCtx *zck) { size_t index_size = 0; - zck->full_hash_digest = zck_hash_finalize(&(zck->full_hash)); + zck->full_hash_digest = hash_finalize(&(zck->full_hash)); if(zck->full_hash_digest == NULL) return False; @@ -122,7 +122,7 @@ static int finish_chunk(zckIndex *index, zckIndexItem *item, char *digest, return True; } -int zck_index_new_chunk(zckIndex *index, char *digest, int digest_size, +int index_new_chunk(zckIndex *index, char *digest, int digest_size, size_t comp_size, size_t orig_size, int finished) { if(index == NULL) { zck_log(ZCK_LOG_ERROR, "Invalid index\n"); @@ -144,51 +144,51 @@ int zck_index_new_chunk(zckIndex *index, char *digest, int digest_size, return finish_chunk(index, idx, digest, finished); } -int zck_index_create_chunk(zckCtx *zck) { +int index_create_chunk(zckCtx *zck) { VALIDATE(zck); - zck_clear_work_index(zck); + clear_work_index(zck); zck->work_index_item = zmalloc(sizeof(zckIndexItem)); if(zck->work_index_item == NULL) { zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n", sizeof(zckIndexItem)); return False; } - if(!zck_hash_init(&(zck->work_index_hash), &(zck->chunk_hash_type))) + if(!hash_init(&(zck->work_index_hash), &(zck->chunk_hash_type))) return False; return True; } -int zck_index_add_to_chunk(zckCtx *zck, char *data, size_t comp_size, +int index_add_to_chunk(zckCtx *zck, char *data, size_t comp_size, size_t orig_size) { VALIDATE(zck); - if(zck->work_index_item == NULL && !zck_index_create_chunk(zck)) + if(zck->work_index_item == NULL && !index_create_chunk(zck)) return False; zck->work_index_item->length += orig_size; if(comp_size == 0) return True; - if(!zck_hash_update(&(zck->full_hash), data, comp_size)) + if(!hash_update(&(zck->full_hash), data, comp_size)) return False; - if(!zck_hash_update(&(zck->work_index_hash), data, comp_size)) + if(!hash_update(&(zck->work_index_hash), data, comp_size)) return False; zck->work_index_item->comp_length += comp_size; return True; } -int zck_index_finish_chunk(zckCtx *zck) { +int index_finish_chunk(zckCtx *zck) { VALIDATE(zck); - if(zck->work_index_item == NULL && !zck_index_create_chunk(zck)) + if(zck->work_index_item == NULL && !index_create_chunk(zck)) return False; char *digest = NULL; if(zck->work_index_item->length > 0) { /* Finalize chunk checksum */ - digest = zck_hash_finalize(&(zck->work_index_hash)); + digest = hash_finalize(&(zck->work_index_hash)); if(digest == NULL) { zck_log(ZCK_LOG_ERROR, "Unable to calculate %s checksum for new chunk\n", @@ -208,6 +208,6 @@ int zck_index_finish_chunk(zckCtx *zck) { free(digest); zck->work_index_item = NULL; - zck_hash_close(&(zck->work_index_hash)); + hash_close(&(zck->work_index_hash)); return True; } diff --git a/src/lib/index/index_read.c b/src/lib/index/index_read.c index 8dc99fd..1fc2ccc 100644 --- a/src/lib/index/index_read.c +++ b/src/lib/index/index_read.c @@ -37,7 +37,7 @@ return False; \ } -int zck_index_read(zckCtx *zck, char *data, size_t size, size_t max_length) { +int index_read(zckCtx *zck, char *data, size_t size, size_t max_length) { VALIDATE(zck); size_t length = 0; diff --git a/src/lib/log.c b/src/lib/log.c index 3174751..91edd14 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -38,7 +38,7 @@ void PUBLIC zck_set_log_level(zck_log_type ll) { log_level = ll; } -void PUBLIC zck_log(zck_log_type lt, const char *format, ...) { +void zck_log(zck_log_type lt, const char *format, ...) { if(lt >= log_level) { va_list args; va_start(args, format); diff --git a/src/lib/zck.c b/src/lib/zck.c index 76f40bb..f69b250 100644 --- a/src/lib/zck.c +++ b/src/lib/zck.c @@ -165,7 +165,7 @@ int PUBLIC zck_set_soption(zckCtx *zck, zck_soption option, const char *value, "*before* the header digest itself\n"); return False; } - if(!zck_hash_setup(&chk_type, zck->prep_hash_type)) { + if(!hash_setup(&chk_type, zck->prep_hash_type)) { free(data); return False; } @@ -199,51 +199,42 @@ int PUBLIC zck_close(zckCtx *zck) { if(zck->mode == ZCK_MODE_WRITE) { if(zck_end_chunk(zck) < 0) return False; - if(!zck_header_create(zck)) + if(!header_create(zck)) return False; - if(!zck_write_header(zck)) + if(!write_header(zck)) return False; zck_log(ZCK_LOG_DEBUG, "Writing chunks\n"); if(!chunks_from_temp(zck)) return False; zck_log(ZCK_LOG_DEBUG, "Finished writing file, cleaning up\n"); - if(!zck_comp_close(zck)) + if(!comp_close(zck)) return False; if(zck->temp_fd) { close(zck->temp_fd); zck->temp_fd = 0; } } else { - if(!zck_validate_file(zck)) + if(!validate_file(zck)) return False; } return True; } -void zck_clear_work_index(zckCtx *zck) { - if(zck == NULL) - return; - - zck_hash_close(&(zck->work_index_hash)); - if(zck->work_index_item) - zck_index_free_item(&(zck->work_index_item)); -} - void zck_clear(zckCtx *zck) { if(zck == NULL) return; - zck_index_free(zck); + index_free(zck); if(zck->header) free(zck->header); zck->header = NULL; zck->header_size = 0; - if(!zck_comp_close(zck)) + if(!comp_close(zck)) zck_log(ZCK_LOG_WARNING, "Unable to close compression\n"); - zck_hash_close(&(zck->full_hash)); - zck_hash_close(&(zck->check_full_hash)); - zck_hash_close(&(zck->check_chunk_hash)); - zck_clear_work_index(zck); + hash_close(&(zck->full_hash)); + hash_close(&(zck->check_full_hash)); + hash_close(&(zck->check_chunk_hash)); + clear_work_index(zck); if(zck->full_hash_digest) { free(zck->full_hash_digest); zck->full_hash_digest = NULL; @@ -311,7 +302,7 @@ zckCtx PUBLIC *zck_init_write (int dst_fd) { return NULL; zck->mode = ZCK_MODE_WRITE; - zck->temp_fd = zck_get_tmp_fd(); + zck->temp_fd = get_tmp_fd(); if(zck->temp_fd < 0) goto iw_error; @@ -420,7 +411,7 @@ ssize_t PUBLIC zck_get_length(zckCtx *zck) { return zck_get_header_length(zck) + zck_get_data_length(zck); } -int zck_get_tmp_fd() { +int get_tmp_fd() { int temp_fd; char *fname = NULL; char template[] = "zcktempXXXXXX"; @@ -454,7 +445,7 @@ int zck_get_tmp_fd() { return temp_fd; } -int zck_import_dict(zckCtx *zck) { +int import_dict(zckCtx *zck) { VALIDATE(zck); size_t size = zck->index.first->length; @@ -474,19 +465,19 @@ int zck_import_dict(zckCtx *zck) { return False; } zck_log(ZCK_LOG_DEBUG, "Resetting compression\n"); - if(!zck_comp_reset(zck)) + if(!comp_reset(zck)) return False; zck_log(ZCK_LOG_DEBUG, "Setting dict\n"); if(!comp_soption(zck, ZCK_COMP_DICT, data, size)) return False; - if(!zck_comp_init(zck)) + if(!comp_init(zck)) return False; free(data); return True; } -int zck_validate_chunk(zckCtx *zck, zckIndexItem *idx, +int validate_chunk(zckCtx *zck, zckIndexItem *idx, zck_log_type bad_checksum) { VALIDATE(zck); if(idx == NULL) { @@ -494,7 +485,7 @@ int zck_validate_chunk(zckCtx *zck, zckIndexItem *idx, return -1; } - char *digest = zck_hash_finalize(&(zck->check_chunk_hash)); + char *digest = hash_finalize(&(zck->check_chunk_hash)); if(digest == NULL) { zck_log(ZCK_LOG_ERROR, "Unable to calculate %s checksum for chunk\n"); @@ -519,15 +510,15 @@ int zck_validate_chunk(zckCtx *zck, zckIndexItem *idx, return 1; } -int zck_validate_current_chunk(zckCtx *zck) { +int validate_current_chunk(zckCtx *zck) { VALIDATE(zck); - return zck_validate_chunk(zck, zck->comp.data_idx, ZCK_LOG_ERROR); + return validate_chunk(zck, zck->comp.data_idx, ZCK_LOG_ERROR); } -int zck_validate_file(zckCtx *zck) { +int validate_file(zckCtx *zck) { VALIDATE(zck); - char *digest = zck_hash_finalize(&(zck->check_full_hash)); + char *digest = hash_finalize(&(zck->check_full_hash)); if(digest == NULL) { zck_log(ZCK_LOG_ERROR, "Unable to calculate %s checksum for full file\n"); @@ -551,9 +542,9 @@ int zck_validate_file(zckCtx *zck) { return 1; } -int zck_validate_header(zckCtx *zck) { +int validate_header(zckCtx *zck) { VALIDATE(zck); - char *digest = zck_hash_finalize(&(zck->check_full_hash)); + char *digest = hash_finalize(&(zck->check_full_hash)); if(digest == NULL) { zck_log(ZCK_LOG_ERROR, "Unable to calculate %s checksum for header\n"); @@ -575,7 +566,7 @@ int zck_validate_header(zckCtx *zck) { zck_log(ZCK_LOG_DEBUG, "Header checksum valid\n"); free(digest); - if(!zck_hash_init(&(zck->check_full_hash), &(zck->hash_type))) + if(!hash_init(&(zck->check_full_hash), &(zck->hash_type))) return -1; return 1; @@ -590,8 +581,8 @@ int PUBLIC zck_validate_checksums(zckCtx *zck) { return -1; } - zck_hash_close(&(zck->check_full_hash)); - if(!zck_hash_init(&(zck->check_full_hash), &(zck->hash_type))) + hash_close(&(zck->check_full_hash)); + if(!hash_init(&(zck->check_full_hash), &(zck->hash_type))) return -1; if(!seek_data(zck->fd, zck->data_offset, SEEK_SET)) @@ -606,7 +597,7 @@ int PUBLIC zck_validate_checksums(zckCtx *zck) { continue; } - if(!zck_hash_init(&(zck->check_chunk_hash), &(zck->chunk_hash_type))) + if(!hash_init(&(zck->check_chunk_hash), &(zck->chunk_hash_type))) return -1; size_t rlen = 0; @@ -616,13 +607,13 @@ int PUBLIC zck_validate_checksums(zckCtx *zck) { rsize = idx->comp_length - rlen; if(read_data(zck->fd, buf, rsize) != rsize) return 0; - if(!zck_hash_update(&(zck->check_chunk_hash), buf, rsize)) + if(!hash_update(&(zck->check_chunk_hash), buf, rsize)) return -1; - if(!zck_hash_update(&(zck->check_full_hash), buf, rsize)) + if(!hash_update(&(zck->check_full_hash), buf, rsize)) return -1; rlen += rsize; } - int valid_chunk = zck_validate_chunk(zck, idx, ZCK_LOG_ERROR); + int valid_chunk = validate_chunk(zck, idx, ZCK_LOG_ERROR); if(valid_chunk < 0) return -1; idx->valid = valid_chunk; @@ -634,7 +625,7 @@ int PUBLIC zck_validate_checksums(zckCtx *zck) { return 0; /* Check data checksum */ - int valid_file = zck_validate_file(zck); + int valid_file = validate_file(zck); if(valid_file < 0) return -1; @@ -643,7 +634,7 @@ int PUBLIC zck_validate_checksums(zckCtx *zck) { return -1; /* Reinitialize data checksum */ - if(!zck_hash_init(&(zck->check_full_hash), &(zck->hash_type))) + if(!hash_init(&(zck->check_full_hash), &(zck->hash_type))) return -1; return valid_file; diff --git a/src/lib/zck_private.h b/src/lib/zck_private.h index f76dcf5..e485a8f 100644 --- a/src/lib/zck_private.h +++ b/src/lib/zck_private.h @@ -157,32 +157,31 @@ typedef struct zckCtx { size_t data_size; } zckCtx; -const char *zck_hash_name_from_type(int hash_type) +int get_tmp_fd() __attribute__ ((warn_unused_result)); -int zck_get_tmp_fd() +int import_dict(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_import_dict(zckCtx *zck) +int validate_file(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_validate_file(zckCtx *zck) +int validate_current_chunk(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_validate_current_chunk(zckCtx *zck) +int validate_header(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_validate_header(zckCtx *zck) +const char *hash_name_from_type(int hash_type) __attribute__ ((warn_unused_result)); -void zck_clear_work_index(zckCtx *zck); char *get_digest_string(const char *digest, int size) __attribute__ ((warn_unused_result)); /* hash/hash.h */ -int zck_hash_setup(zckHashType *ht, int h) +int hash_setup(zckHashType *ht, int h) __attribute__ ((warn_unused_result)); -int zck_hash_init(zckHash *hash, zckHashType *hash_type) +int hash_init(zckHash *hash, zckHashType *hash_type) __attribute__ ((warn_unused_result)); -int zck_hash_update(zckHash *hash, const char *message, const size_t size) +int hash_update(zckHash *hash, const char *message, const size_t size) __attribute__ ((warn_unused_result)); -char *zck_hash_finalize(zckHash *hash) +char *hash_finalize(zckHash *hash) __attribute__ ((warn_unused_result)); -void zck_hash_close(zckHash *hash); +void hash_close(zckHash *hash); const char *zck_hash_get_printable(const char *digest, zckHashType *type) __attribute__ ((warn_unused_result)); int set_full_hash_type(zckCtx *zck, int hash_type) @@ -194,26 +193,28 @@ int get_max_hash_size() /* index/index.c */ -int zck_index_read(zckCtx *zck, char *data, size_t size, size_t max_length) +int index_read(zckCtx *zck, char *data, size_t size, size_t max_length) __attribute__ ((warn_unused_result)); int index_create(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_index_new_chunk(zckIndex *index, char *digest, int digest_size, +int index_new_chunk(zckIndex *index, char *digest, int digest_size, size_t comp_size, size_t orig_size, int valid) __attribute__ ((warn_unused_result)); -int zck_index_add_to_chunk(zckCtx *zck, char *data, size_t comp_size, +int index_add_to_chunk(zckCtx *zck, char *data, size_t comp_size, size_t orig_size) __attribute__ ((warn_unused_result)); -int zck_index_finish_chunk(zckCtx *zck) +int index_finish_chunk(zckCtx *zck) __attribute__ ((warn_unused_result)); -void zck_index_clean(zckIndex *index); -void zck_index_free(zckCtx *zck); -void zck_index_free_item(zckIndexItem **item); -int zck_write_index(zckCtx *zck) +void index_clean(zckIndex *index); +void index_free(zckCtx *zck); +void index_free_item(zckIndexItem **item); +void clear_work_index(zckCtx *zck); +int write_index(zckCtx *zck) __attribute__ ((warn_unused_result)); -zckIndexItem *zck_get_index_of_loc(zckIndex *index, size_t loc) +zckIndexItem *get_index_of_loc(zckIndex *index, size_t loc) __attribute__ ((warn_unused_result)); + /* io.c */ int seek_data(int fd, off_t offset, int whence) __attribute__ ((warn_unused_result)); @@ -239,23 +240,27 @@ int read_index(zckCtx *zck) __attribute__ ((warn_unused_result)); int read_sig(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_read_header(zckCtx *zck) +int header_create(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_header_create(zckCtx *zck) +int sig_create(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_sig_create(zckCtx *zck) +int write_header(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_write_header(zckCtx *zck) - __attribute__ ((warn_unused_result)); -int zck_write_sigs(zckCtx *zck) +int write_sigs(zckCtx *zck) __attribute__ ((warn_unused_result)); /* comp/comp.c */ -int zck_comp_add_to_dc(zckComp *comp, const char *src, size_t src_size) +int comp_init(zckCtx *zck) + __attribute__ ((warn_unused_result)); +int comp_close(zckCtx *zck) __attribute__ ((warn_unused_result)); -int zck_comp_add_to_data(zckComp *comp, const char *src, size_t src_size) +int comp_reset(zckCtx *zck) __attribute__ ((warn_unused_result)); -size_t zck_comp_read_from_dc(zckComp *comp, char *dst, size_t dst_size) +int comp_add_to_dc(zckComp *comp, const char *src, size_t src_size) + __attribute__ ((warn_unused_result)); +int comp_add_to_data(zckComp *comp, const char *src, size_t src_size) + __attribute__ ((warn_unused_result)); +size_t comp_read_from_dc(zckComp *comp, char *dst, size_t dst_size) __attribute__ ((warn_unused_result)); ssize_t comp_read(zckCtx *zck, char *dst, size_t dst_size, int use_dict) __attribute__ ((warn_unused_result)); @@ -266,21 +271,19 @@ int comp_soption(zckCtx *zck, zck_soption option, const void *value, __attribute__ ((warn_unused_result)); /* dl/range.c */ -char *zck_range_get_char(zckRangeItem **range, int max_ranges) +char *range_get_char(zckRangeItem **range, int max_ranges) __attribute__ ((warn_unused_result)); -int zck_range_add(zckRange *info, zckIndexItem *idx, zckCtx *zck) +int range_add(zckRange *info, zckIndexItem *idx, zckCtx *zck) __attribute__ ((warn_unused_result)); /* dl/multipart.c */ -size_t zck_multipart_extract(zckDL *dl, char *b, size_t l) +size_t multipart_extract(zckDL *dl, char *b, size_t l) __attribute__ ((warn_unused_result)); -size_t zck_multipart_get_boundary(zckDL *dl, char *b, size_t size) +size_t multipart_get_boundary(zckDL *dl, char *b, size_t size) __attribute__ ((warn_unused_result)); /* dl/dl.c */ -int zck_dl_write_range(zckDL *dl, const char *at, size_t length) - __attribute__ ((warn_unused_result)); -int zck_dl_range_chk_chunk(zckDL *dl, char *url, int is_chunk) +int dl_write_range(zckDL *dl, const char *at, size_t length) __attribute__ ((warn_unused_result)); /* compint.c */ diff --git a/src/zck_dl.c b/src/zck_dl.c index 3d928c7..aed1c02 100644 --- a/src/zck_dl.c +++ b/src/zck_dl.c @@ -125,6 +125,7 @@ typedef struct dlCtx { int max_ranges; } dlCtx; +/* Fail if dl_ctx->fail_no_ranges is set and we get a 200 response */ size_t dl_header_cb(char *b, size_t l, size_t c, void *dl_v) { dlCtx *dl_ctx = (dlCtx*)dl_v; if(dl_ctx->fail_no_ranges) { @@ -138,8 +139,8 @@ size_t dl_header_cb(char *b, size_t l, size_t c, void *dl_v) { return zck_header_cb(b, l, c, dl_ctx->dl); } -/* Return -1 on error, 0 on 200 response (if is_chunk), and 1 on complete - * success */ +/* Return 0 on error, -1 on 200 response (if dl_ctx->fail_no_ranges), + * and 1 on complete success */ int dl_range(dlCtx *dl_ctx, char *url, char *range, int is_chunk) { if(dl_ctx == NULL || dl_ctx->dl == NULL || dl_ctx->dl->priv == NULL) { free(range); diff --git a/test/lib/util.c b/test/lib/util.c index 734cbd0..fd63d29 100644 --- a/test/lib/util.c +++ b/test/lib/util.c @@ -31,13 +31,13 @@ char *get_hash(char *data, size_t length, int type) { zckHashType hash_type = {0}; zckHash hash = {0}; - if(!zck_hash_setup(&hash_type, type)) + if(!hash_setup(&hash_type, type)) return NULL; - if(!zck_hash_init(&hash, &hash_type)) + if(!hash_init(&hash, &hash_type)) return NULL; - if(!zck_hash_update(&hash, data, length)) + if(!hash_update(&hash, data, length)) return NULL; - char *digest = zck_hash_finalize(&hash); + char *digest = hash_finalize(&hash); if(digest == NULL) return NULL; return get_digest_string(digest, hash_type.digest_size); -- 2.30.2