X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=razf.c;h=f67447c33bde6478787a5f95c9d60a14061bb6a3;hb=17d79b44a564aa4d5853bbc0b5f97ec2cefc927b;hp=e7499f9f61e6c2fae4ec71a5f4a500ed5b151fe8;hpb=317f5e8dd22cc9e1e5e05fbcaeb3b9aca7447351;p=samtools.git diff --git a/razf.c b/razf.c index e7499f9..f67447c 100644 --- a/razf.c +++ b/razf.c @@ -79,10 +79,17 @@ static inline int is_big_endian(){ #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(); + } } 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]; @@ -90,21 +97,39 @@ static void add_zindex(RAZF *rz, int64_t in, int64_t out){ } 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;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); for(i=0;iindex->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 @@ -115,29 +140,52 @@ static void load_zindex(RAZF *rz, int fd){ #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;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); for(i=0;iindex->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]); @@ -156,25 +204,68 @@ static RAZF* razf_open_w(int fd){ #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 @@ -190,6 +281,7 @@ static RAZF* razf_open_w(int fd){ 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){ @@ -197,11 +289,15 @@ static void _razf_write(RAZF* rz, const void *data, int size){ 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; @@ -211,17 +307,22 @@ static void _razf_write(RAZF* rz, const void *data, int size){ } 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; } @@ -230,11 +331,15 @@ static void razf_flush(RAZF *rz){ 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; @@ -244,6 +349,7 @@ static void razf_flush(RAZF *rz){ } 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); @@ -254,11 +360,15 @@ static void razf_end_flush(RAZF *rz){ 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; @@ -355,7 +465,10 @@ static RAZF* razf_open_r(int fd, int _load_index){ 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; @@ -365,9 +478,24 @@ static RAZF* razf_open_r(int fd, int _load_index){ #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); @@ -801,24 +929,48 @@ void razf_close(RAZF *rz){ #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