| 1 |
# /*########################################################################## |
| 2 |
# |
| 3 |
# Copyright (c) 2019 European Synchrotron Radiation Facility |
| 4 |
# |
| 5 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
# of this software and associated documentation files (the "Software"), to deal |
| 7 |
# in the Software without restriction, including without limitation the rights |
| 8 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
# copies of the Software, and to permit persons to whom the Software is |
| 10 |
# furnished to do so, subject to the following conditions: |
| 11 |
# |
| 12 |
# The above copyright notice and this permission notice shall be included in |
| 13 |
# all copies or substantial portions of the Software. |
| 14 |
# |
| 15 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 |
# THE SOFTWARE. |
| 22 |
# |
| 23 |
# ###########################################################################*/ |
| 24 |
/* This provides replacement for HDF5 functions/variables used by filters. |
| 25 |
* |
| 26 |
* Those replacement provides no-op functions by default and if init_filter |
| 27 |
* is called it provides access to HDF5 functions/variables through dynamic |
| 28 |
* loading. |
| 29 |
* This is useful on Linux/macOS to avoid linking the plugin with a dedicated |
| 30 |
* HDF5 library. |
| 31 |
*/ |
| 32 |
#include <stdarg.h> |
| 33 |
#include <dlfcn.h> |
| 34 |
#include <stdbool.h> |
| 35 |
#include "hdf5.h" |
| 36 |
|
| 37 |
|
| 38 |
/*Function types*/ |
| 39 |
/*H5*/ |
| 40 |
typedef herr_t (*DL_func_H5open)(void); |
| 41 |
/*H5E*/ |
| 42 |
typedef herr_t (* DL_func_H5Epush1)( |
| 43 |
const char *file, const char *func, unsigned line, |
| 44 |
H5E_major_t maj, H5E_minor_t min, const char *str); |
| 45 |
typedef herr_t (* DL_func_H5Epush2)( |
| 46 |
hid_t err_stack, const char *file, const char *func, unsigned line, |
| 47 |
hid_t cls_id, hid_t maj_id, hid_t min_id, const char *msg, ...); |
| 48 |
/*H5P*/ |
| 49 |
typedef herr_t (* DL_func_H5Pget_filter_by_id2)(hid_t plist_id, H5Z_filter_t id, |
| 50 |
unsigned int *flags/*out*/, size_t *cd_nelmts/*out*/, |
| 51 |
unsigned cd_values[]/*out*/, size_t namelen, char name[]/*out*/, |
| 52 |
unsigned *filter_config/*out*/); |
| 53 |
typedef int (* DL_func_H5Pget_chunk)( |
| 54 |
hid_t plist_id, int max_ndims, hsize_t dim[]/*out*/); |
| 55 |
typedef herr_t (* DL_func_H5Pmodify_filter)( |
| 56 |
hid_t plist_id, H5Z_filter_t filter, |
| 57 |
unsigned int flags, size_t cd_nelmts, |
| 58 |
const unsigned int cd_values[/*cd_nelmts*/]); |
| 59 |
/*H5T*/ |
| 60 |
typedef size_t (* DL_func_H5Tget_size)( |
| 61 |
hid_t type_id); |
| 62 |
typedef H5T_class_t (* DL_func_H5Tget_class)(hid_t type_id); |
| 63 |
typedef hid_t (* DL_func_H5Tget_super)(hid_t type); |
| 64 |
typedef herr_t (* DL_func_H5Tclose)(hid_t type_id); |
| 65 |
/*H5Z*/ |
| 66 |
typedef herr_t (* DL_func_H5Zregister)( |
| 67 |
const void *cls); |
| 68 |
|
| 69 |
|
| 70 |
static struct { |
| 71 |
/*H5*/ |
| 72 |
DL_func_H5open H5open; |
| 73 |
/*H5E*/ |
| 74 |
DL_func_H5Epush1 H5Epush1; |
| 75 |
DL_func_H5Epush2 H5Epush2; |
| 76 |
/*H5P*/ |
| 77 |
DL_func_H5Pget_filter_by_id2 H5Pget_filter_by_id2; |
| 78 |
DL_func_H5Pget_chunk H5Pget_chunk; |
| 79 |
DL_func_H5Pmodify_filter H5Pmodify_filter; |
| 80 |
/*H5T*/ |
| 81 |
DL_func_H5Tget_size H5Tget_size; |
| 82 |
DL_func_H5Tget_class H5Tget_class; |
| 83 |
DL_func_H5Tget_super H5Tget_super; |
| 84 |
DL_func_H5Tclose H5Tclose; |
| 85 |
/*H5T*/ |
| 86 |
DL_func_H5Zregister H5Zregister; |
| 87 |
} DL_H5Functions = { |
| 88 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; |
| 89 |
|
| 90 |
static struct { |
| 91 |
/*HDF5 variables*/ |
| 92 |
void *h5e_cantregister_ptr; |
| 93 |
void *h5e_callback_ptr; |
| 94 |
void *h5e_pline_ptr; |
| 95 |
void *h5e_err_cls_ptr; |
| 96 |
} H5Variables_ptr = { |
| 97 |
NULL, NULL, NULL, NULL}; |
| 98 |
|
| 99 |
/*HDF5 variables*/ |
| 100 |
hid_t H5E_CANTREGISTER_g = -1; |
| 101 |
hid_t H5E_CALLBACK_g = -1; |
| 102 |
hid_t H5E_PLINE_g = -1; |
| 103 |
hid_t H5E_ERR_CLS_g = -1; |
| 104 |
|
| 105 |
|
| 106 |
static bool is_init = false; |
| 107 |
|
| 108 |
/* |
| 109 |
* Try to find a symbol within a library |
| 110 |
* |
| 111 |
* handle: Handle to the library |
| 112 |
* symbol: Symbol to look for |
| 113 |
* Returns: a pointer to the symbol or NULL |
| 114 |
* if the symbol can't be found |
| 115 |
*/ |
| 116 |
void *find_sym(void *handle, const char *symbol) { |
| 117 |
|
| 118 |
void *ret = NULL, *err = NULL; |
| 119 |
dlerror(); /* clear error code */ |
| 120 |
ret = dlsym(handle, symbol); |
| 121 |
|
| 122 |
if(ret != NULL && (err = dlerror()) == NULL) |
| 123 |
return ret; |
| 124 |
else |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* Check that all symbols have been loaded |
| 130 |
* |
| 131 |
* Returns: -1 if an error occured, 0 for success |
| 132 |
*/ |
| 133 |
int check_symbols(void) { |
| 134 |
|
| 135 |
if(DL_H5Functions.H5open == NULL) |
| 136 |
return -1; |
| 137 |
|
| 138 |
/*H5E*/ |
| 139 |
if(DL_H5Functions.H5Epush1 == NULL) |
| 140 |
return -1; |
| 141 |
|
| 142 |
if(DL_H5Functions.H5Epush2 == NULL) |
| 143 |
return -1; |
| 144 |
|
| 145 |
/*H5P*/ |
| 146 |
if(DL_H5Functions.H5Pget_filter_by_id2 == NULL) |
| 147 |
return -1; |
| 148 |
|
| 149 |
if(DL_H5Functions.H5Pget_chunk == NULL) |
| 150 |
return -1; |
| 151 |
|
| 152 |
if(DL_H5Functions.H5Pmodify_filter == NULL) |
| 153 |
return -1; |
| 154 |
|
| 155 |
/*H5T*/ |
| 156 |
if(DL_H5Functions.H5Tget_size == NULL) |
| 157 |
return -1; |
| 158 |
|
| 159 |
if(DL_H5Functions.H5Tget_class == NULL) |
| 160 |
return -1; |
| 161 |
|
| 162 |
if(DL_H5Functions.H5Tget_super == NULL) |
| 163 |
return -1; |
| 164 |
|
| 165 |
if(DL_H5Functions.H5Tclose == NULL) |
| 166 |
return -1; |
| 167 |
|
| 168 |
/*H5Z*/ |
| 169 |
if(DL_H5Functions.H5Zregister == NULL) |
| 170 |
return -1; |
| 171 |
|
| 172 |
/*Variables*/ |
| 173 |
if(H5Variables_ptr.h5e_cantregister_ptr == NULL) |
| 174 |
return -1; |
| 175 |
|
| 176 |
if(H5Variables_ptr.h5e_callback_ptr == NULL) |
| 177 |
return -1; |
| 178 |
|
| 179 |
if(H5Variables_ptr.h5e_pline_ptr == NULL) |
| 180 |
return -1; |
| 181 |
|
| 182 |
if(H5Variables_ptr.h5e_err_cls_ptr == NULL) |
| 183 |
return -1; |
| 184 |
|
| 185 |
return 0; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/* Initialize the dynamic loading of symbols and register the plugin |
| 190 |
* |
| 191 |
* libname: Name of the DLL from which to load libHDF5 symbols |
| 192 |
* Returns: -1 if an error occured, 0 for success |
| 193 |
*/ |
| 194 |
int init_filter(const char *libname) |
| 195 |
{ |
| 196 |
int retval = -1; |
| 197 |
void *handle = NULL; |
| 198 |
|
| 199 |
handle = dlopen(libname, RTLD_LAZY | RTLD_LOCAL); |
| 200 |
|
| 201 |
if (handle != NULL) { |
| 202 |
/*H5*/ |
| 203 |
if(DL_H5Functions.H5open == NULL) |
| 204 |
// find_sym will return NULL if it fails so no need to check return ptr |
| 205 |
DL_H5Functions.H5open = (DL_func_H5open)find_sym(handle, "H5open"); |
| 206 |
|
| 207 |
/*H5E*/ |
| 208 |
if(DL_H5Functions.H5Epush1 == NULL) |
| 209 |
DL_H5Functions.H5Epush1 = (DL_func_H5Epush1)find_sym(handle, "H5Epush1"); |
| 210 |
|
| 211 |
if(DL_H5Functions.H5Epush2 == NULL) |
| 212 |
DL_H5Functions.H5Epush2 = (DL_func_H5Epush2)find_sym(handle, "H5Epush2"); |
| 213 |
|
| 214 |
/*H5P*/ |
| 215 |
if(DL_H5Functions.H5Pget_filter_by_id2 == NULL) |
| 216 |
DL_H5Functions.H5Pget_filter_by_id2 = |
| 217 |
(DL_func_H5Pget_filter_by_id2)find_sym(handle, "H5Pget_filter_by_id2"); |
| 218 |
|
| 219 |
if(DL_H5Functions.H5Pget_chunk == NULL) |
| 220 |
DL_H5Functions.H5Pget_chunk = (DL_func_H5Pget_chunk)find_sym(handle, "H5Pget_chunk"); |
| 221 |
|
| 222 |
if(DL_H5Functions.H5Pmodify_filter == NULL) |
| 223 |
DL_H5Functions.H5Pmodify_filter = |
| 224 |
(DL_func_H5Pmodify_filter)find_sym(handle, "H5Pmodify_filter"); |
| 225 |
|
| 226 |
/*H5T*/ |
| 227 |
if(DL_H5Functions.H5Tget_size == NULL) |
| 228 |
DL_H5Functions.H5Tget_size = (DL_func_H5Tget_size)find_sym(handle, "H5Tget_size"); |
| 229 |
|
| 230 |
if(DL_H5Functions.H5Tget_class == NULL) |
| 231 |
DL_H5Functions.H5Tget_class = (DL_func_H5Tget_class)find_sym(handle, "H5Tget_class"); |
| 232 |
|
| 233 |
if(DL_H5Functions.H5Tget_super == NULL) |
| 234 |
DL_H5Functions.H5Tget_super = (DL_func_H5Tget_super)find_sym(handle, "H5Tget_super"); |
| 235 |
|
| 236 |
if(DL_H5Functions.H5Tclose == NULL) |
| 237 |
DL_H5Functions.H5Tclose = (DL_func_H5Tclose)find_sym(handle, "H5Tclose"); |
| 238 |
|
| 239 |
/*H5Z*/ |
| 240 |
if(DL_H5Functions.H5Zregister == NULL) |
| 241 |
DL_H5Functions.H5Zregister = (DL_func_H5Zregister)find_sym(handle, "H5Zregister"); |
| 242 |
|
| 243 |
/*Variables*/ |
| 244 |
if(H5Variables_ptr.h5e_cantregister_ptr == NULL) |
| 245 |
H5Variables_ptr.h5e_cantregister_ptr = find_sym(handle, "H5E_CANTREGISTER_g"); |
| 246 |
|
| 247 |
if(H5Variables_ptr.h5e_callback_ptr == NULL) |
| 248 |
H5Variables_ptr.h5e_callback_ptr = find_sym(handle, "H5E_CALLBACK_g"); |
| 249 |
|
| 250 |
if(H5Variables_ptr.h5e_pline_ptr == NULL) |
| 251 |
H5Variables_ptr.h5e_pline_ptr = find_sym(handle, "H5E_PLINE_g"); |
| 252 |
|
| 253 |
if(H5Variables_ptr.h5e_err_cls_ptr == NULL) |
| 254 |
H5Variables_ptr.h5e_err_cls_ptr = find_sym(handle, "H5E_ERR_CLS_g"); |
| 255 |
|
| 256 |
retval = check_symbols(); |
| 257 |
if(!retval) { |
| 258 |
H5E_CANTREGISTER_g = *((hid_t *)H5Variables_ptr.h5e_cantregister_ptr); |
| 259 |
H5E_CALLBACK_g = *((hid_t *)H5Variables_ptr.h5e_callback_ptr); |
| 260 |
H5E_PLINE_g = *((hid_t *)H5Variables_ptr.h5e_pline_ptr); |
| 261 |
H5E_ERR_CLS_g = *((hid_t *)H5Variables_ptr.h5e_err_cls_ptr); |
| 262 |
is_init = true; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
return retval; |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
#define CALL(fallback, func, ...)\ |
| 271 |
if(DL_H5Functions.func != NULL) {\ |
| 272 |
return DL_H5Functions.func(__VA_ARGS__);\ |
| 273 |
} else {\ |
| 274 |
return fallback;\ |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/*Function wrappers*/ |
| 279 |
/*H5*/ |
| 280 |
herr_t H5open(void) |
| 281 |
{ |
| 282 |
CALL(0, H5open) |
| 283 |
}; |
| 284 |
|
| 285 |
/*H5E*/ |
| 286 |
herr_t H5Epush1(const char *file, const char *func, unsigned line, |
| 287 |
H5E_major_t maj, H5E_minor_t min, const char *str) |
| 288 |
{ |
| 289 |
CALL(0, H5Epush1, file, func, line, maj, min, str) |
| 290 |
} |
| 291 |
|
| 292 |
herr_t H5Epush2(hid_t err_stack, const char *file, const char *func, unsigned line, |
| 293 |
hid_t cls_id, hid_t maj_id, hid_t min_id, const char *fmt, ...) |
| 294 |
{ |
| 295 |
if(DL_H5Functions.H5Epush2 != NULL) { |
| 296 |
/* Avoid using variadic: convert fmt+ ... to a message sting */ |
| 297 |
va_list ap; |
| 298 |
char msg_string[256]; /*Buffer hopefully wide enough*/ |
| 299 |
|
| 300 |
va_start(ap, fmt); |
| 301 |
vsnprintf(msg_string, sizeof(msg_string), fmt, ap); |
| 302 |
msg_string[sizeof(msg_string) - 1] = '\0'; |
| 303 |
va_end(ap); |
| 304 |
|
| 305 |
return DL_H5Functions.H5Epush2(err_stack, file, func, line, cls_id, maj_id, min_id, msg_string); |
| 306 |
} else { |
| 307 |
return 0; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/*H5P*/ |
| 312 |
herr_t H5Pget_filter_by_id2(hid_t plist_id, H5Z_filter_t id, |
| 313 |
unsigned int *flags/*out*/, size_t *cd_nelmts/*out*/, |
| 314 |
unsigned cd_values[]/*out*/, size_t namelen, char name[]/*out*/, |
| 315 |
unsigned *filter_config/*out*/) |
| 316 |
{ |
| 317 |
CALL(0, H5Pget_filter_by_id2, plist_id, id, flags, cd_nelmts, cd_values, namelen, name, filter_config) |
| 318 |
} |
| 319 |
|
| 320 |
int H5Pget_chunk(hid_t plist_id, int max_ndims, hsize_t dim[]/*out*/) |
| 321 |
{ |
| 322 |
CALL(0, H5Pget_chunk, plist_id, max_ndims, dim) |
| 323 |
} |
| 324 |
|
| 325 |
herr_t H5Pmodify_filter(hid_t plist_id, H5Z_filter_t filter, |
| 326 |
unsigned int flags, size_t cd_nelmts, |
| 327 |
const unsigned int cd_values[/*cd_nelmts*/]) |
| 328 |
{ |
| 329 |
CALL(0, H5Pmodify_filter, plist_id, filter, flags, cd_nelmts, cd_values) |
| 330 |
} |
| 331 |
|
| 332 |
/*H5T*/ |
| 333 |
size_t H5Tget_size(hid_t type_id) |
| 334 |
{ |
| 335 |
CALL(0, H5Tget_size, type_id) |
| 336 |
} |
| 337 |
|
| 338 |
H5T_class_t H5Tget_class(hid_t type_id) |
| 339 |
{ |
| 340 |
CALL(H5T_NO_CLASS, H5Tget_class, type_id) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
hid_t H5Tget_super(hid_t type) |
| 345 |
{ |
| 346 |
CALL(0, H5Tget_super, type) |
| 347 |
} |
| 348 |
|
| 349 |
herr_t H5Tclose(hid_t type_id) |
| 350 |
{ |
| 351 |
CALL(0, H5Tclose, type_id) |
| 352 |
} |
| 353 |
|
| 354 |
/*H5Z*/ |
| 355 |
herr_t H5Zregister(const void *cls) |
| 356 |
{ |
| 357 |
CALL(-1, H5Zregister, cls) |
| 358 |
} |