annotate pyPRADA_1.2/tools/samtools-0.1.16/razf.c @ 3:f17965495ec9 draft default tip

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