Basic gzip/gunzip in memory buffer examples using zlib code.
Embed:
(wiki syntax)
Show/hide line numbers
zutil.c
00001 /* zutil.c -- target dependent utility functions for the compression library 00002 * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly. 00003 * For conditions of distribution and use, see copyright notice in zlib.h 00004 */ 00005 00006 /* @(#) $Id$ */ 00007 00008 #include "zutil.h" 00009 #ifndef Z_SOLO 00010 # include "gzguts.h" 00011 #endif 00012 00013 #ifndef NO_DUMMY_DECL 00014 struct internal_state {int dummy;}; /* for buggy compilers */ 00015 #endif 00016 00017 const char * const z_errmsg[10] = { 00018 "need dictionary", /* Z_NEED_DICT 2 */ 00019 "stream end", /* Z_STREAM_END 1 */ 00020 "", /* Z_OK 0 */ 00021 "file error", /* Z_ERRNO (-1) */ 00022 "stream error", /* Z_STREAM_ERROR (-2) */ 00023 "data error", /* Z_DATA_ERROR (-3) */ 00024 "insufficient memory", /* Z_MEM_ERROR (-4) */ 00025 "buffer error", /* Z_BUF_ERROR (-5) */ 00026 "incompatible version",/* Z_VERSION_ERROR (-6) */ 00027 ""}; 00028 00029 00030 const char * ZEXPORT zlibVersion() 00031 { 00032 return ZLIB_VERSION; 00033 } 00034 00035 uLong ZEXPORT zlibCompileFlags() 00036 { 00037 uLong flags; 00038 00039 flags = 0; 00040 switch ((int)(sizeof(uInt))) { 00041 case 2: break; 00042 case 4: flags += 1; break; 00043 case 8: flags += 2; break; 00044 default: flags += 3; 00045 } 00046 switch ((int)(sizeof(uLong))) { 00047 case 2: break; 00048 case 4: flags += 1 << 2; break; 00049 case 8: flags += 2 << 2; break; 00050 default: flags += 3 << 2; 00051 } 00052 switch ((int)(sizeof(voidpf))) { 00053 case 2: break; 00054 case 4: flags += 1 << 4; break; 00055 case 8: flags += 2 << 4; break; 00056 default: flags += 3 << 4; 00057 } 00058 switch ((int)(sizeof(z_off_t))) { 00059 case 2: break; 00060 case 4: flags += 1 << 6; break; 00061 case 8: flags += 2 << 6; break; 00062 default: flags += 3 << 6; 00063 } 00064 #ifdef DEBUG 00065 flags += 1 << 8; 00066 #endif 00067 #if defined(ASMV) || defined(ASMINF) 00068 flags += 1 << 9; 00069 #endif 00070 #ifdef ZLIB_WINAPI 00071 flags += 1 << 10; 00072 #endif 00073 #ifdef BUILDFIXED 00074 flags += 1 << 12; 00075 #endif 00076 #ifdef DYNAMIC_CRC_TABLE 00077 flags += 1 << 13; 00078 #endif 00079 #ifdef NO_GZCOMPRESS 00080 flags += 1L << 16; 00081 #endif 00082 #ifdef NO_GZIP 00083 flags += 1L << 17; 00084 #endif 00085 #ifdef PKZIP_BUG_WORKAROUND 00086 flags += 1L << 20; 00087 #endif 00088 #ifdef FASTEST 00089 flags += 1L << 21; 00090 #endif 00091 #if defined(STDC) || defined(Z_HAVE_STDARG_H) 00092 # ifdef NO_vsnprintf 00093 flags += 1L << 25; 00094 # ifdef HAS_vsprintf_void 00095 flags += 1L << 26; 00096 # endif 00097 # else 00098 # ifdef HAS_vsnprintf_void 00099 flags += 1L << 26; 00100 # endif 00101 # endif 00102 #else 00103 flags += 1L << 24; 00104 # ifdef NO_snprintf 00105 flags += 1L << 25; 00106 # ifdef HAS_sprintf_void 00107 flags += 1L << 26; 00108 # endif 00109 # else 00110 # ifdef HAS_snprintf_void 00111 flags += 1L << 26; 00112 # endif 00113 # endif 00114 #endif 00115 return flags; 00116 } 00117 00118 #ifdef DEBUG 00119 00120 # ifndef verbose 00121 # define verbose 0 00122 # endif 00123 int ZLIB_INTERNAL z_verbose = verbose; 00124 00125 void ZLIB_INTERNAL z_error (m) 00126 char *m; 00127 { 00128 fprintf(stderr, "%s\n", m); 00129 exit(1); 00130 } 00131 #endif 00132 00133 /* exported to allow conversion of error code to string for compress() and 00134 * uncompress() 00135 */ 00136 const char * ZEXPORT zError(err) 00137 int err; 00138 { 00139 return ERR_MSG(err); 00140 } 00141 00142 #if defined(_WIN32_WCE) 00143 /* The Microsoft C Run-Time Library for Windows CE doesn't have 00144 * errno. We define it as a global variable to simplify porting. 00145 * Its value is always 0 and should not be used. 00146 */ 00147 int errno = 0; 00148 #endif 00149 00150 #ifndef HAVE_MEMCPY 00151 00152 void ZLIB_INTERNAL zmemcpy(dest, source, len) 00153 Bytef* dest; 00154 const Bytef* source; 00155 uInt len; 00156 { 00157 if (len == 0) return; 00158 do { 00159 *dest++ = *source++; /* ??? to be unrolled */ 00160 } while (--len != 0); 00161 } 00162 00163 int ZLIB_INTERNAL zmemcmp(s1, s2, len) 00164 const Bytef* s1; 00165 const Bytef* s2; 00166 uInt len; 00167 { 00168 uInt j; 00169 00170 for (j = 0; j < len; j++) { 00171 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; 00172 } 00173 return 0; 00174 } 00175 00176 void ZLIB_INTERNAL zmemzero(dest, len) 00177 Bytef* dest; 00178 uInt len; 00179 { 00180 if (len == 0) return; 00181 do { 00182 *dest++ = 0; /* ??? to be unrolled */ 00183 } while (--len != 0); 00184 } 00185 #endif 00186 00187 #ifndef Z_SOLO 00188 00189 #ifdef SYS16BIT 00190 00191 #ifdef __TURBOC__ 00192 /* Turbo C in 16-bit mode */ 00193 00194 # define MY_ZCALLOC 00195 00196 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes 00197 * and farmalloc(64K) returns a pointer with an offset of 8, so we 00198 * must fix the pointer. Warning: the pointer must be put back to its 00199 * original form in order to free it, use zcfree(). 00200 */ 00201 00202 #define MAX_PTR 10 00203 /* 10*64K = 640K */ 00204 00205 local int next_ptr = 0; 00206 00207 typedef struct ptr_table_s { 00208 voidpf org_ptr; 00209 voidpf new_ptr; 00210 } ptr_table; 00211 00212 local ptr_table table[MAX_PTR]; 00213 /* This table is used to remember the original form of pointers 00214 * to large buffers (64K). Such pointers are normalized with a zero offset. 00215 * Since MSDOS is not a preemptive multitasking OS, this table is not 00216 * protected from concurrent access. This hack doesn't work anyway on 00217 * a protected system like OS/2. Use Microsoft C instead. 00218 */ 00219 00220 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) 00221 { 00222 voidpf buf = opaque; /* just to make some compilers happy */ 00223 ulg bsize = (ulg)items*size; 00224 00225 /* If we allocate less than 65520 bytes, we assume that farmalloc 00226 * will return a usable pointer which doesn't have to be normalized. 00227 */ 00228 if (bsize < 65520L) { 00229 buf = farmalloc(bsize); 00230 if (*(ush*)&buf != 0) return buf; 00231 } else { 00232 buf = farmalloc(bsize + 16L); 00233 } 00234 if (buf == NULL || next_ptr >= MAX_PTR) return NULL; 00235 table[next_ptr].org_ptr = buf; 00236 00237 /* Normalize the pointer to seg:0 */ 00238 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; 00239 *(ush*)&buf = 0; 00240 table[next_ptr++].new_ptr = buf; 00241 return buf; 00242 } 00243 00244 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) 00245 { 00246 int n; 00247 if (*(ush*)&ptr != 0) { /* object < 64K */ 00248 farfree(ptr); 00249 return; 00250 } 00251 /* Find the original pointer */ 00252 for (n = 0; n < next_ptr; n++) { 00253 if (ptr != table[n].new_ptr) continue; 00254 00255 farfree(table[n].org_ptr); 00256 while (++n < next_ptr) { 00257 table[n-1] = table[n]; 00258 } 00259 next_ptr--; 00260 return; 00261 } 00262 ptr = opaque; /* just to make some compilers happy */ 00263 Assert(0, "zcfree: ptr not found"); 00264 } 00265 00266 #endif /* __TURBOC__ */ 00267 00268 00269 #ifdef M_I86 00270 /* Microsoft C in 16-bit mode */ 00271 00272 # define MY_ZCALLOC 00273 00274 #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) 00275 # define _halloc halloc 00276 # define _hfree hfree 00277 #endif 00278 00279 voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) 00280 { 00281 if (opaque) opaque = 0; /* to make compiler happy */ 00282 return _halloc((long)items, size); 00283 } 00284 00285 void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) 00286 { 00287 if (opaque) opaque = 0; /* to make compiler happy */ 00288 _hfree(ptr); 00289 } 00290 00291 #endif /* M_I86 */ 00292 00293 #endif /* SYS16BIT */ 00294 00295 00296 #ifndef MY_ZCALLOC /* Any system without a special alloc function */ 00297 00298 #ifndef STDC 00299 extern voidp malloc OF((uInt size)); 00300 extern voidp calloc OF((uInt items, uInt size)); 00301 extern void free OF((voidpf ptr)); 00302 #endif 00303 00304 voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) 00305 voidpf opaque; 00306 unsigned items; 00307 unsigned size; 00308 { 00309 if (opaque) items += size - size; /* make compiler happy */ 00310 return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : 00311 (voidpf)calloc(items, size); 00312 } 00313 00314 void ZLIB_INTERNAL zcfree (opaque, ptr) 00315 voidpf opaque; 00316 voidpf ptr; 00317 { 00318 free(ptr); 00319 if (opaque) return; /* make compiler happy */ 00320 } 00321 00322 #endif /* MY_ZCALLOC */ 00323 00324 #endif /* !Z_SOLO */
Generated on Wed Jul 13 2022 09:05:31 by
1.7.2