]> git.donarmstrong.com Git - samtools.git/blob - razf.c
* no changes to samtools itself
[samtools.git] / razf.c
1 /*
2  * RAZF : Random Access compressed(Z) File
3  * Version: 1.0
4  * Release Date: 2008-10-27
5  *
6  * Copyright 2008, Jue Ruan <ruanjue@gmail.com>, Heng Li <lh3@sanger.ac.uk>
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  *
31  * To compile razf.c, zlib-1.2.3(or greater) is required.
32  */
33
34 #ifndef _NO_RAZF
35
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include "razf.h"
42
43 #if ZLIB_VERNUM < 0x1221
44 struct _gz_header_s {
45     int     text;
46     uLong   time;
47     int     xflags;
48     int     os;
49     Bytef   *extra;
50     uInt    extra_len;
51     uInt    extra_max;
52     Bytef   *name;
53     uInt    name_max;
54     Bytef   *comment;
55     uInt    comm_max;
56     int     hcrc;
57     int     done;
58 };
59 #endif
60
61 #define DEF_MEM_LEVEL 8
62
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);
66 }
67
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);
72 }
73
74 static inline int is_big_endian(){
75         int x = 0x01;
76         char *c = (char*)&x;
77         return (c[0] != 0x01);
78 }
79
80 #ifndef _RZ_READONLY
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));
86         }
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];
89         rz->index->size ++;
90 }
91
92 static void save_zindex(RAZF *rz, int fd){
93         int32_t i, v32;
94         int is_be;
95         is_be = is_big_endian();
96         if(is_be) write(fd, &rz->index->size, sizeof(int));
97         else {
98                 v32 = byte_swap_4((uint32_t)rz->index->size);
99                 write(fd, &v32, sizeof(uint32_t));
100         }
101         v32 = rz->index->size / RZ_BIN_SIZE + 1;
102         if(!is_be){
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]);
105         }
106         write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
107         write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size);
108 }
109 #endif
110
111 static void load_zindex(RAZF *rz, int fd){
112         int32_t i, v32;
113         int is_be;
114         if(!rz->load_index) return;
115         if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
116         is_be = is_big_endian();
117         read(fd, &rz->index->size, sizeof(int));
118         if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
119         rz->index->cap = rz->index->size;
120         v32 = rz->index->size / RZ_BIN_SIZE + 1;
121         rz->index->bin_offsets  = malloc(sizeof(int64_t) * v32);
122         read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
123         rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
124         read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
125         if(!is_be){
126                 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
127                 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
128         }
129 }
130
131 #ifdef _RZ_READONLY
132 static RAZF* razf_open_w(int fd)
133 {
134         fprintf(stderr, "[razf_open_w] Writing is not available with zlib ver < 1.2.2.1\n");
135         return 0;
136 }
137 #else
138 static RAZF* razf_open_w(int fd){
139         RAZF *rz;
140         rz = calloc(1, sizeof(RAZF));
141         rz->mode = 'w';
142         rz->filedes = fd;
143         rz->stream = calloc(sizeof(z_stream), 1);
144         rz->inbuf  = malloc(RZ_BUFFER_SIZE);
145         rz->outbuf = malloc(RZ_BUFFER_SIZE);
146         rz->index = calloc(sizeof(ZBlockIndex), 1);
147         deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
148         rz->stream->avail_out = RZ_BUFFER_SIZE;
149         rz->stream->next_out  = rz->outbuf;
150         rz->header = calloc(sizeof(gz_header), 1);
151         rz->header->os    = 0x03; //Unix
152         rz->header->text  = 0;
153         rz->header->time  = 0;
154         rz->header->extra = malloc(7);
155         strncpy((char*)rz->header->extra, "RAZF", 4);
156         rz->header->extra[4] = 1; // obsolete field
157         // block size = RZ_BLOCK_SIZE, Big-Endian
158         rz->header->extra[5] = RZ_BLOCK_SIZE >> 8;
159         rz->header->extra[6] = RZ_BLOCK_SIZE & 0xFF;
160         rz->header->extra_len = 7;
161         rz->header->name = rz->header->comment  = 0;
162         rz->header->hcrc = 0;
163         deflateSetHeader(rz->stream, rz->header);
164         rz->block_pos = rz->block_off = 0;
165         return rz;
166 }
167
168 static void _razf_write(RAZF* rz, const void *data, int size){
169         int tout;
170         rz->stream->avail_in = size;
171         rz->stream->next_in  = (void*)data;
172         while(1){
173                 tout = rz->stream->avail_out;
174                 deflate(rz->stream, Z_NO_FLUSH);
175                 rz->out += tout - rz->stream->avail_out;
176                 if(rz->stream->avail_out) break;
177                 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
178                 rz->stream->avail_out = RZ_BUFFER_SIZE;
179                 rz->stream->next_out  = rz->outbuf;
180                 if(rz->stream->avail_in == 0) break;
181         };
182         rz->in += size - rz->stream->avail_in;
183         rz->block_off += size - rz->stream->avail_in;
184 }
185
186 static void razf_flush(RAZF *rz){
187         uint32_t tout;
188         if(rz->buf_len){
189                 _razf_write(rz, rz->inbuf, rz->buf_len);
190                 rz->buf_off = rz->buf_len = 0;
191         }
192         if(rz->stream->avail_out){
193                 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
194                 rz->stream->avail_out = RZ_BUFFER_SIZE;
195                 rz->stream->next_out  = rz->outbuf;
196         }
197         while(1){
198                 tout = rz->stream->avail_out;
199                 deflate(rz->stream, Z_FULL_FLUSH);
200                 rz->out += tout - rz->stream->avail_out;
201                 if(rz->stream->avail_out == 0){
202                         write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
203                         rz->stream->avail_out = RZ_BUFFER_SIZE;
204                         rz->stream->next_out  = rz->outbuf;
205                 } else break;
206         }
207         rz->block_pos = rz->out;
208         rz->block_off = 0;
209 }
210
211 static void razf_end_flush(RAZF *rz){
212         uint32_t tout;
213         if(rz->buf_len){
214                 _razf_write(rz, rz->inbuf, rz->buf_len);
215                 rz->buf_off = rz->buf_len = 0;
216         }
217         while(1){
218                 tout = rz->stream->avail_out;
219                 deflate(rz->stream, Z_FINISH);
220                 rz->out += tout - rz->stream->avail_out;
221                 if(rz->stream->avail_out < RZ_BUFFER_SIZE){
222                         write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
223                         rz->stream->avail_out = RZ_BUFFER_SIZE;
224                         rz->stream->next_out  = rz->outbuf;
225                 } else break;
226         }
227 }
228
229 static void _razf_buffered_write(RAZF *rz, const void *data, int size){
230         int i, n;
231         while(1){
232                 if(rz->buf_len == RZ_BUFFER_SIZE){
233                         _razf_write(rz, rz->inbuf, rz->buf_len);
234                         rz->buf_len = 0;
235                 }
236                 if(size + rz->buf_len < RZ_BUFFER_SIZE){
237                         for(i=0;i<size;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
238                         rz->buf_len += size;
239                         return;
240                 } else {
241                         n = RZ_BUFFER_SIZE - rz->buf_len;
242                         for(i=0;i<n;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
243                         size -= n;
244                         data += n;
245                         rz->buf_len += n;
246                 }
247         }
248 }
249
250 int razf_write(RAZF* rz, const void *data, int size){
251         int ori_size, n;
252         int64_t next_block;
253         ori_size = size;
254         next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
255         while(rz->in + rz->buf_len + size >= next_block){
256                 n = next_block - rz->in - rz->buf_len;
257                 _razf_buffered_write(rz, data, n);
258                 data += n;
259                 size -= n;
260                 razf_flush(rz);
261                 add_zindex(rz, rz->in, rz->out);
262                 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
263         }
264         _razf_buffered_write(rz, data, size);
265         return ori_size;
266 }
267 #endif
268
269 /* gzip flag byte */
270 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
271 #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
272 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
273 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
274 #define COMMENT      0x10 /* bit 4 set: file comment present */
275 #define RESERVED     0xE0 /* bits 5..7: reserved */
276
277 static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *extra_len){
278         int method, flags, n, len;
279         if(size < 2) return 0;
280         if(data[0] != 0x1f || data[1] != 0x8b) return 0;
281         if(size < 4) return 0;
282         method = data[2];
283         flags  = data[3];
284         if(method != Z_DEFLATED || (flags & RESERVED)) return 0;
285         n = 4 + 6; // Skip 6 bytes
286         *extra_off = n + 2;
287         *extra_len = 0;
288         if(flags & EXTRA_FIELD){
289                 if(size < n + 2) return 0;
290                 len = ((int)data[n + 1] << 8) | data[n];
291                 n += 2;
292                 *extra_off = n;
293                 while(len){
294                         if(n >= size) return 0;
295                         n ++;
296                         len --;
297                 }
298                 *extra_len = n - (*extra_off);
299         }
300         if(flags & ORIG_NAME) while(n < size && data[n++]);
301         if(flags & COMMENT) while(n < size && data[n++]);
302         if(flags & HEAD_CRC){
303                 if(n + 2 > size) return 0;
304                 n += 2;
305         }
306         return n;
307 }
308
309 static RAZF* razf_open_r(int fd, int _load_index){
310         RAZF *rz;
311         int ext_off, ext_len;
312         int n, is_be, ret;
313         int64_t end;
314         unsigned char c[] = "RAZF";
315         rz = calloc(1, sizeof(RAZF));
316         rz->mode = 'r';
317         rz->filedes = fd;
318         rz->stream = calloc(sizeof(z_stream), 1);
319         rz->inbuf  = malloc(RZ_BUFFER_SIZE);
320         rz->outbuf = malloc(RZ_BUFFER_SIZE);
321         rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
322         n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
323         ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
324         if(ret == 0){
325                 PLAIN_FILE:
326                 rz->in = n;
327                 rz->file_type = FILE_TYPE_PLAIN;
328                 memcpy(rz->outbuf, rz->inbuf, n);
329                 rz->buf_len = n;
330                 free(rz->stream);
331                 rz->stream = NULL;
332                 return rz;
333         }
334         rz->header_size = ret;
335         ret = inflateInit2(rz->stream, -WINDOW_BITS);
336         if(ret != Z_OK){ inflateEnd(rz->stream); goto PLAIN_FILE;}
337         rz->stream->avail_in = n - rz->header_size;
338         rz->stream->next_in  = rz->inbuf + rz->header_size;
339         rz->stream->avail_out = RZ_BUFFER_SIZE;
340         rz->stream->next_out  = rz->outbuf;
341         rz->file_type = FILE_TYPE_GZ;
342         rz->in = rz->header_size;
343         rz->block_pos = rz->header_size;
344         rz->next_block_pos = rz->header_size;
345         rz->block_off = 0;
346         if(ext_len < 7 || memcmp(rz->inbuf + ext_off, c, 4) != 0) return rz;
347         if(((((unsigned char*)rz->inbuf)[ext_off + 5] << 8) | ((unsigned char*)rz->inbuf)[ext_off + 6]) != RZ_BLOCK_SIZE){
348                 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__);
349                 return rz;
350         }
351         rz->load_index = _load_index;
352         rz->file_type = FILE_TYPE_RZ;
353         if(lseek(fd, -16, SEEK_END) == -1){
354                 UNSEEKABLE:
355                 rz->seekable = 0;
356                 rz->index = NULL;
357                 rz->src_end = rz->end = 0x7FFFFFFFFFFFFFFFLL;
358         } else {
359                 is_be = is_big_endian();
360                 rz->seekable = 1;
361                 read(fd, &end, sizeof(int64_t));
362                 if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
363                 else rz->src_end = end;
364                 read(fd, &end, sizeof(int64_t));
365                 if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
366                 else rz->end = end;
367                 if(n > rz->end){
368                         rz->stream->avail_in -= n - rz->end;
369                         n = rz->end;
370                 }
371                 if(rz->end > rz->src_end){
372                         lseek(fd, rz->in, SEEK_SET);
373                         goto UNSEEKABLE;
374                 }
375                 if(lseek(fd, rz->end, SEEK_SET) != rz->end){
376                         lseek(fd, rz->in, SEEK_SET);
377                         goto UNSEEKABLE;
378                 }
379                 load_zindex(rz, fd);
380                 lseek(fd, n, SEEK_SET);
381         }
382         return rz;
383 }
384
385 RAZF* razf_dopen(int fd, const char *mode){
386         if(strcasecmp(mode, "r") == 0) return razf_open_r(fd, 1);
387         else if(strcasecmp(mode, "w") == 0) return razf_open_w(fd);
388         else return NULL;
389 }
390
391 RAZF* razf_dopen2(int fd, const char *mode)
392 {
393         if(strcasecmp(mode, "r") == 0) return razf_open_r(fd, 0);
394         else if(strcasecmp(mode, "w") == 0) return razf_open_w(fd);
395         else return NULL;
396 }
397
398 static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
399         int fd;
400         RAZF *rz;
401         if(strcasecmp(mode, "r") == 0){
402                 fd = open(filename, O_RDONLY);
403                 rz = razf_open_r(fd, _load_index);
404         } else if(strcasecmp(mode, "w") == 0){
405                 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
406                 rz = razf_open_w(fd);
407         } else return NULL;
408         return rz;
409 }
410
411 RAZF* razf_open(const char *filename, const char *mode){
412         return _razf_open(filename, mode, 1);
413 }
414
415 RAZF* razf_open2(const char *filename, const char *mode){
416         return _razf_open(filename, mode, 0);
417 }
418
419 int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){
420         int64_t n;
421         if(rz->mode != 'r' && rz->mode != 'R') return 0;
422         switch(rz->file_type){
423                 case FILE_TYPE_PLAIN:
424                         if(rz->end == 0x7fffffffffffffffLL){
425                                 if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
426                                 rz->end = lseek(rz->filedes, 0, SEEK_END);
427                                 lseek(rz->filedes, n, SEEK_SET);
428                         }
429                         *u_size = *c_size = rz->end;
430                         return 1;
431                 case FILE_TYPE_GZ:
432                         return 0;
433                 case FILE_TYPE_RZ:
434                         if(rz->src_end == rz->end) return 0;
435                         *u_size = rz->src_end;
436                         *c_size = rz->end;
437                         return 1;
438                 default:
439                         return 0;
440         }
441 }
442
443 static int _razf_read(RAZF* rz, void *data, int size){
444         int ret, tin;
445         if(rz->z_eof || rz->z_err) return 0;
446         if (rz->file_type == FILE_TYPE_PLAIN) {
447                 ret = read(rz->filedes, data, size);
448                 if (ret == 0) rz->z_eof = 1;
449                 return ret;
450         }
451         rz->stream->avail_out = size;
452         rz->stream->next_out  = data;
453         while(rz->stream->avail_out){
454                 if(rz->stream->avail_in == 0){
455                         if(rz->in >= rz->end){ rz->z_eof = 1; break; }
456                         if(rz->end - rz->in < RZ_BUFFER_SIZE){
457                                 rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
458                         } else {
459                                 rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
460                         }
461                         if(rz->stream->avail_in == 0){
462                                 rz->z_eof = 1;
463                                 break;
464                         }
465                         rz->stream->next_in = rz->inbuf;
466                 }
467                 tin = rz->stream->avail_in;
468                 ret = inflate(rz->stream, Z_BLOCK);
469                 rz->in += tin - rz->stream->avail_in;
470                 if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
471                         fprintf(stderr, "[_razf_read] inflate error: %d (at %s:%d)\n", ret, __FILE__, __LINE__);
472                         rz->z_err = 1;
473                         break;
474                 }
475                 if(ret == Z_STREAM_END){
476                         rz->z_eof = 1;
477                         break;
478                 }
479                 if ((rz->stream->data_type&128) && !(rz->stream->data_type&64)){
480                         rz->buf_flush = 1;
481                         rz->next_block_pos = rz->in;
482                         break;
483                 }
484         }
485         return size - rz->stream->avail_out;
486 }
487
488 int razf_read(RAZF *rz, void *data, int size){
489         int ori_size, i;
490         ori_size = size;
491         while(size > 0){
492                 if(rz->buf_len){
493                         if(size < rz->buf_len){
494                                 for(i=0;i<size;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
495                                 rz->buf_off += size;
496                                 rz->buf_len -= size;
497                                 data += size;
498                                 rz->block_off += size;
499                                 size = 0;
500                                 break;
501                         } else {
502                                 for(i=0;i<rz->buf_len;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
503                                 data += rz->buf_len;
504                                 size -= rz->buf_len;
505                                 rz->block_off += rz->buf_len;
506                                 rz->buf_off = 0;
507                                 rz->buf_len = 0;
508                                 if(rz->buf_flush){
509                                         rz->block_pos = rz->next_block_pos;
510                                         rz->block_off = 0;
511                                         rz->buf_flush = 0;
512                                 }
513                         }
514                 } else if(rz->buf_flush){
515                         rz->block_pos = rz->next_block_pos;
516                         rz->block_off = 0;
517                         rz->buf_flush = 0;
518                 }
519                 if(rz->buf_flush) continue;
520                 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
521                 if(rz->z_eof && rz->buf_len == 0) break;
522         }
523         rz->out += ori_size - size;
524         return ori_size - size;
525 }
526
527 int razf_skip(RAZF* rz, int size){
528         int ori_size;
529         ori_size = size;
530         while(size > 0){
531                 if(rz->buf_len){
532                         if(size < rz->buf_len){
533                                 rz->buf_off += size;
534                                 rz->buf_len -= size;
535                                 rz->block_off += size;
536                                 size = 0;
537                                 break;
538                         } else {
539                                 size -= rz->buf_len;
540                                 rz->buf_off = 0;
541                                 rz->buf_len = 0;
542                                 rz->block_off += rz->buf_len;
543                                 if(rz->buf_flush){
544                                         rz->block_pos = rz->next_block_pos;
545                                         rz->block_off = 0;
546                                         rz->buf_flush = 0;
547                                 }
548                         }
549                 } else if(rz->buf_flush){
550                         rz->block_pos = rz->next_block_pos;
551                         rz->block_off = 0;
552                         rz->buf_flush = 0;
553                 }
554                 if(rz->buf_flush) continue;
555                 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
556                 if(rz->z_eof) break;
557         }
558         rz->out += ori_size - size;
559         return ori_size - size;
560 }
561
562 static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
563         lseek(rz->filedes, in, SEEK_SET);
564         rz->in  = in;
565         rz->out = out;
566         rz->block_pos = in;
567         rz->next_block_pos = in;
568         rz->block_off = 0;
569         rz->buf_flush = 0;
570         rz->z_eof = rz->z_err = 0;
571         inflateReset(rz->stream);
572         rz->stream->avail_in = 0;
573         rz->buf_off = rz->buf_len = 0;
574 }
575
576 int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){
577         int64_t pos;
578         rz->z_eof = 0;
579         if(rz->file_type == FILE_TYPE_PLAIN){
580                 rz->buf_off = rz->buf_len = 0;
581                 pos = block_start + block_offset;
582                 pos = lseek(rz->filedes, pos, SEEK_SET);
583                 rz->out = rz->in = pos;
584                 return pos;
585         }
586         if(block_start == rz->block_pos && block_offset >= rz->block_off) {
587                 block_offset -= rz->block_off;
588                 goto SKIP; // Needn't reset inflate
589         }
590         if(block_start  == 0) block_start = rz->header_size; // Automaticly revist wrong block_start
591         _razf_reset_read(rz, block_start, 0);
592         SKIP:
593         if(block_offset) razf_skip(rz, block_offset);
594         return rz->block_off;
595 }
596
597 int64_t razf_seek(RAZF* rz, int64_t pos, int where){
598         int64_t idx;
599         int64_t seek_pos, new_out;
600         rz->z_eof = 0;
601         if (where == SEEK_CUR) pos += rz->out;
602         else if (where == SEEK_END) pos += rz->src_end;
603         if(rz->file_type == FILE_TYPE_PLAIN){
604                 seek_pos = lseek(rz->filedes, pos, SEEK_SET);
605                 rz->buf_off = rz->buf_len = 0;
606                 rz->out = rz->in = seek_pos;
607                 return seek_pos;
608         } else if(rz->file_type == FILE_TYPE_GZ){
609                 if(pos >= rz->out) goto SKIP;
610                 return rz->out;
611         }
612         if(pos == rz->out) return pos;
613         if(pos > rz->src_end) return rz->out;
614         if(!rz->seekable || !rz->load_index){
615                 if(pos >= rz->out) goto SKIP;
616         }
617         idx = pos / RZ_BLOCK_SIZE - 1;
618         seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
619         new_out  = (idx + 1) * RZ_BLOCK_SIZE;
620         if(pos > rz->out && new_out <= rz->out) goto SKIP;
621         _razf_reset_read(rz, seek_pos, new_out);
622         SKIP:
623         razf_skip(rz, (int)(pos - rz->out));
624         return rz->out;
625 }
626
627 uint64_t razf_tell2(RAZF *rz)
628 {
629         /*
630         if (rz->load_index) {
631                 int64_t idx, seek_pos;
632                 idx = rz->out / RZ_BLOCK_SIZE - 1;
633                 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
634                 if (seek_pos != rz->block_pos || rz->out%RZ_BLOCK_SIZE != rz->block_off)
635                         fprintf(stderr, "[razf_tell2] inconsistent block offset: (%lld, %lld) != (%lld, %lld)\n",
636                                         (long long)seek_pos, (long long)rz->out%RZ_BLOCK_SIZE, (long long)rz->block_pos, (long long) rz->block_off);
637         }
638         */
639         return (uint64_t)rz->block_pos<<16 | (rz->block_off&0xffff);
640 }
641
642 int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
643 {
644         if (where != SEEK_SET) return -1;
645         return razf_jump(rz, voffset>>16, voffset&0xffff);
646 }
647
648 void razf_close(RAZF *rz){
649         if(rz->mode == 'w'){
650 #ifndef _RZ_READONLY
651                 razf_end_flush(rz);
652                 deflateEnd(rz->stream);
653                 save_zindex(rz, rz->filedes);
654                 if(is_big_endian()){
655                         write(rz->filedes, &rz->in, sizeof(int64_t));
656                         write(rz->filedes, &rz->out, sizeof(int64_t));
657                 } else {
658                         uint64_t v64 = byte_swap_8((uint64_t)rz->in);
659                         write(rz->filedes, &v64, sizeof(int64_t));
660                         v64 = byte_swap_8((uint64_t)rz->out);
661                         write(rz->filedes, &v64, sizeof(int64_t));
662                 }
663 #endif
664         } else if(rz->mode == 'r'){
665                 if(rz->stream) inflateEnd(rz->stream);
666         }
667         if(rz->inbuf) free(rz->inbuf);
668         if(rz->outbuf) free(rz->outbuf);
669         if(rz->header){
670                 free(rz->header->extra);
671                 free(rz->header->name);
672                 free(rz->header->comment);
673                 free(rz->header);
674         }
675         if(rz->index){
676                 free(rz->index->bin_offsets);
677                 free(rz->index->cell_offsets);
678                 free(rz->index);
679         }
680         free(rz->stream);
681         close(rz->filedes);
682         free(rz);
683 }
684
685 #endif