Home | History | Annotate | Line # | Download | only in zlib
zutil.c revision 1.2.76.1
      1  1.2.76.1  pgoyette /*	$NetBSD: zutil.c,v 1.2.76.1 2017/03/20 06:51:33 pgoyette Exp $	*/
      2       1.1  christos 
      3       1.1  christos /* zutil.c -- target dependent utility functions for the compression library
      4  1.2.76.1  pgoyette  * Copyright (C) 1995-2005, 2010, 2011, 2012, 2016 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.2.76.1  pgoyette /* @(#) $Id: zutil.c,v 1.2.76.1 2017/03/20 06:51:33 pgoyette Exp $ */
      9       1.1  christos 
     10       1.1  christos #include "zutil.h"
     11  1.2.76.1  pgoyette #ifndef Z_SOLO
     12  1.2.76.1  pgoyette #  include "gzguts.h"
     13       1.1  christos #endif
     14       1.1  christos 
     15  1.2.76.1  pgoyette z_const char * const z_errmsg[10] = {
     16  1.2.76.1  pgoyette     __UNCONST("need dictionary"),     /* Z_NEED_DICT       2  */
     17  1.2.76.1  pgoyette     __UNCONST("stream end"),          /* Z_STREAM_END      1  */
     18  1.2.76.1  pgoyette     __UNCONST(""),                    /* Z_OK              0  */
     19  1.2.76.1  pgoyette     __UNCONST("file error"),          /* Z_ERRNO         (-1) */
     20  1.2.76.1  pgoyette     __UNCONST("stream error"),        /* Z_STREAM_ERROR  (-2) */
     21  1.2.76.1  pgoyette     __UNCONST("data error"),          /* Z_DATA_ERROR    (-3) */
     22  1.2.76.1  pgoyette     __UNCONST("insufficient memory"), /* Z_MEM_ERROR     (-4) */
     23  1.2.76.1  pgoyette     __UNCONST("buffer error"),        /* Z_BUF_ERROR     (-5) */
     24  1.2.76.1  pgoyette     __UNCONST("incompatible version"),/* Z_VERSION_ERROR (-6) */
     25  1.2.76.1  pgoyette     __UNCONST(""),
     26  1.2.76.1  pgoyette };
     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.2.76.1  pgoyette     switch ((int)(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.2.76.1  pgoyette     switch ((int)(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.2.76.1  pgoyette     switch ((int)(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.2.76.1  pgoyette     switch ((int)(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.2.76.1  pgoyette #if defined(STDC) || defined(Z_HAVE_STDARG_H)
     91       1.1  christos #  ifdef NO_vsnprintf
     92  1.2.76.1  pgoyette     flags += 1L << 25;
     93       1.1  christos #    ifdef HAS_vsprintf_void
     94  1.2.76.1  pgoyette     flags += 1L << 26;
     95       1.1  christos #    endif
     96       1.1  christos #  else
     97       1.1  christos #    ifdef HAS_vsnprintf_void
     98  1.2.76.1  pgoyette     flags += 1L << 26;
     99       1.1  christos #    endif
    100       1.1  christos #  endif
    101       1.1  christos #else
    102  1.2.76.1  pgoyette     flags += 1L << 24;
    103       1.1  christos #  ifdef NO_snprintf
    104  1.2.76.1  pgoyette     flags += 1L << 25;
    105       1.1  christos #    ifdef HAS_sprintf_void
    106  1.2.76.1  pgoyette     flags += 1L << 26;
    107       1.1  christos #    endif
    108       1.1  christos #  else
    109       1.1  christos #    ifdef HAS_snprintf_void
    110  1.2.76.1  pgoyette     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.2.76.1  pgoyette #include <stdlib.h>
    119       1.1  christos #  ifndef verbose
    120       1.1  christos #    define verbose 0
    121       1.1  christos #  endif
    122  1.2.76.1  pgoyette int ZLIB_INTERNAL z_verbose = verbose;
    123       1.1  christos 
    124  1.2.76.1  pgoyette void ZLIB_INTERNAL 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.2.76.1  pgoyette void ZLIB_INTERNAL 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.2.76.1  pgoyette int ZLIB_INTERNAL 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.2.76.1  pgoyette void ZLIB_INTERNAL 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.2.76.1  pgoyette #ifndef Z_SOLO
    187       1.1  christos 
    188       1.1  christos #ifdef SYS16BIT
    189       1.1  christos 
    190       1.1  christos #ifdef __TURBOC__
    191       1.1  christos /* Turbo C in 16-bit mode */
    192       1.1  christos 
    193       1.1  christos #  define MY_ZCALLOC
    194       1.1  christos 
    195       1.1  christos /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
    196       1.1  christos  * and farmalloc(64K) returns a pointer with an offset of 8, so we
    197       1.1  christos  * must fix the pointer. Warning: the pointer must be put back to its
    198       1.1  christos  * original form in order to free it, use zcfree().
    199       1.1  christos  */
    200       1.1  christos 
    201       1.1  christos #define MAX_PTR 10
    202       1.1  christos /* 10*64K = 640K */
    203       1.1  christos 
    204       1.1  christos local int next_ptr = 0;
    205       1.1  christos 
    206       1.1  christos typedef struct ptr_table_s {
    207       1.1  christos     voidpf org_ptr;
    208       1.1  christos     voidpf new_ptr;
    209       1.1  christos } ptr_table;
    210       1.1  christos 
    211       1.1  christos local ptr_table table[MAX_PTR];
    212       1.1  christos /* This table is used to remember the original form of pointers
    213       1.1  christos  * to large buffers (64K). Such pointers are normalized with a zero offset.
    214       1.1  christos  * Since MSDOS is not a preemptive multitasking OS, this table is not
    215       1.1  christos  * protected from concurrent access. This hack doesn't work anyway on
    216       1.1  christos  * a protected system like OS/2. Use Microsoft C instead.
    217       1.1  christos  */
    218       1.1  christos 
    219  1.2.76.1  pgoyette voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
    220       1.1  christos {
    221  1.2.76.1  pgoyette     voidpf buf;
    222       1.1  christos     ulg bsize = (ulg)items*size;
    223       1.1  christos 
    224  1.2.76.1  pgoyette     (void)opaque;
    225  1.2.76.1  pgoyette 
    226       1.1  christos     /* If we allocate less than 65520 bytes, we assume that farmalloc
    227       1.1  christos      * will return a usable pointer which doesn't have to be normalized.
    228       1.1  christos      */
    229       1.1  christos     if (bsize < 65520L) {
    230       1.1  christos         buf = farmalloc(bsize);
    231       1.1  christos         if (*(ush*)&buf != 0) return buf;
    232       1.1  christos     } else {
    233       1.1  christos         buf = farmalloc(bsize + 16L);
    234       1.1  christos     }
    235       1.1  christos     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
    236       1.1  christos     table[next_ptr].org_ptr = buf;
    237       1.1  christos 
    238       1.1  christos     /* Normalize the pointer to seg:0 */
    239       1.1  christos     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
    240       1.1  christos     *(ush*)&buf = 0;
    241       1.1  christos     table[next_ptr++].new_ptr = buf;
    242       1.1  christos     return buf;
    243       1.1  christos }
    244       1.1  christos 
    245  1.2.76.1  pgoyette void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
    246       1.1  christos {
    247       1.1  christos     int n;
    248  1.2.76.1  pgoyette 
    249  1.2.76.1  pgoyette     (void)opaque;
    250  1.2.76.1  pgoyette 
    251       1.1  christos     if (*(ush*)&ptr != 0) { /* object < 64K */
    252       1.1  christos         farfree(ptr);
    253       1.1  christos         return;
    254       1.1  christos     }
    255       1.1  christos     /* Find the original pointer */
    256       1.1  christos     for (n = 0; n < next_ptr; n++) {
    257       1.1  christos         if (ptr != table[n].new_ptr) continue;
    258       1.1  christos 
    259       1.1  christos         farfree(table[n].org_ptr);
    260       1.1  christos         while (++n < next_ptr) {
    261       1.1  christos             table[n-1] = table[n];
    262       1.1  christos         }
    263       1.1  christos         next_ptr--;
    264       1.1  christos         return;
    265       1.1  christos     }
    266       1.1  christos     Assert(0, "zcfree: ptr not found");
    267       1.1  christos }
    268       1.1  christos 
    269       1.1  christos #endif /* __TURBOC__ */
    270       1.1  christos 
    271       1.1  christos 
    272       1.1  christos #ifdef M_I86
    273       1.1  christos /* Microsoft C in 16-bit mode */
    274       1.1  christos 
    275       1.1  christos #  define MY_ZCALLOC
    276       1.1  christos 
    277       1.1  christos #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
    278       1.1  christos #  define _halloc  halloc
    279       1.1  christos #  define _hfree   hfree
    280       1.1  christos #endif
    281       1.1  christos 
    282  1.2.76.1  pgoyette voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
    283       1.1  christos {
    284  1.2.76.1  pgoyette     (void)opaque;
    285       1.1  christos     return _halloc((long)items, size);
    286       1.1  christos }
    287       1.1  christos 
    288  1.2.76.1  pgoyette void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
    289       1.1  christos {
    290  1.2.76.1  pgoyette     (void)opaque;
    291       1.1  christos     _hfree(ptr);
    292       1.1  christos }
    293       1.1  christos 
    294       1.1  christos #endif /* M_I86 */
    295       1.1  christos 
    296       1.1  christos #endif /* SYS16BIT */
    297       1.1  christos 
    298       1.1  christos 
    299       1.1  christos #ifndef MY_ZCALLOC /* Any system without a special alloc function */
    300       1.1  christos 
    301       1.1  christos #ifndef STDC
    302       1.1  christos extern voidp  malloc OF((uInt size));
    303       1.1  christos extern voidp  calloc OF((uInt items, uInt size));
    304       1.1  christos extern void   free   OF((voidpf ptr));
    305       1.1  christos #endif
    306       1.1  christos 
    307  1.2.76.1  pgoyette voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
    308       1.1  christos     voidpf opaque;
    309       1.1  christos     unsigned items;
    310       1.1  christos     unsigned size;
    311       1.1  christos {
    312  1.2.76.1  pgoyette     (void)opaque;
    313       1.1  christos     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
    314       1.1  christos                               (voidpf)calloc(items, size);
    315       1.1  christos }
    316       1.1  christos 
    317  1.2.76.1  pgoyette void ZLIB_INTERNAL zcfree (opaque, ptr)
    318       1.1  christos     voidpf opaque;
    319       1.1  christos     voidpf ptr;
    320       1.1  christos {
    321  1.2.76.1  pgoyette     (void)opaque;
    322       1.1  christos     free(ptr);
    323       1.1  christos }
    324       1.1  christos 
    325       1.1  christos #endif /* MY_ZCALLOC */
    326  1.2.76.1  pgoyette 
    327  1.2.76.1  pgoyette #endif /* !Z_SOLO */
    328