1 /**********************************************************************
2   Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #ifndef _IGZIP_H
31 #define _IGZIP_H
32 
33 /**
34  * @file igzip_lib.h
35  *
36  * @brief This file defines the igzip compression and decompression interface, a
37  * high performance deflate compression interface for storage applications.
38  *
39  * Deflate is a widely used compression standard that can be used standalone, it
40  * also forms the basis of gzip and zlib compression formats. Igzip supports the
41  * following flush features:
42  *
43  * - No Flush: The default method where no special flush is performed.
44  *
45  * - Sync flush: whereby isal_deflate() finishes the current deflate block at
46  *   the end of each input buffer. The deflate block is byte aligned by
47  *   appending an empty stored block.
48  *
49  * - Full flush: whereby isal_deflate() finishes and aligns the deflate block as
50  *   in sync flush but also ensures that subsequent block's history does not
51  *   look back beyond this point and new blocks are fully independent.
52  *
53  * Igzip also supports compression levels from ISAL_DEF_MIN_LEVEL to
54  * ISAL_DEF_MAX_LEVEL.
55  *
56  * Igzip contains some behavior configurable at compile time. These
57  * configurable options are:
58  *
59  * - IGZIP_HIST_SIZE - Defines the window size. The default value is 32K (note K
60  *   represents 1024), but 8K is also supported. Powers of 2 which are at most
61  *   32K may also work.
62  *
63  * - LONGER_HUFFTABLES - Defines whether to use a larger hufftables structure
64  *   which may increase performance with smaller IGZIP_HIST_SIZE values. By
65  *   default this option is not defined. This define sets IGZIP_HIST_SIZE to be
66  *   8 if IGZIP_HIST_SIZE > 8K.
67  *
68  *   As an example, to compile gzip with an 8K window size, in a terminal run
69  *   @verbatim gmake D="-D IGZIP_HIST_SIZE=8*1024" @endverbatim on Linux and
70  *   FreeBSD, or with @verbatim nmake -f Makefile.nmake D="-D
71  *   IGZIP_HIST_SIZE=8*1024" @endverbatim on Windows.
72  *
73  */
74 #include <stdint.h>
75 
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 
80 /******************************************************************************/
81 /* Deflate Compression Standard Defines */
82 /******************************************************************************/
83 #define IGZIP_K  1024
84 #define ISAL_DEF_MAX_HDR_SIZE 328
85 #define ISAL_DEF_MAX_CODE_LEN 15
86 #define ISAL_DEF_HIST_SIZE (32*IGZIP_K)
87 #define ISAL_DEF_MAX_HIST_BITS 15
88 #define ISAL_DEF_MAX_MATCH 258
89 #define ISAL_DEF_MIN_MATCH 3
90 
91 #define ISAL_DEF_LIT_SYMBOLS 257
92 #define ISAL_DEF_LEN_SYMBOLS 29
93 #define ISAL_DEF_DIST_SYMBOLS 30
94 #define ISAL_DEF_LIT_LEN_SYMBOLS (ISAL_DEF_LIT_SYMBOLS + ISAL_DEF_LEN_SYMBOLS)
95 
96 /* Max repeat length, rounded up to 32 byte boundary */
97 #define ISAL_LOOK_AHEAD ((ISAL_DEF_MAX_MATCH + 31) & ~31)
98 
99 /******************************************************************************/
100 /* Deflate Implementation Specific Defines */
101 /******************************************************************************/
102 /* Note IGZIP_HIST_SIZE must be a power of two */
103 #ifndef IGZIP_HIST_SIZE
104 #define IGZIP_HIST_SIZE ISAL_DEF_HIST_SIZE
105 #endif
106 
107 #if (IGZIP_HIST_SIZE > ISAL_DEF_HIST_SIZE)
108 #undef IGZIP_HIST_SIZE
109 #define IGZIP_HIST_SIZE ISAL_DEF_HIST_SIZE
110 #endif
111 
112 #ifdef LONGER_HUFFTABLE
113 #if (IGZIP_HIST_SIZE > 8 * IGZIP_K)
114 #undef IGZIP_HIST_SIZE
115 #define IGZIP_HIST_SIZE (8 * IGZIP_K)
116 #endif
117 #endif
118 
119 #define ISAL_LIMIT_HASH_UPDATE
120 
121 #define IGZIP_HASH8K_HASH_SIZE (8 * IGZIP_K)
122 #define IGZIP_HASH_HIST_SIZE IGZIP_HIST_SIZE
123 #define IGZIP_HASH_MAP_HASH_SIZE IGZIP_HIST_SIZE
124 
125 #define IGZIP_LVL0_HASH_SIZE  (8 * IGZIP_K)
126 #define IGZIP_LVL1_HASH_SIZE  IGZIP_HASH8K_HASH_SIZE
127 #define IGZIP_LVL2_HASH_SIZE  IGZIP_HASH_HIST_SIZE
128 #define IGZIP_LVL3_HASH_SIZE  IGZIP_HASH_MAP_HASH_SIZE
129 
130 #ifdef LONGER_HUFFTABLE
131 enum {IGZIP_DIST_TABLE_SIZE = 8*1024};
132 
133 /* DECODE_OFFSET is dist code index corresponding to DIST_TABLE_SIZE + 1 */
134 enum { IGZIP_DECODE_OFFSET = 26 };
135 #else
136 enum {IGZIP_DIST_TABLE_SIZE = 2};
137 /* DECODE_OFFSET is dist code index corresponding to DIST_TABLE_SIZE + 1 */
138 enum { IGZIP_DECODE_OFFSET = 0 };
139 #endif
140 enum {IGZIP_LEN_TABLE_SIZE = 256};
141 enum {IGZIP_LIT_TABLE_SIZE = ISAL_DEF_LIT_SYMBOLS};
142 
143 #define IGZIP_HUFFTABLE_CUSTOM 0
144 #define IGZIP_HUFFTABLE_DEFAULT 1
145 #define IGZIP_HUFFTABLE_STATIC 2
146 
147 /* Flush Flags */
148 #define NO_FLUSH	0	/* Default */
149 #define SYNC_FLUSH	1
150 #define FULL_FLUSH	2
151 #define FINISH_FLUSH	0	/* Deprecated */
152 
153 /* Gzip Flags */
154 #define IGZIP_DEFLATE	0	/* Default */
155 #define IGZIP_GZIP	1
156 #define IGZIP_GZIP_NO_HDR	2
157 #define IGZIP_ZLIB	3
158 #define IGZIP_ZLIB_NO_HDR	4
159 
160 /* Compression Return values */
161 #define COMP_OK 0
162 #define INVALID_FLUSH -7
163 #define INVALID_PARAM -8
164 #define STATELESS_OVERFLOW -1
165 #define ISAL_INVALID_OPERATION -9
166 #define ISAL_INVALID_STATE -3
167 #define ISAL_INVALID_LEVEL -4	/* Invalid Compression level set */
168 #define ISAL_INVALID_LEVEL_BUF -5 /* Invalid buffer specified for the compression level */
169 
170 /**
171  *  @enum isal_zstate_state
172  *  @brief Compression State please note ZSTATE_TRL only applies for GZIP compression
173  */
174 
175 
176 /* When the state is set to ZSTATE_NEW_HDR or TMP_ZSTATE_NEW_HEADER, the
177  * hufftable being used for compression may be swapped
178  */
179 enum isal_zstate_state {
180 	ZSTATE_NEW_HDR,  //!< Header to be written
181 	ZSTATE_HDR,	//!< Header state
182 	ZSTATE_CREATE_HDR, //!< Header to be created
183 	ZSTATE_BODY,	//!< Body state
184 	ZSTATE_FLUSH_READ_BUFFER, //!< Flush buffer
185 	ZSTATE_FLUSH_ICF_BUFFER,
186 	ZSTATE_TYPE0_HDR, //! Type0 block header to be written
187 	ZSTATE_TYPE0_BODY, //!< Type0 block body to be written
188 	ZSTATE_SYNC_FLUSH, //!< Write sync flush block
189 	ZSTATE_FLUSH_WRITE_BUFFER, //!< Flush bitbuf
190 	ZSTATE_TRL,	//!< Trailer state
191 	ZSTATE_END,	//!< End state
192 	ZSTATE_TMP_NEW_HDR, //!< Temporary Header to be written
193 	ZSTATE_TMP_HDR,	//!< Temporary Header state
194 	ZSTATE_TMP_CREATE_HDR, //!< Temporary Header to be created state
195 	ZSTATE_TMP_BODY,	//!< Temporary Body state
196 	ZSTATE_TMP_FLUSH_READ_BUFFER, //!< Flush buffer
197 	ZSTATE_TMP_FLUSH_ICF_BUFFER,
198 	ZSTATE_TMP_TYPE0_HDR, //! Temporary Type0 block header to be written
199 	ZSTATE_TMP_TYPE0_BODY, //!< Temporary Type0 block body to be written
200 	ZSTATE_TMP_SYNC_FLUSH, //!< Write sync flush block
201 	ZSTATE_TMP_FLUSH_WRITE_BUFFER, //!< Flush bitbuf
202 	ZSTATE_TMP_TRL,	//!< Temporary Trailer state
203 	ZSTATE_TMP_END	//!< Temporary End state
204 };
205 
206 /* Offset used to switch between TMP states and non-tmp states */
207 #define ZSTATE_TMP_OFFSET ZSTATE_TMP_HDR - ZSTATE_HDR
208 
209 /******************************************************************************/
210 /* Inflate Implementation Specific Defines */
211 /******************************************************************************/
212 #define ISAL_DECODE_LONG_BITS 12
213 #define ISAL_DECODE_SHORT_BITS 10
214 
215 /* Current state of decompression */
216 enum isal_block_state {
217 	ISAL_BLOCK_NEW_HDR,	/* Just starting a new block */
218 	ISAL_BLOCK_HDR,		/* In the middle of reading in a block header */
219 	ISAL_BLOCK_TYPE0,	/* Decoding a type 0 block */
220 	ISAL_BLOCK_CODED,	/* Decoding a huffman coded block */
221 	ISAL_BLOCK_INPUT_DONE,	/* Decompression of input is completed */
222 	ISAL_BLOCK_FINISH,	/* Decompression of input is completed and all data has been flushed to output */
223 	ISAL_GZIP_EXTRA_LEN,
224 	ISAL_GZIP_EXTRA,
225 	ISAL_GZIP_NAME,
226 	ISAL_GZIP_COMMENT,
227 	ISAL_GZIP_HCRC,
228 	ISAL_ZLIB_DICT,
229 	ISAL_CHECKSUM_CHECK,
230 };
231 
232 
233 /* Inflate Flags */
234 #define ISAL_DEFLATE	0	/* Default */
235 #define ISAL_GZIP	1
236 #define ISAL_GZIP_NO_HDR	2
237 #define ISAL_ZLIB	3
238 #define ISAL_ZLIB_NO_HDR	4
239 #define ISAL_ZLIB_NO_HDR_VER	5
240 #define ISAL_GZIP_NO_HDR_VER	6
241 
242 /* Inflate Return values */
243 #define ISAL_DECOMP_OK 0	/* No errors encountered while decompressing */
244 #define ISAL_END_INPUT 1	/* End of input reached */
245 #define ISAL_OUT_OVERFLOW 2	/* End of output reached */
246 #define ISAL_NAME_OVERFLOW 3	/* End of gzip name buffer reached */
247 #define ISAL_COMMENT_OVERFLOW 4	/* End of gzip name buffer reached */
248 #define ISAL_EXTRA_OVERFLOW 5	/* End of extra buffer reached */
249 #define ISAL_NEED_DICT 6 /* Stream needs a dictionary to continue */
250 #define ISAL_INVALID_BLOCK -1	/* Invalid deflate block found */
251 #define ISAL_INVALID_SYMBOL -2	/* Invalid deflate symbol found */
252 #define ISAL_INVALID_LOOKBACK -3	/* Invalid lookback distance found */
253 #define ISAL_INVALID_WRAPPER -4 /* Invalid gzip/zlib wrapper found */
254 #define ISAL_UNSUPPORTED_METHOD -5	/* Gzip/zlib wrapper specifies unsupported compress method */
255 #define ISAL_INCORRECT_CHECKSUM -6 /* Incorrect checksum found */
256 
257 /******************************************************************************/
258 /* Compression structures */
259 /******************************************************************************/
260 /** @brief Holds histogram of deflate symbols*/
261 struct isal_huff_histogram {
262 	uint64_t lit_len_histogram[ISAL_DEF_LIT_LEN_SYMBOLS]; //!< Histogram of Literal/Len symbols seen
263 	uint64_t dist_histogram[ISAL_DEF_DIST_SYMBOLS]; //!< Histogram of Distance Symbols seen
264 	uint16_t hash_table[IGZIP_LVL0_HASH_SIZE]; //!< Tmp space used as a hash table
265 };
266 
267 struct isal_mod_hist {
268     uint32_t d_hist[30];
269     uint32_t ll_hist[513];
270 };
271 
272 #define ISAL_DEF_MIN_LEVEL 0
273 #define ISAL_DEF_MAX_LEVEL 3
274 
275 /* Defines used set level data sizes */
276 /* has to be at least sizeof(struct level_buf) + sizeof(struct lvlX_buf */
277 #define ISAL_DEF_LVL0_REQ 0
278 #define ISAL_DEF_LVL1_REQ (4 * IGZIP_K + 2 * IGZIP_LVL1_HASH_SIZE)
279 #define ISAL_DEF_LVL1_TOKEN_SIZE 4
280 #define ISAL_DEF_LVL2_REQ (4 * IGZIP_K + 2 * IGZIP_LVL2_HASH_SIZE)
281 #define ISAL_DEF_LVL2_TOKEN_SIZE 4
282 #define ISAL_DEF_LVL3_REQ 4 * IGZIP_K + 4 * 4 * IGZIP_K + 2 * IGZIP_LVL3_HASH_SIZE
283 #define ISAL_DEF_LVL3_TOKEN_SIZE 4
284 
285 /* Data sizes for level specific data options */
286 #define ISAL_DEF_LVL0_MIN ISAL_DEF_LVL0_REQ
287 #define ISAL_DEF_LVL0_SMALL ISAL_DEF_LVL0_REQ
288 #define ISAL_DEF_LVL0_MEDIUM ISAL_DEF_LVL0_REQ
289 #define ISAL_DEF_LVL0_LARGE ISAL_DEF_LVL0_REQ
290 #define ISAL_DEF_LVL0_EXTRA_LARGE ISAL_DEF_LVL0_REQ
291 #define ISAL_DEF_LVL0_DEFAULT ISAL_DEF_LVL0_REQ
292 
293 #define ISAL_DEF_LVL1_MIN (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 1 * IGZIP_K)
294 #define ISAL_DEF_LVL1_SMALL (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 16 * IGZIP_K)
295 #define ISAL_DEF_LVL1_MEDIUM (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 32 * IGZIP_K)
296 #define ISAL_DEF_LVL1_LARGE (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 64 * IGZIP_K)
297 #define ISAL_DEF_LVL1_EXTRA_LARGE (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 128 * IGZIP_K)
298 #define ISAL_DEF_LVL1_DEFAULT ISAL_DEF_LVL1_LARGE
299 
300 #define ISAL_DEF_LVL2_MIN (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 1 * IGZIP_K)
301 #define ISAL_DEF_LVL2_SMALL (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 16 * IGZIP_K)
302 #define ISAL_DEF_LVL2_MEDIUM (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 32 * IGZIP_K)
303 #define ISAL_DEF_LVL2_LARGE (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 64 * IGZIP_K)
304 #define ISAL_DEF_LVL2_EXTRA_LARGE (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 128 * IGZIP_K)
305 #define ISAL_DEF_LVL2_DEFAULT ISAL_DEF_LVL2_LARGE
306 
307 #define ISAL_DEF_LVL3_MIN (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 1 * IGZIP_K)
308 #define ISAL_DEF_LVL3_SMALL (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 16 * IGZIP_K)
309 #define ISAL_DEF_LVL3_MEDIUM (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 32 * IGZIP_K)
310 #define ISAL_DEF_LVL3_LARGE (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 64 * IGZIP_K)
311 #define ISAL_DEF_LVL3_EXTRA_LARGE (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 128 * IGZIP_K)
312 #define ISAL_DEF_LVL3_DEFAULT ISAL_DEF_LVL3_LARGE
313 
314 #define IGZIP_NO_HIST 0
315 #define IGZIP_HIST 1
316 #define IGZIP_DICT_HIST 2
317 #define IGZIP_DICT_HASH_SET 3
318 
319 /** @brief Holds Bit Buffer information*/
320 struct BitBuf2 {
321 	uint64_t m_bits;	//!< bits in the bit buffer
322 	uint32_t m_bit_count;	//!< number of valid bits in the bit buffer
323 	uint8_t *m_out_buf;	//!< current index of buffer to write to
324 	uint8_t *m_out_end;	//!< end of buffer to write to
325 	uint8_t *m_out_start;	//!< start of buffer to write to
326 };
327 
328 struct isal_zlib_header {
329 	uint32_t info;		//!< base-2 logarithm of the LZ77 window size minus 8
330 	uint32_t level;		//!< Compression level (fastest, fast, default, maximum)
331 	uint32_t dict_id;	//!< Dictionary id
332 	uint32_t dict_flag;	//!< Whether to use a dictionary
333 };
334 
335 struct isal_gzip_header {
336 	uint32_t text;		//!< Optional Text hint
337 	uint32_t time;		//!< Unix modification time in gzip header
338 	uint32_t xflags;		//!< xflags in gzip header
339 	uint32_t os;		//!< OS in gzip header
340 	uint8_t *extra;		//!< Extra field in gzip header
341 	uint32_t extra_buf_len;	//!< Length of extra buffer
342 	uint32_t extra_len;	//!< Actual length of gzip header extra field
343 	char *name;		//!< Name in gzip header
344 	uint32_t name_buf_len;	//!< Length of name buffer
345 	char *comment;		//!< Comments in gzip header
346 	uint32_t comment_buf_len;	//!< Length of comment buffer
347 	uint32_t hcrc;		//!< Header crc or header crc flag
348 	uint32_t flags;		//!< Internal data
349 };
350 
351 /* Variable prefixes:
352  * b_ : Measured wrt the start of the buffer
353  * f_ : Measured wrt the start of the file (aka file_start)
354  */
355 
356 /** @brief Holds the internal state information for input and output compression streams*/
357 struct isal_zstate {
358 	uint32_t total_in_start; //!< Not used, may be replaced with something else
359 	uint32_t block_next;	//!< Start of current deflate block in the input
360 	uint32_t block_end;	//!< End of current deflate block in the input
361 	uint32_t dist_mask;	//!< Distance mask used.
362 	uint32_t hash_mask;
363 	enum isal_zstate_state state;	//!< Current state in processing the data stream
364 	struct BitBuf2 bitbuf;	//!< Bit Buffer
365 	uint32_t crc;		//!< Current checksum without finalize step if any (adler)
366 	uint8_t has_wrap_hdr;	//!< keeps track of wrapper header
367 	uint8_t has_eob_hdr;	//!< keeps track of eob hdr (with BFINAL set)
368 	uint8_t has_eob;	//!< keeps track of eob on the last deflate block
369 	uint8_t has_hist;	//!< flag to track if there is match history
370 	uint16_t has_level_buf_init; //!< flag to track if user supplied memory has been initialized.
371 	uint32_t count;	//!< used for partial header/trailer writes
372 	uint8_t tmp_out_buff[16];	//!< temporary array
373 	uint32_t tmp_out_start;	//!< temporary variable
374 	uint32_t tmp_out_end;	//!< temporary variable
375 	uint32_t b_bytes_valid;	//!< number of valid bytes in buffer
376 	uint32_t b_bytes_processed;	//!< number of bytes processed in buffer
377 	uint8_t buffer[2 * IGZIP_HIST_SIZE + ISAL_LOOK_AHEAD];	//!< Internal buffer
378 
379 	/* Stream should be setup such that the head is cache aligned*/
380 	uint16_t head[IGZIP_LVL0_HASH_SIZE];	//!< Hash array
381 };
382 
383 /** @brief Holds the huffman tree used to huffman encode the input stream **/
384 struct isal_hufftables {
385 
386 	uint8_t deflate_hdr[ISAL_DEF_MAX_HDR_SIZE]; //!< deflate huffman tree header
387 	uint32_t deflate_hdr_count; //!< Number of whole bytes in deflate_huff_hdr
388 	uint32_t deflate_hdr_extra_bits; //!< Number of bits in the partial byte in header
389 	uint32_t dist_table[IGZIP_DIST_TABLE_SIZE]; //!< bits 4:0 are the code length, bits 31:5 are the code
390 	uint32_t len_table[IGZIP_LEN_TABLE_SIZE]; //!< bits 4:0 are the code length, bits 31:5 are the code
391 	uint16_t lit_table[IGZIP_LIT_TABLE_SIZE]; //!< literal code
392 	uint8_t lit_table_sizes[IGZIP_LIT_TABLE_SIZE]; //!< literal code length
393 	uint16_t dcodes[30 - IGZIP_DECODE_OFFSET]; //!< distance code
394 	uint8_t dcodes_sizes[30 - IGZIP_DECODE_OFFSET]; //!< distance code length
395 
396 };
397 
398 /** @brief Holds stream information*/
399 struct isal_zstream {
400 	uint8_t *next_in;	//!< Next input byte
401 	uint32_t avail_in;	//!< number of bytes available at next_in
402 	uint32_t total_in;	//!< total number of bytes read so far
403 
404 	uint8_t *next_out;	//!< Next output byte
405 	uint32_t avail_out;	//!< number of bytes available at next_out
406 	uint32_t total_out;	//!< total number of bytes written so far
407 
408 	struct isal_hufftables *hufftables; //!< Huffman encoding used when compressing
409 	uint32_t level; //!< Compression level to use
410 	uint32_t level_buf_size; //!< Size of level_buf
411 	uint8_t * level_buf; //!< User allocated buffer required for different compression levels
412 	uint16_t end_of_stream;	//!< non-zero if this is the last input buffer
413 	uint16_t flush;	//!< Flush type can be NO_FLUSH, SYNC_FLUSH or FULL_FLUSH
414 	uint16_t gzip_flag; //!< Indicate if gzip compression is to be performed
415 	uint16_t hist_bits; //!< Log base 2 of maximum lookback distance, 0 is use default
416 	struct isal_zstate internal_state;	//!< Internal state for this stream
417 };
418 
419 /******************************************************************************/
420 /* Inflate structures */
421 /******************************************************************************/
422 /*
423  * Inflate_huff_code data structures are used to store a Huffman code for fast
424  * lookup. It works by performing a lookup in small_code_lookup that hopefully
425  * yields the correct symbol. Otherwise a lookup into long_code_lookup is
426  * performed to find the correct symbol. The details of how this works follows:
427  *
428  * Let i be some index into small_code_lookup and let e be the associated
429  * element.  Bit 15 in e is a flag. If bit 15 is not set, then index i contains
430  * a Huffman code for a symbol which has length at most DECODE_LOOKUP_SIZE. Bits
431  * 0 through 8 are the symbol associated with that code and bits 9 through 12 of
432  * e represent the number of bits in the code. If bit 15 is set, the i
433  * corresponds to the first DECODE_LOOKUP_SIZE bits of a Huffman code which has
434  * length longer than DECODE_LOOKUP_SIZE. In this case, bits 0 through 8
435  * represent an offset into long_code_lookup table and bits 9 through 12
436  * represent the maximum length of a Huffman code starting with the bits in the
437  * index i. The offset into long_code_lookup is for an array associated with all
438  * codes which start with the bits in i.
439  *
440  * The elements of long_code_lookup are in the same format as small_code_lookup,
441  * except bit 15 is never set. Let i be a number made up of DECODE_LOOKUP_SIZE
442  * bits.  Then all Huffman codes which start with DECODE_LOOKUP_SIZE bits are
443  * stored in an array starting at index h in long_code_lookup. This index h is
444  * stored in bits 0 through 9 at index i in small_code_lookup. The index j is an
445  * index of this array if the number of bits contained in j and i is the number
446  * of bits in the longest huff_code starting with the bits of i. The symbol
447  * stored at index j is the symbol whose huffcode can be found in (j <<
448  * DECODE_LOOKUP_SIZE) | i. Note these arrays will be stored sorted in order of
449  * maximum Huffman code length.
450  *
451  * The following are explanations for sizes of the tables:
452  *
453  * Since small_code_lookup is a lookup on DECODE_LOOKUP_SIZE bits, it must have
454  * size 2^DECODE_LOOKUP_SIZE.
455  *
456  * To determine the amount of memory required for long_code_lookup, note that
457  * any element of long_code_lookup corresponds to a code, a duplicate of an
458  * existing code, or a invalid code. Since deflate Huffman are stored such that
459  * the code size and the code value form an increasing function, the number of
460  * duplicates is maximized when all the duplicates are contained in a single
461  * array, thus there are at most 2^(15 - DECODE_LOOKUP_SIZE) -
462  * (DECODE_LOOKUP_SIZE + 1) duplicate elements. Similarly the number of invalid
463  * elements is maximized at 2^(15 - DECODE_LOOKUP_SIZE) - 2^(floor((15 -
464  * DECODE_LOOKUP_SIZE)/2) - 2^(ceil((15 - DECODE_LOOKUP_SIZE)/2) + 1. Thus the
465  * amount of memory required is: NUM_CODES + 2^(16 - DECODE_LOOKUP_SIZE) -
466  * (DECODE_LOOKUP_SIZE + 1) - 2^(floor((15 - DECODE_LOOKUP_SIZE)/2) -
467  * 2^(ceil((15 - DECODE_LOOKUP_SIZE)/2) + 1. The values used below are those
468  * values rounded up to the nearest 16 byte boundary
469  *
470  * Note that DECODE_LOOKUP_SIZE can be any length even though the offset in
471  * small_lookup_code is 9 bits long because the increasing relationship between
472  * code length and code value forces the maximum offset to be less than 288.
473  */
474 
475 /* In the following defines, L stands for LARGE and S for SMALL */
476 #define ISAL_L_REM (21 - ISAL_DECODE_LONG_BITS)
477 #define ISAL_S_REM (15 - ISAL_DECODE_SHORT_BITS)
478 
479 #define ISAL_L_DUP ((1 << ISAL_L_REM) - (ISAL_L_REM + 1))
480 #define ISAL_S_DUP ((1 << ISAL_S_REM) - (ISAL_S_REM + 1))
481 
482 #define ISAL_L_UNUSED ((1 << ISAL_L_REM) - (1 << ((ISAL_L_REM)/2)) - (1 << ((ISAL_L_REM + 1)/2)) + 1)
483 #define ISAL_S_UNUSED ((1 << ISAL_S_REM) - (1 << ((ISAL_S_REM)/2)) - (1 << ((ISAL_S_REM + 1)/2)) + 1)
484 
485 #define ISAL_L_SIZE (ISAL_DEF_LIT_LEN_SYMBOLS + ISAL_L_DUP + ISAL_L_UNUSED)
486 #define ISAL_S_SIZE (ISAL_DEF_DIST_SYMBOLS + ISAL_S_DUP + ISAL_S_UNUSED)
487 
488 #define ISAL_HUFF_CODE_LARGE_LONG_ALIGNED (ISAL_L_SIZE + (-ISAL_L_SIZE & 0xf))
489 #define ISAL_HUFF_CODE_SMALL_LONG_ALIGNED (ISAL_S_SIZE + (-ISAL_S_SIZE & 0xf))
490 
491 /* Large lookup table for decoding huffman codes */
492 struct inflate_huff_code_large {
493 	uint32_t short_code_lookup[1 << (ISAL_DECODE_LONG_BITS)];
494 	uint16_t long_code_lookup[ISAL_HUFF_CODE_LARGE_LONG_ALIGNED];
495 };
496 
497 /* Small lookup table for decoding huffman codes */
498 struct inflate_huff_code_small {
499 	uint16_t short_code_lookup[1 << (ISAL_DECODE_SHORT_BITS)];
500 	uint16_t long_code_lookup[ISAL_HUFF_CODE_SMALL_LONG_ALIGNED];
501 };
502 
503 /** @brief Holds decompression state information*/
504 struct inflate_state {
505 	uint8_t *next_out;	//!< Next output Byte
506 	uint32_t avail_out;	//!< Number of bytes available at next_out
507 	uint32_t total_out;	//!< Total bytes written out so far
508 	uint8_t *next_in;	//!< Next input byte
509 	uint64_t read_in;	//!< Bits buffered to handle unaligned streams
510 	uint32_t avail_in;	//!< Number of bytes available at next_in
511 	int32_t read_in_length;	//!< Bits in read_in
512 	struct inflate_huff_code_large lit_huff_code;	//!< Structure for decoding lit/len symbols
513 	struct inflate_huff_code_small dist_huff_code;	//!< Structure for decoding dist symbols
514 	enum isal_block_state block_state;	//!< Current decompression state
515 	uint32_t dict_length;	//!< Length of dictionary used
516 	uint32_t bfinal;	//!< Flag identifying final block
517 	uint32_t crc_flag;	//!< Flag identifying whether to track of crc
518 	uint32_t crc;		//!< Contains crc or adler32 of output if crc_flag is set
519 	uint32_t hist_bits; //!< Log base 2 of maximum lookback distance
520 	union {
521 		int32_t type0_block_len;	//!< Length left to read of type 0 block when outbuffer overflow occurred
522 		int32_t count; //!< Count of bytes remaining to be parsed
523 		uint32_t dict_id;
524 	};
525 	int32_t write_overflow_lits;
526 	int32_t write_overflow_len;
527 	int32_t copy_overflow_length; 	//!< Length left to copy when outbuffer overflow occurred
528 	int32_t copy_overflow_distance;	//!< Lookback distance when outbuffer overflow occurred
529 	int16_t wrapper_flag;
530 	int16_t tmp_in_size;	//!< Number of bytes in tmp_in_buffer
531 	int32_t tmp_out_valid;	//!< Number of bytes in tmp_out_buffer
532 	int32_t tmp_out_processed;	//!< Number of bytes processed in tmp_out_buffer
533 	uint8_t tmp_in_buffer[ISAL_DEF_MAX_HDR_SIZE];	//!< Temporary buffer containing data from the input stream
534 	uint8_t tmp_out_buffer[2 * ISAL_DEF_HIST_SIZE + ISAL_LOOK_AHEAD]; 	//!< Temporary buffer containing data from the output stream
535 };
536 
537 /******************************************************************************/
538 /* Compression functions */
539 /******************************************************************************/
540 /**
541  * @brief Updates histograms to include the symbols found in the input
542  * stream. Since this function only updates the histograms, it can be called on
543  * multiple streams to get a histogram better representing the desired data
544  * set. When first using histogram it must be initialized by zeroing the
545  * structure.
546  *
547  * @param in_stream: Input stream of data.
548  * @param length: The length of start_stream.
549  * @param histogram: The returned histogram of lit/len/dist symbols.
550  */
551 void isal_update_histogram(uint8_t * in_stream, int length, struct isal_huff_histogram * histogram);
552 
553 
554 /**
555  * @brief Creates a custom huffman code for the given histograms in which
556  *  every literal and repeat length is assigned a code and all possible lookback
557  *  distances are assigned a code.
558  *
559  * @param hufftables: the output structure containing the huffman code
560  * @param histogram: histogram containing frequency of literal symbols,
561  *        repeat lengths and lookback distances
562  * @returns Returns a non zero value if an invalid huffman code was created.
563  */
564 int isal_create_hufftables(struct isal_hufftables * hufftables,
565 			struct isal_huff_histogram * histogram);
566 
567 /**
568  * @brief Creates a custom huffman code for the given histograms like
569  * isal_create_hufftables() except literals with 0 frequency in the histogram
570  * are not assigned a code
571  *
572  * @param hufftables: the output structure containing the huffman code
573  * @param histogram: histogram containing frequency of literal symbols,
574  *        repeat lengths and lookback distances
575  * @returns Returns a non zero value if an invalid huffman code was created.
576  */
577 int isal_create_hufftables_subset(struct isal_hufftables * hufftables,
578 				struct isal_huff_histogram * histogram);
579 
580 /**
581  * @brief Initialize compression stream data structure
582  *
583  * @param stream Structure holding state information on the compression streams.
584  * @returns none
585  */
586 void isal_deflate_init(struct isal_zstream *stream);
587 
588 /**
589  * @brief Reinitialize compression stream data structure. Performs the same
590  * action as isal_deflate_init, but does not change user supplied input such as
591  * the level, flush type, compression wrapper (like gzip), hufftables, and
592  * end_of_stream_flag.
593  *
594  * @param stream Structure holding state information on the compression streams.
595  * @returns none
596  */
597 void isal_deflate_reset(struct isal_zstream *stream);
598 
599 
600 /**
601  * @brief Set gzip header default values
602  *
603  * @param gz_hdr: Gzip header to initialize.
604  */
605 void isal_gzip_header_init(struct isal_gzip_header *gz_hdr);
606 
607 /**
608  * @brief Write gzip header to output stream
609  *
610  * Writes the gzip header to the output stream. On entry this function assumes
611  * that the output buffer has been initialized, so stream->next_out,
612  * stream->avail_out and stream->total_out have been set. If the output buffer
613  * contains insufficient space, stream is not modified.
614  *
615  * @param stream: Structure holding state information on the compression stream.
616  * @param gz_hdr: Structure holding the gzip header information to encode.
617  *
618  * @returns Returns 0 if the header is successfully written, otherwise returns
619  * the minimum size required to successfully write the gzip header to the output
620  * buffer.
621  */
622 uint32_t isal_write_gzip_header(struct isal_zstream * stream, struct isal_gzip_header *gz_hdr);
623 
624 /**
625  * @brief Write zlib header to output stream
626  *
627  * Writes the zlib header to the output stream. On entry this function assumes
628  * that the output buffer has been initialized, so stream->next_out,
629  * stream->avail_out and stream->total_out have been set. If the output buffer
630  * contains insufficient space, stream is not modified.
631  *
632  * @param stream: Structure holding state information on the compression stream.
633  * @param z_hdr: Structure holding the zlib header information to encode.
634  *
635  * @returns Returns 0 if the header is successfully written, otherwise returns
636  * the minimum size required to successfully write the zlib header to the output
637  * buffer.
638  */
639 uint32_t isal_write_zlib_header(struct isal_zstream * stream, struct isal_zlib_header *z_hdr);
640 
641 /**
642  * @brief Set stream to use a new Huffman code
643  *
644  * Sets the Huffman code to be used in compression before compression start or
645  * after the successful completion of a SYNC_FLUSH or FULL_FLUSH. If type has
646  * value IGZIP_HUFFTABLE_DEFAULT, the stream is set to use the default Huffman
647  * code. If type has value IGZIP_HUFFTABLE_STATIC, the stream is set to use the
648  * deflate standard static Huffman code, or if type has value
649  * IGZIP_HUFFTABLE_CUSTOM, the stream is set to sue the isal_hufftables
650  * structure input to isal_deflate_set_hufftables.
651  *
652  * @param stream: Structure holding state information on the compression stream.
653  * @param hufftables: new huffman code to use if type is set to
654  * IGZIP_HUFFTABLE_CUSTOM.
655  * @param type: Flag specifying what hufftable to use.
656  *
657  * @returns Returns INVALID_OPERATION if the stream was unmodified. This may be
658  * due to the stream being in a state where changing the huffman code is not
659  * allowed or an invalid input is provided.
660  */
661 int isal_deflate_set_hufftables(struct isal_zstream *stream,
662 				struct isal_hufftables *hufftables, int type);
663 
664 /**
665  * @brief Initialize compression stream data structure
666  *
667  * @param stream Structure holding state information on the compression streams.
668  * @returns none
669  */
670 void isal_deflate_stateless_init(struct isal_zstream *stream);
671 
672 
673 /**
674  * @brief Set compression dictionary to use
675  *
676  * This function is to be called after isal_deflate_init, or after completing a
677  * SYNC_FLUSH or FULL_FLUSH and before the next call do isal_deflate. If the
678  * dictionary is longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE
679  * bytes will be used.
680  *
681  * @param stream Structure holding state information on the compression streams.
682  * @param dict: Array containing dictionary to use.
683  * @param dict_len: Length of dict.
684  * @returns COMP_OK,
685  *          ISAL_INVALID_STATE (dictionary could not be set)
686  */
687 int isal_deflate_set_dict(struct isal_zstream *stream, uint8_t *dict, uint32_t dict_len);
688 
689 /** @brief Structure for holding processed dictionary information */
690 
691 struct isal_dict {
692 	uint32_t params;
693 	uint32_t level;
694 	uint32_t hist_size;
695 	uint32_t hash_size;
696 	uint8_t history[ISAL_DEF_HIST_SIZE];
697 	uint16_t hashtable[IGZIP_LVL3_HASH_SIZE];
698 };
699 
700 /**
701  * @brief Process dictionary to reuse later
702  *
703  * Processes a dictionary so that the generated output can be reused to reset a
704  * new deflate stream more quickly than isal_deflate_set_dict() alone. This
705  * function is paired with isal_deflate_reset_dict() when using the same
706  * dictionary on multiple deflate objects. The stream.level must be set prior to
707  * calling this function to process the dictionary correctly. If the dictionary
708  * is longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE bytes will be
709  * used.
710  *
711  * @param stream Structure holding state information on the compression streams.
712  * @param dict_str: Structure to hold processed dictionary info to reuse later.
713  * @param dict: Array containing dictionary to use.
714  * @param dict_len: Length of dict.
715  * @returns COMP_OK,
716  *          ISAL_INVALID_STATE (dictionary could not be processed)
717  */
718 int isal_deflate_process_dict(struct isal_zstream *stream, struct isal_dict *dict_str,
719 			uint8_t *dict, uint32_t dict_len);
720 
721 /**
722  * @brief Reset compression dictionary to use
723  *
724  * Similar to isal_deflate_set_dict() but on pre-processed dictionary
725  * data. Pairing with isal_deflate_process_dict() can reduce the processing time
726  * on subsequent compression with dictionary especially on small files.
727  *
728  * Like isal_deflate_set_dict(), this function is to be called after
729  * isal_deflate_init, or after completing a SYNC_FLUSH or FULL_FLUSH and before
730  * the next call do isal_deflate. Changing compression level between dictionary
731  * process and reset will cause return of ISAL_INVALID_STATE.
732  *
733  * @param stream Structure holding state information on the compression streams.
734  * @param dict_str: Structure with pre-processed dictionary info.
735  * @returns COMP_OK,
736  *          ISAL_INVALID_STATE or other (dictionary could not be reset)
737  */
738 int isal_deflate_reset_dict(struct isal_zstream *stream, struct isal_dict *dict_str);
739 
740 
741 /**
742  * @brief Fast data (deflate) compression for storage applications.
743  *
744  * The call to isal_deflate() will take data from the input buffer (updating
745  * next_in, avail_in and write a compressed stream to the output buffer
746  * (updating next_out and avail_out). The function returns when either the input
747  * buffer is empty or the output buffer is full.
748  *
749  * On entry to isal_deflate(), next_in points to an input buffer and avail_in
750  * indicates the length of that buffer. Similarly next_out points to an empty
751  * output buffer and avail_out indicates the size of that buffer.
752  *
753  * The fields total_in and total_out start at 0 and are updated by
754  * isal_deflate(). These reflect the total number of bytes read or written so far.
755  *
756  * When the last input buffer is passed in, signaled by setting the
757  * end_of_stream, the routine will complete compression at the end of the input
758  * buffer, as long as the output buffer is big enough.
759  *
760  * The compression level can be set by setting level to any value between
761  * ISAL_DEF_MIN_LEVEL and ISAL_DEF_MAX_LEVEL. When the compression level is
762  * ISAL_DEF_MIN_LEVEL, hufftables can be set to a table trained for the the
763  * specific data type being compressed to achieve better compression. When a
764  * higher compression level is desired, a larger generic memory buffer needs to
765  * be supplied by setting level_buf and level_buf_size to represent the chunk of
766  * memory. For level x, the suggest size for this buffer this buffer is
767  * ISAL_DEFL_LVLx_DEFAULT. The defines ISAL_DEFL_LVLx_MIN, ISAL_DEFL_LVLx_SMALL,
768  * ISAL_DEFL_LVLx_MEDIUM, ISAL_DEFL_LVLx_LARGE, and ISAL_DEFL_LVLx_EXTRA_LARGE
769  * are also provided as other suggested sizes.
770  *
771  * The equivalent of the zlib FLUSH_SYNC operation is currently supported.
772  * Flush types can be NO_FLUSH, SYNC_FLUSH or FULL_FLUSH. Default flush type is
773  * NO_FLUSH. A SYNC_ OR FULL_ flush will byte align the deflate block by
774  * appending an empty stored block once all input has been compressed, including
775  * the buffered input. Checking that the out_buffer is not empty or that
776  * internal_state.state = ZSTATE_NEW_HDR is sufficient to guarantee all input
777  * has been flushed. Additionally FULL_FLUSH will ensure look back history does
778  * not include previous blocks so new blocks are fully independent. Switching
779  * between flush types is supported.
780  *
781  * If a compression dictionary is required, the dictionary can be set calling
782  * isal_deflate_set_dictionary before calling isal_deflate.
783  *
784  * If the gzip_flag is set to IGZIP_GZIP, a generic gzip header and the gzip
785  * trailer are written around the deflate compressed data. If gzip_flag is set
786  * to IGZIP_GZIP_NO_HDR, then only the gzip trailer is written. A full-featured
787  * header is supported by the isal_write_{gzip,zlib}_header() functions.
788  *
789  * @param  stream Structure holding state information on the compression streams.
790  * @return COMP_OK (if everything is ok),
791  *         INVALID_FLUSH (if an invalid FLUSH is selected),
792  *         ISAL_INVALID_LEVEL (if an invalid compression level is selected),
793  *         ISAL_INVALID_LEVEL_BUF (if the level buffer is not large enough).
794  */
795 int isal_deflate(struct isal_zstream *stream);
796 
797 
798 /**
799  * @brief Fast data (deflate) stateless compression for storage applications.
800  *
801  * Stateless (one shot) compression routine with a similar interface to
802  * isal_deflate() but operates on entire input buffer at one time. Parameter
803  * avail_out must be large enough to fit the entire compressed output. Max
804  * expansion is limited to the input size plus the header size of a stored/raw
805  * block.
806  *
807  * When the compression level is set to 1, unlike in isal_deflate(), level_buf
808  * may be optionally set depending on what what performance is desired.
809  *
810  * For stateless the flush types NO_FLUSH and FULL_FLUSH are supported.
811  * FULL_FLUSH will byte align the output deflate block so additional blocks can
812  * be easily appended.
813  *
814  * If the gzip_flag is set to IGZIP_GZIP, a generic gzip header and the gzip
815  * trailer are written around the deflate compressed data. If gzip_flag is set
816  * to IGZIP_GZIP_NO_HDR, then only the gzip trailer is written.
817  *
818  * @param  stream Structure holding state information on the compression streams.
819  * @return COMP_OK (if everything is ok),
820  *         INVALID_FLUSH (if an invalid FLUSH is selected),
821  *         ISAL_INVALID_LEVEL (if an invalid compression level is selected),
822  *         ISAL_INVALID_LEVEL_BUF (if the level buffer is not large enough),
823  *         STATELESS_OVERFLOW (if output buffer will not fit output).
824  */
825 int isal_deflate_stateless(struct isal_zstream *stream);
826 
827 
828 /******************************************************************************/
829 /* Inflate functions */
830 /******************************************************************************/
831 /**
832  * @brief Initialize decompression state data structure
833  *
834  * @param state Structure holding state information on the compression streams.
835  * @returns none
836  */
837 void isal_inflate_init(struct inflate_state *state);
838 
839 /**
840  * @brief Reinitialize decompression state data structure
841  *
842  * @param state Structure holding state information on the compression streams.
843  * @returns none
844  */
845 void isal_inflate_reset(struct inflate_state *state);
846 
847 /**
848  * @brief Set decompression dictionary to use
849  *
850  * This function is to be called after isal_inflate_init. If the dictionary is
851  * longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE bytes will be
852  * used.
853  *
854  * @param state: Structure holding state information on the decompression stream.
855  * @param dict: Array containing dictionary to use.
856  * @param dict_len: Length of dict.
857  * @returns COMP_OK,
858  *          ISAL_INVALID_STATE (dictionary could not be set)
859  */
860 int isal_inflate_set_dict(struct inflate_state *state, uint8_t *dict, uint32_t dict_len);
861 
862 /**
863  * @brief Read and return gzip header information
864  *
865  * On entry state must be initialized and next_in pointing to a gzip compressed
866  * buffer. The buffers gz_hdr->extra, gz_hdr->name, gz_hdr->comments and the
867  * buffer lengths must be set to record the corresponding field, or set to NULL
868  * to disregard that gzip header information. If one of these buffers overflows,
869  * the user can reallocate a larger buffer and call this function again to
870  * continue reading the header information.
871  *
872  * @param state: Structure holding state information on the decompression stream.
873  * @param gz_hdr: Structure to return data encoded in the gzip header
874  * @returns ISAL_DECOMP_OK (header was successfully parsed)
875  *          ISAL_END_INPUT (all input was parsed),
876  *          ISAL_NAME_OVERFLOW (gz_hdr->name overflowed while parsing),
877  *          ISAL_COMMENT_OVERFLOW (gz_hdr->comment overflowed while parsing),
878  *          ISAL_EXTRA_OVERFLOW (gz_hdr->extra overflowed while parsing),
879  *          ISAL_INVALID_WRAPPER (invalid gzip header found),
880  *          ISAL_UNSUPPORTED_METHOD (deflate is not the compression method),
881  *          ISAL_INCORRECT_CHECKSUM (gzip header checksum was incorrect)
882  */
883 int isal_read_gzip_header (struct inflate_state *state, struct isal_gzip_header *gz_hdr);
884 
885 /**
886  * @brief Read and return zlib header information
887  *
888  * On entry state must be initialized and next_in pointing to a zlib compressed
889  * buffer.
890  *
891  * @param state: Structure holding state information on the decompression stream.
892  * @param zlib_hdr: Structure to return data encoded in the zlib header
893  * @returns ISAL_DECOMP_OK (header was successfully parsed),
894  *          ISAL_END_INPUT (all input was parsed),
895  *          ISAL_UNSUPPORTED_METHOD (deflate is not the compression method),
896  *          ISAL_INCORRECT_CHECKSUM (zlib header checksum was incorrect)
897  */
898 int isal_read_zlib_header (struct inflate_state *state, struct isal_zlib_header *zlib_hdr);
899 
900 /**
901  * @brief Fast data (deflate) decompression for storage applications.
902  *
903  * On entry to isal_inflate(), next_in points to an input buffer and avail_in
904  * indicates the length of that buffer. Similarly next_out points to an empty
905  * output buffer and avail_out indicates the size of that buffer.
906  *
907  * The field total_out starts at 0 and is updated by isal_inflate(). This
908  * reflects the total number of bytes written so far.
909  *
910  * The call to isal_inflate() will take data from the input buffer (updating
911  * next_in, avail_in and write a decompressed stream to the output buffer
912  * (updating next_out and avail_out). The function returns when the input buffer
913  * is empty, the output buffer is full, invalid data is found, or in the case of
914  * zlib formatted data if a dictionary is specified. The current state of the
915  * decompression on exit can be read from state->block-state.
916  *
917  * If the crc_flag is set to ISAL_GZIP_NO_HDR the gzip crc of the output is
918  * stored in state->crc. Alternatively, if the crc_flag is set to
919  * ISAL_ZLIB_NO_HDR the adler32 of the output is stored in state->crc (checksum
920  * may not be updated until decompression is complete). When the crc_flag is set
921  * to ISAL_GZIP_NO_HDR_VER or ISAL_ZLIB_NO_HDR_VER, the behavior is the same,
922  * except the checksum is verified with the checksum after immediately following
923  * the deflate data. If the crc_flag is set to ISAL_GZIP or ISAL_ZLIB, the
924  * gzip/zlib header is parsed, state->crc is set to the appropriate checksum,
925  * and the checksum is verified. If the crc_flag is set to ISAL_DEFLATE
926  * (default), then the data is treated as a raw deflate block.
927  *
928  * The element state->hist_bits has values from 0 to 15, where values of 1 to 15
929  * are the log base 2 size of the matching window and 0 is the default with
930  * maximum history size.
931  *
932  * If a dictionary is required, a call to isal_inflate_set_dict will set the
933  * dictionary.
934  *
935  * @param  state Structure holding state information on the compression streams.
936  * @return ISAL_DECOMP_OK (if everything is ok),
937  *         ISAL_INVALID_BLOCK,
938  *         ISAL_NEED_DICT,
939  *         ISAL_INVALID_SYMBOL,
940  *         ISAL_INVALID_LOOKBACK,
941  *         ISAL_INVALID_WRAPPER,
942  *         ISAL_UNSUPPORTED_METHOD,
943  *         ISAL_INCORRECT_CHECKSUM.
944  */
945 
946 int isal_inflate(struct inflate_state *state);
947 
948 /**
949  * @brief Fast data (deflate) stateless decompression for storage applications.
950  *
951  * Stateless (one shot) decompression routine with a similar interface to
952  * isal_inflate() but operates on entire input buffer at one time. Parameter
953  * avail_out must be large enough to fit the entire decompressed
954  * output. Dictionaries are not supported.
955  *
956  * @param  state Structure holding state information on the compression streams.
957  * @return ISAL_DECOMP_OK (if everything is ok),
958  *         ISAL_END_INPUT (if all input was decompressed),
959  *         ISAL_NEED_DICT,
960  *         ISAL_OUT_OVERFLOW (if output buffer ran out of space),
961  *         ISAL_INVALID_BLOCK,
962  *         ISAL_INVALID_SYMBOL,
963  *         ISAL_INVALID_LOOKBACK,
964  *         ISAL_INVALID_WRAPPER,
965  *         ISAL_UNSUPPORTED_METHOD,
966  *         ISAL_INCORRECT_CHECKSUM.
967  */
968 int isal_inflate_stateless(struct inflate_state *state);
969 
970 /******************************************************************************/
971 /* Other functions */
972 /******************************************************************************/
973 /**
974  * @brief Calculate Adler-32 checksum, runs appropriate version.
975  *
976  * This function determines what instruction sets are enabled and selects the
977  * appropriate version at runtime.
978  *
979  * @param init: initial Adler-32 value
980  * @param buf: buffer to calculate checksum on
981  * @param len: buffer length in bytes
982  *
983  * @returns 32-bit Adler-32 checksum
984  */
985 uint32_t isal_adler32(uint32_t init, const unsigned char *buf, uint64_t len);
986 
987 #ifdef __cplusplus
988 }
989 #endif
990 #endif	/* ifndef _IGZIP_H */
991