pngset.c 50 KB

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