zutil.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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(void) {
  23. return ZLIB_VERSION;
  24. }
  25. uLong ZEXPORT zlibCompileFlags(void) {
  26. uLong flags;
  27. flags = 0;
  28. switch ((int)(sizeof(uInt))) {
  29. case 2: break;
  30. case 4: flags += 1; break;
  31. case 8: flags += 2; break;
  32. default: flags += 3;
  33. }
  34. switch ((int)(sizeof(uLong))) {
  35. case 2: break;
  36. case 4: flags += 1 << 2; break;
  37. case 8: flags += 2 << 2; break;
  38. default: flags += 3 << 2;
  39. }
  40. switch ((int)(sizeof(voidpf))) {
  41. case 2: break;
  42. case 4: flags += 1 << 4; break;
  43. case 8: flags += 2 << 4; break;
  44. default: flags += 3 << 4;
  45. }
  46. switch ((int)(sizeof(z_off_t))) {
  47. case 2: break;
  48. case 4: flags += 1 << 6; break;
  49. case 8: flags += 2 << 6; break;
  50. default: flags += 3 << 6;
  51. }
  52. #ifdef ZLIB_DEBUG
  53. flags += 1 << 8;
  54. #endif
  55. /*
  56. #if defined(ASMV) || defined(ASMINF)
  57. flags += 1 << 9;
  58. #endif
  59. */
  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(char *m) {
  114. fprintf(stderr, "%s\n", m);
  115. exit(1);
  116. }
  117. #endif
  118. /* exported to allow conversion of error code to string for compress() and
  119. * uncompress()
  120. */
  121. const char * ZEXPORT zError(int err) {
  122. return ERR_MSG(err);
  123. }
  124. #if defined(_WIN32_WCE) && _WIN32_WCE < 0x800
  125. /* The older 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 ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len) {
  133. if (len == 0) return;
  134. do {
  135. *dest++ = *source++; /* ??? to be unrolled */
  136. } while (--len != 0);
  137. }
  138. int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len) {
  139. uInt j;
  140. for (j = 0; j < len; j++) {
  141. if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  142. }
  143. return 0;
  144. }
  145. void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len) {
  146. if (len == 0) return;
  147. do {
  148. *dest++ = 0; /* ??? to be unrolled */
  149. } while (--len != 0);
  150. }
  151. #endif
  152. #ifndef Z_SOLO
  153. #ifdef SYS16BIT
  154. #ifdef __TURBOC__
  155. /* Turbo C in 16-bit mode */
  156. # define MY_ZCALLOC
  157. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  158. * and farmalloc(64K) returns a pointer with an offset of 8, so we
  159. * must fix the pointer. Warning: the pointer must be put back to its
  160. * original form in order to free it, use zcfree().
  161. */
  162. #define MAX_PTR 10
  163. /* 10*64K = 640K */
  164. local int next_ptr = 0;
  165. typedef struct ptr_table_s {
  166. voidpf org_ptr;
  167. voidpf new_ptr;
  168. } ptr_table;
  169. local ptr_table table[MAX_PTR];
  170. /* This table is used to remember the original form of pointers
  171. * to large buffers (64K). Such pointers are normalized with a zero offset.
  172. * Since MSDOS is not a preemptive multitasking OS, this table is not
  173. * protected from concurrent access. This hack doesn't work anyway on
  174. * a protected system like OS/2. Use Microsoft C instead.
  175. */
  176. voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
  177. voidpf buf;
  178. ulg bsize = (ulg)items*size;
  179. (void)opaque;
  180. /* If we allocate less than 65520 bytes, we assume that farmalloc
  181. * will return a usable pointer which doesn't have to be normalized.
  182. */
  183. if (bsize < 65520L) {
  184. buf = farmalloc(bsize);
  185. if (*(ush*)&buf != 0) return buf;
  186. } else {
  187. buf = farmalloc(bsize + 16L);
  188. }
  189. if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  190. table[next_ptr].org_ptr = buf;
  191. /* Normalize the pointer to seg:0 */
  192. *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  193. *(ush*)&buf = 0;
  194. table[next_ptr++].new_ptr = buf;
  195. return buf;
  196. }
  197. void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
  198. int n;
  199. (void)opaque;
  200. if (*(ush*)&ptr != 0) { /* object < 64K */
  201. farfree(ptr);
  202. return;
  203. }
  204. /* Find the original pointer */
  205. for (n = 0; n < next_ptr; n++) {
  206. if (ptr != table[n].new_ptr) continue;
  207. farfree(table[n].org_ptr);
  208. while (++n < next_ptr) {
  209. table[n-1] = table[n];
  210. }
  211. next_ptr--;
  212. return;
  213. }
  214. Assert(0, "zcfree: ptr not found");
  215. }
  216. #endif /* __TURBOC__ */
  217. #ifdef M_I86
  218. /* Microsoft C in 16-bit mode */
  219. # define MY_ZCALLOC
  220. #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
  221. # define _halloc halloc
  222. # define _hfree hfree
  223. #endif
  224. voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) {
  225. (void)opaque;
  226. return _halloc((long)items, size);
  227. }
  228. void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
  229. (void)opaque;
  230. _hfree(ptr);
  231. }
  232. #endif /* M_I86 */
  233. #endif /* SYS16BIT */
  234. #ifndef MY_ZCALLOC /* Any system without a special alloc function */
  235. #ifndef STDC
  236. extern voidp malloc(uInt size);
  237. extern voidp calloc(uInt items, uInt size);
  238. extern void free(voidpf ptr);
  239. #endif
  240. voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) {
  241. (void)opaque;
  242. return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
  243. (voidpf)calloc(items, size);
  244. }
  245. void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {
  246. (void)opaque;
  247. free(ptr);
  248. }
  249. #endif /* MY_ZCALLOC */
  250. #endif /* !Z_SOLO */