- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# zutil.c

    00001 /* zutil.c -- target dependent utility functions for the compression library
    00002  * Copyright (C) 1995-2002 Jean-loup Gailly.
    00003  * For conditions of distribution and use, see copyright notice in zlib.h
    00004  */
    00005
    00006 /* @(#) $Id: zutil.c,v 1.1 2003/11/09 01:37:56 gabest Exp $ */
    00007
    00008 #include "zutil.h"
    00009
    00010 struct internal_state      {int dummy;}; /* for buggy compilers */
    00011
    00012 #ifndef STDC
    00013 extern void exit OF((int));
    00014 #endif
    00015
    00016 const char *z_errmsg[10] = {
    00017 "need dictionary",     /* Z_NEED_DICT       2  */
    00018 "stream end",          /* Z_STREAM_END      1  */
    00019 "",                    /* Z_OK              0  */
    00020 "file error",          /* Z_ERRNO         (-1) */
    00021 "stream error",        /* Z_STREAM_ERROR  (-2) */
    00022 "data error",          /* Z_DATA_ERROR    (-3) */
    00023 "insufficient memory", /* Z_MEM_ERROR     (-4) */
    00024 "buffer error",        /* Z_BUF_ERROR     (-5) */
    00025 "incompatible version",/* Z_VERSION_ERROR (-6) */
    00026 ""};
    00027
    00028
    00029 const char * ZEXPORT zlibVersion()
    00030 {
    00031     return ZLIB_VERSION;
    00032 }
    00033
    00034 #ifdef DEBUG
    00035
    00036 #  ifndef verbose
    00037 #    define verbose 0
    00038 #  endif
    00039 int z_verbose = verbose;
    00040
    00041 void z_error (m)
    00042     char *m;
    00043 {
    00044     fprintf(stderr, "%s\n", m);
    00045     exit(1);
    00046 }
    00047 #endif
    00048
    00049 /* exported to allow conversion of error code to string for compress() and
    00050  * uncompress()
    00051  */
    00052 const char * ZEXPORT zError(err)
    00053     int err;
    00054 {
    00055     return ERR_MSG(err);
    00056 }
    00057
    00058
    00059 #ifndef HAVE_MEMCPY
    00060
    00061 void zmemcpy(dest, source, len)
    00062     Bytef* dest;
    00063     const Bytef* source;
    00064     uInt  len;
    00065 {
    00066     if (len == 0) return;
    00067     do {
    00068         *dest++ = *source++; /* ??? to be unrolled */
    00069     } while (--len != 0);
    00070 }
    00071
    00072 int zmemcmp(s1, s2, len)
    00073     const Bytef* s1;
    00074     const Bytef* s2;
    00075     uInt  len;
    00076 {
    00077     uInt j;
    00078
    00079     for (j = 0; j < len; j++) {
    00080         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
    00081     }
    00082     return 0;
    00083 }
    00084
    00085 void zmemzero(dest, len)
    00086     Bytef* dest;
    00087     uInt  len;
    00088 {
    00089     if (len == 0) return;
    00090     do {
    00091         *dest++ = 0;  /* ??? to be unrolled */
    00092     } while (--len != 0);
    00093 }
    00094 #endif
    00095
    00096 #ifdef __TURBOC__
    00097 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
    00098 /* Small and medium model in Turbo C are for now limited to near allocation
    00099  * with reduced MAX_WBITS and MAX_MEM_LEVEL
    00100  */
    00101 #  define MY_ZCALLOC
    00102
    00103 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
    00104  * and farmalloc(64K) returns a pointer with an offset of 8, so we
    00105  * must fix the pointer. Warning: the pointer must be put back to its
    00106  * original form in order to free it, use zcfree().
    00107  */
    00108
    00109 #define MAX_PTR 10
    00110 /* 10*64K = 640K */
    00111
    00112 local int next_ptr = 0;
    00113
    00114 typedef struct ptr_table_s {
    00115     voidpf org_ptr;
    00116     voidpf new_ptr;
    00117 } ptr_table;
    00118
    00119 local ptr_table table[MAX_PTR];
    00120 /* This table is used to remember the original form of pointers
    00121  * to large buffers (64K). Such pointers are normalized with a zero offset.
    00122  * Since MSDOS is not a preemptive multitasking OS, this table is not
    00123  * protected from concurrent access. This hack doesn't work anyway on
    00124  * a protected system like OS/2. Use Microsoft C instead.
    00125  */
    00126
    00127 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
    00128 {
    00129     voidpf buf = opaque; /* just to make some compilers happy */
    00130     ulg bsize = (ulg)items*size;
    00131
    00132     /* If we allocate less than 65520 bytes, we assume that farmalloc
    00133      * will return a usable pointer which doesn't have to be normalized.
    00134      */
    00135     if (bsize < 65520L) {
    00136         buf = farmalloc(bsize);
    00137         if (*(ush*)&buf != 0) return buf;
    00138     } else {
    00139         buf = farmalloc(bsize + 16L);
    00140     }
    00141     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
    00142     table[next_ptr].org_ptr = buf;
    00143
    00144     /* Normalize the pointer to seg:0 */
    00145     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
    00146     *(ush*)&buf = 0;
    00147     table[next_ptr++].new_ptr = buf;
    00148     return buf;
    00149 }
    00150
    00151 void  zcfree (voidpf opaque, voidpf ptr)
    00152 {
    00153     int n;
    00154     if (*(ush*)&ptr != 0) { /* object < 64K */
    00155         farfree(ptr);
    00156         return;
    00157     }
    00158     /* Find the original pointer */
    00159     for (n = 0; n < next_ptr; n++) {
    00160         if (ptr != table[n].new_ptr) continue;
    00161
    00162         farfree(table[n].org_ptr);
    00163         while (++n < next_ptr) {
    00164             table[n-1] = table[n];
    00165         }
    00166         next_ptr--;
    00167         return;
    00168     }
    00169     ptr = opaque; /* just to make some compilers happy */
    00170     Assert(0, "zcfree: ptr not found");
    00171 }
    00172 #endif
    00173 #endif /* __TURBOC__ */
    00174
    00175
    00176 #if defined(M_I86) && !defined(__32BIT__)
    00177 /* Microsoft C in 16-bit mode */
    00178
    00179 #  define MY_ZCALLOC
    00180
    00181 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
    00182 #  define _halloc  halloc
    00183 #  define _hfree   hfree
    00184 #endif
    00185
    00186 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
    00187 {
    00188     if (opaque) opaque = 0; /* to make compiler happy */
    00189     return _halloc((long)items, size);
    00190 }
    00191
    00192 void  zcfree (voidpf opaque, voidpf ptr)
    00193 {
    00194     if (opaque) opaque = 0; /* to make compiler happy */
    00195     _hfree(ptr);
    00196 }
    00197
    00198 #endif /* MSC */
    00199
    00200
    00201 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
    00202
    00203 #ifndef STDC
    00204 extern voidp  calloc OF((uInt items, uInt size));
    00205 extern void   free   OF((voidpf ptr));
    00206 #endif
    00207
    00208 voidpf zcalloc (opaque, items, size)
    00209     voidpf opaque;
    00210     unsigned items;
    00211     unsigned size;
    00212 {
    00213     if (opaque) items += size - size; /* make compiler happy */
    00214     return (voidpf)calloc(items, size);
    00215 }
    00216
    00217 void  zcfree (opaque, ptr)
    00218     voidpf opaque;
    00219     voidpf ptr;
    00220 {
    00221     free(ptr);
    00222     if (opaque) return; /* make compiler happy */
    00223 }
    00224
    00225 #endif /* MY_ZCALLOC */