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