pngset.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. /* pngset.c - storage of image information into info struct
  2. *
  3. * Last changed in libpng 1.6.24 [August 4, 2016]
  4. * Copyright (c) 1998-2016 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. if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  748. {
  749. /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  750. info_ptr->trans_alpha = png_voidcast(png_bytep,
  751. png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
  752. memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
  753. }
  754. png_ptr->trans_alpha = info_ptr->trans_alpha;
  755. }
  756. if (trans_color != NULL)
  757. {
  758. #ifdef PNG_WARNINGS_SUPPORTED
  759. if (info_ptr->bit_depth < 16)
  760. {
  761. int sample_max = (1 << info_ptr->bit_depth) - 1;
  762. if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  763. trans_color->gray > sample_max) ||
  764. (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  765. (trans_color->red > sample_max ||
  766. trans_color->green > sample_max ||
  767. trans_color->blue > sample_max)))
  768. png_warning(png_ptr,
  769. "tRNS chunk has out-of-range samples for bit_depth");
  770. }
  771. #endif
  772. info_ptr->trans_color = *trans_color;
  773. if (num_trans == 0)
  774. num_trans = 1;
  775. }
  776. info_ptr->num_trans = (png_uint_16)num_trans;
  777. if (num_trans != 0)
  778. {
  779. info_ptr->valid |= PNG_INFO_tRNS;
  780. info_ptr->free_me |= PNG_FREE_TRNS;
  781. }
  782. }
  783. #endif
  784. #ifdef PNG_sPLT_SUPPORTED
  785. void PNGAPI
  786. png_set_sPLT(png_const_structrp png_ptr,
  787. png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
  788. /*
  789. * entries - array of png_sPLT_t structures
  790. * to be added to the list of palettes
  791. * in the info structure.
  792. *
  793. * nentries - number of palette structures to be
  794. * added.
  795. */
  796. {
  797. png_sPLT_tp np;
  798. if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)
  799. return;
  800. /* Use the internal realloc function, which checks for all the possible
  801. * overflows. Notice that the parameters are (int) and (size_t)
  802. */
  803. np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
  804. info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
  805. sizeof *np));
  806. if (np == NULL)
  807. {
  808. /* Out of memory or too many chunks */
  809. png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);
  810. return;
  811. }
  812. png_free(png_ptr, info_ptr->splt_palettes);
  813. info_ptr->splt_palettes = np;
  814. info_ptr->free_me |= PNG_FREE_SPLT;
  815. np += info_ptr->splt_palettes_num;
  816. do
  817. {
  818. png_size_t length;
  819. /* Skip invalid input entries */
  820. if (entries->name == NULL || entries->entries == NULL)
  821. {
  822. /* png_handle_sPLT doesn't do this, so this is an app error */
  823. png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");
  824. /* Just skip the invalid entry */
  825. continue;
  826. }
  827. np->depth = entries->depth;
  828. /* In the event of out-of-memory just return - there's no point keeping
  829. * on trying to add sPLT chunks.
  830. */
  831. length = strlen(entries->name) + 1;
  832. np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
  833. if (np->name == NULL)
  834. break;
  835. memcpy(np->name, entries->name, length);
  836. /* IMPORTANT: we have memory now that won't get freed if something else
  837. * goes wrong; this code must free it. png_malloc_array produces no
  838. * warnings; use a png_chunk_report (below) if there is an error.
  839. */
  840. np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
  841. entries->nentries, sizeof (png_sPLT_entry)));
  842. if (np->entries == NULL)
  843. {
  844. png_free(png_ptr, np->name);
  845. np->name = NULL;
  846. break;
  847. }
  848. np->nentries = entries->nentries;
  849. /* This multiply can't overflow because png_malloc_array has already
  850. * checked it when doing the allocation.
  851. */
  852. memcpy(np->entries, entries->entries,
  853. entries->nentries * sizeof (png_sPLT_entry));
  854. /* Note that 'continue' skips the advance of the out pointer and out
  855. * count, so an invalid entry is not added.
  856. */
  857. info_ptr->valid |= PNG_INFO_sPLT;
  858. ++(info_ptr->splt_palettes_num);
  859. ++np;
  860. }
  861. while (++entries, --nentries);
  862. if (nentries > 0)
  863. png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
  864. }
  865. #endif /* sPLT */
  866. #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  867. static png_byte
  868. check_location(png_const_structrp png_ptr, int location)
  869. {
  870. location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
  871. /* New in 1.6.0; copy the location and check it. This is an API
  872. * change; previously the app had to use the
  873. * png_set_unknown_chunk_location API below for each chunk.
  874. */
  875. if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
  876. {
  877. /* Write struct, so unknown chunks come from the app */
  878. png_app_warning(png_ptr,
  879. "png_set_unknown_chunks now expects a valid location");
  880. /* Use the old behavior */
  881. location = (png_byte)(png_ptr->mode &
  882. (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));
  883. }
  884. /* This need not be an internal error - if the app calls
  885. * png_set_unknown_chunks on a read pointer it must get the location right.
  886. */
  887. if (location == 0)
  888. png_error(png_ptr, "invalid location in png_set_unknown_chunks");
  889. /* Now reduce the location to the top-most set bit by removing each least
  890. * significant bit in turn.
  891. */
  892. while (location != (location & -location))
  893. location &= ~(location & -location);
  894. /* The cast is safe because 'location' is a bit mask and only the low four
  895. * bits are significant.
  896. */
  897. return (png_byte)location;
  898. }
  899. void PNGAPI
  900. png_set_unknown_chunks(png_const_structrp png_ptr,
  901. png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
  902. {
  903. png_unknown_chunkp np;
  904. if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
  905. unknowns == NULL)
  906. return;
  907. /* Check for the failure cases where support has been disabled at compile
  908. * time. This code is hardly ever compiled - it's here because
  909. * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
  910. * code) but may be meaningless if the read or write handling of unknown
  911. * chunks is not compiled in.
  912. */
  913. # if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
  914. defined(PNG_READ_SUPPORTED)
  915. if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
  916. {
  917. png_app_error(png_ptr, "no unknown chunk support on read");
  918. return;
  919. }
  920. # endif
  921. # if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
  922. defined(PNG_WRITE_SUPPORTED)
  923. if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
  924. {
  925. png_app_error(png_ptr, "no unknown chunk support on write");
  926. return;
  927. }
  928. # endif
  929. /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that
  930. * unknown critical chunks could be lost with just a warning resulting in
  931. * undefined behavior. Now png_chunk_report is used to provide behavior
  932. * appropriate to read or write.
  933. */
  934. np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
  935. info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
  936. sizeof *np));
  937. if (np == NULL)
  938. {
  939. png_chunk_report(png_ptr, "too many unknown chunks",
  940. PNG_CHUNK_WRITE_ERROR);
  941. return;
  942. }
  943. png_free(png_ptr, info_ptr->unknown_chunks);
  944. info_ptr->unknown_chunks = np; /* safe because it is initialized */
  945. info_ptr->free_me |= PNG_FREE_UNKN;
  946. np += info_ptr->unknown_chunks_num;
  947. /* Increment unknown_chunks_num each time round the loop to protect the
  948. * just-allocated chunk data.
  949. */
  950. for (; num_unknowns > 0; --num_unknowns, ++unknowns)
  951. {
  952. memcpy(np->name, unknowns->name, (sizeof np->name));
  953. np->name[(sizeof np->name)-1] = '\0';
  954. np->location = check_location(png_ptr, unknowns->location);
  955. if (unknowns->size == 0)
  956. {
  957. np->data = NULL;
  958. np->size = 0;
  959. }
  960. else
  961. {
  962. np->data = png_voidcast(png_bytep,
  963. png_malloc_base(png_ptr, unknowns->size));
  964. if (np->data == NULL)
  965. {
  966. png_chunk_report(png_ptr, "unknown chunk: out of memory",
  967. PNG_CHUNK_WRITE_ERROR);
  968. /* But just skip storing the unknown chunk */
  969. continue;
  970. }
  971. memcpy(np->data, unknowns->data, unknowns->size);
  972. np->size = unknowns->size;
  973. }
  974. /* These increments are skipped on out-of-memory for the data - the
  975. * unknown chunk entry gets overwritten if the png_chunk_report returns.
  976. * This is correct in the read case (the chunk is just dropped.)
  977. */
  978. ++np;
  979. ++(info_ptr->unknown_chunks_num);
  980. }
  981. }
  982. void PNGAPI
  983. png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
  984. int chunk, int location)
  985. {
  986. /* This API is pretty pointless in 1.6.0 because the location can be set
  987. * before the call to png_set_unknown_chunks.
  988. *
  989. * TODO: add a png_app_warning in 1.7
  990. */
  991. if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
  992. chunk < info_ptr->unknown_chunks_num)
  993. {
  994. if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
  995. {
  996. png_app_error(png_ptr, "invalid unknown chunk location");
  997. /* Fake out the pre 1.6.0 behavior: */
  998. if ((location & PNG_HAVE_IDAT) != 0) /* undocumented! */
  999. location = PNG_AFTER_IDAT;
  1000. else
  1001. location = PNG_HAVE_IHDR; /* also undocumented */
  1002. }
  1003. info_ptr->unknown_chunks[chunk].location =
  1004. check_location(png_ptr, location);
  1005. }
  1006. }
  1007. #endif /* STORE_UNKNOWN_CHUNKS */
  1008. #ifdef PNG_MNG_FEATURES_SUPPORTED
  1009. png_uint_32 PNGAPI
  1010. png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)
  1011. {
  1012. png_debug(1, "in png_permit_mng_features");
  1013. if (png_ptr == NULL)
  1014. return 0;
  1015. png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
  1016. return png_ptr->mng_features_permitted;
  1017. }
  1018. #endif
  1019. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  1020. static unsigned int
  1021. add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
  1022. {
  1023. unsigned int i;
  1024. /* Utility function: update the 'keep' state of a chunk if it is already in
  1025. * the list, otherwise add it to the list.
  1026. */
  1027. for (i=0; i<count; ++i, list += 5)
  1028. {
  1029. if (memcmp(list, add, 4) == 0)
  1030. {
  1031. list[4] = (png_byte)keep;
  1032. return count;
  1033. }
  1034. }
  1035. if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
  1036. {
  1037. ++count;
  1038. memcpy(list, add, 4);
  1039. list[4] = (png_byte)keep;
  1040. }
  1041. return count;
  1042. }
  1043. void PNGAPI
  1044. png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
  1045. png_const_bytep chunk_list, int num_chunks_in)
  1046. {
  1047. png_bytep new_list;
  1048. unsigned int num_chunks, old_num_chunks;
  1049. if (png_ptr == NULL)
  1050. return;
  1051. if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
  1052. {
  1053. png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
  1054. return;
  1055. }
  1056. if (num_chunks_in <= 0)
  1057. {
  1058. png_ptr->unknown_default = keep;
  1059. /* '0' means just set the flags, so stop here */
  1060. if (num_chunks_in == 0)
  1061. return;
  1062. }
  1063. if (num_chunks_in < 0)
  1064. {
  1065. /* Ignore all unknown chunks and all chunks recognized by
  1066. * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
  1067. */
  1068. static PNG_CONST png_byte chunks_to_ignore[] = {
  1069. 98, 75, 71, 68, '\0', /* bKGD */
  1070. 99, 72, 82, 77, '\0', /* cHRM */
  1071. 103, 65, 77, 65, '\0', /* gAMA */
  1072. 104, 73, 83, 84, '\0', /* hIST */
  1073. 105, 67, 67, 80, '\0', /* iCCP */
  1074. 105, 84, 88, 116, '\0', /* iTXt */
  1075. 111, 70, 70, 115, '\0', /* oFFs */
  1076. 112, 67, 65, 76, '\0', /* pCAL */
  1077. 112, 72, 89, 115, '\0', /* pHYs */
  1078. 115, 66, 73, 84, '\0', /* sBIT */
  1079. 115, 67, 65, 76, '\0', /* sCAL */
  1080. 115, 80, 76, 84, '\0', /* sPLT */
  1081. 115, 84, 69, 82, '\0', /* sTER */
  1082. 115, 82, 71, 66, '\0', /* sRGB */
  1083. 116, 69, 88, 116, '\0', /* tEXt */
  1084. 116, 73, 77, 69, '\0', /* tIME */
  1085. 122, 84, 88, 116, '\0' /* zTXt */
  1086. };
  1087. chunk_list = chunks_to_ignore;
  1088. num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;
  1089. }
  1090. else /* num_chunks_in > 0 */
  1091. {
  1092. if (chunk_list == NULL)
  1093. {
  1094. /* Prior to 1.6.0 this was silently ignored, now it is an app_error
  1095. * which can be switched off.
  1096. */
  1097. png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
  1098. return;
  1099. }
  1100. num_chunks = num_chunks_in;
  1101. }
  1102. old_num_chunks = png_ptr->num_chunk_list;
  1103. if (png_ptr->chunk_list == NULL)
  1104. old_num_chunks = 0;
  1105. /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
  1106. */
  1107. if (num_chunks + old_num_chunks > UINT_MAX/5)
  1108. {
  1109. png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
  1110. return;
  1111. }
  1112. /* If these chunks are being reset to the default then no more memory is
  1113. * required because add_one_chunk above doesn't extend the list if the 'keep'
  1114. * parameter is the default.
  1115. */
  1116. if (keep != 0)
  1117. {
  1118. new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
  1119. 5 * (num_chunks + old_num_chunks)));
  1120. if (old_num_chunks > 0)
  1121. memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
  1122. }
  1123. else if (old_num_chunks > 0)
  1124. new_list = png_ptr->chunk_list;
  1125. else
  1126. new_list = NULL;
  1127. /* Add the new chunks together with each one's handling code. If the chunk
  1128. * already exists the code is updated, otherwise the chunk is added to the
  1129. * end. (In libpng 1.6.0 order no longer matters because this code enforces
  1130. * the earlier convention that the last setting is the one that is used.)
  1131. */
  1132. if (new_list != NULL)
  1133. {
  1134. png_const_bytep inlist;
  1135. png_bytep outlist;
  1136. unsigned int i;
  1137. for (i=0; i<num_chunks; ++i)
  1138. {
  1139. old_num_chunks = add_one_chunk(new_list, old_num_chunks,
  1140. chunk_list+5*i, keep);
  1141. }
  1142. /* Now remove any spurious 'default' entries. */
  1143. num_chunks = 0;
  1144. for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
  1145. {
  1146. if (inlist[4])
  1147. {
  1148. if (outlist != inlist)
  1149. memcpy(outlist, inlist, 5);
  1150. outlist += 5;
  1151. ++num_chunks;
  1152. }
  1153. }
  1154. /* This means the application has removed all the specialized handling. */
  1155. if (num_chunks == 0)
  1156. {
  1157. if (png_ptr->chunk_list != new_list)
  1158. png_free(png_ptr, new_list);
  1159. new_list = NULL;
  1160. }
  1161. }
  1162. else
  1163. num_chunks = 0;
  1164. png_ptr->num_chunk_list = num_chunks;
  1165. if (png_ptr->chunk_list != new_list)
  1166. {
  1167. if (png_ptr->chunk_list != NULL)
  1168. png_free(png_ptr, png_ptr->chunk_list);
  1169. png_ptr->chunk_list = new_list;
  1170. }
  1171. }
  1172. #endif
  1173. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  1174. void PNGAPI
  1175. png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
  1176. png_user_chunk_ptr read_user_chunk_fn)
  1177. {
  1178. png_debug(1, "in png_set_read_user_chunk_fn");
  1179. if (png_ptr == NULL)
  1180. return;
  1181. png_ptr->read_user_chunk_fn = read_user_chunk_fn;
  1182. png_ptr->user_chunk_ptr = user_chunk_ptr;
  1183. }
  1184. #endif
  1185. #ifdef PNG_INFO_IMAGE_SUPPORTED
  1186. void PNGAPI
  1187. png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
  1188. png_bytepp row_pointers)
  1189. {
  1190. png_debug1(1, "in %s storage function", "rows");
  1191. if (png_ptr == NULL || info_ptr == NULL)
  1192. return;
  1193. if (info_ptr->row_pointers != NULL &&
  1194. (info_ptr->row_pointers != row_pointers))
  1195. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1196. info_ptr->row_pointers = row_pointers;
  1197. if (row_pointers != NULL)
  1198. info_ptr->valid |= PNG_INFO_IDAT;
  1199. }
  1200. #endif
  1201. void PNGAPI
  1202. png_set_compression_buffer_size(png_structrp png_ptr, png_size_t size)
  1203. {
  1204. if (png_ptr == NULL)
  1205. return;
  1206. if (size == 0 || size > PNG_UINT_31_MAX)
  1207. png_error(png_ptr, "invalid compression buffer size");
  1208. # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  1209. if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
  1210. {
  1211. png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
  1212. return;
  1213. }
  1214. # endif
  1215. # ifdef PNG_WRITE_SUPPORTED
  1216. if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
  1217. {
  1218. if (png_ptr->zowner != 0)
  1219. {
  1220. png_warning(png_ptr,
  1221. "Compression buffer size cannot be changed because it is in use");
  1222. return;
  1223. }
  1224. #ifndef __COVERITY__
  1225. /* Some compilers complain that this is always false. However, it
  1226. * can be true when integer overflow happens.
  1227. */
  1228. if (size > ZLIB_IO_MAX)
  1229. {
  1230. png_warning(png_ptr,
  1231. "Compression buffer size limited to system maximum");
  1232. size = ZLIB_IO_MAX; /* must fit */
  1233. }
  1234. #endif
  1235. if (size < 6)
  1236. {
  1237. /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
  1238. * if this is permitted.
  1239. */
  1240. png_warning(png_ptr,
  1241. "Compression buffer size cannot be reduced below 6");
  1242. return;
  1243. }
  1244. if (png_ptr->zbuffer_size != size)
  1245. {
  1246. png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
  1247. png_ptr->zbuffer_size = (uInt)size;
  1248. }
  1249. }
  1250. # endif
  1251. }
  1252. void PNGAPI
  1253. png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
  1254. {
  1255. if (png_ptr != NULL && info_ptr != NULL)
  1256. info_ptr->valid &= ~mask;
  1257. }
  1258. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1259. /* This function was added to libpng 1.2.6 */
  1260. void PNGAPI
  1261. png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
  1262. png_uint_32 user_height_max)
  1263. {
  1264. /* Images with dimensions larger than these limits will be
  1265. * rejected by png_set_IHDR(). To accept any PNG datastream
  1266. * regardless of dimensions, set both limits to 0x7fffffff.
  1267. */
  1268. if (png_ptr == NULL)
  1269. return;
  1270. png_ptr->user_width_max = user_width_max;
  1271. png_ptr->user_height_max = user_height_max;
  1272. }
  1273. /* This function was added to libpng 1.4.0 */
  1274. void PNGAPI
  1275. png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
  1276. {
  1277. if (png_ptr != NULL)
  1278. png_ptr->user_chunk_cache_max = user_chunk_cache_max;
  1279. }
  1280. /* This function was added to libpng 1.4.1 */
  1281. void PNGAPI
  1282. png_set_chunk_malloc_max (png_structrp png_ptr,
  1283. png_alloc_size_t user_chunk_malloc_max)
  1284. {
  1285. if (png_ptr != NULL)
  1286. png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
  1287. }
  1288. #endif /* ?SET_USER_LIMITS */
  1289. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1290. void PNGAPI
  1291. png_set_benign_errors(png_structrp png_ptr, int allowed)
  1292. {
  1293. png_debug(1, "in png_set_benign_errors");
  1294. /* If allowed is 1, png_benign_error() is treated as a warning.
  1295. *
  1296. * If allowed is 0, png_benign_error() is treated as an error (which
  1297. * is the default behavior if png_set_benign_errors() is not called).
  1298. */
  1299. if (allowed != 0)
  1300. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
  1301. PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
  1302. else
  1303. png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
  1304. PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
  1305. }
  1306. #endif /* BENIGN_ERRORS */
  1307. #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
  1308. /* Whether to report invalid palette index; added at libng-1.5.10.
  1309. * It is possible for an indexed (color-type==3) PNG file to contain
  1310. * pixels with invalid (out-of-range) indexes if the PLTE chunk has
  1311. * fewer entries than the image's bit-depth would allow. We recover
  1312. * from this gracefully by filling any incomplete palette with zeros
  1313. * (opaque black). By default, when this occurs libpng will issue
  1314. * a benign error. This API can be used to override that behavior.
  1315. */
  1316. void PNGAPI
  1317. png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
  1318. {
  1319. png_debug(1, "in png_set_check_for_invalid_index");
  1320. if (allowed > 0)
  1321. png_ptr->num_palette_max = 0;
  1322. else
  1323. png_ptr->num_palette_max = -1;
  1324. }
  1325. #endif
  1326. #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \
  1327. defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
  1328. /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
  1329. * and if invalid, correct the keyword rather than discarding the entire
  1330. * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
  1331. * length, forbids leading or trailing whitespace, multiple internal spaces,
  1332. * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
  1333. *
  1334. * The 'new_key' buffer must be 80 characters in size (for the keyword plus a
  1335. * trailing '\0'). If this routine returns 0 then there was no keyword, or a
  1336. * valid one could not be generated, and the caller must png_error.
  1337. */
  1338. png_uint_32 /* PRIVATE */
  1339. png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
  1340. {
  1341. #ifdef PNG_WARNINGS_SUPPORTED
  1342. png_const_charp orig_key = key;
  1343. #endif
  1344. png_uint_32 key_len = 0;
  1345. int bad_character = 0;
  1346. int space = 1;
  1347. png_debug(1, "in png_check_keyword");
  1348. if (key == NULL)
  1349. {
  1350. *new_key = 0;
  1351. return 0;
  1352. }
  1353. while (*key && key_len < 79)
  1354. {
  1355. png_byte ch = (png_byte)*key++;
  1356. if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
  1357. *new_key++ = ch, ++key_len, space = 0;
  1358. else if (space == 0)
  1359. {
  1360. /* A space or an invalid character when one wasn't seen immediately
  1361. * before; output just a space.
  1362. */
  1363. *new_key++ = 32, ++key_len, space = 1;
  1364. /* If the character was not a space then it is invalid. */
  1365. if (ch != 32)
  1366. bad_character = ch;
  1367. }
  1368. else if (bad_character == 0)
  1369. bad_character = ch; /* just skip it, record the first error */
  1370. }
  1371. if (key_len > 0 && space != 0) /* trailing space */
  1372. {
  1373. --key_len, --new_key;
  1374. if (bad_character == 0)
  1375. bad_character = 32;
  1376. }
  1377. /* Terminate the keyword */
  1378. *new_key = 0;
  1379. if (key_len == 0)
  1380. return 0;
  1381. #ifdef PNG_WARNINGS_SUPPORTED
  1382. /* Try to only output one warning per keyword: */
  1383. if (*key != 0) /* keyword too long */
  1384. png_warning(png_ptr, "keyword truncated");
  1385. else if (bad_character != 0)
  1386. {
  1387. PNG_WARNING_PARAMETERS(p)
  1388. png_warning_parameter(p, 1, orig_key);
  1389. png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
  1390. png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
  1391. }
  1392. #else /* !WARNINGS */
  1393. PNG_UNUSED(png_ptr)
  1394. #endif /* !WARNINGS */
  1395. return key_len;
  1396. }
  1397. #endif /* TEXT || pCAL || iCCP || sPLT */
  1398. #endif /* READ || WRITE */