int finished;
size_t start;
size_t comp_length;
+ size_t length;
struct zckIndexItem *next;
} zckIndexItem;
/* Decompress data src of size src_size, and write to dst, while setting
* dst_size */
int zck_decompress(zckCtx *zck, const char *src, const size_t src_size,
- char **dst, size_t *dst_size);
+ char **dst, size_t dst_size);
/*******************************************************************
* Creating a zchunk file
free(dst);
return False;
}
- zck_index_add_chunk(zck, dst, dst_size);
+ zck_index_add_chunk(zck, dst, dst_size, zck->comp.dict_size);
free(dst);
}
zck->comp.dict = NULL;
free(dst);
return False;
}
- zck_index_add_chunk(zck, dst, dst_size);
+ zck_index_add_chunk(zck, dst, dst_size, src_size);
free(dst);
return True;
}
-int zck_decompress(zckCtx *zck, const char *src, const size_t src_size, char **dst, size_t *dst_size) {
+int zck_decompress(zckCtx *zck, const char *src, const size_t src_size,
+ char **dst, size_t dst_size) {
zckComp *comp = &(zck->comp);
*dst = NULL;
- *dst_size = 0;
if(!zck->comp.started) {
zck_log(ZCK_LOG_ERROR, "Compression hasn't been initialized yet\n");
return True;
}
-/* Nocomp just copies data for both compression and decompression */
-static int zck_nocomp_de_comp(zckComp *comp, const char *src, const size_t src_size,
+static int zck_nocomp_comp(zckComp *comp, const char *src, const size_t src_size,
char **dst, size_t *dst_size, int use_dict) {
*dst = zmalloc(src_size);
if(dst == NULL) {
return True;
}
+static int zck_nocomp_decomp(zckComp *comp, const char *src, const size_t src_size,
+ char **dst, size_t dst_size, int use_dict) {
+ *dst = zmalloc(src_size);
+ if(dst == NULL) {
+ zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n", src_size);
+ return False;
+ }
+
+ memcpy(*dst, src, src_size);
+
+ return True;
+}
+
static int zck_nocomp_close(zckComp *comp) {
return True;
}
int zck_nocomp_setup(zckComp *comp) {
comp->init = zck_nocomp_init;
comp->set_parameter = zck_nocomp_set_parameter;
- comp->compress = zck_nocomp_de_comp;
- comp->decompress = zck_nocomp_de_comp;
+ comp->compress = zck_nocomp_comp;
+ comp->decompress = zck_nocomp_decomp;
comp->close = zck_nocomp_close;
comp->type = ZCK_COMP_NONE;
return zck_nocomp_set_default_parameters(comp);
static int zck_zstd_decompress(zckComp *comp, const char *src,
const size_t src_size, char **dst,
- size_t *dst_size, int use_dict) {
- unsigned long long frame_size;
- size_t retval;
-
- frame_size = ZSTD_getFrameContentSize(src, src_size);
- if(frame_size == ZSTD_CONTENTSIZE_UNKNOWN ||
- frame_size == ZSTD_CONTENTSIZE_ERROR) {
- zck_log(ZCK_LOG_ERROR, "Unknown content size\n");
- return False;
- }
- *dst_size = (size_t)frame_size;
- *dst = zmalloc(*dst_size);
+ size_t dst_size, int use_dict) {
+ *dst = zmalloc(dst_size);
if(dst == NULL) {
- zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n", *dst_size);
+ zck_log(ZCK_LOG_ERROR, "Unable to allocate %lu bytes\n", dst_size);
return False;
}
- if(use_dict && comp->ddict_ctx) {
- retval = ZSTD_decompress_usingDDict(comp->dctx, *dst, *dst_size, src,
+ size_t retval;
+ if(use_dict && comp->ddict_ctx)
+ retval = ZSTD_decompress_usingDDict(comp->dctx, *dst, dst_size, src,
src_size, comp->ddict_ctx);
- } else {
- retval = ZSTD_decompressDCtx(comp->dctx, *dst, *dst_size, src,
+ else
+ retval = ZSTD_decompressDCtx(comp->dctx, *dst, dst_size, src,
src_size);
- }
if(ZSTD_isError(retval)) {
free(*dst);
*dst = NULL;
}
if(add_index)
if(!zck_index_new_chunk(&(info->index), idx->digest, idx->digest_size,
- end-start+1, False)) {
+ end-start+1, end-start+1, False)) {
free(new);
return NULL;
}
if(zck->index.first) {
zckIndexItem *tmp = zck->index.first;
while(tmp) {
- index_malloc += zck->index.digest_size + MAX_COMP_SIZE;
+ index_malloc += zck->index.digest_size + MAX_COMP_SIZE*2;
tmp = tmp->next;
}
}
if(zck->index.first) {
zckIndexItem *tmp = zck->index.first;
while(tmp) {
+ /* Write digest */
memcpy(index+index_size, tmp->digest, zck->index.digest_size);
index_size += zck->index.digest_size;
+ /* Write compressed size */
zck_compint_from_size(index+index_size, tmp->comp_length,
&index_size);
+ /* Write uncompressed size */
+ zck_compint_from_size(index+index_size, tmp->length, &index_size);
+
tmp = tmp->next;
}
}
}
int zck_index_new_chunk(zckIndex *index, char *digest, int digest_size,
- size_t length, int finished) {
+ size_t comp_size, size_t orig_size, int finished) {
if(index == NULL) {
zck_log(ZCK_LOG_ERROR, "Invalid index\n");
return False;
memcpy(idx->digest, digest, digest_size);
idx->digest_size = digest_size;
idx->start = index->length;
- idx->comp_length = length;
+ idx->comp_length = comp_size;
+ idx->length = orig_size;
idx->finished = finished;
if(index->first == NULL) {
index->first = idx;
tmp->next = idx;
}
index->count += 1;
- index->length += length;
+ index->length += comp_size;
return True;
}
-int zck_index_add_chunk(zckCtx *zck, char *data, size_t size) {
+int zck_index_add_chunk(zckCtx *zck, char *data, size_t comp_size,
+ size_t orig_size) {
zckHash hash;
if(zck == NULL) {
return False;
}
- if(size == 0) {
+ if(comp_size == 0) {
if(!zck_index_new_chunk(&(zck->index), NULL, zck->index.digest_size,
- size, True))
+ comp_size, orig_size, True))
return False;
} else {
- if(!zck_hash_update(&(zck->full_hash), data, size))
+ if(!zck_hash_update(&(zck->full_hash), data, comp_size))
return False;
if(!zck_hash_init(&hash, &(zck->chunk_hash_type)))
return False;
- if(!zck_hash_update(&hash, data, size))
+ if(!zck_hash_update(&hash, data, comp_size))
return False;
char *digest = zck_hash_finalize(&hash);
return False;
}
if(!zck_index_new_chunk(&(zck->index), digest, zck->index.digest_size,
- size, True))
+ comp_size, orig_size, True))
return False;
free(digest);
}
return False;
new->start = idx_loc;
new->comp_length = chunk_length;
- new->finished = False;
- idx_loc += chunk_length;
+ /* Read and store uncompressed entry length */
+ chunk_length = 0;
+ if(!zck_compint_to_size(&chunk_length, data+length, &length))
+ return False;
+ new->length = chunk_length;
+
+ new->finished = False;
+ idx_loc += new->comp_length;
zck->index.length = idx_loc;
if(prev)
/* Check if zck file is empty */
for(int count=0; idx; count++) {
size_t csize = idx->comp_length;
+ size_t size = idx->length;
char *cdata;
if(csize == 0)
return False;
}
- size_t size = 0;
char *data = NULL;
- if(!zck_decompress(zck, cdata, csize, &data, &size)) {
+ if(!zck_decompress(zck, cdata, csize, &data, size)) {
free(cdata);
zck_log(ZCK_LOG_ERROR, "Unable to decompress chunk %i\n", count);
return False;
const size_t src_size, char **dst, size_t *dst_size,
int use_dict);
typedef int (*fdecomp)(struct zckComp *comp, const char *src,
- const size_t src_size, char **dst, size_t *dst_size,
+ const size_t src_size, char **dst, size_t dst_size,
int use_dict);
typedef int (*fcclose)(struct zckComp *comp);
int zck_get_tmp_fd();
int zck_validate_file(zckCtx *zck);
-/* comp/comp.c */
-int zck_comp_init(zckCtx *zck);
-int zck_compress(zckCtx *zck, const char *src, const size_t src_size);
-int zck_decompress(zckCtx *zck, const char *src, const size_t src_size, char **dst, size_t *dst_size);
-int zck_comp_close(zckCtx *zck);
-int zck_set_compression_type(zckCtx *zck, int type);
-int zck_set_comp_parameter(zckCtx *zck, int option, void *value);
-
/* hash/hash.h */
int zck_hash_setup(zckHashType *ht, int h);
int zck_hash_init(zckHash *hash, zckHashType *hash_type);
int zck_index_read(zckCtx *zck, char *data, size_t size);
int zck_index_finalize(zckCtx *zck);
int zck_index_new_chunk(zckIndex *index, char *digest, int digest_size,
- size_t length, int finished);
-int zck_index_add_chunk(zckCtx *zck, char *data, size_t size);
+ size_t comp_size, size_t orig_size, int finished);
+int zck_index_add_chunk(zckCtx *zck, char *data, size_t comp_size,
+ size_t orig_size);
void zck_index_clean(zckIndex *index);
void zck_index_free(zckCtx *zck);
int zck_write_index(zckCtx *zck);
while(idx) {
for(int i=0; i<zck_get_chunk_digest_size(zck); i++)
printf("%02x", (unsigned char)idx->digest[i]);
- printf(" %12lu %12lu %12lu\n", idx->start + zck_get_header_length(zck), idx->comp_length, idx->comp_length);
+ printf(" %12lu %12lu %12lu\n", idx->start + zck_get_header_length(zck), idx->comp_length, idx->length);
idx = idx->next;
}