A fork of https://github.com/crosspoint-reader/crosspoint-reader
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#ifdef __cplusplus
43extern "C" {
44#endif
45
46#include "uzlib_conf.h"
47#if UZLIB_CONF_DEBUG_LOG
48#include <stdio.h>
49#endif
50
51/* calling convention */
52#ifndef TINFCC
53 #ifdef __WATCOMC__
54 #define TINFCC __cdecl
55 #else
56 #define TINFCC
57 #endif
58#endif
59
60/* ok status, more data produced */
61#define TINF_OK 0
62/* end of compressed stream reached */
63#define TINF_DONE 1
64#define TINF_DATA_ERROR (-3)
65#define TINF_CHKSUM_ERROR (-4)
66#define TINF_DICT_ERROR (-5)
67
68/* checksum types */
69#define TINF_CHKSUM_NONE 0
70#define TINF_CHKSUM_ADLER 1
71#define TINF_CHKSUM_CRC 2
72
73/* helper macros */
74#define TINF_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
75
76/* data structures */
77
78typedef struct {
79 unsigned short table[16]; /* table of code length counts */
80 unsigned short trans[288]; /* code -> symbol translation table */
81} TINF_TREE;
82
83struct uzlib_uncomp {
84 /* Pointer to the next byte in the input buffer */
85 const unsigned char *source;
86 /* Pointer to the next byte past the input buffer (source_limit = source + len) */
87 const unsigned char *source_limit;
88 /* If source_limit == NULL, or source >= source_limit, this function
89 will be used to read next byte from source stream. The function may
90 also return -1 in case of EOF (or irrecoverable error). Note that
91 besides returning the next byte, it may also update source and
92 source_limit fields, thus allowing for buffered operation. */
93 int (*source_read_cb)(struct uzlib_uncomp *uncomp);
94
95 unsigned int tag;
96 unsigned int bitcount;
97
98 /* Destination (output) buffer start */
99 unsigned char *dest_start;
100 /* Current pointer in dest buffer */
101 unsigned char *dest;
102 /* Pointer past the end of the dest buffer, similar to source_limit */
103 unsigned char *dest_limit;
104
105 /* Accumulating checksum */
106 unsigned int checksum;
107 char checksum_type;
108 bool eof;
109
110 int btype;
111 int bfinal;
112 unsigned int curlen;
113 int lzOff;
114 unsigned char *dict_ring;
115 unsigned int dict_size;
116 unsigned int dict_idx;
117
118 TINF_TREE ltree; /* dynamic length/symbol tree */
119 TINF_TREE dtree; /* dynamic distance tree */
120};
121
122#include "tinf_compat.h"
123
124#define TINF_PUT(d, c) \
125 { \
126 *d->dest++ = c; \
127 if (d->dict_ring) { d->dict_ring[d->dict_idx++] = c; if (d->dict_idx == d->dict_size) d->dict_idx = 0; } \
128 }
129
130unsigned char TINFCC uzlib_get_byte(TINF_DATA *d);
131
132/* Decompression API */
133
134void TINFCC uzlib_init(void);
135void TINFCC uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen);
136int TINFCC uzlib_uncompress(TINF_DATA *d);
137int TINFCC uzlib_uncompress_chksum(TINF_DATA *d);
138
139int TINFCC uzlib_zlib_parse_header(TINF_DATA *d);
140int TINFCC uzlib_gzip_parse_header(TINF_DATA *d);
141
142/* Compression API */
143
144typedef const uint8_t *uzlib_hash_entry_t;
145
146struct uzlib_comp {
147 unsigned char *outbuf;
148 int outlen, outsize;
149 unsigned long outbits;
150 int noutbits;
151 int comp_disabled;
152
153 uzlib_hash_entry_t *hash_table;
154 unsigned int hash_bits;
155 unsigned int dict_size;
156};
157
158void TINFCC uzlib_compress(struct uzlib_comp *c, const uint8_t *src, unsigned slen);
159
160#include "defl_static.h"
161
162/* Checksum API */
163
164/* prev_sum is previous value for incremental computation, 1 initially */
165uint32_t TINFCC uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum);
166/* crc is previous value for incremental computation, 0xffffffff initially */
167uint32_t TINFCC uzlib_crc32(const void *data, unsigned int length, uint32_t crc);
168
169#ifdef __cplusplus
170} /* extern "C" */
171#endif
172
173#endif /* UZLIB_H_INCLUDED */