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