1 #include "config.h"
2 #include <sys/types.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <stdio.h>
7 #include <hdf5.h>
8 /* Older versions of the hdf library may define H5PL_type_t here */
9 #include <H5PLextern.h>
10 
11 
12 
13 #ifndef DLL_EXPORT
14 #define DLL_EXPORT
15 #endif
16 
17 /* WARNING:
18 Starting with HDF5 version 1.10.x, the plugin code MUST be
19 careful when using the standard *malloc()*, *realloc()*, and
20 *free()* function.
21 
22 In the event that the code is allocating, reallocating, for
23 free'ing memory that either came from or will be exported to the
24 calling HDF5 library, then one MUST use the corresponding HDF5
25 functions *H5allocate_memory()*, *H5resize_memory()*,
26 *H5free_memory()* [5] to avoid memory failures.
27 
28 Additionally, if your filter code leaks memory, then the HDF5 library
29 will generate an error.
30 
31 */
32 
33 #include "h5bzip2.h"
34 
35 const H5Z_class2_t H5Z_BZIP2[1] = {{
36     H5Z_CLASS_T_VERS,       /* H5Z_class_t version */
37     (H5Z_filter_t)H5Z_FILTER_BZIP2,         /* Filter id number             */
38     1,              /* encoder_present flag (set to true) */
39     1,              /* decoder_present flag (set to true) */
40     "bzip2",                  /* Filter name for debugging    */
41     (H5Z_can_apply_func_t)H5Z_bzip2_can_apply, /* The "can apply" callback  */
42     NULL,                       /* The "set local" callback     */
43     (H5Z_func_t)H5Z_filter_bzip2,         /* The actual filter function   */
44 }};
45 
46 /* External Discovery Functions */
47 H5PL_type_t
H5PLget_plugin_type(void)48 H5PLget_plugin_type(void)
49 {
50     return H5PL_TYPE_FILTER;
51 }
52 
53 const void*
H5PLget_plugin_info(void)54 H5PLget_plugin_info(void)
55 {
56     return H5Z_BZIP2;
57 }
58 
59 /* Make this explicit */
60 /*
61  * The "can_apply" callback returns positive a valid combination, zero for an
62  * invalid combination and negative for an error.
63  */
64 htri_t
H5Z_bzip2_can_apply(hid_t dcpl_id,hid_t type_id,hid_t space_id)65 H5Z_bzip2_can_apply(hid_t dcpl_id, hid_t type_id, hid_t space_id)
66 {
67     return 1; /* Assume it can always apply */
68 }
69 
H5Z_filter_bzip2(unsigned int flags,size_t cd_nelmts,const unsigned int cd_values[],size_t nbytes,size_t * buf_size,void ** buf)70 size_t H5Z_filter_bzip2(unsigned int flags, size_t cd_nelmts,
71                      const unsigned int cd_values[], size_t nbytes,
72                      size_t *buf_size, void **buf)
73 {
74   char *outbuf = NULL;
75   size_t outbuflen, outdatalen;
76   int ret;
77 
78   if (flags & H5Z_FLAG_REVERSE) {
79 
80     /** Decompress data.
81      **
82      ** This process is troublesome since the size of uncompressed data
83      ** is unknown, so the low-level interface must be used.
84      ** Data is decompressed to the output buffer (which is sized
85      ** for the average case); if it gets full, its size is doubled
86      ** and decompression continues.  This avoids repeatedly trying to
87      ** decompress the whole block, which could be really inefficient.
88      **/
89 
90     bz_stream stream;
91     char *newbuf = NULL;
92     size_t newbuflen;
93 
94     /* Prepare the output buffer. */
95     outbuflen = nbytes * 3 + 1;  /* average bzip2 compression ratio is 3:1 */
96 #ifdef HAVE_H5ALLOCATE_MEMORY
97     outbuf = H5allocate_memory(outbuflen,0);
98 #else
99     outbuf = (char*)malloc(outbuflen * sizeof(char));
100 #endif
101     if (outbuf == NULL) {
102       fprintf(stderr, "memory allocation failed for bzip2 decompression\n");
103       goto cleanupAndFail;
104     }
105 
106     /* Use standard malloc()/free() for internal memory handling. */
107     stream.bzalloc = NULL;
108     stream.bzfree = NULL;
109     stream.opaque = NULL;
110 
111     /* Start decompression. */
112     ret = BZ2_bzDecompressInit(&stream, 0, 0);
113     if (ret != BZ_OK) {
114       fprintf(stderr, "bzip2 decompression start failed with error %d\n", ret);
115       goto cleanupAndFail;
116     }
117 
118     /* Feed data to the decompression process and get decompressed data. */
119     stream.next_out = outbuf;
120     stream.avail_out = outbuflen;
121     stream.next_in = *buf;
122     stream.avail_in = nbytes;
123     do {
124       ret = BZ2_bzDecompress(&stream);
125       if (ret < 0) {
126 	fprintf(stderr, "BUG: bzip2 decompression failed with error %d\n", ret);
127 	goto cleanupAndFail;
128       }
129 
130       if (ret != BZ_STREAM_END && stream.avail_out == 0) {
131         /* Grow the output buffer. */
132         newbuflen = outbuflen * 2;
133 #ifdef HAVE_H5RESIZE_MEMORY
134         newbuf = H5resize_memory(outbuf, newbuflen);
135 #else
136         newbuf = realloc(outbuf,newbuflen);
137 #endif
138         if (newbuf == NULL) {
139           fprintf(stderr, "memory allocation failed for bzip2 decompression\n");
140           goto cleanupAndFail;
141         }
142         stream.next_out = newbuf + outbuflen;  /* half the new buffer behind */
143         stream.avail_out = outbuflen;  /* half the new buffer ahead */
144         outbuf = newbuf;
145         outbuflen = newbuflen;
146       }
147     } while (ret != BZ_STREAM_END);
148 
149     /* End compression. */
150     outdatalen = stream.total_out_lo32;
151     ret = BZ2_bzDecompressEnd(&stream);
152     if (ret != BZ_OK) {
153       fprintf(stderr, "bzip2 compression end failed with error %d\n", ret);
154       goto cleanupAndFail;
155     }
156 
157   } else {
158 
159     /** Compress data.
160      **
161      ** This is quite simple, since the size of compressed data in the worst
162      ** case is known and it is not much bigger than the size of uncompressed
163      ** data.  This allows us to use the simplified one-shot interface to
164      ** compression.
165      **/
166 
167     unsigned int odatalen;  /* maybe not the same size as outdatalen */
168     int blockSize100k = 9;
169 
170     /* Get compression block size if present. */
171     if (cd_nelmts > 0) {
172       blockSize100k = cd_values[0];
173       if (blockSize100k < 1 || blockSize100k > 9) {
174 	fprintf(stderr, "invalid compression block size: %d\n", blockSize100k);
175 	goto cleanupAndFail;
176       }
177     }
178 
179     /* Prepare the output buffer. */
180     outbuflen = nbytes + nbytes / 100 + 600;  /* worst case (bzip2 docs) */
181 #ifdef HAVE_H5ALLOCATE_MEMORY
182     outbuf = H5allocate_memory(outbuflen,0);
183 #else
184     outbuf = (char*)malloc(outbuflen * sizeof(char));
185 #endif
186 
187     if (outbuf == NULL) {
188       fprintf(stderr, "memory allocation failed for bzip2 compression\n");
189       goto cleanupAndFail;
190     }
191 
192     /* Compress data. */
193     odatalen = outbuflen;
194     ret = BZ2_bzBuffToBuffCompress(outbuf, &odatalen, *buf, nbytes,
195                                    blockSize100k, 0, 0);
196     outdatalen = odatalen;
197     if (ret != BZ_OK) {
198       fprintf(stderr, "bzip2 compression failed with error %d\n", ret);
199       goto cleanupAndFail;
200     }
201   }
202 
203   /* Always replace the input buffer with the output buffer. */
204 #ifdef HAVE_H5FREE_MEMORY
205   H5free_memory(*buf);
206 #else
207   free(*buf);
208 #endif
209 
210   *buf = outbuf;
211   *buf_size = outbuflen;
212   return outdatalen;
213 
214  cleanupAndFail:
215   if (outbuf)
216 #ifdef HAVE_H5FREE_MEMORY
217     H5free_memory(outbuf);
218 #else
219   free(outbuf);
220 #endif
221 
222   return 0;
223 }
224