![]() |
|
|||
File indexing completed on 2025-09-17 09:02:13
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 /* When the state is set to ZSTATE_NEW_HDR or TMP_ZSTATE_NEW_HEADER, the 0176 * hufftable being used for compression may be swapped 0177 */ 0178 enum isal_zstate_state { 0179 ZSTATE_NEW_HDR, //!< Header to be written 0180 ZSTATE_HDR, //!< Header state 0181 ZSTATE_CREATE_HDR, //!< Header to be created 0182 ZSTATE_BODY, //!< Body state 0183 ZSTATE_FLUSH_READ_BUFFER, //!< Flush buffer 0184 ZSTATE_FLUSH_ICF_BUFFER, 0185 ZSTATE_TYPE0_HDR, //! Type0 block header to be written 0186 ZSTATE_TYPE0_BODY, //!< Type0 block body to be written 0187 ZSTATE_SYNC_FLUSH, //!< Write sync flush block 0188 ZSTATE_FLUSH_WRITE_BUFFER, //!< Flush bitbuf 0189 ZSTATE_TRL, //!< Trailer state 0190 ZSTATE_END, //!< End state 0191 ZSTATE_TMP_NEW_HDR, //!< Temporary Header to be written 0192 ZSTATE_TMP_HDR, //!< Temporary Header state 0193 ZSTATE_TMP_CREATE_HDR, //!< Temporary Header to be created state 0194 ZSTATE_TMP_BODY, //!< Temporary Body state 0195 ZSTATE_TMP_FLUSH_READ_BUFFER, //!< Flush buffer 0196 ZSTATE_TMP_FLUSH_ICF_BUFFER, 0197 ZSTATE_TMP_TYPE0_HDR, //! Temporary Type0 block header to be written 0198 ZSTATE_TMP_TYPE0_BODY, //!< Temporary Type0 block body to be written 0199 ZSTATE_TMP_SYNC_FLUSH, //!< Write sync flush block 0200 ZSTATE_TMP_FLUSH_WRITE_BUFFER, //!< Flush bitbuf 0201 ZSTATE_TMP_TRL, //!< Temporary Trailer state 0202 ZSTATE_TMP_END //!< Temporary End state 0203 }; 0204 0205 /* Offset used to switch between TMP states and non-tmp states */ 0206 #define ZSTATE_TMP_OFFSET ZSTATE_TMP_HDR - ZSTATE_HDR 0207 0208 /******************************************************************************/ 0209 /* Inflate Implementation Specific Defines */ 0210 /******************************************************************************/ 0211 #define ISAL_DECODE_LONG_BITS 12 0212 #define ISAL_DECODE_SHORT_BITS 10 0213 0214 /* Current state of decompression */ 0215 enum isal_block_state { 0216 ISAL_BLOCK_NEW_HDR, /* Just starting a new block */ 0217 ISAL_BLOCK_HDR, /* In the middle of reading in a block header */ 0218 ISAL_BLOCK_TYPE0, /* Decoding a type 0 block */ 0219 ISAL_BLOCK_CODED, /* Decoding a huffman coded block */ 0220 ISAL_BLOCK_INPUT_DONE, /* Decompression of input is completed */ 0221 ISAL_BLOCK_FINISH, /* Decompression of input is completed and all data has been flushed to 0222 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 /* Inflate Flags */ 0233 #define ISAL_DEFLATE 0 /* Default */ 0234 #define ISAL_GZIP 1 0235 #define ISAL_GZIP_NO_HDR 2 0236 #define ISAL_ZLIB 3 0237 #define ISAL_ZLIB_NO_HDR 4 0238 #define ISAL_ZLIB_NO_HDR_VER 5 0239 #define ISAL_GZIP_NO_HDR_VER 6 0240 0241 /* Inflate Return values */ 0242 #define ISAL_DECOMP_OK 0 /* No errors encountered while decompressing */ 0243 #define ISAL_END_INPUT 1 /* End of input reached */ 0244 #define ISAL_OUT_OVERFLOW 2 /* End of output reached */ 0245 #define ISAL_NAME_OVERFLOW 3 /* End of gzip name buffer reached */ 0246 #define ISAL_COMMENT_OVERFLOW 4 /* End of gzip name buffer reached */ 0247 #define ISAL_EXTRA_OVERFLOW 5 /* End of extra buffer reached */ 0248 #define ISAL_NEED_DICT 6 /* Stream needs a dictionary to continue */ 0249 #define ISAL_INVALID_BLOCK -1 /* Invalid deflate block found */ 0250 #define ISAL_INVALID_SYMBOL -2 /* Invalid deflate symbol found */ 0251 #define ISAL_INVALID_LOOKBACK -3 /* Invalid lookback distance found */ 0252 #define ISAL_INVALID_WRAPPER -4 /* Invalid gzip/zlib wrapper found */ 0253 #define ISAL_UNSUPPORTED_METHOD -5 /* Gzip/zlib wrapper specifies unsupported compress method */ 0254 #define ISAL_INCORRECT_CHECKSUM -6 /* Incorrect checksum found */ 0255 0256 /******************************************************************************/ 0257 /* Compression structures */ 0258 /******************************************************************************/ 0259 /** @brief Holds histogram of deflate symbols*/ 0260 struct isal_huff_histogram { 0261 uint64_t lit_len_histogram[ISAL_DEF_LIT_LEN_SYMBOLS]; //!< Histogram of Literal/Len symbols 0262 //!< 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 /** @brief Holds modified histogram */ 0268 struct isal_mod_hist { 0269 uint32_t d_hist[30]; //!< Distance 0270 uint32_t ll_hist[513]; //! Literal/length 0271 }; 0272 0273 #define ISAL_DEF_MIN_LEVEL 0 0274 #define ISAL_DEF_MAX_LEVEL 3 0275 0276 /* Defines used set level data sizes */ 0277 /* has to be at least sizeof(struct level_buf) + sizeof(struct lvlX_buf */ 0278 #define ISAL_DEF_LVL0_REQ 0 0279 #define ISAL_DEF_LVL1_REQ (4 * IGZIP_K + 2 * IGZIP_LVL1_HASH_SIZE) 0280 #define ISAL_DEF_LVL1_TOKEN_SIZE 4 0281 #define ISAL_DEF_LVL2_REQ (4 * IGZIP_K + 2 * IGZIP_LVL2_HASH_SIZE) 0282 #define ISAL_DEF_LVL2_TOKEN_SIZE 4 0283 #define ISAL_DEF_LVL3_REQ 4 * IGZIP_K + 4 * 4 * IGZIP_K + 2 * IGZIP_LVL3_HASH_SIZE 0284 #define ISAL_DEF_LVL3_TOKEN_SIZE 4 0285 0286 /* Data sizes for level specific data options */ 0287 #define ISAL_DEF_LVL0_MIN ISAL_DEF_LVL0_REQ 0288 #define ISAL_DEF_LVL0_SMALL ISAL_DEF_LVL0_REQ 0289 #define ISAL_DEF_LVL0_MEDIUM ISAL_DEF_LVL0_REQ 0290 #define ISAL_DEF_LVL0_LARGE ISAL_DEF_LVL0_REQ 0291 #define ISAL_DEF_LVL0_EXTRA_LARGE ISAL_DEF_LVL0_REQ 0292 #define ISAL_DEF_LVL0_DEFAULT ISAL_DEF_LVL0_REQ 0293 0294 #define ISAL_DEF_LVL1_MIN (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 1 * IGZIP_K) 0295 #define ISAL_DEF_LVL1_SMALL (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 16 * IGZIP_K) 0296 #define ISAL_DEF_LVL1_MEDIUM (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 32 * IGZIP_K) 0297 #define ISAL_DEF_LVL1_LARGE (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 64 * IGZIP_K) 0298 #define ISAL_DEF_LVL1_EXTRA_LARGE (ISAL_DEF_LVL1_REQ + ISAL_DEF_LVL1_TOKEN_SIZE * 128 * IGZIP_K) 0299 #define ISAL_DEF_LVL1_DEFAULT ISAL_DEF_LVL1_LARGE 0300 0301 #define ISAL_DEF_LVL2_MIN (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 1 * IGZIP_K) 0302 #define ISAL_DEF_LVL2_SMALL (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 16 * IGZIP_K) 0303 #define ISAL_DEF_LVL2_MEDIUM (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 32 * IGZIP_K) 0304 #define ISAL_DEF_LVL2_LARGE (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 64 * IGZIP_K) 0305 #define ISAL_DEF_LVL2_EXTRA_LARGE (ISAL_DEF_LVL2_REQ + ISAL_DEF_LVL2_TOKEN_SIZE * 128 * IGZIP_K) 0306 #define ISAL_DEF_LVL2_DEFAULT ISAL_DEF_LVL2_LARGE 0307 0308 #define ISAL_DEF_LVL3_MIN (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 1 * IGZIP_K) 0309 #define ISAL_DEF_LVL3_SMALL (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 16 * IGZIP_K) 0310 #define ISAL_DEF_LVL3_MEDIUM (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 32 * IGZIP_K) 0311 #define ISAL_DEF_LVL3_LARGE (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 64 * IGZIP_K) 0312 #define ISAL_DEF_LVL3_EXTRA_LARGE (ISAL_DEF_LVL3_REQ + ISAL_DEF_LVL3_TOKEN_SIZE * 128 * IGZIP_K) 0313 #define ISAL_DEF_LVL3_DEFAULT ISAL_DEF_LVL3_LARGE 0314 0315 #define IGZIP_NO_HIST 0 0316 #define IGZIP_HIST 1 0317 #define IGZIP_DICT_HIST 2 0318 #define IGZIP_DICT_HASH_SET 3 0319 0320 /** @brief Holds Bit Buffer information*/ 0321 struct BitBuf2 { 0322 uint64_t m_bits; //!< bits in the bit buffer 0323 uint32_t m_bit_count; //!< number of valid bits in the bit buffer 0324 uint8_t *m_out_buf; //!< current index of buffer to write to 0325 uint8_t *m_out_end; //!< end of buffer to write to 0326 uint8_t *m_out_start; //!< start of buffer to write to 0327 }; 0328 0329 /** @brief Holds Zlib header information */ 0330 struct isal_zlib_header { 0331 uint32_t info; //!< base-2 logarithm of the LZ77 window size minus 8 0332 uint32_t level; //!< Compression level (fastest, fast, default, maximum) 0333 uint32_t dict_id; //!< Dictionary id 0334 uint32_t dict_flag; //!< Whether to use a dictionary 0335 }; 0336 0337 /** @brief Holds Gzip header information */ 0338 struct isal_gzip_header { 0339 uint32_t text; //!< Optional Text hint 0340 uint32_t time; //!< Unix modification time in gzip header 0341 uint32_t xflags; //!< xflags in gzip header 0342 uint32_t os; //!< OS in gzip header 0343 uint8_t *extra; //!< Extra field in gzip header 0344 uint32_t extra_buf_len; //!< Length of extra buffer 0345 uint32_t extra_len; //!< Actual length of gzip header extra field 0346 char *name; //!< Name in gzip header 0347 uint32_t name_buf_len; //!< Length of name buffer 0348 char *comment; //!< Comments in gzip header 0349 uint32_t comment_buf_len; //!< Length of comment buffer 0350 uint32_t hcrc; //!< Header crc or header crc flag 0351 uint32_t flags; //!< Internal data 0352 }; 0353 0354 /* Variable prefixes: 0355 * b_ : Measured wrt the start of the buffer 0356 * f_ : Measured wrt the start of the file (aka file_start) 0357 */ 0358 0359 /** @brief Holds the internal state information for input and output compression streams*/ 0360 struct isal_zstate { 0361 uint32_t total_in_start; //!< Not used, may be replaced with something else 0362 uint32_t block_next; //!< Start of current deflate block in the input 0363 uint32_t block_end; //!< End of current deflate block in the input 0364 uint32_t dist_mask; //!< Distance mask used. 0365 uint32_t hash_mask; 0366 enum isal_zstate_state state; //!< Current state in processing the data stream 0367 struct BitBuf2 bitbuf; //!< Bit Buffer 0368 uint32_t crc; //!< Current checksum without finalize step if any (adler) 0369 uint8_t has_wrap_hdr; //!< keeps track of wrapper header 0370 uint8_t has_eob_hdr; //!< keeps track of eob hdr (with BFINAL set) 0371 uint8_t has_eob; //!< keeps track of eob on the last deflate block 0372 uint8_t has_hist; //!< flag to track if there is match history 0373 uint16_t 0374 has_level_buf_init; //!< flag to track if user supplied memory has been initialized. 0375 uint32_t count; //!< used for partial header/trailer writes 0376 uint8_t tmp_out_buff[16]; //!< temporary array 0377 uint32_t tmp_out_start; //!< temporary variable 0378 uint32_t tmp_out_end; //!< temporary variable 0379 uint32_t b_bytes_valid; //!< number of valid bytes in buffer 0380 uint32_t b_bytes_processed; //!< number of bytes processed in buffer 0381 uint8_t buffer[2 * IGZIP_HIST_SIZE + ISAL_LOOK_AHEAD]; //!< Internal buffer 0382 0383 /* Stream should be setup such that the head is cache aligned*/ 0384 uint16_t head[IGZIP_LVL0_HASH_SIZE]; //!< Hash array 0385 }; 0386 0387 /** @brief Holds the huffman tree used to huffman encode the input stream **/ 0388 struct isal_hufftables { 0389 0390 uint8_t deflate_hdr[ISAL_DEF_MAX_HDR_SIZE]; //!< deflate huffman tree header 0391 uint32_t deflate_hdr_count; //!< Number of whole bytes in deflate_huff_hdr 0392 uint32_t deflate_hdr_extra_bits; //!< Number of bits in the partial byte in header 0393 uint32_t dist_table[IGZIP_DIST_TABLE_SIZE]; //!< bits 4:0 are the code length, bits 31:5 are 0394 //!< the code 0395 uint32_t len_table[IGZIP_LEN_TABLE_SIZE]; //!< bits 4:0 are the code length, bits 31:5 are 0396 //!< the code 0397 uint16_t lit_table[IGZIP_LIT_TABLE_SIZE]; //!< literal code 0398 uint8_t lit_table_sizes[IGZIP_LIT_TABLE_SIZE]; //!< literal code length 0399 uint16_t dcodes[30 - IGZIP_DECODE_OFFSET]; //!< distance code 0400 uint8_t dcodes_sizes[30 - IGZIP_DECODE_OFFSET]; //!< distance code length 0401 }; 0402 0403 /** @brief Holds stream information*/ 0404 struct isal_zstream { 0405 uint8_t *next_in; //!< Next input byte 0406 uint32_t avail_in; //!< number of bytes available at next_in 0407 uint32_t total_in; //!< total number of bytes read so far 0408 0409 uint8_t *next_out; //!< Next output byte 0410 uint32_t avail_out; //!< number of bytes available at next_out 0411 uint32_t total_out; //!< total number of bytes written so far 0412 0413 struct isal_hufftables *hufftables; //!< Huffman encoding used when compressing 0414 uint32_t level; //!< Compression level to use 0415 uint32_t level_buf_size; //!< Size of level_buf 0416 uint8_t *level_buf; //!< User allocated buffer required for different compression levels 0417 uint16_t end_of_stream; //!< non-zero if this is the last input buffer 0418 uint16_t flush; //!< Flush type can be NO_FLUSH, SYNC_FLUSH or FULL_FLUSH 0419 uint16_t gzip_flag; //!< Indicate if gzip compression is to be performed 0420 uint16_t hist_bits; //!< Log base 2 of maximum lookback distance, 0 is use default 0421 struct isal_zstate internal_state; //!< Internal state for this stream 0422 }; 0423 0424 /******************************************************************************/ 0425 /* Inflate structures */ 0426 /******************************************************************************/ 0427 /* 0428 * Inflate_huff_code data structures are used to store a Huffman code for fast 0429 * lookup. It works by performing a lookup in short_code_lookup that hopefully 0430 * yields the correct symbol. Otherwise a lookup into long_code_lookup is 0431 * performed to find the correct symbol. The details of how this works follows: 0432 * 0433 * Let i be some index into short_code_lookup and let e be the associated 0434 * element. Bit 15 in e is a flag. If bit 15 is not set, then index i contains 0435 * a Huffman code for a symbol which has length at most DECODE_LOOKUP_SIZE. Bits 0436 * 0 through 8 are the symbol associated with that code and bits 9 through 12 of 0437 * e represent the number of bits in the code. If bit 15 is set, the i 0438 * corresponds to the first DECODE_LOOKUP_SIZE bits of a Huffman code which has 0439 * length longer than DECODE_LOOKUP_SIZE. In this case, bits 0 through 8 0440 * represent an offset into long_code_lookup table and bits 9 through 12 0441 * represent the maximum length of a Huffman code starting with the bits in the 0442 * index i. The offset into long_code_lookup is for an array associated with all 0443 * codes which start with the bits in i. 0444 * 0445 * The elements of long_code_lookup are in the same format as short_code_lookup, 0446 * except bit 15 is never set. Let i be a number made up of DECODE_LOOKUP_SIZE 0447 * bits. Then all Huffman codes which start with DECODE_LOOKUP_SIZE bits are 0448 * stored in an array starting at index h in long_code_lookup. This index h is 0449 * stored in bits 0 through 9 at index i in short_code_lookup. The index j is an 0450 * index of this array if the number of bits contained in j and i is the number 0451 * of bits in the longest huff_code starting with the bits of i. The symbol 0452 * stored at index j is the symbol whose huffcode can be found in (j << 0453 * DECODE_LOOKUP_SIZE) | i. Note these arrays will be stored sorted in order of 0454 * maximum Huffman code length. 0455 * 0456 * The following are explanations for sizes of the tables: 0457 * 0458 * Since short_code_lookup is a lookup on DECODE_LOOKUP_SIZE bits, it must have 0459 * size 2^DECODE_LOOKUP_SIZE. 0460 * 0461 * To determine the amount of memory required for long_code_lookup, note that 0462 * any element of long_code_lookup corresponds to a code, a duplicate of an 0463 * existing code, or a invalid code. Since deflate Huffman are stored such that 0464 * the code size and the code value form an increasing function, the number of 0465 * duplicates is maximized when all the duplicates are contained in a single 0466 * array, thus there are at most 2^(15 - DECODE_LOOKUP_SIZE) - 0467 * (DECODE_LOOKUP_SIZE + 1) duplicate elements. Similarly the number of invalid 0468 * elements is maximized at 2^(15 - DECODE_LOOKUP_SIZE) - 2^(floor((15 - 0469 * DECODE_LOOKUP_SIZE)/2) - 2^(ceil((15 - DECODE_LOOKUP_SIZE)/2) + 1. Thus the 0470 * amount of memory required is: NUM_CODES + 2^(16 - DECODE_LOOKUP_SIZE) - 0471 * (DECODE_LOOKUP_SIZE + 1) - 2^(floor((15 - DECODE_LOOKUP_SIZE)/2) - 0472 * 2^(ceil((15 - DECODE_LOOKUP_SIZE)/2) + 1. The values used below are those 0473 * values rounded up to the nearest 16 byte boundary 0474 * 0475 * Note that DECODE_LOOKUP_SIZE can be any length even though the offset in 0476 * small_lookup_code is 9 bits long because the increasing relationship between 0477 * code length and code value forces the maximum offset to be less than 288. 0478 */ 0479 0480 /* In the following defines, L stands for LARGE and S for SMALL */ 0481 #define ISAL_L_REM (21 - ISAL_DECODE_LONG_BITS) 0482 #define ISAL_S_REM (15 - ISAL_DECODE_SHORT_BITS) 0483 0484 #define ISAL_L_DUP ((1 << ISAL_L_REM) - (ISAL_L_REM + 1)) 0485 #define ISAL_S_DUP ((1 << ISAL_S_REM) - (ISAL_S_REM + 1)) 0486 0487 #define ISAL_L_UNUSED \ 0488 ((1 << ISAL_L_REM) - (1 << ((ISAL_L_REM) / 2)) - (1 << ((ISAL_L_REM + 1) / 2)) + 1) 0489 #define ISAL_S_UNUSED \ 0490 ((1 << ISAL_S_REM) - (1 << ((ISAL_S_REM) / 2)) - (1 << ((ISAL_S_REM + 1) / 2)) + 1) 0491 0492 #define ISAL_L_SIZE (ISAL_DEF_LIT_LEN_SYMBOLS + ISAL_L_DUP + ISAL_L_UNUSED) 0493 #define ISAL_S_SIZE (ISAL_DEF_DIST_SYMBOLS + ISAL_S_DUP + ISAL_S_UNUSED) 0494 0495 #define ISAL_HUFF_CODE_LARGE_LONG_ALIGNED (ISAL_L_SIZE + (-ISAL_L_SIZE & 0xf)) 0496 #define ISAL_HUFF_CODE_SMALL_LONG_ALIGNED (ISAL_S_SIZE + (-ISAL_S_SIZE & 0xf)) 0497 0498 /** @brief Large lookup table for decoding huffman codes */ 0499 struct inflate_huff_code_large { 0500 uint32_t short_code_lookup[1 << (ISAL_DECODE_LONG_BITS)]; //!< Short code lookup table 0501 uint16_t long_code_lookup[ISAL_HUFF_CODE_LARGE_LONG_ALIGNED]; //!< Long code lookup table 0502 }; 0503 0504 /** @brief Small lookup table for decoding huffman codes */ 0505 struct inflate_huff_code_small { 0506 uint16_t short_code_lookup[1 << (ISAL_DECODE_SHORT_BITS)]; //!< Short code lookup table 0507 uint16_t long_code_lookup[ISAL_HUFF_CODE_SMALL_LONG_ALIGNED]; //!< Long code lookup table 0508 }; 0509 0510 /** @brief Holds decompression state information*/ 0511 struct inflate_state { 0512 uint8_t *next_out; //!< Next output Byte 0513 uint32_t avail_out; //!< Number of bytes available at next_out 0514 uint32_t total_out; //!< Total bytes written out so far 0515 uint8_t *next_in; //!< Next input byte 0516 uint64_t read_in; //!< Bits buffered to handle unaligned streams 0517 uint32_t avail_in; //!< Number of bytes available at next_in 0518 int32_t read_in_length; //!< Bits in read_in 0519 struct inflate_huff_code_large lit_huff_code; //!< Structure for decoding lit/len symbols 0520 struct inflate_huff_code_small dist_huff_code; //!< Structure for decoding dist symbols 0521 enum isal_block_state block_state; //!< Current decompression state 0522 uint32_t dict_length; //!< Length of dictionary used 0523 uint32_t bfinal; //!< Flag identifying final block 0524 uint32_t crc_flag; //!< Flag identifying whether to track of crc 0525 uint32_t crc; //!< Contains crc or adler32 of output if crc_flag is set 0526 uint32_t hist_bits; //!< Log base 2 of maximum lookback distance 0527 union { 0528 int32_t type0_block_len; //!< Length left to read of type 0 block when outbuffer 0529 //!< overflow occurred 0530 int32_t count; //!< Count of bytes remaining to be parsed 0531 uint32_t dict_id; 0532 }; 0533 int32_t write_overflow_lits; 0534 int32_t write_overflow_len; 0535 int32_t copy_overflow_length; //!< Length left to copy when outbuffer overflow occurred 0536 int32_t copy_overflow_distance; //!< Lookback distance when outbuffer overflow occurred 0537 int16_t wrapper_flag; 0538 int16_t tmp_in_size; //!< Number of bytes in tmp_in_buffer 0539 int32_t tmp_out_valid; //!< Number of bytes in tmp_out_buffer 0540 int32_t tmp_out_processed; //!< Number of bytes processed in tmp_out_buffer 0541 uint8_t tmp_in_buffer[ISAL_DEF_MAX_HDR_SIZE]; //!< Temporary buffer containing data from the 0542 //!< input stream 0543 uint8_t tmp_out_buffer[2 * ISAL_DEF_HIST_SIZE + 0544 ISAL_LOOK_AHEAD]; //!< Temporary buffer containing data from the 0545 //!< output stream 0546 }; 0547 0548 /******************************************************************************/ 0549 /* Compression functions */ 0550 /******************************************************************************/ 0551 /** 0552 * @brief Updates histograms to include the symbols found in the input 0553 * stream. Since this function only updates the histograms, it can be called on 0554 * multiple streams to get a histogram better representing the desired data 0555 * set. When first using histogram it must be initialized by zeroing the 0556 * structure. 0557 * 0558 * @param in_stream: Input stream of data. 0559 * @param length: The length of start_stream. 0560 * @param histogram: The returned histogram of lit/len/dist symbols. 0561 */ 0562 void 0563 isal_update_histogram(uint8_t *in_stream, int length, struct isal_huff_histogram *histogram); 0564 0565 /** 0566 * @brief Creates a custom huffman code for the given histograms in which 0567 * every literal and repeat length is assigned a code and all possible lookback 0568 * distances are assigned a code. 0569 * 0570 * @param hufftables: the output structure containing the huffman code 0571 * @param histogram: histogram containing frequency of literal symbols, 0572 * repeat lengths and lookback distances 0573 * @returns Returns a non zero value if an invalid huffman code was created. 0574 */ 0575 int 0576 isal_create_hufftables(struct isal_hufftables *hufftables, struct isal_huff_histogram *histogram); 0577 0578 /** 0579 * @brief Creates a custom huffman code for the given histograms like 0580 * isal_create_hufftables() except literals with 0 frequency in the histogram 0581 * are not assigned a code 0582 * 0583 * @param hufftables: the output structure containing the huffman code 0584 * @param histogram: histogram containing frequency of literal symbols, 0585 * repeat lengths and lookback distances 0586 * @returns Returns a non zero value if an invalid huffman code was created. 0587 */ 0588 int 0589 isal_create_hufftables_subset(struct isal_hufftables *hufftables, 0590 struct isal_huff_histogram *histogram); 0591 0592 /** 0593 * @brief Initialize compression stream data structure 0594 * 0595 * @param stream Structure holding state information on the compression streams. 0596 * @returns none 0597 */ 0598 void 0599 isal_deflate_init(struct isal_zstream *stream); 0600 0601 /** 0602 * @brief Reinitialize compression stream data structure. Performs the same 0603 * action as isal_deflate_init, but does not change user supplied input such as 0604 * the level, flush type, compression wrapper (like gzip), hufftables, and 0605 * end_of_stream_flag. 0606 * 0607 * @param stream Structure holding state information on the compression streams. 0608 * @returns none 0609 */ 0610 void 0611 isal_deflate_reset(struct isal_zstream *stream); 0612 0613 /** 0614 * @brief Set gzip header default values 0615 * 0616 * @param gz_hdr: Gzip header to initialize. 0617 */ 0618 void 0619 isal_gzip_header_init(struct isal_gzip_header *gz_hdr); 0620 0621 /** 0622 * @brief Set zlib header default values 0623 * 0624 * @param z_hdr: zlib header to initialize. 0625 */ 0626 void 0627 isal_zlib_header_init(struct isal_zlib_header *z_hdr); 0628 0629 /** 0630 * @brief Write gzip header to output stream 0631 * 0632 * Writes the gzip header to the output stream. On entry this function assumes 0633 * that the output buffer has been initialized, so stream->next_out, 0634 * stream->avail_out and stream->total_out have been set. If the output buffer 0635 * contains insufficient space, stream is not modified. 0636 * 0637 * @param stream: Structure holding state information on the compression stream. 0638 * @param gz_hdr: Structure holding the gzip header information to encode. 0639 * 0640 * @returns Returns 0 if the header is successfully written, otherwise returns 0641 * the minimum size required to successfully write the gzip header to the output 0642 * buffer. 0643 */ 0644 uint32_t 0645 isal_write_gzip_header(struct isal_zstream *stream, struct isal_gzip_header *gz_hdr); 0646 0647 /** 0648 * @brief Write zlib header to output stream 0649 * 0650 * Writes the zlib header to the output stream. On entry this function assumes 0651 * that the output buffer has been initialized, so stream->next_out, 0652 * stream->avail_out and stream->total_out have been set. If the output buffer 0653 * contains insufficient space, stream is not modified. 0654 * 0655 * @param stream: Structure holding state information on the compression stream. 0656 * @param z_hdr: Structure holding the zlib header information to encode. 0657 * 0658 * @returns Returns 0 if the header is successfully written, otherwise returns 0659 * the minimum size required to successfully write the zlib header to the output 0660 * buffer. 0661 */ 0662 uint32_t 0663 isal_write_zlib_header(struct isal_zstream *stream, struct isal_zlib_header *z_hdr); 0664 0665 /** 0666 * @brief Set stream to use a new Huffman code 0667 * 0668 * Sets the Huffman code to be used in compression before compression start or 0669 * after the successful completion of a SYNC_FLUSH or FULL_FLUSH. If type has 0670 * value IGZIP_HUFFTABLE_DEFAULT, the stream is set to use the default Huffman 0671 * code. If type has value IGZIP_HUFFTABLE_STATIC, the stream is set to use the 0672 * deflate standard static Huffman code, or if type has value 0673 * IGZIP_HUFFTABLE_CUSTOM, the stream is set to sue the isal_hufftables 0674 * structure input to isal_deflate_set_hufftables. 0675 * 0676 * @param stream: Structure holding state information on the compression stream. 0677 * @param hufftables: new huffman code to use if type is set to 0678 * IGZIP_HUFFTABLE_CUSTOM. 0679 * @param type: Flag specifying what hufftable to use. 0680 * 0681 * @returns Returns INVALID_OPERATION if the stream was unmodified. This may be 0682 * due to the stream being in a state where changing the huffman code is not 0683 * allowed or an invalid input is provided. 0684 */ 0685 int 0686 isal_deflate_set_hufftables(struct isal_zstream *stream, struct isal_hufftables *hufftables, 0687 int type); 0688 0689 /** 0690 * @brief Initialize compression stream data structure 0691 * 0692 * @param stream Structure holding state information on the compression streams. 0693 * @returns none 0694 */ 0695 void 0696 isal_deflate_stateless_init(struct isal_zstream *stream); 0697 0698 /** 0699 * @brief Set compression dictionary to use 0700 * 0701 * This function is to be called after isal_deflate_init, or after completing a 0702 * SYNC_FLUSH or FULL_FLUSH and before the next call do isal_deflate. If the 0703 * dictionary is longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE 0704 * bytes will be used. 0705 * 0706 * @param stream Structure holding state information on the compression streams. 0707 * @param dict: Array containing dictionary to use. 0708 * @param dict_len: Length of dict. 0709 * @returns COMP_OK, 0710 * ISAL_INVALID_STATE (dictionary could not be set) 0711 */ 0712 int 0713 isal_deflate_set_dict(struct isal_zstream *stream, uint8_t *dict, uint32_t dict_len); 0714 0715 /** @brief Structure for holding processed dictionary information */ 0716 0717 struct isal_dict { 0718 uint32_t params; 0719 uint32_t level; 0720 uint32_t hist_size; 0721 uint32_t hash_size; 0722 uint8_t history[ISAL_DEF_HIST_SIZE]; 0723 uint16_t hashtable[IGZIP_LVL3_HASH_SIZE]; 0724 }; 0725 0726 /** 0727 * @brief Process dictionary to reuse later 0728 * 0729 * Processes a dictionary so that the generated output can be reused to reset a 0730 * new deflate stream more quickly than isal_deflate_set_dict() alone. This 0731 * function is paired with isal_deflate_reset_dict() when using the same 0732 * dictionary on multiple deflate objects. The stream.level must be set prior to 0733 * calling this function to process the dictionary correctly. If the dictionary 0734 * is longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE bytes will be 0735 * used. 0736 * 0737 * @param stream Structure holding state information on the compression streams. 0738 * @param dict_str: Structure to hold processed dictionary info to reuse later. 0739 * @param dict: Array containing dictionary to use. 0740 * @param dict_len: Length of dict. 0741 * @returns COMP_OK, 0742 * ISAL_INVALID_STATE (dictionary could not be processed) 0743 */ 0744 int 0745 isal_deflate_process_dict(struct isal_zstream *stream, struct isal_dict *dict_str, uint8_t *dict, 0746 uint32_t dict_len); 0747 0748 /** 0749 * @brief Reset compression dictionary to use 0750 * 0751 * Similar to isal_deflate_set_dict() but on pre-processed dictionary 0752 * data. Pairing with isal_deflate_process_dict() can reduce the processing time 0753 * on subsequent compression with dictionary especially on small files. 0754 * 0755 * Like isal_deflate_set_dict(), this function is to be called after 0756 * isal_deflate_init, or after completing a SYNC_FLUSH or FULL_FLUSH and before 0757 * the next call do isal_deflate. Changing compression level between dictionary 0758 * process and reset will cause return of ISAL_INVALID_STATE. 0759 * 0760 * @param stream Structure holding state information on the compression streams. 0761 * @param dict_str: Structure with pre-processed dictionary info. 0762 * @returns COMP_OK, 0763 * ISAL_INVALID_STATE or other (dictionary could not be reset) 0764 */ 0765 int 0766 isal_deflate_reset_dict(struct isal_zstream *stream, struct isal_dict *dict_str); 0767 0768 /** 0769 * @brief Fast data (deflate) compression for storage applications. 0770 * 0771 * The call to isal_deflate() will take data from the input buffer (updating 0772 * next_in, avail_in and write a compressed stream to the output buffer 0773 * (updating next_out and avail_out). The function returns when either the input 0774 * buffer is empty or the output buffer is full. 0775 * 0776 * On entry to isal_deflate(), next_in points to an input buffer and avail_in 0777 * indicates the length of that buffer. Similarly next_out points to an empty 0778 * output buffer and avail_out indicates the size of that buffer. 0779 * 0780 * The fields total_in and total_out start at 0 and are updated by 0781 * isal_deflate(). These reflect the total number of bytes read or written so far. 0782 * 0783 * When the last input buffer is passed in, signaled by setting the 0784 * end_of_stream, the routine will complete compression at the end of the input 0785 * buffer, as long as the output buffer is big enough. 0786 * 0787 * The compression level can be set by setting level to any value between 0788 * ISAL_DEF_MIN_LEVEL and ISAL_DEF_MAX_LEVEL. When the compression level is 0789 * ISAL_DEF_MIN_LEVEL, hufftables can be set to a table trained for the the 0790 * specific data type being compressed to achieve better compression. When a 0791 * higher compression level is desired, a larger generic memory buffer needs to 0792 * be supplied by setting level_buf and level_buf_size to represent the chunk of 0793 * memory. For level x, the suggest size for this buffer this buffer is 0794 * ISAL_DEFL_LVLx_DEFAULT. The defines ISAL_DEFL_LVLx_MIN, ISAL_DEFL_LVLx_SMALL, 0795 * ISAL_DEFL_LVLx_MEDIUM, ISAL_DEFL_LVLx_LARGE, and ISAL_DEFL_LVLx_EXTRA_LARGE 0796 * are also provided as other suggested sizes. 0797 * 0798 * The equivalent of the zlib FLUSH_SYNC operation is currently supported. 0799 * Flush types can be NO_FLUSH, SYNC_FLUSH or FULL_FLUSH. Default flush type is 0800 * NO_FLUSH. A SYNC_ OR FULL_ flush will byte align the deflate block by 0801 * appending an empty stored block once all input has been compressed, including 0802 * the buffered input. Checking that the out_buffer is not empty or that 0803 * internal_state.state = ZSTATE_NEW_HDR is sufficient to guarantee all input 0804 * has been flushed. Additionally FULL_FLUSH will ensure look back history does 0805 * not include previous blocks so new blocks are fully independent. Switching 0806 * between flush types is supported. 0807 * 0808 * If a compression dictionary is required, the dictionary can be set calling 0809 * isal_deflate_set_dictionary before calling isal_deflate. 0810 * 0811 * If the gzip_flag is set to IGZIP_GZIP, a generic gzip header and the gzip 0812 * trailer are written around the deflate compressed data. If gzip_flag is set 0813 * to IGZIP_GZIP_NO_HDR, then only the gzip trailer is written. A full-featured 0814 * header is supported by the isal_write_{gzip,zlib}_header() functions. 0815 * 0816 * @param stream Structure holding state information on the compression streams. 0817 * @return COMP_OK (if everything is ok), 0818 * INVALID_FLUSH (if an invalid FLUSH is selected), 0819 * ISAL_INVALID_LEVEL (if an invalid compression level is selected), 0820 * ISAL_INVALID_LEVEL_BUF (if the level buffer is not large enough). 0821 */ 0822 int 0823 isal_deflate(struct isal_zstream *stream); 0824 0825 /** 0826 * @brief Fast data (deflate) stateless compression for storage applications. 0827 * 0828 * Stateless (one shot) compression routine with a similar interface to 0829 * isal_deflate() but operates on entire input buffer at one time. Parameter 0830 * avail_out must be large enough to fit the entire compressed output. Max 0831 * expansion is limited to the input size plus the header size of a stored/raw 0832 * block. 0833 * 0834 * When the compression level is set to 1, unlike in isal_deflate(), level_buf 0835 * may be optionally set depending on what what performance is desired. 0836 * 0837 * For stateless the flush types NO_FLUSH and FULL_FLUSH are supported. 0838 * FULL_FLUSH will byte align the output deflate block so additional blocks can 0839 * be easily appended. 0840 * 0841 * If the gzip_flag is set to IGZIP_GZIP, a generic gzip header and the gzip 0842 * trailer are written around the deflate compressed data. If gzip_flag is set 0843 * to IGZIP_GZIP_NO_HDR, then only the gzip trailer is written. 0844 * 0845 * @param stream Structure holding state information on the compression streams. 0846 * @return COMP_OK (if everything is ok), 0847 * INVALID_FLUSH (if an invalid FLUSH is selected), 0848 * ISAL_INVALID_LEVEL (if an invalid compression level is selected), 0849 * ISAL_INVALID_LEVEL_BUF (if the level buffer is not large enough), 0850 * STATELESS_OVERFLOW (if output buffer will not fit output). 0851 */ 0852 int 0853 isal_deflate_stateless(struct isal_zstream *stream); 0854 0855 /******************************************************************************/ 0856 /* Inflate functions */ 0857 /******************************************************************************/ 0858 /** 0859 * @brief Initialize decompression state data structure 0860 * 0861 * @param state Structure holding state information on the compression streams. 0862 * @returns none 0863 */ 0864 void 0865 isal_inflate_init(struct inflate_state *state); 0866 0867 /** 0868 * @brief Reinitialize decompression state data structure 0869 * 0870 * @param state Structure holding state information on the compression streams. 0871 * @returns none 0872 */ 0873 void 0874 isal_inflate_reset(struct inflate_state *state); 0875 0876 /** 0877 * @brief Set decompression dictionary to use 0878 * 0879 * This function is to be called after isal_inflate_init. If the dictionary is 0880 * longer than IGZIP_HIST_SIZE, only the last IGZIP_HIST_SIZE bytes will be 0881 * used. 0882 * 0883 * @param state: Structure holding state information on the decompression stream. 0884 * @param dict: Array containing dictionary to use. 0885 * @param dict_len: Length of dict. 0886 * @returns COMP_OK, 0887 * ISAL_INVALID_STATE (dictionary could not be set) 0888 */ 0889 int 0890 isal_inflate_set_dict(struct inflate_state *state, uint8_t *dict, uint32_t dict_len); 0891 0892 /** 0893 * @brief Read and return gzip header information 0894 * 0895 * On entry state must be initialized and next_in pointing to a gzip compressed 0896 * buffer. The buffers gz_hdr->extra, gz_hdr->name, gz_hdr->comments and the 0897 * buffer lengths must be set to record the corresponding field, or set to NULL 0898 * to disregard that gzip header information. If one of these buffers overflows, 0899 * the user can reallocate a larger buffer and call this function again to 0900 * continue reading the header information. 0901 * 0902 * @param state: Structure holding state information on the decompression stream. 0903 * @param gz_hdr: Structure to return data encoded in the gzip header 0904 * @returns ISAL_DECOMP_OK (header was successfully parsed) 0905 * ISAL_END_INPUT (all input was parsed), 0906 * ISAL_NAME_OVERFLOW (gz_hdr->name overflowed while parsing), 0907 * ISAL_COMMENT_OVERFLOW (gz_hdr->comment overflowed while parsing), 0908 * ISAL_EXTRA_OVERFLOW (gz_hdr->extra overflowed while parsing), 0909 * ISAL_INVALID_WRAPPER (invalid gzip header found), 0910 * ISAL_UNSUPPORTED_METHOD (deflate is not the compression method), 0911 * ISAL_INCORRECT_CHECKSUM (gzip header checksum was incorrect) 0912 */ 0913 int 0914 isal_read_gzip_header(struct inflate_state *state, struct isal_gzip_header *gz_hdr); 0915 0916 /** 0917 * @brief Read and return zlib header information 0918 * 0919 * On entry state must be initialized and next_in pointing to a zlib compressed 0920 * buffer. 0921 * 0922 * @param state: Structure holding state information on the decompression stream. 0923 * @param zlib_hdr: Structure to return data encoded in the zlib header 0924 * @returns ISAL_DECOMP_OK (header was successfully parsed), 0925 * ISAL_END_INPUT (all input was parsed), 0926 * ISAL_UNSUPPORTED_METHOD (deflate is not the compression method), 0927 * ISAL_INCORRECT_CHECKSUM (zlib header checksum was incorrect) 0928 */ 0929 int 0930 isal_read_zlib_header(struct inflate_state *state, struct isal_zlib_header *zlib_hdr); 0931 0932 /** 0933 * @brief Fast data (deflate) decompression for storage applications. 0934 * 0935 * On entry to isal_inflate(), next_in points to an input buffer and avail_in 0936 * indicates the length of that buffer. Similarly next_out points to an empty 0937 * output buffer and avail_out indicates the size of that buffer. 0938 * 0939 * The field total_out starts at 0 and is updated by isal_inflate(). This 0940 * reflects the total number of bytes written so far. 0941 * 0942 * The call to isal_inflate() will take data from the input buffer (updating 0943 * next_in, avail_in and write a decompressed stream to the output buffer 0944 * (updating next_out and avail_out). The function returns when the input buffer 0945 * is empty, the output buffer is full, invalid data is found, or in the case of 0946 * zlib formatted data if a dictionary is specified. The current state of the 0947 * decompression on exit can be read from state->block-state. 0948 * 0949 * If the crc_flag is set to ISAL_GZIP_NO_HDR the gzip crc of the output is 0950 * stored in state->crc. Alternatively, if the crc_flag is set to 0951 * ISAL_ZLIB_NO_HDR the adler32 of the output is stored in state->crc (checksum 0952 * may not be updated until decompression is complete). When the crc_flag is set 0953 * to ISAL_GZIP_NO_HDR_VER or ISAL_ZLIB_NO_HDR_VER, the behavior is the same, 0954 * except the checksum is verified with the checksum after immediately following 0955 * the deflate data. If the crc_flag is set to ISAL_GZIP or ISAL_ZLIB, the 0956 * gzip/zlib header is parsed, state->crc is set to the appropriate checksum, 0957 * and the checksum is verified. If the crc_flag is set to ISAL_DEFLATE 0958 * (default), then the data is treated as a raw deflate block. 0959 * 0960 * The element state->hist_bits has values from 0 to 15, where values of 1 to 15 0961 * are the log base 2 size of the matching window and 0 is the default with 0962 * maximum history size. 0963 * 0964 * If a dictionary is required, a call to isal_inflate_set_dict will set the 0965 * dictionary. 0966 * 0967 * @param state Structure holding state information on the compression streams. 0968 * @return ISAL_DECOMP_OK (if everything is ok), 0969 * ISAL_INVALID_BLOCK, 0970 * ISAL_NEED_DICT, 0971 * ISAL_INVALID_SYMBOL, 0972 * ISAL_INVALID_LOOKBACK, 0973 * ISAL_INVALID_WRAPPER, 0974 * ISAL_UNSUPPORTED_METHOD, 0975 * ISAL_INCORRECT_CHECKSUM. 0976 */ 0977 0978 int 0979 isal_inflate(struct inflate_state *state); 0980 0981 /** 0982 * @brief Fast data (deflate) stateless decompression for storage applications. 0983 * 0984 * Stateless (one shot) decompression routine with a similar interface to 0985 * isal_inflate() but operates on entire input buffer at one time. Parameter 0986 * avail_out must be large enough to fit the entire decompressed 0987 * output. Dictionaries are not supported. 0988 * 0989 * @param state Structure holding state information on the compression streams. 0990 * @return ISAL_DECOMP_OK (if everything is ok), 0991 * ISAL_END_INPUT (if all input was decompressed), 0992 * ISAL_NEED_DICT, 0993 * ISAL_OUT_OVERFLOW (if output buffer ran out of space), 0994 * ISAL_INVALID_BLOCK, 0995 * ISAL_INVALID_SYMBOL, 0996 * ISAL_INVALID_LOOKBACK, 0997 * ISAL_INVALID_WRAPPER, 0998 * ISAL_UNSUPPORTED_METHOD, 0999 * ISAL_INCORRECT_CHECKSUM. 1000 */ 1001 int 1002 isal_inflate_stateless(struct inflate_state *state); 1003 1004 /******************************************************************************/ 1005 /* Other functions */ 1006 /******************************************************************************/ 1007 /** 1008 * @brief Calculate Adler-32 checksum, runs appropriate version. 1009 * 1010 * This function determines what instruction sets are enabled and selects the 1011 * appropriate version at runtime. 1012 * 1013 * @param init: initial Adler-32 value 1014 * @param buf: buffer to calculate checksum on 1015 * @param len: buffer length in bytes 1016 * 1017 * @returns 32-bit Adler-32 checksum 1018 */ 1019 uint32_t 1020 isal_adler32(uint32_t init, const unsigned char *buf, uint64_t len); 1021 1022 #ifdef __cplusplus 1023 } 1024 #endif 1025 #endif /* ifndef _IGZIP_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |