zutil.c 6.9 KB

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