| 1 | /* zutil.c -- target dependent utility functions for the compression library |
|---|
| 2 | * Copyright (C) 1995-1998 Jean-loup Gailly. |
|---|
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | /* |
|---|
| 7 | * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/ZLIB/zutil.c#1 $ |
|---|
| 8 | * $Revision: #1 $ |
|---|
| 9 | * $DateTime: 2006/02/24 17:51:46 $ |
|---|
| 10 | * $Change: 42566 $ |
|---|
| 11 | * $Author: pryush.sharma $ |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #include "zutil.h" |
|---|
| 15 | #include "user_dbg.h" |
|---|
| 16 | |
|---|
| 17 | struct internal_state {int dummy;}; /* for buggy compilers */ |
|---|
| 18 | |
|---|
| 19 | #ifndef STDC |
|---|
| 20 | extern void exit OF((int)); |
|---|
| 21 | #endif |
|---|
| 22 | |
|---|
| 23 | const char *z_errmsg[10] = { |
|---|
| 24 | "need dictionary", /* Z_NEED_DICT 2 */ |
|---|
| 25 | "stream end", /* Z_STREAM_END 1 */ |
|---|
| 26 | "", /* Z_OK 0 */ |
|---|
| 27 | "file error", /* Z_ERRNO (-1) */ |
|---|
| 28 | "stream error", /* Z_STREAM_ERROR (-2) */ |
|---|
| 29 | "data error", /* Z_DATA_ERROR (-3) */ |
|---|
| 30 | "insufficient memory", /* Z_MEM_ERROR (-4) */ |
|---|
| 31 | "buffer error", /* Z_BUF_ERROR (-5) */ |
|---|
| 32 | "incompatible version",/* Z_VERSION_ERROR (-6) */ |
|---|
| 33 | ""}; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | const char * ZEXPORT zlibVersion() |
|---|
| 37 | { |
|---|
| 38 | return ZLIB_VERSION; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | #if /* TL_DEBUG */ 0 |
|---|
| 42 | |
|---|
| 43 | # ifndef verbose |
|---|
| 44 | # define verbose 0 |
|---|
| 45 | # endif |
|---|
| 46 | int z_verbose = verbose; |
|---|
| 47 | |
|---|
| 48 | void z_error (m) |
|---|
| 49 | char *m; |
|---|
| 50 | { |
|---|
| 51 | OS_DbgPrintf("%s\n", m); |
|---|
| 52 | exit(1); |
|---|
| 53 | } |
|---|
| 54 | #else |
|---|
| 55 | void z_error(char *message) |
|---|
| 56 | { |
|---|
| 57 | do { |
|---|
| 58 | TLASSERT(0, message); |
|---|
| 59 | } while (1); |
|---|
| 60 | } |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | /* exported to allow conversion of error code to string for compress() and |
|---|
| 64 | * uncompress() |
|---|
| 65 | */ |
|---|
| 66 | const char * ZEXPORT zError( |
|---|
| 67 | int err) |
|---|
| 68 | { |
|---|
| 69 | return ERR_MSG(err); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | #ifndef HAVE_MEMCPY |
|---|
| 74 | |
|---|
| 75 | void zmemcpy(dest, source, len) |
|---|
| 76 | Bytef* dest; |
|---|
| 77 | const Bytef* source; |
|---|
| 78 | uInt len; |
|---|
| 79 | { |
|---|
| 80 | if (len == 0) return; |
|---|
| 81 | do { |
|---|
| 82 | *dest++ = *source++; /* ??? to be unrolled */ |
|---|
| 83 | } while (--len != 0); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | int zmemcmp(s1, s2, len) |
|---|
| 87 | const Bytef* s1; |
|---|
| 88 | const Bytef* s2; |
|---|
| 89 | uInt len; |
|---|
| 90 | { |
|---|
| 91 | uInt j; |
|---|
| 92 | |
|---|
| 93 | for (j = 0; j < len; j++) { |
|---|
| 94 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; |
|---|
| 95 | } |
|---|
| 96 | return 0; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | void zmemzero(dest, len) |
|---|
| 100 | Bytef* dest; |
|---|
| 101 | uInt len; |
|---|
| 102 | { |
|---|
| 103 | if (len == 0) return; |
|---|
| 104 | do { |
|---|
| 105 | *dest++ = 0; /* ??? to be unrolled */ |
|---|
| 106 | } while (--len != 0); |
|---|
| 107 | } |
|---|
| 108 | #endif |
|---|
| 109 | |
|---|
| 110 | #ifdef __TURBOC__ |
|---|
| 111 | #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__) |
|---|
| 112 | /* Small and medium model in Turbo C are for now limited to near allocation |
|---|
| 113 | * with reduced MAX_WBITS and MAX_MEM_LEVEL |
|---|
| 114 | */ |
|---|
| 115 | # define MY_ZCALLOC |
|---|
| 116 | |
|---|
| 117 | /* Turbo C malloc() does not allow dynamic allocation of 64K bytes |
|---|
| 118 | * and farmalloc(64K) returns a pointer with an offset of 8, so we |
|---|
| 119 | * must fix the pointer. Warning: the pointer must be put back to its |
|---|
| 120 | * original form in order to free it, use zcfree(). |
|---|
| 121 | */ |
|---|
| 122 | |
|---|
| 123 | #define MAX_PTR 10 |
|---|
| 124 | /* 10*64K = 640K */ |
|---|
| 125 | |
|---|
| 126 | local int next_ptr = 0; |
|---|
| 127 | |
|---|
| 128 | typedef struct ptr_table_s { |
|---|
| 129 | voidpf org_ptr; |
|---|
| 130 | voidpf new_ptr; |
|---|
| 131 | } ptr_table; |
|---|
| 132 | |
|---|
| 133 | local ptr_table table[MAX_PTR]; |
|---|
| 134 | /* This table is used to remember the original form of pointers |
|---|
| 135 | * to large buffers (64K). Such pointers are normalized with a zero offset. |
|---|
| 136 | * Since MSDOS is not a preemptive multitasking OS, this table is not |
|---|
| 137 | * protected from concurrent access. This hack doesn't work anyway on |
|---|
| 138 | * a protected system like OS/2. Use Microsoft C instead. |
|---|
| 139 | */ |
|---|
| 140 | |
|---|
| 141 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) |
|---|
| 142 | { |
|---|
| 143 | |
|---|
| 144 | voidpf buf = opaque; /* just to make some compilers happy */ |
|---|
| 145 | ulg bsize = (ulg)items*size; |
|---|
| 146 | |
|---|
| 147 | /* If we allocate less than 65520 bytes, we assume that farmalloc |
|---|
| 148 | * will return a usable pointer which doesn't have to be normalized. |
|---|
| 149 | */ |
|---|
| 150 | if (bsize < 65520L) { |
|---|
| 151 | buf = farmalloc(bsize); |
|---|
| 152 | if (*(ush*)&buf != 0) return buf; |
|---|
| 153 | } else { |
|---|
| 154 | buf = farmalloc(bsize + 16L); |
|---|
| 155 | } |
|---|
| 156 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL; |
|---|
| 157 | table[next_ptr].org_ptr = buf; |
|---|
| 158 | |
|---|
| 159 | /* Normalize the pointer to seg:0 */ |
|---|
| 160 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; |
|---|
| 161 | *(ush*)&buf = 0; |
|---|
| 162 | table[next_ptr++].new_ptr = buf; |
|---|
| 163 | return buf; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | void zcfree (voidpf opaque, voidpf ptr) |
|---|
| 167 | { |
|---|
| 168 | int n; |
|---|
| 169 | if (*(ush*)&ptr != 0) { /* object < 64K */ |
|---|
| 170 | farfree(ptr); |
|---|
| 171 | return; |
|---|
| 172 | } |
|---|
| 173 | /* Find the original pointer */ |
|---|
| 174 | for (n = 0; n < next_ptr; n++) { |
|---|
| 175 | if (ptr != table[n].new_ptr) continue; |
|---|
| 176 | |
|---|
| 177 | farfree(table[n].org_ptr); |
|---|
| 178 | while (++n < next_ptr) { |
|---|
| 179 | table[n-1] = table[n]; |
|---|
| 180 | } |
|---|
| 181 | next_ptr--; |
|---|
| 182 | return; |
|---|
| 183 | } |
|---|
| 184 | ptr = opaque; /* just to make some compilers happy */ |
|---|
| 185 | Assert(0, "zcfree: ptr not found"); |
|---|
| 186 | } |
|---|
| 187 | #endif |
|---|
| 188 | #endif /* __TURBOC__ */ |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | #if defined(M_I86) && !defined(__32BIT__) |
|---|
| 192 | /* Microsoft C in 16-bit mode */ |
|---|
| 193 | |
|---|
| 194 | # define MY_ZCALLOC |
|---|
| 195 | |
|---|
| 196 | #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) |
|---|
| 197 | # define _halloc halloc |
|---|
| 198 | # define _hfree hfree |
|---|
| 199 | #endif |
|---|
| 200 | |
|---|
| 201 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) |
|---|
| 202 | { |
|---|
| 203 | |
|---|
| 204 | if (opaque) opaque = 0; /* to make compiler happy */ |
|---|
| 205 | return _halloc((long)items, size); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | void zcfree (voidpf opaque, voidpf ptr) |
|---|
| 209 | { |
|---|
| 210 | if (opaque) opaque = 0; /* to make compiler happy */ |
|---|
| 211 | _hfree(ptr); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | #endif /* MSC */ |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */ |
|---|
| 218 | |
|---|
| 219 | #ifndef STDC |
|---|
| 220 | extern voidp calloc OF((uInt items, uInt size)); |
|---|
| 221 | extern void free OF((voidpf ptr)); |
|---|
| 222 | #endif |
|---|
| 223 | |
|---|
| 224 | void * MsOS_AllocateMemory2 (unsigned long u32Size); |
|---|
| 225 | unsigned char MsOS_FreeMemory2(void *pAddress); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | voidpf zcalloc ( |
|---|
| 229 | voidpf opaque, |
|---|
| 230 | unsigned items, |
|---|
| 231 | unsigned size) |
|---|
| 232 | { |
|---|
| 233 | if (opaque) items += size - size; /* make compiler happy */ |
|---|
| 234 | voidpf *p = MsOS_AllocateMemory2((unsigned long)(items * size)); |
|---|
| 235 | memset(p, 0, items * size); |
|---|
| 236 | return p; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | void zcfree ( |
|---|
| 240 | voidpf opaque, |
|---|
| 241 | voidpf ptr) |
|---|
| 242 | { |
|---|
| 243 | MsOS_FreeMemory2(ptr); |
|---|
| 244 | if (opaque) return; /* make compiler happy */ |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | #endif /* MY_ZCALLOC */ |
|---|