Subversion Repositories Kolibri OS

Rev

Rev 1897 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1897 Rev 3928
Line 1... Line 1...
1
/* pngmem.c - stub functions for memory allocation
1
/* pngmem.c - stub functions for memory allocation
2
 *
2
 *
3
 * Last changed in libpng 1.5.1 [February 3, 2011]
3
 * Last changed in libpng 1.6.0 [February 14, 2013]
4
 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
4
 * Copyright (c) 1998-2013 Glenn Randers-Pehrson
5
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
5
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
6
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
7
 *
7
 *
8
 * This code is released under the libpng license.
8
 * This code is released under the libpng license.
9
 * For conditions of distribution and use, see the disclaimer
9
 * For conditions of distribution and use, see the disclaimer
Line 18... Line 18...
18
 
18
 
Line 19... Line 19...
19
#include "pngpriv.h"
19
#include "pngpriv.h"
Line 20... Line 20...
20
 
20
 
21
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-
 
22
 
-
 
23
/* Borland DOS special memory handler */
-
 
24
#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
-
 
25
/* If you change this, be sure to change the one in png.h also */
-
 
26
 
-
 
27
/* Allocate memory for a png_struct.  The malloc and memset can be replaced
-
 
28
   by a single call to calloc() if this is thought to improve performance. */
-
 
29
PNG_FUNCTION(png_voidp /* PRIVATE */,
-
 
30
png_create_struct,(int type),PNG_ALLOCATED)
-
 
31
{
-
 
32
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
33
   return (png_create_struct_2(type, NULL, NULL));
-
 
34
}
-
 
35
 
-
 
36
/* Alternate version of png_create_struct, for use with user-defined malloc. */
-
 
37
PNG_FUNCTION(png_voidp /* PRIVATE */,
-
 
38
png_create_struct_2,(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr),
-
 
39
   PNG_ALLOCATED)
-
 
40
{
-
 
41
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
42
   png_size_t size;
-
 
43
   png_voidp struct_ptr;
-
 
44
 
-
 
45
   if (type == PNG_STRUCT_INFO)
-
 
46
      size = png_sizeof(png_info);
-
 
47
 
-
 
48
   else if (type == PNG_STRUCT_PNG)
-
 
49
      size = png_sizeof(png_struct);
-
 
50
 
-
 
51
   else
-
 
52
      return (png_get_copyright(NULL));
-
 
53
 
-
 
54
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
55
   if (malloc_fn != NULL)
-
 
56
   {
-
 
57
      png_struct dummy_struct;
-
 
58
      png_structp png_ptr = &dummy_struct;
-
 
59
      png_ptr->mem_ptr=mem_ptr;
-
 
60
      struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
-
 
61
   }
-
 
62
 
-
 
63
   else
-
 
64
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
65
   struct_ptr = (png_voidp)farmalloc(size);
-
 
66
   if (struct_ptr != NULL)
-
 
67
      png_memset(struct_ptr, 0, size);
-
 
68
 
21
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
69
   return (struct_ptr);
-
 
70
}
-
 
71
 
-
 
72
/* Free memory allocated by a png_create_struct() call */
22
/* Free a png_struct */
73
void /* PRIVATE */
23
void /* PRIVATE */
74
png_destroy_struct(png_voidp struct_ptr)
24
png_destroy_png_struct(png_structrp png_ptr)
75
{
-
 
76
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
77
   png_destroy_struct_2(struct_ptr, NULL, NULL);
-
 
78
}
-
 
79
 
-
 
80
/* Free memory allocated by a png_create_struct() call */
-
 
81
void /* PRIVATE */
-
 
82
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
25
{
83
    png_voidp mem_ptr)
26
   if (png_ptr != NULL)
-
 
