1 /* 2 * uzlib - tiny deflate/inflate library (deflate, gzip, zlib) 3 * 4 * Copyright (c) 2003 by Joergen Ibsen / Jibz 5 * All Rights Reserved 6 * http://www.ibsensoftware.com/ 7 * 8 * Copyright (c) 2014-2018 by Paul Sokolovsky 9 * 10 * This software is provided 'as-is', without any express 11 * or implied warranty. In no event will the authors be 12 * held liable for any damages arising from the use of 13 * this software. 14 * 15 * Permission is granted to anyone to use this software 16 * for any purpose, including commercial applications, 17 * and to alter it and redistribute it freely, subject to 18 * the following restrictions: 19 * 20 * 1. The origin of this software must not be 21 * misrepresented; you must not claim that you 22 * wrote the original software. If you use this 23 * software in a product, an acknowledgment in 24 * the product documentation would be appreciated 25 * but is not required. 26 * 27 * 2. Altered source versions must be plainly marked 28 * as such, and must not be misrepresented as 29 * being the original software. 30 * 31 * 3. This notice may not be removed or altered from 32 * any source distribution. 33 */ 34 35 #ifndef UZLIB_H_INCLUDED 36 #define UZLIB_H_INCLUDED 37 38 #include <stdlib.h> 39 #include <stdint.h> 40 #include <stdbool.h> 41 42 #include "defl_static.h" 43 44 #include "uzlib_conf.h" 45 #if UZLIB_CONF_DEBUG_LOG 46 #include <stdio.h> 47 #endif 48 49 /* calling convention */ 50 #ifndef TINFCC 51 #ifdef __WATCOMC__ 52 #define TINFCC __cdecl 53 #else 54 #define TINFCC 55 #endif 56 #endif 57 58 #ifdef __cplusplus 59 extern "C" { 60 #endif 61 62 /* ok status, more data produced */ 63 #define TINF_OK 0 64 /* end of compressed stream reached */ 65 #define TINF_DONE 1 66 #define TINF_DATA_ERROR (-3) 67 #define TINF_CHKSUM_ERROR (-4) 68 #define TINF_DICT_ERROR (-5) 69 70 /* checksum types */ 71 #define TINF_CHKSUM_NONE 0 72 #define TINF_CHKSUM_ADLER 1 73 #define TINF_CHKSUM_CRC 2 74 75 /* helper macros */ 76 #define TINF_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr))) 77 78 /* data structures */ 79 80 typedef struct { 81 unsigned short table[16]; /* table of code length counts */ 82 unsigned short trans[288]; /* code -> symbol translation table */ 83 } TINF_TREE; 84 85 struct uzlib_uncomp { 86 /* Pointer to the next byte in the input buffer */ 87 const unsigned char *source; 88 /* Pointer to the next byte past the input buffer (source_limit = source + len) */ 89 const unsigned char *source_limit; 90 /* If source_limit == NULL, or source >= source_limit, this function 91 will be used to read next byte from source stream. The function may 92 also return -1 in case of EOF (or irrecoverable error). Note that 93 besides returning the next byte, it may also update source and 94 source_limit fields, thus allowing for buffered operation. */ 95 int (*source_read_cb)(struct uzlib_uncomp *uncomp); 96 97 unsigned int tag; 98 unsigned int bitcount; 99 100 /* Destination (output) buffer start */ 101 unsigned char *dest_start; 102 /* Current pointer in dest buffer */ 103 unsigned char *dest; 104 /* Pointer past the end of the dest buffer, similar to source_limit */ 105 unsigned char *dest_limit; 106 107 /* Accumulating checksum */ 108 unsigned int checksum; 109 char checksum_type; 110 bool eof; 111 112 int btype; 113 int bfinal; 114 unsigned int curlen; 115 int lzOff; 116 unsigned char *dict_ring; 117 unsigned int dict_size; 118 unsigned int dict_idx; 119 120 TINF_TREE ltree; /* dynamic length/symbol tree */ 121 TINF_TREE dtree; /* dynamic distance tree */ 122 }; 123 124 #include "tinf_compat.h" 125 126 #define TINF_PUT(d, c) \ 127 { \ 128 *d->dest++ = c; \ 129 if (d->dict_ring) { d->dict_ring[d->dict_idx++] = c; if (d->dict_idx == d->dict_size) d->dict_idx = 0; } \ 130 } 131 132 unsigned char TINFCC uzlib_get_byte(TINF_DATA *d); 133 134 /* Decompression API */ 135 136 void TINFCC uzlib_init(void); 137 void TINFCC uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen); 138 int TINFCC uzlib_uncompress(TINF_DATA *d); 139 int TINFCC uzlib_uncompress_chksum(TINF_DATA *d); 140 141 int TINFCC uzlib_zlib_parse_header(TINF_DATA *d); 142 int TINFCC uzlib_gzip_parse_header(TINF_DATA *d); 143 144 /* Compression API */ 145 146 typedef const uint8_t *uzlib_hash_entry_t; 147 148 struct uzlib_comp { 149 struct Outbuf out; 150 151 uzlib_hash_entry_t *hash_table; 152 unsigned int hash_bits; 153 unsigned int dict_size; 154 }; 155 156 void TINFCC uzlib_compress(struct uzlib_comp *c, const uint8_t *src, unsigned slen); 157 158 /* Checksum API */ 159 160 /* prev_sum is previous value for incremental computation, 1 initially */ 161 uint32_t TINFCC uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum); 162 /* crc is previous value for incremental computation, 0xffffffff initially */ 163 uint32_t TINFCC uzlib_crc32(const void *data, unsigned int length, uint32_t crc); 164 165 #ifdef __cplusplus 166 } /* extern "C" */ 167 #endif 168 169 #endif /* UZLIB_H_INCLUDED */ 170