[2c47b73] | 1 | /** |
---|
| 2 | * @file callZlib.c |
---|
| 3 | * @author Sheng Di |
---|
| 4 | * @date June, 2016 |
---|
| 5 | * @brief gzip compressor code: the interface to call zlib |
---|
| 6 | * (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory. |
---|
| 7 | * See COPYRIGHT in top-level directory. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | #include <stdio.h> |
---|
| 12 | #include <stdlib.h> |
---|
| 13 | #include <zlib.h> |
---|
| 14 | #include <sz.h> |
---|
| 15 | |
---|
| 16 | #if MAX_MEM_LEVEL >= 8 |
---|
| 17 | #define DEF_MEM_LEVEL 8 |
---|
| 18 | #else |
---|
| 19 | #define DEF_MEM_LEVEL MAX_MEM_LEVEL |
---|
| 20 | #endif |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | #define CHECK_ERR(err, msg) { \ |
---|
| 24 | if (err != Z_OK && err != Z_STREAM_END) { \ |
---|
| 25 | fprintf(stderr, "%s error: %d\n", msg, err); \ |
---|
| 26 | return SZ_NSCS; \ |
---|
| 27 | } \ |
---|
| 28 | } |
---|
| 29 | |
---|
[9ee2ce3] | 30 | int isZlibFormat(unsigned char magic1, unsigned char magic2) |
---|
| 31 | { |
---|
| 32 | if(magic1==104&&magic2==5) //DC+BS |
---|
| 33 | return 1; |
---|
| 34 | if(magic1==104&&magic2==129) //DC+DC |
---|
| 35 | return 1; |
---|
| 36 | if(magic1==104&&magic2==222) //DC+BC |
---|
| 37 | return 1; |
---|
| 38 | if(magic1==120&&magic2==1) //BC+BS |
---|
| 39 | return 1; |
---|
| 40 | if(magic1==120&&magic2==94) //BC+? |
---|
| 41 | return 1; |
---|
| 42 | if(magic1==120&&magic2==156) //BC+DC |
---|
| 43 | return 1; |
---|
| 44 | if(magic1==120&&magic2==218) //BC+BS |
---|
| 45 | return 1; |
---|
| 46 | return 0; |
---|
| 47 | } |
---|
| 48 | |
---|
[2c47b73] | 49 | /*zlib_compress() is only valid for median-size data compression. */ |
---|
| 50 | unsigned long zlib_compress(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level) |
---|
| 51 | { |
---|
| 52 | z_stream stream = {0}; |
---|
| 53 | |
---|
| 54 | stream.next_in = data; |
---|
| 55 | stream.avail_in = dataLength; |
---|
| 56 | #ifdef MAXSEG_64K |
---|
| 57 | /* Check for source > 64K on 16-bit machine: */ |
---|
| 58 | if ((uLong)stream.avail_in != dataLength) return Z_BUF_ERROR; |
---|
| 59 | #endif |
---|
| 60 | |
---|
| 61 | uLong estCmpLen = deflateBound(&stream, dataLength); |
---|
| 62 | unsigned long outSize = estCmpLen; |
---|
| 63 | |
---|
| 64 | *compressBytes = (unsigned char*)malloc(sizeof(unsigned char)*estCmpLen); |
---|
| 65 | int err = compress2(*compressBytes, &outSize, data, dataLength, level); |
---|
| 66 | if(err!=Z_OK) |
---|
| 67 | { |
---|
| 68 | printf("Error: err_code=%d; the reason may be your data size is too large (>=2^32), which cannot be compressed by standalone zlib_compress. Sol: inflace_init, ....\n", err); |
---|
| 69 | exit(0); |
---|
| 70 | } |
---|
| 71 | return outSize; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | unsigned long zlib_compress2(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level) |
---|
| 75 | { |
---|
| 76 | unsigned long outSize; |
---|
| 77 | |
---|
| 78 | z_stream stream = {0}; |
---|
| 79 | int err; |
---|
| 80 | |
---|
| 81 | stream.next_in = data; |
---|
| 82 | stream.avail_in = dataLength; |
---|
| 83 | #ifdef MAXSEG_64K |
---|
| 84 | /* Check for source > 64K on 16-bit machine: */ |
---|
| 85 | if ((uLong)stream.avail_in != dataLength) return Z_BUF_ERROR; |
---|
| 86 | #endif |
---|
| 87 | |
---|
| 88 | uLong estCmpLen = deflateBound(&stream, dataLength); |
---|
| 89 | *compressBytes = (unsigned char*)malloc(sizeof(unsigned char)*estCmpLen); |
---|
| 90 | |
---|
| 91 | stream.next_out = *compressBytes; |
---|
| 92 | stream.avail_out = estCmpLen; |
---|
| 93 | //stream.avail_out = dataLength*10; |
---|
| 94 | //if ((uLong)stream.avail_out != dataLength*10) return Z_BUF_ERROR; |
---|
| 95 | |
---|
| 96 | stream.zalloc = (alloc_func)0; |
---|
| 97 | stream.zfree = (free_func)0; |
---|
| 98 | stream.opaque = (voidpf)0; |
---|
| 99 | // stream.data_type = Z_TEXT; |
---|
| 100 | |
---|
| 101 | //err = deflateInit(&stream, level); //default windowBits == 15. |
---|
| 102 | int windowBits = 14; //8-15 |
---|
| 103 | if(confparams_cpr->szMode==SZ_BEST_COMPRESSION) |
---|
| 104 | windowBits = 15; |
---|
| 105 | |
---|
| 106 | err = deflateInit2(&stream, level, Z_DEFLATED, windowBits, DEF_MEM_LEVEL, |
---|
| 107 | Z_DEFAULT_STRATEGY);//Z_FIXED); //Z_DEFAULT_STRATEGY |
---|
| 108 | if (err != Z_OK) return err; |
---|
| 109 | |
---|
| 110 | err = deflate(&stream, Z_FINISH); |
---|
| 111 | if (err != Z_STREAM_END) { |
---|
| 112 | deflateEnd(&stream); |
---|
| 113 | return err == Z_OK ? Z_BUF_ERROR : err; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | err = deflateEnd(&stream); |
---|
| 117 | |
---|
| 118 | outSize = stream.total_out; |
---|
| 119 | return outSize; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | unsigned long zlib_compress3(unsigned char* data, unsigned long dataLength, unsigned char* compressBytes, int level) |
---|
| 123 | { |
---|
| 124 | unsigned long outSize = 0; |
---|
| 125 | |
---|
| 126 | z_stream stream = {0}; |
---|
| 127 | int err; |
---|
| 128 | |
---|
| 129 | stream.next_in = data; |
---|
| 130 | stream.avail_in = dataLength; |
---|
| 131 | #ifdef MAXSEG_64K |
---|
| 132 | /* Check for source > 64K on 16-bit machine: */ |
---|
| 133 | if ((uLong)stream.avail_in != dataLength) return Z_BUF_ERROR; |
---|
| 134 | #endif |
---|
| 135 | |
---|
| 136 | stream.next_out = compressBytes; |
---|
| 137 | stream.avail_out = dataLength; |
---|
| 138 | stream.zalloc = (alloc_func)0; |
---|
| 139 | stream.zfree = (free_func)0; |
---|
| 140 | stream.opaque = (voidpf)0; |
---|
| 141 | |
---|
| 142 | //err = deflateInit(&stream, level); //default windowBits == 15. |
---|
| 143 | int windowBits = 14; //8-15 |
---|
| 144 | if(confparams_cpr->szMode==SZ_BEST_COMPRESSION) |
---|
| 145 | windowBits = 15; |
---|
| 146 | |
---|
| 147 | err = deflateInit2(&stream, level, Z_DEFLATED, windowBits, DEF_MEM_LEVEL, |
---|
| 148 | Z_DEFAULT_STRATEGY);//Z_FIXED); //Z_DEFAULT_STRATEGY |
---|
| 149 | if (err != Z_OK) return err; |
---|
| 150 | |
---|
| 151 | err = deflate(&stream, Z_FINISH); |
---|
| 152 | if (err != Z_STREAM_END) { |
---|
| 153 | deflateEnd(&stream); |
---|
| 154 | return err == Z_OK ? Z_BUF_ERROR : err; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | err = deflateEnd(&stream); |
---|
| 158 | |
---|
| 159 | outSize = stream.total_out; |
---|
| 160 | return outSize; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | unsigned long zlib_compress4(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level) |
---|
| 164 | { |
---|
| 165 | z_stream c_stream = {0}; /* compression stream */ |
---|
| 166 | int err = 0; |
---|
| 167 | |
---|
| 168 | c_stream.zalloc = (alloc_func)0; |
---|
| 169 | c_stream.zfree = (free_func)0; |
---|
| 170 | c_stream.opaque = (voidpf)0; |
---|
| 171 | |
---|
| 172 | int windowBits = 14; //8-15 |
---|
| 173 | if(confparams_cpr->szMode==SZ_BEST_COMPRESSION) |
---|
| 174 | windowBits = 15; |
---|
| 175 | |
---|
| 176 | err = deflateInit2(&c_stream, level, Z_DEFLATED, windowBits, DEF_MEM_LEVEL, |
---|
| 177 | Z_DEFAULT_STRATEGY);//Z_FIXED); //Z_DEFAULT_STRATEGY |
---|
| 178 | CHECK_ERR(err, "deflateInit"); |
---|
| 179 | |
---|
| 180 | uLong estCmpLen = deflateBound(&c_stream, dataLength); |
---|
| 181 | *compressBytes = (unsigned char*)malloc(sizeof(unsigned char)*estCmpLen); |
---|
| 182 | |
---|
| 183 | c_stream.next_in = data; |
---|
| 184 | c_stream.next_out = *compressBytes; |
---|
| 185 | |
---|
| 186 | while (c_stream.total_in < dataLength && c_stream.total_out < estCmpLen) { |
---|
| 187 | c_stream.avail_in = c_stream.avail_out = SZ_ZLIB_BUFFER_SIZE; /* force small buffers */ |
---|
| 188 | err = deflate(&c_stream, Z_NO_FLUSH); |
---|
| 189 | CHECK_ERR(err, "deflate"); |
---|
| 190 | } |
---|
| 191 | /* Finish the stream, still forcing small buffers: */ |
---|
| 192 | for (;;) { |
---|
| 193 | c_stream.avail_out = 1; |
---|
| 194 | err = deflate(&c_stream, Z_FINISH); |
---|
| 195 | if (err == Z_STREAM_END) break; |
---|
| 196 | CHECK_ERR(err, "deflate"); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | err = deflateEnd(&c_stream); |
---|
| 200 | CHECK_ERR(err, "deflateEnd"); |
---|
| 201 | |
---|
| 202 | return c_stream.total_out; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | unsigned long zlib_compress5(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level) |
---|
| 206 | { |
---|
| 207 | int ret, flush; |
---|
| 208 | unsigned have; |
---|
| 209 | z_stream strm; |
---|
| 210 | unsigned char* in = data; |
---|
| 211 | |
---|
| 212 | /* allocate deflate state */ |
---|
| 213 | strm.zalloc = Z_NULL; |
---|
| 214 | strm.zfree = Z_NULL; |
---|
| 215 | strm.opaque = Z_NULL; |
---|
| 216 | ret = deflateInit(&strm, level); |
---|
[9ee2ce3] | 217 | //int windowBits = 15; |
---|
| 218 | //ret = deflateInit2(&strm, level, Z_DEFLATED, windowBits, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);//Z_FIXED); //Z_DEFAULT_STRATEGY |
---|
| 219 | |
---|
[2c47b73] | 220 | if (ret != Z_OK) |
---|
| 221 | return ret; |
---|
| 222 | |
---|
| 223 | size_t p_size = 0, av_in = 0; |
---|
| 224 | uLong estCmpLen = deflateBound(&strm, dataLength); |
---|
| 225 | *compressBytes = (unsigned char*)malloc(sizeof(unsigned char)*estCmpLen); |
---|
| 226 | unsigned char* out = *compressBytes; |
---|
| 227 | |
---|
| 228 | /* compress until end of file */ |
---|
| 229 | do { |
---|
| 230 | p_size += SZ_ZLIB_BUFFER_SIZE; |
---|
| 231 | if(p_size>=dataLength) |
---|
| 232 | { |
---|
| 233 | av_in = dataLength - (p_size - SZ_ZLIB_BUFFER_SIZE); |
---|
| 234 | flush = Z_FINISH; |
---|
| 235 | } |
---|
| 236 | else |
---|
| 237 | { |
---|
| 238 | av_in = SZ_ZLIB_BUFFER_SIZE; |
---|
| 239 | flush = Z_NO_FLUSH; |
---|
| 240 | } |
---|
| 241 | strm.avail_in = av_in; |
---|
| 242 | strm.next_in = in; |
---|
| 243 | |
---|
| 244 | /* run deflate() on input until output buffer not full, finish |
---|
| 245 | compression if all of source has been read in */ |
---|
| 246 | do { |
---|
| 247 | strm.avail_out = SZ_ZLIB_BUFFER_SIZE; |
---|
| 248 | strm.next_out = out; |
---|
| 249 | ret = deflate(&strm, flush); /* no bad return value */ |
---|
| 250 | |
---|
| 251 | have = SZ_ZLIB_BUFFER_SIZE - strm.avail_out; |
---|
| 252 | out += have; |
---|
| 253 | } while (strm.avail_out == 0); |
---|
| 254 | |
---|
| 255 | in+=av_in; |
---|
| 256 | |
---|
| 257 | /* done when last data in file processed */ |
---|
| 258 | } while (flush != Z_FINISH); |
---|
| 259 | |
---|
| 260 | /* clean up and return */ |
---|
| 261 | (void)deflateEnd(&strm); |
---|
| 262 | |
---|
| 263 | return strm.total_out; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | unsigned long zlib_uncompress(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize) |
---|
| 267 | { |
---|
| 268 | unsigned long outSize = targetOriSize; |
---|
| 269 | *oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize); |
---|
| 270 | int status = uncompress(*oriData, &outSize, compressBytes, cmpSize); |
---|
| 271 | if(status!=Z_OK) |
---|
| 272 | { |
---|
| 273 | printf("Error: Zlib decompression error; status=%d\n", status); |
---|
| 274 | exit(0); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | return outSize; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | unsigned long zlib_uncompress2 (unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize) |
---|
| 281 | { |
---|
| 282 | z_stream stream = {0}; |
---|
| 283 | |
---|
| 284 | unsigned long outSize; |
---|
| 285 | *oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize); |
---|
| 286 | |
---|
| 287 | stream.zalloc = Z_NULL; |
---|
| 288 | stream.zfree = Z_NULL; |
---|
| 289 | stream.opaque = Z_NULL; |
---|
| 290 | // stream.data_type = Z_TEXT; |
---|
| 291 | |
---|
| 292 | stream.next_in = compressBytes; |
---|
| 293 | stream.avail_in = cmpSize; |
---|
| 294 | /* Check for source > 64K on 16-bit machine: */ |
---|
| 295 | if ((unsigned long)stream.avail_in != cmpSize) |
---|
| 296 | { |
---|
| 297 | printf("Error: zlib_uncompress2: stream.avail_in != cmpSize"); |
---|
| 298 | //exit(1); |
---|
| 299 | return SZ_NSCS; //-1 |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | stream.next_out = *oriData; |
---|
| 303 | stream.avail_out = targetOriSize; |
---|
| 304 | //if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; |
---|
| 305 | |
---|
| 306 | int err = inflateInit(&stream); |
---|
| 307 | //int windowBits = 15; |
---|
| 308 | //int err = inflateInit2(&stream, windowBits); |
---|
| 309 | if (err != Z_OK) |
---|
| 310 | { |
---|
| 311 | printf("Error: zlib_uncompress2: err != Z_OK\n"); |
---|
| 312 | return SZ_NSCS; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | err = inflate(&stream, Z_FINISH); |
---|
| 316 | if (err != Z_STREAM_END) { |
---|
| 317 | inflateEnd(&stream); |
---|
| 318 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) |
---|
| 319 | return Z_DATA_ERROR; |
---|
| 320 | return err; |
---|
| 321 | } |
---|
| 322 | outSize = stream.total_out; |
---|
| 323 | inflateEnd(&stream); |
---|
| 324 | return outSize; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | unsigned long zlib_uncompress3(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize) |
---|
| 328 | { |
---|
| 329 | int status; |
---|
| 330 | z_stream z_strm; /* decompression stream */ |
---|
| 331 | |
---|
| 332 | size_t nalloc = 65536*4; |
---|
| 333 | |
---|
| 334 | *oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize); |
---|
| 335 | memset(&z_strm, 0, sizeof(z_strm)); |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | /*d_stream.zalloc = (alloc_func)0; |
---|
| 339 | d_stream.zfree = (free_func)0; |
---|
| 340 | d_stream.opaque = (voidpf)0;*/ |
---|
| 341 | |
---|
| 342 | z_strm.next_in = compressBytes; |
---|
| 343 | z_strm.avail_in = 0; |
---|
| 344 | z_strm.next_out = *oriData; |
---|
| 345 | z_strm.avail_out = targetOriSize; |
---|
| 346 | |
---|
| 347 | status = inflateInit(&z_strm); |
---|
| 348 | CHECK_ERR(status, "inflateInit"); |
---|
| 349 | |
---|
| 350 | do{ |
---|
| 351 | z_strm.avail_in = z_strm.avail_out = SZ_ZLIB_BUFFER_SIZE; /* force small buffers */ |
---|
| 352 | /* Uncompress some data */ |
---|
| 353 | status = inflate(&z_strm, Z_SYNC_FLUSH); |
---|
| 354 | |
---|
| 355 | /* Check if we are done uncompressing data */ |
---|
| 356 | if (Z_STREAM_END==status) |
---|
| 357 | break; /*done*/ |
---|
| 358 | |
---|
| 359 | if (Z_OK!=status) { |
---|
| 360 | (void)inflateEnd(&z_strm); |
---|
| 361 | printf("Error: inflate() failed\n"); |
---|
| 362 | exit(0); |
---|
| 363 | } |
---|
| 364 | else |
---|
| 365 | { |
---|
| 366 | /* If we're not done and just ran out of buffer space, get more */ |
---|
| 367 | if(0 == z_strm.avail_out) { |
---|
| 368 | void *new_outbuf; /* Pointer to new output buffer */ |
---|
| 369 | |
---|
| 370 | /* Allocate a buffer twice as big */ |
---|
| 371 | nalloc *= 2; |
---|
| 372 | if(NULL == (new_outbuf = realloc(*oriData, nalloc))) { |
---|
| 373 | (void)inflateEnd(&z_strm); |
---|
| 374 | printf("Error: memory allocation failed for deflate uncompression\n"); |
---|
| 375 | exit(0); |
---|
| 376 | } /* end if */ |
---|
| 377 | *oriData = new_outbuf; |
---|
| 378 | |
---|
| 379 | /* Update pointers to buffer for next set of uncompressed data */ |
---|
| 380 | z_strm.next_out = (*oriData) + z_strm.total_out; |
---|
| 381 | z_strm.avail_out = (uInt)(nalloc - z_strm.total_out); |
---|
| 382 | } /* end if */ |
---|
| 383 | } /* end else*/ |
---|
| 384 | }while(status==Z_OK); |
---|
| 385 | |
---|
| 386 | status = inflateEnd(&z_strm); |
---|
| 387 | CHECK_ERR(status, "inflateEnd"); |
---|
| 388 | |
---|
| 389 | return z_strm.total_out; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | unsigned long zlib_uncompress4(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize) |
---|
| 393 | { |
---|
| 394 | int ret; |
---|
| 395 | unsigned int have; |
---|
| 396 | z_stream strm; |
---|
| 397 | unsigned char *in = compressBytes; |
---|
| 398 | unsigned char *out; |
---|
| 399 | |
---|
| 400 | *oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize); |
---|
| 401 | out = *oriData; |
---|
| 402 | |
---|
| 403 | /* allocate inflate state */ |
---|
| 404 | strm.zalloc = Z_NULL; |
---|
| 405 | strm.zfree = Z_NULL; |
---|
| 406 | strm.opaque = Z_NULL; |
---|
| 407 | strm.avail_in = 0; |
---|
| 408 | strm.next_in = Z_NULL; |
---|
| 409 | ret = inflateInit(&strm); |
---|
| 410 | if (ret != Z_OK) |
---|
| 411 | { |
---|
| 412 | return ret; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | size_t p_size = 0, av_in = 0; |
---|
| 416 | /* decompress until deflate stream ends or end of file */ |
---|
| 417 | do { |
---|
| 418 | p_size += SZ_ZLIB_BUFFER_SIZE; |
---|
| 419 | if(p_size>cmpSize) |
---|
| 420 | av_in = cmpSize - (p_size - SZ_ZLIB_BUFFER_SIZE); |
---|
| 421 | else |
---|
| 422 | av_in = SZ_ZLIB_BUFFER_SIZE; |
---|
| 423 | strm.avail_in = av_in; |
---|
| 424 | |
---|
| 425 | if (strm.avail_in == 0) |
---|
| 426 | break; |
---|
| 427 | strm.next_in = in; |
---|
| 428 | |
---|
| 429 | /* run inflate() on input until output buffer not full */ |
---|
| 430 | do { |
---|
| 431 | strm.avail_out = SZ_ZLIB_BUFFER_SIZE; |
---|
| 432 | strm.next_out = out; |
---|
| 433 | ret = inflate(&strm, Z_NO_FLUSH); |
---|
| 434 | //assert(ret != Z_STREAM_ERROR); /* state not clobbered */ |
---|
| 435 | switch (ret) { |
---|
| 436 | case Z_NEED_DICT: |
---|
| 437 | ret = Z_DATA_ERROR; /* and fall through */ |
---|
| 438 | case Z_DATA_ERROR: |
---|
| 439 | case Z_MEM_ERROR: |
---|
| 440 | (void)inflateEnd(&strm); |
---|
| 441 | return ret; |
---|
| 442 | } |
---|
| 443 | have = SZ_ZLIB_BUFFER_SIZE - strm.avail_out; |
---|
| 444 | |
---|
| 445 | out += have; |
---|
| 446 | |
---|
| 447 | } while (strm.avail_out == 0); |
---|
| 448 | |
---|
| 449 | in+=av_in; |
---|
| 450 | /* done when inflate() says it's done */ |
---|
| 451 | } while (ret != Z_STREAM_END); |
---|
| 452 | |
---|
| 453 | /* clean up and return */ |
---|
| 454 | (void)inflateEnd(&strm); |
---|
| 455 | |
---|
| 456 | return strm.total_out; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | unsigned long zlib_uncompress65536bytes(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData) |
---|
| 460 | { |
---|
| 461 | int err; |
---|
| 462 | unsigned long targetOriSize = 65536; |
---|
| 463 | z_stream d_stream = {0}; /* decompression stream */ |
---|
| 464 | |
---|
| 465 | *oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize); |
---|
| 466 | |
---|
| 467 | d_stream.zalloc = (alloc_func)0; |
---|
| 468 | d_stream.zfree = (free_func)0; |
---|
| 469 | d_stream.opaque = (voidpf)0; |
---|
| 470 | |
---|
| 471 | d_stream.next_in = compressBytes; |
---|
| 472 | d_stream.avail_in = 0; |
---|
| 473 | d_stream.next_out = *oriData; |
---|
| 474 | |
---|
| 475 | err = inflateInit(&d_stream); |
---|
| 476 | CHECK_ERR(err, "inflateInit"); |
---|
| 477 | |
---|
| 478 | while (d_stream.total_out < targetOriSize && d_stream.total_in < cmpSize) { |
---|
| 479 | d_stream.avail_in = d_stream.avail_out = SZ_ZLIB_BUFFER_SIZE; /* force small buffers */ |
---|
| 480 | //err = inflate(&d_stream, Z_NO_FLUSH); |
---|
| 481 | err = inflate(&d_stream, Z_SYNC_FLUSH); |
---|
| 482 | if (err == Z_STREAM_END) break; |
---|
| 483 | if(err<0) |
---|
| 484 | break; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | if(err<0) |
---|
| 488 | return d_stream.total_out; |
---|
| 489 | err = inflateEnd(&d_stream); |
---|
| 490 | |
---|
| 491 | CHECK_ERR(err, "inflateEnd"); |
---|
| 492 | |
---|
| 493 | return d_stream.total_out; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | unsigned long zlib_uncompress5(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize) |
---|
| 497 | { |
---|
| 498 | int err; |
---|
| 499 | z_stream d_stream = {0}; /* decompression stream */ |
---|
| 500 | |
---|
| 501 | *oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize); |
---|
| 502 | |
---|
| 503 | d_stream.zalloc = (alloc_func)0; |
---|
| 504 | d_stream.zfree = (free_func)0; |
---|
| 505 | d_stream.opaque = (voidpf)0; |
---|
| 506 | |
---|
| 507 | d_stream.next_in = compressBytes; |
---|
| 508 | d_stream.avail_in = 0; |
---|
| 509 | d_stream.next_out = *oriData; |
---|
| 510 | |
---|
| 511 | err = inflateInit(&d_stream); |
---|
| 512 | CHECK_ERR(err, "inflateInit"); |
---|
| 513 | |
---|
| 514 | while (d_stream.total_out < targetOriSize && d_stream.total_in < cmpSize) { |
---|
| 515 | d_stream.avail_in = d_stream.avail_out = SZ_ZLIB_BUFFER_SIZE; /* force small buffers */ |
---|
| 516 | //err = inflate(&d_stream, Z_NO_FLUSH); |
---|
| 517 | err = inflate(&d_stream, Z_SYNC_FLUSH); |
---|
| 518 | if (err == Z_STREAM_END) break; |
---|
| 519 | CHECK_ERR(err, "inflate"); |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | err = inflateEnd(&d_stream); |
---|
| 523 | |
---|
| 524 | CHECK_ERR(err, "inflateEnd"); |
---|
| 525 | |
---|
| 526 | return d_stream.total_out; |
---|
| 527 | } |
---|