zutil.cc

Go to the documentation of this file.
00001 /* zutil.c -- target dependent utility functions for the compression library
00002  * Copyright (C) 1995-2003 Jean-loup Gailly.
00003  * For conditions of distribution and use, see copyright notice in zlib.h
00004  */
00005 
00006 /* @(#) $Id: zutil.cc,v 1.1 2005-05-12 21:04:53 duns Exp $ */
00007 
00008 #include "zutil.h"
00009 
00010 #ifndef NO_DUMMY_DECL
00011 struct internal_state      {int dummy;}; /* for buggy compilers */
00012 #endif
00013 
00014 #ifndef STDC
00015 extern void exit OF((int));
00016 #endif
00017 
00018 const char * const z_errmsg[10] = {
00019 "need dictionary",     /* Z_NEED_DICT       2  */
00020 "stream end",          /* Z_STREAM_END      1  */
00021 "",                    /* Z_OK              0  */
00022 "file error",          /* Z_ERRNO         (-1) */
00023 "stream error",        /* Z_STREAM_ERROR  (-2) */
00024 "data error",          /* Z_DATA_ERROR    (-3) */
00025 "insufficient memory", /* Z_MEM_ERROR     (-4) */
00026 "buffer error",        /* Z_BUF_ERROR     (-5) */
00027 "incompatible version",/* Z_VERSION_ERROR (-6) */
00028 ""};
00029 
00030 
00031 const char * ZEXPORT zlibVersion()
00032 {
00033     return ZLIB_VERSION;
00034 }
00035 
00036 uLong ZEXPORT zlibCompileFlags()
00037 {
00038     uLong flags;
00039 
00040     flags = 0;
00041     switch (sizeof(uInt)) {
00042     case 2:     break;
00043     case 4:     flags += 1;     break;
00044     case 8:     flags += 2;     break;
00045     default:    flags += 3;
00046     }
00047     switch (sizeof(uLong)) {
00048     case 2:     break;
00049     case 4:     flags += 1 << 2;        break;
00050     case 8:     flags += 2 << 2;        break;
00051     default:    flags += 3 << 2;
00052     }
00053     switch (sizeof(voidpf)) {
00054     case 2:     break;
00055     case 4:     flags += 1 << 4;        break;
00056     case 8:     flags += 2 << 4;        break;
00057     default:    flags += 3 << 4;
00058     }
00059     switch (sizeof(z_off_t)) {
00060     case 2:     break;
00061     case 4:     flags += 1 << 6;        break;
00062     case 8:     flags += 2 << 6;        break;
00063     default:    flags += 3 << 6;
00064     }
00065 #ifdef DEBUG
00066     flags += 1 << 8;
00067 #endif
00068 #if defined(ASMV) || defined(ASMINF)
00069     flags += 1 << 9;
00070 #endif
00071 #ifdef ZLIB_WINAPI
00072     flags += 1 << 10;
00073 #endif
00074 #ifdef BUILDFIXED
00075     flags += 1 << 12;
00076 #endif
00077 #ifdef DYNAMIC_CRC_TABLE
00078     flags += 1 << 13;
00079 #endif
00080 #ifdef NO_GZCOMPRESS
00081     flags += 1 << 16;
00082 #endif
00083 #ifdef NO_GZIP
00084     flags += 1 << 17;
00085 #endif
00086 #ifdef PKZIP_BUG_WORKAROUND
00087     flags += 1 << 20;
00088 #endif
00089 #ifdef FASTEST
00090     flags += 1 << 21;
00091 #endif
00092 #ifdef STDC
00093 #  ifdef NO_vsnprintf
00094         flags += 1 << 25;
00095 #    ifdef HAS_vsprintf_void
00096         flags += 1 << 26;
00097 #    endif
00098 #  else
00099 #    ifdef HAS_vsnprintf_void
00100         flags += 1 << 26;
00101 #    endif
00102 #  endif
00103 #else
00104         flags += 1 << 24;
00105 #  ifdef NO_snprintf
00106         flags += 1 << 25;
00107 #    ifdef HAS_sprintf_void
00108         flags += 1 << 26;
00109 #    endif
00110 #  else
00111 #    ifdef HAS_snprintf_void
00112         flags += 1 << 26;
00113 #    endif
00114 #  endif
00115 #endif
00116     return flags;
00117 }
00118 
00119 #ifdef DEBUG
00120 
00121 #  ifndef verbose
00122 #    define verbose 0
00123 #  endif
00124 int z_verbose = verbose;
00125 
00126 void z_error (char *m)
00127 {
00128     fprintf(stderr, "%s\n", m);
00129     exit(1);
00130 }
00131 #endif
00132 
00133 /* exported to allow conversion of error code to string for compress() and
00134  * uncompress()
00135  */
00136 const char * ZEXPORT zError(int err)
00137 {
00138     return ERR_MSG(err);
00139 }
00140 
00141 #if defined(_WIN32_WCE)
00142     /* does not exist on WCE */
00143     int errno = 0;
00144 #endif
00145 
00146 #ifndef HAVE_MEMCPY
00147 
00148 void zmemcpy(Bytef *dest, const Bytef *source, uInt len)
00149 {
00150     if (len == 0) return;
00151     do {
00152         *dest++ = *source++; /* ??? to be unrolled */
00153     } while (--len != 0);
00154 }
00155 
00156 int zmemcmp(const Bytef* s1, const Bytef* s2, uInt len)
00157 {
00158     uInt j;
00159 
00160     for (j = 0; j < len; j++) {
00161         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
00162     }
00163     return 0;
00164 }
00165 
00166 void zmemzero(Bytef *dest, uInt len)
00167 {
00168     if (len == 0) return;
00169     do {
00170         *dest++ = 0;  /* ??? to be unrolled */
00171     } while (--len != 0);
00172 }
00173 #endif
00174 
00175 
00176 #ifdef SYS16BIT
00177 
00178 #ifdef __TURBOC__
00179 /* Turbo C in 16-bit mode */
00180 
00181 #  define MY_ZCALLOC
00182 
00183 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
00184  * and farmalloc(64K) returns a pointer with an offset of 8, so we
00185  * must fix the pointer. Warning: the pointer must be put back to its
00186  * original form in order to free it, use zcfree().
00187  */
00188 
00189 #define MAX_PTR 10
00190 /* 10*64K = 640K */
00191 
00192 local int next_ptr = 0;
00193 
00194 typedef struct ptr_table_s {
00195     voidpf org_ptr;
00196     voidpf new_ptr;
00197 } ptr_table;
00198 
00199 local ptr_table table[MAX_PTR];
00200 /* This table is used to remember the original form of pointers
00201  * to large buffers (64K). Such pointers are normalized with a zero offset.
00202  * Since MSDOS is not a preemptive multitasking OS, this table is not
00203  * protected from concurrent access. This hack doesn't work anyway on
00204  * a protected system like OS/2. Use Microsoft C instead.
00205  */
00206 
00207 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
00208 {
00209     voidpf buf = opaque; /* just to make some compilers happy */
00210     ulg bsize = (ulg)items*size;
00211 
00212     /* If we allocate less than 65520 bytes, we assume that farmalloc
00213      * will return a usable pointer which doesn't have to be normalized.
00214      */
00215     if (bsize < 65520L) {
00216         buf = farmalloc(bsize);
00217         if (*(ush*)&buf != 0) return buf;
00218     } else {
00219         buf = farmalloc(bsize + 16L);
00220     }
00221     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
00222     table[next_ptr].org_ptr = buf;
00223 
00224     /* Normalize the pointer to seg:0 */
00225     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
00226     *(ush*)&buf = 0;
00227     table[next_ptr++].new_ptr = buf;
00228     return buf;
00229 }
00230 
00231 void  zcfree (voidpf opaque, voidpf ptr)
00232 {
00233     int n;
00234     if (*(ush*)&ptr != 0) { /* object < 64K */
00235         farfree(ptr);
00236         return;
00237     }
00238     /* Find the original pointer */
00239     for (n = 0; n < next_ptr; n++) {
00240         if (ptr != table[n].new_ptr) continue;
00241 
00242         farfree(table[n].org_ptr);
00243         while (++n < next_ptr) {
00244             table[n-1] = table[n];
00245         }
00246         next_ptr--;
00247         return;
00248     }
00249     ptr = opaque; /* just to make some compilers happy */
00250     Assert(0, "zcfree: ptr not found");
00251 }
00252 
00253 #endif /* __TURBOC__ */
00254 
00255 
00256 #ifdef M_I86
00257 /* Microsoft C in 16-bit mode */
00258 
00259 #  define MY_ZCALLOC
00260 
00261 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
00262 #  define _halloc  halloc
00263 #  define _hfree   hfree
00264 #endif
00265 
00266 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
00267 {
00268     if (opaque) opaque = 0; /* to make compiler happy */
00269     return _halloc((long)items, size);
00270 }
00271 
00272 void  zcfree (voidpf opaque, voidpf ptr)
00273 {
00274     if (opaque) opaque = 0; /* to make compiler happy */
00275     _hfree(ptr);
00276 }
00277 
00278 #endif /* M_I86 */
00279 
00280 #endif /* SYS16BIT */
00281 
00282 
00283 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
00284 
00285 #ifndef STDC
00286 extern voidp  malloc OF((uInt size));
00287 extern voidp  calloc OF((uInt items, uInt size));
00288 extern void   free   OF((voidpf ptr));
00289 #endif
00290 
00291 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
00292 {
00293     if (opaque) items += size - size; /* make compiler happy */
00294     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
00295                               (voidpf)calloc(items, size);
00296 }
00297 
00298 void  zcfree (voidpf opaque, voidpf ptr)
00299 {
00300     free(ptr);
00301     if (opaque) return; /* make compiler happy */
00302 }
00303 
00304 #endif /* MY_ZCALLOC */

Generated on Mon May 27 17:50:37 2013 for Geant4 by  doxygen 1.4.7