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