4 * RAZF : Random Access compressed(Z) File
6 * Release Date: 2008-10-27
8 * Copyright 2008, Jue Ruan <ruanjue@gmail.com>, Heng Li <lh3@sanger.ac.uk>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #if ZLIB_VERNUM < 0x1221
60 #warning "zlib < 1.2.2.1; RAZF writing is disabled."
63 #define DEF_MEM_LEVEL 8
65 static inline uint32_t byte_swap_4(uint32_t v){
66 v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
67 return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
70 static inline uint64_t byte_swap_8(uint64_t v){
71 v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
72 v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
73 return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
76 static inline int is_big_endian(){
79 return (c[0] != 0x01);
83 static void add_zindex(RAZF *rz, int64_t in, int64_t out){
84 if(rz->index->size == rz->index->cap){
85 rz->index->cap = rz->index->cap * 1.5 + 2;
86 rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
87 rz->index->bin_offsets = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
89 if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out;
90 rz->index->cell_offsets[rz->index->size] = out - rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE];
94 static void save_zindex(RAZF *rz, int fd){
97 is_be = is_big_endian();
98 if(is_be) write(fd, &rz->index->size, sizeof(int));
100 v32 = byte_swap_4((uint32_t)rz->index->size);
101 write(fd, &v32, sizeof(uint32_t));
103 v32 = rz->index->size / RZ_BIN_SIZE + 1;
105 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
106 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
108 write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
109 write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size);
114 static void load_zindex(RAZF *rz, knetFile *fp){
116 static void load_zindex(RAZF *rz, int fd){
120 if(!rz->load_index) return;
121 if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
122 is_be = is_big_endian();
124 knet_read(fp, &rz->index->size, sizeof(int));
126 read(fd, &rz->index->size, sizeof(int));
128 if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
129 rz->index->cap = rz->index->size;
130 v32 = rz->index->size / RZ_BIN_SIZE + 1;
131 rz->index->bin_offsets = malloc(sizeof(int64_t) * v32);
133 knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32);
135 read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
137 rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
139 knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
141 read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
144 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
145 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
150 static RAZF* razf_open_w(int fd)
152 fprintf(pysamerr, "[razf_open_w] Writing is not available with zlib ver < 1.2.2.1\n");
156 static RAZF* razf_open_w(int fd){
159 setmode(fd, O_BINARY);
161 rz = calloc(1, sizeof(RAZF));
168 rz->stream = calloc(sizeof(z_stream), 1);
169 rz->inbuf = malloc(RZ_BUFFER_SIZE);
170 rz->outbuf = malloc(RZ_BUFFER_SIZE);
171 rz->index = calloc(sizeof(ZBlockIndex), 1);
172 deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
173 rz->stream->avail_out = RZ_BUFFER_SIZE;
174 rz->stream->next_out = rz->outbuf;
175 rz->header = calloc(sizeof(gz_header), 1);
176 rz->header->os = 0x03; //Unix
177 rz->header->text = 0;
178 rz->header->time = 0;
179 rz->header->extra = malloc(7);
180 strncpy((char*)rz->header->extra, "RAZF", 4);
181 rz->header->extra[4] = 1; // obsolete field
182 // block size = RZ_BLOCK_SIZE, Big-Endian
183 rz->header->extra[5] = RZ_BLOCK_SIZE >> 8;
184 rz->header->extra[6] = RZ_BLOCK_SIZE & 0xFF;
185 rz->header->extra_len = 7;
186 rz->header->name = rz->header->comment = 0;
187 rz->header->hcrc = 0;
188 deflateSetHeader(rz->stream, rz->header);
189 rz->block_pos = rz->block_off = 0;
193 static void _razf_write(RAZF* rz, const void *data, int size){
195 rz->stream->avail_in = size;
196 rz->stream->next_in = (void*)data;
198 tout = rz->stream->avail_out;
199 deflate(rz->stream, Z_NO_FLUSH);
200 rz->out += tout - rz->stream->avail_out;
201 if(rz->stream->avail_out) break;
203 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
205 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
207 rz->stream->avail_out = RZ_BUFFER_SIZE;
208 rz->stream->next_out = rz->outbuf;
209 if(rz->stream->avail_in == 0) break;
211 rz->in += size - rz->stream->avail_in;
212 rz->block_off += size - rz->stream->avail_in;
215 static void razf_flush(RAZF *rz){
218 _razf_write(rz, rz->inbuf, rz->buf_len);
219 rz->buf_off = rz->buf_len = 0;
221 if(rz->stream->avail_out){
223 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
225 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
227 rz->stream->avail_out = RZ_BUFFER_SIZE;
228 rz->stream->next_out = rz->outbuf;
231 tout = rz->stream->avail_out;
232 deflate(rz->stream, Z_FULL_FLUSH);
233 rz->out += tout - rz->stream->avail_out;
234 if(rz->stream->avail_out == 0){
236 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
238 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
240 rz->stream->avail_out = RZ_BUFFER_SIZE;
241 rz->stream->next_out = rz->outbuf;
244 rz->block_pos = rz->out;
248 static void razf_end_flush(RAZF *rz){
251 _razf_write(rz, rz->inbuf, rz->buf_len);
252 rz->buf_off = rz->buf_len = 0;
255 tout = rz->stream->avail_out;
256 deflate(rz->stream, Z_FINISH);
257 rz->out += tout - rz->stream->avail_out;
258 if(rz->stream->avail_out < RZ_BUFFER_SIZE){
260 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
262 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
264 rz->stream->avail_out = RZ_BUFFER_SIZE;
265 rz->stream->next_out = rz->outbuf;
270 static void _razf_buffered_write(RAZF *rz, const void *data, int size){
273 if(rz->buf_len == RZ_BUFFER_SIZE){
274 _razf_write(rz, rz->inbuf, rz->buf_len);
277 if(size + rz->buf_len < RZ_BUFFER_SIZE){
278 for(i=0;i<size;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
282 n = RZ_BUFFER_SIZE - rz->buf_len;
283 for(i=0;i<n;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
291 int razf_write(RAZF* rz, const void *data, int size){
295 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
296 while(rz->in + rz->buf_len + size >= next_block){
297 n = next_block - rz->in - rz->buf_len;
298 _razf_buffered_write(rz, data, n);
302 add_zindex(rz, rz->in, rz->out);
303 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
305 _razf_buffered_write(rz, data, size);
311 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
312 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
313 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
314 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
315 #define COMMENT 0x10 /* bit 4 set: file comment present */
316 #define RESERVED 0xE0 /* bits 5..7: reserved */
318 static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *extra_len){
319 int method, flags, n, len;
320 if(size < 2) return 0;
321 if(data[0] != 0x1f || data[1] != 0x8b) return 0;
322 if(size < 4) return 0;
325 if(method != Z_DEFLATED || (flags & RESERVED)) return 0;
326 n = 4 + 6; // Skip 6 bytes
329 if(flags & EXTRA_FIELD){
330 if(size < n + 2) return 0;
331 len = ((int)data[n + 1] << 8) | data[n];
335 if(n >= size) return 0;
339 *extra_len = n - (*extra_off);
341 if(flags & ORIG_NAME) while(n < size && data[n++]);
342 if(flags & COMMENT) while(n < size && data[n++]);
343 if(flags & HEAD_CRC){
344 if(n + 2 > size) return 0;
351 static RAZF* razf_open_r(knetFile *fp, int _load_index){
353 static RAZF* razf_open_r(int fd, int _load_index){
356 int ext_off, ext_len;
359 unsigned char c[] = "RAZF";
360 rz = calloc(1, sizeof(RAZF));
366 setmode(fd, O_BINARY);
370 rz->stream = calloc(sizeof(z_stream), 1);
371 rz->inbuf = malloc(RZ_BUFFER_SIZE);
372 rz->outbuf = malloc(RZ_BUFFER_SIZE);
373 rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
375 n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
377 n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
379 ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
383 rz->file_type = FILE_TYPE_PLAIN;
384 memcpy(rz->outbuf, rz->inbuf, n);
390 rz->header_size = ret;
391 ret = inflateInit2(rz->stream, -WINDOW_BITS);
392 if(ret != Z_OK){ inflateEnd(rz->stream); goto PLAIN_FILE;}
393 rz->stream->avail_in = n - rz->header_size;
394 rz->stream->next_in = rz->inbuf + rz->header_size;
395 rz->stream->avail_out = RZ_BUFFER_SIZE;
396 rz->stream->next_out = rz->outbuf;
397 rz->file_type = FILE_TYPE_GZ;
398 rz->in = rz->header_size;
399 rz->block_pos = rz->header_size;
400 rz->next_block_pos = rz->header_size;
402 if(ext_len < 7 || memcmp(rz->inbuf + ext_off, c, 4) != 0) return rz;
403 if(((((unsigned char*)rz->inbuf)[ext_off + 5] << 8) | ((unsigned char*)rz->inbuf)[ext_off + 6]) != RZ_BLOCK_SIZE){
404 fprintf(pysamerr, " -- WARNING: RZ_BLOCK_SIZE is not %d, treat source as gz file. in %s -- %s:%d --\n", RZ_BLOCK_SIZE, __FUNCTION__, __FILE__, __LINE__);
407 rz->load_index = _load_index;
408 rz->file_type = FILE_TYPE_RZ;
410 if(knet_seek(fp, -16, SEEK_END) == -1){
412 if(lseek(fd, -16, SEEK_END) == -1){
417 rz->src_end = rz->end = 0x7FFFFFFFFFFFFFFFLL;
419 is_be = is_big_endian();
422 knet_read(fp, &end, sizeof(int64_t));
424 read(fd, &end, sizeof(int64_t));
426 if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
427 else rz->src_end = end;
430 knet_read(fp, &end, sizeof(int64_t));
432 read(fd, &end, sizeof(int64_t));
434 if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
437 rz->stream->avail_in -= n - rz->end;
440 if(rz->end > rz->src_end){
442 knet_seek(fp, rz->in, SEEK_SET);
444 lseek(fd, rz->in, SEEK_SET);
449 knet_seek(fp, rz->end, SEEK_SET);
450 if(knet_tell(fp) != rz->end){
451 knet_seek(fp, rz->in, SEEK_SET);
453 if(lseek(fd, rz->end, SEEK_SET) != rz->end){
454 lseek(fd, rz->in, SEEK_SET);
460 knet_seek(fp, n, SEEK_SET);
463 lseek(fd, n, SEEK_SET);
470 RAZF* razf_dopen(int fd, const char *mode){
471 if (strstr(mode, "r")) fprintf(pysamerr,"[razf_dopen] implement me\n");
472 else if(strstr(mode, "w")) return razf_open_w(fd);
476 RAZF* razf_dopen2(int fd, const char *mode)
478 fprintf(pysamerr,"[razf_dopen2] implement me\n");
482 RAZF* razf_dopen(int fd, const char *mode){
483 if(strstr(mode, "r")) return razf_open_r(fd, 1);
484 else if(strstr(mode, "w")) return razf_open_w(fd);
488 RAZF* razf_dopen2(int fd, const char *mode)
490 if(strstr(mode, "r")) return razf_open_r(fd, 0);
491 else if(strstr(mode, "w")) return razf_open_w(fd);
496 static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
499 if(strstr(mode, "r")){
501 knetFile *fd = knet_open(filename, "r");
503 fprintf(pysamerr, "[_razf_open] fail to open %s\n", filename);
508 fd = open(filename, O_RDONLY | O_BINARY);
510 fd = open(filename, O_RDONLY);
513 if(fd < 0) return NULL;
514 rz = razf_open_r(fd, _load_index);
515 } else if(strstr(mode, "w")){
517 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
519 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
521 if(fd < 0) return NULL;
522 rz = razf_open_w(fd);
527 RAZF* razf_open(const char *filename, const char *mode){
528 return _razf_open(filename, mode, 1);
531 RAZF* razf_open2(const char *filename, const char *mode){
532 return _razf_open(filename, mode, 0);
535 int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){
537 if(rz->mode != 'r' && rz->mode != 'R') return 0;
538 switch(rz->file_type){
539 case FILE_TYPE_PLAIN:
540 if(rz->end == 0x7fffffffffffffffLL){
542 if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
543 n = knet_tell(rz->x.fpr);
544 knet_seek(rz->x.fpr, 0, SEEK_END);
545 rz->end = knet_tell(rz->x.fpr);
546 knet_seek(rz->x.fpr, n, SEEK_SET);
548 if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
549 rz->end = lseek(rz->filedes, 0, SEEK_END);
550 lseek(rz->filedes, n, SEEK_SET);
553 *u_size = *c_size = rz->end;
558 if(rz->src_end == rz->end) return 0;
559 *u_size = rz->src_end;
567 static int _razf_read(RAZF* rz, void *data, int size){
569 if(rz->z_eof || rz->z_err) return 0;
570 if (rz->file_type == FILE_TYPE_PLAIN) {
572 ret = knet_read(rz->x.fpr, data, size);
574 ret = read(rz->filedes, data, size);
576 if (ret == 0) rz->z_eof = 1;
579 rz->stream->avail_out = size;
580 rz->stream->next_out = data;
581 while(rz->stream->avail_out){
582 if(rz->stream->avail_in == 0){
583 if(rz->in >= rz->end){ rz->z_eof = 1; break; }
584 if(rz->end - rz->in < RZ_BUFFER_SIZE){
586 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
588 rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
592 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
594 rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
597 if(rz->stream->avail_in == 0){
601 rz->stream->next_in = rz->inbuf;
603 tin = rz->stream->avail_in;
604 ret = inflate(rz->stream, Z_BLOCK);
605 rz->in += tin - rz->stream->avail_in;
606 if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
607 fprintf(pysamerr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
611 if(ret == Z_STREAM_END){
615 if ((rz->stream->data_type&128) && !(rz->stream->data_type&64)){
617 rz->next_block_pos = rz->in;
621 return size - rz->stream->avail_out;
624 int razf_read(RAZF *rz, void *data, int size){
629 if(size < rz->buf_len){
630 for(i=0;i<size;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
634 rz->block_off += size;
638 for(i=0;i<rz->buf_len;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
641 rz->block_off += rz->buf_len;
645 rz->block_pos = rz->next_block_pos;
650 } else if(rz->buf_flush){
651 rz->block_pos = rz->next_block_pos;
655 if(rz->buf_flush) continue;
656 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
657 if(rz->z_eof && rz->buf_len == 0) break;
659 rz->out += ori_size - size;
660 return ori_size - size;
663 int razf_skip(RAZF* rz, int size){
668 if(size < rz->buf_len){
671 rz->block_off += size;
678 rz->block_off += rz->buf_len;
680 rz->block_pos = rz->next_block_pos;
685 } else if(rz->buf_flush){
686 rz->block_pos = rz->next_block_pos;
690 if(rz->buf_flush) continue;
691 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
692 if(rz->z_eof || rz->z_err) break;
694 rz->out += ori_size - size;
695 return ori_size - size;
698 static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
700 knet_seek(rz->x.fpr, in, SEEK_SET);
702 lseek(rz->filedes, in, SEEK_SET);
707 rz->next_block_pos = in;
710 rz->z_eof = rz->z_err = 0;
711 inflateReset(rz->stream);
712 rz->stream->avail_in = 0;
713 rz->buf_off = rz->buf_len = 0;
716 int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){
719 if(rz->file_type == FILE_TYPE_PLAIN){
720 rz->buf_off = rz->buf_len = 0;
721 pos = block_start + block_offset;
723 knet_seek(rz->x.fpr, pos, SEEK_SET);
724 pos = knet_tell(rz->x.fpr);
726 pos = lseek(rz->filedes, pos, SEEK_SET);
728 rz->out = rz->in = pos;
731 if(block_start == rz->block_pos && block_offset >= rz->block_off) {
732 block_offset -= rz->block_off;
733 goto SKIP; // Needn't reset inflate
735 if(block_start == 0) block_start = rz->header_size; // Automaticly revist wrong block_start
736 _razf_reset_read(rz, block_start, 0);
738 if(block_offset) razf_skip(rz, block_offset);
739 return rz->block_off;
742 int64_t razf_seek(RAZF* rz, int64_t pos, int where){
744 int64_t seek_pos, new_out;
746 if (where == SEEK_CUR) pos += rz->out;
747 else if (where == SEEK_END) pos += rz->src_end;
748 if(rz->file_type == FILE_TYPE_PLAIN){
750 knet_seek(rz->x.fpr, pos, SEEK_SET);
751 seek_pos = knet_tell(rz->x.fpr);
753 seek_pos = lseek(rz->filedes, pos, SEEK_SET);
755 rz->buf_off = rz->buf_len = 0;
756 rz->out = rz->in = seek_pos;
758 } else if(rz->file_type == FILE_TYPE_GZ){
759 if(pos >= rz->out) goto SKIP;
762 if(pos == rz->out) return pos;
763 if(pos > rz->src_end) return rz->out;
764 if(!rz->seekable || !rz->load_index){
765 if(pos >= rz->out) goto SKIP;
767 idx = pos / RZ_BLOCK_SIZE - 1;
768 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
769 new_out = (idx + 1) * RZ_BLOCK_SIZE;
770 if(pos > rz->out && new_out <= rz->out) goto SKIP;
771 _razf_reset_read(rz, seek_pos, new_out);
773 razf_skip(rz, (int)(pos - rz->out));
777 uint64_t razf_tell2(RAZF *rz)
780 if (rz->load_index) {
781 int64_t idx, seek_pos;
782 idx = rz->out / RZ_BLOCK_SIZE - 1;
783 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
784 if (seek_pos != rz->block_pos || rz->out%RZ_BLOCK_SIZE != rz->block_off)
785 fprintf(pysamerr, "[razf_tell2] inconsistent block offset: (%lld, %lld) != (%lld, %lld)\n",
786 (long long)seek_pos, (long long)rz->out%RZ_BLOCK_SIZE, (long long)rz->block_pos, (long long) rz->block_off);
789 return (uint64_t)rz->block_pos<<16 | (rz->block_off&0xffff);
792 int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
794 if (where != SEEK_SET) return -1;
795 return razf_jump(rz, voffset>>16, voffset&0xffff);
798 void razf_close(RAZF *rz){
802 deflateEnd(rz->stream);
804 save_zindex(rz, rz->x.fpw);
806 write(rz->x.fpw, &rz->in, sizeof(int64_t));
807 write(rz->x.fpw, &rz->out, sizeof(int64_t));
809 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
810 write(rz->x.fpw, &v64, sizeof(int64_t));
811 v64 = byte_swap_8((uint64_t)rz->out);
812 write(rz->x.fpw, &v64, sizeof(int64_t));
815 save_zindex(rz, rz->filedes);
817 write(rz->filedes, &rz->in, sizeof(int64_t));
818 write(rz->filedes, &rz->out, sizeof(int64_t));
820 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
821 write(rz->filedes, &v64, sizeof(int64_t));
822 v64 = byte_swap_8((uint64_t)rz->out);
823 write(rz->filedes, &v64, sizeof(int64_t));
827 } else if(rz->mode == 'r'){
828 if(rz->stream) inflateEnd(rz->stream);
830 if(rz->inbuf) free(rz->inbuf);
831 if(rz->outbuf) free(rz->outbuf);
833 free(rz->header->extra);
834 free(rz->header->name);
835 free(rz->header->comment);
839 free(rz->index->bin_offsets);
840 free(rz->index->cell_offsets);
846 knet_close(rz->x.fpr);