2 * RAZF : Random Access compressed(Z) File
4 * Release Date: 2008-10-27
6 * Copyright 2008, Jue Ruan <ruanjue@gmail.com>, Heng Li <lh3@sanger.ac.uk>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #if ZLIB_VERNUM < 0x1221
58 #warning "zlib < 1.2.2.1; RAZF writing is disabled."
61 #define DEF_MEM_LEVEL 8
63 static inline uint32_t byte_swap_4(uint32_t v){
64 v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
65 return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
68 static inline uint64_t byte_swap_8(uint64_t v){
69 v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
70 v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
71 return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
74 static inline int is_big_endian(){
77 return (c[0] != 0x01);
81 static void add_zindex(RAZF *rz, int64_t in, int64_t out){
85 if(rz->index->size == rz->index->cap){
86 rz->index->cap = rz->index->cap * 1.5 + 2;
87 cores = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
88 bores = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
89 if ((cores == NULL) || (bores == NULL)) {
90 fprintf(stderr, "[%s] failure to allocate space for new zindex.\n", __func__);
93 rz->index->cell_offsets = cores;
94 rz->index->bin_offsets = bores;
96 if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out;
97 rz->index->cell_offsets[rz->index->size] = out - rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE];
101 static void save_zindex(RAZF *rz, int fd){
105 is_be = is_big_endian();
107 if (write(fd, &rz->index->size, sizeof(int)) < 0) {
108 fprintf(stderr, "[%s] failure to write zindex size.\n", __func__);
113 v32 = byte_swap_4((uint32_t)rz->index->size);
114 if (write(fd, &v32, sizeof(uint32_t)) < 0) {
115 fprintf(stderr, "[%s] failure to write zindex size.\n", __func__);
119 v32 = rz->index->size / RZ_BIN_SIZE + 1;
121 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
122 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
124 count = sizeof(int64_t) * v32;
125 if (write(fd, rz->index->bin_offsets, count) < 0) {
126 fprintf(stderr, "[%s] failure to write zindex bin_offsets.\n", __func__);
130 count = sizeof(int32_t) * rz->index->size;
131 if (write(fd, rz->index->cell_offsets, count) < 0) {
132 fprintf(stderr, "[%s] failure to write zindex cell_offsets.\n", __func__);
139 static void load_zindex(RAZF *rz, knetFile *fp){
141 static void load_zindex(RAZF *rz, int fd){
146 if(!rz->load_index) return;
147 if(rz->index == NULL) {
148 if ((rz->index = malloc(sizeof(ZBlockIndex))) == NULL) {
149 fprintf(stderr, "[%s] failure to allocate index.\n", __func__);
153 is_be = is_big_endian();
155 if (knet_read(fp, &rz->index->size, sizeof(int)) < 0) {
157 if (read(fd, &rz->index->size, sizeof(int)) < 0) {
159 fprintf(stderr, "[%s] failure to read zindex size.\n", __func__);
162 if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
163 rz->index->cap = rz->index->size;
164 v32 = rz->index->size / RZ_BIN_SIZE + 1;
165 count = sizeof(int64_t) * v32;
166 if ((rz->index->bin_offsets = malloc(count)) == NULL) {
167 fprintf(stderr, "[%s] failure to allocate bin_offsets array.\n", __func__);
171 if (knet_read(fp, rz->index->bin_offsets, count) < 0) {
173 if (read(fd, rz->index->bin_offsets, count) < 0) {
175 fprintf(stderr, "[%s] failure to read bin_offsets.\n", __func__);
178 count = sizeof(int) * rz->index->size;
179 if ((rz->index->cell_offsets = malloc(count)) == NULL) {
180 fprintf(stderr, "[%s] failure to allocate cell_offsets array.\n", __func__);
184 if (knet_read(fp, rz->index->cell_offsets, count) < count) {
186 if (read(fd, rz->index->cell_offsets, count) < count) {
188 fprintf(stderr, "[%s] failure to read cell_offsets.\n", __func__);
192 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
193 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
198 static RAZF* razf_open_w(int fd)
200 fprintf(stderr, "[razf_open_w] Writing is not available with zlib ver < 1.2.2.1\n");
204 static RAZF* razf_open_w(int fd){
207 setmode(fd, O_BINARY);
209 if ((rz = calloc(1, sizeof(RAZF))) == NULL) {
210 fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__);
219 if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) {
220 fprintf(stderr, "[%s] failure to allocate stream buffer.\n", __func__);
224 if ((rz->inbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
225 fprintf(stderr, "[%s] failure to allocate input buffer.\n", __func__);
230 if ((rz->outbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
231 fprintf(stderr, "[%s] failure to allocate output buffer.\n", __func__);
237 if ((rz->index = calloc(sizeof(ZBlockIndex), 1)) == NULL) {
238 fprintf(stderr, "[%s] failure to allocate index.\n", __func__);
246 deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
247 rz->stream->avail_out = RZ_BUFFER_SIZE;
248 rz->stream->next_out = rz->outbuf;
249 if ((rz->header = calloc(sizeof(gz_header), 1)) == NULL) {
250 fprintf(stderr, "[%s] failure to allocate header buffer.\n", __func__);
258 rz->header->os = 0x03; //Unix
259 rz->header->text = 0;
260 rz->header->time = 0;
261 if ((rz->header->extra = malloc(7)) == NULL) {
262 fprintf(stderr, "[%s] failure to allocate header buffer.\n", __func__);
271 strncpy((char*)rz->header->extra, "RAZF", 4);
272 rz->header->extra[4] = 1; // obsolete field
273 // block size = RZ_BLOCK_SIZE, Big-Endian
274 rz->header->extra[5] = RZ_BLOCK_SIZE >> 8;
275 rz->header->extra[6] = RZ_BLOCK_SIZE & 0xFF;
276 rz->header->extra_len = 7;
277 rz->header->name = rz->header->comment = 0;
278 rz->header->hcrc = 0;
279 deflateSetHeader(rz->stream, rz->header);
280 rz->block_pos = rz->block_off = 0;
284 static void _razf_write(RAZF* rz, const void *data, int size){
287 rz->stream->avail_in = size;
288 rz->stream->next_in = (void*)data;
290 tout = rz->stream->avail_out;
291 deflate(rz->stream, Z_NO_FLUSH);
292 rz->out += tout - rz->stream->avail_out;
293 if(rz->stream->avail_out) break;
294 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
296 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
298 if (write(rz->filedes, rz->outbuf, count) < 0) {
300 fprintf(stderr, "[%s] failed to write output buffer.\n", __func__);
303 rz->stream->avail_out = RZ_BUFFER_SIZE;
304 rz->stream->next_out = rz->outbuf;
305 if(rz->stream->avail_in == 0) break;
307 rz->in += size - rz->stream->avail_in;
308 rz->block_off += size - rz->stream->avail_in;
311 static void razf_flush(RAZF *rz){
315 _razf_write(rz, rz->inbuf, rz->buf_len);
316 rz->buf_off = rz->buf_len = 0;
318 if(rz->stream->avail_out){
319 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
321 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
323 if (write(rz->filedes, rz->outbuf, count) < 0) {
325 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
328 rz->stream->avail_out = RZ_BUFFER_SIZE;
329 rz->stream->next_out = rz->outbuf;
332 tout = rz->stream->avail_out;
333 deflate(rz->stream, Z_FULL_FLUSH);
334 rz->out += tout - rz->stream->avail_out;
335 if(rz->stream->avail_out == 0){
336 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
338 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
340 if (write(rz->filedes, rz->outbuf, count) < 0) {
342 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
345 rz->stream->avail_out = RZ_BUFFER_SIZE;
346 rz->stream->next_out = rz->outbuf;
349 rz->block_pos = rz->out;
353 static void razf_end_flush(RAZF *rz){
357 _razf_write(rz, rz->inbuf, rz->buf_len);
358 rz->buf_off = rz->buf_len = 0;
361 tout = rz->stream->avail_out;
362 deflate(rz->stream, Z_FINISH);
363 rz->out += tout - rz->stream->avail_out;
364 if(rz->stream->avail_out < RZ_BUFFER_SIZE){
365 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
367 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
369 if (write(rz->filedes, rz->outbuf, count) < 0) {
371 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
374 rz->stream->avail_out = RZ_BUFFER_SIZE;
375 rz->stream->next_out = rz->outbuf;
380 static void _razf_buffered_write(RAZF *rz, const void *data, int size){
383 if(rz->buf_len == RZ_BUFFER_SIZE){
384 _razf_write(rz, rz->inbuf, rz->buf_len);
387 if(size + rz->buf_len < RZ_BUFFER_SIZE){
388 for(i=0;i<size;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
392 n = RZ_BUFFER_SIZE - rz->buf_len;
393 for(i=0;i<n;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
401 int razf_write(RAZF* rz, const void *data, int size){
405 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
406 while(rz->in + rz->buf_len + size >= next_block){
407 n = next_block - rz->in - rz->buf_len;
408 _razf_buffered_write(rz, data, n);
412 add_zindex(rz, rz->in, rz->out);
413 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
415 _razf_buffered_write(rz, data, size);
421 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
422 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
423 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
424 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
425 #define COMMENT 0x10 /* bit 4 set: file comment present */
426 #define RESERVED 0xE0 /* bits 5..7: reserved */
428 static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *extra_len){
429 int method, flags, n, len;
430 if(size < 2) return 0;
431 if(data[0] != 0x1f || data[1] != 0x8b) return 0;
432 if(size < 4) return 0;
435 if(method != Z_DEFLATED || (flags & RESERVED)) return 0;
436 n = 4 + 6; // Skip 6 bytes
439 if(flags & EXTRA_FIELD){
440 if(size < n + 2) return 0;
441 len = ((int)data[n + 1] << 8) | data[n];
445 if(n >= size) return 0;
449 *extra_len = n - (*extra_off);
451 if(flags & ORIG_NAME) while(n < size && data[n++]);
452 if(flags & COMMENT) while(n < size && data[n++]);
453 if(flags & HEAD_CRC){
454 if(n + 2 > size) return 0;
461 static RAZF* razf_open_r(knetFile *fp, int _load_index){
463 static RAZF* razf_open_r(int fd, int _load_index){
466 int ext_off, ext_len;
469 unsigned char c[] = "RAZF";
470 if ((rz = calloc(1, sizeof(RAZF))) == NULL) {
471 fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__);
479 setmode(fd, O_BINARY);
483 if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) {
484 fprintf(stderr, "[%s] failure to allocate z_stream.\n", __func__);
488 if ((rz->inbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
489 fprintf(stderr, "[%s] failure to allocate input buffer.\n", __func__);
494 if ((rz->outbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
495 fprintf(stderr, "[%s] failure to allocate output buffer.\n", __func__);
501 rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
503 n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
505 n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
507 ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
511 rz->file_type = FILE_TYPE_PLAIN;
512 memcpy(rz->outbuf, rz->inbuf, n);
518 rz->header_size = ret;
519 ret = inflateInit2(rz->stream, -WINDOW_BITS);
520 if(ret != Z_OK){ inflateEnd(rz->stream); goto PLAIN_FILE;}
521 rz->stream->avail_in = n - rz->header_size;
522 rz->stream->next_in = rz->inbuf + rz->header_size;
523 rz->stream->avail_out = RZ_BUFFER_SIZE;
524 rz->stream->next_out = rz->outbuf;
525 rz->file_type = FILE_TYPE_GZ;
526 rz->in = rz->header_size;
527 rz->block_pos = rz->header_size;
528 rz->next_block_pos = rz->header_size;
530 if(ext_len < 7 || memcmp(rz->inbuf + ext_off, c, 4) != 0) return rz;
531 if(((((unsigned char*)rz->inbuf)[ext_off + 5] << 8) | ((unsigned char*)rz->inbuf)[ext_off + 6]) != RZ_BLOCK_SIZE){
532 fprintf(stderr, " -- WARNING: RZ_BLOCK_SIZE is not %d, treat source as gz file. in %s -- %s:%d --\n", RZ_BLOCK_SIZE, __FUNCTION__, __FILE__, __LINE__);
535 rz->load_index = _load_index;
536 rz->file_type = FILE_TYPE_RZ;
538 if(knet_seek(fp, -16, SEEK_END) == -1){
540 if(lseek(fd, -16, SEEK_END) == -1){
545 rz->src_end = rz->end = 0x7FFFFFFFFFFFFFFFLL;
547 is_be = is_big_endian();
550 knet_read(fp, &end, sizeof(int64_t));
552 read(fd, &end, sizeof(int64_t));
554 if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
555 else rz->src_end = end;
558 knet_read(fp, &end, sizeof(int64_t));
560 read(fd, &end, sizeof(int64_t));
562 if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
565 rz->stream->avail_in -= n - rz->end;
568 if(rz->end > rz->src_end){
570 knet_seek(fp, rz->in, SEEK_SET);
572 lseek(fd, rz->in, SEEK_SET);
577 knet_seek(fp, rz->end, SEEK_SET);
578 if(knet_tell(fp) != rz->end){
579 knet_seek(fp, rz->in, SEEK_SET);
581 if(lseek(fd, rz->end, SEEK_SET) != rz->end){
582 lseek(fd, rz->in, SEEK_SET);
588 knet_seek(fp, n, SEEK_SET);
591 lseek(fd, n, SEEK_SET);
598 RAZF* razf_dopen(int fd, const char *mode){
599 if (strstr(mode, "r")) fprintf(stderr,"[razf_dopen] implement me\n");
600 else if(strstr(mode, "w")) return razf_open_w(fd);
604 RAZF* razf_dopen2(int fd, const char *mode)
606 fprintf(stderr,"[razf_dopen2] implement me\n");
610 RAZF* razf_dopen(int fd, const char *mode){
611 if(strstr(mode, "r")) return razf_open_r(fd, 1);
612 else if(strstr(mode, "w")) return razf_open_w(fd);
616 RAZF* razf_dopen2(int fd, const char *mode)
618 if(strstr(mode, "r")) return razf_open_r(fd, 0);
619 else if(strstr(mode, "w")) return razf_open_w(fd);
624 static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
627 if(strstr(mode, "r")){
629 knetFile *fd = knet_open(filename, "r");
631 fprintf(stderr, "[_razf_open] fail to open %s\n", filename);
636 fd = open(filename, O_RDONLY | O_BINARY);
638 fd = open(filename, O_RDONLY);
641 if(fd < 0) return NULL;
642 rz = razf_open_r(fd, _load_index);
643 } else if(strstr(mode, "w")){
645 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
647 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
649 if(fd < 0) return NULL;
650 rz = razf_open_w(fd);
655 RAZF* razf_open(const char *filename, const char *mode){
656 return _razf_open(filename, mode, 1);
659 RAZF* razf_open2(const char *filename, const char *mode){
660 return _razf_open(filename, mode, 0);
663 int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){
665 if(rz->mode != 'r' && rz->mode != 'R') return 0;
666 switch(rz->file_type){
667 case FILE_TYPE_PLAIN:
668 if(rz->end == 0x7fffffffffffffffLL){
670 if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
671 n = knet_tell(rz->x.fpr);
672 knet_seek(rz->x.fpr, 0, SEEK_END);
673 rz->end = knet_tell(rz->x.fpr);
674 knet_seek(rz->x.fpr, n, SEEK_SET);
676 if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
677 rz->end = lseek(rz->filedes, 0, SEEK_END);
678 lseek(rz->filedes, n, SEEK_SET);
681 *u_size = *c_size = rz->end;
686 if(rz->src_end == rz->end) return 0;
687 *u_size = rz->src_end;
695 static int _razf_read(RAZF* rz, void *data, int size){
697 if(rz->z_eof || rz->z_err) return 0;
698 if (rz->file_type == FILE_TYPE_PLAIN) {
700 ret = knet_read(rz->x.fpr, data, size);
702 ret = read(rz->filedes, data, size);
704 if (ret == 0) rz->z_eof = 1;
707 rz->stream->avail_out = size;
708 rz->stream->next_out = data;
709 while(rz->stream->avail_out){
710 if(rz->stream->avail_in == 0){
711 if(rz->in >= rz->end){ rz->z_eof = 1; break; }
712 if(rz->end - rz->in < RZ_BUFFER_SIZE){
714 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
716 rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
720 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
722 rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
725 if(rz->stream->avail_in == 0){
729 rz->stream->next_in = rz->inbuf;
731 tin = rz->stream->avail_in;
732 ret = inflate(rz->stream, Z_BLOCK);
733 rz->in += tin - rz->stream->avail_in;
734 if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
735 fprintf(stderr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
739 if(ret == Z_STREAM_END){
743 if ((rz->stream->data_type&128) && !(rz->stream->data_type&64)){
745 rz->next_block_pos = rz->in;
749 return size - rz->stream->avail_out;
752 int razf_read(RAZF *rz, void *data, int size){
757 if(size < rz->buf_len){
758 for(i=0;i<size;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
762 rz->block_off += size;
766 for(i=0;i<rz->buf_len;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
769 rz->block_off += rz->buf_len;
773 rz->block_pos = rz->next_block_pos;
778 } else if(rz->buf_flush){
779 rz->block_pos = rz->next_block_pos;
783 if(rz->buf_flush) continue;
784 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
785 if(rz->z_eof && rz->buf_len == 0) break;
787 rz->out += ori_size - size;
788 return ori_size - size;
791 int razf_skip(RAZF* rz, int size){
796 if(size < rz->buf_len){
799 rz->block_off += size;
806 rz->block_off += rz->buf_len;
808 rz->block_pos = rz->next_block_pos;
813 } else if(rz->buf_flush){
814 rz->block_pos = rz->next_block_pos;
818 if(rz->buf_flush) continue;
819 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
820 if(rz->z_eof || rz->z_err) break;
822 rz->out += ori_size - size;
823 return ori_size - size;
826 static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
828 knet_seek(rz->x.fpr, in, SEEK_SET);
830 lseek(rz->filedes, in, SEEK_SET);
835 rz->next_block_pos = in;
838 rz->z_eof = rz->z_err = 0;
839 inflateReset(rz->stream);
840 rz->stream->avail_in = 0;
841 rz->buf_off = rz->buf_len = 0;
844 int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){
847 if(rz->file_type == FILE_TYPE_PLAIN){
848 rz->buf_off = rz->buf_len = 0;
849 pos = block_start + block_offset;
851 knet_seek(rz->x.fpr, pos, SEEK_SET);
852 pos = knet_tell(rz->x.fpr);
854 pos = lseek(rz->filedes, pos, SEEK_SET);
856 rz->out = rz->in = pos;
859 if(block_start == rz->block_pos && block_offset >= rz->block_off) {
860 block_offset -= rz->block_off;
861 goto SKIP; // Needn't reset inflate
863 if(block_start == 0) block_start = rz->header_size; // Automaticly revist wrong block_start
864 _razf_reset_read(rz, block_start, 0);
866 if(block_offset) razf_skip(rz, block_offset);
867 return rz->block_off;
870 int64_t razf_seek(RAZF* rz, int64_t pos, int where){
872 int64_t seek_pos, new_out;
874 if (where == SEEK_CUR) pos += rz->out;
875 else if (where == SEEK_END) pos += rz->src_end;
876 if(rz->file_type == FILE_TYPE_PLAIN){
878 knet_seek(rz->x.fpr, pos, SEEK_SET);
879 seek_pos = knet_tell(rz->x.fpr);
881 seek_pos = lseek(rz->filedes, pos, SEEK_SET);
883 rz->buf_off = rz->buf_len = 0;
884 rz->out = rz->in = seek_pos;
886 } else if(rz->file_type == FILE_TYPE_GZ){
887 if(pos >= rz->out) goto SKIP;
890 if(pos == rz->out) return pos;
891 if(pos > rz->src_end) return rz->out;
892 if(!rz->seekable || !rz->load_index){
893 if(pos >= rz->out) goto SKIP;
895 idx = pos / RZ_BLOCK_SIZE - 1;
896 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
897 new_out = (idx + 1) * RZ_BLOCK_SIZE;
898 if(pos > rz->out && new_out <= rz->out) goto SKIP;
899 _razf_reset_read(rz, seek_pos, new_out);
901 razf_skip(rz, (int)(pos - rz->out));
905 uint64_t razf_tell2(RAZF *rz)
908 if (rz->load_index) {
909 int64_t idx, seek_pos;
910 idx = rz->out / RZ_BLOCK_SIZE - 1;
911 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
912 if (seek_pos != rz->block_pos || rz->out%RZ_BLOCK_SIZE != rz->block_off)
913 fprintf(stderr, "[razf_tell2] inconsistent block offset: (%lld, %lld) != (%lld, %lld)\n",
914 (long long)seek_pos, (long long)rz->out%RZ_BLOCK_SIZE, (long long)rz->block_pos, (long long) rz->block_off);
917 return (uint64_t)rz->block_pos<<16 | (rz->block_off&0xffff);
920 int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
922 if (where != SEEK_SET) return -1;
923 return razf_jump(rz, voffset>>16, voffset&0xffff);
926 void razf_close(RAZF *rz){
930 deflateEnd(rz->stream);
932 save_zindex(rz, rz->x.fpw);
934 if (write(rz->x.fpw, &rz->in, sizeof(int64_t)) < 0) {
935 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
938 if (write(rz->x.fpw, &rz->out, sizeof(int64_t)) < 0) {
939 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
943 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
944 if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
945 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
948 v64 = byte_swap_8((uint64_t)rz->out);
949 if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
950 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
955 save_zindex(rz, rz->filedes);
957 if (write(rz->filedes, &rz->in, sizeof(int64_t)) < 0) {
958 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
961 if (write(rz->filedes, &rz->out, sizeof(int64_t)) < 0) {
962 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
966 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
967 if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
968 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
971 v64 = byte_swap_8((uint64_t)rz->out);
972 if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
973 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
979 } else if(rz->mode == 'r'){
980 if(rz->stream) inflateEnd(rz->stream);
982 if(rz->inbuf) free(rz->inbuf);
983 if(rz->outbuf) free(rz->outbuf);
985 free(rz->header->extra);
986 free(rz->header->name);
987 free(rz->header->comment);
991 free(rz->index->bin_offsets);
992 free(rz->index->cell_offsets);
998 knet_close(rz->x.fpr);