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