zutil.c 7.0 KB

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