2 * RAZF : Random Access compressed(Z) File
4 * Release Date: 2008-10-27
6 * Copyright 2008, Jue Ruan <ruanjue@gmail.com>, Heng Li <lh3@sanger.ac.uk>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #define WINDOW_BITS 15
44 #define RZ_BLOCK_SIZE (1<<WINDOW_BITS)
47 #ifndef RZ_BUFFER_SIZE
48 #define RZ_BUFFER_SIZE 4096
51 #ifndef RZ_COMPRESS_LEVEL
52 #define RZ_COMPRESS_LEVEL 6
55 #define RZ_BIN_SIZE ((1LLU << 32) / RZ_BLOCK_SIZE)
58 uint32_t *cell_offsets; // i
59 int64_t *bin_offsets; // i / BIN_SIZE
63 /* When storing index, output bytes in Big-Endian everywhere */
65 #define FILE_TYPE_RZ 1
66 #define FILE_TYPE_PLAIN 2
67 #define FILE_TYPE_GZ 3
69 typedef struct RandomAccessZFile {
70 char mode; /* 'w' : write mode; 'r' : read mode */
72 /* plain file or rz file, razf_read support plain file as input too, in this case, razf_read work as buffered fread */
73 int filedes; /* the file descriptor */
76 int64_t in, out, end, src_end;
77 /* in: n bytes total in; out: n bytes total out; */
78 /* end: the end of all data blocks, while the start of index; src_end: the true end position in uncompressed file */
79 int buf_flush; // buffer should be flush, suspend inflate util buffer is empty
80 int64_t block_pos, block_off, next_block_pos;
81 /* block_pos: the start postiion of current block in compressed file */
82 /* block_off: tell how many bytes have been read from current block */
86 /* header is used to transfer inflate_state->mode from HEAD to TYPE after call inflateReset */
90 /* Indice where the source is seekable */
92 /* set has_index to 0 in mode 'w', then index will be discarded */
99 RAZF* razf_dopen(int data_fd, const char *mode);
100 RAZF *razf_open(const char *fn, const char *mode);
101 int razf_write(RAZF* rz, const void *data, int size);
102 int razf_read(RAZF* rz, void *data, int size);
103 int64_t razf_seek(RAZF* rz, int64_t pos, int where);
104 void razf_close(RAZF* rz);
106 #define razf_tell(rz) ((rz)->out)
108 RAZF* razf_open2(const char *filename, const char *mode);
109 RAZF* razf_dopen2(int fd, const char *mode);
110 uint64_t razf_tell2(RAZF *rz);
111 int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where);