pngset.c 54 KB

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