pngunknown.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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_SAVE_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_SAVE_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_SAVE_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. # if PNG_LIBPNG_VER >= 10700 &&\
  640. !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
  641. if (option < PNG_HANDLE_CHUNK_IF_SAFE)
  642. # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
  643. {
  644. png_byte name[5];
  645. memcpy(name, chunk_info[chunk].name, 5);
  646. png_set_keep_unknown_chunks(d->png_ptr, option, name, 1);
  647. chunk_info[chunk].keep = option;
  648. }
  649. continue;
  650. }
  651. break;
  652. case 7: /* default */
  653. if (memcmp(argv[i], "default", 7) == 0)
  654. {
  655. # if PNG_LIBPNG_VER >= 10700 &&\
  656. !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
  657. if (option < PNG_HANDLE_CHUNK_IF_SAFE)
  658. # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
  659. png_set_keep_unknown_chunks(d->png_ptr, option, NULL, 0);
  660. d->keep = option;
  661. continue;
  662. }
  663. break;
  664. case 3: /* all */
  665. if (memcmp(argv[i], "all", 3) == 0)
  666. {
  667. # if PNG_LIBPNG_VER >= 10700 &&\
  668. !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
  669. if (option < PNG_HANDLE_CHUNK_IF_SAFE)
  670. # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
  671. png_set_keep_unknown_chunks(d->png_ptr, option, NULL, -1);
  672. d->keep = option;
  673. for (chunk = 0; chunk < NINFO; ++chunk)
  674. if (chunk_info[chunk].all)
  675. chunk_info[chunk].keep = option;
  676. continue;
  677. }
  678. break;
  679. default: /* some misplaced = */
  680. break;
  681. }
  682. }
  683. fprintf(stderr, "%s(%s): %s: unrecognized chunk argument\n", d->file,
  684. d->test, argv[i]);
  685. display_exit(d);
  686. }
  687. png_read_info(d->png_ptr, d->info_ptr);
  688. switch (png_get_interlace_type(d->png_ptr, d->info_ptr))
  689. {
  690. case PNG_INTERLACE_NONE:
  691. npasses = 1;
  692. break;
  693. case PNG_INTERLACE_ADAM7:
  694. npasses = PNG_INTERLACE_ADAM7_PASSES;
  695. break;
  696. default:
  697. /* Hard error because it is not test specific */
  698. fprintf(stderr, "%s(%s): invalid interlace type\n", d->file, d->test);
  699. clean_display(d);
  700. exit(1);
  701. }
  702. /* Skip the image data, if IDAT is not being handled then don't do this
  703. * because it will cause a CRC error.
  704. */
  705. if (chunk_info[0/*IDAT*/].keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
  706. {
  707. png_start_read_image(d->png_ptr);
  708. height = png_get_image_height(d->png_ptr, d->info_ptr);
  709. if (npasses > 1)
  710. {
  711. png_uint_32 width = png_get_image_width(d->png_ptr, d->info_ptr);
  712. for (ipass=0; ipass<npasses; ++ipass)
  713. {
  714. png_uint_32 wPass = PNG_PASS_COLS(width, ipass);
  715. if (wPass > 0)
  716. {
  717. png_uint_32 y;
  718. for (y=0; y<height; ++y) if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
  719. png_read_row(d->png_ptr, NULL, NULL);
  720. }
  721. }
  722. } /* interlaced */
  723. else /* not interlaced */
  724. {
  725. png_uint_32 y;
  726. for (y=0; y<height; ++y)
  727. png_read_row(d->png_ptr, NULL, NULL);
  728. }
  729. }
  730. png_read_end(d->png_ptr, d->end_ptr);
  731. flags[0] = get_valid(d, d->info_ptr);
  732. flags[1] = get_unknown(d, d->info_ptr, 0/*before IDAT*/);
  733. /* Only png_read_png sets PNG_INFO_IDAT! */
  734. flags[chunk_info[0/*IDAT*/].keep != PNG_HANDLE_CHUNK_AS_DEFAULT] |=
  735. PNG_INFO_IDAT;
  736. flags[2] = get_valid(d, d->end_ptr);
  737. flags[3] = get_unknown(d, d->end_ptr, 1/*after IDAT*/);
  738. clean_display(d);
  739. return d->keep;
  740. }
  741. static void
  742. check_error(display *d, png_uint_32 flags, const char *message)
  743. {
  744. while (flags)
  745. {
  746. png_uint_32 flag = flags & -(png_int_32)flags;
  747. int i = find_by_flag(flag);
  748. fprintf(stderr, "%s(%s): chunk %s: %s\n", d->file, d->test,
  749. chunk_info[i].name, message);
  750. ++(d->error_count);
  751. flags &= ~flag;
  752. }
  753. }
  754. static void
  755. check_handling(display *d, int def, png_uint_32 chunks, png_uint_32 known,
  756. png_uint_32 unknown, const char *position, int set_callback)
  757. {
  758. while (chunks)
  759. {
  760. png_uint_32 flag = chunks & -(png_int_32)chunks;
  761. int i = find_by_flag(flag);
  762. int keep = chunk_info[i].keep;
  763. const char *type;
  764. const char *errorx = NULL;
  765. if (chunk_info[i].unknown)
  766. {
  767. if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
  768. {
  769. type = "UNKNOWN (default)";
  770. keep = def;
  771. }
  772. else
  773. type = "UNKNOWN (specified)";
  774. if (flag & known)
  775. errorx = "chunk processed";
  776. else switch (keep)
  777. {
  778. case PNG_HANDLE_CHUNK_AS_DEFAULT:
  779. if (flag & unknown)
  780. errorx = "DEFAULT: unknown chunk saved";
  781. break;
  782. case PNG_HANDLE_CHUNK_NEVER:
  783. if (flag & unknown)
  784. errorx = "DISCARD: unknown chunk saved";
  785. break;
  786. case PNG_HANDLE_CHUNK_IF_SAFE:
  787. if (ancillary(chunk_info[i].name))
  788. {
  789. if (!(flag & unknown))
  790. errorx = "IF-SAFE: unknown ancillary chunk lost";
  791. }
  792. else if (flag & unknown)
  793. errorx = "IF-SAFE: unknown critical chunk saved";
  794. break;
  795. case PNG_HANDLE_CHUNK_ALWAYS:
  796. if (!(flag & unknown))
  797. errorx = "SAVE: unknown chunk lost";
  798. break;
  799. default:
  800. errorx = "internal error: bad keep";
  801. break;
  802. }
  803. } /* unknown chunk */
  804. else /* known chunk */
  805. {
  806. type = "KNOWN";
  807. if (flag & known)
  808. {
  809. /* chunk was processed, it won't have been saved because that is
  810. * caught below when checking for inconsistent processing.
  811. */
  812. if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
  813. errorx = "!DEFAULT: known chunk processed";
  814. }
  815. else /* not processed */ switch (keep)
  816. {
  817. case PNG_HANDLE_CHUNK_AS_DEFAULT:
  818. errorx = "DEFAULT: known chunk not processed";
  819. break;
  820. case PNG_HANDLE_CHUNK_NEVER:
  821. if (flag & unknown)
  822. errorx = "DISCARD: known chunk saved";
  823. break;
  824. case PNG_HANDLE_CHUNK_IF_SAFE:
  825. if (ancillary(chunk_info[i].name))
  826. {
  827. if (!(flag & unknown))
  828. errorx = "IF-SAFE: known ancillary chunk lost";
  829. }
  830. else if (flag & unknown)
  831. errorx = "IF-SAFE: known critical chunk saved";
  832. break;
  833. case PNG_HANDLE_CHUNK_ALWAYS:
  834. if (!(flag & unknown))
  835. errorx = "SAVE: known chunk lost";
  836. break;
  837. default:
  838. errorx = "internal error: bad keep (2)";
  839. break;
  840. }
  841. }
  842. if (errorx != NULL)
  843. {
  844. ++(d->error_count);
  845. fprintf(stderr, "%s(%s%s): %s %s %s: %s\n", d->file, d->test,
  846. set_callback ? ",callback" : "",
  847. type, chunk_info[i].name, position, errorx);
  848. }
  849. chunks &= ~flag;
  850. }
  851. }
  852. static void
  853. perform_one_test(FILE *fp, int argc, const char **argv,
  854. png_uint_32 *default_flags, display *d, int set_callback)
  855. {
  856. int def;
  857. png_uint_32 flags[2][4];
  858. rewind(fp);
  859. clear_keep();
  860. memcpy(flags[0], default_flags, sizeof flags[0]);
  861. def = check(fp, argc, argv, flags[1], d, set_callback);
  862. /* Chunks should either be known or unknown, never both and this should apply
  863. * whether the chunk is before or after the IDAT (actually, the app can
  864. * probably change this by swapping the handling after the image, but this
  865. * test does not do that.)
  866. */
  867. check_error(d, (flags[0][0]|flags[0][2]) & (flags[0][1]|flags[0][3]),
  868. "chunk handled inconsistently in count tests");
  869. check_error(d, (flags[1][0]|flags[1][2]) & (flags[1][1]|flags[1][3]),
  870. "chunk handled inconsistently in option tests");
  871. /* Now find out what happened to each chunk before and after the IDAT and
  872. * determine if the behavior was correct. First some basic sanity checks,
  873. * any known chunk should be known in the original count, any unknown chunk
  874. * should be either known or unknown in the original.
  875. */
  876. {
  877. png_uint_32 test;
  878. test = flags[1][0] & ~flags[0][0];
  879. check_error(d, test, "new known chunk before IDAT");
  880. test = flags[1][1] & ~(flags[0][0] | flags[0][1]);
  881. check_error(d, test, "new unknown chunk before IDAT");
  882. test = flags[1][2] & ~flags[0][2];
  883. check_error(d, test, "new known chunk after IDAT");
  884. test = flags[1][3] & ~(flags[0][2] | flags[0][3]);
  885. check_error(d, test, "new unknown chunk after IDAT");
  886. }
  887. /* Now each chunk in the original list should have been handled according to
  888. * the options set for that chunk, regardless of whether libpng knows about
  889. * it or not.
  890. */
  891. check_handling(d, def, flags[0][0] | flags[0][1], flags[1][0], flags[1][1],
  892. "before IDAT", set_callback);
  893. check_handling(d, def, flags[0][2] | flags[0][3], flags[1][2], flags[1][3],
  894. "after IDAT", set_callback);
  895. }
  896. static void
  897. perform_one_test_safe(FILE *fp, int argc, const char **argv,
  898. png_uint_32 *default_flags, display *d, const char *test)
  899. {
  900. if (setjmp(d->error_return) == 0)
  901. {
  902. d->test = test; /* allow use of d->error_return */
  903. # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  904. perform_one_test(fp, argc, argv, default_flags, d, 0);
  905. # endif
  906. # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  907. perform_one_test(fp, argc, argv, default_flags, d, 1);
  908. # endif
  909. d->test = init; /* prevent use of d->error_return */
  910. }
  911. }
  912. static const char *standard_tests[] =
  913. {
  914. "discard", "default=discard", 0,
  915. "save", "default=save", 0,
  916. "if-safe", "default=if-safe", 0,
  917. "vpAg", "vpAg=if-safe", 0,
  918. "sTER", "sTER=if-safe", 0,
  919. "IDAT", "default=discard", "IDAT=save", 0,
  920. "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
  921. "sBIT=save", "sRGB=save", 0,
  922. 0/*end*/
  923. };
  924. static PNG_NORETURN void
  925. usage(const char *program, const char *reason)
  926. {
  927. fprintf(stderr, "pngunknown: %s: usage:\n %s [--strict] "
  928. "--default|{(CHNK|default|all)=(default|discard|if-safe|save)} "
  929. "testfile.png\n", reason, program);
  930. exit(99);
  931. }
  932. int
  933. main(int argc, const char **argv)
  934. {
  935. FILE *fp;
  936. png_uint_32 default_flags[4/*valid,unknown{before,after}*/];
  937. int strict = 0, default_tests = 0;
  938. const char *count_argv = "default=save";
  939. const char *touch_file = NULL;
  940. display d;
  941. init_display(&d, argv[0]);
  942. while (++argv, --argc > 0)
  943. {
  944. if (strcmp(*argv, "--strict") == 0)
  945. strict = 1;
  946. else if (strcmp(*argv, "--default") == 0)
  947. default_tests = 1;
  948. else if (strcmp(*argv, "--touch") == 0)
  949. {
  950. if (argc > 1)
  951. touch_file = *++argv, --argc;
  952. else
  953. usage(d.program, "--touch: missing file name");
  954. }
  955. else
  956. break;
  957. }
  958. /* A file name is required, but there should be no other arguments if
  959. * --default was specified.
  960. */
  961. if (argc <= 0)
  962. usage(d.program, "missing test file");
  963. /* GCC BUG: if (default_tests && argc != 1) triggers some weird GCC argc
  964. * optimization which causes warnings with -Wstrict-overflow!
  965. */
  966. else if (default_tests) if (argc != 1)
  967. usage(d.program, "extra arguments");
  968. /* The name of the test file is the last argument; remove it. */
  969. d.file = argv[--argc];
  970. fp = fopen(d.file, "rb");
  971. if (fp == NULL)
  972. {
  973. perror(d.file);
  974. exit(99);
  975. }
  976. /* First find all the chunks, known and unknown, in the test file, a failure
  977. * here aborts the whole test.
  978. *
  979. * If 'save' is supported then the normal saving method should happen,
  980. * otherwise if 'read' is supported then the read callback will do the
  981. * same thing. If both are supported the 'read' callback won't be
  982. * instantiated by default. If 'save' is *not* supported then a user
  983. * callback is required even though we can call png_get_unknown_chunks.
  984. */
  985. if (check(fp, 1, &count_argv, default_flags, &d,
  986. # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  987. 0
  988. # else
  989. 1
  990. # endif
  991. ) != PNG_HANDLE_CHUNK_ALWAYS)
  992. {
  993. fprintf(stderr, "%s: %s: internal error\n", d.program, d.file);
  994. exit(99);
  995. }
  996. /* Now find what the various supplied options cause to change: */
  997. if (!default_tests)
  998. {
  999. d.test = cmd; /* acts as a flag to say exit, do not longjmp */
  1000. # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  1001. perform_one_test(fp, argc, argv, default_flags, &d, 0);
  1002. # endif
  1003. # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  1004. perform_one_test(fp, argc, argv, default_flags, &d, 1);
  1005. # endif
  1006. d.test = init;
  1007. }
  1008. else
  1009. {
  1010. const char **test = standard_tests;
  1011. /* Set the exit_test pointer here so we can continue after a libpng error.
  1012. * NOTE: this leaks memory because the png_struct data from the failing
  1013. * test is never freed.
  1014. */
  1015. while (*test)
  1016. {
  1017. const char *this_test = *test++;
  1018. const char **next = test;
  1019. int count = display_rc(&d, strict), new_count;
  1020. const char *result;
  1021. int arg_count = 0;
  1022. while (*next) ++next, ++arg_count;
  1023. perform_one_test_safe(fp, arg_count, test, default_flags, &d,
  1024. this_test);
  1025. new_count = display_rc(&d, strict);
  1026. if (new_count == count)
  1027. result = "PASS";
  1028. else
  1029. result = "FAIL";
  1030. printf("%s: %s %s\n", result, d.program, this_test);
  1031. test = next+1;
  1032. }
  1033. }
  1034. fclose(fp);
  1035. if (display_rc(&d, strict) == 0)
  1036. {
  1037. /* Success, touch the success file if appropriate */
  1038. if (touch_file != NULL)
  1039. {
  1040. FILE *fsuccess = fopen(touch_file, "wt");
  1041. if (fsuccess != NULL)
  1042. {
  1043. int err = 0;
  1044. fprintf(fsuccess, "PNG unknown tests succeeded\n");
  1045. fflush(fsuccess);
  1046. err = ferror(fsuccess);
  1047. if (fclose(fsuccess) || err)
  1048. {
  1049. fprintf(stderr, "%s: write failed\n", touch_file);
  1050. exit(99);
  1051. }
  1052. }
  1053. else
  1054. {
  1055. fprintf(stderr, "%s: open failed\n", touch_file);
  1056. exit(99);
  1057. }
  1058. }
  1059. return 0;
  1060. }
  1061. return 1;
  1062. }
  1063. #else /* !(READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS) */
  1064. int
  1065. main(void)
  1066. {
  1067. fprintf(stderr,
  1068. " test ignored: no support to find out about unknown chunks\n");
  1069. /* So the test is skipped: */
  1070. return 77;
  1071. }
  1072. #endif /* READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS */
  1073. #else /* !(SET_UNKNOWN_CHUNKS && READ) */
  1074. int
  1075. main(void)
  1076. {
  1077. fprintf(stderr,
  1078. " test ignored: no support to modify unknown chunk handling\n");
  1079. /* So the test is skipped: */
  1080. return 77;
  1081. }
  1082. #endif /* SET_UNKNOWN_CHUNKS && READ*/