/src/FreeImage/Source/ZLib/zutil.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 301 lines · 222 code · 48 blank · 31 comment · 34 complexity · 0c011220a228dea564099db232fecf24 MD5 · raw file

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