zutil.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: zutil.c,v 1.2 2002/11/06 22:32:54 davidT Exp $ */
  6. #include "zutil.h"
  7. #ifndef STDC
  8. extern void exit OF((int));
  9. #endif
  10. #ifndef HAVE_MEMCPY
  11. void zmemcpy(dest, source, len)
  12. Bytef* dest;
  13. const Bytef* source;
  14. uInt len;
  15. {
  16. if (len == 0) return;
  17. do {
  18. *dest++ = *source++; /* ??? to be unrolled */
  19. } while (--len != 0);
  20. }
  21. int zmemcmp(s1, s2, len)
  22. const Bytef* s1;
  23. const Bytef* s2;
  24. uInt len;
  25. {
  26. uInt j;
  27. for (j = 0; j < len; j++) {
  28. if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  29. }
  30. return 0;
  31. }
  32. void zmemzero(dest, len)
  33. Bytef* dest;
  34. uInt len;
  35. {
  36. if (len == 0) return;
  37. do {
  38. *dest++ = 0; /* ??? to be unrolled */
  39. } while (--len != 0);
  40. }
  41. #endif
  42. #ifdef __TURBOC__
  43. #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
  44. /* Small and medium model in Turbo C are for now limited to near allocation
  45. * with reduced MAX_WBITS and MAX_MEM_LEVEL
  46. */
  47. # define MY_ZCALLOC
  48. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  49. * and farmalloc(64K) returns a pointer with an offset of 8, so we
  50. * must fix the pointer. Warning: the pointer must be put back to its
  51. * original form in order to free it, use zcfree().
  52. */
  53. #define MAX_PTR 10
  54. /* 10*64K = 640K */
  55. local int next_ptr = 0;
  56. typedef struct ptr_table_s {
  57. voidpf org_ptr;
  58. voidpf new_ptr;
  59. } ptr_table;
  60. local ptr_table table[MAX_PTR];
  61. /* This table is used to remember the original form of pointers
  62. * to large buffers (64K). Such pointers are normalized with a zero offset.
  63. * Since MSDOS is not a preemptive multitasking OS, this table is not
  64. * protected from concurrent access. This hack doesn't work anyway on
  65. * a protected system like OS/2. Use Microsoft C instead.
  66. */
  67. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  68. {
  69. voidpf buf = opaque; /* just to make some compilers happy */
  70. ulg bsize = (ulg)items*size;
  71. /* If we allocate less than 65520 bytes, we assume that farmalloc
  72. * will return a usable pointer which doesn't have to be normalized.
  73. */
  74. if (bsize < 65520L) {
  75. buf = farmalloc(bsize);
  76. if (*(ush*)&buf != 0) return buf;
  77. } else {
  78. buf = farmalloc(bsize + 16L);
  79. }
  80. if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  81. table[next_ptr].org_ptr = buf;
  82. /* Normalize the pointer to seg:0 */
  83. *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  84. *(ush*)&buf = 0;
  85. table[next_ptr++].new_ptr = buf;
  86. return buf;
  87. }
  88. void zcfree (voidpf opaque, voidpf ptr)
  89. {
  90. int n;
  91. if (*(ush*)&ptr != 0) { /* object < 64K */
  92. farfree(ptr);
  93. return;
  94. }
  95. /* Find the original pointer */
  96. for (n = 0; n < next_ptr; n++) {
  97. if (ptr != table[n].new_ptr) continue;
  98. farfree(table[n].org_ptr);
  99. while (++n < next_ptr) {
  100. table[n-1] = table[n];
  101. }
  102. next_ptr--;
  103. return;
  104. }
  105. ptr = opaque; /* just to make some compilers happy */
  106. Assert(0, "zcfree: ptr not found");
  107. }
  108. #endif
  109. #endif /* __TURBOC__ */
  110. #if defined(M_I86) && !defined(__32BIT__)
  111. /* Microsoft C in 16-bit mode */
  112. # define MY_ZCALLOC
  113. #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
  114. # define _halloc halloc
  115. # define _hfree hfree
  116. #endif
  117. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  118. {
  119. if (opaque) opaque = 0; /* to make compiler happy */
  120. return _halloc((long)items, size);
  121. }
  122. void zcfree (voidpf opaque, voidpf ptr)
  123. {
  124. if (opaque) opaque = 0; /* to make compiler happy */
  125. _hfree(ptr);
  126. }
  127. #endif /* MSC */
  128. #ifndef MY_ZCALLOC /* Any system without a special alloc function */
  129. #ifndef STDC
  130. extern voidp calloc OF((uInt items, uInt size));
  131. extern void free OF((voidpf ptr));
  132. #endif
  133. voidpf zcalloc (opaque, items, size)
  134. voidpf opaque;
  135. unsigned items;
  136. unsigned size;
  137. {
  138. if (opaque) items += size - size; /* make compiler happy */
  139. return (voidpf)calloc(items, size);
  140. }
  141. void zcfree (opaque, ptr)
  142. voidpf opaque;
  143. voidpf ptr;
  144. {
  145. free(ptr);
  146. if (opaque) return; /* make compiler happy */
  147. }
  148. #endif /* MY_ZCALLOC */