png-fix-itxt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* png-fix-itxt version 1.0.0
  2. *
  3. * Copyright 2013 Glenn Randers-Pehrson
  4. * Last changed in libpng 1.6.3 [July 18, 2013]
  5. *
  6. * This code is released under the libpng license.
  7. * For conditions of distribution and use, see the disclaimer
  8. * and license in png.h
  9. *
  10. * Usage:
  11. *
  12. * png-fix-itxt.exe < bad.png > good.png
  13. *
  14. * Fixes a PNG file written with libpng-1.6.0 or 1.6.1 that has one or more
  15. * uncompressed iTXt chunks. Assumes that the actual length is greater
  16. * than or equal to the value in the length byte, and that the CRC is
  17. * correct for the actual length. This program hunts for the CRC and
  18. * adjusts the length byte accordingly. It is not an error to process a
  19. * PNG file that has no iTXt chunks or one that has valid iTXt chunks;
  20. * such files will simply be copied.
  21. *
  22. * Requires zlib (for crc32 and Z_NULL); build with
  23. *
  24. * gcc -O -o png-fix-itxt png-fix-itxt.c -lz
  25. *
  26. * If you need to handle iTXt chunks larger than 500000 kbytes you must
  27. * rebuild png-fix-itxt with a larger values of MAX_LENGTH (or a smaller value
  28. * if you know you will never encounter such huge iTXt chunks).
  29. */
  30. #include <stdio.h>
  31. #include <zlib.h>
  32. #define MAX_LENGTH 500000
  33. #define GETBREAK ((unsigned char)(inchar=getchar())); if (inchar == EOF) break
  34. int
  35. main(void)
  36. {
  37. unsigned int i;
  38. unsigned char buf[MAX_LENGTH];
  39. unsigned long crc;
  40. unsigned char c;
  41. int inchar;
  42. /* Skip 8-byte signature */
  43. for (i=8; i; i--)
  44. {
  45. c=GETBREAK;
  46. putchar(c);
  47. }
  48. if (inchar != EOF)
  49. for (;;)
  50. {
  51. /* Read the length */
  52. unsigned long length; /* must be 32 bits! */
  53. c=GETBREAK; buf[0] = c; length = c; length <<= 8;
  54. c=GETBREAK; buf[1] = c; length += c; length <<= 8;
  55. c=GETBREAK; buf[2] = c; length += c; length <<= 8;
  56. c=GETBREAK; buf[3] = c; length += c;
  57. /* Read the chunkname */
  58. c=GETBREAK; buf[4] = c;
  59. c=GETBREAK; buf[5] = c;
  60. c=GETBREAK; buf[6] = c;
  61. c=GETBREAK; buf[7] = c;
  62. /* The iTXt chunk type expressed as integers is (105, 84, 88, 116) */
  63. if (buf[4] == 105 && buf[5] == 84 && buf[6] == 88 && buf[7] == 116)
  64. {
  65. if (length >= MAX_LENGTH-12)
  66. break; /* To do: handle this more gracefully */
  67. /* Initialize the CRC */
  68. crc = crc32(0, Z_NULL, 0);
  69. /* Copy the data bytes */
  70. for (i=8; i < length + 12; i++)
  71. {
  72. c=GETBREAK; buf[i] = c;
  73. }
  74. /* Calculate the CRC */
  75. crc = crc32(crc, buf+4, (uInt)length+4);
  76. for (;;)
  77. {
  78. /* Check the CRC */
  79. if (((crc >> 24) & 0xff) == buf[length+8] &&
  80. ((crc >> 16) & 0xff) == buf[length+9] &&
  81. ((crc >> 8) & 0xff) == buf[length+10] &&
  82. ((crc ) & 0xff) == buf[length+11])
  83. break;
  84. length++;
  85. if (length >= MAX_LENGTH-12)
  86. break;
  87. c=GETBREAK;
  88. buf[length+11]=c;
  89. /* Update the CRC */
  90. crc = crc32(crc, buf+7+length, 1);
  91. }
  92. /* Update length bytes */
  93. buf[0] = (unsigned char)((length << 24) & 0xff);
  94. buf[1] = (unsigned char)((length << 16) & 0xff);
  95. buf[2] = (unsigned char)((length << 8) & 0xff);
  96. buf[3] = (unsigned char)((length ) & 0xff);
  97. /* Write the fixed iTXt chunk (length, name, data, crc) */
  98. for (i=0; i<length+12; i++)
  99. putchar(buf[i]);
  100. }
  101. else
  102. {
  103. /* Copy bytes that were already read (length and chunk name) */
  104. for (i=0; i<8; i++)
  105. putchar(buf[i]);
  106. /* Copy data bytes and CRC */
  107. for (i=8; i< length+12; i++)
  108. {
  109. c=GETBREAK;
  110. putchar(c);
  111. }
  112. if (inchar == EOF)
  113. {
  114. break;
  115. }
  116. /* The IEND chunk type expressed as integers is (73, 69, 78, 68) */
  117. if (buf[4] == 73 && buf[5] == 69 && buf[6] == 78 && buf[7] == 68)
  118. break;
  119. }
  120. if (inchar == EOF)
  121. break;
  122. if (buf[4] == 73 && buf[5] == 69 && buf[6] == 78 && buf[7] == 68)
  123. break;
  124. }
  125. return 0;
  126. }