| 1 |
/* |
| 2 |
* This file is an example of an HDF5 filter plugin. |
| 3 |
* The filter function H5Z_filter_bzip2 was adopted from |
| 4 |
* PyTables http://www.pytables.org. |
| 5 |
* The plugin can be used with the HDF5 library vesrion 1.8.11+ to read |
| 6 |
* HDF5 datasets compressed with bzip2 created by PyTables. |
| 7 |
*/ |
| 8 |
|
| 9 |
/* |
| 10 |
* |
| 11 |
Copyright Notice and Statement for PyTables Software Library and Utilities: |
| 12 |
|
| 13 |
Copyright (c) 2002-2004 by Francesc Alted |
| 14 |
Copyright (c) 2005-2007 by Carabos Coop. V. |
| 15 |
Copyright (c) 2008-2010 by Francesc Alted |
| 16 |
All rights reserved. |
| 17 |
|
| 18 |
Redistribution and use in source and binary forms, with or without |
| 19 |
modification, are permitted provided that the following conditions are |
| 20 |
met: |
| 21 |
|
| 22 |
a. Redistributions of source code must retain the above copyright |
| 23 |
notice, this list of conditions and the following disclaimer. |
| 24 |
|
| 25 |
b. Redistributions in binary form must reproduce the above copyright |
| 26 |
notice, this list of conditions and the following disclaimer in the |
| 27 |
documentation and/or other materials provided with the |
| 28 |
distribution. |
| 29 |
|
| 30 |
c. Neither the name of Francesc Alted nor the names of its |
| 31 |
contributors may be used to endorse or promote products derived |
| 32 |
from this software without specific prior written permission. |
| 33 |
|
| 34 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 35 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 36 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 37 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 38 |
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 39 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 40 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 41 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 42 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 43 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 44 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 |
*/ |
| 46 |
#include <sys/types.h> |
| 47 |
#include <stdlib.h> |
| 48 |
#include <string.h> |
| 49 |
#include <assert.h> |
| 50 |
#include <stdio.h> |
| 51 |
#include <H5PLextern.h> |
| 52 |
|
| 53 |
#include <bzlib.h> |
| 54 |
|
| 55 |
static size_t H5Z_filter_bzip2(unsigned int flags, size_t cd_nelmts, |
| 56 |
const unsigned int cd_values[], size_t nbytes, |
| 57 |
size_t *buf_size, void **buf); |
| 58 |
|
| 59 |
/* |
| 60 |
* BZIP2 compression was the first external filter registered by PyTables developers |
| 61 |
* with The HDF Group |
| 62 |
* See http://www.hdfgroup.org/services/contributions.html for more information. |
| 63 |
* |
| 64 |
* If you intend your plugin to be used by others, please register your filter |
| 65 |
* with The HDF Group. |
| 66 |
*/ |
| 67 |
#define H5Z_FILTER_BZIP2 307 |
| 68 |
|
| 69 |
const H5Z_class2_t H5Z_BZIP2[1] = {{ |
| 70 |
H5Z_CLASS_T_VERS, /* H5Z_class_t version */ |
| 71 |
(H5Z_filter_t)H5Z_FILTER_BZIP2, /* Filter id number */ |
| 72 |
1, /* encoder_present flag (set to true) */ |
| 73 |
1, /* decoder_present flag (set to true) */ |
| 74 |
"HDF5 bzip2 filter; see http://www.hdfgroup.org/services/contributions.html", |
| 75 |
/* Filter name for debugging */ |
| 76 |
NULL, /* The "can apply" callback */ |
| 77 |
NULL, /* The "set local" callback */ |
| 78 |
(H5Z_func_t)H5Z_filter_bzip2, /* The actual filter function */ |
| 79 |
}}; |
| 80 |
|
| 81 |
H5PL_type_t H5PLget_plugin_type(void) {return H5PL_TYPE_FILTER;} |
| 82 |
const void *H5PLget_plugin_info(void) {return H5Z_BZIP2;} |
| 83 |
|
| 84 |
static size_t H5Z_filter_bzip2(unsigned int flags, size_t cd_nelmts, |
| 85 |
const unsigned int cd_values[], size_t nbytes, |
| 86 |
size_t *buf_size, void **buf) |
| 87 |
{ |
| 88 |
char *outbuf = NULL; |
| 89 |
size_t outbuflen, outdatalen; |
| 90 |
int ret; |
| 91 |
|
| 92 |
if (flags & H5Z_FLAG_REVERSE) { |
| 93 |
|
| 94 |
/** Decompress data. |
| 95 |
** |
| 96 |
** This process is troublesome since the size of uncompressed data |
| 97 |
** is unknown, so the low-level interface must be used. |
| 98 |
** Data is decompressed to the output buffer (which is sized |
| 99 |
** for the average case); if it gets full, its size is doubled |
| 100 |
** and decompression continues. This avoids repeatedly trying to |
| 101 |
** decompress the whole block, which could be really inefficient. |
| 102 |
**/ |
| 103 |
|
| 104 |
bz_stream stream; |
| 105 |
char *newbuf = NULL; |
| 106 |
size_t newbuflen; |
| 107 |
|
| 108 |
/* Prepare the output buffer. */ |
| 109 |
outbuflen = nbytes * 3 + 1; /* average bzip2 compression ratio is 3:1 */ |
| 110 |
outbuf = malloc(outbuflen); |
| 111 |
if (outbuf == NULL) { |
| 112 |
fprintf(stderr, "memory allocation failed for bzip2 decompression\n"); |
| 113 |
goto cleanupAndFail; |
| 114 |
} |
| 115 |
|
| 116 |
/* Use standard malloc()/free() for internal memory handling. */ |
| 117 |
stream.bzalloc = NULL; |
| 118 |
stream.bzfree = NULL; |
| 119 |
stream.opaque = NULL; |
| 120 |
|
| 121 |
/* Start decompression. */ |
| 122 |
ret = BZ2_bzDecompressInit(&stream, 0, 0); |
| 123 |
if (ret != BZ_OK) { |
| 124 |
fprintf(stderr, "bzip2 decompression start failed with error %d\n", ret); |
| 125 |
goto cleanupAndFail; |
| 126 |
} |
| 127 |
|
| 128 |
/* Feed data to the decompression process and get decompressed data. */ |
| 129 |
stream.next_out = outbuf; |
| 130 |
stream.avail_out = outbuflen; |
| 131 |
stream.next_in = *buf; |
| 132 |
stream.avail_in = nbytes; |
| 133 |
do { |
| 134 |
ret = BZ2_bzDecompress(&stream); |
| 135 |
if (ret < 0) { |
| 136 |
fprintf(stderr, "BUG: bzip2 decompression failed with error %d\n", ret); |
| 137 |
goto cleanupAndFail; |
| 138 |
} |
| 139 |
|
| 140 |
if (ret != BZ_STREAM_END && stream.avail_out == 0) { |
| 141 |
/* Grow the output buffer. */ |
| 142 |
newbuflen = outbuflen * 2; |
| 143 |
newbuf = realloc(outbuf, newbuflen); |
| 144 |
if (newbuf == NULL) { |
| 145 |
fprintf(stderr, "memory allocation failed for bzip2 decompression\n"); |
| 146 |
goto cleanupAndFail; |
| 147 |
} |
| 148 |
stream.next_out = newbuf + outbuflen; /* half the new buffer behind */ |
| 149 |
stream.avail_out = outbuflen; /* half the new buffer ahead */ |
| 150 |
outbuf = newbuf; |
| 151 |
outbuflen = newbuflen; |
| 152 |
} |
| 153 |
} while (ret != BZ_STREAM_END); |
| 154 |
|
| 155 |
/* End compression. */ |
| 156 |
outdatalen = stream.total_out_lo32; |
| 157 |
ret = BZ2_bzDecompressEnd(&stream); |
| 158 |
if (ret != BZ_OK) { |
| 159 |
fprintf(stderr, "bzip2 compression end failed with error %d\n", ret); |
| 160 |
goto cleanupAndFail; |
| 161 |
} |
| 162 |
|
| 163 |
} else { |
| 164 |
|
| 165 |
/** Compress data. |
| 166 |
** |
| 167 |
** This is quite simple, since the size of compressed data in the worst |
| 168 |
** case is known and it is not much bigger than the size of uncompressed |
| 169 |
** data. This allows us to use the simplified one-shot interface to |
| 170 |
** compression. |
| 171 |
**/ |
| 172 |
|
| 173 |
unsigned int odatalen; /* maybe not the same size as outdatalen */ |
| 174 |
int blockSize100k = 9; |
| 175 |
|
| 176 |
/* Get compression block size if present. */ |
| 177 |
if (cd_nelmts > 0) { |
| 178 |
blockSize100k = cd_values[0]; |
| 179 |
if (blockSize100k < 1 || blockSize100k > 9) { |
| 180 |
fprintf(stderr, "invalid compression block size: %d\n", blockSize100k); |
| 181 |
goto cleanupAndFail; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/* Prepare the output buffer. */ |
| 186 |
outbuflen = nbytes + nbytes / 100 + 600; /* worst case (bzip2 docs) */ |
| 187 |
outbuf = malloc(outbuflen); |
| 188 |
if (outbuf == NULL) { |
| 189 |
fprintf(stderr, "memory allocation failed for bzip2 compression\n"); |
| 190 |
goto cleanupAndFail; |
| 191 |
} |
| 192 |
|
| 193 |
/* Compress data. */ |
| 194 |
odatalen = outbuflen; |
| 195 |
ret = BZ2_bzBuffToBuffCompress(outbuf, &odatalen, *buf, nbytes, |
| 196 |
blockSize100k, 0, 0); |
| 197 |
outdatalen = odatalen; |
| 198 |
if (ret != BZ_OK) { |
| 199 |
fprintf(stderr, "bzip2 compression failed with error %d\n", ret); |
| 200 |
goto cleanupAndFail; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/* Always replace the input buffer with the output buffer. */ |
| 205 |
free(*buf); |
| 206 |
*buf = outbuf; |
| 207 |
*buf_size = outbuflen; |
| 208 |
return outdatalen; |
| 209 |
|
| 210 |
cleanupAndFail: |
| 211 |
if (outbuf) |
| 212 |
free(outbuf); |
| 213 |
return 0; |
| 214 |
} |