Uploaded package after misc. improvements.
[samtools.git] / razf.h
1  /*-
2  * RAZF : Random Access compressed(Z) File
3  * Version: 1.0
4  * Release Date: 2008-10-27
5  *
6  * Copyright 2008, Jue Ruan <ruanjue@gmail.com>, Heng Li <lh3@sanger.ac.uk>
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32
33 #ifndef __RAZF_RJ_H
34 #define __RAZF_RJ_H
35
36 #include <stdint.h>
37 #include <stdio.h>
38 #include "zlib.h"
39
40 #ifdef _USE_KNETFILE
41 #include "knetfile.h"
42 #endif
43
44 #if ZLIB_VERNUM < 0x1221
45 #define _RZ_READONLY
46 struct _gz_header_s;
47 typedef struct _gz_header_s _gz_header;
48 #define gz_header _gz_header
49 #endif
50
51 #define WINDOW_BITS   15
52
53 #ifndef RZ_BLOCK_SIZE
54 #define RZ_BLOCK_SIZE (1<<WINDOW_BITS)
55 #endif
56
57 #ifndef RZ_BUFFER_SIZE
58 #define RZ_BUFFER_SIZE 4096
59 #endif
60
61 #ifndef RZ_COMPRESS_LEVEL
62 #define RZ_COMPRESS_LEVEL 6
63 #endif
64
65 #define RZ_BIN_SIZE ((1LLU << 32) / RZ_BLOCK_SIZE)
66
67 typedef struct {
68         uint32_t *cell_offsets; // i
69         int64_t  *bin_offsets; // i / BIN_SIZE
70         int size;
71         int cap;
72 } ZBlockIndex;
73 /* When storing index, output bytes in Big-Endian everywhere */
74
75 #define FILE_TYPE_RZ    1
76 #define FILE_TYPE_PLAIN 2
77 #define FILE_TYPE_GZ    3
78
79 typedef struct RandomAccessZFile  {
80         char mode; /* 'w' : write mode; 'r' : read mode */
81         int file_type;
82         /* plain file or rz file, razf_read support plain file as input too, in this case, razf_read work as buffered fread */
83 #ifdef _USE_KNETFILE
84     union {
85         knetFile *fpr;
86         int fpw;
87     } x;
88 #else
89         int filedes; /* the file descriptor */
90 #endif
91         z_stream *stream;
92         ZBlockIndex *index;
93         int64_t in, out, end, src_end;
94         /* in: n bytes total in; out: n bytes total out; */
95         /* end: the end of all data blocks, while the start of index; src_end: the true end position in uncompressed file */
96         int buf_flush; // buffer should be flush, suspend inflate util buffer is empty
97         int64_t block_pos, block_off, next_block_pos;
98         /* block_pos: the start postiion of current block  in compressed file */
99         /* block_off: tell how many bytes have been read from current block */
100         void *inbuf, *outbuf;
101         int header_size;
102         gz_header *header;
103         /* header is used to transfer inflate_state->mode from HEAD to TYPE after call inflateReset */
104         int buf_off, buf_len;
105         int z_err, z_eof;
106         int seekable;
107         /* Indice where the source is seekable */
108         int load_index;
109         /* set has_index to 0 in mode 'w', then index will be discarded */
110 } RAZF;
111
112 #ifdef __cplusplus
113 extern "C" {
114 #endif
115
116         RAZF* razf_dopen(int data_fd, const char *mode);
117         RAZF *razf_open(const char *fn, const char *mode);
118         int razf_write(RAZF* rz, const void *data, int size);
119         int razf_read(RAZF* rz, void *data, int size);
120         int64_t razf_seek(RAZF* rz, int64_t pos, int where);
121         void razf_close(RAZF* rz);
122
123 #define razf_tell(rz) ((rz)->out)
124
125         RAZF* razf_open2(const char *filename, const char *mode);
126         RAZF* razf_dopen2(int fd, const char *mode);
127         uint64_t razf_tell2(RAZF *rz);
128         int64_t razf_seek2(RAZF *rz, uint64_t voffset, int where);
129
130 #ifdef __cplusplus
131 }
132 #endif
133
134 #endif