00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #define PNG_INTERNAL
00018 #include "png.h"
00019 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
00020
00021
00022 #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
00023
00024
00025
00026
00027 png_voidp
00028 png_create_struct(int type)
00029 {
00030 #ifdef PNG_USER_MEM_SUPPORTED
00031 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
00032 }
00033
00034
00035 png_voidp
00036 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
00037 {
00038 #endif
00039 png_size_t size;
00040 png_voidp struct_ptr;
00041
00042 if (type == PNG_STRUCT_INFO)
00043 size = png_sizeof(png_info);
00044 else if (type == PNG_STRUCT_PNG)
00045 size = png_sizeof(png_struct);
00046 else
00047 return (png_get_copyright(NULL));
00048
00049 #ifdef PNG_USER_MEM_SUPPORTED
00050 if (malloc_fn != NULL)
00051 {
00052 png_struct dummy_struct;
00053 png_structp png_ptr = &dummy_struct;
00054 png_ptr->mem_ptr=mem_ptr;
00055 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
00056 }
00057 else
00058 #endif
00059 struct_ptr = (png_voidp)farmalloc(size);
00060 if (struct_ptr != NULL)
00061 png_memset(struct_ptr, 0, size);
00062 return (struct_ptr);
00063 }
00064
00065
00066 void
00067 png_destroy_struct(png_voidp struct_ptr)
00068 {
00069 #ifdef PNG_USER_MEM_SUPPORTED
00070 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
00071 }
00072
00073
00074 void
00075 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
00076 png_voidp mem_ptr)
00077 {
00078 #endif
00079 if (struct_ptr != NULL)
00080 {
00081 #ifdef PNG_USER_MEM_SUPPORTED
00082 if (free_fn != NULL)
00083 {
00084 png_struct dummy_struct;
00085 png_structp png_ptr = &dummy_struct;
00086 png_ptr->mem_ptr=mem_ptr;
00087 (*(free_fn))(png_ptr, struct_ptr);
00088 return;
00089 }
00090 #endif
00091 farfree (struct_ptr);
00092 }
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 png_voidp PNGAPI
00116 png_malloc(png_structp png_ptr, png_uint_32 size)
00117 {
00118 png_voidp ret;
00119
00120 if (png_ptr == NULL || size == 0)
00121 return (NULL);
00122
00123 #ifdef PNG_USER_MEM_SUPPORTED
00124 if (png_ptr->malloc_fn != NULL)
00125 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
00126 else
00127 ret = (png_malloc_default(png_ptr, size));
00128 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00129 png_error(png_ptr, "Out of memory!");
00130 return (ret);
00131 }
00132
00133 png_voidp PNGAPI
00134 png_malloc_default(png_structp png_ptr, png_uint_32 size)
00135 {
00136 png_voidp ret;
00137 #endif
00138
00139 if (png_ptr == NULL || size == 0)
00140 return (NULL);
00141
00142 #ifdef PNG_MAX_MALLOC_64K
00143 if (size > (png_uint_32)65536L)
00144 {
00145 png_warning(png_ptr, "Cannot Allocate > 64K");
00146 ret = NULL;
00147 }
00148 else
00149 #endif
00150
00151 if (size != (size_t)size)
00152 ret = NULL;
00153 else if (size == (png_uint_32)65536L)
00154 {
00155 if (png_ptr->offset_table == NULL)
00156 {
00157
00158 ret = farmalloc(size);
00159 if (ret == NULL || ((png_size_t)ret & 0xffff))
00160 {
00161 int num_blocks;
00162 png_uint_32 total_size;
00163 png_bytep table;
00164 int i;
00165 png_byte huge * hptr;
00166
00167 if (ret != NULL)
00168 {
00169 farfree(ret);
00170 ret = NULL;
00171 }
00172
00173 if (png_ptr->zlib_window_bits > 14)
00174 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
00175 else
00176 num_blocks = 1;
00177 if (png_ptr->zlib_mem_level >= 7)
00178 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
00179 else
00180 num_blocks++;
00181
00182 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
00183
00184 table = farmalloc(total_size);
00185
00186 if (table == NULL)
00187 {
00188 #ifndef PNG_USER_MEM_SUPPORTED
00189 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00190 png_error(png_ptr, "Out Of Memory.");
00191 else
00192 png_warning(png_ptr, "Out Of Memory.");
00193 #endif
00194 return (NULL);
00195 }
00196
00197 if ((png_size_t)table & 0xfff0)
00198 {
00199 #ifndef PNG_USER_MEM_SUPPORTED
00200 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00201 png_error(png_ptr,
00202 "Farmalloc didn't return normalized pointer");
00203 else
00204 png_warning(png_ptr,
00205 "Farmalloc didn't return normalized pointer");
00206 #endif
00207 return (NULL);
00208 }
00209
00210 png_ptr->offset_table = table;
00211 png_ptr->offset_table_ptr = farmalloc(num_blocks *
00212 png_sizeof(png_bytep));
00213
00214 if (png_ptr->offset_table_ptr == NULL)
00215 {
00216 #ifndef PNG_USER_MEM_SUPPORTED
00217 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00218 png_error(png_ptr, "Out Of memory.");
00219 else
00220 png_warning(png_ptr, "Out Of memory.");
00221 #endif
00222 return (NULL);
00223 }
00224
00225 hptr = (png_byte huge *)table;
00226 if ((png_size_t)hptr & 0xf)
00227 {
00228 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
00229 hptr = hptr + 16L;
00230 }
00231 for (i = 0; i < num_blocks; i++)
00232 {
00233 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
00234 hptr = hptr + (png_uint_32)65536L;
00235 }
00236
00237 png_ptr->offset_table_number = num_blocks;
00238 png_ptr->offset_table_count = 0;
00239 png_ptr->offset_table_count_free = 0;
00240 }
00241 }
00242
00243 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
00244 {
00245 #ifndef PNG_USER_MEM_SUPPORTED
00246 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00247 png_error(png_ptr, "Out of Memory.");
00248 else
00249 png_warning(png_ptr, "Out of Memory.");
00250 #endif
00251 return (NULL);
00252 }
00253
00254 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
00255 }
00256 else
00257 ret = farmalloc(size);
00258
00259 #ifndef PNG_USER_MEM_SUPPORTED
00260 if (ret == NULL)
00261 {
00262 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00263 png_error(png_ptr, "Out of memory.");
00264 else
00265 png_warning(png_ptr, "Out of memory.");
00266 }
00267 #endif
00268
00269 return (ret);
00270 }
00271
00272
00273
00274
00275
00276 void PNGAPI
00277 png_free(png_structp png_ptr, png_voidp ptr)
00278 {
00279 if (png_ptr == NULL || ptr == NULL)
00280 return;
00281
00282 #ifdef PNG_USER_MEM_SUPPORTED
00283 if (png_ptr->free_fn != NULL)
00284 {
00285 (*(png_ptr->free_fn))(png_ptr, ptr);
00286 return;
00287 }
00288 else png_free_default(png_ptr, ptr);
00289 }
00290
00291 void PNGAPI
00292 png_free_default(png_structp png_ptr, png_voidp ptr)
00293 {
00294 #endif
00295
00296 if (png_ptr == NULL || ptr == NULL) return;
00297
00298 if (png_ptr->offset_table != NULL)
00299 {
00300 int i;
00301
00302 for (i = 0; i < png_ptr->offset_table_count; i++)
00303 {
00304 if (ptr == png_ptr->offset_table_ptr[i])
00305 {
00306 ptr = NULL;
00307 png_ptr->offset_table_count_free++;
00308 break;
00309 }
00310 }
00311 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
00312 {
00313 farfree(png_ptr->offset_table);
00314 farfree(png_ptr->offset_table_ptr);
00315 png_ptr->offset_table = NULL;
00316 png_ptr->offset_table_ptr = NULL;
00317 }
00318 }
00319
00320 if (ptr != NULL)
00321 {
00322 farfree(ptr);
00323 }
00324 }
00325
00326 #else
00327
00328
00329
00330
00331 png_voidp
00332 png_create_struct(int type)
00333 {
00334 #ifdef PNG_USER_MEM_SUPPORTED
00335 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
00336 }
00337
00338
00339
00340
00341 png_voidp
00342 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
00343 {
00344 #endif
00345 png_size_t size;
00346 png_voidp struct_ptr;
00347
00348 if (type == PNG_STRUCT_INFO)
00349 size = png_sizeof(png_info);
00350 else if (type == PNG_STRUCT_PNG)
00351 size = png_sizeof(png_struct);
00352 else
00353 return (NULL);
00354
00355 #ifdef PNG_USER_MEM_SUPPORTED
00356 if (malloc_fn != NULL)
00357 {
00358 png_struct dummy_struct;
00359 png_structp png_ptr = &dummy_struct;
00360 png_ptr->mem_ptr=mem_ptr;
00361 struct_ptr = (*(malloc_fn))(png_ptr, size);
00362 if (struct_ptr != NULL)
00363 png_memset(struct_ptr, 0, size);
00364 return (struct_ptr);
00365 }
00366 #endif
00367
00368 #if defined(__TURBOC__) && !defined(__FLAT__)
00369 struct_ptr = (png_voidp)farmalloc(size);
00370 #else
00371 # if defined(_MSC_VER) && defined(MAXSEG_64K)
00372 struct_ptr = (png_voidp)halloc(size, 1);
00373 # else
00374 struct_ptr = (png_voidp)malloc(size);
00375 # endif
00376 #endif
00377 if (struct_ptr != NULL)
00378 png_memset(struct_ptr, 0, size);
00379
00380 return (struct_ptr);
00381 }
00382
00383
00384
00385 void
00386 png_destroy_struct(png_voidp struct_ptr)
00387 {
00388 #ifdef PNG_USER_MEM_SUPPORTED
00389 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
00390 }
00391
00392
00393 void
00394 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
00395 png_voidp mem_ptr)
00396 {
00397 #endif
00398 if (struct_ptr != NULL)
00399 {
00400 #ifdef PNG_USER_MEM_SUPPORTED
00401 if (free_fn != NULL)
00402 {
00403 png_struct dummy_struct;
00404 png_structp png_ptr = &dummy_struct;
00405 png_ptr->mem_ptr=mem_ptr;
00406 (*(free_fn))(png_ptr, struct_ptr);
00407 return;
00408 }
00409 #endif
00410 #if defined(__TURBOC__) && !defined(__FLAT__)
00411 farfree(struct_ptr);
00412 #else
00413 # if defined(_MSC_VER) && defined(MAXSEG_64K)
00414 hfree(struct_ptr);
00415 # else
00416 free(struct_ptr);
00417 # endif
00418 #endif
00419 }
00420 }
00421
00422
00423
00424
00425
00426
00427
00428 png_voidp PNGAPI
00429 png_malloc(png_structp png_ptr, png_uint_32 size)
00430 {
00431 png_voidp ret;
00432
00433 #ifdef PNG_USER_MEM_SUPPORTED
00434 if (png_ptr == NULL || size == 0)
00435 return (NULL);
00436
00437 if (png_ptr->malloc_fn != NULL)
00438 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
00439 else
00440 ret = (png_malloc_default(png_ptr, size));
00441 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00442 png_error(png_ptr, "Out of Memory!");
00443 return (ret);
00444 }
00445
00446 png_voidp PNGAPI
00447 png_malloc_default(png_structp png_ptr, png_uint_32 size)
00448 {
00449 png_voidp ret;
00450 #endif
00451
00452 if (png_ptr == NULL || size == 0)
00453 return (NULL);
00454
00455 #ifdef PNG_MAX_MALLOC_64K
00456 if (size > (png_uint_32)65536L)
00457 {
00458 #ifndef PNG_USER_MEM_SUPPORTED
00459 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00460 png_error(png_ptr, "Cannot Allocate > 64K");
00461 else
00462 #endif
00463 return NULL;
00464 }
00465 #endif
00466
00467
00468 #if defined(__TURBOC__) && !defined(__FLAT__)
00469 if (size != (unsigned long)size)
00470 ret = NULL;
00471 else
00472 ret = farmalloc(size);
00473 #else
00474 # if defined(_MSC_VER) && defined(MAXSEG_64K)
00475 if (size != (unsigned long)size)
00476 ret = NULL;
00477 else
00478 ret = halloc(size, 1);
00479 # else
00480 if (size != (size_t)size)
00481 ret = NULL;
00482 else
00483 ret = malloc((size_t)size);
00484 # endif
00485 #endif
00486
00487 #ifndef PNG_USER_MEM_SUPPORTED
00488 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00489 png_error(png_ptr, "Out of Memory");
00490 #endif
00491
00492 return (ret);
00493 }
00494
00495
00496
00497 void PNGAPI
00498 png_free(png_structp png_ptr, png_voidp ptr)
00499 {
00500 if (png_ptr == NULL || ptr == NULL)
00501 return;
00502
00503 #ifdef PNG_USER_MEM_SUPPORTED
00504 if (png_ptr->free_fn != NULL)
00505 {
00506 (*(png_ptr->free_fn))(png_ptr, ptr);
00507 return;
00508 }
00509 else png_free_default(png_ptr, ptr);
00510 }
00511 void PNGAPI
00512 png_free_default(png_structp png_ptr, png_voidp ptr)
00513 {
00514 if (png_ptr == NULL || ptr == NULL)
00515 return;
00516
00517 #endif
00518
00519 #if defined(__TURBOC__) && !defined(__FLAT__)
00520 farfree(ptr);
00521 #else
00522 # if defined(_MSC_VER) && defined(MAXSEG_64K)
00523 hfree(ptr);
00524 # else
00525 free(ptr);
00526 # endif
00527 #endif
00528 }
00529
00530 #endif
00531
00532 #if defined(PNG_1_0_X)
00533 # define png_malloc_warn png_malloc
00534 #else
00535
00536
00537
00538
00539
00540 png_voidp PNGAPI
00541 png_malloc_warn(png_structp png_ptr, png_uint_32 size)
00542 {
00543 png_voidp ptr;
00544 png_uint_32 save_flags;
00545 if (png_ptr == NULL) return (NULL);
00546
00547 save_flags = png_ptr->flags;
00548 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
00549 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
00550 png_ptr->flags=save_flags;
00551 return(ptr);
00552 }
00553 #endif
00554
00555 png_voidp PNGAPI
00556 png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
00557 png_uint_32 length)
00558 {
00559 png_size_t size;
00560
00561 size = (png_size_t)length;
00562 if ((png_uint_32)size != length)
00563 png_error(png_ptr, "Overflow in png_memcpy_check.");
00564
00565 return(png_memcpy (s1, s2, size));
00566 }
00567
00568 png_voidp PNGAPI
00569 png_memset_check (png_structp png_ptr, png_voidp s1, int value,
00570 png_uint_32 length)
00571 {
00572 png_size_t size;
00573
00574 size = (png_size_t)length;
00575 if ((png_uint_32)size != length)
00576 png_error(png_ptr, "Overflow in png_memset_check.");
00577
00578 return (png_memset (s1, value, size));
00579
00580 }
00581
00582 #ifdef PNG_USER_MEM_SUPPORTED
00583
00584
00585
00586 void PNGAPI
00587 png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
00588 malloc_fn, png_free_ptr free_fn)
00589 {
00590 if (png_ptr != NULL)
00591 {
00592 png_ptr->mem_ptr = mem_ptr;
00593 png_ptr->malloc_fn = malloc_fn;
00594 png_ptr->free_fn = free_fn;
00595 }
00596 }
00597
00598
00599
00600
00601
00602 png_voidp PNGAPI
00603 png_get_mem_ptr(png_structp png_ptr)
00604 {
00605 if (png_ptr == NULL) return (NULL);
00606 return ((png_voidp)png_ptr->mem_ptr);
00607 }
00608 #endif
00609 #endif