pngset.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /* pngset.c - storage of image information into info struct
  2. *
  3. * Last changed in libpng 1.5.26 [December 17, 2015]
  4. * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * The functions here are used during reads to store data from the file
  13. * into the info struct, and during writes to store application data
  14. * into the info struct for writing into the file. This abstracts the
  15. * info struct and allows us to change the structure in the future.
  16. */
  17. #include "pngpriv.h"
  18. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  19. #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) ||\
  20. defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
  21. /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
  22. * and if invalid, correct the keyword rather than discarding the entire
  23. * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
  24. * length, forbids leading or trailing whitespace, multiple internal spaces,
  25. * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
  26. *
  27. * The 'new_key' buffer must be 80 characters in size (for the keyword plus a
  28. * trailing '\0'). If this routine returns 0 then there was no keyword, or a
  29. * valid one could not be generated, and the caller must handle the error by not
  30. * setting the keyword.
  31. */
  32. static png_uint_32
  33. png_check_keyword(png_const_charp key, png_bytep new_key)
  34. {
  35. png_uint_32 key_len = 0;
  36. int space = 1;
  37. if (key == NULL)
  38. {
  39. *new_key = 0;
  40. return 0;
  41. }
  42. while (*key && key_len < 79)
  43. {
  44. png_byte ch = (png_byte)*key++;
  45. if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
  46. *new_key++ = ch, ++key_len, space = 0;
  47. else if (space == 0)
  48. {
  49. /* A space or an invalid character when one wasn't seen immediately
  50. * before; output just a space.
  51. */
  52. *new_key++ = 32, ++key_len, space = 1;
  53. }
  54. }
  55. if (key_len > 0 && space != 0) /* trailing space */
  56. --key_len, --new_key;
  57. /* Terminate the keyword */
  58. *new_key = 0;
  59. if (key_len == 0)
  60. return 0;
  61. return key_len;
  62. }
  63. #endif /* TEXT || pCAL || iCCP || sPLT */
  64. #ifdef PNG_bKGD_SUPPORTED
  65. void PNGAPI
  66. png_set_bKGD(png_structp png_ptr, png_infop info_ptr,
  67. png_const_color_16p background)
  68. {
  69. png_debug1(1, "in %s storage function", "bKGD");
  70. if (png_ptr == NULL || info_ptr == NULL)
  71. return;
  72. png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
  73. info_ptr->valid |= PNG_INFO_bKGD;
  74. }
  75. #endif
  76. #ifdef PNG_cHRM_SUPPORTED
  77. void PNGFAPI
  78. png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
  79. png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
  80. png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
  81. png_fixed_point blue_x, png_fixed_point blue_y)
  82. {
  83. png_debug1(1, "in %s storage function", "cHRM fixed");
  84. if (png_ptr == NULL || info_ptr == NULL)
  85. return;
  86. # ifdef PNG_CHECK_cHRM_SUPPORTED
  87. if (png_check_cHRM_fixed(png_ptr,
  88. white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
  89. # endif
  90. {
  91. info_ptr->x_white = white_x;
  92. info_ptr->y_white = white_y;
  93. info_ptr->x_red = red_x;
  94. info_ptr->y_red = red_y;
  95. info_ptr->x_green = green_x;
  96. info_ptr->y_green = green_y;
  97. info_ptr->x_blue = blue_x;
  98. info_ptr->y_blue = blue_y;
  99. info_ptr->valid |= PNG_INFO_cHRM;
  100. }
  101. }
  102. void PNGFAPI
  103. png_set_cHRM_XYZ_fixed(png_structp png_ptr, png_infop info_ptr,
  104. png_fixed_point int_red_X, png_fixed_point int_red_Y,
  105. png_fixed_point int_red_Z, png_fixed_point int_green_X,
  106. png_fixed_point int_green_Y, png_fixed_point int_green_Z,
  107. png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
  108. png_fixed_point int_blue_Z)
  109. {
  110. png_XYZ XYZ;
  111. png_xy xy;
  112. png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
  113. if (png_ptr == NULL || info_ptr == NULL)
  114. return;
  115. XYZ.redX = int_red_X;
  116. XYZ.redY = int_red_Y;
  117. XYZ.redZ = int_red_Z;
  118. XYZ.greenX = int_green_X;
  119. XYZ.greenY = int_green_Y;
  120. XYZ.greenZ = int_green_Z;
  121. XYZ.blueX = int_blue_X;
  122. XYZ.blueY = int_blue_Y;
  123. XYZ.blueZ = int_blue_Z;
  124. if (png_xy_from_XYZ(&xy, XYZ))
  125. png_error(png_ptr, "XYZ values out of representable range");
  126. png_set_cHRM_fixed(png_ptr, info_ptr, xy.whitex, xy.whitey, xy.redx, xy.redy,
  127. xy.greenx, xy.greeny, xy.bluex, xy.bluey);
  128. }
  129. # ifdef PNG_FLOATING_POINT_SUPPORTED
  130. void PNGAPI
  131. png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
  132. double white_x, double white_y, double red_x, double red_y,
  133. double green_x, double green_y, double blue_x, double blue_y)
  134. {
  135. png_set_cHRM_fixed(png_ptr, info_ptr,
  136. png_fixed(png_ptr, white_x, "cHRM White X"),
  137. png_fixed(png_ptr, white_y, "cHRM White Y"),
  138. png_fixed(png_ptr, red_x, "cHRM Red X"),
  139. png_fixed(png_ptr, red_y, "cHRM Red Y"),
  140. png_fixed(png_ptr, green_x, "cHRM Green X"),
  141. png_fixed(png_ptr, green_y, "cHRM Green Y"),
  142. png_fixed(png_ptr, blue_x, "cHRM Blue X"),
  143. png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
  144. }
  145. void PNGAPI
  146. png_set_cHRM_XYZ(png_structp png_ptr, png_infop info_ptr, double red_X,
  147. double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
  148. double blue_X, double blue_Y, double blue_Z)
  149. {
  150. png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
  151. png_fixed(png_ptr, red_X, "cHRM Red X"),
  152. png_fixed(png_ptr, red_Y, "cHRM Red Y"),
  153. png_fixed(png_ptr, red_Z, "cHRM Red Z"),
  154. png_fixed(png_ptr, green_X, "cHRM Green X"),
  155. png_fixed(png_ptr, green_Y, "cHRM Green Y"),
  156. png_fixed(png_ptr, green_Z, "cHRM Green Z"),
  157. png_fixed(png_ptr, blue_X, "cHRM Blue X"),
  158. png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),
  159. png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));
  160. }
  161. # endif /* PNG_FLOATING_POINT_SUPPORTED */
  162. #endif /* PNG_cHRM_SUPPORTED */
  163. #ifdef PNG_gAMA_SUPPORTED
  164. void PNGFAPI
  165. png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
  166. file_gamma)
  167. {
  168. png_debug1(1, "in %s storage function", "gAMA");
  169. if (png_ptr == NULL || info_ptr == NULL)
  170. return;
  171. /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't
  172. * occur. Since the fixed point representation is assymetrical it is
  173. * possible for 1/gamma to overflow the limit of 21474 and this means the
  174. * gamma value must be at least 5/100000 and hence at most 20000.0. For
  175. * safety the limits here are a little narrower. The values are 0.00016 to
  176. * 6250.0, which are truly ridiculous gamma values (and will produce
  177. * displays that are all black or all white.)
  178. */
  179. if (file_gamma < 16 || file_gamma > 625000000)
  180. png_warning(png_ptr, "Out of range gamma value ignored");
  181. else
  182. {
  183. info_ptr->gamma = file_gamma;
  184. info_ptr->valid |= PNG_INFO_gAMA;
  185. }
  186. }
  187. # ifdef PNG_FLOATING_POINT_SUPPORTED
  188. void PNGAPI
  189. png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
  190. {
  191. png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
  192. "png_set_gAMA"));
  193. }
  194. # endif
  195. #endif
  196. #ifdef PNG_hIST_SUPPORTED
  197. void PNGAPI
  198. png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_const_uint_16p hist)
  199. {
  200. int i;
  201. png_debug1(1, "in %s storage function", "hIST");
  202. if (png_ptr == NULL || info_ptr == NULL)
  203. return;
  204. if (info_ptr->num_palette == 0 || info_ptr->num_palette
  205. > PNG_MAX_PALETTE_LENGTH)
  206. {
  207. png_warning(png_ptr,
  208. "Invalid palette size, hIST allocation skipped");
  209. return;
  210. }
  211. png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
  212. /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
  213. * version 1.2.1
  214. */
  215. png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
  216. PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));
  217. if (png_ptr->hist == NULL)
  218. {
  219. png_warning(png_ptr, "Insufficient memory for hIST chunk data");
  220. return;
  221. }
  222. for (i = 0; i < info_ptr->num_palette; i++)
  223. png_ptr->hist[i] = hist[i];
  224. info_ptr->hist = png_ptr->hist;
  225. info_ptr->valid |= PNG_INFO_hIST;
  226. info_ptr->free_me |= PNG_FREE_HIST;
  227. }
  228. #endif
  229. void PNGAPI
  230. png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
  231. png_uint_32 width, png_uint_32 height, int bit_depth,
  232. int color_type, int interlace_type, int compression_type,
  233. int filter_type)
  234. {
  235. png_debug1(1, "in %s storage function", "IHDR");
  236. if (png_ptr == NULL || info_ptr == NULL)
  237. return;
  238. info_ptr->width = width;
  239. info_ptr->height = height;
  240. info_ptr->bit_depth = (png_byte)bit_depth;
  241. info_ptr->color_type = (png_byte)color_type;
  242. info_ptr->compression_type = (png_byte)compression_type;
  243. info_ptr->filter_type = (png_byte)filter_type;
  244. info_ptr->interlace_type = (png_byte)interlace_type;
  245. png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
  246. info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
  247. info_ptr->compression_type, info_ptr->filter_type);
  248. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  249. info_ptr->channels = 1;
  250. else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  251. info_ptr->channels = 3;
  252. else
  253. info_ptr->channels = 1;
  254. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  255. info_ptr->channels++;
  256. info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
  257. info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
  258. }
  259. #ifdef PNG_oFFs_SUPPORTED
  260. void PNGAPI
  261. png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
  262. png_int_32 offset_x, png_int_32 offset_y, int unit_type)
  263. {
  264. png_debug1(1, "in %s storage function", "oFFs");
  265. if (png_ptr == NULL || info_ptr == NULL)
  266. return;
  267. info_ptr->x_offset = offset_x;
  268. info_ptr->y_offset = offset_y;
  269. info_ptr->offset_unit_type = (png_byte)unit_type;
  270. info_ptr->valid |= PNG_INFO_oFFs;
  271. }
  272. #endif
  273. #ifdef PNG_pCAL_SUPPORTED
  274. void PNGAPI
  275. png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
  276. png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
  277. int nparams, png_const_charp units, png_charpp params)
  278. {
  279. png_byte new_purpose[80];
  280. png_size_t length;
  281. int i;
  282. png_debug1(1, "in %s storage function", "pCAL");
  283. if (png_ptr == NULL || info_ptr == NULL)
  284. return;
  285. length = png_check_keyword(purpose, new_purpose);
  286. if (length == 0)
  287. {
  288. png_warning(png_ptr, "pCAL: invalid purpose keyword");
  289. return;
  290. }
  291. ++length;
  292. png_debug1(3, "allocating purpose for info (%lu bytes)",
  293. (unsigned long)length);
  294. /* TODO: validate format of calibration name and unit name */
  295. /* Check that the type matches the specification. */
  296. if (type < 0 || type > 3)
  297. png_error(png_ptr, "Invalid pCAL equation type");
  298. /* Validate params[nparams] */
  299. for (i=0; i<nparams; ++i)
  300. if (!png_check_fp_string(params[i], png_strlen(params[i])))
  301. png_error(png_ptr, "Invalid format for pCAL parameter");
  302. info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
  303. if (info_ptr->pcal_purpose == NULL)
  304. {
  305. png_warning(png_ptr, "Insufficient memory for pCAL purpose");
  306. return;
  307. }
  308. png_memcpy(info_ptr->pcal_purpose, new_purpose, length);
  309. png_debug(3, "storing X0, X1, type, and nparams in info");
  310. info_ptr->pcal_X0 = X0;
  311. info_ptr->pcal_X1 = X1;
  312. info_ptr->pcal_type = (png_byte)type;
  313. info_ptr->pcal_nparams = (png_byte)nparams;
  314. length = png_strlen(units) + 1;
  315. png_debug1(3, "allocating units for info (%lu bytes)",
  316. (unsigned long)length);
  317. info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
  318. if (info_ptr->pcal_units == NULL)
  319. {
  320. png_warning(png_ptr, "Insufficient memory for pCAL units");
  321. return;
  322. }
  323. png_memcpy(info_ptr->pcal_units, units, length);
  324. info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
  325. (png_size_t)((nparams + 1) * png_sizeof(png_charp)));
  326. if (info_ptr->pcal_params == NULL)
  327. {
  328. png_warning(png_ptr, "Insufficient memory for pCAL params");
  329. return;
  330. }
  331. png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));
  332. for (i = 0; i < nparams; i++)
  333. {
  334. length = png_strlen(params[i]) + 1;
  335. png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
  336. (unsigned long)length);
  337. info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
  338. if (info_ptr->pcal_params[i] == NULL)
  339. {
  340. png_warning(png_ptr, "Insufficient memory for pCAL parameter");
  341. return;
  342. }
  343. png_memcpy(info_ptr->pcal_params[i], params[i], length);
  344. }
  345. info_ptr->valid |= PNG_INFO_pCAL;
  346. info_ptr->free_me |= PNG_FREE_PCAL;
  347. }
  348. #endif
  349. #ifdef PNG_sCAL_SUPPORTED
  350. void PNGAPI
  351. png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  352. int unit, png_const_charp swidth, png_const_charp sheight)
  353. {
  354. png_size_t lengthw = 0, lengthh = 0;
  355. png_debug1(1, "in %s storage function", "sCAL");
  356. if (png_ptr == NULL || info_ptr == NULL)
  357. return;
  358. /* Double check the unit (should never get here with an invalid
  359. * unit unless this is an API call.)
  360. */
  361. if (unit != 1 && unit != 2)
  362. png_error(png_ptr, "Invalid sCAL unit");
  363. if (swidth == NULL || (lengthw = png_strlen(swidth)) == 0 ||
  364. swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
  365. png_error(png_ptr, "Invalid sCAL width");
  366. if (sheight == NULL || (lengthh = png_strlen(sheight)) == 0 ||
  367. sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
  368. png_error(png_ptr, "Invalid sCAL height");
  369. info_ptr->scal_unit = (png_byte)unit;
  370. ++lengthw;
  371. png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
  372. info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, lengthw);
  373. if (info_ptr->scal_s_width == NULL)
  374. {
  375. png_warning(png_ptr, "Memory allocation failed while processing sCAL");
  376. return;
  377. }
  378. png_memcpy(info_ptr->scal_s_width, swidth, lengthw);
  379. ++lengthh;
  380. png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
  381. info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, lengthh);
  382. if (info_ptr->scal_s_height == NULL)
  383. {
  384. png_free (png_ptr, info_ptr->scal_s_width);
  385. info_ptr->scal_s_width = NULL;
  386. png_warning(png_ptr, "Memory allocation failed while processing sCAL");
  387. return;
  388. }
  389. png_memcpy(info_ptr->scal_s_height, sheight, lengthh);
  390. info_ptr->valid |= PNG_INFO_sCAL;
  391. info_ptr->free_me |= PNG_FREE_SCAL;
  392. }
  393. # ifdef PNG_FLOATING_POINT_SUPPORTED
  394. void PNGAPI
  395. png_set_sCAL(png_structp png_ptr, png_infop info_ptr, int unit, double width,
  396. double height)
  397. {
  398. png_debug1(1, "in %s storage function", "sCAL");
  399. /* Check the arguments. */
  400. if (width <= 0)
  401. png_warning(png_ptr, "Invalid sCAL width ignored");
  402. else if (height <= 0)
  403. png_warning(png_ptr, "Invalid sCAL height ignored");
  404. else
  405. {
  406. /* Convert 'width' and 'height' to ASCII. */
  407. char swidth[PNG_sCAL_MAX_DIGITS+1];
  408. char sheight[PNG_sCAL_MAX_DIGITS+1];
  409. png_ascii_from_fp(png_ptr, swidth, sizeof swidth, width,
  410. PNG_sCAL_PRECISION);
  411. png_ascii_from_fp(png_ptr, sheight, sizeof sheight, height,
  412. PNG_sCAL_PRECISION);
  413. png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
  414. }
  415. }
  416. # endif
  417. # ifdef PNG_FIXED_POINT_SUPPORTED
  418. void PNGAPI
  419. png_set_sCAL_fixed(png_structp png_ptr, png_infop info_ptr, int unit,
  420. png_fixed_point width, png_fixed_point height)
  421. {
  422. png_debug1(1, "in %s storage function", "sCAL");
  423. /* Check the arguments. */
  424. if (width <= 0)
  425. png_warning(png_ptr, "Invalid sCAL width ignored");
  426. else if (height <= 0)
  427. png_warning(png_ptr, "Invalid sCAL height ignored");
  428. else
  429. {
  430. /* Convert 'width' and 'height' to ASCII. */
  431. char swidth[PNG_sCAL_MAX_DIGITS+1];
  432. char sheight[PNG_sCAL_MAX_DIGITS+1];
  433. png_ascii_from_fixed(png_ptr, swidth, sizeof swidth, width);
  434. png_ascii_from_fixed(png_ptr, sheight, sizeof sheight, height);
  435. png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
  436. }
  437. }
  438. # endif
  439. #endif
  440. #ifdef PNG_pHYs_SUPPORTED
  441. void PNGAPI
  442. png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
  443. png_uint_32 res_x, png_uint_32 res_y, int unit_type)
  444. {
  445. png_debug1(1, "in %s storage function", "pHYs");
  446. if (png_ptr == NULL || info_ptr == NULL)
  447. return;
  448. info_ptr->x_pixels_per_unit = res_x;
  449. info_ptr->y_pixels_per_unit = res_y;
  450. info_ptr->phys_unit_type = (png_byte)unit_type;
  451. info_ptr->valid |= PNG_INFO_pHYs;
  452. }
  453. #endif
  454. void PNGAPI
  455. png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
  456. png_const_colorp palette, int num_palette)
  457. {
  458. png_uint_32 max_palette_length;
  459. png_debug1(1, "in %s storage function", "PLTE");
  460. if (png_ptr == NULL || info_ptr == NULL)
  461. return;
  462. max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
  463. (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
  464. if (num_palette < 0 || num_palette > (int) max_palette_length)
  465. {
  466. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  467. png_error(png_ptr, "Invalid palette length");
  468. else
  469. {
  470. png_warning(png_ptr, "Invalid palette length");
  471. return;
  472. }
  473. }
  474. /* It may not actually be necessary to set png_ptr->palette here;
  475. * we do it for backward compatibility with the way the png_handle_tRNS
  476. * function used to do the allocation.
  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_colorp)png_calloc(png_ptr,
  484. PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
  485. png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
  486. info_ptr->palette = png_ptr->palette;
  487. info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
  488. info_ptr->free_me |= PNG_FREE_PLTE;
  489. info_ptr->valid |= PNG_INFO_PLTE;
  490. }
  491. #ifdef PNG_sBIT_SUPPORTED
  492. void PNGAPI
  493. png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
  494. png_const_color_8p sig_bit)
  495. {
  496. png_debug1(1, "in %s storage function", "sBIT");
  497. if (png_ptr == NULL || info_ptr == NULL)
  498. return;
  499. png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
  500. info_ptr->valid |= PNG_INFO_sBIT;
  501. }
  502. #endif
  503. #ifdef PNG_sRGB_SUPPORTED
  504. void PNGAPI
  505. png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int srgb_intent)
  506. {
  507. png_debug1(1, "in %s storage function", "sRGB");
  508. if (png_ptr == NULL || info_ptr == NULL)
  509. return;
  510. info_ptr->srgb_intent = (png_byte)srgb_intent;
  511. info_ptr->valid |= PNG_INFO_sRGB;
  512. }
  513. void PNGAPI
  514. png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
  515. int srgb_intent)
  516. {
  517. png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
  518. if (png_ptr == NULL || info_ptr == NULL)
  519. return;
  520. png_set_sRGB(png_ptr, info_ptr, srgb_intent);
  521. # ifdef PNG_gAMA_SUPPORTED
  522. png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
  523. # endif
  524. # ifdef PNG_cHRM_SUPPORTED
  525. png_set_cHRM_fixed(png_ptr, info_ptr,
  526. /* color x y */
  527. /* white */ 31270, 32900,
  528. /* red */ 64000, 33000,
  529. /* green */ 30000, 60000,
  530. /* blue */ 15000, 6000
  531. );
  532. # endif /* cHRM */
  533. }
  534. #endif /* sRGB */
  535. #ifdef PNG_iCCP_SUPPORTED
  536. void PNGAPI
  537. png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
  538. png_const_charp name, int compression_type,
  539. png_const_bytep profile, png_uint_32 proflen)
  540. {
  541. png_byte new_name[80];
  542. png_charp new_iccp_name;
  543. png_bytep new_iccp_profile;
  544. png_size_t length;
  545. png_debug1(1, "in %s storage function", "iCCP");
  546. if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
  547. return;
  548. length = png_check_keyword(name, new_name);
  549. if (length == 0)
  550. {
  551. png_warning(png_ptr, "iCCP: invalid keyword");
  552. return;
  553. }
  554. ++length;
  555. new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
  556. if (new_iccp_name == NULL)
  557. {
  558. png_warning(png_ptr, "Insufficient memory to process iCCP chunk");
  559. return;
  560. }
  561. png_memcpy(new_iccp_name, new_name, length);
  562. new_iccp_profile = (png_bytep)png_malloc_warn(png_ptr, proflen);
  563. if (new_iccp_profile == NULL)
  564. {
  565. png_free (png_ptr, new_iccp_name);
  566. png_warning(png_ptr,
  567. "Insufficient memory to process iCCP profile");
  568. return;
  569. }
  570. png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
  571. png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
  572. info_ptr->iccp_proflen = proflen;
  573. info_ptr->iccp_name = new_iccp_name;
  574. info_ptr->iccp_profile = new_iccp_profile;
  575. /* Compression is always zero but is here so the API and info structure
  576. * does not have to change if we introduce multiple compression types
  577. */
  578. info_ptr->iccp_compression = (png_byte)compression_type;
  579. info_ptr->free_me |= PNG_FREE_ICCP;
  580. info_ptr->valid |= PNG_INFO_iCCP;
  581. }
  582. #endif
  583. #ifdef PNG_TEXT_SUPPORTED
  584. void PNGAPI
  585. png_set_text(png_structp png_ptr, png_infop info_ptr, png_const_textp text_ptr,
  586. int num_text)
  587. {
  588. int ret;
  589. ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
  590. if (ret != 0)
  591. png_error(png_ptr, "Insufficient memory to store text");
  592. }
  593. int /* PRIVATE */
  594. png_set_text_2(png_structp png_ptr, png_infop info_ptr,
  595. png_const_textp text_ptr, int num_text)
  596. {
  597. int i;
  598. size_t element_size;
  599. png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11 :
  600. (unsigned long)png_ptr->chunk_name);
  601. if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
  602. return(0);
  603. /* Make sure we have enough space in the "text" array in info_struct
  604. * to hold all of the incoming text_ptr objects.
  605. */
  606. element_size=png_sizeof(png_text);
  607. if (num_text < 0 ||
  608. num_text > INT_MAX - info_ptr->num_text - 8 ||
  609. (unsigned int)/*SAFE*/(num_text +/*SAFE*/
  610. info_ptr->num_text + 8) >=
  611. PNG_SIZE_MAX/element_size)
  612. {
  613. png_warning(png_ptr, "too many text chunks");
  614. return(0);
  615. }
  616. if (info_ptr->num_text + num_text > info_ptr->max_text)
  617. {
  618. int old_max_text = info_ptr->max_text;
  619. int old_num_text = info_ptr->num_text;
  620. if (info_ptr->text != NULL)
  621. {
  622. png_textp old_text;
  623. info_ptr->max_text = info_ptr->num_text + num_text + 8;
  624. old_text = info_ptr->text;
  625. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  626. (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
  627. if (info_ptr->text == NULL)
  628. {
  629. /* Restore to previous condition */
  630. info_ptr->max_text = old_max_text;
  631. info_ptr->text = old_text;
  632. return(1);
  633. }
  634. png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max_text *
  635. png_sizeof(png_text)));
  636. png_free(png_ptr, old_text);
  637. }
  638. else
  639. {
  640. info_ptr->max_text = num_text + 8;
  641. info_ptr->num_text = 0;
  642. info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  643. (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));
  644. if (info_ptr->text == NULL)
  645. {
  646. /* Restore to previous condition */
  647. info_ptr->num_text = old_num_text;
  648. info_ptr->max_text = old_max_text;
  649. return(1);
  650. }
  651. info_ptr->free_me |= PNG_FREE_TEXT;
  652. }
  653. png_debug1(3, "allocated %d entries for info_ptr->text",
  654. info_ptr->max_text);
  655. }
  656. for (i = 0; i < num_text; i++)
  657. {
  658. png_byte new_key[80], new_lang[80];
  659. png_size_t text_length, key_len;
  660. png_size_t lang_len, lang_key_len;
  661. png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  662. if (text_ptr[i].key == NULL)
  663. continue;
  664. if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
  665. text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
  666. {
  667. png_warning(png_ptr, "text compression mode is out of range");
  668. continue;
  669. }
  670. key_len = png_check_keyword(text_ptr[i].key, new_key);
  671. if (key_len == 0)
  672. {
  673. png_warning(png_ptr, "invalid text keyword");
  674. continue;
  675. }
  676. if (text_ptr[i].compression <= 0)
  677. {
  678. lang_len = 0;
  679. lang_key_len = 0;
  680. }
  681. else
  682. # ifdef PNG_iTXt_SUPPORTED
  683. {
  684. /* Set iTXt data */
  685. /* Zero length language is OK */
  686. if (text_ptr[i].lang != NULL)
  687. lang_len = png_check_keyword(text_ptr[i].lang, new_lang);
  688. else
  689. lang_len = 0;
  690. if (text_ptr[i].lang_key != NULL)
  691. lang_key_len = png_strlen(text_ptr[i].lang_key);
  692. else
  693. lang_key_len = 0;
  694. }
  695. # else /* PNG_iTXt_SUPPORTED */
  696. {
  697. png_warning(png_ptr, "iTXt chunk not supported");
  698. continue;
  699. }
  700. # endif
  701. if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
  702. {
  703. text_length = 0;
  704. # ifdef PNG_iTXt_SUPPORTED
  705. if (text_ptr[i].compression > 0)
  706. textp->compression = PNG_ITXT_COMPRESSION_NONE;
  707. else
  708. # endif
  709. textp->compression = PNG_TEXT_COMPRESSION_NONE;
  710. }
  711. else
  712. {
  713. text_length = png_strlen(text_ptr[i].text);
  714. textp->compression = text_ptr[i].compression;
  715. }
  716. textp->key = (png_charp)png_malloc_warn(png_ptr,
  717. (png_size_t)
  718. (key_len + text_length + lang_len + lang_key_len + 4));
  719. if (textp->key == NULL)
  720. return(1);
  721. png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
  722. (unsigned long)(png_uint_32)
  723. (key_len + lang_len + lang_key_len + text_length + 4),
  724. textp->key);
  725. png_memcpy(textp->key, new_key, (png_size_t)(key_len));
  726. *(textp->key + key_len) = '\0';
  727. if (text_ptr[i].compression > 0)
  728. {
  729. textp->lang = textp->key + key_len + 1;
  730. png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
  731. *(textp->lang + lang_len) = '\0';
  732. textp->lang_key = textp->lang + lang_len + 1;
  733. png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  734. *(textp->lang_key + lang_key_len) = '\0';
  735. textp->text = textp->lang_key + lang_key_len + 1;
  736. }
  737. else
  738. {
  739. textp->lang=NULL;
  740. textp->lang_key=NULL;
  741. textp->text = textp->key + key_len + 1;
  742. }
  743. if (text_length != 0)
  744. png_memcpy(textp->text, text_ptr[i].text,
  745. (png_size_t)(text_length));
  746. *(textp->text + text_length) = '\0';
  747. # ifdef PNG_iTXt_SUPPORTED
  748. if (textp->compression > 0)
  749. {
  750. textp->text_length = 0;
  751. textp->itxt_length = text_length;
  752. }
  753. else
  754. # endif
  755. {
  756. textp->text_length = text_length;
  757. textp->itxt_length = 0;
  758. }
  759. info_ptr->num_text++;
  760. png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
  761. }
  762. return(0);
  763. }
  764. #endif
  765. #ifdef PNG_tIME_SUPPORTED
  766. void PNGAPI
  767. png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_const_timep mod_time)
  768. {
  769. png_debug1(1, "in %s storage function", "tIME");
  770. if (png_ptr == NULL || info_ptr == NULL ||
  771. (png_ptr->mode & PNG_WROTE_tIME))
  772. return;
  773. if (mod_time->month == 0 || mod_time->month > 12 ||
  774. mod_time->day == 0 || mod_time->day > 31 ||
  775. mod_time->hour > 23 || mod_time->minute > 59 ||
  776. mod_time->second > 60)
  777. {
  778. png_warning(png_ptr, "Ignoring invalid time value");
  779. return;
  780. }
  781. png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
  782. info_ptr->valid |= PNG_INFO_tIME;
  783. }
  784. #endif
  785. #ifdef PNG_tRNS_SUPPORTED
  786. void PNGAPI
  787. png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
  788. png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
  789. {
  790. png_debug1(1, "in %s storage function", "tRNS");
  791. if (png_ptr == NULL || info_ptr == NULL)
  792. return;
  793. if (num_trans < 0 || num_trans > PNG_MAX_PALETTE_LENGTH)
  794. {
  795. png_warning(png_ptr, "Ignoring invalid num_trans value");
  796. return;
  797. }
  798. if (trans_alpha != NULL)
  799. {
  800. /* It may not actually be necessary to set png_ptr->trans_alpha here;
  801. * we do it for backward compatibility with the way the png_handle_tRNS
  802. * function used to do the allocation.
  803. */
  804. png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  805. /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  806. png_ptr->trans_alpha = info_ptr->trans_alpha =
  807. (png_bytep)png_malloc(png_ptr, (png_size_t)PNG_MAX_PALETTE_LENGTH);
  808. if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  809. png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
  810. }
  811. if (trans_color != NULL)
  812. {
  813. if (info_ptr->bit_depth < 16)
  814. {
  815. unsigned int sample_max = (1U << info_ptr->bit_depth) - 1U;
  816. if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  817. trans_color->gray > sample_max) ||
  818. (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  819. (trans_color->red > sample_max ||
  820. trans_color->green > sample_max ||
  821. trans_color->blue > sample_max)))
  822. png_warning(png_ptr,
  823. "tRNS chunk has out-of-range samples for bit_depth");
  824. }
  825. png_memcpy(&(info_ptr->trans_color), trans_color,
  826. png_sizeof(png_color_16));
  827. if (num_trans == 0)
  828. num_trans = 1;
  829. }
  830. info_ptr->num_trans = (png_uint_16)num_trans;
  831. if (num_trans != 0)
  832. {
  833. info_ptr->valid |= PNG_INFO_tRNS;
  834. info_ptr->free_me |= PNG_FREE_TRNS;
  835. }
  836. }
  837. #endif
  838. #ifdef PNG_sPLT_SUPPORTED
  839. void PNGAPI
  840. png_set_sPLT(png_structp png_ptr,
  841. png_infop info_ptr, png_const_sPLT_tp entries, int nentries)
  842. /*
  843. * entries - array of png_sPLT_t structures
  844. * to be added to the list of palettes
  845. * in the info structure.
  846. *
  847. * nentries - number of palette structures to be
  848. * added.
  849. */
  850. {
  851. png_sPLT_tp np;
  852. int i, j;
  853. size_t element_size;
  854. if (png_ptr == NULL || info_ptr == NULL)
  855. return;
  856. element_size = png_sizeof(png_sPLT_t);
  857. if (nentries < 0 ||
  858. nentries > INT_MAX-info_ptr->splt_palettes_num ||
  859. (unsigned int)/*SAFE*/(nentries +/*SAFE*/
  860. info_ptr->splt_palettes_num) >=
  861. PNG_SIZE_MAX/element_size)
  862. np=NULL;
  863. else
  864. np = (png_sPLT_tp)png_malloc_warn(png_ptr,
  865. (info_ptr->splt_palettes_num + nentries) *
  866. (png_size_t)png_sizeof(png_sPLT_t));
  867. if (np == NULL)
  868. {
  869. png_warning(png_ptr, "No memory for sPLT palettes");
  870. return;
  871. }
  872. png_memcpy(np, info_ptr->splt_palettes,
  873. info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
  874. png_free(png_ptr, info_ptr->splt_palettes);
  875. info_ptr->splt_palettes=NULL;
  876. for (i = j = 0; i < nentries; i++)
  877. {
  878. png_sPLT_tp to = np + info_ptr->splt_palettes_num + j;
  879. png_const_sPLT_tp from = entries + i;
  880. png_byte new_name[80];
  881. png_size_t length;
  882. length = png_check_keyword(from->name, new_name);
  883. if (length == 0)
  884. {
  885. png_warning(png_ptr, "sPLT: invalid keyword");
  886. continue;
  887. }
  888. ++length; /* for trailing '\0' */
  889. to->name = (png_charp)png_malloc_warn(png_ptr, length);
  890. if (to->name == NULL)
  891. {
  892. png_warning(png_ptr,
  893. "Out of memory while processing sPLT chunk");
  894. continue;
  895. }
  896. png_memcpy(to->name, new_name, length);
  897. to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  898. from->nentries * png_sizeof(png_sPLT_entry));
  899. if (to->entries == NULL)
  900. {
  901. png_warning(png_ptr,
  902. "Out of memory while processing sPLT chunk");
  903. png_free(png_ptr, to->name);
  904. to->name = NULL;
  905. continue;
  906. }
  907. png_memcpy(to->entries, from->entries,
  908. from->nentries * png_sizeof(png_sPLT_entry));
  909. to->nentries = from->nentries;
  910. to->depth = from->depth;
  911. ++j;
  912. }
  913. info_ptr->splt_palettes = np;
  914. info_ptr->splt_palettes_num = j;
  915. info_ptr->valid |= PNG_INFO_sPLT;
  916. info_ptr->free_me |= PNG_FREE_SPLT;
  917. }
  918. #endif /* PNG_sPLT_SUPPORTED */
  919. #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
  920. void PNGAPI
  921. png_set_unknown_chunks(png_structp png_ptr,
  922. png_infop info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
  923. {
  924. png_unknown_chunkp np;
  925. int i;
  926. size_t element_size;
  927. if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
  928. return;
  929. element_size = png_sizeof(png_unknown_chunk);
  930. if (num_unknowns < 0 ||
  931. num_unknowns > INT_MAX-info_ptr->unknown_chunks_num ||
  932. (unsigned int)/*SAFE*/(num_unknowns +/*SAFE*/
  933. info_ptr->unknown_chunks_num) >=
  934. PNG_SIZE_MAX/element_size)
  935. np=NULL;
  936. else
  937. np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
  938. (png_size_t)(info_ptr->unknown_chunks_num + num_unknowns) *
  939. png_sizeof(png_unknown_chunk));
  940. if (np == NULL)
  941. {
  942. png_warning(png_ptr,
  943. "Out of memory while processing unknown chunk");
  944. return;
  945. }
  946. png_memcpy(np, info_ptr->unknown_chunks,
  947. (png_size_t)info_ptr->unknown_chunks_num *
  948. png_sizeof(png_unknown_chunk));
  949. png_free(png_ptr, info_ptr->unknown_chunks);
  950. info_ptr->unknown_chunks = NULL;
  951. for (i = 0; i < num_unknowns; i++)
  952. {
  953. png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
  954. png_const_unknown_chunkp from = unknowns + i;
  955. png_memcpy(to->name, from->name, png_sizeof(from->name));
  956. to->name[png_sizeof(to->name)-1] = '\0';
  957. to->size = from->size;
  958. /* Note our location in the read or write sequence */
  959. to->location = (png_byte)(png_ptr->mode & 0xff);
  960. if (from->size == 0)
  961. to->data=NULL;
  962. else
  963. {
  964. to->data = (png_bytep)png_malloc_warn(png_ptr,
  965. (png_size_t)from->size);
  966. if (to->data == NULL)
  967. {
  968. png_warning(png_ptr,
  969. "Out of memory while processing unknown chunk");
  970. to->size = 0;
  971. }
  972. else
  973. png_memcpy(to->data, from->data, from->size);
  974. }
  975. }
  976. info_ptr->unknown_chunks = np;
  977. info_ptr->unknown_chunks_num += num_unknowns;
  978. info_ptr->free_me |= PNG_FREE_UNKN;
  979. }
  980. void PNGAPI
  981. png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
  982. int chunk, int location)
  983. {
  984. if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
  985. info_ptr->unknown_chunks_num)
  986. info_ptr->unknown_chunks[chunk].location = (png_byte)location;
  987. }
  988. #endif
  989. #ifdef PNG_MNG_FEATURES_SUPPORTED
  990. png_uint_32 PNGAPI
  991. png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
  992. {
  993. png_debug(1, "in png_permit_mng_features");
  994. if (png_ptr == NULL)
  995. return (png_uint_32)0;
  996. png_ptr->mng_features_permitted =
  997. (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
  998. return (png_uint_32)png_ptr->mng_features_permitted;
  999. }
  1000. #endif
  1001. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  1002. void PNGAPI
  1003. png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_const_bytep
  1004. chunk_list, int num_chunks)
  1005. {
  1006. png_bytep new_list, p;
  1007. int i, old_num_chunks;
  1008. if (png_ptr == NULL)
  1009. return;
  1010. if (num_chunks == 0)
  1011. {
  1012. if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
  1013. png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  1014. else
  1015. png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  1016. if (keep == PNG_HANDLE_CHUNK_ALWAYS)
  1017. png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  1018. else
  1019. png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  1020. return;
  1021. }
  1022. if (chunk_list == NULL)
  1023. return;
  1024. old_num_chunks = png_ptr->num_chunk_list;
  1025. new_list=(png_bytep)png_malloc(png_ptr,
  1026. (png_size_t)(5*(num_chunks + old_num_chunks)));
  1027. if (png_ptr->chunk_list != NULL)
  1028. {
  1029. png_memcpy(new_list, png_ptr->chunk_list,
  1030. (png_size_t)(5*old_num_chunks));
  1031. png_free(png_ptr, png_ptr->chunk_list);
  1032. png_ptr->chunk_list=NULL;
  1033. }
  1034. png_memcpy(new_list + 5*old_num_chunks, chunk_list,
  1035. (png_size_t)(5*num_chunks));
  1036. for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
  1037. *p=(png_byte)keep;
  1038. png_ptr->num_chunk_list = old_num_chunks + num_chunks;
  1039. png_ptr->chunk_list = new_list;
  1040. png_ptr->free_me |= PNG_FREE_LIST;
  1041. }
  1042. #endif
  1043. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  1044. void PNGAPI
  1045. png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
  1046. png_user_chunk_ptr read_user_chunk_fn)
  1047. {
  1048. png_debug(1, "in png_set_read_user_chunk_fn");
  1049. if (png_ptr == NULL)
  1050. return;
  1051. png_ptr->read_user_chunk_fn = read_user_chunk_fn;
  1052. png_ptr->user_chunk_ptr = user_chunk_ptr;
  1053. }
  1054. #endif
  1055. #ifdef PNG_INFO_IMAGE_SUPPORTED
  1056. void PNGAPI
  1057. png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
  1058. {
  1059. png_debug1(1, "in %s storage function", "rows");
  1060. if (png_ptr == NULL || info_ptr == NULL)
  1061. return;
  1062. if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
  1063. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1064. info_ptr->row_pointers = row_pointers;
  1065. if (row_pointers != NULL)
  1066. info_ptr->valid |= PNG_INFO_IDAT;
  1067. }
  1068. #endif
  1069. #ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
  1070. void PNGAPI
  1071. png_set_compression_buffer_size(png_structp png_ptr, png_size_t size)
  1072. {
  1073. if (png_ptr == NULL)
  1074. return;
  1075. png_free(png_ptr, png_ptr->zbuf);
  1076. if (size > ZLIB_IO_MAX)
  1077. {
  1078. png_warning(png_ptr, "Attempt to set buffer size beyond max ignored");
  1079. png_ptr->zbuf_size = ZLIB_IO_MAX;
  1080. size = ZLIB_IO_MAX; /* must fit */
  1081. }
  1082. else
  1083. png_ptr->zbuf_size = (uInt)size;
  1084. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
  1085. /* The following ensures a relatively safe failure if this gets called while
  1086. * the buffer is actually in use.
  1087. */
  1088. png_ptr->zstream.next_out = png_ptr->zbuf;
  1089. png_ptr->zstream.avail_out = 0;
  1090. png_ptr->zstream.avail_in = 0;
  1091. }
  1092. #endif /* WRITE_CUSTOMIZE_COMPRESSION */
  1093. void PNGAPI
  1094. png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
  1095. {
  1096. if (png_ptr && info_ptr)
  1097. info_ptr->valid &= ~mask;
  1098. }
  1099. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1100. /* This function was added to libpng 1.2.6 */
  1101. void PNGAPI
  1102. png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
  1103. png_uint_32 user_height_max)
  1104. {
  1105. /* Images with dimensions larger than these limits will be
  1106. * rejected by png_set_IHDR(). To accept any PNG datastream
  1107. * regardless of dimensions, set both limits to 0x7fffffffL.
  1108. */
  1109. if (png_ptr == NULL)
  1110. return;
  1111. png_ptr->user_width_max = user_width_max;
  1112. png_ptr->user_height_max = user_height_max;
  1113. }
  1114. /* This function was added to libpng 1.4.0 */
  1115. void PNGAPI
  1116. png_set_chunk_cache_max (png_structp png_ptr,
  1117. png_uint_32 user_chunk_cache_max)
  1118. {
  1119. if (png_ptr != NULL)
  1120. png_ptr->user_chunk_cache_max = user_chunk_cache_max;
  1121. }
  1122. /* This function was added to libpng 1.4.1 */
  1123. void PNGAPI
  1124. png_set_chunk_malloc_max (png_structp png_ptr,
  1125. png_alloc_size_t user_chunk_malloc_max)
  1126. {
  1127. if (png_ptr != NULL)
  1128. png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
  1129. }
  1130. #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  1131. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1132. void PNGAPI
  1133. png_set_benign_errors(png_structp png_ptr, int allowed)
  1134. {
  1135. png_debug(1, "in png_set_benign_errors");
  1136. if (allowed != 0)
  1137. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
  1138. else
  1139. png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;
  1140. }
  1141. #endif /* PNG_BENIGN_ERRORS_SUPPORTED */
  1142. #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
  1143. /* Whether to report invalid palette index; added at libng-1.5.10
  1144. * allowed - one of 0: disable; 1: enable
  1145. */
  1146. void PNGAPI
  1147. png_set_check_for_invalid_index(png_structp png_ptr, int allowed)
  1148. {
  1149. png_debug(1, "in png_set_check_for_invalid_index");
  1150. if (allowed != 0)
  1151. png_ptr->num_palette_max = 0;
  1152. else
  1153. png_ptr->num_palette_max = -1;
  1154. }
  1155. #endif
  1156. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */