pngmem.c 15.7 KB
Newer Older
1

G
Guy Schalnat 已提交
2
/* pngmem.c - stub functions for memory allocation
3
 *
4
 * libpng version 1.2.6beta3 - July 18, 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 42 43 44 45
   png_voidp struct_ptr;

   if (type == PNG_STRUCT_INFO)
     size = sizeof(png_info);
   else if (type == PNG_STRUCT_PNG)
     size = sizeof(png_struct);
   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
   {
125
       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
126
       if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
127 128 129
          png_error(png_ptr, "Out of memory!");
       return (ret);
   }
130 131 132 133
   else
       return png_malloc_default(png_ptr, size);
}

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

G
Guy Schalnat 已提交
140
#ifdef PNG_MAX_MALLOC_64K
G
Guy Schalnat 已提交
141 142
   if (size > (png_uint_32)65536L)
      png_error(png_ptr, "Cannot Allocate > 64K");
G
Guy Schalnat 已提交
143 144
#endif

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

A
Andreas Dilger 已提交
159
            if (ret != NULL)
160
            {
G
Guy Schalnat 已提交
161
               farfree(ret);
162 163
               ret = NULL;
            }
G
Guy Schalnat 已提交
164

165 166 167
            if(png_ptr->zlib_window_bits > 14)
               num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
            else
G
Guy Schalnat 已提交
168 169 170 171 172 173
               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 已提交
174
            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
G
Guy Schalnat 已提交
175 176 177

            table = farmalloc(total_size);

A
Andreas Dilger 已提交
178
            if (table == NULL)
G
Guy Schalnat 已提交
179
            {
180
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
181 182 183 184
                  png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
               else
                  png_warning(png_ptr, "Out Of Memory.");
               return (NULL);
G
Guy Schalnat 已提交
185 186
            }

A
Andreas Dilger 已提交
187
            if ((png_size_t)table & 0xfff0)
G
Guy Schalnat 已提交
188
            {
189
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
190 191 192 193 194 195
                  png_error(png_ptr,
                    "Farmalloc didn't return normalized pointer");
               else
                  png_warning(png_ptr,
                    "Farmalloc didn't return normalized pointer");
               return (NULL);
G
Guy Schalnat 已提交
196 197
            }

G
Guy Schalnat 已提交
198
            png_ptr->offset_table = table;
A
Andreas Dilger 已提交
199 200
            png_ptr->offset_table_ptr = farmalloc(num_blocks *
               sizeof (png_bytep));
G
Guy Schalnat 已提交
201

A
Andreas Dilger 已提交
202
            if (png_ptr->offset_table_ptr == NULL)
G
Guy Schalnat 已提交
203
            {
204
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
205 206 207 208
                  png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
               else
                  png_warning(png_ptr, "Out Of memory.");
               return (NULL);
G
Guy Schalnat 已提交
209 210 211
            }

            hptr = (png_byte huge *)table;
A
Andreas Dilger 已提交
212
            if ((png_size_t)hptr & 0xf)
G
Guy Schalnat 已提交
213
            {
G
Guy Schalnat 已提交
214
               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
215
               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
G
Guy Schalnat 已提交
216 217 218 219
            }
            for (i = 0; i < num_blocks; i++)
            {
               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
220
               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
G
Guy Schalnat 已提交
221 222 223 224 225 226
            }

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

G
Guy Schalnat 已提交
229
      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
230
      {
231
         if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
232 233 234 235 236
            png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
         else
            png_warning(png_ptr, "Out of Memory.");
         return (NULL);
      }
G
Guy Schalnat 已提交
237

G
Guy Schalnat 已提交
238
      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
G
Guy Schalnat 已提交
239 240 241
   }
   else
      ret = farmalloc(size);
G
Guy Schalnat 已提交
242

G
Guy Schalnat 已提交
243 244
   if (ret == NULL)
   {
245
      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
246 247 248
         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 已提交
249
   }
G
Guy Schalnat 已提交
250

251
   return (ret);
G
Guy Schalnat 已提交
252 253
}

254
/* free a pointer allocated by png_malloc().  In the default
A
Andreas Dilger 已提交
255 256
   configuration, png_ptr is not used, but is passed in case it
   is needed.  If ptr is NULL, return without taking any action. */
257
void PNGAPI
258
png_free(png_structp png_ptr, png_voidp ptr)
G
Guy Schalnat 已提交
259
{
A
Andreas Dilger 已提交
260
   if (png_ptr == NULL || ptr == NULL)
G
Guy Schalnat 已提交
261 262
      return;

263 264 265 266 267 268 269 270 271
#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);
}

