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