security/nss/lib/zlib/zutil.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial