zutil.c 4.9 KB

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