Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1803 lines
50KB

  1. /* pngset.c - storage of image information into info struct
  2. *
  3. * Copyright (c) 2018 Cosmin Truta
  4. * Copyright (c) 1998-2018 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * The functions here are used during reads to store data from the file
  13. * into the info struct, and during writes to store application data
  14. * into the info struct for writing into the file. This abstracts the
  15. * info struct and allows us to change the structure in the future.
  16. */
  17. #include "pngpriv.h"
  18. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  19. #ifdef PNG_bKGD_SUPPORTED
  20. void PNGAPI
  21. png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
  22. png_const_color_16p background)
  23. {
  24. png_debug1(1, "in %s storage function", "bKGD");
  25. if (png_ptr == NULL || info_ptr == NULL || background == NULL)
  26. return;
  27. info_ptr->background = *background;
  28. info_ptr->valid |= PNG_INFO_bKGD;
  29. }
  30. #endif
  31. #ifdef PNG_cHRM_SUPPORTED
  32. void PNGFAPI
  33. png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
  34. png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
  35. png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
  36. png_fixed_point blue_x, png_fixed_point blue_y)
  37. {
  38. png_xy xy;
  39. png_debug1(1, "in %s storage function", "cHRM fixed");
  40. if (png_ptr == NULL || info_ptr == NULL)
  41. return;
  42. xy.redx = red_x;
  43. xy.redy = red_y;
  44. xy.greenx = green_x;
  45. xy.greeny = green_y;
  46. xy.bluex = blue_x;
  47. xy.bluey = blue_y;
  48. xy.whitex = white_x;
  49. xy.whitey = white_y;
  50. if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy,
  51. 2/* override with app values*/) != 0)
  52. info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
  53. png_colorspace_sync_info(png_ptr, info_ptr);
  54. }
  55. void PNGFAPI
  56. png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
  57. png_fixed_point int_red_X, png_fixed_point int_red_Y,
  58. png_fixed_point int_red_Z, png_fixed_point int_green_X,
  59. png_fixed_point int_green_Y, png_fixed_point int_green_Z,
  60. png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
  61. png_fixed_point int_blue_Z)
  62. {
  63. png_XYZ XYZ;
  64. png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
  65. if (png_ptr == NULL || info_ptr == NULL)
  66. return;
  67. XYZ.red_X = int_red_X;
  68. XYZ.red_Y = int_red_Y;
  69. XYZ.red_Z = int_red_Z;
  70. XYZ.green_X = int_green_X;
  71. XYZ.green_Y = int_green_Y;
  72. XYZ.green_Z = int_green_Z;
  73. XYZ.blue_X = int_blue_X;
  74. XYZ.blue_Y = int_blue_Y;
  75. XYZ.blue_Z = int_blue_Z;
  76. if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace,
  77. &XYZ, 2) != 0)
  78. info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
  79. png_colorspace_sync_info(png_ptr, info_ptr);
  80. }
  81. # ifdef PNG_FLOATING_POINT_SUPPORTED
  82. void PNGAPI
  83. png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
  84. double white_x, double white_y, double red_x, double red_y,
  85. double green_x, double green_y, double blue_x, double blue_y)
  86. {
  87. png_set_cHRM_fixed(png_ptr, info_ptr,
  88. png_fixed(png_ptr, white_x, "cHRM White X"),
  89. png_fixed(png_ptr, white_y, "cHRM White Y"),
  90. png_fixed(png_ptr, red_x, "cHRM Red X"),
  91. png_fixed(png_ptr, red_y, "cHRM Red Y"),
  92. png_fixed(png_ptr, green_x, "cHRM Green X"),
  93. png_fixed(png_ptr, green_y, "cHRM Green Y"),
  94. png_fixed(png_ptr, blue_x, "cHRM Blue X"),
  95. png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
  96. }
  97. void PNGAPI
  98. png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
  99. double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
  100. double blue_X, double blue_Y, double blue_Z)
  101. {
  102. png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
  103. png_fixed(png_ptr, red_X, "cHRM Red X"),
  104. png_fixed(png_ptr, red_Y, "cHRM Red Y"),
  105. png_fixed(png_ptr, red_Z, "cHRM Red Z"),
  106. png_fixed(png_ptr, green_X, "cHRM Green X"),
  107. png_fixed(png_ptr, green_Y, "cHRM Green Y"),
  108. png_fixed(png_ptr, green_Z, "cHRM Green Z"),
  109. png_fixed(png_ptr, blue_X, "cHRM Blue X"),
  110. png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),
  111. png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));
  112. }
  113. # endif /* FLOATING_POINT */
  114. #endif /* cHRM */
  115. #ifdef PNG_eXIf_SUPPORTED
  116. void PNGAPI
  117. png_set_eXIf(png_const_structrp png_ptr, png_inforp info_ptr,
  118. png_bytep eXIf_buf)
  119. {
  120. png_warning(png_ptr, "png_set_eXIf does not work; use png_set_eXIf_1");
  121. PNG_UNUSED(info_ptr)
  122. PNG_UNUSED(eXIf_buf)
  123. }
  124. void PNGAPI
  125. png_set_eXIf_1(png_const_structrp png_ptr, png_inforp info_ptr,
  126. png_uint_32 num_exif, png_bytep eXIf_buf)
  127. {
  128. int i;
  129. png_debug1(1, "in %s storage function", "eXIf");
  130. if (png_ptr == NULL || info_ptr == NULL)
  131. return;
  132. if (info_ptr->exif)
  133. {
  134. png_free(png_ptr, info_ptr->exif);
  135. info_ptr->exif = NULL;
  136. }
  137. info_ptr->num_exif = num_exif;
  138. info_ptr->exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr,
  139. info_ptr->num_exif));
  140. if (info_ptr->exif == NULL)
  141. {
  142. png_warning(png_ptr, "Insufficient memory for eXIf chunk data");
  143. return;
  144. }
  145. info_ptr->free_me |= PNG_FREE_EXIF;
  146. for (i = 0; i < (int) info_ptr->num_exif; i++)
  147. info_ptr->exif[i] = eXIf_buf[i];
  148. info_ptr->valid |= PNG_INFO_eXIf;
  149. }
  150. #endif /* eXIf */
  151. #ifdef PNG_gAMA_SUPPORTED
  152. void PNGFAPI
  153. png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
  154. png_fixed_point file_gamma)
  155. {
  156. png_debug1(1, "in %s storage function", "gAMA");
  157. if (png_ptr == NULL || info_ptr == NULL)
  158. return;
  159. png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma);
  160. png_colorspace_sync_info(png_ptr, info_ptr);
  161. }
  162. # ifdef PNG_FLOATING_POINT_SUPPORTED
  163. void PNGAPI
  164. png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
  165. {
  166. png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
  167. "png_set_gAMA"));
  168. }
  169. # endif
  170. #endif
  171. #ifdef PNG_hIST_SUPPORTED
  172. void PNGAPI
  173. png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
  174. png_const_uint_16p hist)
  175. {
  176. int i;
  177. png_debug1(1, "in %s storage function", "hIST");
  178. if (png_ptr == NULL || info_ptr == NULL)
  179. return;
  180. if (info_ptr->num_palette == 0 || info_ptr->num_palette
  181. > PNG_MAX_PALETTE_LENGTH)
  182. {
  183. png_warning(png_ptr,
  184. "Invalid palette size, hIST allocation skipped");
  185. return;
  186. }
  187. png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
  188. /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
  189. * version 1.2.1
  190. */
  191. info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
  192. PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
  193. if (info_ptr->hist == NULL)
  194. {
  195. png_warning(png_ptr, "Insufficient memory for hIST chunk data");
  196. return;
  197. }
  198. info_ptr->free_me |= PNG_FREE_HIST;
  199. for (i = 0; i < info_ptr->num_palette; i++)
  200. info_ptr->hist[i] = hist[i];
  201. info_ptr->valid |= PNG_INFO_hIST;
  202. }
  203. #endif
  204. void PNGAPI
  205. png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
  206. png_uint_32 width, png_uint_32 height, int bit_depth,
  207. int color_type, int interlace_type, int compression_type,
  208. int filter_type)
  209. {
  210. png_debug1(1, "in %s storage function", "IHDR");
  211. if (png_ptr == NULL || info_ptr == NULL)
  212. return;
  213. info_ptr->width = width;
  214. info_ptr->height = height;
  215. info_ptr->bit_depth = (png_byte)bit_depth;
  216. info_ptr->color_type = (png_byte)color_type;
  217. info_ptr->compression_type = (png_byte)compression_type;
  218. info_ptr->filter_type = (png_byte)filter_type;
  219. info_ptr->interlace_type = (png_byte)interlace_type;
  220. png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
  221. info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
  222. info_ptr->compression_type, info_ptr->filter_type);
  223. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  224. info_ptr->channels = 1;
  225. else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  226. info_ptr->channels = 3;
  227. else
  228. info_ptr->channels = 1;
  229. if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  230. info_ptr->channels++;
  231. info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
  232. info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
  233. }
  234. #ifdef PNG_oFFs_SUPPORTED
  235. void PNGAPI
  236. png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
  237. png_int_32 offset_x, png_int_32 offset_y, int unit_type)
  238. {
  239. png_debug1(1, "in %s storage function", "oFFs");
  240. if (png_ptr == NULL || info_ptr == NULL)
  241. return;
  242. info_ptr->x_offset = offset_x;
  243. info_ptr->y_offset = offset_y;
  244. info_ptr->offset_unit_type = (png_byte)unit_type;
  245. info_ptr->valid |= PNG_INFO_oFFs;
  246. }
  247. #endif
  248. #ifdef PNG_pCAL_SUPPORTED
  249. void PNGAPI
  250. png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
  251. png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
  252. int nparams, png_const_charp units, png_charpp params)
  253. {
  254. size_t length;
  255. int i;
  256. png_debug1(1, "in %s storage function", "pCAL");
  257. if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
  258. || (nparams > 0 && params == NULL))
  259. return;
  260. length = strlen(purpose) + 1;
  261. png_debug1(3, "allocating purpose for info (%lu bytes)",
  262. (unsigned long)length);
  263. /* TODO: validate format of calibration name and unit name */
  264. /* Check that the type matches the specification. */
  265. if (type < 0 || type > 3)
  266. {
  267. png_chunk_report(png_ptr, "Invalid pCAL equation type",
  268. PNG_CHUNK_WRITE_ERROR);
  269. return;
  270. }
  271. if (nparams < 0 || nparams > 255)
  272. {
  273. png_chunk_report(png_ptr, "Invalid pCAL parameter count",
  274. PNG_CHUNK_WRITE_ERROR);
  275. return;
  276. }
  277. /* Validate params[nparams] */
  278. for (i=0; i<nparams; ++i)
  279. {
  280. if (params[i] == NULL ||
  281. !png_check_fp_string(params[i], strlen(params[i])))
  282. {
  283. png_chunk_report(png_ptr, "Invalid format for pCAL parameter",
  284. PNG_CHUNK_WRITE_ERROR);
  285. return;
  286. }
  287. }
  288. info_ptr->pcal_purpose = png_voidcast(png_charp,
  289. png_malloc_warn(png_ptr, length));
  290. if (info_ptr->pcal_purpose == NULL)
  291. {
  292. png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose",
  293. PNG_CHUNK_WRITE_ERROR);
  294. return;
  295. }
  296. memcpy(info_ptr->pcal_purpose, purpose, length);
  297. png_debug(3, "storing X0, X1, type, and nparams in info");
  298. info_ptr->pcal_X0 = X0;
  299. info_ptr->pcal_X1 = X1;
  300. info_ptr->pcal_type = (png_byte)type;
  301. info_ptr->pcal_nparams = (png_byte)nparams;
  302. length = strlen(units) + 1;
  303. png_debug1(3, "allocating units for info (%lu bytes)",
  304. (unsigned long)length);
  305. info_ptr->pcal_units = png_voidcast(png_charp,
  306. png_malloc_warn(png_ptr, length));
  307. if (info_ptr->pcal_units == NULL)
  308. {
  309. png_warning(png_ptr, "Insufficient memory for pCAL units");
  310. return;
  311. }
  312. memcpy(info_ptr->pcal_units, units, length);
  313. info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
  314. (size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp)))));
  315. if (info_ptr->pcal_params == NULL)
  316. {
  317. png_warning(png_ptr, "Insufficient memory for pCAL params");
  318. return;
  319. }
  320. memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) *
  321. (sizeof (png_charp)));
  322. for (i = 0; i < nparams; i++)
  323. {
  324. length = strlen(params[i]) + 1;
  325. png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
  326. (unsigned long)length);
  327. info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
  328. if (info_ptr->pcal_params[i] == NULL)
  329. {
  330. png_warning(png_ptr, "Insufficient memory for pCAL parameter");
  331. return;
  332. }
  333. memcpy(info_ptr->pcal_params[i], params[i], length);
  334. }
  335. info_ptr->valid |= PNG_INFO_pCAL;
  336. info_ptr->free_me |= PNG_FREE_PCAL;
  337. }
  338. #endif
  339. #ifdef PNG_sCAL_SUPPORTED
  340. void PNGAPI
  341. png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
  342. int unit, png_const_charp swidth, png_const_charp sheight)
  343. {
  344. size_t lengthw = 0, lengthh = 0;
  345. png_debug1(1, "in %s storage function", "sCAL");
  346. if (png_ptr == NULL || info_ptr == NULL)
  347. return;
  348. /* Double check the unit (should never get here with an invalid
  349. * unit unless this is an API call.)
  350. */
  351. if (unit != 1 && unit != 2)
  352. png_error(png_ptr, "Invalid sCAL unit");
  353. if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
  354. swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
  355. png_error(png_ptr, "Invalid sCAL width");
  356. if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
  357. sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
  358. png_error(png_ptr, "Invalid sCAL height");
  359. info_ptr->scal_unit = (png_byte)unit;
  360. ++lengthw;
  361. png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
  362. info_ptr->scal_s_width = png_voidcast(png_charp,
  363. png_malloc_warn(png_ptr, lengthw));
  364. if (info_ptr->scal_s_width == NULL)
  365. {
  366. png_warning(png_ptr, "Memory allocation failed while processing sCAL");
  367. return;
  368. }
  369. memcpy(info_ptr->scal_s_width, swidth, lengthw);
  370. ++lengthh;
  371. png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
  372. info_ptr->scal_s_height = png_voidcast(png_charp,
  373. png_malloc_warn(png_ptr, lengthh));
  374. if (info_ptr->scal_s_height == NULL)
  375. {
  376. png_free (png_ptr, info_ptr->scal_s_width);
  377. info_ptr->scal_s_width = NULL;
  378. png_warning(png_ptr, "Memory allocation failed while processing sCAL");
  379. return;
  380. }
  381. memcpy(info_ptr->scal_s_height, sheight, lengthh);
  382. info_ptr->valid |= PNG_INFO_sCAL;
  383. info_ptr->free_me |= PNG_FREE_SCAL;
  384. }
  385. # ifdef PNG_FLOATING_POINT_SUPPORTED
  386. void PNGAPI
  387. png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
  388. double width, double height)
  389. {
  390. png_debug1(1, "in %s storage function", "sCAL");
  391. /* Check the arguments. */
  392. if (width <= 0)
  393. png_warning(png_ptr, "Invalid sCAL width ignored");
  394. else if (height <= 0)
  395. png_warning(png_ptr, "Invalid sCAL height ignored");
  396. else
  397. {
  398. /* Convert 'width' and 'height' to ASCII. */
  399. char swidth[PNG_sCAL_MAX_DIGITS+1];
  400. char sheight[PNG_sCAL_MAX_DIGITS+1];
  401. png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
  402. PNG_sCAL_PRECISION);
  403. png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
  404. PNG_sCAL_PRECISION);
  405. png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
  406. }
  407. }
  408. # endif
  409. # ifdef PNG_FIXED_POINT_SUPPORTED
  410. void PNGAPI
  411. png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
  412. png_fixed_point width, png_fixed_point height)
  413. {
  414. png_debug1(1, "in %s storage function", "sCAL");
  415. /* Check the arguments. */
  416. if (width <= 0)
  417. png_warning(png_ptr, "Invalid sCAL width ignored");
  418. else if (height <= 0)
  419. png_warning(png_ptr, "Invalid sCAL height ignored");
  420. else
  421. {
  422. /* Convert 'width' and 'height' to ASCII. */
  423. char swidth[PNG_sCAL_MAX_DIGITS+1];
  424. char sheight[PNG_sCAL_MAX_DIGITS+1];
  425. png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
  426. png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
  427. png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
  428. }
  429. }
  430. # endif
  431. #endif
  432. #ifdef PNG_pHYs_SUPPORTED
  433. void PNGAPI
  434. png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
  435. png_uint_32 res_x, png_uint_32 res_y, int unit_type)
  436. {
  437. png_debug1(1, "in %s storage function", "pHYs");
  438. if (png_ptr == NULL || info_ptr == NULL)
  439. return;
  440. info_ptr->x_pixels_per_unit = res_x;
  441. info_ptr->y_pixels_per_unit = res_y;
  442. info_ptr->phys_unit_type = (png_byte)unit_type;
  443. info_ptr->valid |= PNG_INFO_pHYs;
  444. }
  445. #endif
  446. void PNGAPI
  447. png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
  448. png_const_colorp palette, int num_palette)
  449. {
  450. png_uint_32 max_palette_length;
  451. png_debug1(1, "in %s storage function", "PLTE");
  452. if (png_ptr == NULL || info_ptr == NULL)
  453. return;
  454. max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
  455. (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
  456. if (num_palette < 0 || num_palette > (int) max_palette_length)
  457. {
  458. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  459. png_error(png_ptr, "Invalid palette length");
  460. else
  461. {
  462. png_warning(png_ptr, "Invalid palette length");
  463. return;
  464. }
  465. }
  466. if ((num_palette > 0 && palette == NULL) ||
  467. (num_palette == 0
  468. # ifdef PNG_MNG_FEATURES_SUPPORTED
  469. && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
  470. # endif
  471. ))
  472. {
  473. png_error(png_ptr, "Invalid palette");
  474. }
  475. /* It may not actually be necessary to set png_ptr->palette here;
  476. * we do it for backward compatibility with the way the png_handle_tRNS
  477. * function used to do the allocation.
  478. *
  479. * 1.6.0: the above statement appears to be incorrect; something has to set
  480. * the palette inside png_struct on read.
  481. */
  482. png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
  483. /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
  484. * of num_palette entries, in case of an invalid PNG file or incorrect
  485. * call to png_set_PLTE() with too-large sample values.
  486. */
  487. png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
  488. PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
  489. if (num_palette > 0)
  490. memcpy(png_ptr->palette, palette, (unsigned int)num_palette *
  491. (sizeof (png_color)));
  492. info_ptr->palette = png_ptr->palette;
  493. info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
  494. info_ptr->free_me |= PNG_FREE_PLTE;
  495. info_ptr->valid |= PNG_INFO_PLTE;
  496. }
  497. #ifdef PNG_sBIT_SUPPORTED
  498. void PNGAPI
  499. png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
  500. png_const_color_8p sig_bit)
  501. {
  502. png_debug1(1, "in %s storage function", "sBIT");
  503. if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
  504. return;
  505. info_ptr->sig_bit = *sig_bit;
  506. info_ptr->valid |= PNG_INFO_sBIT;
  507. }
  508. #endif
  509. #ifdef PNG_sRGB_SUPPORTED
  510. void PNGAPI
  511. png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
  512. {
  513. png_debug1(1, "in %s storage function", "sRGB");
  514. if (png_ptr == NULL || info_ptr == NULL)
  515. return;
  516. (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent);
  517. png_colorspace_sync_info(png_ptr, info_ptr);
  518. }
  519. void PNGAPI
  520. png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
  521. int srgb_intent)
  522. {
  523. png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
  524. if (png_ptr == NULL || info_ptr == NULL)
  525. return;
  526. if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace,
  527. srgb_intent) != 0)
  528. {
  529. /* This causes the gAMA and cHRM to be written too */
  530. info_ptr->colorspace.flags |=
  531. PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
  532. }
  533. png_colorspace_sync_info(png_ptr, info_ptr);
  534. }
  535. #endif /* sRGB */
  536. #ifdef PNG_iCCP_SUPPORTED
  537. void PNGAPI
  538. png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
  539. png_const_charp name, int compression_type,
  540. png_const_bytep profile, png_uint_32 proflen)
  541. {
  542. png_charp new_iccp_name;
  543. png_bytep new_iccp_profile;
  544. size_t length;
  545. png_debug1(1, "in %s storage function", "iCCP");
  546. if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
  547. return;
  548. if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  549. png_app_error(png_ptr, "Invalid iCCP compression method");
  550. /* Set the colorspace first because this validates the profile; do not
  551. * override previously set app cHRM or gAMA here (because likely as not the
  552. * application knows better than libpng what the correct values are.) Pass
  553. * the info_ptr color_type field to png_colorspace_set_ICC because in the
  554. * write case it has not yet been stored in png_ptr.
  555. */
  556. {
  557. int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,
  558. proflen, profile, info_ptr->color_type);
  559. png_colorspace_sync_info(png_ptr, info_ptr);
  560. /* Don't do any of the copying if the profile was bad, or inconsistent. */
  561. if (result == 0)
  562. return;
  563. /* But do write the gAMA and cHRM chunks from the profile. */
  564. info_ptr->colorspace.flags |=
  565. PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
  566. }
  567. length = strlen(name)+1;
  568. new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
  569. if (new_iccp_name == NULL)
  570. {
  571. png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
  572. return;
  573. }
  574. memcpy(new_iccp_name, name, length);
  575. new_iccp_profile = png_voidcast(png_bytep,
  576. png_malloc_warn(png_ptr, proflen));
  577. if (new_iccp_profile == NULL)
  578. {
  579. png_free(png_ptr, new_iccp_name);
  580. png_benign_error(png_ptr,
  581. "Insufficient memory to process iCCP profile");
  582. return;
  583. }
  584. memcpy(new_iccp_profile, profile, proflen);
  585. png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
  586. info_ptr->iccp_proflen = proflen;
  587. info_ptr->iccp_name = new_iccp_name;
  588. info_ptr->iccp_profile = new_iccp_profile;
  589. info_ptr->free_me |= PNG_FREE_ICCP;
  590. info_ptr->valid |= PNG_INFO_iCCP;
  591. }
  592. #endif
  593. #ifdef PNG_TEXT_SUPPORTED
  594. void PNGAPI
  595. png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
  596. png_const_textp text_ptr, int num_text)
  597. {
  598. int ret;
  599. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
  600. if (ret != 0)
  601. png_error(png_ptr, "Insufficient memory to store text");
  602. }
  603. int /* PRIVATE */
  604. png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
  605. png_const_textp text_ptr, int num_text)
  606. {
  607. int i;
  608. png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U :
  609. (unsigned long)png_ptr->chunk_name);
  610. if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
  611. return(0);
  612. /* Make sure we have enough space in the "text" array in info_struct
  613. * to hold all of the incoming text_ptr objects. This compare can't overflow
  614. * because max_text >= num_text (anyway, subtract of two positive integers
  615. * can't overflow in any case.)
  616. */
  617. if (num_text > info_ptr->max_text - info_ptr->num_text)
  618. {
  619. int old_num_text = info_ptr->num_text;
  620. int max_text;
  621. png_textp new_text = NULL;
  622. /* Calculate an appropriate max_text, checking for overflow. */
  623. max_text = old_num_text;
  624. if (num_text <= INT_MAX - max_text)
  625. {
  626. max_text += num_text;
  627. /* Round up to a multiple of 8 */
  628. if (max_text < INT_MAX-8)
  629. max_text = (max_text + 8) & ~0x7;
  630. else
  631. max_text = INT_MAX;
  632. /* Now allocate a new array and copy the old members in; this does all
  633. * the overflow checks.
  634. */
  635. new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
  636. info_ptr->text, old_num_text, max_text-old_num_text,
  637. sizeof *new_text));
  638. }
  639. if (new_text == NULL)
  640. {
  641. png_chunk_report(png_ptr, "too many text chunks",
  642. PNG_CHUNK_WRITE_ERROR);
  643. return 1;
  644. }
  645. png_free(png_ptr, info_ptr->text);
  646. info_ptr->text = new_text;
  647. info_ptr->free_me |= PNG_FREE_TEXT;
  648. info_ptr->max_text = max_text;
  649. /* num_text is adjusted below as the entries are copied in */
  650. png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
  651. }
  652. for (i = 0; i < num_text; i++)
  653. {
  654. size_t text_length, key_len;
  655. size_t lang_len, lang_key_len;
  656. png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  657. if (text_ptr[i].key == NULL)
  658. continue;
  659. if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
  660. text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
  661. {
  662. png_chunk_report(png_ptr, "text compression mode is out of range",
  663. PNG_CHUNK_WRITE_ERROR);
  664. continue;
  665. }
  666. key_len = strlen(text_ptr[i].key);
  667. if (text_ptr[i].compression <= 0)
  668. {
  669. lang_len = 0;
  670. lang_key_len = 0;
  671. }
  672. else
  673. # ifdef PNG_iTXt_SUPPORTED
  674. {
  675. /* Set iTXt data */
  676. if (text_ptr[i].lang != NULL)
  677. lang_len = strlen(text_ptr[i].lang);
  678. else
  679. lang_len = 0;
  680. if (text_ptr[i].lang_key != NULL)
  681. lang_key_len = strlen(text_ptr[i].lang_key);
  682. else
  683. lang_key_len = 0;
  684. }
  685. # else /* iTXt */
  686. {
  687. png_chunk_report(png_ptr, "iTXt chunk not supported",
  688. PNG_CHUNK_WRITE_ERROR);
  689. continue;
  690. }
  691. # endif
  692. if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
  693. {
  694. text_length = 0;
  695. # ifdef PNG_iTXt_SUPPORTED
  696. if (text_ptr[i].compression > 0)
  697. textp->compression = PNG_ITXT_COMPRESSION_NONE;
  698. else
  699. # endif
  700. textp->compression = PNG_TEXT_COMPRESSION_NONE;
  701. }
  702. else
  703. {
  704. text_length = strlen(text_ptr[i].text);
  705. textp->compression = text_ptr[i].compression;
  706. }
  707. textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
  708. key_len + text_length + lang_len + lang_key_len + 4));
  709. if (textp->key == NULL)
  710. {
  711. png_chunk_report(png_ptr, "text chunk: out of memory",
  712. PNG_CHUNK_WRITE_ERROR);
  713. return 1;
  714. }
  715. png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
  716. (unsigned long)(png_uint_32)
  717. (key_len + lang_len + lang_key_len + text_length + 4),
  718. textp->key);
  719. memcpy(textp->key, text_ptr[i].key, key_len);
  720. *(textp->key + key_len) = '\0';
  721. if (text_ptr[i].compression > 0)
  722. {
  723. textp->lang = textp->key + key_len + 1;
  724. memcpy(textp->lang, text_ptr[i].lang, lang_len);
  725. *(textp->lang + lang_len) = '\0';
  726. textp->lang_key = textp->lang + lang_len + 1;
  727. memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  728. *(textp->lang_key + lang_key_len) = '\0';
  729. textp->text = textp->lang_key + lang_key_len + 1;
  730. }
  731. else
  732. {
  733. textp->lang=NULL;
  734. textp->lang_key=NULL;
  735. textp->text = textp->key + key_len + 1;
  736. }
  737. if (text_length != 0)
  738. memcpy(textp->text, text_ptr[i].text, text_length);
  739. *(textp->text + text_length) = '\0';
  740. # ifdef PNG_iTXt_SUPPORTED
  741. if (textp->compression > 0)
  742. {
  743. textp->text_length = 0;
  744. textp->itxt_length = text_length;
  745. }
  746. else
  747. # endif
  748. {
  749. textp->text_length = text_length;
  750. textp->itxt_length = 0;
  751. }
  752. info_ptr->num_text++;
  753. png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
  754. }
  755. return(0);
  756. }
  757. #endif
  758. #ifdef PNG_tIME_SUPPORTED
  759. void PNGAPI
  760. png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
  761. png_const_timep mod_time)
  762. {
  763. png_debug1(1, "in %s storage function", "tIME");
  764. if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
  765. (png_ptr->mode & PNG_WROTE_tIME) != 0)
  766. return;
  767. if (mod_time->month == 0 || mod_time->month > 12 ||
  768. mod_time->day == 0 || mod_time->day > 31 ||
  769. mod_time->hour > 23 || mod_time->minute > 59 ||
  770. mod_time->second > 60)
  771. {
  772. png_warning(png_ptr, "Ignoring invalid time value");
  773. return;
  774. }
  775. info_ptr->mod_time = *mod_time;
  776. info_ptr->valid |= PNG_INFO_tIME;
  777. }
  778. #endif
  779. #ifdef PNG_tRNS_SUPPORTED
  780. void PNGAPI
  781. png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
  782. png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
  783. {
  784. png_debug1(1, "in %s storage function", "tRNS");
  785. if (png_ptr == NULL || info_ptr == NULL)
  786. return;
  787. if (trans_alpha != NULL)
  788. {
  789. /* It may not actually be necessary to set png_ptr->trans_alpha here;
  790. * we do it for backward compatibility with the way the png_handle_tRNS
  791. * function used to do the allocation.
  792. *
  793. * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively
  794. * relies on png_set_tRNS storing the information in png_struct
  795. * (otherwise it won't be there for the code in pngrtran.c).
  796. */
  797. png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  798. if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  799. {
  800. /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  801. info_ptr->trans_alpha = png_voidcast(png_bytep,
  802. png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
  803. memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans);
  804. }
  805. png_ptr->trans_alpha = info_ptr->trans_alpha;
  806. }
  807. if (trans_color != NULL)
  808. {
  809. #ifdef PNG_WARNINGS_SUPPORTED
  810. if (info_ptr->bit_depth < 16)
  811. {
  812. int sample_max = (1 << info_ptr->bit_depth) - 1;
  813. if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  814. trans_color->gray > sample_max) ||
  815. (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  816. (trans_color->red > sample_max ||
  817. trans_color->green > sample_max ||
  818. trans_color->blue > sample_max)))
  819. png_warning(png_ptr,
  820. "tRNS chunk has out-of-range samples for bit_depth");
  821. }
  822. #endif
  823. info_ptr->trans_color = *trans_color;
  824. if (num_trans == 0)
  825. num_trans = 1;
  826. }
  827. info_ptr->num_trans = (png_uint_16)num_trans;
  828. if (num_trans != 0)
  829. {
  830. info_ptr->valid |= PNG_INFO_tRNS;
  831. info_ptr->free_me |= PNG_FREE_TRNS;
  832. }
  833. }
  834. #endif
  835. #ifdef PNG_sPLT_SUPPORTED
  836. void PNGAPI
  837. png_set_sPLT(png_const_structrp png_ptr,
  838. png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
  839. /*
  840. * entries - array of png_sPLT_t structures
  841. * to be added to the list of palettes
  842. * in the info structure.
  843. *
  844. * nentries - number of palette structures to be
  845. * added.
  846. */
  847. {
  848. png_sPLT_tp np;
  849. if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)
  850. return;
  851. /* Use the internal realloc function, which checks for all the possible
  852. * overflows. Notice that the parameters are (int) and (size_t)
  853. */
  854. np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
  855. info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
  856. sizeof *np));
  857. if (np == NULL)
  858. {
  859. /* Out of memory or too many chunks */
  860. png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);
  861. return;
  862. }
  863. png_free(png_ptr, info_ptr->splt_palettes);
  864. info_ptr->splt_palettes = np;
  865. info_ptr->free_me |= PNG_FREE_SPLT;
  866. np += info_ptr->splt_palettes_num;
  867. do
  868. {
  869. size_t length;
  870. /* Skip invalid input entries */
  871. if (entries->name == NULL || entries->entries == NULL)
  872. {
  873. /* png_handle_sPLT doesn't do this, so this is an app error */
  874. png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");
  875. /* Just skip the invalid entry */
  876. continue;
  877. }
  878. np->depth = entries->depth;
  879. /* In the event of out-of-memory just return - there's no point keeping
  880. * on trying to add sPLT chunks.
  881. */
  882. length = strlen(entries->name) + 1;
  883. np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
  884. if (np->name == NULL)
  885. break;
  886. memcpy(np->name, entries->name, length);
  887. /* IMPORTANT: we have memory now that won't get freed if something else
  888. * goes wrong; this code must free it. png_malloc_array produces no
  889. * warnings; use a png_chunk_report (below) if there is an error.
  890. */
  891. np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
  892. entries->nentries, sizeof (png_sPLT_entry)));
  893. if (np->entries == NULL)
  894. {
  895. png_free(png_ptr, np->name);
  896. np->name = NULL;
  897. break;
  898. }
  899. np->nentries = entries->nentries;
  900. /* This multiply can't overflow because png_malloc_array has already
  901. * checked it when doing the allocation.
  902. */
  903. memcpy(np->entries, entries->entries,
  904. (unsigned int)entries->nentries * sizeof (png_sPLT_entry));
  905. /* Note that 'continue' skips the advance of the out pointer and out
  906. * count, so an invalid entry is not added.
  907. */
  908. info_ptr->valid |= PNG_INFO_sPLT;
  909. ++(info_ptr->splt_palettes_num);
  910. ++np;
  911. ++entries;
  912. }
  913. while (--nentries);
  914. if (nentries > 0)
  915. png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
  916. }
  917. #endif /* sPLT */
  918. #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  919. static png_byte
  920. check_location(png_const_structrp png_ptr, int location)
  921. {
  922. location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
  923. /* New in 1.6.0; copy the location and check it. This is an API
  924. * change; previously the app had to use the
  925. * png_set_unknown_chunk_location API below for each chunk.
  926. */
  927. if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
  928. {
  929. /* Write struct, so unknown chunks come from the app */
  930. png_app_warning(png_ptr,
  931. "png_set_unknown_chunks now expects a valid location");
  932. /* Use the old behavior */
  933. location = (png_byte)(png_ptr->mode &
  934. (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));
  935. }
  936. /* This need not be an internal error - if the app calls
  937. * png_set_unknown_chunks on a read pointer it must get the location right.
  938. */
  939. if (location == 0)
  940. png_error(png_ptr, "invalid location in png_set_unknown_chunks");
  941. /* Now reduce the location to the top-most set bit by removing each least
  942. * significant bit in turn.
  943. */
  944. while (location != (location & -location))
  945. location &= ~(location & -location);
  946. /* The cast is safe because 'location' is a bit mask and only the low four
  947. * bits are significant.
  948. */
  949. return (png_byte)location;
  950. }
  951. void PNGAPI
  952. png_set_unknown_chunks(png_const_structrp png_ptr,
  953. png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
  954. {
  955. png_unknown_chunkp np;
  956. if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
  957. unknowns == NULL)
  958. return;
  959. /* Check for the failure cases where support has been disabled at compile
  960. * time. This code is hardly ever compiled - it's here because
  961. * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
  962. * code) but may be meaningless if the read or write handling of unknown
  963. * chunks is not compiled in.
  964. */
  965. # if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
  966. defined(PNG_READ_SUPPORTED)
  967. if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
  968. {
  969. png_app_error(png_ptr, "no unknown chunk support on read");
  970. return;
  971. }
  972. # endif
  973. # if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
  974. defined(PNG_WRITE_SUPPORTED)
  975. if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
  976. {
  977. png_app_error(png_ptr, "no unknown chunk support on write");
  978. return;
  979. }
  980. # endif
  981. /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that
  982. * unknown critical chunks could be lost with just a warning resulting in
  983. * undefined behavior. Now png_chunk_report is used to provide behavior
  984. * appropriate to read or write.
  985. */
  986. np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
  987. info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
  988. sizeof *np));
  989. if (np == NULL)
  990. {
  991. png_chunk_report(png_ptr, "too many unknown chunks",
  992. PNG_CHUNK_WRITE_ERROR);
  993. return;
  994. }
  995. png_free(png_ptr, info_ptr->unknown_chunks);
  996. info_ptr->unknown_chunks = np; /* safe because it is initialized */
  997. info_ptr->free_me |= PNG_FREE_UNKN;
  998. np += info_ptr->unknown_chunks_num;
  999. /* Increment unknown_chunks_num each time round the loop to protect the
  1000. * just-allocated chunk data.
  1001. */
  1002. for (; num_unknowns > 0; --num_unknowns, ++unknowns)
  1003. {
  1004. memcpy(np->name, unknowns->name, (sizeof np->name));
  1005. np->name[(sizeof np->name)-1] = '\0';
  1006. np->location = check_location(png_ptr, unknowns->location);
  1007. if (unknowns->size == 0)
  1008. {
  1009. np->data = NULL;
  1010. np->size = 0;
  1011. }
  1012. else
  1013. {
  1014. np->data = png_voidcast(png_bytep,
  1015. png_malloc_base(png_ptr, unknowns->size));
  1016. if (np->data == NULL)
  1017. {
  1018. png_chunk_report(png_ptr, "unknown chunk: out of memory",
  1019. PNG_CHUNK_WRITE_ERROR);
  1020. /* But just skip storing the unknown chunk */
  1021. continue;
  1022. }
  1023. memcpy(np->data, unknowns->data, unknowns->size);
  1024. np->size = unknowns->size;
  1025. }
  1026. /* These increments are skipped on out-of-memory for the data - the
  1027. * unknown chunk entry gets overwritten if the png_chunk_report returns.
  1028. * This is correct in the read case (the chunk is just dropped.)
  1029. */
  1030. ++np;
  1031. ++(info_ptr->unknown_chunks_num);
  1032. }
  1033. }
  1034. void PNGAPI
  1035. png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
  1036. int chunk, int location)
  1037. {
  1038. /* This API is pretty pointless in 1.6.0 because the location can be set
  1039. * before the call to png_set_unknown_chunks.
  1040. *
  1041. * TODO: add a png_app_warning in 1.7
  1042. */
  1043. if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
  1044. chunk < info_ptr->unknown_chunks_num)
  1045. {
  1046. if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
  1047. {
  1048. png_app_error(png_ptr, "invalid unknown chunk location");
  1049. /* Fake out the pre 1.6.0 behavior: */
  1050. if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */
  1051. location = PNG_AFTER_IDAT;
  1052. else
  1053. location = PNG_HAVE_IHDR; /* also undocumented */
  1054. }
  1055. info_ptr->unknown_chunks[chunk].location =
  1056. check_location(png_ptr, location);
  1057. }
  1058. }
  1059. #endif /* STORE_UNKNOWN_CHUNKS */
  1060. #ifdef PNG_MNG_FEATURES_SUPPORTED
  1061. png_uint_32 PNGAPI
  1062. png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)
  1063. {
  1064. png_debug(1, "in png_permit_mng_features");
  1065. if (png_ptr == NULL)
  1066. return 0;
  1067. png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
  1068. return png_ptr->mng_features_permitted;
  1069. }
  1070. #endif
  1071. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  1072. static unsigned int
  1073. add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
  1074. {
  1075. unsigned int i;
  1076. /* Utility function: update the 'keep' state of a chunk if it is already in
  1077. * the list, otherwise add it to the list.
  1078. */
  1079. for (i=0; i<count; ++i, list += 5)
  1080. {
  1081. if (memcmp(list, add, 4) == 0)
  1082. {
  1083. list[4] = (png_byte)keep;
  1084. return count;
  1085. }
  1086. }
  1087. if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
  1088. {
  1089. ++count;
  1090. memcpy(list, add, 4);
  1091. list[4] = (png_byte)keep;
  1092. }
  1093. return count;
  1094. }
  1095. void PNGAPI
  1096. png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
  1097. png_const_bytep chunk_list, int num_chunks_in)
  1098. {
  1099. png_bytep new_list;
  1100. unsigned int num_chunks, old_num_chunks;
  1101. if (png_ptr == NULL)
  1102. return;
  1103. if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
  1104. {
  1105. png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
  1106. return;
  1107. }
  1108. if (num_chunks_in <= 0)
  1109. {
  1110. png_ptr->unknown_default = keep;
  1111. /* '0' means just set the flags, so stop here */
  1112. if (num_chunks_in == 0)
  1113. return;
  1114. }
  1115. if (num_chunks_in < 0)
  1116. {
  1117. /* Ignore all unknown chunks and all chunks recognized by
  1118. * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
  1119. */
  1120. static const png_byte chunks_to_ignore[] = {
  1121. 98, 75, 71, 68, '\0', /* bKGD */
  1122. 99, 72, 82, 77, '\0', /* cHRM */
  1123. 101, 88, 73, 102, '\0', /* eXIf */
  1124. 103, 65, 77, 65, '\0', /* gAMA */
  1125. 104, 73, 83, 84, '\0', /* hIST */
  1126. 105, 67, 67, 80, '\0', /* iCCP */
  1127. 105, 84, 88, 116, '\0', /* iTXt */
  1128. 111, 70, 70, 115, '\0', /* oFFs */
  1129. 112, 67, 65, 76, '\0', /* pCAL */
  1130. 112, 72, 89, 115, '\0', /* pHYs */
  1131. 115, 66, 73, 84, '\0', /* sBIT */
  1132. 115, 67, 65, 76, '\0', /* sCAL */
  1133. 115, 80, 76, 84, '\0', /* sPLT */
  1134. 115, 84, 69, 82, '\0', /* sTER */
  1135. 115, 82, 71, 66, '\0', /* sRGB */
  1136. 116, 69, 88, 116, '\0', /* tEXt */
  1137. 116, 73, 77, 69, '\0', /* tIME */
  1138. 122, 84, 88, 116, '\0' /* zTXt */
  1139. };
  1140. chunk_list = chunks_to_ignore;
  1141. num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;
  1142. }
  1143. else /* num_chunks_in > 0 */
  1144. {
  1145. if (chunk_list == NULL)
  1146. {
  1147. /* Prior to 1.6.0 this was silently ignored, now it is an app_error
  1148. * which can be switched off.
  1149. */
  1150. png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
  1151. return;
  1152. }
  1153. num_chunks = (unsigned int)num_chunks_in;
  1154. }
  1155. old_num_chunks = png_ptr->num_chunk_list;
  1156. if (png_ptr->chunk_list == NULL)
  1157. old_num_chunks = 0;
  1158. /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
  1159. */
  1160. if (num_chunks + old_num_chunks > UINT_MAX/5)
  1161. {
  1162. png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
  1163. return;
  1164. }
  1165. /* If these chunks are being reset to the default then no more memory is
  1166. * required because add_one_chunk above doesn't extend the list if the 'keep'
  1167. * parameter is the default.
  1168. */
  1169. if (keep != 0)
  1170. {
  1171. new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
  1172. 5 * (num_chunks + old_num_chunks)));
  1173. if (old_num_chunks > 0)
  1174. memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
  1175. }
  1176. else if (old_num_chunks > 0)
  1177. new_list = png_ptr->chunk_list;
  1178. else
  1179. new_list = NULL;
  1180. /* Add the new chunks together with each one's handling code. If the chunk
  1181. * already exists the code is updated, otherwise the chunk is added to the
  1182. * end. (In libpng 1.6.0 order no longer matters because this code enforces
  1183. * the earlier convention that the last setting is the one that is used.)
  1184. */
  1185. if (new_list != NULL)
  1186. {
  1187. png_const_bytep inlist;
  1188. png_bytep outlist;
  1189. unsigned int i;
  1190. for (i=0; i<num_chunks; ++i)
  1191. {
  1192. old_num_chunks = add_one_chunk(new_list, old_num_chunks,
  1193. chunk_list+5*i, keep);
  1194. }
  1195. /* Now remove any spurious 'default' entries. */
  1196. num_chunks = 0;
  1197. for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
  1198. {
  1199. if (inlist[4])
  1200. {
  1201. if (outlist != inlist)
  1202. memcpy(outlist, inlist, 5);
  1203. outlist += 5;
  1204. ++num_chunks;
  1205. }
  1206. }
  1207. /* This means the application has removed all the specialized handling. */
  1208. if (num_chunks == 0)
  1209. {
  1210. if (png_ptr->chunk_list != new_list)
  1211. png_free(png_ptr, new_list);
  1212. new_list = NULL;
  1213. }
  1214. }
  1215. else
  1216. num_chunks = 0;
  1217. png_ptr->num_chunk_list = num_chunks;
  1218. if (png_ptr->chunk_list != new_list)
  1219. {
  1220. if (png_ptr->chunk_list != NULL)
  1221. png_free(png_ptr, png_ptr->chunk_list);
  1222. png_ptr->chunk_list = new_list;
  1223. }
  1224. }
  1225. #endif
  1226. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  1227. void PNGAPI
  1228. png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
  1229. png_user_chunk_ptr read_user_chunk_fn)
  1230. {
  1231. png_debug(1, "in png_set_read_user_chunk_fn");
  1232. if (png_ptr == NULL)
  1233. return;
  1234. png_ptr->read_user_chunk_fn = read_user_chunk_fn;
  1235. png_ptr->user_chunk_ptr = user_chunk_ptr;
  1236. }
  1237. #endif
  1238. #ifdef PNG_INFO_IMAGE_SUPPORTED
  1239. void PNGAPI
  1240. png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
  1241. png_bytepp row_pointers)
  1242. {
  1243. png_debug1(1, "in %s storage function", "rows");
  1244. if (png_ptr == NULL || info_ptr == NULL)
  1245. return;
  1246. if (info_ptr->row_pointers != NULL &&
  1247. (info_ptr->row_pointers != row_pointers))
  1248. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1249. info_ptr->row_pointers = row_pointers;
  1250. if (row_pointers != NULL)
  1251. info_ptr->valid |= PNG_INFO_IDAT;
  1252. }
  1253. #endif
  1254. void PNGAPI
  1255. png_set_compression_buffer_size(png_structrp png_ptr, size_t size)
  1256. {
  1257. if (png_ptr == NULL)
  1258. return;
  1259. if (size == 0 || size > PNG_UINT_31_MAX)
  1260. png_error(png_ptr, "invalid compression buffer size");
  1261. # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  1262. if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
  1263. {
  1264. png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
  1265. return;
  1266. }
  1267. # endif
  1268. # ifdef PNG_WRITE_SUPPORTED
  1269. if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
  1270. {
  1271. if (png_ptr->zowner != 0)
  1272. {
  1273. png_warning(png_ptr,
  1274. "Compression buffer size cannot be changed because it is in use");
  1275. return;
  1276. }
  1277. #ifndef __COVERITY__
  1278. /* Some compilers complain that this is always false. However, it
  1279. * can be true when integer overflow happens.
  1280. */
  1281. if (size > ZLIB_IO_MAX)
  1282. {
  1283. png_warning(png_ptr,
  1284. "Compression buffer size limited to system maximum");
  1285. size = ZLIB_IO_MAX; /* must fit */
  1286. }
  1287. #endif
  1288. if (size < 6)
  1289. {
  1290. /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
  1291. * if this is permitted.
  1292. */
  1293. png_warning(png_ptr,
  1294. "Compression buffer size cannot be reduced below 6");
  1295. return;
  1296. }
  1297. if (png_ptr->zbuffer_size != size)
  1298. {
  1299. png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
  1300. png_ptr->zbuffer_size = (uInt)size;
  1301. }
  1302. }
  1303. # endif
  1304. }
  1305. void PNGAPI
  1306. png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
  1307. {
  1308. if (png_ptr != NULL && info_ptr != NULL)
  1309. info_ptr->valid &= (unsigned int)(~mask);
  1310. }
  1311. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1312. /* This function was added to libpng 1.2.6 */
  1313. void PNGAPI
  1314. png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
  1315. png_uint_32 user_height_max)
  1316. {
  1317. /* Images with dimensions larger than these limits will be
  1318. * rejected by png_set_IHDR(). To accept any PNG datastream
  1319. * regardless of dimensions, set both limits to 0x7fffffff.
  1320. */
  1321. if (png_ptr == NULL)
  1322. return;
  1323. png_ptr->user_width_max = user_width_max;
  1324. png_ptr->user_height_max = user_height_max;
  1325. }
  1326. /* This function was added to libpng 1.4.0 */
  1327. void PNGAPI
  1328. png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
  1329. {
  1330. if (png_ptr != NULL)
  1331. png_ptr->user_chunk_cache_max = user_chunk_cache_max;
  1332. }
  1333. /* This function was added to libpng 1.4.1 */
  1334. void PNGAPI
  1335. png_set_chunk_malloc_max (png_structrp png_ptr,
  1336. png_alloc_size_t user_chunk_malloc_max)
  1337. {
  1338. if (png_ptr != NULL)
  1339. png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
  1340. }
  1341. #endif /* ?SET_USER_LIMITS */
  1342. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1343. void PNGAPI
  1344. png_set_benign_errors(png_structrp png_ptr, int allowed)
  1345. {
  1346. png_debug(1, "in png_set_benign_errors");
  1347. /* If allowed is 1, png_benign_error() is treated as a warning.
  1348. *
  1349. * If allowed is 0, png_benign_error() is treated as an error (which
  1350. * is the default behavior if png_set_benign_errors() is not called).
  1351. */
  1352. if (allowed != 0)
  1353. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
  1354. PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
  1355. else
  1356. png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
  1357. PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
  1358. }
  1359. #endif /* BENIGN_ERRORS */
  1360. #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
  1361. /* Whether to report invalid palette index; added at libng-1.5.10.
  1362. * It is possible for an indexed (color-type==3) PNG file to contain
  1363. * pixels with invalid (out-of-range) indexes if the PLTE chunk has
  1364. * fewer entries than the image's bit-depth would allow. We recover
  1365. * from this gracefully by filling any incomplete palette with zeros
  1366. * (opaque black). By default, when this occurs libpng will issue
  1367. * a benign error. This API can be used to override that behavior.
  1368. */
  1369. void PNGAPI
  1370. png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
  1371. {
  1372. png_debug(1, "in png_set_check_for_invalid_index");
  1373. if (allowed > 0)
  1374. png_ptr->num_palette_max = 0;
  1375. else
  1376. png_ptr->num_palette_max = -1;
  1377. }
  1378. #endif
  1379. #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \
  1380. defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
  1381. /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
  1382. * and if invalid, correct the keyword rather than discarding the entire
  1383. * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
  1384. * length, forbids leading or trailing whitespace, multiple internal spaces,
  1385. * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
  1386. *
  1387. * The 'new_key' buffer must be 80 characters in size (for the keyword plus a
  1388. * trailing '\0'). If this routine returns 0 then there was no keyword, or a
  1389. * valid one could not be generated, and the caller must png_error.
  1390. */
  1391. png_uint_32 /* PRIVATE */
  1392. png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
  1393. {
  1394. #ifdef PNG_WARNINGS_SUPPORTED
  1395. png_const_charp orig_key = key;
  1396. #endif
  1397. png_uint_32 key_len = 0;
  1398. int bad_character = 0;
  1399. int space = 1;
  1400. png_debug(1, "in png_check_keyword");
  1401. if (key == NULL)
  1402. {
  1403. *new_key = 0;
  1404. return 0;
  1405. }
  1406. while (*key && key_len < 79)
  1407. {
  1408. png_byte ch = (png_byte)*key++;
  1409. if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
  1410. {
  1411. *new_key++ = ch; ++key_len; space = 0;
  1412. }
  1413. else if (space == 0)
  1414. {
  1415. /* A space or an invalid character when one wasn't seen immediately
  1416. * before; output just a space.
  1417. */
  1418. *new_key++ = 32; ++key_len; space = 1;
  1419. /* If the character was not a space then it is invalid. */
  1420. if (ch != 32)
  1421. bad_character = ch;
  1422. }
  1423. else if (bad_character == 0)
  1424. bad_character = ch; /* just skip it, record the first error */
  1425. }
  1426. if (key_len > 0 && space != 0) /* trailing space */
  1427. {
  1428. --key_len; --new_key;
  1429. if (bad_character == 0)
  1430. bad_character = 32;
  1431. }
  1432. /* Terminate the keyword */
  1433. *new_key = 0;
  1434. if (key_len == 0)
  1435. return 0;
  1436. #ifdef PNG_WARNINGS_SUPPORTED
  1437. /* Try to only output one warning per keyword: */
  1438. if (*key != 0) /* keyword too long */
  1439. png_warning(png_ptr, "keyword truncated");
  1440. else if (bad_character != 0)
  1441. {
  1442. PNG_WARNING_PARAMETERS(p)
  1443. png_warning_parameter(p, 1, orig_key);
  1444. png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
  1445. png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
  1446. }
  1447. #else /* !WARNINGS */
  1448. PNG_UNUSED(png_ptr)
  1449. #endif /* !WARNINGS */
  1450. return key_len;
  1451. }
  1452. #endif /* TEXT || pCAL || iCCP || sPLT */
  1453. #endif /* READ || WRITE */