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){
95 is_be = is_big_endian();
96 if(is_be) write(fd, &rz->index->size, sizeof(int));
98 v32 = byte_swap_4((uint32_t)rz->index->size);
99 write(fd, &v32, sizeof(uint32_t));
101 v32 = rz->index->size / RZ_BIN_SIZE + 1;
103 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
104 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
106 write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
107 write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size);
112 static void load_zindex(RAZF *rz, knetFile *fp){
114 static void load_zindex(RAZF *rz, int fd){
118 if(!rz->load_index) return;
119 if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
120 is_be = is_big_endian();
122 knet_read(fp, &rz->index->size, sizeof(int));
124 read(fd, &rz->index->size, sizeof(int));
126 if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
127 rz->index->cap = rz->index->size;
128 v32 = rz->index->size / RZ_BIN_SIZE + 1;
129 rz->index->bin_offsets = malloc(sizeof(int64_t) * v32);
131 knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32);
133 read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
135 rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
137 knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
139 read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
142 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
143 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
148 static RAZF* razf_open_w(int fd)
150 fprintf(stderr, "[razf_open_w] Writing is not available with zlib ver < 1.2.2.1\n");
154 static RAZF* razf_open_w(int fd){
157 setmode(fd, O_BINARY);
159 rz = calloc(1, sizeof(RAZF));
166 rz->stream = calloc(sizeof(z_stream), 1);
167 rz->inbuf = malloc(RZ_BUFFER_SIZE);
168 rz->outbuf = malloc(RZ_BUFFER_SIZE);
169 rz->index = calloc(sizeof(ZBlockIndex), 1);
170 deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
171 rz->stream->avail_out = RZ_BUFFER_SIZE;
172 rz->stream->next_out = rz->outbuf;
173 rz->header = calloc(sizeof(gz_header), 1);
174 rz->header->os = 0x03; //Unix
175 rz->header->text = 0;
176 rz->header->time = 0;
177 rz->header->extra = malloc(7);
178 strncpy((char*)rz->header->extra, "RAZF", 4);
179 rz->header->extra[4] = 1; // obsolete field
180 // block size = RZ_BLOCK_SIZE, Big-Endian
181 rz->header->extra[5] = RZ_BLOCK_SIZE >> 8;
182 rz->header->extra[6] = RZ_BLOCK_SIZE & 0xFF;
183 rz->header->extra_len = 7;
184 rz->header->name = rz->header->comment = 0;
185 rz->header->hcrc = 0;
186 deflateSetHeader(rz->stream, rz->header);
187 rz->block_pos = rz->block_off = 0;
191 static void _razf_write(RAZF* rz, const void *data, int size){
193 rz->stream->avail_in = size;
194 rz->stream->next_in = (void*)data;
196 tout = rz->stream->avail_out;
197 deflate(rz->stream, Z_NO_FLUSH);
198 rz->out += tout - rz->stream->avail_out;
199 if(rz->stream->avail_out) break;
201 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
203 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
205 rz->stream->avail_out = RZ_BUFFER_SIZE;
206 rz->stream->next_out = rz->outbuf;
207 if(rz->stream->avail_in == 0) break;
209 rz->in += size - rz->stream->avail_in;
210 rz->block_off += size - rz->stream->avail_in;
213 static void razf_flush(RAZF *rz){
216 _razf_write(rz, rz->inbuf, rz->buf_len);
217 rz->buf_off = rz->buf_len = 0;
219 if(rz->stream->avail_out){
221 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
223 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
225 rz->stream->avail_out = RZ_BUFFER_SIZE;
226 rz->stream->next_out = rz->outbuf;
229 tout = rz->stream->avail_out;
230 deflate(rz->stream, Z_FULL_FLUSH);
231 rz->out += tout - rz->stream->avail_out;
232 if(rz->stream->avail_out == 0){
234 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
236 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
238 rz->stream->avail_out = RZ_BUFFER_SIZE;
239 rz->stream->next_out = rz->outbuf;
242 rz->block_pos = rz->out;
246 static void razf_end_flush(RAZF *rz){
249 _razf_write(rz, rz->inbuf, rz->buf_len);
250 rz->buf_off = rz->buf_len = 0;
253 tout = rz->stream->avail_out;
254 deflate(rz->stream, Z_FINISH);
255 rz->out += tout - rz->stream->avail_out;
256 if(rz->stream->avail_out < RZ_BUFFER_SIZE){
258 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
260 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
262 rz->stream->avail_out = RZ_BUFFER_SIZE;
263 rz->stream->next_out = rz->outbuf;
268 static void _razf_buffered_write(RAZF *rz, const void *data, int size){
271 if(rz->buf_len == RZ_BUFFER_SIZE){
272 _razf_write(rz, rz->inbuf, rz->buf_len);
275 if(size + rz->buf_len < RZ_BUFFER_SIZE){
276 for(i=0;i<size;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
280 n = RZ_BUFFER_SIZE - rz->buf_len;
281 for(i=0;i<n;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
289 int razf_write(RAZF* rz, const void *data, int size){
293 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
294 while(rz->in + rz->buf_len + size >= next_block){
295 n = next_block - rz->in - rz->buf_len;
296 _razf_buffered_write(rz, data, n);
300 add_zindex(rz, rz->in, rz->out);
301 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
303 _razf_buffered_write(rz, data, size);
309 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
310 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
311 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
312 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
313 #define COMMENT 0x10 /* bit 4 set: file comment present */
314 #define RESERVED 0xE0 /* bits 5..7: reserved */
316 static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *extra_len){
317 int method, flags, n, len;
318 if(size < 2) return 0;
319 if(data[0] != 0x1f || data[1] != 0x8b) return 0;
320 if(size < 4) return 0;
323 if(method != Z_DEFLATED || (flags & RESERVED)) return 0;
324 n = 4 + 6; // Skip 6 bytes
327 if(flags & EXTRA_FIELD){
328 if(size < n + 2) return 0;
329 len = ((int)data[n + 1] << 8) | data[n];
333 if(n >= size) return 0;
337 *extra_len = n - (*extra_off);
339 if(flags & ORIG_NAME) while(n < size && data[n++]);
340 if(flags & COMMENT) while(n < size && data[n++]);
341 if(flags & HEAD_CRC){
342 if(n + 2 > size) return 0;
349 static RAZF* razf_open_r(knetFile *fp, int _load_index){
351 static RAZF* razf_open_r(int fd, int _load_index){
354 int ext_off, ext_len;
357 unsigned char c[] = "RAZF";
358 rz = calloc(1, sizeof(RAZF));
364 setmode(fd, O_BINARY);
368 rz->stream = calloc(sizeof(z_stream), 1);
369 rz->inbuf = malloc(RZ_BUFFER_SIZE);
370 rz->outbuf = malloc(RZ_BUFFER_SIZE);
371 rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
373 n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
375 n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
377 ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
381 rz->file_type = FILE_TYPE_PLAIN;
382 memcpy(rz->outbuf, rz->inbuf, n);
388 rz->header_size = ret;
389 ret = inflateInit2(rz->stream, -WINDOW_BITS);
390 if(ret != Z_OK){ inflateEnd(rz->stream); goto PLAIN_FILE;}
391 rz->stream->avail_in = n - rz->header_size;
392 rz->stream->next_in = rz->inbuf + rz->header_size;
393 rz->stream->avail_out = RZ_BUFFER_SIZE;
394 rz->stream->next_out = rz->outbuf;
395 rz->file_type = FILE_TYPE_GZ;
396 rz->in = rz->header_size;
397 rz->block_pos = rz->header_size;
398 rz->next_block_pos = rz->header_size;
400 if(ext_len < 7 || memcmp(rz->inbuf + ext_off, c, 4) != 0) return rz;
401 if(((((unsigned char*)rz->inbuf)[ext_off + 5] << 8) | ((unsigned char*)rz->inbuf)[ext_off + 6]) != RZ_BLOCK_SIZE){
402 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__);
405 rz->load_index = _load_index;
406 rz->file_type = FILE_TYPE_RZ;
408 if(knet_seek(fp, -16, SEEK_END) == -1){
410 if(lseek(fd, -16, SEEK_END) == -1){
415 rz->src_end = rz->end = 0x7FFFFFFFFFFFFFFFLL;
417 is_be = is_big_endian();
420 knet_read(fp, &end, sizeof(int64_t));
422 read(fd, &end, sizeof(int64_t));
424 if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
425 else rz->src_end = end;
428 knet_read(fp, &end, sizeof(int64_t));
430 read(fd, &end, sizeof(int64_t));
432 if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
435 rz->stream->avail_in -= n - rz->end;
438 if(rz->end > rz->src_end){
440 knet_seek(fp, rz->in, SEEK_SET);
442 lseek(fd, rz->in, SEEK_SET);
447 knet_seek(fp, rz->end, SEEK_SET);
448 if(knet_tell(fp) != rz->end){
449 knet_seek(fp, rz->in, SEEK_SET);
451 if(lseek(fd, rz->end, SEEK_SET) != rz->end){
452 lseek(fd, rz->in, SEEK_SET);
458 knet_seek(fp, n, SEEK_SET);
461 lseek(fd, n, SEEK_SET);
468 RAZF* razf_dopen(int fd, const char *mode){
469 if (strstr(mode, "r")) fprintf(stderr,"[razf_dopen] implement me\n");
470 else if(strstr(mode, "w")) return razf_open_w(fd);
474 RAZF* razf_dopen2(int fd, const char *mode)
476 fprintf(stderr,"[razf_dopen2] implement me\n");
480 RAZF* razf_dopen(int fd, const char *mode){
481 if(strstr(mode, "r")) return razf_open_r(fd, 1);
482 else if(strstr(mode, "w")) return razf_open_w(fd);
486 RAZF* razf_dopen2(int fd, const char *mode)
488 if(strstr(mode, "r")) return razf_open_r(fd, 0);
489 else if(strstr(mode, "w")) return razf_open_w(fd);
494 static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
497 if(strstr(mode, "r")){
499 knetFile *fd = knet_open(filename, "r");
501 fprintf(stderr, "[_razf_open] fail to open %s\n", filename);
506 fd = open(filename, O_RDONLY | O_BINARY);
508 fd = open(filename, O_RDONLY);
511 if(fd < 0) return NULL;
512 rz = razf_open_r(fd, _load_index);
513 } else if(strstr(mode, "w")){
515 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
517 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
519 if(fd < 0) return NULL;
520 rz = razf_open_w(fd);
525 RAZF* razf_open(const char *filename, const char *mode){
526 return _razf_open(filename, mode, 1);
529 RAZF* razf_open2(const char *filename, const char *mode){
530 return _razf_open(filename, mode, 0);
533 int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){
535 if(rz->mode != 'r' && rz->mode != 'R') return 0;
536 switch(rz->file_type){
537 case FILE_TYPE_PLAIN:
538 if(rz->end == 0x7fffffffffffffffLL){
540 if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
541 n = knet_tell(rz->x.fpr);
542 knet_seek(rz->x.fpr, 0, SEEK_END);
543 rz->end = knet_tell(rz->x.fpr);
544 knet_seek(rz->x.fpr, n, SEEK_SET);
546 if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
547 rz->end = lseek(rz->filedes, 0, SEEK_END);
548 lseek(rz->filedes, n, SEEK_SET);
551 *u_size = *c_size = rz->end;
556 if(rz->src_end == rz->end) return 0;
557 *u_size = rz->src_end;
565 static int _razf_read(RAZF* rz, void *data, int size){
567 if(rz->z_eof || rz->z_err) return 0;
568 if (rz->file_type == FILE_TYPE_PLAIN) {
570 ret = knet_read(rz->x.fpr, data, size);
572 ret = read(rz->filedes, data, size);
574 if (ret == 0) rz->z_eof = 1;
577 rz->stream->avail_out = size;
578 rz->stream->next_out = data;
579 while(rz->stream->avail_out){
580 if(rz->stream->avail_in == 0){
581 if(rz->in >= rz->end){ rz->z_eof = 1; break; }
582 if(rz->end - rz->in < RZ_BUFFER_SIZE){
584 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
586 rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
590 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
592 rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
595 if(rz->stream->avail_in == 0){
599 rz->stream->next_in = rz->inbuf;
601 tin = rz->stream->avail_in;
602 ret = inflate(rz->stream, Z_BLOCK);
603 rz->in += tin - rz->stream->avail_in;
604 if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
605 fprintf(stderr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
609 if(ret == Z_STREAM_END){
613 if ((rz->stream->data_type&128) && !(rz->stream->data_type&64)){
615 rz->next_block_pos = rz->in;
619 return size - rz->stream->avail_out;
622 int razf_read(RAZF *rz, void *data, int size){
627 if(size < rz->buf_len){
628 for(i=0;i<size;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
632 rz->block_off += size;
636 for(i=0;i<rz->buf_len;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
639 rz->block_off += rz->buf_len;
643 rz->block_pos = rz->next_block_pos;
648 } else if(rz->buf_flush){
649 rz->block_pos = rz->next_block_pos;
653 if(rz->buf_flush) continue;
654 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
655 if(rz->z_eof && rz->buf_len == 0) break;
657 rz->out += ori_size - size;
658 return ori_size - size;
661 int razf_skip(RAZF* rz, int size){
666 if(size < rz->buf_len){
669 rz->block_off += size;
676 rz->block_off += rz->buf_len;
678 rz->block_pos = rz->next_block_pos;
683 } else if(rz->buf_flush){
684 rz->block_pos = rz->next_block_pos;
688 if(rz->buf_flush) continue;
689 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
690 if(rz->z_eof || rz->z_err) break;
692 rz->out += ori_size - size;
693 return ori_size - size;
696 static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
698 knet_seek(rz->x.fpr, in, SEEK_SET);
700 lseek(rz->filedes, in, SEEK_SET);
705 rz->next_block_pos = in;
708 rz->z_eof = rz->z_err = 0;
709 inflateReset(rz->stream);
710 rz->stream->avail_in = 0;
711 rz->buf_off = rz->buf_len = 0;
714 int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){
717 if(rz->file_type == FILE_TYPE_PLAIN){
718 rz->buf_off = rz->buf_len = 0;
719 pos = block_start + block_offset;
721 knet_seek(rz->x.fpr, pos, SEEK_SET);
722 pos = knet_tell(rz->x.fpr);
724 pos = lseek(rz->filedes, pos, SEEK_SET);
726 rz->out = rz->in = pos;
729 if(block_start == rz->block_pos && block_offset >= rz->block_off) {
730 block_offset -= rz->block_off;
731 goto SKIP; // Needn't reset inflate
733 if(block_start == 0) block_start = rz->header_size; // Automaticly revist wrong block_start
734 _razf_reset_read(rz, block_start, 0);
736 if(block_offset) razf_skip(rz, block_offset);
737 return rz->block_off;
740 int64_t razf_seek(RAZF* rz, int64_t pos, int where){
742 int64_t seek_pos, new_out;
744 if (where == SEEK_CUR) pos += rz->out;
745 else if (where == SEEK_END) pos += rz->src_end;
746 if(rz->file_type == FILE_TYPE_PLAIN){
748 knet_seek(rz->x.fpr, pos, SEEK_SET);
749 seek_pos = knet_tell(rz->x.fpr);
751 seek_pos = lseek(rz->filedes, pos, SEEK_SET);
753 rz->buf_off = rz->buf_len = 0;
754 rz->out = rz->in = seek_pos;
756 } else if(rz->file_type == FILE_TYPE_GZ){
757 if(pos >= rz->out) goto SKIP;
760 if(pos == rz->out) return pos;
761 if(pos > rz->src_end) return rz->out;
762 if(!rz->seekable || !rz->load_index){
763 if(pos >= rz->out) goto SKIP;
765 idx = pos / RZ_BLOCK_SIZE - 1;
766 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
767 new_out = (idx + 1) * RZ_BLOCK_SIZE;
768 if(pos > rz->out && new_out <= rz->out) goto SKIP;
769 _razf_reset_read(rz, seek_pos, new_out);
771 razf_skip(rz, (int)(pos - rz->out));
775 uint64_t razf_tell2(RAZF *rz)
778 if (rz->load_index) {
779 int64_t idx, seek_pos;
780 idx = rz->out / RZ_BLOCK_SIZE - 1;
781 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
782 if (seek_pos != rz->block_pos || rz->out%RZ_BLOCK_SIZE != rz->block_off)
783 fprintf(stderr, "[razf_tell2] inconsistent block offset: (%lld, %lld) != (%lld, %lld)\n",
784 (long long)seek_pos, (long long)rz->out%RZ_BLOCK_SIZE, (long long)rz->block_pos, (long long) rz->block_off);
787 return (uint64_t)rz->block_pos<<16 | (rz->block_off&0xffff);
790 int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
792 if (where != SEEK_SET) return -1;
793 return razf_jump(rz, voffset>>16, voffset&0xffff);
796 void razf_close(RAZF *rz){
800 deflateEnd(rz->stream);
802 save_zindex(rz, rz->x.fpw);
804 write(rz->x.fpw, &rz->in, sizeof(int64_t));
805 write(rz->x.fpw, &rz->out, sizeof(int64_t));
807 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
808 write(rz->x.fpw, &v64, sizeof(int64_t));
809 v64 = byte_swap_8((uint64_t)rz->out);
810 write(rz->x.fpw, &v64, sizeof(int64_t));
813 save_zindex(rz, rz->filedes);
815 write(rz->filedes, &rz->in, sizeof(int64_t));
816 write(rz->filedes, &rz->out, sizeof(int64_t));
818 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
819 write(rz->filedes, &v64, sizeof(int64_t));
820 v64 = byte_swap_8((uint64_t)rz->out);
821 write(rz->filedes, &v64, sizeof(int64_t));
825 } else if(rz->mode == 'r'){
826 if(rz->stream) inflateEnd(rz->stream);
828 if(rz->inbuf) free(rz->inbuf);
829 if(rz->outbuf) free(rz->outbuf);
831 free(rz->header->extra);
832 free(rz->header->name);
833 free(rz->header->comment);
837 free(rz->index->bin_offsets);
838 free(rz->index->cell_offsets);
844 knet_close(rz->x.fpr);