bgzf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* The MIT License
  2. Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
  3. 2011 Attractive Chaos <attractor@live.co.uk>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <assert.h>
  25. #include <sys/types.h>
  26. #include "bgzf.h"
  27. #ifdef _USE_KNETFILE
  28. #include "knetfile.h"
  29. typedef knetFile *_bgzf_file_t;
  30. #define _bgzf_open(fn, mode) knet_open(fn, mode)
  31. #define _bgzf_dopen(fp, mode) knet_dopen(fp, mode)
  32. #define _bgzf_close(fp) knet_close(fp)
  33. #define _bgzf_fileno(fp) ((fp)->fd)
  34. #define _bgzf_tell(fp) knet_tell(fp)
  35. #define _bgzf_seek(fp, offset, whence) knet_seek(fp, offset, whence)
  36. #define _bgzf_read(fp, buf, len) knet_read(fp, buf, len)
  37. #define _bgzf_write(fp, buf, len) knet_write(fp, buf, len)
  38. #else // ~defined(_USE_KNETFILE)
  39. #if defined(_WIN32) || defined(_MSC_VER)
  40. #define ftello(fp) ftell(fp)
  41. #define fseeko(fp, offset, whence) fseek(fp, offset, whence)
  42. #else // ~defined(_WIN32)
  43. extern off_t ftello(FILE *stream);
  44. extern int fseeko(FILE *stream, off_t offset, int whence);
  45. #endif // ~defined(_WIN32)
  46. typedef FILE *_bgzf_file_t;
  47. #define _bgzf_open(fn, mode) fopen(fn, mode)
  48. #define _bgzf_dopen(fp, mode) fdopen(fp, mode)
  49. #define _bgzf_close(fp) fclose(fp)
  50. #define _bgzf_fileno(fp) fileno(fp)
  51. #define _bgzf_tell(fp) ftello(fp)
  52. #define _bgzf_seek(fp, offset, whence) fseeko(fp, offset, whence)
  53. #define _bgzf_read(fp, buf, len) fread(buf, 1, len, fp)
  54. #define _bgzf_write(fp, buf, len) fwrite(buf, 1, len, fp)
  55. #endif // ~define(_USE_KNETFILE)
  56. #define BLOCK_HEADER_LENGTH 18
  57. #define BLOCK_FOOTER_LENGTH 8
  58. /* BGZF/GZIP header (speciallized from RFC 1952; little endian):
  59. +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  60. | 31|139| 8| 4| 0| 0|255| 6| 66| 67| 2|BLK_LEN|
  61. +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  62. */
  63. static const uint8_t g_magic[19] = "\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0\0\0";
  64. #ifdef BGZF_CACHE
  65. typedef struct {
  66. int size;
  67. uint8_t *block;
  68. int64_t end_offset;
  69. } cache_t;
  70. #include "khash.h"
  71. KHASH_MAP_INIT_INT64(cache, cache_t)
  72. #endif
  73. static inline void packInt16(uint8_t *buffer, uint16_t value)
  74. {
  75. buffer[0] = value;
  76. buffer[1] = value >> 8;
  77. }
  78. static inline int unpackInt16(const uint8_t *buffer)
  79. {
  80. return buffer[0] | buffer[1] << 8;
  81. }
  82. static inline void packInt32(uint8_t *buffer, uint32_t value)
  83. {
  84. buffer[0] = value;
  85. buffer[1] = value >> 8;
  86. buffer[2] = value >> 16;
  87. buffer[3] = value >> 24;
  88. }
  89. static BGZF *bgzf_read_init()
  90. {
  91. BGZF *fp;
  92. fp = calloc(1, sizeof(BGZF));
  93. fp->open_mode = 'r';
  94. fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
  95. fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
  96. #ifdef BGZF_CACHE
  97. fp->cache = kh_init(cache);
  98. #endif
  99. return fp;
  100. }
  101. static BGZF *bgzf_write_init(int compress_level) // compress_level==-1 for the default level
  102. {
  103. BGZF *fp;
  104. fp = calloc(1, sizeof(BGZF));
  105. fp->open_mode = 'w';
  106. fp->uncompressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
  107. fp->compressed_block = malloc(BGZF_MAX_BLOCK_SIZE);
  108. fp->compress_level = compress_level < 0? Z_DEFAULT_COMPRESSION : compress_level; // Z_DEFAULT_COMPRESSION==-1
  109. if (fp->compress_level > 9) fp->compress_level = Z_DEFAULT_COMPRESSION;
  110. return fp;
  111. }
  112. // get the compress level from the mode string
  113. static int mode2level(const char *__restrict mode)
  114. {
  115. int i, compress_level = -1;
  116. for (i = 0; mode[i]; ++i)
  117. if (mode[i] >= '0' && mode[i] <= '9') break;
  118. if (mode[i]) compress_level = (int)mode[i] - '0';
  119. if (strchr(mode, 'u')) compress_level = 0;
  120. return compress_level;
  121. }
  122. BGZF *bgzf_open(const char *path, const char *mode)
  123. {
  124. BGZF *fp = 0;
  125. if (strchr(mode, 'r') || strchr(mode, 'R')) {
  126. _bgzf_file_t fpr;
  127. if ((fpr = _bgzf_open(path, "r")) == 0) return 0;
  128. fp = bgzf_read_init();
  129. fp->fp = fpr;
  130. } else if (strchr(mode, 'w') || strchr(mode, 'W')) {
  131. FILE *fpw;
  132. if ((fpw = fopen(path, "w")) == 0) return 0;
  133. fp = bgzf_write_init(mode2level(mode));
  134. fp->fp = fpw;
  135. }
  136. return fp;
  137. }
  138. BGZF *bgzf_dopen(int fd, const char *mode)
  139. {
  140. BGZF *fp = 0;
  141. if (strchr(mode, 'r') || strchr(mode, 'R')) {
  142. _bgzf_file_t fpr;
  143. if ((fpr = _bgzf_dopen(fd, "r")) == 0) return 0;
  144. fp = bgzf_read_init();
  145. fp->fp = fpr;
  146. } else if (strchr(mode, 'w') || strchr(mode, 'W')) {
  147. FILE *fpw;
  148. if ((fpw = fdopen(fd, "w")) == 0) return 0;
  149. fp = bgzf_write_init(mode2level(mode));
  150. fp->fp = fpw;
  151. }
  152. return fp;
  153. }
  154. // Deflate the block in fp->uncompressed_block into fp->compressed_block. Also adds an extra field that stores the compressed block length.
  155. static int deflate_block(BGZF *fp, int block_length)
  156. {
  157. uint8_t *buffer = fp->compressed_block;
  158. int buffer_size = BGZF_BLOCK_SIZE;
  159. int input_length = block_length;
  160. int compressed_length = 0;
  161. int remaining;
  162. uint32_t crc;
  163. assert(block_length <= BGZF_BLOCK_SIZE); // guaranteed by the caller
  164. memcpy(buffer, g_magic, BLOCK_HEADER_LENGTH); // the last two bytes are a place holder for the length of the block
  165. while (1) { // loop to retry for blocks that do not compress enough
  166. int status;
  167. z_stream zs;
  168. zs.zalloc = NULL;
  169. zs.zfree = NULL;
  170. zs.next_in = fp->uncompressed_block;
  171. zs.avail_in = input_length;
  172. zs.next_out = (void*)&buffer[BLOCK_HEADER_LENGTH];
  173. zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;
  174. status = deflateInit2(&zs, fp->compress_level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); // -15 to disable zlib header/footer
  175. if (status != Z_OK) {
  176. fp->errcode |= BGZF_ERR_ZLIB;
  177. return -1;
  178. }
  179. status = deflate(&zs, Z_FINISH);
  180. if (status != Z_STREAM_END) { // not compressed enough
  181. deflateEnd(&zs); // reset the stream
  182. if (status == Z_OK) { // reduce the size and recompress
  183. input_length -= 1024;
  184. assert(input_length > 0); // logically, this should not happen
  185. continue;
  186. }
  187. fp->errcode |= BGZF_ERR_ZLIB;
  188. return -1;
  189. }
  190. if (deflateEnd(&zs) != Z_OK) {
  191. fp->errcode |= BGZF_ERR_ZLIB;
  192. return -1;
  193. }
  194. compressed_length = zs.total_out;
  195. compressed_length += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;
  196. assert(compressed_length <= BGZF_BLOCK_SIZE);
  197. break;
  198. }
  199. assert(compressed_length > 0);
  200. packInt16((uint8_t*)&buffer[16], compressed_length - 1); // write the compressed_length; -1 to fit 2 bytes
  201. crc = crc32(0L, NULL, 0L);
  202. crc = crc32(crc, fp->uncompressed_block, input_length);
  203. packInt32((uint8_t*)&buffer[compressed_length-8], crc);
  204. packInt32((uint8_t*)&buffer[compressed_length-4], input_length);
  205. remaining = block_length - input_length;
  206. if (remaining > 0) {
  207. assert(remaining <= input_length);
  208. memcpy(fp->uncompressed_block, fp->uncompressed_block + input_length, remaining);
  209. }
  210. fp->block_offset = remaining;
  211. return compressed_length;
  212. }
  213. // Inflate the block in fp->compressed_block into fp->uncompressed_block
  214. static int inflate_block(BGZF* fp, int block_length)
  215. {
  216. z_stream zs;
  217. zs.zalloc = NULL;
  218. zs.zfree = NULL;
  219. zs.next_in = fp->compressed_block + 18;
  220. zs.avail_in = block_length - 16;
  221. zs.next_out = fp->uncompressed_block;
  222. zs.avail_out = BGZF_BLOCK_SIZE;
  223. if (inflateInit2(&zs, -15) != Z_OK) {
  224. fp->errcode |= BGZF_ERR_ZLIB;
  225. return -1;
  226. }
  227. if (inflate(&zs, Z_FINISH) != Z_STREAM_END) {
  228. inflateEnd(&zs);
  229. fp->errcode |= BGZF_ERR_ZLIB;
  230. return -1;
  231. }
  232. if (inflateEnd(&zs) != Z_OK) {
  233. fp->errcode |= BGZF_ERR_ZLIB;
  234. return -1;
  235. }
  236. return zs.total_out;
  237. }
  238. static int check_header(const uint8_t *header)
  239. {
  240. return (header[0] == 31 && header[1] == 139 && header[2] == 8 && (header[3] & 4) != 0
  241. && unpackInt16((uint8_t*)&header[10]) == 6
  242. && header[12] == 'B' && header[13] == 'C'
  243. && unpackInt16((uint8_t*)&header[14]) == 2);
  244. }
  245. #ifdef BGZF_CACHE
  246. static void free_cache(BGZF *fp)
  247. {
  248. khint_t k;
  249. khash_t(cache) *h = (khash_t(cache)*)fp->cache;
  250. if (fp->open_mode != 'r') return;
  251. for (k = kh_begin(h); k < kh_end(h); ++k)
  252. if (kh_exist(h, k)) free(kh_val(h, k).block);
  253. kh_destroy(cache, h);
  254. }
  255. static int load_block_from_cache(BGZF *fp, int64_t block_address)
  256. {
  257. khint_t k;
  258. cache_t *p;
  259. khash_t(cache) *h = (khash_t(cache)*)fp->cache;
  260. k = kh_get(cache, h, block_address);
  261. if (k == kh_end(h)) return 0;
  262. p = &kh_val(h, k);
  263. if (fp->block_length != 0) fp->block_offset = 0;
  264. fp->block_address = block_address;
  265. fp->block_length = p->size;
  266. memcpy(fp->uncompressed_block, p->block, BGZF_BLOCK_SIZE);
  267. _bgzf_seek((_bgzf_file_t)fp->fp, p->end_offset, SEEK_SET);
  268. return p->size;
  269. }
  270. static void cache_block(BGZF *fp, int size)
  271. {
  272. int ret;
  273. khint_t k;
  274. cache_t *p;
  275. khash_t(cache) *h = (khash_t(cache)*)fp->cache;
  276. if (BGZF_BLOCK_SIZE >= fp->cache_size) return;
  277. if ((kh_size(h) + 1) * BGZF_BLOCK_SIZE > fp->cache_size) {
  278. /* A better way would be to remove the oldest block in the
  279. * cache, but here we remove a random one for simplicity. This
  280. * should not have a big impact on performance. */
  281. for (k = kh_begin(h); k < kh_end(h); ++k)
  282. if (kh_exist(h, k)) break;
  283. if (k < kh_end(h)) {
  284. free(kh_val(h, k).block);
  285. kh_del(cache, h, k);
  286. }
  287. }
  288. k = kh_put(cache, h, fp->block_address, &ret);
  289. if (ret == 0) return; // if this happens, a bug!
  290. p = &kh_val(h, k);
  291. p->size = fp->block_length;
  292. p->end_offset = fp->block_address + size;
  293. p->block = malloc(BGZF_BLOCK_SIZE);
  294. memcpy(kh_val(h, k).block, fp->uncompressed_block, BGZF_BLOCK_SIZE);
  295. }
  296. #else
  297. static void free_cache(BGZF *fp) {}
  298. static int load_block_from_cache(BGZF *fp, int64_t block_address) {return 0;}
  299. static void cache_block(BGZF *fp, int size) {}
  300. #endif
  301. int bgzf_read_block(BGZF *fp)
  302. {
  303. uint8_t header[BLOCK_HEADER_LENGTH], *compressed_block;
  304. int count, size = 0, block_length, remaining;
  305. int64_t block_address;
  306. block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
  307. if (load_block_from_cache(fp, block_address)) return 0;
  308. count = _bgzf_read(fp->fp, header, sizeof(header));
  309. if (count == 0) { // no data read
  310. fp->block_length = 0;
  311. return 0;
  312. }
  313. if (count != sizeof(header) || !check_header(header)) {
  314. fp->errcode |= BGZF_ERR_HEADER;
  315. return -1;
  316. }
  317. size = count;
  318. block_length = unpackInt16((uint8_t*)&header[16]) + 1; // +1 because when writing this number, we used "-1"
  319. compressed_block = (uint8_t*)fp->compressed_block;
  320. memcpy(compressed_block, header, BLOCK_HEADER_LENGTH);
  321. remaining = block_length - BLOCK_HEADER_LENGTH;
  322. count = _bgzf_read(fp->fp, &compressed_block[BLOCK_HEADER_LENGTH], remaining);
  323. if (count != remaining) {
  324. fp->errcode |= BGZF_ERR_IO;
  325. return -1;
  326. }
  327. size += count;
  328. if ((count = inflate_block(fp, block_length)) < 0) return -1;
  329. if (fp->block_length != 0) fp->block_offset = 0; // Do not reset offset if this read follows a seek.
  330. fp->block_address = block_address;
  331. fp->block_length = count;
  332. cache_block(fp, size);
  333. return 0;
  334. }
  335. ssize_t bgzf_read(BGZF *fp, void *data, ssize_t length)
  336. {
  337. ssize_t bytes_read = 0;
  338. uint8_t *output = data;
  339. if (length <= 0) return 0;
  340. assert(fp->open_mode == 'r');
  341. while (bytes_read < length) {
  342. int copy_length, available = fp->block_length - fp->block_offset;
  343. uint8_t *buffer;
  344. if (available <= 0) {
  345. if (bgzf_read_block(fp) != 0) return -1;
  346. available = fp->block_length - fp->block_offset;
  347. if (available <= 0) break;
  348. }
  349. copy_length = length - bytes_read < available? length - bytes_read : available;
  350. buffer = fp->uncompressed_block;
  351. memcpy(output, buffer + fp->block_offset, copy_length);
  352. fp->block_offset += copy_length;
  353. output += copy_length;
  354. bytes_read += copy_length;
  355. }
  356. if (fp->block_offset == fp->block_length) {
  357. fp->block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
  358. fp->block_offset = fp->block_length = 0;
  359. }
  360. return bytes_read;
  361. }
  362. int bgzf_flush(BGZF *fp)
  363. {
  364. assert(fp->open_mode == 'w');
  365. while (fp->block_offset > 0) {
  366. int block_length;
  367. block_length = deflate_block(fp, fp->block_offset);
  368. if (block_length < 0) return -1;
  369. if (fwrite(fp->compressed_block, 1, block_length, fp->fp) != block_length) {
  370. fp->errcode |= BGZF_ERR_IO; // possibly truncated file
  371. return -1;
  372. }
  373. fp->block_address += block_length;
  374. }
  375. return 0;
  376. }
  377. int bgzf_flush_try(BGZF *fp, ssize_t size)
  378. {
  379. if (fp->block_offset + size > BGZF_BLOCK_SIZE)
  380. return bgzf_flush(fp);
  381. return -1;
  382. }
  383. ssize_t bgzf_write(BGZF *fp, const void *data, ssize_t length)
  384. {
  385. const uint8_t *input = data;
  386. int block_length = BGZF_BLOCK_SIZE, bytes_written;
  387. assert(fp->open_mode == 'w');
  388. input = data;
  389. bytes_written = 0;
  390. while (bytes_written < length) {
  391. uint8_t* buffer = fp->uncompressed_block;
  392. int copy_length = block_length - fp->block_offset < length - bytes_written? block_length - fp->block_offset : length - bytes_written;
  393. memcpy(buffer + fp->block_offset, input, copy_length);
  394. fp->block_offset += copy_length;
  395. input += copy_length;
  396. bytes_written += copy_length;
  397. if (fp->block_offset == block_length && bgzf_flush(fp)) break;
  398. }
  399. return bytes_written;
  400. }
  401. int bgzf_close(BGZF* fp)
  402. {
  403. int ret, count, block_length;
  404. if (fp == 0) return -1;
  405. if (fp->open_mode == 'w') {
  406. if (bgzf_flush(fp) != 0) return -1;
  407. block_length = deflate_block(fp, 0); // write an empty block
  408. count = fwrite(fp->compressed_block, 1, block_length, fp->fp);
  409. if (fflush(fp->fp) != 0) {
  410. fp->errcode |= BGZF_ERR_IO;
  411. return -1;
  412. }
  413. }
  414. ret = fp->open_mode == 'w'? fclose(fp->fp) : _bgzf_close(fp->fp);
  415. if (ret != 0) return -1;
  416. free(fp->uncompressed_block);
  417. free(fp->compressed_block);
  418. free_cache(fp);
  419. free(fp);
  420. return 0;
  421. }
  422. void bgzf_set_cache_size(BGZF *fp, int cache_size)
  423. {
  424. if (fp) fp->cache_size = cache_size;
  425. }
  426. int bgzf_check_EOF(BGZF *fp)
  427. {
  428. 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";
  429. uint8_t buf[28];
  430. off_t offset;
  431. offset = _bgzf_tell((_bgzf_file_t)fp->fp);
  432. if (_bgzf_seek(fp->fp, -28, SEEK_END) < 0) return 0;
  433. _bgzf_read(fp->fp, buf, 28);
  434. _bgzf_seek(fp->fp, offset, SEEK_SET);
  435. return (memcmp(magic, buf, 28) == 0)? 1 : 0;
  436. }
  437. int64_t bgzf_seek(BGZF* fp, int64_t pos, int where)
  438. {
  439. int block_offset;
  440. int64_t block_address;
  441. if (fp->open_mode != 'r' || where != SEEK_SET) {
  442. fp->errcode |= BGZF_ERR_MISUSE;
  443. return -1;
  444. }
  445. block_offset = pos & 0xFFFF;
  446. block_address = pos >> 16;
  447. if (_bgzf_seek(fp->fp, block_address, SEEK_SET) < 0) {
  448. fp->errcode |= BGZF_ERR_IO;
  449. return -1;
  450. }
  451. fp->block_length = 0; // indicates current block has not been loaded
  452. fp->block_address = block_address;
  453. fp->block_offset = block_offset;
  454. return 0;
  455. }
  456. int bgzf_is_bgzf(const char *fn)
  457. {
  458. uint8_t buf[16];
  459. int n;
  460. _bgzf_file_t fp;
  461. if ((fp = _bgzf_open(fn, "r")) == 0) return 0;
  462. n = _bgzf_read(fp, buf, 16);
  463. _bgzf_close(fp);
  464. if (n != 16) return 0;
  465. return memcmp(g_magic, buf, 16) == 0? 1 : 0;
  466. }
  467. int bgzf_getc(BGZF *fp)
  468. {
  469. int c;
  470. if (fp->block_offset >= fp->block_length) {
  471. if (bgzf_read_block(fp) != 0) return -2; /* error */
  472. if (fp->block_length == 0) return -1; /* end-of-file */
  473. }
  474. c = ((unsigned char*)fp->uncompressed_block)[fp->block_offset++];
  475. if (fp->block_offset == fp->block_length) {
  476. fp->block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
  477. fp->block_offset = 0;
  478. fp->block_length = 0;
  479. }
  480. return c;
  481. }
  482. #ifndef kroundup32
  483. #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
  484. #endif
  485. int bgzf_getline(BGZF *fp, int delim, kstring_t *str)
  486. {
  487. int l, state = 0;
  488. unsigned char *buf = (unsigned char*)fp->uncompressed_block;
  489. str->l = 0;
  490. do {
  491. if (fp->block_offset >= fp->block_length) {
  492. if (bgzf_read_block(fp) != 0) { state = -2; break; }
  493. if (fp->block_length == 0) { state = -1; break; }
  494. }
  495. for (l = fp->block_offset; l < fp->block_length && buf[l] != delim; ++l);
  496. if (l < fp->block_length) state = 1;
  497. l -= fp->block_offset;
  498. if (str->l + l + 1 >= str->m) {
  499. str->m = str->l + l + 2;
  500. kroundup32(str->m);
  501. str->s = (char*)realloc(str->s, str->m);
  502. }
  503. memcpy(str->s + str->l, buf + fp->block_offset, l);
  504. str->l += l;
  505. fp->block_offset += l + 1;
  506. if (fp->block_offset >= fp->block_length) {
  507. fp->block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
  508. fp->block_offset = 0;
  509. fp->block_length = 0;
  510. }
  511. } while (state == 0);
  512. if (str->l == 0 && state < 0) return state;
  513. str->s[str->l] = 0;
  514. return str->l;
  515. }