zutil.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* zutil.c -- target dependent utility functions for the compression library
  2. * Copyright (C) 1995-2003 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. #ifndef STDC
  11. extern void exit OF((int));
  12. #endif
  13. const char * const z_errmsg[10] = {
  14. "need dictionary", /* Z_NEED_DICT 2 */
  15. "stream end", /* Z_STREAM_END 1 */
  16. "", /* Z_OK 0 */
  17. "file error", /* Z_ERRNO (-1) */
  18. "stream error", /* Z_STREAM_ERROR (-2) */
  19. "data error", /* Z_DATA_ERROR (-3) */
  20. "insufficient memory", /* Z_MEM_ERROR (-4) */
  21. "buffer error", /* Z_BUF_ERROR (-5) */
  22. "incompatible version",/* Z_VERSION_ERROR (-6) */
  23. ""};
  24. const char * ZEXPORT zlibVersion()
  25. {
  26. return ZLIB_VERSION;
  27. }
  28. uLong ZEXPORT zlibCompileFlags()
  29. {
  30. uLong flags;
  31. flags = 0;
  32. switch (sizeof(uInt)) {
  33. case 2: break;
  34. case 4: flags += 1; break;
  35. case 8: flags += 2; break;
  36. default: flags += 3;
  37. }
  38. switch (sizeof(uLong)) {
  39. case 2: break;
  40. case 4: flags += 1 << 2; break;
  41. case 8: flags += 2 << 2; break;
  42. default: flags += 3 << 2;
  43. }
  44. switch (sizeof(voidpf)) {
  45. case 2: break;
  46. case 4: flags += 1 << 4; break;
  47. case 8: flags += 2 << 4; break;
  48. default: flags += 3 << 4;
  49. }
  50. switch (sizeof(z_off_t)) {
  51. case 2: break;
  52. case 4: flags += 1 << 6; break;
  53. case 8: flags += 2 << 6; break;
  54. default: flags += 3 << 6;
  55. }
  56. #ifdef DEBUG
  57. flags += 1 << 8;
  58. #endif
  59. #if defined(ASMV) || defined(ASMINF)
  60. flags += 1 << 9;
  61. #endif
  62. #ifdef ZLIB_WINAPI
  63. flags += 1 << 10;
  64. #endif
  65. #ifdef BUILDFIXED
  66. flags += 1 << 12;
  67. #endif
  68. #ifdef DYNAMIC_CRC_TABLE
  69. flags += 1 << 13;
  70. #endif
  71. #ifdef NO_GZCOMPRESS
  72. flags += 1 << 16;
  73. #endif
  74. #ifdef NO_GZIP
  75. flags += 1 << 17;
  76. #endif
  77. #ifdef PKZIP_BUG_WORKAROUND
  78. flags += 1 << 20;
  79. #endif
  80. #ifdef FASTEST
  81. flags += 1 << 21;
  82. #endif
  83. #ifdef STDC
  84. # ifdef NO_vsnprintf
  85. flags += 1 << 25;
  86. # ifdef HAS_vsprintf_void
  87. flags += 1 << 26;
  88. # endif
  89. # else
  90. # ifdef HAS_vsnprintf_void
  91. flags += 1 << 26;
  92. # endif
  93. # endif
  94. #else
  95. flags += 1 << 24;
  96. # ifdef NO_snprintf
  97. flags += 1 << 25;
  98. # ifdef HAS_sprintf_void
  99. flags += 1 << 26;
  100. # endif
  101. # else
  102. # ifdef HAS_snprintf_void
  103. flags += 1 << 26;
  104. # endif
  105. # endif
  106. #endif
  107. return flags;
  108. }
  109. #ifdef DEBUG
  110. # ifndef verbose
  111. # define verbose 0
  112. # endif
  113. int z_verbose = verbose;
  114. void z_error (m)
  115. char *m;
  116. {
  117. fprintf(stderr, "%s\n", m);
  118. exit(1);
  119. }
  120. #endif
  121. /* exported to allow conversion of error code to string for compress() and
  122. * uncompress()
  123. */
  124. const char * ZEXPORT zError(err)
  125. int err;
  126. {
  127. return ERR_MSG(err);
  128. }
  129. #if defined(_WIN32_WCE)
  130. /* does not exist on WCE */
  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 */