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

http://ftk.googlecode.com/ · C · 641 lines · 486 code · 77 blank · 78 comment · 162 complexity · 1f06048c321cbd9fb6fe40ff5fceb519 MD5 · raw file

  1. /* pngmem.c - stub functions for memory allocation
  2. *
  3. * Last changed in libpng 1.2.41 [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. * This file provides a location for all memory allocation. Users who
  13. * need special memory handling are expected to supply replacement
  14. * functions for png_malloc() and png_free(), and to use
  15. * png_create_read_struct_2() and png_create_write_struct_2() to
  16. * identify the replacement functions.
  17. */
  18. #define PNG_INTERNAL
  19. #define PNG_NO_PEDANTIC_WARNINGS
  20. #include "png.h"
  21. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  22. /* Borland DOS special memory handler */
  23. #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
  24. /* If you change this, be sure to change the one in png.h also */
  25. /* Allocate memory for a png_struct. The malloc and memset can be replaced
  26. by a single call to calloc() if this is thought to improve performance. */
  27. png_voidp /* PRIVATE */
  28. png_create_struct(int type)
  29. {
  30. #ifdef PNG_USER_MEM_SUPPORTED
  31. return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
  32. }
  33. /* Alternate version of png_create_struct, for use with user-defined malloc. */
  34. png_voidp /* PRIVATE */
  35. png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
  36. {
  37. #endif /* PNG_USER_MEM_SUPPORTED */
  38. png_size_t size;
  39. png_voidp struct_ptr;
  40. if (type == PNG_STRUCT_INFO)
  41. size = png_sizeof(png_info);
  42. else if (type == PNG_STRUCT_PNG)
  43. size = png_sizeof(png_struct);
  44. else
  45. return (png_get_copyright(NULL));
  46. #ifdef PNG_USER_MEM_SUPPORTED
  47. if (malloc_fn != NULL)
  48. {
  49. png_struct dummy_struct;
  50. png_structp png_ptr = &dummy_struct;
  51. png_ptr->mem_ptr=mem_ptr;
  52. struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
  53. }
  54. else
  55. #endif /* PNG_USER_MEM_SUPPORTED */
  56. struct_ptr = (png_voidp)farmalloc(size);
  57. if (struct_ptr != NULL)
  58. png_memset(struct_ptr, 0, size);
  59. return (struct_ptr);
  60. }
  61. /* Free memory allocated by a png_create_struct() call */
  62. void /* PRIVATE */
  63. png_destroy_struct(png_voidp struct_ptr)
  64. {
  65. #ifdef PNG_USER_MEM_SUPPORTED
  66. png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
  67. }
  68. /* Free memory allocated by a png_create_struct() call */
  69. void /* PRIVATE */
  70. png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
  71. png_voidp mem_ptr)
  72. {
  73. #endif
  74. if (struct_ptr != NULL)
  75. {
  76. #ifdef PNG_USER_MEM_SUPPORTED
  77. if (free_fn != NULL)
  78. {
  79. png_struct dummy_struct;
  80. png_structp png_ptr = &dummy_struct;
  81. png_ptr->mem_ptr=mem_ptr;
  82. (*(free_fn))(png_ptr, struct_ptr);
  83. return;
  84. }
  85. #endif /* PNG_USER_MEM_SUPPORTED */
  86. farfree (struct_ptr);
  87. }
  88. }
  89. /* Allocate memory. For reasonable files, size should never exceed
  90. * 64K. However, zlib may allocate more then 64K if you don't tell
  91. * it not to. See zconf.h and png.h for more information. zlib does
  92. * need to allocate exactly 64K, so whatever you call here must
  93. * have the ability to do that.
  94. *
  95. * Borland seems to have a problem in DOS mode for exactly 64K.
  96. * It gives you a segment with an offset of 8 (perhaps to store its
  97. * memory stuff). zlib doesn't like this at all, so we have to
  98. * detect and deal with it. This code should not be needed in
  99. * Windows or OS/2 modes, and only in 16 bit mode. This code has
  100. * been updated by Alexander Lehmann for version 0.89 to waste less
  101. * memory.
  102. *
  103. * Note that we can't use png_size_t for the "size" declaration,
  104. * since on some systems a png_size_t is a 16-bit quantity, and as a
  105. * result, we would be truncating potentially larger memory requests
  106. * (which should cause a fatal error) and introducing major problems.
  107. */
  108. png_voidp /* PRIVATE */
  109. png_calloc(png_structp png_ptr, png_uint_32 size)
  110. {
  111. png_voidp ret;
  112. ret = (png_malloc(png_ptr, size));
  113. if (ret != NULL)
  114. png_memset(ret,0,(png_size_t)size);
  115. return (ret);
  116. }
  117. png_voidp PNGAPI
  118. png_malloc(png_structp png_ptr, png_uint_32 size)
  119. {
  120. png_voidp ret;
  121. if (png_ptr == NULL || size == 0)
  122. return (NULL);
  123. #ifdef PNG_USER_MEM_SUPPORTED
  124. if (png_ptr->malloc_fn != NULL)
  125. ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
  126. else
  127. ret = (png_malloc_default(png_ptr, size));
  128. if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  129. png_error(png_ptr, "Out of memory!");
  130. return (ret);
  131. }
  132. png_voidp PNGAPI
  133. png_malloc_default(png_structp png_ptr, png_uint_32 size)
  134. {
  135. png_voidp ret;
  136. #endif /* PNG_USER_MEM_SUPPORTED */
  137. if (png_ptr == NULL || size == 0)
  138. return (NULL);
  139. #ifdef PNG_MAX_MALLOC_64K
  140. if (size > (png_uint_32)65536L)
  141. {
  142. png_warning(png_ptr, "Cannot Allocate > 64K");
  143. ret = NULL;
  144. }
  145. else
  146. #endif
  147. if (size != (size_t)size)
  148. ret = NULL;
  149. else if (size == (png_uint_32)65536L)
  150. {
  151. if (png_ptr->offset_table == NULL)
  152. {
  153. /* Try to see if we need to do any of this fancy stuff */
  154. ret = farmalloc(size);
  155. if (ret == NULL || ((png_size_t)ret & 0xffff))
  156. {
  157. int num_blocks;
  158. png_uint_32 total_size;
  159. png_bytep table;
  160. int i;
  161. png_byte huge * hptr;
  162. if (ret != NULL)
  163. {
  164. farfree(ret);
  165. ret = NULL;
  166. }
  167. if (png_ptr->zlib_window_bits > 14)
  168. num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
  169. else
  170. num_blocks = 1;
  171. if (png_ptr->zlib_mem_level >= 7)
  172. num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
  173. else
  174. num_blocks++;
  175. total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
  176. table = farmalloc(total_size);
  177. if (table == NULL)
  178. {
  179. #ifndef PNG_USER_MEM_SUPPORTED
  180. if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  181. png_error(png_ptr, "Out Of Memory."); /* Note "O", "M" */
  182. else
  183. png_warning(png_ptr, "Out Of Memory.");
  184. #endif
  185. return (NULL);
  186. }
  187. if ((png_size_t)table & 0xfff0)
  188. {
  189. #ifndef PNG_USER_MEM_SUPPORTED
  190. if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  191. png_error(png_ptr,
  192. "Farmalloc didn't return normalized pointer");
  193. else
  194. png_warning(png_ptr,
  195. "Farmalloc didn't return normalized pointer");
  196. #endif
  197. return (NULL);
  198. }
  199. png_ptr->offset_table = table;
  200. png_ptr->offset_table_ptr = farmalloc(num_blocks *
  201. png_sizeof(png_bytep));
  202. if (png_ptr->offset_table_ptr == NULL)
  203. {
  204. #ifndef PNG_USER_MEM_SUPPORTED
  205. if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  206. png_error(png_ptr, "Out Of memory."); /* Note "O", "m" */
  207. else
  208. png_warning(png_ptr, "Out Of memory.");
  209. #endif
  210. return (NULL);
  211. }
  212. hptr = (png_byte huge *)table;
  213. if ((png_size_t)hptr & 0xf)
  214. {
  215. hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
  216. hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
  217. }
  218. for (i = 0; i < num_blocks; i++)
  219. {
  220. png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
  221. hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
  222. }
  223. png_ptr->offset_table_number = num_blocks;
  224. png_ptr->offset_table_count = 0;
  225. png_ptr->offset_table_count_free = 0;
  226. }
  227. }
  228. if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
  229. {
  230. #ifndef PNG_USER_MEM_SUPPORTED
  231. if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  232. png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
  233. else
  234. png_warning(png_ptr, "Out of Memory.");
  235. #endif
  236. return (NULL);
  237. }
  238. ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
  239. }
  240. else
  241. ret = farmalloc(size);
  242. #ifndef PNG_USER_MEM_SUPPORTED
  243. if (ret == NULL)
  244. {
  245. if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  246. png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
  247. else
  248. png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
  249. }
  250. #endif
  251. return (ret);
  252. }
  253. /* Free a pointer allocated by png_malloc(). In the default
  254. * configuration, png_ptr is not used, but is passed in case it
  255. * is needed. If ptr is NULL, return without taking any action.
  256. */
  257. void PNGAPI
  258. png_free(png_structp png_ptr, png_voidp ptr)
  259. {
  260. if (png_ptr == NULL || ptr == NULL)
  261. return;
  262. #ifdef PNG_USER_MEM_SUPPORTED
  263. if (png_ptr->free_fn != NULL)
  264. {
  265. (*(png_ptr->free_fn))(png_ptr, ptr);
  266. return;
  267. }
  268. else
  269. png_free_default(png_ptr, ptr);
  270. }
  271. void PNGAPI
  272. png_free_default(png_structp png_ptr, png_voidp ptr)
  273. {
  274. #endif /* PNG_USER_MEM_SUPPORTED */
  275. if (png_ptr == NULL || ptr == NULL)
  276. return;
  277. if (png_ptr->offset_table != NULL)
  278. {
  279. int i;
  280. for (i = 0; i < png_ptr->offset_table_count; i++)
  281. {
  282. if (ptr == png_ptr->offset_table_ptr[i])
  283. {
  284. ptr = NULL;
  285. png_ptr->offset_table_count_free++;
  286. break;
  287. }
  288. }
  289. if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
  290. {
  291. farfree(png_ptr->offset_table);
  292. farfree(png_ptr->offset_table_ptr);
  293. png_ptr->offset_table = NULL;
  294. png_ptr->offset_table_ptr = NULL;
  295. }
  296. }
  297. if (ptr != NULL)
  298. {
  299. farfree(ptr);
  300. }
  301. }
  302. #else /* Not the Borland DOS special memory handler */
  303. /* Allocate memory for a png_struct or a png_info. The malloc and
  304. memset can be replaced by a single call to calloc() if this is thought
  305. to improve performance noticably. */
  306. png_voidp /* PRIVATE */
  307. png_create_struct(int type)
  308. {
  309. #ifdef PNG_USER_MEM_SUPPORTED
  310. return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
  311. }
  312. /* Allocate memory for a png_struct or a png_info. The malloc and
  313. memset can be replaced by a single call to calloc() if this is thought
  314. to improve performance noticably. */
  315. png_voidp /* PRIVATE */
  316. png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
  317. {
  318. #endif /* PNG_USER_MEM_SUPPORTED */
  319. png_size_t size;
  320. png_voidp struct_ptr;
  321. if (type == PNG_STRUCT_INFO)
  322. size = png_sizeof(png_info);
  323. else if (type == PNG_STRUCT_PNG)
  324. size = png_sizeof(png_struct);
  325. else
  326. return (NULL);
  327. #ifdef PNG_USER_MEM_SUPPORTED
  328. if (malloc_fn != NULL)
  329. {
  330. png_struct dummy_struct;
  331. png_structp png_ptr = &dummy_struct;
  332. png_ptr->mem_ptr=mem_ptr;
  333. struct_ptr = (*(malloc_fn))(png_ptr, size);
  334. if (struct_ptr != NULL)
  335. png_memset(struct_ptr, 0, size);
  336. return (struct_ptr);
  337. }
  338. #endif /* PNG_USER_MEM_SUPPORTED */
  339. #if defined(__TURBOC__) && !defined(__FLAT__)
  340. struct_ptr = (png_voidp)farmalloc(size);
  341. #else
  342. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  343. struct_ptr = (png_voidp)halloc(size, 1);
  344. # else
  345. struct_ptr = (png_voidp)malloc(size);
  346. # endif
  347. #endif
  348. if (struct_ptr != NULL)
  349. png_memset(struct_ptr, 0, size);
  350. return (struct_ptr);
  351. }
  352. /* Free memory allocated by a png_create_struct() call */
  353. void /* PRIVATE */
  354. png_destroy_struct(png_voidp struct_ptr)
  355. {
  356. #ifdef PNG_USER_MEM_SUPPORTED
  357. png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
  358. }
  359. /* Free memory allocated by a png_create_struct() call */
  360. void /* PRIVATE */
  361. png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
  362. png_voidp mem_ptr)
  363. {
  364. #endif /* PNG_USER_MEM_SUPPORTED */
  365. if (struct_ptr != NULL)
  366. {
  367. #ifdef PNG_USER_MEM_SUPPORTED
  368. if (free_fn != NULL)
  369. {
  370. png_struct dummy_struct;
  371. png_structp png_ptr = &dummy_struct;
  372. png_ptr->mem_ptr=mem_ptr;
  373. (*(free_fn))(png_ptr, struct_ptr);
  374. return;
  375. }
  376. #endif /* PNG_USER_MEM_SUPPORTED */
  377. #if defined(__TURBOC__) && !defined(__FLAT__)
  378. farfree(struct_ptr);
  379. #else
  380. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  381. hfree(struct_ptr);
  382. # else
  383. free(struct_ptr);
  384. # endif
  385. #endif
  386. }
  387. }
  388. /* Allocate memory. For reasonable files, size should never exceed
  389. * 64K. However, zlib may allocate more then 64K if you don't tell
  390. * it not to. See zconf.h and png.h for more information. zlib does
  391. * need to allocate exactly 64K, so whatever you call here must
  392. * have the ability to do that.
  393. */
  394. png_voidp PNGAPI
  395. png_calloc(png_structp png_ptr, png_uint_32 size)
  396. {
  397. png_voidp ret;
  398. ret = (png_malloc(png_ptr, size));
  399. if (ret != NULL)
  400. png_memset(ret,0,(png_size_t)size);
  401. return (ret);
  402. }
  403. png_voidp PNGAPI
  404. png_malloc(png_structp png_ptr, png_uint_32 size)
  405. {
  406. png_voidp ret;
  407. #ifdef PNG_USER_MEM_SUPPORTED
  408. if (png_ptr == NULL || size == 0)
  409. return (NULL);
  410. if (png_ptr->malloc_fn != NULL)
  411. ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
  412. else
  413. ret = (png_malloc_default(png_ptr, size));
  414. if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  415. png_error(png_ptr, "Out of Memory!");
  416. return (ret);
  417. }
  418. png_voidp PNGAPI
  419. png_malloc_default(png_structp png_ptr, png_uint_32 size)
  420. {
  421. png_voidp ret;
  422. #endif /* PNG_USER_MEM_SUPPORTED */
  423. if (png_ptr == NULL || size == 0)
  424. return (NULL);
  425. #ifdef PNG_MAX_MALLOC_64K
  426. if (size > (png_uint_32)65536L)
  427. {
  428. #ifndef PNG_USER_MEM_SUPPORTED
  429. if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  430. png_error(png_ptr, "Cannot Allocate > 64K");
  431. else
  432. #endif
  433. return NULL;
  434. }
  435. #endif
  436. /* Check for overflow */
  437. #if defined(__TURBOC__) && !defined(__FLAT__)
  438. if (size != (unsigned long)size)
  439. ret = NULL;
  440. else
  441. ret = farmalloc(size);
  442. #else
  443. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  444. if (size != (unsigned long)size)
  445. ret = NULL;
  446. else
  447. ret = halloc(size, 1);
  448. # else
  449. if (size != (size_t)size)
  450. ret = NULL;
  451. else
  452. ret = malloc((size_t)size);
  453. # endif
  454. #endif
  455. #ifndef PNG_USER_MEM_SUPPORTED
  456. if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  457. png_error(png_ptr, "Out of Memory");
  458. #endif
  459. return (ret);
  460. }
  461. /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
  462. * without taking any action.
  463. */
  464. void PNGAPI
  465. png_free(png_structp png_ptr, png_voidp ptr)
  466. {
  467. if (png_ptr == NULL || ptr == NULL)
  468. return;
  469. #ifdef PNG_USER_MEM_SUPPORTED
  470. if (png_ptr->free_fn != NULL)
  471. {
  472. (*(png_ptr->free_fn))(png_ptr, ptr);
  473. return;
  474. }
  475. else
  476. png_free_default(png_ptr, ptr);
  477. }
  478. void PNGAPI
  479. png_free_default(png_structp png_ptr, png_voidp ptr)
  480. {
  481. if (png_ptr == NULL || ptr == NULL)
  482. return;
  483. #endif /* PNG_USER_MEM_SUPPORTED */
  484. #if defined(__TURBOC__) && !defined(__FLAT__)
  485. farfree(ptr);
  486. #else
  487. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  488. hfree(ptr);
  489. # else
  490. free(ptr);
  491. # endif
  492. #endif
  493. }
  494. #endif /* Not Borland DOS special memory handler */
  495. #ifdef PNG_1_0_X
  496. # define png_malloc_warn png_malloc
  497. #else
  498. /* This function was added at libpng version 1.2.3. The png_malloc_warn()
  499. * function will set up png_malloc() to issue a png_warning and return NULL
  500. * instead of issuing a png_error, if it fails to allocate the requested
  501. * memory.
  502. */
  503. png_voidp PNGAPI
  504. png_malloc_warn(png_structp png_ptr, png_uint_32 size)
  505. {
  506. png_voidp ptr;
  507. png_uint_32 save_flags;
  508. if (png_ptr == NULL)
  509. return (NULL);
  510. save_flags = png_ptr->flags;
  511. png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  512. ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
  513. png_ptr->flags=save_flags;
  514. return(ptr);
  515. }
  516. #endif
  517. png_voidp PNGAPI
  518. png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
  519. png_uint_32 length)
  520. {
  521. png_size_t size;
  522. size = (png_size_t)length;
  523. if ((png_uint_32)size != length)
  524. png_error(png_ptr, "Overflow in png_memcpy_check.");
  525. return(png_memcpy (s1, s2, size));
  526. }
  527. png_voidp PNGAPI
  528. png_memset_check (png_structp png_ptr, png_voidp s1, int value,
  529. png_uint_32 length)
  530. {
  531. png_size_t size;
  532. size = (png_size_t)length;
  533. if ((png_uint_32)size != length)
  534. png_error(png_ptr, "Overflow in png_memset_check.");
  535. return (png_memset (s1, value, size));
  536. }
  537. #ifdef PNG_USER_MEM_SUPPORTED
  538. /* This function is called when the application wants to use another method
  539. * of allocating and freeing memory.
  540. */
  541. void PNGAPI
  542. png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  543. malloc_fn, png_free_ptr free_fn)
  544. {
  545. if (png_ptr != NULL)
  546. {
  547. png_ptr->mem_ptr = mem_ptr;
  548. png_ptr->malloc_fn = malloc_fn;
  549. png_ptr->free_fn = free_fn;
  550. }
  551. }
  552. /* This function returns a pointer to the mem_ptr associated with the user
  553. * functions. The application should free any memory associated with this
  554. * pointer before png_write_destroy and png_read_destroy are called.
  555. */
  556. png_voidp PNGAPI
  557. png_get_mem_ptr(png_structp png_ptr)
  558. {
  559. if (png_ptr == NULL)
  560. return (NULL);
  561. return ((png_voidp)png_ptr->mem_ptr);
  562. }
  563. #endif /* PNG_USER_MEM_SUPPORTED */
  564. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */