pngmem.c 13.9 KB
Newer Older
1

G
Guy Schalnat 已提交
2
/* pngmem.c - stub functions for memory allocation
3
 *
4
 * libpng 1.2.1 - December 12, 2001
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 * Copyright (c) 1998-2001 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 (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 59
      struct_ptr = (png_voidp)farmalloc(size));
   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
png_voidp PNGAPI
114
png_malloc(png_structp png_ptr, png_uint_32 size)
G
Guy Schalnat 已提交
115
{
116
#ifndef PNG_USER_MEM_SUPPORTED
G
Guy Schalnat 已提交
117
   png_voidp ret;
118
#endif
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 127 128 129
       if (ret == NULL)
          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
               png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
G
Guy Schalnat 已提交
181 182
            }

A
Andreas Dilger 已提交
183
            if ((png_size_t)table & 0xfff0)
G
Guy Schalnat 已提交
184
            {
G
Guy Schalnat 已提交
185
               png_error(png_ptr, "Farmalloc didn't return normalized pointer");
G
Guy Schalnat 已提交
186 187
            }

G
Guy Schalnat 已提交
188
            png_ptr->offset_table = table;
A
Andreas Dilger 已提交
189 190
            png_ptr->offset_table_ptr = farmalloc(num_blocks *
               sizeof (png_bytep));
G
Guy Schalnat 已提交
191

A
Andreas Dilger 已提交
192
            if (png_ptr->offset_table_ptr == NULL)
G
Guy Schalnat 已提交
193
            {
194
               png_error(png_ptr, "Out Of memory.");
G
Guy Schalnat 已提交
195 196 197
            }

            hptr = (png_byte huge *)table;
A
Andreas Dilger 已提交
198
            if ((png_size_t)hptr & 0xf)
G
Guy Schalnat 已提交
199
            {
G
Guy Schalnat 已提交
200
               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
201
               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
G
Guy Schalnat 已提交
202 203 204 205
            }
            for (i = 0; i < num_blocks; i++)
            {
               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
206
               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
G
Guy Schalnat 已提交
207 208 209 210 211 212
            }

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

G
Guy Schalnat 已提交
215
      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
216
         png_error(png_ptr, "Out of Memory.");
G
Guy Schalnat 已提交
217

G
Guy Schalnat 已提交
218
      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
G
Guy Schalnat 已提交
219 220 221
   }
   else
      ret = farmalloc(size);
G
Guy Schalnat 已提交
222

G
Guy Schalnat 已提交
223 224
   if (ret == NULL)
   {
225
      png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
G
Guy Schalnat 已提交
226
   }
G
Guy Schalnat 已提交
227

228
   return (ret);
G
Guy Schalnat 已提交
229 230
}

231
/* free a pointer allocated by png_malloc().  In the default
A
Andreas Dilger 已提交
232 233
   configuration, png_ptr is not used, but is passed in case it
   is needed.  If ptr is NULL, return without taking any action. */
234
void PNGAPI
235
png_free(png_structp png_ptr, png_voidp ptr)
G
Guy Schalnat 已提交
236
{
A
Andreas Dilger 已提交
237
   if (png_ptr == NULL || ptr == NULL)
G
Guy Schalnat 已提交
238 239
      return;

240 241 242 243 244 245 246 247 248
#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);
}

249
void PNGAPI
250 251 252
png_free_default(png_structp png_ptr, png_voidp ptr)
{
#endif /* PNG_USER_MEM_SUPPORTED */
253

A
Andreas Dilger 已提交
254
   if (png_ptr->offset_table != NULL)
G
Guy Schalnat 已提交
255
   {
A
Andreas Dilger 已提交
256
      int i;
G
Guy Schalnat 已提交
257

A
Andreas Dilger 已提交
258 259 260
      for (i = 0; i < png_ptr->offset_table_count; i++)
      {
         if (ptr == png_ptr->offset_table_ptr[i])
G
Guy Schalnat 已提交
261
         {
A
Andreas Dilger 已提交
262 263 264
            ptr = NULL;
            png_ptr->offset_table_count_free++;
            break;
G
Guy Schalnat 已提交
265
         }
G
Guy Schalnat 已提交
266
      }
A
Andreas Dilger 已提交
267 268 269 270 271 272 273
      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 已提交
274
   }
A
Andreas Dilger 已提交
275 276

   if (ptr != NULL)
277
   {
A
Andreas Dilger 已提交
278
      farfree(ptr);
279
   }
G
Guy Schalnat 已提交
280 281 282 283
}

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

