Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
5.1 KiB

  1. /* zutil.c -- target dependent utility functions for the compression library
  2. * Copyright (C) 1995-2002 Jean-loup Gailly.
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /* @(#) $Id$ */
  6. #include "zutil.h"
  7. struct internal_state {int dummy;}; /* for buggy compilers */
  8. #ifndef STDC
  9. extern void exit OF((int));
  10. #endif
  11. const char *z_errmsg[10] = {
  12. "need dictionary", /* Z_NEED_DICT 2 */
  13. "stream end", /* Z_STREAM_END 1 */
  14. "", /* Z_OK 0 */
  15. "file error", /* Z_ERRNO (-1) */
  16. "stream error", /* Z_STREAM_ERROR (-2) */
  17. "data error", /* Z_DATA_ERROR (-3) */
  18. "insufficient memory", /* Z_MEM_ERROR (-4) */
  19. "buffer error", /* Z_BUF_ERROR (-5) */
  20. "incompatible version",/* Z_VERSION_ERROR (-6) */
  21. ""};
  22. const char * ZEXPORT zlibVersion()
  23. {
  24. return ZLIB_VERSION;
  25. }
  26. #ifdef DEBUG
  27. # ifndef verbose
  28. # define verbose 0
  29. # endif
  30. int z_verbose = verbose;
  31. void z_error (m)
  32. char *m;
  33. {
  34. fprintf(stderr, "%s\n", m);
  35. exit(1);
  36. }
  37. #endif
  38. /* exported to allow conversion of error code to string for compress() and
  39. * uncompress()
  40. */
  41. const char * ZEXPORT zError(err)
  42. int err;
  43. {
  44. return ERR_MSG(err);
  45. }
  46. #ifndef HAVE_MEMCPY
  47. void zmemcpy(dest, source, len)
  48. Bytef* dest;
  49. const Bytef* source;
  50. uInt len;
  51. {
  52. if (len == 0) return;
  53. do {
  54. *dest++ = *source++; /* ??? to be unrolled */
  55. } while (--len != 0);
  56. }
  57. int zmemcmp(s1, s2, len)
  58. const Bytef* s1;
  59. const Bytef* s2;
  60. uInt len;
  61. {
  62. uInt j;
  63. for (j = 0; j < len; j++) {
  64. if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  65. }
  66. return 0;
  67. }
  68. void zmemzero(dest, len)
  69. Bytef* dest;
  70. uInt len;
  71. {
  72. if (len == 0) return;
  73. do {
  74. *dest++ = 0; /* ??? to be unrolled */
  75. } while (--len != 0);
  76. }
  77. #endif
  78. #ifdef __TURBOC__
  79. #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
  80. /* Small and medium model in Turbo C are for now limited to near allocation
  81. * with reduced MAX_WBITS and MAX_MEM_LEVEL
  82. */
  83. # define MY_ZCALLOC
  84. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  85. * and farmalloc(64K) returns a pointer with an offset of 8, so we
  86. * must fix the pointer. Warning: the pointer must be put back to its
  87. * original form in order to free it, use zcfree().
  88. */
  89. #define MAX_PTR 10
  90. /* 10*64K = 640K */
  91. local int next_ptr = 0;
  92. typedef struct ptr_table_s {
  93. voidpf org_ptr;
  94. voidpf new_ptr;
  95. } ptr_table;
  96. local ptr_table table[MAX_PTR];
  97. /* This table is used to remember the original form of pointers
  98. * to large buffers (64K). Such pointers are normalized with a zero offset.
  99. * Since MSDOS is not a preemptive multitasking OS, this table is not
  100. * protected from concurrent access. This hack doesn't work anyway on
  101. * a protected system like OS/2. Use Microsoft C instead.
  102. */
  103. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  104. {
  105. voidpf buf = opaque; /* just to make some compilers happy */
  106. ulg bsize = (ulg)items*size;
  107. /* If we allocate less than 65520 bytes, we assume that farmalloc
  108. * will return a usable pointer which doesn't have to be normalized.
  109. */
  110. if (bsize < 65520L) {
  111. buf = farmalloc(bsize);
  112. if (*(ush*)&buf != 0) return buf;
  113. } else {
  114. buf = farmalloc(bsize + 16L);
  115. }
  116. if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  117. table[next_ptr].org_ptr = buf;
  118. /* Normalize the pointer to seg:0 */
  119. *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  120. *(ush*)&buf = 0;
  121. table[next_ptr++].new_ptr = buf;
  122. return buf;
  123. }
  124. void zcfree (voidpf opaque, voidpf ptr)
  125. {
  126. int n;
  127. if (*(ush*)&ptr != 0) { /* object < 64K */
  128. farfree(ptr);
  129. return;
  130. }
  131. /* Find the original pointer */
  132. for (n = 0; n < next_ptr; n++) {
  133. if (ptr != table[n].new_ptr) continue;
  134. farfree(table[n].org_ptr);
  135. while (++n < next_ptr) {
  136. table[n-1] = table[n];
  137. }
  138. next_ptr--;
  139. return;
  140. }
  141. ptr = opaque; /* just to make some compilers happy */
  142. Assert(0, "zcfree: ptr not found");
  143. }
  144. #endif
  145. #endif /* __TURBOC__ */
  146. #if defined(M_I86) && !defined(__32BIT__)
  147. /* Microsoft C in 16-bit mode */
  148. # define MY_ZCALLOC
  149. #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
  150. # define _halloc halloc
  151. # define _hfree hfree
  152. #endif
  153. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  154. {
  155. if (opaque) opaque = 0; /* to make compiler happy */
  156. return _halloc((long)items, size);
  157. }
  158. void zcfree (voidpf opaque, voidpf ptr)
  159. {
  160. if (opaque) opaque = 0; /* to make compiler happy */
  161. _hfree(ptr);
  162. }
  163. #endif /* MSC */
  164. #ifndef MY_ZCALLOC /* Any system without a special alloc function */
  165. #ifndef STDC
  166. extern voidp calloc OF((uInt items, uInt size));
  167. extern void free OF((voidpf ptr));
  168. #endif
  169. voidpf zcalloc (opaque, items, size)
  170. voidpf opaque;
  171. unsigned items;
  172. unsigned size;
  173. {
  174. if (opaque) items += size - size; /* make compiler happy */
  175. return (voidpf)calloc(items, size);
  176. }
  177. void zcfree (opaque, ptr)
  178. voidpf opaque;
  179. voidpf ptr;
  180. {
  181. free(ptr);
  182. if (opaque) return; /* make compiler happy */
  183. }
  184. #endif /* MY_ZCALLOC */