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