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