/src/compiler/android/jni/ftk/png.c

http://ftk.googlecode.com/ · C · 1100 lines · 827 code · 129 blank · 144 comment · 185 complexity · 752b6dd749f29f9e64d27a004cd35bc2 MD5 · raw file

  1. /* png.c - location for general purpose libpng functions
  2. *
  3. * Last changed in libpng 1.2.43 [February 25, 2010]
  4. * Copyright (c) 1998-2010 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. */
  12. #define PNG_INTERNAL
  13. #define PNG_NO_EXTERN
  14. #define PNG_NO_PEDANTIC_WARNINGS
  15. #include "png.h"
  16. /* Generate a compiler error if there is an old png.h in the search path. */
  17. typedef version_1_2_44 Your_png_h_is_not_version_1_2_44;
  18. /* Version information for C files. This had better match the version
  19. * string defined in png.h.
  20. */
  21. #ifdef PNG_USE_GLOBAL_ARRAYS
  22. /* png_libpng_ver was changed to a function in version 1.0.5c */
  23. PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
  24. #ifdef PNG_READ_SUPPORTED
  25. /* png_sig was changed to a function in version 1.0.5c */
  26. /* Place to hold the signature string for a PNG file. */
  27. PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  28. #endif /* PNG_READ_SUPPORTED */
  29. /* Invoke global declarations for constant strings for known chunk types */
  30. PNG_IHDR;
  31. PNG_IDAT;
  32. PNG_IEND;
  33. PNG_PLTE;
  34. PNG_bKGD;
  35. PNG_cHRM;
  36. PNG_gAMA;
  37. PNG_hIST;
  38. PNG_iCCP;
  39. PNG_iTXt;
  40. PNG_oFFs;
  41. PNG_pCAL;
  42. PNG_sCAL;
  43. PNG_pHYs;
  44. PNG_sBIT;
  45. PNG_sPLT;
  46. PNG_sRGB;
  47. PNG_tEXt;
  48. PNG_tIME;
  49. PNG_tRNS;
  50. PNG_zTXt;
  51. #ifdef PNG_READ_SUPPORTED
  52. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  53. /* Start of interlace block */
  54. PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  55. /* Offset to next interlace block */
  56. PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  57. /* Start of interlace block in the y direction */
  58. PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  59. /* Offset to next interlace block in the y direction */
  60. PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  61. /* Height of interlace block. This is not currently used - if you need
  62. * it, uncomment it here and in png.h
  63. PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  64. */
  65. /* Mask to determine which pixels are valid in a pass */
  66. PNG_CONST int FARDATA png_pass_mask[] =
  67. {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  68. /* Mask to determine which pixels to overwrite while displaying */
  69. PNG_CONST int FARDATA png_pass_dsp_mask[]
  70. = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  71. #endif /* PNG_READ_SUPPORTED */
  72. #endif /* PNG_USE_GLOBAL_ARRAYS */
  73. /* Tells libpng that we have already handled the first "num_bytes" bytes
  74. * of the PNG file signature. If the PNG data is embedded into another
  75. * stream we can set num_bytes = 8 so that libpng will not attempt to read
  76. * or write any of the magic bytes before it starts on the IHDR.
  77. */
  78. #ifdef PNG_READ_SUPPORTED
  79. void PNGAPI
  80. png_set_sig_bytes(png_structp png_ptr, int num_bytes)
  81. {
  82. png_debug(1, "in png_set_sig_bytes");
  83. if (png_ptr == NULL)
  84. return;
  85. if (num_bytes > 8)
  86. png_error(png_ptr, "Too many bytes for PNG signature.");
  87. png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
  88. }
  89. /* Checks whether the supplied bytes match the PNG signature. We allow
  90. * checking less than the full 8-byte signature so that those apps that
  91. * already read the first few bytes of a file to determine the file type
  92. * can simply check the remaining bytes for extra assurance. Returns
  93. * an integer less than, equal to, or greater than zero if sig is found,
  94. * respectively, to be less than, to match, or be greater than the correct
  95. * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
  96. */
  97. int PNGAPI
  98. png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
  99. {
  100. png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  101. if (num_to_check > 8)
  102. num_to_check = 8;
  103. else if (num_to_check < 1)
  104. return (-1);
  105. if (start > 7)
  106. return (-1);
  107. if (start + num_to_check > 8)
  108. num_to_check = 8 - start;
  109. return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
  110. }
  111. #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  112. /* (Obsolete) function to check signature bytes. It does not allow one
  113. * to check a partial signature. This function might be removed in the
  114. * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG.
  115. */
  116. int PNGAPI
  117. png_check_sig(png_bytep sig, int num)
  118. {
  119. return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
  120. }
  121. #endif
  122. #endif /* PNG_READ_SUPPORTED */
  123. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  124. /* Function to allocate memory for zlib and clear it to 0. */
  125. #ifdef PNG_1_0_X
  126. voidpf PNGAPI
  127. #else
  128. voidpf /* PRIVATE */
  129. #endif
  130. png_zalloc(voidpf png_ptr, uInt items, uInt size)
  131. {
  132. png_voidp ptr;
  133. png_structp p=(png_structp)png_ptr;
  134. png_uint_32 save_flags=p->flags;
  135. png_uint_32 num_bytes;
  136. if (png_ptr == NULL)
  137. return (NULL);
  138. if (items > PNG_UINT_32_MAX/size)
  139. {
  140. png_warning (p, "Potential overflow in png_zalloc()");
  141. return (NULL);
  142. }
  143. num_bytes = (png_uint_32)items * size;
  144. p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  145. ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
  146. p->flags=save_flags;
  147. #if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
  148. if (ptr == NULL)
  149. return ((voidpf)ptr);
  150. if (num_bytes > (png_uint_32)0x8000L)
  151. {
  152. png_memset(ptr, 0, (png_size_t)0x8000L);
  153. png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  154. (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  155. }
  156. else
  157. {
  158. png_memset(ptr, 0, (png_size_t)num_bytes);
  159. }
  160. #endif
  161. return ((voidpf)ptr);
  162. }
  163. /* Function to free memory for zlib */
  164. #ifdef PNG_1_0_X
  165. void PNGAPI
  166. #else
  167. void /* PRIVATE */
  168. #endif
  169. png_zfree(voidpf png_ptr, voidpf ptr)
  170. {
  171. png_free((png_structp)png_ptr, (png_voidp)ptr);
  172. }
  173. /* Reset the CRC variable to 32 bits of 1's. Care must be taken
  174. * in case CRC is > 32 bits to leave the top bits 0.
  175. */
  176. void /* PRIVATE */
  177. png_reset_crc(png_structp png_ptr)
  178. {
  179. png_ptr->crc = crc32(0, Z_NULL, 0);
  180. }
  181. /* Calculate the CRC over a section of data. We can only pass as
  182. * much data to this routine as the largest single buffer size. We
  183. * also check that this data will actually be used before going to the
  184. * trouble of calculating it.
  185. */
  186. void /* PRIVATE */
  187. png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
  188. {
  189. int need_crc = 1;
  190. if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
  191. {
  192. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  193. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  194. need_crc = 0;
  195. }
  196. else /* critical */
  197. {
  198. if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  199. need_crc = 0;
  200. }
  201. if (need_crc)
  202. png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
  203. }
  204. /* Allocate the memory for an info_struct for the application. We don't
  205. * really need the png_ptr, but it could potentially be useful in the
  206. * future. This should be used in favour of malloc(png_sizeof(png_info))
  207. * and png_info_init() so that applications that want to use a shared
  208. * libpng don't have to be recompiled if png_info changes size.
  209. */
  210. png_infop PNGAPI
  211. png_create_info_struct(png_structp png_ptr)
  212. {
  213. png_infop info_ptr;
  214. png_debug(1, "in png_create_info_struct");
  215. if (png_ptr == NULL)
  216. return (NULL);
  217. #ifdef PNG_USER_MEM_SUPPORTED
  218. info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
  219. png_ptr->malloc_fn, png_ptr->mem_ptr);
  220. #else
  221. info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  222. #endif
  223. if (info_ptr != NULL)
  224. png_info_init_3(&info_ptr, png_sizeof(png_info));
  225. return (info_ptr);
  226. }
  227. /* This function frees the memory associated with a single info struct.
  228. * Normally, one would use either png_destroy_read_struct() or
  229. * png_destroy_write_struct() to free an info struct, but this may be
  230. * useful for some applications.
  231. */
  232. void PNGAPI
  233. png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
  234. {
  235. png_infop info_ptr = NULL;
  236. png_debug(1, "in png_destroy_info_struct");
  237. if (png_ptr == NULL)
  238. return;
  239. if (info_ptr_ptr != NULL)
  240. info_ptr = *info_ptr_ptr;
  241. if (info_ptr != NULL)
  242. {
  243. png_info_destroy(png_ptr, info_ptr);
  244. #ifdef PNG_USER_MEM_SUPPORTED
  245. png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
  246. png_ptr->mem_ptr);
  247. #else
  248. png_destroy_struct((png_voidp)info_ptr);
  249. #endif
  250. *info_ptr_ptr = NULL;
  251. }
  252. }
  253. /* Initialize the info structure. This is now an internal function (0.89)
  254. * and applications using it are urged to use png_create_info_struct()
  255. * instead.
  256. */
  257. #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  258. #undef png_info_init
  259. void PNGAPI
  260. png_info_init(png_infop info_ptr)
  261. {
  262. /* We only come here via pre-1.0.12-compiled applications */
  263. png_info_init_3(&info_ptr, 0);
  264. }
  265. #endif
  266. void PNGAPI
  267. png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
  268. {
  269. png_infop info_ptr = *ptr_ptr;
  270. png_debug(1, "in png_info_init_3");
  271. if (info_ptr == NULL)
  272. return;
  273. if (png_sizeof(png_info) > png_info_struct_size)
  274. {
  275. png_destroy_struct(info_ptr);
  276. info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  277. *ptr_ptr = info_ptr;
  278. }
  279. /* Set everything to 0 */
  280. png_memset(info_ptr, 0, png_sizeof(png_info));
  281. }
  282. #ifdef PNG_FREE_ME_SUPPORTED
  283. void PNGAPI
  284. png_data_freer(png_structp png_ptr, png_infop info_ptr,
  285. int freer, png_uint_32 mask)
  286. {
  287. png_debug(1, "in png_data_freer");
  288. if (png_ptr == NULL || info_ptr == NULL)
  289. return;
  290. if (freer == PNG_DESTROY_WILL_FREE_DATA)
  291. info_ptr->free_me |= mask;
  292. else if (freer == PNG_USER_WILL_FREE_DATA)
  293. info_ptr->free_me &= ~mask;
  294. else
  295. png_warning(png_ptr,
  296. "Unknown freer parameter in png_data_freer.");
  297. }
  298. #endif
  299. void PNGAPI
  300. png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
  301. int num)
  302. {
  303. png_debug(1, "in png_free_data");
  304. if (png_ptr == NULL || info_ptr == NULL)
  305. return;
  306. #ifdef PNG_TEXT_SUPPORTED
  307. /* Free text item num or (if num == -1) all text items */
  308. #ifdef PNG_FREE_ME_SUPPORTED
  309. if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
  310. #else
  311. if (mask & PNG_FREE_TEXT)
  312. #endif
  313. {
  314. if (num != -1)
  315. {
  316. if (info_ptr->text && info_ptr->text[num].key)
  317. {
  318. png_free(png_ptr, info_ptr->text[num].key);
  319. info_ptr->text[num].key = NULL;
  320. }
  321. }
  322. else
  323. {
  324. int i;
  325. for (i = 0; i < info_ptr->num_text; i++)
  326. png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
  327. png_free(png_ptr, info_ptr->text);
  328. info_ptr->text = NULL;
  329. info_ptr->num_text=0;
  330. }
  331. }
  332. #endif
  333. #ifdef PNG_tRNS_SUPPORTED
  334. /* Free any tRNS entry */
  335. #ifdef PNG_FREE_ME_SUPPORTED
  336. if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
  337. #else
  338. if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
  339. #endif
  340. {
  341. png_free(png_ptr, info_ptr->trans);
  342. info_ptr->trans = NULL;
  343. info_ptr->valid &= ~PNG_INFO_tRNS;
  344. #ifndef PNG_FREE_ME_SUPPORTED
  345. png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
  346. #endif
  347. }
  348. #endif
  349. #ifdef PNG_sCAL_SUPPORTED
  350. /* Free any sCAL entry */
  351. #ifdef PNG_FREE_ME_SUPPORTED
  352. if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
  353. #else
  354. if (mask & PNG_FREE_SCAL)
  355. #endif
  356. {
  357. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  358. png_free(png_ptr, info_ptr->scal_s_width);
  359. png_free(png_ptr, info_ptr->scal_s_height);
  360. info_ptr->scal_s_width = NULL;
  361. info_ptr->scal_s_height = NULL;
  362. #endif
  363. info_ptr->valid &= ~PNG_INFO_sCAL;
  364. }
  365. #endif
  366. #ifdef PNG_pCAL_SUPPORTED
  367. /* Free any pCAL entry */
  368. #ifdef PNG_FREE_ME_SUPPORTED
  369. if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
  370. #else
  371. if (mask & PNG_FREE_PCAL)
  372. #endif
  373. {
  374. png_free(png_ptr, info_ptr->pcal_purpose);
  375. png_free(png_ptr, info_ptr->pcal_units);
  376. info_ptr->pcal_purpose = NULL;
  377. info_ptr->pcal_units = NULL;
  378. if (info_ptr->pcal_params != NULL)
  379. {
  380. int i;
  381. for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
  382. {
  383. png_free(png_ptr, info_ptr->pcal_params[i]);
  384. info_ptr->pcal_params[i] = NULL;
  385. }
  386. png_free(png_ptr, info_ptr->pcal_params);
  387. info_ptr->pcal_params = NULL;
  388. }
  389. info_ptr->valid &= ~PNG_INFO_pCAL;
  390. }
  391. #endif
  392. #ifdef PNG_iCCP_SUPPORTED
  393. /* Free any iCCP entry */
  394. #ifdef PNG_FREE_ME_SUPPORTED
  395. if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
  396. #else
  397. if (mask & PNG_FREE_ICCP)
  398. #endif
  399. {
  400. png_free(png_ptr, info_ptr->iccp_name);
  401. png_free(png_ptr, info_ptr->iccp_profile);
  402. info_ptr->iccp_name = NULL;
  403. info_ptr->iccp_profile = NULL;
  404. info_ptr->valid &= ~PNG_INFO_iCCP;
  405. }
  406. #endif
  407. #ifdef PNG_sPLT_SUPPORTED
  408. /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
  409. #ifdef PNG_FREE_ME_SUPPORTED
  410. if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
  411. #else
  412. if (mask & PNG_FREE_SPLT)
  413. #endif
  414. {
  415. if (num != -1)
  416. {
  417. if (info_ptr->splt_palettes)
  418. {
  419. png_free(png_ptr, info_ptr->splt_palettes[num].name);
  420. png_free(png_ptr, info_ptr->splt_palettes[num].entries);
  421. info_ptr->splt_palettes[num].name = NULL;
  422. info_ptr->splt_palettes[num].entries = NULL;
  423. }
  424. }
  425. else
  426. {
  427. if (info_ptr->splt_palettes_num)
  428. {
  429. int i;
  430. for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  431. png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
  432. png_free(png_ptr, info_ptr->splt_palettes);
  433. info_ptr->splt_palettes = NULL;
  434. info_ptr->splt_palettes_num = 0;
  435. }
  436. info_ptr->valid &= ~PNG_INFO_sPLT;
  437. }
  438. }
  439. #endif
  440. #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
  441. if (png_ptr->unknown_chunk.data)
  442. {
  443. png_free(png_ptr, png_ptr->unknown_chunk.data);
  444. png_ptr->unknown_chunk.data = NULL;
  445. }
  446. #ifdef PNG_FREE_ME_SUPPORTED
  447. if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
  448. #else
  449. if (mask & PNG_FREE_UNKN)
  450. #endif
  451. {
  452. if (num != -1)
  453. {
  454. if (info_ptr->unknown_chunks)
  455. {
  456. png_free(png_ptr, info_ptr->unknown_chunks[num].data);
  457. info_ptr->unknown_chunks[num].data = NULL;
  458. }
  459. }
  460. else
  461. {
  462. int i;
  463. if (info_ptr->unknown_chunks_num)
  464. {
  465. for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
  466. png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
  467. png_free(png_ptr, info_ptr->unknown_chunks);
  468. info_ptr->unknown_chunks = NULL;
  469. info_ptr->unknown_chunks_num = 0;
  470. }
  471. }
  472. }
  473. #endif
  474. #ifdef PNG_hIST_SUPPORTED
  475. /* Free any hIST entry */
  476. #ifdef PNG_FREE_ME_SUPPORTED
  477. if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
  478. #else
  479. if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
  480. #endif
  481. {
  482. png_free(png_ptr, info_ptr->hist);
  483. info_ptr->hist = NULL;
  484. info_ptr->valid &= ~PNG_INFO_hIST;
  485. #ifndef PNG_FREE_ME_SUPPORTED
  486. png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
  487. #endif
  488. }
  489. #endif
  490. /* Free any PLTE entry that was internally allocated */
  491. #ifdef PNG_FREE_ME_SUPPORTED
  492. if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
  493. #else
  494. if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
  495. #endif
  496. {
  497. png_zfree(png_ptr, info_ptr->palette);
  498. info_ptr->palette = NULL;
  499. info_ptr->valid &= ~PNG_INFO_PLTE;
  500. #ifndef PNG_FREE_ME_SUPPORTED
  501. png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
  502. #endif
  503. info_ptr->num_palette = 0;
  504. }
  505. #ifdef PNG_INFO_IMAGE_SUPPORTED
  506. /* Free any image bits attached to the info structure */
  507. #ifdef PNG_FREE_ME_SUPPORTED
  508. if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
  509. #else
  510. if (mask & PNG_FREE_ROWS)
  511. #endif
  512. {
  513. if (info_ptr->row_pointers)
  514. {
  515. int row;
  516. for (row = 0; row < (int)info_ptr->height; row++)
  517. {
  518. png_free(png_ptr, info_ptr->row_pointers[row]);
  519. info_ptr->row_pointers[row] = NULL;
  520. }
  521. png_free(png_ptr, info_ptr->row_pointers);
  522. info_ptr->row_pointers = NULL;
  523. }
  524. info_ptr->valid &= ~PNG_INFO_IDAT;
  525. }
  526. #endif
  527. #ifdef PNG_FREE_ME_SUPPORTED
  528. if (num == -1)
  529. info_ptr->free_me &= ~mask;
  530. else
  531. info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
  532. #endif
  533. }
  534. /* This is an internal routine to free any memory that the info struct is
  535. * pointing to before re-using it or freeing the struct itself. Recall
  536. * that png_free() checks for NULL pointers for us.
  537. */
  538. void /* PRIVATE */
  539. png_info_destroy(png_structp png_ptr, png_infop info_ptr)
  540. {
  541. png_debug(1, "in png_info_destroy");
  542. png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  543. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  544. if (png_ptr->num_chunk_list)
  545. {
  546. png_free(png_ptr, png_ptr->chunk_list);
  547. png_ptr->chunk_list = NULL;
  548. png_ptr->num_chunk_list = 0;
  549. }
  550. #endif
  551. png_info_init_3(&info_ptr, png_sizeof(png_info));
  552. }
  553. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  554. /* This function returns a pointer to the io_ptr associated with the user
  555. * functions. The application should free any memory associated with this
  556. * pointer before png_write_destroy() or png_read_destroy() are called.
  557. */
  558. png_voidp PNGAPI
  559. png_get_io_ptr(png_structp png_ptr)
  560. {
  561. if (png_ptr == NULL)
  562. return (NULL);
  563. return (png_ptr->io_ptr);
  564. }
  565. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  566. #ifdef PNG_STDIO_SUPPORTED
  567. /* Initialize the default input/output functions for the PNG file. If you
  568. * use your own read or write routines, you can call either png_set_read_fn()
  569. * or png_set_write_fn() instead of png_init_io(). If you have defined
  570. * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
  571. * necessarily available.
  572. */
  573. void PNGAPI
  574. png_init_io(png_structp png_ptr, png_FILE_p fp)
  575. {
  576. png_debug(1, "in png_init_io");
  577. if (png_ptr == NULL)
  578. return;
  579. png_ptr->io_ptr = (png_voidp)fp;
  580. }
  581. #endif
  582. #ifdef PNG_TIME_RFC1123_SUPPORTED
  583. /* Convert the supplied time into an RFC 1123 string suitable for use in
  584. * a "Creation Time" or other text-based time string.
  585. */
  586. png_charp PNGAPI
  587. png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
  588. {
  589. static PNG_CONST char short_months[12][4] =
  590. {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  591. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  592. if (png_ptr == NULL)
  593. return (NULL);
  594. if (png_ptr->time_buffer == NULL)
  595. {
  596. png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
  597. png_sizeof(char)));
  598. }
  599. #ifdef _WIN32_WCE
  600. {
  601. wchar_t time_buf[29];
  602. wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
  603. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  604. ptime->year, ptime->hour % 24, ptime->minute % 60,
  605. ptime->second % 61);
  606. WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer,
  607. 29, NULL, NULL);
  608. }
  609. #else
  610. #ifdef USE_FAR_KEYWORD
  611. {
  612. char near_time_buf[29];
  613. png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
  614. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  615. ptime->year, ptime->hour % 24, ptime->minute % 60,
  616. ptime->second % 61);
  617. png_memcpy(png_ptr->time_buffer, near_time_buf,
  618. 29*png_sizeof(char));
  619. }
  620. #else
  621. png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
  622. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  623. ptime->year, ptime->hour % 24, ptime->minute % 60,
  624. ptime->second % 61);
  625. #endif
  626. #endif /* _WIN32_WCE */
  627. return ((png_charp)png_ptr->time_buffer);
  628. }
  629. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  630. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  631. png_charp PNGAPI
  632. png_get_copyright(png_structp png_ptr)
  633. {
  634. png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
  635. #ifdef PNG_STRING_COPYRIGHT
  636. return PNG_STRING_COPYRIGHT
  637. #else
  638. #ifdef __STDC__
  639. return ((png_charp) PNG_STRING_NEWLINE \
  640. "libpng version 1.2.44 - June 26, 2010" PNG_STRING_NEWLINE \
  641. "Copyright (c) 1998-2010 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
  642. "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
  643. "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
  644. PNG_STRING_NEWLINE);
  645. #else
  646. return ((png_charp) "libpng version 1.2.44 - June 26, 2010\
  647. Copyright (c) 1998-2010 Glenn Randers-Pehrson\
  648. Copyright (c) 1996-1997 Andreas Dilger\
  649. Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
  650. #endif
  651. #endif
  652. }
  653. /* The following return the library version as a short string in the
  654. * format 1.0.0 through 99.99.99zz. To get the version of *.h files
  655. * used with your application, print out PNG_LIBPNG_VER_STRING, which
  656. * is defined in png.h.
  657. * Note: now there is no difference between png_get_libpng_ver() and
  658. * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
  659. * it is guaranteed that png.c uses the correct version of png.h.
  660. */
  661. png_charp PNGAPI
  662. png_get_libpng_ver(png_structp png_ptr)
  663. {
  664. /* Version of *.c files used when building libpng */
  665. png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
  666. return ((png_charp) PNG_LIBPNG_VER_STRING);
  667. }
  668. png_charp PNGAPI
  669. png_get_header_ver(png_structp png_ptr)
  670. {
  671. /* Version of *.h files used when building libpng */
  672. png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
  673. return ((png_charp) PNG_LIBPNG_VER_STRING);
  674. }
  675. png_charp PNGAPI
  676. png_get_header_version(png_structp png_ptr)
  677. {
  678. /* Returns longer string containing both version and date */
  679. png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
  680. #ifdef __STDC__
  681. return ((png_charp) PNG_HEADER_VERSION_STRING
  682. #ifndef PNG_READ_SUPPORTED
  683. " (NO READ SUPPORT)"
  684. #endif
  685. PNG_STRING_NEWLINE);
  686. #else
  687. return ((png_charp) PNG_HEADER_VERSION_STRING);
  688. #endif
  689. }
  690. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  691. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  692. int PNGAPI
  693. png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
  694. {
  695. /* Check chunk_name and return "keep" value if it's on the list, else 0 */
  696. int i;
  697. png_bytep p;
  698. if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
  699. return 0;
  700. p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
  701. for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
  702. if (!png_memcmp(chunk_name, p, 4))
  703. return ((int)*(p + 4));
  704. return 0;
  705. }
  706. #endif
  707. /* This function, added to libpng-1.0.6g, is untested. */
  708. int PNGAPI
  709. png_reset_zstream(png_structp png_ptr)
  710. {
  711. if (png_ptr == NULL)
  712. return Z_STREAM_ERROR;
  713. return (inflateReset(&png_ptr->zstream));
  714. }
  715. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  716. /* This function was added to libpng-1.0.7 */
  717. png_uint_32 PNGAPI
  718. png_access_version_number(void)
  719. {
  720. /* Version of *.c files used when building libpng */
  721. return((png_uint_32) PNG_LIBPNG_VER);
  722. }
  723. #if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
  724. #ifndef PNG_1_0_X
  725. /* This function was added to libpng 1.2.0 */
  726. int PNGAPI
  727. png_mmx_support(void)
  728. {
  729. /* Obsolete, to be removed from libpng-1.4.0 */
  730. return -1;
  731. }
  732. #endif /* PNG_1_0_X */
  733. #endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
  734. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  735. #ifdef PNG_SIZE_T
  736. /* Added at libpng version 1.2.6 */
  737. PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
  738. png_size_t PNGAPI
  739. png_convert_size(size_t size)
  740. {
  741. if (size > (png_size_t)-1)
  742. PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
  743. return ((png_size_t)size);
  744. }
  745. #endif /* PNG_SIZE_T */
  746. /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
  747. #ifdef PNG_cHRM_SUPPORTED
  748. #ifdef PNG_CHECK_cHRM_SUPPORTED
  749. /*
  750. * Multiply two 32-bit numbers, V1 and V2, using 32-bit
  751. * arithmetic, to produce a 64 bit result in the HI/LO words.
  752. *
  753. * A B
  754. * x C D
  755. * ------
  756. * AD || BD
  757. * AC || CB || 0
  758. *
  759. * where A and B are the high and low 16-bit words of V1,
  760. * C and D are the 16-bit words of V2, AD is the product of
  761. * A and D, and X || Y is (X << 16) + Y.
  762. */
  763. void /* PRIVATE */
  764. png_64bit_product (long v1, long v2, unsigned long *hi_product,
  765. unsigned long *lo_product)
  766. {
  767. int a, b, c, d;
  768. long lo, hi, x, y;
  769. a = (v1 >> 16) & 0xffff;
  770. b = v1 & 0xffff;
  771. c = (v2 >> 16) & 0xffff;
  772. d = v2 & 0xffff;
  773. lo = b * d; /* BD */
  774. x = a * d + c * b; /* AD + CB */
  775. y = ((lo >> 16) & 0xffff) + x;
  776. lo = (lo & 0xffff) | ((y & 0xffff) << 16);
  777. hi = (y >> 16) & 0xffff;
  778. hi += a * c; /* AC */
  779. *hi_product = (unsigned long)hi;
  780. *lo_product = (unsigned long)lo;
  781. }
  782. int /* PRIVATE */
  783. png_check_cHRM_fixed(png_structp png_ptr,
  784. png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
  785. png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
  786. png_fixed_point blue_x, png_fixed_point blue_y)
  787. {
  788. int ret = 1;
  789. unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
  790. png_debug(1, "in function png_check_cHRM_fixed");
  791. if (png_ptr == NULL)
  792. return 0;
  793. if (white_x < 0 || white_y <= 0 ||
  794. red_x < 0 || red_y < 0 ||
  795. green_x < 0 || green_y < 0 ||
  796. blue_x < 0 || blue_y < 0)
  797. {
  798. png_warning(png_ptr,
  799. "Ignoring attempt to set negative chromaticity value");
  800. ret = 0;
  801. }
  802. if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
  803. white_y > (png_fixed_point) PNG_UINT_31_MAX ||
  804. red_x > (png_fixed_point) PNG_UINT_31_MAX ||
  805. red_y > (png_fixed_point) PNG_UINT_31_MAX ||
  806. green_x > (png_fixed_point) PNG_UINT_31_MAX ||
  807. green_y > (png_fixed_point) PNG_UINT_31_MAX ||
  808. blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
  809. blue_y > (png_fixed_point) PNG_UINT_31_MAX )
  810. {
  811. png_warning(png_ptr,
  812. "Ignoring attempt to set chromaticity value exceeding 21474.83");
  813. ret = 0;
  814. }
  815. if (white_x > 100000L - white_y)
  816. {
  817. png_warning(png_ptr, "Invalid cHRM white point");
  818. ret = 0;
  819. }
  820. if (red_x > 100000L - red_y)
  821. {
  822. png_warning(png_ptr, "Invalid cHRM red point");
  823. ret = 0;
  824. }
  825. if (green_x > 100000L - green_y)
  826. {
  827. png_warning(png_ptr, "Invalid cHRM green point");
  828. ret = 0;
  829. }
  830. if (blue_x > 100000L - blue_y)
  831. {
  832. png_warning(png_ptr, "Invalid cHRM blue point");
  833. ret = 0;
  834. }
  835. png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
  836. png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
  837. if (xy_hi == yx_hi && xy_lo == yx_lo)
  838. {
  839. png_warning(png_ptr,
  840. "Ignoring attempt to set cHRM RGB triangle with zero area");
  841. ret = 0;
  842. }
  843. return ret;
  844. }
  845. #endif /* PNG_CHECK_cHRM_SUPPORTED */
  846. #endif /* PNG_cHRM_SUPPORTED */
  847. void /* PRIVATE */
  848. png_check_IHDR(png_structp png_ptr,
  849. png_uint_32 width, png_uint_32 height, int bit_depth,
  850. int color_type, int interlace_type, int compression_type,
  851. int filter_type)
  852. {
  853. int error = 0;
  854. /* Check for width and height valid values */
  855. if (width == 0)
  856. {
  857. png_warning(png_ptr, "Image width is zero in IHDR");
  858. error = 1;
  859. }
  860. if (height == 0)
  861. {
  862. png_warning(png_ptr, "Image height is zero in IHDR");
  863. error = 1;
  864. }
  865. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  866. if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX)
  867. #else
  868. if (width > PNG_USER_WIDTH_MAX)
  869. #endif
  870. {
  871. png_warning(png_ptr, "Image width exceeds user limit in IHDR");
  872. error = 1;
  873. }
  874. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  875. if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX)
  876. #else
  877. if (height > PNG_USER_HEIGHT_MAX)
  878. #endif
  879. {
  880. png_warning(png_ptr, "Image height exceeds user limit in IHDR");
  881. error = 1;
  882. }
  883. if (width > PNG_UINT_31_MAX)
  884. {
  885. png_warning(png_ptr, "Invalid image width in IHDR");
  886. error = 1;
  887. }
  888. if ( height > PNG_UINT_31_MAX)
  889. {
  890. png_warning(png_ptr, "Invalid image height in IHDR");
  891. error = 1;
  892. }
  893. if ( width > (PNG_UINT_32_MAX
  894. >> 3) /* 8-byte RGBA pixels */
  895. - 64 /* bigrowbuf hack */
  896. - 1 /* filter byte */
  897. - 7*8 /* rounding of width to multiple of 8 pixels */
  898. - 8) /* extra max_pixel_depth pad */
  899. png_warning(png_ptr, "Width is too large for libpng to process pixels");
  900. /* Check other values */
  901. if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
  902. bit_depth != 8 && bit_depth != 16)
  903. {
  904. png_warning(png_ptr, "Invalid bit depth in IHDR");
  905. error = 1;
  906. }
  907. if (color_type < 0 || color_type == 1 ||
  908. color_type == 5 || color_type > 6)
  909. {
  910. png_warning(png_ptr, "Invalid color type in IHDR");
  911. error = 1;
  912. }
  913. if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
  914. ((color_type == PNG_COLOR_TYPE_RGB ||
  915. color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  916. color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
  917. {
  918. png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
  919. error = 1;
  920. }
  921. if (interlace_type >= PNG_INTERLACE_LAST)
  922. {
  923. png_warning(png_ptr, "Unknown interlace method in IHDR");
  924. error = 1;
  925. }
  926. if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  927. {
  928. png_warning(png_ptr, "Unknown compression method in IHDR");
  929. error = 1;
  930. }
  931. #ifdef PNG_MNG_FEATURES_SUPPORTED
  932. /* Accept filter_method 64 (intrapixel differencing) only if
  933. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  934. * 2. Libpng did not read a PNG signature (this filter_method is only
  935. * used in PNG datastreams that are embedded in MNG datastreams) and
  936. * 3. The application called png_permit_mng_features with a mask that
  937. * included PNG_FLAG_MNG_FILTER_64 and
  938. * 4. The filter_method is 64 and
  939. * 5. The color_type is RGB or RGBA
  940. */
  941. if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
  942. png_ptr->mng_features_permitted)
  943. png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
  944. if (filter_type != PNG_FILTER_TYPE_BASE)
  945. {
  946. if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  947. (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
  948. ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
  949. (color_type == PNG_COLOR_TYPE_RGB ||
  950. color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
  951. {
  952. png_warning(png_ptr, "Unknown filter method in IHDR");
  953. error = 1;
  954. }
  955. if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
  956. {
  957. png_warning(png_ptr, "Invalid filter method in IHDR");
  958. error = 1;
  959. }
  960. }
  961. #else
  962. if (filter_type != PNG_FILTER_TYPE_BASE)
  963. {
  964. png_warning(png_ptr, "Unknown filter method in IHDR");
  965. error = 1;
  966. }
  967. #endif
  968. if (error == 1)
  969. png_error(png_ptr, "Invalid IHDR data");
  970. }
  971. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */