pngmem.c 16.2 KB
Newer Older
1

G
Guy Schalnat 已提交
2
/* pngmem.c - stub functions for memory allocation
3
 *
4
 * libpng version 1.2.8beta4 - November 13, 2004
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 * Copyright (c) 1998-2004 Glenn Randers-Pehrson
7 8
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9
 *
10
 * This file provides a location for all memory allocation.  Users who
11 12 13 14
 * need special memory handling are expected to supply replacement
 * functions for png_malloc() and png_free(), and to use
 * png_create_read_struct_2() and png_create_write_struct_2() to
 * identify the replacement functions.
15
 */
G
Guy Schalnat 已提交
16 17 18 19

#define PNG_INTERNAL
#include "png.h"

G
Guy Schalnat 已提交
20 21 22 23
/* Borland DOS special memory handler */
#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
/* if you change this, be sure to change the one in png.h also */

G
Guy Schalnat 已提交
24
/* Allocate memory for a png_struct.  The malloc and memset can be replaced
A
Andreas Dilger 已提交
25
   by a single call to calloc() if this is thought to improve performance. */
26
png_voidp /* PRIVATE */
A
Andreas Dilger 已提交
27
png_create_struct(int type)
G
Guy Schalnat 已提交
28
{
29
#ifdef PNG_USER_MEM_SUPPORTED
30
   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
31 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 */
A
Andreas Dilger 已提交
38
   png_size_t size;
G
Guy Schalnat 已提交
39 40 41
   png_voidp struct_ptr;

   if (type == PNG_STRUCT_INFO)
42
     size = png_sizeof(png_info);
G
Guy Schalnat 已提交
43
   else if (type == PNG_STRUCT_PNG)
44
     size = png_sizeof(png_struct);
G
Guy Schalnat 已提交
45
   else
46
     return (png_get_copyright(NULL));
G
Guy Schalnat 已提交
47

48 49 50
#ifdef PNG_USER_MEM_SUPPORTED
   if(malloc_fn != NULL)
   {
51 52 53
      png_struct dummy_struct;
      png_structp png_ptr = &dummy_struct;
      png_ptr->mem_ptr=mem_ptr;
54
      struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
55
   }
56
   else
57
#endif /* PNG_USER_MEM_SUPPORTED */
58
      struct_ptr = (png_voidp)farmalloc(size);
59
   if (struct_ptr != NULL)
G
Guy Schalnat 已提交
60 61 62 63 64
      png_memset(struct_ptr, 0, size);
   return (struct_ptr);
}

/* Free memory allocated by a png_create_struct() call */
65
void /* PRIVATE */
G
Guy Schalnat 已提交
66 67
png_destroy_struct(png_voidp struct_ptr)
{
68
#ifdef PNG_USER_MEM_SUPPORTED
69
   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
70 71 72
}

/* Free memory allocated by a png_create_struct() call */
73
void /* PRIVATE */
74 75
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
    png_voidp mem_ptr)
76 77
{
#endif
A
Andreas Dilger 已提交
78
   if (struct_ptr != NULL)
79
   {
80 81 82 83 84
#ifdef PNG_USER_MEM_SUPPORTED
      if(free_fn != NULL)
      {
         png_struct dummy_struct;
         png_structp png_ptr = &dummy_struct;
85
         png_ptr->mem_ptr=mem_ptr;
86 87 88 89
         (*(free_fn))(png_ptr, struct_ptr);
         return;
      }
#endif /* PNG_USER_MEM_SUPPORTED */
G
Guy Schalnat 已提交
90
      farfree (struct_ptr);
91
   }
G
Guy Schalnat 已提交
92 93
}

G
Guy Schalnat 已提交
94
/* Allocate memory.  For reasonable files, size should never exceed
A
Andreas Dilger 已提交
95 96 97 98 99 100
 * 64K.  However, zlib may allocate more then 64K if you don't tell
 * it not to.  See zconf.h and png.h for more information. zlib does
 * need to allocate exactly 64K, so whatever you call here must
 * have the ability to do that.
 *
 * Borland seems to have a problem in DOS mode for exactly 64K.
101
 * It gives you a segment with an offset of 8 (perhaps to store its
A
Andreas Dilger 已提交
102 103 104 105 106 107 108 109 110 111 112
 * memory stuff).  zlib doesn't like this at all, so we have to
 * detect and deal with it.  This code should not be needed in
 * Windows or OS/2 modes, and only in 16 bit mode.  This code has
 * been updated by Alexander Lehmann for version 0.89 to waste less
 * memory.
 *
 * Note that we can't use png_size_t for the "size" declaration,
 * since on some systems a png_size_t is a 16-bit quantity, and as a
 * result, we would be truncating potentially larger memory requests
 * (which should cause a fatal error) and introducing major problems.
 */
113

114
png_voidp PNGAPI
115
png_malloc(png_structp png_ptr, png_uint_32 size)
G
Guy Schalnat 已提交
116
{
G
Guy Schalnat 已提交
117
   png_voidp ret;
118

A
Andreas Dilger 已提交
119
   if (png_ptr == NULL || size == 0)
120
      return (NULL);
G
Guy Schalnat 已提交
121

122 123
#ifdef PNG_USER_MEM_SUPPORTED
   if(png_ptr->malloc_fn != NULL)
124
       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
125
   else
126 127 128 129
       ret = (png_malloc_default(png_ptr, size));
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
       png_error(png_ptr, "Out of memory!");
   return (ret);
130 131
}

132
png_voidp PNGAPI
133 134 135
png_malloc_default(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ret;
136
#endif /* PNG_USER_MEM_SUPPORTED */
137

G
Guy Schalnat 已提交
138
#ifdef PNG_MAX_MALLOC_64K
G
Guy Schalnat 已提交
139
   if (size > (png_uint_32)65536L)
140 141 142 143 144
   {
      png_warning(png_ptr, "Cannot Allocate > 64K");
      ret = NULL;
   }
   else
G
Guy Schalnat 已提交
145 146
#endif

147 148 149
   if (size != (size_t)size)
     ret = NULL;
   else if (size == (png_uint_32)65536L)
G
Guy Schalnat 已提交
150
   {
A
Andreas Dilger 已提交
151
      if (png_ptr->offset_table == NULL)
G
Guy Schalnat 已提交
152 153 154
      {
         /* try to see if we need to do any of this fancy stuff */
         ret = farmalloc(size);
A
Andreas Dilger 已提交
155
         if (ret == NULL || ((png_size_t)ret & 0xffff))
G
Guy Schalnat 已提交
156 157 158 159 160
         {
            int num_blocks;
            png_uint_32 total_size;
            png_bytep table;
            int i;
G
Guy Schalnat 已提交
161 162
            png_byte huge * hptr;

A
Andreas Dilger 已提交
163
            if (ret != NULL)
164
            {
G
Guy Schalnat 已提交
165
               farfree(ret);
166 167
               ret = NULL;
            }
G
Guy Schalnat 已提交
168

169 170 171
            if(png_ptr->zlib_window_bits > 14)
               num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
            else
G
Guy Schalnat 已提交
172 173 174 175 176 177
               num_blocks = 1;
            if (png_ptr->zlib_mem_level >= 7)
               num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
            else
               num_blocks++;

G
Guy Schalnat 已提交
178
            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
G
Guy Schalnat 已提交
179 180 181

            table = farmalloc(total_size);

A
Andreas Dilger 已提交
182
            if (table == NULL)
G
Guy Schalnat 已提交
183
            {
184
#ifndef PNG_USER_MEM_SUPPORTED
185
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
186 187 188
                  png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
               else
                  png_warning(png_ptr, "Out Of Memory.");
189
#endif
190
               return (NULL);
G
Guy Schalnat 已提交
191 192
            }

A
Andreas Dilger 已提交
193
            if ((png_size_t)table & 0xfff0)
G
Guy Schalnat 已提交
194
            {
195
#ifndef PNG_USER_MEM_SUPPORTED
196
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
197 198 199 200 201
                  png_error(png_ptr,
                    "Farmalloc didn't return normalized pointer");
               else
                  png_warning(png_ptr,
                    "Farmalloc didn't return normalized pointer");
202
#endif
203
               return (NULL);
G
Guy Schalnat 已提交
204 205
            }

G
Guy Schalnat 已提交
206
            png_ptr->offset_table = table;
A
Andreas Dilger 已提交
207
            png_ptr->offset_table_ptr = farmalloc(num_blocks *
208
               png_sizeof (png_bytep));
G
Guy Schalnat 已提交
209

A
Andreas Dilger 已提交
210
            if (png_ptr->offset_table_ptr == NULL)
G
Guy Schalnat 已提交
211
            {
212
#ifndef PNG_USER_MEM_SUPPORTED
213
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
214 215 216
                  png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
               else
                  png_warning(png_ptr, "Out Of memory.");
217
#endif
218
               return (NULL);
G
Guy Schalnat 已提交
219 220 221
            }

            hptr = (png_byte huge *)table;
A
Andreas Dilger 已提交
222
            if ((png_size_t)hptr & 0xf)
G
Guy Schalnat 已提交
223
            {
G
Guy Schalnat 已提交
224
               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
225
               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
G
Guy Schalnat 已提交
226 227 228 229
            }
            for (i = 0; i < num_blocks; i++)
            {
               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
230
               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
G
Guy Schalnat 已提交
231 232 233 234 235 236
            }

            png_ptr->offset_table_number = num_blocks;
            png_ptr->offset_table_count = 0;
            png_ptr->offset_table_count_free = 0;
         }
G
Guy Schalnat 已提交
237
      }
G
Guy Schalnat 已提交
238

G
Guy Schalnat 已提交
239
      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
240
      {
241
#ifndef PNG_USER_MEM_SUPPORTED
242
         if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
243 244 245
            png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
         else
            png_warning(png_ptr, "Out of Memory.");
246
#endif
247 248
         return (NULL);
      }
G
Guy Schalnat 已提交
249

G
Guy Schalnat 已提交
250
      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
G
Guy Schalnat 已提交
251 252 253
   }
   else
      ret = farmalloc(size);
G
Guy Schalnat 已提交
254

255
#ifndef PNG_USER_MEM_SUPPORTED
G
Guy Schalnat 已提交
256 257
   if (ret == NULL)
   {
258
      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
259 260 261
         png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
      else
         png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
G
Guy Schalnat 已提交
262
   }
263
#endif
G
Guy Schalnat 已提交
264

265
   return (ret);
G
Guy Schalnat 已提交
266 267
}

268
/* free a pointer allocated by png_malloc().  In the default
A
Andreas Dilger 已提交
269 270
   configuration, png_ptr is not used, but is passed in case it
   is needed.  If ptr is NULL, return without taking any action. */
271
void PNGAPI
272
png_free(png_structp png_ptr, png_voidp ptr)
G
Guy Schalnat 已提交
273
{
A
Andreas Dilger 已提交
274
   if (png_ptr == NULL || ptr == NULL)
G
Guy Schalnat 已提交
275 276
      return;

277 278 279 280 281 282 283 284 285
#ifdef PNG_USER_MEM_SUPPORTED
   if (png_ptr->free_fn != NULL)
   {
      (*(png_ptr->free_fn))(png_ptr, ptr);
      return;
   }
   else png_free_default(png_ptr, ptr);
}

286
void PNGAPI
287 288 289
png_free_default(png_structp png_ptr, png_voidp ptr)
{
#endif /* PNG_USER_MEM_SUPPORTED */
290

A
Andreas Dilger 已提交
291
   if (png_ptr->offset_table != NULL)
G
Guy Schalnat 已提交
292
   {
A
Andreas Dilger 已提交
293
      int i;
G
Guy Schalnat 已提交
294

A
Andreas Dilger 已提交
295 296 297
      for (i = 0; i < png_ptr->offset_table_count; i++)
      {
         if (ptr == png_ptr->offset_table_ptr[i])
G
Guy Schalnat 已提交
298
         {
A
Andreas Dilger 已提交
299 300 301
            ptr = NULL;
            png_ptr->offset_table_count_free++;
            break;
G
Guy Schalnat 已提交
302
         }
G
Guy Schalnat 已提交
303
      }
A
Andreas Dilger 已提交
304 305 306 307 308 309 310
      if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
      {
         farfree(png_ptr->offset_table);
         farfree(png_ptr->offset_table_ptr);
         png_ptr->offset_table = NULL;
         png_ptr->offset_table_ptr = NULL;
      }
G
Guy Schalnat 已提交
311
   }
A
Andreas Dilger 已提交
312 313

   if (ptr != NULL)
314
   {
A
Andreas Dilger 已提交
315
      farfree(ptr);
316
   }
G
Guy Schalnat 已提交
317 318 319 320
}

#else /* Not the Borland DOS special memory handler */

G
Guy Schalnat 已提交
321
/* Allocate memory for a png_struct or a png_info.  The malloc and
A
Andreas Dilger 已提交
322
   memset can be replaced by a single call to calloc() if this is thought
323
   to improve performance noticably. */
324
png_voidp /* PRIVATE */
A
Andreas Dilger 已提交
325
png_create_struct(int type)
G
Guy Schalnat 已提交
326
{
327
#ifdef PNG_USER_MEM_SUPPORTED
328
   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
329 330 331 332
}

/* Allocate memory for a png_struct or a png_info.  The malloc and
   memset can be replaced by a single call to calloc() if this is thought
333
   to improve performance noticably. */
334
png_voidp /* PRIVATE */
335
png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
336 337
{
#endif /* PNG_USER_MEM_SUPPORTED */
A
Andreas Dilger 已提交
338
   png_size_t size;
G
Guy Schalnat 已提交
339 340 341
   png_voidp struct_ptr;

   if (type == PNG_STRUCT_INFO)
342
      size = png_sizeof(png_info);
G
Guy Schalnat 已提交
343
   else if (type == PNG_STRUCT_PNG)
344
      size = png_sizeof(png_struct);
G
Guy Schalnat 已提交
345
   else
346
      return (NULL);
G
Guy Schalnat 已提交
347

348 349 350
#ifdef PNG_USER_MEM_SUPPORTED
   if(malloc_fn != NULL)
   {
351 352 353
      png_struct dummy_struct;
      png_structp png_ptr = &dummy_struct;
      png_ptr->mem_ptr=mem_ptr;
354
      struct_ptr = (*(malloc_fn))(png_ptr, size);
355
      if (struct_ptr != NULL)
356 357 358 359 360
         png_memset(struct_ptr, 0, size);
      return (struct_ptr);
   }
#endif /* PNG_USER_MEM_SUPPORTED */

G
Guy Schalnat 已提交
361
#if defined(__TURBOC__) && !defined(__FLAT__)
362
   struct_ptr = (png_voidp)farmalloc(size);
G
Guy Schalnat 已提交
363 364
#else
# if defined(_MSC_VER) && defined(MAXSEG_64K)
365
   struct_ptr = (png_voidp)halloc(size,1);
G
Guy Schalnat 已提交
366
# else
367
   struct_ptr = (png_voidp)malloc(size);
G
Guy Schalnat 已提交
368 369
# endif
#endif
370
   if (struct_ptr != NULL)
G
Guy Schalnat 已提交
371 372 373 374 375 376 377
      png_memset(struct_ptr, 0, size);

   return (struct_ptr);
}


/* Free memory allocated by a png_create_struct() call */
378
void /* PRIVATE */
G
Guy Schalnat 已提交
379 380
png_destroy_struct(png_voidp struct_ptr)
{
381
#ifdef PNG_USER_MEM_SUPPORTED
382
   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
383 384 385
}

/* Free memory allocated by a png_create_struct() call */
386
void /* PRIVATE */
387 388
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
    png_voidp mem_ptr)
389 390
{
#endif /* PNG_USER_MEM_SUPPORTED */
A
Andreas Dilger 已提交
391
   if (struct_ptr != NULL)
392
   {
393 394 395 396 397
#ifdef PNG_USER_MEM_SUPPORTED
      if(free_fn != NULL)
      {
         png_struct dummy_struct;
         png_structp png_ptr = &dummy_struct;
398
         png_ptr->mem_ptr=mem_ptr;
399 400 401 402
         (*(free_fn))(png_ptr, struct_ptr);
         return;
      }
#endif /* PNG_USER_MEM_SUPPORTED */
G
Guy Schalnat 已提交
403 404 405 406 407 408 409 410 411
#if defined(__TURBOC__) && !defined(__FLAT__)
      farfree(struct_ptr);
#else
# if defined(_MSC_VER) && defined(MAXSEG_64K)
      hfree(struct_ptr);
# else
      free(struct_ptr);
# endif
#endif
412
   }
G
Guy Schalnat 已提交
413 414
}

G
Guy Schalnat 已提交
415
/* Allocate memory.  For reasonable files, size should never exceed
G
Guy Schalnat 已提交
416
   64K.  However, zlib may allocate more then 64K if you don't tell
A
Andreas Dilger 已提交
417
   it not to.  See zconf.h and png.h for more information.  zlib does
G
Guy Schalnat 已提交
418 419
   need to allocate exactly 64K, so whatever you call here must
   have the ability to do that. */
G
Guy Schalnat 已提交
420

421
png_voidp PNGAPI
422
png_malloc(png_structp png_ptr, png_uint_32 size)
G
Guy Schalnat 已提交
423
{
G
Guy Schalnat 已提交
424
   png_voidp ret;
425

426
#ifdef PNG_USER_MEM_SUPPORTED
A
Andreas Dilger 已提交
427
   if (png_ptr == NULL || size == 0)
428
      return (NULL);
G
Guy Schalnat 已提交
429

430
   if(png_ptr->malloc_fn != NULL)
431
       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
432
   else
433 434 435 436
       ret = (png_malloc_default(png_ptr, size));
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
       png_error(png_ptr, "Out of Memory!");
   return (ret);
437
}
438

439
png_voidp PNGAPI
440 441 442 443 444
png_malloc_default(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ret;
#endif /* PNG_USER_MEM_SUPPORTED */

445 446 447
   if (png_ptr == NULL || size == 0)
      return (NULL);

G
Guy Schalnat 已提交
448
#ifdef PNG_MAX_MALLOC_64K
449
   if (size > (png_uint_32)65536L)
450
   {
451
#ifndef PNG_USER_MEM_SUPPORTED
452 453 454
      if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
         png_error(png_ptr, "Cannot Allocate > 64K");
      else
455
#endif
456 457
         return NULL;
   }
G
Guy Schalnat 已提交
458 459
#endif

460
 /* Check for overflow */
G
Guy Schalnat 已提交
461
#if defined(__TURBOC__) && !defined(__FLAT__)
462 463 464
 if (size != (unsigned long)size)
   ret = NULL;
 else
465
   ret = farmalloc(size);
G
Guy Schalnat 已提交
466
#else
G
Guy Schalnat 已提交
467
# if defined(_MSC_VER) && defined(MAXSEG_64K)
468 469 470
 if (size != (unsigned long)size)
   ret = NULL;
 else
471
   ret = halloc(size, 1);
G
Guy Schalnat 已提交
472
# else
473 474 475
 if (size != (size_t)size)
   ret = NULL;
 else
476
   ret = malloc((size_t)size);
G
Guy Schalnat 已提交
477
# endif
G
Guy Schalnat 已提交
478 479
#endif

480
#ifndef PNG_USER_MEM_SUPPORTED
481
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
G
Guy Schalnat 已提交
482
      png_error(png_ptr, "Out of Memory");
483
#endif
G
Guy Schalnat 已提交
484

485
   return (ret);
G
Guy Schalnat 已提交
486 487
}

