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){
82 if(rz->index->size == rz->index->cap){
83 rz->index->cap = rz->index->cap * 1.5 + 2;
84 rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
85 rz->index->bin_offsets = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
87 if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out;
88 rz->index->cell_offsets[rz->index->size] = out - rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE];
92 static void save_zindex(RAZF *rz, int fd){
96 is_be = is_big_endian();
98 if (write(fd, &rz->index->size, sizeof(int)) < 0) {
99 fprintf(stderr, "[%s] failure to write zindex size.\n", __func__);
104 v32 = byte_swap_4((uint32_t)rz->index->size);
105 if (write(fd, &v32, sizeof(uint32_t)) < 0) {
106 fprintf(stderr, "[%s] failure to write zindex size.\n", __func__);
110 v32 = rz->index->size / RZ_BIN_SIZE + 1;
112 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
113 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
115 count = sizeof(int64_t) * v32;
116 if (write(fd, rz->index->bin_offsets, count) < 0) {
117 fprintf(stderr, "[%s] failure to write zindex bin_offsets.\n", __func__);
121 count = sizeof(int32_t) * rz->index->size;
122 if (write(fd, rz->index->cell_offsets, count) < 0) {
123 fprintf(stderr, "[%s] failure to write zindex cell_offsets.\n", __func__);
130 static void load_zindex(RAZF *rz, knetFile *fp){
132 static void load_zindex(RAZF *rz, int fd){
137 if(!rz->load_index) return;
138 if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
139 if(rz->index == NULL) {
140 fprintf(stderr, "[%s] failure to allocate index.\n", __func__);
143 is_be = is_big_endian();
145 if (knet_read(fp, &rz->index->size, sizeof(int)) < 0) {
147 if (read(fd, &rz->index->size, sizeof(int)) < 0) {
149 fprintf(stderr, "[%s] failure to read zindex size.\n", __func__);
152 if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
153 rz->index->cap = rz->index->size;
154 v32 = rz->index->size / RZ_BIN_SIZE + 1;
155 count = sizeof(int64_t) * v32;
156 if ((rz->index->bin_offsets = malloc(count)) == NULL) {
157 fprintf(stderr, "[%s] failure to allocate bin_offsets array.\n", __func__);
161 if (knet_read(fp, rz->index->bin_offsets, count) < 0) {
163 if (read(fd, rz->index->bin_offsets, count) < 0) {
165 fprintf(stderr, "[%s] failure to read bin_offsets.\n", __func__);
168 count = sizeof(int) * rz->index->size;
169 if ((rz->index->cell_offsets = malloc(count)) == NULL) {
170 fprintf(stderr, "[%s] failure to allocate cell_offsets array.\n", __func__);
174 if (knet_read(fp, rz->index->cell_offsets, count) < count) {
176 if (read(fd, rz->index->cell_offsets, count) < count) {
178 fprintf(stderr, "[%s] failure to read cell_offsets.\n", __func__);
182 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
183 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
188 static RAZF* razf_open_w(int fd)
190 fprintf(stderr, "[razf_open_w] Writing is not available with zlib ver < 1.2.2.1\n");
194 static RAZF* razf_open_w(int fd){
197 setmode(fd, O_BINARY);
199 rz = calloc(1, sizeof(RAZF));
206 rz->stream = calloc(sizeof(z_stream), 1);
207 rz->inbuf = malloc(RZ_BUFFER_SIZE);
208 rz->outbuf = malloc(RZ_BUFFER_SIZE);
209 rz->index = calloc(sizeof(ZBlockIndex), 1);
210 deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
211 rz->stream->avail_out = RZ_BUFFER_SIZE;
212 rz->stream->next_out = rz->outbuf;
213 rz->header = calloc(sizeof(gz_header), 1);
214 rz->header->os = 0x03; //Unix
215 rz->header->text = 0;
216 rz->header->time = 0;
217 rz->header->extra = malloc(7);
218 strncpy((char*)rz->header->extra, "RAZF", 4);
219 rz->header->extra[4] = 1; // obsolete field
220 // block size = RZ_BLOCK_SIZE, Big-Endian
221 rz->header->extra[5] = RZ_BLOCK_SIZE >> 8;
222 rz->header->extra[6] = RZ_BLOCK_SIZE & 0xFF;
223 rz->header->extra_len = 7;
224 rz->header->name = rz->header->comment = 0;
225 rz->header->hcrc = 0;
226 deflateSetHeader(rz->stream, rz->header);
227 rz->block_pos = rz->block_off = 0;
231 static void _razf_write(RAZF* rz, const void *data, int size){
234 rz->stream->avail_in = size;
235 rz->stream->next_in = (void*)data;
237 tout = rz->stream->avail_out;
238 deflate(rz->stream, Z_NO_FLUSH);
239 rz->out += tout - rz->stream->avail_out;
240 if(rz->stream->avail_out) break;
241 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
243 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
245 if (write(rz->filedes, rz->outbuf, count) < 0) {
247 fprintf(stderr, "[%s] failed to write output buffer.\n", __func__);
250 rz->stream->avail_out = RZ_BUFFER_SIZE;
251 rz->stream->next_out = rz->outbuf;
252 if(rz->stream->avail_in == 0) break;
254 rz->in += size - rz->stream->avail_in;
255 rz->block_off += size - rz->stream->avail_in;
258 static void razf_flush(RAZF *rz){
262 _razf_write(rz, rz->inbuf, rz->buf_len);
263 rz->buf_off = rz->buf_len = 0;
265 if(rz->stream->avail_out){
266 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
268 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
270 if (write(rz->filedes, rz->outbuf, count) < 0) {
272 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
275 rz->stream->avail_out = RZ_BUFFER_SIZE;
276 rz->stream->next_out = rz->outbuf;
279 tout = rz->stream->avail_out;
280 deflate(rz->stream, Z_FULL_FLUSH);
281 rz->out += tout - rz->stream->avail_out;
282 if(rz->stream->avail_out == 0){
283 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
285 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
287 if (write(rz->filedes, rz->outbuf, count) < 0) {
289 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
292 rz->stream->avail_out = RZ_BUFFER_SIZE;
293 rz->stream->next_out = rz->outbuf;
296 rz->block_pos = rz->out;
300 static void razf_end_flush(RAZF *rz){
304 _razf_write(rz, rz->inbuf, rz->buf_len);
305 rz->buf_off = rz->buf_len = 0;
308 tout = rz->stream->avail_out;
309 deflate(rz->stream, Z_FINISH);
310 rz->out += tout - rz->stream->avail_out;
311 if(rz->stream->avail_out < RZ_BUFFER_SIZE){
312 count = RZ_BUFFER_SIZE - rz->stream->avail_out;
314 if (write(rz->x.fpw, rz->outbuf, count) < 0) {
316 if (write(rz->filedes, rz->outbuf, count) < 0) {
318 fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
321 rz->stream->avail_out = RZ_BUFFER_SIZE;
322 rz->stream->next_out = rz->outbuf;
327 static void _razf_buffered_write(RAZF *rz, const void *data, int size){
330 if(rz->buf_len == RZ_BUFFER_SIZE){
331 _razf_write(rz, rz->inbuf, rz->buf_len);
334 if(size + rz->buf_len < RZ_BUFFER_SIZE){
335 for(i=0;i<size;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
339 n = RZ_BUFFER_SIZE - rz->buf_len;
340 for(i=0;i<n;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
348 int razf_write(RAZF* rz, const void *data, int size){
352 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
353 while(rz->in + rz->buf_len + size >= next_block){
354 n = next_block - rz->in - rz->buf_len;
355 _razf_buffered_write(rz, data, n);
359 add_zindex(rz, rz->in, rz->out);
360 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
362 _razf_buffered_write(rz, data, size);
368 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
369 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
370 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
371 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
372 #define COMMENT 0x10 /* bit 4 set: file comment present */
373 #define RESERVED 0xE0 /* bits 5..7: reserved */
375 static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *extra_len){
376 int method, flags, n, len;
377 if(size < 2) return 0;
378 if(data[0] != 0x1f || data[1] != 0x8b) return 0;
379 if(size < 4) return 0;
382 if(method != Z_DEFLATED || (flags & RESERVED)) return 0;
383 n = 4 + 6; // Skip 6 bytes
386 if(flags & EXTRA_FIELD){
387 if(size < n + 2) return 0;
388 len = ((int)data[n + 1] << 8) | data[n];
392 if(n >= size) return 0;
396 *extra_len = n - (*extra_off);
398 if(flags & ORIG_NAME) while(n < size && data[n++]);
399 if(flags & COMMENT) while(n < size && data[n++]);
400 if(flags & HEAD_CRC){
401 if(n + 2 > size) return 0;
408 static RAZF* razf_open_r(knetFile *fp, int _load_index){
410 static RAZF* razf_open_r(int fd, int _load_index){
413 int ext_off, ext_len;
416 unsigned char c[] = "RAZF";
417 if ((rz = calloc(1, sizeof(RAZF))) == NULL) {
418 fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__);
426 setmode(fd, O_BINARY);
430 if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) {
431 fprintf(stderr, "[%s] failure to allocate z_stream.\n", __func__);
435 if ((rz->inbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
436 fprintf(stderr, "[%s] failure to allocate input buffer.\n", __func__);
441 if ((rz->outbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
442 fprintf(stderr, "[%s] failure to allocate output buffer.\n", __func__);
448 rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
450 n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
452 n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
454 ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
458 rz->file_type = FILE_TYPE_PLAIN;
459 memcpy(rz->outbuf, rz->inbuf, n);
465 rz->header_size = ret;
466 ret = inflateInit2(rz->stream, -WINDOW_BITS);
467 if(ret != Z_OK){ inflateEnd(rz->stream); goto PLAIN_FILE;}
468 rz->stream->avail_in = n - rz->header_size;
469 rz->stream->next_in = rz->inbuf + rz->header_size;
470 rz->stream->avail_out = RZ_BUFFER_SIZE;
471 rz->stream->next_out = rz->outbuf;
472 rz->file_type = FILE_TYPE_GZ;
473 rz->in = rz->header_size;
474 rz->block_pos = rz->header_size;
475 rz->next_block_pos = rz->header_size;
477 if(ext_len < 7 || memcmp(rz->inbuf + ext_off, c, 4) != 0) return rz;
478 if(((((unsigned char*)rz->inbuf)[ext_off + 5] << 8) | ((unsigned char*)rz->inbuf)[ext_off + 6]) != RZ_BLOCK_SIZE){
479 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__);
482 rz->load_index = _load_index;
483 rz->file_type = FILE_TYPE_RZ;
485 if(knet_seek(fp, -16, SEEK_END) == -1){
487 if(lseek(fd, -16, SEEK_END) == -1){
492 rz->src_end = rz->end = 0x7FFFFFFFFFFFFFFFLL;
494 is_be = is_big_endian();
497 knet_read(fp, &end, sizeof(int64_t));
499 read(fd, &end, sizeof(int64_t));
501 if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
502 else rz->src_end = end;
505 knet_read(fp, &end, sizeof(int64_t));
507 read(fd, &end, sizeof(int64_t));
509 if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
512 rz->stream->avail_in -= n - rz->end;
515 if(rz->end > rz->src_end){
517 knet_seek(fp, rz->in, SEEK_SET);
519 lseek(fd, rz->in, SEEK_SET);
524 knet_seek(fp, rz->end, SEEK_SET);
525 if(knet_tell(fp) != rz->end){
526 knet_seek(fp, rz->in, SEEK_SET);
528 if(lseek(fd, rz->end, SEEK_SET) != rz->end){
529 lseek(fd, rz->in, SEEK_SET);
535 knet_seek(fp, n, SEEK_SET);
538 lseek(fd, n, SEEK_SET);
545 RAZF* razf_dopen(int fd, const char *mode){
546 if (strstr(mode, "r")) fprintf(stderr,"[razf_dopen] implement me\n");
547 else if(strstr(mode, "w")) return razf_open_w(fd);
551 RAZF* razf_dopen2(int fd, const char *mode)
553 fprintf(stderr,"[razf_dopen2] implement me\n");
557 RAZF* razf_dopen(int fd, const char *mode){
558 if(strstr(mode, "r")) return razf_open_r(fd, 1);
559 else if(strstr(mode, "w")) return razf_open_w(fd);
563 RAZF* razf_dopen2(int fd, const char *mode)
565 if(strstr(mode, "r")) return razf_open_r(fd, 0);
566 else if(strstr(mode, "w")) return razf_open_w(fd);
571 static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
574 if(strstr(mode, "r")){
576 knetFile *fd = knet_open(filename, "r");
578 fprintf(stderr, "[_razf_open] fail to open %s\n", filename);
583 fd = open(filename, O_RDONLY | O_BINARY);
585 fd = open(filename, O_RDONLY);
588 if(fd < 0) return NULL;
589 rz = razf_open_r(fd, _load_index);
590 } else if(strstr(mode, "w")){
592 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
594 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
596 if(fd < 0) return NULL;
597 rz = razf_open_w(fd);
602 RAZF* razf_open(const char *filename, const char *mode){
603 return _razf_open(filename, mode, 1);
606 RAZF* razf_open2(const char *filename, const char *mode){
607 return _razf_open(filename, mode, 0);
610 int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){
612 if(rz->mode != 'r' && rz->mode != 'R') return 0;
613 switch(rz->file_type){
614 case FILE_TYPE_PLAIN:
615 if(rz->end == 0x7fffffffffffffffLL){
617 if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
618 n = knet_tell(rz->x.fpr);
619 knet_seek(rz->x.fpr, 0, SEEK_END);
620 rz->end = knet_tell(rz->x.fpr);
621 knet_seek(rz->x.fpr, n, SEEK_SET);
623 if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
624 rz->end = lseek(rz->filedes, 0, SEEK_END);
625 lseek(rz->filedes, n, SEEK_SET);
628 *u_size = *c_size = rz->end;
633 if(rz->src_end == rz->end) return 0;
634 *u_size = rz->src_end;
642 static int _razf_read(RAZF* rz, void *data, int size){
644 if(rz->z_eof || rz->z_err) return 0;
645 if (rz->file_type == FILE_TYPE_PLAIN) {
647 ret = knet_read(rz->x.fpr, data, size);
649 ret = read(rz->filedes, data, size);
651 if (ret == 0) rz->z_eof = 1;
654 rz->stream->avail_out = size;
655 rz->stream->next_out = data;
656 while(rz->stream->avail_out){
657 if(rz->stream->avail_in == 0){
658 if(rz->in >= rz->end){ rz->z_eof = 1; break; }
659 if(rz->end - rz->in < RZ_BUFFER_SIZE){
661 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
663 rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
667 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
669 rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
672 if(rz->stream->avail_in == 0){
676 rz->stream->next_in = rz->inbuf;
678 tin = rz->stream->avail_in;
679 ret = inflate(rz->stream, Z_BLOCK);
680 rz->in += tin - rz->stream->avail_in;
681 if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
682 fprintf(stderr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
686 if(ret == Z_STREAM_END){
690 if ((rz->stream->data_type&128) && !(rz->stream->data_type&64)){
692 rz->next_block_pos = rz->in;
696 return size - rz->stream->avail_out;
699 int razf_read(RAZF *rz, void *data, int size){
704 if(size < rz->buf_len){
705 for(i=0;i<size;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
709 rz->block_off += size;
713 for(i=0;i<rz->buf_len;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
716 rz->block_off += rz->buf_len;
720 rz->block_pos = rz->next_block_pos;
725 } else if(rz->buf_flush){
726 rz->block_pos = rz->next_block_pos;
730 if(rz->buf_flush) continue;
731 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
732 if(rz->z_eof && rz->buf_len == 0) break;
734 rz->out += ori_size - size;
735 return ori_size - size;
738 int razf_skip(RAZF* rz, int size){
743 if(size < rz->buf_len){
746 rz->block_off += size;
753 rz->block_off += rz->buf_len;
755 rz->block_pos = rz->next_block_pos;
760 } else if(rz->buf_flush){
761 rz->block_pos = rz->next_block_pos;
765 if(rz->buf_flush) continue;
766 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
767 if(rz->z_eof || rz->z_err) break;
769 rz->out += ori_size - size;
770 return ori_size - size;
773 static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
775 knet_seek(rz->x.fpr, in, SEEK_SET);
777 lseek(rz->filedes, in, SEEK_SET);
782 rz->next_block_pos = in;
785 rz->z_eof = rz->z_err = 0;
786 inflateReset(rz->stream);
787 rz->stream->avail_in = 0;
788 rz->buf_off = rz->buf_len = 0;
791 int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){
794 if(rz->file_type == FILE_TYPE_PLAIN){
795 rz->buf_off = rz->buf_len = 0;
796 pos = block_start + block_offset;
798 knet_seek(rz->x.fpr, pos, SEEK_SET);
799 pos = knet_tell(rz->x.fpr);
801 pos = lseek(rz->filedes, pos, SEEK_SET);
803 rz->out = rz->in = pos;
806 if(block_start == rz->block_pos && block_offset >= rz->block_off) {
807 block_offset -= rz->block_off;
808 goto SKIP; // Needn't reset inflate
810 if(block_start == 0) block_start = rz->header_size; // Automaticly revist wrong block_start
811 _razf_reset_read(rz, block_start, 0);
813 if(block_offset) razf_skip(rz, block_offset);
814 return rz->block_off;
817 int64_t razf_seek(RAZF* rz, int64_t pos, int where){
819 int64_t seek_pos, new_out;
821 if (where == SEEK_CUR) pos += rz->out;
822 else if (where == SEEK_END) pos += rz->src_end;
823 if(rz->file_type == FILE_TYPE_PLAIN){
825 knet_seek(rz->x.fpr, pos, SEEK_SET);
826 seek_pos = knet_tell(rz->x.fpr);
828 seek_pos = lseek(rz->filedes, pos, SEEK_SET);
830 rz->buf_off = rz->buf_len = 0;
831 rz->out = rz->in = seek_pos;
833 } else if(rz->file_type == FILE_TYPE_GZ){
834 if(pos >= rz->out) goto SKIP;
837 if(pos == rz->out) return pos;
838 if(pos > rz->src_end) return rz->out;
839 if(!rz->seekable || !rz->load_index){
840 if(pos >= rz->out) goto SKIP;
842 idx = pos / RZ_BLOCK_SIZE - 1;
843 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
844 new_out = (idx + 1) * RZ_BLOCK_SIZE;
845 if(pos > rz->out && new_out <= rz->out) goto SKIP;
846 _razf_reset_read(rz, seek_pos, new_out);
848 razf_skip(rz, (int)(pos - rz->out));
852 uint64_t razf_tell2(RAZF *rz)
855 if (rz->load_index) {
856 int64_t idx, seek_pos;
857 idx = rz->out / RZ_BLOCK_SIZE - 1;
858 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
859 if (seek_pos != rz->block_pos || rz->out%RZ_BLOCK_SIZE != rz->block_off)
860 fprintf(stderr, "[razf_tell2] inconsistent block offset: (%lld, %lld) != (%lld, %lld)\n",
861 (long long)seek_pos, (long long)rz->out%RZ_BLOCK_SIZE, (long long)rz->block_pos, (long long) rz->block_off);
864 return (uint64_t)rz->block_pos<<16 | (rz->block_off&0xffff);
867 int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
869 if (where != SEEK_SET) return -1;
870 return razf_jump(rz, voffset>>16, voffset&0xffff);
873 void razf_close(RAZF *rz){
877 deflateEnd(rz->stream);
879 save_zindex(rz, rz->x.fpw);
881 if (write(rz->x.fpw, &rz->in, sizeof(int64_t)) < 0) {
882 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
885 if (write(rz->x.fpw, &rz->out, sizeof(int64_t)) < 0) {
886 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
890 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
891 if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
892 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
895 v64 = byte_swap_8((uint64_t)rz->out);
896 if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
897 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
902 save_zindex(rz, rz->filedes);
904 if (write(rz->filedes, &rz->in, sizeof(int64_t)) < 0) {
905 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
908 if (write(rz->filedes, &rz->out, sizeof(int64_t)) < 0) {
909 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
913 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
914 if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
915 fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
918 v64 = byte_swap_8((uint64_t)rz->out);
919 if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
920 fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
926 } else if(rz->mode == 'r'){
927 if(rz->stream) inflateEnd(rz->stream);
929 if(rz->inbuf) free(rz->inbuf);
930 if(rz->outbuf) free(rz->outbuf);
932 free(rz->header->extra);
933 free(rz->header->name);
934 free(rz->header->comment);
938 free(rz->index->bin_offsets);
939 free(rz->index->cell_offsets);
945 knet_close(rz->x.fpr);