pngset.c 46 KB

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