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