Home | History | Annotate | Line # | Download | only in zlib
zutil.c revision 1.2
      1  1.2  christos /*	$NetBSD: zutil.c,v 1.2 2006/01/16 17:02:29 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /* zutil.c -- target dependent utility functions for the compression library
      4  1.1  christos  * Copyright (C) 1995-2005 Jean-loup Gailly.
      5  1.1  christos  * For conditions of distribution and use, see copyright notice in zlib.h
      6  1.1  christos  */
      7  1.1  christos 
      8  1.1  christos /* @(#) Id */
      9  1.1  christos 
     10  1.1  christos #include "zutil.h"
     11  1.1  christos 
     12  1.1  christos #ifndef NO_DUMMY_DECL
     13  1.1  christos struct internal_state      {int dummy;}; /* for buggy compilers */
     14  1.1  christos #endif
     15  1.1  christos 
     16  1.1  christos const char * const z_errmsg[10] = {
     17  1.1  christos "need dictionary",     /* Z_NEED_DICT       2  */
     18  1.1  christos "stream end",          /* Z_STREAM_END      1  */
     19  1.1  christos "",                    /* Z_OK              0  */
     20  1.1  christos "file error",          /* Z_ERRNO         (-1) */
     21  1.1  christos "stream error",        /* Z_STREAM_ERROR  (-2) */
     22  1.1  christos "data error",          /* Z_DATA_ERROR    (-3) */
     23  1.1  christos "insufficient memory", /* Z_MEM_ERROR     (-4) */
     24  1.1  christos "buffer error",        /* Z_BUF_ERROR     (-5) */
     25  1.1  christos "incompatible version",/* Z_VERSION_ERROR (-6) */
     26  1.1  christos ""};
     27  1.1  christos 
     28  1.1  christos 
     29  1.1  christos const char * ZEXPORT zlibVersion()
     30  1.1  christos {
     31  1.1  christos     return ZLIB_VERSION;
     32  1.1  christos }
     33  1.1  christos 
     34  1.1  christos uLong ZEXPORT zlibCompileFlags()
     35  1.1  christos {
     36  1.1  christos     uLong flags;
     37  1.1  christos 
     38  1.1  christos     flags = 0;
     39  1.1  christos     switch (sizeof(uInt)) {
     40  1.1  christos     case 2:     break;
     41  1.1  christos     case 4:     flags += 1;     break;
     42  1.1  christos     case 8:     flags += 2;     break;
     43  1.1  christos     default:    flags += 3;
     44  1.1  christos     }
     45  1.1  christos     switch (sizeof(uLong)) {
     46  1.1  christos     case 2:     break;
     47  1.1  christos     case 4:     flags += 1 << 2;        break;
     48  1.1  christos     case 8:     flags += 2 << 2;        break;
     49  1.1  christos     default:    flags += 3 << 2;
     50  1.1  christos     }
     51  1.1  christos     switch (sizeof(voidpf)) {
     52  1.1  christos     case 2:     break;
     53  1.1  christos     case 4:     flags += 1 << 4;        break;
     54  1.1  christos     case 8:     flags += 2 << 4;        break;
     55  1.1  christos     default:    flags += 3 << 4;
     56  1.1  christos     }
     57  1.1  christos     switch (sizeof(z_off_t)) {
     58  1.1  christos     case 2:     break;
     59  1.1  christos     case 4:     flags += 1 << 6;        break;
     60  1.1  christos     case 8:     flags += 2 << 6;        break;
     61  1.1  christos     default:    flags += 3 << 6;
     62  1.1  christos     }
     63  1.2  christos #ifdef ZLIB_DEBUG
     64  1.1  christos     flags += 1 << 8;
     65  1.1  christos #endif
     66  1.1  christos #if defined(ASMV) || defined(ASMINF)
     67  1.1  christos     flags += 1 << 9;
     68  1.1  christos #endif
     69  1.1  christos #ifdef ZLIB_WINAPI
     70  1.1  christos     flags += 1 << 10;
     71  1.1  christos #endif
     72  1.1  christos #ifdef BUILDFIXED
     73  1.1  christos     flags += 1 << 12;
     74  1.1  christos #endif
     75  1.1  christos #ifdef DYNAMIC_CRC_TABLE
     76  1.1  christos     flags += 1 << 13;
     77  1.1  christos #endif
     78  1.1  christos #ifdef NO_GZCOMPRESS
     79  1.1  christos     flags += 1L << 16;
     80  1.1  christos #endif
     81  1.1  christos #ifdef NO_GZIP
     82  1.1  christos     flags += 1L << 17;
     83  1.1  christos #endif
     84  1.1  christos #ifdef PKZIP_BUG_WORKAROUND
     85  1.1  christos     flags += 1L << 20;
     86  1.1  christos #endif
     87  1.1  christos #ifdef FASTEST
     88  1.1  christos     flags += 1L << 21;
     89  1.1  christos #endif
     90  1.1  christos #ifdef STDC
     91  1.1  christos #  ifdef NO_vsnprintf
     92  1.1  christos         flags += 1L << 25;
     93  1.1  christos #    ifdef HAS_vsprintf_void
     94  1.1  christos         flags += 1L << 26;
     95  1.1  christos #    endif
     96  1.1  christos #  else
     97  1.1  christos #    ifdef HAS_vsnprintf_void
     98  1.1  christos         flags += 1L << 26;
     99  1.1  christos #    endif
    100  1.1  christos #  endif
    101  1.1  christos #else
    102  1.1  christos         flags += 1L << 24;
    103  1.1  christos #  ifdef NO_snprintf
    104  1.1  christos         flags += 1L << 25;
    105  1.1  christos #    ifdef HAS_sprintf_void
    106  1.1  christos         flags += 1L << 26;
    107  1.1  christos #    endif
    108  1.1  christos #  else
    109  1.1  christos #    ifdef HAS_snprintf_void
    110  1.1  christos         flags += 1L << 26;
    111  1.1  christos #    endif
    112  1.1  christos #  endif
    113  1.1  christos #endif
    114  1.1  christos     return flags;
    115  1.1  christos }
    116  1.1  christos 
    117  1.2  christos #ifdef ZLIB_DEBUG
    118  1.1  christos 
    119  1.1  christos #  ifndef verbose
    120  1.1  christos #    define verbose 0
    121  1.1  christos #  endif
    122  1.1  christos int z_verbose = verbose;
    123  1.1  christos 
    124  1.1  christos void z_error (m)
    125  1.1  christos     char *m;
    126  1.1  christos {
    127  1.1  christos     fprintf(stderr, "%s\n", m);
    128  1.1  christos     exit(1);
    129  1.1  christos }
    130  1.1  christos #endif
    131  1.1  christos 
    132  1.1  christos /* exported to allow conversion of error code to string for compress() and
    133  1.1  christos  * uncompress()
    134  1.1  christos  */
    135  1.1  christos const char * ZEXPORT zError(err)
    136  1.1  christos     int err;
    137  1.1  christos {
    138  1.1  christos     return ERR_MSG(err);
    139  1.1  christos }
    140  1.1  christos 
    141  1.1  christos #if defined(_WIN32_WCE)
    142  1.1  christos     /* The Microsoft C Run-Time Library for Windows CE doesn't have
    143  1.1  christos      * errno.  We define it as a global variable to simplify porting.
    144  1.1  christos      * Its value is always 0 and should not be used.
    145  1.1  christos      */
    146  1.1  christos     int errno = 0;
    147  1.1  christos #endif
    148  1.1  christos 
    149  1.1  christos #ifndef HAVE_MEMCPY
    150  1.1  christos 
    151  1.1  christos void zmemcpy(dest, source, len)
    152  1.1  christos     Bytef* dest;
    153  1.1  christos     const Bytef* source;
    154  1.1  christos     uInt  len;
    155  1.1  christos {
    156  1.1  christos     if (len == 0) return;
    157  1.1  christos     do {
    158  1.1  christos         *dest++ = *source++; /* ??? to be unrolled */
    159  1.1  christos     } while (--len != 0);
    160  1.1  christos }
    161  1.1  christos 
    162  1.1  christos int zmemcmp(s1, s2, len)
    163  1.1  christos     const Bytef* s1;
    164  1.1  christos     const Bytef* s2;
    165  1.1  christos     uInt  len;
    166  1.1  christos {
    167  1.1  christos     uInt j;
    168  1.1  christos 
    169  1.1  christos     for (j = 0; j < len; j++) {
    170  1.1  christos         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
    171  1.1  christos     }
    172  1.1  christos     return 0;
    173  1.1  christos }
    174  1.1  christos 
    175  1.1  christos void zmemzero(dest, len)
    176  1.1  christos     Bytef* dest;
    177  1.1  christos     uInt  len;
    178  1.1  christos {
    179  1.1  christos     if (len == 0) return;
    180  1.1  christos     do {
    181  1.1  christos         *dest++ = 0;  /* ??? to be unrolled */
    182  1.1  christos     } while (--len != 0);
    183  1.1  christos }
    184  1.1  christos #endif
    185  1.1  christos 
    186  1.1  christos 
    187  1.1  christos #ifdef SYS16BIT
    188  1.1  christos 
    189  1.1  christos #ifdef __TURBOC__
    190  1.1  christos /* Turbo C in 16-bit mode */
    191  1.1  christos 
    192  1.1  christos #  define MY_ZCALLOC
    193  1.1  christos 
    194  1.1  christos /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
    195  1.1  christos  * and farmalloc(64K) returns a pointer with an offset of 8, so we
    196  1.1  christos  * must fix the pointer. Warning: the pointer must be put back to its
    197  1.1  christos  * original form in order to free it, use zcfree().
    198  1.1  christos  */
    199  1.1  christos 
    200  1.1  christos #define MAX_PTR 10
    201  1.1  christos /* 10*64K = 640K */
    202  1.1  christos 
    203  1.1  christos local int next_ptr = 0;
    204  1.1  christos 
    205  1.1  christos typedef struct ptr_table_s {
    206  1.1  christos     voidpf org_ptr;
    207  1.1  christos     voidpf new_ptr;
    208  1.1  christos } ptr_table;
    209  1.1  christos 
    210  1.1  christos local ptr_table table[MAX_PTR];
    211  1.1  christos /* This table is used to remember the original form of pointers
    212  1.1  christos  * to large buffers (64K). Such pointers are normalized with a zero offset.
    213  1.1  christos  * Since MSDOS is not a preemptive multitasking OS, this table is not
    214  1.1  christos  * protected from concurrent access. This hack doesn't work anyway on
    215  1.1  christos  * a protected system like OS/2. Use Microsoft C instead.
    216  1.1  christos  */
    217  1.1  christos 
    218  1.1  christos voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
    219  1.1  christos {
    220  1.1  christos     voidpf buf = opaque; /* just to make some compilers happy */
    221  1.1  christos     ulg bsize = (ulg)items*size;
    222  1.1  christos 
    223  1.1  christos     /* If we allocate less than 65520 bytes, we assume that farmalloc
    224  1.1  christos      * will return a usable pointer which doesn't have to be normalized.
    225  1.1  christos      */
    226  1.1  christos     if (bsize < 65520L) {
    227  1.1  christos         buf = farmalloc(bsize);
    228  1.1  christos         if (*(ush*)&buf != 0) return buf;
    229  1.1  christos     } else {
    230  1.1  christos         buf = farmalloc(bsize + 16L);
    231  1.1  christos     }
    232  1.1  christos     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
    233  1.1  christos     table[next_ptr].org_ptr = buf;
    234  1.1  christos 
    235  1.1  christos     /* Normalize the pointer to seg:0 */
    236  1.1  christos     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
    237  1.1  christos     *(ush*)&buf = 0;
    238  1.1  christos     table[next_ptr++].new_ptr = buf;
    239  1.1  christos     return buf;
    240  1.1  christos }
    241  1.1  christos 
    242  1.1  christos void  zcfree (voidpf opaque, voidpf ptr)
    243  1.1  christos {
    244  1.1  christos     int n;
    245  1.1  christos     if (*(ush*)&ptr != 0) { /* object < 64K */
    246  1.1  christos         farfree(ptr);
    247  1.1  christos         return;
    248  1.1  christos     }
    249  1.1  christos     /* Find the original pointer */
    250  1.1  christos     for (n = 0; n < next_ptr; n++) {
    251  1.1  christos         if (ptr != table[n].new_ptr) continue;
    252  1.1  christos 
    253  1.1  christos         farfree(table[n].org_ptr);
    254  1.1  christos         while (++n < next_ptr) {
    255  1.1  christos             table[n-1] = table[n];
    256  1.1  christos         }
    257  1.1  christos         next_ptr--;
    258  1.1  christos         return;
    259  1.1  christos     }
    260  1.1  christos     ptr = opaque; /* just to make some compilers happy */
    261  1.1  christos     Assert(0, "zcfree: ptr not found");
    262  1.1  christos }
    263  1.1  christos 
    264  1.1  christos #endif /* __TURBOC__ */
    265  1.1  christos 
    266  1.1  christos 
    267  1.1  christos #ifdef M_I86
    268  1.1  christos /* Microsoft C in 16-bit mode */
    269  1.1  christos 
    270  1.1  christos #  define MY_ZCALLOC
    271  1.1  christos 
    272  1.1  christos #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
    273  1.1  christos #  define _halloc  halloc
    274  1.1  christos #  define _hfree   hfree
    275  1.1  christos #endif
    276  1.1  christos 
    277  1.1  christos voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
    278  1.1  christos {
    279  1.1  christos     if (opaque) opaque = 0; /* to make compiler happy */
    280  1.1  christos     return _halloc((long)items, size);
    281  1.1  christos }
    282  1.1  christos 
    283  1.1  christos void  zcfree (voidpf opaque, voidpf ptr)
    284  1.1  christos {
    285  1.1  christos     if (opaque) opaque = 0; /* to make compiler happy */
    286  1.1  christos     _hfree(ptr);
    287  1.1  christos }
    288  1.1  christos 
    289  1.1  christos #endif /* M_I86 */
    290  1.1  christos 
    291  1.1  christos #endif /* SYS16BIT */
    292  1.1  christos 
    293  1.1  christos 
    294  1.1  christos #ifndef MY_ZCALLOC /* Any system without a special alloc function */
    295  1.1  christos 
    296  1.1  christos #ifndef STDC
    297  1.1  christos extern voidp  malloc OF((uInt size));
    298  1.1  christos extern voidp  calloc OF((uInt items, uInt size));
    299  1.1  christos extern void   free   OF((voidpf ptr));
    300  1.1  christos #endif
    301  1.1  christos 
    302  1.1  christos voidpf zcalloc (opaque, items, size)
    303  1.1  christos     voidpf opaque;
    304  1.1  christos     unsigned items;
    305  1.1  christos     unsigned size;
    306  1.1  christos {
    307  1.1  christos     if (opaque) items += size - size; /* make compiler happy */
    308  1.1  christos     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
    309  1.1  christos                               (voidpf)calloc(items, size);
    310  1.1  christos }
    311  1.1  christos 
    312  1.1  christos void  zcfree (opaque, ptr)
    313  1.1  christos     voidpf opaque;
    314  1.1  christos     voidpf ptr;
    315  1.1  christos {
    316  1.1  christos     free(ptr);
    317  1.1  christos     if (opaque) return; /* make compiler happy */
    318  1.1  christos }
    319  1.1  christos 
    320  1.1  christos #endif /* MY_ZCALLOC */
    321