pngset.c 56 KB

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