zutil.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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: zutil.c,v 1.1 2007/06/07 17:54:37 jules_rms Exp $ */
  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. #if 0
  107. # ifndef verbose
  108. # define verbose 0
  109. # endif
  110. int z_verbose = verbose;
  111. void z_error (const char *m)
  112. {
  113. fprintf(stderr, "%s\n", m);
  114. exit(1);
  115. }
  116. #endif
  117. /* exported to allow conversion of error code to string for compress() and
  118. * uncompress()
  119. */
  120. const char * ZEXPORT zError(int err)
  121. {
  122. return ERR_MSG(err);
  123. }
  124. #if defined(_WIN32_WCE)
  125. /* The Microsoft C Run-Time Library for Windows CE doesn't have
  126. * errno. We define it as a global variable to simplify porting.
  127. * Its value is always 0 and should not be used.
  128. */
  129. int errno = 0;
  130. #endif
  131. #ifndef HAVE_MEMCPY
  132. void zmemcpy(dest, source, len)
  133. Bytef* dest;
  134. const Bytef* source;
  135. uInt len;
  136. {
  137. if (len == 0) return;
  138. do {
  139. *dest++ = *source++; /* ??? to be unrolled */
  140. } while (--len != 0);
  141. }
  142. int zmemcmp(s1, s2, len)
  143. const Bytef* s1;
  144. const Bytef* s2;
  145. uInt len;
  146. {
  147. uInt j;
  148. for (j = 0; j < len; j++) {
  149. if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  150. }
  151. return 0;
  152. }
  153. void zmemzero(dest, len)
  154. Bytef* dest;
  155. uInt len;
  156. {
  157. if (len == 0) return;
  158. do {
  159. *dest++ = 0; /* ??? to be unrolled */
  160. } while (--len != 0);
  161. }
  162. #endif
  163. #ifdef SYS16BIT
  164. #ifdef __TURBOC__
  165. /* Turbo C in 16-bit mode */
  166. # define MY_ZCALLOC
  167. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  168. * and farmalloc(64K) returns a pointer with an offset of 8, so we
  169. * must fix the pointer. Warning: the pointer must be put back to its
  170. * original form in order to free it, use zcfree().
  171. */
  172. #define MAX_PTR 10
  173. /* 10*64K = 640K */
  174. local int next_ptr = 0;
  175. typedef struct ptr_table_s {
  176. voidpf org_ptr;
  177. voidpf new_ptr;
  178. } ptr_table;
  179. local ptr_table table[MAX_PTR];
  180. /* This table is used to remember the original form of pointers
  181. * to large buffers (64K). Such pointers are normalized with a zero offset.
  182. * Since MSDOS is not a preemptive multitasking OS, this table is not
  183. * protected from concurrent access. This hack doesn't work anyway on
  184. * a protected system like OS/2. Use Microsoft C instead.
  185. */
  186. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  187. {
  188. voidpf buf = opaque; /* just to make some compilers happy */
  189. ulg bsize = (ulg)items*size;
  190. /* If we allocate less than 65520 bytes, we assume that farmalloc
  191. * will return a usable pointer which doesn't have to be normalized.
  192. */
  193. if (bsize < 65520L) {
  194. buf = farmalloc(bsize);
  195. if (*(ush*)&buf != 0) return buf;
  196. } else {
  197. buf = farmalloc(bsize + 16L);
  198. }
  199. if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  200. table[next_ptr].org_ptr = buf;
  201. /* Normalize the pointer to seg:0 */
  202. *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  203. *(ush*)&buf = 0;
  204. table[next_ptr++].new_ptr = buf;
  205. return buf;
  206. }
  207. void zcfree (voidpf opaque, voidpf ptr)
  208. {
  209. int n;
  210. if (*(ush*)&ptr != 0) { /* object < 64K */
  211. farfree(ptr);
  212. return;
  213. }
  214. /* Find the original pointer */
  215. for (n = 0; n < next_ptr; n++) {
  216. if (ptr != table[n].new_ptr) continue;
  217. farfree(table[n].org_ptr);
  218. while (++n < next_ptr) {
  219. table[n-1] = table[n];
  220. }
  221. next_ptr--;
  222. return;
  223. }
  224. ptr = opaque; /* just to make some compilers happy */
  225. Assert(0, "zcfree: ptr not found");
  226. }
  227. #endif /* __TURBOC__ */
  228. #ifdef M_I86
  229. /* Microsoft C in 16-bit mode */
  230. # define MY_ZCALLOC
  231. #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
  232. # define _halloc halloc
  233. # define _hfree hfree
  234. #endif
  235. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  236. {
  237. if (opaque) opaque = 0; /* to make compiler happy */
  238. return _halloc((long)items, size);
  239. }
  240. void zcfree (voidpf opaque, voidpf ptr)
  241. {
  242. if (opaque) opaque = 0; /* to make compiler happy */
  243. _hfree(ptr);
  244. }
  245. #endif /* M_I86 */
  246. #endif /* SYS16BIT */
  247. #ifndef MY_ZCALLOC /* Any system without a special alloc function */
  248. #ifndef STDC
  249. extern voidp malloc OF((uInt size));
  250. extern voidp calloc OF((uInt items, uInt size));
  251. extern void free OF((voidpf ptr));
  252. #endif
  253. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  254. {
  255. if (opaque) items += size - size; /* make compiler happy */
  256. return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
  257. (voidpf)calloc(items, size);
  258. }
  259. void zcfree (voidpf opaque, voidpf ptr)
  260. {
  261. free(ptr);
  262. if (opaque) return; /* make compiler happy */
  263. }
  264. #endif /* MY_ZCALLOC */