/thirdparty/libpng/png.c

http://crashrpt.googlecode.com/ · C · 826 lines · 616 code · 92 blank · 118 comment · 110 complexity · 2ffce6485b39a4b8391ff665abafe315 MD5 · raw file

  1. /* png.c - location for general purpose libpng functions
  2. *
  3. * libpng version 1.2.7 - September 12, 2004
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1998-2004 Glenn Randers-Pehrson
  6. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8. */
  9. #define PNG_INTERNAL
  10. #define PNG_NO_EXTERN
  11. #include "png.h"
  12. /* Generate a compiler error if there is an old png.h in the search path. */
  13. typedef version_1_2_7 Your_png_h_is_not_version_1_2_7;
  14. /* Version information for C files. This had better match the version
  15. * string defined in png.h. */
  16. #ifdef PNG_USE_GLOBAL_ARRAYS
  17. /* png_libpng_ver was changed to a function in version 1.0.5c */
  18. const char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
  19. /* png_sig was changed to a function in version 1.0.5c */
  20. /* Place to hold the signature string for a PNG file. */
  21. const png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  22. /* Invoke global declarations for constant strings for known chunk types */
  23. PNG_IHDR;
  24. PNG_IDAT;
  25. PNG_IEND;
  26. PNG_PLTE;
  27. PNG_bKGD;
  28. PNG_cHRM;
  29. PNG_gAMA;
  30. PNG_hIST;
  31. PNG_iCCP;
  32. PNG_iTXt;
  33. PNG_oFFs;
  34. PNG_pCAL;
  35. PNG_sCAL;
  36. PNG_pHYs;
  37. PNG_sBIT;
  38. PNG_sPLT;
  39. PNG_sRGB;
  40. PNG_tEXt;
  41. PNG_tIME;
  42. PNG_tRNS;
  43. PNG_zTXt;
  44. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  45. /* start of interlace block */
  46. const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  47. /* offset to next interlace block */
  48. const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  49. /* start of interlace block in the y direction */
  50. const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  51. /* offset to next interlace block in the y direction */
  52. const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  53. /* width of interlace block (used in assembler routines only) */
  54. #ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW
  55. const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
  56. #endif
  57. /* Height of interlace block. This is not currently used - if you need
  58. * it, uncomment it here and in png.h
  59. const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  60. */
  61. /* Mask to determine which pixels are valid in a pass */
  62. const int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  63. /* Mask to determine which pixels to overwrite while displaying */
  64. const int FARDATA png_pass_dsp_mask[]
  65. = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  66. #endif
  67. /* Tells libpng that we have already handled the first "num_bytes" bytes
  68. * of the PNG file signature. If the PNG data is embedded into another
  69. * stream we can set num_bytes = 8 so that libpng will not attempt to read
  70. * or write any of the magic bytes before it starts on the IHDR.
  71. */
  72. void PNGAPI
  73. png_set_sig_bytes(png_structp png_ptr, int num_bytes)
  74. {
  75. png_debug(1, "in png_set_sig_bytes\n");
  76. if (num_bytes > 8)
  77. png_error(png_ptr, "Too many bytes for PNG signature.");
  78. png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
  79. }
  80. /* Checks whether the supplied bytes match the PNG signature. We allow
  81. * checking less than the full 8-byte signature so that those apps that
  82. * already read the first few bytes of a file to determine the file type
  83. * can simply check the remaining bytes for extra assurance. Returns
  84. * an integer less than, equal to, or greater than zero if sig is found,
  85. * respectively, to be less than, to match, or be greater than the correct
  86. * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
  87. */
  88. int PNGAPI
  89. png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
  90. {
  91. png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  92. if (num_to_check > 8)
  93. num_to_check = 8;
  94. else if (num_to_check < 1)
  95. return (0);
  96. if (start > 7)
  97. return (0);
  98. if (start + num_to_check > 8)
  99. num_to_check = 8 - start;
  100. return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
  101. }
  102. /* (Obsolete) function to check signature bytes. It does not allow one
  103. * to check a partial signature. This function might be removed in the
  104. * future - use png_sig_cmp(). Returns true (nonzero) if the file is a PNG.
  105. */
  106. int PNGAPI
  107. png_check_sig(png_bytep sig, int num)
  108. {
  109. return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
  110. }
  111. /* Function to allocate memory for zlib and clear it to 0. */
  112. #ifdef PNG_1_0_X
  113. voidpf PNGAPI
  114. #else
  115. voidpf /* private */
  116. #endif
  117. png_zalloc(voidpf png_ptr, uInt items, uInt size)
  118. {
  119. png_voidp ptr;
  120. png_structp p=png_ptr;
  121. png_uint_32 save_flags=p->flags;
  122. png_uint_32 num_bytes;
  123. if (items > PNG_UINT_32_MAX/size)
  124. {
  125. png_warning (png_ptr, "Potential overflow in png_zalloc()");
  126. return (NULL);
  127. }
  128. num_bytes = (png_uint_32)items * size;
  129. p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  130. ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
  131. p->flags=save_flags;
  132. #if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
  133. if (ptr == NULL)
  134. return ((voidpf)ptr);
  135. if (num_bytes > (png_uint_32)0x8000L)
  136. {
  137. png_memset(ptr, 0, (png_size_t)0x8000L);
  138. png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  139. (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  140. }
  141. else
  142. {
  143. png_memset(ptr, 0, (png_size_t)num_bytes);
  144. }
  145. #endif
  146. return ((voidpf)ptr);
  147. }
  148. /* function to free memory for zlib */
  149. #ifdef PNG_1_0_X
  150. void PNGAPI
  151. #else
  152. void /* private */
  153. #endif
  154. png_zfree(voidpf png_ptr, voidpf ptr)
  155. {
  156. png_free((png_structp)png_ptr, (png_voidp)ptr);
  157. }
  158. /* Reset the CRC variable to 32 bits of 1's. Care must be taken
  159. * in case CRC is > 32 bits to leave the top bits 0.
  160. */
  161. void /* PRIVATE */
  162. png_reset_crc(png_structp png_ptr)
  163. {
  164. png_ptr->crc = crc32(0, Z_NULL, 0);
  165. }
  166. /* Calculate the CRC over a section of data. We can only pass as
  167. * much data to this routine as the largest single buffer size. We
  168. * also check that this data will actually be used before going to the
  169. * trouble of calculating it.
  170. */
  171. void /* PRIVATE */
  172. png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
  173. {
  174. int need_crc = 1;
  175. if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
  176. {
  177. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  178. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  179. need_crc = 0;
  180. }
  181. else /* critical */
  182. {
  183. if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  184. need_crc = 0;
  185. }
  186. if (need_crc)
  187. png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
  188. }
  189. /* Allocate the memory for an info_struct for the application. We don't
  190. * really need the png_ptr, but it could potentially be useful in the
  191. * future. This should be used in favour of malloc(png_sizeof(png_info))
  192. * and png_info_init() so that applications that want to use a shared
  193. * libpng don't have to be recompiled if png_info changes size.
  194. */
  195. png_infop PNGAPI
  196. png_create_info_struct(png_structp png_ptr)
  197. {
  198. png_infop info_ptr;
  199. png_debug(1, "in png_create_info_struct\n");
  200. if(png_ptr == NULL) return (NULL);
  201. #ifdef PNG_USER_MEM_SUPPORTED
  202. info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
  203. png_ptr->malloc_fn, png_ptr->mem_ptr);
  204. #else
  205. info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  206. #endif
  207. if (info_ptr != NULL)
  208. png_info_init_3(&info_ptr, png_sizeof(png_info));
  209. return (info_ptr);
  210. }
  211. /* This function frees the memory associated with a single info struct.
  212. * Normally, one would use either png_destroy_read_struct() or
  213. * png_destroy_write_struct() to free an info struct, but this may be
  214. * useful for some applications.
  215. */
  216. void PNGAPI
  217. png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
  218. {
  219. png_infop info_ptr = NULL;
  220. png_debug(1, "in png_destroy_info_struct\n");
  221. if (info_ptr_ptr != NULL)
  222. info_ptr = *info_ptr_ptr;
  223. if (info_ptr != NULL)
  224. {
  225. png_info_destroy(png_ptr, info_ptr);
  226. #ifdef PNG_USER_MEM_SUPPORTED
  227. png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
  228. png_ptr->mem_ptr);
  229. #else
  230. png_destroy_struct((png_voidp)info_ptr);
  231. #endif
  232. *info_ptr_ptr = NULL;
  233. }
  234. }
  235. /* Initialize the info structure. This is now an internal function (0.89)
  236. * and applications using it are urged to use png_create_info_struct()
  237. * instead.
  238. */
  239. #undef png_info_init
  240. void PNGAPI
  241. png_info_init(png_infop info_ptr)
  242. {
  243. /* We only come here via pre-1.0.12-compiled applications */
  244. png_info_init_3(&info_ptr, 0);
  245. }
  246. void PNGAPI
  247. png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
  248. {
  249. png_infop info_ptr = *ptr_ptr;
  250. png_debug(1, "in png_info_init_3\n");
  251. if(png_sizeof(png_info) > png_info_struct_size)
  252. {
  253. png_destroy_struct(info_ptr);
  254. info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  255. *ptr_ptr = info_ptr;
  256. }
  257. /* set everything to 0 */
  258. png_memset(info_ptr, 0, png_sizeof (png_info));
  259. }
  260. #ifdef PNG_FREE_ME_SUPPORTED
  261. void PNGAPI
  262. png_data_freer(png_structp png_ptr, png_infop info_ptr,
  263. int freer, png_uint_32 mask)
  264. {
  265. png_debug(1, "in png_data_freer\n");
  266. if (png_ptr == NULL || info_ptr == NULL)
  267. return;
  268. if(freer == PNG_DESTROY_WILL_FREE_DATA)
  269. info_ptr->free_me |= mask;
  270. else if(freer == PNG_USER_WILL_FREE_DATA)
  271. info_ptr->free_me &= ~mask;
  272. else
  273. png_warning(png_ptr,
  274. "Unknown freer parameter in png_data_freer.");
  275. }
  276. #endif
  277. void PNGAPI
  278. png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
  279. int num)
  280. {
  281. png_debug(1, "in png_free_data\n");
  282. if (png_ptr == NULL || info_ptr == NULL)
  283. return;
  284. #if defined(PNG_TEXT_SUPPORTED)
  285. /* free text item num or (if num == -1) all text items */
  286. #ifdef PNG_FREE_ME_SUPPORTED
  287. if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
  288. #else
  289. if (mask & PNG_FREE_TEXT)
  290. #endif
  291. {
  292. if (num != -1)
  293. {
  294. if (info_ptr->text && info_ptr->text[num].key)
  295. {
  296. png_free(png_ptr, info_ptr->text[num].key);
  297. info_ptr->text[num].key = NULL;
  298. }
  299. }
  300. else
  301. {
  302. int i;
  303. for (i = 0; i < info_ptr->num_text; i++)
  304. png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
  305. png_free(png_ptr, info_ptr->text);
  306. info_ptr->text = NULL;
  307. info_ptr->num_text=0;
  308. }
  309. }
  310. #endif
  311. #if defined(PNG_tRNS_SUPPORTED)
  312. /* free any tRNS entry */
  313. #ifdef PNG_FREE_ME_SUPPORTED
  314. if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
  315. #else
  316. if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
  317. #endif
  318. {
  319. png_free(png_ptr, info_ptr->trans);
  320. info_ptr->valid &= ~PNG_INFO_tRNS;
  321. #ifndef PNG_FREE_ME_SUPPORTED
  322. png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
  323. #endif
  324. info_ptr->trans = NULL;
  325. }
  326. #endif
  327. #if defined(PNG_sCAL_SUPPORTED)
  328. /* free any sCAL entry */
  329. #ifdef PNG_FREE_ME_SUPPORTED
  330. if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
  331. #else
  332. if (mask & PNG_FREE_SCAL)
  333. #endif
  334. {
  335. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  336. png_free(png_ptr, info_ptr->scal_s_width);
  337. png_free(png_ptr, info_ptr->scal_s_height);
  338. info_ptr->scal_s_width = NULL;
  339. info_ptr->scal_s_height = NULL;
  340. #endif
  341. info_ptr->valid &= ~PNG_INFO_sCAL;
  342. }
  343. #endif
  344. #if defined(PNG_pCAL_SUPPORTED)
  345. /* free any pCAL entry */
  346. #ifdef PNG_FREE_ME_SUPPORTED
  347. if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
  348. #else
  349. if (mask & PNG_FREE_PCAL)
  350. #endif
  351. {
  352. png_free(png_ptr, info_ptr->pcal_purpose);
  353. png_free(png_ptr, info_ptr->pcal_units);
  354. info_ptr->pcal_purpose = NULL;
  355. info_ptr->pcal_units = NULL;
  356. if (info_ptr->pcal_params != NULL)
  357. {
  358. int i;
  359. for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
  360. {
  361. png_free(png_ptr, info_ptr->pcal_params[i]);
  362. info_ptr->pcal_params[i]=NULL;
  363. }
  364. png_free(png_ptr, info_ptr->pcal_params);
  365. info_ptr->pcal_params = NULL;
  366. }
  367. info_ptr->valid &= ~PNG_INFO_pCAL;
  368. }
  369. #endif
  370. #if defined(PNG_iCCP_SUPPORTED)
  371. /* free any iCCP entry */
  372. #ifdef PNG_FREE_ME_SUPPORTED
  373. if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
  374. #else
  375. if (mask & PNG_FREE_ICCP)
  376. #endif
  377. {
  378. png_free(png_ptr, info_ptr->iccp_name);
  379. png_free(png_ptr, info_ptr->iccp_profile);
  380. info_ptr->iccp_name = NULL;
  381. info_ptr->iccp_profile = NULL;
  382. info_ptr->valid &= ~PNG_INFO_iCCP;
  383. }
  384. #endif
  385. #if defined(PNG_sPLT_SUPPORTED)
  386. /* free a given sPLT entry, or (if num == -1) all sPLT entries */
  387. #ifdef PNG_FREE_ME_SUPPORTED
  388. if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
  389. #else
  390. if (mask & PNG_FREE_SPLT)
  391. #endif
  392. {
  393. if (num != -1)
  394. {
  395. if(info_ptr->splt_palettes)
  396. {
  397. png_free(png_ptr, info_ptr->splt_palettes[num].name);
  398. png_free(png_ptr, info_ptr->splt_palettes[num].entries);
  399. info_ptr->splt_palettes[num].name = NULL;
  400. info_ptr->splt_palettes[num].entries = NULL;
  401. }
  402. }
  403. else
  404. {
  405. if(info_ptr->splt_palettes_num)
  406. {
  407. int i;
  408. for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  409. png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
  410. png_free(png_ptr, info_ptr->splt_palettes);
  411. info_ptr->splt_palettes = NULL;
  412. info_ptr->splt_palettes_num = 0;
  413. }
  414. info_ptr->valid &= ~PNG_INFO_sPLT;
  415. }
  416. }
  417. #endif
  418. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  419. #ifdef PNG_FREE_ME_SUPPORTED
  420. if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
  421. #else
  422. if (mask & PNG_FREE_UNKN)
  423. #endif
  424. {
  425. if (num != -1)
  426. {
  427. if(info_ptr->unknown_chunks)
  428. {
  429. png_free(png_ptr, info_ptr->unknown_chunks[num].data);
  430. info_ptr->unknown_chunks[num].data = NULL;
  431. }
  432. }
  433. else
  434. {
  435. int i;
  436. if(info_ptr->unknown_chunks_num)
  437. {
  438. for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
  439. png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
  440. png_free(png_ptr, info_ptr->unknown_chunks);
  441. info_ptr->unknown_chunks = NULL;
  442. info_ptr->unknown_chunks_num = 0;
  443. }
  444. }
  445. }
  446. #endif
  447. #if defined(PNG_hIST_SUPPORTED)
  448. /* free any hIST entry */
  449. #ifdef PNG_FREE_ME_SUPPORTED
  450. if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
  451. #else
  452. if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
  453. #endif
  454. {
  455. png_free(png_ptr, info_ptr->hist);
  456. info_ptr->hist = NULL;
  457. info_ptr->valid &= ~PNG_INFO_hIST;
  458. #ifndef PNG_FREE_ME_SUPPORTED
  459. png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
  460. #endif
  461. }
  462. #endif
  463. /* free any PLTE entry that was internally allocated */
  464. #ifdef PNG_FREE_ME_SUPPORTED
  465. if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
  466. #else
  467. if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
  468. #endif
  469. {
  470. png_zfree(png_ptr, info_ptr->palette);
  471. info_ptr->palette = NULL;
  472. info_ptr->valid &= ~PNG_INFO_PLTE;
  473. #ifndef PNG_FREE_ME_SUPPORTED
  474. png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
  475. #endif
  476. info_ptr->num_palette = 0;
  477. }
  478. #if defined(PNG_INFO_IMAGE_SUPPORTED)
  479. /* free any image bits attached to the info structure */
  480. #ifdef PNG_FREE_ME_SUPPORTED
  481. if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
  482. #else
  483. if (mask & PNG_FREE_ROWS)
  484. #endif
  485. {
  486. if(info_ptr->row_pointers)
  487. {
  488. int row;
  489. for (row = 0; row < (int)info_ptr->height; row++)
  490. {
  491. png_free(png_ptr, info_ptr->row_pointers[row]);
  492. info_ptr->row_pointers[row]=NULL;
  493. }
  494. png_free(png_ptr, info_ptr->row_pointers);
  495. info_ptr->row_pointers=NULL;
  496. }
  497. info_ptr->valid &= ~PNG_INFO_IDAT;
  498. }
  499. #endif
  500. #ifdef PNG_FREE_ME_SUPPORTED
  501. if(num == -1)
  502. info_ptr->free_me &= ~mask;
  503. else
  504. info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
  505. #endif
  506. }
  507. /* This is an internal routine to free any memory that the info struct is
  508. * pointing to before re-using it or freeing the struct itself. Recall
  509. * that png_free() checks for NULL pointers for us.
  510. */
  511. void /* PRIVATE */
  512. png_info_destroy(png_structp png_ptr, png_infop info_ptr)
  513. {
  514. png_debug(1, "in png_info_destroy\n");
  515. png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  516. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  517. if (png_ptr->num_chunk_list)
  518. {
  519. png_free(png_ptr, png_ptr->chunk_list);
  520. png_ptr->chunk_list=NULL;
  521. png_ptr->num_chunk_list=0;
  522. }
  523. #endif
  524. png_info_init_3(&info_ptr, png_sizeof(png_info));
  525. }
  526. /* This function returns a pointer to the io_ptr associated with the user
  527. * functions. The application should free any memory associated with this
  528. * pointer before png_write_destroy() or png_read_destroy() are called.
  529. */
  530. png_voidp PNGAPI
  531. png_get_io_ptr(png_structp png_ptr)
  532. {
  533. return (png_ptr->io_ptr);
  534. }
  535. #if !defined(PNG_NO_STDIO)
  536. /* Initialize the default input/output functions for the PNG file. If you
  537. * use your own read or write routines, you can call either png_set_read_fn()
  538. * or png_set_write_fn() instead of png_init_io(). If you have defined
  539. * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
  540. * necessarily available.
  541. */
  542. void PNGAPI
  543. png_init_io(png_structp png_ptr, png_FILE_p fp)
  544. {
  545. png_debug(1, "in png_init_io\n");
  546. png_ptr->io_ptr = (png_voidp)fp;
  547. }
  548. #endif
  549. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  550. /* Convert the supplied time into an RFC 1123 string suitable for use in
  551. * a "Creation Time" or other text-based time string.
  552. */
  553. png_charp PNGAPI
  554. png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
  555. {
  556. static PNG_CONST char short_months[12][4] =
  557. {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  558. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  559. if (png_ptr->time_buffer == NULL)
  560. {
  561. png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
  562. png_sizeof(char)));
  563. }
  564. #if defined(_WIN32_WCE)
  565. {
  566. wchar_t time_buf[29];
  567. wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
  568. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  569. ptime->year, ptime->hour % 24, ptime->minute % 60,
  570. ptime->second % 61);
  571. WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
  572. NULL, NULL);
  573. }
  574. #else
  575. #ifdef USE_FAR_KEYWORD
  576. {
  577. char near_time_buf[29];
  578. sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000",
  579. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  580. ptime->year, ptime->hour % 24, ptime->minute % 60,
  581. ptime->second % 61);
  582. png_memcpy(png_ptr->time_buffer, near_time_buf,
  583. 29*png_sizeof(char));
  584. }
  585. #else
  586. sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000",
  587. ptime->day % 32, short_months[(ptime->month - 1) % 12],
  588. ptime->year, ptime->hour % 24, ptime->minute % 60,
  589. ptime->second % 61);
  590. #endif
  591. #endif /* _WIN32_WCE */
  592. return ((png_charp)png_ptr->time_buffer);
  593. }
  594. #endif /* PNG_TIME_RFC1123_SUPPORTED */
  595. #if 0
  596. /* Signature string for a PNG file. */
  597. png_bytep PNGAPI
  598. png_sig_bytes(void)
  599. {
  600. return ((png_bytep)"\211\120\116\107\015\012\032\012");
  601. }
  602. #endif
  603. png_charp PNGAPI
  604. png_get_copyright(png_structp png_ptr)
  605. {
  606. if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */
  607. return ((png_charp) "\n libpng version 1.2.7 - September 12, 2004\n\
  608. Copyright (c) 1998-2004 Glenn Randers-Pehrson\n\
  609. Copyright (c) 1996-1997 Andreas Dilger\n\
  610. Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
  611. return ((png_charp) "");
  612. }
  613. /* The following return the library version as a short string in the
  614. * format 1.0.0 through 99.99.99zz. To get the version of *.h files
  615. * used with your application, print out PNG_LIBPNG_VER_STRING, which
  616. * is defined in png.h.
  617. * Note: now there is no difference between png_get_libpng_ver() and
  618. * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
  619. * it is guaranteed that png.c uses the correct version of png.h.
  620. */
  621. png_charp PNGAPI
  622. png_get_libpng_ver(png_structp png_ptr)
  623. {
  624. /* Version of *.c files used when building libpng */
  625. if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */
  626. return ((png_charp) PNG_LIBPNG_VER_STRING);
  627. return ((png_charp) "");
  628. }
  629. png_charp PNGAPI
  630. png_get_header_ver(png_structp png_ptr)
  631. {
  632. /* Version of *.h files used when building libpng */
  633. if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */
  634. return ((png_charp) PNG_LIBPNG_VER_STRING);
  635. return ((png_charp) "");
  636. }
  637. png_charp PNGAPI
  638. png_get_header_version(png_structp png_ptr)
  639. {
  640. /* Returns longer string containing both version and date */
  641. if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */
  642. return ((png_charp) PNG_HEADER_VERSION_STRING);
  643. return ((png_charp) "");
  644. }
  645. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  646. int PNGAPI
  647. png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
  648. {
  649. /* check chunk_name and return "keep" value if it's on the list, else 0 */
  650. int i;
  651. png_bytep p;
  652. if((png_ptr == NULL && chunk_name == NULL) || png_ptr->num_chunk_list<=0)
  653. return 0;
  654. p=png_ptr->chunk_list+png_ptr->num_chunk_list*5-5;
  655. for (i = png_ptr->num_chunk_list; i; i--, p-=5)
  656. if (!png_memcmp(chunk_name, p, 4))
  657. return ((int)*(p+4));
  658. return 0;
  659. }
  660. #endif
  661. /* This function, added to libpng-1.0.6g, is untested. */
  662. int PNGAPI
  663. png_reset_zstream(png_structp png_ptr)
  664. {
  665. return (inflateReset(&png_ptr->zstream));
  666. }
  667. /* This function was added to libpng-1.0.7 */
  668. png_uint_32 PNGAPI
  669. png_access_version_number(void)
  670. {
  671. /* Version of *.c files used when building libpng */
  672. return((png_uint_32) PNG_LIBPNG_VER);
  673. }
  674. #if !defined(PNG_1_0_X)
  675. #if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
  676. /* GRR: could add this: && defined(PNG_MMX_CODE_SUPPORTED) */
  677. /* this INTERNAL function was added to libpng 1.2.0 */
  678. void /* PRIVATE */
  679. png_init_mmx_flags (png_structp png_ptr)
  680. {
  681. png_ptr->mmx_rowbytes_threshold = 0;
  682. png_ptr->mmx_bitdepth_threshold = 0;
  683. # if (defined(PNG_USE_PNGVCRD) || defined(PNG_USE_PNGGCCRD))
  684. png_ptr->asm_flags |= PNG_ASM_FLAG_MMX_SUPPORT_COMPILED;
  685. if (png_mmx_support() > 0) {
  686. png_ptr->asm_flags |= PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
  687. # ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW
  688. | PNG_ASM_FLAG_MMX_READ_COMBINE_ROW
  689. # endif
  690. # ifdef PNG_HAVE_ASSEMBLER_READ_INTERLACE
  691. | PNG_ASM_FLAG_MMX_READ_INTERLACE
  692. # endif
  693. # ifndef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW
  694. ;
  695. # else
  696. | PNG_ASM_FLAG_MMX_READ_FILTER_SUB
  697. | PNG_ASM_FLAG_MMX_READ_FILTER_UP
  698. | PNG_ASM_FLAG_MMX_READ_FILTER_AVG
  699. | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ;
  700. png_ptr->mmx_rowbytes_threshold = PNG_MMX_ROWBYTES_THRESHOLD_DEFAULT;
  701. png_ptr->mmx_bitdepth_threshold = PNG_MMX_BITDEPTH_THRESHOLD_DEFAULT;
  702. # endif
  703. } else {
  704. png_ptr->asm_flags &= ~( PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
  705. | PNG_MMX_READ_FLAGS
  706. | PNG_MMX_WRITE_FLAGS );
  707. }
  708. # else /* !((PNGVCRD || PNGGCCRD) && PNG_ASSEMBLER_CODE_SUPPORTED)) */
  709. /* clear all MMX flags; no support is compiled in */
  710. png_ptr->asm_flags &= ~( PNG_MMX_FLAGS );
  711. # endif /* ?(PNGVCRD || PNGGCCRD) */
  712. }
  713. #endif /* !(PNG_ASSEMBLER_CODE_SUPPORTED) */
  714. /* this function was added to libpng 1.2.0 */
  715. #if !defined(PNG_USE_PNGGCCRD) && \
  716. !(defined(PNG_ASSEMBLER_CODE_SUPPORTED) && defined(PNG_USE_PNGVCRD))
  717. int PNGAPI
  718. png_mmx_support(void)
  719. {
  720. return -1;
  721. }
  722. #endif
  723. #endif /* PNG_1_0_X */
  724. #ifdef PNG_SIZE_T
  725. /* Added at libpng version 1.2.6 */
  726. PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
  727. png_size_t PNGAPI
  728. png_convert_size(size_t size)
  729. {
  730. if (size > (png_size_t)-1)
  731. PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
  732. return ((png_size_t)size);
  733. }
  734. #endif /* PNG_SIZE_T */