pngunknown.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /* pngunknown.c - test the read side unknown chunk handling
  2. *
  3. * Last changed in libpng 1.6.10 [March 6, 2014]
  4. * Copyright (c) 2014 Glenn Randers-Pehrson
  5. * Written by John Cunningham Bowler
  6. *
  7. * This code is released under the libpng license.
  8. * For conditions of distribution and use, see the disclaimer
  9. * and license in png.h
  10. *
  11. * NOTES:
  12. * This is a C program that is intended to be linked against libpng. It
  13. * allows the libpng unknown handling code to be tested by interpreting
  14. * arguments to save or discard combinations of chunks. The program is
  15. * currently just a minimal validation for the built-in libpng facilities.
  16. */
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <setjmp.h>
  21. /* Define the following to use this test against your installed libpng, rather
  22. * than the one being built here:
  23. */
  24. #ifdef PNG_FREESTANDING_TESTS
  25. # include <png.h>
  26. #else
  27. # include "../../png.h"
  28. #endif
  29. /* Since this program tests the ability to change the unknown chunk handling
  30. * these must be defined:
  31. */
  32. #if defined(PNG_SET_UNKNOWN_CHUNKS_SUPPORTED) &&\
  33. defined(PNG_READ_SUPPORTED)
  34. /* One of these must be defined to allow us to find out what happened. It is
  35. * still useful to set unknown chunk handling without either of these in order
  36. * to cause *known* chunks to be discarded. This can be a significant
  37. * efficiency gain, but it can't really be tested here.
  38. */
  39. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) ||\
  40. defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
  41. #if PNG_LIBPNG_VER < 10500
  42. /* This deliberately lacks the PNG_CONST. */
  43. typedef png_byte *png_const_bytep;
  44. /* This is copied from 1.5.1 png.h: */
  45. #define PNG_INTERLACE_ADAM7_PASSES 7
  46. #define PNG_PASS_START_ROW(pass) (((1U&~(pass))<<(3-((pass)>>1)))&7)
  47. #define PNG_PASS_START_COL(pass) (((1U& (pass))<<(3-(((pass)+1)>>1)))&7)
  48. #define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3)
  49. #define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3)
  50. #define PNG_PASS_ROWS(height, pass) (((height)+(((1<<PNG_PASS_ROW_SHIFT(pass))\
  51. -1)-PNG_PASS_START_ROW(pass)))>>PNG_PASS_ROW_SHIFT(pass))
  52. #define PNG_PASS_COLS(width, pass) (((width)+(((1<<PNG_PASS_COL_SHIFT(pass))\
  53. -1)-PNG_PASS_START_COL(pass)))>>PNG_PASS_COL_SHIFT(pass))
  54. #define PNG_ROW_FROM_PASS_ROW(yIn, pass) \
  55. (((yIn)<<PNG_PASS_ROW_SHIFT(pass))+PNG_PASS_START_ROW(pass))
  56. #define PNG_COL_FROM_PASS_COL(xIn, pass) \
  57. (((xIn)<<PNG_PASS_COL_SHIFT(pass))+PNG_PASS_START_COL(pass))
  58. #define PNG_PASS_MASK(pass,off) ( \
  59. ((0x110145AFU>>(((7-(off))-(pass))<<2)) & 0xFU) | \
  60. ((0x01145AF0U>>(((7-(off))-(pass))<<2)) & 0xF0U))
  61. #define PNG_ROW_IN_INTERLACE_PASS(y, pass) \
  62. ((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1)
  63. #define PNG_COL_IN_INTERLACE_PASS(x, pass) \
  64. ((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1)
  65. /* These are needed too for the default build: */
  66. #define PNG_WRITE_16BIT_SUPPORTED
  67. #define PNG_READ_16BIT_SUPPORTED
  68. /* This comes from pnglibconf.h afer 1.5: */
  69. #define PNG_FP_1 100000
  70. #define PNG_GAMMA_THRESHOLD_FIXED\
  71. ((png_fixed_point)(PNG_GAMMA_THRESHOLD * PNG_FP_1))
  72. #endif
  73. #if PNG_LIBPNG_VER < 10600
  74. /* 1.6.0 constifies many APIs. The following exists to allow pngvalid to be
  75. * compiled against earlier versions.
  76. */
  77. # define png_const_structp png_structp
  78. #endif
  79. #if PNG_LIBPNG_VER < 10700
  80. /* Copied from libpng 1.7.0 png.h */
  81. #define PNG_u2(b1, b2) (((unsigned int)(b1) << 8) + (b2))
  82. #define PNG_U16(b1, b2) ((png_uint_16)PNG_u2(b1, b2))
  83. #define PNG_U32(b1, b2, b3, b4)\
  84. (((png_uint_32)PNG_u2(b1, b2) << 16) + PNG_u2(b3, b4))
  85. /* Constants for known chunk types.
  86. */
  87. #define png_IDAT PNG_U32( 73, 68, 65, 84)
  88. #define png_IEND PNG_U32( 73, 69, 78, 68)
  89. #define png_IHDR PNG_U32( 73, 72, 68, 82)
  90. #define png_PLTE PNG_U32( 80, 76, 84, 69)
  91. #define png_bKGD PNG_U32( 98, 75, 71, 68)
  92. #define png_cHRM PNG_U32( 99, 72, 82, 77)
  93. #define png_fRAc PNG_U32(102, 82, 65, 99) /* registered, not defined */
  94. #define png_gAMA PNG_U32(103, 65, 77, 65)
  95. #define png_gIFg PNG_U32(103, 73, 70, 103)
  96. #define png_gIFt PNG_U32(103, 73, 70, 116) /* deprecated */
  97. #define png_gIFx PNG_U32(103, 73, 70, 120)
  98. #define png_hIST PNG_U32(104, 73, 83, 84)
  99. #define png_iCCP PNG_U32(105, 67, 67, 80)
  100. #define png_iTXt PNG_U32(105, 84, 88, 116)
  101. #define png_oFFs PNG_U32(111, 70, 70, 115)
  102. #define png_pCAL PNG_U32(112, 67, 65, 76)
  103. #define png_pHYs PNG_U32(112, 72, 89, 115)
  104. #define png_sBIT PNG_U32(115, 66, 73, 84)
  105. #define png_sCAL PNG_U32(115, 67, 65, 76)
  106. #define png_sPLT PNG_U32(115, 80, 76, 84)
  107. #define png_sRGB PNG_U32(115, 82, 71, 66)
  108. #define png_sTER PNG_U32(115, 84, 69, 82)
  109. #define png_tEXt PNG_U32(116, 69, 88, 116)
  110. #define png_tIME PNG_U32(116, 73, 77, 69)
  111. #define png_tRNS PNG_U32(116, 82, 78, 83)
  112. #define png_zTXt PNG_U32(122, 84, 88, 116)
  113. /* Test on flag values as defined in the spec (section 5.4): */
  114. #define PNG_CHUNK_ANCILLARY(c) (1 & ((c) >> 29))
  115. #define PNG_CHUNK_CRITICAL(c) (!PNG_CHUNK_ANCILLARY(c))
  116. #define PNG_CHUNK_PRIVATE(c) (1 & ((c) >> 21))
  117. #define PNG_CHUNK_RESERVED(c) (1 & ((c) >> 13))
  118. #define PNG_CHUNK_SAFE_TO_COPY(c) (1 & ((c) >> 5))
  119. #endif /* PNG_LIBPNG_VER < 10700 */
  120. #ifdef __cplusplus
  121. # define this not_the_cpp_this
  122. # define new not_the_cpp_new
  123. # define voidcast(type, value) static_cast<type>(value)
  124. #else
  125. # define voidcast(type, value) (value)
  126. #endif /* __cplusplus */
  127. /* Unused formal parameter errors are removed using the following macro which is
  128. * expected to have no bad effects on performance.
  129. */
  130. #ifndef UNUSED
  131. # if defined(__GNUC__) || defined(_MSC_VER)
  132. # define UNUSED(param) (void)param;
  133. # else
  134. # define UNUSED(param)
  135. # endif
  136. #endif
  137. /* Types of chunks not known to libpng */
  138. #define png_vpAg PNG_U32(118, 112, 65, 103)
  139. /* Chunk information */
  140. #define PNG_INFO_tEXt 0x10000000U
  141. #define PNG_INFO_iTXt 0x20000000U
  142. #define PNG_INFO_zTXt 0x40000000U
  143. #define PNG_INFO_sTER 0x01000000U
  144. #define PNG_INFO_vpAg 0x02000000U
  145. #define ABSENT 0
  146. #define START 1
  147. #define END 2
  148. static struct
  149. {
  150. char name[5];
  151. png_uint_32 flag;
  152. png_uint_32 tag;
  153. int unknown; /* Chunk not known to libpng */
  154. int all; /* Chunk set by the '-1' option */
  155. int position; /* position in pngtest.png */
  156. int keep; /* unknown handling setting */
  157. } chunk_info[] = {
  158. /* Critical chunks */
  159. { "IDAT", PNG_INFO_IDAT, png_IDAT, 0, 0, START, 0 }, /* must be [0] */
  160. { "PLTE", PNG_INFO_PLTE, png_PLTE, 0, 0, ABSENT, 0 },
  161. /* Non-critical chunks that libpng handles */
  162. /* This is a mess but it seems to be the only way to do it - there is no way
  163. * to check for a definition outside a #if.
  164. */
  165. { "bKGD", PNG_INFO_bKGD, png_bKGD,
  166. # ifdef PNG_READ_bKGD_SUPPORTED
  167. 0,
  168. # else
  169. 1,
  170. # endif
  171. 1, START, 0 },
  172. { "cHRM", PNG_INFO_cHRM, png_cHRM,
  173. # ifdef PNG_READ_cHRM_SUPPORTED
  174. 0,
  175. # else
  176. 1,
  177. # endif
  178. 1, START, 0 },
  179. { "gAMA", PNG_INFO_gAMA, png_gAMA,
  180. # ifdef PNG_READ_gAMA_SUPPORTED
  181. 0,
  182. # else
  183. 1,
  184. # endif
  185. 1, START, 0 },
  186. { "hIST", PNG_INFO_hIST, png_hIST,
  187. # ifdef PNG_READ_hIST_SUPPORTED
  188. 0,
  189. # else
  190. 1,
  191. # endif
  192. 1, ABSENT, 0 },
  193. { "iCCP", PNG_INFO_iCCP, png_iCCP,
  194. # ifdef PNG_READ_iCCP_SUPPORTED
  195. 0,
  196. # else
  197. 1,
  198. # endif
  199. 1, ABSENT, 0 },
  200. { "iTXt", PNG_INFO_iTXt, png_iTXt,
  201. # ifdef PNG_READ_iTXt_SUPPORTED
  202. 0,
  203. # else
  204. 1,
  205. # endif
  206. 1, ABSENT, 0 },
  207. { "oFFs", PNG_INFO_oFFs, png_oFFs,
  208. # ifdef PNG_READ_oFFs_SUPPORTED
  209. 0,
  210. # else
  211. 1,
  212. # endif
  213. 1, START, 0 },
  214. { "pCAL", PNG_INFO_pCAL, png_pCAL,
  215. # ifdef PNG_READ_pCAL_SUPPORTED
  216. 0,
  217. # else
  218. 1,
  219. # endif
  220. 1, START, 0 },
  221. { "pHYs", PNG_INFO_pHYs, png_pHYs,
  222. # ifdef PNG_READ_pHYs_SUPPORTED
  223. 0,
  224. # else
  225. 1,
  226. # endif
  227. 1, START, 0 },
  228. { "sBIT", PNG_INFO_sBIT, png_sBIT,
  229. # ifdef PNG_READ_sBIT_SUPPORTED
  230. 0,
  231. # else
  232. 1,
  233. # endif
  234. 1, START, 0 },
  235. { "sCAL", PNG_INFO_sCAL, png_sCAL,
  236. # ifdef PNG_READ_sCAL_SUPPORTED
  237. 0,
  238. # else
  239. 1,
  240. # endif
  241. 1, START, 0 },
  242. { "sPLT", PNG_INFO_sPLT, png_sPLT,
  243. # ifdef PNG_READ_sPLT_SUPPORTED
  244. 0,
  245. # else
  246. 1,
  247. # endif
  248. 1, ABSENT, 0 },
  249. { "sRGB", PNG_INFO_sRGB, png_sRGB,
  250. # ifdef PNG_READ_sRGB_SUPPORTED
  251. 0,
  252. # else
  253. 1,
  254. # endif
  255. 1, START, 0 },
  256. { "tEXt", PNG_INFO_tEXt, png_tEXt,
  257. # ifdef PNG_READ_tEXt_SUPPORTED
  258. 0,
  259. # else
  260. 1,
  261. # endif
  262. 1, START, 0 },
  263. { "tIME", PNG_INFO_tIME, png_tIME,
  264. # ifdef PNG_READ_tIME_SUPPORTED
  265. 0,
  266. # else
  267. 1,
  268. # endif
  269. 1, START, 0 },
  270. { "tRNS", PNG_INFO_tRNS, png_tRNS,
  271. # ifdef PNG_READ_tRNS_SUPPORTED
  272. 0,
  273. # else
  274. 1,
  275. # endif
  276. 0, ABSENT, 0 },
  277. { "zTXt", PNG_INFO_zTXt, png_zTXt,
  278. # ifdef PNG_READ_zTXt_SUPPORTED
  279. 0,
  280. # else
  281. 1,
  282. # endif
  283. 1, END, 0 },
  284. /* No libpng handling */
  285. { "sTER", PNG_INFO_sTER, png_sTER, 1, 1, START, 0 },
  286. { "vpAg", PNG_INFO_vpAg, png_vpAg, 1, 0, START, 0 },
  287. };
  288. #define NINFO ((int)((sizeof chunk_info)/(sizeof chunk_info[0])))
  289. static void
  290. clear_keep(void)
  291. {
  292. int i = NINFO;
  293. while (--i >= 0)
  294. chunk_info[i].keep = 0;
  295. }
  296. static int
  297. find(const char *name)
  298. {
  299. int i = NINFO;
  300. while (--i >= 0)
  301. {
  302. if (memcmp(chunk_info[i].name, name, 4) == 0)
  303. break;
  304. }
  305. return i;
  306. }
  307. static int
  308. findb(const png_byte *name)
  309. {
  310. int i = NINFO;
  311. while (--i >= 0)
  312. {
  313. if (memcmp(chunk_info[i].name, name, 4) == 0)
  314. break;
  315. }
  316. return i;
  317. }
  318. static int
  319. find_by_flag(png_uint_32 flag)
  320. {
  321. int i = NINFO;
  322. while (--i >= 0) if (chunk_info[i].flag == flag) return i;
  323. fprintf(stderr, "pngunknown: internal error\n");
  324. exit(4);
  325. }
  326. static int
  327. ancillary(const char *name)
  328. {
  329. return PNG_CHUNK_ANCILLARY(PNG_U32(name[0], name[1], name[2], name[3]));
  330. }
  331. #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  332. static int
  333. ancillaryb(const png_byte *name)
  334. {
  335. return PNG_CHUNK_ANCILLARY(PNG_U32(name[0], name[1], name[2], name[3]));
  336. }
  337. #endif
  338. /* Type of an error_ptr */
  339. typedef struct
  340. {
  341. jmp_buf error_return;
  342. png_structp png_ptr;
  343. png_infop info_ptr, end_ptr;
  344. png_uint_32 before_IDAT;
  345. png_uint_32 after_IDAT;
  346. int error_count;
  347. int warning_count;
  348. int keep; /* the default value */
  349. const char *program;
  350. const char *file;
  351. const char *test;
  352. } display;
  353. static const char init[] = "initialization";
  354. static const char cmd[] = "command line";
  355. static void
  356. init_display(display *d, const char *program)
  357. {
  358. memset(d, 0, sizeof *d);
  359. d->png_ptr = NULL;
  360. d->info_ptr = d->end_ptr = NULL;
  361. d->error_count = d->warning_count = 0;
  362. d->program = program;
  363. d->file = program;
  364. d->test = init;
  365. }
  366. static void
  367. clean_display(display *d)
  368. {
  369. png_destroy_read_struct(&d->png_ptr, &d->info_ptr, &d->end_ptr);
  370. /* This must not happen - it might cause an app crash */
  371. if (d->png_ptr != NULL || d->info_ptr != NULL || d->end_ptr != NULL)
  372. {
  373. fprintf(stderr, "%s(%s): png_destroy_read_struct error\n", d->file,
  374. d->test);
  375. exit(1);
  376. }
  377. }
  378. PNG_FUNCTION(void, display_exit, (display *d), static PNG_NORETURN)
  379. {
  380. ++(d->error_count);
  381. if (d->png_ptr != NULL)
  382. clean_display(d);
  383. /* During initialization and if this is a single command line argument set
  384. * exit now - there is only one test, otherwise longjmp to do the next test.
  385. */
  386. if (d->test == init || d->test == cmd)
  387. exit(1);
  388. longjmp(d->error_return, 1);
  389. }
  390. static int
  391. display_rc(const display *d, int strict)
  392. {
  393. return d->error_count + (strict ? d->warning_count : 0);
  394. }
  395. /* libpng error and warning callbacks */
  396. PNG_FUNCTION(void, (PNGCBAPI error), (png_structp png_ptr, const char *message),
  397. static PNG_NORETURN)
  398. {
  399. display *d = (display*)png_get_error_ptr(png_ptr);
  400. fprintf(stderr, "%s(%s): libpng error: %s\n", d->file, d->test, message);
  401. display_exit(d);
  402. }
  403. static void PNGCBAPI
  404. warning(png_structp png_ptr, const char *message)
  405. {
  406. display *d = (display*)png_get_error_ptr(png_ptr);
  407. fprintf(stderr, "%s(%s): libpng warning: %s\n", d->file, d->test, message);
  408. ++(d->warning_count);
  409. }
  410. static png_uint_32
  411. get_valid(display *d, png_infop info_ptr)
  412. {
  413. png_uint_32 flags = png_get_valid(d->png_ptr, info_ptr, (png_uint_32)~0);
  414. /* Map the text chunks back into the flags */
  415. {
  416. png_textp text;
  417. png_uint_32 ntext = png_get_text(d->png_ptr, info_ptr, &text, NULL);
  418. while (ntext-- > 0) switch (text[ntext].compression)
  419. {
  420. case -1:
  421. flags |= PNG_INFO_tEXt;
  422. break;
  423. case 0:
  424. flags |= PNG_INFO_zTXt;
  425. break;
  426. case 1:
  427. case 2:
  428. flags |= PNG_INFO_iTXt;
  429. break;
  430. default:
  431. fprintf(stderr, "%s(%s): unknown text compression %d\n", d->file,
  432. d->test, text[ntext].compression);
  433. display_exit(d);
  434. }
  435. }
  436. return flags;
  437. }
  438. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  439. static int PNGCBAPI
  440. read_callback(png_structp pp, png_unknown_chunkp pc)
  441. {
  442. /* This function mimics the behavior of png_set_keep_unknown_chunks by
  443. * returning '0' to keep the chunk and '1' to discard it.
  444. */
  445. display *d = voidcast(display*, png_get_user_chunk_ptr(pp));
  446. int chunk = findb(pc->name);
  447. int keep, discard;
  448. if (chunk < 0) /* not one in our list, so not a known chunk */
  449. keep = d->keep;
  450. else
  451. {
  452. keep = chunk_info[chunk].keep;
  453. if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
  454. {
  455. /* See the comments in png.h - use the default for unknown chunks,
  456. * do not keep known chunks.
  457. */
  458. if (chunk_info[chunk].unknown)
  459. keep = d->keep;
  460. else
  461. keep = PNG_HANDLE_CHUNK_NEVER;
  462. }
  463. }
  464. switch (keep)
  465. {
  466. default:
  467. fprintf(stderr, "%s(%s): %d: unrecognized chunk option\n", d->file,
  468. d->test, chunk_info[chunk].keep);
  469. display_exit(d);
  470. case PNG_HANDLE_CHUNK_AS_DEFAULT:
  471. case PNG_HANDLE_CHUNK_NEVER:
  472. discard = 1/*handled; discard*/;
  473. break;
  474. case PNG_HANDLE_CHUNK_IF_SAFE:
  475. case PNG_HANDLE_CHUNK_ALWAYS:
  476. discard = 0/*not handled; keep*/;
  477. break;
  478. }
  479. /* Also store information about this chunk in the display, the relevant flag
  480. * is set if the chunk is to be kept ('not handled'.)
  481. */
  482. if (chunk >= 0) if (!discard) /* stupidity to stop a GCC warning */
  483. {
  484. png_uint_32 flag = chunk_info[chunk].flag;
  485. if (pc->location & PNG_AFTER_IDAT)
  486. d->after_IDAT |= flag;
  487. else
  488. d->before_IDAT |= flag;
  489. }
  490. /* However if there is no support to store unknown chunks don't ask libpng to
  491. * do it; there will be an png_error.
  492. */
  493. # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  494. return discard;
  495. # else
  496. return 1; /*handled; discard*/
  497. # endif
  498. }
  499. #endif /* READ_USER_CHUNKS_SUPPORTED */
  500. #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  501. static png_uint_32
  502. get_unknown(display *d, png_infop info_ptr, int after_IDAT)
  503. {
  504. /* Create corresponding 'unknown' flags */
  505. png_uint_32 flags = 0;
  506. UNUSED(after_IDAT)
  507. {
  508. png_unknown_chunkp unknown;
  509. int num_unknown = png_get_unknown_chunks(d->png_ptr, info_ptr, &unknown);
  510. while (--num_unknown >= 0)
  511. {
  512. int chunk = findb(unknown[num_unknown].name);
  513. /* Chunks not known to pngunknown must be validated here; since they
  514. * must also be unknown to libpng the 'display->keep' behavior should
  515. * have been used.
  516. */
  517. if (chunk < 0) switch (d->keep)
  518. {
  519. default: /* impossible */
  520. case PNG_HANDLE_CHUNK_AS_DEFAULT:
  521. case PNG_HANDLE_CHUNK_NEVER:
  522. fprintf(stderr, "%s(%s): %s: %s: unknown chunk saved\n",
  523. d->file, d->test, d->keep ? "discard" : "default",
  524. unknown[num_unknown].name);
  525. ++(d->error_count);
  526. break;
  527. case PNG_HANDLE_CHUNK_IF_SAFE:
  528. if (!ancillaryb(unknown[num_unknown].name))
  529. {
  530. fprintf(stderr,
  531. "%s(%s): if-safe: %s: unknown critical chunk saved\n",
  532. d->file, d->test, unknown[num_unknown].name);
  533. ++(d->error_count);
  534. break;
  535. }
  536. /* FALL THROUGH (safe) */
  537. case PNG_HANDLE_CHUNK_ALWAYS:
  538. break;
  539. }
  540. else
  541. flags |= chunk_info[chunk].flag;
  542. }
  543. }
  544. return flags;
  545. }
  546. #else
  547. static png_uint_32
  548. get_unknown(display *d, png_infop info_ptr, int after_IDAT)
  549. /* Otherwise this will return the cached values set by any user callback */
  550. {
  551. UNUSED(info_ptr);
  552. if (after_IDAT)
  553. return d->after_IDAT;
  554. else
  555. return d->before_IDAT;
  556. }
  557. # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
  558. /* The #defines above should mean this is never reached, it's just here as
  559. * a check to ensure the logic is correct.
  560. */
  561. # error No store support and no user chunk support, this will not work
  562. # endif
  563. #endif
  564. static int
  565. check(FILE *fp, int argc, const char **argv, png_uint_32p flags/*out*/,
  566. display *d, int set_callback)
  567. {
  568. int i, npasses, ipass;
  569. png_uint_32 height;
  570. d->keep = PNG_HANDLE_CHUNK_AS_DEFAULT;
  571. d->before_IDAT = 0;
  572. d->after_IDAT = 0;
  573. /* Some of these errors are permanently fatal and cause an exit here, others
  574. * are per-test and cause an error return.
  575. */
  576. d->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, d, error,
  577. warning);
  578. if (d->png_ptr == NULL)
  579. {
  580. fprintf(stderr, "%s(%s): could not allocate png struct\n", d->file,
  581. d->test);
  582. /* Terminate here, this error is not test specific. */
  583. exit(1);
  584. }
  585. d->info_ptr = png_create_info_struct(d->png_ptr);
  586. d->end_ptr = png_create_info_struct(d->png_ptr);
  587. if (d->info_ptr == NULL || d->end_ptr == NULL)
  588. {
  589. fprintf(stderr, "%s(%s): could not allocate png info\n", d->file,
  590. d->test);
  591. clean_display(d);
  592. exit(1);
  593. }
  594. png_init_io(d->png_ptr, fp);
  595. # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  596. /* This is only done if requested by the caller; it interferes with the
  597. * standard store/save mechanism.
  598. */
  599. if (set_callback)
  600. png_set_read_user_chunk_fn(d->png_ptr, d, read_callback);
  601. # else
  602. UNUSED(set_callback)
  603. # endif
  604. /* Handle each argument in turn; multiple settings are possible for the same
  605. * chunk and multiple calls will occur (the last one should override all
  606. * preceding ones).
  607. */
  608. for (i=0; i<argc; ++i)
  609. {
  610. const char *equals = strchr(argv[i], '=');
  611. if (equals != NULL)
  612. {
  613. int chunk, option;
  614. if (strcmp(equals+1, "default") == 0)
  615. option = PNG_HANDLE_CHUNK_AS_DEFAULT;
  616. else if (strcmp(equals+1, "discard") == 0)
  617. option = PNG_HANDLE_CHUNK_NEVER;
  618. else if (strcmp(equals+1, "if-safe") == 0)
  619. option = PNG_HANDLE_CHUNK_IF_SAFE;
  620. else if (strcmp(equals+1, "save") == 0)
  621. option = PNG_HANDLE_CHUNK_ALWAYS;
  622. else
  623. {
  624. fprintf(stderr, "%s(%s): %s: unrecognized chunk option\n", d->file,
  625. d->test, argv[i]);
  626. display_exit(d);
  627. }
  628. switch (equals - argv[i])
  629. {
  630. case 4: /* chunk name */
  631. chunk = find(argv[i]);
  632. if (chunk >= 0)
  633. {
  634. /* These #if tests have the effect of skipping the arguments
  635. * if SAVE support is unavailable - we can't do a useful test
  636. * in this case, so we just check the arguments! This could
  637. * be improved in the future by using the read callback.
  638. */
  639. png_byte name[5];
  640. memcpy(name, chunk_info[chunk].name, 5);
  641. png_set_keep_unknown_chunks(d->png_ptr, option, name, 1);
  642. chunk_info[chunk].keep = option;
  643. continue;
  644. }
  645. break;
  646. case 7: /* default */
  647. if (memcmp(argv[i], "default", 7) == 0)
  648. {
  649. png_set_keep_unknown_chunks(d->png_ptr, option, NULL, 0);
  650. d->keep = option;
  651. continue;
  652. }
  653. break;
  654. case 3: /* all */
  655. if (memcmp(argv[i], "all", 3) == 0)
  656. {
  657. png_set_keep_unknown_chunks(d->png_ptr, option, NULL, -1);
  658. d->keep = option;
  659. for (chunk = 0; chunk < NINFO; ++chunk)
  660. if (chunk_info[chunk].all)
  661. chunk_info[chunk].keep = option;
  662. continue;
  663. }
  664. break;
  665. default: /* some misplaced = */
  666. break;
  667. }
  668. }
  669. fprintf(stderr, "%s(%s): %s: unrecognized chunk argument\n", d->file,
  670. d->test, argv[i]);
  671. display_exit(d);
  672. }
  673. png_read_info(d->png_ptr, d->info_ptr);
  674. switch (png_get_interlace_type(d->png_ptr, d->info_ptr))
  675. {
  676. case PNG_INTERLACE_NONE:
  677. npasses = 1;
  678. break;
  679. case PNG_INTERLACE_ADAM7:
  680. npasses = PNG_INTERLACE_ADAM7_PASSES;
  681. break;
  682. default:
  683. /* Hard error because it is not test specific */
  684. fprintf(stderr, "%s(%s): invalid interlace type\n", d->file, d->test);
  685. clean_display(d);
  686. exit(1);
  687. }
  688. /* Skip the image data, if IDAT is not being handled then don't do this
  689. * because it will cause a CRC error.
  690. */
  691. if (chunk_info[0/*IDAT*/].keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
  692. {
  693. png_start_read_image(d->png_ptr);
  694. height = png_get_image_height(d->png_ptr, d->info_ptr);
  695. if (npasses > 1)
  696. {
  697. png_uint_32 width = png_get_image_width(d->png_ptr, d->info_ptr);
  698. for (ipass=0; ipass<npasses; ++ipass)
  699. {
  700. png_uint_32 wPass = PNG_PASS_COLS(width, ipass);
  701. if (wPass > 0)
  702. {
  703. png_uint_32 y;
  704. for (y=0; y<height; ++y) if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
  705. png_read_row(d->png_ptr, NULL, NULL);
  706. }
  707. }
  708. } /* interlaced */
  709. else /* not interlaced */
  710. {
  711. png_uint_32 y;
  712. for (y=0; y<height; ++y)
  713. png_read_row(d->png_ptr, NULL, NULL);
  714. }
  715. }
  716. png_read_end(d->png_ptr, d->end_ptr);
  717. flags[0] = get_valid(d, d->info_ptr);
  718. flags[1] = get_unknown(d, d->info_ptr, 0/*before IDAT*/);
  719. /* Only png_read_png sets PNG_INFO_IDAT! */
  720. flags[chunk_info[0/*IDAT*/].keep != PNG_HANDLE_CHUNK_AS_DEFAULT] |=
  721. PNG_INFO_IDAT;
  722. flags[2] = get_valid(d, d->end_ptr);
  723. flags[3] = get_unknown(d, d->end_ptr, 1/*after IDAT*/);
  724. clean_display(d);
  725. return d->keep;
  726. }
  727. static void
  728. check_error(display *d, png_uint_32 flags, const char *message)
  729. {
  730. while (flags)
  731. {
  732. png_uint_32 flag = flags & -(png_int_32)flags;
  733. int i = find_by_flag(flag);
  734. fprintf(stderr, "%s(%s): chunk %s: %s\n", d->file, d->test,
  735. chunk_info[i].name, message);
  736. ++(d->error_count);
  737. flags &= ~flag;
  738. }
  739. }
  740. static void
  741. check_handling(display *d, int def, png_uint_32 chunks, png_uint_32 known,
  742. png_uint_32 unknown, const char *position, int set_callback)
  743. {
  744. while (chunks)
  745. {
  746. png_uint_32 flag = chunks & -(png_int_32)chunks;
  747. int i = find_by_flag(flag);
  748. int keep = chunk_info[i].keep;
  749. const char *type;
  750. const char *errorx = NULL;
  751. if (chunk_info[i].unknown)
  752. {
  753. if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
  754. {
  755. type = "UNKNOWN (default)";
  756. keep = def;
  757. }
  758. else
  759. type = "UNKNOWN (specified)";
  760. if (flag & known)
  761. errorx = "chunk processed";
  762. else switch (keep)
  763. {
  764. case PNG_HANDLE_CHUNK_AS_DEFAULT:
  765. if (flag & unknown)
  766. errorx = "DEFAULT: unknown chunk saved";
  767. break;
  768. case PNG_HANDLE_CHUNK_NEVER:
  769. if (flag & unknown)
  770. errorx = "DISCARD: unknown chunk saved";
  771. break;
  772. case PNG_HANDLE_CHUNK_IF_SAFE:
  773. if (ancillary(chunk_info[i].name))
  774. {
  775. if (!(flag & unknown))
  776. errorx = "IF-SAFE: unknown ancillary chunk lost";
  777. }
  778. else if (flag & unknown)
  779. errorx = "IF-SAFE: unknown critical chunk saved";
  780. break;
  781. case PNG_HANDLE_CHUNK_ALWAYS:
  782. if (!(flag & unknown))
  783. errorx = "SAVE: unknown chunk lost";
  784. break;
  785. default:
  786. errorx = "internal error: bad keep";
  787. break;
  788. }
  789. } /* unknown chunk */
  790. else /* known chunk */
  791. {
  792. type = "KNOWN";
  793. if (flag & known)
  794. {
  795. /* chunk was processed, it won't have been saved because that is
  796. * caught below when checking for inconsistent processing.
  797. */
  798. if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
  799. errorx = "!DEFAULT: known chunk processed";
  800. }
  801. else /* not processed */ switch (keep)
  802. {
  803. case PNG_HANDLE_CHUNK_AS_DEFAULT:
  804. errorx = "DEFAULT: known chunk not processed";
  805. break;
  806. case PNG_HANDLE_CHUNK_NEVER:
  807. if (flag & unknown)
  808. errorx = "DISCARD: known chunk saved";
  809. break;
  810. case PNG_HANDLE_CHUNK_IF_SAFE:
  811. if (ancillary(chunk_info[i].name))
  812. {
  813. if (!(flag & unknown))
  814. errorx = "IF-SAFE: known ancillary chunk lost";
  815. }
  816. else if (flag & unknown)
  817. errorx = "IF-SAFE: known critical chunk saved";
  818. break;
  819. case PNG_HANDLE_CHUNK_ALWAYS:
  820. if (!(flag & unknown))
  821. errorx = "SAVE: known chunk lost";
  822. break;
  823. default:
  824. errorx = "internal error: bad keep (2)";
  825. break;
  826. }
  827. }
  828. if (errorx != NULL)
  829. {
  830. ++(d->error_count);
  831. fprintf(stderr, "%s(%s%s): %s %s %s: %s\n", d->file, d->test,
  832. set_callback ? ",callback" : "",
  833. type, chunk_info[i].name, position, errorx);
  834. }
  835. chunks &= ~flag;
  836. }
  837. }
  838. static void
  839. perform_one_test(FILE *fp, int argc, const char **argv,
  840. png_uint_32 *default_flags, display *d, int set_callback)
  841. {
  842. int def;
  843. png_uint_32 flags[2][4];
  844. rewind(fp);
  845. clear_keep();
  846. memcpy(flags[0], default_flags, sizeof flags[0]);
  847. def = check(fp, argc, argv, flags[1], d, set_callback);
  848. /* Chunks should either be known or unknown, never both and this should apply
  849. * whether the chunk is before or after the IDAT (actually, the app can
  850. * probably change this by swapping the handling after the image, but this
  851. * test does not do that.)
  852. */
  853. check_error(d, (flags[0][0]|flags[0][2]) & (flags[0][1]|flags[0][3]),
  854. "chunk handled inconsistently in count tests");
  855. check_error(d, (flags[1][0]|flags[1][2]) & (flags[1][1]|flags[1][3]),
  856. "chunk handled inconsistently in option tests");
  857. /* Now find out what happened to each chunk before and after the IDAT and
  858. * determine if the behavior was correct. First some basic sanity checks,
  859. * any known chunk should be known in the original count, any unknown chunk
  860. * should be either known or unknown in the original.
  861. */
  862. {
  863. png_uint_32 test;
  864. test = flags[1][0] & ~flags[0][0];
  865. check_error(d, test, "new known chunk before IDAT");
  866. test = flags[1][1] & ~(flags[0][0] | flags[0][1]);
  867. check_error(d, test, "new unknown chunk before IDAT");
  868. test = flags[1][2] & ~flags[0][2];
  869. check_error(d, test, "new known chunk after IDAT");
  870. test = flags[1][3] & ~(flags[0][2] | flags[0][3]);
  871. check_error(d, test, "new unknown chunk after IDAT");
  872. }
  873. /* Now each chunk in the original list should have been handled according to
  874. * the options set for that chunk, regardless of whether libpng knows about
  875. * it or not.
  876. */
  877. check_handling(d, def, flags[0][0] | flags[0][1], flags[1][0], flags[1][1],
  878. "before IDAT", set_callback);
  879. check_handling(d, def, flags[0][2] | flags[0][3], flags[1][2], flags[1][3],
  880. "after IDAT", set_callback);
  881. }
  882. static void
  883. perform_one_test_safe(FILE *fp, int argc, const char **argv,
  884. png_uint_32 *default_flags, display *d, const char *test)
  885. {
  886. if (setjmp(d->error_return) == 0)
  887. {
  888. d->test = test; /* allow use of d->error_return */
  889. # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  890. perform_one_test(fp, argc, argv, default_flags, d, 0);
  891. # endif
  892. # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  893. perform_one_test(fp, argc, argv, default_flags, d, 1);
  894. # endif
  895. d->test = init; /* prevent use of d->error_return */
  896. }
  897. }
  898. static const char *standard_tests[] =
  899. {
  900. "discard", "default=discard", 0,
  901. "save", "default=save", 0,
  902. "if-safe", "default=if-safe", 0,
  903. "vpAg", "vpAg=if-safe", 0,
  904. "sTER", "sTER=if-safe", 0,
  905. "IDAT", "default=discard", "IDAT=save", 0,
  906. "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
  907. "sBIT=save", "sRGB=save", 0,
  908. 0/*end*/
  909. };
  910. static PNG_NORETURN void
  911. usage(const char *program, const char *reason)
  912. {
  913. fprintf(stderr, "pngunknown: %s: usage:\n %s [--strict] "
  914. "--default|{(CHNK|default|all)=(default|discard|if-safe|save)} "
  915. "testfile.png\n", reason, program);
  916. exit(99);
  917. }
  918. int
  919. main(int argc, const char **argv)
  920. {
  921. FILE *fp;
  922. png_uint_32 default_flags[4/*valid,unknown{before,after}*/];
  923. int strict = 0, default_tests = 0;
  924. const char *count_argv = "default=save";
  925. const char *touch_file = NULL;
  926. display d;
  927. init_display(&d, argv[0]);
  928. while (++argv, --argc > 0)
  929. {
  930. if (strcmp(*argv, "--strict") == 0)
  931. strict = 1;
  932. else if (strcmp(*argv, "--default") == 0)
  933. default_tests = 1;
  934. else if (strcmp(*argv, "--touch") == 0)
  935. {
  936. if (argc > 1)
  937. touch_file = *++argv, --argc;
  938. else
  939. usage(d.program, "--touch: missing file name");
  940. }
  941. else
  942. break;
  943. }
  944. /* A file name is required, but there should be no other arguments if
  945. * --default was specified.
  946. */
  947. if (argc <= 0)
  948. usage(d.program, "missing test file");
  949. /* GCC BUG: if (default_tests && argc != 1) triggers some weird GCC argc
  950. * optimization which causes warnings with -Wstrict-overflow!
  951. */
  952. else if (default_tests) if (argc != 1)
  953. usage(d.program, "extra arguments");
  954. /* The name of the test file is the last argument; remove it. */
  955. d.file = argv[--argc];
  956. fp = fopen(d.file, "rb");
  957. if (fp == NULL)
  958. {
  959. perror(d.file);
  960. exit(99);
  961. }
  962. /* First find all the chunks, known and unknown, in the test file, a failure
  963. * here aborts the whole test.
  964. *
  965. * If 'save' is supported then the normal saving method should happen,
  966. * otherwise if 'read' is supported then the read callback will do the
  967. * same thing. If both are supported the 'read' callback won't be
  968. * instantiated by default. If 'save' is *not* supported then a user
  969. * callback is required even though we can call png_get_unknown_chunks.
  970. */
  971. if (check(fp, 1, &count_argv, default_flags, &d,
  972. # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  973. 0
  974. # else
  975. 1
  976. # endif
  977. ) != PNG_HANDLE_CHUNK_ALWAYS)
  978. {
  979. fprintf(stderr, "%s: %s: internal error\n", d.program, d.file);
  980. exit(99);
  981. }
  982. /* Now find what the various supplied options cause to change: */
  983. if (!default_tests)
  984. {
  985. d.test = cmd; /* acts as a flag to say exit, do not longjmp */
  986. # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  987. perform_one_test(fp, argc, argv, default_flags, &d, 0);
  988. # endif
  989. # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  990. perform_one_test(fp, argc, argv, default_flags, &d, 1);
  991. # endif
  992. d.test = init;
  993. }
  994. else
  995. {
  996. const char **test = standard_tests;
  997. /* Set the exit_test pointer here so we can continue after a libpng error.
  998. * NOTE: this leaks memory because the png_struct data from the failing
  999. * test is never freed.
  1000. */
  1001. while (*test)
  1002. {
  1003. const char *this_test = *test++;
  1004. const char **next = test;
  1005. int count = display_rc(&d, strict), new_count;
  1006. const char *result;
  1007. int arg_count = 0;
  1008. while (*next) ++next, ++arg_count;
  1009. perform_one_test_safe(fp, arg_count, test, default_flags, &d,
  1010. this_test);
  1011. new_count = display_rc(&d, strict);
  1012. if (new_count == count)
  1013. result = "PASS";
  1014. else
  1015. result = "FAIL";
  1016. printf("%s: %s %s\n", result, d.program, this_test);
  1017. test = next+1;
  1018. }
  1019. }
  1020. fclose(fp);
  1021. if (display_rc(&d, strict) == 0)
  1022. {
  1023. /* Success, touch the success file if appropriate */
  1024. if (touch_file != NULL)
  1025. {
  1026. FILE *fsuccess = fopen(touch_file, "wt");
  1027. if (fsuccess != NULL)
  1028. {
  1029. int err = 0;
  1030. fprintf(fsuccess, "PNG unknown tests succeeded\n");
  1031. fflush(fsuccess);
  1032. err = ferror(fsuccess);
  1033. if (fclose(fsuccess) || err)
  1034. {
  1035. fprintf(stderr, "%s: write failed\n", touch_file);
  1036. exit(99);
  1037. }
  1038. }
  1039. else
  1040. {
  1041. fprintf(stderr, "%s: open failed\n", touch_file);
  1042. exit(99);
  1043. }
  1044. }
  1045. return 0;
  1046. }
  1047. return 1;
  1048. }
  1049. #else /* !(READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS) */
  1050. int
  1051. main(void)
  1052. {
  1053. fprintf(stderr,
  1054. " test ignored: no support to find out about unknown chunks\n");
  1055. /* So the test is skipped: */
  1056. return 77;
  1057. }
  1058. #endif /* READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS */
  1059. #else /* !(SET_UNKNOWN_CHUNKS && READ) */
  1060. int
  1061. main(void)
  1062. {
  1063. fprintf(stderr,
  1064. " test ignored: no support to modify unknown chunk handling\n");
  1065. /* So the test is skipped: */
  1066. return 77;
  1067. }
  1068. #endif /* SET_UNKNOWN_CHUNKS && READ*/