1
2
3
4
5
6 #include <cmajor/rt/BZ2Interface.h>
7 #include <cmajor/system/ext/bzip2-1.0.6/bzlib.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <stdlib.h>
11
12 #define COMPRESS 0
13 #define DECOMPRESS 1
14
15 int32_t bz2_init(int32_t mode, int32_t compressionLevel, int32_t compressionWorkFactor, void** handle)
16 {
17 int32_t ret = BZ_OK;
18 if (!handle)
19 {
20 ret = BZ_MEM_ERROR;
21 }
22 else
23 {
24 bz_stream* stream = (bz_stream*)malloc(sizeof(bz_stream));
25 switch (mode)
26 {
27 case 0:
28 {
29 stream->bzalloc = NULL;
30 stream->bzfree = NULL;
31 stream->opaque = NULL;
32 ret = BZ2_bzCompressInit(stream, compressionLevel, 0, compressionWorkFactor);
33 break;
34 }
35 case 1:
36 {
37 stream->bzalloc = NULL;
38 stream->bzfree = NULL;
39 stream->opaque = NULL;
40 ret = BZ2_bzDecompressInit(stream, 0, 0);
41 break;
42 }
43 }
44 if (ret != BZ_OK)
45 {
46 free(stream);
47 *handle = NULL;
48 }
49 else
50 {
51 *handle = stream;
52 }
53 }
54 return ret;
55 }
56
57 void bz2_done(int32_t mode, void* handle)
58 {
59 bz_stream* strm = (bz_stream*)handle;
60 switch (mode)
61 {
62 case 0:
63 {
64 BZ2_bzCompressEnd(strm);
65 break;
66 }
67 case 1:
68 {
69 BZ2_bzDecompressEnd(strm);
70 break;
71 }
72 }
73 free(strm);
74 }
75
76 void bz2_set_input(void* inChunk, uint32_t inAvail, void* handle)
77 {
78 bz_stream* strm = (bz_stream*)handle;
79 strm->next_in = (char*)inChunk;
80 strm->avail_in = inAvail;
81 }
82
83 int32_t bz2_compress(void* outChunk, uint32_t outChunkSize, uint32_t* have, uint32_t* outAvail, void* handle, int32_t action)
84 {
85 bz_stream* strm = (bz_stream*)handle;
86 strm->next_out = outChunk;
87 strm->avail_out = outChunkSize;
88 int ret = BZ2_bzCompress(strm, action);
89 *have = outChunkSize - strm->avail_out;
90 *outAvail = strm->avail_out;
91 return ret;
92 }
93
94 int32_t bz2_decompress(void* outChunk, uint32_t outChunkSize, uint32_t* have, uint32_t* outAvail, uint32_t* inAvail, void* handle)
95 {
96 bz_stream* strm = (bz_stream*)handle;
97 strm->next_out = outChunk;
98 strm->avail_out = outChunkSize;
99 int32_t ret = BZ2_bzDecompress(strm);
100 *have = outChunkSize - strm->avail_out;
101 *outAvail = strm->avail_out;
102 *inAvail = strm->avail_in;
103 return ret;
104 }
105
106 const char* bz2_retval_str(int32_t retVal)
107 {
108 switch (retVal)
109 {
110 case BZ_OK: return "BZ_OK";
111 case BZ_RUN_OK: return "BZ_RUN_OK";
112 case BZ_FLUSH_OK: return "BZ_FLUSH_OK";
113 case BZ_FINISH_OK: return "BZ_FINISH_OK";
114 case BZ_STREAM_END: return "BZ_STREAM_END";
115 case BZ_SEQUENCE_ERROR: return "BZ_SEQUENCE_ERROR";
116 case BZ_PARAM_ERROR: return "BZ_PARAM_ERROR";
117 case BZ_MEM_ERROR: return "BZ_MEM_ERROR";
118 case BZ_DATA_ERROR: return "BZ_DATA_ERROR";
119 case BZ_DATA_ERROR_MAGIC: return "BZ_DATA_ERROR_MAGIC";
120 case BZ_IO_ERROR: return strerror(errno);
121 case BZ_UNEXPECTED_EOF: return "BZ_UNEXPECTED_EOF";
122 case BZ_OUTBUFF_FULL: return "BZ_OUTBUFF_FULL";
123 case BZ_CONFIG_ERROR: return "BZ_CONFIG_ERROR";
124 }
125 return "";
126 }