27
   {
-
 
28
      /* png_free might call png_error and may certainly call
-
 
29
       * png_get_mem_ptr, so fake a temporary png_struct to support this.
-
 
30
       */
-
 
31
      png_struct dummy_struct = *png_ptr;
-
 
32
      memset(png_ptr, 0, (sizeof *png_ptr));
-
 
33
      png_free(&dummy_struct, png_ptr);
-
 
34
 
-
 
35
#     ifdef PNG_SETJMP_SUPPORTED
-
 
36
         /* We may have a jmp_buf left to deallocate. */
84
{
37
         png_free_jmpbuf(&dummy_struct);
85
#  endif
-
 
86
   if (struct_ptr != NULL)
-
 
87
   {
-
 
88
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
89
      if (free_fn != NULL)
-
 
90
      {
-
 
91
         png_struct dummy_struct;
-
 
92
         png_structp png_ptr = &dummy_struct;
-
 
93
         png_ptr->mem_ptr=mem_ptr;
-
 
94
         (*(free_fn))(png_ptr, struct_ptr);
-
 
95
         return;
-
 
96
      }
-
 
97
 
-
 
98
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
99
      farfree (struct_ptr);
38
#     endif
100
   }
39
   }
Line 101... Line 40...
101
}
40
}
102
 
41
 
103
/* Allocate memory.  For reasonable files, size should never exceed
42
/* Allocate memory.  For reasonable files, size should never exceed
104
 * 64K.  However, zlib may allocate more then 64K if you don't tell
43
 * 64K.  However, zlib may allocate more then 64K if you don't tell
105
 * it not to.  See zconf.h and png.h for more information. zlib does
44
 * it not to.  See zconf.h and png.h for more information.  zlib does
106
 * need to allocate exactly 64K, so whatever you call here must
-
 
107
 * have the ability to do that.
-
 
108
 *
-
 
109
 * Borland seems to have a problem in DOS mode for exactly 64K.
-
 
110
 * It gives you a segment with an offset of 8 (perhaps to store its
-
 
111
 * memory stuff).  zlib doesn't like this at all, so we have to
-
 
112
 * detect and deal with it.  This code should not be needed in
-
 
113
 * Windows or OS/2 modes, and only in 16 bit mode.  This code has
-
 
114
 * been updated by Alexander Lehmann for version 0.89 to waste less
-
 
115
 * memory.
-
 
116
 *
-
 
117
 * Note that we can't use png_size_t for the "size" declaration,
-
 
118
 * since on some systems a png_size_t is a 16-bit quantity, and as a
-
 
119
 * result, we would be truncating potentially larger memory requests
45
 * need to allocate exactly 64K, so whatever you call here must
120
 * (which should cause a fatal error) and introducing major problems.
46
 * have the ability to do that.
121
 */
47
 */
