| 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 |
#include "blosc2.h" |
| 13 |
#include "blosc-private.h" |
| 14 |
#include "../plugins/codecs/zfp/blosc2-zfp.h" |
| 15 |
#include "frame.h" |
| 16 |
#include "b2nd-private.h" |
| 17 |
#include "schunk-private.h" |
| 18 |
|
| 19 |
#if defined(USING_CMAKE) |
| 20 |
#include "config.h" |
| 21 |
#endif /* USING_CMAKE */ |
| 22 |
#include "context.h" |
| 23 |
|
| 24 |
#include "shuffle.h" |
| 25 |
#include "delta.h" |
| 26 |
#include "trunc-prec.h" |
| 27 |
#include "blosclz.h" |
| 28 |
#include "stune.h" |
| 29 |
#include "blosc2/codecs-registry.h" |
| 30 |
#include "blosc2/filters-registry.h" |
| 31 |
#include "blosc2/tuners-registry.h" |
| 32 |
|
| 33 |
#include "lz4.h" |
| 34 |
#include "lz4hc.h" |
| 35 |
#ifdef HAVE_IPP |
| 36 |
#include <ipps.h> |
| 37 |
#include <ippdc.h> |
| 38 |
#endif |
| 39 |
#if defined(HAVE_ZLIB_NG) |
| 40 |
#ifdef ZLIB_COMPAT |
| 41 |
#include "zlib.h" |
| 42 |
#else |
| 43 |
#include "zlib-ng.h" |
| 44 |
#endif |
| 45 |
#elif defined(HAVE_ZLIB) |
| 46 |
#include "zlib.h" |
| 47 |
#endif /* HAVE_MINIZ */ |
| 48 |
#if defined(HAVE_ZSTD) |
| 49 |
#include "zstd.h" |
| 50 |
#include "zstd_errors.h" |
| 51 |
// #include "cover.h" // for experimenting with fast cover training for building dicts |
| 52 |
#include "zdict.h" |
| 53 |
#endif /* HAVE_ZSTD */ |
| 54 |
|
| 55 |
#if defined(_WIN32) && !defined(__MINGW32__) |
| 56 |
#include <windows.h> |
| 57 |
#include <malloc.h> |
| 58 |
#include <process.h> |
| 59 |
#define getpid _getpid |
| 60 |
#endif /* _WIN32 */ |
| 61 |
|
| 62 |
#if defined(_WIN32) && !defined(__GNUC__) |
| 63 |
#include "win32/pthread.c" |
| 64 |
#endif |
| 65 |
|
| 66 |
#include <stdio.h> |
| 67 |
#include <stdlib.h> |
| 68 |
#include <errno.h> |
| 69 |
#include <string.h> |
| 70 |
#include <sys/types.h> |
| 71 |
#include <assert.h> |
| 72 |
#include <math.h> |
| 73 |
|
| 74 |
|
| 75 |
/* Synchronization variables */ |
| 76 |
|
| 77 |
/* Global context for non-contextual API */ |
| 78 |
static blosc2_context* g_global_context; |
| 79 |
static pthread_mutex_t global_comp_mutex; |
| 80 |
static int g_compressor = BLOSC_BLOSCLZ; |
| 81 |
static int g_delta = 0; |
| 82 |
/* The default splitmode */ |
| 83 |
static int32_t g_splitmode = BLOSC_FORWARD_COMPAT_SPLIT; |
| 84 |
/* the compressor to use by default */ |
| 85 |
static int16_t g_nthreads = 1; |
| 86 |
static int32_t g_force_blocksize = 0; |
| 87 |
static int g_initlib = 0; |
| 88 |
static blosc2_schunk* g_schunk = NULL; /* the pointer to super-chunk */ |
| 89 |
|
| 90 |
blosc2_codec g_codecs[256] = {0}; |
| 91 |
uint8_t g_ncodecs = 0; |
| 92 |
|
| 93 |
static blosc2_filter g_filters[256] = {0}; |
| 94 |
static uint64_t g_nfilters = 0; |
| 95 |
|
| 96 |
static blosc2_io_cb g_ios[256] = {0}; |
| 97 |
static uint64_t g_nio = 0; |
| 98 |
|
| 99 |
blosc2_tuner g_tuners[256] = {0}; |
| 100 |
int g_ntuners = 0; |
| 101 |
|
| 102 |
static int g_tuner = BLOSC_STUNE; |
| 103 |
|
| 104 |
// Forward declarations |
| 105 |
int init_threadpool(blosc2_context *context); |
| 106 |
int release_threadpool(blosc2_context *context); |
| 107 |
|
| 108 |
/* Macros for synchronization */ |
| 109 |
|
| 110 |
/* Wait until all threads are initialized */ |
| 111 |
#ifdef BLOSC_POSIX_BARRIERS |
| 112 |
#define WAIT_INIT(RET_VAL, CONTEXT_PTR) \ |
| 113 |
do { \ |
| 114 |
rc = pthread_barrier_wait(&(CONTEXT_PTR)->barr_init); \ |
| 115 |
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) { \ |
| 116 |
BLOSC_TRACE_ERROR("Could not wait on barrier (init): %d", rc); \ |
| 117 |
return((RET_VAL)); \ |
| 118 |
} \ |
| 119 |
} while (0) |
| 120 |
#else |
| 121 |
#define WAIT_INIT(RET_VAL, CONTEXT_PTR) \ |
| 122 |
do { \ |
| 123 |
pthread_mutex_lock(&(CONTEXT_PTR)->count_threads_mutex); \ |
| 124 |
if ((CONTEXT_PTR)->count_threads < (CONTEXT_PTR)->nthreads) { \ |
| 125 |
(CONTEXT_PTR)->count_threads++; \ |
| 126 |
pthread_cond_wait(&(CONTEXT_PTR)->count_threads_cv, \ |
| 127 |
&(CONTEXT_PTR)->count_threads_mutex); \ |
| 128 |
} \ |
| 129 |
else { \ |
| 130 |
pthread_cond_broadcast(&(CONTEXT_PTR)->count_threads_cv); \ |
| 131 |
} \ |
| 132 |
pthread_mutex_unlock(&(CONTEXT_PTR)->count_threads_mutex); \ |
| 133 |
} while (0) |
| 134 |
#endif |
| 135 |
|
| 136 |
/* Wait for all threads to finish */ |
| 137 |
#ifdef BLOSC_POSIX_BARRIERS |
| 138 |
#define WAIT_FINISH(RET_VAL, CONTEXT_PTR) \ |
| 139 |
do { \ |
| 140 |
rc = pthread_barrier_wait(&(CONTEXT_PTR)->barr_finish); \ |
| 141 |
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) { \ |
| 142 |
BLOSC_TRACE_ERROR("Could not wait on barrier (finish)"); \ |
| 143 |
return((RET_VAL)); \ |
| 144 |
} \ |
| 145 |
} while (0) |
| 146 |
#else |
| 147 |
#define WAIT_FINISH(RET_VAL, CONTEXT_PTR) \ |
| 148 |
do { \ |
| 149 |
pthread_mutex_lock(&(CONTEXT_PTR)->count_threads_mutex); \ |
| 150 |
if ((CONTEXT_PTR)->count_threads > 0) { \ |
| 151 |
(CONTEXT_PTR)->count_threads--; \ |
| 152 |
pthread_cond_wait(&(CONTEXT_PTR)->count_threads_cv, \ |
| 153 |
&(CONTEXT_PTR)->count_threads_mutex); \ |
| 154 |
} \ |
| 155 |
else { \ |
| 156 |
pthread_cond_broadcast(&(CONTEXT_PTR)->count_threads_cv); \ |
| 157 |
} \ |
| 158 |
pthread_mutex_unlock(&(CONTEXT_PTR)->count_threads_mutex); \ |
| 159 |
} while (0) |
| 160 |
#endif |
| 161 |
|
| 162 |
|
| 163 |
/* global variable to change threading backend from Blosc-managed to caller-managed */ |
| 164 |
static blosc_threads_callback threads_callback = 0; |
| 165 |
static void *threads_callback_data = 0; |
| 166 |
|
| 167 |
|
| 168 |
/* non-threadsafe function should be called before any other Blosc function in |
| 169 |
order to change how threads are managed */ |
| 170 |
void blosc2_set_threads_callback(blosc_threads_callback callback, void *callback_data) |
| 171 |
{ |
| 172 |
threads_callback = callback; |
| 173 |
threads_callback_data = callback_data; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
/* A function for aligned malloc that is portable */ |
| 178 |
static uint8_t* my_malloc(size_t size) { |
| 179 |
void* block = NULL; |
| 180 |
int res = 0; |
| 181 |
|
| 182 |
/* Do an alignment to 32 bytes because AVX2 is supported */ |
| 183 |
#if defined(_WIN32) |
| 184 |
/* A (void *) cast needed for avoiding a warning with MINGW :-/ */ |
| 185 |
block = (void *)_aligned_malloc(size, 32); |
| 186 |
#elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 |
| 187 |
/* Platform does have an implementation of posix_memalign */ |
| 188 |
res = posix_memalign(&block, 32, size); |
| 189 |
#else |
| 190 |
block = malloc(size); |
| 191 |
#endif /* _WIN32 */ |
| 192 |
|
| 193 |
if (block == NULL || res != 0) { |
| 194 |
BLOSC_TRACE_ERROR("Error allocating memory!"); |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
return (uint8_t*)block; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
/* Release memory booked by my_malloc */ |
| 203 |
static void my_free(void* block) { |
| 204 |
#if defined(_WIN32) |
| 205 |
_aligned_free(block); |
| 206 |
#else |
| 207 |
free(block); |
| 208 |
#endif /* _WIN32 */ |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/* |
| 213 |
* Conversion routines between compressor and compression libraries |
| 214 |
*/ |
| 215 |
|
| 216 |
/* Return the library code associated with the compressor name */ |
| 217 |
static int compname_to_clibcode(const char* compname) { |
| 218 |
if (strcmp(compname, BLOSC_BLOSCLZ_COMPNAME) == 0) |
| 219 |
return BLOSC_BLOSCLZ_LIB; |
| 220 |
if (strcmp(compname, BLOSC_LZ4_COMPNAME) == 0) |
| 221 |
return BLOSC_LZ4_LIB; |
| 222 |
if (strcmp(compname, BLOSC_LZ4HC_COMPNAME) == 0) |
| 223 |
return BLOSC_LZ4_LIB; |
| 224 |
if (strcmp(compname, BLOSC_ZLIB_COMPNAME) == 0) |
| 225 |
return BLOSC_ZLIB_LIB; |
| 226 |
if (strcmp(compname, BLOSC_ZSTD_COMPNAME) == 0) |
| 227 |
return BLOSC_ZSTD_LIB; |
| 228 |
for (int i = 0; i < g_ncodecs; ++i) { |
| 229 |
if (strcmp(compname, g_codecs[i].compname) == 0) |
| 230 |
return g_codecs[i].complib; |
| 231 |
} |
| 232 |
return BLOSC2_ERROR_NOT_FOUND; |
| 233 |
} |
| 234 |
|
| 235 |
/* Return the library name associated with the compressor code */ |
| 236 |
static const char* clibcode_to_clibname(int clibcode) { |
| 237 |
if (clibcode == BLOSC_BLOSCLZ_LIB) return BLOSC_BLOSCLZ_LIBNAME; |
| 238 |
if (clibcode == BLOSC_LZ4_LIB) return BLOSC_LZ4_LIBNAME; |
| 239 |
if (clibcode == BLOSC_ZLIB_LIB) return BLOSC_ZLIB_LIBNAME; |
| 240 |
if (clibcode == BLOSC_ZSTD_LIB) return BLOSC_ZSTD_LIBNAME; |
| 241 |
for (int i = 0; i < g_ncodecs; ++i) { |
| 242 |
if (clibcode == g_codecs[i].complib) |
| 243 |
return g_codecs[i].compname; |
| 244 |
} |
| 245 |
return NULL; /* should never happen */ |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
/* |
| 250 |
* Conversion routines between compressor names and compressor codes |
| 251 |
*/ |
| 252 |
|
| 253 |
/* Get the compressor name associated with the compressor code */ |
| 254 |
int blosc2_compcode_to_compname(int compcode, const char** compname) { |
| 255 |
int code = -1; /* -1 means non-existent compressor code */ |
| 256 |
const char* name = NULL; |
| 257 |
|
| 258 |
/* Map the compressor code */ |
| 259 |
if (compcode == BLOSC_BLOSCLZ) |
| 260 |
name = BLOSC_BLOSCLZ_COMPNAME; |
| 261 |
else if (compcode == BLOSC_LZ4) |
| 262 |
name = BLOSC_LZ4_COMPNAME; |
| 263 |
else if (compcode == BLOSC_LZ4HC) |
| 264 |
name = BLOSC_LZ4HC_COMPNAME; |
| 265 |
else if (compcode == BLOSC_ZLIB) |
| 266 |
name = BLOSC_ZLIB_COMPNAME; |
| 267 |
else if (compcode == BLOSC_ZSTD) |
| 268 |
name = BLOSC_ZSTD_COMPNAME; |
| 269 |
else { |
| 270 |
for (int i = 0; i < g_ncodecs; ++i) { |
| 271 |
if (compcode == g_codecs[i].compcode) { |
| 272 |
name = g_codecs[i].compname; |
| 273 |
break; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
*compname = name; |
| 279 |
|
| 280 |
/* Guess if there is support for this code */ |
| 281 |
if (compcode == BLOSC_BLOSCLZ) |
| 282 |
code = BLOSC_BLOSCLZ; |
| 283 |
else if (compcode == BLOSC_LZ4) |
| 284 |
code = BLOSC_LZ4; |
| 285 |
else if (compcode == BLOSC_LZ4HC) |
| 286 |
code = BLOSC_LZ4HC; |
| 287 |
#if defined(HAVE_ZLIB) |
| 288 |
else if (compcode == BLOSC_ZLIB) |
| 289 |
code = BLOSC_ZLIB; |
| 290 |
#endif /* HAVE_ZLIB */ |
| 291 |
#if defined(HAVE_ZSTD) |
| 292 |
else if (compcode == BLOSC_ZSTD) |
| 293 |
code = BLOSC_ZSTD; |
| 294 |
#endif /* HAVE_ZSTD */ |
| 295 |
else if (compcode >= BLOSC_LAST_CODEC) |
| 296 |
code = compcode; |
| 297 |
return code; |
| 298 |
} |
| 299 |
|
| 300 |
/* Get the compressor code for the compressor name. -1 if it is not available */ |
| 301 |
int blosc2_compname_to_compcode(const char* compname) { |
| 302 |
int code = -1; /* -1 means non-existent compressor code */ |
| 303 |
|
| 304 |
if (strcmp(compname, BLOSC_BLOSCLZ_COMPNAME) == 0) { |
| 305 |
code = BLOSC_BLOSCLZ; |
| 306 |
} |
| 307 |
else if (strcmp(compname, BLOSC_LZ4_COMPNAME) == 0) { |
| 308 |
code = BLOSC_LZ4; |
| 309 |
} |
| 310 |
else if (strcmp(compname, BLOSC_LZ4HC_COMPNAME) == 0) { |
| 311 |
code = BLOSC_LZ4HC; |
| 312 |
} |
| 313 |
#if defined(HAVE_ZLIB) |
| 314 |
else if (strcmp(compname, BLOSC_ZLIB_COMPNAME) == 0) { |
| 315 |
code = BLOSC_ZLIB; |
| 316 |
} |
| 317 |
#endif /* HAVE_ZLIB */ |
| 318 |
#if defined(HAVE_ZSTD) |
| 319 |
else if (strcmp(compname, BLOSC_ZSTD_COMPNAME) == 0) { |
| 320 |
code = BLOSC_ZSTD; |
| 321 |
} |
| 322 |
#endif /* HAVE_ZSTD */ |
| 323 |
else{ |
| 324 |
for (int i = 0; i < g_ncodecs; ++i) { |
| 325 |
if (strcmp(compname, g_codecs[i].compname) == 0) { |
| 326 |
code = g_codecs[i].compcode; |
| 327 |
break; |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
return code; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
/* Convert compressor code to blosc compressor format code */ |
| 336 |
static int compcode_to_compformat(int compcode) { |
| 337 |
switch (compcode) { |
| 338 |
case BLOSC_BLOSCLZ: return BLOSC_BLOSCLZ_FORMAT; |
| 339 |
case BLOSC_LZ4: return BLOSC_LZ4_FORMAT; |
| 340 |
case BLOSC_LZ4HC: return BLOSC_LZ4HC_FORMAT; |
| 341 |
|
| 342 |
#if defined(HAVE_ZLIB) |
| 343 |
case BLOSC_ZLIB: return BLOSC_ZLIB_FORMAT; |
| 344 |
#endif /* HAVE_ZLIB */ |
| 345 |
|
| 346 |
#if defined(HAVE_ZSTD) |
| 347 |
case BLOSC_ZSTD: return BLOSC_ZSTD_FORMAT; |
| 348 |
break; |
| 349 |
#endif /* HAVE_ZSTD */ |
| 350 |
default: |
| 351 |
return BLOSC_UDCODEC_FORMAT; |
| 352 |
} |
| 353 |
BLOSC_ERROR(BLOSC2_ERROR_FAILURE); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
/* Convert compressor code to blosc compressor format version */ |
| 358 |
static int compcode_to_compversion(int compcode) { |
| 359 |
/* Write compressor format */ |
| 360 |
switch (compcode) { |
| 361 |
case BLOSC_BLOSCLZ: |
| 362 |
return BLOSC_BLOSCLZ_VERSION_FORMAT; |
| 363 |
case BLOSC_LZ4: |
| 364 |
return BLOSC_LZ4_VERSION_FORMAT; |
| 365 |
case BLOSC_LZ4HC: |
| 366 |
return BLOSC_LZ4HC_VERSION_FORMAT; |
| 367 |
|
| 368 |
#if defined(HAVE_ZLIB) |
| 369 |
case BLOSC_ZLIB: |
| 370 |
return BLOSC_ZLIB_VERSION_FORMAT; |
| 371 |
break; |
| 372 |
#endif /* HAVE_ZLIB */ |
| 373 |
|
| 374 |
#if defined(HAVE_ZSTD) |
| 375 |
case BLOSC_ZSTD: |
| 376 |
return BLOSC_ZSTD_VERSION_FORMAT; |
| 377 |
break; |
| 378 |
#endif /* HAVE_ZSTD */ |
| 379 |
default: |
| 380 |
for (int i = 0; i < g_ncodecs; ++i) { |
| 381 |
if (compcode == g_codecs[i].compcode) { |
| 382 |
return g_codecs[i].version; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
return BLOSC2_ERROR_FAILURE; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
static int lz4_wrap_compress(const char* input, size_t input_length, |
| 391 |
char* output, size_t maxout, int accel, void* hash_table) { |
| 392 |
BLOSC_UNUSED_PARAM(accel); |
| 393 |
int cbytes; |
| 394 |
#ifdef HAVE_IPP |
| 395 |
if (hash_table == NULL) { |
| 396 |
return BLOSC2_ERROR_INVALID_PARAM; // the hash table should always be initialized |
| 397 |
} |
| 398 |
int outlen = (int)maxout; |
| 399 |
int inlen = (int)input_length; |
| 400 |
// I have not found any function that uses `accel` like in `LZ4_compress_fast`, but |
| 401 |
// the IPP LZ4Safe call does a pretty good job on compressing well, so let's use it |
| 402 |
IppStatus status = ippsEncodeLZ4Safe_8u((const Ipp8u*)input, &inlen, |
| 403 |
(Ipp8u*)output, &outlen, (Ipp8u*)hash_table); |
| 404 |
if (status == ippStsDstSizeLessExpected) { |
| 405 |
return 0; // we cannot compress in required outlen |
| 406 |
} |
| 407 |
else if (status != ippStsNoErr) { |
| 408 |
return BLOSC2_ERROR_FAILURE; // an unexpected error happened |
| 409 |
} |
| 410 |
cbytes = outlen; |
| 411 |
#else |
| 412 |
BLOSC_UNUSED_PARAM(hash_table); |
| 413 |
accel = 1; // deactivate acceleration to match IPP behaviour |
| 414 |
cbytes = LZ4_compress_fast(input, output, (int)input_length, (int)maxout, accel); |
| 415 |
#endif |
| 416 |
return cbytes; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
static int lz4hc_wrap_compress(const char* input, size_t input_length, |
| 421 |
char* output, size_t maxout, int clevel) { |
| 422 |
int cbytes; |
| 423 |
if (input_length > (size_t)(UINT32_C(2) << 30)) |
| 424 |
return BLOSC2_ERROR_2GB_LIMIT; |
| 425 |
/* clevel for lz4hc goes up to 12, at least in LZ4 1.7.5 |
| 426 |
* but levels larger than 9 do not buy much compression. */ |
| 427 |
cbytes = LZ4_compress_HC(input, output, (int)input_length, (int)maxout, |
| 428 |
clevel); |
| 429 |
return cbytes; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
static int lz4_wrap_decompress(const char* input, size_t compressed_length, |
| 434 |
char* output, size_t maxout) { |
| 435 |
int nbytes; |
| 436 |
#ifdef HAVE_IPP |
| 437 |
int outlen = (int)maxout; |
| 438 |
int inlen = (int)compressed_length; |
| 439 |
IppStatus status; |
| 440 |
status = ippsDecodeLZ4_8u((const Ipp8u*)input, inlen, (Ipp8u*)output, &outlen); |
| 441 |
//status = ippsDecodeLZ4Dict_8u((const Ipp8u*)input, &inlen, (Ipp8u*)output, 0, &outlen, NULL, 1 << 16); |
| 442 |
nbytes = (status == ippStsNoErr) ? outlen : -outlen; |
| 443 |
#else |
| 444 |
nbytes = LZ4_decompress_safe(input, output, (int)compressed_length, (int)maxout); |
| 445 |
#endif |
| 446 |
if (nbytes != (int)maxout) { |
| 447 |
return 0; |
| 448 |
} |
| 449 |
return (int)maxout; |
| 450 |
} |
| 451 |
|
| 452 |
#if defined(HAVE_ZLIB) |
| 453 |
/* zlib is not very respectful with sharing name space with others. |
| 454 |
Fortunately, its names do not collide with those already in blosc. */ |
| 455 |
static int zlib_wrap_compress(const char* input, size_t input_length, |
| 456 |
char* output, size_t maxout, int clevel) { |
| 457 |
int status; |
| 458 |
#if defined(HAVE_ZLIB_NG) && ! defined(ZLIB_COMPAT) |
| 459 |
size_t cl = maxout; |
| 460 |
status = zng_compress2( |
| 461 |
(uint8_t*)output, &cl, (uint8_t*)input, input_length, clevel); |
| 462 |
#else |
| 463 |
uLongf cl = (uLongf)maxout; |
| 464 |
status = compress2( |
| 465 |
(Bytef*)output, &cl, (Bytef*)input, (uLong)input_length, clevel); |
| 466 |
#endif |
| 467 |
if (status != Z_OK) { |
| 468 |
return 0; |
| 469 |
} |
| 470 |
return (int)cl; |
| 471 |
} |
| 472 |
|
| 473 |
static int zlib_wrap_decompress(const char* input, size_t compressed_length, |
| 474 |
char* output, size_t maxout) { |
| 475 |
int status; |
| 476 |
#if defined(HAVE_ZLIB_NG) && ! defined(ZLIB_COMPAT) |
| 477 |
size_t ul = maxout; |
| 478 |
status = zng_uncompress( |
| 479 |
(uint8_t*)output, &ul, (uint8_t*)input, compressed_length); |
| 480 |
#else |
| 481 |
uLongf ul = (uLongf)maxout; |
| 482 |
status = uncompress( |
| 483 |
(Bytef*)output, &ul, (Bytef*)input, (uLong)compressed_length); |
| 484 |
#endif |
| 485 |
if (status != Z_OK) { |
| 486 |
return 0; |
| 487 |
} |
| 488 |
return (int)ul; |
| 489 |
} |
| 490 |
#endif /* HAVE_ZLIB */ |
| 491 |
|
| 492 |
|
| 493 |
#if defined(HAVE_ZSTD) |
| 494 |
static int zstd_wrap_compress(struct thread_context* thread_context, |
| 495 |
const char* input, size_t input_length, |
| 496 |
char* output, size_t maxout, int clevel) { |
| 497 |
size_t code; |
| 498 |
blosc2_context* context = thread_context->parent_context; |
| 499 |
|
| 500 |
clevel = (clevel < 9) ? clevel * 2 - 1 : ZSTD_maxCLevel(); |
| 501 |
/* Make the level 8 close enough to maxCLevel */ |
| 502 |
if (clevel == 8) clevel = ZSTD_maxCLevel() - 2; |
| 503 |
|
| 504 |
if (thread_context->zstd_cctx == NULL) { |
| 505 |
thread_context->zstd_cctx = ZSTD_createCCtx(); |
| 506 |
} |
| 507 |
|
| 508 |
if (context->use_dict) { |
| 509 |
assert(context->dict_cdict != NULL); |
| 510 |
code = ZSTD_compress_usingCDict( |
| 511 |
thread_context->zstd_cctx, (void*)output, maxout, (void*)input, |
| 512 |
input_length, context->dict_cdict); |
| 513 |
} else { |
| 514 |
code = ZSTD_compressCCtx(thread_context->zstd_cctx, |
| 515 |
(void*)output, maxout, (void*)input, input_length, clevel); |
| 516 |
} |
| 517 |
if (ZSTD_isError(code) != ZSTD_error_no_error) { |
| 518 |
// Blosc will just memcpy this buffer |
| 519 |
return 0; |
| 520 |
} |
| 521 |
return (int)code; |
| 522 |
} |
| 523 |
|
| 524 |
static int zstd_wrap_decompress(struct thread_context* thread_context, |
| 525 |
const char* input, size_t compressed_length, |
| 526 |
char* output, size_t maxout) { |
| 527 |
size_t code; |
| 528 |
blosc2_context* context = thread_context->parent_context; |
| 529 |
|
| 530 |
if (thread_context->zstd_dctx == NULL) { |
| 531 |
thread_context->zstd_dctx = ZSTD_createDCtx(); |
| 532 |
} |
| 533 |
|
| 534 |
if (context->use_dict) { |
| 535 |
assert(context->dict_ddict != NULL); |
| 536 |
code = ZSTD_decompress_usingDDict( |
| 537 |
thread_context->zstd_dctx, (void*)output, maxout, (void*)input, |
| 538 |
compressed_length, context->dict_ddict); |
| 539 |
} else { |
| 540 |
code = ZSTD_decompressDCtx(thread_context->zstd_dctx, |
| 541 |
(void*)output, maxout, (void*)input, compressed_length); |
| 542 |
} |
| 543 |
if (ZSTD_isError(code) != ZSTD_error_no_error) { |
| 544 |
BLOSC_TRACE_ERROR("Error in ZSTD decompression: '%s'. Giving up.", |
| 545 |
ZDICT_getErrorName(code)); |
| 546 |
return 0; |
| 547 |
} |
| 548 |
return (int)code; |
| 549 |
} |
| 550 |
#endif /* HAVE_ZSTD */ |
| 551 |
|
| 552 |
/* Compute acceleration for blosclz */ |
| 553 |
static int get_accel(const blosc2_context* context) { |
| 554 |
int clevel = context->clevel; |
| 555 |
|
| 556 |
if (context->compcode == BLOSC_LZ4) { |
| 557 |
/* This acceleration setting based on discussions held in: |
| 558 |
* https://groups.google.com/forum/#!topic/lz4c/zosy90P8MQw |
| 559 |
*/ |
| 560 |
return (10 - clevel); |
| 561 |
} |
| 562 |
return 1; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
int do_nothing(uint8_t filter, char cmode) { |
| 567 |
if (cmode == 'c') { |
| 568 |
return (filter == BLOSC_NOFILTER); |
| 569 |
} else { |
| 570 |
// TRUNC_PREC do not have to be applied during decompression |
| 571 |
return ((filter == BLOSC_NOFILTER) || (filter == BLOSC_TRUNC_PREC)); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
int next_filter(const uint8_t* filters, int current_filter, char cmode) { |
| 577 |
for (int i = current_filter - 1; i >= 0; i--) { |
| 578 |
if (!do_nothing(filters[i], cmode)) { |
| 579 |
return filters[i]; |
| 580 |
} |
| 581 |
} |
| 582 |
return BLOSC_NOFILTER; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
int last_filter(const uint8_t* filters, char cmode) { |
| 587 |
int last_index = -1; |
| 588 |
for (int i = BLOSC2_MAX_FILTERS - 1; i >= 0; i--) { |
| 589 |
if (!do_nothing(filters[i], cmode)) { |
| 590 |
last_index = i; |
| 591 |
} |
| 592 |
} |
| 593 |
return last_index; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
/* Convert filter pipeline to filter flags */ |
| 598 |
static uint8_t filters_to_flags(const uint8_t* filters) { |
| 599 |
uint8_t flags = 0; |
| 600 |
|
| 601 |
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
| 602 |
switch (filters[i]) { |
| 603 |
case BLOSC_SHUFFLE: |
| 604 |
flags |= BLOSC_DOSHUFFLE; |
| 605 |
break; |
| 606 |
case BLOSC_BITSHUFFLE: |
| 607 |
flags |= BLOSC_DOBITSHUFFLE; |
| 608 |
break; |
| 609 |
case BLOSC_DELTA: |
| 610 |
flags |= BLOSC_DODELTA; |
| 611 |
break; |
| 612 |
default : |
| 613 |
break; |
| 614 |
} |
| 615 |
} |
| 616 |
return flags; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
/* Convert filter flags to filter pipeline */ |
| 621 |
static void flags_to_filters(const uint8_t flags, uint8_t* filters) { |
| 622 |
/* Initialize the filter pipeline */ |
| 623 |
memset(filters, 0, BLOSC2_MAX_FILTERS); |
| 624 |
/* Fill the filter pipeline */ |
| 625 |
if (flags & BLOSC_DOSHUFFLE) |
| 626 |
filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_SHUFFLE; |
| 627 |
if (flags & BLOSC_DOBITSHUFFLE) |
| 628 |
filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_BITSHUFFLE; |
| 629 |
if (flags & BLOSC_DODELTA) |
| 630 |
filters[BLOSC2_MAX_FILTERS - 2] = BLOSC_DELTA; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
/* Get filter flags from header flags */ |
| 635 |
static uint8_t get_filter_flags(const uint8_t header_flags, |
| 636 |
const int32_t typesize) { |
| 637 |
uint8_t flags = 0; |
| 638 |
|
| 639 |
if ((header_flags & BLOSC_DOSHUFFLE) && (typesize > 1)) { |
| 640 |
flags |= BLOSC_DOSHUFFLE; |
| 641 |
} |
| 642 |
if (header_flags & BLOSC_DOBITSHUFFLE) { |
| 643 |
flags |= BLOSC_DOBITSHUFFLE; |
| 644 |
} |
| 645 |
if (header_flags & BLOSC_DODELTA) { |
| 646 |
flags |= BLOSC_DODELTA; |
| 647 |
} |
| 648 |
if (header_flags & BLOSC_MEMCPYED) { |
| 649 |
flags |= BLOSC_MEMCPYED; |
| 650 |
} |
| 651 |
return flags; |
| 652 |
} |
| 653 |
|
| 654 |
typedef struct blosc_header_s { |
| 655 |
uint8_t version; |
| 656 |
uint8_t versionlz; |
| 657 |
uint8_t flags; |
| 658 |
uint8_t typesize; |
| 659 |
int32_t nbytes; |
| 660 |
int32_t blocksize; |
| 661 |
int32_t cbytes; |
| 662 |
// Extended Blosc2 header |
| 663 |
uint8_t filters[BLOSC2_MAX_FILTERS]; |
| 664 |
uint8_t udcompcode; |
| 665 |
uint8_t compcode_meta; |
| 666 |
uint8_t filters_meta[BLOSC2_MAX_FILTERS]; |
| 667 |
uint8_t reserved2; |
| 668 |
uint8_t blosc2_flags; |
| 669 |
} blosc_header; |
| 670 |
|
| 671 |
|
| 672 |
int read_chunk_header(const uint8_t* src, int32_t srcsize, bool extended_header, blosc_header* header) |
| 673 |
{ |
| 674 |
memset(header, 0, sizeof(blosc_header)); |
| 675 |
|
| 676 |
if (srcsize < BLOSC_MIN_HEADER_LENGTH) { |
| 677 |
BLOSC_TRACE_ERROR("Not enough space to read Blosc header."); |
| 678 |
return BLOSC2_ERROR_READ_BUFFER; |
| 679 |
} |
| 680 |
|
| 681 |
memcpy(header, src, BLOSC_MIN_HEADER_LENGTH); |
| 682 |
|
| 683 |
bool little_endian = is_little_endian(); |
| 684 |
|
| 685 |
if (!little_endian) { |
| 686 |
header->nbytes = bswap32_(header->nbytes); |
| 687 |
header->blocksize = bswap32_(header->blocksize); |
| 688 |
header->cbytes = bswap32_(header->cbytes); |
| 689 |
} |
| 690 |
|
| 691 |
if (header->version > BLOSC2_VERSION_FORMAT) { |
| 692 |
/* Version from future */ |
| 693 |
return BLOSC2_ERROR_VERSION_SUPPORT; |
| 694 |
} |
| 695 |
if (header->cbytes < BLOSC_MIN_HEADER_LENGTH) { |
| 696 |
BLOSC_TRACE_ERROR("`cbytes` is too small to read min header."); |
| 697 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 698 |
} |
| 699 |
if (header->blocksize <= 0 || (header->nbytes > 0 && (header->blocksize > header->nbytes))) { |
| 700 |
BLOSC_TRACE_ERROR("`blocksize` is zero or greater than uncompressed size"); |
| 701 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 702 |
} |
| 703 |
if (header->blocksize > BLOSC2_MAXBLOCKSIZE) { |
| 704 |
BLOSC_TRACE_ERROR("`blocksize` greater than maximum allowed"); |
| 705 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 706 |
} |
| 707 |
if (header->typesize == 0) { |
| 708 |
BLOSC_TRACE_ERROR("`typesize` is zero."); |
| 709 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 710 |
} |
| 711 |
|
| 712 |
/* Read extended header if it is wanted */ |
| 713 |
if ((extended_header) && (header->flags & BLOSC_DOSHUFFLE) && (header->flags & BLOSC_DOBITSHUFFLE)) { |
| 714 |
if (header->cbytes < BLOSC_EXTENDED_HEADER_LENGTH) { |
| 715 |
BLOSC_TRACE_ERROR("`cbytes` is too small to read extended header."); |
| 716 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 717 |
} |
| 718 |
if (srcsize < BLOSC_EXTENDED_HEADER_LENGTH) { |
| 719 |
BLOSC_TRACE_ERROR("Not enough space to read Blosc extended header."); |
| 720 |
return BLOSC2_ERROR_READ_BUFFER; |
| 721 |
} |
| 722 |
|
| 723 |
memcpy((uint8_t *)header + BLOSC_MIN_HEADER_LENGTH, src + BLOSC_MIN_HEADER_LENGTH, |
| 724 |
BLOSC_EXTENDED_HEADER_LENGTH - BLOSC_MIN_HEADER_LENGTH); |
| 725 |
|
| 726 |
int32_t special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK; |
| 727 |
if (special_type != 0) { |
| 728 |
if (special_type == BLOSC2_SPECIAL_VALUE) { |
| 729 |
// In this case, the actual type size must be derived from the cbytes |
| 730 |
int32_t typesize = header->cbytes - BLOSC_EXTENDED_HEADER_LENGTH; |
| 731 |
if (typesize <= 0) { |
| 732 |
BLOSC_TRACE_ERROR("`typesize` is zero or negative"); |
| 733 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 734 |
} |
| 735 |
if (typesize > BLOSC2_MAXTYPESIZE) { |
| 736 |
BLOSC_TRACE_ERROR("`typesize` is greater than maximum allowed"); |
| 737 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 738 |
} |
| 739 |
if (typesize > header->nbytes) { |
| 740 |
BLOSC_TRACE_ERROR("`typesize` is greater than `nbytes`"); |
| 741 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 742 |
} |
| 743 |
if (header->nbytes % typesize != 0) { |
| 744 |
BLOSC_TRACE_ERROR("`nbytes` is not a multiple of typesize"); |
| 745 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 746 |
} |
| 747 |
} |
| 748 |
else { |
| 749 |
if (header->nbytes % header->typesize != 0) { |
| 750 |
BLOSC_TRACE_ERROR("`nbytes` is not a multiple of typesize"); |
| 751 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
// The number of filters depends on the version of the header. Blosc2 alpha series |
| 756 |
// did not initialize filters to zero beyond the max supported. |
| 757 |
if (header->version == BLOSC2_VERSION_FORMAT_ALPHA) { |
| 758 |
header->filters[5] = 0; |
| 759 |
header->filters_meta[5] = 0; |
| 760 |
} |
| 761 |
} |
| 762 |
else { |
| 763 |
flags_to_filters(header->flags, header->filters); |
| 764 |
} |
| 765 |
return 0; |
| 766 |
} |
| 767 |
|
| 768 |
static inline void blosc2_calculate_blocks(blosc2_context* context) { |
| 769 |
/* Compute number of blocks in buffer */ |
| 770 |
context->nblocks = context->sourcesize / context->blocksize; |
| 771 |
context->leftover = context->sourcesize % context->blocksize; |
| 772 |
context->nblocks = (context->leftover > 0) ? |
| 773 |
(context->nblocks + 1) : context->nblocks; |
| 774 |
} |
| 775 |
|
| 776 |
static int blosc2_initialize_context_from_header(blosc2_context* context, blosc_header* header) { |
| 777 |
context->header_flags = header->flags; |
| 778 |
context->typesize = header->typesize; |
| 779 |
context->sourcesize = header->nbytes; |
| 780 |
context->blocksize = header->blocksize; |
| 781 |
context->blosc2_flags = header->blosc2_flags; |
| 782 |
context->compcode = header->flags >> 5; |
| 783 |
if (context->compcode == BLOSC_UDCODEC_FORMAT) { |
| 784 |
context->compcode = header->udcompcode; |
| 785 |
} |
| 786 |
blosc2_calculate_blocks(context); |
| 787 |
|
| 788 |
bool is_lazy = false; |
| 789 |
if ((context->header_flags & BLOSC_DOSHUFFLE) && |
| 790 |
(context->header_flags & BLOSC_DOBITSHUFFLE)) { |
| 791 |
/* Extended header */ |
| 792 |
context->header_overhead = BLOSC_EXTENDED_HEADER_LENGTH; |
| 793 |
|
| 794 |
memcpy(context->filters, header->filters, BLOSC2_MAX_FILTERS); |
| 795 |
memcpy(context->filters_meta, header->filters_meta, BLOSC2_MAX_FILTERS); |
| 796 |
context->compcode_meta = header->compcode_meta; |
| 797 |
|
| 798 |
context->filter_flags = filters_to_flags(header->filters); |
| 799 |
context->special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK; |
| 800 |
|
| 801 |
is_lazy = (context->blosc2_flags & 0x08u); |
| 802 |
} |
| 803 |
else { |
| 804 |
context->header_overhead = BLOSC_MIN_HEADER_LENGTH; |
| 805 |
context->filter_flags = get_filter_flags(context->header_flags, context->typesize); |
| 806 |
flags_to_filters(context->header_flags, context->filters); |
| 807 |
} |
| 808 |
|
| 809 |
// Some checks for malformed headers |
| 810 |
if (!is_lazy && header->cbytes > context->srcsize) { |
| 811 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 812 |
} |
| 813 |
|
| 814 |
return 0; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
int fill_filter(blosc2_filter *filter) { |
| 819 |
char libpath[PATH_MAX]; |
| 820 |
void *lib = load_lib(filter->name, libpath); |
| 821 |
if(lib == NULL) { |
| 822 |
BLOSC_TRACE_ERROR("Error while loading the library"); |
| 823 |
return BLOSC2_ERROR_FAILURE; |
| 824 |
} |
| 825 |
|
| 826 |
filter_info *info = dlsym(lib, "info"); |
| 827 |
filter->forward = dlsym(lib, info->forward); |
| 828 |
filter->backward = dlsym(lib, info->backward); |
| 829 |
|
| 830 |
if (filter->forward == NULL || filter->backward == NULL){ |
| 831 |
BLOSC_TRACE_ERROR("Wrong library loaded"); |
| 832 |
dlclose(lib); |
| 833 |
return BLOSC2_ERROR_FAILURE; |
| 834 |
} |
| 835 |
|
| 836 |
return BLOSC2_ERROR_SUCCESS; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
int fill_codec(blosc2_codec *codec) { |
| 841 |
char libpath[PATH_MAX]; |
| 842 |
void *lib = load_lib(codec->compname, libpath); |
| 843 |
if(lib == NULL) { |
| 844 |
BLOSC_TRACE_ERROR("Error while loading the library for codec `%s`", codec->compname); |
| 845 |
return BLOSC2_ERROR_FAILURE; |
| 846 |
} |
| 847 |
|
| 848 |
codec_info *info = dlsym(lib, "info"); |
| 849 |
if (info == NULL) { |
| 850 |
BLOSC_TRACE_ERROR("`info` symbol cannot be loaded from plugin `%s`", codec->compname); |
| 851 |
dlclose(lib); |
| 852 |
return BLOSC2_ERROR_FAILURE; |
| 853 |
} |
| 854 |
|
| 855 |
codec->encoder = dlsym(lib, info->encoder); |
| 856 |
codec->decoder = dlsym(lib, info->decoder); |
| 857 |
if (codec->encoder == NULL || codec->decoder == NULL) { |
| 858 |
BLOSC_TRACE_ERROR("encoder or decoder cannot be loaded from plugin `%s`", codec->compname); |
| 859 |
dlclose(lib); |
| 860 |
return BLOSC2_ERROR_FAILURE; |
| 861 |
} |
| 862 |
|
| 863 |
return BLOSC2_ERROR_SUCCESS; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
int fill_tuner(blosc2_tuner *tuner) { |
| 868 |
char libpath[PATH_MAX] = {0}; |
| 869 |
void *lib = load_lib(tuner->name, libpath); |
| 870 |
if(lib == NULL) { |
| 871 |
BLOSC_TRACE_ERROR("Error while loading the library"); |
| 872 |
return BLOSC2_ERROR_FAILURE; |
| 873 |
} |
| 874 |
|
| 875 |
tuner_info *info = dlsym(lib, "info"); |
| 876 |
tuner->init = dlsym(lib, info->init); |
| 877 |
tuner->update = dlsym(lib, info->update); |
| 878 |
tuner->next_blocksize = dlsym(lib, info->next_blocksize); |
| 879 |
tuner->free = dlsym(lib, info->free); |
| 880 |
tuner->next_cparams = dlsym(lib, info->next_cparams); |
| 881 |
|
| 882 |
if (tuner->init == NULL || tuner->update == NULL || tuner->next_blocksize == NULL || tuner->free == NULL |
| 883 |
|| tuner->next_cparams == NULL){ |
| 884 |
BLOSC_TRACE_ERROR("Wrong library loaded"); |
| 885 |
dlclose(lib); |
| 886 |
return BLOSC2_ERROR_FAILURE; |
| 887 |
} |
| 888 |
|
| 889 |
return BLOSC2_ERROR_SUCCESS; |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
static int blosc2_intialize_header_from_context(blosc2_context* context, blosc_header* header, bool extended_header) { |
| 894 |
memset(header, 0, sizeof(blosc_header)); |
| 895 |
|
| 896 |
header->version = BLOSC2_VERSION_FORMAT; |
| 897 |
header->versionlz = compcode_to_compversion(context->compcode); |
| 898 |
header->flags = context->header_flags; |
| 899 |
header->typesize = (uint8_t)context->typesize; |
| 900 |
header->nbytes = (int32_t)context->sourcesize; |
| 901 |
header->blocksize = (int32_t)context->blocksize; |
| 902 |
|
| 903 |
int little_endian = is_little_endian(); |
| 904 |
if (!little_endian) { |
| 905 |
header->nbytes = bswap32_(header->nbytes); |
| 906 |
header->blocksize = bswap32_(header->blocksize); |
| 907 |
// cbytes written after compression |
| 908 |
} |
| 909 |
|
| 910 |
if (extended_header) { |
| 911 |
/* Store filter pipeline info at the end of the header */ |
| 912 |
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
| 913 |
header->filters[i] = context->filters[i]; |
| 914 |
header->filters_meta[i] = context->filters_meta[i]; |
| 915 |
} |
| 916 |
header->udcompcode = context->compcode; |
| 917 |
header->compcode_meta = context->compcode_meta; |
| 918 |
|
| 919 |
if (!little_endian) { |
| 920 |
header->blosc2_flags |= BLOSC2_BIGENDIAN; |
| 921 |
} |
| 922 |
if (context->use_dict) { |
| 923 |
header->blosc2_flags |= BLOSC2_USEDICT; |
| 924 |
} |
| 925 |
if (context->blosc2_flags & BLOSC2_INSTR_CODEC) { |
| 926 |
header->blosc2_flags |= BLOSC2_INSTR_CODEC; |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
return 0; |
| 931 |
} |
| 932 |
|
| 933 |
void _cycle_buffers(uint8_t **src, uint8_t **dest, uint8_t **tmp) { |
| 934 |
uint8_t *tmp2 = *src; |
| 935 |
*src = *dest; |
| 936 |
*dest = *tmp; |
| 937 |
*tmp = tmp2; |
| 938 |
} |
| 939 |
|
| 940 |
uint8_t* pipeline_forward(struct thread_context* thread_context, const int32_t bsize, |
| 941 |
const uint8_t* src, const int32_t offset, |
| 942 |
uint8_t* dest, uint8_t* tmp) { |
| 943 |
blosc2_context* context = thread_context->parent_context; |
| 944 |
uint8_t* _src = (uint8_t*)src + offset; |
| 945 |
uint8_t* _tmp = tmp; |
| 946 |
uint8_t* _dest = dest; |
| 947 |
int32_t typesize = context->typesize; |
| 948 |
uint8_t* filters = context->filters; |
| 949 |
uint8_t* filters_meta = context->filters_meta; |
| 950 |
bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
| 951 |
|
| 952 |
/* Prefilter function */ |
| 953 |
if (context->prefilter != NULL) { |
| 954 |
/* Set unwritten values to zero */ |
| 955 |
memset(_dest, 0, bsize); |
| 956 |
// Create new prefilter parameters for this block (must be private for each thread) |
| 957 |
blosc2_prefilter_params preparams; |
| 958 |
memcpy(&preparams, context->preparams, sizeof(preparams)); |
| 959 |
preparams.input = _src; |
| 960 |
preparams.output = _dest; |
| 961 |
preparams.output_size = bsize; |
| 962 |
preparams.output_typesize = typesize; |
| 963 |
preparams.output_offset = offset; |
| 964 |
preparams.nblock = offset / context->blocksize; |
| 965 |
preparams.nchunk = context->schunk != NULL ? context->schunk->current_nchunk : -1; |
| 966 |
preparams.tid = thread_context->tid; |
| 967 |
preparams.ttmp = thread_context->tmp; |
| 968 |
preparams.ttmp_nbytes = thread_context->tmp_nbytes; |
| 969 |
preparams.ctx = context; |
| 970 |
|
| 971 |
if (context->prefilter(&preparams) != 0) { |
| 972 |
BLOSC_TRACE_ERROR("Execution of prefilter function failed"); |
| 973 |
return NULL; |
| 974 |
} |
| 975 |
|
| 976 |
if (memcpyed) { |
| 977 |
// No more filters are required |
| 978 |
return _dest; |
| 979 |
} |
| 980 |
_cycle_buffers(&_src, &_dest, &_tmp); |
| 981 |
} |
| 982 |
|
| 983 |
/* Process the filter pipeline */ |
| 984 |
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
| 985 |
int rc = BLOSC2_ERROR_SUCCESS; |
| 986 |
if (filters[i] <= BLOSC2_DEFINED_FILTERS_STOP) { |
| 987 |
switch (filters[i]) { |
| 988 |
case BLOSC_SHUFFLE: |
| 989 |
shuffle(typesize, bsize, _src, _dest); |
| 990 |
break; |
| 991 |
case BLOSC_BITSHUFFLE: |
| 992 |
if (bitshuffle(typesize, bsize, _src, _dest) < 0) { |
| 993 |
return NULL; |
| 994 |
} |
| 995 |
break; |
| 996 |
case BLOSC_DELTA: |
| 997 |
delta_encoder(src, offset, bsize, typesize, _src, _dest); |
| 998 |
break; |
| 999 |
case BLOSC_TRUNC_PREC: |
| 1000 |
if (truncate_precision(filters_meta[i], typesize, bsize, _src, _dest) < 0) { |
| 1001 |
return NULL; |
| 1002 |
} |
| 1003 |
break; |
| 1004 |
default: |
| 1005 |
if (filters[i] != BLOSC_NOFILTER) { |
| 1006 |
BLOSC_TRACE_ERROR("Filter %d not handled during compression\n", filters[i]); |
| 1007 |
return NULL; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
else { |
| 1012 |
// Look for the filters_meta in user filters and run it |
| 1013 |
for (uint64_t j = 0; j < g_nfilters; ++j) { |
| 1014 |
if (g_filters[j].id == filters[i]) { |
| 1015 |
if (g_filters[j].forward == NULL) { |
| 1016 |
// Dynamically load library |
| 1017 |
if (fill_filter(&g_filters[j]) < 0) { |
| 1018 |
BLOSC_TRACE_ERROR("Could not load filter %d\n", g_filters[j].id); |
| 1019 |
return NULL; |
| 1020 |
} |
| 1021 |
} |
| 1022 |
if (g_filters[j].forward != NULL) { |
| 1023 |
blosc2_cparams cparams; |
| 1024 |
blosc2_ctx_get_cparams(context, &cparams); |
| 1025 |
rc = g_filters[j].forward(_src, _dest, bsize, filters_meta[i], &cparams, g_filters[j].id); |
| 1026 |
} else { |
| 1027 |
BLOSC_TRACE_ERROR("Forward function is NULL"); |
| 1028 |
return NULL; |
| 1029 |
} |
| 1030 |
if (rc != BLOSC2_ERROR_SUCCESS) { |
| 1031 |
BLOSC_TRACE_ERROR("User-defined filter %d failed during compression\n", filters[i]); |
| 1032 |
return NULL; |
| 1033 |
} |
| 1034 |
goto urfiltersuccess; |
| 1035 |
} |
| 1036 |
} |
| 1037 |
BLOSC_TRACE_ERROR("User-defined filter %d not found during compression\n", filters[i]); |
| 1038 |
return NULL; |
| 1039 |
|
| 1040 |
urfiltersuccess:; |
| 1041 |
|
| 1042 |
} |
| 1043 |
|
| 1044 |
// Cycle buffers when required |
| 1045 |
if (filters[i] != BLOSC_NOFILTER) { |
| 1046 |
_cycle_buffers(&_src, &_dest, &_tmp); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
return _src; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
// Optimized version for detecting runs. It compares 8 bytes values wherever possible. |
| 1054 |
static bool get_run(const uint8_t* ip, const uint8_t* ip_bound) { |
| 1055 |
uint8_t x = *ip; |
| 1056 |
int64_t value, value2; |
| 1057 |
/* Broadcast the value for every byte in a 64-bit register */ |
| 1058 |
memset(&value, x, 8); |
| 1059 |
while (ip < (ip_bound - 8)) { |
| 1060 |
#if defined(BLOSC_STRICT_ALIGN) |
| 1061 |
memcpy(&value2, ip, 8); |
| 1062 |
#else |
| 1063 |
value2 = *(int64_t*)ip; |
| 1064 |
#endif |
| 1065 |
if (value != value2) { |
| 1066 |
// Values differ. We don't have a run. |
| 1067 |
return false; |
| 1068 |
} |
| 1069 |
else { |
| 1070 |
ip += 8; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
/* Look into the remainder */ |
| 1074 |
while ((ip < ip_bound) && (*ip == x)) ip++; |
| 1075 |
return ip == ip_bound ? true : false; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
/* Shuffle & compress a single block */ |
| 1080 |
static int blosc_c(struct thread_context* thread_context, int32_t bsize, |
| 1081 |
int32_t leftoverblock, int32_t ntbytes, int32_t destsize, |
| 1082 |
const uint8_t* src, const int32_t offset, uint8_t* dest, |
| 1083 |
uint8_t* tmp, uint8_t* tmp2) { |
| 1084 |
blosc2_context* context = thread_context->parent_context; |
| 1085 |
int dont_split = (context->header_flags & 0x10) >> 4; |
| 1086 |
int dict_training = context->use_dict && context->dict_cdict == NULL; |
| 1087 |
int32_t j, neblock, nstreams; |
| 1088 |
int32_t cbytes; /* number of compressed bytes in split */ |
| 1089 |
int32_t ctbytes = 0; /* number of compressed bytes in block */ |
| 1090 |
int32_t maxout; |
| 1091 |
int32_t typesize = context->typesize; |
| 1092 |
const char* compname; |
| 1093 |
int accel; |
| 1094 |
const uint8_t* _src; |
| 1095 |
uint8_t *_tmp = tmp, *_tmp2 = tmp2; |
| 1096 |
int last_filter_index = last_filter(context->filters, 'c'); |
| 1097 |
bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
| 1098 |
bool instr_codec = context->blosc2_flags & BLOSC2_INSTR_CODEC; |
| 1099 |
blosc_timestamp_t last, current; |
| 1100 |
float filter_time = 0.f; |
| 1101 |
|
| 1102 |
if (instr_codec) { |
| 1103 |
blosc_set_timestamp(&last); |
| 1104 |
} |
| 1105 |
|
| 1106 |
// See whether we have a run here |
| 1107 |
if (last_filter_index >= 0 || context->prefilter != NULL) { |
| 1108 |
/* Apply the filter pipeline just for the prefilter */ |
| 1109 |
if (memcpyed && context->prefilter != NULL) { |
| 1110 |
// We only need the prefilter output |
| 1111 |
_src = pipeline_forward(thread_context, bsize, src, offset, dest, _tmp2); |
| 1112 |
if (_src == NULL) { |
| 1113 |
return BLOSC2_ERROR_FILTER_PIPELINE; |
| 1114 |
} |
| 1115 |
return bsize; |
| 1116 |
} |
| 1117 |
/* Apply regular filter pipeline */ |
| 1118 |
_src = pipeline_forward(thread_context, bsize, src, offset, _tmp, _tmp2); |
| 1119 |
if (_src == NULL) { |
| 1120 |
return BLOSC2_ERROR_FILTER_PIPELINE; |
| 1121 |
} |
| 1122 |
} else { |
| 1123 |
_src = src + offset; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (instr_codec) { |
| 1127 |
blosc_set_timestamp(¤t); |
| 1128 |
filter_time = (float) blosc_elapsed_secs(last, current); |
| 1129 |
last = current; |
| 1130 |
} |
| 1131 |
|
| 1132 |
assert(context->clevel > 0); |
| 1133 |
|
| 1134 |
/* Calculate acceleration for different compressors */ |
| 1135 |
accel = get_accel(context); |
| 1136 |
|
| 1137 |
/* The number of compressed data streams for this block */ |
| 1138 |
if (!dont_split && !leftoverblock && !dict_training) { |
| 1139 |
nstreams = (int32_t)typesize; |
| 1140 |
} |
| 1141 |
else { |
| 1142 |
nstreams = 1; |
| 1143 |
} |
| 1144 |
neblock = bsize / nstreams; |
| 1145 |
for (j = 0; j < nstreams; j++) { |
| 1146 |
if (instr_codec) { |
| 1147 |
blosc_set_timestamp(&last); |
| 1148 |
} |
| 1149 |
if (!dict_training) { |
| 1150 |
dest += sizeof(int32_t); |
| 1151 |
ntbytes += sizeof(int32_t); |
| 1152 |
ctbytes += sizeof(int32_t); |
| 1153 |
|
| 1154 |
const uint8_t *ip = (uint8_t *) _src + j * neblock; |
| 1155 |
const uint8_t *ipbound = (uint8_t *) _src + (j + 1) * neblock; |
| 1156 |
|
| 1157 |
if (context->header_overhead == BLOSC_EXTENDED_HEADER_LENGTH && get_run(ip, ipbound)) { |
| 1158 |
// A run |
| 1159 |
int32_t value = _src[j * neblock]; |
| 1160 |
if (ntbytes > destsize) { |
| 1161 |
return 0; /* Non-compressible data */ |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (instr_codec) { |
| 1165 |
blosc_set_timestamp(¤t); |
| 1166 |
int32_t instr_size = sizeof(blosc2_instr); |
| 1167 |
ntbytes += instr_size; |
| 1168 |
ctbytes += instr_size; |
| 1169 |
if (ntbytes > destsize) { |
| 1170 |
return 0; /* Non-compressible data */ |
| 1171 |
} |
| 1172 |
_sw32(dest - 4, instr_size); |
| 1173 |
blosc2_instr *desti = (blosc2_instr *)dest; |
| 1174 |
memset(desti, 0, sizeof(blosc2_instr)); |
| 1175 |
// Special values have an overhead of about 1 int32 |
| 1176 |
int32_t ssize = value == 0 ? sizeof(int32_t) : sizeof(int32_t) + 1; |
| 1177 |
desti->cratio = (float) neblock / (float) ssize; |
| 1178 |
float ctime = (float) blosc_elapsed_secs(last, current); |
| 1179 |
desti->cspeed = (float) neblock / ctime; |
| 1180 |
desti->filter_speed = (float) neblock / filter_time; |
| 1181 |
desti->flags[0] = 1; // mark a runlen |
| 1182 |
dest += instr_size; |
| 1183 |
continue; |
| 1184 |
} |
| 1185 |
|
| 1186 |
// Encode the repeated byte in the first (LSB) byte of the length of the split. |
| 1187 |
_sw32(dest - 4, -value); // write the value in two's complement |
| 1188 |
if (value > 0) { |
| 1189 |
// Mark encoding as a run-length (== 0 is always a 0's run) |
| 1190 |
ntbytes += 1; |
| 1191 |
ctbytes += 1; |
| 1192 |
if (ntbytes > destsize) { |
| 1193 |
return 0; /* Non-compressible data */ |
| 1194 |
} |
| 1195 |
// Set MSB bit (sign) to 1 (not really necessary here, but for demonstration purposes) |
| 1196 |
// dest[-1] |= 0x80; |
| 1197 |
dest[0] = 0x1; // set run-length bit (0) in token |
| 1198 |
dest += 1; |
| 1199 |
} |
| 1200 |
continue; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
maxout = neblock; |
| 1205 |
if (ntbytes + maxout > destsize && !instr_codec) { |
| 1206 |
/* avoid buffer * overrun */ |
| 1207 |
maxout = destsize - ntbytes; |
| 1208 |
if (maxout <= 0) { |
| 1209 |
return 0; /* non-compressible block */ |
| 1210 |
} |
| 1211 |
} |
| 1212 |
if (dict_training) { |
| 1213 |
// We are in the build dict state, so don't compress |
| 1214 |
// TODO: copy only a percentage for sampling |
| 1215 |
memcpy(dest, _src + j * neblock, (unsigned int)neblock); |
| 1216 |
cbytes = (int32_t)neblock; |
| 1217 |
} |
| 1218 |
else if (context->compcode == BLOSC_BLOSCLZ) { |
| 1219 |
cbytes = blosclz_compress(context->clevel, _src + j * neblock, |
| 1220 |
(int)neblock, dest, maxout, context); |
| 1221 |
} |
| 1222 |
else if (context->compcode == BLOSC_LZ4) { |
| 1223 |
void *hash_table = NULL; |
| 1224 |
#ifdef HAVE_IPP |
| 1225 |
hash_table = (void*)thread_context->lz4_hash_table; |
| 1226 |
#endif |
| 1227 |
cbytes = lz4_wrap_compress((char*)_src + j * neblock, (size_t)neblock, |
| 1228 |
(char*)dest, (size_t)maxout, accel, hash_table); |
| 1229 |
} |
| 1230 |
else if (context->compcode == BLOSC_LZ4HC) { |
| 1231 |
cbytes = lz4hc_wrap_compress((char*)_src + j * neblock, (size_t)neblock, |
| 1232 |
(char*)dest, (size_t)maxout, context->clevel); |
| 1233 |
} |
| 1234 |
#if defined(HAVE_ZLIB) |
| 1235 |
else if (context->compcode == BLOSC_ZLIB) { |
| 1236 |
cbytes = zlib_wrap_compress((char*)_src + j * neblock, (size_t)neblock, |
| 1237 |
(char*)dest, (size_t)maxout, context->clevel); |
| 1238 |
} |
| 1239 |
#endif /* HAVE_ZLIB */ |
| 1240 |
#if defined(HAVE_ZSTD) |
| 1241 |
else if (context->compcode == BLOSC_ZSTD) { |
| 1242 |
cbytes = zstd_wrap_compress(thread_context, |
| 1243 |
(char*)_src + j * neblock, (size_t)neblock, |
| 1244 |
(char*)dest, (size_t)maxout, context->clevel); |
| 1245 |
} |
| 1246 |
#endif /* HAVE_ZSTD */ |
| 1247 |
else if (context->compcode > BLOSC2_DEFINED_CODECS_STOP) { |
| 1248 |
for (int i = 0; i < g_ncodecs; ++i) { |
| 1249 |
if (g_codecs[i].compcode == context->compcode) { |
| 1250 |
if (g_codecs[i].encoder == NULL) { |
| 1251 |
// Dynamically load codec plugin |
| 1252 |
if (fill_codec(&g_codecs[i]) < 0) { |
| 1253 |
BLOSC_TRACE_ERROR("Could not load codec %d.", g_codecs[i].compcode); |
| 1254 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
blosc2_cparams cparams; |
| 1258 |
blosc2_ctx_get_cparams(context, &cparams); |
| 1259 |
cbytes = g_codecs[i].encoder(_src + j * neblock, |
| 1260 |
neblock, |
| 1261 |
dest, |
| 1262 |
maxout, |
| 1263 |
context->compcode_meta, |
| 1264 |
&cparams, |
| 1265 |
context->src); |
| 1266 |
goto urcodecsuccess; |
| 1267 |
} |
| 1268 |
} |
| 1269 |
BLOSC_TRACE_ERROR("User-defined compressor codec %d not found during compression", context->compcode); |
| 1270 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 1271 |
urcodecsuccess: |
| 1272 |
; |
| 1273 |
} else { |
| 1274 |
blosc2_compcode_to_compname(context->compcode, &compname); |
| 1275 |
BLOSC_TRACE_ERROR("Blosc has not been compiled with '%s' compression support." |
| 1276 |
"Please use one having it.", compname); |
| 1277 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 1278 |
} |
| 1279 |
|
| 1280 |
if (cbytes > maxout) { |
| 1281 |
/* Buffer overrun caused by compression (should never happen) */ |
| 1282 |
return BLOSC2_ERROR_WRITE_BUFFER; |
| 1283 |
} |
| 1284 |
if (cbytes < 0) { |
| 1285 |
/* cbytes should never be negative */ |
| 1286 |
return BLOSC2_ERROR_DATA; |
| 1287 |
} |
| 1288 |
if (cbytes == 0) { |
| 1289 |
// When cbytes is 0, the compressor has not been able to compress anything |
| 1290 |
cbytes = neblock; |
| 1291 |
} |
| 1292 |
|
| 1293 |
if (instr_codec) { |
| 1294 |
blosc_set_timestamp(¤t); |
| 1295 |
int32_t instr_size = sizeof(blosc2_instr); |
| 1296 |
ntbytes += instr_size; |
| 1297 |
ctbytes += instr_size; |
| 1298 |
if (ntbytes > destsize) { |
| 1299 |
return 0; /* Non-compressible data */ |
| 1300 |
} |
| 1301 |
_sw32(dest - 4, instr_size); |
| 1302 |
float ctime = (float)blosc_elapsed_secs(last, current); |
| 1303 |
blosc2_instr *desti = (blosc2_instr *)dest; |
| 1304 |
memset(desti, 0, sizeof(blosc2_instr)); |
| 1305 |
// cratio is computed having into account 1 additional int (csize) |
| 1306 |
desti->cratio = (float)neblock / (float)(cbytes + sizeof(int32_t)); |
| 1307 |
desti->cspeed = (float)neblock / ctime; |
| 1308 |
desti->filter_speed = (float) neblock / filter_time; |
| 1309 |
dest += instr_size; |
| 1310 |
continue; |
| 1311 |
} |
| 1312 |
|
| 1313 |
if (!dict_training) { |
| 1314 |
if (cbytes == neblock) { |
| 1315 |
/* The compressor has been unable to compress data at all. */ |
| 1316 |
/* Before doing the copy, check that we are not running into a |
| 1317 |
buffer overflow. */ |
| 1318 |
if ((ntbytes + neblock) > destsize) { |
| 1319 |
return 0; /* Non-compressible data */ |
| 1320 |
} |
| 1321 |
memcpy(dest, _src + j * neblock, (unsigned int)neblock); |
| 1322 |
cbytes = neblock; |
| 1323 |
} |
| 1324 |
_sw32(dest - 4, cbytes); |
| 1325 |
} |
| 1326 |
dest += cbytes; |
| 1327 |
ntbytes += cbytes; |
| 1328 |
ctbytes += cbytes; |
| 1329 |
} /* Closes j < nstreams */ |
| 1330 |
|
| 1331 |
return ctbytes; |
| 1332 |
} |
| 1333 |
|
| 1334 |
|
| 1335 |
/* Process the filter pipeline (decompression mode) */ |
| 1336 |
int pipeline_backward(struct thread_context* thread_context, const int32_t bsize, uint8_t* dest, |
| 1337 |
const int32_t offset, uint8_t* src, uint8_t* tmp, |
| 1338 |
uint8_t* tmp2, int last_filter_index, int32_t nblock) { |
| 1339 |
blosc2_context* context = thread_context->parent_context; |
| 1340 |
int32_t typesize = context->typesize; |
| 1341 |
uint8_t* filters = context->filters; |
| 1342 |
uint8_t* filters_meta = context->filters_meta; |
| 1343 |
uint8_t* _src = src; |
| 1344 |
uint8_t* _dest = tmp; |
| 1345 |
uint8_t* _tmp = tmp2; |
| 1346 |
int errcode = 0; |
| 1347 |
|
| 1348 |
for (int i = BLOSC2_MAX_FILTERS - 1; i >= 0; i--) { |
| 1349 |
// Delta filter requires the whole chunk ready |
| 1350 |
int last_copy_filter = (last_filter_index == i) || (next_filter(filters, i, 'd') == BLOSC_DELTA); |
| 1351 |
if (last_copy_filter && context->postfilter == NULL) { |
| 1352 |
_dest = dest + offset; |
| 1353 |
} |
| 1354 |
int rc = BLOSC2_ERROR_SUCCESS; |
| 1355 |
if (filters[i] <= BLOSC2_DEFINED_FILTERS_STOP) { |
| 1356 |
switch (filters[i]) { |
| 1357 |
case BLOSC_SHUFFLE: |
| 1358 |
unshuffle(typesize, bsize, _src, _dest); |
| 1359 |
break; |
| 1360 |
case BLOSC_BITSHUFFLE: |
| 1361 |
if (bitunshuffle(typesize, bsize, _src, _dest, context->src[BLOSC2_CHUNK_VERSION]) < 0) { |
| 1362 |
return BLOSC2_ERROR_FILTER_PIPELINE; |
| 1363 |
} |
| 1364 |
break; |
| 1365 |
case BLOSC_DELTA: |
| 1366 |
if (context->nthreads == 1) { |
| 1367 |
/* Serial mode */ |
| 1368 |
delta_decoder(dest, offset, bsize, typesize, _dest); |
| 1369 |
} else { |
| 1370 |
/* Force the thread in charge of the block 0 to go first */ |
| 1371 |
pthread_mutex_lock(&context->delta_mutex); |
| 1372 |
if (context->dref_not_init) { |
| 1373 |
if (offset != 0) { |
| 1374 |
pthread_cond_wait(&context->delta_cv, &context->delta_mutex); |
| 1375 |
} else { |
| 1376 |
delta_decoder(dest, offset, bsize, typesize, _dest); |
| 1377 |
context->dref_not_init = 0; |
| 1378 |
pthread_cond_broadcast(&context->delta_cv); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
pthread_mutex_unlock(&context->delta_mutex); |
| 1382 |
if (offset != 0) { |
| 1383 |
delta_decoder(dest, offset, bsize, typesize, _dest); |
| 1384 |
} |
| 1385 |
} |
| 1386 |
break; |
| 1387 |
case BLOSC_TRUNC_PREC: |
| 1388 |
// TRUNC_PREC filter does not need to be undone |
| 1389 |
break; |
| 1390 |
default: |
| 1391 |
if (filters[i] != BLOSC_NOFILTER) { |
| 1392 |
BLOSC_TRACE_ERROR("Filter %d not handled during decompression.", |
| 1393 |
filters[i]); |
| 1394 |
errcode = -1; |
| 1395 |
} |
| 1396 |
} |
| 1397 |
} else { |
| 1398 |
// Look for the filters_meta in user filters and run it |
| 1399 |
for (uint64_t j = 0; j < g_nfilters; ++j) { |
| 1400 |
if (g_filters[j].id == filters[i]) { |
| 1401 |
if (g_filters[j].backward == NULL) { |
| 1402 |
// Dynamically load filter |
| 1403 |
if (fill_filter(&g_filters[j]) < 0) { |
| 1404 |
BLOSC_TRACE_ERROR("Could not load filter %d.", g_filters[j].id); |
| 1405 |
return BLOSC2_ERROR_FILTER_PIPELINE; |
| 1406 |
} |
| 1407 |
} |
| 1408 |
if (g_filters[j].backward != NULL) { |
| 1409 |
blosc2_dparams dparams; |
| 1410 |
blosc2_ctx_get_dparams(context, &dparams); |
| 1411 |
rc = g_filters[j].backward(_src, _dest, bsize, filters_meta[i], &dparams, g_filters[j].id); |
| 1412 |
} else { |
| 1413 |
BLOSC_TRACE_ERROR("Backward function is NULL"); |
| 1414 |
return BLOSC2_ERROR_FILTER_PIPELINE; |
| 1415 |
} |
| 1416 |
if (rc != BLOSC2_ERROR_SUCCESS) { |
| 1417 |
BLOSC_TRACE_ERROR("User-defined filter %d failed during decompression.", filters[i]); |
| 1418 |
return rc; |
| 1419 |
} |
| 1420 |
goto urfiltersuccess; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
BLOSC_TRACE_ERROR("User-defined filter %d not found during decompression.", filters[i]); |
| 1424 |
return BLOSC2_ERROR_FILTER_PIPELINE; |
| 1425 |
urfiltersuccess:; |
| 1426 |
} |
| 1427 |
|
| 1428 |
// Cycle buffers when required |
| 1429 |
if ((filters[i] != BLOSC_NOFILTER) && (filters[i] != BLOSC_TRUNC_PREC)) { |
| 1430 |
_cycle_buffers(&_src, &_dest, &_tmp); |
| 1431 |
} |
| 1432 |
if (last_filter_index == i) { |
| 1433 |
break; |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
/* Postfilter function */ |
| 1438 |
if (context->postfilter != NULL) { |
| 1439 |
// Create new postfilter parameters for this block (must be private for each thread) |
| 1440 |
blosc2_postfilter_params postparams; |
| 1441 |
memcpy(&postparams, context->postparams, sizeof(postparams)); |
| 1442 |
postparams.input = _src; |
| 1443 |
postparams.output = dest + offset; |
| 1444 |
postparams.size = bsize; |
| 1445 |
postparams.typesize = typesize; |
| 1446 |
postparams.offset = nblock * context->blocksize; |
| 1447 |
postparams.nchunk = context->schunk != NULL ? context->schunk->current_nchunk : -1; |
| 1448 |
postparams.nblock = nblock; |
| 1449 |
postparams.tid = thread_context->tid; |
| 1450 |
postparams.ttmp = thread_context->tmp; |
| 1451 |
postparams.ttmp_nbytes = thread_context->tmp_nbytes; |
| 1452 |
postparams.ctx = context; |
| 1453 |
|
| 1454 |
if (context->postfilter(&postparams) != 0) { |
| 1455 |
BLOSC_TRACE_ERROR("Execution of postfilter function failed"); |
| 1456 |
return BLOSC2_ERROR_POSTFILTER; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
return errcode; |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
static int32_t set_nans(int32_t typesize, uint8_t* dest, int32_t destsize) { |
| 1465 |
if (destsize % typesize != 0) { |
| 1466 |
BLOSC_TRACE_ERROR("destsize can only be a multiple of typesize"); |
| 1467 |
BLOSC_ERROR(BLOSC2_ERROR_FAILURE); |
| 1468 |
} |
| 1469 |
int32_t nitems = destsize / typesize; |
| 1470 |
if (nitems == 0) { |
| 1471 |
return 0; |
| 1472 |
} |
| 1473 |
|
| 1474 |
if (typesize == 4) { |
| 1475 |
float* dest_ = (float*)dest; |
| 1476 |
float val = nanf(""); |
| 1477 |
for (int i = 0; i < nitems; i++) { |
| 1478 |
dest_[i] = val; |
| 1479 |
} |
| 1480 |
return nitems; |
| 1481 |
} |
| 1482 |
else if (typesize == 8) { |
| 1483 |
double* dest_ = (double*)dest; |
| 1484 |
double val = nan(""); |
| 1485 |
for (int i = 0; i < nitems; i++) { |
| 1486 |
dest_[i] = val; |
| 1487 |
} |
| 1488 |
return nitems; |
| 1489 |
} |
| 1490 |
|
| 1491 |
BLOSC_TRACE_ERROR("Unsupported typesize for NaN"); |
| 1492 |
return BLOSC2_ERROR_DATA; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
static int32_t set_values(int32_t typesize, const uint8_t* src, uint8_t* dest, int32_t destsize) { |
| 1497 |
#if defined(BLOSC_STRICT_ALIGN) |
| 1498 |
if (destsize % typesize != 0) { |
| 1499 |
BLOSC_ERROR(BLOSC2_ERROR_FAILURE); |
| 1500 |
} |
| 1501 |
int32_t nitems = destsize / typesize; |
| 1502 |
if (nitems == 0) { |
| 1503 |
return 0; |
| 1504 |
} |
| 1505 |
for (int i = 0; i < nitems; i++) { |
| 1506 |
memcpy(dest + i * typesize, src + BLOSC_EXTENDED_HEADER_LENGTH, typesize); |
| 1507 |
} |
| 1508 |
#else |
| 1509 |
// destsize can only be a multiple of typesize |
| 1510 |
int64_t val8; |
| 1511 |
int64_t* dest8; |
| 1512 |
int32_t val4; |
| 1513 |
int32_t* dest4; |
| 1514 |
int16_t val2; |
| 1515 |
int16_t* dest2; |
| 1516 |
int8_t val1; |
| 1517 |
int8_t* dest1; |
| 1518 |
|
| 1519 |
if (destsize % typesize != 0) { |
| 1520 |
BLOSC_ERROR(BLOSC2_ERROR_FAILURE); |
| 1521 |
} |
| 1522 |
int32_t nitems = destsize / typesize; |
| 1523 |
if (nitems == 0) { |
| 1524 |
return 0; |
| 1525 |
} |
| 1526 |
|
| 1527 |
switch (typesize) { |
| 1528 |
case 8: |
| 1529 |
val8 = ((int64_t*)(src + BLOSC_EXTENDED_HEADER_LENGTH))[0]; |
| 1530 |
dest8 = (int64_t*)dest; |
| 1531 |
for (int i = 0; i < nitems; i++) { |
| 1532 |
dest8[i] = val8; |
| 1533 |
} |
| 1534 |
break; |
| 1535 |
case 4: |
| 1536 |
val4 = ((int32_t*)(src + BLOSC_EXTENDED_HEADER_LENGTH))[0]; |
| 1537 |
dest4 = (int32_t*)dest; |
| 1538 |
for (int i = 0; i < nitems; i++) { |
| 1539 |
dest4[i] = val4; |
| 1540 |
} |
| 1541 |
break; |
| 1542 |
case 2: |
| 1543 |
val2 = ((int16_t*)(src + BLOSC_EXTENDED_HEADER_LENGTH))[0]; |
| 1544 |
dest2 = (int16_t*)dest; |
| 1545 |
for (int i = 0; i < nitems; i++) { |
| 1546 |
dest2[i] = val2; |
| 1547 |
} |
| 1548 |
break; |
| 1549 |
case 1: |
| 1550 |
val1 = ((int8_t*)(src + BLOSC_EXTENDED_HEADER_LENGTH))[0]; |
| 1551 |
dest1 = (int8_t*)dest; |
| 1552 |
for (int i = 0; i < nitems; i++) { |
| 1553 |
dest1[i] = val1; |
| 1554 |
} |
| 1555 |
break; |
| 1556 |
default: |
| 1557 |
for (int i = 0; i < nitems; i++) { |
| 1558 |
memcpy(dest + i * typesize, src + BLOSC_EXTENDED_HEADER_LENGTH, typesize); |
| 1559 |
} |
| 1560 |
} |
| 1561 |
#endif |
| 1562 |
|
| 1563 |
return nitems; |
| 1564 |
} |
| 1565 |
|
| 1566 |
|
| 1567 |
/* Decompress & unshuffle a single block */ |
| 1568 |
static int blosc_d( |
| 1569 |
struct thread_context* thread_context, int32_t bsize, |
| 1570 |
int32_t leftoverblock, bool memcpyed, const uint8_t* src, int32_t srcsize, int32_t src_offset, |
| 1571 |
int32_t nblock, uint8_t* dest, int32_t dest_offset, uint8_t* tmp, uint8_t* tmp2) { |
| 1572 |
blosc2_context* context = thread_context->parent_context; |
| 1573 |
uint8_t* filters = context->filters; |
| 1574 |
uint8_t *tmp3 = thread_context->tmp4; |
| 1575 |
int32_t compformat = (context->header_flags & (uint8_t)0xe0) >> 5u; |
| 1576 |
int dont_split = (context->header_flags & 0x10) >> 4; |
| 1577 |
int32_t chunk_nbytes; |
| 1578 |
int32_t chunk_cbytes; |
| 1579 |
int nstreams; |
| 1580 |
int32_t neblock; |
| 1581 |
int32_t nbytes; /* number of decompressed bytes in split */ |
| 1582 |
int32_t cbytes; /* number of compressed bytes in split */ |
| 1583 |
// int32_t ctbytes = 0; /* number of compressed bytes in block */ |
| 1584 |
int32_t ntbytes = 0; /* number of uncompressed bytes in block */ |
| 1585 |
uint8_t* _dest; |
| 1586 |
int32_t typesize = context->typesize; |
| 1587 |
bool instr_codec = context->blosc2_flags & BLOSC2_INSTR_CODEC; |
| 1588 |
const char* compname; |
| 1589 |
int rc; |
| 1590 |
|
| 1591 |
if (context->block_maskout != NULL && context->block_maskout[nblock]) { |
| 1592 |
// Do not decompress, but act as if we successfully decompressed everything |
| 1593 |
return bsize; |
| 1594 |
} |
| 1595 |
|
| 1596 |
rc = blosc2_cbuffer_sizes(src, &chunk_nbytes, &chunk_cbytes, NULL); |
| 1597 |
if (rc < 0) { |
| 1598 |
return rc; |
| 1599 |
} |
| 1600 |
if (context->special_type == BLOSC2_SPECIAL_VALUE) { |
| 1601 |
// We need the actual typesize in this case, but it cannot be encoded in the header, so derive it from cbytes |
| 1602 |
typesize = chunk_cbytes - context->header_overhead; |
| 1603 |
} |
| 1604 |
|
| 1605 |
// In some situations (lazychunks) the context can arrive uninitialized |
| 1606 |
// (but BITSHUFFLE needs it for accessing the format of the chunk) |
| 1607 |
if (context->src == NULL) { |
| 1608 |
context->src = src; |
| 1609 |
} |
| 1610 |
|
| 1611 |
// Chunks with special values cannot be lazy |
| 1612 |
bool is_lazy = ((context->header_overhead == BLOSC_EXTENDED_HEADER_LENGTH) && |
| 1613 |
(context->blosc2_flags & 0x08u) && !context->special_type); |
| 1614 |
if (is_lazy) { |
| 1615 |
// The chunk is on disk, so just lazily load the block |
| 1616 |
if (context->schunk == NULL) { |
| 1617 |
BLOSC_TRACE_ERROR("Lazy chunk needs an associated super-chunk."); |
| 1618 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 1619 |
} |
| 1620 |
if (context->schunk->frame == NULL) { |
| 1621 |
BLOSC_TRACE_ERROR("Lazy chunk needs an associated frame."); |
| 1622 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 1623 |
} |
| 1624 |
blosc2_frame_s* frame = (blosc2_frame_s*)context->schunk->frame; |
| 1625 |
char* urlpath = frame->urlpath; |
| 1626 |
size_t trailer_offset = BLOSC_EXTENDED_HEADER_LENGTH + context->nblocks * sizeof(int32_t); |
| 1627 |
int32_t nchunk; |
| 1628 |
int64_t chunk_offset; |
| 1629 |
// The nchunk and the offset of the current chunk are in the trailer |
| 1630 |
nchunk = *(int32_t*)(src + trailer_offset); |
| 1631 |
chunk_offset = *(int64_t*)(src + trailer_offset + sizeof(int32_t)); |
| 1632 |
// Get the csize of the nblock |
| 1633 |
int32_t *block_csizes = (int32_t *)(src + trailer_offset + sizeof(int32_t) + sizeof(int64_t)); |
| 1634 |
int32_t block_csize = block_csizes[nblock]; |
| 1635 |
// Read the lazy block on disk |
| 1636 |
void* fp = NULL; |
| 1637 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(context->schunk->storage->io->id); |
| 1638 |
if (io_cb == NULL) { |
| 1639 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 1640 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 1641 |
} |
| 1642 |
|
| 1643 |
int64_t io_pos = 0; |
| 1644 |
if (frame->sframe) { |
| 1645 |
// The chunk is not in the frame |
| 1646 |
char* chunkpath = malloc(strlen(frame->urlpath) + 1 + 8 + strlen(".chunk") + 1); |
| 1647 |
BLOSC_ERROR_NULL(chunkpath, BLOSC2_ERROR_MEMORY_ALLOC); |
| 1648 |
sprintf(chunkpath, "%s/%08X.chunk", frame->urlpath, nchunk); |
| 1649 |
fp = io_cb->open(chunkpath, "rb", context->schunk->storage->io->params); |
| 1650 |
BLOSC_ERROR_NULL(fp, BLOSC2_ERROR_FILE_OPEN); |
| 1651 |
free(chunkpath); |
| 1652 |
// The offset of the block is src_offset |
| 1653 |
io_pos = src_offset; |
| 1654 |
} |
| 1655 |
else { |
| 1656 |
fp = io_cb->open(urlpath, "rb", context->schunk->storage->io->params); |
| 1657 |
BLOSC_ERROR_NULL(fp, BLOSC2_ERROR_FILE_OPEN); |
| 1658 |
// The offset of the block is src_offset |
| 1659 |
io_pos = frame->file_offset + chunk_offset + src_offset; |
| 1660 |
} |
| 1661 |
// We can make use of tmp3 because it will be used after src is not needed anymore |
| 1662 |
int64_t rbytes = io_cb->read((void**)&tmp3, 1, block_csize, io_pos, fp); |
| 1663 |
io_cb->close(fp); |
| 1664 |
if ((int32_t)rbytes != block_csize) { |
| 1665 |
BLOSC_TRACE_ERROR("Cannot read the (lazy) block out of the fileframe."); |
| 1666 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1667 |
} |
| 1668 |
src = tmp3; |
| 1669 |
src_offset = 0; |
| 1670 |
srcsize = block_csize; |
| 1671 |
} |
| 1672 |
|
| 1673 |
// If the chunk is memcpyed, we just have to copy the block to dest and return |
| 1674 |
if (memcpyed) { |
| 1675 |
int bsize_ = leftoverblock ? chunk_nbytes % context->blocksize : bsize; |
| 1676 |
if (!context->special_type) { |
| 1677 |
if (chunk_nbytes + context->header_overhead != chunk_cbytes) { |
| 1678 |
return BLOSC2_ERROR_WRITE_BUFFER; |
| 1679 |
} |
| 1680 |
if (chunk_cbytes < context->header_overhead + (nblock * context->blocksize) + bsize_) { |
| 1681 |
/* Not enough input to copy block */ |
| 1682 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1683 |
} |
| 1684 |
} |
| 1685 |
if (!is_lazy) { |
| 1686 |
src += context->header_overhead + nblock * context->blocksize; |
| 1687 |
} |
| 1688 |
_dest = dest + dest_offset; |
| 1689 |
if (context->postfilter != NULL) { |
| 1690 |
// We are making use of a postfilter, so use a temp for destination |
| 1691 |
_dest = tmp; |
| 1692 |
} |
| 1693 |
rc = 0; |
| 1694 |
switch (context->special_type) { |
| 1695 |
case BLOSC2_SPECIAL_VALUE: |
| 1696 |
// All repeated values |
| 1697 |
rc = set_values(typesize, context->src, _dest, bsize_); |
| 1698 |
if (rc < 0) { |
| 1699 |
BLOSC_TRACE_ERROR("set_values failed"); |
| 1700 |
return BLOSC2_ERROR_DATA; |
| 1701 |
} |
| 1702 |
break; |
| 1703 |
case BLOSC2_SPECIAL_NAN: |
| 1704 |
rc = set_nans(context->typesize, _dest, bsize_); |
| 1705 |
if (rc < 0) { |
| 1706 |
BLOSC_TRACE_ERROR("set_nans failed"); |
| 1707 |
return BLOSC2_ERROR_DATA; |
| 1708 |
} |
| 1709 |
break; |
| 1710 |
case BLOSC2_SPECIAL_ZERO: |
| 1711 |
memset(_dest, 0, bsize_); |
| 1712 |
break; |
| 1713 |
case BLOSC2_SPECIAL_UNINIT: |
| 1714 |
// We do nothing here |
| 1715 |
break; |
| 1716 |
default: |
| 1717 |
memcpy(_dest, src, bsize_); |
| 1718 |
} |
| 1719 |
if (context->postfilter != NULL) { |
| 1720 |
// Create new postfilter parameters for this block (must be private for each thread) |
| 1721 |
blosc2_postfilter_params postparams; |
| 1722 |
memcpy(&postparams, context->postparams, sizeof(postparams)); |
| 1723 |
postparams.input = tmp; |
| 1724 |
postparams.output = dest + dest_offset; |
| 1725 |
postparams.size = bsize; |
| 1726 |
postparams.typesize = typesize; |
| 1727 |
postparams.offset = nblock * context->blocksize; |
| 1728 |
postparams.nchunk = context->schunk != NULL ? context->schunk->current_nchunk : -1; |
| 1729 |
postparams.nblock = nblock; |
| 1730 |
postparams.tid = thread_context->tid; |
| 1731 |
postparams.ttmp = thread_context->tmp; |
| 1732 |
postparams.ttmp_nbytes = thread_context->tmp_nbytes; |
| 1733 |
postparams.ctx = context; |
| 1734 |
|
| 1735 |
// Execute the postfilter (the processed block will be copied to dest) |
| 1736 |
if (context->postfilter(&postparams) != 0) { |
| 1737 |
BLOSC_TRACE_ERROR("Execution of postfilter function failed"); |
| 1738 |
return BLOSC2_ERROR_POSTFILTER; |
| 1739 |
} |
| 1740 |
} |
| 1741 |
thread_context->zfp_cell_nitems = 0; |
| 1742 |
|
| 1743 |
return bsize_; |
| 1744 |
} |
| 1745 |
|
| 1746 |
if (!is_lazy && (src_offset <= 0 || src_offset >= srcsize)) { |
| 1747 |
/* Invalid block src offset encountered */ |
| 1748 |
return BLOSC2_ERROR_DATA; |
| 1749 |
} |
| 1750 |
|
| 1751 |
src += src_offset; |
| 1752 |
srcsize -= src_offset; |
| 1753 |
|
| 1754 |
int last_filter_index = last_filter(filters, 'd'); |
| 1755 |
if (instr_codec) { |
| 1756 |
// If instrumented, we don't want to run the filters |
| 1757 |
_dest = dest + dest_offset; |
| 1758 |
} |
| 1759 |
else if (((last_filter_index >= 0) && |
| 1760 |
(next_filter(filters, BLOSC2_MAX_FILTERS, 'd') != BLOSC_DELTA)) || |
| 1761 |
context->postfilter != NULL) { |
| 1762 |
// We are making use of some filter, so use a temp for destination |
| 1763 |
_dest = tmp; |
| 1764 |
} |
| 1765 |
else { |
| 1766 |
// If no filters, or only DELTA in pipeline |
| 1767 |
_dest = dest + dest_offset; |
| 1768 |
} |
| 1769 |
|
| 1770 |
/* The number of compressed data streams for this block */ |
| 1771 |
if (!dont_split && !leftoverblock) { |
| 1772 |
nstreams = context->typesize; |
| 1773 |
} |
| 1774 |
else { |
| 1775 |
nstreams = 1; |
| 1776 |
} |
| 1777 |
|
| 1778 |
neblock = bsize / nstreams; |
| 1779 |
if (neblock == 0) { |
| 1780 |
/* Not enough space to output bytes */ |
| 1781 |
BLOSC_ERROR(BLOSC2_ERROR_WRITE_BUFFER); |
| 1782 |
} |
| 1783 |
for (int j = 0; j < nstreams; j++) { |
| 1784 |
if (srcsize < (signed)sizeof(int32_t)) { |
| 1785 |
/* Not enough input to read compressed size */ |
| 1786 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1787 |
} |
| 1788 |
srcsize -= sizeof(int32_t); |
| 1789 |
cbytes = sw32_(src); /* amount of compressed bytes */ |
| 1790 |
if (cbytes > 0) { |
| 1791 |
if (srcsize < cbytes) { |
| 1792 |
/* Not enough input to read compressed bytes */ |
| 1793 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1794 |
} |
| 1795 |
srcsize -= cbytes; |
| 1796 |
} |
| 1797 |
src += sizeof(int32_t); |
| 1798 |
// ctbytes += (signed)sizeof(int32_t); |
| 1799 |
|
| 1800 |
/* Uncompress */ |
| 1801 |
if (cbytes == 0) { |
| 1802 |
// A run of 0's |
| 1803 |
memset(_dest, 0, (unsigned int)neblock); |
| 1804 |
nbytes = neblock; |
| 1805 |
} |
| 1806 |
else if (cbytes < 0) { |
| 1807 |
// A negative number means some encoding depending on the token that comes next |
| 1808 |
uint8_t token; |
| 1809 |
|
| 1810 |
if (srcsize < (signed)sizeof(uint8_t)) { |
| 1811 |
// Not enough input to read token */ |
| 1812 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1813 |
} |
| 1814 |
srcsize -= sizeof(uint8_t); |
| 1815 |
|
| 1816 |
token = src[0]; |
| 1817 |
src += 1; |
| 1818 |
// ctbytes += 1; |
| 1819 |
|
| 1820 |
if (token & 0x1) { |
| 1821 |
// A run of bytes that are different than 0 |
| 1822 |
if (cbytes < -255) { |
| 1823 |
// Runs can only encode a byte |
| 1824 |
return BLOSC2_ERROR_RUN_LENGTH; |
| 1825 |
} |
| 1826 |
uint8_t value = -cbytes; |
| 1827 |
memset(_dest, value, (unsigned int)neblock); |
| 1828 |
} else { |
| 1829 |
BLOSC_TRACE_ERROR("Invalid or unsupported compressed stream token value - %d", token); |
| 1830 |
return BLOSC2_ERROR_RUN_LENGTH; |
| 1831 |
} |
| 1832 |
nbytes = neblock; |
| 1833 |
cbytes = 0; // everything is encoded in the cbytes token |
| 1834 |
} |
| 1835 |
else if (cbytes == neblock) { |
| 1836 |
memcpy(_dest, src, (unsigned int)neblock); |
| 1837 |
nbytes = (int32_t)neblock; |
| 1838 |
} |
| 1839 |
else { |
| 1840 |
if (compformat == BLOSC_BLOSCLZ_FORMAT) { |
| 1841 |
nbytes = blosclz_decompress(src, cbytes, _dest, (int)neblock); |
| 1842 |
} |
| 1843 |
else if (compformat == BLOSC_LZ4_FORMAT) { |
| 1844 |
nbytes = lz4_wrap_decompress((char*)src, (size_t)cbytes, |
| 1845 |
(char*)_dest, (size_t)neblock); |
| 1846 |
} |
| 1847 |
#if defined(HAVE_ZLIB) |
| 1848 |
else if (compformat == BLOSC_ZLIB_FORMAT) { |
| 1849 |
nbytes = zlib_wrap_decompress((char*)src, (size_t)cbytes, |
| 1850 |
(char*)_dest, (size_t)neblock); |
| 1851 |
} |
| 1852 |
#endif /* HAVE_ZLIB */ |
| 1853 |
#if defined(HAVE_ZSTD) |
| 1854 |
else if (compformat == BLOSC_ZSTD_FORMAT) { |
| 1855 |
nbytes = zstd_wrap_decompress(thread_context, |
| 1856 |
(char*)src, (size_t)cbytes, |
| 1857 |
(char*)_dest, (size_t)neblock); |
| 1858 |
} |
| 1859 |
#endif /* HAVE_ZSTD */ |
| 1860 |
else if (compformat == BLOSC_UDCODEC_FORMAT) { |
| 1861 |
bool getcell = false; |
| 1862 |
|
| 1863 |
#if defined(HAVE_PLUGINS) |
| 1864 |
if ((context->compcode == BLOSC_CODEC_ZFP_FIXED_RATE) && |
| 1865 |
(thread_context->zfp_cell_nitems > 0)) { |
| 1866 |
nbytes = zfp_getcell(thread_context, src, cbytes, _dest, neblock); |
| 1867 |
if (nbytes < 0) { |
| 1868 |
return BLOSC2_ERROR_DATA; |
| 1869 |
} |
| 1870 |
if (nbytes == thread_context->zfp_cell_nitems * typesize) { |
| 1871 |
getcell = true; |
| 1872 |
} |
| 1873 |
} |
| 1874 |
#endif /* HAVE_PLUGINS */ |
| 1875 |
if (!getcell) { |
| 1876 |
thread_context->zfp_cell_nitems = 0; |
| 1877 |
for (int i = 0; i < g_ncodecs; ++i) { |
| 1878 |
if (g_codecs[i].compcode == context->compcode) { |
| 1879 |
if (g_codecs[i].decoder == NULL) { |
| 1880 |
// Dynamically load codec plugin |
| 1881 |
if (fill_codec(&g_codecs[i]) < 0) { |
| 1882 |
BLOSC_TRACE_ERROR("Could not load codec %d.", g_codecs[i].compcode); |
| 1883 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 1884 |
} |
| 1885 |
} |
| 1886 |
blosc2_dparams dparams; |
| 1887 |
blosc2_ctx_get_dparams(context, &dparams); |
| 1888 |
nbytes = g_codecs[i].decoder(src, |
| 1889 |
cbytes, |
| 1890 |
_dest, |
| 1891 |
neblock, |
| 1892 |
context->compcode_meta, |
| 1893 |
&dparams, |
| 1894 |
context->src); |
| 1895 |
goto urcodecsuccess; |
| 1896 |
} |
| 1897 |
} |
| 1898 |
BLOSC_TRACE_ERROR("User-defined compressor codec %d not found during decompression", context->compcode); |
| 1899 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 1900 |
} |
| 1901 |
urcodecsuccess: |
| 1902 |
; |
| 1903 |
} |
| 1904 |
else { |
| 1905 |
compname = clibcode_to_clibname(compformat); |
| 1906 |
BLOSC_TRACE_ERROR( |
| 1907 |
"Blosc has not been compiled with decompression " |
| 1908 |
"support for '%s' format. " |
| 1909 |
"Please recompile for adding this support.", compname); |
| 1910 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 1911 |
} |
| 1912 |
|
| 1913 |
/* Check that decompressed bytes number is correct */ |
| 1914 |
if ((nbytes != neblock) && (thread_context->zfp_cell_nitems == 0)) { |
| 1915 |
return BLOSC2_ERROR_DATA; |
| 1916 |
} |
| 1917 |
|
| 1918 |
} |
| 1919 |
src += cbytes; |
| 1920 |
// ctbytes += cbytes; |
| 1921 |
_dest += nbytes; |
| 1922 |
ntbytes += nbytes; |
| 1923 |
} /* Closes j < nstreams */ |
| 1924 |
|
| 1925 |
if (!instr_codec) { |
| 1926 |
if (last_filter_index >= 0 || context->postfilter != NULL) { |
| 1927 |
/* Apply regular filter pipeline */ |
| 1928 |
int errcode = pipeline_backward(thread_context, bsize, dest, dest_offset, tmp, tmp2, tmp3, |
| 1929 |
last_filter_index, nblock); |
| 1930 |
if (errcode < 0) |
| 1931 |
return errcode; |
| 1932 |
} |
| 1933 |
} |
| 1934 |
|
| 1935 |
/* Return the number of uncompressed bytes */ |
| 1936 |
return (int)ntbytes; |
| 1937 |
} |
| 1938 |
|
| 1939 |
|
| 1940 |
/* Serial version for compression/decompression */ |
| 1941 |
static int serial_blosc(struct thread_context* thread_context) { |
| 1942 |
blosc2_context* context = thread_context->parent_context; |
| 1943 |
int32_t j, bsize, leftoverblock; |
| 1944 |
int32_t cbytes; |
| 1945 |
int32_t ntbytes = context->output_bytes; |
| 1946 |
int32_t* bstarts = context->bstarts; |
| 1947 |
uint8_t* tmp = thread_context->tmp; |
| 1948 |
uint8_t* tmp2 = thread_context->tmp2; |
| 1949 |
int dict_training = context->use_dict && (context->dict_cdict == NULL); |
| 1950 |
bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
| 1951 |
if (!context->do_compress && context->special_type) { |
| 1952 |
// Fake a runlen as if it was a memcpyed chunk |
| 1953 |
memcpyed = true; |
| 1954 |
} |
| 1955 |
|
| 1956 |
for (j = 0; j < context->nblocks; j++) { |
| 1957 |
if (context->do_compress && !memcpyed && !dict_training) { |
| 1958 |
_sw32(bstarts + j, ntbytes); |
| 1959 |
} |
| 1960 |
bsize = context->blocksize; |
| 1961 |
leftoverblock = 0; |
| 1962 |
if ((j == context->nblocks - 1) && (context->leftover > 0)) { |
| 1963 |
bsize = context->leftover; |
| 1964 |
leftoverblock = 1; |
| 1965 |
} |
| 1966 |
if (context->do_compress) { |
| 1967 |
if (memcpyed && !context->prefilter) { |
| 1968 |
/* We want to memcpy only */ |
| 1969 |
memcpy(context->dest + context->header_overhead + j * context->blocksize, |
| 1970 |
context->src + j * context->blocksize, (unsigned int)bsize); |
| 1971 |
cbytes = (int32_t)bsize; |
| 1972 |
} |
| 1973 |
else { |
| 1974 |
/* Regular compression */ |
| 1975 |
cbytes = blosc_c(thread_context, bsize, leftoverblock, ntbytes, |
| 1976 |
context->destsize, context->src, j * context->blocksize, |
| 1977 |
context->dest + ntbytes, tmp, tmp2); |
| 1978 |
if (cbytes == 0) { |
| 1979 |
ntbytes = 0; /* incompressible data */ |
| 1980 |
break; |
| 1981 |
} |
| 1982 |
} |
| 1983 |
} |
| 1984 |
else { |
| 1985 |
/* Regular decompression */ |
| 1986 |
// If memcpyed we don't have a bstarts section (because it is not needed) |
| 1987 |
int32_t src_offset = memcpyed ? |
| 1988 |
context->header_overhead + j * context->blocksize : sw32_(bstarts + j); |
| 1989 |
cbytes = blosc_d(thread_context, bsize, leftoverblock, memcpyed, |
| 1990 |
context->src, context->srcsize, src_offset, j, |
| 1991 |
context->dest, j * context->blocksize, tmp, tmp2); |
| 1992 |
} |
| 1993 |
|
| 1994 |
if (cbytes < 0) { |
| 1995 |
ntbytes = cbytes; /* error in blosc_c or blosc_d */ |
| 1996 |
break; |
| 1997 |
} |
| 1998 |
ntbytes += cbytes; |
| 1999 |
} |
| 2000 |
|
| 2001 |
return ntbytes; |
| 2002 |
} |
| 2003 |
|
| 2004 |
static void t_blosc_do_job(void *ctxt); |
| 2005 |
|
| 2006 |
/* Threaded version for compression/decompression */ |
| 2007 |
static int parallel_blosc(blosc2_context* context) { |
| 2008 |
#ifdef BLOSC_POSIX_BARRIERS |
| 2009 |
int rc; |
| 2010 |
#endif |
| 2011 |
/* Set sentinels */ |
| 2012 |
context->thread_giveup_code = 1; |
| 2013 |
context->thread_nblock = -1; |
| 2014 |
|
| 2015 |
if (threads_callback) { |
| 2016 |
threads_callback(threads_callback_data, t_blosc_do_job, |
| 2017 |
context->nthreads, sizeof(struct thread_context), (void*) context->thread_contexts); |
| 2018 |
} |
| 2019 |
else { |
| 2020 |
/* Synchronization point for all threads (wait for initialization) */ |
| 2021 |
WAIT_INIT(-1, context); |
| 2022 |
|
| 2023 |
/* Synchronization point for all threads (wait for finalization) */ |
| 2024 |
WAIT_FINISH(-1, context); |
| 2025 |
} |
| 2026 |
|
| 2027 |
if (context->thread_giveup_code <= 0) { |
| 2028 |
/* Compression/decompression gave up. Return error code. */ |
| 2029 |
return context->thread_giveup_code; |
| 2030 |
} |
| 2031 |
|
| 2032 |
/* Return the total bytes (de-)compressed in threads */ |
| 2033 |
return (int)context->output_bytes; |
| 2034 |
} |
| 2035 |
|
| 2036 |
/* initialize a thread_context that has already been allocated */ |
| 2037 |
static int init_thread_context(struct thread_context* thread_context, blosc2_context* context, int32_t tid) |
| 2038 |
{ |
| 2039 |
int32_t ebsize; |
| 2040 |
|
| 2041 |
thread_context->parent_context = context; |
| 2042 |
thread_context->tid = tid; |
| 2043 |
|
| 2044 |
ebsize = context->blocksize + context->typesize * (signed)sizeof(int32_t); |
| 2045 |
thread_context->tmp_nbytes = (size_t)4 * ebsize; |
| 2046 |
thread_context->tmp = my_malloc(thread_context->tmp_nbytes); |
| 2047 |
BLOSC_ERROR_NULL(thread_context->tmp, BLOSC2_ERROR_MEMORY_ALLOC); |
| 2048 |
thread_context->tmp2 = thread_context->tmp + ebsize; |
| 2049 |
thread_context->tmp3 = thread_context->tmp2 + ebsize; |
| 2050 |
thread_context->tmp4 = thread_context->tmp3 + ebsize; |
| 2051 |
thread_context->tmp_blocksize = context->blocksize; |
| 2052 |
thread_context->zfp_cell_nitems = 0; |
| 2053 |
thread_context->zfp_cell_start = 0; |
| 2054 |
#if defined(HAVE_ZSTD) |
| 2055 |
thread_context->zstd_cctx = NULL; |
| 2056 |
thread_context->zstd_dctx = NULL; |
| 2057 |
#endif |
| 2058 |
|
| 2059 |
/* Create the hash table for LZ4 in case we are using IPP */ |
| 2060 |
#ifdef HAVE_IPP |
| 2061 |
IppStatus status; |
| 2062 |
int inlen = thread_context->tmp_blocksize > 0 ? thread_context->tmp_blocksize : 1 << 16; |
| 2063 |
int hash_size = 0; |
| 2064 |
status = ippsEncodeLZ4HashTableGetSize_8u(&hash_size); |
| 2065 |
if (status != ippStsNoErr) { |
| 2066 |
BLOSC_TRACE_ERROR("Error in ippsEncodeLZ4HashTableGetSize_8u."); |
| 2067 |
} |
| 2068 |
Ipp8u *hash_table = ippsMalloc_8u(hash_size); |
| 2069 |
status = ippsEncodeLZ4HashTableInit_8u(hash_table, inlen); |
| 2070 |
if (status != ippStsNoErr) { |
| 2071 |
BLOSC_TRACE_ERROR("Error in ippsEncodeLZ4HashTableInit_8u."); |
| 2072 |
} |
| 2073 |
thread_context->lz4_hash_table = hash_table; |
| 2074 |
#endif |
| 2075 |
return 0; |
| 2076 |
} |
| 2077 |
|
| 2078 |
static struct thread_context* |
| 2079 |
create_thread_context(blosc2_context* context, int32_t tid) { |
| 2080 |
struct thread_context* thread_context; |
| 2081 |
thread_context = (struct thread_context*)my_malloc(sizeof(struct thread_context)); |
| 2082 |
BLOSC_ERROR_NULL(thread_context, NULL); |
| 2083 |
int rc = init_thread_context(thread_context, context, tid); |
| 2084 |
if (rc < 0) { |
| 2085 |
return NULL; |
| 2086 |
} |
| 2087 |
return thread_context; |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* free members of thread_context, but not thread_context itself */ |
| 2091 |
static void destroy_thread_context(struct thread_context* thread_context) { |
| 2092 |
my_free(thread_context->tmp); |
| 2093 |
#if defined(HAVE_ZSTD) |
| 2094 |
if (thread_context->zstd_cctx != NULL) { |
| 2095 |
ZSTD_freeCCtx(thread_context->zstd_cctx); |
| 2096 |
} |
| 2097 |
if (thread_context->zstd_dctx != NULL) { |
| 2098 |
ZSTD_freeDCtx(thread_context->zstd_dctx); |
| 2099 |
} |
| 2100 |
#endif |
| 2101 |
#ifdef HAVE_IPP |
| 2102 |
if (thread_context->lz4_hash_table != NULL) { |
| 2103 |
ippsFree(thread_context->lz4_hash_table); |
| 2104 |
} |
| 2105 |
#endif |
| 2106 |
} |
| 2107 |
|
| 2108 |
void free_thread_context(struct thread_context* thread_context) { |
| 2109 |
destroy_thread_context(thread_context); |
| 2110 |
my_free(thread_context); |
| 2111 |
} |
| 2112 |
|
| 2113 |
|
| 2114 |
int check_nthreads(blosc2_context* context) { |
| 2115 |
if (context->nthreads <= 0) { |
| 2116 |
BLOSC_TRACE_ERROR("nthreads must be >= 1 and <= %d", INT16_MAX); |
| 2117 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2118 |
} |
| 2119 |
|
| 2120 |
if (context->new_nthreads != context->nthreads) { |
| 2121 |
if (context->nthreads > 1) { |
| 2122 |
release_threadpool(context); |
| 2123 |
} |
| 2124 |
context->nthreads = context->new_nthreads; |
| 2125 |
} |
| 2126 |
if (context->new_nthreads > 1 && context->threads_started == 0) { |
| 2127 |
init_threadpool(context); |
| 2128 |
} |
| 2129 |
|
| 2130 |
return context->nthreads; |
| 2131 |
} |
| 2132 |
|
| 2133 |
/* Do the compression or decompression of the buffer depending on the |
| 2134 |
global params. */ |
| 2135 |
static int do_job(blosc2_context* context) { |
| 2136 |
int32_t ntbytes; |
| 2137 |
|
| 2138 |
/* Set sentinels */ |
| 2139 |
context->dref_not_init = 1; |
| 2140 |
|
| 2141 |
/* Check whether we need to restart threads */ |
| 2142 |
check_nthreads(context); |
| 2143 |
|
| 2144 |
/* Run the serial version when nthreads is 1 or when the buffers are |
| 2145 |
not larger than blocksize */ |
| 2146 |
if (context->nthreads == 1 || (context->sourcesize / context->blocksize) <= 1) { |
| 2147 |
/* The context for this 'thread' has no been initialized yet */ |
| 2148 |
if (context->serial_context == NULL) { |
| 2149 |
context->serial_context = create_thread_context(context, 0); |
| 2150 |
} |
| 2151 |
else if (context->blocksize != context->serial_context->tmp_blocksize) { |
| 2152 |
free_thread_context(context->serial_context); |
| 2153 |
context->serial_context = create_thread_context(context, 0); |
| 2154 |
} |
| 2155 |
BLOSC_ERROR_NULL(context->serial_context, BLOSC2_ERROR_THREAD_CREATE); |
| 2156 |
ntbytes = serial_blosc(context->serial_context); |
| 2157 |
} |
| 2158 |
else { |
| 2159 |
ntbytes = parallel_blosc(context); |
| 2160 |
} |
| 2161 |
|
| 2162 |
return ntbytes; |
| 2163 |
} |
| 2164 |
|
| 2165 |
|
| 2166 |
static int initialize_context_compression( |
| 2167 |
blosc2_context* context, const void* src, int32_t srcsize, void* dest, |
| 2168 |
int32_t destsize, int clevel, uint8_t const *filters, |
| 2169 |
uint8_t const *filters_meta, int32_t typesize, int compressor, |
| 2170 |
int32_t blocksize, int16_t new_nthreads, int16_t nthreads, |
| 2171 |
int32_t splitmode, |
| 2172 |
int tuner_id, void *tuner_params, |
| 2173 |
blosc2_schunk* schunk) { |
| 2174 |
|
| 2175 |
/* Set parameters */ |
| 2176 |
context->do_compress = 1; |
| 2177 |
context->src = (const uint8_t*)src; |
| 2178 |
context->srcsize = srcsize; |
| 2179 |
context->dest = (uint8_t*)dest; |
| 2180 |
context->output_bytes = 0; |
| 2181 |
context->destsize = destsize; |
| 2182 |
context->sourcesize = srcsize; |
| 2183 |
context->typesize = typesize; |
| 2184 |
context->filter_flags = filters_to_flags(filters); |
| 2185 |
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
| 2186 |
context->filters[i] = filters[i]; |
| 2187 |
context->filters_meta[i] = filters_meta[i]; |
| 2188 |
} |
| 2189 |
context->compcode = compressor; |
| 2190 |
context->nthreads = nthreads; |
| 2191 |
context->new_nthreads = new_nthreads; |
| 2192 |
context->end_threads = 0; |
| 2193 |
context->clevel = clevel; |
| 2194 |
context->schunk = schunk; |
| 2195 |
context->tuner_params = tuner_params; |
| 2196 |
context->tuner_id = tuner_id; |
| 2197 |
context->splitmode = splitmode; |
| 2198 |
/* tuner some compression parameters */ |
| 2199 |
context->blocksize = (int32_t)blocksize; |
| 2200 |
int rc = 0; |
| 2201 |
if (context->tuner_params != NULL) { |
| 2202 |
if (context->tuner_id < BLOSC_LAST_TUNER && context->tuner_id == BLOSC_STUNE) { |
| 2203 |
if (blosc_stune_next_cparams(context) < 0) { |
| 2204 |
BLOSC_TRACE_ERROR("Error in stune next_cparams func\n"); |
| 2205 |
return BLOSC2_ERROR_TUNER; |
| 2206 |
} |
| 2207 |
} else { |
| 2208 |
for (int i = 0; i < g_ntuners; ++i) { |
| 2209 |
if (g_tuners[i].id == context->tuner_id) { |
| 2210 |
if (g_tuners[i].next_cparams == NULL) { |
| 2211 |
if (fill_tuner(&g_tuners[i]) < 0) { |
| 2212 |
BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
| 2213 |
return BLOSC2_ERROR_FAILURE; |
| 2214 |
} |
| 2215 |
} |
| 2216 |
if (g_tuners[i].next_cparams(context) < 0) { |
| 2217 |
BLOSC_TRACE_ERROR("Error in tuner %d next_cparams func\n", context->tuner_id); |
| 2218 |
return BLOSC2_ERROR_TUNER; |
| 2219 |
} |
| 2220 |
if (g_tuners[i].id == BLOSC_BTUNE && context->blocksize == 0) { |
| 2221 |
// Call stune for initializing blocksize |
| 2222 |
if (blosc_stune_next_blocksize(context) < 0) { |
| 2223 |
BLOSC_TRACE_ERROR("Error in stune next_blocksize func\n"); |
| 2224 |
return BLOSC2_ERROR_TUNER; |
| 2225 |
} |
| 2226 |
} |
| 2227 |
goto urtunersuccess; |
| 2228 |
} |
| 2229 |
} |
| 2230 |
BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", context->tuner_id); |
| 2231 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2232 |
} |
| 2233 |
} else { |
| 2234 |
if (context->tuner_id < BLOSC_LAST_TUNER && context->tuner_id == BLOSC_STUNE) { |
| 2235 |
rc = blosc_stune_next_blocksize(context); |
| 2236 |
} else { |
| 2237 |
for (int i = 0; i < g_ntuners; ++i) { |
| 2238 |
if (g_tuners[i].id == context->tuner_id) { |
| 2239 |
if (g_tuners[i].next_blocksize == NULL) { |
| 2240 |
if (fill_tuner(&g_tuners[i]) < 0) { |
| 2241 |
BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
| 2242 |
return BLOSC2_ERROR_FAILURE; |
| 2243 |
} |
| 2244 |
} |
| 2245 |
rc = g_tuners[i].next_blocksize(context); |
| 2246 |
goto urtunersuccess; |
| 2247 |
} |
| 2248 |
} |
| 2249 |
BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", context->tuner_id); |
| 2250 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2251 |
} |
| 2252 |
} |
| 2253 |
urtunersuccess:; |
| 2254 |
if (rc < 0) { |
| 2255 |
BLOSC_TRACE_ERROR("Error in tuner next_blocksize func\n"); |
| 2256 |
return BLOSC2_ERROR_TUNER; |
| 2257 |
} |
| 2258 |
|
| 2259 |
|
| 2260 |
/* Check buffer size limits */ |
| 2261 |
if (srcsize > BLOSC2_MAX_BUFFERSIZE) { |
| 2262 |
BLOSC_TRACE_ERROR("Input buffer size cannot exceed %d bytes.", |
| 2263 |
BLOSC2_MAX_BUFFERSIZE); |
| 2264 |
return BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED; |
| 2265 |
} |
| 2266 |
|
| 2267 |
if (destsize < BLOSC2_MAX_OVERHEAD) { |
| 2268 |
BLOSC_TRACE_ERROR("Output buffer size should be larger than %d bytes.", |
| 2269 |
BLOSC2_MAX_OVERHEAD); |
| 2270 |
return BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED; |
| 2271 |
} |
| 2272 |
|
| 2273 |
/* Compression level */ |
| 2274 |
if (clevel < 0 || clevel > 9) { |
| 2275 |
/* If clevel not in 0..9, print an error */ |
| 2276 |
BLOSC_TRACE_ERROR("`clevel` parameter must be between 0 and 9!."); |
| 2277 |
return BLOSC2_ERROR_CODEC_PARAM; |
| 2278 |
} |
| 2279 |
|
| 2280 |
/* Check typesize limits */ |
| 2281 |
if (context->typesize > BLOSC2_MAXTYPESIZE) { |
| 2282 |
// If typesize is too large for Blosc2, return an error |
| 2283 |
BLOSC_TRACE_ERROR("Typesize cannot exceed %d bytes.", BLOSC2_MAXTYPESIZE); |
| 2284 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2285 |
} |
| 2286 |
/* Now, cap typesize so that blosc2 split machinery can continue to work */ |
| 2287 |
if (context->typesize > BLOSC_MAX_TYPESIZE) { |
| 2288 |
/* If typesize is too large, treat buffer as an 1-byte stream. */ |
| 2289 |
context->typesize = 1; |
| 2290 |
} |
| 2291 |
|
| 2292 |
blosc2_calculate_blocks(context); |
| 2293 |
|
| 2294 |
return 1; |
| 2295 |
} |
| 2296 |
|
| 2297 |
|
| 2298 |
static int initialize_context_decompression(blosc2_context* context, blosc_header* header, const void* src, |
| 2299 |
int32_t srcsize, void* dest, int32_t destsize) { |
| 2300 |
int32_t bstarts_end; |
| 2301 |
|
| 2302 |
context->do_compress = 0; |
| 2303 |
context->src = (const uint8_t*)src; |
| 2304 |
context->srcsize = srcsize; |
| 2305 |
context->dest = (uint8_t*)dest; |
| 2306 |
context->destsize = destsize; |
| 2307 |
context->output_bytes = 0; |
| 2308 |
context->end_threads = 0; |
| 2309 |
|
| 2310 |
int rc = blosc2_initialize_context_from_header(context, header); |
| 2311 |
if (rc < 0) { |
| 2312 |
return rc; |
| 2313 |
} |
| 2314 |
|
| 2315 |
/* Check that we have enough space to decompress */ |
| 2316 |
if (context->sourcesize > (int32_t)context->destsize) { |
| 2317 |
return BLOSC2_ERROR_WRITE_BUFFER; |
| 2318 |
} |
| 2319 |
|
| 2320 |
if (context->block_maskout != NULL && context->block_maskout_nitems != context->nblocks) { |
| 2321 |
BLOSC_TRACE_ERROR("The number of items in block_maskout (%d) must match the number" |
| 2322 |
" of blocks in chunk (%d).", |
| 2323 |
context->block_maskout_nitems, context->nblocks); |
| 2324 |
return BLOSC2_ERROR_DATA; |
| 2325 |
} |
| 2326 |
|
| 2327 |
context->special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK; |
| 2328 |
if (context->special_type > BLOSC2_SPECIAL_LASTID) { |
| 2329 |
BLOSC_TRACE_ERROR("Unknown special values ID (%d) ", |
| 2330 |
context->special_type); |
| 2331 |
return BLOSC2_ERROR_DATA; |
| 2332 |
} |
| 2333 |
|
| 2334 |
int memcpyed = (context->header_flags & (uint8_t) BLOSC_MEMCPYED); |
| 2335 |
if (memcpyed && (header->cbytes != header->nbytes + context->header_overhead)) { |
| 2336 |
BLOSC_TRACE_ERROR("Wrong header info for this memcpyed chunk"); |
| 2337 |
return BLOSC2_ERROR_DATA; |
| 2338 |
} |
| 2339 |
|
| 2340 |
if ((header->nbytes == 0) && (header->cbytes == context->header_overhead) && |
| 2341 |
!context->special_type) { |
| 2342 |
// A compressed buffer with only a header can only contain a zero-length buffer |
| 2343 |
return 0; |
| 2344 |
} |
| 2345 |
|
| 2346 |
context->bstarts = (int32_t *) (context->src + context->header_overhead); |
| 2347 |
bstarts_end = context->header_overhead; |
| 2348 |
if (!context->special_type && !memcpyed) { |
| 2349 |
/* If chunk is not special or a memcpyed, we do have a bstarts section */ |
| 2350 |
bstarts_end = (int32_t)(context->header_overhead + (context->nblocks * sizeof(int32_t))); |
| 2351 |
} |
| 2352 |
|
| 2353 |
if (srcsize < bstarts_end) { |
| 2354 |
BLOSC_TRACE_ERROR("`bstarts` exceeds length of source buffer."); |
| 2355 |
return BLOSC2_ERROR_READ_BUFFER; |
| 2356 |
} |
| 2357 |
srcsize -= bstarts_end; |
| 2358 |
|
| 2359 |
/* Read optional dictionary if flag set */ |
| 2360 |
if (context->blosc2_flags & BLOSC2_USEDICT) { |
| 2361 |
#if defined(HAVE_ZSTD) |
| 2362 |
context->use_dict = 1; |
| 2363 |
if (context->dict_ddict != NULL) { |
| 2364 |
// Free the existing dictionary (probably from another chunk) |
| 2365 |
ZSTD_freeDDict(context->dict_ddict); |
| 2366 |
} |
| 2367 |
// The trained dictionary is after the bstarts block |
| 2368 |
if (srcsize < (signed)sizeof(int32_t)) { |
| 2369 |
BLOSC_TRACE_ERROR("Not enough space to read size of dictionary."); |
| 2370 |
return BLOSC2_ERROR_READ_BUFFER; |
| 2371 |
} |
| 2372 |
srcsize -= sizeof(int32_t); |
| 2373 |
// Read dictionary size |
| 2374 |
context->dict_size = sw32_(context->src + bstarts_end); |
| 2375 |
if (context->dict_size <= 0 || context->dict_size > BLOSC2_MAXDICTSIZE) { |
| 2376 |
BLOSC_TRACE_ERROR("Dictionary size is smaller than minimum or larger than maximum allowed."); |
| 2377 |
return BLOSC2_ERROR_CODEC_DICT; |
| 2378 |
} |
| 2379 |
if (srcsize < (int32_t)context->dict_size) { |
| 2380 |
BLOSC_TRACE_ERROR("Not enough space to read entire dictionary."); |
| 2381 |
return BLOSC2_ERROR_READ_BUFFER; |
| 2382 |
} |
| 2383 |
srcsize -= context->dict_size; |
| 2384 |
// Read dictionary |
| 2385 |
context->dict_buffer = (void*)(context->src + bstarts_end + sizeof(int32_t)); |
| 2386 |
context->dict_ddict = ZSTD_createDDict(context->dict_buffer, context->dict_size); |
| 2387 |
#endif // HAVE_ZSTD |
| 2388 |
} |
| 2389 |
|
| 2390 |
return 0; |
| 2391 |
} |
| 2392 |
|
| 2393 |
static int write_compression_header(blosc2_context* context, bool extended_header) { |
| 2394 |
blosc_header header; |
| 2395 |
int dont_split; |
| 2396 |
int dict_training = context->use_dict && (context->dict_cdict == NULL); |
| 2397 |
|
| 2398 |
context->header_flags = 0; |
| 2399 |
|
| 2400 |
if (context->clevel == 0) { |
| 2401 |
/* Compression level 0 means buffer to be memcpy'ed */ |
| 2402 |
context->header_flags |= (uint8_t)BLOSC_MEMCPYED; |
| 2403 |
} |
| 2404 |
if (context->sourcesize < BLOSC_MIN_BUFFERSIZE) { |
| 2405 |
/* Buffer is too small. Try memcpy'ing. */ |
| 2406 |
context->header_flags |= (uint8_t)BLOSC_MEMCPYED; |
| 2407 |
} |
| 2408 |
|
| 2409 |
bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
| 2410 |
if (extended_header) { |
| 2411 |
/* Indicate that we are building an extended header */ |
| 2412 |
context->header_overhead = BLOSC_EXTENDED_HEADER_LENGTH; |
| 2413 |
context->header_flags |= (BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE); |
| 2414 |
/* Store filter pipeline info at the end of the header */ |
| 2415 |
if (dict_training || memcpyed) { |
| 2416 |
context->bstarts = NULL; |
| 2417 |
context->output_bytes = context->header_overhead; |
| 2418 |
} else { |
| 2419 |
context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
| 2420 |
context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
| 2421 |
} |
| 2422 |
} else { |
| 2423 |
// Regular header |
| 2424 |
context->header_overhead = BLOSC_MIN_HEADER_LENGTH; |
| 2425 |
if (memcpyed) { |
| 2426 |
context->bstarts = NULL; |
| 2427 |
context->output_bytes = context->header_overhead; |
| 2428 |
} else { |
| 2429 |
context->bstarts = (int32_t *) (context->dest + context->header_overhead); |
| 2430 |
context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
| 2431 |
} |
| 2432 |
} |
| 2433 |
|
| 2434 |
// when memcpyed bit is set, there is no point in dealing with others |
| 2435 |
if (!memcpyed) { |
| 2436 |
if (context->filter_flags & BLOSC_DOSHUFFLE) { |
| 2437 |
/* Byte-shuffle is active */ |
| 2438 |
context->header_flags |= BLOSC_DOSHUFFLE; |
| 2439 |
} |
| 2440 |
|
| 2441 |
if (context->filter_flags & BLOSC_DOBITSHUFFLE) { |
| 2442 |
/* Bit-shuffle is active */ |
| 2443 |
context->header_flags |= BLOSC_DOBITSHUFFLE; |
| 2444 |
} |
| 2445 |
|
| 2446 |
if (context->filter_flags & BLOSC_DODELTA) { |
| 2447 |
/* Delta is active */ |
| 2448 |
context->header_flags |= BLOSC_DODELTA; |
| 2449 |
} |
| 2450 |
|
| 2451 |
dont_split = !split_block(context, context->typesize, |
| 2452 |
context->blocksize); |
| 2453 |
|
| 2454 |
/* dont_split is in bit 4 */ |
| 2455 |
context->header_flags |= dont_split << 4; |
| 2456 |
/* codec starts at bit 5 */ |
| 2457 |
uint8_t compformat = compcode_to_compformat(context->compcode); |
| 2458 |
context->header_flags |= compformat << 5; |
| 2459 |
} |
| 2460 |
|
| 2461 |
// Create blosc header and store to dest |
| 2462 |
blosc2_intialize_header_from_context(context, &header, extended_header); |
| 2463 |
|
| 2464 |
memcpy(context->dest, &header, (extended_header) ? |
| 2465 |
BLOSC_EXTENDED_HEADER_LENGTH : BLOSC_MIN_HEADER_LENGTH); |
| 2466 |
|
| 2467 |
return 1; |
| 2468 |
} |
| 2469 |
|
| 2470 |
|
| 2471 |
static int blosc_compress_context(blosc2_context* context) { |
| 2472 |
int ntbytes = 0; |
| 2473 |
blosc_timestamp_t last, current; |
| 2474 |
bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
| 2475 |
|
| 2476 |
blosc_set_timestamp(&last); |
| 2477 |
|
| 2478 |
if (!memcpyed) { |
| 2479 |
/* Do the actual compression */ |
| 2480 |
ntbytes = do_job(context); |
| 2481 |
if (ntbytes < 0) { |
| 2482 |
return ntbytes; |
| 2483 |
} |
| 2484 |
if (ntbytes == 0) { |
| 2485 |
// Try out with a memcpy later on (last chance for fitting src buffer in dest). |
| 2486 |
context->header_flags |= (uint8_t)BLOSC_MEMCPYED; |
| 2487 |
memcpyed = true; |
| 2488 |
} |
| 2489 |
} |
| 2490 |
|
| 2491 |
int dont_split = (context->header_flags & 0x10) >> 4; |
| 2492 |
int nstreams = context->nblocks; |
| 2493 |
if (!dont_split) { |
| 2494 |
// When splitting, the number of streams is computed differently |
| 2495 |
if (context->leftover) { |
| 2496 |
nstreams = (context->nblocks - 1) * context->typesize + 1; |
| 2497 |
} |
| 2498 |
else { |
| 2499 |
nstreams *= context->typesize; |
| 2500 |
} |
| 2501 |
} |
| 2502 |
|
| 2503 |
if (memcpyed) { |
| 2504 |
if (context->sourcesize + context->header_overhead > context->destsize) { |
| 2505 |
/* We are exceeding maximum output size */ |
| 2506 |
ntbytes = 0; |
| 2507 |
} |
| 2508 |
else { |
| 2509 |
context->output_bytes = context->header_overhead; |
| 2510 |
ntbytes = do_job(context); |
| 2511 |
if (ntbytes < 0) { |
| 2512 |
return ntbytes; |
| 2513 |
} |
| 2514 |
// Success! update the memcpy bit in header |
| 2515 |
context->dest[BLOSC2_CHUNK_FLAGS] = context->header_flags; |
| 2516 |
// and clear the memcpy bit in context (for next reuse) |
| 2517 |
context->header_flags &= ~(uint8_t)BLOSC_MEMCPYED; |
| 2518 |
} |
| 2519 |
} |
| 2520 |
else { |
| 2521 |
// Check whether we have a run for the whole chunk |
| 2522 |
int start_csizes = context->header_overhead + 4 * context->nblocks; |
| 2523 |
if (ntbytes == (int)(start_csizes + nstreams * sizeof(int32_t))) { |
| 2524 |
// The streams are all zero runs (by construction). Encode it... |
| 2525 |
context->dest[BLOSC2_CHUNK_BLOSC2_FLAGS] |= BLOSC2_SPECIAL_ZERO << 4; |
| 2526 |
// ...and assign the new chunk length |
| 2527 |
ntbytes = context->header_overhead; |
| 2528 |
} |
| 2529 |
} |
| 2530 |
|
| 2531 |
/* Set the number of compressed bytes in header */ |
| 2532 |
_sw32(context->dest + BLOSC2_CHUNK_CBYTES, ntbytes); |
| 2533 |
if (context->blosc2_flags & BLOSC2_INSTR_CODEC) { |
| 2534 |
dont_split = (context->header_flags & 0x10) >> 4; |
| 2535 |
int32_t blocksize = dont_split ? (int32_t)sizeof(blosc2_instr) : (int32_t)sizeof(blosc2_instr) * context->typesize; |
| 2536 |
_sw32(context->dest + BLOSC2_CHUNK_NBYTES, nstreams * (int32_t)sizeof(blosc2_instr)); |
| 2537 |
_sw32(context->dest + BLOSC2_CHUNK_BLOCKSIZE, blocksize); |
| 2538 |
} |
| 2539 |
|
| 2540 |
/* Set the number of bytes in dest buffer (might be useful for tuner) */ |
| 2541 |
context->destsize = ntbytes; |
| 2542 |
|
| 2543 |
if (context->tuner_params != NULL) { |
| 2544 |
blosc_set_timestamp(¤t); |
| 2545 |
double ctime = blosc_elapsed_secs(last, current); |
| 2546 |
int rc; |
| 2547 |
if (context->tuner_id < BLOSC_LAST_TUNER && context->tuner_id == BLOSC_STUNE) { |
| 2548 |
rc = blosc_stune_update(context, ctime); |
| 2549 |
} else { |
| 2550 |
for (int i = 0; i < g_ntuners; ++i) { |
| 2551 |
if (g_tuners[i].id == context->tuner_id) { |
| 2552 |
if (g_tuners[i].update == NULL) { |
| 2553 |
if (fill_tuner(&g_tuners[i]) < 0) { |
| 2554 |
BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
| 2555 |
return BLOSC2_ERROR_FAILURE; |
| 2556 |
} |
| 2557 |
} |
| 2558 |
rc = g_tuners[i].update(context, ctime); |
| 2559 |
goto urtunersuccess; |
| 2560 |
} |
| 2561 |
} |
| 2562 |
BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", context->tuner_id); |
| 2563 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2564 |
urtunersuccess:; |
| 2565 |
} |
| 2566 |
if (rc < 0) { |
| 2567 |
BLOSC_TRACE_ERROR("Error in tuner update func\n"); |
| 2568 |
return BLOSC2_ERROR_TUNER; |
| 2569 |
} |
| 2570 |
} |
| 2571 |
|
| 2572 |
return ntbytes; |
| 2573 |
} |
| 2574 |
|
| 2575 |
|
| 2576 |
/* The public secure routine for compression with context. */ |
| 2577 |
int blosc2_compress_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
| 2578 |
void* dest, int32_t destsize) { |
| 2579 |
int error, cbytes; |
| 2580 |
|
| 2581 |
if (context->do_compress != 1) { |
| 2582 |
BLOSC_TRACE_ERROR("Context is not meant for compression. Giving up."); |
| 2583 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2584 |
} |
| 2585 |
|
| 2586 |
error = initialize_context_compression( |
| 2587 |
context, src, srcsize, dest, destsize, |
| 2588 |
context->clevel, context->filters, context->filters_meta, |
| 2589 |
context->typesize, context->compcode, context->blocksize, |
| 2590 |
context->new_nthreads, context->nthreads, context->splitmode, |
| 2591 |
context->tuner_id, context->tuner_params, context->schunk); |
| 2592 |
if (error <= 0) { |
| 2593 |
return error; |
| 2594 |
} |
| 2595 |
|
| 2596 |
/* Write the extended header */ |
| 2597 |
error = write_compression_header(context, true); |
| 2598 |
if (error < 0) { |
| 2599 |
return error; |
| 2600 |
} |
| 2601 |
|
| 2602 |
cbytes = blosc_compress_context(context); |
| 2603 |
if (cbytes < 0) { |
| 2604 |
return cbytes; |
| 2605 |
} |
| 2606 |
|
| 2607 |
if (context->use_dict && context->dict_cdict == NULL) { |
| 2608 |
|
| 2609 |
if (context->compcode != BLOSC_ZSTD) { |
| 2610 |
const char* compname; |
| 2611 |
compname = clibcode_to_clibname(context->compcode); |
| 2612 |
BLOSC_TRACE_ERROR("Codec %s does not support dicts. Giving up.", |
| 2613 |
compname); |
| 2614 |
return BLOSC2_ERROR_CODEC_DICT; |
| 2615 |
} |
| 2616 |
|
| 2617 |
#ifdef HAVE_ZSTD |
| 2618 |
// Build the dictionary out of the filters outcome and compress with it |
| 2619 |
int32_t dict_maxsize = BLOSC2_MAXDICTSIZE; |
| 2620 |
// Do not make the dict more than 5% larger than uncompressed buffer |
| 2621 |
if (dict_maxsize > srcsize / 20) { |
| 2622 |
dict_maxsize = srcsize / 20; |
| 2623 |
} |
| 2624 |
void* samples_buffer = context->dest + context->header_overhead; |
| 2625 |
unsigned nblocks = (unsigned)context->nblocks; |
| 2626 |
int dont_split = (context->header_flags & 0x10) >> 4; |
| 2627 |
if (!dont_split) { |
| 2628 |
nblocks = nblocks * context->typesize; |
| 2629 |
} |
| 2630 |
if (nblocks < 8) { |
| 2631 |
nblocks = 8; // the minimum that accepts zstd as of 1.4.0 |
| 2632 |
} |
| 2633 |
|
| 2634 |
// 1 allows to use most of the chunk for training, but it is slower, |
| 2635 |
// and it does not always seem to improve compression ratio. |
| 2636 |
// Let's use 16, which is faster and still gives good results |
| 2637 |
// on test_dict_schunk.c, but this seems very dependent on the data. |
| 2638 |
unsigned sample_fraction = 16; |
| 2639 |
size_t sample_size = context->sourcesize / nblocks / sample_fraction; |
| 2640 |
|
| 2641 |
// Populate the samples sizes for training the dictionary |
| 2642 |
size_t* samples_sizes = malloc(nblocks * sizeof(void*)); |
| 2643 |
BLOSC_ERROR_NULL(samples_sizes, BLOSC2_ERROR_MEMORY_ALLOC); |
| 2644 |
for (size_t i = 0; i < nblocks; i++) { |
| 2645 |
samples_sizes[i] = sample_size; |
| 2646 |
} |
| 2647 |
|
| 2648 |
// Train from samples |
| 2649 |
void* dict_buffer = malloc(dict_maxsize); |
| 2650 |
BLOSC_ERROR_NULL(dict_buffer, BLOSC2_ERROR_MEMORY_ALLOC); |
| 2651 |
int32_t dict_actual_size = (int32_t)ZDICT_trainFromBuffer( |
| 2652 |
dict_buffer, dict_maxsize, |
| 2653 |
samples_buffer, samples_sizes, nblocks); |
| 2654 |
|
| 2655 |
// TODO: experiment with parameters of low-level fast cover algorithm |
| 2656 |
// Note that this API is still unstable. See: https://github.com/facebook/zstd/issues/1599 |
| 2657 |
// ZDICT_fastCover_params_t fast_cover_params; |
| 2658 |
// memset(&fast_cover_params, 0, sizeof(fast_cover_params)); |
| 2659 |
// fast_cover_params.d = nblocks; |
| 2660 |
// fast_cover_params.steps = 4; |
| 2661 |
// fast_cover_params.zParams.compressionLevel = context->clevel; |
| 2662 |
// size_t dict_actual_size = ZDICT_optimizeTrainFromBuffer_fastCover( |
| 2663 |
// dict_buffer, dict_maxsize, samples_buffer, samples_sizes, nblocks, |
| 2664 |
// &fast_cover_params); |
| 2665 |
|
| 2666 |
if (ZDICT_isError(dict_actual_size) != ZSTD_error_no_error) { |
| 2667 |
BLOSC_TRACE_ERROR("Error in ZDICT_trainFromBuffer(): '%s'." |
| 2668 |
" Giving up.", ZDICT_getErrorName(dict_actual_size)); |
| 2669 |
return BLOSC2_ERROR_CODEC_DICT; |
| 2670 |
} |
| 2671 |
assert(dict_actual_size > 0); |
| 2672 |
free(samples_sizes); |
| 2673 |
|
| 2674 |
// Update bytes counter and pointers to bstarts for the new compressed buffer |
| 2675 |
context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
| 2676 |
context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
| 2677 |
/* Write the size of trained dict at the end of bstarts */ |
| 2678 |
_sw32(context->dest + context->output_bytes, (int32_t)dict_actual_size); |
| 2679 |
context->output_bytes += sizeof(int32_t); |
| 2680 |
/* Write the trained dict afterwards */ |
| 2681 |
context->dict_buffer = context->dest + context->output_bytes; |
| 2682 |
memcpy(context->dict_buffer, dict_buffer, (unsigned int)dict_actual_size); |
| 2683 |
context->dict_cdict = ZSTD_createCDict(dict_buffer, dict_actual_size, 1); // TODO: use get_accel() |
| 2684 |
free(dict_buffer); // the dictionary is copied in the header now |
| 2685 |
context->output_bytes += (int32_t)dict_actual_size; |
| 2686 |
context->dict_size = dict_actual_size; |
| 2687 |
|
| 2688 |
/* Compress with dict */ |
| 2689 |
cbytes = blosc_compress_context(context); |
| 2690 |
|
| 2691 |
// Invalidate the dictionary for compressing other chunks using the same context |
| 2692 |
context->dict_buffer = NULL; |
| 2693 |
ZSTD_freeCDict(context->dict_cdict); |
| 2694 |
context->dict_cdict = NULL; |
| 2695 |
#endif // HAVE_ZSTD |
| 2696 |
} |
| 2697 |
|
| 2698 |
return cbytes; |
| 2699 |
} |
| 2700 |
|
| 2701 |
|
| 2702 |
void build_filters(const int doshuffle, const int delta, |
| 2703 |
const int32_t typesize, uint8_t* filters) { |
| 2704 |
|
| 2705 |
/* Fill the end part of the filter pipeline */ |
| 2706 |
if ((doshuffle == BLOSC_SHUFFLE) && (typesize > 1)) |
| 2707 |
filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_SHUFFLE; |
| 2708 |
if (doshuffle == BLOSC_BITSHUFFLE) |
| 2709 |
filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_BITSHUFFLE; |
| 2710 |
if (doshuffle == BLOSC_NOSHUFFLE) |
| 2711 |
filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_NOSHUFFLE; |
| 2712 |
if (delta) |
| 2713 |
filters[BLOSC2_MAX_FILTERS - 2] = BLOSC_DELTA; |
| 2714 |
} |
| 2715 |
|
| 2716 |
/* The public secure routine for compression. */ |
| 2717 |
int blosc2_compress(int clevel, int doshuffle, int32_t typesize, |
| 2718 |
const void* src, int32_t srcsize, void* dest, int32_t destsize) { |
| 2719 |
int error; |
| 2720 |
int result; |
| 2721 |
char* envvar; |
| 2722 |
|
| 2723 |
/* Check whether the library should be initialized */ |
| 2724 |
if (!g_initlib) blosc2_init(); |
| 2725 |
|
| 2726 |
/* Check for a BLOSC_CLEVEL environment variable */ |
| 2727 |
envvar = getenv("BLOSC_CLEVEL"); |
| 2728 |
if (envvar != NULL) { |
| 2729 |
long value; |
| 2730 |
value = strtol(envvar, NULL, 10); |
| 2731 |
if ((errno != EINVAL) && (value >= 0)) { |
| 2732 |
clevel = (int)value; |
| 2733 |
} |
| 2734 |
else { |
| 2735 |
BLOSC_TRACE_WARNING("BLOSC_CLEVEL environment variable '%s' not recognized\n", envvar); |
| 2736 |
} |
| 2737 |
} |
| 2738 |
|
| 2739 |
/* Check for a BLOSC_SHUFFLE environment variable */ |
| 2740 |
envvar = getenv("BLOSC_SHUFFLE"); |
| 2741 |
if (envvar != NULL) { |
| 2742 |
if (strcmp(envvar, "NOSHUFFLE") == 0) { |
| 2743 |
doshuffle = BLOSC_NOSHUFFLE; |
| 2744 |
} |
| 2745 |
else if (strcmp(envvar, "SHUFFLE") == 0) { |
| 2746 |
doshuffle = BLOSC_SHUFFLE; |
| 2747 |
} |
| 2748 |
else if (strcmp(envvar, "BITSHUFFLE") == 0) { |
| 2749 |
doshuffle = BLOSC_BITSHUFFLE; |
| 2750 |
} |
| 2751 |
else { |
| 2752 |
BLOSC_TRACE_WARNING("BLOSC_SHUFFLE environment variable '%s' not recognized\n", envvar); |
| 2753 |
} |
| 2754 |
} |
| 2755 |
|
| 2756 |
/* Check for a BLOSC_DELTA environment variable */ |
| 2757 |
envvar = getenv("BLOSC_DELTA"); |
| 2758 |
if (envvar != NULL) { |
| 2759 |
if (strcmp(envvar, "1") == 0) { |
| 2760 |
blosc2_set_delta(1); |
| 2761 |
} else if (strcmp(envvar, "0") == 0) { |
| 2762 |
blosc2_set_delta(0); |
| 2763 |
} |
| 2764 |
else { |
| 2765 |
BLOSC_TRACE_WARNING("BLOSC_DELTA environment variable '%s' not recognized\n", envvar); |
| 2766 |
} |
| 2767 |
} |
| 2768 |
|
| 2769 |
/* Check for a BLOSC_TYPESIZE environment variable */ |
| 2770 |
envvar = getenv("BLOSC_TYPESIZE"); |
| 2771 |
if (envvar != NULL) { |
| 2772 |
long value; |
| 2773 |
value = strtol(envvar, NULL, 10); |
| 2774 |
if ((errno != EINVAL) && (value > 0)) { |
| 2775 |
typesize = (int32_t)value; |
| 2776 |
} |
| 2777 |
else { |
| 2778 |
BLOSC_TRACE_WARNING("BLOSC_TYPESIZE environment variable '%s' not recognized\n", envvar); |
| 2779 |
} |
| 2780 |
} |
| 2781 |
|
| 2782 |
/* Check for a BLOSC_COMPRESSOR environment variable */ |
| 2783 |
envvar = getenv("BLOSC_COMPRESSOR"); |
| 2784 |
if (envvar != NULL) { |
| 2785 |
result = blosc1_set_compressor(envvar); |
| 2786 |
if (result < 0) { |
| 2787 |
BLOSC_TRACE_WARNING("BLOSC_COMPRESSOR environment variable '%s' not recognized\n", envvar); |
| 2788 |
} |
| 2789 |
} |
| 2790 |
|
| 2791 |
/* Check for a BLOSC_BLOCKSIZE environment variable */ |
| 2792 |
envvar = getenv("BLOSC_BLOCKSIZE"); |
| 2793 |
if (envvar != NULL) { |
| 2794 |
long blocksize; |
| 2795 |
blocksize = strtol(envvar, NULL, 10); |
| 2796 |
if ((errno != EINVAL) && (blocksize > 0)) { |
| 2797 |
blosc1_set_blocksize((size_t) blocksize); |
| 2798 |
} |
| 2799 |
else { |
| 2800 |
BLOSC_TRACE_WARNING("BLOSC_BLOCKSIZE environment variable '%s' not recognized\n", envvar); |
| 2801 |
} |
| 2802 |
} |
| 2803 |
|
| 2804 |
/* Check for a BLOSC_NTHREADS environment variable */ |
| 2805 |
envvar = getenv("BLOSC_NTHREADS"); |
| 2806 |
if (envvar != NULL) { |
| 2807 |
long nthreads; |
| 2808 |
nthreads = strtol(envvar, NULL, 10); |
| 2809 |
if ((errno != EINVAL) && (nthreads > 0)) { |
| 2810 |
result = blosc2_set_nthreads((int16_t) nthreads); |
| 2811 |
if (result < 0) { |
| 2812 |
BLOSC_TRACE_WARNING("BLOSC_NTHREADS environment variable '%s' not recognized\n", envvar); |
| 2813 |
} |
| 2814 |
} |
| 2815 |
} |
| 2816 |
|
| 2817 |
/* Check for a BLOSC_SPLITMODE environment variable */ |
| 2818 |
envvar = getenv("BLOSC_SPLITMODE"); |
| 2819 |
if (envvar != NULL) { |
| 2820 |
int32_t splitmode = -1; |
| 2821 |
if (strcmp(envvar, "ALWAYS") == 0) { |
| 2822 |
splitmode = BLOSC_ALWAYS_SPLIT; |
| 2823 |
} |
| 2824 |
else if (strcmp(envvar, "NEVER") == 0) { |
| 2825 |
splitmode = BLOSC_NEVER_SPLIT; |
| 2826 |
} |
| 2827 |
else if (strcmp(envvar, "AUTO") == 0) { |
| 2828 |
splitmode = BLOSC_AUTO_SPLIT; |
| 2829 |
} |
| 2830 |
else if (strcmp(envvar, "FORWARD_COMPAT") == 0) { |
| 2831 |
splitmode = BLOSC_FORWARD_COMPAT_SPLIT; |
| 2832 |
} |
| 2833 |
else { |
| 2834 |
BLOSC_TRACE_WARNING("BLOSC_SPLITMODE environment variable '%s' not recognized\n", envvar); |
| 2835 |
} |
| 2836 |
|
| 2837 |
if (splitmode >= 0) { |
| 2838 |
blosc1_set_splitmode(splitmode); |
| 2839 |
} |
| 2840 |
} |
| 2841 |
|
| 2842 |
/* Check for a BLOSC_NOLOCK environment variable. It is important |
| 2843 |
that this should be the last env var so that it can take the |
| 2844 |
previous ones into account */ |
| 2845 |
envvar = getenv("BLOSC_NOLOCK"); |
| 2846 |
if (envvar != NULL) { |
| 2847 |
// TODO: here is the only place that returns an extended header from |
| 2848 |
// a blosc1_compress() call. This should probably be fixed. |
| 2849 |
const char *compname; |
| 2850 |
blosc2_context *cctx; |
| 2851 |
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS; |
| 2852 |
|
| 2853 |
blosc2_compcode_to_compname(g_compressor, &compname); |
| 2854 |
/* Create a context for compression */ |
| 2855 |
build_filters(doshuffle, g_delta, typesize, cparams.filters); |
| 2856 |
// TODO: cparams can be shared in a multithreaded environment. do a copy! |
| 2857 |
cparams.typesize = (uint8_t)typesize; |
| 2858 |
cparams.compcode = (uint8_t)g_compressor; |
| 2859 |
cparams.clevel = (uint8_t)clevel; |
| 2860 |
cparams.nthreads = g_nthreads; |
| 2861 |
cparams.splitmode = g_splitmode; |
| 2862 |
cctx = blosc2_create_cctx(cparams); |
| 2863 |
if (cctx == NULL) { |
| 2864 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 2865 |
return BLOSC2_ERROR_NULL_POINTER; |
| 2866 |
} |
| 2867 |
/* Do the actual compression */ |
| 2868 |
result = blosc2_compress_ctx(cctx, src, srcsize, dest, destsize); |
| 2869 |
/* Release context resources */ |
| 2870 |
blosc2_free_ctx(cctx); |
| 2871 |
return result; |
| 2872 |
} |
| 2873 |
|
| 2874 |
pthread_mutex_lock(&global_comp_mutex); |
| 2875 |
|
| 2876 |
/* Initialize a context compression */ |
| 2877 |
uint8_t* filters = calloc(1, BLOSC2_MAX_FILTERS); |
| 2878 |
BLOSC_ERROR_NULL(filters, BLOSC2_ERROR_MEMORY_ALLOC); |
| 2879 |
uint8_t* filters_meta = calloc(1, BLOSC2_MAX_FILTERS); |
| 2880 |
BLOSC_ERROR_NULL(filters_meta, BLOSC2_ERROR_MEMORY_ALLOC); |
| 2881 |
build_filters(doshuffle, g_delta, typesize, filters); |
| 2882 |
error = initialize_context_compression( |
| 2883 |
g_global_context, src, srcsize, dest, destsize, clevel, filters, |
| 2884 |
filters_meta, (int32_t)typesize, g_compressor, g_force_blocksize, g_nthreads, g_nthreads, |
| 2885 |
g_splitmode, g_tuner, NULL, g_schunk); |
| 2886 |
free(filters); |
| 2887 |
free(filters_meta); |
| 2888 |
if (error <= 0) { |
| 2889 |
pthread_mutex_unlock(&global_comp_mutex); |
| 2890 |
return error; |
| 2891 |
} |
| 2892 |
|
| 2893 |
envvar = getenv("BLOSC_BLOSC1_COMPAT"); |
| 2894 |
if (envvar != NULL) { |
| 2895 |
/* Write chunk header without extended header (Blosc1 compatibility mode) */ |
| 2896 |
error = write_compression_header(g_global_context, false); |
| 2897 |
} |
| 2898 |
else { |
| 2899 |
error = write_compression_header(g_global_context, true); |
| 2900 |
} |
| 2901 |
if (error < 0) { |
| 2902 |
pthread_mutex_unlock(&global_comp_mutex); |
| 2903 |
return error; |
| 2904 |
} |
| 2905 |
|
| 2906 |
result = blosc_compress_context(g_global_context); |
| 2907 |
|
| 2908 |
pthread_mutex_unlock(&global_comp_mutex); |
| 2909 |
|
| 2910 |
return result; |
| 2911 |
} |
| 2912 |
|
| 2913 |
|
| 2914 |
/* The public routine for compression. */ |
| 2915 |
int blosc1_compress(int clevel, int doshuffle, size_t typesize, size_t nbytes, |
| 2916 |
const void* src, void* dest, size_t destsize) { |
| 2917 |
return blosc2_compress(clevel, doshuffle, (int32_t)typesize, src, (int32_t)nbytes, dest, (int32_t)destsize); |
| 2918 |
} |
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
static int blosc_run_decompression_with_context(blosc2_context* context, const void* src, int32_t srcsize, |
| 2923 |
void* dest, int32_t destsize) { |
| 2924 |
blosc_header header; |
| 2925 |
int32_t ntbytes; |
| 2926 |
int rc; |
| 2927 |
|
| 2928 |
rc = read_chunk_header(src, srcsize, true, &header); |
| 2929 |
if (rc < 0) { |
| 2930 |
return rc; |
| 2931 |
} |
| 2932 |
|
| 2933 |
if (header.nbytes > destsize) { |
| 2934 |
// Not enough space for writing into the destination |
| 2935 |
return BLOSC2_ERROR_WRITE_BUFFER; |
| 2936 |
} |
| 2937 |
|
| 2938 |
rc = initialize_context_decompression(context, &header, src, srcsize, dest, destsize); |
| 2939 |
if (rc < 0) { |
| 2940 |
return rc; |
| 2941 |
} |
| 2942 |
|
| 2943 |
/* Do the actual decompression */ |
| 2944 |
ntbytes = do_job(context); |
| 2945 |
if (ntbytes < 0) { |
| 2946 |
return ntbytes; |
| 2947 |
} |
| 2948 |
|
| 2949 |
assert(ntbytes <= (int32_t)destsize); |
| 2950 |
return ntbytes; |
| 2951 |
} |
| 2952 |
|
| 2953 |
|
| 2954 |
/* The public secure routine for decompression with context. */ |
| 2955 |
int blosc2_decompress_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
| 2956 |
void* dest, int32_t destsize) { |
| 2957 |
int result; |
| 2958 |
|
| 2959 |
if (context->do_compress != 0) { |
| 2960 |
BLOSC_TRACE_ERROR("Context is not meant for decompression. Giving up."); |
| 2961 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2962 |
} |
| 2963 |
|
| 2964 |
result = blosc_run_decompression_with_context(context, src, srcsize, dest, destsize); |
| 2965 |
|
| 2966 |
// Reset a possible block_maskout |
| 2967 |
if (context->block_maskout != NULL) { |
| 2968 |
free(context->block_maskout); |
| 2969 |
context->block_maskout = NULL; |
| 2970 |
} |
| 2971 |
context->block_maskout_nitems = 0; |
| 2972 |
|
| 2973 |
return result; |
| 2974 |
} |
| 2975 |
|
| 2976 |
|
| 2977 |
/* The public secure routine for decompression. */ |
| 2978 |
int blosc2_decompress(const void* src, int32_t srcsize, void* dest, int32_t destsize) { |
| 2979 |
int result; |
| 2980 |
char* envvar; |
| 2981 |
long nthreads; |
| 2982 |
blosc2_context *dctx; |
| 2983 |
blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS; |
| 2984 |
|
| 2985 |
/* Check whether the library should be initialized */ |
| 2986 |
if (!g_initlib) blosc2_init(); |
| 2987 |
|
| 2988 |
/* Check for a BLOSC_NTHREADS environment variable */ |
| 2989 |
envvar = getenv("BLOSC_NTHREADS"); |
| 2990 |
if (envvar != NULL) { |
| 2991 |
nthreads = strtol(envvar, NULL, 10); |
| 2992 |
if ((errno != EINVAL)) { |
| 2993 |
if ((nthreads <= 0) || (nthreads > INT16_MAX)) { |
| 2994 |
BLOSC_TRACE_ERROR("nthreads must be >= 1 and <= %d", INT16_MAX); |
| 2995 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2996 |
} |
| 2997 |
result = blosc2_set_nthreads((int16_t) nthreads); |
| 2998 |
if (result < 0) { |
| 2999 |
return result; |
| 3000 |
} |
| 3001 |
} |
| 3002 |
} |
| 3003 |
|
| 3004 |
/* Check for a BLOSC_NOLOCK environment variable. It is important |
| 3005 |
that this should be the last env var so that it can take the |
| 3006 |
previous ones into account */ |
| 3007 |
envvar = getenv("BLOSC_NOLOCK"); |
| 3008 |
if (envvar != NULL) { |
| 3009 |
dparams.nthreads = g_nthreads; |
| 3010 |
dctx = blosc2_create_dctx(dparams); |
| 3011 |
if (dctx == NULL) { |
| 3012 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 3013 |
return BLOSC2_ERROR_NULL_POINTER; |
| 3014 |
} |
| 3015 |
result = blosc2_decompress_ctx(dctx, src, srcsize, dest, destsize); |
| 3016 |
blosc2_free_ctx(dctx); |
| 3017 |
return result; |
| 3018 |
} |
| 3019 |
|
| 3020 |
pthread_mutex_lock(&global_comp_mutex); |
| 3021 |
|
| 3022 |
result = blosc_run_decompression_with_context( |
| 3023 |
g_global_context, src, srcsize, dest, destsize); |
| 3024 |
|
| 3025 |
pthread_mutex_unlock(&global_comp_mutex); |
| 3026 |
|
| 3027 |
return result; |
| 3028 |
} |
| 3029 |
|
| 3030 |
|
| 3031 |
/* The public routine for decompression. */ |
| 3032 |
int blosc1_decompress(const void* src, void* dest, size_t destsize) { |
| 3033 |
return blosc2_decompress(src, INT32_MAX, dest, (int32_t)destsize); |
| 3034 |
} |
| 3035 |
|
| 3036 |
|
| 3037 |
/* Specific routine optimized for decompression a small number of |
| 3038 |
items out of a compressed chunk. This does not use threads because |
| 3039 |
it would affect negatively to performance. */ |
| 3040 |
int _blosc_getitem(blosc2_context* context, blosc_header* header, const void* src, int32_t srcsize, |
| 3041 |
int start, int nitems, void* dest, int32_t destsize) { |
| 3042 |
uint8_t* _src = (uint8_t*)(src); /* current pos for source buffer */ |
| 3043 |
uint8_t* _dest = (uint8_t*)(dest); |
| 3044 |
int32_t ntbytes = 0; /* the number of uncompressed bytes */ |
| 3045 |
int32_t bsize, bsize2, ebsize, leftoverblock; |
| 3046 |
int32_t startb, stopb; |
| 3047 |
int32_t stop = start + nitems; |
| 3048 |
int j, rc; |
| 3049 |
|
| 3050 |
if (nitems == 0) { |
| 3051 |
// We have nothing to do |
| 3052 |
return 0; |
| 3053 |
} |
| 3054 |
if (nitems * header->typesize > destsize) { |
| 3055 |
BLOSC_TRACE_ERROR("`nitems`*`typesize` out of dest bounds."); |
| 3056 |
return BLOSC2_ERROR_WRITE_BUFFER; |
| 3057 |
} |
| 3058 |
|
| 3059 |
context->bstarts = (int32_t*)(_src + context->header_overhead); |
| 3060 |
|
| 3061 |
/* Check region boundaries */ |
| 3062 |
if ((start < 0) || (start * header->typesize > header->nbytes)) { |
| 3063 |
BLOSC_TRACE_ERROR("`start` out of bounds."); |
| 3064 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 3065 |
} |
| 3066 |
|
| 3067 |
if ((stop < 0) || (stop * header->typesize > header->nbytes)) { |
| 3068 |
BLOSC_TRACE_ERROR("`start`+`nitems` out of bounds."); |
| 3069 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 3070 |
} |
| 3071 |
|
| 3072 |
int chunk_memcpy = header->flags & 0x1; |
| 3073 |
if (!context->special_type && !chunk_memcpy && |
| 3074 |
((uint8_t *)(_src + srcsize) < (uint8_t *)(context->bstarts + context->nblocks))) { |
| 3075 |
BLOSC_TRACE_ERROR("`bstarts` out of bounds."); |
| 3076 |
return BLOSC2_ERROR_READ_BUFFER; |
| 3077 |
} |
| 3078 |
|
| 3079 |
bool memcpyed = header->flags & (uint8_t)BLOSC_MEMCPYED; |
| 3080 |
if (context->special_type) { |
| 3081 |
// Fake a runlen as if its a memcpyed chunk |
| 3082 |
memcpyed = true; |
| 3083 |
} |
| 3084 |
|
| 3085 |
bool is_lazy = ((context->header_overhead == BLOSC_EXTENDED_HEADER_LENGTH) && |
| 3086 |
(context->blosc2_flags & 0x08u) && !context->special_type); |
| 3087 |
if (memcpyed && !is_lazy && !context->postfilter) { |
| 3088 |
// Short-circuit for (non-lazy) memcpyed or special values |
| 3089 |
ntbytes = nitems * header->typesize; |
| 3090 |
switch (context->special_type) { |
| 3091 |
case BLOSC2_SPECIAL_VALUE: |
| 3092 |
// All repeated values |
| 3093 |
rc = set_values(context->typesize, _src, _dest, ntbytes); |
| 3094 |
if (rc < 0) { |
| 3095 |
BLOSC_TRACE_ERROR("set_values failed"); |
| 3096 |
return BLOSC2_ERROR_DATA; |
| 3097 |
} |
| 3098 |
break; |
| 3099 |
case BLOSC2_SPECIAL_NAN: |
| 3100 |
rc = set_nans(context->typesize, _dest, ntbytes); |
| 3101 |
if (rc < 0) { |
| 3102 |
BLOSC_TRACE_ERROR("set_nans failed"); |
| 3103 |
return BLOSC2_ERROR_DATA; |
| 3104 |
} |
| 3105 |
break; |
| 3106 |
case BLOSC2_SPECIAL_ZERO: |
| 3107 |
memset(_dest, 0, ntbytes); |
| 3108 |
break; |
| 3109 |
case BLOSC2_SPECIAL_UNINIT: |
| 3110 |
// We do nothing here |
| 3111 |
break; |
| 3112 |
case BLOSC2_NO_SPECIAL: |
| 3113 |
_src += context->header_overhead + start * context->typesize; |
| 3114 |
memcpy(_dest, _src, ntbytes); |
| 3115 |
break; |
| 3116 |
default: |
| 3117 |
BLOSC_TRACE_ERROR("Unhandled special value case"); |
| 3118 |
BLOSC_ERROR(BLOSC2_ERROR_SCHUNK_SPECIAL); |
| 3119 |
} |
| 3120 |
return ntbytes; |
| 3121 |
} |
| 3122 |
|
| 3123 |
ebsize = header->blocksize + header->typesize * (signed)sizeof(int32_t); |
| 3124 |
struct thread_context* scontext = context->serial_context; |
| 3125 |
/* Resize the temporaries in serial context if needed */ |
| 3126 |
if (header->blocksize > scontext->tmp_blocksize) { |
| 3127 |
my_free(scontext->tmp); |
| 3128 |
scontext->tmp_nbytes = (size_t)4 * ebsize; |
| 3129 |
scontext->tmp = my_malloc(scontext->tmp_nbytes); |
| 3130 |
BLOSC_ERROR_NULL(scontext->tmp, BLOSC2_ERROR_MEMORY_ALLOC); |
| 3131 |
scontext->tmp2 = scontext->tmp + ebsize; |
| 3132 |
scontext->tmp3 = scontext->tmp2 + ebsize; |
| 3133 |
scontext->tmp4 = scontext->tmp3 + ebsize; |
| 3134 |
scontext->tmp_blocksize = (int32_t)header->blocksize; |
| 3135 |
} |
| 3136 |
|
| 3137 |
for (j = 0; j < context->nblocks; j++) { |
| 3138 |
bsize = header->blocksize; |
| 3139 |
leftoverblock = 0; |
| 3140 |
if ((j == context->nblocks - 1) && (context->leftover > 0)) { |
| 3141 |
bsize = context->leftover; |
| 3142 |
leftoverblock = 1; |
| 3143 |
} |
| 3144 |
|
| 3145 |
/* Compute start & stop for each block */ |
| 3146 |
startb = start * header->typesize - j * header->blocksize; |
| 3147 |
stopb = stop * header->typesize - j * header->blocksize; |
| 3148 |
if (stopb <= 0) { |
| 3149 |
// We can exit as soon as this block is beyond stop |
| 3150 |
break; |
| 3151 |
} |
| 3152 |
if (startb >= header->blocksize) { |
| 3153 |
continue; |
| 3154 |
} |
| 3155 |
if (startb < 0) { |
| 3156 |
startb = 0; |
| 3157 |
} |
| 3158 |
if (stopb > header->blocksize) { |
| 3159 |
stopb = header->blocksize; |
| 3160 |
} |
| 3161 |
bsize2 = stopb - startb; |
| 3162 |
|
| 3163 |
#if defined(HAVE_PLUGINS) |
| 3164 |
if (context->compcode == BLOSC_CODEC_ZFP_FIXED_RATE) { |
| 3165 |
scontext->zfp_cell_start = startb / context->typesize; |
| 3166 |
scontext->zfp_cell_nitems = nitems; |
| 3167 |
} |
| 3168 |
#endif /* HAVE_PLUGINS */ |
| 3169 |
|
| 3170 |
/* Do the actual data copy */ |
| 3171 |
// Regular decompression. Put results in tmp2. |
| 3172 |
// If the block is aligned and the worst case fits in destination, let's avoid a copy |
| 3173 |
bool get_single_block = ((startb == 0) && (bsize == nitems * header->typesize)); |
| 3174 |
uint8_t* tmp2 = get_single_block ? dest : scontext->tmp2; |
| 3175 |
|
| 3176 |
// If memcpyed we don't have a bstarts section (because it is not needed) |
| 3177 |
int32_t src_offset = memcpyed ? |
| 3178 |
context->header_overhead + j * header->blocksize : sw32_(context->bstarts + j); |
| 3179 |
|
| 3180 |
int32_t cbytes = blosc_d(context->serial_context, bsize, leftoverblock, memcpyed, |
| 3181 |
src, srcsize, src_offset, j, |
| 3182 |
tmp2, 0, scontext->tmp, scontext->tmp3); |
| 3183 |
if (cbytes < 0) { |
| 3184 |
ntbytes = cbytes; |
| 3185 |
break; |
| 3186 |
} |
| 3187 |
if (scontext->zfp_cell_nitems > 0) { |
| 3188 |
if (cbytes == bsize2) { |
| 3189 |
memcpy((uint8_t *) dest, tmp2, (unsigned int) bsize2); |
| 3190 |
} else if (cbytes == context->blocksize) { |
| 3191 |
memcpy((uint8_t *) dest, tmp2 + scontext->zfp_cell_start * context->typesize, (unsigned int) bsize2); |
| 3192 |
cbytes = bsize2; |
| 3193 |
} |
| 3194 |
} else if (!get_single_block) { |
| 3195 |
/* Copy to destination */ |
| 3196 |
memcpy((uint8_t *) dest + ntbytes, tmp2 + startb, (unsigned int) bsize2); |
| 3197 |
} |
| 3198 |
ntbytes += bsize2; |
| 3199 |
} |
| 3200 |
|
| 3201 |
scontext->zfp_cell_nitems = 0; |
| 3202 |
|
| 3203 |
return ntbytes; |
| 3204 |
} |
| 3205 |
|
| 3206 |
int blosc2_getitem(const void* src, int32_t srcsize, int start, int nitems, void* dest, int32_t destsize) { |
| 3207 |
blosc2_context context; |
| 3208 |
int result; |
| 3209 |
|
| 3210 |
/* Minimally populate the context */ |
| 3211 |
memset(&context, 0, sizeof(blosc2_context)); |
| 3212 |
|
| 3213 |
context.schunk = g_schunk; |
| 3214 |
context.nthreads = 1; // force a serial decompression; fixes #95 |
| 3215 |
|
| 3216 |
/* Call the actual getitem function */ |
| 3217 |
result = blosc2_getitem_ctx(&context, src, srcsize, start, nitems, dest, destsize); |
| 3218 |
|
| 3219 |
/* Release resources */ |
| 3220 |
if (context.serial_context != NULL) { |
| 3221 |
free_thread_context(context.serial_context); |
| 3222 |
} |
| 3223 |
return result; |
| 3224 |
} |
| 3225 |
|
| 3226 |
/* Specific routine optimized for decompression a small number of |
| 3227 |
items out of a compressed chunk. Public non-contextual API. */ |
| 3228 |
int blosc1_getitem(const void* src, int start, int nitems, void* dest) { |
| 3229 |
return blosc2_getitem(src, INT32_MAX, start, nitems, dest, INT32_MAX); |
| 3230 |
} |
| 3231 |
|
| 3232 |
int blosc2_getitem_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
| 3233 |
int start, int nitems, void* dest, int32_t destsize) { |
| 3234 |
blosc_header header; |
| 3235 |
int result; |
| 3236 |
|
| 3237 |
/* Minimally populate the context */ |
| 3238 |
result = read_chunk_header((uint8_t *) src, srcsize, true, &header); |
| 3239 |
if (result < 0) { |
| 3240 |
return result; |
| 3241 |
} |
| 3242 |
|
| 3243 |
context->src = src; |
| 3244 |
context->srcsize = srcsize; |
| 3245 |
context->dest = dest; |
| 3246 |
context->destsize = destsize; |
| 3247 |
|
| 3248 |
result = blosc2_initialize_context_from_header(context, &header); |
| 3249 |
if (result < 0) { |
| 3250 |
return result; |
| 3251 |
} |
| 3252 |
|
| 3253 |
if (context->serial_context == NULL) { |
| 3254 |
context->serial_context = create_thread_context(context, 0); |
| 3255 |
} |
| 3256 |
BLOSC_ERROR_NULL(context->serial_context, BLOSC2_ERROR_THREAD_CREATE); |
| 3257 |
/* Call the actual getitem function */ |
| 3258 |
result = _blosc_getitem(context, &header, src, srcsize, start, nitems, dest, destsize); |
| 3259 |
|
| 3260 |
return result; |
| 3261 |
} |
| 3262 |
|
| 3263 |
/* execute single compression/decompression job for a single thread_context */ |
| 3264 |
static void t_blosc_do_job(void *ctxt) |
| 3265 |
{ |
| 3266 |
struct thread_context* thcontext = (struct thread_context*)ctxt; |
| 3267 |
blosc2_context* context = thcontext->parent_context; |
| 3268 |
int32_t cbytes; |
| 3269 |
int32_t ntdest; |
| 3270 |
int32_t tblocks; /* number of blocks per thread */ |
| 3271 |
int32_t tblock; /* limit block on a thread */ |
| 3272 |
int32_t nblock_; /* private copy of nblock */ |
| 3273 |
int32_t bsize; |
| 3274 |
int32_t leftoverblock; |
| 3275 |
/* Parameters for threads */ |
| 3276 |
int32_t blocksize; |
| 3277 |
int32_t ebsize; |
| 3278 |
int32_t srcsize; |
| 3279 |
bool compress = context->do_compress != 0; |
| 3280 |
int32_t maxbytes; |
| 3281 |
int32_t nblocks; |
| 3282 |
int32_t leftover; |
| 3283 |
int32_t leftover2; |
| 3284 |
int32_t* bstarts; |
| 3285 |
const uint8_t* src; |
| 3286 |
uint8_t* dest; |
| 3287 |
uint8_t* tmp; |
| 3288 |
uint8_t* tmp2; |
| 3289 |
uint8_t* tmp3; |
| 3290 |
|
| 3291 |
/* Get parameters for this thread before entering the main loop */ |
| 3292 |
blocksize = context->blocksize; |
| 3293 |
ebsize = blocksize + context->typesize * (int32_t)sizeof(int32_t); |
| 3294 |
maxbytes = context->destsize; |
| 3295 |
nblocks = context->nblocks; |
| 3296 |
leftover = context->leftover; |
| 3297 |
bstarts = context->bstarts; |
| 3298 |
src = context->src; |
| 3299 |
srcsize = context->srcsize; |
| 3300 |
dest = context->dest; |
| 3301 |
|
| 3302 |
/* Resize the temporaries if needed */ |
| 3303 |
if (blocksize > thcontext->tmp_blocksize) { |
| 3304 |
my_free(thcontext->tmp); |
| 3305 |
thcontext->tmp_nbytes = (size_t) 4 * ebsize; |
| 3306 |
thcontext->tmp = my_malloc(thcontext->tmp_nbytes); |
| 3307 |
thcontext->tmp2 = thcontext->tmp + ebsize; |
| 3308 |
thcontext->tmp3 = thcontext->tmp2 + ebsize; |
| 3309 |
thcontext->tmp4 = thcontext->tmp3 + ebsize; |
| 3310 |
thcontext->tmp_blocksize = blocksize; |
| 3311 |
} |
| 3312 |
|
| 3313 |
tmp = thcontext->tmp; |
| 3314 |
tmp2 = thcontext->tmp2; |
| 3315 |
tmp3 = thcontext->tmp3; |
| 3316 |
|
| 3317 |
// Determine whether we can do a static distribution of workload among different threads |
| 3318 |
bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
| 3319 |
if (!context->do_compress && context->special_type) { |
| 3320 |
// Fake a runlen as if its a memcpyed chunk |
| 3321 |
memcpyed = true; |
| 3322 |
} |
| 3323 |
|
| 3324 |
bool static_schedule = (!compress || memcpyed) && context->block_maskout == NULL; |
| 3325 |
if (static_schedule) { |
| 3326 |
/* Blocks per thread */ |
| 3327 |
tblocks = nblocks / context->nthreads; |
| 3328 |
leftover2 = nblocks % context->nthreads; |
| 3329 |
tblocks = (leftover2 > 0) ? tblocks + 1 : tblocks; |
| 3330 |
nblock_ = thcontext->tid * tblocks; |
| 3331 |
tblock = nblock_ + tblocks; |
| 3332 |
if (tblock > nblocks) { |
| 3333 |
tblock = nblocks; |
| 3334 |
} |
| 3335 |
} |
| 3336 |
else { |
| 3337 |
// Use dynamic schedule via a queue. Get the next block. |
| 3338 |
pthread_mutex_lock(&context->count_mutex); |
| 3339 |
context->thread_nblock++; |
| 3340 |
nblock_ = context->thread_nblock; |
| 3341 |
pthread_mutex_unlock(&context->count_mutex); |
| 3342 |
tblock = nblocks; |
| 3343 |
} |
| 3344 |
|
| 3345 |
/* Loop over blocks */ |
| 3346 |
leftoverblock = 0; |
| 3347 |
while ((nblock_ < tblock) && (context->thread_giveup_code > 0)) { |
| 3348 |
bsize = blocksize; |
| 3349 |
if (nblock_ == (nblocks - 1) && (leftover > 0)) { |
| 3350 |
bsize = leftover; |
| 3351 |
leftoverblock = 1; |
| 3352 |
} |
| 3353 |
if (compress) { |
| 3354 |
if (memcpyed) { |
| 3355 |
if (!context->prefilter) { |
| 3356 |
/* We want to memcpy only */ |
| 3357 |
memcpy(dest + context->header_overhead + nblock_ * blocksize, |
| 3358 |
src + nblock_ * blocksize, (unsigned int) bsize); |
| 3359 |
cbytes = (int32_t) bsize; |
| 3360 |
} |
| 3361 |
else { |
| 3362 |
/* Only the prefilter has to be executed, and this is done in blosc_c(). |
| 3363 |
* However, no further actions are needed, so we can put the result |
| 3364 |
* directly in dest. */ |
| 3365 |
cbytes = blosc_c(thcontext, bsize, leftoverblock, 0, |
| 3366 |
ebsize, src, nblock_ * blocksize, |
| 3367 |
dest + context->header_overhead + nblock_ * blocksize, |
| 3368 |
tmp, tmp3); |
| 3369 |
} |
| 3370 |
} |
| 3371 |
else { |
| 3372 |
/* Regular compression */ |
| 3373 |
cbytes = blosc_c(thcontext, bsize, leftoverblock, 0, |
| 3374 |
ebsize, src, nblock_ * blocksize, tmp2, tmp, tmp3); |
| 3375 |
} |
| 3376 |
} |
| 3377 |
else { |
| 3378 |
/* Regular decompression */ |
| 3379 |
if (context->special_type == BLOSC2_NO_SPECIAL && !memcpyed && |
| 3380 |
(srcsize < (int32_t)(context->header_overhead + (sizeof(int32_t) * nblocks)))) { |
| 3381 |
/* Not enough input to read all `bstarts` */ |
| 3382 |
cbytes = -1; |
| 3383 |
} |
| 3384 |
else { |
| 3385 |
// If memcpyed we don't have a bstarts section (because it is not needed) |
| 3386 |
int32_t src_offset = memcpyed ? |
| 3387 |
context->header_overhead + nblock_ * blocksize : sw32_(bstarts + nblock_); |
| 3388 |
cbytes = blosc_d(thcontext, bsize, leftoverblock, memcpyed, |
| 3389 |
src, srcsize, src_offset, nblock_, |
| 3390 |
dest, nblock_ * blocksize, tmp, tmp2); |
| 3391 |
} |
| 3392 |
} |
| 3393 |
|
| 3394 |
/* Check whether current thread has to giveup */ |
| 3395 |
if (context->thread_giveup_code <= 0) { |
| 3396 |
break; |
| 3397 |
} |
| 3398 |
|
| 3399 |
/* Check results for the compressed/decompressed block */ |
| 3400 |
if (cbytes < 0) { /* compr/decompr failure */ |
| 3401 |
/* Set giveup_code error */ |
| 3402 |
pthread_mutex_lock(&context->count_mutex); |
| 3403 |
context->thread_giveup_code = cbytes; |
| 3404 |
pthread_mutex_unlock(&context->count_mutex); |
| 3405 |
break; |
| 3406 |
} |
| 3407 |
|
| 3408 |
if (compress && !memcpyed) { |
| 3409 |
/* Start critical section */ |
| 3410 |
pthread_mutex_lock(&context->count_mutex); |
| 3411 |
ntdest = context->output_bytes; |
| 3412 |
// Note: do not use a typical local dict_training variable here |
| 3413 |
// because it is probably cached from previous calls if the number of |
| 3414 |
// threads does not change (the usual thing). |
| 3415 |
if (!(context->use_dict && context->dict_cdict == NULL)) { |
| 3416 |
_sw32(bstarts + nblock_, (int32_t) ntdest); |
| 3417 |
} |
| 3418 |
|
| 3419 |
if ((cbytes == 0) || (ntdest + cbytes > maxbytes)) { |
| 3420 |
context->thread_giveup_code = 0; /* incompressible buf */ |
| 3421 |
pthread_mutex_unlock(&context->count_mutex); |
| 3422 |
break; |
| 3423 |
} |
| 3424 |
context->thread_nblock++; |
| 3425 |
nblock_ = context->thread_nblock; |
| 3426 |
context->output_bytes += cbytes; |
| 3427 |
pthread_mutex_unlock(&context->count_mutex); |
| 3428 |
/* End of critical section */ |
| 3429 |
|
| 3430 |
/* Copy the compressed buffer to destination */ |
| 3431 |
memcpy(dest + ntdest, tmp2, (unsigned int) cbytes); |
| 3432 |
} |
| 3433 |
else if (static_schedule) { |
| 3434 |
nblock_++; |
| 3435 |
} |
| 3436 |
else { |
| 3437 |
pthread_mutex_lock(&context->count_mutex); |
| 3438 |
context->thread_nblock++; |
| 3439 |
nblock_ = context->thread_nblock; |
| 3440 |
context->output_bytes += cbytes; |
| 3441 |
pthread_mutex_unlock(&context->count_mutex); |
| 3442 |
} |
| 3443 |
|
| 3444 |
} /* closes while (nblock_) */ |
| 3445 |
|
| 3446 |
if (static_schedule) { |
| 3447 |
pthread_mutex_lock(&context->count_mutex); |
| 3448 |
context->output_bytes = context->sourcesize; |
| 3449 |
if (compress) { |
| 3450 |
context->output_bytes += context->header_overhead; |
| 3451 |
} |
| 3452 |
pthread_mutex_unlock(&context->count_mutex); |
| 3453 |
} |
| 3454 |
|
| 3455 |
} |
| 3456 |
|
| 3457 |
/* Decompress & unshuffle several blocks in a single thread */ |
| 3458 |
static void* t_blosc(void* ctxt) { |
| 3459 |
struct thread_context* thcontext = (struct thread_context*)ctxt; |
| 3460 |
blosc2_context* context = thcontext->parent_context; |
| 3461 |
#ifdef BLOSC_POSIX_BARRIERS |
| 3462 |
int rc; |
| 3463 |
#endif |
| 3464 |
|
| 3465 |
while (1) { |
| 3466 |
/* Synchronization point for all threads (wait for initialization) */ |
| 3467 |
WAIT_INIT(NULL, context); |
| 3468 |
|
| 3469 |
if (context->end_threads) { |
| 3470 |
break; |
| 3471 |
} |
| 3472 |
|
| 3473 |
t_blosc_do_job(ctxt); |
| 3474 |
|
| 3475 |
/* Meeting point for all threads (wait for finalization) */ |
| 3476 |
WAIT_FINISH(NULL, context); |
| 3477 |
} |
| 3478 |
|
| 3479 |
/* Cleanup our working space and context */ |
| 3480 |
free_thread_context(thcontext); |
| 3481 |
|
| 3482 |
return (NULL); |
| 3483 |
} |
| 3484 |
|
| 3485 |
|
| 3486 |
int init_threadpool(blosc2_context *context) { |
| 3487 |
int32_t tid; |
| 3488 |
int rc2; |
| 3489 |
|
| 3490 |
/* Initialize mutex and condition variable objects */ |
| 3491 |
pthread_mutex_init(&context->count_mutex, NULL); |
| 3492 |
pthread_mutex_init(&context->delta_mutex, NULL); |
| 3493 |
pthread_mutex_init(&context->nchunk_mutex, NULL); |
| 3494 |
pthread_cond_init(&context->delta_cv, NULL); |
| 3495 |
|
| 3496 |
/* Set context thread sentinels */ |
| 3497 |
context->thread_giveup_code = 1; |
| 3498 |
context->thread_nblock = -1; |
| 3499 |
|
| 3500 |
/* Barrier initialization */ |
| 3501 |
#ifdef BLOSC_POSIX_BARRIERS |
| 3502 |
pthread_barrier_init(&context->barr_init, NULL, context->nthreads + 1); |
| 3503 |
pthread_barrier_init(&context->barr_finish, NULL, context->nthreads + 1); |
| 3504 |
#else |
| 3505 |
pthread_mutex_init(&context->count_threads_mutex, NULL); |
| 3506 |
pthread_cond_init(&context->count_threads_cv, NULL); |
| 3507 |
context->count_threads = 0; /* Reset threads counter */ |
| 3508 |
#endif |
| 3509 |
|
| 3510 |
if (threads_callback) { |
| 3511 |
/* Create thread contexts to store data for callback threads */ |
| 3512 |
context->thread_contexts = (struct thread_context *)my_malloc( |
| 3513 |
context->nthreads * sizeof(struct thread_context)); |
| 3514 |
BLOSC_ERROR_NULL(context->thread_contexts, BLOSC2_ERROR_MEMORY_ALLOC); |
| 3515 |
for (tid = 0; tid < context->nthreads; tid++) |
| 3516 |
init_thread_context(context->thread_contexts + tid, context, tid); |
| 3517 |
} |
| 3518 |
else { |
| 3519 |
#if !defined(_WIN32) |
| 3520 |
/* Initialize and set thread detached attribute */ |
| 3521 |
pthread_attr_init(&context->ct_attr); |
| 3522 |
pthread_attr_setdetachstate(&context->ct_attr, PTHREAD_CREATE_JOINABLE); |
| 3523 |
#endif |
| 3524 |
|
| 3525 |
/* Make space for thread handlers */ |
| 3526 |
context->threads = (pthread_t*)my_malloc( |
| 3527 |
context->nthreads * sizeof(pthread_t)); |
| 3528 |
BLOSC_ERROR_NULL(context->threads, BLOSC2_ERROR_MEMORY_ALLOC); |
| 3529 |
/* Finally, create the threads */ |
| 3530 |
for (tid = 0; tid < context->nthreads; tid++) { |
| 3531 |
/* Create a thread context (will destroy when finished) */ |
| 3532 |
struct thread_context *thread_context = create_thread_context(context, tid); |
| 3533 |
BLOSC_ERROR_NULL(thread_context, BLOSC2_ERROR_THREAD_CREATE); |
| 3534 |
#if !defined(_WIN32) |
| 3535 |
rc2 = pthread_create(&context->threads[tid], &context->ct_attr, t_blosc, |
| 3536 |
(void*)thread_context); |
| 3537 |
#else |
| 3538 |
rc2 = pthread_create(&context->threads[tid], NULL, t_blosc, |
| 3539 |
(void *)thread_context); |
| 3540 |
#endif |
| 3541 |
if (rc2) { |
| 3542 |
BLOSC_TRACE_ERROR("Return code from pthread_create() is %d.\n" |
| 3543 |
"\tError detail: %s\n", rc2, strerror(rc2)); |
| 3544 |
return BLOSC2_ERROR_THREAD_CREATE; |
| 3545 |
} |
| 3546 |
} |
| 3547 |
} |
| 3548 |
|
| 3549 |
/* We have now started/initialized the threads */ |
| 3550 |
context->threads_started = context->nthreads; |
| 3551 |
context->new_nthreads = context->nthreads; |
| 3552 |
|
| 3553 |
return 0; |
| 3554 |
} |
| 3555 |
|
| 3556 |
int16_t blosc2_get_nthreads(void) |
| 3557 |
{ |
| 3558 |
return g_nthreads; |
| 3559 |
} |
| 3560 |
|
| 3561 |
int16_t blosc2_set_nthreads(int16_t nthreads) { |
| 3562 |
int16_t ret = g_nthreads; /* the previous number of threads */ |
| 3563 |
|
| 3564 |
/* Check whether the library should be initialized */ |
| 3565 |
if (!g_initlib) blosc2_init(); |
| 3566 |
|
| 3567 |
if (nthreads != ret) { |
| 3568 |
g_nthreads = nthreads; |
| 3569 |
g_global_context->new_nthreads = nthreads; |
| 3570 |
int16_t ret2 = check_nthreads(g_global_context); |
| 3571 |
if (ret2 < 0) { |
| 3572 |
return ret2; |
| 3573 |
} |
| 3574 |
} |
| 3575 |
|
| 3576 |
return ret; |
| 3577 |
} |
| 3578 |
|
| 3579 |
|
| 3580 |
const char* blosc1_get_compressor(void) |
| 3581 |
{ |
| 3582 |
const char* compname; |
| 3583 |
blosc2_compcode_to_compname(g_compressor, &compname); |
| 3584 |
|
| 3585 |
return compname; |
| 3586 |
} |
| 3587 |
|
| 3588 |
int blosc1_set_compressor(const char* compname) { |
| 3589 |
int code = blosc2_compname_to_compcode(compname); |
| 3590 |
if (code >= BLOSC_LAST_CODEC) { |
| 3591 |
BLOSC_TRACE_ERROR("User defined codecs cannot be set here. Use Blosc2 mechanism instead."); |
| 3592 |
BLOSC_ERROR(BLOSC2_ERROR_CODEC_SUPPORT); |
| 3593 |
} |
| 3594 |
g_compressor = code; |
| 3595 |
|
| 3596 |
/* Check whether the library should be initialized */ |
| 3597 |
if (!g_initlib) blosc2_init(); |
| 3598 |
|
| 3599 |
return code; |
| 3600 |
} |
| 3601 |
|
| 3602 |
void blosc2_set_delta(int dodelta) { |
| 3603 |
|
| 3604 |
g_delta = dodelta; |
| 3605 |
|
| 3606 |
/* Check whether the library should be initialized */ |
| 3607 |
if (!g_initlib) blosc2_init(); |
| 3608 |
|
| 3609 |
} |
| 3610 |
|
| 3611 |
const char* blosc2_list_compressors(void) { |
| 3612 |
static int compressors_list_done = 0; |
| 3613 |
static char ret[256]; |
| 3614 |
|
| 3615 |
if (compressors_list_done) return ret; |
| 3616 |
ret[0] = '\0'; |
| 3617 |
strcat(ret, BLOSC_BLOSCLZ_COMPNAME); |
| 3618 |
strcat(ret, ","); |
| 3619 |
strcat(ret, BLOSC_LZ4_COMPNAME); |
| 3620 |
strcat(ret, ","); |
| 3621 |
strcat(ret, BLOSC_LZ4HC_COMPNAME); |
| 3622 |
#if defined(HAVE_ZLIB) |
| 3623 |
strcat(ret, ","); |
| 3624 |
strcat(ret, BLOSC_ZLIB_COMPNAME); |
| 3625 |
#endif /* HAVE_ZLIB */ |
| 3626 |
#if defined(HAVE_ZSTD) |
| 3627 |
strcat(ret, ","); |
| 3628 |
strcat(ret, BLOSC_ZSTD_COMPNAME); |
| 3629 |
#endif /* HAVE_ZSTD */ |
| 3630 |
compressors_list_done = 1; |
| 3631 |
return ret; |
| 3632 |
} |
| 3633 |
|
| 3634 |
|
| 3635 |
const char* blosc2_get_version_string(void) { |
| 3636 |
return BLOSC2_VERSION_STRING; |
| 3637 |
} |
| 3638 |
|
| 3639 |
|
| 3640 |
int blosc2_get_complib_info(const char* compname, char** complib, char** version) { |
| 3641 |
int clibcode; |
| 3642 |
const char* clibname; |
| 3643 |
const char* clibversion = "unknown"; |
| 3644 |
char sbuffer[256]; |
| 3645 |
|
| 3646 |
clibcode = compname_to_clibcode(compname); |
| 3647 |
clibname = clibcode_to_clibname(clibcode); |
| 3648 |
|
| 3649 |
/* complib version */ |
| 3650 |
if (clibcode == BLOSC_BLOSCLZ_LIB) { |
| 3651 |
clibversion = BLOSCLZ_VERSION_STRING; |
| 3652 |
} |
| 3653 |
else if (clibcode == BLOSC_LZ4_LIB) { |
| 3654 |
sprintf(sbuffer, "%d.%d.%d", |
| 3655 |
LZ4_VERSION_MAJOR, LZ4_VERSION_MINOR, LZ4_VERSION_RELEASE); |
| 3656 |
clibversion = sbuffer; |
| 3657 |
} |
| 3658 |
#if defined(HAVE_ZLIB) |
| 3659 |
else if (clibcode == BLOSC_ZLIB_LIB) { |
| 3660 |
#ifdef ZLIB_COMPAT |
| 3661 |
clibversion = ZLIB_VERSION; |
| 3662 |
#elif defined(HAVE_ZLIB_NG) |
| 3663 |
clibversion = ZLIBNG_VERSION; |
| 3664 |
#else |
| 3665 |
clibversion = ZLIB_VERSION; |
| 3666 |
#endif |
| 3667 |
} |
| 3668 |
#endif /* HAVE_ZLIB */ |
| 3669 |
#if defined(HAVE_ZSTD) |
| 3670 |
else if (clibcode == BLOSC_ZSTD_LIB) { |
| 3671 |
sprintf(sbuffer, "%d.%d.%d", |
| 3672 |
ZSTD_VERSION_MAJOR, ZSTD_VERSION_MINOR, ZSTD_VERSION_RELEASE); |
| 3673 |
clibversion = sbuffer; |
| 3674 |
} |
| 3675 |
#endif /* HAVE_ZSTD */ |
| 3676 |
|
| 3677 |
#ifdef _MSC_VER |
| 3678 |
*complib = _strdup(clibname); |
| 3679 |
*version = _strdup(clibversion); |
| 3680 |
#else |
| 3681 |
*complib = strdup(clibname); |
| 3682 |
*version = strdup(clibversion); |
| 3683 |
#endif |
| 3684 |
return clibcode; |
| 3685 |
} |
| 3686 |
|
| 3687 |
/* Return `nbytes`, `cbytes` and `blocksize` from a compressed buffer. */ |
| 3688 |
void blosc1_cbuffer_sizes(const void* cbuffer, size_t* nbytes, size_t* cbytes, size_t* blocksize) { |
| 3689 |
int32_t nbytes32, cbytes32, blocksize32; |
| 3690 |
blosc2_cbuffer_sizes(cbuffer, &nbytes32, &cbytes32, &blocksize32); |
| 3691 |
*nbytes = nbytes32; |
| 3692 |
*cbytes = cbytes32; |
| 3693 |
*blocksize = blocksize32; |
| 3694 |
} |
| 3695 |
|
| 3696 |
int blosc2_cbuffer_sizes(const void* cbuffer, int32_t* nbytes, int32_t* cbytes, int32_t* blocksize) { |
| 3697 |
blosc_header header; |
| 3698 |
int rc = read_chunk_header((uint8_t *) cbuffer, BLOSC_MIN_HEADER_LENGTH, false, &header); |
| 3699 |
if (rc < 0) { |
| 3700 |
/* Return zeros if error reading header */ |
| 3701 |
memset(&header, 0, sizeof(header)); |
| 3702 |
} |
| 3703 |
|
| 3704 |
/* Read the interesting values */ |
| 3705 |
if (nbytes != NULL) |
| 3706 |
*nbytes = header.nbytes; |
| 3707 |
if (cbytes != NULL) |
| 3708 |
*cbytes = header.cbytes; |
| 3709 |
if (blocksize != NULL) |
| 3710 |
*blocksize = header.blocksize; |
| 3711 |
return rc; |
| 3712 |
} |
| 3713 |
|
| 3714 |
int blosc1_cbuffer_validate(const void* cbuffer, size_t cbytes, size_t* nbytes) { |
| 3715 |
int32_t header_cbytes; |
| 3716 |
int32_t header_nbytes; |
| 3717 |
if (cbytes < BLOSC_MIN_HEADER_LENGTH) { |
| 3718 |
/* Compressed data should contain enough space for header */ |
| 3719 |
*nbytes = 0; |
| 3720 |
return BLOSC2_ERROR_WRITE_BUFFER; |
| 3721 |
} |
| 3722 |
int rc = blosc2_cbuffer_sizes(cbuffer, &header_nbytes, &header_cbytes, NULL); |
| 3723 |
if (rc < 0) { |
| 3724 |
*nbytes = 0; |
| 3725 |
return rc; |
| 3726 |
} |
| 3727 |
*nbytes = header_nbytes; |
| 3728 |
if (header_cbytes != (int32_t)cbytes) { |
| 3729 |
/* Compressed size from header does not match `cbytes` */ |
| 3730 |
*nbytes = 0; |
| 3731 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 3732 |
} |
| 3733 |
if (*nbytes > BLOSC2_MAX_BUFFERSIZE) { |
| 3734 |
/* Uncompressed size is larger than allowed */ |
| 3735 |
*nbytes = 0; |
| 3736 |
return BLOSC2_ERROR_MEMORY_ALLOC; |
| 3737 |
} |
| 3738 |
return 0; |
| 3739 |
} |
| 3740 |
|
| 3741 |
/* Return `typesize` and `flags` from a compressed buffer. */ |
| 3742 |
void blosc1_cbuffer_metainfo(const void* cbuffer, size_t* typesize, int* flags) { |
| 3743 |
blosc_header header; |
| 3744 |
int rc = read_chunk_header((uint8_t *) cbuffer, BLOSC_MIN_HEADER_LENGTH, false, &header); |
| 3745 |
if (rc < 0) { |
| 3746 |
*typesize = *flags = 0; |
| 3747 |
return; |
| 3748 |
} |
| 3749 |
|
| 3750 |
/* Read the interesting values */ |
| 3751 |
*flags = header.flags; |
| 3752 |
*typesize = header.typesize; |
| 3753 |
} |
| 3754 |
|
| 3755 |
|
| 3756 |
/* Return version information from a compressed buffer. */ |
| 3757 |
void blosc2_cbuffer_versions(const void* cbuffer, int* version, int* versionlz) { |
| 3758 |
blosc_header header; |
| 3759 |
int rc = read_chunk_header((uint8_t *) cbuffer, BLOSC_MIN_HEADER_LENGTH, false, &header); |
| 3760 |
if (rc < 0) { |
| 3761 |
*version = *versionlz = 0; |
| 3762 |
return; |
| 3763 |
} |
| 3764 |
|
| 3765 |
/* Read the version info */ |
| 3766 |
*version = header.version; |
| 3767 |
*versionlz = header.versionlz; |
| 3768 |
} |
| 3769 |
|
| 3770 |
|
| 3771 |
/* Return the compressor library/format used in a compressed buffer. */ |
| 3772 |
const char* blosc2_cbuffer_complib(const void* cbuffer) { |
| 3773 |
blosc_header header; |
| 3774 |
int clibcode; |
| 3775 |
const char* complib; |
| 3776 |
int rc = read_chunk_header((uint8_t *) cbuffer, BLOSC_MIN_HEADER_LENGTH, false, &header); |
| 3777 |
if (rc < 0) { |
| 3778 |
return NULL; |
| 3779 |
} |
| 3780 |
|
| 3781 |
/* Read the compressor format/library info */ |
| 3782 |
clibcode = (header.flags & 0xe0) >> 5; |
| 3783 |
complib = clibcode_to_clibname(clibcode); |
| 3784 |
return complib; |
| 3785 |
} |
| 3786 |
|
| 3787 |
|
| 3788 |
/* Get the internal blocksize to be used during compression. 0 means |
| 3789 |
that an automatic blocksize is computed internally. */ |
| 3790 |
int blosc1_get_blocksize(void) |
| 3791 |
{ |
| 3792 |
return (int)g_force_blocksize; |
| 3793 |
} |
| 3794 |
|
| 3795 |
|
| 3796 |
/* Force the use of a specific blocksize. If 0, an automatic |
| 3797 |
blocksize will be used (the default). */ |
| 3798 |
void blosc1_set_blocksize(size_t blocksize) { |
| 3799 |
g_force_blocksize = (int32_t)blocksize; |
| 3800 |
} |
| 3801 |
|
| 3802 |
|
| 3803 |
/* Force the use of a specific split mode. */ |
| 3804 |
void blosc1_set_splitmode(int mode) |
| 3805 |
{ |
| 3806 |
g_splitmode = mode; |
| 3807 |
} |
| 3808 |
|
| 3809 |
|
| 3810 |
/* Set pointer to super-chunk. If NULL, no super-chunk will be |
| 3811 |
reachable (the default). */ |
| 3812 |
void blosc_set_schunk(blosc2_schunk* schunk) { |
| 3813 |
g_schunk = schunk; |
| 3814 |
g_global_context->schunk = schunk; |
| 3815 |
} |
| 3816 |
|
| 3817 |
blosc2_io *blosc2_io_global = NULL; |
| 3818 |
blosc2_io_cb BLOSC2_IO_CB_DEFAULTS; |
| 3819 |
blosc2_io_cb BLOSC2_IO_CB_MMAP; |
| 3820 |
|
| 3821 |
void blosc2_init(void) { |
| 3822 |
/* Return if Blosc is already initialized */ |
| 3823 |
if (g_initlib) return; |
| 3824 |
|
| 3825 |
BLOSC2_IO_CB_DEFAULTS.id = BLOSC2_IO_FILESYSTEM; |
| 3826 |
BLOSC2_IO_CB_DEFAULTS.name = "filesystem"; |
| 3827 |
BLOSC2_IO_CB_DEFAULTS.is_allocation_necessary = true; |
| 3828 |
BLOSC2_IO_CB_DEFAULTS.open = (blosc2_open_cb) blosc2_stdio_open; |
| 3829 |
BLOSC2_IO_CB_DEFAULTS.close = (blosc2_close_cb) blosc2_stdio_close; |
| 3830 |
BLOSC2_IO_CB_DEFAULTS.size = (blosc2_size_cb) blosc2_stdio_size; |
| 3831 |
BLOSC2_IO_CB_DEFAULTS.write = (blosc2_write_cb) blosc2_stdio_write; |
| 3832 |
BLOSC2_IO_CB_DEFAULTS.read = (blosc2_read_cb) blosc2_stdio_read; |
| 3833 |
BLOSC2_IO_CB_DEFAULTS.truncate = (blosc2_truncate_cb) blosc2_stdio_truncate; |
| 3834 |
BLOSC2_IO_CB_DEFAULTS.destroy = (blosc2_destroy_cb) blosc2_stdio_destroy; |
| 3835 |
|
| 3836 |
BLOSC2_IO_CB_MMAP.id = BLOSC2_IO_FILESYSTEM_MMAP; |
| 3837 |
BLOSC2_IO_CB_MMAP.name = "filesystem_mmap"; |
| 3838 |
BLOSC2_IO_CB_MMAP.is_allocation_necessary = false; |
| 3839 |
BLOSC2_IO_CB_MMAP.open = (blosc2_open_cb) blosc2_stdio_mmap_open; |
| 3840 |
BLOSC2_IO_CB_MMAP.close = (blosc2_close_cb) blosc2_stdio_mmap_close; |
| 3841 |
BLOSC2_IO_CB_MMAP.read = (blosc2_read_cb) blosc2_stdio_mmap_read; |
| 3842 |
BLOSC2_IO_CB_MMAP.size = (blosc2_size_cb) blosc2_stdio_mmap_size; |
| 3843 |
BLOSC2_IO_CB_MMAP.write = (blosc2_write_cb) blosc2_stdio_mmap_write; |
| 3844 |
BLOSC2_IO_CB_MMAP.truncate = (blosc2_truncate_cb) blosc2_stdio_mmap_truncate; |
| 3845 |
BLOSC2_IO_CB_MMAP.destroy = (blosc2_destroy_cb) blosc2_stdio_mmap_destroy; |
| 3846 |
|
| 3847 |
g_ncodecs = 0; |
| 3848 |
g_nfilters = 0; |
| 3849 |
g_ntuners = 0; |
| 3850 |
|
| 3851 |
#if defined(HAVE_PLUGINS) |
| 3852 |
#include "blosc2/blosc2-common.h" |
| 3853 |
#include "blosc2/blosc2-stdio.h" |
| 3854 |
register_codecs(); |
| 3855 |
register_filters(); |
| 3856 |
register_tuners(); |
| 3857 |
#endif |
| 3858 |
pthread_mutex_init(&global_comp_mutex, NULL); |
| 3859 |
/* Create a global context */ |
| 3860 |
g_global_context = (blosc2_context*)my_malloc(sizeof(blosc2_context)); |
| 3861 |
memset(g_global_context, 0, sizeof(blosc2_context)); |
| 3862 |
g_global_context->nthreads = g_nthreads; |
| 3863 |
g_global_context->new_nthreads = g_nthreads; |
| 3864 |
g_initlib = 1; |
| 3865 |
} |
| 3866 |
|
| 3867 |
|
| 3868 |
int blosc2_free_resources(void) { |
| 3869 |
/* Return if Blosc is not initialized */ |
| 3870 |
if (!g_initlib) return BLOSC2_ERROR_FAILURE; |
| 3871 |
|
| 3872 |
return release_threadpool(g_global_context); |
| 3873 |
} |
| 3874 |
|
| 3875 |
|
| 3876 |
void blosc2_destroy(void) { |
| 3877 |
/* Return if Blosc is not initialized */ |
| 3878 |
if (!g_initlib) return; |
| 3879 |
|
| 3880 |
blosc2_free_resources(); |
| 3881 |
g_initlib = 0; |
| 3882 |
blosc2_free_ctx(g_global_context); |
| 3883 |
|
| 3884 |
pthread_mutex_destroy(&global_comp_mutex); |
| 3885 |
|
| 3886 |
} |
| 3887 |
|
| 3888 |
|
| 3889 |
int release_threadpool(blosc2_context *context) { |
| 3890 |
int32_t t; |
| 3891 |
void* status; |
| 3892 |
int rc; |
| 3893 |
|
| 3894 |
if (context->threads_started > 0) { |
| 3895 |
if (threads_callback) { |
| 3896 |
/* free context data for user-managed threads */ |
| 3897 |
for (t=0; t<context->threads_started; t++) |
| 3898 |
destroy_thread_context(context->thread_contexts + t); |
| 3899 |
my_free(context->thread_contexts); |
| 3900 |
} |
| 3901 |
else { |
| 3902 |
/* Tell all existing threads to finish */ |
| 3903 |
context->end_threads = 1; |
| 3904 |
WAIT_INIT(-1, context); |
| 3905 |
|
| 3906 |
/* Join exiting threads */ |
| 3907 |
for (t = 0; t < context->threads_started; t++) { |
| 3908 |
rc = pthread_join(context->threads[t], &status); |
| 3909 |
if (rc) { |
| 3910 |
BLOSC_TRACE_ERROR("Return code from pthread_join() is %d\n" |
| 3911 |
"\tError detail: %s.", rc, strerror(rc)); |
| 3912 |
} |
| 3913 |
} |
| 3914 |
|
| 3915 |
/* Thread attributes */ |
| 3916 |
#if !defined(_WIN32) |
| 3917 |
pthread_attr_destroy(&context->ct_attr); |
| 3918 |
#endif |
| 3919 |
|
| 3920 |
/* Release thread handlers */ |
| 3921 |
my_free(context->threads); |
| 3922 |
} |
| 3923 |
|
| 3924 |
/* Release mutex and condition variable objects */ |
| 3925 |
pthread_mutex_destroy(&context->count_mutex); |
| 3926 |
pthread_mutex_destroy(&context->delta_mutex); |
| 3927 |
pthread_mutex_destroy(&context->nchunk_mutex); |
| 3928 |
pthread_cond_destroy(&context->delta_cv); |
| 3929 |
|
| 3930 |
/* Barriers */ |
| 3931 |
#ifdef BLOSC_POSIX_BARRIERS |
| 3932 |
pthread_barrier_destroy(&context->barr_init); |
| 3933 |
pthread_barrier_destroy(&context->barr_finish); |
| 3934 |
#else |
| 3935 |
pthread_mutex_destroy(&context->count_threads_mutex); |
| 3936 |
pthread_cond_destroy(&context->count_threads_cv); |
| 3937 |
context->count_threads = 0; /* Reset threads counter */ |
| 3938 |
#endif |
| 3939 |
|
| 3940 |
/* Reset flags and counters */ |
| 3941 |
context->end_threads = 0; |
| 3942 |
context->threads_started = 0; |
| 3943 |
} |
| 3944 |
|
| 3945 |
|
| 3946 |
return 0; |
| 3947 |
} |
| 3948 |
|
| 3949 |
|
| 3950 |
/* Contexts */ |
| 3951 |
|
| 3952 |
/* Create a context for compression */ |
| 3953 |
blosc2_context* blosc2_create_cctx(blosc2_cparams cparams) { |
| 3954 |
blosc2_context* context = (blosc2_context*)my_malloc(sizeof(blosc2_context)); |
| 3955 |
BLOSC_ERROR_NULL(context, NULL); |
| 3956 |
|
| 3957 |
/* Populate the context, using zeros as default values */ |
| 3958 |
memset(context, 0, sizeof(blosc2_context)); |
| 3959 |
context->do_compress = 1; /* meant for compression */ |
| 3960 |
context->use_dict = cparams.use_dict; |
| 3961 |
if (cparams.instr_codec) { |
| 3962 |
context->blosc2_flags = BLOSC2_INSTR_CODEC; |
| 3963 |
} |
| 3964 |
|
| 3965 |
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
| 3966 |
context->filters[i] = cparams.filters[i]; |
| 3967 |
context->filters_meta[i] = cparams.filters_meta[i]; |
| 3968 |
|
| 3969 |
if (context->filters[i] >= BLOSC_LAST_FILTER && context->filters[i] <= BLOSC2_DEFINED_FILTERS_STOP) { |
| 3970 |
BLOSC_TRACE_ERROR("filter (%d) is not yet defined", |
| 3971 |
context->filters[i]); |
| 3972 |
free(context); |
| 3973 |
return NULL; |
| 3974 |
} |
| 3975 |
if (context->filters[i] > BLOSC_LAST_REGISTERED_FILTER && context->filters[i] <= BLOSC2_GLOBAL_REGISTERED_FILTERS_STOP) { |
| 3976 |
BLOSC_TRACE_ERROR("filter (%d) is not yet defined", |
| 3977 |
context->filters[i]); |
| 3978 |
free(context); |
| 3979 |
return NULL; |
| 3980 |
} |
| 3981 |
} |
| 3982 |
|
| 3983 |
#if defined(HAVE_PLUGINS) |
| 3984 |
#include "blosc2/codecs-registry.h" |
| 3985 |
if ((context->compcode >= BLOSC_CODEC_ZFP_FIXED_ACCURACY) && (context->compcode <= BLOSC_CODEC_ZFP_FIXED_RATE)) { |
| 3986 |
for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) { |
| 3987 |
if ((context->filters[i] == BLOSC_SHUFFLE) || (context->filters[i] == BLOSC_BITSHUFFLE)) { |
| 3988 |
BLOSC_TRACE_ERROR("ZFP cannot be run in presence of SHUFFLE / BITSHUFFLE"); |
| 3989 |
return NULL; |
| 3990 |
} |
| 3991 |
} |
| 3992 |
} |
| 3993 |
#endif /* HAVE_PLUGINS */ |
| 3994 |
|
| 3995 |
/* Check for a BLOSC_SHUFFLE environment variable */ |
| 3996 |
int doshuffle = -1; |
| 3997 |
char* envvar = getenv("BLOSC_SHUFFLE"); |
| 3998 |
if (envvar != NULL) { |
| 3999 |
if (strcmp(envvar, "NOSHUFFLE") == 0) { |
| 4000 |
doshuffle = BLOSC_NOSHUFFLE; |
| 4001 |
} |
| 4002 |
else if (strcmp(envvar, "SHUFFLE") == 0) { |
| 4003 |
doshuffle = BLOSC_SHUFFLE; |
| 4004 |
} |
| 4005 |
else if (strcmp(envvar, "BITSHUFFLE") == 0) { |
| 4006 |
doshuffle = BLOSC_BITSHUFFLE; |
| 4007 |
} |
| 4008 |
else { |
| 4009 |
BLOSC_TRACE_WARNING("BLOSC_SHUFFLE environment variable '%s' not recognized\n", envvar); |
| 4010 |
} |
| 4011 |
} |
| 4012 |
/* Check for a BLOSC_DELTA environment variable */ |
| 4013 |
int dodelta = BLOSC_NOFILTER; |
| 4014 |
envvar = getenv("BLOSC_DELTA"); |
| 4015 |
if (envvar != NULL) { |
| 4016 |
if (strcmp(envvar, "1") == 0) { |
| 4017 |
dodelta = BLOSC_DELTA; |
| 4018 |
} else if (strcmp(envvar, "0") == 0){ |
| 4019 |
dodelta = BLOSC_NOFILTER; |
| 4020 |
} |
| 4021 |
else { |
| 4022 |
BLOSC_TRACE_WARNING("BLOSC_DELTA environment variable '%s' not recognized\n", envvar); |
| 4023 |
} |
| 4024 |
} |
| 4025 |
/* Check for a BLOSC_TYPESIZE environment variable */ |
| 4026 |
context->typesize = cparams.typesize; |
| 4027 |
envvar = getenv("BLOSC_TYPESIZE"); |
| 4028 |
if (envvar != NULL) { |
| 4029 |
int32_t value; |
| 4030 |
value = (int32_t) strtol(envvar, NULL, 10); |
| 4031 |
if ((errno != EINVAL) && (value > 0)) { |
| 4032 |
context->typesize = value; |
| 4033 |
} |
| 4034 |
else { |
| 4035 |
BLOSC_TRACE_WARNING("BLOSC_TYPESIZE environment variable '%s' not recognized\n", envvar); |
| 4036 |
} |
| 4037 |
} |
| 4038 |
build_filters(doshuffle, dodelta, context->typesize, context->filters); |
| 4039 |
|
| 4040 |
context->clevel = cparams.clevel; |
| 4041 |
/* Check for a BLOSC_CLEVEL environment variable */ |
| 4042 |
envvar = getenv("BLOSC_CLEVEL"); |
| 4043 |
if (envvar != NULL) { |
| 4044 |
int value; |
| 4045 |
value = (int)strtol(envvar, NULL, 10); |
| 4046 |
if ((errno != EINVAL) && (value >= 0)) { |
| 4047 |
context->clevel = value; |
| 4048 |
} |
| 4049 |
else { |
| 4050 |
BLOSC_TRACE_WARNING("BLOSC_CLEVEL environment variable '%s' not recognized\n", envvar); |
| 4051 |
} |
| 4052 |
} |
| 4053 |
|
| 4054 |
context->compcode = cparams.compcode; |
| 4055 |
/* Check for a BLOSC_COMPRESSOR environment variable */ |
| 4056 |
envvar = getenv("BLOSC_COMPRESSOR"); |
| 4057 |
if (envvar != NULL) { |
| 4058 |
int codec = blosc2_compname_to_compcode(envvar); |
| 4059 |
if (codec >= BLOSC_LAST_CODEC) { |
| 4060 |
BLOSC_TRACE_ERROR("User defined codecs cannot be set here. Use Blosc2 mechanism instead."); |
| 4061 |
return NULL; |
| 4062 |
} |
| 4063 |
context->compcode = codec; |
| 4064 |
} |
| 4065 |
context->compcode_meta = cparams.compcode_meta; |
| 4066 |
|
| 4067 |
context->blocksize = cparams.blocksize; |
| 4068 |
/* Check for a BLOSC_BLOCKSIZE environment variable */ |
| 4069 |
envvar = getenv("BLOSC_BLOCKSIZE"); |
| 4070 |
if (envvar != NULL) { |
| 4071 |
int32_t blocksize; |
| 4072 |
blocksize = (int32_t) strtol(envvar, NULL, 10); |
| 4073 |
if ((errno != EINVAL) && (blocksize > 0)) { |
| 4074 |
context->blocksize = blocksize; |
| 4075 |
} |
| 4076 |
else { |
| 4077 |
BLOSC_TRACE_WARNING("BLOSC_BLOCKSIZE environment variable '%s' not recognized\n", envvar); |
| 4078 |
} |
| 4079 |
} |
| 4080 |
|
| 4081 |
context->nthreads = cparams.nthreads; |
| 4082 |
/* Check for a BLOSC_NTHREADS environment variable */ |
| 4083 |
envvar = getenv("BLOSC_NTHREADS"); |
| 4084 |
if (envvar != NULL) { |
| 4085 |
int16_t nthreads = (int16_t) strtol(envvar, NULL, 10); |
| 4086 |
if ((errno != EINVAL) && (nthreads > 0)) { |
| 4087 |
context->nthreads = nthreads; |
| 4088 |
} |
| 4089 |
else { |
| 4090 |
BLOSC_TRACE_WARNING("BLOSC_NTHREADS environment variable '%s' not recognized\n", envvar); |
| 4091 |
} |
| 4092 |
} |
| 4093 |
context->new_nthreads = context->nthreads; |
| 4094 |
|
| 4095 |
context->splitmode = cparams.splitmode; |
| 4096 |
/* Check for a BLOSC_SPLITMODE environment variable */ |
| 4097 |
envvar = getenv("BLOSC_SPLITMODE"); |
| 4098 |
if (envvar != NULL) { |
| 4099 |
int32_t splitmode = -1; |
| 4100 |
if (strcmp(envvar, "ALWAYS") == 0) { |
| 4101 |
splitmode = BLOSC_ALWAYS_SPLIT; |
| 4102 |
} |
| 4103 |
else if (strcmp(envvar, "NEVER") == 0) { |
| 4104 |
splitmode = BLOSC_NEVER_SPLIT; |
| 4105 |
} |
| 4106 |
else if (strcmp(envvar, "AUTO") == 0) { |
| 4107 |
splitmode = BLOSC_AUTO_SPLIT; |
| 4108 |
} |
| 4109 |
else if (strcmp(envvar, "FORWARD_COMPAT") == 0) { |
| 4110 |
splitmode = BLOSC_FORWARD_COMPAT_SPLIT; |
| 4111 |
} |
| 4112 |
else { |
| 4113 |
BLOSC_TRACE_WARNING("BLOSC_SPLITMODE environment variable '%s' not recognized\n", envvar); |
| 4114 |
} |
| 4115 |
if (splitmode >= 0) { |
| 4116 |
context->splitmode = splitmode; |
| 4117 |
} |
| 4118 |
} |
| 4119 |
|
| 4120 |
context->threads_started = 0; |
| 4121 |
context->schunk = cparams.schunk; |
| 4122 |
|
| 4123 |
if (cparams.prefilter != NULL) { |
| 4124 |
context->prefilter = cparams.prefilter; |
| 4125 |
context->preparams = (blosc2_prefilter_params*)my_malloc(sizeof(blosc2_prefilter_params)); |
| 4126 |
BLOSC_ERROR_NULL(context->preparams, NULL); |
| 4127 |
memcpy(context->preparams, cparams.preparams, sizeof(blosc2_prefilter_params)); |
| 4128 |
} |
| 4129 |
|
| 4130 |
if (cparams.tuner_id <= 0) { |
| 4131 |
cparams.tuner_id = g_tuner; |
| 4132 |
} else { |
| 4133 |
for (int i = 0; i < g_ntuners; ++i) { |
| 4134 |
if (g_tuners[i].id == cparams.tuner_id) { |
| 4135 |
if (g_tuners[i].init == NULL) { |
| 4136 |
if (fill_tuner(&g_tuners[i]) < 0) { |
| 4137 |
BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
| 4138 |
return NULL; |
| 4139 |
} |
| 4140 |
} |
| 4141 |
if (g_tuners[i].init(cparams.tuner_params, context, NULL) < 0) { |
| 4142 |
BLOSC_TRACE_ERROR("Error in user-defined tuner %d init function\n", cparams.tuner_id); |
| 4143 |
return NULL; |
| 4144 |
} |
| 4145 |
goto urtunersuccess; |
| 4146 |
} |
| 4147 |
} |
| 4148 |
BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", cparams.tuner_id); |
| 4149 |
return NULL; |
| 4150 |
} |
| 4151 |
urtunersuccess:; |
| 4152 |
|
| 4153 |
context->tuner_id = cparams.tuner_id; |
| 4154 |
|
| 4155 |
context->codec_params = cparams.codec_params; |
| 4156 |
memcpy(context->filter_params, cparams.filter_params, BLOSC2_MAX_FILTERS * sizeof(void*)); |
| 4157 |
|
| 4158 |
return context; |
| 4159 |
} |
| 4160 |
|
| 4161 |
/* Create a context for decompression */ |
| 4162 |
blosc2_context* blosc2_create_dctx(blosc2_dparams dparams) { |
| 4163 |
blosc2_context* context = (blosc2_context*)my_malloc(sizeof(blosc2_context)); |
| 4164 |
BLOSC_ERROR_NULL(context, NULL); |
| 4165 |
|
| 4166 |
/* Populate the context, using zeros as default values */ |
| 4167 |
memset(context, 0, sizeof(blosc2_context)); |
| 4168 |
context->do_compress = 0; /* Meant for decompression */ |
| 4169 |
|
| 4170 |
context->nthreads = dparams.nthreads; |
| 4171 |
char* envvar = getenv("BLOSC_NTHREADS"); |
| 4172 |
if (envvar != NULL) { |
| 4173 |
long nthreads = strtol(envvar, NULL, 10); |
| 4174 |
if ((errno != EINVAL) && (nthreads > 0)) { |
| 4175 |
context->nthreads = (int16_t) nthreads; |
| 4176 |
} |
| 4177 |
} |
| 4178 |
context->new_nthreads = context->nthreads; |
| 4179 |
|
| 4180 |
context->threads_started = 0; |
| 4181 |
context->block_maskout = NULL; |
| 4182 |
context->block_maskout_nitems = 0; |
| 4183 |
context->schunk = dparams.schunk; |
| 4184 |
|
| 4185 |
if (dparams.postfilter != NULL) { |
| 4186 |
context->postfilter = dparams.postfilter; |
| 4187 |
context->postparams = (blosc2_postfilter_params*)my_malloc(sizeof(blosc2_postfilter_params)); |
| 4188 |
BLOSC_ERROR_NULL(context->postparams, NULL); |
| 4189 |
memcpy(context->postparams, dparams.postparams, sizeof(blosc2_postfilter_params)); |
| 4190 |
} |
| 4191 |
|
| 4192 |
return context; |
| 4193 |
} |
| 4194 |
|
| 4195 |
|
| 4196 |
void blosc2_free_ctx(blosc2_context* context) { |
| 4197 |
release_threadpool(context); |
| 4198 |
if (context->serial_context != NULL) { |
| 4199 |
free_thread_context(context->serial_context); |
| 4200 |
} |
| 4201 |
if (context->dict_cdict != NULL) { |
| 4202 |
#ifdef HAVE_ZSTD |
| 4203 |
ZSTD_freeCDict(context->dict_cdict); |
| 4204 |
#endif |
| 4205 |
} |
| 4206 |
if (context->dict_ddict != NULL) { |
| 4207 |
#ifdef HAVE_ZSTD |
| 4208 |
ZSTD_freeDDict(context->dict_ddict); |
| 4209 |
#endif |
| 4210 |
} |
| 4211 |
if (context->tuner_params != NULL) { |
| 4212 |
int rc; |
| 4213 |
if (context->tuner_id < BLOSC_LAST_TUNER && context->tuner_id == BLOSC_STUNE) { |
| 4214 |
rc = blosc_stune_free(context); |
| 4215 |
} else { |
| 4216 |
for (int i = 0; i < g_ntuners; ++i) { |
| 4217 |
if (g_tuners[i].id == context->tuner_id) { |
| 4218 |
if (g_tuners[i].free == NULL) { |
| 4219 |
if (fill_tuner(&g_tuners[i]) < 0) { |
| 4220 |
BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
| 4221 |
return; |
| 4222 |
} |
| 4223 |
} |
| 4224 |
rc = g_tuners[i].free(context); |
| 4225 |
goto urtunersuccess; |
| 4226 |
} |
| 4227 |
} |
| 4228 |
BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", context->tuner_id); |
| 4229 |
return; |
| 4230 |
urtunersuccess:; |
| 4231 |
} |
| 4232 |
if (rc < 0) { |
| 4233 |
BLOSC_TRACE_ERROR("Error in user-defined tuner free function\n"); |
| 4234 |
return; |
| 4235 |
} |
| 4236 |
} |
| 4237 |
if (context->prefilter != NULL) { |
| 4238 |
my_free(context->preparams); |
| 4239 |
} |
| 4240 |
if (context->postfilter != NULL) { |
| 4241 |
my_free(context->postparams); |
| 4242 |
} |
| 4243 |
|
| 4244 |
if (context->block_maskout != NULL) { |
| 4245 |
free(context->block_maskout); |
| 4246 |
} |
| 4247 |
my_free(context); |
| 4248 |
} |
| 4249 |
|
| 4250 |
|
| 4251 |
int blosc2_ctx_get_cparams(blosc2_context *ctx, blosc2_cparams *cparams) { |
| 4252 |
cparams->compcode = ctx->compcode; |
| 4253 |
cparams->compcode_meta = ctx->compcode_meta; |
| 4254 |
cparams->clevel = ctx->clevel; |
| 4255 |
cparams->use_dict = ctx->use_dict; |
| 4256 |
cparams->instr_codec = ctx->blosc2_flags & BLOSC2_INSTR_CODEC; |
| 4257 |
cparams->typesize = ctx->typesize; |
| 4258 |
cparams->nthreads = ctx->nthreads; |
| 4259 |
cparams->blocksize = ctx->blocksize; |
| 4260 |
cparams->splitmode = ctx->splitmode; |
| 4261 |
cparams->schunk = ctx->schunk; |
| 4262 |
for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) { |
| 4263 |
cparams->filters[i] = ctx->filters[i]; |
| 4264 |
cparams->filters_meta[i] = ctx->filters_meta[i]; |
| 4265 |
} |
| 4266 |
cparams->prefilter = ctx->prefilter; |
| 4267 |
cparams->preparams = ctx->preparams; |
| 4268 |
cparams->tuner_id = ctx->tuner_id; |
| 4269 |
cparams->codec_params = ctx->codec_params; |
| 4270 |
|
| 4271 |
return BLOSC2_ERROR_SUCCESS; |
| 4272 |
} |
| 4273 |
|
| 4274 |
|
| 4275 |
int blosc2_ctx_get_dparams(blosc2_context *ctx, blosc2_dparams *dparams) { |
| 4276 |
dparams->nthreads = ctx->nthreads; |
| 4277 |
dparams->schunk = ctx->schunk; |
| 4278 |
dparams->postfilter = ctx->postfilter; |
| 4279 |
dparams->postparams = ctx->postparams; |
| 4280 |
|
| 4281 |
return BLOSC2_ERROR_SUCCESS; |
| 4282 |
} |
| 4283 |
|
| 4284 |
|
| 4285 |
/* Set a maskout in decompression context */ |
| 4286 |
int blosc2_set_maskout(blosc2_context *ctx, bool *maskout, int nblocks) { |
| 4287 |
|
| 4288 |
if (ctx->block_maskout != NULL) { |
| 4289 |
// Get rid of a possible mask here |
| 4290 |
free(ctx->block_maskout); |
| 4291 |
} |
| 4292 |
|
| 4293 |
bool *maskout_ = malloc(nblocks); |
| 4294 |
BLOSC_ERROR_NULL(maskout_, BLOSC2_ERROR_MEMORY_ALLOC); |
| 4295 |
memcpy(maskout_, maskout, nblocks); |
| 4296 |
ctx->block_maskout = maskout_; |
| 4297 |
ctx->block_maskout_nitems = nblocks; |
| 4298 |
|
| 4299 |
return 0; |
| 4300 |
} |
| 4301 |
|
| 4302 |
|
| 4303 |
/* Create a chunk made of zeros */ |
| 4304 |
int blosc2_chunk_zeros(blosc2_cparams cparams, const int32_t nbytes, void* dest, int32_t destsize) { |
| 4305 |
if (destsize < BLOSC_EXTENDED_HEADER_LENGTH) { |
| 4306 |
BLOSC_TRACE_ERROR("dest buffer is not long enough"); |
| 4307 |
return BLOSC2_ERROR_DATA; |
| 4308 |
} |
| 4309 |
|
| 4310 |
if (nbytes % cparams.typesize) { |
| 4311 |
BLOSC_TRACE_ERROR("nbytes must be a multiple of typesize"); |
| 4312 |
return BLOSC2_ERROR_DATA; |
| 4313 |
} |
| 4314 |
|
| 4315 |
blosc_header header; |
| 4316 |
blosc2_context* context = blosc2_create_cctx(cparams); |
| 4317 |
if (context == NULL) { |
| 4318 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 4319 |
return BLOSC2_ERROR_NULL_POINTER; |
| 4320 |
} |
| 4321 |
|
| 4322 |
int error = initialize_context_compression( |
| 4323 |
context, NULL, nbytes, dest, destsize, |
| 4324 |
context->clevel, context->filters, context->filters_meta, |
| 4325 |
context->typesize, context->compcode, context->blocksize, |
| 4326 |
context->new_nthreads, context->nthreads, context->splitmode, |
| 4327 |
context->tuner_id, context->tuner_params, context->schunk); |
| 4328 |
if (error <= 0) { |
| 4329 |
blosc2_free_ctx(context); |
| 4330 |
return error; |
| 4331 |
} |
| 4332 |
|
| 4333 |
memset(&header, 0, sizeof(header)); |
| 4334 |
header.version = BLOSC2_VERSION_FORMAT; |
| 4335 |
header.versionlz = BLOSC_BLOSCLZ_VERSION_FORMAT; |
| 4336 |
header.flags = BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE; // extended header |
| 4337 |
header.typesize = context->typesize; |
| 4338 |
header.nbytes = (int32_t)nbytes; |
| 4339 |
header.blocksize = context->blocksize; |
| 4340 |
header.cbytes = BLOSC_EXTENDED_HEADER_LENGTH; |
| 4341 |
header.blosc2_flags = BLOSC2_SPECIAL_ZERO << 4; // mark chunk as all zeros |
| 4342 |
memcpy((uint8_t *)dest, &header, sizeof(header)); |
| 4343 |
|
| 4344 |
blosc2_free_ctx(context); |
| 4345 |
|
| 4346 |
return BLOSC_EXTENDED_HEADER_LENGTH; |
| 4347 |
} |
| 4348 |
|
| 4349 |
|
| 4350 |
/* Create a chunk made of uninitialized values */ |
| 4351 |
int blosc2_chunk_uninit(blosc2_cparams cparams, const int32_t nbytes, void* dest, int32_t destsize) { |
| 4352 |
if (destsize < BLOSC_EXTENDED_HEADER_LENGTH) { |
| 4353 |
BLOSC_TRACE_ERROR("dest buffer is not long enough"); |
| 4354 |
return BLOSC2_ERROR_DATA; |
| 4355 |
} |
| 4356 |
|
| 4357 |
if (nbytes % cparams.typesize) { |
| 4358 |
BLOSC_TRACE_ERROR("nbytes must be a multiple of typesize"); |
| 4359 |
return BLOSC2_ERROR_DATA; |
| 4360 |
} |
| 4361 |
|
| 4362 |
blosc_header header; |
| 4363 |
blosc2_context* context = blosc2_create_cctx(cparams); |
| 4364 |
if (context == NULL) { |
| 4365 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 4366 |
return BLOSC2_ERROR_NULL_POINTER; |
| 4367 |
} |
| 4368 |
int error = initialize_context_compression( |
| 4369 |
context, NULL, nbytes, dest, destsize, |
| 4370 |
context->clevel, context->filters, context->filters_meta, |
| 4371 |
context->typesize, context->compcode, context->blocksize, |
| 4372 |
context->new_nthreads, context->nthreads, context->splitmode, |
| 4373 |
context->tuner_id, context->tuner_params, context->schunk); |
| 4374 |
if (error <= 0) { |
| 4375 |
blosc2_free_ctx(context); |
| 4376 |
return error; |
| 4377 |
} |
| 4378 |
|
| 4379 |
memset(&header, 0, sizeof(header)); |
| 4380 |
header.version = BLOSC2_VERSION_FORMAT; |
| 4381 |
header.versionlz = BLOSC_BLOSCLZ_VERSION_FORMAT; |
| 4382 |
header.flags = BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE; // extended header |
| 4383 |
header.typesize = context->typesize; |
| 4384 |
header.nbytes = (int32_t)nbytes; |
| 4385 |
header.blocksize = context->blocksize; |
| 4386 |
header.cbytes = BLOSC_EXTENDED_HEADER_LENGTH; |
| 4387 |
header.blosc2_flags = BLOSC2_SPECIAL_UNINIT << 4; // mark chunk as uninitialized |
| 4388 |
memcpy((uint8_t *)dest, &header, sizeof(header)); |
| 4389 |
|
| 4390 |
blosc2_free_ctx(context); |
| 4391 |
|
| 4392 |
return BLOSC_EXTENDED_HEADER_LENGTH; |
| 4393 |
} |
| 4394 |
|
| 4395 |
|
| 4396 |
/* Create a chunk made of nans */ |
| 4397 |
int blosc2_chunk_nans(blosc2_cparams cparams, const int32_t nbytes, void* dest, int32_t destsize) { |
| 4398 |
if (destsize < BLOSC_EXTENDED_HEADER_LENGTH) { |
| 4399 |
BLOSC_TRACE_ERROR("dest buffer is not long enough"); |
| 4400 |
return BLOSC2_ERROR_DATA; |
| 4401 |
} |
| 4402 |
|
| 4403 |
if (nbytes % cparams.typesize) { |
| 4404 |
BLOSC_TRACE_ERROR("nbytes must be a multiple of typesize"); |
| 4405 |
return BLOSC2_ERROR_DATA; |
| 4406 |
} |
| 4407 |
|
| 4408 |
blosc_header header; |
| 4409 |
blosc2_context* context = blosc2_create_cctx(cparams); |
| 4410 |
if (context == NULL) { |
| 4411 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 4412 |
return BLOSC2_ERROR_NULL_POINTER; |
| 4413 |
} |
| 4414 |
|
| 4415 |
int error = initialize_context_compression( |
| 4416 |
context, NULL, nbytes, dest, destsize, |
| 4417 |
context->clevel, context->filters, context->filters_meta, |
| 4418 |
context->typesize, context->compcode, context->blocksize, |
| 4419 |
context->new_nthreads, context->nthreads, context->splitmode, |
| 4420 |
context->tuner_id, context->tuner_params, context->schunk); |
| 4421 |
if (error <= 0) { |
| 4422 |
blosc2_free_ctx(context); |
| 4423 |
return error; |
| 4424 |
} |
| 4425 |
|
| 4426 |
memset(&header, 0, sizeof(header)); |
| 4427 |
header.version = BLOSC2_VERSION_FORMAT; |
| 4428 |
header.versionlz = BLOSC_BLOSCLZ_VERSION_FORMAT; |
| 4429 |
header.flags = BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE; // extended header |
| 4430 |
header.typesize = context->typesize; |
| 4431 |
header.nbytes = (int32_t)nbytes; |
| 4432 |
header.blocksize = context->blocksize; |
| 4433 |
header.cbytes = BLOSC_EXTENDED_HEADER_LENGTH; |
| 4434 |
header.blosc2_flags = BLOSC2_SPECIAL_NAN << 4; // mark chunk as all NaNs |
| 4435 |
memcpy((uint8_t *)dest, &header, sizeof(header)); |
| 4436 |
|
| 4437 |
blosc2_free_ctx(context); |
| 4438 |
|
| 4439 |
return BLOSC_EXTENDED_HEADER_LENGTH; |
| 4440 |
} |
| 4441 |
|
| 4442 |
|
| 4443 |
/* Create a chunk made of repeated values */ |
| 4444 |
int blosc2_chunk_repeatval(blosc2_cparams cparams, const int32_t nbytes, |
| 4445 |
void* dest, int32_t destsize, const void* repeatval) { |
| 4446 |
if (destsize < BLOSC_EXTENDED_HEADER_LENGTH + cparams.typesize) { |
| 4447 |
BLOSC_TRACE_ERROR("dest buffer is not long enough"); |
| 4448 |
return BLOSC2_ERROR_DATA; |
| 4449 |
} |
| 4450 |
|
| 4451 |
if (nbytes % cparams.typesize) { |
| 4452 |
BLOSC_TRACE_ERROR("nbytes must be a multiple of typesize"); |
| 4453 |
return BLOSC2_ERROR_DATA; |
| 4454 |
} |
| 4455 |
|
| 4456 |
blosc_header header; |
| 4457 |
blosc2_context* context = blosc2_create_cctx(cparams); |
| 4458 |
if (context == NULL) { |
| 4459 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 4460 |
return BLOSC2_ERROR_NULL_POINTER; |
| 4461 |
} |
| 4462 |
|
| 4463 |
int error = initialize_context_compression( |
| 4464 |
context, NULL, nbytes, dest, destsize, |
| 4465 |
context->clevel, context->filters, context->filters_meta, |
| 4466 |
context->typesize, context->compcode, context->blocksize, |
| 4467 |
context->new_nthreads, context->nthreads, context->splitmode, |
| 4468 |
context->tuner_id, context->tuner_params, context->schunk); |
| 4469 |
if (error <= 0) { |
| 4470 |
blosc2_free_ctx(context); |
| 4471 |
return error; |
| 4472 |
} |
| 4473 |
|
| 4474 |
memset(&header, 0, sizeof(header)); |
| 4475 |
header.version = BLOSC2_VERSION_FORMAT; |
| 4476 |
header.versionlz = BLOSC_BLOSCLZ_VERSION_FORMAT; |
| 4477 |
header.flags = BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE; // extended header |
| 4478 |
header.typesize = context->typesize; |
| 4479 |
header.nbytes = (int32_t)nbytes; |
| 4480 |
header.blocksize = context->blocksize; |
| 4481 |
header.cbytes = BLOSC_EXTENDED_HEADER_LENGTH + cparams.typesize; |
| 4482 |
header.blosc2_flags = BLOSC2_SPECIAL_VALUE << 4; // mark chunk as all repeated value |
| 4483 |
memcpy((uint8_t *)dest, &header, sizeof(header)); |
| 4484 |
memcpy((uint8_t *)dest + sizeof(header), repeatval, cparams.typesize); |
| 4485 |
|
| 4486 |
blosc2_free_ctx(context); |
| 4487 |
|
| 4488 |
return BLOSC_EXTENDED_HEADER_LENGTH + cparams.typesize; |
| 4489 |
} |
| 4490 |
|
| 4491 |
|
| 4492 |
/* Register filters */ |
| 4493 |
|
| 4494 |
int register_filter_private(blosc2_filter *filter) { |
| 4495 |
BLOSC_ERROR_NULL(filter, BLOSC2_ERROR_INVALID_PARAM); |
| 4496 |
if (g_nfilters == UINT8_MAX) { |
| 4497 |
BLOSC_TRACE_ERROR("Can not register more filters"); |
| 4498 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 4499 |
} |
| 4500 |
if (filter->id < BLOSC2_GLOBAL_REGISTERED_FILTERS_START) { |
| 4501 |
BLOSC_TRACE_ERROR("The id must be greater or equal than %d", BLOSC2_GLOBAL_REGISTERED_FILTERS_START); |
| 4502 |
return BLOSC2_ERROR_FAILURE; |
| 4503 |
} |
| 4504 |
/* This condition can never be fulfilled |
| 4505 |
if (filter->id > BLOSC2_USER_REGISTERED_FILTERS_STOP) { |
| 4506 |
BLOSC_TRACE_ERROR("The id must be less than or equal to %d", BLOSC2_USER_REGISTERED_FILTERS_STOP); |
| 4507 |
return BLOSC2_ERROR_FAILURE; |
| 4508 |
} |
| 4509 |
*/ |
| 4510 |
|
| 4511 |
for (uint64_t i = 0; i < g_nfilters; ++i) { |
| 4512 |
if (g_filters[i].id == filter->id) { |
| 4513 |
if (strcmp(g_filters[i].name, filter->name) != 0) { |
| 4514 |
BLOSC_TRACE_ERROR("The filter (ID: %d) plugin is already registered with name: %s." |
| 4515 |
" Choose another one !", filter->id, g_filters[i].name); |
| 4516 |
return BLOSC2_ERROR_FAILURE; |
| 4517 |
} |
| 4518 |
else { |
| 4519 |
// Already registered, so no more actions needed |
| 4520 |
return BLOSC2_ERROR_SUCCESS; |
| 4521 |
} |
| 4522 |
} |
| 4523 |
} |
| 4524 |
|
| 4525 |
blosc2_filter *filter_new = &g_filters[g_nfilters++]; |
| 4526 |
memcpy(filter_new, filter, sizeof(blosc2_filter)); |
| 4527 |
|
| 4528 |
return BLOSC2_ERROR_SUCCESS; |
| 4529 |
} |
| 4530 |
|
| 4531 |
|
| 4532 |
int blosc2_register_filter(blosc2_filter *filter) { |
| 4533 |
if (filter->id < BLOSC2_USER_REGISTERED_FILTERS_START) { |
| 4534 |
BLOSC_TRACE_ERROR("The id must be greater or equal to %d", BLOSC2_USER_REGISTERED_FILTERS_START); |
| 4535 |
return BLOSC2_ERROR_FAILURE; |
| 4536 |
} |
| 4537 |
|
| 4538 |
return register_filter_private(filter); |
| 4539 |
} |
| 4540 |
|
| 4541 |
|
| 4542 |
/* Register codecs */ |
| 4543 |
|
| 4544 |
int register_codec_private(blosc2_codec *codec) { |
| 4545 |
BLOSC_ERROR_NULL(codec, BLOSC2_ERROR_INVALID_PARAM); |
| 4546 |
if (g_ncodecs == UINT8_MAX) { |
| 4547 |
BLOSC_TRACE_ERROR("Can not register more codecs"); |
| 4548 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 4549 |
} |
| 4550 |
if (codec->compcode < BLOSC2_GLOBAL_REGISTERED_CODECS_START) { |
| 4551 |
BLOSC_TRACE_ERROR("The id must be greater or equal than %d", BLOSC2_GLOBAL_REGISTERED_CODECS_START); |
| 4552 |
return BLOSC2_ERROR_FAILURE; |
| 4553 |
} |
| 4554 |
/* This condition can never be fulfilled |
| 4555 |
if (codec->compcode > BLOSC2_USER_REGISTERED_CODECS_STOP) { |
| 4556 |
BLOSC_TRACE_ERROR("The id must be less or equal to %d", BLOSC2_USER_REGISTERED_CODECS_STOP); |
| 4557 |
return BLOSC2_ERROR_FAILURE; |
| 4558 |
} |
| 4559 |
*/ |
| 4560 |
|
| 4561 |
for (int i = 0; i < g_ncodecs; ++i) { |
| 4562 |
if (g_codecs[i].compcode == codec->compcode) { |
| 4563 |
if (strcmp(g_codecs[i].compname, codec->compname) != 0) { |
| 4564 |
BLOSC_TRACE_ERROR("The codec (ID: %d) plugin is already registered with name: %s." |
| 4565 |
" Choose another one !", codec->compcode, codec->compname); |
| 4566 |
return BLOSC2_ERROR_CODEC_PARAM; |
| 4567 |
} |
| 4568 |
else { |
| 4569 |
// Already registered, so no more actions needed |
| 4570 |
return BLOSC2_ERROR_SUCCESS; |
| 4571 |
} |
| 4572 |
} |
| 4573 |
} |
| 4574 |
|
| 4575 |
blosc2_codec *codec_new = &g_codecs[g_ncodecs++]; |
| 4576 |
memcpy(codec_new, codec, sizeof(blosc2_codec)); |
| 4577 |
|
| 4578 |
return BLOSC2_ERROR_SUCCESS; |
| 4579 |
} |
| 4580 |
|
| 4581 |
|
| 4582 |
int blosc2_register_codec(blosc2_codec *codec) { |
| 4583 |
if (codec->compcode < BLOSC2_USER_REGISTERED_CODECS_START) { |
| 4584 |
BLOSC_TRACE_ERROR("The compcode must be greater or equal than %d", BLOSC2_USER_REGISTERED_CODECS_START); |
| 4585 |
return BLOSC2_ERROR_CODEC_PARAM; |
| 4586 |
} |
| 4587 |
|
| 4588 |
return register_codec_private(codec); |
| 4589 |
} |
| 4590 |
|
| 4591 |
|
| 4592 |
/* Register tuners */ |
| 4593 |
|
| 4594 |
int register_tuner_private(blosc2_tuner *tuner) { |
| 4595 |
BLOSC_ERROR_NULL(tuner, BLOSC2_ERROR_INVALID_PARAM); |
| 4596 |
if (g_ntuners == UINT8_MAX) { |
| 4597 |
BLOSC_TRACE_ERROR("Can not register more tuners"); |
| 4598 |
return BLOSC2_ERROR_CODEC_SUPPORT; |
| 4599 |
} |
| 4600 |
if (tuner->id < BLOSC2_GLOBAL_REGISTERED_TUNER_START) { |
| 4601 |
BLOSC_TRACE_ERROR("The id must be greater or equal than %d", BLOSC2_GLOBAL_REGISTERED_TUNER_START); |
| 4602 |
return BLOSC2_ERROR_FAILURE; |
| 4603 |
} |
| 4604 |
|
| 4605 |
for (int i = 0; i < g_ntuners; ++i) { |
| 4606 |
if (g_tuners[i].id == tuner->id) { |
| 4607 |
if (strcmp(g_tuners[i].name, tuner->name) != 0) { |
| 4608 |
BLOSC_TRACE_ERROR("The tuner (ID: %d) plugin is already registered with name: %s." |
| 4609 |
" Choose another one !", tuner->id, g_tuners[i].name); |
| 4610 |
return BLOSC2_ERROR_FAILURE; |
| 4611 |
} |
| 4612 |
else { |
| 4613 |
// Already registered, so no more actions needed |
| 4614 |
return BLOSC2_ERROR_SUCCESS; |
| 4615 |
} |
| 4616 |
} |
| 4617 |
} |
| 4618 |
|
| 4619 |
blosc2_tuner *tuner_new = &g_tuners[g_ntuners++]; |
| 4620 |
memcpy(tuner_new, tuner, sizeof(blosc2_tuner)); |
| 4621 |
|
| 4622 |
return BLOSC2_ERROR_SUCCESS; |
| 4623 |
} |
| 4624 |
|
| 4625 |
|
| 4626 |
int blosc2_register_tuner(blosc2_tuner *tuner) { |
| 4627 |
if (tuner->id < BLOSC2_USER_REGISTERED_TUNER_START) { |
| 4628 |
BLOSC_TRACE_ERROR("The id must be greater or equal to %d", BLOSC2_USER_REGISTERED_TUNER_START); |
| 4629 |
return BLOSC2_ERROR_FAILURE; |
| 4630 |
} |
| 4631 |
|
| 4632 |
return register_tuner_private(tuner); |
| 4633 |
} |
| 4634 |
|
| 4635 |
|
| 4636 |
int _blosc2_register_io_cb(const blosc2_io_cb *io) { |
| 4637 |
|
| 4638 |
for (uint64_t i = 0; i < g_nio; ++i) { |
| 4639 |
if (g_ios[i].id == io->id) { |
| 4640 |
if (strcmp(g_ios[i].name, io->name) != 0) { |
| 4641 |
BLOSC_TRACE_ERROR("The IO (ID: %d) plugin is already registered with name: %s." |
| 4642 |
" Choose another one !", io->id, g_ios[i].name); |
| 4643 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 4644 |
} |
| 4645 |
else { |
| 4646 |
// Already registered, so no more actions needed |
| 4647 |
return BLOSC2_ERROR_SUCCESS; |
| 4648 |
} |
| 4649 |
} |
| 4650 |
} |
| 4651 |
|
| 4652 |
blosc2_io_cb *io_new = &g_ios[g_nio++]; |
| 4653 |
memcpy(io_new, io, sizeof(blosc2_io_cb)); |
| 4654 |
|
| 4655 |
return BLOSC2_ERROR_SUCCESS; |
| 4656 |
} |
| 4657 |
|
| 4658 |
int blosc2_register_io_cb(const blosc2_io_cb *io) { |
| 4659 |
BLOSC_ERROR_NULL(io, BLOSC2_ERROR_INVALID_PARAM); |
| 4660 |
if (g_nio == UINT8_MAX) { |
| 4661 |
BLOSC_TRACE_ERROR("Can not register more codecs"); |
| 4662 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 4663 |
} |
| 4664 |
|
| 4665 |
if (io->id < BLOSC2_IO_REGISTERED) { |
| 4666 |
BLOSC_TRACE_ERROR("The compcode must be greater or equal than %d", BLOSC2_IO_REGISTERED); |
| 4667 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 4668 |
} |
| 4669 |
|
| 4670 |
return _blosc2_register_io_cb(io); |
| 4671 |
} |
| 4672 |
|
| 4673 |
blosc2_io_cb *blosc2_get_io_cb(uint8_t id) { |
| 4674 |
// If g_initlib is not set by blosc2_init() this function will try to read |
| 4675 |
// uninitialized memory. We should therefore always return NULL in that case |
| 4676 |
if (!g_initlib) { |
| 4677 |
return NULL; |
| 4678 |
} |
| 4679 |
for (uint64_t i = 0; i < g_nio; ++i) { |
| 4680 |
if (g_ios[i].id == id) { |
| 4681 |
return &g_ios[i]; |
| 4682 |
} |
| 4683 |
} |
| 4684 |
if (id == BLOSC2_IO_FILESYSTEM) { |
| 4685 |
if (_blosc2_register_io_cb(&BLOSC2_IO_CB_DEFAULTS) < 0) { |
| 4686 |
BLOSC_TRACE_ERROR("Error registering the default IO API"); |
| 4687 |
return NULL; |
| 4688 |
} |
| 4689 |
return blosc2_get_io_cb(id); |
| 4690 |
} |
| 4691 |
else if (id == BLOSC2_IO_FILESYSTEM_MMAP) { |
| 4692 |
if (_blosc2_register_io_cb(&BLOSC2_IO_CB_MMAP) < 0) { |
| 4693 |
BLOSC_TRACE_ERROR("Error registering the mmap IO API"); |
| 4694 |
return NULL; |
| 4695 |
} |
| 4696 |
return blosc2_get_io_cb(id); |
| 4697 |
} |
| 4698 |
return NULL; |
| 4699 |
} |
| 4700 |
|
| 4701 |
void blosc2_unidim_to_multidim(uint8_t ndim, int64_t *shape, int64_t i, int64_t *index) { |
| 4702 |
if (ndim == 0) { |
| 4703 |
return; |
| 4704 |
} |
| 4705 |
int64_t *strides = malloc(ndim * sizeof(int64_t)); |
| 4706 |
strides[ndim - 1] = 1; |
| 4707 |
for (int j = ndim - 2; j >= 0; --j) { |
| 4708 |
strides[j] = shape[j + 1] * strides[j + 1]; |
| 4709 |
} |
| 4710 |
|
| 4711 |
index[0] = i / strides[0]; |
| 4712 |
for (int j = 1; j < ndim; ++j) { |
| 4713 |
index[j] = (i % strides[j - 1]) / strides[j]; |
| 4714 |
} |
| 4715 |
free(strides); |
| 4716 |
} |
| 4717 |
|
| 4718 |
void blosc2_multidim_to_unidim(const int64_t *index, int8_t ndim, const int64_t *strides, int64_t *i) { |
| 4719 |
*i = 0; |
| 4720 |
for (int j = 0; j < ndim; ++j) { |
| 4721 |
*i += index[j] * strides[j]; |
| 4722 |
} |
| 4723 |
} |
| 4724 |
|
| 4725 |
int blosc2_get_slice_nchunks(blosc2_schunk* schunk, int64_t *start, int64_t *stop, int64_t **chunks_idx) { |
| 4726 |
BLOSC_ERROR_NULL(schunk, BLOSC2_ERROR_NULL_POINTER); |
| 4727 |
if (blosc2_meta_exists(schunk, "b2nd") < 0) { |
| 4728 |
// Try with a caterva metalayer; we are meant to be backward compatible with it |
| 4729 |
if (blosc2_meta_exists(schunk, "caterva") < 0) { |
| 4730 |
return schunk_get_slice_nchunks(schunk, *start, *stop, chunks_idx); |
| 4731 |
} |
| 4732 |
} |
| 4733 |
|
| 4734 |
b2nd_array_t *array; |
| 4735 |
int rc = b2nd_from_schunk(schunk, &array); |
| 4736 |
if (rc < 0) { |
| 4737 |
BLOSC_TRACE_ERROR("Could not get b2nd array from schunk."); |
| 4738 |
return rc; |
| 4739 |
} |
| 4740 |
rc = b2nd_get_slice_nchunks(array, start, stop, chunks_idx); |
| 4741 |
array->sc = NULL; // Free only array struct |
| 4742 |
b2nd_free(array); |
| 4743 |
|
| 4744 |
return rc; |
| 4745 |
} |
| 4746 |
|
| 4747 |
blosc2_cparams blosc2_get_blosc2_cparams_defaults(void) { |
| 4748 |
return BLOSC2_CPARAMS_DEFAULTS; |
| 4749 |
}; |
| 4750 |
|
| 4751 |
blosc2_dparams blosc2_get_blosc2_dparams_defaults(void) { |
| 4752 |
return BLOSC2_DPARAMS_DEFAULTS; |
| 4753 |
}; |
| 4754 |
|
| 4755 |
blosc2_storage blosc2_get_blosc2_storage_defaults(void) { |
| 4756 |
return BLOSC2_STORAGE_DEFAULTS; |
| 4757 |
}; |
| 4758 |
|
| 4759 |
blosc2_io blosc2_get_blosc2_io_defaults(void) { |
| 4760 |
return BLOSC2_IO_DEFAULTS; |
| 4761 |
}; |
| 4762 |
|
| 4763 |
blosc2_stdio_mmap blosc2_get_blosc2_stdio_mmap_defaults(void) { |
| 4764 |
return BLOSC2_STDIO_MMAP_DEFAULTS; |
| 4765 |
}; |