pngset.c 56 KB

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