zutil.c 7.2 KB

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