zutil.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:4k
源码类别:

浏览器

开发平台:

Unix_Linux

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