inflate.c (205194) | inflate.c (230837) |
---|---|
1/* inflate.c -- zlib decompression | 1/* inflate.c -- zlib decompression |
2 * Copyright (C) 1995-2010 Mark Adler | 2 * Copyright (C) 1995-2011 Mark Adler |
3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 6/* 7 * Change history: 8 * 9 * 1.2.beta0 24 Nov 2002 10 * - First version -- complete rewrite of inflate to simplify code, avoid --- 84 unchanged lines hidden (view full) --- 95local void fixedtables OF((struct inflate_state FAR *state)); 96local int updatewindow OF((z_streamp strm, unsigned out)); 97#ifdef BUILDFIXED 98 void makefixed OF((void)); 99#endif 100local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, 101 unsigned len)); 102 | 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 5 6/* 7 * Change history: 8 * 9 * 1.2.beta0 24 Nov 2002 10 * - First version -- complete rewrite of inflate to simplify code, avoid --- 84 unchanged lines hidden (view full) --- 95local void fixedtables OF((struct inflate_state FAR *state)); 96local int updatewindow OF((z_streamp strm, unsigned out)); 97#ifdef BUILDFIXED 98 void makefixed OF((void)); 99#endif 100local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, 101 unsigned len)); 102 |
103int ZEXPORT inflateReset(strm) | 103int ZEXPORT inflateResetKeep(strm) |
104z_streamp strm; 105{ 106 struct inflate_state FAR *state; 107 108 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 109 state = (struct inflate_state FAR *)strm->state; 110 strm->total_in = strm->total_out = state->total = 0; 111 strm->msg = Z_NULL; | 104z_streamp strm; 105{ 106 struct inflate_state FAR *state; 107 108 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 109 state = (struct inflate_state FAR *)strm->state; 110 strm->total_in = strm->total_out = state->total = 0; 111 strm->msg = Z_NULL; |
112 strm->adler = 1; /* to support ill-conceived Java test suite */ | 112 if (state->wrap) /* to support ill-conceived Java test suite */ 113 strm->adler = state->wrap & 1; |
113 state->mode = HEAD; 114 state->last = 0; 115 state->havedict = 0; 116 state->dmax = 32768U; 117 state->head = Z_NULL; | 114 state->mode = HEAD; 115 state->last = 0; 116 state->havedict = 0; 117 state->dmax = 32768U; 118 state->head = Z_NULL; |
118 state->wsize = 0; 119 state->whave = 0; 120 state->wnext = 0; | |
121 state->hold = 0; 122 state->bits = 0; 123 state->lencode = state->distcode = state->next = state->codes; 124 state->sane = 1; 125 state->back = -1; 126 Tracev((stderr, "inflate: reset\n")); 127 return Z_OK; 128} 129 | 119 state->hold = 0; 120 state->bits = 0; 121 state->lencode = state->distcode = state->next = state->codes; 122 state->sane = 1; 123 state->back = -1; 124 Tracev((stderr, "inflate: reset\n")); 125 return Z_OK; 126} 127 |
128int ZEXPORT inflateReset(strm) 129z_streamp strm; 130{ 131 struct inflate_state FAR *state; 132 133 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 134 state = (struct inflate_state FAR *)strm->state; 135 state->wsize = 0; 136 state->whave = 0; 137 state->wnext = 0; 138 return inflateResetKeep(strm); 139} 140 |
|
130int ZEXPORT inflateReset2(strm, windowBits) 131z_streamp strm; 132int windowBits; 133{ 134 int wrap; 135 struct inflate_state FAR *state; 136 137 /* get the state */ --- 37 unchanged lines hidden (view full) --- 175 struct inflate_state FAR *state; 176 177 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 178 stream_size != (int)(sizeof(z_stream))) 179 return Z_VERSION_ERROR; 180 if (strm == Z_NULL) return Z_STREAM_ERROR; 181 strm->msg = Z_NULL; /* in case we return an error */ 182 if (strm->zalloc == (alloc_func)0) { | 141int ZEXPORT inflateReset2(strm, windowBits) 142z_streamp strm; 143int windowBits; 144{ 145 int wrap; 146 struct inflate_state FAR *state; 147 148 /* get the state */ --- 37 unchanged lines hidden (view full) --- 186 struct inflate_state FAR *state; 187 188 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 189 stream_size != (int)(sizeof(z_stream))) 190 return Z_VERSION_ERROR; 191 if (strm == Z_NULL) return Z_STREAM_ERROR; 192 strm->msg = Z_NULL; /* in case we return an error */ 193 if (strm->zalloc == (alloc_func)0) { |
194#ifdef Z_SOLO 195 return Z_STREAM_ERROR; 196#else |
|
183 strm->zalloc = zcalloc; 184 strm->opaque = (voidpf)0; | 197 strm->zalloc = zcalloc; 198 strm->opaque = (voidpf)0; |
199#endif |
|
185 } | 200 } |
186 if (strm->zfree == (free_func)0) strm->zfree = zcfree; | 201 if (strm->zfree == (free_func)0) 202#ifdef Z_SOLO 203 return Z_STREAM_ERROR; 204#else 205 strm->zfree = zcfree; 206#endif |
187 state = (struct inflate_state FAR *) 188 ZALLOC(strm, 1, sizeof(struct inflate_state)); 189 if (state == Z_NULL) return Z_MEM_ERROR; 190 Tracev((stderr, "inflate: allocated\n")); 191 strm->state = (struct internal_state FAR *)state; 192 state->window = Z_NULL; 193 ret = inflateReset2(strm, windowBits); 194 if (ret != Z_OK) { --- 121 unchanged lines hidden (view full) --- 316 puts(" subject to change. Applications should only use zlib.h."); 317 puts(" */"); 318 puts(""); 319 size = 1U << 9; 320 printf(" static const code lenfix[%u] = {", size); 321 low = 0; 322 for (;;) { 323 if ((low % 7) == 0) printf("\n "); | 207 state = (struct inflate_state FAR *) 208 ZALLOC(strm, 1, sizeof(struct inflate_state)); 209 if (state == Z_NULL) return Z_MEM_ERROR; 210 Tracev((stderr, "inflate: allocated\n")); 211 strm->state = (struct internal_state FAR *)state; 212 state->window = Z_NULL; 213 ret = inflateReset2(strm, windowBits); 214 if (ret != Z_OK) { --- 121 unchanged lines hidden (view full) --- 336 puts(" subject to change. Applications should only use zlib.h."); 337 puts(" */"); 338 puts(""); 339 size = 1U << 9; 340 printf(" static const code lenfix[%u] = {", size); 341 low = 0; 342 for (;;) { 343 if ((low % 7) == 0) printf("\n "); |
324 printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits, 325 state.lencode[low].val); | 344 printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, 345 state.lencode[low].bits, state.lencode[low].val); |
326 if (++low == size) break; 327 putchar(','); 328 } 329 puts("\n };"); 330 size = 1U << 5; 331 printf("\n static const code distfix[%u] = {", size); 332 low = 0; 333 for (;;) { --- 586 unchanged lines hidden (view full) --- 920 case CODELENS: 921 while (state->have < state->nlen + state->ndist) { 922 for (;;) { 923 here = state->lencode[BITS(state->lenbits)]; 924 if ((unsigned)(here.bits) <= bits) break; 925 PULLBYTE(); 926 } 927 if (here.val < 16) { | 346 if (++low == size) break; 347 putchar(','); 348 } 349 puts("\n };"); 350 size = 1U << 5; 351 printf("\n static const code distfix[%u] = {", size); 352 low = 0; 353 for (;;) { --- 586 unchanged lines hidden (view full) --- 940 case CODELENS: 941 while (state->have < state->nlen + state->ndist) { 942 for (;;) { 943 here = state->lencode[BITS(state->lenbits)]; 944 if ((unsigned)(here.bits) <= bits) break; 945 PULLBYTE(); 946 } 947 if (here.val < 16) { |
928 NEEDBITS(here.bits); | |
929 DROPBITS(here.bits); 930 state->lens[state->have++] = here.val; 931 } 932 else { 933 if (here.val == 16) { 934 NEEDBITS(here.bits + 2); 935 DROPBITS(here.bits); 936 if (state->have == 0) { --- 272 unchanged lines hidden (view full) --- 1209 /* 1210 Return from inflate(), updating the total counts and the check value. 1211 If there was no progress during the inflate() call, return a buffer 1212 error. Call updatewindow() to create and/or update the window state. 1213 Note: a memory error from inflate() is non-recoverable. 1214 */ 1215 inf_leave: 1216 RESTORE(); | 948 DROPBITS(here.bits); 949 state->lens[state->have++] = here.val; 950 } 951 else { 952 if (here.val == 16) { 953 NEEDBITS(here.bits + 2); 954 DROPBITS(here.bits); 955 if (state->have == 0) { --- 272 unchanged lines hidden (view full) --- 1228 /* 1229 Return from inflate(), updating the total counts and the check value. 1230 If there was no progress during the inflate() call, return a buffer 1231 error. Call updatewindow() to create and/or update the window state. 1232 Note: a memory error from inflate() is non-recoverable. 1233 */ 1234 inf_leave: 1235 RESTORE(); |
1217 if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) | 1236 if (state->wsize || (out != strm->avail_out && state->mode < BAD && 1237 (state->mode < CHECK || flush != Z_FINISH))) |
1218 if (updatewindow(strm, out)) { 1219 state->mode = MEM; 1220 return Z_MEM_ERROR; 1221 } 1222 in -= strm->avail_in; 1223 out -= strm->avail_out; 1224 strm->total_in += in; 1225 strm->total_out += out; --- 25 unchanged lines hidden (view full) --- 1251 1252int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) 1253z_streamp strm; 1254const Bytef *dictionary; 1255uInt dictLength; 1256{ 1257 struct inflate_state FAR *state; 1258 unsigned long id; | 1238 if (updatewindow(strm, out)) { 1239 state->mode = MEM; 1240 return Z_MEM_ERROR; 1241 } 1242 in -= strm->avail_in; 1243 out -= strm->avail_out; 1244 strm->total_in += in; 1245 strm->total_out += out; --- 25 unchanged lines hidden (view full) --- 1271 1272int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) 1273z_streamp strm; 1274const Bytef *dictionary; 1275uInt dictLength; 1276{ 1277 struct inflate_state FAR *state; 1278 unsigned long id; |
1279 unsigned char *next; 1280 unsigned avail; 1281 int ret; |
|
1259 1260 /* check state */ 1261 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 1262 state = (struct inflate_state FAR *)strm->state; 1263 if (state->wrap != 0 && state->mode != DICT) 1264 return Z_STREAM_ERROR; 1265 1266 /* check for correct dictionary id */ 1267 if (state->mode == DICT) { 1268 id = adler32(0L, Z_NULL, 0); 1269 id = adler32(id, dictionary, dictLength); 1270 if (id != state->check) 1271 return Z_DATA_ERROR; 1272 } 1273 | 1282 1283 /* check state */ 1284 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 1285 state = (struct inflate_state FAR *)strm->state; 1286 if (state->wrap != 0 && state->mode != DICT) 1287 return Z_STREAM_ERROR; 1288 1289 /* check for correct dictionary id */ 1290 if (state->mode == DICT) { 1291 id = adler32(0L, Z_NULL, 0); 1292 id = adler32(id, dictionary, dictLength); 1293 if (id != state->check) 1294 return Z_DATA_ERROR; 1295 } 1296 |
1274 /* copy dictionary to window */ 1275 if (updatewindow(strm, strm->avail_out)) { | 1297 /* copy dictionary to window using updatewindow(), which will amend the 1298 existing dictionary if appropriate */ 1299 next = strm->next_out; 1300 avail = strm->avail_out; 1301 strm->next_out = (Bytef *)dictionary + dictLength; 1302 strm->avail_out = 0; 1303 ret = updatewindow(strm, dictLength); 1304 strm->avail_out = avail; 1305 strm->next_out = next; 1306 if (ret) { |
1276 state->mode = MEM; 1277 return Z_MEM_ERROR; 1278 } | 1307 state->mode = MEM; 1308 return Z_MEM_ERROR; 1309 } |
1279 if (dictLength > state->wsize) { 1280 zmemcpy(state->window, dictionary + dictLength - state->wsize, 1281 state->wsize); 1282 state->whave = state->wsize; 1283 } 1284 else { 1285 zmemcpy(state->window + state->wsize - dictLength, dictionary, 1286 dictLength); 1287 state->whave = dictLength; 1288 } | |
1289 state->havedict = 1; 1290 Tracev((stderr, "inflate: dictionary set\n")); 1291 return Z_OK; 1292} 1293 1294int ZEXPORT inflateGetHeader(strm, head) 1295z_streamp strm; 1296gz_headerp head; --- 131 unchanged lines hidden (view full) --- 1428 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); 1429 if (window == Z_NULL) { 1430 ZFREE(source, copy); 1431 return Z_MEM_ERROR; 1432 } 1433 } 1434 1435 /* copy state */ | 1310 state->havedict = 1; 1311 Tracev((stderr, "inflate: dictionary set\n")); 1312 return Z_OK; 1313} 1314 1315int ZEXPORT inflateGetHeader(strm, head) 1316z_streamp strm; 1317gz_headerp head; --- 131 unchanged lines hidden (view full) --- 1449 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); 1450 if (window == Z_NULL) { 1451 ZFREE(source, copy); 1452 return Z_MEM_ERROR; 1453 } 1454 } 1455 1456 /* copy state */ |
1436 zmemcpy(dest, source, sizeof(z_stream)); 1437 zmemcpy(copy, state, sizeof(struct inflate_state)); | 1457 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); 1458 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); |
1438 if (state->lencode >= state->codes && 1439 state->lencode <= state->codes + ENOUGH - 1) { 1440 copy->lencode = copy->codes + (state->lencode - state->codes); 1441 copy->distcode = copy->codes + (state->distcode - state->codes); 1442 } 1443 copy->next = copy->codes + (state->next - state->codes); 1444 if (window != Z_NULL) { 1445 wsize = 1U << state->wbits; --- 35 unchanged lines hidden --- | 1459 if (state->lencode >= state->codes && 1460 state->lencode <= state->codes + ENOUGH - 1) { 1461 copy->lencode = copy->codes + (state->lencode - state->codes); 1462 copy->distcode = copy->codes + (state->distcode - state->codes); 1463 } 1464 copy->next = copy->codes + (state->next - state->codes); 1465 if (window != Z_NULL) { 1466 wsize = 1U << state->wbits; --- 35 unchanged lines hidden --- |