pngset.c 55 KB

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