1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/***** Preamble block *********************************************************
*
* This file is part of h5py, a low-level Python interface to the HDF5 library.
*
* Copyright (C) 2008 Andrew Collette
* http://h5py.alfven.org
* License: BSD (See LICENSE.txt for full license)
*
* $Date$
*
****** End preamble block ****************************************************/
/*
Implements an LZF filter module for HDF5, using the BSD-licensed library
by Marc Alexander Lehmann (http://www.goof.com/pcg/marc/liblzf.html).
No Python-specific code is used. The filter behaves like the DEFLATE
filter, in that it is called for every type and space, and returns 0
if the data cannot be compressed.
The only public function is (int) register_lzf(void), which passes on
the result from H5Zregister.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "hdf5.h"
#include "lzf.h"
#include "lzf_filter.h"
/* Our own versions of H5Epush_sim, as it changed in 1.8 */
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR < 7
#define PUSH_ERR(func, minor, str) H5Epush(__FILE__, func, __LINE__, H5E_PLINE, minor, str)
#define H5PY_GET_FILTER H5Pget_filter_by_id
#else
#define PUSH_ERR(func, minor, str) H5Epush1(__FILE__, func, __LINE__, H5E_PLINE, minor, str)
#define H5PY_GET_FILTER(a,b,c,d,e,f,g) H5Pget_filter_by_id2(a,b,c,d,e,f,g,NULL)
#endif
/* Deal with the mutiple definitions for H5Z_class_t.
Note: HDF5 1.6 and >= 1.8 are supported.
See https://hdfgroup.github.io/hdf5/develop/group___h5_z.html#ga93145acc38c2c60d832b7a9b0123706b
for version history.
(1) The old class should always be used for HDF5 1.6
(2) The new class should always be used for HDF5 1.8 < 1.8.3
(3) The old class should be used for HDF5 >= 1.8.3 only if the
macro H5_USE_16_API is set
*/
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR < 8
#define H5PY_H5Z_NEWCLS 0
#elif H5_VERS_MAJOR == 1 && H5_VERS_MINOR >= 8 && H5_VERS_RELEASE >= 3 && H5_USE_16_API
#define H5PY_H5Z_NEWCLS 0
#else
#define H5PY_H5Z_NEWCLS 1
#endif
size_t lzf_filter(unsigned flags, size_t cd_nelmts,
const unsigned cd_values[], size_t nbytes,
size_t *buf_size, void **buf);
herr_t lzf_set_local(hid_t dcpl, hid_t type, hid_t space);
/* Try to register the filter, passing on the HDF5 return value */
int register_lzf(void){
int retval;
#if H5PY_H5Z_NEWCLS
H5Z_class_t filter_class = {
H5Z_CLASS_T_VERS,
(H5Z_filter_t)(H5PY_FILTER_LZF),
1, 1,
"lzf",
NULL,
(H5Z_set_local_func_t)(lzf_set_local),
(H5Z_func_t)(lzf_filter)
};
#else
H5Z_class_t filter_class = {
(H5Z_filter_t)(H5PY_FILTER_LZF),
"lzf",
NULL,
(H5Z_set_local_func_t)(lzf_set_local),
(H5Z_func_t)(lzf_filter)
};
#endif
retval = H5Zregister(&filter_class);
if(retval<0){
PUSH_ERR("register_lzf", H5E_CANTREGISTER, "Can't register LZF filter");
}
return retval;
}
/* Filter setup. Records the following inside the DCPL:
1. If version information is not present, set slots 0 and 1 to the filter
revision and LZF API version, respectively.
2. Compute the chunk size in bytes and store it in slot 2.
*/
herr_t lzf_set_local(hid_t dcpl, hid_t type, hid_t space){
int ndims;
int i;
herr_t r;
unsigned int bufsize;
hsize_t chunkdims[32];
unsigned int flags;
size_t nelements = 8;
unsigned values[] = {0,0,0,0,0,0,0,0};
r = H5PY_GET_FILTER(dcpl, H5PY_FILTER_LZF, &flags, &nelements, values, 0, NULL);
if(r<0) return -1;
if(nelements < 3) nelements = 3; /* First 3 slots reserved. If any higher
slots are used, preserve the contents. */
/* It seems the H5Z_FLAG_REVERSE flag doesn't work here, so we have to be
careful not to clobber any existing version info */
if(values[0]==0) values[0] = H5PY_FILTER_LZF_VERSION;
if(values[1]==0) values[1] = LZF_VERSION;
ndims = H5Pget_chunk(dcpl, 32, chunkdims);
if(ndims<0) return -1;
if(ndims>32){
PUSH_ERR("lzf_set_local", H5E_CALLBACK, "Chunk rank exceeds limit");
return -1;
}
bufsize = H5Tget_size(type);
if(bufsize==0) return -1;
for(i=0;i<ndims;i++){
bufsize *= chunkdims[i];
}
values[2] = bufsize;
#ifdef H5PY_LZF_DEBUG
fprintf(stderr, "LZF: Computed buffer size %d\n", bufsize);
#endif
r = H5Pmodify_filter(dcpl, H5PY_FILTER_LZF, flags, nelements, values);
if(r<0) return -1;
return 1;
}
/* The filter function */
size_t lzf_filter(unsigned flags, size_t cd_nelmts,
const unsigned cd_values[], size_t nbytes,
size_t *buf_size, void **buf){
void* outbuf = NULL;
size_t outbuf_size = 0;
unsigned int status = 0; /* Return code from lzf routines */
/* We're compressing */
if(!(flags & H5Z_FLAG_REVERSE)){
/* Allocate an output buffer exactly as long as the input data; if
the result is larger, we simply return 0. The filter is flagged
as optional, so HDF5 marks the chunk as uncompressed and
proceeds.
*/
outbuf_size = (*buf_size);
outbuf = malloc(outbuf_size);
if(outbuf == NULL){
PUSH_ERR("lzf_filter", H5E_CALLBACK, "Can't allocate compression buffer");
goto failed;
}
status = lzf_compress(*buf, nbytes, outbuf, outbuf_size);
/* We're decompressing */
} else {
if((cd_nelmts>=3)&&(cd_values[2]!=0)){
outbuf_size = cd_values[2]; /* Precomputed buffer guess */
}else{
outbuf_size = (*buf_size);
}
#ifdef H5PY_LZF_DEBUG
fprintf(stderr, "Decompress %d chunk w/buffer %d\n", nbytes, outbuf_size);
#endif
while(!status){
free(outbuf);
outbuf = malloc(outbuf_size);
if(outbuf == NULL){
PUSH_ERR("lzf_filter", H5E_CALLBACK, "Can't allocate decompression buffer");
goto failed;
}
status = lzf_decompress(*buf, nbytes, outbuf, outbuf_size);
if(!status){ /* compression failed */
if(errno == E2BIG){
outbuf_size += (*buf_size);
#ifdef H5PY_LZF_DEBUG
fprintf(stderr, " Too small: %d\n", outbuf_size);
#endif
} else if(errno == EINVAL) {
PUSH_ERR("lzf_filter", H5E_CALLBACK, "Invalid data for LZF decompression");
goto failed;
} else {
PUSH_ERR("lzf_filter", H5E_CALLBACK, "Unknown LZF decompression error");
goto failed;
}
} /* if !status */
} /* while !status */
} /* compressing vs decompressing */
if(status != 0){
free(*buf);
*buf = outbuf;
*buf_size = outbuf_size;
return status; /* Size of compressed/decompressed data */
}
failed:
free(outbuf);
return 0;
} /* End filter function */
|