| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#ifndef INFLATE_P_H |
| 6 |
#define INFLATE_P_H |
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
|
| 11 |
#ifdef S390_DFLTCC_INFLATE |
| 12 |
# include "arch/s390/dfltcc_inflate.h" |
| 13 |
#else |
| 14 |
|
| 15 |
# define ZALLOC_INFLATE_STATE(strm) ((struct inflate_state *)ZALLOC(strm, 1, sizeof(struct inflate_state))) |
| 16 |
# define ZFREE_STATE(strm, addr) ZFREE(strm, addr) |
| 17 |
# define ZCOPY_INFLATE_STATE(dst, src) memcpy(dst, src, sizeof(struct inflate_state)) |
| 18 |
|
| 19 |
# define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size) |
| 20 |
# define ZFREE_WINDOW(strm, addr) ZFREE(strm, addr) |
| 21 |
|
| 22 |
# define INFLATE_RESET_KEEP_HOOK(strm) do {} while (0) |
| 23 |
|
| 24 |
# define INFLATE_PRIME_HOOK(strm, bits, value) do {} while (0) |
| 25 |
|
| 26 |
# define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0) |
| 27 |
|
| 28 |
# define INFLATE_NEED_CHECKSUM(strm) 1 |
| 29 |
|
| 30 |
# define INFLATE_NEED_UPDATEWINDOW(strm) 1 |
| 31 |
|
| 32 |
# define INFLATE_MARK_HOOK(strm) do {} while (0) |
| 33 |
|
| 34 |
# define INFLATE_SYNC_POINT_HOOK(strm) do {} while (0) |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#ifdef GUNZIP |
| 43 |
# define UPDATE(check, buf, len) \ |
| 44 |
(state->flags ? PREFIX(crc32)(check, buf, len) : functable.adler32(check, buf, len)) |
| 45 |
#else |
| 46 |
# define UPDATE(check, buf, len) functable.adler32(check, buf, len) |
| 47 |
#endif |
| 48 |
|
| 49 |
|
| 50 |
#ifdef GUNZIP |
| 51 |
# define CRC2(check, word) \ |
| 52 |
do { \ |
| 53 |
hbuf[0] = (unsigned char)(word); \ |
| 54 |
hbuf[1] = (unsigned char)((word) >> 8); \ |
| 55 |
check = PREFIX(crc32)(check, hbuf, 2); \ |
| 56 |
} while (0) |
| 57 |
|
| 58 |
# define CRC4(check, word) \ |
| 59 |
do { \ |
| 60 |
hbuf[0] = (unsigned char)(word); \ |
| 61 |
hbuf[1] = (unsigned char)((word) >> 8); \ |
| 62 |
hbuf[2] = (unsigned char)((word) >> 16); \ |
| 63 |
hbuf[3] = (unsigned char)((word) >> 24); \ |
| 64 |
check = PREFIX(crc32)(check, hbuf, 4); \ |
| 65 |
} while (0) |
| 66 |
#endif |
| 67 |
|
| 68 |
|
| 69 |
#define LOAD() \ |
| 70 |
do { \ |
| 71 |
put = strm->next_out; \ |
| 72 |
left = strm->avail_out; \ |
| 73 |
next = strm->next_in; \ |
| 74 |
have = strm->avail_in; \ |
| 75 |
hold = state->hold; \ |
| 76 |
bits = state->bits; \ |
| 77 |
} while (0) |
| 78 |
|
| 79 |
|
| 80 |
#define RESTORE() \ |
| 81 |
do { \ |
| 82 |
strm->next_out = put; \ |
| 83 |
strm->avail_out = left; \ |
| 84 |
strm->next_in = (z_const unsigned char *)next; \ |
| 85 |
strm->avail_in = have; \ |
| 86 |
state->hold = hold; \ |
| 87 |
state->bits = bits; \ |
| 88 |
} while (0) |
| 89 |
|
| 90 |
|
| 91 |
#define INITBITS() \ |
| 92 |
do { \ |
| 93 |
hold = 0; \ |
| 94 |
bits = 0; \ |
| 95 |
} while (0) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#define NEEDBITS(n) \ |
| 100 |
do { \ |
| 101 |
while (bits < (unsigned)(n)) \ |
| 102 |
PULLBYTE(); \ |
| 103 |
} while (0) |
| 104 |
|
| 105 |
|
| 106 |
#define BITS(n) \ |
| 107 |
(hold & ((1U << (unsigned)(n)) - 1)) |
| 108 |
|
| 109 |
|
| 110 |
#define DROPBITS(n) \ |
| 111 |
do { \ |
| 112 |
hold >>= (n); \ |
| 113 |
bits -= (unsigned)(n); \ |
| 114 |
} while (0) |
| 115 |
|
| 116 |
|
| 117 |
#define BYTEBITS() \ |
| 118 |
do { \ |
| 119 |
hold >>= bits & 7; \ |
| 120 |
bits -= bits & 7; \ |
| 121 |
} while (0) |
| 122 |
|
| 123 |
#endif |
| 124 |
|
| 125 |
|
| 126 |
#define SET_BAD(errmsg) \ |
| 127 |
do { \ |
| 128 |
state->mode = BAD; \ |
| 129 |
strm->msg = (char *)errmsg; \ |
| 130 |
} while (0) |
| 131 |
|
| 132 |
|
| 133 |
static inline uint8_t* chunkcopy_safe(uint8_t *out, uint8_t *from, uint64_t len, uint8_t *safe) { |
| 134 |
uint64_t safelen = (safe - out) + 1; |
| 135 |
len = MIN(len, safelen); |
| 136 |
int32_t olap_src = from >= out && from < out + len; |
| 137 |
int32_t olap_dst = out >= from && out < from + len; |
| 138 |
uint64_t tocopy; |
| 139 |
|
| 140 |
|
| 141 |
if (!(olap_src || olap_dst)) { |
| 142 |
memcpy(out, from, (size_t)len); |
| 143 |
return out + len; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
uint64_t non_olap_size = llabs(from - out); |
| 152 |
|
| 153 |
memcpy(out, from, (size_t)non_olap_size); |
| 154 |
out += non_olap_size; |
| 155 |
from += non_olap_size; |
| 156 |
len -= non_olap_size; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
while (len) { |
| 161 |
tocopy = MIN(non_olap_size, len); |
| 162 |
len -= tocopy; |
| 163 |
|
| 164 |
while (tocopy >= 32) { |
| 165 |
memcpy(out, from, 32); |
| 166 |
out += 32; |
| 167 |
from += 32; |
| 168 |
tocopy -= 32; |
| 169 |
} |
| 170 |
|
| 171 |
if (tocopy >= 16) { |
| 172 |
memcpy(out, from, 16); |
| 173 |
out += 16; |
| 174 |
from += 16; |
| 175 |
tocopy -= 16; |
| 176 |
} |
| 177 |
|
| 178 |
if (tocopy >= 8) { |
| 179 |
memcpy(out, from, 8); |
| 180 |
out += 8; |
| 181 |
from += 8; |
| 182 |
tocopy -= 8; |
| 183 |
} |
| 184 |
|
| 185 |
if (tocopy >= 4) { |
| 186 |
memcpy(out, from, 4); |
| 187 |
out += 4; |
| 188 |
from += 4; |
| 189 |
tocopy -= 4; |
| 190 |
} |
| 191 |
|
| 192 |
if (tocopy >= 2) { |
| 193 |
memcpy(out, from, 2); |
| 194 |
out += 2; |
| 195 |
from += 2; |
| 196 |
tocopy -= 2; |
| 197 |
} |
| 198 |
|
| 199 |
if (tocopy) { |
| 200 |
*out++ = *from++; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return out; |
| 205 |
} |