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