488 489
/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
   without taking any action. */
490
void PNGAPI
491
png_free(png_structp png_ptr, png_voidp ptr)
G
Guy Schalnat 已提交
492
{
A
Andreas Dilger 已提交
493
   if (png_ptr == NULL || ptr == NULL)
G
Guy Schalnat 已提交
494
      return;
G
Guy Schalnat 已提交
495

496 497 498 499 500 501 502 503
#ifdef PNG_USER_MEM_SUPPORTED
   if (png_ptr->free_fn != NULL)
   {
      (*(png_ptr->free_fn))(png_ptr, ptr);
      return;
   }
   else png_free_default(png_ptr, ptr);
}
504
void PNGAPI
505 506
png_free_default(png_structp png_ptr, png_voidp ptr)
{
507 508 509
   if (png_ptr == NULL || ptr == NULL)
      return;

510 511
#endif /* PNG_USER_MEM_SUPPORTED */

G
Guy Schalnat 已提交
512
#if defined(__TURBOC__) && !defined(__FLAT__)
A
Andreas Dilger 已提交
513
   farfree(ptr);
G
Guy Schalnat 已提交
514
#else
G
Guy Schalnat 已提交
515
# if defined(_MSC_VER) && defined(MAXSEG_64K)
A
Andreas Dilger 已提交
516
   hfree(ptr);
G
Guy Schalnat 已提交
517
# else
A
Andreas Dilger 已提交
518
   free(ptr);
G
Guy Schalnat 已提交
519
# endif
G
Guy Schalnat 已提交
520 521 522
#endif
}

G
Guy Schalnat 已提交
523
#endif /* Not Borland DOS special memory handler */
G
Guy Schalnat 已提交
524

525 526 527 528
#if defined(PNG_1_0_X)
#  define png_malloc_warn png_malloc
#else
/* This function was added at libpng version 1.2.3.  The png_malloc_warn()
529 530 531
 * function will set up png_malloc() to issue a png_warning and return NULL
 * instead of issuing a png_error, if it fails to allocate the requested
 * memory.
532 533 534 535 536 537 538 539 540 541 542 543
 */
png_voidp PNGAPI
png_malloc_warn(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ptr;
   png_uint_32 save_flags=png_ptr->flags;

   png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
   ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
   png_ptr->flags=save_flags;
   return(ptr);
}
544
#endif
545

546
png_voidp PNGAPI
547
png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
548 549 550
   png_uint_32 length)
{
   png_size_t size;
551 552 553 554

   size = (png_size_t)length;
   if ((png_uint_32)size != length)
      png_error(png_ptr,"Overflow in png_memcpy_check.");
555

556
   return(png_memcpy (s1, s2, size));
557 558
}

559
png_voidp PNGAPI
560
png_memset_check (png_structp png_ptr, png_voidp s1, int value,
561 562 563
   png_uint_32 length)
{
   png_size_t size;
564 565 566 567 568 569 570

   size = (png_size_t)length;
   if ((png_uint_32)size != length)
      png_error(png_ptr,"Overflow in png_memset_check.");

   return (png_memset (s1, value, size));

571
}
572 573 574 575 576

#ifdef PNG_USER_MEM_SUPPORTED
/* This function is called when the application wants to use another method
 * of allocating and freeing memory.
 */
577
void PNGAPI
578 579 580 581 582 583 584 585 586 587 588 589
png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  malloc_fn, png_free_ptr free_fn)
{
   png_ptr->mem_ptr = mem_ptr;
   png_ptr->malloc_fn = malloc_fn;
   png_ptr->free_fn = free_fn;
}

/* This function returns a pointer to the mem_ptr associated with the user
 * functions.  The application should free any memory associated with this
 * pointer before png_write_destroy and png_read_destroy are called.
 */
590
png_voidp PNGAPI
591 592 593 594 595
png_get_mem_ptr(png_structp png_ptr)
{
   return ((png_voidp)png_ptr->mem_ptr);
}
#endif /* PNG_USER_MEM_SUPPORTED */