source: thirdparty/SZ/sz/src/callZlib.c @ 2c47b73

Revision 2c47b73, 14.0 KB checked in by Hal Finkel <hfinkel@…>, 6 years ago (diff)

more work on adding SZ (latest version)

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