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, RAZF_STRUCT_SIZE)) == NULL) {
210 fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__);
213 rz->stream = (z_stream *)rz + sizeof(RAZF);
214 rz->inbuf = rz->stream + sizeof(z_stream);
215 rz->outbuf = rz->inbuf + RZ_BUFFER_SIZE;
216 rz->index = rz->outbuf + RZ_BUFFER_SIZE;
217 rz->header = (gz_header *)rz->index + sizeof(ZBlockIndex);
218 rz->header->extra = (Bytef *)rz->header + sizeof(gz_header);
225 deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
226 rz->stream->avail_out = RZ_BUFFER_SIZE;
227 rz->stream->next_out = rz->outbuf;
228 rz->header->os = 0x03; //Unix
229 rz->header->text = 0;
230 rz->header->time = 0;
231 strncpy((char*)rz->header->extra, "RAZF", 4);
232 rz->header->extra[4] = 1; // obsolete field
233 // block size = RZ_BLOCK_SIZE, Big-Endian
234 rz->header->extra[5] = RZ_BLOCK_SIZE >> 8;
235 rz->header->extra[6] = RZ_BLOCK_SIZE & 0xFF;
236 rz->header->extra_len = 7;
237 rz->header->name = rz->header->comment = 0;
238 rz->header->hcrc = 0;
239 deflateSetHeader(rz->stream, rz->header);
240 rz->block_pos = rz->block_off = 0;
244 static void _razf_write(RAZF* rz, const void *data, int size){
247 rz->stream->avail_in = size;
248 rz->stream->next_in = (void*)data;
250 tout = rz->stream->avail_out;
251 deflate(rz->stream, Z_NO_FLUSH);
252 rz->out += tout - rz->stream->avail_out;
253 if(rz->stream->avail_out) break;
254 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
256 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
258 if (write(rz->filedes, rz->outbuf, count) < 0) {
260 fprintf(stderr, "[%s] failed to write output buffer.\n", __func__);
263 rz->stream->avail_out = RZ_BUFFER_SIZE;
264 rz->stream->next_out = rz->outbuf;
265 if(rz->stream->avail_in == 0) break;
267 rz->in += size - rz->stream->avail_in;
268 rz->block_off += size - rz->stream->avail_in;
271 static void razf_flush(RAZF *rz){
275 _razf_write(rz, rz->inbuf, rz->buf_len);
276 rz->buf_off = rz->buf_len = 0;
278 if(rz->stream->avail_out){
279 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
281 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
283 if (write(rz->filedes, rz->outbuf, count) < 0) {
285 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
288 rz->stream->avail_out = RZ_BUFFER_SIZE;
289 rz->stream->next_out = rz->outbuf;
292 tout = rz->stream->avail_out;
293 deflate(rz->stream, Z_FULL_FLUSH);
294 rz->out += tout - rz->stream->avail_out;
295 if(rz->stream->avail_out == 0){
296 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
298 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
300 if (write(rz->filedes, rz->outbuf, count) < 0) {
302 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
305 rz->stream->avail_out = RZ_BUFFER_SIZE;
306 rz->stream->next_out = rz->outbuf;
309 rz->block_pos = rz->out;
313 static void razf_end_flush(RAZF *rz){
317 _razf_write(rz, rz->inbuf, rz->buf_len);
318 rz->buf_off = rz->buf_len = 0;
321 tout = rz->stream->avail_out;
322 deflate(rz->stream, Z_FINISH);
323 rz->out += tout - rz->stream->avail_out;
324 if(rz->stream->avail_out < RZ_BUFFER_SIZE){
325 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
327 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
329 if (write(rz->filedes, rz->outbuf, count) < 0) {
331 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
334 rz->stream->avail_out = RZ_BUFFER_SIZE;
335 rz->stream->next_out = rz->outbuf;
340 static void _razf_buffered_write(RAZF *rz, const void *data, int size){
343 if(rz->buf_len == RZ_BUFFER_SIZE){
344 _razf_write(rz, rz->inbuf, rz->buf_len);
347 if(size + rz->buf_len < RZ_BUFFER_SIZE){
348 for(i=0;i<size;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
352 n = RZ_BUFFER_SIZE - rz->buf_len;
353 for(i=0;i<n;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
361 int razf_write(RAZF* rz, const void *data, int size){
365 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
366 while(rz->in + rz->buf_len + size >= next_block){
367 n = next_block - rz->in - rz->buf_len;
368 _razf_buffered_write(rz, data, n);
372 add_zindex(rz, rz->in, rz->out);
373 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
375 _razf_buffered_write(rz, data, size);
381 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
382 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
383 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
384 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
385 #define COMMENT 0x10 /* bit 4 set: file comment present */
386 #define RESERVED 0xE0 /* bits 5..7: reserved */
388 static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *extra_len){
389 int method, flags, n, len;
390 if(size < 2) return 0;
391 if(data[0] != 0x1f || data[1] != 0x8b) return 0;
392 if(size < 4) return 0;
395 if(method != Z_DEFLATED || (flags & RESERVED)) return 0;
396 n = 4 + 6; // Skip 6 bytes
399 if(flags & EXTRA_FIELD){
400 if(size < n + 2) return 0;
401 len = ((int)data[n + 1] << 8) | data[n];
405 if(n >= size) return 0;
409 *extra_len = n - (*extra_off);
411 if(flags & ORIG_NAME) while(n < size && data[n++]);
412 if(flags & COMMENT) while(n < size && data[n++]);
413 if(flags & HEAD_CRC){
414 if(n + 2 > size) return 0;
421 static RAZF* razf_open_r(knetFile *fp, int _load_index){
423 static RAZF* razf_open_r(int fd, int _load_index){
426 int ext_off, ext_len;
429 unsigned char c[] = "RAZF";
430 if ((rz = calloc(1, RAZF_STRUCT_SIZE)) == NULL) {
431 fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__);
434 rz->stream = (z_stream *)rz + sizeof(RAZF);
435 rz->inbuf = rz->stream + sizeof(z_stream);
436 rz->outbuf = rz->inbuf + RZ_BUFFER_SIZE;
437 rz->index = rz->outbuf + RZ_BUFFER_SIZE;
438 rz->header = (gz_header *)rz->index + sizeof(ZBlockIndex);
439 rz->header->extra = (Bytef *)rz->header + sizeof(gz_header);
445 setmode(fd, O_BINARY);
449 rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
451 n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
453 n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
455 ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
459 rz->file_type = FILE_TYPE_PLAIN;
460 memcpy(rz->outbuf, rz->inbuf, n);
466 rz->header_size = ret;
467 ret = inflateInit2(rz->stream, -WINDOW_BITS);
468 if(ret != Z_OK){ inflateEnd(rz->stream); goto PLAIN_FILE;}
469 rz->stream->avail_in = n - rz->header_size;
470 rz->stream->next_in = rz->inbuf + rz->header_size;
471 rz->stream->avail_out = RZ_BUFFER_SIZE;
472 rz->stream->next_out = rz->outbuf;
473 rz->file_type = FILE_TYPE_GZ;
474 rz->in = rz->header_size;
475 rz->block_pos = rz->header_size;
476 rz->next_block_pos = rz->header_size;
478 if(ext_len < 7 || memcmp(rz->inbuf + ext_off, c, 4) != 0) return rz;
479 if(((((unsigned char*)rz->inbuf)[ext_off + 5] << 8) | ((unsigned char*)rz->inbuf)[ext_off + 6]) != RZ_BLOCK_SIZE){
480 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__);
483 rz->load_index = _load_index;
484 rz->file_type = FILE_TYPE_RZ;
486 if(knet_seek(fp, -16, SEEK_END) == -1){
488 if(lseek(fd, -16, SEEK_END) == -1){
493 rz->src_end = rz->end = 0x7FFFFFFFFFFFFFFFLL;
495 is_be = is_big_endian();
498 knet_read(fp, &end, sizeof(int64_t));
500 read(fd, &end, sizeof(int64_t));
502 if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
503 else rz->src_end = end;
506 knet_read(fp, &end, sizeof(int64_t));
508 read(fd, &end, sizeof(int64_t));
510 if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
513 rz->stream->avail_in -= n - rz->end;
516 if(rz->end > rz->src_end){
518 knet_seek(fp, rz->in, SEEK_SET);
520 lseek(fd, rz->in, SEEK_SET);
525 knet_seek(fp, rz->end, SEEK_SET);
526 if(knet_tell(fp) != rz->end){
527 knet_seek(fp, rz->in, SEEK_SET);
529 if(lseek(fd, rz->end, SEEK_SET) != rz->end){
530 lseek(fd, rz->in, SEEK_SET);
536 knet_seek(fp, n, SEEK_SET);
539 lseek(fd, n, SEEK_SET);
546 RAZF* razf_dopen(int fd, const char *mode){
547 if (strstr(mode, "r")) fprintf(stderr,"[razf_dopen] implement me\n");
548 else if(strstr(mode, "w")) return razf_open_w(fd);
552 RAZF* razf_dopen2(int fd, const char *mode)
554 fprintf(stderr,"[razf_dopen2] implement me\n");
558 RAZF* razf_dopen(int fd, const char *mode){
559 if(strstr(mode, "r")) return razf_open_r(fd, 1);
560 else if(strstr(mode, "w")) return razf_open_w(fd);
564 RAZF* razf_dopen2(int fd, const char *mode)
566 if(strstr(mode, "r")) return razf_open_r(fd, 0);
567 else if(strstr(mode, "w")) return razf_open_w(fd);
572 static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
575 if(strstr(mode, "r")){
577 knetFile *fd = knet_open(filename, "r");
579 fprintf(stderr, "[_razf_open] fail to open %s\n", filename);
584 fd = open(filename, O_RDONLY | O_BINARY);
586 fd = open(filename, O_RDONLY);
589 if(fd < 0) return NULL;
590 rz = razf_open_r(fd, _load_index);
591 } else if(strstr(mode, "w")){
593 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
595 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
597 if(fd < 0) return NULL;
598 rz = razf_open_w(fd);
603 RAZF* razf_open(const char *filename, const char *mode){
604 return _razf_open(filename, mode, 1);
607 RAZF* razf_open2(const char *filename, const char *mode){
608 return _razf_open(filename, mode, 0);
611 int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){
613 if(rz->mode != 'r' && rz->mode != 'R') return 0;
614 switch(rz->file_type){
615 case FILE_TYPE_PLAIN:
616 if(rz->end == 0x7fffffffffffffffLL){
618 if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
619 n = knet_tell(rz->x.fpr);
620 knet_seek(rz->x.fpr, 0, SEEK_END);
621 rz->end = knet_tell(rz->x.fpr);
622 knet_seek(rz->x.fpr, n, SEEK_SET);
624 if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
625 rz->end = lseek(rz->filedes, 0, SEEK_END);
626 lseek(rz->filedes, n, SEEK_SET);
629 *u_size = *c_size = rz->end;
634 if(rz->src_end == rz->end) return 0;
635 *u_size = rz->src_end;
643 static int _razf_read(RAZF* rz, void *data, int size){
645 if(rz->z_eof || rz->z_err) return 0;
646 if (rz->file_type == FILE_TYPE_PLAIN) {
648 ret = knet_read(rz->x.fpr, data, size);
650 ret = read(rz->filedes, data, size);
652 if (ret == 0) rz->z_eof = 1;
655 rz->stream->avail_out = size;
656 rz->stream->next_out = data;
657 while(rz->stream->avail_out){
658 if(rz->stream->avail_in == 0){
659 if(rz->in >= rz->end){ rz->z_eof = 1; break; }
660 if(rz->end - rz->in < RZ_BUFFER_SIZE){
662 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
664 rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
668 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
670 rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
673 if(rz->stream->avail_in == 0){
677 rz->stream->next_in = rz->inbuf;
679 tin = rz->stream->avail_in;
680 ret = inflate(rz->stream, Z_BLOCK);
681 rz->in += tin - rz->stream->avail_in;
682 if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
683 fprintf(stderr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
687 if(ret == Z_STREAM_END){
691 if ((rz->stream->data_type&128) && !(rz->stream->data_type&64)){
693 rz->next_block_pos = rz->in;
697 return size - rz->stream->avail_out;
700 int razf_read(RAZF *rz, void *data, int size){
705 if(size < rz->buf_len){
706 for(i=0;i<size;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
710 rz->block_off += size;
714 for(i=0;i<rz->buf_len;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
717 rz->block_off += rz->buf_len;
721 rz->block_pos = rz->next_block_pos;
726 } else if(rz->buf_flush){
727 rz->block_pos = rz->next_block_pos;
731 if(rz->buf_flush) continue;
732 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
733 if(rz->z_eof && rz->buf_len == 0) break;
735 rz->out += ori_size - size;
736 return ori_size - size;
739 int razf_skip(RAZF* rz, int size){
744 if(size < rz->buf_len){
747 rz->block_off += size;
754 rz->block_off += rz->buf_len;
756 rz->block_pos = rz->next_block_pos;
761 } else if(rz->buf_flush){
762 rz->block_pos = rz->next_block_pos;
766 if(rz->buf_flush) continue;
767 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
768 if(rz->z_eof || rz->z_err) break;
770 rz->out += ori_size - size;
771 return ori_size - size;
774 static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
776 knet_seek(rz->x.fpr, in, SEEK_SET);
778 lseek(rz->filedes, in, SEEK_SET);
783 rz->next_block_pos = in;
786 rz->z_eof = rz->z_err = 0;
787 inflateReset(rz->stream);
788 rz->stream->avail_in = 0;
789 rz->buf_off = rz->buf_len = 0;
792 int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){
795 if(rz->file_type == FILE_TYPE_PLAIN){
796 rz->buf_off = rz->buf_len = 0;
797 pos = block_start + block_offset;
799 knet_seek(rz->x.fpr, pos, SEEK_SET);
800 pos = knet_tell(rz->x.fpr);
802 pos = lseek(rz->filedes, pos, SEEK_SET);
804 rz->out = rz->in = pos;
807 if(block_start == rz->block_pos && block_offset >= rz->block_off) {
808 block_offset -= rz->block_off;
809 goto SKIP; // Needn't reset inflate
811 if(block_start == 0) block_start = rz->header_size; // Automaticly revist wrong block_start
812 _razf_reset_read(rz, block_start, 0);
814 if(block_offset) razf_skip(rz, block_offset);
815 return rz->block_off;
818 int64_t razf_seek(RAZF* rz, int64_t pos, int where){
820 int64_t seek_pos, new_out;
822 if (where == SEEK_CUR) pos += rz->out;
823 else if (where == SEEK_END) pos += rz->src_end;
824 if(rz->file_type == FILE_TYPE_PLAIN){
826 knet_seek(rz->x.fpr, pos, SEEK_SET);
827 seek_pos = knet_tell(rz->x.fpr);
829 seek_pos = lseek(rz->filedes, pos, SEEK_SET);
831 rz->buf_off = rz->buf_len = 0;
832 rz->out = rz->in = seek_pos;
834 } else if(rz->file_type == FILE_TYPE_GZ){
835 if(pos >= rz->out) goto SKIP;
838 if(pos == rz->out) return pos;
839 if(pos > rz->src_end) return rz->out;
840 if(!rz->seekable || !rz->load_index){
841 if(pos >= rz->out) goto SKIP;
843 idx = pos / RZ_BLOCK_SIZE - 1;
844 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
845 new_out = (idx + 1) * RZ_BLOCK_SIZE;
846 if(pos > rz->out && new_out <= rz->out) goto SKIP;
847 _razf_reset_read(rz, seek_pos, new_out);
849 razf_skip(rz, (int)(pos - rz->out));
853 uint64_t razf_tell2(RAZF *rz)
856 if (rz->load_index) {
857 int64_t idx, seek_pos;
858 idx = rz->out / RZ_BLOCK_SIZE - 1;
859 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
860 if (seek_pos != rz->block_pos || rz->out%RZ_BLOCK_SIZE != rz->block_off)
861 fprintf(stderr, "[razf_tell2] inconsistent block offset: (%lld, %lld) != (%lld, %lld)\n",
862 (long long)seek_pos, (long long)rz->out%RZ_BLOCK_SIZE, (long long)rz->block_pos, (long long) rz->block_off);
865 return (uint64_t)rz->block_pos<<16 | (rz->block_off&0xffff);
868 int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
870 if (where != SEEK_SET) return -1;
871 return razf_jump(rz, voffset>>16, voffset&0xffff);
874 void razf_close(RAZF *rz){
878 deflateEnd(rz->stream);
880 save_zindex(rz, rz->x.fpw);
882 if (write(rz->x.fpw, &rz->in, sizeof(int64_t)) < 0) {
883 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
886 if (write(rz->x.fpw, &rz->out, sizeof(int64_t)) < 0) {
887 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
891 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
892 if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
893 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
896 v64 = byte_swap_8((uint64_t)rz->out);
897 if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
898 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
903 save_zindex(rz, rz->filedes);
905 if (write(rz->filedes, &rz->in, sizeof(int64_t)) < 0) {
906 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
909 if (write(rz->filedes, &rz->out, sizeof(int64_t)) < 0) {
910 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
914 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
915 if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
916 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
919 v64 = byte_swap_8((uint64_t)rz->out);
920 if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
921 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
927 } else if(rz->mode == 'r'){
928 if(rz->stream) inflateEnd(rz->stream);
930 if(rz->inbuf) free(rz->inbuf);
931 if(rz->outbuf) free(rz->outbuf);
933 free(rz->header->extra);
934 free(rz->header->name);
935 free(rz->header->comment);
939 free(rz->index->bin_offsets);
940 free(rz->index->cell_offsets);
946 knet_close(rz->x.fpr);