Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/isa-l/igzip_lib.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**********************************************************************
0002   Copyright(c) 2011-2016 Intel Corporation All rights reserved.
0003 
0004   Redistribution and use in source and binary forms, with or without
0005   modification, are permitted provided that the following conditions
0006   are met:
0007     * Redistributions of source code must retain the above copyright
0008       notice, this list of conditions and the following disclaimer.
0009     * Redistributions in binary form must reproduce the above copyright
0010       notice, this list of conditions and the following disclaimer in
0011       the documentation and/or other materials provided with the
0012       distribution.
0013     * Neither the name of Intel Corporation nor the names of its
0014       contributors may be used to endorse or promote products derived
0015       from this software without specific prior written permission.
0016 
0017   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0018   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0019   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0020   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0021   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0022   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0023   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0024   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0025   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0026   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0027   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0028 **********************************************************************/
0029 
0030 #ifndef _IGZIP_H
0031 #define _IGZIP_H
0032 
0033 /**
0034  * @file igzip_lib.h
0035  *
0036  * @brief This file defines the igzip compression and decompression interface, a
0037  * high performance deflate compression interface for storage applications.
0038  *
0039  * Deflate is a widely used compression standard that can be used standalone, it
0040  * also forms the basis of gzip and zlib compression formats. Igzip supports the
0041  * following flush features:
0042  *
0043  * - No Flush: The default method where no special flush is performed.
0044  *
0045  * - Sync flush: whereby isal_deflate() finishes the current deflate block at
0046  *   the end of each input buffer. The deflate block is byte aligned by
0047  *   appending an empty stored block.
0048  *
0049  * - Full flush: whereby isal_deflate() finishes and aligns the deflate block as
0050  *   in sync flush but also ensures that subsequent block's history does not
0051  *   look back beyond this point and new blocks are fully independent.
0052  *
0053  * Igzip also supports compression levels from ISAL_DEF_MIN_LEVEL to
0054  * ISAL_DEF_MAX_LEVEL.
0055  *
0056  * Igzip contains some behavior configurable at compile time. These
0057  * configurable options are:
0058  *
0059  * - IGZIP_HIST_SIZE - Defines the window size. The default value is 32K (note K
0060  *   represents 1024), but 8K is also supported. Powers of 2 which are at most
0061  *   32K may also work.
0062  *
0063  * - LONGER_HUFFTABLES - Defines whether to use a larger hufftables structure
0064  *   which may increase performance with smaller IGZIP_HIST_SIZE values. By
0065  *   default this option is not defined. This define sets IGZIP_HIST_SIZE to be
0066  *   8 if IGZIP_HIST_SIZE > 8K.
0067  *
0068  *   As an example, to compile gzip with an 8K window size, in a terminal run
0069  *   @verbatim gmake D="-D IGZIP_HIST_SIZE=8*1024" @endverbatim on Linux and
0070  *   FreeBSD, or with @verbatim nmake -f Makefile.nmake D="-D
0071  *   IGZIP_HIST_SIZE=8*1024" @endverbatim on Windows.
0072  *
0073  */
0074 #include <stdint.h>
0075 
0076 #ifdef __cplusplus
0077 extern "C" {
0078 #endif
0079 
0080 /******************************************************************************/
0081 /* Deflate Compression Standard Defines */
0082 /******************************************************************************/
0083 #define IGZIP_K  1024
0084 #define ISAL_DEF_MAX_HDR_SIZE 328
0085 #define ISAL_DEF_MAX_CODE_LEN 15
0086 #define ISAL_DEF_HIST_SIZE (32*IGZIP_K)
0087 #define ISAL_DEF_MAX_HIST_BITS 15
0088 #define ISAL_DEF_MAX_MATCH 258
0089 #define ISAL_DEF_MIN_MATCH 3
0090 
0091 #define ISAL_DEF_LIT_SYMBOLS 257
0092 #define ISAL_DEF_LEN_SYMBOLS 29
0093 #define ISAL_DEF_DIST_SYMBOLS 30
0094 #define ISAL_DEF_LIT_LEN_SYMBOLS (ISAL_DEF_LIT_SYMBOLS + ISAL_DEF_LEN_SYMBOLS)
0095 
0096 /* Max repeat length, rounded up to 32 byte boundary */
0097 #define ISAL_LOOK_AHEAD ((ISAL_DEF_MAX_MATCH + 31) & ~31)
0098 
0099 /******************************************************************************/
0100 /* Deflate Implementation Specific Defines */
0101 /******************************************************************************/
0102 /* Note IGZIP_HIST_SIZE must be a power of two */
0103 #ifndef IGZIP_HIST_SIZE
0104 #define IGZIP_HIST_SIZE ISAL_DEF_HIST_SIZE
0105 #endif
0106 
0107 #if (IGZIP_HIST_SIZE > ISAL_DEF_HIST_SIZE)
0108 #undef IGZIP_HIST_SIZE
0109 #define IGZIP_HIST_SIZE ISAL_DEF_HIST_SIZE
0110 #endif
0111 
0112 #ifdef LONGER_HUFFTABLE
0113 #if (IGZIP_HIST_SIZE > 8 * IGZIP_K)
0114 #undef IGZIP_HIST_SIZE
0115 #define IGZIP_HIST_SIZE (8 * IGZIP_K)
0116 #endif
0117 #endif
0118 
0119 #define ISAL_LIMIT_HASH_UPDATE
0120 
0121 #define IGZIP_HASH8K_HASH_SIZE (8 * IGZIP_K)
0122 #define IGZIP_HASH_HIST_SIZE IGZIP_HIST_SIZE
0123 #define IGZIP_HASH_MAP_HASH_SIZE IGZIP_HIST_SIZE
0124 
0125 #define IGZIP_LVL0_HASH_SIZE  (8 * IGZIP_K)
0126 #define IGZIP_LVL1_HASH_SIZE  IGZIP_HASH8K_HASH_SIZE
0127 #define IGZIP_LVL2_HASH_SIZE  IGZIP_HASH_HIST_SIZE
0128 #define IGZIP_LVL3_HASH_SIZE  IGZIP_HASH_MAP_HASH_SIZE
0129 
0130 #ifdef LONGER_HUFFTABLE
0131 enum {IGZIP_DIST_TABLE_SIZE = 8*1024};
0132 
0133 /* DECODE_OFFSET is dist code index corresponding to DIST_TABLE_SIZE + 1 */
0134 enum { IGZIP_DECODE_OFFSET = 26 };
0135 #else
0136 enum {IGZIP_DIST_TABLE_SIZE = 2};
0137 /* DECODE_OFFSET is dist code index corresponding to DIST_TABLE_SIZE + 1 */
0138 enum { IGZIP_DECODE_OFFSET = 0 };
0139 #endif
0140 enum {IGZIP_LEN_TABLE_SIZE = 256};
0141 enum {IGZIP_LIT_TABLE_SIZE = ISAL_DEF_LIT_SYMBOLS};
0142 
0143 #define IGZIP_HUFFTABLE_CUSTOM 0
0144 #define IGZIP_HUFFTABLE_DEFAULT 1
0145 #define IGZIP_HUFFTABLE_STATIC 2
0146 
0147 /* Flush Flags */
0148 #define NO_FLUSH    0   /* Default */
0149 #define SYNC_FLUSH  1
0150 #define FULL_FLUSH  2
0151 #define FINISH_FLUSH    0   /* Deprecated */
0152 
0153 /* Gzip Flags */
0154 #define IGZIP_DEFLATE   0   /* Default */
0155 #define IGZIP_GZIP  1
0156 #define IGZIP_GZIP_NO_HDR   2
0157 #define IGZIP_ZLIB  3
0158 #define IGZIP_ZLIB_NO_HDR   4
0159 
0160 /* Compression Return values */
0161 #define COMP_OK 0
0162 #define INVALID_FLUSH -7
0163 #define INVALID_PARAM -8
0164 #define STATELESS_OVERFLOW -1
0165 #define ISAL_INVALID_OPERATION -9
0166 #define ISAL_INVALID_STATE -3
0167 #define ISAL_INVALID_LEVEL -4   /* Invalid Compression level set */
0168 #define ISAL_INVALID_LEVEL_BUF -5 /* Invalid buffer specified for the compression level */
0169 
0170 /**
0171  *  @enum isal_zstate_state
0172  *  @brief Compression State please note ZSTATE_TRL only applies for GZIP compression
0173  */
0174 
0175 
0176 /* When the state is set to ZSTATE_NEW_HDR or TMP_ZSTATE_NEW_HEADER, the
0177  * hufftable being used for compression may be swapped
0178  */
0179 enum isal_zstate_state {
0180     ZSTATE_NEW_HDR,  //!< Header to be written
0181     ZSTATE_HDR, //!< Header state
0182     ZSTATE_CREATE_HDR, //!< Header to be created
0183     ZSTATE_BODY,    //!< Body state
0184     ZSTATE_FLUSH_READ_BUFFER, //!< Flush buffer
0185     ZSTATE_FLUSH_ICF_BUFFER,
0186     ZSTATE_TYPE0_HDR, //! Type0 block header to be written
0187     ZSTATE_TYPE0_BODY, //!< Type0 block body to be written
0188     ZSTATE_SYNC_FLUSH, //!< Write sync flush block
0189     ZSTATE_FLUSH_WRITE_BUFFER, //!< Flush bitbuf
0190     ZSTATE_TRL, //!< Trailer state
0191     ZSTATE_END, //!< End state
0192     ZSTATE_TMP_NEW_HDR, //!< Temporary Header to be written
0193     ZSTATE_TMP_HDR, //!< Temporary Header state
0194     ZSTATE_TMP_CREATE_HDR, //!< Temporary Header to be created state
0195     ZSTATE_TMP_BODY,    //!< Temporary Body state
0196     ZSTATE_TMP_FLUSH_READ_BUFFER, //!< Flush buffer
0197     ZSTATE_TMP_FLUSH_ICF_BUFFER,
0198     ZSTATE_TMP_TYPE0_HDR, //! Temporary Type0 block header to be written
0199     ZSTATE_TMP_TYPE0_BODY, //!< Temporary Type0 block body to be written
0200     ZSTATE_TMP_SYNC_FLUSH, //!< Write sync flush block
0201     ZSTATE_TMP_FLUSH_WRITE_BUFFER, //!< Flush bitbuf
0202     ZSTATE_TMP_TRL, //!< Temporary Trailer state
0203     ZSTATE_TMP_END  //!< Temporary End state
0204 };
0205 
0206 /* Offset used to switch between TMP states and non-tmp states */
0207 #define ZSTATE_TMP_OFFSET ZSTATE_TMP_HDR - ZSTATE_HDR
0208 
0209 /******************************************************************************/
0210 /* Inflate Implementation Specific Defines */
0211 /******************************************************************************/
0212 #define ISAL_DECODE_LONG_BITS 12
0213 #define ISAL_DECODE_SHORT_BITS 10
0214 
0215 /* Current state of decompression */
0216 enum isal_block_state {
0217     ISAL_BLOCK_NEW_HDR, /* Just starting a new block */
0218     ISAL_BLOCK_HDR,     /* In the middle of reading in a block header */
0219     ISAL_BLOCK_TYPE0,   /* Decoding a type 0 block */
0220     ISAL_BLOCK_CODED,   /* Decoding a huffman coded block */
0221     ISAL_BLOCK_INPUT_DONE,  /* Decompression of input is completed */
0222     ISAL_BLOCK_FINISH,  /* Decompression of input is completed and all data has been flushed to output */
0223     ISAL_GZIP_EXTRA_LEN,
0224     ISAL_GZIP_EXTRA,
0225     ISAL_GZIP_NAME,
0226     ISAL_GZIP_COMMENT,
0227     ISAL_GZIP_HCRC,
0228     ISAL_ZLIB_DICT,
0229     ISAL_CHECKSUM_CHECK,
0230 };
0231 
0232 
0233 /* Inflate Flags */
0234 #define ISAL_DEFLATE    0   /* Default */
0235 #define ISAL_GZIP   1
0236 #define ISAL_GZIP_NO_HDR    2
0237 #define ISAL_ZLIB   3
0238 #define ISAL_ZLIB_NO_HDR    4
0239 #define ISAL_ZLIB_NO_HDR_VER    5
0240 #define ISAL_GZIP_NO_HDR_VER    6
0241 
0242 /* Inflate Return values */
0243 #define ISAL_DECOMP_OK 0    /* No errors encountered while decompressing */
0244 #define ISAL_END_INPUT 1    /* End of input reached */
0245 #define ISAL_OUT_OVERFLOW 2 /* End of output reached */
0246 #define ISAL_NAME_OVERFLOW 3    /* End of gzip name buffer reached */
0247 #define ISAL_COMMENT_OVERFLOW 4 /* End of gzip name buffer reached */
0248 #define ISAL_EXTRA_OVERFLOW 5   /* End of extra buffer reached */
0249 #define ISAL_NEED_DICT 6 /* Stream needs a dictionary to continue */
0250 #define ISAL_INVALID_BLOCK -1   /* Invalid deflate block found */
0251 #define ISAL_INVALID_SYMBOL -2  /* Invalid deflate symbol found */
0252 #define ISAL_INVALID_LOOKBACK -3    /* Invalid lookback distance found */
0253 #define ISAL_INVALID_WRAPPER -4 /* Invalid gzip/zlib wrapper found */
0254 #define ISAL_UNSUPPORTED_METHOD -5  /* Gzip/zlib wrapper specifies unsupported compress method */
0255 #define ISAL_INCORRECT_CHECKSUM -6 /* Incorrect checksum found */
0256 
0257 /******************************************************************************/
0258 /* Compression structures */
0259 /******************************************************************************/
0260 /** @brief Holds histogram of deflate symbols*/
0261 struct isal_huff_histogram {
0262     uint64_t lit_len_histogram[ISAL_DEF_LIT_LEN_SYMBOLS]; //!< Histogram of Literal/Len symbols seen
0263     uint64_t dist_histogram[ISAL_DEF_DIST_SYMBOLS]; //!< Histogram of Distance Symbols seen
0264     uint16_t hash_table[IGZIP_LVL0_HASH_SIZE]; //!< Tmp space used as a hash table
0265 };
0266 
0267 struct isal_mod_hist {
0268     uint32_t d_hist[30];
0269     uint32_t ll_hist[513];
0270 };
0271 
0272 #define ISAL_DEF_MIN_LEVEL 0
0273 #define ISAL_DEF_MAX_LEVEL 3
0274 
0275 /* Defines used set level data sizes */
0276 /* has to be at least sizeof(struct level_buf) + sizeof(struct lvlX_buf */
0277 #define ISAL_DEF_LVL0_REQ 0
0278 #define ISAL_DEF_LVL1_REQ (4 * IGZIP_K + 2 * IGZIP_LVL1_HASH_SIZE)
0279 #define ISAL_DEF_LVL1_TOKEN_SIZE 4
0280 #define ISAL_DEF_LVL2_REQ (4 * IGZIP_K + 2 * IGZIP_LVL2_HASH_SIZE)
0281 #define ISAL_DEF_LVL2_TOKEN_SIZE 4
0282 #define ISAL_DEF_LVL3_REQ 4 * IGZIP_K + 4 * 4 * IGZIP_K + 2 * IGZIP_LVL3_HASH_SIZE
0283 #define ISAL_DEF_LVL3_TOKEN_SIZE 4
0284 
0285 /* Data sizes for level specific data options */
0286 #define ISAL_DEF_LVL0_MIN ISAL_DEF_LVL0_REQ
0287 #define ISAL_DEF_LVL0_SMALL ISAL_DEF_LVL0_REQ
0288 #define ISAL_DEF_LVL0_MEDIUM ISAL_DEF_LVL0_REQ
0289 #define ISAL_DEF_LVL0_LARGE ISAL_DEF_LVL0_REQ
0290 #define ISAL_DEF_LVL0_EXTRA_LARGE ISAL_DEF_LVL0_REQ
0291 #define ISAL_DEF_LVL0_DEFAULT ISAL_DEF_LVL0_REQ
0292 
0293 #define ISAL_DEF_LVL1_MIN (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 1 * IGZIP_K)
0294 #define ISAL_DEF_LVL1_SMALL (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 16 * IGZIP_K)
0295 #define ISAL_DEF_LVL1_MEDIUM (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 32 * IGZIP_K)
0296 #define ISAL_DEF_LVL1_LARGE (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 64 * IGZIP_K)
0297 #define ISAL_DEF_LVL1_EXTRA_LARGE (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 128 * IGZIP_K)
0298 #define ISAL_DEF_LVL1_DEFAULT ISAL_DEF_LVL1_LARGE
0299 
0300 #define ISAL_DEF_LVL2_MIN (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 1 * IGZIP_K)
0301 #define ISAL_DEF_LVL2_SMALL (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 16 * IGZIP_K)
0302 #define ISAL_DEF_LVL2_MEDIUM (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 32 * IGZIP_K)
0303 #define ISAL_DEF_LVL2_LARGE (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 64 * IGZIP_K)
0304 #define ISAL_DEF_LVL2_EXTRA_LARGE (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 128 * IGZIP_K)
0305 #define ISAL_DEF_LVL2_DEFAULT ISAL_DEF_LVL2_LARGE
0306 
0307 #define ISAL_DEF_LVL3_MIN (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 1 * IGZIP_K)
0308 #define ISAL_DEF_LVL3_SMALL (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 16 * IGZIP_K)
0309 #define ISAL_DEF_LVL3_MEDIUM (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 32 * IGZIP_K)
0310 #define ISAL_DEF_LVL3_LARGE (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 64 * IGZIP_K)
0311 #define ISAL_DEF_LVL3_EXTRA_LARGE (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 128 * IGZIP_K)
0312 #define ISAL_DEF_LVL3_DEFAULT ISAL_DEF_LVL3_LARGE
0313 
0314 #define IGZIP_NO_HIST 0
0315 #define IGZIP_HIST 1
0316 #define IGZIP_DICT_HIST 2
0317 #define IGZIP_DICT_HASH_SET 3
0318 
0319 /** @brief Holds Bit Buffer information*/
0320 struct BitBuf2 {
0321     uint64_t m_bits;    //!< bits in the bit buffer
0322     uint32_t m_bit_count;   //!< number of valid bits in the bit buffer
0323     uint8_t *m_out_buf; //!< current index of buffer to write to
0324     uint8_t *m_out_end; //!< end of buffer to write to
0325     uint8_t *m_out_start;   //!< start of buffer to write to
0326 };
0327 
0328 struct isal_zlib_header {
0329     uint32_t info;      //!< base-2 logarithm of the LZ77 window size minus 8
0330     uint32_t level;     //!< Compression level (fastest, fast, default, maximum)
0331     uint32_t dict_id;   //!< Dictionary id
0332     uint32_t dict_flag; //!< Whether to use a dictionary
0333 };
0334 
0335 struct isal_gzip_header {
0336     uint32_t text;      //!< Optional Text hint
0337     uint32_t time;      //!< Unix modification time in gzip header
0338     uint32_t xflags;        //!< xflags in gzip header
0339     uint32_t os;        //!< OS in gzip header
0340     uint8_t *extra;     //!< Extra field in gzip header
0341     uint32_t extra_buf_len; //!< Length of extra buffer
0342     uint32_t extra_len; //!< Actual length of gzip header extra field
0343     char *name;     //!< Name in gzip header
0344     uint32_t name_buf_len;  //!< Length of name buffer
0345     char *comment;      //!< Comments in gzip header
0346     uint32_t comment_buf_len;   //!< Length of comment buffer
0347     uint32_t hcrc;      //!< Header crc or header crc flag
0348     uint32_t flags;     //!< Internal data
0349 };
0350 
0351 /* Variable prefixes:
0352  * b_ : Measured wrt the start of the buffer
0353  * f_ : Measured wrt the start of the file (aka file_start)
0354  */
0355 
0356 /** @brief Holds the internal state information for input and output compression streams*/
0357 struct isal_zstate {
0358     uint32_t total_in_start; //!< Not used, may be replaced with something else
0359     uint32_t block_next;    //!< Start of current deflate block in the input
0360     uint32_t block_end; //!< End of current deflate block in the input
0361     uint32_t dist_mask; //!< Distance mask used.
0362     uint32_t hash_mask;
0363     enum isal_zstate_state state;   //!< Current state in processing the data stream
0364     struct BitBuf2 bitbuf;  //!< Bit Buffer
0365     uint32_t crc;       //!< Current checksum without finalize step if any (adler)
0366     uint8_t has_wrap_hdr;   //!< keeps track of wrapper header
0367     uint8_t has_eob_hdr;    //!< keeps track of eob hdr (with BFINAL set)
0368     uint8_t has_eob;    //!< keeps track of eob on the last deflate block
0369     uint8_t has_hist;   //!< flag to track if there is match history
0370     uint16_t has_level_buf_init; //!< flag to track if user supplied memory has been initialized.
0371     uint32_t count; //!< used for partial header/trailer writes
0372     uint8_t tmp_out_buff[16];   //!< temporary array
0373     uint32_t tmp_out_start; //!< temporary variable
0374     uint32_t tmp_out_end;   //!< temporary variable
0375     uint32_t b_bytes_valid; //!< number of valid bytes in buffer
0376     uint32_t b_bytes_processed; //!< number of bytes processed in buffer
0377     uint8_t buffer[2 * IGZIP_HIST_SIZE + ISAL_LOOK_AHEAD];  //!< Internal buffer
0378 
0379     /* Stream should be setup such that the head is cache aligned*/
0380     uint16_t head[IGZIP_LVL0_HASH_SIZE];    //!< Hash array
0381 };
0382 
0383 /** @brief Holds the huffman tree used to huffman encode the input stream **/
0384 struct isal_hufftables {
0385 
0386     uint8_t deflate_hdr[ISAL_DEF_MAX_HDR_SIZE]; //!< deflate huffman tree header
0387     uint32_t deflate_hdr_count; //!< Number of whole bytes in deflate_huff_hdr
0388     uint32_t deflate_hdr_extra_bits; //!< Number of bits in the partial byte in header
0389     uint32_t dist_table[IGZIP_DIST_TABLE_SIZE]; //!< bits 4:0 are the code length, bits 31:5 are the code
0390     uint32_t len_table[IGZIP_LEN_TABLE_SIZE]; //!< bits 4:0 are the code length, bits 31:5 are the code
0391     uint16_t lit_table[IGZIP_LIT_TABLE_SIZE]; //!< literal code
0392     uint8_t lit_table_sizes[IGZIP_LIT_TABLE_SIZE]; //!< literal code length
0393     uint16_t dcodes[30 - IGZIP_DECODE_OFFSET]; //!< distance code
0394     uint8_t dcodes_sizes[30 - IGZIP_DECODE_OFFSET]; //!< distance code length
0395 
0396 };
0397 
0398 /** @brief Holds stream information*/
0399 struct isal_zstream {
0400     uint8_t *next_in;   //!< Next input byte
0401     uint32_t avail_in;  //!< number of bytes available at next_in
0402     uint32_t total_in;  //!< total number of bytes read so far
0403 
0404     uint8_t *next_out;  //!< Next output byte
0405     uint32_t avail_out; //!< number of bytes available at next_out
0406     uint32_t total_out; //!< total number of bytes written so far
0407 
0408     struct isal_hufftables *hufftables; //!< Huffman encoding used when compressing
0409     uint32_t level; //!< Compression level to use
0410     uint32_t level_buf_size; //!< Size of level_buf
0411     uint8_t * level_buf; //!< User allocated buffer required for different compression levels
0412     uint16_t end_of_stream; //!< non-zero if this is the last input buffer
0413     uint16_t flush; //!< Flush type can be NO_FLUSH, SYNC_FLUSH or FULL_FLUSH
0414     uint16_t gzip_flag; //!< Indicate if gzip compression is to be performed
0415     uint16_t hist_bits; //!< Log base 2 of maximum lookback distance, 0 is use default
0416     struct isal_zstate internal_state;  //!< Internal state for this stream
0417 };
0418 
0419 /******************************************************************************/
0420 /* Inflate structures */
0421 /******************************************************************************/
0422 /*
0423  * Inflate_huff_code data structures are used to store a Huffman code for fast
0424  * lookup. It works by performing a lookup in small_code_lookup that hopefully
0425  * yields the correct symbol. Otherwise a lookup into long_code_lookup is
0426  * performed to find the correct symbol. The details of how this works follows:
0427  *
0428  * Let i be some index into small_code_lookup and let e be the associated
0429  * element.  Bit 15 in e is a flag. If bit 15 is not set, then index i contains
0430  * a Huffman code for a symbol which has length at most DECODE_LOOKUP_SIZE. Bits
0431  * 0 through 8 are the symbol associated with that code and bits 9 through 12 of
0432  * e represent the number of bits in the code. If bit 15 is set, the i
0433  * corresponds to the first DECODE_LOOKUP_SIZE bits of a Huffman code which has
0434  * length longer than DECODE_LOOKUP_SIZE. In this case, bits 0 through 8
0435  * represent an offset into long_code_lookup table and bits 9 through 12
0436  * represent the maximum length of a Huffman code starting with the bits in the
0437  * index i. The offset into long_code_lookup is for an array associated with all
0438  * codes which start with the bits in i.
0439  *
0440  * The elements of long_code_lookup are in the same format as small_code_lookup,
0441  * except bit 15 is never set. Let i be a number made up of DECODE_LOOKUP_SIZE
0442  * bits.  Then all Huffman codes which start with DECODE_LOOKUP_SIZE bits are
0443  * stored in an array starting at index h in long_code_lookup. This index h is
0444  * stored in bits 0 through 9 at index i in small_code_lookup. The index j is an
0445  * index of this array if the number of bits contained in j and i is the number
0446  * of bits in the longest huff_code starting with the bits of i. The symbol
0447  * stored at index j is the symbol whose huffcode can be found in (j <<
0448  * DECODE_LOOKUP_SIZE) | i. Note these arrays will be stored sorted in order of
0449  * maximum Huffman code length.
0450  *
0451  * The following are explanations for sizes of the tables:
0452  *
0453  * Since small_code_lookup is a lookup on DECODE_LOOKUP_SIZE bits, it must have
0454  * size 2^DECODE_LOOKUP_SIZE.
0455  *
0456  * To determine the amount of memory required for long_code_lookup, note that
0457  * any element of long_code_lookup corresponds to a code, a duplicate of an
0458  * existing code, or a invalid code. Since deflate Huffman are stored such that
0459  * the code size and the code value form an increasing function, the number of
0460  * duplicates is maximized when all the duplicates are contained in a single
0461  * array, thus there are at most 2^(15 - DECODE_LOOKUP_SIZE) -
0462  * (DECODE_LOOKUP_SIZE + 1) duplicate elements. Similarly the number of invalid
0463  * elements is maximized at 2^(15 - DECODE_LOOKUP_SIZE) - 2^(floor((15 -
0464  * DECODE_LOOKUP_SIZE)/2) - 2^(ceil((15 - DECODE_LOOKUP_SIZE)/2) + 1. Thus the
0465  * amount of memory required is: NUM_CODES + 2^(16 - DECODE_LOOKUP_SIZE) -
0466  * (DECODE_LOOKUP_SIZE + 1) - 2^(floor((15 - DECODE_LOOKUP_SIZE)/2) -
0467  * 2^(ceil((15 - DECODE_LOOKUP_SIZE)/2) + 1. The values used below are those
0468  * values rounded up to the nearest 16 byte boundary
0469  *
0470  * Note that DECODE_LOOKUP_SIZE can be any length even though the offset in
0471  * small_lookup_code is 9 bits long because the increasing relationship between
0472  * code length and code value forces the maximum offset to be less than 288.
0473  */
0474 
0475 /* In the following defines, L stands for LARGE and S for SMALL */
0476 #define ISAL_L_REM (21 - ISAL_DECODE_LONG_BITS)
0477 #define ISAL_S_REM (15 - ISAL_DECODE_SHORT_BITS)
0478 
0479 #define ISAL_L_DUP ((1 << ISAL_L_REM) - (ISAL_L_REM + 1))
0480 #define ISAL_S_DUP ((1 << ISAL_S_REM) - (ISAL_S_REM + 1))
0481 
0482 #define ISAL_L_UNUSED ((1 << ISAL_L_REM) - (1 << ((ISAL_L_REM)/2)) - (1 << ((ISAL_L_REM + 1)/2)) + 1)
0483 #define ISAL_S_UNUSED ((1 << ISAL_S_REM) - (1 << ((ISAL_S_REM)/2)) - (1 << ((ISAL_S_REM + 1)/2)) + 1)
0484 
0485 #define ISAL_L_SIZE (ISAL_DEF_LIT_LEN_SYMBOLS + ISAL_L_DUP + ISAL_L_UNUSED)
0486 #define ISAL_S_SIZE (ISAL_DEF_DIST_SYMBOLS + ISAL_S_DUP + ISAL_S_UNUSED)
0487 
0488 #define ISAL_HUFF_CODE_LARGE_LONG_ALIGNED (ISAL_L_SIZE + (-ISAL_L_SIZE & 0xf))
0489 #define ISAL_HUFF_CODE_SMALL_LONG_ALIGNED (ISAL_S_SIZE + (-ISAL_S_SIZE & 0xf))
0490 
0491 /* Large lookup table for decoding huffman codes */
0492 struct inflate_huff_code_large {
0493     uint32_t short_code_lookup[1 << (ISAL_DECODE_LONG_BITS)];
0494     uint16_t long_code_lookup[ISAL_HUFF_CODE_LARGE_LONG_ALIGNED];
0495 };
0496 
0497 /* Small lookup table for decoding huffman codes */
0498 struct inflate_huff_code_small {
0499     uint16_t short_code_lookup[1 << (ISAL_DECODE_SHORT_BITS)];
0500     uint16_t long_code_lookup[ISAL_HUFF_CODE_SMALL_LONG_ALIGNED];
0501 };
0502 
0503 /** @brief Holds decompression state information*/
0504 struct inflate_state {
0505     uint8_t *next_out;  //!< Next output Byte
0506     uint32_t avail_out; //!< Number of bytes available at next_out
0507     uint32_t total_out; //!< Total bytes written out so far
0508     uint8_t *next_in;   //!< Next input byte
0509     uint64_t read_in;   //!< Bits buffered to handle unaligned streams
0510     uint32_t avail_in;  //!< Number of bytes available at next_in
0511     int32_t read_in_length; //!< Bits in read_in
0512     struct inflate_huff_code_large lit_huff_code;   //!< Structure for decoding lit/len symbols
0513     struct inflate_huff_code_small dist_huff_code;  //!< Structure for decoding dist symbols
0514     enum isal_block_state block_state;  //!< Current decompression state
0515     uint32_t dict_length;   //!< Length of dictionary used
0516     uint32_t bfinal;    //!< Flag identifying final block
0517     uint32_t crc_flag;  //!< Flag identifying whether to track of crc
0518     uint32_t crc;       //!< Contains crc or adler32 of output if crc_flag is set
0519     uint32_t hist_bits; //!< Log base 2 of maximum lookback distance
0520     union {
0521         int32_t type0_block_len;    //!< Length left to read of type 0 block when outbuffer overflow occurred
0522         int32_t count; //!< Count of bytes remaining to be parsed
0523         uint32_t dict_id;
0524     };
0525     int32_t write_overflow_lits;
0526     int32_t write_overflow_len;
0527     int32_t copy_overflow_length;   //!< Length left to copy when outbuffer overflow occurred
0528     int32_t copy_overflow_distance; //!< Lookback distance when outbuffer overflow occurred
0529     int16_t wrapper_flag;
0530     int16_t tmp_in_size;    //!< Number of bytes in tmp_in_buffer
0531     int32_t tmp_out_valid;  //!< Number of bytes in tmp_out_buffer
0532     int32_t tmp_out_processed;  //!< Number of bytes processed in tmp_out_buffer
0533     uint8_t tmp_in_buffer[ISAL_DEF_MAX_HDR_SIZE];   //!< Temporary buffer containing data from the input stream
0534     uint8_t tmp_out_buffer[2 * ISAL_DEF_HIST_SIZE + ISAL_LOOK_AHEAD];   //!< Temporary buffer containing data from the output stream
0535 };
0536 
0537 /******************************************************************************/
0538 /* Compression functions */
0539 /******************************************************************************/
0540 /**
0541  * @brief Updates histograms to include the symbols found in the input
0542  * stream. Since this function only updates the histograms, it can be called on
0543  * multiple streams to get a histogram better representing the desired data
0544  * set. When first using histogram it must be initialized by zeroing the
0545  * structure.
0546  *
0547  * @param in_stream: Input stream of data.
0548  * @param length: The length of start_stream.
0549  * @param histogram: The returned histogram of lit/len/dist symbols.
0550  */
0551 void isal_update_histogram(uint8_t * in_stream, int length, struct isal_huff_histogram * histogram);
0552 
0553 
0554 /**
0555  * @brief Creates a custom huffman code for the given histograms in which
0556  *  every literal and repeat length is assigned a code and all possible lookback
0557  *  distances are assigned a code.
0558  *
0559  * @param hufftables: the output structure containing the huffman code
0560  * @param histogram: histogram containing frequency of literal symbols,
0561  *        repeat lengths and lookback distances
0562  * @returns Returns a non zero value if an invalid huffman code was created.
0563  */
0564 int isal_create_hufftables(struct isal_hufftables * hufftables,
0565             struct isal_huff_histogram * histogram);
0566 
0567 /**
0568  * @brief Creates a custom huffman code for the given histograms like
0569  * isal_create_hufftables() except literals with 0 frequency in the histogram
0570  * are not assigned a code
0571  *
0572  * @param hufftables: the output structure containing the huffman code
0573  * @param histogram: histogram containing frequency of literal symbols,
0574  *        repeat lengths and lookback distances
0575  * @returns Returns a non zero value if an invalid huffman code was created.
0576  */
0577 int isal_create_hufftables_subset(struct isal_hufftables * hufftables,
0578                 struct isal_huff_histogram * histogram);
0579 
0580 /**
0581  * @brief Initialize compression stream data structure
0582  *
0583  * @param stream Structure holding state information on the compression streams.
0584  * @returns none
0585  */
0586 void isal_deflate_init(struct isal_zstream *stream);
0587 
0588 /**
0589  * @brief Reinitialize compression stream data structure. Performs the same
0590  * action as isal_deflate_init, but does not change user supplied input such as
0591  * the level, flush type, compression wrapper (like gzip), hufftables, and
0592  * end_of_stream_flag.
0593  *
0594  * @param stream Structure holding state information on the compression streams.
0595  * @returns none
0596  */
0597 void isal_deflate_reset(struct isal_zstream *stream);
0598 
0599 
0600 /**
0601  * @brief Set gzip header default values
0602  *
0603  * @param gz_hdr: Gzip header to initialize.
0604  */
0605 void isal_gzip_header_init(struct isal_gzip_header *gz_hdr);
0606 
0607 /**
0608  * @brief Write gzip header to output stream
0609  *
0610  * Writes the gzip header to the output stream. On entry this function assumes
0611  * that the output buffer has been initialized, so stream->next_out,
0612  * stream->avail_out and stream->total_out have been set. If the output buffer
0613  * contains insufficient space, stream is not modified.
0614  *
0615  * @param stream: Structure holding state information on the compression stream.
0616  * @param gz_hdr: Structure holding the gzip header information to encode.
0617  *
0618  * @returns Returns 0 if the header is successfully written, otherwise returns
0619  * the minimum size required to successfully write the gzip header to the output
0620  * buffer.
0621  */
0622 uint32_t isal_write_gzip_header(struct isal_zstream * stream, struct isal_gzip_header *gz_hdr);
0623 
0624 /**
0625  * @brief Write zlib header to output stream
0626  *
0627  * Writes the zlib header to the output stream. On entry this function assumes
0628  * that the output buffer has been initialized, so stream->next_out,
0629  * stream->avail_out and stream->total_out have been set. If the output buffer
0630  * contains insufficient space, stream is not modified.
0631  *
0632  * @param stream: Structure holding state information on the compression stream.
0633  * @param z_hdr: Structure holding the zlib header information to encode.
0634  *
0635  * @returns Returns 0 if the header is successfully written, otherwise returns
0636  * the minimum size required to successfully write the zlib header to the output
0637  * buffer.
0638  */
0639 uint32_t isal_write_zlib_header(struct isal_zstream * stream, struct isal_zlib_header *z_hdr);
0640 
0641 /**
0642  * @brief Set stream to use a new Huffman code
0643  *
0644  * Sets the Huffman code to be used in compression before compression start or
0645  * after the successful completion of a SYNC_FLUSH or FULL_FLUSH. If type has
0646  * value IGZIP_HUFFTABLE_DEFAULT, the stream is set to use the default Huffman
0647  * code. If type has value IGZIP_HUFFTABLE_STATIC, the stream is set to use the
0648  * deflate standard static Huffman code, or if type has value
0649  * IGZIP_HUFFTABLE_CUSTOM, the stream is set to sue the isal_hufftables
0650  * structure input to isal_deflate_set_hufftables.
0651  *
0652  * @param stream: Structure holding state information on the compression stream.
0653  * @param hufftables: new huffman code to use if type is set to
0654  * IGZIP_HUFFTABLE_CUSTOM.
0655  * @param type: Flag specifying what hufftable to use.
0656  *
0657  * @returns Returns INVALID_OPERATION if the stream was unmodified. This may be
0658  * due to the stream being in a state where changing the huffman code is not
0659  * allowed or an invalid input is provided.
0660  */
0661 int isal_deflate_set_hufftables(struct isal_zstream *stream,
0662                 struct isal_hufftables *hufftables, int type);
0663 
0664 /**
0665  * @brief Initialize compression stream data structure
0666  *
0667  * @param stream Structure holding state information on the compression streams.
0668  * @returns none
0669  */
0670 void isal_deflate_stateless_init(struct isal_zstream *stream);
0671 
0672 
0673 /**
0674  * @brief Set compression dictionary to use
0675  *
0676  * This function is to be called after isal_deflate_init, or after completing a
0677  * SYNC_FLUSH or FULL_FLUSH and before the next call do isal_deflate. If the
0678  * dictionary is longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE
0679  * bytes will be used.
0680  *
0681  * @param stream Structure holding state information on the compression streams.
0682  * @param dict: Array containing dictionary to use.
0683  * @param dict_len: Length of dict.
0684  * @returns COMP_OK,
0685  *          ISAL_INVALID_STATE (dictionary could not be set)
0686  */
0687 int isal_deflate_set_dict(struct isal_zstream *stream, uint8_t *dict, uint32_t dict_len);
0688 
0689 /** @brief Structure for holding processed dictionary information */
0690 
0691 struct isal_dict {
0692     uint32_t params;
0693     uint32_t level;
0694     uint32_t hist_size;
0695     uint32_t hash_size;
0696     uint8_t history[ISAL_DEF_HIST_SIZE];
0697     uint16_t hashtable[IGZIP_LVL3_HASH_SIZE];
0698 };
0699 
0700 /**
0701  * @brief Process dictionary to reuse later
0702  *
0703  * Processes a dictionary so that the generated output can be reused to reset a
0704  * new deflate stream more quickly than isal_deflate_set_dict() alone. This
0705  * function is paired with isal_deflate_reset_dict() when using the same
0706  * dictionary on multiple deflate objects. The stream.level must be set prior to
0707  * calling this function to process the dictionary correctly. If the dictionary
0708  * is longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE bytes will be
0709  * used.
0710  *
0711  * @param stream Structure holding state information on the compression streams.
0712  * @param dict_str: Structure to hold processed dictionary info to reuse later.
0713  * @param dict: Array containing dictionary to use.
0714  * @param dict_len: Length of dict.
0715  * @returns COMP_OK,
0716  *          ISAL_INVALID_STATE (dictionary could not be processed)
0717  */
0718 int isal_deflate_process_dict(struct isal_zstream *stream, struct isal_dict *dict_str,
0719             uint8_t *dict, uint32_t dict_len);
0720 
0721 /**
0722  * @brief Reset compression dictionary to use
0723  *
0724  * Similar to isal_deflate_set_dict() but on pre-processed dictionary
0725  * data. Pairing with isal_deflate_process_dict() can reduce the processing time
0726  * on subsequent compression with dictionary especially on small files.
0727  *
0728  * Like isal_deflate_set_dict(), this function is to be called after
0729  * isal_deflate_init, or after completing a SYNC_FLUSH or FULL_FLUSH and before
0730  * the next call do isal_deflate. Changing compression level between dictionary
0731  * process and reset will cause return of ISAL_INVALID_STATE.
0732  *
0733  * @param stream Structure holding state information on the compression streams.
0734  * @param dict_str: Structure with pre-processed dictionary info.
0735  * @returns COMP_OK,
0736  *          ISAL_INVALID_STATE or other (dictionary could not be reset)
0737  */
0738 int isal_deflate_reset_dict(struct isal_zstream *stream, struct isal_dict *dict_str);
0739 
0740 
0741 /**
0742  * @brief Fast data (deflate) compression for storage applications.
0743  *
0744  * The call to isal_deflate() will take data from the input buffer (updating
0745  * next_in, avail_in and write a compressed stream to the output buffer
0746  * (updating next_out and avail_out). The function returns when either the input
0747  * buffer is empty or the output buffer is full.
0748  *
0749  * On entry to isal_deflate(), next_in points to an input buffer and avail_in
0750  * indicates the length of that buffer. Similarly next_out points to an empty
0751  * output buffer and avail_out indicates the size of that buffer.
0752  *
0753  * The fields total_in and total_out start at 0 and are updated by
0754  * isal_deflate(). These reflect the total number of bytes read or written so far.
0755  *
0756  * When the last input buffer is passed in, signaled by setting the
0757  * end_of_stream, the routine will complete compression at the end of the input
0758  * buffer, as long as the output buffer is big enough.
0759  *
0760  * The compression level can be set by setting level to any value between
0761  * ISAL_DEF_MIN_LEVEL and ISAL_DEF_MAX_LEVEL. When the compression level is
0762  * ISAL_DEF_MIN_LEVEL, hufftables can be set to a table trained for the the
0763  * specific data type being compressed to achieve better compression. When a
0764  * higher compression level is desired, a larger generic memory buffer needs to
0765  * be supplied by setting level_buf and level_buf_size to represent the chunk of
0766  * memory. For level x, the suggest size for this buffer this buffer is
0767  * ISAL_DEFL_LVLx_DEFAULT. The defines ISAL_DEFL_LVLx_MIN, ISAL_DEFL_LVLx_SMALL,
0768  * ISAL_DEFL_LVLx_MEDIUM, ISAL_DEFL_LVLx_LARGE, and ISAL_DEFL_LVLx_EXTRA_LARGE
0769  * are also provided as other suggested sizes.
0770  *
0771  * The equivalent of the zlib FLUSH_SYNC operation is currently supported.
0772  * Flush types can be NO_FLUSH, SYNC_FLUSH or FULL_FLUSH. Default flush type is
0773  * NO_FLUSH. A SYNC_ OR FULL_ flush will byte align the deflate block by
0774  * appending an empty stored block once all input has been compressed, including
0775  * the buffered input. Checking that the out_buffer is not empty or that
0776  * internal_state.state = ZSTATE_NEW_HDR is sufficient to guarantee all input
0777  * has been flushed. Additionally FULL_FLUSH will ensure look back history does
0778  * not include previous blocks so new blocks are fully independent. Switching
0779  * between flush types is supported.
0780  *
0781  * If a compression dictionary is required, the dictionary can be set calling
0782  * isal_deflate_set_dictionary before calling isal_deflate.
0783  *
0784  * If the gzip_flag is set to IGZIP_GZIP, a generic gzip header and the gzip
0785  * trailer are written around the deflate compressed data. If gzip_flag is set
0786  * to IGZIP_GZIP_NO_HDR, then only the gzip trailer is written. A full-featured
0787  * header is supported by the isal_write_{gzip,zlib}_header() functions.
0788  *
0789  * @param  stream Structure holding state information on the compression streams.
0790  * @return COMP_OK (if everything is ok),
0791  *         INVALID_FLUSH (if an invalid FLUSH is selected),
0792  *         ISAL_INVALID_LEVEL (if an invalid compression level is selected),
0793  *         ISAL_INVALID_LEVEL_BUF (if the level buffer is not large enough).
0794  */
0795 int isal_deflate(struct isal_zstream *stream);
0796 
0797 
0798 /**
0799  * @brief Fast data (deflate) stateless compression for storage applications.
0800  *
0801  * Stateless (one shot) compression routine with a similar interface to
0802  * isal_deflate() but operates on entire input buffer at one time. Parameter
0803  * avail_out must be large enough to fit the entire compressed output. Max
0804  * expansion is limited to the input size plus the header size of a stored/raw
0805  * block.
0806  *
0807  * When the compression level is set to 1, unlike in isal_deflate(), level_buf
0808  * may be optionally set depending on what what performance is desired.
0809  *
0810  * For stateless the flush types NO_FLUSH and FULL_FLUSH are supported.
0811  * FULL_FLUSH will byte align the output deflate block so additional blocks can
0812  * be easily appended.
0813  *
0814  * If the gzip_flag is set to IGZIP_GZIP, a generic gzip header and the gzip
0815  * trailer are written around the deflate compressed data. If gzip_flag is set
0816  * to IGZIP_GZIP_NO_HDR, then only the gzip trailer is written.
0817  *
0818  * @param  stream Structure holding state information on the compression streams.
0819  * @return COMP_OK (if everything is ok),
0820  *         INVALID_FLUSH (if an invalid FLUSH is selected),
0821  *         ISAL_INVALID_LEVEL (if an invalid compression level is selected),
0822  *         ISAL_INVALID_LEVEL_BUF (if the level buffer is not large enough),
0823  *         STATELESS_OVERFLOW (if output buffer will not fit output).
0824  */
0825 int isal_deflate_stateless(struct isal_zstream *stream);
0826 
0827 
0828 /******************************************************************************/
0829 /* Inflate functions */
0830 /******************************************************************************/
0831 /**
0832  * @brief Initialize decompression state data structure
0833  *
0834  * @param state Structure holding state information on the compression streams.
0835  * @returns none
0836  */
0837 void isal_inflate_init(struct inflate_state *state);
0838 
0839 /**
0840  * @brief Reinitialize decompression state data structure
0841  *
0842  * @param state Structure holding state information on the compression streams.
0843  * @returns none
0844  */
0845 void isal_inflate_reset(struct inflate_state *state);
0846 
0847 /**
0848  * @brief Set decompression dictionary to use
0849  *
0850  * This function is to be called after isal_inflate_init. If the dictionary is
0851  * longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE bytes will be
0852  * used.
0853  *
0854  * @param state: Structure holding state information on the decompression stream.
0855  * @param dict: Array containing dictionary to use.
0856  * @param dict_len: Length of dict.
0857  * @returns COMP_OK,
0858  *          ISAL_INVALID_STATE (dictionary could not be set)
0859  */
0860 int isal_inflate_set_dict(struct inflate_state *state, uint8_t *dict, uint32_t dict_len);
0861 
0862 /**
0863  * @brief Read and return gzip header information
0864  *
0865  * On entry state must be initialized and next_in pointing to a gzip compressed
0866  * buffer. The buffers gz_hdr->extra, gz_hdr->name, gz_hdr->comments and the
0867  * buffer lengths must be set to record the corresponding field, or set to NULL
0868  * to disregard that gzip header information. If one of these buffers overflows,
0869  * the user can reallocate a larger buffer and call this function again to
0870  * continue reading the header information.
0871  *
0872  * @param state: Structure holding state information on the decompression stream.
0873  * @param gz_hdr: Structure to return data encoded in the gzip header
0874  * @returns ISAL_DECOMP_OK (header was successfully parsed)
0875  *          ISAL_END_INPUT (all input was parsed),
0876  *          ISAL_NAME_OVERFLOW (gz_hdr->name overflowed while parsing),
0877  *          ISAL_COMMENT_OVERFLOW (gz_hdr->comment overflowed while parsing),
0878  *          ISAL_EXTRA_OVERFLOW (gz_hdr->extra overflowed while parsing),
0879  *          ISAL_INVALID_WRAPPER (invalid gzip header found),
0880  *          ISAL_UNSUPPORTED_METHOD (deflate is not the compression method),
0881  *          ISAL_INCORRECT_CHECKSUM (gzip header checksum was incorrect)
0882  */
0883 int isal_read_gzip_header (struct inflate_state *state, struct isal_gzip_header *gz_hdr);
0884 
0885 /**
0886  * @brief Read and return zlib header information
0887  *
0888  * On entry state must be initialized and next_in pointing to a zlib compressed
0889  * buffer.
0890  *
0891  * @param state: Structure holding state information on the decompression stream.
0892  * @param zlib_hdr: Structure to return data encoded in the zlib header
0893  * @returns ISAL_DECOMP_OK (header was successfully parsed),
0894  *          ISAL_END_INPUT (all input was parsed),
0895  *          ISAL_UNSUPPORTED_METHOD (deflate is not the compression method),
0896  *          ISAL_INCORRECT_CHECKSUM (zlib header checksum was incorrect)
0897  */
0898 int isal_read_zlib_header (struct inflate_state *state, struct isal_zlib_header *zlib_hdr);
0899 
0900 /**
0901  * @brief Fast data (deflate) decompression for storage applications.
0902  *
0903  * On entry to isal_inflate(), next_in points to an input buffer and avail_in
0904  * indicates the length of that buffer. Similarly next_out points to an empty
0905  * output buffer and avail_out indicates the size of that buffer.
0906  *
0907  * The field total_out starts at 0 and is updated by isal_inflate(). This
0908  * reflects the total number of bytes written so far.
0909  *
0910  * The call to isal_inflate() will take data from the input buffer (updating
0911  * next_in, avail_in and write a decompressed stream to the output buffer
0912  * (updating next_out and avail_out). The function returns when the input buffer
0913  * is empty, the output buffer is full, invalid data is found, or in the case of
0914  * zlib formatted data if a dictionary is specified. The current state of the
0915  * decompression on exit can be read from state->block-state.
0916  *
0917  * If the crc_flag is set to ISAL_GZIP_NO_HDR the gzip crc of the output is
0918  * stored in state->crc. Alternatively, if the crc_flag is set to
0919  * ISAL_ZLIB_NO_HDR the adler32 of the output is stored in state->crc (checksum
0920  * may not be updated until decompression is complete). When the crc_flag is set
0921  * to ISAL_GZIP_NO_HDR_VER or ISAL_ZLIB_NO_HDR_VER, the behavior is the same,
0922  * except the checksum is verified with the checksum after immediately following
0923  * the deflate data. If the crc_flag is set to ISAL_GZIP or ISAL_ZLIB, the
0924  * gzip/zlib header is parsed, state->crc is set to the appropriate checksum,
0925  * and the checksum is verified. If the crc_flag is set to ISAL_DEFLATE
0926  * (default), then the data is treated as a raw deflate block.
0927  *
0928  * The element state->hist_bits has values from 0 to 15, where values of 1 to 15
0929  * are the log base 2 size of the matching window and 0 is the default with
0930  * maximum history size.
0931  *
0932  * If a dictionary is required, a call to isal_inflate_set_dict will set the
0933  * dictionary.
0934  *
0935  * @param  state Structure holding state information on the compression streams.
0936  * @return ISAL_DECOMP_OK (if everything is ok),
0937  *         ISAL_INVALID_BLOCK,
0938  *         ISAL_NEED_DICT,
0939  *         ISAL_INVALID_SYMBOL,
0940  *         ISAL_INVALID_LOOKBACK,
0941  *         ISAL_INVALID_WRAPPER,
0942  *         ISAL_UNSUPPORTED_METHOD,
0943  *         ISAL_INCORRECT_CHECKSUM.
0944  */
0945 
0946 int isal_inflate(struct inflate_state *state);
0947 
0948 /**
0949  * @brief Fast data (deflate) stateless decompression for storage applications.
0950  *
0951  * Stateless (one shot) decompression routine with a similar interface to
0952  * isal_inflate() but operates on entire input buffer at one time. Parameter
0953  * avail_out must be large enough to fit the entire decompressed
0954  * output. Dictionaries are not supported.
0955  *
0956  * @param  state Structure holding state information on the compression streams.
0957  * @return ISAL_DECOMP_OK (if everything is ok),
0958  *         ISAL_END_INPUT (if all input was decompressed),
0959  *         ISAL_NEED_DICT,
0960  *         ISAL_OUT_OVERFLOW (if output buffer ran out of space),
0961  *         ISAL_INVALID_BLOCK,
0962  *         ISAL_INVALID_SYMBOL,
0963  *         ISAL_INVALID_LOOKBACK,
0964  *         ISAL_INVALID_WRAPPER,
0965  *         ISAL_UNSUPPORTED_METHOD,
0966  *         ISAL_INCORRECT_CHECKSUM.
0967  */
0968 int isal_inflate_stateless(struct inflate_state *state);
0969 
0970 /******************************************************************************/
0971 /* Other functions */
0972 /******************************************************************************/
0973 /**
0974  * @brief Calculate Adler-32 checksum, runs appropriate version.
0975  *
0976  * This function determines what instruction sets are enabled and selects the
0977  * appropriate version at runtime.
0978  *
0979  * @param init: initial Adler-32 value
0980  * @param buf: buffer to calculate checksum on
0981  * @param len: buffer length in bytes
0982  *
0983  * @returns 32-bit Adler-32 checksum
0984  */
0985 uint32_t isal_adler32(uint32_t init, const unsigned char *buf, uint64_t len);
0986 
0987 #ifdef __cplusplus
0988 }
0989 #endif
0990 #endif  /* ifndef _IGZIP_H */