annotate pyPRADA_1.2/tools/samtools-0.1.16/bgzf.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 /* The MIT License
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining a copy
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 of this software and associated documentation files (the "Software"), to deal
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 in the Software without restriction, including without limitation the rights
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 copies of the Software, and to permit persons to whom the Software is
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 furnished to do so, subject to the following conditions:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 The above copyright notice and this permission notice shall be included in
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 all copies or substantial portions of the Software.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 THE SOFTWARE.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 /*
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 2009-06-29 by lh3: cache recent uncompressed blocks.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 2009-06-25 by lh3: optionally use my knetfile library to access file on a FTP.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 2009-06-12 by lh3: support a mode string like "wu" where 'u' for uncompressed output */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 #include <unistd.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 #include <fcntl.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 #include <sys/types.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 #include <sys/stat.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 #include "bgzf.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 #include "khash.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 int size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 uint8_t *block;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 int64_t end_offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 } cache_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 KHASH_MAP_INIT_INT64(cache, cache_t)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 #if defined(_WIN32) || defined(_MSC_VER)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 #define ftello(fp) ftell(fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 #define fseeko(fp, offset, whence) fseek(fp, offset, whence)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 extern off_t ftello(FILE *stream);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 extern int fseeko(FILE *stream, off_t offset, int whence);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 typedef int8_t bgzf_byte_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 static const int DEFAULT_BLOCK_SIZE = 64 * 1024;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 static const int MAX_BLOCK_SIZE = 64 * 1024;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 static const int BLOCK_HEADER_LENGTH = 18;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 static const int BLOCK_FOOTER_LENGTH = 8;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 static const int GZIP_ID1 = 31;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 static const int GZIP_ID2 = 139;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 static const int CM_DEFLATE = 8;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 static const int FLG_FEXTRA = 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 static const int OS_UNKNOWN = 255;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 static const int BGZF_ID1 = 66; // 'B'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 static const int BGZF_ID2 = 67; // 'C'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 static const int BGZF_LEN = 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 static const int BGZF_XLEN = 6; // BGZF_LEN+4
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 static const int GZIP_WINDOW_BITS = -15; // no zlib header
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 static const int Z_DEFAULT_MEM_LEVEL = 8;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 inline
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 void
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 packInt16(uint8_t* buffer, uint16_t value)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 buffer[0] = value;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 buffer[1] = value >> 8;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 inline
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 int
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 unpackInt16(const uint8_t* buffer)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 return (buffer[0] | (buffer[1] << 8));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 inline
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 void
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 packInt32(uint8_t* buffer, uint32_t value)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 buffer[0] = value;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 buffer[1] = value >> 8;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 buffer[2] = value >> 16;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 buffer[3] = value >> 24;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 static inline
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 int
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 bgzf_min(int x, int y)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 return (x < y) ? x : y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 static
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 void
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 report_error(BGZF* fp, const char* message) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 fp->error = message;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 int bgzf_check_bgzf(const char *fn)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 BGZF *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 uint8_t buf[10],magic[10]="\037\213\010\4\0\0\0\0\0\377";
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 int n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 if ((fp = bgzf_open(fn, "r")) == 0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 fprintf(stderr, "[bgzf_check_bgzf] failed to open the file: %s\n",fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 n = knet_read(fp->x.fpr, buf, 10);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 n = fread(buf, 1, 10, fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 bgzf_close(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 if ( n!=10 )
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 if ( !memcmp(magic, buf, 10) ) return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 static BGZF *bgzf_read_init()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 BGZF *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 fp = calloc(1, sizeof(BGZF));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 fp->uncompressed_block_size = MAX_BLOCK_SIZE;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 fp->uncompressed_block = malloc(MAX_BLOCK_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 fp->compressed_block_size = MAX_BLOCK_SIZE;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 fp->compressed_block = malloc(MAX_BLOCK_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 fp->cache_size = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 fp->cache = kh_init(cache);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 return fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 static
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 BGZF*
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 open_read(int fd)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 knetFile *file = knet_dopen(fd, "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 FILE* file = fdopen(fd, "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 BGZF* fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 if (file == 0) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 fp = bgzf_read_init();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 fp->file_descriptor = fd;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 fp->open_mode = 'r';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 fp->x.fpr = file;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 fp->file = file;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 return fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 static
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 BGZF*
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 open_write(int fd, int compress_level) // compress_level==-1 for the default level
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 FILE* file = fdopen(fd, "w");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180 BGZF* fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 if (file == 0) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182 fp = malloc(sizeof(BGZF));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 fp->file_descriptor = fd;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 fp->open_mode = 'w';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 fp->owned_file = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186 fp->compress_level = compress_level < 0? Z_DEFAULT_COMPRESSION : compress_level; // Z_DEFAULT_COMPRESSION==-1
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187 if (fp->compress_level > 9) fp->compress_level = Z_DEFAULT_COMPRESSION;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 fp->x.fpw = file;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 fp->file = file;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193 fp->uncompressed_block_size = DEFAULT_BLOCK_SIZE;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 fp->uncompressed_block = NULL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 fp->compressed_block_size = MAX_BLOCK_SIZE;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196 fp->compressed_block = malloc(MAX_BLOCK_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 fp->block_address = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198 fp->block_offset = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 fp->block_length = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 fp->error = NULL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 return fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 BGZF*
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 bgzf_open(const char* __restrict path, const char* __restrict mode)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 BGZF* fp = NULL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208 if (strchr(mode, 'r') || strchr(mode, 'R')) { /* The reading mode is preferred. */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
210 knetFile *file = knet_open(path, mode);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
211 if (file == 0) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
212 fp = bgzf_read_init();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
213 fp->file_descriptor = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
214 fp->open_mode = 'r';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
215 fp->x.fpr = file;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
216 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
217 int fd, oflag = O_RDONLY;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
218 #ifdef _WIN32
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
219 oflag |= O_BINARY;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
220 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
221 fd = open(path, oflag);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
222 if (fd == -1) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
223 fp = open_read(fd);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
224 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
225 } else if (strchr(mode, 'w') || strchr(mode, 'W')) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
226 int fd, compress_level = -1, oflag = O_WRONLY | O_CREAT | O_TRUNC;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
227 #ifdef _WIN32
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
228 oflag |= O_BINARY;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
229 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
230 fd = open(path, oflag, 0666);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
231 if (fd == -1) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
232 { // set compress_level
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
233 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
234 for (i = 0; mode[i]; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
235 if (mode[i] >= '0' && mode[i] <= '9') break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
236 if (mode[i]) compress_level = (int)mode[i] - '0';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
237 if (strchr(mode, 'u')) compress_level = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
238 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
239 fp = open_write(fd, compress_level);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
240 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
241 if (fp != NULL) fp->owned_file = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
242 return fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
243 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
244
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
245 BGZF*
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
246 bgzf_fdopen(int fd, const char * __restrict mode)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
247 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
248 if (fd == -1) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
249 if (mode[0] == 'r' || mode[0] == 'R') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
250 return open_read(fd);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
251 } else if (mode[0] == 'w' || mode[0] == 'W') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
252 int i, compress_level = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
253 for (i = 0; mode[i]; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
254 if (mode[i] >= '0' && mode[i] <= '9') break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
255 if (mode[i]) compress_level = (int)mode[i] - '0';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
256 if (strchr(mode, 'u')) compress_level = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
257 return open_write(fd, compress_level);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
258 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
259 return NULL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
260 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
261 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
262
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
263 static
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
264 int
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
265 deflate_block(BGZF* fp, int block_length)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
266 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
267 // Deflate the block in fp->uncompressed_block into fp->compressed_block.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
268 // Also adds an extra field that stores the compressed block length.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
269
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
270 bgzf_byte_t* buffer = fp->compressed_block;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
271 int buffer_size = fp->compressed_block_size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
272
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
273 // Init gzip header
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
274 buffer[0] = GZIP_ID1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
275 buffer[1] = GZIP_ID2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
276 buffer[2] = CM_DEFLATE;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
277 buffer[3] = FLG_FEXTRA;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
278 buffer[4] = 0; // mtime
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
279 buffer[5] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
280 buffer[6] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
281 buffer[7] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
282 buffer[8] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
283 buffer[9] = OS_UNKNOWN;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
284 buffer[10] = BGZF_XLEN;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
285 buffer[11] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
286 buffer[12] = BGZF_ID1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
287 buffer[13] = BGZF_ID2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
288 buffer[14] = BGZF_LEN;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
289 buffer[15] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
290 buffer[16] = 0; // placeholder for block length
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
291 buffer[17] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
292
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
293 // loop to retry for blocks that do not compress enough
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
294 int input_length = block_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
295 int compressed_length = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
296 while (1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
297 z_stream zs;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
298 zs.zalloc = NULL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
299 zs.zfree = NULL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
300 zs.next_in = fp->uncompressed_block;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
301 zs.avail_in = input_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
302 zs.next_out = (void*)&buffer[BLOCK_HEADER_LENGTH];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
303 zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
304
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
305 int status = deflateInit2(&zs, fp->compress_level, Z_DEFLATED,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
306 GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
307 if (status != Z_OK) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
308 report_error(fp, "deflate init failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
309 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
310 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
311 status = deflate(&zs, Z_FINISH);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
312 if (status != Z_STREAM_END) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
313 deflateEnd(&zs);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
314 if (status == Z_OK) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
315 // Not enough space in buffer.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
316 // Can happen in the rare case the input doesn't compress enough.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
317 // Reduce the amount of input until it fits.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
318 input_length -= 1024;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
319 if (input_length <= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
320 // should never happen
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
321 report_error(fp, "input reduction failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
322 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
323 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
324 continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
325 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
326 report_error(fp, "deflate failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
327 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
328 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
329 status = deflateEnd(&zs);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
330 if (status != Z_OK) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
331 report_error(fp, "deflate end failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
332 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
333 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
334 compressed_length = zs.total_out;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
335 compressed_length += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
336 if (compressed_length > MAX_BLOCK_SIZE) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
337 // should never happen
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
338 report_error(fp, "deflate overflow");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
339 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
340 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
341 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
342 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
343
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
344 packInt16((uint8_t*)&buffer[16], compressed_length-1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
345 uint32_t crc = crc32(0L, NULL, 0L);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
346 crc = crc32(crc, fp->uncompressed_block, input_length);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
347 packInt32((uint8_t*)&buffer[compressed_length-8], crc);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
348 packInt32((uint8_t*)&buffer[compressed_length-4], input_length);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
349
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
350 int remaining = block_length - input_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
351 if (remaining > 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
352 if (remaining > input_length) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
353 // should never happen (check so we can use memcpy)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
354 report_error(fp, "remainder too large");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
355 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
356 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
357 memcpy(fp->uncompressed_block,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
358 fp->uncompressed_block + input_length,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
359 remaining);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
360 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
361 fp->block_offset = remaining;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
362 return compressed_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
363 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
364
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
365 static
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
366 int
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
367 inflate_block(BGZF* fp, int block_length)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
368 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
369 // Inflate the block in fp->compressed_block into fp->uncompressed_block
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
370
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
371 z_stream zs;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
372 int status;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
373 zs.zalloc = NULL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
374 zs.zfree = NULL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
375 zs.next_in = fp->compressed_block + 18;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
376 zs.avail_in = block_length - 16;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
377 zs.next_out = fp->uncompressed_block;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
378 zs.avail_out = fp->uncompressed_block_size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
379
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
380 status = inflateInit2(&zs, GZIP_WINDOW_BITS);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
381 if (status != Z_OK) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
382 report_error(fp, "inflate init failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
383 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
384 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
385 status = inflate(&zs, Z_FINISH);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
386 if (status != Z_STREAM_END) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
387 inflateEnd(&zs);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
388 report_error(fp, "inflate failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
389 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
390 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
391 status = inflateEnd(&zs);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
392 if (status != Z_OK) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
393 report_error(fp, "inflate failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
394 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
395 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
396 return zs.total_out;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
397 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
398
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
399 static
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
400 int
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
401 check_header(const bgzf_byte_t* header)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
402 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
403 return (header[0] == GZIP_ID1 &&
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
404 header[1] == (bgzf_byte_t) GZIP_ID2 &&
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
405 header[2] == Z_DEFLATED &&
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
406 (header[3] & FLG_FEXTRA) != 0 &&
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
407 unpackInt16((uint8_t*)&header[10]) == BGZF_XLEN &&
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
408 header[12] == BGZF_ID1 &&
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
409 header[13] == BGZF_ID2 &&
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
410 unpackInt16((uint8_t*)&header[14]) == BGZF_LEN);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
411 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
412
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
413 static void free_cache(BGZF *fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
414 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
415 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
416 khash_t(cache) *h = (khash_t(cache)*)fp->cache;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
417 if (fp->open_mode != 'r') return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
418 for (k = kh_begin(h); k < kh_end(h); ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
419 if (kh_exist(h, k)) free(kh_val(h, k).block);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
420 kh_destroy(cache, h);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
421 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
422
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
423 static int load_block_from_cache(BGZF *fp, int64_t block_address)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
424 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
425 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
426 cache_t *p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
427 khash_t(cache) *h = (khash_t(cache)*)fp->cache;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
428 k = kh_get(cache, h, block_address);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
429 if (k == kh_end(h)) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
430 p = &kh_val(h, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
431 if (fp->block_length != 0) fp->block_offset = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
432 fp->block_address = block_address;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
433 fp->block_length = p->size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
434 memcpy(fp->uncompressed_block, p->block, MAX_BLOCK_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
435 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
436 knet_seek(fp->x.fpr, p->end_offset, SEEK_SET);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
437 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
438 fseeko(fp->file, p->end_offset, SEEK_SET);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
439 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
440 return p->size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
441 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
442
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
443 static void cache_block(BGZF *fp, int size)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
444 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
445 int ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
446 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
447 cache_t *p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
448 khash_t(cache) *h = (khash_t(cache)*)fp->cache;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
449 if (MAX_BLOCK_SIZE >= fp->cache_size) return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
450 if ((kh_size(h) + 1) * MAX_BLOCK_SIZE > fp->cache_size) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
451 /* A better way would be to remove the oldest block in the
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
452 * cache, but here we remove a random one for simplicity. This
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
453 * should not have a big impact on performance. */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
454 for (k = kh_begin(h); k < kh_end(h); ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
455 if (kh_exist(h, k)) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
456 if (k < kh_end(h)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
457 free(kh_val(h, k).block);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
458 kh_del(cache, h, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
459 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
460 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
461 k = kh_put(cache, h, fp->block_address, &ret);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
462 if (ret == 0) return; // if this happens, a bug!
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
463 p = &kh_val(h, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
464 p->size = fp->block_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
465 p->end_offset = fp->block_address + size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
466 p->block = malloc(MAX_BLOCK_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
467 memcpy(kh_val(h, k).block, fp->uncompressed_block, MAX_BLOCK_SIZE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
468 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
469
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
470 int
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
471 bgzf_read_block(BGZF* fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
472 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
473 bgzf_byte_t header[BLOCK_HEADER_LENGTH];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
474 int count, size = 0, block_length, remaining;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
475 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
476 int64_t block_address = knet_tell(fp->x.fpr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
477 if (load_block_from_cache(fp, block_address)) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
478 count = knet_read(fp->x.fpr, header, sizeof(header));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
479 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
480 int64_t block_address = ftello(fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
481 if (load_block_from_cache(fp, block_address)) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
482 count = fread(header, 1, sizeof(header), fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
483 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
484 if (count == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
485 fp->block_length = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
486 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
487 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
488 size = count;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
489 if (count != sizeof(header)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
490 report_error(fp, "read failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
491 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
492 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
493 if (!check_header(header)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
494 report_error(fp, "invalid block header");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
495 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
496 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
497 block_length = unpackInt16((uint8_t*)&header[16]) + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
498 bgzf_byte_t* compressed_block = (bgzf_byte_t*) fp->compressed_block;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
499 memcpy(compressed_block, header, BLOCK_HEADER_LENGTH);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
500 remaining = block_length - BLOCK_HEADER_LENGTH;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
501 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
502 count = knet_read(fp->x.fpr, &compressed_block[BLOCK_HEADER_LENGTH], remaining);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
503 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
504 count = fread(&compressed_block[BLOCK_HEADER_LENGTH], 1, remaining, fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
505 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
506 if (count != remaining) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
507 report_error(fp, "read failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
508 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
509 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
510 size += count;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
511 count = inflate_block(fp, block_length);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
512 if (count < 0) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
513 if (fp->block_length != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
514 // Do not reset offset if this read follows a seek.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
515 fp->block_offset = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
516 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
517 fp->block_address = block_address;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
518 fp->block_length = count;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
519 cache_block(fp, size);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
520 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
521 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
522
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
523 int
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
524 bgzf_read(BGZF* fp, void* data, int length)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
525 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
526 if (length <= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
527 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
528 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
529 if (fp->open_mode != 'r') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
530 report_error(fp, "file not open for reading");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
531 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
532 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
533
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
534 int bytes_read = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
535 bgzf_byte_t* output = data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
536 while (bytes_read < length) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
537 int copy_length, available = fp->block_length - fp->block_offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
538 bgzf_byte_t *buffer;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
539 if (available <= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
540 if (bgzf_read_block(fp) != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
541 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
542 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
543 available = fp->block_length - fp->block_offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
544 if (available <= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
545 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
546 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
547 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
548 copy_length = bgzf_min(length-bytes_read, available);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
549 buffer = fp->uncompressed_block;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
550 memcpy(output, buffer + fp->block_offset, copy_length);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
551 fp->block_offset += copy_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
552 output += copy_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
553 bytes_read += copy_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
554 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
555 if (fp->block_offset == fp->block_length) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
556 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
557 fp->block_address = knet_tell(fp->x.fpr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
558 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
559 fp->block_address = ftello(fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
560 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
561 fp->block_offset = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
562 fp->block_length = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
563 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
564 return bytes_read;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
565 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
566
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
567 int bgzf_flush(BGZF* fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
568 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
569 while (fp->block_offset > 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
570 int count, block_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
571 block_length = deflate_block(fp, fp->block_offset);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
572 if (block_length < 0) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
573 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
574 count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
575 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
576 count = fwrite(fp->compressed_block, 1, block_length, fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
577 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
578 if (count != block_length) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
579 report_error(fp, "write failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
580 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
581 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
582 fp->block_address += block_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
583 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
584 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
585 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
586
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
587 int bgzf_flush_try(BGZF *fp, int size)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
588 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
589 if (fp->block_offset + size > fp->uncompressed_block_size)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
590 return bgzf_flush(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
591 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
592 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
593
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
594 int bgzf_write(BGZF* fp, const void* data, int length)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
595 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
596 const bgzf_byte_t *input = data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
597 int block_length, bytes_written;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
598 if (fp->open_mode != 'w') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
599 report_error(fp, "file not open for writing");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
600 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
601 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
602
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
603 if (fp->uncompressed_block == NULL)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
604 fp->uncompressed_block = malloc(fp->uncompressed_block_size);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
605
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
606 input = data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
607 block_length = fp->uncompressed_block_size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
608 bytes_written = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
609 while (bytes_written < length) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
610 int copy_length = bgzf_min(block_length - fp->block_offset, length - bytes_written);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
611 bgzf_byte_t* buffer = fp->uncompressed_block;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
612 memcpy(buffer + fp->block_offset, input, copy_length);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
613 fp->block_offset += copy_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
614 input += copy_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
615 bytes_written += copy_length;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
616 if (fp->block_offset == block_length) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
617 if (bgzf_flush(fp) != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
618 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
619 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
620 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
621 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
622 return bytes_written;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
623 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
624
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
625 int bgzf_close(BGZF* fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
626 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
627 if (fp->open_mode == 'w') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
628 if (bgzf_flush(fp) != 0) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
629 { // add an empty block
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
630 int count, block_length = deflate_block(fp, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
631 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
632 count = fwrite(fp->compressed_block, 1, block_length, fp->x.fpw);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
633 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
634 count = fwrite(fp->compressed_block, 1, block_length, fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
635 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
636 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
637 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
638 if (fflush(fp->x.fpw) != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
639 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
640 if (fflush(fp->file) != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
641 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
642 report_error(fp, "flush failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
643 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
644 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
645 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
646 if (fp->owned_file) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
647 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
648 int ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
649 if (fp->open_mode == 'w') ret = fclose(fp->x.fpw);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
650 else ret = knet_close(fp->x.fpr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
651 if (ret != 0) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
652 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
653 if (fclose(fp->file) != 0) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
654 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
655 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
656 free(fp->uncompressed_block);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
657 free(fp->compressed_block);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
658 free_cache(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
659 free(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
660 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
661 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
662
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
663 void bgzf_set_cache_size(BGZF *fp, int cache_size)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
664 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
665 if (fp) fp->cache_size = cache_size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
666 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
667
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
668 int bgzf_check_EOF(BGZF *fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
669 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
670 static uint8_t magic[28] = "\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0\033\0\3\0\0\0\0\0\0\0\0\0";
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
671 uint8_t buf[28];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
672 off_t offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
673 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
674 offset = knet_tell(fp->x.fpr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
675 if (knet_seek(fp->x.fpr, -28, SEEK_END) != 0) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
676 knet_read(fp->x.fpr, buf, 28);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
677 knet_seek(fp->x.fpr, offset, SEEK_SET);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
678 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
679 offset = ftello(fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
680 if (fseeko(fp->file, -28, SEEK_END) != 0) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
681 fread(buf, 1, 28, fp->file);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
682 fseeko(fp->file, offset, SEEK_SET);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
683 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
684 return (memcmp(magic, buf, 28) == 0)? 1 : 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
685 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
686
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
687 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
688 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
689 int block_offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
690 int64_t block_address;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
691
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
692 if (fp->open_mode != 'r') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
693 report_error(fp, "file not open for read");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
694 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
695 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
696 if (where != SEEK_SET) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
697 report_error(fp, "unimplemented seek option");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
698 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
699 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
700 block_offset = pos & 0xFFFF;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
701 block_address = (pos >> 16) & 0xFFFFFFFFFFFFLL;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
702 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
703 if (knet_seek(fp->x.fpr, block_address, SEEK_SET) != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
704 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
705 if (fseeko(fp->file, block_address, SEEK_SET) != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
706 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
707 report_error(fp, "seek failed");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
708 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
709 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
710 fp->block_length = 0; // indicates current block is not loaded
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
711 fp->block_address = block_address;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
712 fp->block_offset = block_offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
713 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
714 }