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