Back to index

tetex-bin  3.0
zutil.c
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$ */
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 (m)
00127     char *m;
00128 {
00129     fprintf(stderr, "%s\n", m);
00130     exit(1);
00131 }
00132 #endif
00133 
00134 /* exported to allow conversion of error code to string for compress() and
00135  * uncompress()
00136  */
00137 const char * ZEXPORT zError(err)
00138     int err;
00139 {
00140     return ERR_MSG(err);
00141 }
00142 
00143 #if defined(_WIN32_WCE)
00144     /* does not exist on WCE */
00145     int errno = 0;
00146 #endif
00147 
00148 #ifndef HAVE_MEMCPY
00149 
00150 void zmemcpy(dest, source, len)
00151     Bytef* dest;
00152     const Bytef* source;
00153     uInt  len;
00154 {
00155     if (len == 0) return;
00156     do {
00157         *dest++ = *source++; /* ??? to be unrolled */
00158     } while (--len != 0);
00159 }
00160 
00161 int zmemcmp(s1, s2, len)
00162     const Bytef* s1;
00163     const Bytef* s2;
00164     uInt  len;
00165 {
00166     uInt j;
00167 
00168     for (j = 0; j < len; j++) {
00169         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
00170     }
00171     return 0;
00172 }
00173 
00174 void zmemzero(dest, len)
00175     Bytef* dest;
00176     uInt  len;
00177 {
00178     if (len == 0) return;
00179     do {
00180         *dest++ = 0;  /* ??? to be unrolled */
00181     } while (--len != 0);
00182 }
00183 #endif
00184 
00185 
00186 #ifdef SYS16BIT
00187 
00188 #ifdef __TURBOC__
00189 /* Turbo C in 16-bit mode */
00190 
00191 #  define MY_ZCALLOC
00192 
00193 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
00194  * and farmalloc(64K) returns a pointer with an offset of 8, so we
00195  * must fix the pointer. Warning: the pointer must be put back to its
00196  * original form in order to free it, use zcfree().
00197  */
00198 
00199 #define MAX_PTR 10
00200 /* 10*64K = 640K */
00201 
00202 local int next_ptr = 0;
00203 
00204 typedef struct ptr_table_s {
00205     voidpf org_ptr;
00206     voidpf new_ptr;
00207 } ptr_table;
00208 
00209 local ptr_table table[MAX_PTR];
00210 /* This table is used to remember the original form of pointers
00211  * to large buffers (64K). Such pointers are normalized with a zero offset.
00212  * Since MSDOS is not a preemptive multitasking OS, this table is not
00213  * protected from concurrent access. This hack doesn't work anyway on
00214  * a protected system like OS/2. Use Microsoft C instead.
00215  */
00216 
00217 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
00218 {
00219     voidpf buf = opaque; /* just to make some compilers happy */
00220     ulg bsize = (ulg)items*size;
00221 
00222     /* If we allocate less than 65520 bytes, we assume that farmalloc
00223      * will return a usable pointer which doesn't have to be normalized.
00224      */
00225     if (bsize < 65520L) {
00226         buf = farmalloc(bsize);
00227         if (*(ush*)&buf != 0) return buf;
00228     } else {
00229         buf = farmalloc(bsize + 16L);
00230     }
00231     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
00232     table[next_ptr].org_ptr = buf;
00233 
00234     /* Normalize the pointer to seg:0 */
00235     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
00236     *(ush*)&buf = 0;
00237     table[next_ptr++].new_ptr = buf;
00238     return buf;
00239 }
00240 
00241 void  zcfree (voidpf opaque, voidpf ptr)
00242 {
00243     int n;
00244     if (*(ush*)&ptr != 0) { /* object < 64K */
00245         farfree(ptr);
00246         return;
00247     }
00248     /* Find the original pointer */
00249     for (n = 0; n < next_ptr; n++) {
00250         if (ptr != table[n].new_ptr) continue;
00251 
00252         farfree(table[n].org_ptr);
00253         while (++n < next_ptr) {
00254             table[n-1] = table[n];
00255         }
00256         next_ptr--;
00257         return;
00258     }
00259     ptr = opaque; /* just to make some compilers happy */
00260     Assert(0, "zcfree: ptr not found");
00261 }
00262 
00263 #endif /* __TURBOC__ */
00264 
00265 
00266 #ifdef M_I86
00267 /* Microsoft C in 16-bit mode */
00268 
00269 #  define MY_ZCALLOC
00270 
00271 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
00272 #  define _halloc  halloc
00273 #  define _hfree   hfree
00274 #endif
00275 
00276 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
00277 {
00278     if (opaque) opaque = 0; /* to make compiler happy */
00279     return _halloc((long)items, size);
00280 }
00281 
00282 void  zcfree (voidpf opaque, voidpf ptr)
00283 {
00284     if (opaque) opaque = 0; /* to make compiler happy */
00285     _hfree(ptr);
00286 }
00287 
00288 #endif /* M_I86 */
00289 
00290 #endif /* SYS16BIT */
00291 
00292 
00293 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
00294 
00295 #ifndef STDC
00296 extern voidp  malloc OF((uInt size));
00297 extern voidp  calloc OF((uInt items, uInt size));
00298 extern void   free   OF((voidpf ptr));
00299 #endif
00300 
00301 voidpf zcalloc (opaque, items, size)
00302     voidpf opaque;
00303     unsigned items;
00304     unsigned size;
00305 {
00306     if (opaque) items += size - size; /* make compiler happy */
00307     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
00308                               (voidpf)calloc(items, size);
00309 }
00310 
00311 void  zcfree (opaque, ptr)
00312     voidpf opaque;
00313     voidpf ptr;
00314 {
00315     free(ptr);
00316     if (opaque) return; /* make compiler happy */
00317 }
00318 
00319 #endif /* MY_ZCALLOC */