pngset.c 46 KB

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