zutil.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* zutil.c -- target dependent utility functions for the compression library
  2. * Copyright (C) 1995-2005, 2010, 2011, 2012 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 Z_SOLO
  8. # include "gzguts.h"
  9. #endif
  10. #ifndef NO_DUMMY_DECL
  11. struct internal_state {int dummy;}; /* for buggy compilers */
  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 ((int)(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 ((int)(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 ((int)(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 ((int)(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 += 1L << 16;
  73. #endif
  74. #ifdef NO_GZIP
  75. flags += 1L << 17;
  76. #endif
  77. #ifdef PKZIP_BUG_WORKAROUND
  78. flags += 1L << 20;
  79. #endif
  80. #ifdef FASTEST
  81. flags += 1L << 21;
  82. #endif
  83. #if defined(STDC) || defined(Z_HAVE_STDARG_H)
  84. # ifdef NO_vsnprintf
  85. flags += 1L << 25;
  86. # ifdef HAS_vsprintf_void
  87. flags += 1L << 26;
  88. # endif
  89. # else
  90. # ifdef HAS_vsnprintf_void
  91. flags += 1L << 26;
  92. # endif
  93. # endif
  94. #else
  95. flags += 1L << 24;
  96. # ifdef NO_snprintf
  97. flags += 1L << 25;
  98. # ifdef HAS_sprintf_void
  99. flags += 1L << 26;
  100. # endif
  101. # else
  102. # ifdef HAS_snprintf_void
  103. flags += 1L << 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 ZLIB_INTERNAL z_verbose = verbose;
  114. void ZLIB_INTERNAL 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. /* The Microsoft C Run-Time Library for Windows CE doesn't have
  131. * errno. We define it as a global variable to simplify porting.
  132. * Its value is always 0 and should not be used.
  133. */
  134. int errno = 0;
  135. #endif
  136. #ifndef HAVE_MEMCPY
  137. void ZLIB_INTERNAL zmemcpy(dest, source, len)
  138. Bytef* dest;
  139. const Bytef* source;
  140. uInt len;
  141. {
  142. if (len == 0) return;
  143. do {
  144. *dest++ = *source++; /* ??? to be unrolled */
  145. } while (--len != 0);
  146. }
  147. int ZLIB_INTERNAL zmemcmp(s1, s2, len)
  148. const Bytef* s1;
  149. const Bytef* s2;
  150. uInt len;
  151. {
  152. uInt j;
  153. for (j = 0; j < len; j++) {
  154. if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  155. }
  156. return 0;
  157. }
  158. void ZLIB_INTERNAL zmemzero(dest, len)
  159. Bytef* dest;
  160. uInt len;
  161. {
  162. if (len == 0) return;
  163. do {
  164. *dest++ = 0; /* ??? to be unrolled */
  165. } while (--len != 0);
  166. }
  167. #endif
  168. #ifndef Z_SOLO
  169. #ifdef SYS16BIT
  170. #ifdef __TURBOC__
  171. /* Turbo C in 16-bit mode */
  172. # define MY_ZCALLOC
  173. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  174. * and farmalloc(64K) returns a pointer with an offset of 8, so we
  175. * must fix the pointer. Warning: the pointer must be put back to its
  176. * original form in order to free it, use zcfree().
  177. */
  178. #define MAX_PTR 10
  179. /* 10*64K = 640K */
  180. local int next_ptr = 0;
  181. typedef struct ptr_table_s {
  182. voidpf org_ptr;
  183. voidpf new_ptr;
  184. } ptr_table;
  185. local ptr_table table[MAX_PTR];
  186. /* This table is used to remember the original form of pointers
  187. * to large buffers (64K). Such pointers are normalized with a zero offset.
  188. * Since MSDOS is not a preemptive multitasking OS, this table is not
  189. * protected from concurrent access. This hack doesn't work anyway on
  190. * a protected system like OS/2. Use Microsoft C instead.
  191. */
  192. voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
  193. {
  194. voidpf buf = opaque; /* just to make some compilers happy */
  195. ulg bsize = (ulg)items*size;
  196. /* If we allocate less than 65520 bytes, we assume that farmalloc
  197. * will return a usable pointer which doesn't have to be normalized.
  198. */
  199. if (bsize < 65520L) {
  200. buf = farmalloc(bsize);
  201. if (*(ush*)&buf != 0) return buf;
  202. } else {
  203. buf = farmalloc(bsize + 16L);
  204. }
  205. if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  206. table[next_ptr].org_ptr = buf;
  207. /* Normalize the pointer to seg:0 */
  208. *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  209. *(ush*)&buf = 0;
  210. table[next_ptr++].new_ptr = buf;
  211. return buf;
  212. }
  213. void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
  214. {
  215. int n;
  216. if (*(ush*)&ptr != 0) { /* object < 64K */
  217. farfree(ptr);
  218. return;
  219. }
  220. /* Find the original pointer */
  221. for (n = 0; n < next_ptr; n++) {
  222. if (ptr != table[n].new_ptr) continue;
  223. farfree(table[n].org_ptr);
  224. while (++n < next_ptr) {
  225. table[n-1] = table[n];
  226. }
  227. next_ptr--;
  228. return;
  229. }
  230. ptr = opaque; /* just to make some compilers happy */
  231. Assert(0, "zcfree: ptr not found");
  232. }
  233. #endif /* __TURBOC__ */
  234. #ifdef M_I86
  235. /* Microsoft C in 16-bit mode */
  236. # define MY_ZCALLOC
  237. #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
  238. # define _halloc halloc
  239. # define _hfree hfree
  240. #endif
  241. voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
  242. {
  243. if (opaque) opaque = 0; /* to make compiler happy */
  244. return _halloc((long)items, size);
  245. }
  246. void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
  247. {
  248. if (opaque) opaque = 0; /* to make compiler happy */
  249. _hfree(ptr);
  250. }
  251. #endif /* M_I86 */
  252. #endif /* SYS16BIT */
  253. #ifndef MY_ZCALLOC /* Any system without a special alloc function */
  254. #ifndef STDC
  255. extern voidp malloc OF((uInt size));
  256. extern voidp calloc OF((uInt items, uInt size));
  257. extern void free OF((voidpf ptr));
  258. #endif
  259. voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
  260. voidpf opaque;
  261. unsigned items;
  262. unsigned size;
  263. {
  264. if (opaque) items += size - size; /* make compiler happy */
  265. return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
  266. (voidpf)calloc(items, size);
  267. }
  268. void ZLIB_INTERNAL zcfree (opaque, ptr)
  269. voidpf opaque;
  270. voidpf ptr;
  271. {
  272. free(ptr);
  273. if (opaque) return; /* make compiler happy */
  274. }
  275. #endif /* MY_ZCALLOC */
  276. #endif /* !Z_SOLO */