stdlibs/libz/zlib/zutil.cpp
changeset 65 c4aad78f92f5
parent 50 79045913e4e9
child 66 38bdaa106551
equal deleted inserted replaced
50:79045913e4e9 65:c4aad78f92f5
     1 /* zutil.cpp -- target dependent utility functions for the compression library
       
     2  * Copyright (C) 1995-2005 Jean-loup Gailly.
       
     3  * For conditions of distribution and use, see copyright notice in zlib.h
       
     4  */
       
     5 
       
     6 /* @(#) $Id: zutil.cpp,v 1.1.2.1 2008/08/14 15:26:58 e0222316 Exp $ */
       
     7 
       
     8 #ifdef SYMBIAN_EZLIB_DEVICE
       
     9   #include <e32base.h>
       
    10 #endif
       
    11 
       
    12 #include "zutil.h"
       
    13 
       
    14 
       
    15 #ifndef NO_DUMMY_DECL
       
    16 struct internal_state      {int dummy;}; /* for buggy compilers */
       
    17 #endif
       
    18 
       
    19 
       
    20 const char * const 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 #ifdef __SYMBIAN32__
       
    34 EXPORT_C const char *  zlibVersion_r()
       
    35 #else
       
    36 const char * ZEXPORT zlibVersion()
       
    37 #endif //__SYMBIAN32__
       
    38 {
       
    39     return ZLIB_VERSION;
       
    40 }
       
    41 
       
    42 
       
    43 #ifdef __SYMBIAN32__
       
    44 EXPORT_C uLong  zlibCompileFlags_r()
       
    45 #else
       
    46 uLong ZEXPORT zlibCompileFlags()
       
    47 #endif //__SYMBIAN32__
       
    48 {
       
    49     uLong flags;
       
    50 
       
    51     flags = 0;
       
    52     switch (sizeof(uInt)) {
       
    53     case 2:     break;
       
    54     case 4:     flags += 1;     break;
       
    55     case 8:     flags += 2;     break;
       
    56     default:    flags += 3;
       
    57     }
       
    58     switch (sizeof(uLong)) {
       
    59     case 2:     break;
       
    60     case 4:     flags += 1 << 2;        break;
       
    61     case 8:     flags += 2 << 2;        break;
       
    62     default:    flags += 3 << 2;
       
    63     }
       
    64     switch (sizeof(voidpf)) {
       
    65     case 2:     break;
       
    66     case 4:     flags += 1 << 4;        break;
       
    67     case 8:     flags += 2 << 4;        break;
       
    68     default:    flags += 3 << 4;
       
    69     }
       
    70     switch (sizeof(z_off_t)) {
       
    71     case 2:     break;
       
    72     case 4:     flags += 1 << 6;        break;
       
    73     case 8:     flags += 2 << 6;        break;
       
    74     default:    flags += 3 << 6;
       
    75     }
       
    76 #ifdef DEBUG
       
    77     flags += 1 << 8;
       
    78 #endif
       
    79 #if defined(ASMV) || defined(ASMINF)
       
    80     flags += 1 << 9;
       
    81 #endif
       
    82 #ifdef ZLIB_WINAPI
       
    83     flags += 1 << 10;
       
    84 #endif
       
    85 #ifdef BUILDFIXED
       
    86     flags += 1 << 12;
       
    87 #endif
       
    88 #ifdef DYNAMIC_CRC_TABLE
       
    89     flags += 1 << 13;
       
    90 #endif
       
    91 #ifdef NO_GZCOMPRESS
       
    92     flags += 1L << 16;
       
    93 #endif
       
    94 #ifdef NO_GZIP
       
    95     flags += 1L << 17;
       
    96 #endif
       
    97 #ifdef PKZIP_BUG_WORKAROUND
       
    98     flags += 1L << 20;
       
    99 #endif
       
   100 #ifdef FASTEST
       
   101     flags += 1L << 21;
       
   102 #endif
       
   103 #ifdef STDC
       
   104 #  ifdef NO_vsnprintf
       
   105         flags += 1L << 25;
       
   106 #    ifdef HAS_vsprintf_void
       
   107         flags += 1L << 26;
       
   108 #    endif
       
   109 #  else
       
   110 #    ifdef HAS_vsnprintf_void
       
   111         flags += 1L << 26;
       
   112 #    endif
       
   113 #  endif
       
   114 #else
       
   115         flags += 1L << 24;
       
   116 #  ifdef NO_snprintf
       
   117         flags += 1L << 25;
       
   118 #    ifdef HAS_sprintf_void
       
   119         flags += 1L << 26;
       
   120 #    endif
       
   121 #  else
       
   122 #    ifdef HAS_snprintf_void
       
   123         flags += 1L << 26;
       
   124 #    endif
       
   125 #  endif
       
   126 #endif
       
   127     return flags;
       
   128 }
       
   129 
       
   130 #ifdef DEBUG
       
   131 
       
   132 #  ifndef verbose
       
   133 #    define verbose 0
       
   134 #  endif
       
   135 int z_verbose = verbose;
       
   136 
       
   137 void z_error (m)
       
   138     char *m;
       
   139 {
       
   140     fprintf(stderr, "%s\n", m);
       
   141     exit(1);
       
   142 }
       
   143 #endif
       
   144 
       
   145 
       
   146 /* exported to allow conversion of error code to string for compress() and
       
   147  * uncompress()
       
   148  */
       
   149 #ifdef __SYMBIAN32__
       
   150 EXPORT_C const char *  zError_r(int err)
       
   151 #else
       
   152 const char * ZEXPORT zError(err)
       
   153     int err;
       
   154 #endif //__SYMBIAN32__
       
   155 {
       
   156     return ERR_MSG(err);
       
   157 }
       
   158 
       
   159 
       
   160 #if defined(_WIN32_WCE)
       
   161     /* The Microsoft C Run-Time Library for Windows CE doesn't have
       
   162      * errno.  We define it as a global variable to simplify porting.
       
   163      * Its value is always 0 and should not be used.
       
   164      */
       
   165     int errno = 0;
       
   166 #endif
       
   167 
       
   168 #ifndef HAVE_MEMCPY
       
   169 
       
   170 #ifdef __SYMBIAN32__
       
   171 void zmemcpy(    Bytef* dest,const Bytef*  source,uInt  len)
       
   172 #else
       
   173 void zmemcpy(dest, source, len)
       
   174     Bytef* dest;
       
   175     const Bytef* source;
       
   176     uInt  len;
       
   177 #endif //__SYMBIAN32__	
       
   178 {
       
   179     if (len == 0) return;
       
   180     do {
       
   181         *dest++ = *source++; /* ??? to be unrolled */
       
   182     } while (--len != 0);
       
   183 }
       
   184 
       
   185 #ifdef __SYMBIAN32__
       
   186 int zmemcmp(    const Bytef* s1,    const Bytef*  s2,uInt  len)
       
   187 #else	
       
   188 int zmemcmp(s1, s2, len)
       
   189     const Bytef* s1;
       
   190     const Bytef* s2;
       
   191     uInt  len;
       
   192 #endif //__SYMBIAN32__		
       
   193 {
       
   194     uInt j;
       
   195 
       
   196     for (j = 0; j < len; j++) {
       
   197         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
       
   198     }
       
   199     return 0;
       
   200 }
       
   201 
       
   202 #ifdef __SYMBIAN32__
       
   203 void zmemzero( Bytef* dest,uInt  len)
       
   204 #else	
       
   205 void zmemzero(dest, len)
       
   206     Bytef* dest;
       
   207     uInt  len;
       
   208 #endif //__SYMBIAN32__		
       
   209 {
       
   210     if (len == 0) return;
       
   211     do {
       
   212         *dest++ = 0;  /* ??? to be unrolled */
       
   213     } while (--len != 0);
       
   214 }
       
   215 #endif
       
   216 
       
   217 
       
   218 #ifdef SYS16BIT
       
   219 
       
   220 #ifdef __TURBOC__
       
   221 /* Turbo C in 16-bit mode */
       
   222 
       
   223 #  define MY_ZCALLOC
       
   224 
       
   225 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
       
   226  * and farmalloc(64K) returns a pointer with an offset of 8, so we
       
   227  * must fix the pointer. Warning: the pointer must be put back to its
       
   228  * original form in order to free it, use zcfree().
       
   229  */
       
   230 
       
   231 #define MAX_PTR 10
       
   232 /* 10*64K = 640K */
       
   233 
       
   234 local int next_ptr = 0;
       
   235 
       
   236 typedef struct ptr_table_s {
       
   237     voidpf org_ptr;
       
   238     voidpf new_ptr;
       
   239 } ptr_table;
       
   240 
       
   241 local ptr_table table[MAX_PTR];
       
   242 /* This table is used to remember the original form of pointers
       
   243  * to large buffers (64K). Such pointers are normalized with a zero offset.
       
   244  * Since MSDOS is not a preemptive multitasking OS, this table is not
       
   245  * protected from concurrent access. This hack doesn't work anyway on
       
   246  * a protected system like OS/2. Use Microsoft C instead.
       
   247  */
       
   248 
       
   249 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
       
   250 {
       
   251     voidpf buf = opaque; /* just to make some compilers happy */
       
   252     ulg bsize = (ulg)items*size;
       
   253 
       
   254     /* If we allocate less than 65520 bytes, we assume that farmalloc
       
   255      * will return a usable pointer which doesn't have to be normalized.
       
   256      */
       
   257     if (bsize < 65520L) {
       
   258         buf = farmalloc(bsize);
       
   259         if (*(ush*)&buf != 0) return buf;
       
   260     } else {
       
   261         buf = farmalloc(bsize + 16L);
       
   262     }
       
   263     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
       
   264     table[next_ptr].org_ptr = buf;
       
   265 
       
   266     /* Normalize the pointer to seg:0 */
       
   267     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
       
   268     *(ush*)&buf = 0;
       
   269     table[next_ptr++].new_ptr = buf;
       
   270     return buf;
       
   271 }
       
   272 
       
   273 void  zcfree (voidpf opaque, voidpf ptr)
       
   274 {
       
   275     int n;
       
   276     if (*(ush*)&ptr != 0) { /* object < 64K */
       
   277         farfree(ptr);
       
   278         return;
       
   279     }
       
   280     /* Find the original pointer */
       
   281     for (n = 0; n < next_ptr; n++) {
       
   282         if (ptr != table[n].new_ptr) continue;
       
   283 
       
   284         farfree(table[n].org_ptr);
       
   285         while (++n < next_ptr) {
       
   286             table[n-1] = table[n];
       
   287         }
       
   288         next_ptr--;
       
   289         return;
       
   290     }
       
   291     ptr = opaque; /* just to make some compilers happy */
       
   292     Assert(0, "zcfree: ptr not found");
       
   293 }
       
   294 
       
   295 #endif /* __TURBOC__ */
       
   296 
       
   297 
       
   298 #ifdef M_I86
       
   299 /* Microsoft C in 16-bit mode */
       
   300 
       
   301 #  define MY_ZCALLOC
       
   302 
       
   303 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
       
   304 #  define _halloc  halloc
       
   305 #  define _hfree   hfree
       
   306 #endif
       
   307 
       
   308 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
       
   309 {
       
   310     if (opaque) opaque = 0; /* to make compiler happy */
       
   311     return _halloc((long)items, size);
       
   312 }
       
   313 
       
   314 void  zcfree (voidpf opaque, voidpf ptr)
       
   315 {
       
   316     if (opaque) opaque = 0; /* to make compiler happy */
       
   317     _hfree(ptr);
       
   318 }
       
   319 
       
   320 #endif /* M_I86 */
       
   321 
       
   322 #endif /* SYS16BIT */
       
   323 
       
   324 
       
   325 
       
   326 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
       
   327 
       
   328 #ifndef STDC
       
   329 extern voidp  malloc OF((uInt size));
       
   330 extern voidp  calloc OF((uInt items, uInt size));
       
   331 extern void   free   OF((voidpf ptr));
       
   332 #endif
       
   333 
       
   334 #ifdef __SYMBIAN32__
       
   335 voidpf zcalloc (voidpf opaque,unsigned  items, unsigned size)
       
   336 #else 	
       
   337 voidpf zcalloc (opaque, items, size)
       
   338     voidpf opaque;
       
   339     unsigned items;
       
   340     unsigned size;
       
   341 #endif //__SYMBIAN32__	
       
   342 {
       
   343     if (opaque) items += size - size; /* make compiler happy */
       
   344 #ifndef SYMBIAN_EZLIB_DEVICE
       
   345   return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : (voidpf)calloc(items, size);
       
   346 #else
       
   347 	TInt bytes = items*size;
       
   348 	TAny *ptr = User::AllocZ(bytes); // Symbian Native memory allocation API
       
   349 	return (voidpf) ptr;
       
   350 #endif //SYMBIAN_EZLIB_DEVICE
       
   351 }
       
   352 
       
   353 #ifdef __SYMBIAN32__
       
   354 void  zcfree (voidpf opaque,voidpf  ptr)
       
   355 #else 	
       
   356 void  zcfree (opaque, ptr)
       
   357     voidpf opaque;
       
   358     voidpf ptr;
       
   359 #endif //__SYMBIAN32__	
       
   360 {
       
   361 #ifndef SYMBIAN_EZLIB_DEVICE
       
   362   free(ptr);
       
   363 #else
       
   364 	User::Free(ptr); // Symbian Native memory deallocation API
       
   365 #endif //SYMBIAN_EZLIB_DEVICE
       
   366     if (opaque) return; /* make compiler happy */
       
   367 }
       
   368 
       
   369 #endif /* MY_ZCALLOC */
       
   370 
       
   371