G
Guy Schalnat 已提交
284
/* Allocate memory for a png_struct or a png_info.  The malloc and
A
Andreas Dilger 已提交
285 286
   memset can be replaced by a single call to calloc() if this is thought
   to improve performance noticably.*/
287
png_voidp /* PRIVATE */
A
Andreas Dilger 已提交
288
png_create_struct(int type)
G
Guy Schalnat 已提交
289
{
290
#ifdef PNG_USER_MEM_SUPPORTED
291
   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
292 293 294 295 296
}

/* 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
   to improve performance noticably.*/
297
png_voidp /* PRIVATE */
298
png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
299 300
{
#endif /* PNG_USER_MEM_SUPPORTED */
A
Andreas Dilger 已提交
301
   png_size_t size;
G
Guy Schalnat 已提交
302 303 304
   png_voidp struct_ptr;

   if (type == PNG_STRUCT_INFO)
A
Andreas Dilger 已提交
305
      size = sizeof(png_info);
G
Guy Schalnat 已提交
306
   else if (type == PNG_STRUCT_PNG)
A
Andreas Dilger 已提交
307
      size = sizeof(png_struct);
G
Guy Schalnat 已提交
308
   else
309
      return (NULL);
G
Guy Schalnat 已提交
310

311 312 313
#ifdef PNG_USER_MEM_SUPPORTED
   if(malloc_fn != NULL)
   {
314 315 316
      png_struct dummy_struct;
      png_structp png_ptr = &dummy_struct;
      png_ptr->mem_ptr=mem_ptr;
317
      struct_ptr = (*(malloc_fn))(png_ptr, size);
318
      if (struct_ptr != NULL)
319 320 321 322 323
         png_memset(struct_ptr, 0, size);
      return (struct_ptr);
   }
#endif /* PNG_USER_MEM_SUPPORTED */

G
Guy Schalnat 已提交
324 325 326 327
#if defined(__TURBOC__) && !defined(__FLAT__)
   if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
#else
# if defined(_MSC_VER) && defined(MAXSEG_64K)
G
Guy Schalnat 已提交
328
   if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
G
Guy Schalnat 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341
# 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 */
342
void /* PRIVATE */
G
Guy Schalnat 已提交
343 344
png_destroy_struct(png_voidp struct_ptr)
{
345
#ifdef PNG_USER_MEM_SUPPORTED
346
   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
347 348 349
}

/* Free memory allocated by a png_create_struct() call */
350
void /* PRIVATE */
351 352
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
    png_voidp mem_ptr)
353 354
{
#endif /* PNG_USER_MEM_SUPPORTED */
A
Andreas Dilger 已提交
355
   if (struct_ptr != NULL)
356
   {
357 358 359 360 361
#ifdef PNG_USER_MEM_SUPPORTED
      if(free_fn != NULL)
      {
         png_struct dummy_struct;
         png_structp png_ptr = &dummy_struct;
362
         png_ptr->mem_ptr=mem_ptr;
363 364 365 366
         (*(free_fn))(png_ptr, struct_ptr);
         return;
      }
#endif /* PNG_USER_MEM_SUPPORTED */
G
Guy Schalnat 已提交
367 368 369 370 371 372 373 374 375
#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
376
   }
G
Guy Schalnat 已提交
377 378 379
}


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

