Home | History | Annotate | Download | only in tools

Lines Matching refs:zlib

11  * Tool to check and fix the zlib inflate 'too far back' problem.
59 /* zlib.h defines the structure z_stream, an instance of which is included
64 /* We must ensure that zlib uses 'const' in declarations. */
67 #include <zlib.h>
69 /* zlib.h sometimes #defines const to nothing, undo this. */
73 /* zlib.h has mediocre z_const use before 1.2.6, this stuff is for compatibility
486 * This is to avoid a function call to the zlib DLL and to optimize the
546 * zlib and the result value is obtained by XORing with CRC_INIT, which is also
560 /* This is an alternative to the algorithm used in zlib, which requires four
909 #define ZLIB_ERROR_CODE 3 /* generic zlib error */
939 case ZLIB_ERROR_CODE: return "zlib";
961 case ZLIB_ERROR_CODE: reason = "zlib error:"; break;
1546 /* PUBLIC IDAT INFORMATION: SET BY THE ZLIB CODE */
1602 /* Compresssed/uncompressed size information (from the zlib control structure
1656 * modification to the zlib header may be required.)
1658 * The compressed data is in zlib format (RFC1950) and consequently has a
1912 * function does is establish whether the zlib header needs to be modified.
1916 * checks the zlib data and returns true.
2010 /* ZLIB CONTROL STRUCTURE */
2011 struct zlib
2019 /* GLOBAL ZLIB INFORMATION: SET BY THE CALLER */
2022 /* GLOBAL ZLIB INFORMATION: SET BY THE ZLIB READ CODE */
2031 /* PROTECTED ZLIB INFORMATION: USED BY THE ZLIB ROUTINES */
2041 zlib_flevel(struct zlib *zlib)
2043 switch (zlib->header[1] >> 6)
2056 zlib_rc(struct zlib *zlib)
2057 /* Return a string for the zlib return code */
2059 switch (zlib->rc)
2075 zlib_message(struct zlib *zlib, int unexpected)
2076 /* Output a message given a zlib rc */
2078 if (zlib->global->errors)
2080 const char *reason = zlib->z.msg;
2085 fputs(zlib->file->file_name, stderr);
2087 type_name(zlib->chunk->chunk_type, stderr);
2089 unexpected ? "unexpected " : "", zlib->rc, zlib_rc(zlib), reason);
2094 zlib_end(struct zlib *zlib)
2099 if (!zlib->global->quiet)
2101 if (zlib->ok_bits < 16) /* stream was read ok */
2105 if (zlib->cksum)
2108 else if (zlib->ok_bits > zlib->file_bits)
2111 else if (zlib->ok_bits == zlib->file_bits)
2117 /* SUMMARY FORMAT (for a successful zlib inflate):
2121 type_name(zlib->chunk->chunk_type, stdout);
2122 printf(" %s %s %d %d ", reason, zlib_flevel(zlib), zlib->file_bits,
2123 zlib->ok_bits);
2124 uarb_print(zlib->compressed_bytes, zlib->compressed_digits, stdout);
2126 uarb_print(zlib->uncompressed_bytes, zlib->uncompressed_digits,
2129 fputs(zlib->file->file_name, stdout);
2135 /* This is a zlib read error; the chunk will be skipped. For an IDAT
2142 * z-rc is the zlib failure code; message is the error message with
2144 * in the zlib stream the error occurred.
2146 type_name(zlib
2147 printf(" SKP %s %d %s ", zlib_flevel(zlib), zlib->file_bits,
2148 zlib_rc(zlib));
2149 uarb_print(zlib->compressed_bytes, zlib->compressed_digits, stdout);
2151 emit_string(zlib->z.msg ? zlib->z.msg : "[no_message]", stdout);
2153 fputs(zlib->file->file_name, stdout);
2158 if (zlib->state >= 0)
2160 zlib->rc = inflateEnd(&zlib->z);
2162 if (zlib->rc != Z_OK)
2163 zlib_message(zlib, 1/*unexpected*/);
2166 CLEAR(*zlib);
2170 zlib_reset(struct zlib *zlib, int window_bits)
2171 /* Reinitializes a zlib with a different window_bits */
2173 assert(zlib->state >= 0); /* initialized by zlib_init */
2175 zlib->z.next_in = Z_NULL;
2176 zlib->z.avail_in = 0;
2177 zlib->z.next_out = Z_NULL;
2178 zlib->z.avail_out = 0;
2180 zlib->window_bits = window_bits;
2181 zlib->compressed_digits = 0;
2182 zlib->uncompressed_digits = 0;
2184 zlib->state = 0; /* initialized, once */
2185 zlib->rc = inflateReset2(&zlib->z, 0);
2186 if (zlib->rc != Z_OK)
2188 zlib_message(zlib, 1/*unexpected*/);
2196 zlib_init(struct zlib *zlib, struct IDAT *idat, struct chunk *chunk,
2200 CLEAR(*zlib);
2202 zlib->idat = idat;
2203 zlib->chunk = chunk;
2204 zlib->file = chunk->file;
2205 zlib->global = chunk->global;
2206 zlib->rewrite_offset = offset; /* never changed for this zlib */
2209 zlib->z.next_in = Z_NULL;
2210 zlib->z.avail_in = 0;
2211 zlib->z.zalloc = Z_NULL;
2212 zlib->z.zfree = Z_NULL;
2213 zlib->z.opaque = Z_NULL;
2215 zlib->state = -1;
2216 zlib->window_bits = window_bits;
2218 zlib->compressed_digits = 0;
2219 zlib->uncompressed_digits = 0;
2224 zlib->file_bits = 24;
2225 zlib->ok_bits = 16; /* unset */
2226 zlib->cksum = 0; /* set when a checksum error is detected */
2231 zlib->rc = inflateInit2(&zlib->z, 0);
2232 if (zlib->rc != Z_OK)
2234 zlib_message(zlib, 1/*unexpected*/);
2240 zlib->state = 0; /* initialized */
2247 /* Return the zlib stream window bits required for data of the given size. */
2267 zlib_advance(struct zlib *zlib, png_uint_32 nbytes)
2274 * 1: saw Z_STREAM_END (zlib->extra_bytes indicates too much data)
2275 * 2: a zlib error that cannot be corrected (error message already
2283 int state = zlib->state;
2286 struct file *file = zlib->file;
2302 int new_bits = zlib->window_bits;
2304 zlib->file_bits = file_bits;
2312 zlib->window_bits = ((file_bits > 15) ? 15 : file_bits);
2318 zlib->header[0] = bIn;
2319 zlib->state = state = 1;
2327 b2 += 0x1f - ((zlib->header[0] << 8) + b2) % 0x1f;
2335 if (zlib->file_bits == zlib->window_bits)
2336 zlib->cksum = 1;
2342 zlib->header[1] = bIn;
2343 zlib->state = state = 2;
2356 zlib->z.next_in = &bIn;
2357 zlib->z.avail_in = 1;
2358 zlib->z.next_out = &bOut;
2359 zlib->z.avail_out = 0; /* Initially */
2361 /* Initially use Z_NO_FLUSH in an attempt to persuade zlib to look at this
2372 zlib->z.next_out = &bOut,
2373 zlib->z.avail_out = 1,
2376 zlib->rc = inflate(&zlib->z, flush);
2377 out_bytes -= zlib->z.avail_out;
2379 switch (zlib->rc)
2382 if (zlib->z.avail_out == 0)
2385 if (zlib->z.avail_in == 0)
2388 /* Both avail_out and avail_in are 1 yet zlib returned a code
2391 zlib_message(zlib, 1/*unexpected*/);
2396 /* Zlib is supposed to have made progress: */
2397 assert(zlib->z.avail_out == 0 || zlib->z.avail_in == 0);
2402 zlib->state = 3; /* end of stream */
2407 zlib_message(zlib, 0/*stream error*/);
2413 if (zlib->z.msg != NULL &&
2414 strcmp(zlib->z.msg, "invalid distance too far back") == 0)
2422 zlib_message(zlib, 0/*stream error*/);
2434 zlib->uncompressed_digits = uarb_add32(zlib->uncompressed_bytes,
2435 zlib->uncompressed_digits, out_bytes);
2441 assert(zlib->z.avail_in == 0 || endrc != ZLIB_OK);
2443 in_bytes += 1 - zlib->z.avail_in;
2449 zlib->compressed_digits = uarb_add32(zlib->compressed_bytes,
2450 zlib->compressed_digits, in_bytes - zlib->z.avail_in);
2455 if (endrc == ZLIB_STREAM_END && zlib->window_bits < zlib->ok_bits)
2457 struct chunk *chunk = zlib->chunk;
2460 zlib->uncompressed_bytes, zlib->uncompressed_digits);
2462 zlib->compressed_bytes, zlib->compressed_digits);
2463 chunk->rewrite_buffer[0] = zlib->header[0];
2464 chunk->rewrite_buffer[1] = zlib->header[1];
2466 if (zlib->window_bits != zlib->file_bits || zlib->cksum)
2469 chunk->rewrite_offset = zlib->rewrite_offset;
2482 zlib->extra_bytes = nbytes - in_bytes;
2483 zlib->ok_bits = zlib->window_bits;
2490 zlib_run(struct zlib *zlib)
2500 zlib->extra_bytes = 0;
2502 if (zlib->idat != NULL)
2504 struct IDAT_list *list = zlib->idat->idat_list_head;
2505 struct IDAT_list *last = zlib->idat->idat_list_tail;
2511 assert(zlib->rewrite_offset == 0);
2526 skip_12(zlib->file);
2530 rc = zlib_advance(zlib, list->lengths[i]);
2542 if (zlib->global->errors && zlib->extra_bytes == 0)
2552 chunk_message(zlib->chunk,
2570 list->lengths[i] -= zlib->extra_bytes;
2572 zlib->idat->idat_list_tail = list;
2590 struct chunk *chunk = zlib->chunk;
2593 assert(zlib->rewrite_offset < chunk->chunk_length);
2595 rc = zlib_advance(zlib, chunk->chunk_length - zlib->rewrite_offset);
2598 * length to exclude them; the zlib data is always stored at the end of
2602 chunk->chunk_length -= zlib->extra_bytes;
2609 /* Check the stream of zlib compressed data in either idat (if given) or (if
2613 * In either case the input file must be positioned at the first byte of zlib
2616 * The return value is true on success, including the case where the zlib
2623 struct zlib zlib;
2629 if (zlib_init(&zlib, file->idat, file->chunk, 0/*window bits*/, offset))
2634 rc = zlib_run(&zlib);
2641 min_bits = zlib.window_bits + 1;
2646 if (!zlib.global->optimize_zlib &&
2647 zlib.window_bits == zlib.file_bits && !zlib.cksum)
2652 zlib_end(&zlib);
2656 max_bits = max_window_bits(zlib.uncompressed_bytes,
2657 zlib.uncompressed_digits);
2658 if (zlib.ok_bits < max_bits)
2659 max_bits = zlib.ok_bits;
2662 /* cksum is set if there is an error in the zlib header checksum
2667 if (zlib.cksum)
2668 zlib.chunk, "zlib checkum");
2674 zlib.z.msg = PNGZ_MSG_CAST("[truncated]");
2675 zlib_message(&zlib, 0/*expected*/);
2682 zlib_end(&zlib);
2690 while (min_bits < max_bits || max_bits < zlib.ok_bits/*if 16*/)
2694 if (zlib_reset(&zlib, test_bits))
2697 rc = zlib_run(&zlib);
2712 if (zlib.z.msg == NULL)
2713 zlib.z.msg = PNGZ_MSG_CAST(
2715 zlib_message(&zlib, 0/*stream error*/);
2716 zlib_end(&zlib);
2730 zlib_end(&zlib);
2737 zlib_end(&zlib);
2743 assert(zlib.ok_bits == max_bits);
2744 zlib_end(&zlib);
2748 else /* zlib initialization failed - skip the chunk */
2750 zlib_end(&zlib);
2765 * which is pretty much everything except the 'zlib' control structure.
2933 * than IDAT this means that the zlib compressed data is fatally damanged and
3356 * chunk which is zlib data and which must be rewritten,
3680 " Tests, optimizes and optionally fixes the zlib header in PNG files.",
3686 " of zlib issues founds for each compressed chunk and the IDAT stream in",
3755 " 0x01: The zlib too-far-back error existed in at least one chunk.",
3778 " The exit code says what problems were fixed. In particular the zlib error:",
3782 " caused by an incorrect optimization of a zlib stream is fixed in any",
3790 " Notice that some PNG files with the zlib optimization problem can still be",
3798 " The summary lines describe issues encountered with the zlib compressed",
3808 " chunk SKP comp-level file-bits zlib-rc compressed message file",
3816 " CHK: A zlib header checksum was detected and fixed.",
3817 " TFB: The zlib too far back error was detected and fixed.",
3818 " OK : No errors were detected in the zlib stream and optimization",
3820 " OPT: The zlib stream window bits value could be improved (and was).",
3821 " SKP: The chunk was skipped because of a zlib issue (zlib-rc) with",
3826 " comp-level: The recorded compression level (FLEVEL) of a zlib stream",
3829 " string {warning,libpng,zlib,invalid,read,write,unexpected}.",
3830 " file-bits: The zlib window bits recorded in the file.",
3832 " zlib-rc: A zlib return code as a string (see zlib.h).",
3833 " ok-bits: The smallest zlib window bits value that works.",
3835 " compressed: The count of compressed bytes in the zlib stream, when the",
3839 " uncompress: The count of bytes from uncompressing the zlib stream; this",
4026 "pngfix needs libpng with a zlib >=1.2.4 (not 0x%x)\n",