#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];
}
static void save_zindex(RAZF *rz, int fd){
+ size_t count;
int32_t i, v32;
int is_be;
is_be = is_big_endian();
- if(is_be) write(fd, &rz->index->size, sizeof(int));
+ if(is_be) {
+ if (write(fd, &rz->index->size, sizeof(int)) < 0) {
+ fprintf(stderr, "[%s] failure to write zindex size.\n", __func__);
+ abort();
+ }
+ }
else {
v32 = byte_swap_4((uint32_t)rz->index->size);
- write(fd, &v32, sizeof(uint32_t));
+ if (write(fd, &v32, sizeof(uint32_t)) < 0) {
+ fprintf(stderr, "[%s] failure to write zindex size.\n", __func__);
+ abort();
+ }
}
v32 = rz->index->size / RZ_BIN_SIZE + 1;
if(!is_be){
for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
}
- write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
- write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size);
+ count = sizeof(int64_t) * v32;
+ if (write(fd, rz->index->bin_offsets, count) < 0) {
+ fprintf(stderr, "[%s] failure to write zindex bin_offsets.\n", __func__);
+ abort();
+ }
+
+ count = sizeof(int32_t) * rz->index->size;
+ if (write(fd, rz->index->cell_offsets, count) < 0) {
+ fprintf(stderr, "[%s] failure to write zindex cell_offsets.\n", __func__);
+ abort();
+ }
}
#endif
#endif
int32_t i, v32;
int is_be;
+ size_t count;
if(!rz->load_index) return;
- if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
+ if(rz->index == NULL) {
+ 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
- knet_read(fp, &rz->index->size, sizeof(int));
+ if (knet_read(fp, &rz->index->size, sizeof(int)) < 0) {
#else
- read(fd, &rz->index->size, sizeof(int));
+ if (read(fd, &rz->index->size, sizeof(int)) < 0) {
#endif
+ fprintf(stderr, "[%s] failure to read zindex size.\n", __func__);
+ abort();
+ }
if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
rz->index->cap = rz->index->size;
v32 = rz->index->size / RZ_BIN_SIZE + 1;
- rz->index->bin_offsets = malloc(sizeof(int64_t) * v32);
+ count = sizeof(int64_t) * v32;
+ if ((rz->index->bin_offsets = malloc(count)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate bin_offsets array.\n", __func__);
+ abort();
+ }
#ifdef _USE_KNETFILE
- knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32);
+ if (knet_read(fp, rz->index->bin_offsets, count) < 0) {
#else
- read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
+ if (read(fd, rz->index->bin_offsets, count) < 0) {
#endif
- rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
+ fprintf(stderr, "[%s] failure to read bin_offsets.\n", __func__);
+ abort();
+ }
+ count = sizeof(int) * rz->index->size;
+ if ((rz->index->cell_offsets = malloc(count)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate cell_offsets array.\n", __func__);
+ abort();
+ }
#ifdef _USE_KNETFILE
- knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
+ if (knet_read(fp, rz->index->cell_offsets, count) < count) {
#else
- read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
+ if (read(fd, rz->index->cell_offsets, count) < count) {
#endif
+ fprintf(stderr, "[%s] failure to read cell_offsets.\n", __func__);
+ abort();
+ }
if(!is_be){
for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
#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
static void _razf_write(RAZF* rz, const void *data, int size){
int tout;
+ size_t count;
rz->stream->avail_in = size;
rz->stream->next_in = (void*)data;
while(1){
deflate(rz->stream, Z_NO_FLUSH);
rz->out += tout - rz->stream->avail_out;
if(rz->stream->avail_out) break;
+ count = RZ_BUFFER_SIZE - rz->stream->avail_out;
#ifdef _USE_KNETFILE
- write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+ if (write(rz->x.fpw, rz->outbuf, count) < 0) {
#else
- write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+ if (write(rz->filedes, rz->outbuf, count) < 0) {
#endif
+ fprintf(stderr, "[%s] failed to write output buffer.\n", __func__);
+ abort();
+ }
rz->stream->avail_out = RZ_BUFFER_SIZE;
rz->stream->next_out = rz->outbuf;
if(rz->stream->avail_in == 0) break;
}
static void razf_flush(RAZF *rz){
+ size_t count;
uint32_t tout;
if(rz->buf_len){
_razf_write(rz, rz->inbuf, rz->buf_len);
rz->buf_off = rz->buf_len = 0;
}
if(rz->stream->avail_out){
+ count = RZ_BUFFER_SIZE - rz->stream->avail_out;
#ifdef _USE_KNETFILE
- write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+ if (write(rz->x.fpw, rz->outbuf, count) < 0) {
#else
- write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+ if (write(rz->filedes, rz->outbuf, count) < 0) {
#endif
+ fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
+ abort();
+ }
rz->stream->avail_out = RZ_BUFFER_SIZE;
rz->stream->next_out = rz->outbuf;
}
deflate(rz->stream, Z_FULL_FLUSH);
rz->out += tout - rz->stream->avail_out;
if(rz->stream->avail_out == 0){
+ count = RZ_BUFFER_SIZE - rz->stream->avail_out;
#ifdef _USE_KNETFILE
- write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+ if (write(rz->x.fpw, rz->outbuf, count) < 0) {
#else
- write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+ if (write(rz->filedes, rz->outbuf, count) < 0) {
#endif
+ fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
+ abort();
+ }
rz->stream->avail_out = RZ_BUFFER_SIZE;
rz->stream->next_out = rz->outbuf;
} else break;
}
static void razf_end_flush(RAZF *rz){
+ size_t count;
uint32_t tout;
if(rz->buf_len){
_razf_write(rz, rz->inbuf, rz->buf_len);
deflate(rz->stream, Z_FINISH);
rz->out += tout - rz->stream->avail_out;
if(rz->stream->avail_out < RZ_BUFFER_SIZE){
+ count = RZ_BUFFER_SIZE - rz->stream->avail_out;
#ifdef _USE_KNETFILE
- write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+ if (write(rz->x.fpw, rz->outbuf, count) < 0) {
#else
- write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+ if (write(rz->filedes, rz->outbuf, count) < 0) {
#endif
+ fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
+ abort();
+ }
rz->stream->avail_out = RZ_BUFFER_SIZE;
rz->stream->next_out = rz->outbuf;
} else break;
int n, is_be, ret;
int64_t end;
unsigned char c[] = "RAZF";
- 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 = 'r';
#ifdef _USE_KNETFILE
rz->x.fpr = fp;
#endif
rz->filedes = fd;
#endif
- rz->stream = calloc(sizeof(z_stream), 1);
- rz->inbuf = malloc(RZ_BUFFER_SIZE);
- rz->outbuf = malloc(RZ_BUFFER_SIZE);
+ if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) {
+ fprintf(stderr, "[%s] failure to allocate z_stream.\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->inbuf);
+ free(rz->stream);
+ free(rz);
+ return NULL;
+ }
rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
#ifdef _USE_KNETFILE
n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
#ifdef _USE_KNETFILE
save_zindex(rz, rz->x.fpw);
if(is_big_endian()){
- write(rz->x.fpw, &rz->in, sizeof(int64_t));
- write(rz->x.fpw, &rz->out, sizeof(int64_t));
+ if (write(rz->x.fpw, &rz->in, sizeof(int64_t)) < 0) {
+ fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
+ abort();
+ }
+ if (write(rz->x.fpw, &rz->out, sizeof(int64_t)) < 0) {
+ fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
+ abort();
+ }
} else {
uint64_t v64 = byte_swap_8((uint64_t)rz->in);
- write(rz->x.fpw, &v64, sizeof(int64_t));
+ if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
+ fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
+ abort();
+ }
v64 = byte_swap_8((uint64_t)rz->out);
- write(rz->x.fpw, &v64, sizeof(int64_t));
+ if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
+ fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
+ abort();
+ }
}
#else
save_zindex(rz, rz->filedes);
if(is_big_endian()){
- write(rz->filedes, &rz->in, sizeof(int64_t));
- write(rz->filedes, &rz->out, sizeof(int64_t));
+ if (write(rz->filedes, &rz->in, sizeof(int64_t)) < 0) {
+ fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
+ abort();
+ }
+ if (write(rz->filedes, &rz->out, sizeof(int64_t)) < 0) {
+ fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
+ abort();
+ }
} else {
uint64_t v64 = byte_swap_8((uint64_t)rz->in);
- write(rz->filedes, &v64, sizeof(int64_t));
+ if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
+ fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
+ abort();
+ }
v64 = byte_swap_8((uint64_t)rz->out);
- write(rz->filedes, &v64, sizeof(int64_t));
+ if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
+ fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
+ abort();
+ }
}
#endif
#endif