Lines Matching refs:zlib
11 * Tool to check and fix the zlib inflate 'too far back' problem, see the usage
53 /* zlib.h defines the structure z_stream, an instance of which is included
58 /* We must ensure that zlib uses 'const' in declarations. */
61 #include <zlib.h>
63 /* zlib.h sometimes #defines const to nothing, undo this. */
67 /* zlib.h has mediocre z_const use before 1.2.6, this stuff is for compatibility
480 * This is to avoid a function call to the zlib DLL and to optimize the
540 * zlib and the result value is obtained by XORing with CRC_INIT, which is also
554 /* This is an alternative to the algorithm used in zlib, which requires four
903 #define ZLIB_ERROR_CODE 3 /* generic zlib error */
933 case ZLIB_ERROR_CODE: return "zlib";
955 case ZLIB_ERROR_CODE: reason = "zlib error:"; break;
1596 /* Compresssed/uncompressed size information (from the zlib control structure
1650 * modification to the zlib header may be required.)
1652 * The compressed data is in zlib format (RFC1950) and consequently has a
1906 * function does is establish whether the zlib header needs to be modified.
1910 * checks the zlib data and returns true.
2005 struct zlib
2035 zlib_flevel(struct zlib *zlib)
2037 switch (zlib->header[1] >> 6)
2050 zlib_rc(struct zlib *zlib)
2051 /* Return a string for the zlib return code */
2053 switch (zlib->rc)
2069 zlib_message(struct zlib *zlib, int unexpected)
2070 /* Output a message given a zlib rc */
2072 if (zlib->global->errors)
2074 const char *reason = zlib->z.msg;
2079 fputs(zlib->file->file_name, stderr);
2081 type_name(zlib->chunk->chunk_type, stderr);
2083 unexpected ? "unexpected " : "", zlib->rc, zlib_rc(zlib), reason);
2088 zlib_end(struct zlib *zlib)
2093 if (!zlib->global->quiet)
2095 if (zlib->ok_bits < 16) /* stream was read ok */
2099 if (zlib->cksum)
2102 else if (zlib->ok_bits > zlib->file_bits)
2105 else if (zlib->ok_bits == zlib->file_bits)
2111 /* SUMMARY FORMAT (for a successful zlib inflate):
2115 type_name(zlib->chunk->chunk_type, stdout);
2116 printf(" %s %s %d %d ", reason, zlib_flevel(zlib), zlib->file_bits,
2117 zlib->ok_bits);
2118 uarb_print(zlib->compressed_bytes, zlib->compressed_digits, stdout);
2120 uarb_print(zlib->uncompressed_bytes, zlib->uncompressed_digits,
2123 fputs(zlib->file->file_name, stdout);
2129 /* This is a zlib read error; the chunk will be skipped. For an IDAT
2136 * z-rc is the zlib failure code; message is the error message with
2138 * in the zlib stream the error occured.
2140 type_name(zlib->chunk->chunk_type, stdout);
2141 printf(" SKP %s %d %s ", zlib_flevel(zlib), zlib->file_bits,
2142 zlib_rc(zlib));
2143 uarb_print(zlib->compressed_bytes, zlib->compressed_digits, stdout);
2145 emit_string(zlib->z.msg ? zlib->z.msg : "[no_message]", stdout);
2147 fputs(zlib->file->file_name, stdout);
2152 if (zlib->state >= 0)
2154 zlib->rc = inflateEnd(&zlib->z);
2156 if (zlib->rc != Z_OK)
2157 zlib_message(zlib, 1/*unexpected*/);
2160 CLEAR(*zlib);
2164 zlib_reset(struct zlib *zlib, int window_bits)
2165 /* Reinitializes a zlib with a different window_bits */
2167 assert(zlib->state >= 0); /* initialized by zlib_init */
2169 zlib->z.next_in = Z_NULL;
2170 zlib->z.avail_in = 0;
2171 zlib->z.next_out = Z_NULL;
2172 zlib->z.avail_out = 0;
2174 zlib->window_bits = window_bits;
2175 zlib->compressed_digits = 0;
2176 zlib->uncompressed_digits = 0;
2178 zlib->state = 0; /* initialized, once */
2179 zlib->rc = inflateReset2(&zlib->z, 0);
2180 if (zlib->rc != Z_OK)
2182 zlib_message(zlib, 1/*unexpected*/);
2190 zlib_init(struct zlib *zlib, struct IDAT *idat, struct chunk *chunk,
2194 CLEAR(*zlib);
2196 zlib->idat = idat;
2197 zlib->chunk = chunk;
2198 zlib->file = chunk->file;
2199 zlib->global = chunk->global;
2200 zlib->rewrite_offset = offset; /* never changed for this zlib */
2203 zlib->z.next_in = Z_NULL;
2204 zlib->z.avail_in = 0;
2205 zlib->z.zalloc = Z_NULL;
2206 zlib->z.zfree = Z_NULL;
2207 zlib->z.opaque = Z_NULL;
2209 zlib->state = -1;
2210 zlib->window_bits = window_bits;
2212 zlib->compressed_digits = 0;
2213 zlib->uncompressed_digits = 0;
2218 zlib->file_bits = 16;
2219 zlib->ok_bits = 16; /* unset */
2220 zlib->cksum = 0; /* set when a checksum error is detected */
2225 zlib->rc = inflateInit2(&zlib->z, 0);
2226 if (zlib->rc != Z_OK)
2228 zlib_message(zlib, 1/*unexpected*/);
2234 zlib->state = 0; /* initialized */
2241 /* Return the zlib stream window bits required for data of the given size. */
2261 zlib_advance(struct zlib *zlib, png_uint_32 nbytes)
2268 * 1: saw Z_STREAM_END (zlib->extra_bytes indicates too much data)
2269 * 2: a zlib error that cannot be corrected (error message already
2277 int state = zlib->state;
2280 struct file *file = zlib->file;
2296 int new_bits = zlib->window_bits;
2298 zlib->file_bits = file_bits;
2304 zlib->window_bits = file_bits;
2310 zlib->header[0] = bIn;
2311 zlib->state = state = 1;
2319 b2 += 0x1f - ((zlib->header[0] << 8) + b2) % 0x1f;
2328 if (zlib->file_bits == zlib->window_bits)
2329 zlib->cksum = 1;
2335 zlib->header[1] = bIn;
2336 zlib->state = state = 2;
2349 zlib->z.next_in = &bIn;
2350 zlib->z.avail_in = 1;
2351 zlib->z.next_out = &bOut;
2352 zlib->z.avail_out = 0; /* Initially */
2354 /* Initially use Z_NO_FLUSH in an attempt to persuade zlib to look at this
2365 zlib->z.next_out = &bOut,
2366 zlib->z.avail_out = 1,
2369 zlib->rc = inflate(&zlib->z, flush);
2370 out_bytes -= zlib->z.avail_out;
2372 switch (zlib->rc)
2375 if (zlib->z.avail_out == 0)
2378 if (zlib->z.avail_in == 0)
2381 /* Both avail_out and avail_in are 1 yet zlib returned a code
2384 zlib_message(zlib, 1/*unexpected*/);
2390 assert(zlib->z.avail_out == 0 || zlib->z.avail_in == 0);
2395 zlib->state = 3; /* end of stream */
2400 zlib_message(zlib, 0/*stream error*/);
2406 if (zlib->z.msg != NULL &&
2407 strcmp(zlib->z.msg, "invalid distance too far back") == 0)
2415 zlib_message(zlib, 0/*stream error*/);
2427 zlib->uncompressed_digits = uarb_add32(zlib->uncompressed_bytes,
2428 zlib->uncompressed_digits, out_bytes);
2434 assert(zlib->z.avail_in == 0 || endrc != ZLIB_OK);
2436 in_bytes += 1 - zlib->z.avail_in;
2442 zlib->compressed_digits = uarb_add32(zlib->compressed_bytes,
2443 zlib->compressed_digits, in_bytes - zlib->z.avail_in);
2448 if (endrc == ZLIB_STREAM_END && zlib->window_bits < zlib->ok_bits)
2450 struct chunk *chunk = zlib->chunk;
2453 zlib->uncompressed_bytes, zlib->uncompressed_digits);
2455 zlib->compressed_bytes, zlib->compressed_digits);
2456 chunk->rewrite_buffer[0] = zlib->header[0];
2457 chunk->rewrite_buffer[1] = zlib->header[1];
2459 if (zlib->window_bits != zlib->file_bits || zlib->cksum)
2462 chunk->rewrite_offset = zlib->rewrite_offset;
2475 zlib->extra_bytes = nbytes - in_bytes;
2476 zlib->ok_bits = zlib->window_bits;
2483 zlib_run(struct zlib *zlib)
2493 zlib->extra_bytes = 0;
2495 if (zlib->idat != NULL)
2497 struct IDAT_list *list = zlib->idat->idat_list_head;
2498 struct IDAT_list *last = zlib->idat->idat_list_tail;
2504 assert(zlib->rewrite_offset == 0);
2519 skip_12(zlib->file);
2523 rc = zlib_advance(zlib, list->lengths[i]);
2535 if (zlib->global->errors && zlib->extra_bytes == 0)
2545 chunk_message(zlib->chunk,
2563 list->lengths[i] -= zlib->extra_bytes;
2565 zlib->idat->idat_list_tail = list;
2583 struct chunk *chunk = zlib->chunk;
2586 assert(zlib->rewrite_offset < chunk->chunk_length);
2588 rc = zlib_advance(zlib, chunk->chunk_length - zlib->rewrite_offset);
2591 * length to exclude them; the zlib data is always stored at the end of
2595 chunk->chunk_length -= zlib->extra_bytes;
2602 /* Check the stream of zlib compressed data in either idat (if given) or (if
2606 * In either case the input file must be positioned at the first byte of zlib
2609 * The return value is true on success, including the case where the zlib
2616 struct zlib zlib;
2622 if (zlib_init(&zlib, file->idat, file->chunk, 0/*window bits*/, offset))
2627 rc = zlib_run(&zlib);
2634 min_bits = zlib.window_bits + 1;
2639 if (!zlib.global->optimize_zlib &&
2640 zlib.window_bits == zlib.file_bits && !zlib.cksum)
2645 zlib_end(&zlib);
2649 max_bits = max_window_bits(zlib.uncompressed_bytes,
2650 zlib.uncompressed_digits);
2651 if (zlib.ok_bits < max_bits)
2652 max_bits = zlib.ok_bits;
2655 /* cksum is set if there is an error in the zlib header checksum
2660 if (zlib.cksum)
2661 chunk_message(zlib.chunk, "zlib checkum");
2667 zlib.z.msg = PNGZ_MSG_CAST("[truncated]");
2668 zlib_message(&zlib, 0/*expected*/);
2675 zlib_end(&zlib);
2683 while (min_bits < max_bits || max_bits < zlib.ok_bits/*if 16*/)
2687 if (zlib_reset(&zlib, test_bits))
2690 rc = zlib_run(&zlib);
2705 if (zlib.z.msg == NULL)
2706 zlib.z.msg = PNGZ_MSG_CAST(
2708 zlib_message(&zlib, 0/*stream error*/);
2709 zlib_end(&zlib);
2723 zlib_end(&zlib);
2730 zlib_end(&zlib);
2736 assert(zlib.ok_bits == max_bits);
2737 zlib_end(&zlib);
2741 else /* zlib initialization failed - skip the chunk */
2743 zlib_end(&zlib);
2758 * which is pretty much everything except the 'zlib' control structure.
2926 * than IDAT this means that the zlib compressed data is fatally damanged and
3349 * chunk which is zlib data and which must be rewritten,
3676 " Tests, optimizes and optionally fixes the zlib header in PNG files.",
3682 " of zlib issues founds for each compressed chunk and the IDAT stream in",
3751 " 0x01: The zlib too-far-back error existed in at least one chunk.",
3774 " The exit code says what problems were fixed. In particular the zlib error:",
3778 " caused by an incorrect optimization of a zlib stream is fixed in any",
3786 " Notice that some PNG files with the zlib optimization problem can still be",
3794 " The summary lines describe issues encountered with the zlib compressed",
3804 " chunk SKP comp-level file-bits zlib-rc compressed message file",
3812 " CHK: A zlib header checksum was detected and fixed.",
3813 " TFB: The zlib too far back error was detected and fixed.",
3814 " OK : No errors were detected in the zlib stream and optimization",
3816 " OPT: The zlib stream window bits value could be improved (and was).",
3817 " SKP: The chunk was skipped because of a zlib issue (zlib-rc) with",
3822 " comp-level: The recorded compression level (FLEVEL) of a zlib stream",
3825 " string {warning,libpng,zlib,invalid,read,write,unexpected}.",
3826 " file-bits: The zlib window bits recorded in the file.",
3828 " zlib-rc: A zlib return code as a string (see zlib.h).",
3829 " ok-bits: The smallest zlib window bits value that works.",
3831 " compressed: The count of compressed bytes in the zlib stream, when the",
3835 " uncompress: The count of bytes from uncompressing the zlib stream; this",
4022 "pngfix needs libpng with a zlib >=1.2.4 (not 0x%x)\n",