Back to index

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