annotate PsiCLASS-1.0.2/samtools-0.1.19/razf.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 * RAZF : Random Access compressed(Z) File
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 * Version: 1.0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 * Release Date: 2008-10-27
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 * Copyright 2008, Jue Ruan <ruanjue@gmail.com>, Heng Li <lh3@sanger.ac.uk>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 * All rights reserved.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 * Redistribution and use in source and binary forms, with or without
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 * modification, are permitted provided that the following conditions
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 * are met:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 * 1. Redistributions of source code must retain the above copyright
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 * notice, this list of conditions and the following disclaimer.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 * 2. Redistributions in binary form must reproduce the above copyright
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 * notice, this list of conditions and the following disclaimer in the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 * documentation and/or other materials provided with the distribution.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 * SUCH DAMAGE.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 #ifndef _NO_RAZF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 #include <fcntl.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 #include <unistd.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 #include "razf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 #if ZLIB_VERNUM < 0x1221
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 struct _gz_header_s {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 int text;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 uLong time;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 int xflags;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 int os;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 Bytef *extra;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 uInt extra_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 uInt extra_max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 Bytef *name;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 uInt name_max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 Bytef *comment;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 uInt comm_max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 int hcrc;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 int done;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 #warning "zlib < 1.2.2.1; RAZF writing is disabled."
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 #define DEF_MEM_LEVEL 8
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 static inline uint32_t byte_swap_4(uint32_t v){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 static inline uint64_t byte_swap_8(uint64_t v){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 static inline int is_big_endian(){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 int x = 0x01;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 char *c = (char*)&x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 return (c[0] != 0x01);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 #ifndef _RZ_READONLY
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 static void add_zindex(RAZF *rz, int64_t in, int64_t out){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 if(rz->index->size == rz->index->cap){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 rz->index->cap = rz->index->cap * 1.5 + 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 rz->index->bin_offsets = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 rz->index->cell_offsets[rz->index->size] = out - rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 rz->index->size ++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 static void save_zindex(RAZF *rz, int fd){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 int32_t i, v32;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 int is_be;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 is_be = is_big_endian();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 if(is_be) write(fd, &rz->index->size, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 v32 = byte_swap_4((uint32_t)rz->index->size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 write(fd, &v32, sizeof(uint32_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 v32 = rz->index->size / RZ_BIN_SIZE + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 if(!is_be){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 static void load_zindex(RAZF *rz, knetFile *fp){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 static void load_zindex(RAZF *rz, int fd){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 int32_t i, v32;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 int is_be;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 if(!rz->load_index) return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 is_be = is_big_endian();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 knet_read(fp, &rz->index->size, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 read(fd, &rz->index->size, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 rz->index->cap = rz->index->size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 v32 = rz->index->size / RZ_BIN_SIZE + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 rz->index->bin_offsets = malloc(sizeof(int64_t) * v32);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 if(!is_be){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 #ifdef _RZ_READONLY
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 static RAZF* razf_open_w(int fd)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 fprintf(stderr, "[razf_open_w] Writing is not available with zlib ver < 1.2.2.1\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 static RAZF* razf_open_w(int fd){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 RAZF *rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 #ifdef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 setmode(fd, O_BINARY);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 rz = calloc(1, sizeof(RAZF));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 rz->mode = 'w';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 rz->x.fpw = fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 rz->filedes = fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 rz->stream = calloc(sizeof(z_stream), 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 rz->inbuf = malloc(RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 rz->outbuf = malloc(RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 rz->index = calloc(sizeof(ZBlockIndex), 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 rz->stream->avail_out = RZ_BUFFER_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 rz->stream->next_out = rz->outbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 rz->header = calloc(sizeof(gz_header), 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 rz->header->os = 0x03; //Unix
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 rz->header->text = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 rz->header->time = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 rz->header->extra = malloc(7);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 strncpy((char*)rz->header->extra, "RAZF", 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 rz->header->extra[4] = 1; // obsolete field
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 // block size = RZ_BLOCK_SIZE, Big-Endian
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 rz->header->extra[5] = RZ_BLOCK_SIZE >> 8;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 rz->header->extra[6] = RZ_BLOCK_SIZE & 0xFF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 rz->header->extra_len = 7;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 rz->header->name = rz->header->comment = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 rz->header->hcrc = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 deflateSetHeader(rz->stream, rz->header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 rz->block_pos = rz->block_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 return rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 static void _razf_write(RAZF* rz, const void *data, int size){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 int tout;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 rz->stream->avail_in = size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 rz->stream->next_in = (void*)data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 while(1){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 tout = rz->stream->avail_out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 deflate(rz->stream, Z_NO_FLUSH);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 rz->out += tout - rz->stream->avail_out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 if(rz->stream->avail_out) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 rz->stream->avail_out = RZ_BUFFER_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 rz->stream->next_out = rz->outbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 if(rz->stream->avail_in == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 rz->in += size - rz->stream->avail_in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 rz->block_off += size - rz->stream->avail_in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 static void razf_flush(RAZF *rz){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 uint32_t tout;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 if(rz->buf_len){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 _razf_write(rz, rz->inbuf, rz->buf_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 rz->buf_off = rz->buf_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 if(rz->stream->avail_out){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 rz->stream->avail_out = RZ_BUFFER_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 rz->stream->next_out = rz->outbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 while(1){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 tout = rz->stream->avail_out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 deflate(rz->stream, Z_FULL_FLUSH);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 rz->out += tout - rz->stream->avail_out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 if(rz->stream->avail_out == 0){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 rz->stream->avail_out = RZ_BUFFER_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 rz->stream->next_out = rz->outbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 } else break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 rz->block_pos = rz->out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 rz->block_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 static void razf_end_flush(RAZF *rz){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 uint32_t tout;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 if(rz->buf_len){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 _razf_write(rz, rz->inbuf, rz->buf_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 rz->buf_off = rz->buf_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 while(1){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 tout = rz->stream->avail_out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 deflate(rz->stream, Z_FINISH);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 rz->out += tout - rz->stream->avail_out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 if(rz->stream->avail_out < RZ_BUFFER_SIZE){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 rz->stream->avail_out = RZ_BUFFER_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 rz->stream->next_out = rz->outbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 } else break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 static void _razf_buffered_write(RAZF *rz, const void *data, int size){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 int i, n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 while(1){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 if(rz->buf_len == RZ_BUFFER_SIZE){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 _razf_write(rz, rz->inbuf, rz->buf_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 rz->buf_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 if(size + rz->buf_len < RZ_BUFFER_SIZE){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 for(i=0;i<size;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 rz->buf_len += size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 n = RZ_BUFFER_SIZE - rz->buf_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 for(i=0;i<n;i++) ((char*)rz->inbuf + rz->buf_len)[i] = ((char*)data)[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 size -= n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 data += n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 rz->buf_len += n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 int razf_write(RAZF* rz, const void *data, int size){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 int ori_size, n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 int64_t next_block;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 ori_size = size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 while(rz->in + rz->buf_len + size >= next_block){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 n = next_block - rz->in - rz->buf_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 _razf_buffered_write(rz, data, n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 data += n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 size -= n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 razf_flush(rz);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 add_zindex(rz, rz->in, rz->out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 next_block = ((rz->in / RZ_BLOCK_SIZE) + 1) * RZ_BLOCK_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 _razf_buffered_write(rz, data, size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 return ori_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 /* gzip flag byte */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 #define COMMENT 0x10 /* bit 4 set: file comment present */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 #define RESERVED 0xE0 /* bits 5..7: reserved */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *extra_len){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 int method, flags, n, len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 if(size < 2) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 if(data[0] != 0x1f || data[1] != 0x8b) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 if(size < 4) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 method = data[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 flags = data[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 if(method != Z_DEFLATED || (flags & RESERVED)) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 n = 4 + 6; // Skip 6 bytes
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 *extra_off = n + 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 *extra_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 if(flags & EXTRA_FIELD){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 if(size < n + 2) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 len = ((int)data[n + 1] << 8) | data[n];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 n += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 *extra_off = n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 while(len){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 if(n >= size) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 n ++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 len --;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 *extra_len = n - (*extra_off);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 if(flags & ORIG_NAME) while(n < size && data[n++]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 if(flags & COMMENT) while(n < size && data[n++]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 if(flags & HEAD_CRC){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 if(n + 2 > size) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 n += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 return n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 static RAZF* razf_open_r(knetFile *fp, int _load_index){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 static RAZF* razf_open_r(int fd, int _load_index){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 RAZF *rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 int ext_off, ext_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 int n, is_be, ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 int64_t end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 unsigned char c[] = "RAZF";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 rz = calloc(1, sizeof(RAZF));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 rz->mode = 'r';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 rz->x.fpr = fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 #ifdef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 setmode(fd, O_BINARY);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 rz->filedes = fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 rz->stream = calloc(sizeof(z_stream), 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 rz->inbuf = malloc(RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 rz->outbuf = malloc(RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 if(ret == 0){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 PLAIN_FILE:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 rz->in = n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 rz->file_type = FILE_TYPE_PLAIN;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 memcpy(rz->outbuf, rz->inbuf, n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 rz->buf_len = n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 free(rz->stream);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 rz->stream = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 return rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 rz->header_size = ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 ret = inflateInit2(rz->stream, -WINDOW_BITS);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 if(ret != Z_OK){ inflateEnd(rz->stream); goto PLAIN_FILE;}
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 rz->stream->avail_in = n - rz->header_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 rz->stream->next_in = rz->inbuf + rz->header_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 rz->stream->avail_out = RZ_BUFFER_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 rz->stream->next_out = rz->outbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 rz->file_type = FILE_TYPE_GZ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 rz->in = rz->header_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 rz->block_pos = rz->header_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 rz->next_block_pos = rz->header_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 rz->block_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 if(ext_len < 7 || memcmp(rz->inbuf + ext_off, c, 4) != 0) return rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 if(((((unsigned char*)rz->inbuf)[ext_off + 5] << 8) | ((unsigned char*)rz->inbuf)[ext_off + 6]) != RZ_BLOCK_SIZE){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
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__);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 return rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 rz->load_index = _load_index;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 rz->file_type = FILE_TYPE_RZ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 if(knet_seek(fp, -16, SEEK_END) == -1){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 if(lseek(fd, -16, SEEK_END) == -1){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 UNSEEKABLE:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 rz->seekable = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 rz->index = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 rz->src_end = rz->end = 0x7FFFFFFFFFFFFFFFLL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 is_be = is_big_endian();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 rz->seekable = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 knet_read(fp, &end, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 read(fd, &end, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 else rz->src_end = end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 knet_read(fp, &end, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 read(fd, &end, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 else rz->end = end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 if(n > rz->end){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 rz->stream->avail_in -= n - rz->end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 n = rz->end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 if(rz->end > rz->src_end){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 knet_seek(fp, rz->in, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 lseek(fd, rz->in, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 goto UNSEEKABLE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 knet_seek(fp, rz->end, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 if(knet_tell(fp) != rz->end){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 knet_seek(fp, rz->in, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 if(lseek(fd, rz->end, SEEK_SET) != rz->end){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 lseek(fd, rz->in, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 goto UNSEEKABLE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 load_zindex(rz, fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 knet_seek(fp, n, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 load_zindex(rz, fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 lseek(fd, n, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 return rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 RAZF* razf_dopen(int fd, const char *mode){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 if (strstr(mode, "r")) fprintf(stderr,"[razf_dopen] implement me\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 else if(strstr(mode, "w")) return razf_open_w(fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 return NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 RAZF* razf_dopen2(int fd, const char *mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 fprintf(stderr,"[razf_dopen2] implement me\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 return NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 RAZF* razf_dopen(int fd, const char *mode){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 if(strstr(mode, "r")) return razf_open_r(fd, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 else if(strstr(mode, "w")) return razf_open_w(fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 else return NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 RAZF* razf_dopen2(int fd, const char *mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 if(strstr(mode, "r")) return razf_open_r(fd, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 else if(strstr(mode, "w")) return razf_open_w(fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 else return NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494 static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 int fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 RAZF *rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 if(strstr(mode, "r")){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 knetFile *fd = knet_open(filename, "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500 if (fd == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 fprintf(stderr, "[_razf_open] fail to open %s\n", filename);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502 return NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 #ifdef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 fd = open(filename, O_RDONLY | O_BINARY);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 fd = open(filename, O_RDONLY);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511 if(fd < 0) return NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512 rz = razf_open_r(fd, _load_index);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513 } else if(strstr(mode, "w")){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 #ifdef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519 if(fd < 0) return NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 rz = razf_open_w(fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 } else return NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 return rz;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525 RAZF* razf_open(const char *filename, const char *mode){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 return _razf_open(filename, mode, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
529 RAZF* razf_open2(const char *filename, const char *mode){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
530 return _razf_open(filename, mode, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
531 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
532
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
533 int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
534 int64_t n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
535 if(rz->mode != 'r' && rz->mode != 'R') return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
536 switch(rz->file_type){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
537 case FILE_TYPE_PLAIN:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
538 if(rz->end == 0x7fffffffffffffffLL){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
539 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
540 if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
541 n = knet_tell(rz->x.fpr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
542 knet_seek(rz->x.fpr, 0, SEEK_END);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
543 rz->end = knet_tell(rz->x.fpr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
544 knet_seek(rz->x.fpr, n, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
545 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
546 if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
547 rz->end = lseek(rz->filedes, 0, SEEK_END);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
548 lseek(rz->filedes, n, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
549 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
550 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
551 *u_size = *c_size = rz->end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
552 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
553 case FILE_TYPE_GZ:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
554 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
555 case FILE_TYPE_RZ:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
556 if(rz->src_end == rz->end) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
557 *u_size = rz->src_end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
558 *c_size = rz->end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
559 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
560 default:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
561 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
562 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
563 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
564
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
565 static int _razf_read(RAZF* rz, void *data, int size){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
566 int ret, tin;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
567 if(rz->z_eof || rz->z_err) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
568 if (rz->file_type == FILE_TYPE_PLAIN) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
569 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
570 ret = knet_read(rz->x.fpr, data, size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
571 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
572 ret = read(rz->filedes, data, size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
573 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
574 if (ret == 0) rz->z_eof = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
575 return ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
576 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
577 rz->stream->avail_out = size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
578 rz->stream->next_out = data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
579 while(rz->stream->avail_out){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
580 if(rz->stream->avail_in == 0){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
581 if(rz->in >= rz->end){ rz->z_eof = 1; break; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
582 if(rz->end - rz->in < RZ_BUFFER_SIZE){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
583 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
584 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
585 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
586 rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
587 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
588 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
589 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
590 rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
591 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
592 rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
593 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
594 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
595 if(rz->stream->avail_in == 0){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
596 rz->z_eof = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
597 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
598 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
599 rz->stream->next_in = rz->inbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
600 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
601 tin = rz->stream->avail_in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
602 ret = inflate(rz->stream, Z_BLOCK);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
603 rz->in += tin - rz->stream->avail_in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
604 if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
605 fprintf(stderr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
606 rz->z_err = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
607 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
608 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
609 if(ret == Z_STREAM_END){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
610 rz->z_eof = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
611 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
612 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
613 if ((rz->stream->data_type&128) && !(rz->stream->data_type&64)){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
614 rz->buf_flush = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
615 rz->next_block_pos = rz->in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
616 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
617 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
618 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
619 return size - rz->stream->avail_out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
620 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
621
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
622 int razf_read(RAZF *rz, void *data, int size){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
623 int ori_size, i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
624 ori_size = size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
625 while(size > 0){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
626 if(rz->buf_len){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
627 if(size < rz->buf_len){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
628 for(i=0;i<size;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
629 rz->buf_off += size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
630 rz->buf_len -= size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
631 data += size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
632 rz->block_off += size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
633 size = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
634 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
635 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
636 for(i=0;i<rz->buf_len;i++) ((char*)data)[i] = ((char*)rz->outbuf + rz->buf_off)[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
637 data += rz->buf_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
638 size -= rz->buf_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
639 rz->block_off += rz->buf_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
640 rz->buf_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
641 rz->buf_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
642 if(rz->buf_flush){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
643 rz->block_pos = rz->next_block_pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
644 rz->block_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
645 rz->buf_flush = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
646 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
647 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
648 } else if(rz->buf_flush){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
649 rz->block_pos = rz->next_block_pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
650 rz->block_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
651 rz->buf_flush = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
652 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
653 if(rz->buf_flush) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
654 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
655 if(rz->z_eof && rz->buf_len == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
656 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
657 rz->out += ori_size - size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
658 return ori_size - size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
659 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
660
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
661 int razf_skip(RAZF* rz, int size){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
662 int ori_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
663 ori_size = size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
664 while(size > 0){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
665 if(rz->buf_len){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
666 if(size < rz->buf_len){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
667 rz->buf_off += size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
668 rz->buf_len -= size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
669 rz->block_off += size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
670 size = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
671 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
672 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
673 size -= rz->buf_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
674 rz->buf_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
675 rz->buf_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
676 rz->block_off += rz->buf_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
677 if(rz->buf_flush){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
678 rz->block_pos = rz->next_block_pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
679 rz->block_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
680 rz->buf_flush = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
681 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
682 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
683 } else if(rz->buf_flush){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
684 rz->block_pos = rz->next_block_pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
685 rz->block_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
686 rz->buf_flush = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
687 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
688 if(rz->buf_flush) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
689 rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
690 if(rz->z_eof || rz->z_err) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
691 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
692 rz->out += ori_size - size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
693 return ori_size - size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
694 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
695
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
696 static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
697 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
698 knet_seek(rz->x.fpr, in, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
699 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
700 lseek(rz->filedes, in, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
701 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
702 rz->in = in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
703 rz->out = out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
704 rz->block_pos = in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
705 rz->next_block_pos = in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
706 rz->block_off = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
707 rz->buf_flush = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
708 rz->z_eof = rz->z_err = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
709 inflateReset(rz->stream);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
710 rz->stream->avail_in = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
711 rz->buf_off = rz->buf_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
712 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
713
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
714 int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
715 int64_t pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
716 rz->z_eof = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
717 if(rz->file_type == FILE_TYPE_PLAIN){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
718 rz->buf_off = rz->buf_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
719 pos = block_start + block_offset;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
720 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
721 knet_seek(rz->x.fpr, pos, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
722 pos = knet_tell(rz->x.fpr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
723 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
724 pos = lseek(rz->filedes, pos, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
725 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
726 rz->out = rz->in = pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
727 return pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
728 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
729 if(block_start == rz->block_pos && block_offset >= rz->block_off) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
730 block_offset -= rz->block_off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
731 goto SKIP; // Needn't reset inflate
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
732 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
733 if(block_start == 0) block_start = rz->header_size; // Automaticly revist wrong block_start
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
734 _razf_reset_read(rz, block_start, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
735 SKIP:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
736 if(block_offset) razf_skip(rz, block_offset);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
737 return rz->block_off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
738 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
739
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
740 int64_t razf_seek(RAZF* rz, int64_t pos, int where){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
741 int64_t idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
742 int64_t seek_pos, new_out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
743 rz->z_eof = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
744 if (where == SEEK_CUR) pos += rz->out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
745 else if (where == SEEK_END) pos += rz->src_end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
746 if(rz->file_type == FILE_TYPE_PLAIN){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
747 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
748 knet_seek(rz->x.fpr, pos, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
749 seek_pos = knet_tell(rz->x.fpr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
750 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
751 seek_pos = lseek(rz->filedes, pos, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
752 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
753 rz->buf_off = rz->buf_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
754 rz->out = rz->in = seek_pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
755 return seek_pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
756 } else if(rz->file_type == FILE_TYPE_GZ){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
757 if(pos >= rz->out) goto SKIP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
758 return rz->out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
759 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
760 if(pos == rz->out) return pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
761 if(pos > rz->src_end) return rz->out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
762 if(!rz->seekable || !rz->load_index){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
763 if(pos >= rz->out) goto SKIP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
764 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
765 idx = pos / RZ_BLOCK_SIZE - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
766 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
767 new_out = (idx + 1) * RZ_BLOCK_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
768 if(pos > rz->out && new_out <= rz->out) goto SKIP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
769 _razf_reset_read(rz, seek_pos, new_out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
770 SKIP:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
771 razf_skip(rz, (int)(pos - rz->out));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
772 return rz->out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
773 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
774
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
775 uint64_t razf_tell2(RAZF *rz)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
776 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
777 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
778 if (rz->load_index) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
779 int64_t idx, seek_pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
780 idx = rz->out / RZ_BLOCK_SIZE - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
781 seek_pos = (idx < 0)? rz->header_size:(rz->index->cell_offsets[idx] + rz->index->bin_offsets[idx / RZ_BIN_SIZE]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
782 if (seek_pos != rz->block_pos || rz->out%RZ_BLOCK_SIZE != rz->block_off)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
783 fprintf(stderr, "[razf_tell2] inconsistent block offset: (%lld, %lld) != (%lld, %lld)\n",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
784 (long long)seek_pos, (long long)rz->out%RZ_BLOCK_SIZE, (long long)rz->block_pos, (long long) rz->block_off);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
785 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
786 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
787 return (uint64_t)rz->block_pos<<16 | (rz->block_off&0xffff);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
788 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
789
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
790 int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
791 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
792 if (where != SEEK_SET) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
793 return razf_jump(rz, voffset>>16, voffset&0xffff);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
794 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
795
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
796 void razf_close(RAZF *rz){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
797 if(rz->mode == 'w'){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
798 #ifndef _RZ_READONLY
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
799 razf_end_flush(rz);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
800 deflateEnd(rz->stream);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
801 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
802 save_zindex(rz, rz->x.fpw);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
803 if(is_big_endian()){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
804 write(rz->x.fpw, &rz->in, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
805 write(rz->x.fpw, &rz->out, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
806 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
807 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
808 write(rz->x.fpw, &v64, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
809 v64 = byte_swap_8((uint64_t)rz->out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
810 write(rz->x.fpw, &v64, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
811 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
812 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
813 save_zindex(rz, rz->filedes);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
814 if(is_big_endian()){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
815 write(rz->filedes, &rz->in, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
816 write(rz->filedes, &rz->out, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
817 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
818 uint64_t v64 = byte_swap_8((uint64_t)rz->in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
819 write(rz->filedes, &v64, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
820 v64 = byte_swap_8((uint64_t)rz->out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
821 write(rz->filedes, &v64, sizeof(int64_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
822 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
823 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
824 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
825 } else if(rz->mode == 'r'){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
826 if(rz->stream) inflateEnd(rz->stream);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
827 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
828 if(rz->inbuf) free(rz->inbuf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
829 if(rz->outbuf) free(rz->outbuf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
830 if(rz->header){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
831 free(rz->header->extra);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
832 free(rz->header->name);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
833 free(rz->header->comment);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
834 free(rz->header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
835 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
836 if(rz->index){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
837 free(rz->index->bin_offsets);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
838 free(rz->index->cell_offsets);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
839 free(rz->index);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
840 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
841 free(rz->stream);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
842 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
843 if (rz->mode == 'r')
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
844 knet_close(rz->x.fpr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
845 if (rz->mode == 'w')
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
846 close(rz->x.fpw);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
847 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
848 close(rz->filedes);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
849 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
850 free(rz);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
851 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
852
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
853 #endif