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