122
PNG_FUNCTION(png_voidp,PNGAPI
48
PNG_FUNCTION(png_voidp,PNGAPI
123
png_calloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
49
png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Line 124... Line 50...
124
{
50
{
Line 125... Line 51...
125
   png_voidp ret;
51
   png_voidp ret;
126
 
52
 
Line 127... Line 53...
127
   ret = (png_malloc(png_ptr, size));
53
   ret = png_malloc(png_ptr, size);
128
 
54
 
Line -... Line 55...
-
 
55
   if (ret != NULL)
-
 
56
      memset(ret, 0, size);
-
 
57
 
-
 
58
   return ret;
-
 
59
}
129
   if (ret != NULL)
60
 
130
      png_memset(ret,0,(png_size_t)size);
61
/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of
-
 
62
 * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.
131
 
63
 * Checking and error handling must happen outside this routine; it returns NULL
132
   return (ret);
64
 * if the allocation cannot be done (for any reason.)
133
}
-
 
-
 
65
 */
134
 
66
PNG_FUNCTION(png_voidp /* PRIVATE */,
135
PNG_FUNCTION(png_voidp,PNGAPI
67
png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
136
png_malloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
68
   PNG_ALLOCATED)
137
{
69
{
138
   png_voidp ret;
-
 
139
 
-
 
140
   if (png_ptr == NULL || size == 0)
-
 
141
      return (NULL);
-
 
142
 
-
 
143
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
144
   if (png_ptr->malloc_fn != NULL)
-
 
145
      ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
-
 
146
 
-
 
147
   else
-
 
148
      ret = (png_malloc_default(png_ptr, size));
-
 
149
 
-
 
150
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
70
   /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
151
       png_error(png_ptr, "Out of memory");
-
 
152
 
-
 
153
   return (ret);
-
 
154
}
-
 
155
 
-
 
156
PNG_FUNCTION(png_voidp,PNGAPI
-
 
157
png_malloc_default,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
-
 
158
{
-
 
159
   png_voidp ret;
-
 
160
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
161
 
-
 
162
   if (png_ptr == NULL || size == 0)
-
 
163
      return (NULL);
-
 
164
 
-
 
165
#  ifdef PNG_MAX_MALLOC_64K
-
 
166
   if (size > (png_uint_32)65536L)
-
 
167
   {
71
    * allocators have also been removed in 1.6.0, so any 16-bit system now has
168
      png_warning(png_ptr, "Cannot Allocate > 64K");
-
 
169
      ret = NULL;
72
    * to implement a user memory handler.  This checks to be sure it isn't
170
   }
-
 
171
 
-
 
172
   else
-
 
173
#  endif
-
 
174
 
-
 
175
   if (size != (size_t)size)
-
 
176
      ret = NULL;
-
 
177
 
-
 
178
   else if (size == (png_uint_32)65536L)
-
 
179
   {
-
 
180
      if (png_ptr->offset_table == NULL)
-
 
181
      {
-
 
182
         /* Try to see if we need to do any of this fancy stuff */
-
 
183
         ret = farmalloc(size);
-
 
184
         if (ret == NULL || ((png_size_t)ret & 0xffff))
-
 
185
         {
-
 
186
            int num_blocks;
-
 
187
            png_uint_32 total_size;
-
 
188
            png_bytep table;
-
 
189
            int i;
-
 
190
            png_byte huge * hptr;
-
 
191
 
-
 
192
            if (ret != NULL)
-
 
193
            {
-
 
194
               farfree(ret);
-
 
195
               ret = NULL;
-
 
196
            }
-
 
197
 
-
 
198
            if (png_ptr->zlib_window_bits > 14)
-
 
199
               num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
-
 
200
 
-
 
201
            else
-
 
202
               num_blocks = 1;
-
 
203
 
-
 
204
            if (png_ptr->zlib_mem_level >= 7)
-
 
205
               num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
-
 
206
 
-
 
207
            else
-
 
208
               num_blocks++;
-
 
209
 
-
 
210
            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
-
 
211
 
-
 
212
            table = farmalloc(total_size);
-
 
213
 
-
 
214
            if (table == NULL)
-
 
215
            {
-
 
216
#  ifndef PNG_USER_MEM_SUPPORTED
-
 
217
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-
 
218
                  png_error(png_ptr, "Out Of Memory"); /* Note "O", "M" */
-
 
219
 
-
 
220
               else
-
 
221
                  png_warning(png_ptr, "Out Of Memory");
-
 
222
#  endif
73
    * called with big numbers.
223
               return (NULL);
-
 
224
            }
-
 
225
 
-
 
226
            if ((png_size_t)table & 0xfff0)
-
 
227
            {
74
    */
228
#  ifndef PNG_USER_MEM_SUPPORTED
-
 
229
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-
 
230
                  png_error(png_ptr,
-
 
231
                    "Farmalloc didn't return normalized pointer");
-
 
232
 
-
 
233
               else
-
 
234
                  png_warning(png_ptr,
-
 
235
                    "Farmalloc didn't return normalized pointer");
-
 
236
#  endif
-
 
237
               return (NULL);
-
 
238
            }
-
 
239
 
-
 
240
            png_ptr->offset_table = table;
-
 
241
            png_ptr->offset_table_ptr = farmalloc(num_blocks *
-
 
242
               png_sizeof(png_bytep));
-
 
243
 
-
 
244
            if (png_ptr->offset_table_ptr == NULL)
-
 
245
            {
-
 
246
#  ifndef PNG_USER_MEM_SUPPORTED
75
#ifdef PNG_USER_MEM_SUPPORTED
247
               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-
 
248
                  png_error(png_ptr, "Out Of memory"); /* Note "O", "m" */
-
 
249
 
-
 
250
               else
-
 
251
                  png_warning(png_ptr, "Out Of memory");
-
 
252
#  endif
-
 
253
               return (NULL);
-
 
254
            }
-
 
255
 
-
 
256
            hptr = (png_byte huge *)table;
-
 
257
            if ((png_size_t)hptr & 0xf)
-
 
258
            {
-
 
259
               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
-
 
260
               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
-
 
261
            }
-
 
262
 
-
 
263
            for (i = 0; i < num_blocks; i++)
-
 
264
            {
-
 
265
               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
-
 
266
               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
-
 
267
            }
76
   PNG_UNUSED(png_ptr)
268
 
-
 
269
            png_ptr->offset_table_number = num_blocks;
-
 
270
            png_ptr->offset_table_count = 0;
77
#endif
271
            png_ptr->offset_table_count_free = 0;
78
   if (size > 0 && size <= PNG_SIZE_MAX
272
         }
79
#     ifdef PNG_MAX_MALLOC_64K
273
      }
80
         && size <= 65536U
Line 274... Line 81...
274
 
81
#     endif
275
      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
-
 
276
      {
82
      )
277
#  ifndef PNG_USER_MEM_SUPPORTED
-
 
278
         if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-
 
279
            png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
-
 
280
 
83
   {
281
         else
84
#ifdef PNG_USER_MEM_SUPPORTED
Line 282... Line 85...
282
            png_warning(png_ptr, "Out of Memory");
85
      if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
283
#  endif
-
 
284
         return (NULL);
-
 
285
      }
-
 
286
 
86
         return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
287
      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
-
 
288
   }
-
 
289
 
-
 
290
   else
-
 
291
      ret = farmalloc(size);
-
 
292
 
-
 
293
#  ifndef PNG_USER_MEM_SUPPORTED
-
 
294
   if (ret == NULL)
-
 
295
   {
-
 
296
      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-
 
297
         png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
87
 
Line 298... Line 88...
298
 
88
      else
299
      else
89
#endif
300
         png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
90
         return malloc((size_t)size); /* checked for truncation above */
301
   }
91
   }
302
#  endif
92
 
303
 
93
   else
-
 
94
      return NULL;
304
   return (ret);
95
}
305
}
96
 
306
 
-
 
Line 307... Line 97...
307
/* Free a pointer allocated by png_malloc().  In the default
97
/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
308
 * configuration, png_ptr is not used, but is passed in case it
-
 
309
 * is needed.  If ptr is NULL, return without taking any action.
-
 
310
 */
98
 * that arises because of the checks in png_realloc_array that are repeated in
311
void PNGAPI
-
 
312
png_free(png_structp png_ptr, png_voidp ptr)
-
 
Line -... Line 99...
-
 
99
 * png_malloc_array.
313
{
100
 */
314
   if (png_ptr == NULL || ptr == NULL)
-
 
315
      return;
101
static png_voidp
Line 316... Line 102...
316
 
102
png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
317
#  ifdef PNG_USER_MEM_SUPPORTED
103
   size_t element_size)
318
   if (png_ptr->free_fn != NULL)
-
 
319
   {
-
 
320
      (*(png_ptr->free_fn))(png_ptr, ptr);
-
 
321
      return;
-
 
322
   }
-
 
323
 
-
 
324
   else
104
{
325
      png_free_default(png_ptr, ptr);
-
 
326
}
-
 
327
 
-
 
328
void PNGAPI
-
 
329
png_free_default(png_structp png_ptr, png_voidp ptr)
-
 
330
{
-
 
331
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
332
 
-
 
333
   if (png_ptr == NULL || ptr == NULL)
-
 
334
      return;
-
 
335
 
-
 
336
   if (png_ptr->offset_table != NULL)
-
 
337
   {
-
 
338
      int i;
105
   png_alloc_size_t req = nelements; /* known to be > 0 */
339
 
-
 
340
      for (i = 0; i < png_ptr->offset_table_count; i++)
106
 
341
      {
-
 
342
         if (ptr == png_ptr->offset_table_ptr[i])
107
   if (req <= PNG_SIZE_MAX/element_size)
343
         {
-
 
344
            ptr = NULL;
-
 
Line 345... Line -...
345
            png_ptr->offset_table_count_free++;
-
 
346
            break;
108
      return png_malloc_base(png_ptr, req * element_size);
347
         }
109
 
Line 348... Line -...
348
      }
-
 
349
      if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
-
 
350
      {
-
 
351
         farfree(png_ptr->offset_table);
-
 
352
         farfree(png_ptr->offset_table_ptr);
-
 
353
         png_ptr->offset_table = NULL;
110
   /* The failure case when the request is too large */
-
 
111
   return NULL;
354
         png_ptr->offset_table_ptr = NULL;
112
}
355
      }
113
 
356
   }
114
PNG_FUNCTION(png_voidp /* PRIVATE */,
-
 
115
png_malloc_array,(png_const_structrp png_ptr, int nelements,
-
 
116
   size_t element_size),PNG_ALLOCATED)
357
 
117
{
358
   if (ptr != NULL)
-
 
Line 359... Line -...
359
      farfree(ptr);
-
 
360
}
118
   if (nelements <= 0 || element_size == 0)
361
 
119
      png_error(png_ptr, "internal error: array alloc");
362
#else /* Not the Borland DOS special memory handler */
120
 
363
 
121
   return png_malloc_array_checked(png_ptr, nelements, element_size);
364
/* Allocate memory for a png_struct or a png_info.  The malloc and
-
 
365
   memset can be replaced by a single call to calloc() if this is thought
122
}
366
   to improve performance noticably. */
-
 
367
PNG_FUNCTION(png_voidp /* PRIVATE */,
-
 
368
png_create_struct,(int type),PNG_ALLOCATED)
-
 
369
{
-
 
370
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
371
   return (png_create_struct_2(type, NULL, NULL));
123
 
372
}
-
 
373
 
-
 
374
/* Allocate memory for a png_struct or a png_info.  The malloc and
124
PNG_FUNCTION(png_voidp /* PRIVATE */,
Line 375... Line -...
375
   memset can be replaced by a single call to calloc() if this is thought
-
 
376
   to improve performance noticably. */
-
 
377
PNG_FUNCTION(png_voidp /* PRIVATE */,
-
 
378
png_create_struct_2,(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr),
-
 
379
   PNG_ALLOCATED)
125
png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
380
{
126
   int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
-
 
127
{
381
#  endif /* PNG_USER_MEM_SUPPORTED */
128
   /* These are internal errors: */
382
   png_size_t size;
129
   if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
383
   png_voidp struct_ptr;
130
      (old_array == NULL && old_elements > 0))
384
 
131
      png_error(png_ptr, "internal error: array realloc");
Line 385... Line 132...
385
   if (type == PNG_STRUCT_INFO)
132
 
386
      size = png_sizeof(png_info);
133
   /* Check for overflow on the elements count (so the caller does not have to
Line 387... Line 134...
387
 
134
    * check.)
388
   else if (type == PNG_STRUCT_PNG)
135
    */
389
      size = png_sizeof(png_struct);
-
 
390
 
-
 
391
   else
-
 
392
      return (NULL);
-
 
393
 
-
 
394
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
395
   if (malloc_fn != NULL)
-
 
396
   {
-
 
397
      png_struct dummy_struct;
-
 
398
      png_structp png_ptr = &dummy_struct;
-
 
399
      png_ptr->mem_ptr=mem_ptr;
-
 
400
      struct_ptr = (*(malloc_fn))(png_ptr, size);
-
 
401
 
-
 
402
      if (struct_ptr != NULL)
-
 
403
         png_memset(struct_ptr, 0, size);
-
 
404
 
-
 
405
      return (struct_ptr);
136
   if (add_elements <= INT_MAX - old_elements)
Line 406... Line -...
406
   }
-
 
407
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
408
 
-
 
409
#  if defined(__TURBOC__) && !defined(__FLAT__)
-
 
410
   struct_ptr = (png_voidp)farmalloc(size);
-
 
411
#  else
137
   {
412
#    if defined(_MSC_VER) && defined(MAXSEG_64K)
-
 
413
   struct_ptr = (png_voidp)halloc(size, 1);
138
      png_voidp new_array = png_malloc_array_checked(png_ptr,
Line 414... Line -...
414
#    else
-
 
415
   struct_ptr = (png_voidp)malloc(size);
-
 
416
#    endif
139
         old_elements+add_elements, element_size);
417
#  endif
-
 
418
 
-
 
419
   if (struct_ptr != NULL)
140
 
420
      png_memset(struct_ptr, 0, size);
-
 
421
 
-
 
422
   return (struct_ptr);
-
 
423
}
-
 
424
 
-
 
425
 
-
 
426
/* Free memory allocated by a png_create_struct() call */
141
      if (new_array != NULL)
427
void /* PRIVATE */
-
 
428
png_destroy_struct(png_voidp struct_ptr)
-
 
429
{
-
 
430
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
431
   png_destroy_struct_2(struct_ptr, NULL, NULL);
-
 
432
}
-
 
433
 
-
 
434
/* Free memory allocated by a png_create_struct() call */
-
 
435
void /* PRIVATE */
-
 
436
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
-
 
437
    png_voidp mem_ptr)
-
 
438
{
-
 
439
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
440
   if (struct_ptr != NULL)
-
 
441
   {
-
 
442
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
443
      if (free_fn != NULL)
-
 
444
      {
-
 
445
         png_struct dummy_struct;
-
 
446
         png_structp png_ptr = &dummy_struct;
-
 
447
         png_ptr->mem_ptr=mem_ptr;
-
 
448
         (*(free_fn))(png_ptr, struct_ptr);
-
 
449
         return;
-
 
450
      }
-
 
451
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
452
#  if defined(__TURBOC__) && !defined(__FLAT__)
142
      {
453
      farfree(struct_ptr);
-
 
454
 
143
         /* Because png_malloc_array worked the size calculations below cannot
455
#  else
144
          * overflow.
456
#    if defined(_MSC_VER) && defined(MAXSEG_64K)
145
          */
457
      hfree(struct_ptr);
146
         if (old_elements > 0)
Line 458... Line 147...
458
 
147
            memcpy(new_array, old_array, element_size*(unsigned)old_elements);
-
 
148
 
Line 459... Line -...
459
#    else
-
 
460
      free(struct_ptr);
149
         memset((char*)new_array + element_size*(unsigned)old_elements, 0,
Line -... Line 150...
-
 
150
            element_size*(unsigned)add_elements);
-
 
151
 
-
 
152
         return new_array;
461
 
153
      }
462
#    endif
154
   }
Line -... Line 155...
-
 
155
 
463
#  endif
156
   return NULL; /* error */
464
   }
157
}
-
 
158
 
465
}
159
/* Various functions that have different error handling are derived from this.
466
 
160
 * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
Line 467... Line -...
467
/* Allocate memory.  For reasonable files, size should never exceed
-
 
468
 * 64K.  However, zlib may allocate more then 64K if you don't tell
161
 * function png_malloc_default is also provided.
469
 * it not to.  See zconf.h and png.h for more information.  zlib does
162
 */
470
 * need to allocate exactly 64K, so whatever you call here must
-
 
471
 * have the ability to do that.
-
 
472
 */
-
 
Line 473... Line -...
473
 
-
 
-
 
163
PNG_FUNCTION(png_voidp,PNGAPI
474
PNG_FUNCTION(png_voidp,PNGAPI
164
png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Line 475... Line 165...
475
png_calloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
165
{
476
{
166
   png_voidp ret;
Line 477... Line 167...
477
   png_voidp ret;
167
 
478
 
168
   if (png_ptr == NULL)
-
 
169
      return NULL;
Line -... Line 170...
-
 
170
 
-
 
171
   ret = png_malloc_base(png_ptr, size);
-
 
172
 
-
 
173
   if (ret == NULL)
479
   ret = (png_malloc(png_ptr, size));
174
       png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
480
 
175
 
-
 
176
   return ret;
481
   if (ret != NULL)
177
}
482
      png_memset(ret,0,(png_size_t)size);
-
 
483
 
-
 
484
   return (ret);
-
 
485
}
178
 
486
 
-
 
487
PNG_FUNCTION(png_voidp,PNGAPI
-
 
488
png_malloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
-
 
489
{
-
 
490
   png_voidp ret;
179
#ifdef PNG_USER_MEM_SUPPORTED
491
 
-
 
492
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
493
   if (png_ptr == NULL || size == 0)
180
PNG_FUNCTION(png_voidp,PNGAPI
494
      return (NULL);
-
 
495
 
-
 
496
   if (png_ptr->malloc_fn != NULL)
-
 
497
      ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
-
 
498
 
-
 
499
   else
-
 
500
      ret = (png_malloc_default(png_ptr, size));
-
 
501
 
-
 
502
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-
 
Line 503... Line -...
503
       png_error(png_ptr, "Out of Memory");
-
 
504
 
181
png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
505
   return (ret);
-
 
506
}
-
 
507
 
-
 
508
PNG_FUNCTION(png_voidp,PNGAPI
-
 
509
png_malloc_default,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
-
 
510
{
-
 
511
   png_voidp ret;
-
 
512
#  endif /* PNG_USER_MEM_SUPPORTED */
-
 
513
 
-
 
514
   if (png_ptr == NULL || size == 0)
-
 
515
      return (NULL);
-
 
516
 
-
 
517
#  ifdef PNG_MAX_MALLOC_64K
-
 
518
   if (size > (png_uint_32)65536L)
-
 
519
   {
182
   PNG_ALLOCATED PNG_DEPRECATED)
520
#    ifndef PNG_USER_MEM_SUPPORTED
-
 
521
      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-
 
522
         png_error(png_ptr, "Cannot Allocate > 64K");
-
 
523
 
-
 
524
      else
-
 
Line 525... Line -...
525
#    endif
-
 
526
         return NULL;
-
 
527
   }
183
{
528
#  endif
184
   png_voidp ret;
Line 529... Line 185...
529
 
185
 
530
   /* Check for overflow */
186
   if (png_ptr == NULL)
Line 531... Line 187...
531
#  if defined(__TURBOC__) && !defined(__FLAT__)
187
      return NULL;
532
 
188
 
533
   if (size != (unsigned long)size)
189
   /* Passing 'NULL' here bypasses the application provided memory handler. */
534
      ret = NULL;
190
   ret = png_malloc_base(NULL/*use malloc*/, size);
535
 
191
 
536
   else
192
   if (ret == NULL)
537
      ret = farmalloc(size);
193
      png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
538
 
194
 
Line 539... Line 195...
539
#  else
195
   return ret;
540
#    if defined(_MSC_VER) && defined(MAXSEG_64K)
196
}
541
   if (size != (unsigned long)size)
-
 
542
      ret = NULL;
197
#endif /* PNG_USER_MEM_SUPPORTED */
543
 
-
 
544
   else
-
 
Line 545... Line 198...
545
      ret = halloc(size, 1);
198
 
546
 
199
/* This function was added at libpng version 1.2.3.  The png_malloc_warn()
547
#    else
200
 * function will issue a png_warning and return NULL instead of issuing a
Line 548... Line 201...
548
   if (size != (size_t)size)
201
 * png_error, if it fails to allocate the requested memory.
549
      ret = NULL;
202
 */
550
 
203
PNG_FUNCTION(png_voidp,PNGAPI
551
   else
204
png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
552
      ret = malloc((size_t)size);
205
   PNG_ALLOCATED)
553
#    endif
-
 
554
#  endif
206
{
Line 555... Line -...
555
 
-
 
556
#  ifndef PNG_USER_MEM_SUPPORTED
-
 
557
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-
 
558
      png_error(png_ptr, "Out of Memory");
-
 
559
#  endif
-
 
560
 
-
 
561
   return (ret);
-
 
562
}
-
 
563
 
207
   if (png_ptr != NULL)
564
/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
-
 
565
 * without taking any action.
-
 
566
 */
-
 
567
void PNGAPI
-
 
568
png_free(png_structp png_ptr, png_voidp ptr)
-
 
569
{
-
 
570
   if (png_ptr == NULL || ptr == NULL)
-
 
571
      return;
-
 
572
 
-
 
573
#  ifdef PNG_USER_MEM_SUPPORTED
-
 
574
   if (png_ptr->free_fn != NULL)
-
 
575
   {
-
 
576
      (*(png_ptr->free_fn))(png_ptr, ptr);
-
 
577
      return;
-
 
578
   }
-
 
579
 
-
 
580
   else
-
 
581
      png_free_default(png_ptr, ptr);
-
 
582
}
-
 
583
 
-
 
584
void PNGAPI
-
 
585
png_free_default(png_structp png_ptr, png_voidp ptr)
-
 
586
{
-
 
587
   if (png_ptr == NULL || ptr == NULL)
-
 
588
      return;
208
   {
Line 589... Line -...
589
 
-
 
590
#  endif /* PNG_USER_MEM_SUPPORTED */
209
      png_voidp ret = png_malloc_base(png_ptr, size);
591
 
210
 
592
#  if defined(__TURBOC__) && !defined(__FLAT__)
211
      if (ret != NULL)
593
   farfree(ptr);
212
         return ret;
594
 
213
 
595
#  else
214
      png_warning(png_ptr, "Out of memory");
596
#    if defined(_MSC_VER) && defined(MAXSEG_64K)
215
   }
597
   hfree(ptr);
216
 
598
 
217
   return NULL;
599
#    else
218
}
600
   free(ptr);
219
 
Line 645... Line 264...
645
 * functions.  The application should free any memory associated with this
264
 * functions.  The application should free any memory associated with this
646
 * pointer before png_write_destroy and png_read_destroy are called.
265
 * pointer before png_write_destroy and png_read_destroy are called.
647
 */
266
 */
648
png_voidp PNGAPI
267
png_voidp PNGAPI
649
png_get_mem_ptr(png_const_structp png_ptr)
268
png_get_mem_ptr(png_const_structrp png_ptr)
650
{
269
{
651
   if (png_ptr == NULL)
270
   if (png_ptr == NULL)
652
      return (NULL);
271
      return NULL;
653
 
272
 
Line 654... Line 273...
654
   return ((png_voidp)png_ptr->mem_ptr);
273
   return png_ptr->mem_ptr;
655
}
274
}
656
#endif /* PNG_USER_MEM_SUPPORTED */
275
#endif /* PNG_USER_MEM_SUPPORTED */
657
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
276
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */