opencv
1/* zutil.c -- target dependent utility functions for the compression library
2* Copyright (C) 1995-2017 Jean-loup Gailly
3* For conditions of distribution and use, see copyright notice in zlib.h
4*/
5
6#include "zbuild.h"7#include "zutil_p.h"8#include "zutil.h"9
10z_const char * const PREFIX(z_errmsg)[10] = {11(z_const char *)"need dictionary", /* Z_NEED_DICT 2 */12(z_const char *)"stream end", /* Z_STREAM_END 1 */13(z_const char *)"", /* Z_OK 0 */14(z_const char *)"file error", /* Z_ERRNO (-1) */15(z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */16(z_const char *)"data error", /* Z_DATA_ERROR (-3) */17(z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */18(z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */19(z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */20(z_const char *)""21};22
23const char PREFIX3(vstring)[] =24" zlib-ng 2.1.6";25
26#ifdef ZLIB_COMPAT27const char * Z_EXPORT zlibVersion(void) {28return ZLIB_VERSION;29}
30#else31const char * Z_EXPORT zlibng_version(void) {32return ZLIBNG_VERSION;33}
34#endif35
36unsigned long Z_EXPORT PREFIX(zlibCompileFlags)(void) {37unsigned long flags;38
39flags = 0;40switch ((int)(sizeof(unsigned int))) {41case 2: break;42case 4: flags += 1; break;43case 8: flags += 2; break;44default: flags += 3;45}46switch ((int)(sizeof(unsigned long))) {47case 2: break;48case 4: flags += 1 << 2; break;49case 8: flags += 2 << 2; break;50default: flags += 3 << 2;51}52switch ((int)(sizeof(void *))) {53case 2: break;54case 4: flags += 1 << 4; break;55case 8: flags += 2 << 4; break;56default: flags += 3 << 4;57}58switch ((int)(sizeof(z_off_t))) {59case 2: break;60case 4: flags += 1 << 6; break;61case 8: flags += 2 << 6; break;62default: flags += 3 << 6;63}64#ifdef ZLIB_DEBUG65flags += 1 << 8;66#endif67#ifdef ZLIB_WINAPI68flags += 1 << 10;69#endif70/* Bit 13 reserved for DYNAMIC_CRC_TABLE */71#ifdef NO_GZCOMPRESS72flags += 1L << 16;73#endif74#ifdef NO_GZIP75flags += 1L << 17;76#endif77#ifdef PKZIP_BUG_WORKAROUND78flags += 1L << 20;79#endif80return flags;81}
82
83#ifdef ZLIB_DEBUG84# include <stdlib.h>85# ifndef verbose86# define verbose 087# endif88int Z_INTERNAL z_verbose = verbose;89
90void Z_INTERNAL z_error(const char *m) {91fprintf(stderr, "%s\n", m);92exit(1);93}
94#endif95
96/* exported to allow conversion of error code to string for compress() and
97* uncompress()
98*/
99const char * Z_EXPORT PREFIX(zError)(int err) {100return ERR_MSG(err);101}
102
103void Z_INTERNAL *PREFIX(zcalloc)(void *opaque, unsigned items, unsigned size) {104Z_UNUSED(opaque);105return zng_alloc((size_t)items * (size_t)size);106}
107
108void Z_INTERNAL PREFIX(zcfree)(void *opaque, void *ptr) {109Z_UNUSED(opaque);110zng_free(ptr);111}
112
113/* Since we support custom memory allocators, some which might not align memory as we expect,
114* we have to ask for extra memory and return an aligned pointer. */
115void Z_INTERNAL *PREFIX3(alloc_aligned)(zng_calloc_func zalloc, void *opaque, unsigned items, unsigned size, unsigned align) {116uintptr_t return_ptr, original_ptr;117uint32_t alloc_size, align_diff;118void *ptr;119
120/* If no custom calloc function used then call zlib-ng's aligned calloc */121if (zalloc == PREFIX(zcalloc))122return PREFIX(zcalloc)(opaque, items, size);123
124/* Allocate enough memory for proper alignment and to store the original memory pointer */125alloc_size = sizeof(void *) + (items * size) + align;126ptr = zalloc(opaque, 1, alloc_size);127if (!ptr)128return NULL;129
130/* Calculate return pointer address with space enough to store original pointer */131align_diff = align - ((uintptr_t)ptr % align);132return_ptr = (uintptr_t)ptr + align_diff;133if (align_diff < sizeof(void *))134return_ptr += align;135
136/* Store the original pointer for free() */137original_ptr = return_ptr - sizeof(void *);138memcpy((void *)original_ptr, &ptr, sizeof(void *));139
140/* Return properly aligned pointer in allocation */141return (void *)return_ptr;142}
143
144void Z_INTERNAL PREFIX3(free_aligned)(zng_cfree_func zfree, void *opaque, void *ptr) {145/* If no custom cfree function used then call zlib-ng's aligned cfree */146if (zfree == PREFIX(zcfree)) {147PREFIX(zcfree)(opaque, ptr);148return;149}150if (!ptr)151return;152
153/* Calculate offset to original memory allocation pointer */154void *original_ptr = (void *)((uintptr_t)ptr - sizeof(void *));155void *free_ptr = *(void **)original_ptr;156
157/* Free original memory allocation */158zfree(opaque, free_ptr);159}
160