zutil.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #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. #if defined(STDC) || defined(Z_HAVE_STDARG_H)
  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 ZLIB_DEBUG
  108. #include <stdlib.h>
  109. # ifndef verbose
  110. # define verbose 0
  111. # endif
  112. int ZLIB_INTERNAL z_verbose = verbose;
  113. void ZLIB_INTERNAL z_error (m)
  114. char *m;
  115. {
  116. fprintf(stderr, "%s\n", m);
  117. exit(1);
  118. }
  119. #endif
  120. /* exported to allow conversion of error code to string for compress() and
  121. * uncompress()
  122. */
  123. const char * ZEXPORT zError(err)
  124. int err;
  125. {
  126. return ERR_MSG(err);
  127. }
  128. #if defined(_WIN32_WCE)
  129. /* The Microsoft C Run-Time Library for Windows CE doesn't have
  130. * errno. We define it as a global variable to simplify porting.
  131. * Its value is always 0 and should not be used.
  132. */
  133. int errno = 0;
  134. #endif
  135. #ifndef HAVE_MEMCPY
  136. void ZLIB_INTERNAL zmemcpy(dest, source, len)
  137. Bytef* dest;
  138. const Bytef* source;
  139. uInt len;
  140. {
  141. if (len == 0) return;
  142. do {
  143. *dest++ = *source++; /* ??? to be unrolled */
  144. } while (--len != 0);
  145. }
  146. int ZLIB_INTERNAL zmemcmp(s1, s2, len)
  147. const Bytef* s1;
  148. const Bytef* s2;
  149. uInt len;
  150. {
  151. uInt j;
  152. for (j = 0; j < len; j++) {
  153. if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  154. }
  155. return 0;
  156. }
  157. void ZLIB_INTERNAL zmemzero(dest, len)
  158. Bytef* dest;
  159. uInt len;
  160. {
  161. if (len == 0) return;
  162. do {
  163. *dest++ = 0; /* ??? to be unrolled */
  164. } while (--len != 0);
  165. }
  166. #endif
  167. #ifndef Z_SOLO
  168. #ifdef SYS16BIT
  169. #ifdef __TURBOC__
  170. /* Turbo C in 16-bit mode */
  171. # define MY_ZCALLOC
  172. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  173. * and farmalloc(64K) returns a pointer with an offset of 8, so we
  174. * must fix the pointer. Warning: the pointer must be put back to its
  175. * original form in order to free it, use zcfree().
  176. */
  177. #define MAX_PTR 10
  178. /* 10*64K = 640K */
  179. local int next_ptr = 0;
  180. typedef struct ptr_table_s {
  181. voidpf org_ptr;
  182. voidpf new_ptr;
  183. } ptr_table;
  184. local ptr_table table[MAX_PTR];
  185. /* This table is used to remember the original form of pointers
  186. * to large buffers (64K). Such pointers are normalized with a zero offset.
  187. * Since MSDOS is not a preemptive multitasking OS, this table is not
  188. * protected from concurrent access. This hack doesn't work anyway on
  189. * a protected system like OS/2. Use Microsoft C instead.
  190. */
  191. voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
  192. {
  193. voidpf buf;
  194. ulg bsize = (ulg)items*size;
  195. (void)opaque;
  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. (void)opaque;
  217. if (*(ush*)&ptr != 0) { /* object < 64K */
  218. farfree(ptr);
  219. return;
  220. }
  221. /* Find the original pointer */
  222. for (n = 0; n < next_ptr; n++) {
  223. if (ptr != table[n].new_ptr) continue;
  224. farfree(table[n].org_ptr);
  225. while (++n < next_ptr) {
  226. table[n-1] = table[n];
  227. }
  228. next_ptr--;
  229. return;
  230. }
  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. (void)opaque;
  244. return _halloc((long)items, size);
  245. }
  246. void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
  247. {
  248. (void)opaque;
  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. (void)opaque;
  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. (void)opaque;
  273. free(ptr);
  274. }
  275. #endif /* MY_ZCALLOC */
  276. #endif /* !Z_SOLO */