| 1 |
/********************************************************************* |
| 2 |
Blosc - Blocked Shuffling and Compression Library |
| 3 |
|
| 4 |
Copyright (c) 2021 Blosc Development Team <[email protected]> |
| 5 |
https://blosc.org |
| 6 |
License: BSD 3-Clause (see LICENSE.txt) |
| 7 |
|
| 8 |
See LICENSE.txt for details about copyright and rights to use. |
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
|
| 12 |
#ifndef BLOSC_BLOSC_PRIVATE_H |
| 13 |
#define BLOSC_BLOSC_PRIVATE_H |
| 14 |
|
| 15 |
#include "blosc2/blosc2-common.h" |
| 16 |
#include "blosc2.h" |
| 17 |
|
| 18 |
#include <stdbool.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <stdint.h> |
| 21 |
|
| 22 |
/********************************************************************* |
| 23 |
|
| 24 |
Utility functions meant to be used internally. |
| 25 |
|
| 26 |
*********************************************************************/ |
| 27 |
|
| 28 |
#define to_little(dest, src, itemsize) endian_handler(true, dest, src, itemsize) |
| 29 |
#define from_little(dest, src, itemsize) endian_handler(true, dest, src, itemsize) |
| 30 |
#define to_big(dest, src, itemsize) endian_handler(false, dest, src, itemsize) |
| 31 |
#define from_big(dest, src, itemsize) endian_handler(false, dest, src, itemsize) |
| 32 |
|
| 33 |
|
| 34 |
// Return true if platform is little endian; else false |
| 35 |
static bool is_little_endian(void) { |
| 36 |
static const int i = 1; |
| 37 |
char* p = (char*)&i; |
| 38 |
|
| 39 |
if (p[0] == 1) { |
| 40 |
return true; |
| 41 |
} |
| 42 |
else { |
| 43 |
return false; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
static inline void endian_handler(bool little, void *dest, const void *pa, int size) |
| 49 |
{ |
| 50 |
bool little_endian = is_little_endian(); |
| 51 |
if (little_endian == little) { |
| 52 |
memcpy(dest, pa, size); |
| 53 |
} |
| 54 |
else { |
| 55 |
uint8_t* pa_ = (uint8_t*)pa; |
| 56 |
uint8_t pa2_[8]; |
| 57 |
switch (size) { |
| 58 |
case 8: |
| 59 |
pa2_[0] = pa_[7]; |
| 60 |
pa2_[1] = pa_[6]; |
| 61 |
pa2_[2] = pa_[5]; |
| 62 |
pa2_[3] = pa_[4]; |
| 63 |
pa2_[4] = pa_[3]; |
| 64 |
pa2_[5] = pa_[2]; |
| 65 |
pa2_[6] = pa_[1]; |
| 66 |
pa2_[7] = pa_[0]; |
| 67 |
break; |
| 68 |
case 4: |
| 69 |
pa2_[0] = pa_[3]; |
| 70 |
pa2_[1] = pa_[2]; |
| 71 |
pa2_[2] = pa_[1]; |
| 72 |
pa2_[3] = pa_[0]; |
| 73 |
break; |
| 74 |
case 2: |
| 75 |
pa2_[0] = pa_[1]; |
| 76 |
pa2_[1] = pa_[0]; |
| 77 |
break; |
| 78 |
case 1: |
| 79 |
pa2_[0] = pa_[0]; |
| 80 |
break; |
| 81 |
default: |
| 82 |
BLOSC_TRACE_ERROR("Unhandled size: %d.", size); |
| 83 |
} |
| 84 |
memcpy(dest, pa2_, size); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/* Copy 4 bytes from @p *pa to int32_t, changing endianness if necessary. */ |
| 89 |
static inline int32_t sw32_(const void* pa) { |
| 90 |
int32_t idest; |
| 91 |
|
| 92 |
bool little_endian = is_little_endian(); |
| 93 |
if (little_endian) { |
| 94 |
memcpy(&idest, pa, sizeof(idest)); |
| 95 |
} |
| 96 |
else { |
| 97 |
#if defined (__GNUC__) |
| 98 |
return __builtin_bswap32(*(unsigned int *)pa); |
| 99 |
#elif defined (_MSC_VER) /* Visual Studio */ |
| 100 |
return _byteswap_ulong(*(unsigned int *)pa); |
| 101 |
#else |
| 102 |
uint8_t *dest = (uint8_t *)&idest; |
| 103 |
dest[0] = pa_[3]; |
| 104 |
dest[1] = pa_[2]; |
| 105 |
dest[2] = pa_[1]; |
| 106 |
dest[3] = pa_[0]; |
| 107 |
#endif |
| 108 |
} |
| 109 |
return idest; |
| 110 |
} |
| 111 |
|
| 112 |
/* Copy 4 bytes from int32_t to @p *dest, changing endianness if necessary. */ |
| 113 |
static inline void _sw32(void* dest, int32_t a) { |
| 114 |
uint8_t* dest_ = (uint8_t*)dest; |
| 115 |
uint8_t* pa = (uint8_t*)&a; |
| 116 |
|
| 117 |
bool little_endian = is_little_endian(); |
| 118 |
if (little_endian) { |
| 119 |
memcpy(dest_, &a, sizeof(a));; |
| 120 |
} |
| 121 |
else { |
| 122 |
#if defined (__GNUC__) |
| 123 |
*(int32_t *)dest_ = __builtin_bswap32(*(unsigned int *)pa); |
| 124 |
#elif defined (_MSC_VER) /* Visual Studio */ |
| 125 |
*(int32_t *)dest_ = _byteswap_ulong(*(unsigned int *)pa); |
| 126 |
#else |
| 127 |
dest_[0] = pa[3]; |
| 128 |
dest_[1] = pa[2]; |
| 129 |
dest_[2] = pa[1]; |
| 130 |
dest_[3] = pa[0]; |
| 131 |
#endif |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/* Reverse swap bits in a 32-bit integer */ |
| 136 |
static inline int32_t bswap32_(int32_t a) { |
| 137 |
#if defined (__GNUC__) |
| 138 |
return __builtin_bswap32(a); |
| 139 |
|
| 140 |
#elif defined (_MSC_VER) /* Visual Studio */ |
| 141 |
return _byteswap_ulong(a); |
| 142 |
#else |
| 143 |
a = ((a & 0x000000FF) << 24) | |
| 144 |
((a & 0x0000FF00) << 8) | |
| 145 |
((a & 0x00FF0000) >> 8) | |
| 146 |
((a & 0xFF000000) >> 24); |
| 147 |
return a; |
| 148 |
#endif |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @brief Register a filter in Blosc. |
| 153 |
* |
| 154 |
* @param filter The filter to register. |
| 155 |
* |
| 156 |
* @return 0 if succeeds. Else a negative code is returned. |
| 157 |
*/ |
| 158 |
int register_filter_private(blosc2_filter *filter); |
| 159 |
|
| 160 |
/** |
| 161 |
* @brief Register a codec in Blosc. |
| 162 |
* |
| 163 |
* @param codec The codec to register. |
| 164 |
* |
| 165 |
* @return 0 if succeeds. Else a negative code is returned. |
| 166 |
*/ |
| 167 |
int register_codec_private(blosc2_codec *codec); |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* @brief Register a tune in Blosc. |
| 172 |
* |
| 173 |
* @param tune The tune to register. |
| 174 |
* |
| 175 |
* @return 0 if succeeds. Else a negative code is returned. |
| 176 |
*/ |
| 177 |
int register_tuner_private(blosc2_tuner *tuner); |
| 178 |
|
| 179 |
int fill_tuner(blosc2_tuner *tuner); |
| 180 |
|
| 181 |
extern blosc2_tuner g_tuners[256]; |
| 182 |
extern int g_ntuners; |
| 183 |
|
| 184 |
|
| 185 |
#if defined(_WIN32) |
| 186 |
#include <windows.h> |
| 187 |
#ifndef PATH_MAX |
| 188 |
#define PATH_MAX MAX_PATH |
| 189 |
#endif |
| 190 |
#define RTLD_LAZY 0x000 |
| 191 |
#define popen _popen |
| 192 |
#define pclose _pclose |
| 193 |
|
| 194 |
static struct { |
| 195 |
long lasterror; |
| 196 |
const char *err_rutin; |
| 197 |
} |
| 198 |
var = { |
| 199 |
0, |
| 200 |
NULL |
| 201 |
}; |
| 202 |
|
| 203 |
static inline void *dlopen (const char *filename, int flags) { |
| 204 |
BLOSC_UNUSED_PARAM(flags); |
| 205 |
HINSTANCE hInst; |
| 206 |
hInst = LoadLibrary(filename); |
| 207 |
if (hInst==NULL) { |
| 208 |
var.lasterror = GetLastError(); |
| 209 |
var.err_rutin = "dlopen"; |
| 210 |
} |
| 211 |
|
| 212 |
return hInst; |
| 213 |
} |
| 214 |
|
| 215 |
static inline void *dlsym(void *handle, const char *name) { |
| 216 |
FARPROC fp; |
| 217 |
fp = GetProcAddress((HINSTANCE)handle, name); |
| 218 |
if (!fp) { |
| 219 |
var.lasterror = GetLastError (); |
| 220 |
var.err_rutin = "dlsym"; |
| 221 |
} |
| 222 |
return (void *)(intptr_t)fp; |
| 223 |
} |
| 224 |
|
| 225 |
static inline int dlclose(void *handle) { |
| 226 |
bool ok = FreeLibrary((HINSTANCE)handle); |
| 227 |
if (!ok) { |
| 228 |
var.lasterror = GetLastError(); |
| 229 |
var.err_rutin = "dlclose"; |
| 230 |
return BLOSC2_ERROR_FAILURE; |
| 231 |
} |
| 232 |
return BLOSC2_ERROR_SUCCESS; |
| 233 |
} |
| 234 |
|
| 235 |
static inline const char *dlerror (void) { |
| 236 |
static char errstr [88]; |
| 237 |
if (var.lasterror) { |
| 238 |
sprintf (errstr, "%s error #%ld", var.err_rutin, var.lasterror); |
| 239 |
return errstr; |
| 240 |
} else { |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
} |
| 244 |
#else |
| 245 |
#include <dlfcn.h> |
| 246 |
#endif |
| 247 |
|
| 248 |
|
| 249 |
static inline int get_libpath(char *plugin_name, char *libpath, char *python_version) { |
| 250 |
BLOSC_TRACE_INFO("Trying to get plugin path with python%s\n", python_version); |
| 251 |
char python_cmd[PATH_MAX] = {0}; |
| 252 |
sprintf(python_cmd, "python%s -c \"import blosc2_%s; blosc2_%s.print_libpath()\"", python_version, plugin_name, plugin_name); |
| 253 |
FILE *fp = popen(python_cmd, "r"); |
| 254 |
if (fp == NULL) { |
| 255 |
BLOSC_TRACE_ERROR("Could not run python"); |
| 256 |
return BLOSC2_ERROR_FAILURE; |
| 257 |
} |
| 258 |
if (fgets(libpath, PATH_MAX, fp) == NULL) { |
| 259 |
BLOSC_TRACE_ERROR("Could not read python output"); |
| 260 |
pclose(fp); |
| 261 |
return BLOSC2_ERROR_FAILURE; |
| 262 |
} |
| 263 |
pclose(fp); |
| 264 |
|
| 265 |
return BLOSC2_ERROR_SUCCESS; |
| 266 |
} |
| 267 |
|
| 268 |
static inline void* load_lib(char *plugin_name, char *libpath) { |
| 269 |
// Attempt to directly load the library by name |
| 270 |
#if defined(_WIN32) |
| 271 |
// Windows dynamic library (DLL) format |
| 272 |
snprintf(libpath, PATH_MAX, "blosc2_%s.dll", plugin_name); |
| 273 |
#else |
| 274 |
// Unix/Linux/Mac OS dynamic library (.so) format |
| 275 |
snprintf(libpath, PATH_MAX, "libblosc2_%s.so", plugin_name); |
| 276 |
#endif |
| 277 |
void* loaded_lib = dlopen(libpath, RTLD_LAZY); |
| 278 |
if (loaded_lib != NULL) { |
| 279 |
BLOSC_TRACE_INFO("Successfully loaded %s directly\n", libpath); |
| 280 |
return loaded_lib; |
| 281 |
} else { |
| 282 |
#if defined(_WIN32) |
| 283 |
BLOSC_TRACE_INFO("Failed to load %s directly, error: %lu\n", libpath, GetLastError()); |
| 284 |
#else |
| 285 |
BLOSC_TRACE_INFO("Failed to load %s directly, error: %s\n", libpath, dlerror()); |
| 286 |
#endif |
| 287 |
} |
| 288 |
// If direct loading fails, fallback to using Python to find the library path |
| 289 |
if (get_libpath(plugin_name, libpath, "") < 0 && get_libpath(plugin_name, libpath, "3") < 0) { |
| 290 |
BLOSC_TRACE_ERROR("Problems when running python or python3 for getting plugin path"); |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
|
| 294 |
if (strlen(libpath) == 0) { |
| 295 |
BLOSC_TRACE_ERROR("Could not find plugin libpath"); |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
|
| 299 |
// Try to load the library again with the path from Python |
| 300 |
loaded_lib = dlopen(libpath, RTLD_LAZY); |
| 301 |
if (loaded_lib == NULL) { |
| 302 |
BLOSC_TRACE_ERROR("Attempt to load plugin in path '%s' failed with error: %s", libpath, dlerror()); |
| 303 |
} else { |
| 304 |
BLOSC_TRACE_INFO("Successfully loaded library with Python path: %s\n", libpath); |
| 305 |
} |
| 306 |
|
| 307 |
return loaded_lib; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
#endif /* BLOSC_BLOSC_PRIVATE_H */ |