pngset.c 55 KB

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