My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /classification/libncd/bz2/bz2.c
a3875c174f01 vs. 0136ea839f68 Compare: vs.  Format:
Revision 0136ea839f68
Go to: 
Project members, sign in to write a code review
/classification/libncd/bz2/bz2.c   a3875c174f01 /classification/libncd/bz2/bz2.c   0136ea839f68
1 #include "bz2.h" 1 #include "bz2.h"
2 2
3 #include <bzlib.h> 3 #include <bzlib.h>
4 4
5 int bz2Init(bz2_wrapper_t *bwt) 5 int bz2Compress(int blockSize100k, int verbosity, int workFactor, void *data, unsigned int avail_in, void *odata, unsigned int *avail_out)
6 {
7 bwt->strm = malloc(sizeof(bz_stream));
8 return 0;
9 }
10
11 int bz2Compress(bz2_wrapper_t *bwt, int blockSize100k, int verbosity, int workFactor, void *data, unsigned int avail_in, void *odata, unsigned int *avail_out)
12 { 6 {
13 int ret; 7 int ret;
14 bz_stream *strm = bwt->strm; 8 bz_stream strm;
15 9
16 strm->bzalloc = NULL; 10 strm.bzalloc = NULL;
17 strm->bzfree = NULL; 11 strm.bzfree = NULL;
18 strm->opaque = NULL; 12 strm.opaque = NULL;
19 13
20 ret = BZ2_bzCompressInit(strm, blockSize100k, verbosity, workFactor); 14 ret = BZ2_bzCompressInit(&strm, blockSize100k, verbosity, workFactor);
21 if (ret != BZ_OK) return ret; 15 if (ret != BZ_OK) return ret;
22 16
23 strm->next_in = data; 17 strm.next_in = data;
24 strm->next_out = odata; 18 strm.next_out = odata;
25 strm->avail_in = avail_in; 19 strm.avail_in = avail_in;
26 strm->avail_out = *avail_out; 20 strm.avail_out = *avail_out;
27 21
28 ret = BZ2_bzCompress ( strm, BZ_FINISH ); 22 ret = BZ2_bzCompress ( &strm, BZ_FINISH );
29 if (ret == BZ_FINISH_OK) goto output_overflow; 23 if (ret == BZ_FINISH_OK) goto output_overflow;
30 if (ret != BZ_STREAM_END) goto errhandler; 24 if (ret != BZ_STREAM_END) goto errhandler;
31 25
32 /* normal termination */ 26 /* normal termination */
33 *avail_out -= strm->avail_out; 27 *avail_out -= strm.avail_out;
34 BZ2_bzCompressEnd ( strm ); 28 BZ2_bzCompressEnd ( &strm );
35 return BZ_OK; 29 return BZ_OK;
36 30
37 output_overflow: 31 output_overflow:
38 BZ2_bzCompressEnd ( strm ); 32 BZ2_bzCompressEnd ( &strm );
39 return BZ_OUTBUFF_FULL; 33 return BZ_OUTBUFF_FULL;
40 34
41
42 errhandler: 35 errhandler:
43 BZ2_bzCompressEnd ( strm ); 36 BZ2_bzCompressEnd ( &strm );
44
45 return ret;
46 }
47
48 int bz2Decompress(bz2_wrapper_t *bwt, int small, int verbosity, void *data, unsigned int avail_in, void *odata, unsigned int *avail_out)
49 {
50 int ret;
51 bz_stream *strm = bwt->strm;
52
53 strm->bzalloc = NULL;
54 strm->bzfree = NULL;
55 strm->opaque = NULL;
56
57 ret = BZ2_bzDecompressInit ( strm, verbosity, small );
58 if (ret != BZ_OK) return ret;
59
60
61 strm->next_in = data;
62 strm->next_out = odata;
63 strm->avail_in = avail_in;
64 strm->avail_out = *avail_out;
65
66 ret = BZ2_bzDecompress ( strm );
67 if (ret == BZ_OK) goto output_overflow_or_eof;
68 if (ret != BZ_STREAM_END) goto errhandler;
69
70
71 /* normal termination */
72 *avail_out -= strm->avail_out;
73 BZ2_bzDecompressEnd ( strm );
74 return BZ_OK;
75
76
77
78 output_overflow_or_eof:
79 if (strm->avail_out > 0) {
80 BZ2_bzDecompressEnd ( strm );
81 return BZ_UNEXPECTED_EOF;
82 } else {
83 BZ2_bzDecompressEnd ( strm );
84 return BZ_OUTBUFF_FULL;
85 };
86
87 37
88 errhandler:
89 BZ2_bzDecompressEnd ( strm );
90 return ret; 38 return ret;
91 } 39 }
Powered by Google Project Hosting