272
void PNGAPI
273 274 275
png_free_default(png_structp png_ptr, png_voidp ptr)
{
#endif /* PNG_USER_MEM_SUPPORTED */
276

A
Andreas Dilger 已提交
277
   if (png_ptr->offset_table != NULL)
G
Guy Schalnat 已提交
278
   {
A
Andreas Dilger 已提交
279
      int i;
G
Guy Schalnat 已提交
280

A
Andreas Dilger 已提交
281 282 283
      for (i = 0; i < png_ptr->offset_table_count; i++)
      {
         if (ptr == png_ptr->offset_table_ptr[i])
G
Guy Schalnat 已提交
284
         {
A
Andreas Dilger 已提交
285 286 287
            ptr = NULL;
            png_ptr->offset_table_count_free++;
            break;
G
Guy Schalnat 已提交
288
         }
G
Guy Schalnat 已提交
289
      }
A
Andreas Dilger 已提交
290 291 292 293 294 295 296
      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 已提交
297
   }
A
Andreas Dilger 已提交
298 299

   if (ptr != NULL)
300
   {
A
Andreas Dilger 已提交
301
      farfree(ptr);
302
   }
G
Guy Schalnat 已提交
303 304 305 306
}

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

G
Guy Schalnat 已提交
307
/* Allocate memory for a png_struct or a png_info.  The malloc and
A
Andreas Dilger 已提交
308
   memset can be replaced by a single call to calloc() if this is thought
309
   to improve performance noticably. */
310
png_voidp /* PRIVATE */
A
Andreas Dilger 已提交
311
png_create_struct(int type)
G
Guy Schalnat 已提交
312
{
313
#ifdef PNG_USER_MEM_SUPPORTED
314
   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
315 316 317 318
}

/* 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
319
   to improve performance noticably. */
320
png_voidp /* PRIVATE */
321
png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
322 323
{
#endif /* PNG_USER_MEM_SUPPORTED */
A
Andreas Dilger 已提交
324
   png_size_t size;
G
Guy Schalnat 已提交
325 326 327
   png_voidp struct_ptr;

   if (type == PNG_STRUCT_INFO)
A
Andreas Dilger 已提交
328
      size = sizeof(png_info);
G
Guy Schalnat 已提交
329
   else if (type == PNG_STRUCT_PNG)
A
Andreas Dilger 已提交
330
      size = sizeof(png_struct);
G
Guy Schalnat 已提交
331
   else
332
      return (NULL);
G
Guy Schalnat 已提交
333

334 335 336
#ifdef PNG_USER_MEM_SUPPORTED
   if(malloc_fn != NULL)
   {
337 338 339
      png_struct dummy_struct;
      png_structp png_ptr = &dummy_struct;
      png_ptr->mem_ptr=mem_ptr;
340
      struct_ptr = (*(malloc_fn))(png_ptr, size);
341
      if (struct_ptr != NULL)
342 343 344 345 346
         png_memset(struct_ptr, 0, size);
      return (struct_ptr);
   }
#endif /* PNG_USER_MEM_SUPPORTED */

G
Guy Schalnat 已提交
347 348 349 350
#if defined(__TURBOC__) && !defined(__FLAT__)
   if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
#else
# if defined(_MSC_VER) && defined(MAXSEG_64K)
G
Guy Schalnat 已提交
351
   if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
G
Guy Schalnat 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364
# else
   if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
# endif
#endif
   {
      png_memset(struct_ptr, 0, size);
   }

   return (struct_ptr);
}


/* Free memory allocated by a png_create_struct() call */
365
void /* PRIVATE */
G
Guy Schalnat 已提交
366 367
png_destroy_struct(png_voidp struct_ptr)
{
368
#ifdef PNG_USER_MEM_SUPPORTED
369
   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
370 371 372
}

/* Free memory allocated by a png_create_struct() call */
373
void /* PRIVATE */
374 375
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
    png_voidp mem_ptr)
376 377
{
#endif /* PNG_USER_MEM_SUPPORTED */
A
Andreas Dilger 已提交
378
   if (struct_ptr != NULL)
379
   {
380 381 382 383 384
#ifdef PNG_USER_MEM_SUPPORTED
      if(free_fn != NULL)
      {
         png_struct dummy_struct;
         png_structp png_ptr = &dummy_struct;
385
         png_ptr->mem_ptr=mem_ptr;
386 387 388 389
         (*(free_fn))(png_ptr, struct_ptr);
         return;
      }
#endif /* PNG_USER_MEM_SUPPORTED */
G
Guy Schalnat 已提交
390 391 392 393 394 395 396 397 398
#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
399
   }
G
Guy Schalnat 已提交
400 401
}

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

