#ifndef _RZ_READONLY
static void add_zindex(RAZF *rz, int64_t in, int64_t out){
+ uint32_t *cores;
+ int64_t *bores;
+
if(rz->index->size == rz->index->cap){
rz->index->cap = rz->index->cap * 1.5 + 2;
- rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
- rz->index->bin_offsets = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
+ cores = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
+ bores = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
+ if ((cores == NULL) || (bores == NULL)) {
+ fprintf(stderr, "[%s] failure to allocate space for new zindex.\n", __func__);
+ abort();
+ }
+ rz->index->cell_offsets = cores;
+ rz->index->bin_offsets = bores;
}
if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out;
rz->index->cell_offsets[rz->index->size] = out - rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE];
int is_be;
size_t count;
if(!rz->load_index) return;
- if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
if(rz->index == NULL) {
- fprintf(stderr, "[%s] failure to allocate index.\n", __func__);
- abort();
+ if ((rz->index = malloc(sizeof(ZBlockIndex))) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate index.\n", __func__);
+ abort();
+ }
}
is_be = is_big_endian();
#ifdef _USE_KNETFILE
#ifdef _WIN32
setmode(fd, O_BINARY);
#endif
- rz = calloc(1, sizeof(RAZF));
+ if ((rz = calloc(1, sizeof(RAZF))) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__);
+ return NULL;
+ }
rz->mode = 'w';
#ifdef _USE_KNETFILE
rz->x.fpw = fd;
#else
rz->filedes = fd;
#endif
- rz->stream = calloc(sizeof(z_stream), 1);
- rz->inbuf = malloc(RZ_BUFFER_SIZE);
- rz->outbuf = malloc(RZ_BUFFER_SIZE);
- rz->index = calloc(sizeof(ZBlockIndex), 1);
+ if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate stream buffer.\n", __func__);
+ free(rz);
+ return NULL;
+ }
+ if ((rz->inbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate input buffer.\n", __func__);
+ free(rz->stream);
+ free(rz);
+ return NULL;
+ }
+ if ((rz->outbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate output buffer.\n", __func__);
+ free(rz->stream);
+ free(rz->inbuf);
+ free(rz);
+ return NULL;
+ }
+ if ((rz->index = calloc(sizeof(ZBlockIndex), 1)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate index.\n", __func__);
+ free(rz->stream);
+ free(rz->inbuf);
+ free(rz->outbuf);
+ free(rz);
+ return NULL;
+ }
+
deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
rz->stream->avail_out = RZ_BUFFER_SIZE;
rz->stream->next_out = rz->outbuf;
- rz->header = calloc(sizeof(gz_header), 1);
+ if ((rz->header = calloc(sizeof(gz_header), 1)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate header buffer.\n", __func__);
+ free(rz->stream);
+ free(rz->inbuf);
+ free(rz->outbuf);
+ free(rz->index);
+ free(rz);
+ return NULL;
+ }
rz->header->os = 0x03; //Unix
rz->header->text = 0;
rz->header->time = 0;
- rz->header->extra = malloc(7);
+ if ((rz->header->extra = malloc(7)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate header buffer.\n", __func__);
+ free(rz->stream);
+ free(rz->inbuf);
+ free(rz->outbuf);
+ free(rz->index);
+ free(rz->header);
+ free(rz);
+ return NULL;
+ }
strncpy((char*)rz->header->extra, "RAZF", 4);
rz->header->extra[4] = 1; // obsolete field
// block size = RZ_BLOCK_SIZE, Big-Endian