home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / zlib / src / zutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.2 KB  |  213 lines

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