408
png_voidp PNGAPI
409
png_malloc(png_structp png_ptr, png_uint_32 size)
G
Guy Schalnat 已提交
410
{
G
Guy Schalnat 已提交
411
   png_voidp ret;
412

A
Andreas Dilger 已提交
413
   if (png_ptr == NULL || size == 0)
414
      return (NULL);
G
Guy Schalnat 已提交
415

416 417
#ifdef PNG_USER_MEM_SUPPORTED
   if(png_ptr->malloc_fn != NULL)
418
   {
419
       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
420
       if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
421 422 423
          png_error(png_ptr, "Out of Memory!");
       return (ret);
   }
424 425 426
   else
       return (png_malloc_default(png_ptr, size));
}
427

428
png_voidp PNGAPI
429 430 431 432 433
png_malloc_default(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ret;
#endif /* PNG_USER_MEM_SUPPORTED */

G
Guy Schalnat 已提交
434
#ifdef PNG_MAX_MALLOC_64K
435
   if (size > (png_uint_32)65536L)
436 437 438 439 440 441
   {
      if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
         png_error(png_ptr, "Cannot Allocate > 64K");
      else
         return NULL;
   }
G
Guy Schalnat 已提交
442 443
#endif

G
Guy Schalnat 已提交
444
#if defined(__TURBOC__) && !defined(__FLAT__)
445
   ret = farmalloc(size);
G
Guy Schalnat 已提交
446
#else
G
Guy Schalnat 已提交
447
# if defined(_MSC_VER) && defined(MAXSEG_64K)
448
   ret = halloc(size, 1);
G
Guy Schalnat 已提交
449
# else
450
   ret = malloc((size_t)size);
G
Guy Schalnat 已提交
451
# endif
G
Guy Schalnat 已提交
452 453
#endif

454
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
G
Guy Schalnat 已提交
455
      png_error(png_ptr, "Out of Memory");
G
Guy Schalnat 已提交
456

457
   return (ret);
G
Guy Schalnat 已提交
458 459
}

460 461
/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
   without taking any action. */
462
void PNGAPI
463
png_free(png_structp png_ptr, png_voidp ptr)
G
Guy Schalnat 已提交
464
{
A
Andreas Dilger 已提交
465
   if (png_ptr == NULL || ptr == NULL)
G
Guy Schalnat 已提交
466
      return;
G
Guy Schalnat 已提交
467

468 469 470 471 472 473 474 475
#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);
}
476
void PNGAPI
477 478
png_free_default(png_structp png_ptr, png_voidp ptr)
{
479 480 481
   if (png_ptr == NULL || ptr == NULL)
      return;

482 483
#endif /* PNG_USER_MEM_SUPPORTED */

G
Guy Schalnat 已提交
484
#if defined(__TURBOC__) && !defined(__FLAT__)
A
Andreas Dilger 已提交
485
   farfree(ptr);
G
Guy Schalnat 已提交
486
#else
G
Guy Schalnat 已提交
487
# if defined(_MSC_VER) && defined(MAXSEG_64K)
A
Andreas Dilger 已提交
488
   hfree(ptr);
G
Guy Schalnat 已提交
489
# else
A
Andreas Dilger 已提交
490
   free(ptr);
G
Guy Schalnat 已提交
491
# endif
G
Guy Schalnat 已提交
492 493 494
#endif
}

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

497 498 499 500
#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()
501 502 503 504 505 506 507 508 509 510 511 512 513 514
 * function will issue a png_warning and return NULL instead of issuing a
 * png_error, if it fails to allocate the requested memory.
 */
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);
}
515
#endif
516

517
png_voidp PNGAPI
518
png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
519 520 521
   png_uint_32 length)
{
   png_size_t size;
522 523 524 525

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

527
   return(png_memcpy (s1, s2, size));
528 529
}

530
png_voidp PNGAPI
531
png_memset_check (png_structp png_ptr, png_voidp s1, int value,
532 533 534
   png_uint_32 length)
{
   png_size_t size;
535 536 537 538 539 540 541

   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));

542
}
543 544 545 546 547

#ifdef PNG_USER_MEM_SUPPORTED
/* This function is called when the application wants to use another method
 * of allocating and freeing memory.
 */
548
void PNGAPI
549 550 551 552 553 554 555 556 557 558 559 560
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.
 */
561
png_voidp PNGAPI
562 563 564 565 566
png_get_mem_ptr(png_structp png_ptr)
{
   return ((png_voidp)png_ptr->mem_ptr);
}
#endif /* PNG_USER_MEM_SUPPORTED */