2013-04-17 00:12:09 -04:00
|
|
|
|
2013-05-08 20:46:10 -04:00
|
|
|
/* png-fix-itxt version 1.0.0
|
2013-04-17 00:12:09 -04:00
|
|
|
*
|
2015-06-06 17:31:52 -04:00
|
|
|
* Copyright 2015 Glenn Randers-Pehrson
|
2015-07-22 23:36:43 -04:00
|
|
|
* Last changed in libpng 1.6.18 [July 23, 2015]
|
2013-04-17 00:12:09 -04:00
|
|
|
*
|
|
|
|
* This code is released under the libpng license.
|
|
|
|
* For conditions of distribution and use, see the disclaimer
|
|
|
|
* and license in png.h
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
2013-05-08 20:46:10 -04:00
|
|
|
* png-fix-itxt.exe < bad.png > good.png
|
2013-04-17 00:12:09 -04:00
|
|
|
*
|
|
|
|
* Fixes a PNG file written with libpng-1.6.0 or 1.6.1 that has one or more
|
|
|
|
* uncompressed iTXt chunks. Assumes that the actual length is greater
|
|
|
|
* than or equal to the value in the length byte, and that the CRC is
|
|
|
|
* correct for the actual length. This program hunts for the CRC and
|
|
|
|
* adjusts the length byte accordingly. It is not an error to process a
|
|
|
|
* PNG file that has no iTXt chunks or one that has valid iTXt chunks;
|
|
|
|
* such files will simply be copied.
|
|
|
|
*
|
|
|
|
* Requires zlib (for crc32 and Z_NULL); build with
|
|
|
|
*
|
2013-05-08 20:46:10 -04:00
|
|
|
* gcc -O -o png-fix-itxt png-fix-itxt.c -lz
|
|
|
|
*
|
|
|
|
* If you need to handle iTXt chunks larger than 500000 kbytes you must
|
|
|
|
* rebuild png-fix-itxt with a larger values of MAX_LENGTH (or a smaller value
|
|
|
|
* if you know you will never encounter such huge iTXt chunks).
|
2013-04-17 00:12:09 -04:00
|
|
|
*/
|
2013-05-08 20:46:10 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-05-07 22:59:05 -04:00
|
|
|
#include <zlib.h>
|
2013-04-17 00:12:09 -04:00
|
|
|
|
2013-04-18 06:53:49 -04:00
|
|
|
#define MAX_LENGTH 500000
|
|
|
|
|
2015-06-30 11:36:11 -04:00
|
|
|
/* Read one character (inchar), also return octet (c), break if EOF */
|
2015-06-10 08:05:18 -04:00
|
|
|
#define GETBREAK inchar=getchar(); \
|
2015-06-30 11:36:11 -04:00
|
|
|
c=(inchar & 0xffU);\
|
2015-07-01 15:06:39 -04:00
|
|
|
if (inchar != c) break
|
2013-05-07 22:59:05 -04:00
|
|
|
int
|
|
|
|
main(void)
|
2013-04-17 00:12:09 -04:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2013-04-18 06:53:49 -04:00
|
|
|
unsigned char buf[MAX_LENGTH];
|
2013-04-17 00:12:09 -04:00
|
|
|
unsigned long crc;
|
2013-05-07 22:59:05 -04:00
|
|
|
unsigned char c;
|
|
|
|
int inchar;
|
2013-04-17 00:12:09 -04:00
|
|
|
|
|
|
|
/* Skip 8-byte signature */
|
|
|
|
for (i=8; i; i--)
|
|
|
|
{
|
2015-06-10 08:05:18 -04:00
|
|
|
GETBREAK;
|
2013-04-17 00:12:09 -04:00
|
|
|
putchar(c);
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:06:39 -04:00
|
|
|
if (inchar == c) /* !EOF */
|
2013-04-17 00:12:09 -04:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Read the length */
|
2013-05-07 22:59:05 -04:00
|
|
|
unsigned long length; /* must be 32 bits! */
|
2015-06-10 08:05:18 -04:00
|
|
|
GETBREAK; buf[0] = c; length = c; length <<= 8;
|
|
|
|
GETBREAK; buf[1] = c; length += c; length <<= 8;
|
|
|
|
GETBREAK; buf[2] = c; length += c; length <<= 8;
|
|
|
|
GETBREAK; buf[3] = c; length += c;
|
2013-04-17 00:12:09 -04:00
|
|
|
|
|
|
|
/* Read the chunkname */
|
2015-06-10 08:05:18 -04:00
|
|
|
GETBREAK; buf[4] = c;
|
|
|
|
GETBREAK; buf[5] = c;
|
|
|
|
GETBREAK; buf[6] = c;
|
|
|
|
GETBREAK; buf[7] = c;
|
2013-04-17 00:12:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* The iTXt chunk type expressed as integers is (105, 84, 88, 116) */
|
|
|
|
if (buf[4] == 105 && buf[5] == 84 && buf[6] == 88 && buf[7] == 116)
|
|
|
|
{
|
2013-04-18 06:53:49 -04:00
|
|
|
if (length >= MAX_LENGTH-12)
|
|
|
|
break; /* To do: handle this more gracefully */
|
|
|
|
|
2013-04-17 00:12:09 -04:00
|
|
|
/* Initialize the CRC */
|
|
|
|
crc = crc32(0, Z_NULL, 0);
|
|
|
|
|
|
|
|
/* Copy the data bytes */
|
|
|
|
for (i=8; i < length + 12; i++)
|
|
|
|
{
|
2015-06-10 08:05:18 -04:00
|
|
|
GETBREAK; buf[i] = c;
|
2013-04-17 00:12:09 -04:00
|
|
|
}
|
|
|
|
|
2015-07-01 15:06:39 -04:00
|
|
|
if (inchar != c) /* EOF */
|
2015-06-10 08:05:18 -04:00
|
|
|
break;
|
|
|
|
|
2013-04-17 00:12:09 -04:00
|
|
|
/* Calculate the CRC */
|
|
|
|
crc = crc32(crc, buf+4, (uInt)length+4);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Check the CRC */
|
2015-06-30 11:36:11 -04:00
|
|
|
if (((crc >> 24) & 0xffU) == buf[length+8] &&
|
|
|
|
((crc >> 16) & 0xffU) == buf[length+9] &&
|
|
|
|
((crc >> 8) & 0xffU) == buf[length+10] &&
|
|
|
|
((crc ) & 0xffU) == buf[length+11])
|
2013-04-17 00:12:09 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
length++;
|
|
|
|
|
2013-04-18 06:53:49 -04:00
|
|
|
if (length >= MAX_LENGTH-12)
|
|
|
|
break;
|
|
|
|
|
2015-06-10 08:05:18 -04:00
|
|
|
GETBREAK;
|
|
|
|
buf[length+11] = c;
|
2013-04-17 00:12:09 -04:00
|
|
|
|
|
|
|
/* Update the CRC */
|
|
|
|
crc = crc32(crc, buf+7+length, 1);
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:06:39 -04:00
|
|
|
if (inchar != c) /* EOF */
|
2015-06-10 08:05:18 -04:00
|
|
|
break;
|
|
|
|
|
2013-04-17 00:12:09 -04:00
|
|
|
/* Update length bytes */
|
2015-06-30 11:36:11 -04:00
|
|
|
buf[0] = (unsigned char)((length >> 24) & 0xffU);
|
|
|
|
buf[1] = (unsigned char)((length >> 16) & 0xffU);
|
|
|
|
buf[2] = (unsigned char)((length >> 8) & 0xffU);
|
|
|
|
buf[3] = (unsigned char)((length ) & 0xffU);
|
2013-04-17 00:12:09 -04:00
|
|
|
|
|
|
|
/* Write the fixed iTXt chunk (length, name, data, crc) */
|
|
|
|
for (i=0; i<length+12; i++)
|
|
|
|
putchar(buf[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2015-07-01 15:06:39 -04:00
|
|
|
if (inchar != c) /* EOF */
|
2015-06-10 08:05:18 -04:00
|
|
|
break;
|
|
|
|
|
2013-04-17 00:12:09 -04:00
|
|
|
/* Copy bytes that were already read (length and chunk name) */
|
|
|
|
for (i=0; i<8; i++)
|
|
|
|
putchar(buf[i]);
|
|
|
|
|
|
|
|
/* Copy data bytes and CRC */
|
|
|
|
for (i=8; i< length+12; i++)
|
|
|
|
{
|
2015-06-10 08:05:18 -04:00
|
|
|
GETBREAK;
|
|
|
|
putchar(c);
|
2013-04-17 00:12:09 -04:00
|
|
|
}
|
|
|
|
|
2015-07-01 15:06:39 -04:00
|
|
|
if (inchar != c) /* EOF */
|
2013-04-17 00:12:09 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The IEND chunk type expressed as integers is (73, 69, 78, 68) */
|
|
|
|
if (buf[4] == 73 && buf[5] == 69 && buf[6] == 78 && buf[7] == 68)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:06:39 -04:00
|
|
|
if (inchar != c) /* EOF */
|
2013-04-17 00:12:09 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (buf[4] == 73 && buf[5] == 69 && buf[6] == 78 && buf[7] == 68)
|
|
|
|
break;
|
|
|
|
}
|
2013-05-07 22:59:05 -04:00
|
|
|
|
|
|
|
return 0;
|
2013-04-17 00:12:09 -04:00
|
|
|
}
|