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