386
png_voidp PNGAPI
387
png_malloc(png_structp png_ptr, png_uint_32 size)
G
Guy Schalnat 已提交
388
{
G
Guy Schalnat 已提交
389
   png_voidp ret;
A
Andreas Dilger 已提交
390
   if (png_ptr == NULL || size == 0)
391
      return (NULL);
G
Guy Schalnat 已提交
392

393 394
#ifdef PNG_USER_MEM_SUPPORTED
   if(png_ptr->malloc_fn != NULL)
395 396 397 398 399 400
   {
       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
       if (ret == NULL)
          png_error(png_ptr, "Out of Memory!");
       return (ret);
   }
401 402 403
   else
       return (png_malloc_default(png_ptr, size));
}
404
png_voidp /* PRIVATE */
405 406 407 408 409
png_malloc_default(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ret;
#endif /* PNG_USER_MEM_SUPPORTED */

G
Guy Schalnat 已提交
410
#ifdef PNG_MAX_MALLOC_64K
411
   if (size > (png_uint_32)65536L)
G
Guy Schalnat 已提交
412
      png_error(png_ptr, "Cannot Allocate > 64K");
G
Guy Schalnat 已提交
413 414
#endif

G
Guy Schalnat 已提交
415
#if defined(__TURBOC__) && !defined(__FLAT__)
416
   ret = farmalloc(size);
G
Guy Schalnat 已提交
417
#else
G
Guy Schalnat 已提交
418
# if defined(_MSC_VER) && defined(MAXSEG_64K)
419
   ret = halloc(size, 1);
G
Guy Schalnat 已提交
420
# else
421
   ret = malloc((size_t)size);
G
Guy Schalnat 已提交
422
# endif
G
Guy Schalnat 已提交
423 424
#endif

G
Guy Schalnat 已提交
425
   if (ret == NULL)
G
Guy Schalnat 已提交
426
      png_error(png_ptr, "Out of Memory");
G
Guy Schalnat 已提交
427

428
   return (ret);
G
Guy Schalnat 已提交
429 430
}

431 432
/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
   without taking any action. */
433
void PNGAPI
434
png_free(png_structp png_ptr, png_voidp ptr)
G
Guy Schalnat 已提交
435
{
A
Andreas Dilger 已提交
436
   if (png_ptr == NULL || ptr == NULL)
G
Guy Schalnat 已提交
437
      return;
G
Guy Schalnat 已提交
438

439 440 441 442 443 444 445 446
#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);
}
447
void /* PRIVATE */
448 449
png_free_default(png_structp png_ptr, png_voidp ptr)
{
450 451 452
   if (png_ptr == NULL || ptr == NULL)
      return;

453 454
#endif /* PNG_USER_MEM_SUPPORTED */

G
Guy Schalnat 已提交
455
#if defined(__TURBOC__) && !defined(__FLAT__)
A
Andreas Dilger 已提交
456
   farfree(ptr);
G
Guy Schalnat 已提交
457
#else
G
Guy Schalnat 已提交
458
# if defined(_MSC_VER) && defined(MAXSEG_64K)
A
Andreas Dilger 已提交
459
   hfree(ptr);
G
Guy Schalnat 已提交
460
# else
A
Andreas Dilger 已提交
461
   free(ptr);
G
Guy Schalnat 已提交
462
# endif
G
Guy Schalnat 已提交
463 464 465
#endif
}

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

468
png_voidp /* PRIVATE */
469
png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
470 471 472
   png_uint_32 length)
{
   png_size_t size;
473 474 475 476

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

478
   return(png_memcpy (s1, s2, size));
479 480
}

481
png_voidp /* PRIVATE */
482
png_memset_check (png_structp png_ptr, png_voidp s1, int value,
483 484 485
   png_uint_32 length)
{
   png_size_t size;
486 487 488 489 490 491 492

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

493
}
494 495 496 497 498

#ifdef PNG_USER_MEM_SUPPORTED
/* This function is called when the application wants to use another method
 * of allocating and freeing memory.
 */
499
void PNGAPI
500 501 502 503 504 505 506 507 508 509 510 511
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.
 */
512
png_voidp PNGAPI
513 514 515 516 517
png_get_mem_ptr(png_structp png_ptr)
{
   return ((png_voidp)png_ptr->mem_ptr);
}
#endif /* PNG_USER_MEM_SUPPORTED */