Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:11

0001 /* zutil.c -- target dependent utility functions for the compression library
0002  * Copyright (C) 1995-2017 Jean-loup Gailly
0003  * For conditions of distribution and use, see copyright notice in zlib.h
0004  */
0005 
0006 /* @(#) $Id$ */
0007 
0008 #include "zutil.h"
0009 #ifndef Z_SOLO
0010 #  include "gzguts.h"
0011 #endif
0012 
0013 z_const char * const z_errmsg[10] = {
0014     (z_const char *)"need dictionary",     /* Z_NEED_DICT       2  */
0015     (z_const char *)"stream end",          /* Z_STREAM_END      1  */
0016     (z_const char *)"",                    /* Z_OK              0  */
0017     (z_const char *)"file error",          /* Z_ERRNO         (-1) */
0018     (z_const char *)"stream error",        /* Z_STREAM_ERROR  (-2) */
0019     (z_const char *)"data error",          /* Z_DATA_ERROR    (-3) */
0020     (z_const char *)"insufficient memory", /* Z_MEM_ERROR     (-4) */
0021     (z_const char *)"buffer error",        /* Z_BUF_ERROR     (-5) */
0022     (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
0023     (z_const char *)""
0024 };
0025 
0026 
0027 const char * ZEXPORT zlibVersion()
0028 {
0029     return ZLIB_VERSION;
0030 }
0031 
0032 uLong ZEXPORT zlibCompileFlags()
0033 {
0034     uLong flags;
0035 
0036     flags = 0;
0037     switch ((int)(sizeof(uInt))) {
0038     case 2:     break;
0039     case 4:     flags += 1;     break;
0040     case 8:     flags += 2;     break;
0041     default:    flags += 3;
0042     }
0043     switch ((int)(sizeof(uLong))) {
0044     case 2:     break;
0045     case 4:     flags += 1 << 2;        break;
0046     case 8:     flags += 2 << 2;        break;
0047     default:    flags += 3 << 2;
0048     }
0049     switch ((int)(sizeof(voidpf))) {
0050     case 2:     break;
0051     case 4:     flags += 1 << 4;        break;
0052     case 8:     flags += 2 << 4;        break;
0053     default:    flags += 3 << 4;
0054     }
0055     switch ((int)(sizeof(z_off_t))) {
0056     case 2:     break;
0057     case 4:     flags += 1 << 6;        break;
0058     case 8:     flags += 2 << 6;        break;
0059     default:    flags += 3 << 6;
0060     }
0061 #ifdef ZLIB_DEBUG
0062     flags += 1 << 8;
0063 #endif
0064     /*
0065 #if defined(ASMV) || defined(ASMINF)
0066     flags += 1 << 9;
0067 #endif
0068      */
0069 #ifdef ZLIB_WINAPI
0070     flags += 1 << 10;
0071 #endif
0072 #ifdef BUILDFIXED
0073     flags += 1 << 12;
0074 #endif
0075 #ifdef DYNAMIC_CRC_TABLE
0076     flags += 1 << 13;
0077 #endif
0078 #ifdef NO_GZCOMPRESS
0079     flags += 1L << 16;
0080 #endif
0081 #ifdef NO_GZIP
0082     flags += 1L << 17;
0083 #endif
0084 #ifdef PKZIP_BUG_WORKAROUND
0085     flags += 1L << 20;
0086 #endif
0087 #ifdef FASTEST
0088     flags += 1L << 21;
0089 #endif
0090 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
0091 #  ifdef NO_vsnprintf
0092     flags += 1L << 25;
0093 #    ifdef HAS_vsprintf_void
0094     flags += 1L << 26;
0095 #    endif
0096 #  else
0097 #    ifdef HAS_vsnprintf_void
0098     flags += 1L << 26;
0099 #    endif
0100 #  endif
0101 #else
0102     flags += 1L << 24;
0103 #  ifdef NO_snprintf
0104     flags += 1L << 25;
0105 #    ifdef HAS_sprintf_void
0106     flags += 1L << 26;
0107 #    endif
0108 #  else
0109 #    ifdef HAS_snprintf_void
0110     flags += 1L << 26;
0111 #    endif
0112 #  endif
0113 #endif
0114     return flags;
0115 }
0116 
0117 #ifdef ZLIB_DEBUG
0118 #include <stdlib.h>
0119 #  ifndef verbose
0120 #    define verbose 0
0121 #  endif
0122 int ZLIB_INTERNAL z_verbose = verbose;
0123 
0124 void ZLIB_INTERNAL z_error(m)
0125     char *m;
0126 {
0127     fprintf(stderr, "%s\n", m);
0128     exit(1);
0129 }
0130 #endif
0131 
0132 /* exported to allow conversion of error code to string for compress() and
0133  * uncompress()
0134  */
0135 const char * ZEXPORT zError(err)
0136     int err;
0137 {
0138     return ERR_MSG(err);
0139 }
0140 
0141 #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
0142     /* The older Microsoft C Run-Time Library for Windows CE doesn't have
0143      * errno.  We define it as a global variable to simplify porting.
0144      * Its value is always 0 and should not be used.
0145      */
0146     int errno = 0;
0147 #endif
0148 
0149 #ifndef HAVE_MEMCPY
0150 
0151 void ZLIB_INTERNAL zmemcpy(dest, source, len)
0152     Bytef* dest;
0153     const Bytef* source;
0154     uInt  len;
0155 {
0156     if (len == 0) return;
0157     do {
0158         *dest++ = *source++; /* ??? to be unrolled */
0159     } while (--len != 0);
0160 }
0161 
0162 int ZLIB_INTERNAL zmemcmp(s1, s2, len)
0163     const Bytef* s1;
0164     const Bytef* s2;
0165     uInt  len;
0166 {
0167     uInt j;
0168 
0169     for (j = 0; j < len; j++) {
0170         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
0171     }
0172     return 0;
0173 }
0174 
0175 void ZLIB_INTERNAL zmemzero(dest, len)
0176     Bytef* dest;
0177     uInt  len;
0178 {
0179     if (len == 0) return;
0180     do {
0181         *dest++ = 0;  /* ??? to be unrolled */
0182     } while (--len != 0);
0183 }
0184 #endif
0185 
0186 #ifndef Z_SOLO
0187 
0188 #ifdef SYS16BIT
0189 
0190 #ifdef __TURBOC__
0191 /* Turbo C in 16-bit mode */
0192 
0193 #  define MY_ZCALLOC
0194 
0195 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
0196  * and farmalloc(64K) returns a pointer with an offset of 8, so we
0197  * must fix the pointer. Warning: the pointer must be put back to its
0198  * original form in order to free it, use zcfree().
0199  */
0200 
0201 #define MAX_PTR 10
0202 /* 10*64K = 640K */
0203 
0204 local int next_ptr = 0;
0205 
0206 typedef struct ptr_table_s {
0207     voidpf org_ptr;
0208     voidpf new_ptr;
0209 } ptr_table;
0210 
0211 local ptr_table table[MAX_PTR];
0212 /* This table is used to remember the original form of pointers
0213  * to large buffers (64K). Such pointers are normalized with a zero offset.
0214  * Since MSDOS is not a preemptive multitasking OS, this table is not
0215  * protected from concurrent access. This hack doesn't work anyway on
0216  * a protected system like OS/2. Use Microsoft C instead.
0217  */
0218 
0219 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
0220 {
0221     voidpf buf;
0222     ulg bsize = (ulg)items*size;
0223 
0224     (void)opaque;
0225 
0226     /* If we allocate less than 65520 bytes, we assume that farmalloc
0227      * will return a usable pointer which doesn't have to be normalized.
0228      */
0229     if (bsize < 65520L) {
0230         buf = farmalloc(bsize);
0231         if (*(ush*)&buf != 0) return buf;
0232     } else {
0233         buf = farmalloc(bsize + 16L);
0234     }
0235     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
0236     table[next_ptr].org_ptr = buf;
0237 
0238     /* Normalize the pointer to seg:0 */
0239     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
0240     *(ush*)&buf = 0;
0241     table[next_ptr++].new_ptr = buf;
0242     return buf;
0243 }
0244 
0245 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
0246 {
0247     int n;
0248 
0249     (void)opaque;
0250 
0251     if (*(ush*)&ptr != 0) { /* object < 64K */
0252         farfree(ptr);
0253         return;
0254     }
0255     /* Find the original pointer */
0256     for (n = 0; n < next_ptr; n++) {
0257         if (ptr != table[n].new_ptr) continue;
0258 
0259         farfree(table[n].org_ptr);
0260         while (++n < next_ptr) {
0261             table[n-1] = table[n];
0262         }
0263         next_ptr--;
0264         return;
0265     }
0266     Assert(0, "zcfree: ptr not found");
0267 }
0268 
0269 #endif /* __TURBOC__ */
0270 
0271 
0272 #ifdef M_I86
0273 /* Microsoft C in 16-bit mode */
0274 
0275 #  define MY_ZCALLOC
0276 
0277 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
0278 #  define _halloc  halloc
0279 #  define _hfree   hfree
0280 #endif
0281 
0282 voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size)
0283 {
0284     (void)opaque;
0285     return _halloc((long)items, size);
0286 }
0287 
0288 void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
0289 {
0290     (void)opaque;
0291     _hfree(ptr);
0292 }
0293 
0294 #endif /* M_I86 */
0295 
0296 #endif /* SYS16BIT */
0297 
0298 
0299 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
0300 
0301 #ifndef STDC
0302 extern voidp  malloc OF((uInt size));
0303 extern voidp  calloc OF((uInt items, uInt size));
0304 extern void   free   OF((voidpf ptr));
0305 #endif
0306 
0307 voidpf ZLIB_INTERNAL zcalloc(opaque, items, size)
0308     voidpf opaque;
0309     unsigned items;
0310     unsigned size;
0311 {
0312     (void)opaque;
0313     return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
0314                               (voidpf)calloc(items, size);
0315 }
0316 
0317 void ZLIB_INTERNAL zcfree(opaque, ptr)
0318     voidpf opaque;
0319     voidpf ptr;
0320 {
0321     (void)opaque;
0322     free(ptr);
0323 }
0324 
0325 #endif /* MY_ZCALLOC */
0326 
0327 #endif /* !Z_SOLO */