Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

658 lines
17 KiB

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