Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4349 Serge 1
 
2
 *
3
 * Last changed in libpng 1.6.0 [February 14, 2013]
4
 * Copyright (c) 1998-2013 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
19
 
20
21
 
22
/* Free a png_struct */
23
void /* PRIVATE */
24
png_destroy_png_struct(png_structrp png_ptr)
25
{
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
 
36
         /* We may have a jmp_buf left to deallocate. */
37
         png_free_jmpbuf(&dummy_struct);
38
#     endif
39
   }
40
}
41
42
 
43
 * 64K.  However, zlib may allocate more then 64K if you don't tell
44
 * it not to.  See zconf.h and png.h for more information.  zlib does
45
 * need to allocate exactly 64K, so whatever you call here must
46
 * have the ability to do that.
47
 */
48
PNG_FUNCTION(png_voidp,PNGAPI
49
png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
50
{
51
   png_voidp ret;
52
53
 
54
55
 
56
      memset(ret, 0, size);
57
58
 
59
}
60
61
 
62
 * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.
63
 * Checking and error handling must happen outside this routine; it returns NULL
64
 * if the allocation cannot be done (for any reason.)
65
 */
66
PNG_FUNCTION(png_voidp /* PRIVATE */,
67
png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
68
   PNG_ALLOCATED)
69
{
70
   /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
71
    * allocators have also been removed in 1.6.0, so any 16-bit system now has
72
    * to implement a user memory handler.  This checks to be sure it isn't
73
    * called with big numbers.
74
    */
75
#ifdef PNG_USER_MEM_SUPPORTED
76
   PNG_UNUSED(png_ptr)
77
#endif
78
   if (size > 0 && size <= PNG_SIZE_MAX
79
#     ifdef PNG_MAX_MALLOC_64K
80
         && size <= 65536U
81
#     endif
82
      )
83
   {
84
#ifdef PNG_USER_MEM_SUPPORTED
85
      if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
86
         return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
87
88
 
89
#endif
90
         return malloc((size_t)size); /* checked for truncation above */
91
   }
92
93
 
94
      return NULL;
95
}
96
97
 
98
 * that arises because of the checks in png_realloc_array that are repeated in
99
 * png_malloc_array.
100
 */
101
static png_voidp
102
png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
103
   size_t element_size)
104
{
105
   png_alloc_size_t req = nelements; /* known to be > 0 */
106
107
 
108
      return png_malloc_base(png_ptr, req * element_size);
109
110
 
111
   return NULL;
112
}
113
114
 
115
png_malloc_array,(png_const_structrp png_ptr, int nelements,
116
   size_t element_size),PNG_ALLOCATED)
117
{
118
   if (nelements <= 0 || element_size == 0)
119
      png_error(png_ptr, "internal error: array alloc");
120
121
 
122
}
123
124
 
125
png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
126
   int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
127
{
128
   /* These are internal errors: */
129
   if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
130
      (old_array == NULL && old_elements > 0))
131
      png_error(png_ptr, "internal error: array realloc");
132
133
 
134
    * check.)
135
    */
136
   if (add_elements <= INT_MAX - old_elements)
137
   {
138
      png_voidp new_array = png_malloc_array_checked(png_ptr,
139
         old_elements+add_elements, element_size);
140
141
 
142
      {
143
         /* Because png_malloc_array worked the size calculations below cannot
144
          * overflow.
145
          */
146
         if (old_elements > 0)
147
            memcpy(new_array, old_array, element_size*(unsigned)old_elements);
148
149
 
150
            element_size*(unsigned)add_elements);
151
152
 
153
      }
154
   }
155
156
 
157
}
158
159
 
160
 * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
161
 * function png_malloc_default is also provided.
162
 */
163
PNG_FUNCTION(png_voidp,PNGAPI
164
png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
165
{
166
   png_voidp ret;
167
168
 
169
      return NULL;
170
171
 
172
173
 
174
       png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
175
176
 
177
}
178
179
 
180
PNG_FUNCTION(png_voidp,PNGAPI
181
png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
182
   PNG_ALLOCATED PNG_DEPRECATED)
183
{
184
   png_voidp ret;
185
186
 
187
      return NULL;
188
189
 
190
   ret = png_malloc_base(NULL/*use malloc*/, size);
191
192
 
193
      png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
194
195
 
196
}
197
#endif /* PNG_USER_MEM_SUPPORTED */
198
199
 
200
 * function will issue a png_warning and return NULL instead of issuing a
201
 * png_error, if it fails to allocate the requested memory.
202
 */
203
PNG_FUNCTION(png_voidp,PNGAPI
204
png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
205
   PNG_ALLOCATED)
206
{
207
   if (png_ptr != NULL)
208
   {
209
      png_voidp ret = png_malloc_base(png_ptr, size);
210
211
 
212
         return ret;
213
214
 
215
   }
216
217
 
218
}
219
220
 
221
 * without taking any action.
222
 */
223
void PNGAPI
224
png_free(png_const_structrp png_ptr, png_voidp ptr)
225
{
226
   if (png_ptr == NULL || ptr == NULL)
227
      return;
228
229
 
230
   if (png_ptr->free_fn != NULL)
231
      png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
232
233
 
234
      png_free_default(png_ptr, ptr);
235
}
236
237
 
238
png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
239
{
240
   if (png_ptr == NULL || ptr == NULL)
241
      return;
242
#endif /* PNG_USER_MEM_SUPPORTED */
243
244
 
245
}
246
247
 
248
/* This function is called when the application wants to use another method
249
 * of allocating and freeing memory.
250
 */
251
void PNGAPI
252
png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
253
  malloc_fn, png_free_ptr free_fn)
254
{
255
   if (png_ptr != NULL)
256
   {
257
      png_ptr->mem_ptr = mem_ptr;
258
      png_ptr->malloc_fn = malloc_fn;
259
      png_ptr->free_fn = free_fn;
260
   }
261
}
262
263
 
264
 * functions.  The application should free any memory associated with this
265
 * pointer before png_write_destroy and png_read_destroy are called.
266
 */
267
png_voidp PNGAPI
268
png_get_mem_ptr(png_const_structrp png_ptr)
269
{
270
   if (png_ptr == NULL)
271
      return NULL;
272
273
 
274
}
275
#endif /* PNG_USER_MEM_SUPPORTED */
276
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
277