/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 "blosc-checked.h" |
15 | | #include "../plugins/codecs/zfp/blosc2-zfp.h" |
16 | | #include "frame.h" |
17 | | #include "sframe.h" |
18 | | #include "b2nd-private.h" |
19 | | #include "schunk-private.h" |
20 | | |
21 | | #if defined(USING_CMAKE) |
22 | | #include "config.h" |
23 | | #endif /* USING_CMAKE */ |
24 | | #include "context.h" |
25 | | |
26 | | #include "shuffle.h" |
27 | | #include "delta.h" |
28 | | #include "trunc-prec.h" |
29 | | #include "blosclz.h" |
30 | | #include "stune.h" |
31 | | #include "blosc2/codecs-registry.h" |
32 | | #include "blosc2/filters-registry.h" |
33 | | #include "blosc2/tuners-registry.h" |
34 | | |
35 | | #include "lz4.h" |
36 | | #define LZ4_HC_STATIC_LINKING_ONLY |
37 | | #include "lz4hc.h" |
38 | | #if defined(HAVE_ZLIB_NG) |
39 | | #ifdef ZLIB_COMPAT |
40 | | #include "zlib.h" |
41 | | #else |
42 | | #include "zlib-ng.h" |
43 | | #endif |
44 | | #elif defined(HAVE_ZLIB) |
45 | | #include "zlib.h" |
46 | | #endif /* HAVE_MINIZ */ |
47 | | #if defined(HAVE_ZSTD) |
48 | | #include "zstd.h" |
49 | | #include "zstd_errors.h" |
50 | | // #include "cover.h" // for experimenting with fast cover training for building dicts |
51 | | #include "zdict.h" |
52 | | #endif /* HAVE_ZSTD */ |
53 | | |
54 | | #if defined(_WIN32) && !defined(__MINGW32__) |
55 | | #include <windows.h> |
56 | | #include <malloc.h> |
57 | | #include <process.h> |
58 | | #define getpid _getpid |
59 | | #endif /* _WIN32 */ |
60 | | |
61 | | #include "threading.h" |
62 | | |
63 | | #include <stdio.h> |
64 | | #include <stdlib.h> |
65 | | #include <errno.h> |
66 | | #include <string.h> |
67 | | #include <sys/types.h> |
68 | | #include <assert.h> |
69 | | #include <math.h> |
70 | | #include <stdint.h> |
71 | | |
72 | | |
73 | | /* Synchronization variables */ |
74 | | |
75 | | /* Global context for non-contextual API */ |
76 | | static blosc2_context* g_global_context; |
77 | | static blosc2_pthread_mutex_t global_comp_mutex; |
78 | | static int g_compressor = BLOSC_BLOSCLZ; |
79 | | static int g_delta = 0; |
80 | | /* The default splitmode */ |
81 | | static int32_t g_splitmode = BLOSC_FORWARD_COMPAT_SPLIT; |
82 | | /* the compressor to use by default */ |
83 | | static int16_t g_nthreads = 1; |
84 | | static int32_t g_force_blocksize = 0; |
85 | | static int g_initlib = 0; |
86 | | static blosc2_schunk* g_schunk = NULL; /* the pointer to super-chunk */ |
87 | | |
88 | | blosc2_codec g_codecs[256] = {0}; |
89 | | uint8_t g_ncodecs = 0; |
90 | | |
91 | | static blosc2_filter g_filters[256] = {0}; |
92 | | static uint64_t g_nfilters = 0; |
93 | | |
94 | | static blosc2_io_cb g_ios[256] = {0}; |
95 | | static uint64_t g_nio = 0; |
96 | | |
97 | | blosc2_tuner g_tuners[256] = {0}; |
98 | | int g_ntuners = 0; |
99 | | |
100 | | static int g_tuner = BLOSC_STUNE; |
101 | | |
102 | | struct blosc_job_group { |
103 | | blosc2_context *context; |
104 | | int32_t next_block; |
105 | | int32_t next_output_block; |
106 | | int32_t blocks_completed; |
107 | | int32_t active_workers; |
108 | | int32_t pending_workers; |
109 | | int32_t output_bytes; |
110 | | int giveup_code; |
111 | | int dref_not_init; |
112 | | bool static_schedule; |
113 | | bool completed; |
114 | | blosc2_pthread_mutex_t mutex; |
115 | | blosc2_pthread_mutex_t delta_mutex; |
116 | | blosc2_pthread_cond_t delta_cv; |
117 | | blosc2_pthread_cond_t completion_cv; |
118 | | }; |
119 | | |
120 | | struct blosc_job_queue_entry { |
121 | | struct blosc_job_group *job; |
122 | | int32_t logical_tid; |
123 | | struct blosc_job_queue_entry *next; |
124 | | }; |
125 | | |
126 | | struct blosc_shared_pool { |
127 | | int16_t nthreads; |
128 | | int16_t shutdown; |
129 | | int32_t context_refs; |
130 | | int32_t active_jobs; |
131 | | blosc2_pthread_t *threads; |
132 | | struct thread_context *thread_contexts; |
133 | | blosc2_pthread_mutex_t mutex; |
134 | | blosc2_pthread_cond_t work_cv; |
135 | | blosc2_pthread_cond_t idle_cv; |
136 | | struct blosc_job_queue_entry *job_queue_head; |
137 | | struct blosc_job_queue_entry *job_queue_tail; |
138 | | struct blosc_shared_pool *next; |
139 | | #if !defined(_WIN32) |
140 | | pthread_attr_t ct_attr; |
141 | | #endif |
142 | | }; |
143 | | |
144 | | // Forward declarations |
145 | | static int init_callback_threads(blosc2_context *context); |
146 | | static int release_thread_backend(blosc2_context *context); |
147 | | static int attach_shared_pool(blosc2_context *context); |
148 | | #if defined(_WIN32) |
149 | | static int init_threadpool(blosc2_context *context); |
150 | | #endif |
151 | | static int parallel_blosc(blosc2_context* context); |
152 | | |
153 | | /* global variable to change threading backend from Blosc-managed to caller-managed */ |
154 | | static blosc_threads_callback threads_callback = 0; |
155 | | static void *threads_callback_data = 0; |
156 | | static blosc2_pthread_mutex_t pool_registry_mutex; |
157 | | static struct blosc_shared_pool *shared_pools = NULL; |
158 | | /* Incremented each time blosc2_destroy() tears down the pool registry. |
159 | | * Contexts store the epoch at attach time; a mismatch means the pool they |
160 | | * hold a pointer to has already been freed. */ |
161 | | static volatile int32_t g_destroy_count = 0; |
162 | | |
163 | | |
164 | | /* non-threadsafe function should be called before any other Blosc function in |
165 | | order to change how threads are managed */ |
166 | | void blosc2_set_threads_callback(blosc_threads_callback callback, void *callback_data) |
167 | 0 | { |
168 | 0 | threads_callback = callback; |
169 | 0 | threads_callback_data = callback_data; |
170 | 0 | } |
171 | | |
172 | | typedef struct { |
173 | | void (*dojob)(void *); |
174 | | void *jobdata; |
175 | | } blosc2_parallel_job_data; |
176 | | |
177 | 0 | static void *blosc2_parallel_pthread_entry(void *arg) { |
178 | 0 | blosc2_parallel_job_data *job = (blosc2_parallel_job_data *)arg; |
179 | 0 | job->dojob(job->jobdata); |
180 | 0 | return NULL; |
181 | 0 | } |
182 | | |
183 | | int blosc2_run_parallel(int16_t nthreads, void (*dojob)(void *), |
184 | 0 | size_t jobdata_elsize, void *jobdata) { |
185 | 0 | if (nthreads <= 0 || dojob == NULL || jobdata == NULL || jobdata_elsize == 0) { |
186 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
187 | 0 | } |
188 | | |
189 | 0 | if (nthreads == 1) { |
190 | 0 | dojob(jobdata); |
191 | 0 | return BLOSC2_ERROR_SUCCESS; |
192 | 0 | } |
193 | | |
194 | 0 | if (threads_callback != NULL) { |
195 | 0 | threads_callback(threads_callback_data, dojob, nthreads, jobdata_elsize, jobdata); |
196 | 0 | return BLOSC2_ERROR_SUCCESS; |
197 | 0 | } |
198 | | |
199 | 0 | blosc2_pthread_t *threads = malloc((size_t)nthreads * sizeof(blosc2_pthread_t)); |
200 | 0 | blosc2_parallel_job_data *jobs = malloc((size_t)nthreads * sizeof(blosc2_parallel_job_data)); |
201 | 0 | if (threads == NULL || jobs == NULL) { |
202 | 0 | free(threads); |
203 | 0 | free(jobs); |
204 | 0 | return BLOSC2_ERROR_MEMORY_ALLOC; |
205 | 0 | } |
206 | | |
207 | 0 | int16_t started = 0; |
208 | 0 | for (int16_t i = 0; i < nthreads; ++i) { |
209 | 0 | jobs[i].dojob = dojob; |
210 | 0 | jobs[i].jobdata = (uint8_t *)jobdata + (size_t)i * jobdata_elsize; |
211 | 0 | int err = blosc2_pthread_create(&threads[i], NULL, blosc2_parallel_pthread_entry, &jobs[i]); |
212 | 0 | if (err != 0) { |
213 | 0 | break; |
214 | 0 | } |
215 | 0 | ++started; |
216 | 0 | } |
217 | 0 | for (int16_t i = 0; i < started; ++i) { |
218 | 0 | blosc2_pthread_join(threads[i], NULL); |
219 | 0 | } |
220 | 0 | free(threads); |
221 | 0 | free(jobs); |
222 | |
|
223 | 0 | return started == nthreads ? BLOSC2_ERROR_SUCCESS : BLOSC2_ERROR_THREAD_CREATE; |
224 | 0 | } |
225 | | |
226 | | |
227 | | /* A function for aligned malloc that is portable */ |
228 | 162k | static uint8_t* my_malloc(size_t size) { |
229 | 162k | void* block = NULL; |
230 | 162k | int res = 0; |
231 | | /* Keep aligned allocations valid under Valgrind and POSIX wrappers. */ |
232 | 162k | if (size == 0) { |
233 | 0 | size = 1; |
234 | 0 | } |
235 | | |
236 | | /* Do an alignment to 32 bytes because AVX2 is supported */ |
237 | | #if defined(_WIN32) |
238 | | /* A (void *) cast needed for avoiding a warning with MINGW :-/ */ |
239 | | block = (void *)_aligned_malloc(size, 32); |
240 | | #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 |
241 | | /* Platform does have an implementation of posix_memalign */ |
242 | 162k | res = posix_memalign(&block, 32, size); |
243 | | #else |
244 | | block = malloc(size); |
245 | | #endif /* _WIN32 */ |
246 | | |
247 | 162k | if (block == NULL || res != 0) { |
248 | 0 | BLOSC_TRACE_ERROR("Error allocating memory!"); |
249 | 0 | return NULL; |
250 | 0 | } |
251 | | |
252 | 162k | return (uint8_t*)block; |
253 | 162k | } |
254 | | |
255 | | |
256 | | /* Release memory booked by my_malloc */ |
257 | 162k | static void my_free(void* block) { |
258 | | #if defined(_WIN32) |
259 | | _aligned_free(block); |
260 | | #else |
261 | 162k | free(block); |
262 | 162k | #endif /* _WIN32 */ |
263 | 162k | } |
264 | | |
265 | | |
266 | | /* |
267 | | * Conversion routines between compressor and compression libraries |
268 | | */ |
269 | | |
270 | | /* Return the library code associated with the compressor name */ |
271 | 0 | static int compname_to_clibcode(const char* compname) { |
272 | 0 | if (strcmp(compname, BLOSC_BLOSCLZ_COMPNAME) == 0) |
273 | 0 | return BLOSC_BLOSCLZ_LIB; |
274 | 0 | if (strcmp(compname, BLOSC_LZ4_COMPNAME) == 0) |
275 | 0 | return BLOSC_LZ4_LIB; |
276 | 0 | if (strcmp(compname, BLOSC_LZ4HC_COMPNAME) == 0) |
277 | 0 | return BLOSC_LZ4_LIB; |
278 | 0 | if (strcmp(compname, BLOSC_ZLIB_COMPNAME) == 0) |
279 | 0 | return BLOSC_ZLIB_LIB; |
280 | 0 | if (strcmp(compname, BLOSC_ZSTD_COMPNAME) == 0) |
281 | 0 | return BLOSC_ZSTD_LIB; |
282 | 0 | for (int i = 0; i < g_ncodecs; ++i) { |
283 | 0 | if (strcmp(compname, g_codecs[i].compname) == 0) |
284 | 0 | return g_codecs[i].complib; |
285 | 0 | } |
286 | 0 | return BLOSC2_ERROR_NOT_FOUND; |
287 | 0 | } |
288 | | |
289 | | /* Return the library name associated with the compressor code */ |
290 | 6 | static const char* clibcode_to_clibname(int clibcode) { |
291 | 6 | if (clibcode == BLOSC_BLOSCLZ_LIB) return BLOSC_BLOSCLZ_LIBNAME; |
292 | 6 | if (clibcode == BLOSC_LZ4_LIB) return BLOSC_LZ4_LIBNAME; |
293 | 6 | if (clibcode == BLOSC_ZLIB_LIB) return BLOSC_ZLIB_LIBNAME; |
294 | 6 | if (clibcode == BLOSC_ZSTD_LIB) return BLOSC_ZSTD_LIBNAME; |
295 | 60 | for (int i = 0; i < g_ncodecs; ++i) { |
296 | 54 | if (clibcode == g_codecs[i].complib) |
297 | 0 | return g_codecs[i].compname; |
298 | 54 | } |
299 | 6 | return NULL; /* should never happen */ |
300 | 6 | } |
301 | | |
302 | | |
303 | | /* |
304 | | * Conversion routines between compressor names and compressor codes |
305 | | */ |
306 | | |
307 | | /* Get the compressor name associated with the compressor code */ |
308 | 0 | int blosc2_compcode_to_compname(int compcode, const char** compname) { |
309 | 0 | int code = -1; /* -1 means non-existent compressor code */ |
310 | 0 | const char* name = NULL; |
311 | |
|
312 | 0 | if (compcode == BLOSC_BLOSCLZ) { |
313 | 0 | code = BLOSC_BLOSCLZ; |
314 | 0 | name = BLOSC_BLOSCLZ_COMPNAME; |
315 | 0 | } else if (compcode == BLOSC_LZ4) { |
316 | 0 | code = BLOSC_LZ4; |
317 | 0 | name = BLOSC_LZ4_COMPNAME; |
318 | 0 | } else if (compcode == BLOSC_LZ4HC) { |
319 | 0 | code = BLOSC_LZ4HC; |
320 | 0 | name = BLOSC_LZ4HC_COMPNAME; |
321 | 0 | } else if (compcode == BLOSC_ZLIB) { |
322 | 0 | #if defined(HAVE_ZLIB) |
323 | 0 | code = BLOSC_ZLIB; |
324 | 0 | #endif /* HAVE_ZLIB */ |
325 | 0 | name = BLOSC_ZLIB_COMPNAME; |
326 | 0 | } else if (compcode == BLOSC_ZSTD) { |
327 | 0 | #if defined(HAVE_ZSTD) |
328 | 0 | code = BLOSC_ZSTD; |
329 | 0 | #endif /* HAVE_ZSTD */ |
330 | 0 | name = BLOSC_ZSTD_COMPNAME; |
331 | 0 | } else { |
332 | 0 | for (int i = 0; i < g_ncodecs; ++i) { |
333 | 0 | if (compcode == g_codecs[i].compcode) { |
334 | 0 | code = compcode; |
335 | 0 | name = g_codecs[i].compname; |
336 | 0 | break; |
337 | 0 | } |
338 | 0 | } |
339 | 0 | } |
340 | |
|
341 | 0 | *compname = name; |
342 | 0 | return code; |
343 | 0 | } |
344 | | |
345 | | /* Get the compressor code for the compressor name. -1 if it is not available */ |
346 | 21.0k | int blosc2_compname_to_compcode(const char* compname) { |
347 | 21.0k | int code = -1; /* -1 means non-existent compressor code */ |
348 | | |
349 | 21.0k | if (strcmp(compname, BLOSC_BLOSCLZ_COMPNAME) == 0) { |
350 | 3.61k | code = BLOSC_BLOSCLZ; |
351 | 3.61k | } |
352 | 17.4k | else if (strcmp(compname, BLOSC_LZ4_COMPNAME) == 0) { |
353 | 762 | code = BLOSC_LZ4; |
354 | 762 | } |
355 | 16.7k | else if (strcmp(compname, BLOSC_LZ4HC_COMPNAME) == 0) { |
356 | 3.63k | code = BLOSC_LZ4HC; |
357 | 3.63k | } |
358 | 13.0k | #if defined(HAVE_ZLIB) |
359 | 13.0k | else if (strcmp(compname, BLOSC_ZLIB_COMPNAME) == 0) { |
360 | 3.67k | code = BLOSC_ZLIB; |
361 | 3.67k | } |
362 | 9.39k | #endif /* HAVE_ZLIB */ |
363 | 9.39k | #if defined(HAVE_ZSTD) |
364 | 9.39k | else if (strcmp(compname, BLOSC_ZSTD_COMPNAME) == 0) { |
365 | 9.39k | code = BLOSC_ZSTD; |
366 | 9.39k | } |
367 | 0 | #endif /* HAVE_ZSTD */ |
368 | 0 | else{ |
369 | 0 | for (int i = 0; i < g_ncodecs; ++i) { |
370 | 0 | if (strcmp(compname, g_codecs[i].compname) == 0) { |
371 | 0 | code = g_codecs[i].compcode; |
372 | 0 | break; |
373 | 0 | } |
374 | 0 | } |
375 | 0 | } |
376 | 21.0k | return code; |
377 | 21.0k | } |
378 | | |
379 | | |
380 | | /* Convert compressor code to blosc compressor format code */ |
381 | 63.1k | static int compcode_to_compformat(int compcode) { |
382 | 63.1k | switch (compcode) { |
383 | 45.7k | case BLOSC_BLOSCLZ: return BLOSC_BLOSCLZ_FORMAT; |
384 | 758 | case BLOSC_LZ4: return BLOSC_LZ4_FORMAT; |
385 | 3.63k | case BLOSC_LZ4HC: return BLOSC_LZ4HC_FORMAT; |
386 | | |
387 | 0 | #if defined(HAVE_ZLIB) |
388 | 3.66k | case BLOSC_ZLIB: return BLOSC_ZLIB_FORMAT; |
389 | 0 | #endif /* HAVE_ZLIB */ |
390 | | |
391 | 0 | #if defined(HAVE_ZSTD) |
392 | 9.37k | case BLOSC_ZSTD: return BLOSC_ZSTD_FORMAT; |
393 | 0 | break; |
394 | 0 | #endif /* HAVE_ZSTD */ |
395 | 0 | default: |
396 | 0 | return BLOSC_UDCODEC_FORMAT; |
397 | 63.1k | } |
398 | 0 | BLOSC_ERROR(BLOSC2_ERROR_FAILURE); |
399 | 0 | } |
400 | | |
401 | | |
402 | | /* Convert compressor code to blosc compressor format version */ |
403 | 63.5k | static int compcode_to_compversion(int compcode) { |
404 | | /* Write compressor format */ |
405 | 63.5k | switch (compcode) { |
406 | 46.1k | case BLOSC_BLOSCLZ: |
407 | 46.1k | return BLOSC_BLOSCLZ_VERSION_FORMAT; |
408 | 761 | case BLOSC_LZ4: |
409 | 761 | return BLOSC_LZ4_VERSION_FORMAT; |
410 | 3.63k | case BLOSC_LZ4HC: |
411 | 3.63k | return BLOSC_LZ4HC_VERSION_FORMAT; |
412 | | |
413 | 0 | #if defined(HAVE_ZLIB) |
414 | 3.67k | case BLOSC_ZLIB: |
415 | 3.67k | return BLOSC_ZLIB_VERSION_FORMAT; |
416 | 0 | break; |
417 | 0 | #endif /* HAVE_ZLIB */ |
418 | | |
419 | 0 | #if defined(HAVE_ZSTD) |
420 | 9.37k | case BLOSC_ZSTD: |
421 | 9.37k | return BLOSC_ZSTD_VERSION_FORMAT; |
422 | 0 | break; |
423 | 0 | #endif /* HAVE_ZSTD */ |
424 | 0 | default: |
425 | 0 | for (int i = 0; i < g_ncodecs; ++i) { |
426 | 0 | if (compcode == g_codecs[i].compcode) { |
427 | 0 | return g_codecs[i].version; |
428 | 0 | } |
429 | 0 | } |
430 | 63.5k | } |
431 | 0 | return BLOSC2_ERROR_FAILURE; |
432 | 63.5k | } |
433 | | |
434 | | |
435 | | static int lz4_wrap_compress(const char* input, size_t input_length, |
436 | | char* output, size_t maxout, int accel, |
437 | 13.7k | struct thread_context* thread_context) { |
438 | 13.7k | int cbytes; |
439 | 13.7k | blosc2_context* context = thread_context->parent_context; |
440 | 13.7k | if (context->use_dict && context->dict_cdict != NULL) { |
441 | 0 | if (thread_context->lz4_cstream == NULL) { |
442 | 0 | thread_context->lz4_cstream = LZ4_createStream(); |
443 | 0 | } |
444 | | // Reset the thread stream to dict-only context before each block so that |
445 | | // each compressed block references only the dictionary (not prior blocks). |
446 | | // This ensures independent decompressibility with LZ4_decompress_safe_usingDict. |
447 | 0 | LZ4_loadDict((LZ4_stream_t*)thread_context->lz4_cstream, |
448 | 0 | (const char*)context->dict_buffer, (int)context->dict_size); |
449 | 0 | cbytes = LZ4_compress_fast_continue((LZ4_stream_t*)thread_context->lz4_cstream, |
450 | 0 | input, output, (int)input_length, (int)maxout, accel); |
451 | 13.7k | } else { |
452 | 13.7k | cbytes = LZ4_compress_fast(input, output, (int)input_length, (int)maxout, accel); |
453 | 13.7k | } |
454 | 13.7k | return cbytes; |
455 | 13.7k | } |
456 | | |
457 | | |
458 | | static int lz4hc_wrap_compress(const char* input, size_t input_length, |
459 | | char* output, size_t maxout, int clevel, |
460 | 33.1k | struct thread_context* thread_context) { |
461 | 33.1k | int cbytes; |
462 | 33.1k | if (input_length > (size_t)(UINT32_C(2) << 30)) |
463 | 0 | return BLOSC2_ERROR_2GB_LIMIT; |
464 | 33.1k | blosc2_context* context = thread_context->parent_context; |
465 | | /* clevel for lz4hc goes up to 12, at least in LZ4 1.7.5 |
466 | | * but levels larger than 9 do not buy much compression. */ |
467 | 33.1k | if (context->use_dict && context->dict_cdict != NULL) { |
468 | 0 | if (thread_context->lz4hc_cstream == NULL) { |
469 | 0 | thread_context->lz4hc_cstream = LZ4_createStreamHC(); |
470 | 0 | } |
471 | | // Reset to dict-only context (with correct clevel) before each block. |
472 | 0 | LZ4_resetStreamHC_fast((LZ4_streamHC_t*)thread_context->lz4hc_cstream, clevel); |
473 | 0 | LZ4_loadDictHC((LZ4_streamHC_t*)thread_context->lz4hc_cstream, |
474 | 0 | (const char*)context->dict_buffer, (int)context->dict_size); |
475 | 0 | cbytes = LZ4_compress_HC_continue((LZ4_streamHC_t*)thread_context->lz4hc_cstream, |
476 | 0 | input, output, (int)input_length, (int)maxout); |
477 | 33.1k | } else { |
478 | 33.1k | cbytes = LZ4_compress_HC(input, output, (int)input_length, (int)maxout, clevel); |
479 | 33.1k | } |
480 | 33.1k | return cbytes; |
481 | 33.1k | } |
482 | | |
483 | | |
484 | | static int lz4_wrap_decompress(const char* input, size_t compressed_length, |
485 | | char* output, size_t maxout, |
486 | 6.64k | struct thread_context* thread_context) { |
487 | 6.64k | int nbytes; |
488 | 6.64k | blosc2_context* context = thread_context->parent_context; |
489 | 6.64k | if (context->use_dict && context->dict_buffer != NULL && context->dict_size > 0) { |
490 | 773 | nbytes = LZ4_decompress_safe_usingDict(input, output, |
491 | 773 | (int)compressed_length, (int)maxout, |
492 | 773 | (const char*)context->dict_buffer, |
493 | 773 | (int)context->dict_size); |
494 | 5.86k | } else { |
495 | 5.86k | nbytes = LZ4_decompress_safe(input, output, (int)compressed_length, (int)maxout); |
496 | 5.86k | } |
497 | 6.64k | if (nbytes != (int)maxout) { |
498 | 1.38k | return 0; |
499 | 1.38k | } |
500 | 5.25k | return (int)maxout; |
501 | 6.64k | } |
502 | | |
503 | | #if defined(HAVE_ZLIB) |
504 | | /* zlib is not very respectful with sharing name space with others. |
505 | | Fortunately, its names do not collide with those already in blosc. */ |
506 | | static int zlib_wrap_compress(const char* input, size_t input_length, |
507 | 38.8k | char* output, size_t maxout, int clevel) { |
508 | 38.8k | int status; |
509 | 38.8k | #if defined(HAVE_ZLIB_NG) && ! defined(ZLIB_COMPAT) |
510 | 38.8k | size_t cl = maxout; |
511 | 38.8k | status = zng_compress2( |
512 | 38.8k | (uint8_t*)output, &cl, (uint8_t*)input, input_length, clevel); |
513 | | #else |
514 | | uLongf cl = (uLongf)maxout; |
515 | | status = compress2( |
516 | | (Bytef*)output, &cl, (Bytef*)input, (uLong)input_length, clevel); |
517 | | #endif |
518 | 38.8k | if (status != Z_OK) { |
519 | 10.5k | return 0; |
520 | 10.5k | } |
521 | 28.3k | return (int)cl; |
522 | 38.8k | } |
523 | | |
524 | | static int zlib_wrap_decompress(const char* input, size_t compressed_length, |
525 | 8.53k | char* output, size_t maxout) { |
526 | 8.53k | int status; |
527 | 8.53k | #if defined(HAVE_ZLIB_NG) && ! defined(ZLIB_COMPAT) |
528 | 8.53k | size_t ul = maxout; |
529 | 8.53k | status = zng_uncompress( |
530 | 8.53k | (uint8_t*)output, &ul, (uint8_t*)input, compressed_length); |
531 | | #else |
532 | | uLongf ul = (uLongf)maxout; |
533 | | status = uncompress( |
534 | | (Bytef*)output, &ul, (Bytef*)input, (uLong)compressed_length); |
535 | | #endif |
536 | 8.53k | if (status != Z_OK) { |
537 | 1.75k | return 0; |
538 | 1.75k | } |
539 | 6.78k | return (int)ul; |
540 | 8.53k | } |
541 | | #endif /* HAVE_ZLIB */ |
542 | | |
543 | | |
544 | | #if defined(HAVE_ZSTD) |
545 | | static int zstd_wrap_compress(struct thread_context* thread_context, |
546 | | const char* input, size_t input_length, |
547 | 311k | char* output, size_t maxout, int clevel) { |
548 | 311k | size_t code; |
549 | 311k | blosc2_context* context = thread_context->parent_context; |
550 | | |
551 | 311k | clevel = (clevel < 9) ? clevel * 2 - 1 : ZSTD_maxCLevel(); |
552 | | /* Make the level 8 close enough to maxCLevel */ |
553 | 311k | if (clevel == 8) clevel = ZSTD_maxCLevel() - 2; |
554 | | |
555 | 311k | if (thread_context->zstd_cctx == NULL) { |
556 | 969 | thread_context->zstd_cctx = ZSTD_createCCtx(); |
557 | 969 | } |
558 | | |
559 | 311k | if (context->use_dict) { |
560 | 0 | assert(context->dict_cdict != NULL); |
561 | 0 | code = ZSTD_compress_usingCDict( |
562 | 0 | thread_context->zstd_cctx, (void*)output, maxout, (void*)input, |
563 | 0 | input_length, context->dict_cdict); |
564 | 311k | } else { |
565 | 311k | code = ZSTD_compressCCtx(thread_context->zstd_cctx, |
566 | 311k | (void*)output, maxout, (void*)input, input_length, clevel); |
567 | 311k | } |
568 | 311k | if (ZSTD_isError(code) != ZSTD_error_no_error) { |
569 | | // Blosc will just memcpy this buffer |
570 | 65.8k | return 0; |
571 | 65.8k | } |
572 | 245k | return (int)code; |
573 | 311k | } |
574 | | |
575 | | static int zstd_wrap_decompress(struct thread_context* thread_context, |
576 | | const char* input, size_t compressed_length, |
577 | 31.4k | char* output, size_t maxout) { |
578 | 31.4k | size_t code; |
579 | 31.4k | blosc2_context* context = thread_context->parent_context; |
580 | | |
581 | 31.4k | if (thread_context->zstd_dctx == NULL) { |
582 | 17.0k | thread_context->zstd_dctx = ZSTD_createDCtx(); |
583 | 17.0k | } |
584 | | |
585 | 31.4k | if (context->use_dict) { |
586 | 6.41k | assert(context->dict_ddict != NULL); |
587 | 6.41k | code = ZSTD_decompress_usingDDict( |
588 | 6.41k | thread_context->zstd_dctx, (void*)output, maxout, (void*)input, |
589 | 6.41k | compressed_length, context->dict_ddict); |
590 | 25.0k | } else { |
591 | 25.0k | code = ZSTD_decompressDCtx(thread_context->zstd_dctx, |
592 | 25.0k | (void*)output, maxout, (void*)input, compressed_length); |
593 | 25.0k | } |
594 | 31.4k | if (ZSTD_isError(code) != ZSTD_error_no_error) { |
595 | 16.6k | BLOSC_TRACE_ERROR("Error in ZSTD decompression: '%s'. Giving up.", |
596 | 16.6k | ZDICT_getErrorName(code)); |
597 | 16.6k | return 0; |
598 | 16.6k | } |
599 | 14.8k | return (int)code; |
600 | 31.4k | } |
601 | | #endif /* HAVE_ZSTD */ |
602 | | |
603 | | /* Compute acceleration for blosclz */ |
604 | 461k | static int get_accel(const blosc2_context* context) { |
605 | 461k | int clevel = context->clevel; |
606 | | |
607 | 461k | if (context->compcode == BLOSC_LZ4) { |
608 | | /* This acceleration setting based on discussions held in: |
609 | | * https://groups.google.com/forum/#!topic/lz4c/zosy90P8MQw |
610 | | */ |
611 | 15.2k | return (10 - clevel); |
612 | 15.2k | } |
613 | 446k | return 1; |
614 | 461k | } |
615 | | |
616 | | |
617 | 4.24M | int do_nothing(uint8_t filter, char cmode) { |
618 | 4.24M | if (cmode == 'c') { |
619 | 2.77M | return (filter == BLOSC_NOFILTER); |
620 | 2.77M | } else { |
621 | | // TRUNC_PREC do not have to be applied during decompression |
622 | 1.47M | return ((filter == BLOSC_NOFILTER) || (filter == BLOSC_TRUNC_PREC)); |
623 | 1.47M | } |
624 | 4.24M | } |
625 | | |
626 | | |
627 | 379k | int next_filter(const uint8_t* filters, int current_filter, char cmode) { |
628 | 565k | for (int i = current_filter - 1; i >= 0; i--) { |
629 | 565k | if (!do_nothing(filters[i], cmode)) { |
630 | 379k | return filters[i]; |
631 | 379k | } |
632 | 565k | } |
633 | 0 | return BLOSC_NOFILTER; |
634 | 379k | } |
635 | | |
636 | | |
637 | 612k | int last_filter(const uint8_t* filters, char cmode) { |
638 | 612k | int last_index = -1; |
639 | 4.28M | for (int i = BLOSC2_MAX_FILTERS - 1; i >= 0; i--) { |
640 | 3.67M | if (!do_nothing(filters[i], cmode)) { |
641 | 556k | last_index = i; |
642 | 556k | } |
643 | 3.67M | } |
644 | 612k | return last_index; |
645 | 612k | } |
646 | | |
647 | | |
648 | | /* Convert filter pipeline to filter flags */ |
649 | 137k | static uint8_t filters_to_flags(const uint8_t* filters) { |
650 | 137k | uint8_t flags = 0; |
651 | | |
652 | 963k | for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
653 | 825k | switch (filters[i]) { |
654 | 69.2k | case BLOSC_SHUFFLE: |
655 | 69.2k | flags |= BLOSC_DOSHUFFLE; |
656 | 69.2k | break; |
657 | 43.0k | case BLOSC_BITSHUFFLE: |
658 | 43.0k | flags |= BLOSC_DOBITSHUFFLE; |
659 | 43.0k | break; |
660 | 26.8k | case BLOSC_DELTA: |
661 | 26.8k | flags |= BLOSC_DODELTA; |
662 | 26.8k | break; |
663 | 686k | default : |
664 | 686k | break; |
665 | 825k | } |
666 | 825k | } |
667 | 137k | return flags; |
668 | 137k | } |
669 | | |
670 | | |
671 | | /* Convert filter flags to filter pipeline */ |
672 | 22.0M | static void flags_to_filters(const uint8_t flags, uint8_t* filters) { |
673 | | /* Initialize the filter pipeline */ |
674 | 22.0M | memset(filters, 0, BLOSC2_MAX_FILTERS); |
675 | | /* Fill the filter pipeline */ |
676 | 22.0M | if (flags & BLOSC_DOSHUFFLE) |
677 | 22.0M | filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_SHUFFLE; |
678 | 22.0M | if (flags & BLOSC_DOBITSHUFFLE) |
679 | 22.0M | filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_BITSHUFFLE; |
680 | 22.0M | if (flags & BLOSC_DODELTA) |
681 | 307k | filters[BLOSC2_MAX_FILTERS - 2] = BLOSC_DELTA; |
682 | 22.0M | } |
683 | | |
684 | | |
685 | | /* Get filter flags from header flags */ |
686 | | static uint8_t get_filter_flags(const uint8_t header_flags, |
687 | 9.97k | const int32_t typesize) { |
688 | 9.97k | uint8_t flags = 0; |
689 | | |
690 | 9.97k | if ((header_flags & BLOSC_DOSHUFFLE) && (typesize > 1)) { |
691 | 1.96k | flags |= BLOSC_DOSHUFFLE; |
692 | 1.96k | } |
693 | 9.97k | if (header_flags & BLOSC_DOBITSHUFFLE) { |
694 | 2.24k | flags |= BLOSC_DOBITSHUFFLE; |
695 | 2.24k | } |
696 | 9.97k | if (header_flags & BLOSC_DODELTA) { |
697 | 3.38k | flags |= BLOSC_DODELTA; |
698 | 3.38k | } |
699 | 9.97k | if (header_flags & BLOSC_MEMCPYED) { |
700 | 58 | flags |= BLOSC_MEMCPYED; |
701 | 58 | } |
702 | 9.97k | return flags; |
703 | 9.97k | } |
704 | | |
705 | | typedef struct blosc_header_s { |
706 | | uint8_t version; |
707 | | uint8_t versionlz; |
708 | | uint8_t flags; |
709 | | uint8_t typesize; |
710 | | int32_t nbytes; |
711 | | int32_t blocksize; |
712 | | int32_t cbytes; |
713 | | // Extended Blosc2 header |
714 | | uint8_t filters[BLOSC2_MAX_FILTERS]; |
715 | | uint8_t udcompcode; |
716 | | uint8_t compcode_meta; |
717 | | uint8_t filters_meta[BLOSC2_MAX_FILTERS]; |
718 | | uint8_t blosc2_flags2; |
719 | | uint8_t blosc2_flags; |
720 | | } blosc_header; |
721 | | |
722 | | |
723 | | int read_chunk_header(const uint8_t* src, int32_t srcsize, bool extended_header, blosc_header* header) |
724 | 22.1M | { |
725 | 22.1M | memset(header, 0, sizeof(blosc_header)); |
726 | | |
727 | 22.1M | if (srcsize < BLOSC_MIN_HEADER_LENGTH) { |
728 | 0 | BLOSC_TRACE_ERROR("Not enough space to read Blosc header."); |
729 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
730 | 0 | } |
731 | | |
732 | 22.1M | memcpy(header, src, BLOSC_MIN_HEADER_LENGTH); |
733 | | |
734 | 22.1M | bool little_endian = is_little_endian(); |
735 | | |
736 | 22.1M | if (!little_endian) { |
737 | 0 | header->nbytes = bswap32_(header->nbytes); |
738 | 0 | header->blocksize = bswap32_(header->blocksize); |
739 | 0 | header->cbytes = bswap32_(header->cbytes); |
740 | 0 | } |
741 | | |
742 | 22.1M | if (header->cbytes < BLOSC_MIN_HEADER_LENGTH) { |
743 | 37 | BLOSC_TRACE_ERROR("`cbytes` is too small to read min header."); |
744 | 37 | return BLOSC2_ERROR_INVALID_HEADER; |
745 | 37 | } |
746 | 22.1M | if (header->blocksize <= 0) { |
747 | 43 | BLOSC_TRACE_ERROR("`blocksize` is zero"); |
748 | 43 | return BLOSC2_ERROR_INVALID_HEADER; |
749 | 43 | } |
750 | 22.1M | if (header->blocksize > BLOSC2_MAXBLOCKSIZE) { |
751 | 14 | BLOSC_TRACE_ERROR("`blocksize` greater than maximum allowed"); |
752 | 14 | return BLOSC2_ERROR_INVALID_HEADER; |
753 | 14 | } |
754 | 22.1M | if (header->typesize == 0) { |
755 | 5 | BLOSC_TRACE_ERROR("`typesize` is zero."); |
756 | 5 | return BLOSC2_ERROR_INVALID_HEADER; |
757 | 5 | } |
758 | | |
759 | | /* Read extended header if it is wanted */ |
760 | 22.1M | if ((extended_header) && (header->flags & BLOSC_DOSHUFFLE) && (header->flags & BLOSC_DOBITSHUFFLE)) { |
761 | 80.2k | if (header->cbytes < BLOSC_EXTENDED_HEADER_LENGTH) { |
762 | 15 | BLOSC_TRACE_ERROR("`cbytes` is too small to read extended header."); |
763 | 15 | return BLOSC2_ERROR_INVALID_HEADER; |
764 | 15 | } |
765 | 80.2k | if (srcsize < BLOSC_EXTENDED_HEADER_LENGTH) { |
766 | 0 | BLOSC_TRACE_ERROR("Not enough space to read Blosc extended header."); |
767 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
768 | 0 | } |
769 | | |
770 | 80.2k | memcpy((uint8_t *)header + BLOSC_MIN_HEADER_LENGTH, src + BLOSC_MIN_HEADER_LENGTH, |
771 | 80.2k | BLOSC_EXTENDED_HEADER_LENGTH - BLOSC_MIN_HEADER_LENGTH); |
772 | | |
773 | 80.2k | if ((header->blosc2_flags2 & BLOSC2_VL_BLOCKS) && (header->blosc2_flags != 0)) { |
774 | 170 | int32_t special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK; |
775 | 170 | if (special_type != 0) { |
776 | 6 | BLOSC_TRACE_ERROR("VL-block chunks cannot use special chunk encodings."); |
777 | 6 | return BLOSC2_ERROR_INVALID_HEADER; |
778 | 6 | } |
779 | 170 | } |
780 | | |
781 | 80.2k | int32_t special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK; |
782 | 80.2k | if (special_type != 0) { |
783 | 8.11k | if (special_type == BLOSC2_SPECIAL_VALUE) { |
784 | | // In this case, the actual type size must be derived from the cbytes |
785 | 142 | int32_t typesize = header->cbytes - BLOSC_EXTENDED_HEADER_LENGTH; |
786 | 142 | if (typesize <= 0) { |
787 | 1 | BLOSC_TRACE_ERROR("`typesize` is zero or negative"); |
788 | 1 | return BLOSC2_ERROR_INVALID_HEADER; |
789 | 1 | } |
790 | 141 | if (typesize > BLOSC2_MAXTYPESIZE) { |
791 | 0 | BLOSC_TRACE_ERROR("`typesize` is greater than maximum allowed"); |
792 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
793 | 0 | } |
794 | 141 | if (typesize > header->nbytes) { |
795 | 23 | BLOSC_TRACE_ERROR("`typesize` is greater than `nbytes`"); |
796 | 23 | return BLOSC2_ERROR_INVALID_HEADER; |
797 | 23 | } |
798 | 118 | if (header->nbytes % typesize != 0) { |
799 | 8 | BLOSC_TRACE_ERROR("`nbytes` is not a multiple of typesize"); |
800 | 8 | return BLOSC2_ERROR_INVALID_HEADER; |
801 | 8 | } |
802 | 118 | } |
803 | 7.97k | else { |
804 | | // All-zero chunks fill with memset, which works regardless of element alignment. |
805 | 7.97k | if (special_type != BLOSC2_SPECIAL_ZERO |
806 | 1.22k | && header->nbytes % header->typesize != 0) { |
807 | 4 | BLOSC_TRACE_ERROR("`nbytes` is not a multiple of typesize"); |
808 | 4 | return BLOSC2_ERROR_INVALID_HEADER; |
809 | 4 | } |
810 | 7.97k | } |
811 | 8.11k | } |
812 | | // The number of filters depends on the version of the header. Blosc2 alpha series |
813 | | // did not initialize filters to zero beyond the max supported. |
814 | 80.2k | if (header->version == BLOSC2_VERSION_FORMAT_ALPHA) { |
815 | 4.85k | header->filters[5] = 0; |
816 | 4.85k | header->filters_meta[5] = 0; |
817 | 4.85k | } |
818 | 80.2k | } |
819 | 22.0M | else { |
820 | 22.0M | flags_to_filters(header->flags, header->filters); |
821 | 22.0M | } |
822 | 22.1M | if (header->version > BLOSC2_VERSION_FORMAT && |
823 | 325k | (header->blosc2_flags2 & (uint8_t)~BLOSC2_VL_BLOCKS) != 0) { |
824 | | /* Version from future with unsupported chunk features. */ |
825 | 10 | return BLOSC2_ERROR_VERSION_SUPPORT; |
826 | 10 | } |
827 | 22.1M | if ((header->blosc2_flags2 & BLOSC2_VL_BLOCKS) != 0 && |
828 | 357 | (header->flags & (uint8_t)BLOSC_MEMCPYED) != 0) { |
829 | 2 | BLOSC_TRACE_ERROR("VL-block chunks cannot be memcpyed."); |
830 | 2 | return BLOSC2_ERROR_INVALID_HEADER; |
831 | 2 | } |
832 | 22.1M | if ((header->blosc2_flags2 & BLOSC2_VL_BLOCKS) == 0 && |
833 | 22.1M | header->nbytes > 0 && header->blocksize > header->nbytes) { |
834 | 64.5k | header->blocksize = header->nbytes; |
835 | 64.5k | } |
836 | 22.1M | return 0; |
837 | 22.1M | } |
838 | | |
839 | 147k | static inline void blosc2_calculate_blocks(blosc2_context* context) { |
840 | | /* Compute number of blocks in buffer */ |
841 | 147k | context->nblocks = context->sourcesize / context->blocksize; |
842 | 147k | context->leftover = context->sourcesize % context->blocksize; |
843 | 147k | context->nblocks = (context->leftover > 0) ? |
844 | 95.6k | (context->nblocks + 1) : context->nblocks; |
845 | 147k | } |
846 | | |
847 | 81.0k | static int blosc2_initialize_context_from_header(blosc2_context* context, blosc_header* header) { |
848 | 81.0k | context->header_flags = header->flags; |
849 | 81.0k | context->typesize = header->typesize; |
850 | 81.0k | context->sourcesize = header->nbytes; |
851 | 81.0k | context->header_blocksize = header->blocksize; |
852 | 81.0k | context->blocksize = header->blocksize; |
853 | 81.0k | context->blosc2_flags2 = header->blosc2_flags2; |
854 | 81.0k | context->blosc2_flags = header->blosc2_flags; |
855 | 81.0k | context->compcode = header->flags >> 5; |
856 | 81.0k | if (context->compcode == BLOSC_UDCODEC_FORMAT) { |
857 | 29.6k | context->compcode = header->udcompcode; |
858 | 29.6k | } |
859 | 81.0k | if (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) { |
860 | 355 | context->nblocks = header->blocksize; |
861 | 355 | context->leftover = 0; |
862 | 355 | context->blocksize = 0; |
863 | 355 | } |
864 | 80.7k | else { |
865 | 80.7k | blosc2_calculate_blocks(context); |
866 | 80.7k | } |
867 | | |
868 | 81.0k | bool is_lazy = false; |
869 | 81.0k | if ((context->header_flags & BLOSC_DOSHUFFLE) && |
870 | 73.7k | (context->header_flags & BLOSC_DOBITSHUFFLE)) { |
871 | | /* Extended header */ |
872 | 71.0k | context->header_overhead = BLOSC_EXTENDED_HEADER_LENGTH; |
873 | | |
874 | 71.0k | memcpy(context->filters, header->filters, BLOSC2_MAX_FILTERS); |
875 | 71.0k | memcpy(context->filters_meta, header->filters_meta, BLOSC2_MAX_FILTERS); |
876 | 71.0k | context->compcode_meta = header->compcode_meta; |
877 | | |
878 | 71.0k | context->filter_flags = filters_to_flags(header->filters); |
879 | 71.0k | context->special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK; |
880 | | |
881 | 71.0k | is_lazy = (context->blosc2_flags & 0x08u); |
882 | 71.0k | } |
883 | 9.97k | else { |
884 | 9.97k | context->header_overhead = BLOSC_MIN_HEADER_LENGTH; |
885 | 9.97k | context->filter_flags = get_filter_flags(context->header_flags, context->typesize); |
886 | 9.97k | flags_to_filters(context->header_flags, context->filters); |
887 | 9.97k | } |
888 | | |
889 | | // Some checks for malformed headers |
890 | 81.0k | if (!is_lazy && header->cbytes > context->srcsize) { |
891 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
892 | 0 | } |
893 | | |
894 | 81.0k | return 0; |
895 | 81.0k | } |
896 | | |
897 | | |
898 | 0 | int fill_filter(blosc2_filter *filter) { |
899 | 0 | char libpath[PATH_MAX]; |
900 | 0 | void *lib = load_lib(filter->name, libpath); |
901 | 0 | if(lib == NULL) { |
902 | 0 | BLOSC_TRACE_ERROR("Error while loading the library"); |
903 | 0 | return BLOSC2_ERROR_FAILURE; |
904 | 0 | } |
905 | | |
906 | 0 | filter_info *info = dlsym(lib, "info"); |
907 | 0 | filter->forward = dlsym(lib, info->forward); |
908 | 0 | filter->backward = dlsym(lib, info->backward); |
909 | |
|
910 | 0 | if (filter->forward == NULL || filter->backward == NULL){ |
911 | 0 | BLOSC_TRACE_ERROR("Wrong library loaded"); |
912 | 0 | dlclose(lib); |
913 | 0 | return BLOSC2_ERROR_FAILURE; |
914 | 0 | } |
915 | | |
916 | 0 | return BLOSC2_ERROR_SUCCESS; |
917 | 0 | } |
918 | | |
919 | | |
920 | 8 | int fill_codec(blosc2_codec *codec) { |
921 | 8 | char libpath[PATH_MAX]; |
922 | 8 | void *lib = load_lib(codec->compname, libpath); |
923 | 8 | if(lib == NULL) { |
924 | 8 | BLOSC_TRACE_ERROR("Error while loading the library for codec `%s`", codec->compname); |
925 | 8 | return BLOSC2_ERROR_FAILURE; |
926 | 8 | } |
927 | | |
928 | 0 | codec_info *info = dlsym(lib, "info"); |
929 | 0 | if (info == NULL) { |
930 | 0 | BLOSC_TRACE_ERROR("`info` symbol cannot be loaded from plugin `%s`", codec->compname); |
931 | 0 | dlclose(lib); |
932 | 0 | return BLOSC2_ERROR_FAILURE; |
933 | 0 | } |
934 | 0 | codec->encoder = dlsym(lib, info->encoder); |
935 | 0 | codec->decoder = dlsym(lib, info->decoder); |
936 | |
|
937 | 0 | if (codec->encoder == NULL || codec->decoder == NULL) { |
938 | 0 | BLOSC_TRACE_ERROR("encoder or decoder cannot be loaded from plugin `%s`", codec->compname); |
939 | 0 | dlclose(lib); |
940 | 0 | return BLOSC2_ERROR_FAILURE; |
941 | 0 | } |
942 | | |
943 | | /* If ever add .free function in future for codec params |
944 | | codecparams_info *info2 = dlsym(lib, "info2"); |
945 | | if (info2 != NULL) { |
946 | | // New plugin (e.g. openzl) with free function for codec_params defined |
947 | | // will be used when destroying context in blosc2_free_ctx |
948 | | codec->free = dlsym(lib, info2->free); |
949 | | } |
950 | | else{ |
951 | | codec->free = NULL; |
952 | | } |
953 | | */ |
954 | | |
955 | 0 | return BLOSC2_ERROR_SUCCESS; |
956 | 0 | } |
957 | | |
958 | | |
959 | 0 | int fill_tuner(blosc2_tuner *tuner) { |
960 | 0 | char libpath[PATH_MAX] = {0}; |
961 | 0 | void *lib = load_lib(tuner->name, libpath); |
962 | 0 | if(lib == NULL) { |
963 | 0 | BLOSC_TRACE_ERROR("Error while loading the library"); |
964 | 0 | return BLOSC2_ERROR_FAILURE; |
965 | 0 | } |
966 | | |
967 | 0 | tuner_info *info = dlsym(lib, "info"); |
968 | 0 | tuner->init = dlsym(lib, info->init); |
969 | 0 | tuner->update = dlsym(lib, info->update); |
970 | 0 | tuner->next_blocksize = dlsym(lib, info->next_blocksize); |
971 | 0 | tuner->free = dlsym(lib, info->free); |
972 | 0 | tuner->next_cparams = dlsym(lib, info->next_cparams); |
973 | |
|
974 | 0 | if (tuner->init == NULL || tuner->update == NULL || tuner->next_blocksize == NULL || tuner->free == NULL |
975 | 0 | || tuner->next_cparams == NULL){ |
976 | 0 | BLOSC_TRACE_ERROR("Wrong library loaded"); |
977 | 0 | dlclose(lib); |
978 | 0 | return BLOSC2_ERROR_FAILURE; |
979 | 0 | } |
980 | | |
981 | 0 | return BLOSC2_ERROR_SUCCESS; |
982 | 0 | } |
983 | | |
984 | | |
985 | 63.5k | static int blosc2_initialize_header_from_context(blosc2_context* context, blosc_header* header, bool extended_header) { |
986 | 63.5k | int32_t header_blocksize = (int32_t)(context->header_blocksize > 0 ? context->header_blocksize : context->blocksize); |
987 | 63.5k | if ((context->blosc2_flags2 & BLOSC2_VL_BLOCKS) == 0 && |
988 | 63.5k | context->sourcesize > 0 && header_blocksize > context->sourcesize) { |
989 | 0 | header_blocksize = (int32_t)context->sourcesize; |
990 | 0 | } |
991 | 63.5k | memset(header, 0, sizeof(blosc_header)); |
992 | | |
993 | 63.5k | header->version = (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) ? |
994 | 0 | BLOSC2_VERSION_FORMAT_VL_BLOCKS : |
995 | 63.5k | BLOSC2_VERSION_FORMAT_STABLE; |
996 | 63.5k | if (context->blosc1_format) { |
997 | 0 | header->version = BLOSC1_VERSION_FORMAT; |
998 | 0 | } |
999 | 63.5k | header->versionlz = compcode_to_compversion(context->compcode); |
1000 | 63.5k | header->flags = context->header_flags; |
1001 | 63.5k | header->typesize = (uint8_t)context->typesize; |
1002 | 63.5k | header->nbytes = (int32_t)context->sourcesize; |
1003 | 63.5k | header->blocksize = header_blocksize; |
1004 | | |
1005 | 63.5k | int little_endian = is_little_endian(); |
1006 | 63.5k | if (!little_endian) { |
1007 | 0 | header->nbytes = bswap32_(header->nbytes); |
1008 | 0 | header->blocksize = bswap32_(header->blocksize); |
1009 | | // cbytes written after compression |
1010 | 0 | } |
1011 | | |
1012 | 63.5k | if (extended_header) { |
1013 | | /* Store filter pipeline info at the end of the header */ |
1014 | 445k | for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
1015 | 381k | header->filters[i] = context->filters[i]; |
1016 | 381k | header->filters_meta[i] = context->filters_meta[i]; |
1017 | 381k | } |
1018 | 63.5k | header->udcompcode = context->compcode; |
1019 | 63.5k | header->compcode_meta = context->compcode_meta; |
1020 | 63.5k | header->blosc2_flags2 = context->blosc2_flags2; |
1021 | | |
1022 | 63.5k | if (!little_endian) { |
1023 | 0 | header->blosc2_flags |= BLOSC2_BIGENDIAN; |
1024 | 0 | } |
1025 | 63.5k | if (context->use_dict) { |
1026 | 0 | header->blosc2_flags |= BLOSC2_USEDICT; |
1027 | 0 | } |
1028 | 63.5k | if (context->blosc2_flags & BLOSC2_INSTR_CODEC) { |
1029 | 0 | header->blosc2_flags |= BLOSC2_INSTR_CODEC; |
1030 | 0 | } |
1031 | 63.5k | } |
1032 | | |
1033 | 63.5k | return 0; |
1034 | 63.5k | } |
1035 | | |
1036 | | /* Cycle buffers for the compression pipeline. The input block is read-only |
1037 | | (and the DELTA filter needs it unmodified as a reference), so it must never |
1038 | | be recycled as a destination; ping-pong between the two scratch buffers. */ |
1039 | | static void _cycle_buffers_forward(uint8_t **src, uint8_t **dest, uint8_t **tmp, |
1040 | 271k | const uint8_t *input) { |
1041 | 271k | uint8_t *prev_src = *src; |
1042 | 271k | *src = *dest; |
1043 | 271k | *dest = (prev_src == input) ? *tmp : prev_src; |
1044 | 271k | } |
1045 | | |
1046 | | uint8_t* pipeline_forward(struct thread_context* thread_context, const int32_t bsize, |
1047 | | const uint8_t* src, const int32_t offset, |
1048 | 271k | uint8_t* dest, uint8_t* tmp) { |
1049 | 271k | blosc2_context* context = thread_context->parent_context; |
1050 | 271k | uint8_t* _src = (uint8_t*)src + offset; |
1051 | 271k | uint8_t* _tmp = tmp; |
1052 | 271k | uint8_t* _dest = dest; |
1053 | 271k | int32_t typesize = context->typesize; |
1054 | 271k | uint8_t* filters = context->filters; |
1055 | 271k | uint8_t* filters_meta = context->filters_meta; |
1056 | 271k | bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
1057 | 271k | bool output_is_disposable = (context->preparams != NULL) ? context->preparams->output_is_disposable : false; |
1058 | | |
1059 | | /* Prefilter function */ |
1060 | 271k | if (context->prefilter != NULL) { |
1061 | | // Create new prefilter parameters for this block (must be private for each thread) |
1062 | 0 | blosc2_prefilter_params preparams; |
1063 | 0 | memcpy(&preparams, context->preparams, sizeof(preparams)); |
1064 | | // Calculate output_size based on number of elements and output typesize |
1065 | 0 | int32_t nelems = bsize / typesize; // number of elements in the input block |
1066 | | // If output_typesize is not set (0), default to input typesize (no type conversion) |
1067 | 0 | int32_t output_typesize_actual = (preparams.output_typesize > 0) ? preparams.output_typesize : typesize; |
1068 | 0 | int32_t output_size = nelems * output_typesize_actual; // output size in bytes |
1069 | 0 | preparams.output_typesize = output_typesize_actual; // ensure it's set |
1070 | | /* Set unwritten values to zero */ |
1071 | 0 | if (!output_is_disposable) { |
1072 | 0 | memset(_dest, 0, output_size); |
1073 | 0 | } |
1074 | 0 | preparams.input = _src; |
1075 | 0 | preparams.output = _dest; |
1076 | 0 | preparams.output_size = output_size; |
1077 | 0 | preparams.output_offset = offset; |
1078 | 0 | preparams.nblock = offset / context->blocksize; |
1079 | 0 | preparams.nchunk = context->schunk != NULL ? context->schunk->current_nchunk : -1; |
1080 | 0 | preparams.tid = thread_context->tid; |
1081 | 0 | preparams.ttmp = thread_context->tmp; |
1082 | 0 | preparams.ttmp_nbytes = thread_context->tmp_nbytes; |
1083 | 0 | preparams.ctx = context; |
1084 | 0 | preparams.output_is_disposable = output_is_disposable; |
1085 | |
|
1086 | 0 | if (context->prefilter(&preparams) != 0) { |
1087 | 0 | if (output_is_disposable) { |
1088 | | // Output is going to be discarded; no more filters are required |
1089 | 0 | BLOSC_TRACE_INFO("Output is disposable"); |
1090 | 0 | return _dest; |
1091 | 0 | } |
1092 | 0 | BLOSC_TRACE_ERROR("Execution of prefilter function failed"); |
1093 | 0 | return NULL; |
1094 | 0 | } |
1095 | | |
1096 | 0 | if (memcpyed) { |
1097 | | // No more filters are required |
1098 | 0 | return _dest; |
1099 | 0 | } |
1100 | 0 | _cycle_buffers_forward(&_src, &_dest, &_tmp, src + offset); |
1101 | 0 | } |
1102 | | |
1103 | | /* Process the filter pipeline */ |
1104 | 1.89M | for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
1105 | 1.62M | int rc = BLOSC2_ERROR_SUCCESS; |
1106 | 1.62M | if (filters[i] <= BLOSC2_DEFINED_FILTERS_STOP) { |
1107 | 1.62M | switch (filters[i]) { |
1108 | 22.4k | case BLOSC_SHUFFLE: |
1109 | | // if filters_meta is different to 0, interpret it as grouped bytes to shuffle |
1110 | 22.4k | blosc2_shuffle(filters_meta[i] == 0 ? typesize : filters_meta[i], bsize, _src, _dest); |
1111 | 22.4k | break; |
1112 | 248k | case BLOSC_BITSHUFFLE: |
1113 | 248k | if (context->blosc1_format && ((bsize / typesize) % 8) != 0) { |
1114 | | // Format version 2 (Blosc1) only bit-shuffles blocks whose number of |
1115 | | // elements is a multiple of 8; other blocks are stored unshuffled. |
1116 | 0 | memcpy(_dest, _src, (size_t)bsize); |
1117 | 0 | } |
1118 | 248k | else if (blosc2_bitshuffle(typesize, bsize, _src, _dest) < 0) { |
1119 | 0 | return NULL; |
1120 | 0 | } |
1121 | 248k | break; |
1122 | 248k | case BLOSC_DELTA: |
1123 | 0 | delta_encoder(offset == 0 ? _src : src, offset, bsize, typesize, _src, _dest); |
1124 | 0 | break; |
1125 | 0 | case BLOSC_TRUNC_PREC: |
1126 | 0 | if (truncate_precision(filters_meta[i], typesize, bsize, _src, _dest) < 0) { |
1127 | 0 | return NULL; |
1128 | 0 | } |
1129 | 0 | break; |
1130 | 1.35M | default: |
1131 | 1.35M | if (filters[i] != BLOSC_NOFILTER) { |
1132 | 0 | BLOSC_TRACE_ERROR("Filter %d not handled during compression\n", filters[i]); |
1133 | 0 | return NULL; |
1134 | 0 | } |
1135 | 1.62M | } |
1136 | 1.62M | } |
1137 | 0 | else { |
1138 | | // Look for the filters_meta in user filters and run it |
1139 | 0 | for (uint64_t j = 0; j < g_nfilters; ++j) { |
1140 | 0 | if (g_filters[j].id == filters[i]) { |
1141 | 0 | if (g_filters[j].forward == NULL) { |
1142 | | // Dynamically load library |
1143 | 0 | if (fill_filter(&g_filters[j]) < 0) { |
1144 | 0 | BLOSC_TRACE_ERROR("Could not load filter %d\n", g_filters[j].id); |
1145 | 0 | return NULL; |
1146 | 0 | } |
1147 | 0 | } |
1148 | 0 | if (g_filters[j].forward != NULL) { |
1149 | 0 | blosc2_cparams cparams; |
1150 | 0 | blosc2_ctx_get_cparams(context, &cparams); |
1151 | 0 | rc = g_filters[j].forward(_src, _dest, bsize, filters_meta[i], &cparams, g_filters[j].id); |
1152 | 0 | } else { |
1153 | 0 | BLOSC_TRACE_ERROR("Forward function is NULL"); |
1154 | 0 | return NULL; |
1155 | 0 | } |
1156 | 0 | if (rc != BLOSC2_ERROR_SUCCESS) { |
1157 | 0 | BLOSC_TRACE_ERROR("User-defined filter %d failed during compression\n", filters[i]); |
1158 | 0 | return NULL; |
1159 | 0 | } |
1160 | 0 | goto urfiltersuccess; |
1161 | 0 | } |
1162 | 0 | } |
1163 | 0 | BLOSC_TRACE_ERROR("User-defined filter %d not found during compression\n", filters[i]); |
1164 | 0 | return NULL; |
1165 | | |
1166 | 0 | urfiltersuccess:; |
1167 | |
|
1168 | 0 | } |
1169 | | |
1170 | | // Cycle buffers when required |
1171 | 1.62M | if (filters[i] != BLOSC_NOFILTER) { |
1172 | 271k | _cycle_buffers_forward(&_src, &_dest, &_tmp, src + offset); |
1173 | 271k | } |
1174 | 1.62M | } |
1175 | 271k | return _src; |
1176 | 271k | } |
1177 | | |
1178 | | |
1179 | | // Optimized version for detecting runs. It compares 8 bytes values wherever possible. |
1180 | 461k | static bool get_run(const uint8_t* ip, const uint8_t* ip_bound) { |
1181 | 461k | uint8_t x = *ip; |
1182 | 461k | int64_t value, value2; |
1183 | | /* Broadcast the value for every byte in a 64-bit register */ |
1184 | 461k | memset(&value, x, 8); |
1185 | 10.8M | while (ip < (ip_bound - 8)) { |
1186 | | #if defined(BLOSC_STRICT_ALIGN) |
1187 | | memcpy(&value2, ip, 8); |
1188 | | #else |
1189 | 10.8M | value2 = *(int64_t*)ip; |
1190 | 10.8M | #endif |
1191 | 10.8M | if (value != value2) { |
1192 | | // Values differ. We don't have a run. |
1193 | 441k | return false; |
1194 | 441k | } |
1195 | 10.3M | else { |
1196 | 10.3M | ip += 8; |
1197 | 10.3M | } |
1198 | 10.8M | } |
1199 | | /* Look into the remainder */ |
1200 | 177k | while ((ip < ip_bound) && (*ip == x)) ip++; |
1201 | 20.6k | return ip == ip_bound ? true : false; |
1202 | 461k | } |
1203 | | |
1204 | | |
1205 | | /* Shuffle & compress a single block */ |
1206 | | static int blosc_c(struct thread_context* thread_context, int32_t bsize, |
1207 | | int32_t leftoverblock, int32_t ntbytes, int32_t destsize, |
1208 | | const uint8_t* src, const int32_t offset, uint8_t* dest, |
1209 | 461k | uint8_t* tmp, uint8_t* tmp2) { |
1210 | 461k | blosc2_context* context = thread_context->parent_context; |
1211 | 461k | int dont_split = (context->header_flags & 0x10) >> 4; |
1212 | 461k | bool vlblocks = (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) != 0; |
1213 | 461k | int dict_training = context->use_dict && context->dict_cdict == NULL; |
1214 | 461k | int32_t j, neblock, nstreams; |
1215 | 461k | int32_t cbytes; /* number of compressed bytes in split */ |
1216 | 461k | int32_t ctbytes = 0; /* number of compressed bytes in block */ |
1217 | 461k | int32_t maxout; |
1218 | 461k | int32_t typesize = context->typesize; |
1219 | 461k | bool output_is_disposable = (context->preparams != NULL) ? context->preparams->output_is_disposable : false; |
1220 | 461k | const char* compname; |
1221 | 461k | int accel; |
1222 | 461k | const uint8_t* _src; |
1223 | 461k | uint8_t *_tmp = tmp, *_tmp2 = tmp2; |
1224 | 461k | int last_filter_index = last_filter(context->filters, 'c'); |
1225 | 461k | bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
1226 | 461k | bool instr_codec = context->blosc2_flags & BLOSC2_INSTR_CODEC; |
1227 | 461k | blosc_timestamp_t last, current; |
1228 | 461k | float filter_time = 0.f; |
1229 | | |
1230 | 461k | if (instr_codec) { |
1231 | 0 | blosc_set_timestamp(&last); |
1232 | 0 | } |
1233 | | |
1234 | | // See whether we have a run here |
1235 | 461k | if (last_filter_index >= 0 || context->prefilter != NULL) { |
1236 | | /* Apply the filter pipeline just for the prefilter */ |
1237 | 271k | if (memcpyed && context->prefilter != NULL) { |
1238 | | // We only need the prefilter output |
1239 | 0 | _src = pipeline_forward(thread_context, bsize, src, offset, dest, _tmp2); |
1240 | 0 | if (_src == NULL) { |
1241 | 0 | return BLOSC2_ERROR_FILTER_PIPELINE; |
1242 | 0 | } |
1243 | 0 | return bsize; |
1244 | 0 | } |
1245 | | /* Apply regular filter pipeline */ |
1246 | 271k | _src = pipeline_forward(thread_context, bsize, src, offset, _tmp, _tmp2); |
1247 | 271k | if (_src == NULL) { |
1248 | 0 | return BLOSC2_ERROR_FILTER_PIPELINE; |
1249 | 0 | } |
1250 | 271k | } else { |
1251 | 190k | _src = src + offset; |
1252 | 190k | } |
1253 | | |
1254 | 461k | if (instr_codec) { |
1255 | 0 | blosc_set_timestamp(¤t); |
1256 | 0 | filter_time = (float) blosc_elapsed_secs(last, current); |
1257 | 0 | last = current; |
1258 | 0 | } |
1259 | | |
1260 | 461k | assert(context->clevel > 0); |
1261 | | |
1262 | | /* Calculate acceleration for different compressors */ |
1263 | 461k | accel = get_accel(context); |
1264 | | |
1265 | | /* The number of compressed data streams for this block */ |
1266 | 461k | if (!dont_split && !leftoverblock && !dict_training) { |
1267 | 22.3k | nstreams = (int32_t)typesize; |
1268 | 22.3k | } |
1269 | 439k | else { |
1270 | 439k | nstreams = 1; |
1271 | 439k | } |
1272 | 461k | neblock = bsize / nstreams; |
1273 | 906k | for (j = 0; j < nstreams; j++) { |
1274 | 461k | if (instr_codec) { |
1275 | 0 | blosc_set_timestamp(&last); |
1276 | 0 | } |
1277 | 461k | if (!dict_training) { |
1278 | 461k | dest += sizeof(int32_t); |
1279 | 461k | ntbytes += sizeof(int32_t); |
1280 | 461k | ctbytes += sizeof(int32_t); |
1281 | | |
1282 | 461k | if (!vlblocks && context->header_overhead == BLOSC_EXTENDED_HEADER_LENGTH && output_is_disposable) { |
1283 | | // Simulate a run of 0s |
1284 | 0 | BLOSC_TRACE_INFO("Output is disposable, simulating a run of 0s"); |
1285 | 0 | memset(dest - 4, 0, sizeof(int32_t)); |
1286 | 0 | continue; |
1287 | 0 | } |
1288 | | |
1289 | 461k | const uint8_t *ip = (uint8_t *) _src + j * neblock; |
1290 | 461k | const uint8_t *ipbound = (uint8_t *) _src + (j + 1) * neblock; |
1291 | | |
1292 | 461k | if (!vlblocks && context->header_overhead == BLOSC_EXTENDED_HEADER_LENGTH && get_run(ip, ipbound)) { |
1293 | | // A run |
1294 | 19.4k | int32_t value = _src[j * neblock]; |
1295 | 19.4k | if (ntbytes > destsize) { |
1296 | 6 | return 0; /* Non-compressible data */ |
1297 | 6 | } |
1298 | | |
1299 | 19.4k | if (instr_codec) { |
1300 | 0 | blosc_set_timestamp(¤t); |
1301 | 0 | int32_t instr_size = sizeof(blosc2_instr); |
1302 | 0 | ntbytes += instr_size; |
1303 | 0 | ctbytes += instr_size; |
1304 | 0 | if (ntbytes > destsize) { |
1305 | 0 | return 0; /* Non-compressible data */ |
1306 | 0 | } |
1307 | 0 | _sw32(dest - 4, instr_size); |
1308 | 0 | blosc2_instr *desti = (blosc2_instr *)dest; |
1309 | 0 | memset(desti, 0, sizeof(blosc2_instr)); |
1310 | | // Special values have an overhead of about 1 int32 |
1311 | 0 | int32_t ssize = value == 0 ? sizeof(int32_t) : sizeof(int32_t) + 1; |
1312 | 0 | desti->cratio = (float) neblock / (float) ssize; |
1313 | 0 | float ctime = (float) blosc_elapsed_secs(last, current); |
1314 | 0 | desti->cspeed = (float) neblock / ctime; |
1315 | 0 | desti->filter_speed = (float) neblock / filter_time; |
1316 | 0 | desti->flags[0] = 1; // mark a runlen |
1317 | 0 | dest += instr_size; |
1318 | 0 | continue; |
1319 | 0 | } |
1320 | | |
1321 | | // Encode the repeated byte in the first (LSB) byte of the length of the split. |
1322 | 19.4k | _sw32(dest - 4, -value); // write the value in two's complement |
1323 | 19.4k | if (value > 0) { |
1324 | | // Mark encoding as a run-length (== 0 is always a 0's run) |
1325 | 8.71k | ntbytes += 1; |
1326 | 8.71k | ctbytes += 1; |
1327 | 8.71k | if (ntbytes > destsize) { |
1328 | 9 | return 0; /* Non-compressible data */ |
1329 | 9 | } |
1330 | | // Set MSB bit (sign) to 1 (not really necessary here, but for demonstration purposes) |
1331 | | // dest[-1] |= 0x80; |
1332 | 8.70k | dest[0] = 0x1; // set run-length bit (0) in token |
1333 | 8.70k | dest += 1; |
1334 | 8.70k | } |
1335 | 19.4k | continue; |
1336 | 19.4k | } |
1337 | 461k | } |
1338 | | |
1339 | 442k | maxout = neblock; |
1340 | 442k | if (ntbytes + maxout > destsize && !instr_codec) { |
1341 | | /* avoid buffer * overrun */ |
1342 | 47.7k | maxout = destsize - ntbytes; |
1343 | 47.7k | if (maxout <= 0) { |
1344 | 120 | return 0; /* non-compressible block */ |
1345 | 120 | } |
1346 | 47.7k | } |
1347 | 442k | if (dict_training) { |
1348 | | // We are in the build dict state, so don't compress |
1349 | | // TODO: copy only a percentage for sampling |
1350 | 0 | memcpy(dest, _src + j * neblock, (unsigned int)neblock); |
1351 | 0 | cbytes = (int32_t)neblock; |
1352 | 0 | } |
1353 | 442k | else if (context->compcode == BLOSC_BLOSCLZ) { |
1354 | 45.3k | cbytes = blosclz_compress(context->clevel, _src + j * neblock, |
1355 | 45.3k | (int)neblock, dest, maxout, context); |
1356 | 45.3k | } |
1357 | 396k | else if (context->compcode == BLOSC_LZ4) { |
1358 | 13.7k | cbytes = lz4_wrap_compress((char*)_src + j * neblock, (size_t)neblock, |
1359 | 13.7k | (char*)dest, (size_t)maxout, accel, |
1360 | 13.7k | thread_context); |
1361 | 13.7k | } |
1362 | 383k | else if (context->compcode == BLOSC_LZ4HC) { |
1363 | 33.1k | cbytes = lz4hc_wrap_compress((char*)_src + j * neblock, (size_t)neblock, |
1364 | 33.1k | (char*)dest, (size_t)maxout, context->clevel, |
1365 | 33.1k | thread_context); |
1366 | 33.1k | } |
1367 | 350k | #if defined(HAVE_ZLIB) |
1368 | 350k | else if (context->compcode == BLOSC_ZLIB) { |
1369 | 38.8k | cbytes = zlib_wrap_compress((char*)_src + j * neblock, (size_t)neblock, |
1370 | 38.8k | (char*)dest, (size_t)maxout, context->clevel); |
1371 | 38.8k | } |
1372 | 311k | #endif /* HAVE_ZLIB */ |
1373 | 311k | #if defined(HAVE_ZSTD) |
1374 | 311k | else if (context->compcode == BLOSC_ZSTD) { |
1375 | 311k | cbytes = zstd_wrap_compress(thread_context, |
1376 | 311k | (char*)_src + j * neblock, (size_t)neblock, |
1377 | 311k | (char*)dest, (size_t)maxout, context->clevel); |
1378 | 311k | } |
1379 | 0 | #endif /* HAVE_ZSTD */ |
1380 | 0 | else if (context->compcode > BLOSC2_DEFINED_CODECS_STOP) { |
1381 | 0 | for (int i = 0; i < g_ncodecs; ++i) { |
1382 | 0 | if (g_codecs[i].compcode == context->compcode) { |
1383 | 0 | if (g_codecs[i].encoder == NULL) { |
1384 | | // Dynamically load codec plugin |
1385 | 0 | if (fill_codec(&g_codecs[i]) < 0) { |
1386 | 0 | BLOSC_TRACE_ERROR("Could not load codec %d.", g_codecs[i].compcode); |
1387 | 0 | return BLOSC2_ERROR_CODEC_SUPPORT; |
1388 | 0 | } |
1389 | 0 | } |
1390 | 0 | blosc2_cparams cparams; |
1391 | 0 | blosc2_ctx_get_cparams(context, &cparams); |
1392 | 0 | cbytes = g_codecs[i].encoder(_src + j * neblock, |
1393 | 0 | neblock, |
1394 | 0 | dest, |
1395 | 0 | maxout, |
1396 | 0 | context->compcode_meta, |
1397 | 0 | &cparams, |
1398 | 0 | context->src); |
1399 | 0 | goto urcodecsuccess; |
1400 | 0 | } |
1401 | 0 | } |
1402 | 0 | BLOSC_TRACE_ERROR("User-defined compressor codec %d not found during compression", context->compcode); |
1403 | 0 | return BLOSC2_ERROR_CODEC_SUPPORT; |
1404 | 0 | urcodecsuccess: |
1405 | 0 | ; |
1406 | 0 | } else { |
1407 | 0 | blosc2_compcode_to_compname(context->compcode, &compname); |
1408 | 0 | BLOSC_TRACE_ERROR("Blosc has not been compiled with '%s' compression support." |
1409 | 0 | "Please use one having it.", compname); |
1410 | 0 | return BLOSC2_ERROR_CODEC_SUPPORT; |
1411 | 0 | } |
1412 | | |
1413 | 442k | if (cbytes > maxout) { |
1414 | | /* Buffer overrun caused by compression (should never happen) */ |
1415 | 0 | return BLOSC2_ERROR_WRITE_BUFFER; |
1416 | 0 | } |
1417 | 442k | if (cbytes < 0) { |
1418 | | /* cbytes should never be negative */ |
1419 | 0 | return BLOSC2_ERROR_DATA; |
1420 | 0 | } |
1421 | 442k | if (cbytes == 0) { |
1422 | | // When cbytes is 0, the compressor has not been able to compress anything |
1423 | 105k | cbytes = neblock; |
1424 | 105k | } |
1425 | | |
1426 | 442k | if (instr_codec) { |
1427 | 0 | blosc_set_timestamp(¤t); |
1428 | 0 | int32_t instr_size = sizeof(blosc2_instr); |
1429 | 0 | ntbytes += instr_size; |
1430 | 0 | ctbytes += instr_size; |
1431 | 0 | if (ntbytes > destsize) { |
1432 | 0 | return 0; /* Non-compressible data */ |
1433 | 0 | } |
1434 | 0 | _sw32(dest - 4, vlblocks ? neblock : instr_size); |
1435 | 0 | float ctime = (float)blosc_elapsed_secs(last, current); |
1436 | 0 | blosc2_instr *desti = (blosc2_instr *)dest; |
1437 | 0 | memset(desti, 0, sizeof(blosc2_instr)); |
1438 | | // cratio is computed having into account 1 additional int (csize) |
1439 | 0 | desti->cratio = (float)neblock / (float)(cbytes + sizeof(int32_t)); |
1440 | 0 | desti->cspeed = (float)neblock / ctime; |
1441 | 0 | desti->filter_speed = (float) neblock / filter_time; |
1442 | 0 | dest += instr_size; |
1443 | 0 | continue; |
1444 | 0 | } |
1445 | | |
1446 | 442k | if (!dict_training) { |
1447 | 442k | if (cbytes == neblock) { |
1448 | | /* The compressor has been unable to compress data at all. */ |
1449 | | /* Before doing the copy, check that we are not running into a |
1450 | | buffer overflow. */ |
1451 | 106k | if ((ntbytes + neblock) > destsize) { |
1452 | 17.1k | return 0; /* Non-compressible data */ |
1453 | 17.1k | } |
1454 | 89.3k | memcpy(dest, _src + j * neblock, (unsigned int)neblock); |
1455 | 89.3k | cbytes = neblock; |
1456 | 89.3k | } |
1457 | 425k | _sw32(dest - 4, vlblocks ? neblock : cbytes); |
1458 | 425k | } |
1459 | 425k | dest += cbytes; |
1460 | 425k | ntbytes += cbytes; |
1461 | 425k | ctbytes += cbytes; |
1462 | 425k | } /* Closes j < nstreams */ |
1463 | | |
1464 | 444k | return ctbytes; |
1465 | 461k | } |
1466 | | |
1467 | | |
1468 | | static void delta_dref_sync(blosc2_context* context, blosc2_pthread_mutex_t** delta_mutex, |
1469 | 0 | blosc2_pthread_cond_t** delta_cv, int** dref_not_init) { |
1470 | 0 | struct blosc_job_group* job = context->job; |
1471 | 0 | *delta_mutex = job != NULL ? &job->delta_mutex : &context->delta_mutex; |
1472 | 0 | *delta_cv = job != NULL ? &job->delta_cv : &context->delta_cv; |
1473 | 0 | *dref_not_init = job != NULL ? &job->dref_not_init : &context->dref_not_init; |
1474 | 0 | } |
1475 | | |
1476 | | |
1477 | 0 | static void delta_wait_dref(blosc2_context* context) { |
1478 | 0 | blosc2_pthread_mutex_t* delta_mutex; |
1479 | 0 | blosc2_pthread_cond_t* delta_cv; |
1480 | 0 | int* dref_not_init; |
1481 | |
|
1482 | 0 | delta_dref_sync(context, &delta_mutex, &delta_cv, &dref_not_init); |
1483 | 0 | blosc2_pthread_mutex_lock(delta_mutex); |
1484 | 0 | while (*dref_not_init) { |
1485 | 0 | blosc2_pthread_cond_wait(delta_cv, delta_mutex); |
1486 | 0 | } |
1487 | 0 | blosc2_pthread_mutex_unlock(delta_mutex); |
1488 | 0 | } |
1489 | | |
1490 | | |
1491 | 0 | static void delta_release_dref(blosc2_context* context) { |
1492 | 0 | blosc2_pthread_mutex_t* delta_mutex; |
1493 | 0 | blosc2_pthread_cond_t* delta_cv; |
1494 | 0 | int* dref_not_init; |
1495 | |
|
1496 | 0 | delta_dref_sync(context, &delta_mutex, &delta_cv, &dref_not_init); |
1497 | 0 | blosc2_pthread_mutex_lock(delta_mutex); |
1498 | 0 | if (*dref_not_init) { |
1499 | 0 | *dref_not_init = 0; |
1500 | 0 | blosc2_pthread_cond_broadcast(delta_cv); |
1501 | 0 | } |
1502 | 0 | blosc2_pthread_mutex_unlock(delta_mutex); |
1503 | 0 | } |
1504 | | |
1505 | | |
1506 | | /* Process the filter pipeline (decompression mode) */ |
1507 | | int pipeline_backward(struct thread_context* thread_context, const int32_t bsize, uint8_t* dest, |
1508 | | const int32_t offset, uint8_t* src, uint8_t* tmp, |
1509 | 99.6k | uint8_t* tmp2, int last_filter_index, int32_t nblock) { |
1510 | 99.6k | (void)tmp2; |
1511 | 99.6k | blosc2_context* context = thread_context->parent_context; |
1512 | 99.6k | int32_t typesize = context->typesize; |
1513 | 99.6k | uint8_t* filters = context->filters; |
1514 | 99.6k | uint8_t* filters_meta = context->filters_meta; |
1515 | 99.6k | uint8_t* _src = (next_filter(filters, BLOSC2_MAX_FILTERS, 'd') == BLOSC_DELTA && |
1516 | 87.1k | context->postfilter == NULL) ? (dest + offset) : src; |
1517 | 99.6k | uint8_t* _dest = tmp; |
1518 | 99.6k | int errcode = 0; |
1519 | | |
1520 | 298k | for (int i = BLOSC2_MAX_FILTERS - 1; i >= 0; i--) { |
1521 | | // Delta filter requires the whole chunk ready |
1522 | 298k | int last_copy_filter = (last_filter_index == i) || (filters[i] == BLOSC_DELTA) || |
1523 | 164k | (next_filter(filters, i, 'd') == BLOSC_DELTA); |
1524 | 298k | if (last_copy_filter && context->postfilter == NULL && |
1525 | 167k | (filters[i] == BLOSC_DELTA || _src != dest + offset)) { |
1526 | 148k | _dest = dest + offset; |
1527 | 150k | } else { |
1528 | 150k | _dest = (_src == src) ? tmp : src; |
1529 | 150k | } |
1530 | 298k | int rc = BLOSC2_ERROR_SUCCESS; |
1531 | 298k | if (filters[i] <= BLOSC2_DEFINED_FILTERS_STOP) { |
1532 | 246k | switch (filters[i]) { |
1533 | 52.2k | case BLOSC_SHUFFLE: |
1534 | | // if filters_meta is not 0, interpret as number of bytes to be grouped together for shuffle |
1535 | 52.2k | blosc2_unshuffle(filters_meta[i] == 0 ? typesize : filters_meta[i], bsize, _src, _dest); |
1536 | 52.2k | break; |
1537 | 78.7k | case BLOSC_BITSHUFFLE: |
1538 | 78.7k | if (bitunshuffle(typesize, bsize, _src, _dest, context->src[BLOSC2_CHUNK_VERSION]) < 0) { |
1539 | 0 | return BLOSC2_ERROR_FILTER_PIPELINE; |
1540 | 0 | } |
1541 | 78.7k | break; |
1542 | 78.7k | case BLOSC_DELTA: |
1543 | 40.7k | if (_src != _dest) { |
1544 | 813 | memcpy(_dest, _src, (size_t)bsize); |
1545 | 813 | } |
1546 | 40.7k | if (offset != 0 && thread_context != context->serial_context) { |
1547 | 0 | delta_wait_dref(context); |
1548 | 0 | } |
1549 | 40.7k | delta_decoder(offset == 0 ? _dest : dest, offset, bsize, typesize, _dest); |
1550 | 40.7k | break; |
1551 | 30.9k | case BLOSC_TRUNC_PREC: |
1552 | | // TRUNC_PREC filter does not need to be undone |
1553 | 30.9k | break; |
1554 | 43.7k | default: |
1555 | 43.7k | if (filters[i] != BLOSC_NOFILTER) { |
1556 | 169 | BLOSC_TRACE_ERROR("Filter %d not handled during decompression.", |
1557 | 169 | filters[i]); |
1558 | 169 | errcode = -1; |
1559 | 169 | } |
1560 | 246k | } |
1561 | 246k | } else { |
1562 | | // Look for the filters_meta in user filters and run it |
1563 | 192k | for (uint64_t j = 0; j < g_nfilters; ++j) { |
1564 | 191k | if (g_filters[j].id == filters[i]) { |
1565 | 52.1k | if (g_filters[j].backward == NULL) { |
1566 | | // Dynamically load filter |
1567 | 0 | if (fill_filter(&g_filters[j]) < 0) { |
1568 | 0 | BLOSC_TRACE_ERROR("Could not load filter %d.", g_filters[j].id); |
1569 | 0 | return BLOSC2_ERROR_FILTER_PIPELINE; |
1570 | 0 | } |
1571 | 0 | } |
1572 | 52.1k | if (g_filters[j].backward != NULL) { |
1573 | 52.1k | blosc2_dparams dparams; |
1574 | 52.1k | blosc2_ctx_get_dparams(context, &dparams); |
1575 | 52.1k | rc = g_filters[j].backward(_src, _dest, bsize, filters_meta[i], &dparams, g_filters[j].id); |
1576 | 52.1k | } else { |
1577 | 0 | BLOSC_TRACE_ERROR("Backward function is NULL"); |
1578 | 0 | return BLOSC2_ERROR_FILTER_PIPELINE; |
1579 | 0 | } |
1580 | 52.1k | if (rc != BLOSC2_ERROR_SUCCESS) { |
1581 | 34 | BLOSC_TRACE_ERROR("User-defined filter %d failed during decompression.", filters[i]); |
1582 | 34 | return rc; |
1583 | 34 | } |
1584 | 52.0k | goto urfiltersuccess; |
1585 | 52.1k | } |
1586 | 191k | } |
1587 | 146 | BLOSC_TRACE_ERROR("User-defined filter %d not found during decompression.", filters[i]); |
1588 | 146 | return BLOSC2_ERROR_FILTER_PIPELINE; |
1589 | 52.0k | urfiltersuccess:; |
1590 | 52.0k | } |
1591 | | |
1592 | | // Update input buffer for the next filter in the backward pipeline |
1593 | 298k | if ((filters[i] != BLOSC_NOFILTER) && (filters[i] != BLOSC_TRUNC_PREC)) { |
1594 | 223k | _src = _dest; |
1595 | 223k | } |
1596 | 298k | if (last_filter_index == i) { |
1597 | 99.4k | break; |
1598 | 99.4k | } |
1599 | 298k | } |
1600 | | |
1601 | 99.4k | if (context->postfilter == NULL && _src != dest + offset) { |
1602 | 5.49k | memcpy(dest + offset, _src, (unsigned int)bsize); |
1603 | 5.49k | } |
1604 | | |
1605 | | /* Postfilter function */ |
1606 | 99.4k | if (context->postfilter != NULL) { |
1607 | | // Create new postfilter parameters for this block (must be private for each thread) |
1608 | 0 | blosc2_postfilter_params postparams; |
1609 | 0 | memcpy(&postparams, context->postparams, sizeof(postparams)); |
1610 | 0 | postparams.input = _src; |
1611 | 0 | postparams.output = dest + offset; |
1612 | 0 | postparams.size = bsize; |
1613 | 0 | postparams.typesize = typesize; |
1614 | 0 | postparams.offset = nblock * context->blocksize; |
1615 | 0 | postparams.nchunk = context->schunk != NULL ? context->schunk->current_nchunk : -1; |
1616 | 0 | postparams.nblock = nblock; |
1617 | 0 | postparams.tid = thread_context->tid; |
1618 | 0 | postparams.ttmp = thread_context->tmp; |
1619 | 0 | postparams.ttmp_nbytes = thread_context->tmp_nbytes; |
1620 | 0 | postparams.ctx = context; |
1621 | |
|
1622 | 0 | if (context->postfilter(&postparams) != 0) { |
1623 | 0 | BLOSC_TRACE_ERROR("Execution of postfilter function failed"); |
1624 | 0 | return BLOSC2_ERROR_POSTFILTER; |
1625 | 0 | } |
1626 | 0 | } |
1627 | | |
1628 | 99.4k | return errcode; |
1629 | 99.4k | } |
1630 | | |
1631 | | |
1632 | 22.5k | static int32_t set_nans(int32_t typesize, uint8_t* dest, int32_t destsize) { |
1633 | 22.5k | if (destsize % typesize != 0) { |
1634 | 2 | BLOSC_TRACE_ERROR("destsize can only be a multiple of typesize"); |
1635 | 2 | BLOSC_ERROR(BLOSC2_ERROR_FAILURE); |
1636 | 2 | } |
1637 | 22.5k | int32_t nitems = destsize / typesize; |
1638 | 22.5k | if (nitems == 0) { |
1639 | 0 | return 0; |
1640 | 0 | } |
1641 | | |
1642 | 22.5k | if (typesize == 4) { |
1643 | 22.1k | float* dest_ = (float*)dest; |
1644 | 22.1k | float val = nanf(""); |
1645 | 2.28M | for (int i = 0; i < nitems; i++) { |
1646 | 2.26M | dest_[i] = val; |
1647 | 2.26M | } |
1648 | 22.1k | return nitems; |
1649 | 22.1k | } |
1650 | 396 | else if (typesize == 8) { |
1651 | 384 | double* dest_ = (double*)dest; |
1652 | 384 | double val = nan(""); |
1653 | 26.9k | for (int i = 0; i < nitems; i++) { |
1654 | 26.5k | dest_[i] = val; |
1655 | 26.5k | } |
1656 | 384 | return nitems; |
1657 | 384 | } |
1658 | | |
1659 | 12 | BLOSC_TRACE_ERROR("Unsupported typesize for NaN"); |
1660 | 12 | return BLOSC2_ERROR_DATA; |
1661 | 22.5k | } |
1662 | | |
1663 | | |
1664 | 215 | static int32_t set_values(int32_t typesize, const uint8_t* src, uint8_t* dest, int32_t destsize) { |
1665 | | #if defined(BLOSC_STRICT_ALIGN) |
1666 | | if (destsize % typesize != 0) { |
1667 | | BLOSC_ERROR(BLOSC2_ERROR_FAILURE); |
1668 | | } |
1669 | | int32_t nitems = destsize / typesize; |
1670 | | if (nitems == 0) { |
1671 | | return 0; |
1672 | | } |
1673 | | for (int i = 0; i < nitems; i++) { |
1674 | | memcpy(dest + i * typesize, src + BLOSC_EXTENDED_HEADER_LENGTH, typesize); |
1675 | | } |
1676 | | #else |
1677 | 215 | if (destsize % typesize != 0) { |
1678 | 2 | BLOSC_ERROR(BLOSC2_ERROR_FAILURE); |
1679 | 2 | } |
1680 | 213 | int32_t nitems = destsize / typesize; |
1681 | 213 | if (nitems == 0) { |
1682 | 0 | return 0; |
1683 | 0 | } |
1684 | | |
1685 | 213 | switch (typesize) { |
1686 | 14 | case 8: { |
1687 | 14 | int64_t val8 = ((int64_t*)(src + BLOSC_EXTENDED_HEADER_LENGTH))[0]; |
1688 | 14 | int64_t* dest8 = (int64_t*)dest; |
1689 | 42 | for (int i = 0; i < nitems; i++) { |
1690 | 28 | dest8[i] = val8; |
1691 | 28 | } |
1692 | 14 | break; |
1693 | 0 | } |
1694 | 34 | case 4: { |
1695 | 34 | int32_t val4 = ((int32_t*)(src + BLOSC_EXTENDED_HEADER_LENGTH))[0]; |
1696 | 34 | int32_t* dest4 = (int32_t*)dest; |
1697 | 450k | for (int i = 0; i < nitems; i++) { |
1698 | 450k | dest4[i] = val4; |
1699 | 450k | } |
1700 | 34 | break; |
1701 | 0 | } |
1702 | 61 | case 2: { |
1703 | 61 | int16_t val2 = ((int16_t*)(src + BLOSC_EXTENDED_HEADER_LENGTH))[0]; |
1704 | 61 | int16_t* dest2 = (int16_t*)dest; |
1705 | 294 | for (int i = 0; i < nitems; i++) { |
1706 | 233 | dest2[i] = val2; |
1707 | 233 | } |
1708 | 61 | break; |
1709 | 0 | } |
1710 | 81 | case 1: { |
1711 | 81 | int8_t val1 = ((int8_t*)(src + BLOSC_EXTENDED_HEADER_LENGTH))[0]; |
1712 | 81 | int8_t* dest1 = (int8_t*)dest; |
1713 | 335 | for (int i = 0; i < nitems; i++) { |
1714 | 254 | dest1[i] = val1; |
1715 | 254 | } |
1716 | 81 | break; |
1717 | 0 | } |
1718 | 23 | default: |
1719 | 84 | for (int i = 0; i < nitems; i++) { |
1720 | 61 | memcpy(dest + i * typesize, src + BLOSC_EXTENDED_HEADER_LENGTH, typesize); |
1721 | 61 | } |
1722 | 213 | } |
1723 | 213 | #endif |
1724 | | |
1725 | 213 | return nitems; |
1726 | 213 | } |
1727 | | |
1728 | | |
1729 | | /* Decompress & unshuffle a single block */ |
1730 | | static int blosc_d( |
1731 | | struct thread_context* thread_context, int32_t bsize, |
1732 | | int32_t leftoverblock, bool memcpyed, const uint8_t* src, int32_t srcsize, int32_t src_offset, |
1733 | 21.8M | int32_t nblock, uint8_t* dest, int32_t dest_offset, uint8_t* tmp, uint8_t* tmp2) { |
1734 | 21.8M | blosc2_context* context = thread_context->parent_context; |
1735 | 21.8M | uint8_t* filters = context->filters; |
1736 | 21.8M | uint8_t *tmp3 = thread_context->tmp4; |
1737 | 21.8M | bool vlblocks = (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) != 0; |
1738 | 21.8M | int32_t compformat = (context->header_flags & (uint8_t)0xe0) >> 5u; |
1739 | 21.8M | int dont_split = (context->header_flags & 0x10) >> 4; |
1740 | 21.8M | int32_t chunk_nbytes; |
1741 | 21.8M | int32_t chunk_cbytes; |
1742 | 21.8M | int nstreams; |
1743 | 21.8M | int32_t neblock; |
1744 | 21.8M | int32_t nbytes; /* number of decompressed bytes in split */ |
1745 | 21.8M | int32_t cbytes; /* number of compressed bytes in split */ |
1746 | | // int32_t ctbytes = 0; /* number of compressed bytes in block */ |
1747 | 21.8M | int32_t ntbytes = 0; /* number of uncompressed bytes in block */ |
1748 | 21.8M | uint8_t* _dest; |
1749 | 21.8M | int32_t typesize = context->typesize; |
1750 | 21.8M | bool instr_codec = context->blosc2_flags & BLOSC2_INSTR_CODEC; |
1751 | 21.8M | const char* compname; |
1752 | 21.8M | int rc; |
1753 | | |
1754 | 21.8M | if (context->block_maskout != NULL && context->block_maskout[nblock]) { |
1755 | | // Do not decompress, but act as if we successfully decompressed everything |
1756 | 0 | return bsize; |
1757 | 0 | } |
1758 | | |
1759 | 21.8M | rc = blosc2_cbuffer_sizes(src, &chunk_nbytes, &chunk_cbytes, NULL); |
1760 | 21.8M | if (rc < 0) { |
1761 | 0 | return rc; |
1762 | 0 | } |
1763 | 21.8M | if (context->special_type == BLOSC2_SPECIAL_VALUE) { |
1764 | | // We need the actual typesize in this case, but it cannot be encoded in the header, so derive it from cbytes |
1765 | 215 | typesize = chunk_cbytes - context->header_overhead; |
1766 | 215 | } |
1767 | | |
1768 | | // In some situations (lazychunks) the context can arrive uninitialized |
1769 | | // (but BITSHUFFLE needs it for accessing the format of the chunk) |
1770 | 21.8M | if (context->src == NULL) { |
1771 | 0 | context->src = src; |
1772 | 0 | } |
1773 | | |
1774 | | // Chunks with special values cannot be lazy |
1775 | 21.8M | bool is_lazy = ((context->header_overhead == BLOSC_EXTENDED_HEADER_LENGTH) && |
1776 | 21.8M | (context->blosc2_flags & 0x08u) && !context->special_type); |
1777 | 21.8M | if (is_lazy) { |
1778 | | // The chunk is on disk, so just lazily load the block |
1779 | 12 | if (context->schunk == NULL) { |
1780 | 8 | BLOSC_TRACE_ERROR("Lazy chunk needs an associated super-chunk."); |
1781 | 8 | return BLOSC2_ERROR_INVALID_PARAM; |
1782 | 8 | } |
1783 | 4 | if (context->schunk->frame == NULL) { |
1784 | 0 | BLOSC_TRACE_ERROR("Lazy chunk needs an associated frame."); |
1785 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
1786 | 0 | } |
1787 | 4 | blosc2_frame_s* frame = (blosc2_frame_s*)context->schunk->frame; |
1788 | 4 | size_t bstarts_nbytes; |
1789 | 4 | size_t trailer_offset; |
1790 | 4 | size_t block_csizes_nbytes; |
1791 | 4 | size_t trailer_meta_nbytes; |
1792 | 4 | size_t lazy_trailer_end; |
1793 | 4 | if (srcsize < 0 || context->nblocks < 0 || |
1794 | 4 | !blosc2_checked_mul_size((size_t)context->nblocks, sizeof(int32_t), &bstarts_nbytes) || |
1795 | 4 | !blosc2_checked_add_size((size_t)BLOSC_EXTENDED_HEADER_LENGTH, bstarts_nbytes, &trailer_offset) || |
1796 | 4 | !blosc2_checked_mul_size((size_t)context->nblocks, sizeof(int32_t), &block_csizes_nbytes) || |
1797 | 4 | !blosc2_checked_add_size(sizeof(int32_t) + sizeof(int64_t), block_csizes_nbytes, &trailer_meta_nbytes) || |
1798 | 4 | !blosc2_checked_add_size(trailer_offset, trailer_meta_nbytes, &lazy_trailer_end) || |
1799 | 4 | (size_t)srcsize < lazy_trailer_end) { |
1800 | 1 | BLOSC_TRACE_ERROR("Lazy trailer exceeds source buffer."); |
1801 | 1 | return BLOSC2_ERROR_READ_BUFFER; |
1802 | 1 | } |
1803 | 3 | int32_t nchunk; |
1804 | 3 | int64_t chunk_offset; |
1805 | | // The nchunk and the offset of the current chunk are in the trailer |
1806 | 3 | memcpy(&nchunk, src + trailer_offset, sizeof(nchunk)); |
1807 | 3 | memcpy(&chunk_offset, src + trailer_offset + sizeof(int32_t), sizeof(chunk_offset)); |
1808 | | // Get the csize of the nblock |
1809 | 3 | if (nblock < 0 || nblock >= context->nblocks) { |
1810 | 0 | BLOSC_TRACE_ERROR("Invalid block index in lazy trailer."); |
1811 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
1812 | 0 | } |
1813 | 3 | int32_t *block_csizes = (int32_t *)(src + trailer_offset + sizeof(int32_t) + sizeof(int64_t)); |
1814 | 3 | int32_t block_csize = block_csizes[nblock]; |
1815 | 3 | int32_t max_lazy_block_csize = context->blocksize + context->typesize * (signed)sizeof(int32_t); |
1816 | 3 | if (block_csize <= 0 || block_csize > max_lazy_block_csize) { |
1817 | 3 | BLOSC_TRACE_ERROR("Invalid lazy block size in trailer."); |
1818 | 3 | return BLOSC2_ERROR_INVALID_HEADER; |
1819 | 3 | } |
1820 | 0 | if (vlblocks && block_csize <= (int32_t)sizeof(int32_t)) { |
1821 | 0 | BLOSC_TRACE_ERROR("Lazy VL block compressed size is too small."); |
1822 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
1823 | 0 | } |
1824 | | // Read the lazy block on disk |
1825 | 0 | void* fp = NULL; |
1826 | 0 | blosc2_io_cb *io_cb = blosc2_get_io_cb(context->schunk->storage->io->id); |
1827 | 0 | if (io_cb == NULL) { |
1828 | 0 | BLOSC_TRACE_ERROR("Error getting the input/output API"); |
1829 | 0 | return BLOSC2_ERROR_PLUGIN_IO; |
1830 | 0 | } |
1831 | | |
1832 | 0 | int64_t io_pos = 0; |
1833 | 0 | if (frame->sframe) { |
1834 | | // The chunk is not in the frame |
1835 | 0 | fp = sframe_open_chunk(frame->urlpath, nchunk, "rb", context->schunk->storage->io); |
1836 | 0 | BLOSC_ERROR_NULL(fp, BLOSC2_ERROR_FILE_OPEN); |
1837 | | // The offset of the block is src_offset |
1838 | 0 | if (src_offset < 0) { |
1839 | 0 | frame_reader_release(frame, io_cb, fp); |
1840 | 0 | BLOSC_TRACE_ERROR("Lazy block offset cannot be negative."); |
1841 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
1842 | 0 | } |
1843 | 0 | io_pos = src_offset; |
1844 | 0 | } |
1845 | 0 | else { |
1846 | 0 | fp = frame_reader_acquire(frame, context->schunk->storage->io); |
1847 | 0 | BLOSC_ERROR_NULL(fp, BLOSC2_ERROR_FILE_OPEN); |
1848 | | // The offset of the block is src_offset |
1849 | 0 | if (src_offset < 0) { |
1850 | 0 | frame_reader_release(frame, io_cb, fp); |
1851 | 0 | BLOSC_TRACE_ERROR("Lazy block offset cannot be negative."); |
1852 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
1853 | 0 | } |
1854 | 0 | if (chunk_offset < 0) { |
1855 | 0 | frame_reader_release(frame, io_cb, fp); |
1856 | 0 | BLOSC_TRACE_ERROR("Lazy chunk offset cannot be negative."); |
1857 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
1858 | 0 | } |
1859 | 0 | if (!blosc2_checked_add_i64(frame->file_offset, chunk_offset, &io_pos)) { |
1860 | 0 | frame_reader_release(frame, io_cb, fp); |
1861 | 0 | BLOSC_TRACE_ERROR("Lazy chunk offset overflows file position."); |
1862 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
1863 | 0 | } |
1864 | 0 | if (!blosc2_checked_add_i64(io_pos, src_offset, &io_pos)) { |
1865 | 0 | frame_reader_release(frame, io_cb, fp); |
1866 | 0 | BLOSC_TRACE_ERROR("Lazy block offset overflows file position."); |
1867 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
1868 | 0 | } |
1869 | 0 | } |
1870 | | // We can make use of tmp3 because it will be used after src is not needed anymore |
1871 | 0 | int64_t rbytes = io_cb->read((void**)&tmp3, 1, block_csize, io_pos, fp); |
1872 | 0 | frame_reader_release(frame, io_cb, fp); |
1873 | 0 | if ((int32_t)rbytes != block_csize) { |
1874 | 0 | BLOSC_TRACE_ERROR("Cannot read the (lazy) block out of the fileframe."); |
1875 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
1876 | 0 | } |
1877 | 0 | src = tmp3; |
1878 | 0 | src_offset = 0; |
1879 | 0 | srcsize = block_csize; |
1880 | 0 | } |
1881 | | |
1882 | | // If the chunk is memcpyed, we just have to copy the block to dest and return |
1883 | 21.8M | if (memcpyed) { |
1884 | 21.6M | int bsize_ = leftoverblock ? chunk_nbytes % context->blocksize : bsize; |
1885 | 21.6M | if (!context->special_type) { |
1886 | 4.09k | if (chunk_nbytes + context->header_overhead != chunk_cbytes) { |
1887 | 0 | return BLOSC2_ERROR_WRITE_BUFFER; |
1888 | 0 | } |
1889 | 4.09k | if (chunk_cbytes < context->header_overhead + (nblock * context->blocksize) + bsize_) { |
1890 | | /* Not enough input to copy block */ |
1891 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
1892 | 0 | } |
1893 | 4.09k | } |
1894 | 21.6M | if (!is_lazy) { |
1895 | 21.6M | src += context->header_overhead + nblock * context->blocksize; |
1896 | 21.6M | } |
1897 | 21.6M | _dest = dest + dest_offset; |
1898 | 21.6M | if (context->postfilter != NULL) { |
1899 | | // We are making use of a postfilter, so use a temp for destination |
1900 | 0 | _dest = tmp; |
1901 | 0 | } |
1902 | 21.6M | rc = 0; |
1903 | 21.6M | switch (context->special_type) { |
1904 | 215 | case BLOSC2_SPECIAL_VALUE: |
1905 | | // All repeated values |
1906 | 215 | rc = set_values(typesize, context->src, _dest, bsize_); |
1907 | 215 | if (rc < 0) { |
1908 | 2 | BLOSC_TRACE_ERROR("set_values failed"); |
1909 | 2 | return BLOSC2_ERROR_DATA; |
1910 | 2 | } |
1911 | 213 | break; |
1912 | 22.5k | case BLOSC2_SPECIAL_NAN: |
1913 | 22.5k | rc = set_nans(context->typesize, _dest, bsize_); |
1914 | 22.5k | if (rc < 0) { |
1915 | 14 | BLOSC_TRACE_ERROR("set_nans failed"); |
1916 | 14 | return BLOSC2_ERROR_DATA; |
1917 | 14 | } |
1918 | 22.5k | break; |
1919 | 11.8M | case BLOSC2_SPECIAL_ZERO: |
1920 | 11.8M | memset(_dest, 0, bsize_); |
1921 | 11.8M | break; |
1922 | 9.79M | case BLOSC2_SPECIAL_UNINIT: |
1923 | | // We do nothing here |
1924 | 9.79M | break; |
1925 | 4.09k | default: |
1926 | 4.09k | memcpy(_dest, src, bsize_); |
1927 | 21.6M | } |
1928 | 21.6M | if (context->postfilter != NULL) { |
1929 | | // Create new postfilter parameters for this block (must be private for each thread) |
1930 | 0 | blosc2_postfilter_params postparams; |
1931 | 0 | memcpy(&postparams, context->postparams, sizeof(postparams)); |
1932 | 0 | postparams.input = tmp; |
1933 | 0 | postparams.output = dest + dest_offset; |
1934 | 0 | postparams.size = bsize; |
1935 | 0 | postparams.typesize = typesize; |
1936 | 0 | postparams.offset = nblock * context->blocksize; |
1937 | 0 | postparams.nchunk = context->schunk != NULL ? context->schunk->current_nchunk : -1; |
1938 | 0 | postparams.nblock = nblock; |
1939 | 0 | postparams.tid = thread_context->tid; |
1940 | 0 | postparams.ttmp = thread_context->tmp; |
1941 | 0 | postparams.ttmp_nbytes = thread_context->tmp_nbytes; |
1942 | 0 | postparams.ctx = context; |
1943 | | |
1944 | | // Execute the postfilter (the processed block will be copied to dest) |
1945 | 0 | if (context->postfilter(&postparams) != 0) { |
1946 | 0 | BLOSC_TRACE_ERROR("Execution of postfilter function failed"); |
1947 | 0 | return BLOSC2_ERROR_POSTFILTER; |
1948 | 0 | } |
1949 | 0 | } |
1950 | 21.6M | thread_context->zfp_cell_nitems = 0; |
1951 | | |
1952 | 21.6M | return bsize_; |
1953 | 21.6M | } |
1954 | | |
1955 | 151k | if (!is_lazy && (src_offset <= 0 || src_offset >= srcsize)) { |
1956 | | /* Invalid block src offset encountered */ |
1957 | 521 | return BLOSC2_ERROR_DATA; |
1958 | 521 | } |
1959 | | |
1960 | 150k | src += src_offset; |
1961 | 150k | if (vlblocks) { |
1962 | 4 | if (context->blockcbytes == NULL || nblock >= context->nblocks || |
1963 | 4 | context->blockcbytes[nblock] <= (int32_t)sizeof(int32_t) || |
1964 | 3 | src_offset > srcsize - context->blockcbytes[nblock]) { |
1965 | 1 | return BLOSC2_ERROR_DATA; |
1966 | 1 | } |
1967 | 3 | srcsize = context->blockcbytes[nblock]; |
1968 | 3 | } |
1969 | 150k | else { |
1970 | 150k | srcsize -= src_offset; |
1971 | 150k | } |
1972 | | |
1973 | 150k | int last_filter_index = last_filter(filters, 'd'); |
1974 | 150k | if (instr_codec) { |
1975 | | // If instrumented, we don't want to run the filters |
1976 | 4.08k | _dest = dest + dest_offset; |
1977 | 4.08k | } |
1978 | 146k | else if (((last_filter_index >= 0) && |
1979 | 115k | (next_filter(filters, BLOSC2_MAX_FILTERS, 'd') != BLOSC_DELTA)) || |
1980 | 100k | context->postfilter != NULL) { |
1981 | | // We are making use of some filter, so use a temp for destination |
1982 | 100k | _dest = tmp; |
1983 | 100k | } |
1984 | 46.1k | else { |
1985 | | // If no filters, or only DELTA in pipeline |
1986 | 46.1k | _dest = dest + dest_offset; |
1987 | 46.1k | } |
1988 | | |
1989 | | /* The number of compressed data streams for this block */ |
1990 | 150k | if (vlblocks) { |
1991 | 3 | nstreams = 1; |
1992 | 3 | } |
1993 | 150k | else if (!dont_split && !leftoverblock) { |
1994 | 9.44k | nstreams = context->typesize; |
1995 | 9.44k | } |
1996 | 141k | else { |
1997 | 141k | nstreams = 1; |
1998 | 141k | } |
1999 | | |
2000 | 150k | neblock = bsize / nstreams; |
2001 | 150k | if (neblock == 0) { |
2002 | | /* Not enough space to output bytes */ |
2003 | 3 | BLOSC_ERROR(BLOSC2_ERROR_WRITE_BUFFER); |
2004 | 3 | } |
2005 | | /* The splits must cover the whole block, or part of tmp is left unwritten */ |
2006 | 150k | if (neblock * nstreams != bsize) { |
2007 | 16 | BLOSC_ERROR(BLOSC2_ERROR_DATA); |
2008 | 16 | } |
2009 | 280k | for (int j = 0; j < nstreams; j++) { |
2010 | 151k | if (vlblocks) { |
2011 | 3 | if (srcsize < (signed)sizeof(int32_t)) { |
2012 | | /* Not enough input to read compressed bytes */ |
2013 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
2014 | 0 | } |
2015 | 3 | neblock = sw32_(src); |
2016 | 3 | if (neblock != bsize) { |
2017 | 0 | return BLOSC2_ERROR_DATA; |
2018 | 0 | } |
2019 | 3 | src += sizeof(int32_t); |
2020 | 3 | cbytes = srcsize - (int32_t)sizeof(int32_t); |
2021 | 3 | srcsize = 0; |
2022 | 3 | } |
2023 | 151k | else { |
2024 | 151k | if (srcsize < (signed)sizeof(int32_t)) { |
2025 | | /* Not enough input to read compressed size */ |
2026 | 10 | return BLOSC2_ERROR_READ_BUFFER; |
2027 | 10 | } |
2028 | 151k | srcsize -= sizeof(int32_t); |
2029 | 151k | cbytes = sw32_(src); /* amount of compressed bytes */ |
2030 | 151k | if (cbytes > 0) { |
2031 | 101k | if (srcsize < cbytes) { |
2032 | | /* Not enough input to read compressed bytes */ |
2033 | 102 | return BLOSC2_ERROR_READ_BUFFER; |
2034 | 102 | } |
2035 | 101k | srcsize -= cbytes; |
2036 | 101k | } |
2037 | 151k | src += sizeof(int32_t); |
2038 | 151k | } |
2039 | | // ctbytes += (signed)sizeof(int32_t); |
2040 | | |
2041 | | /* Uncompress */ |
2042 | 151k | if (!vlblocks && cbytes == 0) { |
2043 | | // A run of 0's |
2044 | 26.7k | memset(_dest, 0, (unsigned int)neblock); |
2045 | 26.7k | nbytes = neblock; |
2046 | 26.7k | } |
2047 | 124k | else if (!vlblocks && cbytes < 0) { |
2048 | | // A negative number means some encoding depending on the token that comes next |
2049 | 22.6k | uint8_t token; |
2050 | | |
2051 | 22.6k | if (srcsize < (signed)sizeof(uint8_t)) { |
2052 | | // Not enough input to read token */ |
2053 | 11 | return BLOSC2_ERROR_READ_BUFFER; |
2054 | 11 | } |
2055 | 22.5k | srcsize -= sizeof(uint8_t); |
2056 | | |
2057 | 22.5k | token = src[0]; |
2058 | 22.5k | src += 1; |
2059 | | // ctbytes += 1; |
2060 | | |
2061 | 22.5k | if (token & 0x1) { |
2062 | | // A run of bytes that are different than 0 |
2063 | 22.5k | if (cbytes < -255) { |
2064 | | // Runs can only encode a byte |
2065 | 74 | return BLOSC2_ERROR_RUN_LENGTH; |
2066 | 74 | } |
2067 | 22.4k | uint8_t value = -cbytes; |
2068 | 22.4k | memset(_dest, value, (unsigned int)neblock); |
2069 | 22.4k | } else { |
2070 | 27 | BLOSC_TRACE_ERROR("Invalid or unsupported compressed stream token value - %d", token); |
2071 | 27 | return BLOSC2_ERROR_RUN_LENGTH; |
2072 | 27 | } |
2073 | 22.4k | nbytes = neblock; |
2074 | 22.4k | cbytes = 0; // everything is encoded in the cbytes token |
2075 | 22.4k | } |
2076 | 101k | else if (cbytes == neblock) { |
2077 | 45.0k | memcpy(_dest, src, (unsigned int)neblock); |
2078 | 45.0k | nbytes = (int32_t)neblock; |
2079 | 45.0k | } |
2080 | 56.7k | else { |
2081 | 56.7k | if (compformat == BLOSC_BLOSCLZ_FORMAT) { |
2082 | 8.05k | nbytes = blosclz_decompress(src, cbytes, _dest, (int)neblock); |
2083 | 8.05k | } |
2084 | 48.6k | else if (compformat == BLOSC_LZ4_FORMAT) { |
2085 | 6.64k | nbytes = lz4_wrap_decompress((char*)src, (size_t)cbytes, |
2086 | 6.64k | (char*)_dest, (size_t)neblock, |
2087 | 6.64k | thread_context); |
2088 | 6.64k | } |
2089 | 42.0k | #if defined(HAVE_ZLIB) |
2090 | 42.0k | else if (compformat == BLOSC_ZLIB_FORMAT) { |
2091 | 8.53k | nbytes = zlib_wrap_decompress((char*)src, (size_t)cbytes, |
2092 | 8.53k | (char*)_dest, (size_t)neblock); |
2093 | 8.53k | } |
2094 | 33.5k | #endif /* HAVE_ZLIB */ |
2095 | 33.5k | #if defined(HAVE_ZSTD) |
2096 | 33.5k | else if (compformat == BLOSC_ZSTD_FORMAT) { |
2097 | 31.4k | nbytes = zstd_wrap_decompress(thread_context, |
2098 | 31.4k | (char*)src, (size_t)cbytes, |
2099 | 31.4k | (char*)_dest, (size_t)neblock); |
2100 | 31.4k | } |
2101 | 2.06k | #endif /* HAVE_ZSTD */ |
2102 | 2.06k | else if (compformat == BLOSC_UDCODEC_FORMAT) { |
2103 | 2.05k | bool getcell = false; |
2104 | | |
2105 | 2.05k | #if defined(HAVE_ZFP) |
2106 | 2.05k | if ((context->compcode == BLOSC_CODEC_ZFP_FIXED_RATE) && |
2107 | 2 | (thread_context->zfp_cell_nitems > 0)) { |
2108 | 0 | nbytes = zfp_getcell(thread_context, src, cbytes, _dest, neblock); |
2109 | 0 | if (nbytes < 0) { |
2110 | 0 | return BLOSC2_ERROR_DATA; |
2111 | 0 | } |
2112 | 0 | if (nbytes == thread_context->zfp_cell_nitems * typesize) { |
2113 | 0 | getcell = true; |
2114 | 0 | } |
2115 | 0 | } |
2116 | 2.05k | #endif /* HAVE_ZFP */ |
2117 | 2.05k | if (!getcell) { |
2118 | 2.05k | thread_context->zfp_cell_nitems = 0; |
2119 | 2.29k | for (int i = 0; i < g_ncodecs; ++i) { |
2120 | 2.27k | if (g_codecs[i].compcode == context->compcode) { |
2121 | 2.03k | if (g_codecs[i].decoder == NULL) { |
2122 | | // Dynamically load codec plugin |
2123 | 8 | if (fill_codec(&g_codecs[i]) < 0) { |
2124 | 8 | BLOSC_TRACE_ERROR("Could not load codec %d.", g_codecs[i].compcode); |
2125 | 8 | return BLOSC2_ERROR_CODEC_SUPPORT; |
2126 | 8 | } |
2127 | 8 | } |
2128 | 2.02k | blosc2_dparams dparams; |
2129 | 2.02k | blosc2_ctx_get_dparams(context, &dparams); |
2130 | 2.02k | nbytes = g_codecs[i].decoder(src, |
2131 | 2.02k | cbytes, |
2132 | 2.02k | _dest, |
2133 | 2.02k | neblock, |
2134 | 2.02k | context->compcode_meta, |
2135 | 2.02k | &dparams, |
2136 | 2.02k | context->src); |
2137 | 2.02k | goto urcodecsuccess; |
2138 | 2.03k | } |
2139 | 2.27k | } |
2140 | 20 | BLOSC_TRACE_ERROR("User-defined compressor codec %d not found during decompression", context->compcode); |
2141 | 20 | return BLOSC2_ERROR_CODEC_SUPPORT; |
2142 | 2.05k | } |
2143 | 2.02k | urcodecsuccess: |
2144 | 2.02k | ; |
2145 | 2.02k | } |
2146 | 6 | else { |
2147 | 6 | compname = clibcode_to_clibname(compformat); |
2148 | 6 | BLOSC_TRACE_ERROR( |
2149 | 6 | "Blosc has not been compiled with decompression " |
2150 | 6 | "support for '%s' format. " |
2151 | 6 | "Please recompile for adding this support.", compname); |
2152 | 6 | return BLOSC2_ERROR_CODEC_SUPPORT; |
2153 | 6 | } |
2154 | | |
2155 | | /* Check that decompressed bytes number is correct */ |
2156 | 56.7k | if ((nbytes != neblock) && (thread_context->zfp_cell_nitems == 0)) { |
2157 | 21.5k | return BLOSC2_ERROR_DATA; |
2158 | 21.5k | } |
2159 | | |
2160 | 56.7k | } |
2161 | 129k | src += cbytes; |
2162 | | // ctbytes += cbytes; |
2163 | 129k | _dest += nbytes; |
2164 | 129k | ntbytes += nbytes; |
2165 | 129k | } /* Closes j < nstreams */ |
2166 | | |
2167 | 129k | if (!instr_codec) { |
2168 | 127k | if (last_filter_index >= 0 || context->postfilter != NULL) { |
2169 | | /* Apply regular filter pipeline */ |
2170 | 99.6k | int errcode = pipeline_backward(thread_context, bsize, dest, dest_offset, tmp, tmp2, tmp3, |
2171 | 99.6k | last_filter_index, nblock); |
2172 | 99.6k | if (errcode < 0) |
2173 | 270 | return errcode; |
2174 | 99.6k | } |
2175 | 127k | } |
2176 | | |
2177 | | /* Return the number of uncompressed bytes */ |
2178 | 128k | return (int)ntbytes; |
2179 | 129k | } |
2180 | | |
2181 | | |
2182 | | /* Serial version for compression/decompression */ |
2183 | 137k | static int serial_blosc(struct thread_context* thread_context) { |
2184 | 137k | blosc2_context* context = thread_context->parent_context; |
2185 | 137k | bool vlblocks = (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) != 0; |
2186 | 137k | int32_t j, bsize, leftoverblock; |
2187 | 137k | int32_t cbytes; |
2188 | 137k | int32_t ntbytes = context->output_bytes; |
2189 | 137k | int32_t* bstarts = context->bstarts; |
2190 | 137k | uint8_t* tmp = thread_context->tmp; |
2191 | 137k | uint8_t* tmp2 = thread_context->tmp2; |
2192 | 137k | int dict_training = context->use_dict && (context->dict_cdict == NULL); |
2193 | 137k | bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
2194 | 137k | if (!context->do_compress && context->special_type) { |
2195 | | // Fake a runlen as if it was a memcpyed chunk |
2196 | 7.02k | memcpyed = true; |
2197 | 7.02k | } |
2198 | | |
2199 | 22.3M | for (j = 0; j < context->nblocks; j++) { |
2200 | 22.2M | if (context->do_compress && !memcpyed && !dict_training) { |
2201 | 461k | _sw32(bstarts + j, ntbytes); |
2202 | 461k | } |
2203 | 22.2M | bsize = vlblocks ? context->blocknbytes[j] : context->blocksize; |
2204 | 22.2M | leftoverblock = 0; |
2205 | 22.2M | if (!vlblocks && (j == context->nblocks - 1) && (context->leftover > 0)) { |
2206 | 28.6k | bsize = context->leftover; |
2207 | 28.6k | leftoverblock = 1; |
2208 | 28.6k | } |
2209 | 22.2M | if (context->do_compress) { |
2210 | 473k | if (memcpyed && !context->prefilter) { |
2211 | | /* We want to memcpy only */ |
2212 | 11.4k | memcpy(context->dest + context->header_overhead + j * context->blocksize, |
2213 | 11.4k | context->src + j * context->blocksize, (unsigned int)bsize); |
2214 | 11.4k | cbytes = (int32_t)bsize; |
2215 | 11.4k | } |
2216 | 461k | else { |
2217 | | /* Regular compression */ |
2218 | 461k | cbytes = blosc_c(thread_context, bsize, leftoverblock, ntbytes, |
2219 | 461k | context->destsize, |
2220 | 461k | vlblocks ? context->vlblock_sources[j] : context->src, |
2221 | 461k | vlblocks ? 0 : j * context->blocksize, |
2222 | 461k | context->dest + ntbytes, tmp, tmp2); |
2223 | 461k | if (cbytes == 0) { |
2224 | 17.2k | ntbytes = 0; /* incompressible data */ |
2225 | 17.2k | break; |
2226 | 17.2k | } |
2227 | 461k | } |
2228 | 473k | } |
2229 | 21.8M | else { |
2230 | | /* Regular decompression */ |
2231 | | // If memcpyed we don't have a bstarts section (because it is not needed) |
2232 | 21.8M | int32_t src_offset = memcpyed ? |
2233 | 21.6M | context->header_overhead + j * context->blocksize : sw32_(bstarts + j); |
2234 | 21.8M | uint8_t *dest_block = (vlblocks && context->vlblock_dests != NULL) ? context->vlblock_dests[j] : context->dest; |
2235 | 21.8M | int32_t dest_offset = (vlblocks && context->vlblock_dests != NULL) ? 0 : |
2236 | 21.8M | (vlblocks ? context->blockoffsets[j] : j * context->blocksize); |
2237 | 21.8M | cbytes = blosc_d(thread_context, bsize, leftoverblock, memcpyed, |
2238 | 21.8M | context->src, context->srcsize, src_offset, j, |
2239 | 21.8M | dest_block, dest_offset, tmp, tmp2); |
2240 | 21.8M | } |
2241 | | |
2242 | 22.2M | if (cbytes < 0) { |
2243 | 22.6k | ntbytes = cbytes; /* error in blosc_c or blosc_d */ |
2244 | 22.6k | break; |
2245 | 22.6k | } |
2246 | 22.2M | ntbytes += cbytes; |
2247 | 22.2M | } |
2248 | | |
2249 | 137k | return ntbytes; |
2250 | 137k | } |
2251 | | |
2252 | | static void t_blosc_do_job(void *ctxt); |
2253 | | |
2254 | | /* initialize a thread_context that has already been allocated */ |
2255 | | static int init_thread_context(struct thread_context* thread_context, blosc2_context* context, int32_t tid) |
2256 | 48.7k | { |
2257 | 48.7k | int32_t ebsize; |
2258 | | |
2259 | 48.7k | thread_context->parent_context = context; |
2260 | 48.7k | thread_context->owner_pool = NULL; |
2261 | 48.7k | thread_context->tid = tid; |
2262 | | |
2263 | 48.7k | int32_t blocksize = context != NULL ? context->blocksize : 0; |
2264 | 48.7k | int32_t typesize = context != NULL ? context->typesize : 0; |
2265 | 48.7k | ebsize = blocksize + typesize * (signed)sizeof(int32_t); |
2266 | 48.7k | thread_context->tmp_nbytes = (size_t)4 * ebsize; |
2267 | 48.7k | thread_context->tmp = my_malloc(thread_context->tmp_nbytes); |
2268 | 48.7k | BLOSC_ERROR_NULL(thread_context->tmp, BLOSC2_ERROR_MEMORY_ALLOC); |
2269 | 48.7k | thread_context->tmp2 = thread_context->tmp + ebsize; |
2270 | 48.7k | thread_context->tmp3 = thread_context->tmp2 + ebsize; |
2271 | 48.7k | thread_context->tmp4 = thread_context->tmp3 + ebsize; |
2272 | 48.7k | thread_context->tmp_blocksize = blocksize; |
2273 | 48.7k | thread_context->zfp_cell_nitems = 0; |
2274 | 48.7k | thread_context->zfp_cell_start = 0; |
2275 | 48.7k | #if defined(HAVE_ZSTD) |
2276 | 48.7k | thread_context->zstd_cctx = NULL; |
2277 | 48.7k | thread_context->zstd_dctx = NULL; |
2278 | 48.7k | #endif |
2279 | 48.7k | thread_context->lz4_cstream = NULL; |
2280 | 48.7k | thread_context->lz4hc_cstream = NULL; |
2281 | | |
2282 | 48.7k | return 0; |
2283 | 48.7k | } |
2284 | | |
2285 | | static struct thread_context* |
2286 | 48.7k | create_thread_context(blosc2_context* context, int32_t tid) { |
2287 | 48.7k | struct thread_context* thread_context; |
2288 | 48.7k | thread_context = (struct thread_context*)my_malloc(sizeof(struct thread_context)); |
2289 | 48.7k | BLOSC_ERROR_NULL(thread_context, NULL); |
2290 | 48.7k | int rc = init_thread_context(thread_context, context, tid); |
2291 | 48.7k | if (rc < 0) { |
2292 | 0 | return NULL; |
2293 | 0 | } |
2294 | 48.7k | return thread_context; |
2295 | 48.7k | } |
2296 | | |
2297 | | /* free members of thread_context, but not thread_context itself */ |
2298 | 48.7k | static void destroy_thread_context(struct thread_context* thread_context) { |
2299 | 48.7k | my_free(thread_context->tmp); |
2300 | 48.7k | #if defined(HAVE_ZSTD) |
2301 | 48.7k | if (thread_context->zstd_cctx != NULL) { |
2302 | 968 | ZSTD_freeCCtx(thread_context->zstd_cctx); |
2303 | 968 | } |
2304 | 48.7k | if (thread_context->zstd_dctx != NULL) { |
2305 | 17.0k | ZSTD_freeDCtx(thread_context->zstd_dctx); |
2306 | 17.0k | } |
2307 | 48.7k | #endif |
2308 | 48.7k | if (thread_context->lz4_cstream != NULL) { |
2309 | 0 | LZ4_freeStream((LZ4_stream_t*)thread_context->lz4_cstream); |
2310 | 0 | } |
2311 | 48.7k | if (thread_context->lz4hc_cstream != NULL) { |
2312 | 0 | LZ4_freeStreamHC((LZ4_streamHC_t*)thread_context->lz4hc_cstream); |
2313 | 0 | } |
2314 | 48.7k | } |
2315 | | |
2316 | 48.7k | void free_thread_context(struct thread_context* thread_context) { |
2317 | 48.7k | destroy_thread_context(thread_context); |
2318 | 48.7k | my_free(thread_context); |
2319 | 48.7k | } |
2320 | | |
2321 | | |
2322 | 137k | static int check_nthreads(blosc2_context* context, bool start_backend) { |
2323 | 137k | if (context->new_nthreads != context->nthreads && context->new_nthreads <= 0) { |
2324 | 7.39k | BLOSC_TRACE_ERROR("nthreads must be >= 1 and <= %d", INT16_MAX); |
2325 | 7.39k | return BLOSC2_ERROR_INVALID_PARAM; |
2326 | 7.39k | } |
2327 | 129k | if (context->nthreads <= 0) { |
2328 | 0 | BLOSC_TRACE_ERROR("nthreads must be >= 1 and <= %d", INT16_MAX); |
2329 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
2330 | 0 | } |
2331 | | |
2332 | | /* Detect a pool that was torn down by blosc2_destroy() while this context |
2333 | | * was still alive. The epoch mismatch tells us the pool pointer is dangling; |
2334 | | * clear it so the re-attach logic below creates a fresh one. */ |
2335 | 129k | if (context->thread_backend == BLOSC_BACKEND_SHARED_POOL && |
2336 | 0 | context->pool_epoch != g_destroy_count) { |
2337 | 0 | context->thread_pool = NULL; |
2338 | 0 | context->threads_started = 0; |
2339 | 0 | context->thread_backend = BLOSC_BACKEND_SERIAL; |
2340 | 0 | } |
2341 | | |
2342 | 129k | if (context->new_nthreads != context->nthreads) { |
2343 | 0 | release_thread_backend(context); |
2344 | 0 | context->nthreads = context->new_nthreads; |
2345 | 0 | } |
2346 | 129k | if (start_backend && context->nthreads > 1 && context->threads_started == 0) { |
2347 | 0 | int rc; |
2348 | 0 | if (threads_callback) { |
2349 | 0 | rc = init_callback_threads(context); |
2350 | 0 | } |
2351 | 0 | else { |
2352 | | #if defined(_WIN32) |
2353 | | rc = init_threadpool(context); |
2354 | | #else |
2355 | 0 | rc = attach_shared_pool(context); |
2356 | 0 | #endif |
2357 | 0 | } |
2358 | 0 | if (rc < 0) { |
2359 | 0 | return rc; |
2360 | 0 | } |
2361 | 0 | } |
2362 | 129k | if (context->nthreads <= 1) { |
2363 | 129k | context->thread_backend = BLOSC_BACKEND_SERIAL; |
2364 | 129k | } |
2365 | | |
2366 | 129k | return context->nthreads; |
2367 | 129k | } |
2368 | | |
2369 | 137k | static int ensure_serial_context(blosc2_context* context) { |
2370 | 137k | if (context->serial_context == NULL) { |
2371 | 30.3k | context->serial_context = create_thread_context(context, 0); |
2372 | 30.3k | } |
2373 | 106k | else if (context->blocksize != context->serial_context->tmp_blocksize) { |
2374 | 2.05k | free_thread_context(context->serial_context); |
2375 | 2.05k | context->serial_context = create_thread_context(context, 0); |
2376 | 2.05k | } |
2377 | 137k | return context->serial_context != NULL ? 0 : BLOSC2_ERROR_THREAD_CREATE; |
2378 | 137k | } |
2379 | | |
2380 | | /* Do the compression or decompression of the buffer depending on the |
2381 | | global params. */ |
2382 | 137k | static int do_job(blosc2_context* context) { |
2383 | 137k | int32_t ntbytes; |
2384 | | |
2385 | | /* Set sentinels */ |
2386 | 137k | context->dref_not_init = 1; |
2387 | | |
2388 | | /* Apply thread-count changes before deciding whether this operation actually |
2389 | | needs a parallel backend. POSIX defers backend startup for serial jobs. |
2390 | | Windows keeps its existing per-context thread lifecycle and starts/rebinds |
2391 | | the backend eagerly. */ |
2392 | | #if defined(_WIN32) |
2393 | | int rc = check_nthreads(context, true); |
2394 | | #else |
2395 | 137k | int rc = check_nthreads(context, false); |
2396 | 137k | #endif |
2397 | | |
2398 | | /* Run serially with one thread, with fewer than two full blocks of source |
2399 | | data, or if thread configuration/backend setup fails. */ |
2400 | 137k | if (context->nthreads == 1 || (context->sourcesize / context->blocksize) <= 1 || rc < 0) { |
2401 | 137k | if (ensure_serial_context(context) < 0) { |
2402 | 0 | return BLOSC2_ERROR_THREAD_CREATE; |
2403 | 0 | } |
2404 | 137k | ntbytes = serial_blosc(context->serial_context); |
2405 | 137k | } |
2406 | 0 | else { |
2407 | 0 | rc = check_nthreads(context, true); |
2408 | 0 | if (rc < 0) { |
2409 | | /* Backend startup failed; run serially and retry on the next parallel job. */ |
2410 | 0 | if (ensure_serial_context(context) < 0) { |
2411 | 0 | return BLOSC2_ERROR_THREAD_CREATE; |
2412 | 0 | } |
2413 | 0 | ntbytes = serial_blosc(context->serial_context); |
2414 | 0 | } |
2415 | 0 | else { |
2416 | 0 | ntbytes = parallel_blosc(context); |
2417 | 0 | } |
2418 | 0 | } |
2419 | | |
2420 | 137k | return ntbytes; |
2421 | 137k | } |
2422 | | |
2423 | | |
2424 | | static int initialize_context_compression( |
2425 | | blosc2_context* context, const void* src, int32_t srcsize, void* dest, |
2426 | | int32_t destsize, int clevel, uint8_t const *filters, |
2427 | | uint8_t const *filters_meta, int32_t typesize, int compressor, |
2428 | | int32_t blocksize, int16_t new_nthreads, int16_t nthreads, |
2429 | | int32_t splitmode, |
2430 | | int tuner_id, void *tuner_params, |
2431 | 66.4k | blosc2_schunk* schunk) { |
2432 | | |
2433 | | /* The block size and the filters divide by the typesize */ |
2434 | 66.4k | if (typesize <= 0) { |
2435 | 0 | BLOSC_TRACE_ERROR("The typesize must be positive, not %d.", typesize); |
2436 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
2437 | 0 | } |
2438 | | |
2439 | | /* Check typesize limits */ |
2440 | 66.4k | if (typesize > BLOSC2_MAXTYPESIZE) { |
2441 | | // If typesize is too large for Blosc2, return an error |
2442 | 0 | BLOSC_TRACE_ERROR("Typesize cannot exceed %d bytes.", BLOSC2_MAXTYPESIZE); |
2443 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
2444 | 0 | } |
2445 | | /* Now, cap typesize so that blosc2 split machinery can continue to work. This has to be |
2446 | | done before the tuner chooses the block size, which depends on the typesize: a buffer |
2447 | | with a larger typesize is a plain stream of bytes and is blocked as one. */ |
2448 | 66.4k | if (typesize > BLOSC_MAX_TYPESIZE) { |
2449 | | /* If typesize is too large, treat buffer as an 1-byte stream. */ |
2450 | 265 | typesize = 1; |
2451 | 265 | } |
2452 | | |
2453 | | /* Set parameters */ |
2454 | 66.4k | context->do_compress = 1; |
2455 | 66.4k | context->src = (const uint8_t*)src; |
2456 | 66.4k | context->srcsize = srcsize; |
2457 | 66.4k | context->dest = (uint8_t*)dest; |
2458 | 66.4k | context->output_bytes = 0; |
2459 | 66.4k | context->destsize = destsize; |
2460 | 66.4k | context->sourcesize = srcsize; |
2461 | 66.4k | context->typesize = typesize; |
2462 | 66.4k | context->filter_flags = filters_to_flags(filters); |
2463 | 465k | for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
2464 | 398k | context->filters[i] = filters[i]; |
2465 | 398k | context->filters_meta[i] = filters_meta[i]; |
2466 | 398k | } |
2467 | 66.4k | context->compcode = compressor; |
2468 | 66.4k | context->nthreads = nthreads; |
2469 | 66.4k | context->new_nthreads = new_nthreads; |
2470 | 66.4k | context->clevel = clevel; |
2471 | 66.4k | context->schunk = schunk; |
2472 | 66.4k | context->tuner_params = tuner_params; |
2473 | 66.4k | context->tuner_id = tuner_id; |
2474 | 66.4k | context->splitmode = splitmode; |
2475 | 66.4k | context->blosc2_flags2 = 0; |
2476 | 66.4k | if (context->blocknbytes != NULL) { |
2477 | 0 | free(context->blocknbytes); |
2478 | 0 | context->blocknbytes = NULL; |
2479 | 0 | } |
2480 | 66.4k | if (context->blockoffsets != NULL) { |
2481 | 0 | free(context->blockoffsets); |
2482 | 0 | context->blockoffsets = NULL; |
2483 | 0 | } |
2484 | 66.4k | if (context->blockcbytes != NULL) { |
2485 | 0 | free(context->blockcbytes); |
2486 | 0 | context->blockcbytes = NULL; |
2487 | 0 | } |
2488 | 66.4k | context->vlblock_sources = NULL; |
2489 | 66.4k | context->vlblock_dests = NULL; |
2490 | | /* tuner some compression parameters */ |
2491 | 66.4k | context->blocksize = (int32_t)blocksize; |
2492 | 66.4k | int rc = 0; |
2493 | 66.4k | if (context->tuner_params != NULL) { |
2494 | 0 | if (context->tuner_id < BLOSC_LAST_TUNER && context->tuner_id == BLOSC_STUNE) { |
2495 | 0 | if (blosc_stune_next_cparams(context) < 0) { |
2496 | 0 | BLOSC_TRACE_ERROR("Error in stune next_cparams func\n"); |
2497 | 0 | return BLOSC2_ERROR_TUNER; |
2498 | 0 | } |
2499 | 0 | } else { |
2500 | 0 | for (int i = 0; i < g_ntuners; ++i) { |
2501 | 0 | if (g_tuners[i].id == context->tuner_id) { |
2502 | 0 | if (g_tuners[i].next_cparams == NULL) { |
2503 | 0 | if (fill_tuner(&g_tuners[i]) < 0) { |
2504 | 0 | BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
2505 | 0 | return BLOSC2_ERROR_FAILURE; |
2506 | 0 | } |
2507 | 0 | } |
2508 | 0 | if (g_tuners[i].next_cparams(context) < 0) { |
2509 | 0 | BLOSC_TRACE_ERROR("Error in tuner %d next_cparams func\n", context->tuner_id); |
2510 | 0 | return BLOSC2_ERROR_TUNER; |
2511 | 0 | } |
2512 | 0 | if (g_tuners[i].id == BLOSC_BTUNE && context->blocksize == 0) { |
2513 | | // Call stune for initializing blocksize |
2514 | 0 | if (blosc_stune_next_blocksize(context) < 0) { |
2515 | 0 | BLOSC_TRACE_ERROR("Error in stune next_blocksize func\n"); |
2516 | 0 | return BLOSC2_ERROR_TUNER; |
2517 | 0 | } |
2518 | 0 | } |
2519 | 0 | goto urtunersuccess; |
2520 | 0 | } |
2521 | 0 | } |
2522 | 0 | BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", context->tuner_id); |
2523 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
2524 | 0 | } |
2525 | 66.4k | } else { |
2526 | 66.4k | if (context->tuner_id < BLOSC_LAST_TUNER && context->tuner_id == BLOSC_STUNE) { |
2527 | 66.4k | rc = blosc_stune_next_blocksize(context); |
2528 | 66.4k | } else { |
2529 | 0 | for (int i = 0; i < g_ntuners; ++i) { |
2530 | 0 | if (g_tuners[i].id == context->tuner_id) { |
2531 | 0 | if (g_tuners[i].next_blocksize == NULL) { |
2532 | 0 | if (fill_tuner(&g_tuners[i]) < 0) { |
2533 | 0 | BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
2534 | 0 | return BLOSC2_ERROR_FAILURE; |
2535 | 0 | } |
2536 | 0 | } |
2537 | 0 | rc = g_tuners[i].next_blocksize(context); |
2538 | 0 | goto urtunersuccess; |
2539 | 0 | } |
2540 | 0 | } |
2541 | 0 | BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", context->tuner_id); |
2542 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
2543 | 0 | } |
2544 | 66.4k | } |
2545 | 66.4k | urtunersuccess:; |
2546 | 66.4k | if (rc < 0) { |
2547 | 0 | BLOSC_TRACE_ERROR("Error in tuner next_blocksize func\n"); |
2548 | 0 | return BLOSC2_ERROR_TUNER; |
2549 | 0 | } |
2550 | | /* The header must record the block size that the tuner settled on (it rounds a requested |
2551 | | one to a multiple of the typesize, for instance), not the one that was asked for: a reader |
2552 | | splits the chunk into blocks of the size in the header. Variable-length block chunks |
2553 | | store something else there and set it themselves. */ |
2554 | 66.4k | context->header_blocksize = context->blocksize; |
2555 | | |
2556 | | |
2557 | | /* Check buffer size limits */ |
2558 | 66.4k | if (srcsize > BLOSC2_MAX_BUFFERSIZE) { |
2559 | 0 | BLOSC_TRACE_ERROR("Input buffer size cannot exceed %d bytes.", |
2560 | 0 | BLOSC2_MAX_BUFFERSIZE); |
2561 | 0 | return BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED; |
2562 | 0 | } |
2563 | | |
2564 | 66.4k | if (destsize < BLOSC2_MAX_OVERHEAD) { |
2565 | 31 | BLOSC_TRACE_ERROR("Output buffer size should be larger than %d bytes.", |
2566 | 31 | BLOSC2_MAX_OVERHEAD); |
2567 | 31 | return BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED; |
2568 | 31 | } |
2569 | | |
2570 | | /* Compression level */ |
2571 | 66.4k | if (clevel < 0 || clevel > 9) { |
2572 | | /* If clevel not in 0..9, print an error */ |
2573 | 0 | BLOSC_TRACE_ERROR("`clevel` parameter must be between 0 and 9!."); |
2574 | 0 | return BLOSC2_ERROR_CODEC_PARAM; |
2575 | 0 | } |
2576 | | |
2577 | | /* Dictionary support is only available for ZSTD, LZ4, and LZ4HC. |
2578 | | * Skip the check when src is NULL (special-value chunks): no compression |
2579 | | * will actually happen, so codec compatibility is irrelevant. */ |
2580 | 66.4k | if (src != NULL && context->use_dict && context->compcode != BLOSC_ZSTD && |
2581 | 0 | context->compcode != BLOSC_LZ4 && context->compcode != BLOSC_LZ4HC) { |
2582 | 0 | BLOSC_TRACE_ERROR("`use_dict` is only supported for ZSTD, LZ4, and LZ4HC codecs."); |
2583 | 0 | return BLOSC2_ERROR_CODEC_PARAM; |
2584 | 0 | } |
2585 | | |
2586 | 66.4k | blosc2_calculate_blocks(context); |
2587 | | |
2588 | 66.4k | return 1; |
2589 | 66.4k | } |
2590 | | |
2591 | 129k | static void release_context_dict_buffer(blosc2_context* context) { |
2592 | 129k | if (context->dict_buffer_owned && context->dict_buffer != NULL) { |
2593 | 0 | free(context->dict_buffer); |
2594 | 0 | } |
2595 | 129k | context->dict_buffer = NULL; |
2596 | 129k | context->dict_buffer_owned = false; |
2597 | 129k | context->dict_size = 0; |
2598 | 129k | } |
2599 | | |
2600 | | |
2601 | 64.7k | static void clear_context_decompression_dict(blosc2_context* context) { |
2602 | 64.7k | context->use_dict = 0; |
2603 | 64.7k | release_context_dict_buffer(context); |
2604 | 64.7k | #if defined(HAVE_ZSTD) |
2605 | 64.7k | if (context->dict_ddict != NULL) { |
2606 | 512 | ZSTD_freeDDict(context->dict_ddict); |
2607 | 512 | context->dict_ddict = NULL; |
2608 | 512 | } |
2609 | | #else |
2610 | | context->dict_ddict = NULL; |
2611 | | #endif |
2612 | 64.7k | } |
2613 | | |
2614 | | |
2615 | | static int read_lazy_chunk_bytes(blosc2_context* context, int32_t offset, uint8_t* buffer, int32_t nbytes, |
2616 | 213 | const char* open_error, const char* read_error) { |
2617 | 213 | int32_t chunk_cbytes; |
2618 | 213 | if (context->src == NULL || context->srcsize < BLOSC_EXTENDED_HEADER_LENGTH || |
2619 | 213 | offset < 0 || nbytes < 0 || context->nblocks <= 0 || |
2620 | 195 | blosc2_cbuffer_sizes(context->src, NULL, &chunk_cbytes, NULL) < 0 || |
2621 | 195 | offset > chunk_cbytes || nbytes > chunk_cbytes - offset) { |
2622 | 18 | return BLOSC2_ERROR_INVALID_HEADER; |
2623 | 18 | } |
2624 | 195 | int64_t trailer_end = BLOSC_EXTENDED_HEADER_LENGTH + |
2625 | 195 | (int64_t)context->nblocks * sizeof(int32_t) + sizeof(int32_t) + sizeof(int64_t); |
2626 | 195 | if (trailer_end > context->srcsize) return BLOSC2_ERROR_READ_BUFFER; |
2627 | 184 | if (context->schunk == NULL || context->schunk->frame == NULL) { |
2628 | 18 | BLOSC_TRACE_ERROR("Lazy chunk needs an associated super-chunk with a frame."); |
2629 | 18 | return BLOSC2_ERROR_INVALID_PARAM; |
2630 | 18 | } |
2631 | | |
2632 | 166 | blosc2_frame_s* frame = (blosc2_frame_s*)context->schunk->frame; |
2633 | 166 | blosc2_io_cb* io_cb = blosc2_get_io_cb(context->schunk->storage->io->id); |
2634 | 166 | if (io_cb == NULL) { |
2635 | 0 | BLOSC_TRACE_ERROR("Error getting the input/output API"); |
2636 | 0 | return BLOSC2_ERROR_PLUGIN_IO; |
2637 | 0 | } |
2638 | | |
2639 | 166 | int32_t trailer_offset = BLOSC_EXTENDED_HEADER_LENGTH + |
2640 | 166 | context->nblocks * (int32_t)sizeof(int32_t); |
2641 | 166 | int32_t nchunk_lazy; |
2642 | 166 | int64_t chunk_offset; |
2643 | 166 | memcpy(&nchunk_lazy, context->src + trailer_offset, sizeof(nchunk_lazy)); |
2644 | 166 | memcpy(&chunk_offset, context->src + trailer_offset + (int32_t)sizeof(int32_t), sizeof(chunk_offset)); |
2645 | 166 | if (nchunk_lazy < 0 || chunk_offset < 0 || frame->file_offset < 0 || |
2646 | 94 | (!frame->sframe && (chunk_offset < FRAME_HEADER_MINLEN || |
2647 | 153 | chunk_offset > frame->len || chunk_cbytes > frame->len - chunk_offset))) { |
2648 | 153 | return BLOSC2_ERROR_INVALID_HEADER; |
2649 | 153 | } |
2650 | | |
2651 | 13 | void* fp = NULL; |
2652 | 13 | int64_t io_pos; |
2653 | 13 | if (frame->sframe) { |
2654 | 0 | fp = sframe_open_chunk(frame->urlpath, nchunk_lazy, "rb", context->schunk->storage->io); |
2655 | 0 | io_pos = offset; |
2656 | 0 | } |
2657 | 13 | else { |
2658 | 13 | if (chunk_offset < 0 || offset < 0) { |
2659 | 0 | BLOSC_TRACE_ERROR("Lazy chunk offset cannot be negative."); |
2660 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
2661 | 0 | } |
2662 | 13 | fp = frame_reader_acquire(frame, context->schunk->storage->io); |
2663 | 13 | if (!blosc2_checked_add_i64(frame->file_offset, chunk_offset, &io_pos)) { |
2664 | 0 | BLOSC_TRACE_ERROR("Lazy chunk offset overflows file position."); |
2665 | 0 | if (fp != NULL) { |
2666 | 0 | frame_reader_release(frame, io_cb, fp); |
2667 | 0 | } |
2668 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
2669 | 0 | } |
2670 | 13 | if (!blosc2_checked_add_i64(io_pos, offset, &io_pos)) { |
2671 | 0 | BLOSC_TRACE_ERROR("Lazy block offset overflows file position."); |
2672 | 0 | if (fp != NULL) { |
2673 | 0 | frame_reader_release(frame, io_cb, fp); |
2674 | 0 | } |
2675 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
2676 | 0 | } |
2677 | 13 | } |
2678 | 13 | if (fp == NULL) { |
2679 | 13 | BLOSC_TRACE_ERROR("%s", open_error); |
2680 | 13 | return BLOSC2_ERROR_FILE_OPEN; |
2681 | 13 | } |
2682 | 0 | if (!blosc2_checked_add_i64(io_pos, nbytes, NULL)) { |
2683 | 0 | frame_reader_release(frame, io_cb, fp); |
2684 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
2685 | 0 | } |
2686 | | |
2687 | 0 | uint8_t* read_buffer = buffer; |
2688 | 0 | int64_t rbytes = io_cb->read((void**)&read_buffer, 1, nbytes, io_pos, fp); |
2689 | 0 | frame_reader_release(frame, io_cb, fp); |
2690 | 0 | if (read_buffer != buffer) { |
2691 | 0 | if (rbytes == nbytes && read_buffer != NULL) memcpy(buffer, read_buffer, (size_t)nbytes); |
2692 | 0 | free(read_buffer); |
2693 | 0 | } |
2694 | 0 | if (rbytes != nbytes || read_buffer == NULL) { |
2695 | 0 | BLOSC_TRACE_ERROR("%s", read_error); |
2696 | 0 | return BLOSC2_ERROR_FILE_READ; |
2697 | 0 | } |
2698 | | |
2699 | 0 | return 0; |
2700 | 0 | } |
2701 | | |
2702 | | |
2703 | 214 | static int load_lazy_chunk_dict(blosc2_context* context, blosc_header* header, int32_t bstarts_end) { |
2704 | 214 | int32_t dict_offset = bstarts_end; |
2705 | 214 | if (header->cbytes < dict_offset + (int32_t)sizeof(int32_t)) { |
2706 | 1 | BLOSC_TRACE_ERROR("Lazy chunk dictionary header exceeds chunk length."); |
2707 | 1 | return BLOSC2_ERROR_INVALID_HEADER; |
2708 | 1 | } |
2709 | | |
2710 | 213 | uint8_t dict_size_buf[sizeof(int32_t)]; |
2711 | 213 | int rc = read_lazy_chunk_bytes(context, dict_offset, dict_size_buf, (int32_t)sizeof(dict_size_buf), |
2712 | 213 | "Cannot open frame file for lazy chunk dictionary read.", |
2713 | 213 | "Cannot read lazy chunk dictionary size from disk."); |
2714 | 213 | if (rc < 0) { |
2715 | 213 | return rc; |
2716 | 213 | } |
2717 | | |
2718 | 0 | context->dict_size = sw32_(dict_size_buf); |
2719 | 0 | if (context->dict_size <= 0 || context->dict_size > BLOSC2_MAXDICTSIZE) { |
2720 | 0 | BLOSC_TRACE_ERROR("Dictionary size is smaller than minimum or larger than maximum allowed."); |
2721 | 0 | return BLOSC2_ERROR_CODEC_DICT; |
2722 | 0 | } |
2723 | 0 | if (header->cbytes < dict_offset + (int32_t)sizeof(int32_t) + context->dict_size) { |
2724 | 0 | BLOSC_TRACE_ERROR("Lazy chunk dictionary exceeds chunk length."); |
2725 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
2726 | 0 | } |
2727 | | |
2728 | 0 | context->dict_buffer = malloc((size_t)context->dict_size); |
2729 | 0 | BLOSC_ERROR_NULL(context->dict_buffer, BLOSC2_ERROR_MEMORY_ALLOC); |
2730 | 0 | context->dict_buffer_owned = true; |
2731 | 0 | rc = read_lazy_chunk_bytes(context, dict_offset + (int32_t)sizeof(int32_t), |
2732 | 0 | context->dict_buffer, context->dict_size, |
2733 | 0 | "Cannot open frame file for lazy chunk dictionary read.", |
2734 | 0 | "Cannot read lazy chunk dictionary from disk."); |
2735 | 0 | if (rc < 0) { |
2736 | 0 | release_context_dict_buffer(context); |
2737 | 0 | return rc; |
2738 | 0 | } |
2739 | | |
2740 | 0 | context->use_dict = 1; |
2741 | 0 | #if defined(HAVE_ZSTD) |
2742 | 0 | if (context->compcode == BLOSC_ZSTD_FORMAT) { |
2743 | 0 | context->dict_ddict = ZSTD_createDDict(context->dict_buffer, context->dict_size); |
2744 | 0 | if (context->dict_ddict == NULL) { |
2745 | 0 | release_context_dict_buffer(context); |
2746 | 0 | BLOSC_TRACE_ERROR("Cannot create ZSTD dictionary for lazy chunk."); |
2747 | 0 | return BLOSC2_ERROR_CODEC_DICT; |
2748 | 0 | } |
2749 | 0 | } |
2750 | 0 | #endif |
2751 | | |
2752 | 0 | return 0; |
2753 | 0 | } |
2754 | | |
2755 | | |
2756 | | static int initialize_context_decompression(blosc2_context* context, blosc_header* header, const void* src, |
2757 | 64.7k | int32_t srcsize, void* dest, int32_t destsize) { |
2758 | 64.7k | int32_t bstarts_end; |
2759 | 64.7k | bool vlblocks; |
2760 | | |
2761 | 64.7k | context->do_compress = 0; |
2762 | 64.7k | context->src = (const uint8_t*)src; |
2763 | 64.7k | context->srcsize = srcsize; |
2764 | 64.7k | context->dest = (uint8_t*)dest; |
2765 | 64.7k | context->destsize = destsize; |
2766 | 64.7k | context->output_bytes = 0; |
2767 | 64.7k | context->vlblock_sources = NULL; |
2768 | 64.7k | context->vlblock_dests = NULL; |
2769 | 64.7k | if (context->blocknbytes != NULL) { |
2770 | 0 | free(context->blocknbytes); |
2771 | 0 | context->blocknbytes = NULL; |
2772 | 0 | } |
2773 | 64.7k | if (context->blockoffsets != NULL) { |
2774 | 0 | free(context->blockoffsets); |
2775 | 0 | context->blockoffsets = NULL; |
2776 | 0 | } |
2777 | 64.7k | if (context->blockcbytes != NULL) { |
2778 | 0 | free(context->blockcbytes); |
2779 | 0 | context->blockcbytes = NULL; |
2780 | 0 | } |
2781 | | |
2782 | 64.7k | int rc = blosc2_initialize_context_from_header(context, header); |
2783 | 64.7k | if (rc < 0) { |
2784 | 0 | return rc; |
2785 | 0 | } |
2786 | 64.7k | clear_context_decompression_dict(context); |
2787 | 64.7k | vlblocks = (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) != 0; |
2788 | 64.7k | bool is_lazy = ((context->header_overhead == BLOSC_EXTENDED_HEADER_LENGTH) && |
2789 | 54.9k | (context->blosc2_flags & 0x08u)); |
2790 | | |
2791 | | /* Check that we have enough space to decompress */ |
2792 | 64.7k | if (context->sourcesize > (int32_t)context->destsize) { |
2793 | 0 | return BLOSC2_ERROR_WRITE_BUFFER; |
2794 | 0 | } |
2795 | | |
2796 | 64.7k | if (context->block_maskout != NULL) { |
2797 | 0 | if (context->block_maskout_nitems != context->nblocks) { |
2798 | 0 | BLOSC_TRACE_ERROR("The number of items in block_maskout (%d) must match the number" |
2799 | 0 | " of blocks in chunk (%d).", |
2800 | 0 | context->block_maskout_nitems, context->nblocks); |
2801 | 0 | return BLOSC2_ERROR_DATA; |
2802 | 0 | } |
2803 | 0 | bool has_delta = false; |
2804 | 0 | for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) { |
2805 | 0 | if (context->filters[i] == BLOSC_DELTA) { |
2806 | 0 | has_delta = true; |
2807 | 0 | break; |
2808 | 0 | } |
2809 | 0 | } |
2810 | 0 | if (has_delta) { |
2811 | 0 | bool any_needed = false; |
2812 | 0 | for (int i = 0; i < context->block_maskout_nitems; ++i) { |
2813 | 0 | if (!context->block_maskout[i]) { |
2814 | 0 | any_needed = true; |
2815 | 0 | break; |
2816 | 0 | } |
2817 | 0 | } |
2818 | 0 | if (any_needed) { |
2819 | | // Delta filter encodes subsequent blocks relative to block 0; block 0 must be unmasked. |
2820 | 0 | context->block_maskout[0] = false; |
2821 | 0 | } |
2822 | 0 | } |
2823 | 0 | } |
2824 | | |
2825 | 64.7k | context->special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK; |
2826 | 64.7k | if (context->special_type > BLOSC2_SPECIAL_LASTID) { |
2827 | 2 | BLOSC_TRACE_ERROR("Unknown special values ID (%d) ", |
2828 | 2 | context->special_type); |
2829 | 2 | return BLOSC2_ERROR_DATA; |
2830 | 2 | } |
2831 | | |
2832 | 64.7k | int memcpyed = (context->header_flags & (uint8_t) BLOSC_MEMCPYED); |
2833 | 64.7k | if (memcpyed && (header->cbytes != header->nbytes + context->header_overhead)) { |
2834 | 37 | BLOSC_TRACE_ERROR("Wrong header info for this memcpyed chunk"); |
2835 | 37 | return BLOSC2_ERROR_DATA; |
2836 | 37 | } |
2837 | | |
2838 | 64.6k | if ((header->nbytes == 0) && (header->cbytes == context->header_overhead) && |
2839 | 0 | !context->special_type) { |
2840 | | // A compressed buffer with only a header can only contain a zero-length buffer |
2841 | 0 | return 0; |
2842 | 0 | } |
2843 | | |
2844 | 64.6k | context->bstarts = (int32_t *) (context->src + context->header_overhead); |
2845 | 64.6k | bstarts_end = context->header_overhead; |
2846 | 64.6k | if (!context->special_type && !memcpyed) { |
2847 | 55.2k | size_t bstarts_end_tmp; |
2848 | 55.2k | size_t bstarts_nbytes; |
2849 | 55.2k | if (context->nblocks < 0 || |
2850 | 55.2k | !blosc2_checked_mul_size((size_t)context->nblocks, sizeof(int32_t), &bstarts_nbytes) || |
2851 | 55.2k | !blosc2_checked_add_size((size_t)context->header_overhead, bstarts_nbytes, &bstarts_end_tmp) || |
2852 | 55.2k | bstarts_end_tmp > (size_t)INT32_MAX) { |
2853 | 9 | BLOSC_TRACE_ERROR("Invalid bstarts size in chunk header."); |
2854 | 9 | return BLOSC2_ERROR_INVALID_HEADER; |
2855 | 9 | } |
2856 | | /* If chunk is not special or a memcpyed, we do have a bstarts section */ |
2857 | 55.2k | bstarts_end = (int32_t)bstarts_end_tmp; |
2858 | 55.2k | } |
2859 | | |
2860 | 64.6k | if (srcsize < bstarts_end) { |
2861 | 70 | BLOSC_TRACE_ERROR("`bstarts` exceeds length of source buffer."); |
2862 | 70 | return BLOSC2_ERROR_READ_BUFFER; |
2863 | 70 | } |
2864 | | |
2865 | 64.5k | if (vlblocks && is_lazy && !context->special_type && !memcpyed) { |
2866 | 122 | size_t block_csizes_nbytes; |
2867 | 122 | size_t trailer_meta_nbytes; |
2868 | 122 | size_t lazy_trailer_end; |
2869 | 122 | if (context->srcsize < 0 || context->nblocks < 0 || |
2870 | 122 | !blosc2_checked_mul_size((size_t)context->nblocks, sizeof(int32_t), &block_csizes_nbytes) || |
2871 | 122 | !blosc2_checked_add_size(sizeof(int32_t) + sizeof(int64_t), block_csizes_nbytes, &trailer_meta_nbytes) || |
2872 | 122 | !blosc2_checked_add_size((size_t)bstarts_end, trailer_meta_nbytes, &lazy_trailer_end) || |
2873 | 122 | (size_t)context->srcsize < lazy_trailer_end) { |
2874 | 7 | BLOSC_TRACE_ERROR("Lazy trailer exceeds source buffer."); |
2875 | 7 | return BLOSC2_ERROR_READ_BUFFER; |
2876 | 7 | } |
2877 | 122 | } |
2878 | 64.5k | srcsize -= bstarts_end; |
2879 | | |
2880 | | /* Read optional dictionary if flag set */ |
2881 | 64.5k | if ((context->blosc2_flags & BLOSC2_USEDICT) && !is_lazy) { |
2882 | 8.47k | context->use_dict = 1; |
2883 | | // The dictionary section is after the bstarts block: [int32 size | raw bytes] |
2884 | 8.47k | if (srcsize < (signed)sizeof(int32_t)) { |
2885 | 4 | BLOSC_TRACE_ERROR("Not enough space to read size of dictionary."); |
2886 | 4 | return BLOSC2_ERROR_READ_BUFFER; |
2887 | 4 | } |
2888 | 8.46k | srcsize -= sizeof(int32_t); |
2889 | | // Read dictionary size |
2890 | 8.46k | context->dict_size = sw32_(context->src + bstarts_end); |
2891 | 8.46k | if (context->dict_size <= 0 || context->dict_size > BLOSC2_MAXDICTSIZE) { |
2892 | 51 | BLOSC_TRACE_ERROR("Dictionary size is smaller than minimum or larger than maximum allowed."); |
2893 | 51 | return BLOSC2_ERROR_CODEC_DICT; |
2894 | 51 | } |
2895 | 8.41k | if (srcsize < (int32_t)context->dict_size) { |
2896 | 20 | BLOSC_TRACE_ERROR("Not enough space to read entire dictionary."); |
2897 | 20 | return BLOSC2_ERROR_READ_BUFFER; |
2898 | 20 | } |
2899 | 8.39k | srcsize -= context->dict_size; |
2900 | | // dict_buffer points directly into the source chunk — no copy needed |
2901 | 8.39k | context->dict_buffer = (void*)(context->src + bstarts_end + sizeof(int32_t)); |
2902 | 8.39k | context->dict_buffer_owned = false; |
2903 | 8.39k | #if defined(HAVE_ZSTD) |
2904 | | // context->compcode during decompression holds the format code (flags >> 5), |
2905 | | // so compare against BLOSC_ZSTD_FORMAT (not BLOSC_ZSTD). |
2906 | 8.39k | if (context->compcode == BLOSC_ZSTD_FORMAT) { |
2907 | 7.39k | context->dict_ddict = ZSTD_createDDict(context->dict_buffer, context->dict_size); |
2908 | 7.39k | if (context->dict_ddict == NULL) { |
2909 | 761 | BLOSC_TRACE_ERROR("Cannot create ZSTD dictionary for chunk."); |
2910 | 761 | return BLOSC2_ERROR_CODEC_DICT; |
2911 | 761 | } |
2912 | 7.39k | } |
2913 | 8.39k | #endif // HAVE_ZSTD |
2914 | | // For LZ4/LZ4HC: dict_buffer and dict_size are sufficient; no digested object needed. |
2915 | 8.39k | } |
2916 | 56.1k | else if ((context->blosc2_flags & BLOSC2_USEDICT) && is_lazy) { |
2917 | 214 | rc = load_lazy_chunk_dict(context, header, bstarts_end); |
2918 | 214 | if (rc < 0) { |
2919 | 214 | return rc; |
2920 | 214 | } |
2921 | 214 | } |
2922 | | |
2923 | 63.5k | if (vlblocks && !context->special_type && !memcpyed) { |
2924 | 288 | context->blocknbytes = malloc((size_t)context->nblocks * sizeof(int32_t)); |
2925 | 288 | BLOSC_ERROR_NULL(context->blocknbytes, BLOSC2_ERROR_MEMORY_ALLOC); |
2926 | 288 | context->blockoffsets = malloc((size_t)context->nblocks * sizeof(int32_t)); |
2927 | 288 | BLOSC_ERROR_NULL(context->blockoffsets, BLOSC2_ERROR_MEMORY_ALLOC); |
2928 | 288 | context->blockcbytes = malloc((size_t)context->nblocks * sizeof(int32_t)); |
2929 | 288 | BLOSC_ERROR_NULL(context->blockcbytes, BLOSC2_ERROR_MEMORY_ALLOC); |
2930 | | |
2931 | 288 | if (is_lazy) { |
2932 | | // Lazy VL: block data is on disk, so blocknbytes is unknown at this point. |
2933 | | // Populate blockcbytes from bstarts differences; blocksize gets max(blockcbytes) |
2934 | | // as a safe upper bound so tmp buffers are large enough for the lazy block read. |
2935 | 113 | int32_t max_csize = 0; |
2936 | 150 | for (int32_t i = 0; i < context->nblocks; ++i) { |
2937 | 143 | int32_t bstart = sw32_(context->bstarts + i); |
2938 | 143 | int32_t next_bstart = (i + 1 < context->nblocks) ? |
2939 | 95 | sw32_(context->bstarts + i + 1) : header->cbytes; |
2940 | 143 | if (bstart < bstarts_end || next_bstart <= bstart || |
2941 | 106 | next_bstart > header->cbytes) { |
2942 | 106 | BLOSC_TRACE_ERROR("Invalid VL-block offsets in lazy chunk."); |
2943 | 106 | return BLOSC2_ERROR_INVALID_HEADER; |
2944 | 106 | } |
2945 | 37 | context->blocknbytes[i] = 0; // unknown until block is read from disk |
2946 | 37 | context->blockoffsets[i] = 0; // unknown |
2947 | 37 | context->blockcbytes[i] = next_bstart - bstart; |
2948 | 37 | if (context->blockcbytes[i] > max_csize) { |
2949 | 24 | max_csize = context->blockcbytes[i]; |
2950 | 24 | } |
2951 | 37 | } |
2952 | 7 | context->blocksize = max_csize; |
2953 | 7 | context->leftover = 0; |
2954 | 7 | } |
2955 | 175 | else { |
2956 | 175 | int32_t max_blocksize = 0; |
2957 | 175 | int64_t total_nbytes = 0; |
2958 | 175 | int32_t prev_bstart = 0; |
2959 | 199 | for (int32_t i = 0; i < context->nblocks; ++i) { |
2960 | 193 | int32_t bstart = sw32_(context->bstarts + i); |
2961 | 193 | int32_t next_bstart = (i + 1 < context->nblocks) ? |
2962 | 119 | sw32_(context->bstarts + i + 1) : header->cbytes; |
2963 | 193 | if (bstart < bstarts_end || bstart <= prev_bstart || next_bstart <= bstart || |
2964 | 144 | next_bstart > header->cbytes || bstart > context->srcsize - (int32_t)sizeof(int32_t)) { |
2965 | 101 | BLOSC_TRACE_ERROR("Invalid VL-block offsets in chunk."); |
2966 | 101 | return BLOSC2_ERROR_INVALID_HEADER; |
2967 | 101 | } |
2968 | 92 | context->blocknbytes[i] = sw32_(context->src + bstart); |
2969 | 92 | context->blockcbytes[i] = next_bstart - bstart; |
2970 | 92 | if (context->blocknbytes[i] <= 0) { |
2971 | 24 | BLOSC_TRACE_ERROR("Invalid VL-block uncompressed size in chunk."); |
2972 | 24 | return BLOSC2_ERROR_INVALID_HEADER; |
2973 | 24 | } |
2974 | 68 | if (total_nbytes > INT32_MAX) { |
2975 | 0 | BLOSC_TRACE_ERROR("Invalid VL-block cumulative size in chunk."); |
2976 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
2977 | 0 | } |
2978 | 68 | context->blockoffsets[i] = (int32_t)total_nbytes; |
2979 | 68 | total_nbytes += context->blocknbytes[i]; |
2980 | 68 | if (total_nbytes > context->sourcesize) { |
2981 | 44 | BLOSC_TRACE_ERROR("VL-block sizes exceed chunk nbytes."); |
2982 | 44 | return BLOSC2_ERROR_INVALID_HEADER; |
2983 | 44 | } |
2984 | 24 | if (context->blocknbytes[i] > max_blocksize) { |
2985 | 18 | max_blocksize = context->blocknbytes[i]; |
2986 | 18 | } |
2987 | 24 | prev_bstart = bstart; |
2988 | 24 | } |
2989 | 6 | if (total_nbytes != context->sourcesize) { |
2990 | 3 | BLOSC_TRACE_ERROR("VL-block sizes do not add up to chunk nbytes."); |
2991 | 3 | return BLOSC2_ERROR_INVALID_HEADER; |
2992 | 3 | } |
2993 | 3 | context->blocksize = max_blocksize; |
2994 | 3 | context->leftover = 0; |
2995 | 3 | } |
2996 | 288 | } |
2997 | | |
2998 | 63.2k | return 0; |
2999 | 63.5k | } |
3000 | | |
3001 | 63.5k | static int write_compression_header(blosc2_context* context, bool extended_header) { |
3002 | 63.5k | blosc_header header; |
3003 | 63.5k | int dont_split; |
3004 | 63.5k | bool vlblocks = (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) != 0; |
3005 | | |
3006 | 63.5k | if (context->clevel == 0) { |
3007 | | /* Compression level 0 means no compression — dicts serve no purpose here */ |
3008 | 298 | context->use_dict = 0; |
3009 | 298 | } |
3010 | | |
3011 | 63.5k | int dict_training = context->use_dict && (context->dict_cdict == NULL); |
3012 | | |
3013 | 63.5k | context->header_flags = 0; |
3014 | | |
3015 | 63.5k | if (!vlblocks && context->clevel == 0) { |
3016 | | /* Compression level 0 means buffer to be memcpy'ed */ |
3017 | 298 | context->header_flags |= (uint8_t)BLOSC_MEMCPYED; |
3018 | 298 | } |
3019 | 63.5k | if (!vlblocks && context->sourcesize < BLOSC_MIN_BUFFERSIZE) { |
3020 | | /* Buffer is too small. Try memcpy'ing. */ |
3021 | 155 | context->header_flags |= (uint8_t)BLOSC_MEMCPYED; |
3022 | 155 | } |
3023 | | |
3024 | 63.5k | bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
3025 | 63.5k | if (extended_header) { |
3026 | | /* Indicate that we are building an extended header */ |
3027 | 63.5k | context->header_overhead = BLOSC_EXTENDED_HEADER_LENGTH; |
3028 | 63.5k | context->header_flags |= (BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE); |
3029 | | /* Store filter pipeline info at the end of the header */ |
3030 | 63.5k | if (dict_training || memcpyed) { |
3031 | 442 | context->bstarts = NULL; |
3032 | 442 | context->output_bytes = context->header_overhead; |
3033 | 63.1k | } else { |
3034 | 63.1k | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3035 | 63.1k | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
3036 | 63.1k | } |
3037 | 63.5k | } else { |
3038 | | // Regular header |
3039 | 0 | context->header_overhead = BLOSC_MIN_HEADER_LENGTH; |
3040 | 0 | if (memcpyed) { |
3041 | 0 | context->bstarts = NULL; |
3042 | 0 | context->output_bytes = context->header_overhead; |
3043 | 0 | } else { |
3044 | 0 | context->bstarts = (int32_t *) (context->dest + context->header_overhead); |
3045 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
3046 | 0 | } |
3047 | 0 | } |
3048 | | |
3049 | | /* If the header + block starts don't fit in destsize, fall back to memcpy */ |
3050 | 63.5k | if (!memcpyed && context->output_bytes > context->destsize) { |
3051 | 9 | context->header_flags |= (uint8_t)BLOSC_MEMCPYED; |
3052 | 9 | memcpyed = true; |
3053 | 9 | context->bstarts = NULL; |
3054 | 9 | context->output_bytes = context->header_overhead; |
3055 | 9 | } |
3056 | | |
3057 | | // when memcpyed bit is set, there is no point in dealing with others |
3058 | 63.5k | if (!memcpyed) { |
3059 | 63.1k | if (context->filter_flags & BLOSC_DOSHUFFLE) { |
3060 | | /* Byte-shuffle is active */ |
3061 | 22.2k | context->header_flags |= BLOSC_DOSHUFFLE; |
3062 | 22.2k | } |
3063 | | |
3064 | 63.1k | if (context->filter_flags & BLOSC_DOBITSHUFFLE) { |
3065 | | /* Bit-shuffle is active */ |
3066 | 13.3k | context->header_flags |= BLOSC_DOBITSHUFFLE; |
3067 | 13.3k | } |
3068 | | |
3069 | 63.1k | if (context->filter_flags & BLOSC_DODELTA) { |
3070 | | /* Delta is active */ |
3071 | 0 | context->header_flags |= BLOSC_DODELTA; |
3072 | 0 | } |
3073 | | |
3074 | 63.1k | dont_split = vlblocks || !split_block(context, context->typesize, |
3075 | 63.1k | context->blocksize); |
3076 | | |
3077 | | /* dont_split is in bit 4 */ |
3078 | 63.1k | context->header_flags |= dont_split << 4; |
3079 | | /* codec starts at bit 5 */ |
3080 | 63.1k | uint8_t compformat = compcode_to_compformat(context->compcode); |
3081 | 63.1k | context->header_flags |= compformat << 5; |
3082 | 63.1k | } |
3083 | | |
3084 | | // Create blosc header and store to dest |
3085 | 63.5k | blosc2_initialize_header_from_context(context, &header, extended_header); |
3086 | | |
3087 | 63.5k | memcpy(context->dest, &header, (extended_header) ? |
3088 | 63.5k | BLOSC_EXTENDED_HEADER_LENGTH : BLOSC_MIN_HEADER_LENGTH); |
3089 | | |
3090 | 63.5k | return 1; |
3091 | 63.5k | } |
3092 | | |
3093 | | |
3094 | 63.5k | static int blosc_compress_context(blosc2_context* context) { |
3095 | 63.5k | int ntbytes = 0; |
3096 | 63.5k | blosc_timestamp_t last, current; |
3097 | 63.5k | bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
3098 | | |
3099 | 63.5k | blosc_set_timestamp(&last); |
3100 | | |
3101 | 63.5k | if (!memcpyed) { |
3102 | | /* Do the actual compression */ |
3103 | 63.1k | ntbytes = do_job(context); |
3104 | 63.1k | if (ntbytes < 0) { |
3105 | 0 | return ntbytes; |
3106 | 0 | } |
3107 | 63.1k | if (ntbytes == 0) { |
3108 | | // Try out with a memcpy later on (last chance for fitting src buffer in dest). |
3109 | 17.2k | context->header_flags |= (uint8_t)BLOSC_MEMCPYED; |
3110 | 17.2k | memcpyed = true; |
3111 | 17.2k | } |
3112 | 63.1k | } |
3113 | | |
3114 | 63.5k | int dont_split = (context->header_flags & 0x10) >> 4; |
3115 | 63.5k | int nstreams = context->nblocks; |
3116 | 63.5k | if (!dont_split) { |
3117 | | // When splitting, the number of streams is computed differently |
3118 | 22.7k | if (context->leftover) { |
3119 | 270 | nstreams = (context->nblocks - 1) * context->typesize + 1; |
3120 | 270 | } |
3121 | 22.4k | else { |
3122 | 22.4k | nstreams *= context->typesize; |
3123 | 22.4k | } |
3124 | 22.7k | } |
3125 | | |
3126 | 63.5k | if (memcpyed) { |
3127 | 17.6k | if (context->sourcesize + context->header_overhead > context->destsize) { |
3128 | | /* We are exceeding maximum output size */ |
3129 | 6.85k | ntbytes = 0; |
3130 | 6.85k | } |
3131 | 10.8k | else { |
3132 | 10.8k | context->output_bytes = context->header_overhead; |
3133 | 10.8k | ntbytes = do_job(context); |
3134 | 10.8k | if (ntbytes < 0) { |
3135 | 0 | return ntbytes; |
3136 | 0 | } |
3137 | | // Success! update the memcpy bit in header |
3138 | 10.8k | context->dest[BLOSC2_CHUNK_FLAGS] = context->header_flags; |
3139 | | // and clear the memcpy bit in context (for next reuse) |
3140 | 10.8k | context->header_flags &= ~(uint8_t)BLOSC_MEMCPYED; |
3141 | 10.8k | } |
3142 | 17.6k | } |
3143 | 45.9k | else { |
3144 | | // Check whether we have a run for the whole chunk |
3145 | 45.9k | int dict_training = context->use_dict && (context->dict_cdict == NULL); |
3146 | 45.9k | int start_csizes = context->header_overhead + 4 * context->nblocks; |
3147 | 45.9k | if (!dict_training && ntbytes == (int)(start_csizes + nstreams * sizeof(int32_t))) { |
3148 | | // The streams are all zero runs (by construction). Encode it... |
3149 | 6.29k | context->dest[BLOSC2_CHUNK_BLOSC2_FLAGS] |= BLOSC2_SPECIAL_ZERO << 4; |
3150 | | // ...and assign the new chunk length |
3151 | 6.29k | ntbytes = context->header_overhead; |
3152 | 6.29k | } |
3153 | 45.9k | } |
3154 | | |
3155 | | /* Set the number of compressed bytes in header */ |
3156 | 63.5k | _sw32(context->dest + BLOSC2_CHUNK_CBYTES, ntbytes); |
3157 | 63.5k | if (context->blosc2_flags & BLOSC2_INSTR_CODEC) { |
3158 | 0 | dont_split = (context->header_flags & 0x10) >> 4; |
3159 | 0 | int32_t blocksize = dont_split ? (int32_t)sizeof(blosc2_instr) : (int32_t)sizeof(blosc2_instr) * context->typesize; |
3160 | 0 | _sw32(context->dest + BLOSC2_CHUNK_NBYTES, nstreams * (int32_t)sizeof(blosc2_instr)); |
3161 | 0 | _sw32(context->dest + BLOSC2_CHUNK_BLOCKSIZE, blocksize); |
3162 | 0 | } |
3163 | | |
3164 | | /* Set the number of bytes in dest buffer (might be useful for tuner) */ |
3165 | 63.5k | context->destsize = ntbytes; |
3166 | | |
3167 | 63.5k | if (context->tuner_params != NULL) { |
3168 | 0 | blosc_set_timestamp(¤t); |
3169 | 0 | double ctime = blosc_elapsed_secs(last, current); |
3170 | 0 | int rc; |
3171 | 0 | if (context->tuner_id < BLOSC_LAST_TUNER && context->tuner_id == BLOSC_STUNE) { |
3172 | 0 | rc = blosc_stune_update(context, ctime); |
3173 | 0 | } else { |
3174 | 0 | for (int i = 0; i < g_ntuners; ++i) { |
3175 | 0 | if (g_tuners[i].id == context->tuner_id) { |
3176 | 0 | if (g_tuners[i].update == NULL) { |
3177 | 0 | if (fill_tuner(&g_tuners[i]) < 0) { |
3178 | 0 | BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
3179 | 0 | return BLOSC2_ERROR_FAILURE; |
3180 | 0 | } |
3181 | 0 | } |
3182 | 0 | rc = g_tuners[i].update(context, ctime); |
3183 | 0 | goto urtunersuccess; |
3184 | 0 | } |
3185 | 0 | } |
3186 | 0 | BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", context->tuner_id); |
3187 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3188 | 0 | urtunersuccess:; |
3189 | 0 | } |
3190 | 0 | if (rc < 0) { |
3191 | 0 | BLOSC_TRACE_ERROR("Error in tuner update func\n"); |
3192 | 0 | return BLOSC2_ERROR_TUNER; |
3193 | 0 | } |
3194 | 0 | } |
3195 | | |
3196 | 63.5k | return ntbytes; |
3197 | 63.5k | } |
3198 | | |
3199 | | |
3200 | 0 | static int blosc_compress_context_without_dict(blosc2_context* context) { |
3201 | 0 | int saved_use_dict = context->use_dict; |
3202 | 0 | context->use_dict = 0; |
3203 | 0 | context->dest[BLOSC2_CHUNK_BLOSC2_FLAGS] &= ~(uint8_t)BLOSC2_USEDICT; |
3204 | 0 | int cbytes = blosc_compress_context(context); |
3205 | 0 | context->use_dict = saved_use_dict; |
3206 | 0 | return cbytes; |
3207 | 0 | } |
3208 | | |
3209 | | |
3210 | 0 | int blosc2_ctx_set_blosc1_format(blosc2_context* context, bool enable) { |
3211 | 0 | if (context == NULL || context->do_compress != 1) { |
3212 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3213 | 0 | } |
3214 | 0 | context->blosc1_format = enable; |
3215 | 0 | return BLOSC2_ERROR_SUCCESS; |
3216 | 0 | } |
3217 | | |
3218 | | |
3219 | 0 | bool blosc2_ctx_get_blosc1_format(const blosc2_context* context) { |
3220 | 0 | return context != NULL && context->blosc1_format; |
3221 | 0 | } |
3222 | | |
3223 | | |
3224 | | /* Check that a context set to write Blosc1 chunks only uses what the format can express */ |
3225 | 63.5k | static int blosc1_format_check(const blosc2_context* context) { |
3226 | 63.5k | if (!context->blosc1_format) { |
3227 | 63.5k | return BLOSC2_ERROR_SUCCESS; |
3228 | 63.5k | } |
3229 | 0 | switch (context->compcode) { |
3230 | 0 | case BLOSC_BLOSCLZ: |
3231 | 0 | case BLOSC_LZ4: |
3232 | 0 | case BLOSC_LZ4HC: |
3233 | 0 | case BLOSC_ZLIB: |
3234 | 0 | case BLOSC_ZSTD: |
3235 | 0 | break; |
3236 | 0 | default: |
3237 | 0 | BLOSC_TRACE_ERROR("Codec %d cannot be used when writing Blosc1 chunks.", context->compcode); |
3238 | 0 | return BLOSC2_ERROR_CODEC_SUPPORT; |
3239 | 0 | } |
3240 | | /* The Blosc1 header only has flags for a shuffle or a bitshuffle, so the pipeline can have |
3241 | | one of those and nothing else, in any of its slots. */ |
3242 | 0 | int nfilters = 0; |
3243 | 0 | for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
3244 | 0 | uint8_t filter = context->filters[i]; |
3245 | 0 | if (filter == BLOSC_NOFILTER) { |
3246 | 0 | continue; |
3247 | 0 | } |
3248 | 0 | bool plain_shuffle = (filter == BLOSC_SHUFFLE || filter == BLOSC_BITSHUFFLE) && |
3249 | 0 | context->filters_meta[i] == 0; |
3250 | 0 | if (!plain_shuffle || ++nfilters > 1) { |
3251 | 0 | BLOSC_TRACE_ERROR("Filter %d in slot %d cannot be used when writing Blosc1 chunks.", filter, i); |
3252 | 0 | return BLOSC2_ERROR_FILTER_PIPELINE; |
3253 | 0 | } |
3254 | 0 | } |
3255 | 0 | if (context->use_dict || (context->blosc2_flags2 & BLOSC2_VL_BLOCKS)) { |
3256 | 0 | BLOSC_TRACE_ERROR("Dictionaries and variable-length blocks cannot be used when writing Blosc1 chunks."); |
3257 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3258 | 0 | } |
3259 | 0 | return BLOSC2_ERROR_SUCCESS; |
3260 | 0 | } |
3261 | | |
3262 | | |
3263 | | /* The public secure routine for compression with context. */ |
3264 | | int blosc2_compress_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
3265 | 45.5k | void* dest, int32_t destsize) { |
3266 | 45.5k | int error, cbytes; |
3267 | | |
3268 | 45.5k | if (context->do_compress != 1) { |
3269 | 0 | BLOSC_TRACE_ERROR("Context is not meant for compression. Giving up."); |
3270 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3271 | 0 | } |
3272 | | |
3273 | 45.5k | error = initialize_context_compression( |
3274 | 45.5k | context, src, srcsize, dest, destsize, |
3275 | 45.5k | context->clevel, context->filters, context->filters_meta, |
3276 | 45.5k | context->typesize, context->compcode, context->blocksize, |
3277 | 45.5k | context->new_nthreads, context->nthreads, context->splitmode, |
3278 | 45.5k | context->tuner_id, context->tuner_params, context->schunk); |
3279 | 45.5k | if (error <= 0) { |
3280 | 0 | return error; |
3281 | 0 | } |
3282 | | |
3283 | 45.5k | error = blosc1_format_check(context); |
3284 | 45.5k | if (error < 0) { |
3285 | 0 | return error; |
3286 | 0 | } |
3287 | | |
3288 | | /* Write the extended header, or the Blosc1 header if requested */ |
3289 | 45.5k | error = write_compression_header(context, !context->blosc1_format); |
3290 | 45.5k | if (error < 0) { |
3291 | 0 | return error; |
3292 | 0 | } |
3293 | | |
3294 | 45.5k | cbytes = blosc_compress_context(context); |
3295 | 45.5k | if (cbytes < 0) { |
3296 | 0 | return cbytes; |
3297 | 0 | } |
3298 | | |
3299 | 45.5k | if (context->use_dict && context->dict_cdict == NULL) { |
3300 | | /* blosc_compress_context() overwrites context->destsize with the training-pass output |
3301 | | * size. Restore it so that the real compression pass has the correct output-buffer size. */ |
3302 | 0 | context->destsize = destsize; |
3303 | |
|
3304 | 0 | bool is_lz4 = (context->compcode == BLOSC_LZ4 || context->compcode == BLOSC_LZ4HC); |
3305 | 0 | if (!is_lz4 && context->compcode != BLOSC_ZSTD) { |
3306 | 0 | const char* compname; |
3307 | 0 | compname = clibcode_to_clibname(context->compcode); |
3308 | 0 | BLOSC_TRACE_ERROR("Codec %s does not support dicts. Giving up.", |
3309 | 0 | compname); |
3310 | 0 | return BLOSC2_ERROR_CODEC_DICT; |
3311 | 0 | } |
3312 | | |
3313 | | // Build the dictionary out of the filters outcome and compress with it. |
3314 | | // For LZ4/LZ4HC the raw samples are used directly (no training algorithm). |
3315 | | // For ZSTD, ZDICT_trainFromBuffer() is used. |
3316 | 0 | int32_t dict_maxsize = BLOSC2_MAXDICTSIZE; |
3317 | | // Do not make the dict more than 5% larger than uncompressed buffer |
3318 | 0 | if (dict_maxsize > srcsize / 20) { |
3319 | 0 | dict_maxsize = srcsize / 20; |
3320 | 0 | } |
3321 | 0 | void* samples_buffer = context->dest + context->header_overhead; |
3322 | 0 | unsigned nblocks = (unsigned)context->nblocks; |
3323 | 0 | int dont_split = (context->header_flags & 0x10) >> 4; |
3324 | 0 | if (!dont_split) { |
3325 | 0 | nblocks = nblocks * context->typesize; |
3326 | 0 | } |
3327 | 0 | if (nblocks < 8) { |
3328 | 0 | nblocks = 8; |
3329 | 0 | } |
3330 | |
|
3331 | 0 | unsigned sample_fraction = 16; |
3332 | 0 | size_t sample_size = context->sourcesize / nblocks / sample_fraction; |
3333 | | |
3334 | | // When the data is too small to produce useful dict samples, |
3335 | | // fall back to plain compression without a dict. |
3336 | 0 | if (dict_maxsize < BLOSC2_MINUSEFULDICT || sample_size == 0) { |
3337 | 0 | BLOSC_TRACE_WARNING("Data too small for dict training (dict_maxsize=%d, sample_size=%zu)." |
3338 | 0 | " Falling back to plain compression.", dict_maxsize, sample_size); |
3339 | 0 | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3340 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
3341 | 0 | cbytes = blosc_compress_context_without_dict(context); |
3342 | 0 | } |
3343 | 0 | else if (is_lz4) { |
3344 | | // LZ4/LZ4HC: use raw sample data directly as the dictionary (no training step). |
3345 | 0 | int32_t dict_actual_size = (int32_t)(nblocks * sample_size); |
3346 | 0 | if (dict_actual_size > dict_maxsize) { |
3347 | 0 | dict_actual_size = dict_maxsize; |
3348 | 0 | } |
3349 | | |
3350 | | // Reset bstarts and embed dict in the output buffer. |
3351 | 0 | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3352 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
3353 | | /* Move dict bytes; samples_buffer overlaps the dict (and its size) in dest, |
3354 | | so this has to be a memmove, done before writing the size */ |
3355 | 0 | context->dict_buffer = context->dest + context->output_bytes + sizeof(int32_t); |
3356 | 0 | memmove(context->dict_buffer, samples_buffer, (size_t)dict_actual_size); |
3357 | | /* Write dict size */ |
3358 | 0 | _sw32(context->dest + context->output_bytes, dict_actual_size); |
3359 | 0 | context->output_bytes += (int32_t)sizeof(int32_t); |
3360 | | /* Build the stream used as cdict (pre-loaded with the dict bytes) */ |
3361 | 0 | if (context->compcode == BLOSC_LZ4HC) { |
3362 | 0 | LZ4_streamHC_t* lz4hc_cdict = LZ4_createStreamHC(); |
3363 | 0 | LZ4_loadDictHC(lz4hc_cdict, (const char*)context->dict_buffer, dict_actual_size); |
3364 | 0 | context->dict_cdict = lz4hc_cdict; |
3365 | 0 | } else { |
3366 | 0 | LZ4_stream_t* lz4_cdict = LZ4_createStream(); |
3367 | 0 | LZ4_loadDict(lz4_cdict, (const char*)context->dict_buffer, dict_actual_size); |
3368 | 0 | context->dict_cdict = lz4_cdict; |
3369 | 0 | } |
3370 | 0 | context->output_bytes += dict_actual_size; |
3371 | 0 | context->dict_size = dict_actual_size; |
3372 | | |
3373 | | /* Compress with dict */ |
3374 | 0 | cbytes = blosc_compress_context(context); |
3375 | | |
3376 | | // Invalidate the dictionary so the context can be reused for the next chunk |
3377 | 0 | context->dict_buffer = NULL; |
3378 | 0 | if (context->compcode == BLOSC_LZ4HC) { |
3379 | 0 | LZ4_freeStreamHC((LZ4_streamHC_t*)context->dict_cdict); |
3380 | 0 | } else { |
3381 | 0 | LZ4_freeStream((LZ4_stream_t*)context->dict_cdict); |
3382 | 0 | } |
3383 | 0 | context->dict_cdict = NULL; |
3384 | 0 | } |
3385 | 0 | #ifdef HAVE_ZSTD |
3386 | 0 | else { |
3387 | | // Populate the samples sizes for training the dictionary |
3388 | 0 | size_t* samples_sizes = malloc(nblocks * sizeof(size_t)); |
3389 | 0 | BLOSC_ERROR_NULL(samples_sizes, BLOSC2_ERROR_MEMORY_ALLOC); |
3390 | 0 | for (size_t i = 0; i < nblocks; i++) { |
3391 | 0 | samples_sizes[i] = sample_size; |
3392 | 0 | } |
3393 | | |
3394 | | // Train from samples |
3395 | 0 | void* dict_buffer = malloc(dict_maxsize); |
3396 | 0 | if (dict_buffer == NULL) { |
3397 | 0 | free(samples_sizes); |
3398 | 0 | BLOSC_ERROR_NULL(dict_buffer, BLOSC2_ERROR_MEMORY_ALLOC); |
3399 | 0 | } |
3400 | 0 | int32_t dict_actual_size = (int32_t)ZDICT_trainFromBuffer( |
3401 | 0 | dict_buffer, dict_maxsize, |
3402 | 0 | samples_buffer, samples_sizes, nblocks); |
3403 | | |
3404 | | // TODO: experiment with parameters of low-level fast cover algorithm |
3405 | | // Note that this API is still unstable. See: https://github.com/facebook/zstd/issues/1599 |
3406 | | // ZDICT_fastCover_params_t fast_cover_params; |
3407 | | // memset(&fast_cover_params, 0, sizeof(fast_cover_params)); |
3408 | | // fast_cover_params.d = nblocks; |
3409 | | // fast_cover_params.steps = 4; |
3410 | | // fast_cover_params.zParams.compressionLevel = context->clevel; |
3411 | | // size_t dict_actual_size = ZDICT_optimizeTrainFromBuffer_fastCover( |
3412 | | // dict_buffer, dict_maxsize, samples_buffer, samples_sizes, nblocks, |
3413 | | // &fast_cover_params); |
3414 | |
|
3415 | 0 | free(samples_sizes); |
3416 | 0 | if (ZDICT_isError(dict_actual_size) != ZSTD_error_no_error) { |
3417 | 0 | BLOSC_TRACE_WARNING("ZDICT_trainFromBuffer() failed: '%s'." |
3418 | 0 | " Falling back to plain compression.", |
3419 | 0 | ZDICT_getErrorName(dict_actual_size)); |
3420 | 0 | free(dict_buffer); |
3421 | 0 | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3422 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
3423 | 0 | cbytes = blosc_compress_context_without_dict(context); |
3424 | 0 | } |
3425 | 0 | else { |
3426 | 0 | assert(dict_actual_size > 0); |
3427 | | |
3428 | | // Update bytes counter and pointers to bstarts for the new compressed buffer |
3429 | 0 | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3430 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * context->nblocks; |
3431 | | /* Write the size of trained dict at the end of bstarts */ |
3432 | 0 | _sw32(context->dest + context->output_bytes, (int32_t)dict_actual_size); |
3433 | 0 | context->output_bytes += sizeof(int32_t); |
3434 | | /* Write the trained dict afterwards */ |
3435 | 0 | context->dict_buffer = context->dest + context->output_bytes; |
3436 | 0 | memcpy(context->dict_buffer, dict_buffer, (unsigned int)dict_actual_size); |
3437 | 0 | context->dict_cdict = ZSTD_createCDict(dict_buffer, dict_actual_size, 1); // TODO: use get_accel() |
3438 | 0 | free(dict_buffer); // the dictionary is copied in the header now |
3439 | 0 | context->output_bytes += (int32_t)dict_actual_size; |
3440 | 0 | context->dict_size = dict_actual_size; |
3441 | | |
3442 | | /* Compress with dict */ |
3443 | 0 | cbytes = blosc_compress_context(context); |
3444 | | |
3445 | | // Invalidate the dictionary for compressing other chunks using the same context |
3446 | 0 | context->dict_buffer = NULL; |
3447 | 0 | ZSTD_freeCDict(context->dict_cdict); |
3448 | 0 | context->dict_cdict = NULL; |
3449 | 0 | } // ZDICT_isError |
3450 | 0 | } // ZSTD else branch |
3451 | 0 | #endif // HAVE_ZSTD |
3452 | 0 | } |
3453 | | |
3454 | 45.5k | return cbytes; |
3455 | 45.5k | } |
3456 | | |
3457 | | /* Helper for reorder_vl_blocks_output: sort (bstart, logical_index) pairs by bstart */ |
3458 | | typedef struct { int32_t bstart; int32_t idx; } vl_bstart_entry_t; |
3459 | 0 | static int cmp_vl_bstart(const void *a, const void *b) { |
3460 | 0 | int32_t ba = ((const vl_bstart_entry_t *)a)->bstart; |
3461 | 0 | int32_t bb = ((const vl_bstart_entry_t *)b)->bstart; |
3462 | 0 | return (ba > bb) - (ba < bb); |
3463 | 0 | } |
3464 | | |
3465 | | /* Ensure that VL-block compressed data is stored in block-index order. |
3466 | | * |
3467 | | * The decompressor derives each block's compressed span as |
3468 | | * bstarts[i+1] - bstarts[i], which requires bstarts to be monotonically |
3469 | | * increasing. Multi-threaded compression writes blocks in finish order |
3470 | | * (non-deterministic), so bstarts may be out of order. This function |
3471 | | * rearranges the compressed block data in the output buffer and fixes |
3472 | | * bstarts so that block 0 comes first, block 1 second, and so on. |
3473 | | */ |
3474 | 0 | static int reorder_vl_blocks_output(blosc2_context *context) { |
3475 | 0 | int32_t nblocks = context->nblocks; |
3476 | 0 | if (nblocks <= 1) { |
3477 | 0 | return 0; |
3478 | 0 | } |
3479 | | |
3480 | 0 | int32_t output_bytes = context->output_bytes; |
3481 | 0 | int32_t *bstarts = context->bstarts; |
3482 | 0 | uint8_t *dest = context->dest; |
3483 | | |
3484 | | /* Fast path: blocks are already in index order (serial or lucky MT) */ |
3485 | 0 | bool ordered = true; |
3486 | 0 | for (int32_t i = 1; i < nblocks; i++) { |
3487 | 0 | if (sw32_(bstarts + i) < sw32_(bstarts + i - 1)) { |
3488 | 0 | ordered = false; |
3489 | 0 | break; |
3490 | 0 | } |
3491 | 0 | } |
3492 | 0 | if (ordered) { |
3493 | 0 | return 0; |
3494 | 0 | } |
3495 | | |
3496 | | /* When a dict is embedded, block data starts after [bstarts | dict_size | dict_data]. |
3497 | | * Use the minimum bstart to locate the actual start of the compressed block region; |
3498 | | * this avoids touching (and corrupting) the dict bytes. */ |
3499 | 0 | int32_t data_start = sw32_(bstarts); |
3500 | 0 | for (int32_t i = 1; i < nblocks; i++) { |
3501 | 0 | int32_t bs = sw32_(bstarts + i); |
3502 | 0 | if (bs < data_start) { |
3503 | 0 | data_start = bs; |
3504 | 0 | } |
3505 | 0 | } |
3506 | |
|
3507 | 0 | int32_t data_size = output_bytes - data_start; |
3508 | 0 | vl_bstart_entry_t *entries = malloc((size_t)nblocks * sizeof(vl_bstart_entry_t)); |
3509 | 0 | int32_t *block_cbytes = malloc((size_t)nblocks * sizeof(int32_t)); |
3510 | 0 | uint8_t *temp = malloc((size_t)data_size); |
3511 | 0 | if (entries == NULL || block_cbytes == NULL || temp == NULL) { |
3512 | 0 | free(entries); |
3513 | 0 | free(block_cbytes); |
3514 | 0 | free(temp); |
3515 | 0 | return BLOSC2_ERROR_MEMORY_ALLOC; |
3516 | 0 | } |
3517 | | |
3518 | 0 | for (int32_t i = 0; i < nblocks; i++) { |
3519 | 0 | entries[i].bstart = sw32_(bstarts + i); |
3520 | 0 | entries[i].idx = i; |
3521 | 0 | } |
3522 | | |
3523 | | /* Sort entries by physical position to compute each block's compressed size */ |
3524 | 0 | qsort(entries, (size_t)nblocks, sizeof(vl_bstart_entry_t), cmp_vl_bstart); |
3525 | |
|
3526 | 0 | for (int32_t j = 0; j < nblocks; j++) { |
3527 | 0 | int32_t next = (j + 1 < nblocks) ? entries[j + 1].bstart : output_bytes; |
3528 | 0 | block_cbytes[entries[j].idx] = next - entries[j].bstart; |
3529 | 0 | } |
3530 | | |
3531 | | /* Snapshot compressed block data (not the dict) so we can safely overwrite dest */ |
3532 | 0 | memcpy(temp, dest + data_start, (size_t)data_size); |
3533 | | |
3534 | | /* Write blocks to dest in logical index order and update bstarts */ |
3535 | 0 | int32_t cur_pos = data_start; |
3536 | 0 | for (int32_t i = 0; i < nblocks; i++) { |
3537 | 0 | int32_t old_pos = sw32_(bstarts + i); |
3538 | 0 | _sw32(bstarts + i, cur_pos); |
3539 | 0 | memcpy(dest + cur_pos, temp + (old_pos - data_start), (size_t)block_cbytes[i]); |
3540 | 0 | cur_pos += block_cbytes[i]; |
3541 | 0 | } |
3542 | |
|
3543 | 0 | free(entries); |
3544 | 0 | free(block_cbytes); |
3545 | 0 | free(temp); |
3546 | 0 | return 0; |
3547 | 0 | } |
3548 | | |
3549 | | |
3550 | 0 | int blosc2_vlchunk_get_nblocks(const void* src, int32_t srcsize, int32_t* nblocks) { |
3551 | 0 | if (src == NULL || nblocks == NULL) { |
3552 | 0 | BLOSC_TRACE_ERROR("src and nblocks must not be NULL."); |
3553 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3554 | 0 | } |
3555 | 0 | blosc_header header; |
3556 | 0 | int result = read_chunk_header((const uint8_t*)src, srcsize, true, &header); |
3557 | 0 | if (result < 0) { |
3558 | 0 | return result; |
3559 | 0 | } |
3560 | 0 | if ((header.blosc2_flags2 & BLOSC2_VL_BLOCKS) == 0) { |
3561 | 0 | BLOSC_TRACE_ERROR("Chunk does not use VL blocks."); |
3562 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3563 | 0 | } |
3564 | | /* For VL-block chunks the blocksize field in the header stores nblocks. */ |
3565 | 0 | *nblocks = header.blocksize; |
3566 | 0 | return 0; |
3567 | 0 | } |
3568 | | |
3569 | | |
3570 | | int blosc2_vlcompress_ctx(blosc2_context* context, const void* const* srcs, const int32_t* srcsizes, |
3571 | 0 | int32_t nblocks, void* dest, int32_t destsize) { |
3572 | 0 | int error, cbytes; |
3573 | 0 | int32_t max_blocksize = 0; |
3574 | 0 | int64_t srcsize = 0; |
3575 | |
|
3576 | 0 | if (context->do_compress != 1) { |
3577 | 0 | BLOSC_TRACE_ERROR("Context is not meant for compression. Giving up."); |
3578 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3579 | 0 | } |
3580 | 0 | if (srcs == NULL || srcsizes == NULL || nblocks <= 0) { |
3581 | 0 | BLOSC_TRACE_ERROR("Invalid sources for VL-block compression."); |
3582 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3583 | 0 | } |
3584 | | |
3585 | 0 | for (int32_t i = 0; i < nblocks; ++i) { |
3586 | 0 | if (srcs[i] == NULL || srcsizes[i] <= 0) { |
3587 | 0 | BLOSC_TRACE_ERROR("Invalid VL block at index %d.", i); |
3588 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
3589 | 0 | } |
3590 | 0 | srcsize += srcsizes[i]; |
3591 | 0 | if (srcsize > BLOSC2_MAX_BUFFERSIZE) { |
3592 | 0 | BLOSC_TRACE_ERROR("Input buffer size cannot exceed %d bytes.", BLOSC2_MAX_BUFFERSIZE); |
3593 | 0 | return BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED; |
3594 | 0 | } |
3595 | 0 | if (srcsizes[i] > max_blocksize) { |
3596 | 0 | max_blocksize = srcsizes[i]; |
3597 | 0 | } |
3598 | 0 | } |
3599 | | |
3600 | 0 | error = initialize_context_compression( |
3601 | 0 | context, NULL, (int32_t)srcsize, dest, destsize, |
3602 | 0 | context->clevel, context->filters, context->filters_meta, |
3603 | 0 | context->typesize, context->compcode, max_blocksize, |
3604 | 0 | context->new_nthreads, context->nthreads, context->splitmode, |
3605 | 0 | context->tuner_id, context->tuner_params, context->schunk); |
3606 | 0 | if (error <= 0) { |
3607 | 0 | return error; |
3608 | 0 | } |
3609 | | |
3610 | 0 | context->blosc2_flags2 = BLOSC2_VL_BLOCKS; |
3611 | 0 | context->header_blocksize = nblocks; |
3612 | 0 | context->nblocks = nblocks; |
3613 | 0 | context->leftover = 0; |
3614 | 0 | context->blocksize = max_blocksize; |
3615 | 0 | context->vlblock_sources = (const uint8_t**)srcs; |
3616 | 0 | context->blocknbytes = malloc((size_t)nblocks * sizeof(int32_t)); |
3617 | 0 | BLOSC_ERROR_NULL(context->blocknbytes, BLOSC2_ERROR_MEMORY_ALLOC); |
3618 | 0 | memcpy(context->blocknbytes, srcsizes, (size_t)nblocks * sizeof(int32_t)); |
3619 | |
|
3620 | 0 | error = write_compression_header(context, true); |
3621 | 0 | if (error < 0) { |
3622 | 0 | return error; |
3623 | 0 | } |
3624 | | |
3625 | 0 | cbytes = blosc_compress_context(context); |
3626 | 0 | if (cbytes < 0) { |
3627 | 0 | context->vlblock_sources = NULL; |
3628 | 0 | return cbytes; |
3629 | 0 | } |
3630 | | |
3631 | | /* blosc_compress_context() overwrites context->destsize with the training-pass output |
3632 | | * size (which is tiny — just the raw sample data). Restore it so that the real |
3633 | | * compression pass below has the correct output-buffer size. */ |
3634 | 0 | context->destsize = destsize; |
3635 | |
|
3636 | 0 | #ifdef HAVE_ZSTD |
3637 | 0 | if (context->use_dict && context->dict_cdict == NULL) { |
3638 | 0 | bool is_lz4 = (context->compcode == BLOSC_LZ4 || context->compcode == BLOSC_LZ4HC); |
3639 | | // The first blosc_compress_context() above was a dict-training pass that stored |
3640 | | // raw (uncompressed) VL block data at dest+header_overhead as samples. Now build |
3641 | | // the dictionary from those samples and do the real compression pass. |
3642 | 0 | int32_t dict_maxsize = BLOSC2_MAXDICTSIZE; |
3643 | | // Do not make the dict more than 5% of the uncompressed size |
3644 | 0 | if (dict_maxsize > (int32_t)srcsize / 20) { |
3645 | 0 | dict_maxsize = (int32_t)srcsize / 20; |
3646 | 0 | } |
3647 | | // Mirror the sample_size guard from blosc2_compress_ctx: if the average |
3648 | | // per-block sample is too small for useful dict training, fall back. |
3649 | 0 | size_t vl_sample_size = (nblocks > 0) ? ((size_t)srcsize / (size_t)nblocks / 16) : 0; |
3650 | 0 | if (dict_maxsize < BLOSC2_MINUSEFULDICT || nblocks < 8 || vl_sample_size == 0) { |
3651 | | // Data is too small or too few VL blocks to build a useful dictionary; |
3652 | | // fall back to plain compression without a dict. |
3653 | 0 | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3654 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * nblocks; |
3655 | 0 | context->vlblock_sources = (const uint8_t**)srcs; |
3656 | 0 | cbytes = blosc_compress_context_without_dict(context); |
3657 | 0 | context->vlblock_sources = NULL; |
3658 | 0 | } |
3659 | 0 | else if (is_lz4) { |
3660 | | // LZ4/LZ4HC: use the concatenated raw VL block data directly as the dictionary. |
3661 | 0 | void* samples_buffer = context->dest + context->header_overhead; |
3662 | 0 | int32_t total_raw = 0; |
3663 | 0 | for (int32_t i = 0; i < nblocks; i++) { |
3664 | 0 | total_raw += context->blocknbytes[i]; |
3665 | 0 | } |
3666 | 0 | int32_t dict_actual_size = total_raw < dict_maxsize ? total_raw : dict_maxsize; |
3667 | |
|
3668 | 0 | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3669 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * nblocks; |
3670 | | // samples_buffer overlaps the dict (and its size) in dest: memmove before writing the size |
3671 | 0 | context->dict_buffer = context->dest + context->output_bytes + sizeof(int32_t); |
3672 | 0 | memmove(context->dict_buffer, samples_buffer, (size_t)dict_actual_size); |
3673 | 0 | _sw32(context->dest + context->output_bytes, dict_actual_size); |
3674 | 0 | context->output_bytes += (int32_t)sizeof(int32_t); |
3675 | 0 | if (context->compcode == BLOSC_LZ4HC) { |
3676 | 0 | LZ4_streamHC_t* lz4hc_cdict = LZ4_createStreamHC(); |
3677 | 0 | LZ4_loadDictHC(lz4hc_cdict, (const char*)context->dict_buffer, dict_actual_size); |
3678 | 0 | context->dict_cdict = lz4hc_cdict; |
3679 | 0 | } else { |
3680 | 0 | LZ4_stream_t* lz4_cdict = LZ4_createStream(); |
3681 | 0 | LZ4_loadDict(lz4_cdict, (const char*)context->dict_buffer, dict_actual_size); |
3682 | 0 | context->dict_cdict = lz4_cdict; |
3683 | 0 | } |
3684 | 0 | context->output_bytes += dict_actual_size; |
3685 | 0 | context->dict_size = dict_actual_size; |
3686 | |
|
3687 | 0 | context->vlblock_sources = (const uint8_t**)srcs; |
3688 | 0 | cbytes = blosc_compress_context(context); |
3689 | 0 | context->vlblock_sources = NULL; |
3690 | |
|
3691 | 0 | context->dict_buffer = NULL; |
3692 | 0 | if (context->compcode == BLOSC_LZ4HC) { |
3693 | 0 | LZ4_freeStreamHC((LZ4_streamHC_t*)context->dict_cdict); |
3694 | 0 | } else { |
3695 | 0 | LZ4_freeStream((LZ4_stream_t*)context->dict_cdict); |
3696 | 0 | } |
3697 | 0 | context->dict_cdict = NULL; |
3698 | 0 | } |
3699 | 0 | else { |
3700 | | // ZSTD: use ZDICT_trainFromBuffer for dictionary training. |
3701 | | // The training pass left all VL blocks concatenated at dest+header_overhead. |
3702 | | // Use the actual per-block sizes as ZDICT sample sizes so that each VL block |
3703 | | // is treated as a complete, independent training example. |
3704 | 0 | void* samples_buffer = context->dest + context->header_overhead; |
3705 | 0 | size_t* samples_sizes = malloc((size_t)nblocks * sizeof(size_t)); |
3706 | 0 | if (samples_sizes == NULL) { |
3707 | 0 | context->vlblock_sources = NULL; |
3708 | 0 | return BLOSC2_ERROR_MEMORY_ALLOC; |
3709 | 0 | } |
3710 | 0 | for (int32_t i = 0; i < nblocks; i++) { |
3711 | 0 | samples_sizes[i] = (size_t)context->blocknbytes[i]; |
3712 | 0 | } |
3713 | 0 | void* dict_buffer = malloc((size_t)dict_maxsize); |
3714 | 0 | if (dict_buffer == NULL) { |
3715 | 0 | free(samples_sizes); |
3716 | 0 | context->vlblock_sources = NULL; |
3717 | 0 | return BLOSC2_ERROR_MEMORY_ALLOC; |
3718 | 0 | } |
3719 | 0 | int32_t dict_actual_size = (int32_t)ZDICT_trainFromBuffer( |
3720 | 0 | dict_buffer, (size_t)dict_maxsize, samples_buffer, samples_sizes, (unsigned)nblocks); |
3721 | 0 | free(samples_sizes); |
3722 | 0 | if (ZDICT_isError(dict_actual_size) != ZSTD_error_no_error) { |
3723 | | // Training failed (e.g. data is too small for a useful ZSTD dict). |
3724 | | // Fall back to plain compression rather than returning an error. |
3725 | 0 | BLOSC_TRACE_WARNING("ZDICT_trainFromBuffer() failed ('%s'); falling back to plain compression.", |
3726 | 0 | ZDICT_getErrorName(dict_actual_size)); |
3727 | 0 | free(dict_buffer); |
3728 | 0 | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3729 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * nblocks; |
3730 | 0 | context->vlblock_sources = (const uint8_t**)srcs; |
3731 | 0 | cbytes = blosc_compress_context_without_dict(context); |
3732 | 0 | context->vlblock_sources = NULL; |
3733 | 0 | } |
3734 | 0 | else { |
3735 | | // Set up bstarts and embed the trained dictionary in the output buffer. |
3736 | | // Layout after header: [bstarts | dict_size(int32) | dict_data | compressed blocks] |
3737 | 0 | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3738 | 0 | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * nblocks; |
3739 | 0 | _sw32(context->dest + context->output_bytes, dict_actual_size); |
3740 | 0 | context->output_bytes += (int32_t)sizeof(int32_t); |
3741 | 0 | context->dict_buffer = context->dest + context->output_bytes; |
3742 | 0 | memcpy(context->dict_buffer, dict_buffer, (size_t)dict_actual_size); |
3743 | 0 | context->dict_cdict = ZSTD_createCDict(dict_buffer, (size_t)dict_actual_size, 1); |
3744 | 0 | free(dict_buffer); |
3745 | 0 | context->output_bytes += dict_actual_size; |
3746 | 0 | context->dict_size = dict_actual_size; |
3747 | | |
3748 | | /* Actual compression pass using the trained dictionary */ |
3749 | 0 | context->vlblock_sources = (const uint8_t**)srcs; |
3750 | 0 | cbytes = blosc_compress_context(context); |
3751 | 0 | context->vlblock_sources = NULL; |
3752 | | |
3753 | | /* Invalidate the dictionary so the context can be reused for the next chunk */ |
3754 | 0 | context->dict_buffer = NULL; |
3755 | 0 | ZSTD_freeCDict(context->dict_cdict); |
3756 | 0 | context->dict_cdict = NULL; |
3757 | 0 | } |
3758 | 0 | } |
3759 | 0 | if (cbytes < 0) { |
3760 | 0 | return cbytes; |
3761 | 0 | } |
3762 | 0 | } |
3763 | | #else |
3764 | | if (context->use_dict && context->dict_cdict == NULL) { |
3765 | | bool is_lz4 = (context->compcode == BLOSC_LZ4 || context->compcode == BLOSC_LZ4HC); |
3766 | | int32_t dict_maxsize = BLOSC2_MAXDICTSIZE; |
3767 | | if (dict_maxsize > (int32_t)srcsize / 20) { |
3768 | | dict_maxsize = (int32_t)srcsize / 20; |
3769 | | } |
3770 | | size_t vl_sample_size_lz4 = (nblocks > 0) ? ((size_t)srcsize / (size_t)nblocks / 16) : 0; |
3771 | | if (!is_lz4 || dict_maxsize < BLOSC2_MINUSEFULDICT || nblocks < 8 || vl_sample_size_lz4 == 0) { |
3772 | | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3773 | | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * nblocks; |
3774 | | context->vlblock_sources = (const uint8_t**)srcs; |
3775 | | cbytes = blosc_compress_context_without_dict(context); |
3776 | | context->vlblock_sources = NULL; |
3777 | | } |
3778 | | else { |
3779 | | void* samples_buffer = context->dest + context->header_overhead; |
3780 | | int32_t total_raw = 0; |
3781 | | for (int32_t i = 0; i < nblocks; i++) { |
3782 | | total_raw += context->blocknbytes[i]; |
3783 | | } |
3784 | | int32_t dict_actual_size = total_raw < dict_maxsize ? total_raw : dict_maxsize; |
3785 | | |
3786 | | context->bstarts = (int32_t*)(context->dest + context->header_overhead); |
3787 | | context->output_bytes = context->header_overhead + (int32_t)sizeof(int32_t) * nblocks; |
3788 | | // samples_buffer overlaps the dict (and its size) in dest: memmove before writing the size |
3789 | | context->dict_buffer = context->dest + context->output_bytes + sizeof(int32_t); |
3790 | | memmove(context->dict_buffer, samples_buffer, (size_t)dict_actual_size); |
3791 | | _sw32(context->dest + context->output_bytes, dict_actual_size); |
3792 | | context->output_bytes += (int32_t)sizeof(int32_t); |
3793 | | if (context->compcode == BLOSC_LZ4HC) { |
3794 | | LZ4_streamHC_t* lz4hc_cdict = LZ4_createStreamHC(); |
3795 | | LZ4_loadDictHC(lz4hc_cdict, (const char*)context->dict_buffer, dict_actual_size); |
3796 | | context->dict_cdict = lz4hc_cdict; |
3797 | | } else { |
3798 | | LZ4_stream_t* lz4_cdict = LZ4_createStream(); |
3799 | | LZ4_loadDict(lz4_cdict, (const char*)context->dict_buffer, dict_actual_size); |
3800 | | context->dict_cdict = lz4_cdict; |
3801 | | } |
3802 | | context->output_bytes += dict_actual_size; |
3803 | | context->dict_size = dict_actual_size; |
3804 | | |
3805 | | context->vlblock_sources = (const uint8_t**)srcs; |
3806 | | cbytes = blosc_compress_context(context); |
3807 | | context->vlblock_sources = NULL; |
3808 | | |
3809 | | context->dict_buffer = NULL; |
3810 | | if (context->compcode == BLOSC_LZ4HC) { |
3811 | | LZ4_freeStreamHC((LZ4_streamHC_t*)context->dict_cdict); |
3812 | | } else { |
3813 | | LZ4_freeStream((LZ4_stream_t*)context->dict_cdict); |
3814 | | } |
3815 | | context->dict_cdict = NULL; |
3816 | | } |
3817 | | if (cbytes < 0) { |
3818 | | return cbytes; |
3819 | | } |
3820 | | } |
3821 | | #endif // HAVE_ZSTD |
3822 | | |
3823 | 0 | context->vlblock_sources = NULL; |
3824 | | |
3825 | | /* Multi-threaded compression may have written blocks in non-index order. |
3826 | | * Rearrange the output so that bstarts is monotonically increasing, as |
3827 | | * required by the decompressor. */ |
3828 | 0 | error = reorder_vl_blocks_output(context); |
3829 | 0 | if (error < 0) { |
3830 | 0 | return error; |
3831 | 0 | } |
3832 | | |
3833 | 0 | return cbytes; |
3834 | 0 | } |
3835 | | |
3836 | | |
3837 | | void build_filters(const int doshuffle, const int delta, |
3838 | 32.9k | const int32_t typesize, uint8_t* filters) { |
3839 | | |
3840 | | /* Fill the end part of the filter pipeline */ |
3841 | 32.9k | if ((doshuffle == BLOSC_SHUFFLE) && (typesize > 1)) |
3842 | 0 | filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_SHUFFLE; |
3843 | 32.9k | if (doshuffle == BLOSC_BITSHUFFLE) |
3844 | 5.47k | filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_BITSHUFFLE; |
3845 | 32.9k | if (doshuffle == BLOSC_NOSHUFFLE) |
3846 | 6.87k | filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_NOSHUFFLE; |
3847 | 32.9k | if (delta) |
3848 | 0 | filters[BLOSC2_MAX_FILTERS - 2] = BLOSC_DELTA; |
3849 | 32.9k | } |
3850 | | |
3851 | | /* The public secure routine for compression. */ |
3852 | | int blosc2_compress(int clevel, int doshuffle, int32_t typesize, |
3853 | 18.0k | const void* src, int32_t srcsize, void* dest, int32_t destsize) { |
3854 | 18.0k | int error; |
3855 | 18.0k | int result; |
3856 | 18.0k | char* envvar; |
3857 | | |
3858 | | /* Check whether the library should be initialized */ |
3859 | 18.0k | if (!g_initlib) blosc2_init(); |
3860 | | |
3861 | | /* Check for a BLOSC_CLEVEL environment variable */ |
3862 | 18.0k | envvar = getenv("BLOSC_CLEVEL"); |
3863 | 18.0k | if (envvar != NULL) { |
3864 | 0 | long value; |
3865 | 0 | errno = 0; /* To distinguish success/failure after call */ |
3866 | 0 | value = strtol(envvar, NULL, 10); |
3867 | 0 | if ((errno != EINVAL) && (value >= 0)) { |
3868 | 0 | clevel = (int)value; |
3869 | 0 | } |
3870 | 0 | else { |
3871 | 0 | BLOSC_TRACE_WARNING("BLOSC_CLEVEL environment variable '%s' not recognized\n", envvar); |
3872 | 0 | } |
3873 | 0 | } |
3874 | | |
3875 | | /* Check for a BLOSC_SHUFFLE environment variable */ |
3876 | 18.0k | envvar = getenv("BLOSC_SHUFFLE"); |
3877 | 18.0k | if (envvar != NULL) { |
3878 | 0 | if (strcmp(envvar, "NOSHUFFLE") == 0) { |
3879 | 0 | doshuffle = BLOSC_NOSHUFFLE; |
3880 | 0 | } |
3881 | 0 | else if (strcmp(envvar, "SHUFFLE") == 0) { |
3882 | 0 | doshuffle = BLOSC_SHUFFLE; |
3883 | 0 | } |
3884 | 0 | else if (strcmp(envvar, "BITSHUFFLE") == 0) { |
3885 | 0 | doshuffle = BLOSC_BITSHUFFLE; |
3886 | 0 | } |
3887 | 0 | else { |
3888 | 0 | BLOSC_TRACE_WARNING("BLOSC_SHUFFLE environment variable '%s' not recognized\n", envvar); |
3889 | 0 | } |
3890 | 0 | } |
3891 | | |
3892 | | /* Check for a BLOSC_DELTA environment variable */ |
3893 | 18.0k | envvar = getenv("BLOSC_DELTA"); |
3894 | 18.0k | if (envvar != NULL) { |
3895 | 0 | if (strcmp(envvar, "1") == 0) { |
3896 | 0 | blosc2_set_delta(1); |
3897 | 0 | } else if (strcmp(envvar, "0") == 0) { |
3898 | 0 | blosc2_set_delta(0); |
3899 | 0 | } |
3900 | 0 | else { |
3901 | 0 | BLOSC_TRACE_WARNING("BLOSC_DELTA environment variable '%s' not recognized\n", envvar); |
3902 | 0 | } |
3903 | 0 | } |
3904 | | |
3905 | | /* Check for a BLOSC_TYPESIZE environment variable */ |
3906 | 18.0k | envvar = getenv("BLOSC_TYPESIZE"); |
3907 | 18.0k | if (envvar != NULL) { |
3908 | 0 | long value; |
3909 | 0 | errno = 0; /* To distinguish success/failure after call */ |
3910 | 0 | value = strtol(envvar, NULL, 10); |
3911 | 0 | if ((errno != EINVAL) && (value > 0)) { |
3912 | 0 | typesize = (int32_t)value; |
3913 | 0 | } |
3914 | 0 | else { |
3915 | 0 | BLOSC_TRACE_WARNING("BLOSC_TYPESIZE environment variable '%s' not recognized\n", envvar); |
3916 | 0 | } |
3917 | 0 | } |
3918 | | |
3919 | | /* Check for a BLOSC_COMPRESSOR environment variable */ |
3920 | 18.0k | envvar = getenv("BLOSC_COMPRESSOR"); |
3921 | 18.0k | if (envvar != NULL) { |
3922 | 0 | result = blosc1_set_compressor(envvar); |
3923 | 0 | if (result < 0) { |
3924 | 0 | BLOSC_TRACE_WARNING("BLOSC_COMPRESSOR environment variable '%s' not recognized\n", envvar); |
3925 | 0 | } |
3926 | 0 | } |
3927 | | |
3928 | | /* Check for a BLOSC_BLOCKSIZE environment variable */ |
3929 | 18.0k | envvar = getenv("BLOSC_BLOCKSIZE"); |
3930 | 18.0k | if (envvar != NULL) { |
3931 | 0 | long blocksize; |
3932 | 0 | errno = 0; /* To distinguish success/failure after call */ |
3933 | 0 | blocksize = strtol(envvar, NULL, 10); |
3934 | 0 | if ((errno != EINVAL) && (blocksize > 0)) { |
3935 | 0 | blosc1_set_blocksize((size_t) blocksize); |
3936 | 0 | } |
3937 | 0 | else { |
3938 | 0 | BLOSC_TRACE_WARNING("BLOSC_BLOCKSIZE environment variable '%s' not recognized\n", envvar); |
3939 | 0 | } |
3940 | 0 | } |
3941 | | |
3942 | | /* Check for a BLOSC_NTHREADS environment variable */ |
3943 | 18.0k | envvar = getenv("BLOSC_NTHREADS"); |
3944 | 18.0k | if (envvar != NULL) { |
3945 | 0 | long nthreads; |
3946 | 0 | errno = 0; /* To distinguish success/failure after call */ |
3947 | 0 | nthreads = strtol(envvar, NULL, 10); |
3948 | 0 | if ((errno != EINVAL) && (nthreads > 0)) { |
3949 | 0 | result = blosc2_set_nthreads((int16_t) nthreads); |
3950 | 0 | if (result < 0) { |
3951 | 0 | BLOSC_TRACE_WARNING("BLOSC_NTHREADS environment variable '%s' not recognized\n", envvar); |
3952 | 0 | } |
3953 | 0 | } |
3954 | 0 | } |
3955 | | |
3956 | | /* Check for a BLOSC_SPLITMODE environment variable */ |
3957 | 18.0k | envvar = getenv("BLOSC_SPLITMODE"); |
3958 | 18.0k | if (envvar != NULL) { |
3959 | 0 | int32_t splitmode = -1; |
3960 | 0 | if (strcmp(envvar, "ALWAYS") == 0) { |
3961 | 0 | splitmode = BLOSC_ALWAYS_SPLIT; |
3962 | 0 | } |
3963 | 0 | else if (strcmp(envvar, "NEVER") == 0) { |
3964 | 0 | splitmode = BLOSC_NEVER_SPLIT; |
3965 | 0 | } |
3966 | 0 | else if (strcmp(envvar, "AUTO") == 0) { |
3967 | 0 | splitmode = BLOSC_AUTO_SPLIT; |
3968 | 0 | } |
3969 | 0 | else if (strcmp(envvar, "FORWARD_COMPAT") == 0) { |
3970 | 0 | splitmode = BLOSC_FORWARD_COMPAT_SPLIT; |
3971 | 0 | } |
3972 | 0 | else { |
3973 | 0 | BLOSC_TRACE_WARNING("BLOSC_SPLITMODE environment variable '%s' not recognized\n", envvar); |
3974 | 0 | } |
3975 | |
|
3976 | 0 | if (splitmode >= 0) { |
3977 | 0 | blosc1_set_splitmode(splitmode); |
3978 | 0 | } |
3979 | 0 | } |
3980 | | |
3981 | | /* Check for a BLOSC_NOLOCK environment variable. It is important |
3982 | | that this should be the last env var so that it can take the |
3983 | | previous ones into account */ |
3984 | 18.0k | envvar = getenv("BLOSC_NOLOCK"); |
3985 | 18.0k | if (envvar != NULL) { |
3986 | | // TODO: here is the only place that returns an extended header from |
3987 | | // a blosc1_compress() call. This should probably be fixed. |
3988 | 0 | const char *compname; |
3989 | 0 | blosc2_context *cctx; |
3990 | 0 | blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS; |
3991 | |
|
3992 | 0 | blosc2_compcode_to_compname(g_compressor, &compname); |
3993 | | /* Create a context for compression */ |
3994 | 0 | build_filters(doshuffle, g_delta, typesize, cparams.filters); |
3995 | | // TODO: cparams can be shared in a multithreaded environment. do a copy! |
3996 | 0 | cparams.typesize = (uint8_t)typesize; |
3997 | 0 | cparams.compcode = (uint8_t)g_compressor; |
3998 | 0 | cparams.clevel = (uint8_t)clevel; |
3999 | 0 | cparams.nthreads = g_nthreads; |
4000 | 0 | cparams.splitmode = g_splitmode; |
4001 | 0 | cctx = blosc2_create_cctx(cparams); |
4002 | 0 | if (cctx == NULL) { |
4003 | 0 | BLOSC_TRACE_ERROR("Error while creating the compression context"); |
4004 | 0 | return BLOSC2_ERROR_NULL_POINTER; |
4005 | 0 | } |
4006 | 0 | blosc2_ctx_set_blosc1_format(cctx, getenv("BLOSC_BLOSC1_COMPAT") != NULL); |
4007 | | /* Do the actual compression */ |
4008 | 0 | result = blosc2_compress_ctx(cctx, src, srcsize, dest, destsize); |
4009 | | /* Release context resources */ |
4010 | 0 | blosc2_free_ctx(cctx); |
4011 | 0 | return result; |
4012 | 0 | } |
4013 | | |
4014 | 18.0k | blosc2_pthread_mutex_lock(&global_comp_mutex); |
4015 | | |
4016 | | /* Blosc1 format is read before anything depends on it (e.g. block splitting) */ |
4017 | 18.0k | g_global_context->blosc1_format = (getenv("BLOSC_BLOSC1_COMPAT") != NULL); |
4018 | | |
4019 | | /* Initialize a context compression */ |
4020 | 18.0k | uint8_t* filters = calloc(1, BLOSC2_MAX_FILTERS); |
4021 | 18.0k | BLOSC_ERROR_NULL(filters, BLOSC2_ERROR_MEMORY_ALLOC); |
4022 | 18.0k | uint8_t* filters_meta = calloc(1, BLOSC2_MAX_FILTERS); |
4023 | 18.0k | BLOSC_ERROR_NULL(filters_meta, BLOSC2_ERROR_MEMORY_ALLOC); |
4024 | 18.0k | build_filters(doshuffle, g_delta, typesize, filters); |
4025 | 18.0k | error = initialize_context_compression( |
4026 | 18.0k | g_global_context, src, srcsize, dest, destsize, clevel, filters, |
4027 | 18.0k | filters_meta, (int32_t)typesize, g_compressor, g_force_blocksize, g_nthreads, g_nthreads, |
4028 | 18.0k | g_splitmode, g_tuner, NULL, g_schunk); |
4029 | 18.0k | free(filters); |
4030 | 18.0k | free(filters_meta); |
4031 | 18.0k | if (error <= 0) { |
4032 | 31 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
4033 | 31 | return error; |
4034 | 31 | } |
4035 | | |
4036 | 18.0k | error = blosc1_format_check(g_global_context); |
4037 | 18.0k | if (error < 0) { |
4038 | 0 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
4039 | 0 | return error; |
4040 | 0 | } |
4041 | | |
4042 | | /* Blosc1 format: chunk header without extended header */ |
4043 | 18.0k | error = write_compression_header(g_global_context, !g_global_context->blosc1_format); |
4044 | 18.0k | if (error < 0) { |
4045 | 0 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
4046 | 0 | return error; |
4047 | 0 | } |
4048 | | |
4049 | 18.0k | result = blosc_compress_context(g_global_context); |
4050 | | |
4051 | 18.0k | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
4052 | | |
4053 | 18.0k | return result; |
4054 | 18.0k | } |
4055 | | |
4056 | | |
4057 | | /* The public routine for compression. */ |
4058 | | int blosc1_compress(int clevel, int doshuffle, size_t typesize, size_t nbytes, |
4059 | 0 | const void* src, void* dest, size_t destsize) { |
4060 | 0 | return blosc2_compress(clevel, doshuffle, (int32_t)typesize, src, (int32_t)nbytes, dest, (int32_t)destsize); |
4061 | 0 | } |
4062 | | |
4063 | | |
4064 | | |
4065 | | static int blosc_run_decompression_with_context(blosc2_context* context, const void* src, int32_t srcsize, |
4066 | 73.9k | void* dest, int32_t destsize) { |
4067 | 73.9k | blosc_header header; |
4068 | 73.9k | int32_t ntbytes; |
4069 | 73.9k | int rc; |
4070 | | |
4071 | 73.9k | rc = read_chunk_header(src, srcsize, true, &header); |
4072 | 73.9k | if (rc < 0) { |
4073 | 69 | return rc; |
4074 | 69 | } |
4075 | | |
4076 | 73.8k | if (header.nbytes > destsize) { |
4077 | | // Not enough space for writing into the destination |
4078 | 9.18k | return BLOSC2_ERROR_WRITE_BUFFER; |
4079 | 9.18k | } |
4080 | | |
4081 | 64.7k | rc = initialize_context_decompression(context, &header, src, srcsize, dest, destsize); |
4082 | 64.7k | if (rc < 0) { |
4083 | 1.45k | return rc; |
4084 | 1.45k | } |
4085 | | |
4086 | | /* Do the actual decompression */ |
4087 | 63.2k | ntbytes = do_job(context); |
4088 | 63.2k | if (ntbytes < 0) { |
4089 | 22.6k | return ntbytes; |
4090 | 22.6k | } |
4091 | | |
4092 | 63.2k | assert(ntbytes <= (int32_t)destsize); |
4093 | 40.5k | return ntbytes; |
4094 | 63.2k | } |
4095 | | |
4096 | | |
4097 | | /* The public secure routine for decompression with context. */ |
4098 | | int blosc2_decompress_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
4099 | 46.0k | void* dest, int32_t destsize) { |
4100 | 46.0k | int result; |
4101 | | |
4102 | 46.0k | if (context->do_compress != 0) { |
4103 | 0 | BLOSC_TRACE_ERROR("Context is not meant for decompression. Giving up."); |
4104 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4105 | 0 | } |
4106 | | |
4107 | 46.0k | result = blosc_run_decompression_with_context(context, src, srcsize, dest, destsize); |
4108 | | |
4109 | | // Reset a possible block_maskout |
4110 | 46.0k | if (context->block_maskout != NULL) { |
4111 | 0 | free(context->block_maskout); |
4112 | 0 | context->block_maskout = NULL; |
4113 | 0 | } |
4114 | 46.0k | context->block_maskout_nitems = 0; |
4115 | | |
4116 | 46.0k | return result; |
4117 | 46.0k | } |
4118 | | |
4119 | | int blosc2_vldecompress_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
4120 | 0 | void** dests, int32_t* destsizes, int32_t maxblocks) { |
4121 | 0 | int result; |
4122 | 0 | blosc_header header; |
4123 | |
|
4124 | 0 | if (context->do_compress != 0) { |
4125 | 0 | BLOSC_TRACE_ERROR("Context is not meant for decompression. Giving up."); |
4126 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4127 | 0 | } |
4128 | 0 | if (dests == NULL || destsizes == NULL || maxblocks <= 0) { |
4129 | 0 | BLOSC_TRACE_ERROR("Invalid destinations for VL-block decompression."); |
4130 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4131 | 0 | } |
4132 | | |
4133 | 0 | result = read_chunk_header(src, srcsize, true, &header); |
4134 | 0 | if (result < 0) { |
4135 | 0 | return result; |
4136 | 0 | } |
4137 | 0 | if ((header.blosc2_flags2 & BLOSC2_VL_BLOCKS) == 0) { |
4138 | 0 | BLOSC_TRACE_ERROR("Chunk does not use VL blocks."); |
4139 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4140 | 0 | } |
4141 | | |
4142 | 0 | result = initialize_context_decompression(context, &header, src, srcsize, NULL, header.nbytes); |
4143 | 0 | if (result < 0) { |
4144 | 0 | return result; |
4145 | 0 | } |
4146 | 0 | if (context->nblocks > maxblocks) { |
4147 | 0 | BLOSC_TRACE_ERROR("Not enough output entries for VL-block decompression."); |
4148 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4149 | 0 | } |
4150 | | |
4151 | 0 | for (int32_t i = 0; i < context->nblocks; ++i) { |
4152 | 0 | destsizes[i] = context->blocknbytes[i]; |
4153 | 0 | dests[i] = malloc((size_t)destsizes[i]); |
4154 | 0 | BLOSC_ERROR_NULL(dests[i], BLOSC2_ERROR_MEMORY_ALLOC); |
4155 | 0 | } |
4156 | | |
4157 | 0 | context->vlblock_dests = (uint8_t**)dests; |
4158 | 0 | result = do_job(context); |
4159 | 0 | context->vlblock_dests = NULL; |
4160 | 0 | if (result < 0) { |
4161 | 0 | for (int32_t i = 0; i < context->nblocks; ++i) { |
4162 | 0 | free(dests[i]); |
4163 | 0 | dests[i] = NULL; |
4164 | 0 | } |
4165 | 0 | return result; |
4166 | 0 | } |
4167 | | |
4168 | 0 | return context->nblocks; |
4169 | 0 | } |
4170 | | |
4171 | | |
4172 | | /* Decompress a single VL block from an already-initialised decompression |
4173 | | * context. The context must have been prepared by initialize_context_decompression() |
4174 | | * on a VL-block chunk so that blocknbytes[], blockoffsets[], blockcbytes[], and |
4175 | | * bstarts[] are all valid. |
4176 | | * |
4177 | | * On success, *dest points to a newly allocated buffer of *destsize bytes that |
4178 | | * the caller must free(). Returns *destsize, or a negative error code. */ |
4179 | | static int decompress_single_vlblock(blosc2_context* context, int32_t nblock, |
4180 | 0 | uint8_t** dest, int32_t* destsize) { |
4181 | 0 | if (nblock < 0 || nblock >= context->nblocks) { |
4182 | 0 | BLOSC_TRACE_ERROR("nblock (%d) out of range [0, %d).", nblock, context->nblocks); |
4183 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4184 | 0 | } |
4185 | | |
4186 | | // For lazy VL chunks blocknbytes[nblock] == 0 because the uncompressed size is |
4187 | | // stored as the first 4 bytes of the block span on disk, not in the in-memory |
4188 | | // header. Peek at the file to resolve it before we can allocate the output buffer. |
4189 | 0 | if (context->blocknbytes[nblock] == 0) { |
4190 | 0 | if (context->schunk == NULL || context->schunk->frame == NULL) { |
4191 | 0 | BLOSC_TRACE_ERROR("Lazy VL block needs an associated super-chunk with a frame."); |
4192 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4193 | 0 | } |
4194 | 0 | blosc2_frame_s* frame = (blosc2_frame_s*)context->schunk->frame; |
4195 | 0 | blosc2_io_cb *io_cb = blosc2_get_io_cb(context->schunk->storage->io->id); |
4196 | 0 | if (io_cb == NULL) { |
4197 | 0 | BLOSC_TRACE_ERROR("Error getting the input/output API"); |
4198 | 0 | return BLOSC2_ERROR_PLUGIN_IO; |
4199 | 0 | } |
4200 | | |
4201 | | // Lazy chunk trailer: [nchunk int32 | chunk_offset int64 | block_csizes int32*N] |
4202 | 0 | int64_t trailer_offset = (int64_t)BLOSC_EXTENDED_HEADER_LENGTH + |
4203 | 0 | (int64_t)context->nblocks * (int64_t)sizeof(int32_t); |
4204 | 0 | int64_t trailer_min_end = trailer_offset + (int64_t)sizeof(int32_t) + (int64_t)sizeof(int64_t); |
4205 | 0 | int64_t block_csize_pos = trailer_min_end + ((int64_t)nblock * (int64_t)sizeof(int32_t)); |
4206 | 0 | if (trailer_offset < 0 || block_csize_pos < 0 || |
4207 | 0 | (block_csize_pos + (int64_t)sizeof(int32_t)) > context->srcsize) { |
4208 | 0 | BLOSC_TRACE_ERROR("Malformed lazy trailer exceeds chunk bounds."); |
4209 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4210 | 0 | } |
4211 | 0 | int32_t nchunk_lazy; |
4212 | 0 | int64_t chunk_offset; |
4213 | 0 | memcpy(&nchunk_lazy, context->src + trailer_offset, sizeof(nchunk_lazy)); |
4214 | 0 | memcpy(&chunk_offset, context->src + trailer_offset + (int32_t)sizeof(int32_t), sizeof(chunk_offset)); |
4215 | 0 | int32_t block_csize = *(const int32_t*)(context->src + block_csize_pos); |
4216 | 0 | if (block_csize < (int32_t)sizeof(int32_t)) { |
4217 | 0 | BLOSC_TRACE_ERROR("Lazy VL block compressed size is too small."); |
4218 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4219 | 0 | } |
4220 | 0 | int32_t bstart = sw32_(context->bstarts + nblock); |
4221 | 0 | if (bstart < 0) { |
4222 | 0 | BLOSC_TRACE_ERROR("Lazy VL block offset cannot be negative."); |
4223 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4224 | 0 | } |
4225 | | |
4226 | 0 | void* fp = NULL; |
4227 | 0 | int64_t io_pos; |
4228 | 0 | if (frame->sframe) { |
4229 | 0 | fp = sframe_open_chunk(frame->urlpath, nchunk_lazy, "rb", context->schunk->storage->io); |
4230 | 0 | io_pos = bstart; |
4231 | 0 | } |
4232 | 0 | else { |
4233 | 0 | fp = frame_reader_acquire(frame, context->schunk->storage->io); |
4234 | 0 | if (fp == NULL) { |
4235 | 0 | BLOSC_TRACE_ERROR("Cannot open frame file for lazy VL block size peek."); |
4236 | 0 | return BLOSC2_ERROR_FILE_OPEN; |
4237 | 0 | } |
4238 | 0 | if (chunk_offset < 0) { |
4239 | 0 | BLOSC_TRACE_ERROR("Lazy chunk offset cannot be negative."); |
4240 | 0 | frame_reader_release(frame, io_cb, fp); |
4241 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4242 | 0 | } |
4243 | 0 | if (!blosc2_checked_add_i64(frame->file_offset, chunk_offset, &io_pos)) { |
4244 | 0 | BLOSC_TRACE_ERROR("Lazy chunk offset overflows file position."); |
4245 | 0 | frame_reader_release(frame, io_cb, fp); |
4246 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4247 | 0 | } |
4248 | 0 | if (!blosc2_checked_add_i64(io_pos, bstart, &io_pos)) { |
4249 | 0 | BLOSC_TRACE_ERROR("Lazy block offset overflows file position."); |
4250 | 0 | frame_reader_release(frame, io_cb, fp); |
4251 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4252 | 0 | } |
4253 | 0 | } |
4254 | 0 | if (fp == NULL) { |
4255 | 0 | BLOSC_TRACE_ERROR("Cannot open frame file for lazy VL block size peek."); |
4256 | 0 | return BLOSC2_ERROR_FILE_OPEN; |
4257 | 0 | } |
4258 | | |
4259 | | // Read only the 4-byte uncompressed-size prefix of the block span. |
4260 | 0 | uint8_t nbuf[sizeof(int32_t)]; |
4261 | 0 | uint8_t* nbufp = nbuf; |
4262 | 0 | int64_t rbytes = io_cb->read((void**)&nbufp, 1, sizeof(int32_t), io_pos, fp); |
4263 | 0 | frame_reader_release(frame, io_cb, fp); |
4264 | 0 | if (nbufp != nbuf) { |
4265 | | // io_cb allocated new memory; copy the result and free. |
4266 | 0 | memcpy(nbuf, nbufp, sizeof(int32_t)); |
4267 | 0 | free(nbufp); |
4268 | 0 | } |
4269 | 0 | if (rbytes != (int64_t)sizeof(int32_t)) { |
4270 | 0 | BLOSC_TRACE_ERROR("Cannot read VL-block uncompressed-size prefix from disk."); |
4271 | 0 | return BLOSC2_ERROR_FILE_READ; |
4272 | 0 | } |
4273 | 0 | int32_t neblock = sw32_(nbuf); |
4274 | 0 | if (neblock <= 0) { |
4275 | 0 | BLOSC_TRACE_ERROR("Invalid VL-block uncompressed size read from disk."); |
4276 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4277 | 0 | } |
4278 | 0 | context->blocknbytes[nblock] = neblock; |
4279 | | // Keep blocksize as an upper bound for tmp buffer allocation. |
4280 | 0 | if (neblock > context->blocksize) { |
4281 | 0 | context->blocksize = neblock; |
4282 | 0 | } |
4283 | 0 | } |
4284 | | |
4285 | 0 | int32_t bsize = context->blocknbytes[nblock]; |
4286 | 0 | uint8_t* buf = malloc((size_t)bsize); |
4287 | 0 | if (buf == NULL) { |
4288 | 0 | return BLOSC2_ERROR_MEMORY_ALLOC; |
4289 | 0 | } |
4290 | | |
4291 | | /* Ensure we have a serial thread context sized for this blocksize. */ |
4292 | 0 | if (context->serial_context == NULL) { |
4293 | 0 | context->serial_context = create_thread_context(context, 0); |
4294 | 0 | } |
4295 | 0 | else if (context->blocksize != context->serial_context->tmp_blocksize) { |
4296 | 0 | free_thread_context(context->serial_context); |
4297 | 0 | context->serial_context = create_thread_context(context, 0); |
4298 | 0 | } |
4299 | 0 | if (context->serial_context == NULL) { |
4300 | 0 | free(buf); |
4301 | 0 | return BLOSC2_ERROR_THREAD_CREATE; |
4302 | 0 | } |
4303 | | |
4304 | 0 | bool memcpyed = (context->header_flags & (uint8_t)BLOSC_MEMCPYED) != 0; |
4305 | 0 | int32_t src_offset = sw32_(context->bstarts + nblock); |
4306 | 0 | int cbytes = blosc_d(context->serial_context, bsize, 0, memcpyed, |
4307 | 0 | context->src, context->srcsize, src_offset, nblock, |
4308 | 0 | buf, 0, |
4309 | 0 | context->serial_context->tmp, context->serial_context->tmp2); |
4310 | 0 | if (cbytes < 0) { |
4311 | 0 | free(buf); |
4312 | 0 | return cbytes; |
4313 | 0 | } |
4314 | | |
4315 | 0 | *dest = buf; |
4316 | 0 | *destsize = bsize; |
4317 | 0 | return bsize; |
4318 | 0 | } |
4319 | | |
4320 | | |
4321 | | /* Admission preflight: no payload, dictionary, or thread-buffer allocation. |
4322 | | * Lazy sources read just the selected size prefix through the owning frame. */ |
4323 | | int blosc2_vlblock_get_nbytes_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
4324 | 0 | int32_t nblock, int32_t* nbytes) { |
4325 | 0 | if (context == NULL || context->do_compress != 0 || src == NULL || nbytes == NULL) |
4326 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4327 | 0 | blosc_header header; |
4328 | 0 | int rc = read_chunk_header(src, srcsize, true, &header); |
4329 | 0 | if (rc < 0) return rc; |
4330 | 0 | if (!(header.blosc2_flags2 & BLOSC2_VL_BLOCKS) || |
4331 | 0 | (header.flags & BLOSC_MEMCPYED) || (header.blosc2_flags >> 4) != 0) |
4332 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4333 | 0 | int32_t count = header.blocksize; |
4334 | 0 | if (nblock < 0 || nblock >= count) return BLOSC2_ERROR_INVALID_PARAM; |
4335 | 0 | int64_t table_end = BLOSC_EXTENDED_HEADER_LENGTH + (int64_t)count * sizeof(int32_t); |
4336 | 0 | bool lazy = header.blosc2_flags & 0x08u; |
4337 | 0 | int64_t required = table_end + (lazy ? sizeof(int32_t) + sizeof(int64_t) + |
4338 | 0 | (int64_t)count * sizeof(int32_t) : 0); |
4339 | 0 | if (required > srcsize || header.nbytes <= 0 || (!lazy && header.cbytes > srcsize)) |
4340 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
4341 | 0 | const uint8_t* bytes = src; |
4342 | 0 | int32_t start = sw32_(bytes + BLOSC_EXTENDED_HEADER_LENGTH + (int64_t)nblock * 4); |
4343 | 0 | int32_t end = nblock + 1 < count ? |
4344 | 0 | sw32_(bytes + BLOSC_EXTENDED_HEADER_LENGTH + (int64_t)(nblock + 1) * 4) : header.cbytes; |
4345 | 0 | if (start < table_end || end > header.cbytes || end <= start || end - start <= 4) |
4346 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4347 | 0 | uint8_t prefix[4]; |
4348 | 0 | if (lazy) { |
4349 | 0 | int32_t span; |
4350 | 0 | memcpy(&span, bytes + table_end + sizeof(int32_t) + sizeof(int64_t) + (int64_t)nblock * 4, 4); |
4351 | 0 | if (span != end - start) return BLOSC2_ERROR_INVALID_HEADER; |
4352 | 0 | blosc2_context probe = {0}; |
4353 | 0 | probe.schunk = context->schunk; |
4354 | 0 | probe.src = src; |
4355 | 0 | probe.srcsize = srcsize; |
4356 | 0 | probe.nblocks = count; |
4357 | 0 | rc = read_lazy_chunk_bytes(&probe, start, prefix, 4, |
4358 | 0 | "Cannot open frame for VL size query.", "Cannot read VL size prefix."); |
4359 | 0 | if (rc < 0) return rc; |
4360 | 0 | } |
4361 | 0 | else { |
4362 | 0 | memcpy(prefix, bytes + start, 4); |
4363 | 0 | } |
4364 | 0 | int32_t size = sw32_(prefix); |
4365 | 0 | if (size <= 0 || size > header.nbytes || size > BLOSC2_MAXBLOCKSIZE) |
4366 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4367 | 0 | *nbytes = size; |
4368 | 0 | return 0; |
4369 | 0 | } |
4370 | | |
4371 | | int blosc2_vldecompress_block_into_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
4372 | 0 | int32_t nblock, void* dest, int32_t destsize) { |
4373 | 0 | if (dest == NULL || destsize < 0) return BLOSC2_ERROR_INVALID_PARAM; |
4374 | 0 | int32_t size; |
4375 | 0 | int rc = blosc2_vlblock_get_nbytes_ctx(context, src, srcsize, nblock, &size); |
4376 | 0 | if (rc < 0) return rc; |
4377 | 0 | if (size > destsize) return BLOSC2_ERROR_WRITE_BUFFER; |
4378 | 0 | blosc_header header; |
4379 | 0 | rc = read_chunk_header(src, srcsize, true, &header); |
4380 | 0 | if (rc < 0) return rc; |
4381 | 0 | rc = initialize_context_decompression(context, &header, src, srcsize, NULL, header.nbytes); |
4382 | 0 | if (rc < 0) return rc; |
4383 | 0 | if (context->use_dict && sw32_(context->bstarts + nblock) < |
4384 | 0 | BLOSC_EXTENDED_HEADER_LENGTH + (int64_t)context->nblocks * 4 + 4 + context->dict_size) |
4385 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
4386 | | /* Never size scratch from a different block (or max compressed lazy span). */ |
4387 | 0 | context->blocksize = size; |
4388 | 0 | context->blocknbytes[nblock] = size; |
4389 | 0 | if (context->serial_context != NULL && context->serial_context->tmp_blocksize != size) { |
4390 | 0 | free_thread_context(context->serial_context); |
4391 | 0 | context->serial_context = NULL; |
4392 | 0 | } |
4393 | 0 | if (context->serial_context == NULL) context->serial_context = create_thread_context(context, 0); |
4394 | 0 | if (context->serial_context == NULL) return BLOSC2_ERROR_MEMORY_ALLOC; |
4395 | 0 | rc = blosc_d(context->serial_context, size, 0, false, context->src, context->srcsize, |
4396 | 0 | sw32_(context->bstarts + nblock), nblock, dest, 0, |
4397 | 0 | context->serial_context->tmp, context->serial_context->tmp2); |
4398 | 0 | if (rc < 0) return rc; |
4399 | 0 | return rc == size ? rc : BLOSC2_ERROR_DATA; |
4400 | 0 | } |
4401 | | |
4402 | | int blosc2_vldecompress_block_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
4403 | 0 | int32_t nblock, uint8_t** dest, int32_t* destsize) { |
4404 | 0 | if (context->do_compress != 0) { |
4405 | 0 | BLOSC_TRACE_ERROR("Context is not meant for decompression. Giving up."); |
4406 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4407 | 0 | } |
4408 | 0 | if (dest == NULL || destsize == NULL) { |
4409 | 0 | BLOSC_TRACE_ERROR("dest and destsize must not be NULL."); |
4410 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4411 | 0 | } |
4412 | | |
4413 | 0 | blosc_header header; |
4414 | 0 | int result = read_chunk_header((const uint8_t*)src, srcsize, true, &header); |
4415 | 0 | if (result < 0) { |
4416 | 0 | return result; |
4417 | 0 | } |
4418 | 0 | if ((header.blosc2_flags2 & BLOSC2_VL_BLOCKS) == 0) { |
4419 | 0 | BLOSC_TRACE_ERROR("Chunk does not use VL blocks."); |
4420 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4421 | 0 | } |
4422 | | |
4423 | | /* Pass header.nbytes as destsize so the size-check inside |
4424 | | * initialize_context_decompression passes; context->dest is set to NULL and |
4425 | | * is never written by blosc_d (which uses its own dest argument). */ |
4426 | 0 | result = initialize_context_decompression(context, &header, src, srcsize, NULL, header.nbytes); |
4427 | 0 | if (result < 0) { |
4428 | 0 | return result; |
4429 | 0 | } |
4430 | | |
4431 | 0 | return decompress_single_vlblock(context, nblock, dest, destsize); |
4432 | 0 | } |
4433 | | |
4434 | | |
4435 | | /* The public secure routine for decompression. */ |
4436 | 27.8k | int blosc2_decompress(const void* src, int32_t srcsize, void* dest, int32_t destsize) { |
4437 | 27.8k | int result; |
4438 | 27.8k | char* envvar; |
4439 | 27.8k | long nthreads; |
4440 | 27.8k | blosc2_context *dctx; |
4441 | 27.8k | blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS; |
4442 | | |
4443 | | /* Check whether the library should be initialized */ |
4444 | 27.8k | if (!g_initlib) blosc2_init(); |
4445 | | |
4446 | | /* Check for a BLOSC_NTHREADS environment variable */ |
4447 | 27.8k | envvar = getenv("BLOSC_NTHREADS"); |
4448 | 27.8k | if (envvar != NULL) { |
4449 | 0 | errno = 0; /* To distinguish success/failure after call */ |
4450 | 0 | nthreads = strtol(envvar, NULL, 10); |
4451 | 0 | if ((errno != EINVAL)) { |
4452 | 0 | if ((nthreads <= 0) || (nthreads > INT16_MAX)) { |
4453 | 0 | BLOSC_TRACE_ERROR("nthreads must be >= 1 and <= %d", INT16_MAX); |
4454 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4455 | 0 | } |
4456 | 0 | result = blosc2_set_nthreads((int16_t) nthreads); |
4457 | 0 | if (result < 0) { |
4458 | 0 | return result; |
4459 | 0 | } |
4460 | 0 | } |
4461 | 0 | } |
4462 | | |
4463 | | /* Check for a BLOSC_NOLOCK environment variable. It is important |
4464 | | that this should be the last env var so that it can take the |
4465 | | previous ones into account */ |
4466 | 27.8k | envvar = getenv("BLOSC_NOLOCK"); |
4467 | 27.8k | if (envvar != NULL) { |
4468 | 0 | dparams.nthreads = g_nthreads; |
4469 | 0 | dctx = blosc2_create_dctx(dparams); |
4470 | 0 | if (dctx == NULL) { |
4471 | 0 | BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
4472 | 0 | return BLOSC2_ERROR_NULL_POINTER; |
4473 | 0 | } |
4474 | 0 | result = blosc2_decompress_ctx(dctx, src, srcsize, dest, destsize); |
4475 | 0 | blosc2_free_ctx(dctx); |
4476 | 0 | return result; |
4477 | 0 | } |
4478 | | |
4479 | 27.8k | blosc2_pthread_mutex_lock(&global_comp_mutex); |
4480 | | |
4481 | 27.8k | result = blosc_run_decompression_with_context( |
4482 | 27.8k | g_global_context, src, srcsize, dest, destsize); |
4483 | | |
4484 | 27.8k | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
4485 | | |
4486 | 27.8k | return result; |
4487 | 27.8k | } |
4488 | | |
4489 | | |
4490 | | /* The public routine for decompression. */ |
4491 | 11.1k | int blosc1_decompress(const void* src, void* dest, size_t destsize) { |
4492 | 11.1k | return blosc2_decompress(src, INT32_MAX, dest, (int32_t)destsize); |
4493 | 11.1k | } |
4494 | | |
4495 | | |
4496 | | /* Specific routine optimized for decompression a small number of |
4497 | | items out of a compressed chunk. This does not use threads because |
4498 | | it would affect negatively to performance. */ |
4499 | | int _blosc_getitem(blosc2_context* context, blosc_header* header, const void* src, int32_t srcsize, |
4500 | 16.3k | int start, int nitems, void* dest, int32_t destsize) { |
4501 | 16.3k | uint8_t* _src = (uint8_t*)(src); /* current pos for source buffer */ |
4502 | 16.3k | uint8_t* _dest = (uint8_t*)(dest); |
4503 | 16.3k | int32_t ntbytes = 0; /* the number of uncompressed bytes */ |
4504 | 16.3k | int32_t bsize, bsize2, ebsize, leftoverblock; |
4505 | 16.3k | int32_t startb, stopb; |
4506 | 16.3k | int32_t stop; |
4507 | 16.3k | int32_t nitems_bytes; |
4508 | 16.3k | int64_t start64 = (int64_t)start; |
4509 | 16.3k | int64_t nitems64 = (int64_t)nitems; |
4510 | 16.3k | int64_t typesize64 = (int64_t)header->typesize; |
4511 | 16.3k | int64_t stop64; |
4512 | 16.3k | int64_t nitems_bytes64; |
4513 | 16.3k | int64_t start_bytes64; |
4514 | 16.3k | int64_t stop_bytes64; |
4515 | 16.3k | int j, rc; |
4516 | | |
4517 | 16.3k | if (nitems == 0) { |
4518 | | // We have nothing to do |
4519 | 0 | return 0; |
4520 | 0 | } |
4521 | 16.3k | if (nitems < 0) { |
4522 | 0 | BLOSC_TRACE_ERROR("`nitems` out of bounds."); |
4523 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4524 | 0 | } |
4525 | | |
4526 | 16.3k | nitems_bytes64 = nitems64 * typesize64; |
4527 | 16.3k | if (nitems_bytes64 < 0 || nitems_bytes64 > INT32_MAX || nitems_bytes64 > destsize) { |
4528 | 16 | BLOSC_TRACE_ERROR("`nitems`*`typesize` out of dest bounds."); |
4529 | 16 | return BLOSC2_ERROR_WRITE_BUFFER; |
4530 | 16 | } |
4531 | 16.3k | nitems_bytes = (int32_t)nitems_bytes64; |
4532 | | |
4533 | 16.3k | int32_t* bstarts = (int32_t*)(_src + context->header_overhead); |
4534 | | |
4535 | | /* Check region boundaries */ |
4536 | 16.3k | start_bytes64 = start64 * typesize64; |
4537 | 16.3k | if ((start < 0) || (start_bytes64 < 0) || (start_bytes64 > header->nbytes)) { |
4538 | 0 | BLOSC_TRACE_ERROR("`start` out of bounds."); |
4539 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4540 | 0 | } |
4541 | | |
4542 | 16.3k | stop64 = start64 + nitems64; |
4543 | 16.3k | if ((stop64 < 0) || (stop64 > INT32_MAX)) { |
4544 | 0 | BLOSC_TRACE_ERROR("`start`+`nitems` out of bounds."); |
4545 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4546 | 0 | } |
4547 | 16.3k | stop = (int32_t)stop64; |
4548 | | |
4549 | 16.3k | stop_bytes64 = stop64 * typesize64; |
4550 | 16.3k | if ((stop_bytes64 < 0) || (stop_bytes64 > header->nbytes)) { |
4551 | 0 | BLOSC_TRACE_ERROR("`start`+`nitems` out of bounds."); |
4552 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4553 | 0 | } |
4554 | | |
4555 | 16.3k | bool chunk_memcpyed = (header->flags & (uint8_t)BLOSC_MEMCPYED) != 0; |
4556 | 16.3k | if (!context->special_type && !chunk_memcpyed && |
4557 | 14.3k | ((uint8_t *)(_src + srcsize) < (uint8_t *)(bstarts + context->nblocks))) { |
4558 | 0 | BLOSC_TRACE_ERROR("`bstarts` out of bounds."); |
4559 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
4560 | 0 | } |
4561 | | |
4562 | 16.3k | bool memcpyed = header->flags & (uint8_t)BLOSC_MEMCPYED; |
4563 | 16.3k | if (context->special_type) { |
4564 | | // Fake a runlen as if its a memcpyed chunk |
4565 | 636 | memcpyed = true; |
4566 | 636 | } |
4567 | | |
4568 | 16.3k | bool is_lazy = ((context->header_overhead == BLOSC_EXTENDED_HEADER_LENGTH) && |
4569 | 16.0k | (context->blosc2_flags & 0x08u) && !context->special_type); |
4570 | 16.3k | if (memcpyed && !is_lazy && !context->postfilter) { |
4571 | | // Short-circuit for (non-lazy) memcpyed or special values |
4572 | 2.00k | ntbytes = nitems_bytes; |
4573 | 2.00k | switch (context->special_type) { |
4574 | 0 | case BLOSC2_SPECIAL_VALUE: { |
4575 | | // The repeated value's width is cbytes minus the header, not the header |
4576 | | // typesize byte (which is left unconstrained for special-value chunks). |
4577 | | // Derive it the same way blosc_d() does so a crafted typesize cannot make |
4578 | | // set_values() read past the end of the chunk. |
4579 | 0 | int32_t value_typesize = header->cbytes - context->header_overhead; |
4580 | 0 | rc = set_values(value_typesize, _src, _dest, ntbytes); |
4581 | 0 | if (rc < 0) { |
4582 | 0 | BLOSC_TRACE_ERROR("set_values failed"); |
4583 | 0 | return BLOSC2_ERROR_DATA; |
4584 | 0 | } |
4585 | 0 | break; |
4586 | 0 | } |
4587 | 1 | case BLOSC2_SPECIAL_NAN: |
4588 | 1 | rc = set_nans(context->typesize, _dest, ntbytes); |
4589 | 1 | if (rc < 0) { |
4590 | 0 | BLOSC_TRACE_ERROR("set_nans failed"); |
4591 | 0 | return BLOSC2_ERROR_DATA; |
4592 | 0 | } |
4593 | 1 | break; |
4594 | 633 | case BLOSC2_SPECIAL_ZERO: |
4595 | 633 | memset(_dest, 0, ntbytes); |
4596 | 633 | break; |
4597 | 2 | case BLOSC2_SPECIAL_UNINIT: |
4598 | | // We do nothing here |
4599 | 2 | break; |
4600 | 1.36k | case BLOSC2_NO_SPECIAL: |
4601 | 1.36k | { |
4602 | 1.36k | int64_t src_offset64 = (int64_t)context->header_overhead + start_bytes64; |
4603 | 1.36k | int64_t src_stop64 = src_offset64 + nitems_bytes64; |
4604 | 1.36k | if ((src_offset64 < 0) || (src_stop64 < src_offset64) || (src_stop64 > srcsize)) { |
4605 | 0 | BLOSC_TRACE_ERROR("getitem memcpy source out of bounds."); |
4606 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
4607 | 0 | } |
4608 | 1.36k | _src += (int32_t)src_offset64; |
4609 | 1.36k | } |
4610 | 0 | memcpy(_dest, _src, ntbytes); |
4611 | 1.36k | break; |
4612 | 0 | default: |
4613 | 0 | BLOSC_TRACE_ERROR("Unhandled special value case"); |
4614 | 0 | BLOSC_ERROR(BLOSC2_ERROR_SCHUNK_SPECIAL); |
4615 | 2.00k | } |
4616 | 2.00k | return ntbytes; |
4617 | 2.00k | } |
4618 | | |
4619 | 16.3k | bool has_delta = false; |
4620 | 79.1k | for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) { |
4621 | 72.1k | if (context->filters[i] == BLOSC_DELTA) { |
4622 | 7.39k | has_delta = true; |
4623 | 7.39k | break; |
4624 | 7.39k | } |
4625 | 72.1k | } |
4626 | 14.3k | if (has_delta) { |
4627 | 7.39k | uint8_t* full_chunk = malloc((size_t)header->nbytes); |
4628 | 7.39k | if (full_chunk == NULL) { |
4629 | 0 | return BLOSC2_ERROR_MEMORY_ALLOC; |
4630 | 0 | } |
4631 | 7.39k | int dbytes = blosc2_decompress_ctx(context, src, srcsize, full_chunk, header->nbytes); |
4632 | 7.39k | context->dest = NULL; |
4633 | 7.39k | context->destsize = 0; |
4634 | 7.39k | if (dbytes < 0 || dbytes != header->nbytes) { |
4635 | 0 | free(full_chunk); |
4636 | 0 | return dbytes < 0 ? dbytes : BLOSC2_ERROR_DATA; |
4637 | 0 | } |
4638 | 7.39k | memcpy(dest, full_chunk + start_bytes64, (size_t)nitems_bytes); |
4639 | 7.39k | free(full_chunk); |
4640 | 7.39k | return nitems_bytes; |
4641 | 7.39k | } |
4642 | | |
4643 | 6.92k | ebsize = header->blocksize + header->typesize * (signed)sizeof(int32_t); |
4644 | 6.92k | struct thread_context* scontext = context->serial_context; |
4645 | | /* Resize the temporaries in serial context if needed */ |
4646 | 6.92k | if (header->blocksize > scontext->tmp_blocksize) { |
4647 | 0 | my_free(scontext->tmp); |
4648 | 0 | scontext->tmp_nbytes = (size_t)4 * ebsize; |
4649 | 0 | scontext->tmp = my_malloc(scontext->tmp_nbytes); |
4650 | 0 | BLOSC_ERROR_NULL(scontext->tmp, BLOSC2_ERROR_MEMORY_ALLOC); |
4651 | 0 | scontext->tmp2 = scontext->tmp + ebsize; |
4652 | 0 | scontext->tmp3 = scontext->tmp2 + ebsize; |
4653 | 0 | scontext->tmp4 = scontext->tmp3 + ebsize; |
4654 | 0 | scontext->tmp_blocksize = (int32_t)header->blocksize; |
4655 | 0 | } |
4656 | | |
4657 | 14.0k | for (j = 0; j < context->nblocks; j++) { |
4658 | 12.6k | bsize = header->blocksize; |
4659 | 12.6k | leftoverblock = 0; |
4660 | 12.6k | if ((j == context->nblocks - 1) && (context->leftover > 0)) { |
4661 | 5.72k | bsize = context->leftover; |
4662 | 5.72k | leftoverblock = 1; |
4663 | 5.72k | } |
4664 | | |
4665 | | /* Compute start & stop for each block */ |
4666 | 12.6k | startb = start * header->typesize - j * header->blocksize; |
4667 | 12.6k | stopb = stop * header->typesize - j * header->blocksize; |
4668 | 12.6k | if (stopb <= 0) { |
4669 | | // We can exit as soon as this block is beyond stop |
4670 | 5.51k | break; |
4671 | 5.51k | } |
4672 | 7.14k | if (startb >= header->blocksize) { |
4673 | 201 | continue; |
4674 | 201 | } |
4675 | 6.94k | if (startb < 0) { |
4676 | 17 | startb = 0; |
4677 | 17 | } |
4678 | 6.94k | if (stopb > header->blocksize) { |
4679 | 17 | stopb = header->blocksize; |
4680 | 17 | } |
4681 | 6.94k | bsize2 = stopb - startb; |
4682 | | |
4683 | 6.94k | #if defined(HAVE_ZFP) |
4684 | 6.94k | if (context->compcode == BLOSC_CODEC_ZFP_FIXED_RATE) { |
4685 | | // The items in this block |
4686 | 2.11k | scontext->zfp_cell_start = startb / context->typesize; |
4687 | 2.11k | scontext->zfp_cell_nitems = bsize2 / context->typesize; |
4688 | 2.11k | } |
4689 | 6.94k | #endif /* HAVE_ZFP */ |
4690 | | |
4691 | | /* Do the actual data copy */ |
4692 | | // Regular decompression. Put results in tmp2. |
4693 | | // If the items are exactly this block, decompress it in destination and avoid a copy |
4694 | 6.94k | bool get_single_block = ((ntbytes == 0) && (startb == 0) && (bsize == nitems_bytes)); |
4695 | 6.94k | uint8_t* tmp2 = get_single_block ? dest : scontext->tmp2; |
4696 | | |
4697 | | // If memcpyed we don't have a bstarts section (because it is not needed) |
4698 | 6.94k | int32_t src_offset = memcpyed ? |
4699 | 6.94k | context->header_overhead + j * header->blocksize : sw32_(bstarts + j); |
4700 | | |
4701 | 6.94k | int32_t cbytes = blosc_d(context->serial_context, bsize, leftoverblock, memcpyed, |
4702 | 6.94k | src, srcsize, src_offset, j, |
4703 | 6.94k | tmp2, 0, scontext->tmp, scontext->tmp3); |
4704 | 6.94k | if (cbytes < 0) { |
4705 | 0 | ntbytes = cbytes; |
4706 | 0 | break; |
4707 | 0 | } |
4708 | 6.94k | if (!get_single_block) { |
4709 | | /* Copy to destination. When ZFP decoded just the requested items (a single |
4710 | | cell), they are at the start of tmp2, else the whole block was decoded. */ |
4711 | 6.93k | bool zfp_cell = (scontext->zfp_cell_nitems > 0) && (cbytes == bsize2); |
4712 | 6.93k | memcpy((uint8_t *) dest + ntbytes, tmp2 + (zfp_cell ? 0 : startb), (unsigned int) bsize2); |
4713 | 6.93k | } |
4714 | 6.94k | ntbytes += bsize2; |
4715 | 6.94k | } |
4716 | | |
4717 | 6.92k | scontext->zfp_cell_nitems = 0; |
4718 | | |
4719 | 6.92k | if (ntbytes >= 0 && ntbytes != nitems_bytes) { |
4720 | | // A partial decode must not look like a success: callers that only test for |
4721 | | // a negative return would silently keep hold of an incompletely filled dest. |
4722 | 0 | BLOSC_TRACE_ERROR("Only %d bytes out of the %d requested could be decoded.", |
4723 | 0 | ntbytes, nitems_bytes); |
4724 | 0 | return BLOSC2_ERROR_DATA; |
4725 | 0 | } |
4726 | | |
4727 | 6.92k | return ntbytes; |
4728 | 6.92k | } |
4729 | | |
4730 | 16.3k | int blosc2_getitem(const void* src, int32_t srcsize, int start, int nitems, void* dest, int32_t destsize) { |
4731 | 16.3k | blosc2_context context; |
4732 | 16.3k | int result; |
4733 | | |
4734 | | /* Minimally populate the context */ |
4735 | 16.3k | memset(&context, 0, sizeof(blosc2_context)); |
4736 | | |
4737 | 16.3k | context.schunk = g_schunk; |
4738 | 16.3k | context.nthreads = 1; // force a serial decompression; fixes #95 |
4739 | | |
4740 | | /* Call the actual getitem function */ |
4741 | 16.3k | result = blosc2_getitem_ctx(&context, src, srcsize, start, nitems, dest, destsize); |
4742 | | |
4743 | | /* Release resources */ |
4744 | 16.3k | if (context.serial_context != NULL) { |
4745 | 16.3k | free_thread_context(context.serial_context); |
4746 | 16.3k | } |
4747 | 16.3k | return result; |
4748 | 16.3k | } |
4749 | | |
4750 | | /* Specific routine optimized for decompression a small number of |
4751 | | items out of a compressed chunk. Public non-contextual API. */ |
4752 | 0 | int blosc1_getitem(const void* src, int start, int nitems, void* dest) { |
4753 | 0 | return blosc2_getitem(src, INT32_MAX, start, nitems, dest, INT32_MAX); |
4754 | 0 | } |
4755 | | |
4756 | | /* Read and validate the chunk header for the getitem entry points below. |
4757 | | read_chunk_header() already rejects a zero typesize, so on success |
4758 | | header->typesize is in [1, BLOSC_MAX_TYPESIZE]. */ |
4759 | 16.3k | static int getitem_read_header(const void* src, int32_t srcsize, blosc_header* header) { |
4760 | 16.3k | int result = read_chunk_header((uint8_t *) src, srcsize, true, header); |
4761 | 16.3k | if (result < 0) { |
4762 | 0 | return result; |
4763 | 0 | } |
4764 | 16.3k | if (header->blosc2_flags2 & BLOSC2_VL_BLOCKS) { |
4765 | 0 | BLOSC_TRACE_ERROR("getitem is not supported for VL-block chunks."); |
4766 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4767 | 0 | } |
4768 | 16.3k | return 0; |
4769 | 16.3k | } |
4770 | | |
4771 | | /* Shared body of the getitem entry points. `start` and `nitems` are counted in |
4772 | | the typesize that `header` records. */ |
4773 | | static int getitem_with_header(blosc2_context* context, blosc_header* header, |
4774 | | const void* src, int32_t srcsize, |
4775 | 16.3k | int start, int nitems, void* dest, int32_t destsize) { |
4776 | | /* Minimally populate the context */ |
4777 | 16.3k | context->src = src; |
4778 | 16.3k | context->srcsize = srcsize; |
4779 | 16.3k | context->dest = dest; |
4780 | 16.3k | context->destsize = destsize; |
4781 | | |
4782 | 16.3k | int result = blosc2_initialize_context_from_header(context, header); |
4783 | 16.3k | if (result < 0) { |
4784 | 0 | return result; |
4785 | 0 | } |
4786 | | |
4787 | 16.3k | if (context->serial_context == NULL) { |
4788 | 16.3k | context->serial_context = create_thread_context(context, 0); |
4789 | 16.3k | } |
4790 | 16.3k | BLOSC_ERROR_NULL(context->serial_context, BLOSC2_ERROR_THREAD_CREATE); |
4791 | | /* Call the actual getitem function */ |
4792 | 16.3k | return _blosc_getitem(context, header, src, srcsize, start, nitems, dest, destsize); |
4793 | 16.3k | } |
4794 | | |
4795 | | int blosc2_getitem_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
4796 | 16.3k | int start, int nitems, void* dest, int32_t destsize) { |
4797 | 16.3k | blosc_header header; |
4798 | 16.3k | int result = getitem_read_header(src, srcsize, &header); |
4799 | 16.3k | if (result < 0) { |
4800 | 0 | return result; |
4801 | 0 | } |
4802 | | |
4803 | 16.3k | return getitem_with_header(context, &header, src, srcsize, start, nitems, dest, destsize); |
4804 | 16.3k | } |
4805 | | |
4806 | | int blosc2_getitem_bytes_ctx(blosc2_context* context, const void* src, int32_t srcsize, |
4807 | 0 | int32_t start, int32_t nbytes, void* dest, int32_t destsize) { |
4808 | 0 | blosc_header header; |
4809 | 0 | int result = getitem_read_header(src, srcsize, &header); |
4810 | 0 | if (result < 0) { |
4811 | 0 | return result; |
4812 | 0 | } |
4813 | | |
4814 | 0 | if (start < 0 || nbytes < 0) { |
4815 | 0 | BLOSC_TRACE_ERROR("`start` and `nbytes` must not be negative."); |
4816 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4817 | 0 | } |
4818 | | |
4819 | | /* The typesize a chunk records is 1 whenever the original one exceeded |
4820 | | BLOSC_MAX_TYPESIZE, so for wide types this conversion is a no-op and the |
4821 | | alignment check below is vacuous. That is the whole point of this entry |
4822 | | point: the caller never has to know whether the cap kicked in. */ |
4823 | 0 | int32_t typesize = header.typesize; |
4824 | 0 | if ((start % typesize != 0) || (nbytes % typesize != 0)) { |
4825 | 0 | BLOSC_TRACE_ERROR("`start` (%d) and `nbytes` (%d) must both be multiples of " |
4826 | 0 | "the typesize stored in the chunk (%d).", start, nbytes, typesize); |
4827 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4828 | 0 | } |
4829 | | |
4830 | 0 | return getitem_with_header(context, &header, src, srcsize, start / typesize, |
4831 | 0 | nbytes / typesize, dest, destsize); |
4832 | 0 | } |
4833 | | |
4834 | | int blosc2_decompress_block_ctx(blosc2_context* context, const void* src, |
4835 | | int32_t srcsize, int32_t nblock, void* dest, |
4836 | 0 | int32_t destsize) { |
4837 | 0 | blosc_header header; |
4838 | 0 | int result = read_chunk_header((uint8_t *)src, srcsize, true, &header); |
4839 | 0 | if (result < 0) { |
4840 | 0 | return result; |
4841 | 0 | } |
4842 | 0 | if (header.blosc2_flags2 & BLOSC2_VL_BLOCKS) { |
4843 | 0 | BLOSC_TRACE_ERROR("block decompression is not supported for VL-block chunks."); |
4844 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4845 | 0 | } |
4846 | | |
4847 | 0 | context->do_compress = 0; |
4848 | 0 | context->src = src; |
4849 | 0 | context->srcsize = srcsize; |
4850 | 0 | context->dest = dest; |
4851 | 0 | context->destsize = destsize; |
4852 | 0 | context->output_bytes = 0; |
4853 | 0 | context->vlblock_sources = NULL; |
4854 | 0 | context->vlblock_dests = NULL; |
4855 | 0 | if (context->blocknbytes != NULL) { |
4856 | 0 | free(context->blocknbytes); |
4857 | 0 | context->blocknbytes = NULL; |
4858 | 0 | } |
4859 | 0 | if (context->blockoffsets != NULL) { |
4860 | 0 | free(context->blockoffsets); |
4861 | 0 | context->blockoffsets = NULL; |
4862 | 0 | } |
4863 | 0 | if (context->blockcbytes != NULL) { |
4864 | 0 | free(context->blockcbytes); |
4865 | 0 | context->blockcbytes = NULL; |
4866 | 0 | } |
4867 | |
|
4868 | 0 | result = blosc2_initialize_context_from_header(context, &header); |
4869 | 0 | if (result < 0) { |
4870 | 0 | return result; |
4871 | 0 | } |
4872 | 0 | clear_context_decompression_dict(context); |
4873 | |
|
4874 | 0 | context->special_type = (header.blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK; |
4875 | 0 | if (context->special_type > BLOSC2_SPECIAL_LASTID) { |
4876 | 0 | BLOSC_TRACE_ERROR("Unknown special values ID (%d) ", context->special_type); |
4877 | 0 | return BLOSC2_ERROR_DATA; |
4878 | 0 | } |
4879 | 0 | if (nblock < 0 || nblock >= context->nblocks) { |
4880 | 0 | BLOSC_TRACE_ERROR("`nblock` out of bounds."); |
4881 | 0 | return BLOSC2_ERROR_INVALID_PARAM; |
4882 | 0 | } |
4883 | | |
4884 | 0 | bool memcpyed = (header.flags & (uint8_t)BLOSC_MEMCPYED) != 0; |
4885 | 0 | if (context->special_type) { |
4886 | 0 | memcpyed = true; |
4887 | 0 | } |
4888 | 0 | if (!context->special_type && !memcpyed) { |
4889 | 0 | size_t bstarts_nbytes; |
4890 | 0 | size_t bstarts_end; |
4891 | 0 | if (context->nblocks < 0 || |
4892 | 0 | !blosc2_checked_mul_size((size_t)context->nblocks, sizeof(int32_t), &bstarts_nbytes) || |
4893 | 0 | !blosc2_checked_add_size((size_t)context->header_overhead, bstarts_nbytes, &bstarts_end) || |
4894 | 0 | bstarts_end > (size_t)srcsize) { |
4895 | 0 | BLOSC_TRACE_ERROR("`bstarts` out of bounds."); |
4896 | 0 | return BLOSC2_ERROR_READ_BUFFER; |
4897 | 0 | } |
4898 | 0 | } |
4899 | | |
4900 | 0 | int32_t bsize = context->blocksize; |
4901 | 0 | int32_t leftoverblock = 0; |
4902 | 0 | if ((nblock == context->nblocks - 1) && (context->leftover > 0)) { |
4903 | 0 | bsize = context->leftover; |
4904 | 0 | leftoverblock = 1; |
4905 | 0 | } |
4906 | 0 | if (destsize < bsize) { |
4907 | 0 | BLOSC_TRACE_ERROR("Destination is too small for block."); |
4908 | 0 | return BLOSC2_ERROR_WRITE_BUFFER; |
4909 | 0 | } |
4910 | | |
4911 | 0 | bool has_delta = false; |
4912 | 0 | for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) { |
4913 | 0 | if (context->filters[i] == BLOSC_DELTA) { |
4914 | 0 | has_delta = true; |
4915 | 0 | break; |
4916 | 0 | } |
4917 | 0 | } |
4918 | 0 | if (has_delta) { |
4919 | 0 | uint8_t* full_chunk = malloc((size_t)header.nbytes); |
4920 | 0 | if (full_chunk == NULL) { |
4921 | 0 | return BLOSC2_ERROR_MEMORY_ALLOC; |
4922 | 0 | } |
4923 | 0 | int dbytes = blosc2_decompress_ctx(context, src, srcsize, full_chunk, header.nbytes); |
4924 | 0 | context->dest = dest; |
4925 | 0 | context->destsize = destsize; |
4926 | 0 | if (dbytes < 0 || dbytes != header.nbytes) { |
4927 | 0 | free(full_chunk); |
4928 | 0 | return dbytes < 0 ? dbytes : BLOSC2_ERROR_DATA; |
4929 | 0 | } |
4930 | 0 | memcpy(dest, full_chunk + (int64_t)nblock * context->blocksize, (size_t)bsize); |
4931 | 0 | free(full_chunk); |
4932 | 0 | return bsize; |
4933 | 0 | } |
4934 | | |
4935 | 0 | if (context->serial_context == NULL) { |
4936 | 0 | context->serial_context = create_thread_context(context, 0); |
4937 | 0 | } |
4938 | 0 | BLOSC_ERROR_NULL(context->serial_context, BLOSC2_ERROR_THREAD_CREATE); |
4939 | 0 | context->serial_context->parent_context = context; |
4940 | |
|
4941 | 0 | int32_t ebsize = context->blocksize + context->typesize * (int32_t)sizeof(int32_t); |
4942 | 0 | if (context->blocksize > context->serial_context->tmp_blocksize) { |
4943 | 0 | my_free(context->serial_context->tmp); |
4944 | 0 | context->serial_context->tmp_nbytes = (size_t)4 * ebsize; |
4945 | 0 | context->serial_context->tmp = my_malloc(context->serial_context->tmp_nbytes); |
4946 | 0 | BLOSC_ERROR_NULL(context->serial_context->tmp, BLOSC2_ERROR_MEMORY_ALLOC); |
4947 | 0 | context->serial_context->tmp2 = context->serial_context->tmp + ebsize; |
4948 | 0 | context->serial_context->tmp3 = context->serial_context->tmp2 + ebsize; |
4949 | 0 | context->serial_context->tmp4 = context->serial_context->tmp3 + ebsize; |
4950 | 0 | context->serial_context->tmp_blocksize = context->blocksize; |
4951 | 0 | } |
4952 | | |
4953 | 0 | int32_t* bstarts = (int32_t*)((uint8_t*)src + context->header_overhead); |
4954 | 0 | int32_t src_offset = memcpyed ? |
4955 | 0 | context->header_overhead + nblock * context->blocksize : sw32_(bstarts + nblock); |
4956 | 0 | int nbytes = blosc_d(context->serial_context, bsize, leftoverblock, memcpyed, |
4957 | 0 | src, srcsize, src_offset, nblock, |
4958 | 0 | (uint8_t*)dest, 0, |
4959 | 0 | context->serial_context->tmp, |
4960 | 0 | context->serial_context->tmp3); |
4961 | 0 | if (nbytes < 0) { |
4962 | 0 | return nbytes; |
4963 | 0 | } |
4964 | 0 | return bsize; |
4965 | 0 | } |
4966 | | |
4967 | | /* execute single compression/decompression job for a single thread_context */ |
4968 | | #if defined(_WIN32) |
4969 | | |
4970 | | /* Windows implementation: uses context fields directly (no job-group struct). */ |
4971 | | static void t_blosc_do_job(void *ctxt) |
4972 | | { |
4973 | | struct thread_context* thcontext = (struct thread_context*)ctxt; |
4974 | | blosc2_context* context = thcontext->parent_context; |
4975 | | int32_t cbytes; |
4976 | | int32_t ntdest; |
4977 | | int32_t tblocks; |
4978 | | int32_t tblock; |
4979 | | int32_t nblock_; |
4980 | | int32_t bsize; |
4981 | | int32_t leftoverblock; |
4982 | | int32_t blocksize; |
4983 | | int32_t ebsize; |
4984 | | int32_t srcsize; |
4985 | | bool compress = context->do_compress != 0; |
4986 | | int32_t maxbytes; |
4987 | | int32_t nblocks; |
4988 | | int32_t leftover; |
4989 | | int32_t leftover2; |
4990 | | int32_t* bstarts; |
4991 | | const uint8_t* src; |
4992 | | uint8_t* dest; |
4993 | | uint8_t* tmp; |
4994 | | uint8_t* tmp2; |
4995 | | uint8_t* tmp3; |
4996 | | |
4997 | | blocksize = context->blocksize; |
4998 | | ebsize = blocksize + context->typesize * (int32_t)sizeof(int32_t); |
4999 | | maxbytes = context->destsize; |
5000 | | nblocks = context->nblocks; |
5001 | | leftover = context->leftover; |
5002 | | bstarts = context->bstarts; |
5003 | | src = context->src; |
5004 | | srcsize = context->srcsize; |
5005 | | dest = context->dest; |
5006 | | |
5007 | | /* Resize the temporaries if needed */ |
5008 | | if (blocksize > thcontext->tmp_blocksize) { |
5009 | | my_free(thcontext->tmp); |
5010 | | thcontext->tmp_nbytes = (size_t)4 * ebsize; |
5011 | | thcontext->tmp = my_malloc(thcontext->tmp_nbytes); |
5012 | | thcontext->tmp2 = thcontext->tmp + ebsize; |
5013 | | thcontext->tmp3 = thcontext->tmp2 + ebsize; |
5014 | | thcontext->tmp4 = thcontext->tmp3 + ebsize; |
5015 | | thcontext->tmp_blocksize = blocksize; |
5016 | | } |
5017 | | |
5018 | | tmp = thcontext->tmp; |
5019 | | tmp2 = thcontext->tmp2; |
5020 | | tmp3 = thcontext->tmp3; |
5021 | | |
5022 | | bool vlblocks = (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) != 0; |
5023 | | bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
5024 | | if (!context->do_compress && context->special_type) { |
5025 | | memcpyed = true; |
5026 | | } |
5027 | | |
5028 | | bool static_schedule = (!compress || memcpyed) && context->block_maskout == NULL; |
5029 | | if (static_schedule) { |
5030 | | tblocks = nblocks / context->nthreads; |
5031 | | leftover2 = nblocks % context->nthreads; |
5032 | | tblocks = (leftover2 > 0) ? tblocks + 1 : tblocks; |
5033 | | nblock_ = thcontext->tid * tblocks; |
5034 | | tblock = nblock_ + tblocks; |
5035 | | if (tblock > nblocks) { |
5036 | | tblock = nblocks; |
5037 | | } |
5038 | | } |
5039 | | else { |
5040 | | blosc2_pthread_mutex_lock(&context->count_mutex); |
5041 | | context->thread_nblock++; |
5042 | | nblock_ = context->thread_nblock; |
5043 | | blosc2_pthread_mutex_unlock(&context->count_mutex); |
5044 | | tblock = nblocks; |
5045 | | } |
5046 | | |
5047 | | leftoverblock = 0; |
5048 | | while ((nblock_ < tblock) && (context->thread_giveup_code > 0)) { |
5049 | | bsize = vlblocks ? context->blocknbytes[nblock_] : blocksize; |
5050 | | leftoverblock = 0; |
5051 | | if (!vlblocks && nblock_ == (nblocks - 1) && (leftover > 0)) { |
5052 | | bsize = leftover; |
5053 | | leftoverblock = 1; |
5054 | | } |
5055 | | if (compress) { |
5056 | | if (memcpyed) { |
5057 | | if (!context->prefilter) { |
5058 | | memcpy(dest + context->header_overhead + nblock_ * blocksize, |
5059 | | src + nblock_ * blocksize, (unsigned int)bsize); |
5060 | | cbytes = (int32_t)bsize; |
5061 | | } |
5062 | | else { |
5063 | | cbytes = blosc_c(thcontext, bsize, leftoverblock, 0, |
5064 | | ebsize, |
5065 | | vlblocks ? context->vlblock_sources[nblock_] : src, |
5066 | | vlblocks ? 0 : nblock_ * blocksize, |
5067 | | dest + context->header_overhead + nblock_ * blocksize, |
5068 | | tmp, tmp3); |
5069 | | } |
5070 | | } |
5071 | | else { |
5072 | | cbytes = blosc_c(thcontext, bsize, leftoverblock, 0, |
5073 | | ebsize, |
5074 | | vlblocks ? context->vlblock_sources[nblock_] : src, |
5075 | | vlblocks ? 0 : nblock_ * blocksize, |
5076 | | tmp2, tmp, tmp3); |
5077 | | } |
5078 | | } |
5079 | | else { |
5080 | | if (context->special_type == BLOSC2_NO_SPECIAL && !memcpyed && |
5081 | | (srcsize < (int32_t)(context->header_overhead + (sizeof(int32_t) * nblocks)))) { |
5082 | | cbytes = -1; |
5083 | | } |
5084 | | else { |
5085 | | int32_t src_offset = memcpyed ? |
5086 | | context->header_overhead + nblock_ * blocksize : sw32_(bstarts + nblock_); |
5087 | | uint8_t *dest_block = (vlblocks && context->vlblock_dests != NULL) ? context->vlblock_dests[nblock_] : dest; |
5088 | | int32_t dest_offset = (vlblocks && context->vlblock_dests != NULL) ? 0 : |
5089 | | (vlblocks ? context->blockoffsets[nblock_] : nblock_ * blocksize); |
5090 | | cbytes = blosc_d(thcontext, bsize, leftoverblock, memcpyed, |
5091 | | src, srcsize, src_offset, nblock_, |
5092 | | dest_block, dest_offset, tmp, tmp2); |
5093 | | } |
5094 | | if (nblock_ == 0 || cbytes < 0) { |
5095 | | delta_release_dref(context); |
5096 | | } |
5097 | | } |
5098 | | |
5099 | | if (context->thread_giveup_code <= 0) { |
5100 | | break; |
5101 | | } |
5102 | | |
5103 | | if (cbytes < 0) { |
5104 | | blosc2_pthread_mutex_lock(&context->count_mutex); |
5105 | | context->thread_giveup_code = cbytes; |
5106 | | blosc2_pthread_mutex_unlock(&context->count_mutex); |
5107 | | break; |
5108 | | } |
5109 | | |
5110 | | if (compress && !memcpyed) { |
5111 | | blosc2_pthread_mutex_lock(&context->count_mutex); |
5112 | | ntdest = context->output_bytes; |
5113 | | if (!(context->use_dict && context->dict_cdict == NULL)) { |
5114 | | _sw32(bstarts + nblock_, (int32_t)ntdest); |
5115 | | } |
5116 | | if ((cbytes == 0) || (ntdest + cbytes > maxbytes)) { |
5117 | | context->thread_giveup_code = 0; |
5118 | | blosc2_pthread_mutex_unlock(&context->count_mutex); |
5119 | | break; |
5120 | | } |
5121 | | context->thread_nblock++; |
5122 | | nblock_ = context->thread_nblock; |
5123 | | context->output_bytes += cbytes; |
5124 | | blosc2_pthread_mutex_unlock(&context->count_mutex); |
5125 | | memcpy(dest + ntdest, tmp2, (unsigned int)cbytes); |
5126 | | } |
5127 | | else if (static_schedule) { |
5128 | | nblock_++; |
5129 | | } |
5130 | | else { |
5131 | | blosc2_pthread_mutex_lock(&context->count_mutex); |
5132 | | context->thread_nblock++; |
5133 | | nblock_ = context->thread_nblock; |
5134 | | context->output_bytes += cbytes; |
5135 | | blosc2_pthread_mutex_unlock(&context->count_mutex); |
5136 | | } |
5137 | | } |
5138 | | |
5139 | | if (static_schedule) { |
5140 | | blosc2_pthread_mutex_lock(&context->count_mutex); |
5141 | | context->output_bytes = context->sourcesize; |
5142 | | if (compress) { |
5143 | | context->output_bytes += context->header_overhead; |
5144 | | } |
5145 | | blosc2_pthread_mutex_unlock(&context->count_mutex); |
5146 | | } |
5147 | | } |
5148 | | |
5149 | | #else /* !_WIN32 */ |
5150 | | |
5151 | 0 | static int ensure_thread_context_capacity(struct thread_context* thread_context, blosc2_context* context) { |
5152 | 0 | int32_t blocksize = context->blocksize; |
5153 | 0 | int32_t ebsize = blocksize + context->typesize * (int32_t)sizeof(int32_t); |
5154 | |
|
5155 | 0 | if (blocksize <= thread_context->tmp_blocksize) { |
5156 | 0 | return 0; |
5157 | 0 | } |
5158 | | |
5159 | 0 | my_free(thread_context->tmp); |
5160 | 0 | thread_context->tmp_nbytes = (size_t)4 * ebsize; |
5161 | 0 | thread_context->tmp = my_malloc(thread_context->tmp_nbytes); |
5162 | 0 | BLOSC_ERROR_NULL(thread_context->tmp, BLOSC2_ERROR_MEMORY_ALLOC); |
5163 | 0 | thread_context->tmp2 = thread_context->tmp + ebsize; |
5164 | 0 | thread_context->tmp3 = thread_context->tmp2 + ebsize; |
5165 | 0 | thread_context->tmp4 = thread_context->tmp3 + ebsize; |
5166 | 0 | thread_context->tmp_blocksize = blocksize; |
5167 | 0 | return 0; |
5168 | 0 | } |
5169 | | |
5170 | 0 | static int32_t claim_job_block(struct blosc_job_group *job) { |
5171 | 0 | int32_t nblock_; |
5172 | 0 | blosc2_pthread_mutex_lock(&job->mutex); |
5173 | 0 | nblock_ = ++job->next_block; |
5174 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5175 | 0 | return nblock_; |
5176 | 0 | } |
5177 | | |
5178 | | /* Non-Windows implementation: uses the job-group struct for thread coordination. */ |
5179 | 0 | static void t_blosc_do_job(void *ctxt) { |
5180 | 0 | struct thread_context* thcontext = (struct thread_context*)ctxt; |
5181 | 0 | blosc2_context* context = thcontext->parent_context; |
5182 | 0 | struct blosc_job_group* job = context->job; |
5183 | 0 | int32_t cbytes; |
5184 | 0 | int32_t ntdest; |
5185 | 0 | int32_t tblocks; |
5186 | 0 | int32_t tblock; |
5187 | 0 | int32_t nblock_; |
5188 | 0 | int32_t bsize; |
5189 | 0 | int32_t leftoverblock; |
5190 | 0 | int32_t blocksize; |
5191 | 0 | int32_t ebsize; |
5192 | 0 | int32_t srcsize; |
5193 | 0 | bool compress = context->do_compress != 0; |
5194 | 0 | int32_t maxbytes; |
5195 | 0 | int32_t nblocks; |
5196 | 0 | int32_t leftover; |
5197 | 0 | int32_t leftover2; |
5198 | 0 | int32_t* bstarts; |
5199 | 0 | const uint8_t* src; |
5200 | 0 | uint8_t* dest; |
5201 | 0 | uint8_t* tmp; |
5202 | 0 | uint8_t* tmp2; |
5203 | 0 | uint8_t* tmp3; |
5204 | |
|
5205 | 0 | blocksize = context->blocksize; |
5206 | 0 | ebsize = blocksize + context->typesize * (int32_t)sizeof(int32_t); |
5207 | 0 | maxbytes = context->destsize; |
5208 | 0 | nblocks = context->nblocks; |
5209 | 0 | leftover = context->leftover; |
5210 | 0 | bstarts = context->bstarts; |
5211 | 0 | src = context->src; |
5212 | 0 | srcsize = context->srcsize; |
5213 | 0 | dest = context->dest; |
5214 | |
|
5215 | 0 | if (ensure_thread_context_capacity(thcontext, context) < 0) { |
5216 | 0 | blosc2_pthread_mutex_lock(&job->mutex); |
5217 | 0 | job->giveup_code = BLOSC2_ERROR_MEMORY_ALLOC; |
5218 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5219 | 0 | if (!compress) { |
5220 | 0 | delta_release_dref(context); |
5221 | 0 | } |
5222 | 0 | goto job_done; |
5223 | 0 | } |
5224 | | |
5225 | 0 | tmp = thcontext->tmp; |
5226 | 0 | tmp2 = thcontext->tmp2; |
5227 | 0 | tmp3 = thcontext->tmp3; |
5228 | |
|
5229 | 0 | bool vlblocks = (context->blosc2_flags2 & BLOSC2_VL_BLOCKS) != 0; |
5230 | 0 | bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
5231 | 0 | if (!context->do_compress && context->special_type) { |
5232 | 0 | memcpyed = true; |
5233 | 0 | } |
5234 | |
|
5235 | 0 | if (job->static_schedule) { |
5236 | 0 | tblocks = nblocks / context->nthreads; |
5237 | 0 | leftover2 = nblocks % context->nthreads; |
5238 | 0 | tblocks = (leftover2 > 0) ? tblocks + 1 : tblocks; |
5239 | 0 | nblock_ = thcontext->tid * tblocks; |
5240 | 0 | tblock = nblock_ + tblocks; |
5241 | 0 | if (tblock > nblocks) { |
5242 | 0 | tblock = nblocks; |
5243 | 0 | } |
5244 | 0 | } |
5245 | 0 | else { |
5246 | 0 | nblock_ = claim_job_block(job); |
5247 | 0 | tblock = nblocks; |
5248 | 0 | } |
5249 | |
|
5250 | 0 | leftoverblock = 0; |
5251 | 0 | while (nblock_ < tblock) { |
5252 | 0 | blosc2_pthread_mutex_lock(&job->mutex); |
5253 | 0 | if (job->giveup_code <= 0) { |
5254 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5255 | 0 | break; |
5256 | 0 | } |
5257 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5258 | |
|
5259 | 0 | bsize = vlblocks ? context->blocknbytes[nblock_] : blocksize; |
5260 | 0 | leftoverblock = 0; |
5261 | 0 | if (!vlblocks && nblock_ == (nblocks - 1) && (leftover > 0)) { |
5262 | 0 | bsize = leftover; |
5263 | 0 | leftoverblock = 1; |
5264 | 0 | } |
5265 | 0 | if (compress) { |
5266 | 0 | if (memcpyed) { |
5267 | 0 | if (!context->prefilter) { |
5268 | 0 | memcpy(dest + context->header_overhead + nblock_ * blocksize, |
5269 | 0 | src + nblock_ * blocksize, (unsigned int) bsize); |
5270 | 0 | cbytes = (int32_t)bsize; |
5271 | 0 | } |
5272 | 0 | else { |
5273 | 0 | cbytes = blosc_c(thcontext, bsize, leftoverblock, 0, |
5274 | 0 | ebsize, |
5275 | 0 | vlblocks ? context->vlblock_sources[nblock_] : src, |
5276 | 0 | vlblocks ? 0 : nblock_ * blocksize, |
5277 | 0 | dest + context->header_overhead + nblock_ * blocksize, |
5278 | 0 | tmp, tmp3); |
5279 | 0 | } |
5280 | 0 | } |
5281 | 0 | else { |
5282 | 0 | cbytes = blosc_c(thcontext, bsize, leftoverblock, 0, |
5283 | 0 | ebsize, |
5284 | 0 | vlblocks ? context->vlblock_sources[nblock_] : src, |
5285 | 0 | vlblocks ? 0 : nblock_ * blocksize, |
5286 | 0 | tmp2, tmp, tmp3); |
5287 | 0 | } |
5288 | 0 | } |
5289 | 0 | else { |
5290 | 0 | if (context->special_type == BLOSC2_NO_SPECIAL && !memcpyed && |
5291 | 0 | (srcsize < (int32_t)(context->header_overhead + (sizeof(int32_t) * nblocks)))) { |
5292 | 0 | cbytes = -1; |
5293 | 0 | } |
5294 | 0 | else { |
5295 | 0 | int32_t src_offset = memcpyed ? |
5296 | 0 | context->header_overhead + nblock_ * blocksize : sw32_(bstarts + nblock_); |
5297 | 0 | uint8_t *dest_block = (vlblocks && context->vlblock_dests != NULL) ? context->vlblock_dests[nblock_] : dest; |
5298 | 0 | int32_t dest_offset = (vlblocks && context->vlblock_dests != NULL) ? 0 : |
5299 | 0 | (vlblocks ? context->blockoffsets[nblock_] : nblock_ * blocksize); |
5300 | 0 | cbytes = blosc_d(thcontext, bsize, leftoverblock, memcpyed, |
5301 | 0 | src, srcsize, src_offset, nblock_, |
5302 | 0 | dest_block, dest_offset, tmp, tmp2); |
5303 | 0 | } |
5304 | 0 | if (nblock_ == 0 || cbytes < 0) { |
5305 | 0 | delta_release_dref(context); |
5306 | 0 | } |
5307 | 0 | } |
5308 | |
|
5309 | 0 | if (cbytes < 0) { |
5310 | 0 | blosc2_pthread_mutex_lock(&job->mutex); |
5311 | 0 | if (job->giveup_code > 0) { |
5312 | 0 | job->giveup_code = cbytes; |
5313 | 0 | } |
5314 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5315 | 0 | break; |
5316 | 0 | } |
5317 | | |
5318 | 0 | blosc2_pthread_mutex_lock(&job->mutex); |
5319 | 0 | if (compress && !memcpyed) { |
5320 | 0 | ntdest = job->output_bytes; |
5321 | 0 | if (!(context->use_dict && context->dict_cdict == NULL)) { |
5322 | 0 | _sw32(bstarts + nblock_, (int32_t)ntdest); |
5323 | 0 | } |
5324 | 0 | if ((cbytes == 0) || (ntdest + cbytes > maxbytes)) { |
5325 | 0 | job->giveup_code = 0; |
5326 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5327 | 0 | break; |
5328 | 0 | } |
5329 | 0 | job->output_bytes += cbytes; |
5330 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5331 | 0 | memcpy(dest + ntdest, tmp2, (unsigned int)cbytes); |
5332 | 0 | } |
5333 | 0 | else { |
5334 | 0 | job->output_bytes += cbytes; |
5335 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5336 | 0 | } |
5337 | | |
5338 | 0 | if (job->static_schedule) { |
5339 | 0 | nblock_++; |
5340 | 0 | } |
5341 | 0 | else { |
5342 | 0 | nblock_ = claim_job_block(job); |
5343 | 0 | } |
5344 | 0 | } |
5345 | |
|
5346 | 0 | job_done: |
5347 | 0 | blosc2_pthread_mutex_lock(&job->mutex); |
5348 | 0 | job->blocks_completed++; |
5349 | 0 | if (--job->active_workers == 0) { |
5350 | 0 | if (job->static_schedule && job->giveup_code > 0) { |
5351 | 0 | job->output_bytes = context->sourcesize; |
5352 | 0 | if (compress) { |
5353 | 0 | job->output_bytes += context->header_overhead; |
5354 | 0 | } |
5355 | 0 | } |
5356 | 0 | if (context->thread_backend == BLOSC_BACKEND_CALLBACK) { |
5357 | 0 | job->completed = true; |
5358 | 0 | blosc2_pthread_cond_broadcast(&job->completion_cv); |
5359 | 0 | } |
5360 | 0 | } |
5361 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5362 | 0 | } |
5363 | | |
5364 | | #endif /* _WIN32 */ |
5365 | | |
5366 | 0 | static void job_group_init(struct blosc_job_group *job, blosc2_context *context) { |
5367 | 0 | memset(job, 0, sizeof(*job)); |
5368 | 0 | job->context = context; |
5369 | 0 | job->next_block = -1; |
5370 | 0 | job->output_bytes = context->output_bytes; |
5371 | 0 | job->giveup_code = 1; |
5372 | 0 | job->dref_not_init = 1; |
5373 | 0 | bool compress = context->do_compress != 0; |
5374 | 0 | bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED; |
5375 | 0 | if (!compress && context->special_type) { |
5376 | 0 | memcpyed = true; |
5377 | 0 | } |
5378 | 0 | job->static_schedule = (!compress || memcpyed) && context->block_maskout == NULL; |
5379 | 0 | blosc2_pthread_mutex_init(&job->mutex, NULL); |
5380 | 0 | blosc2_pthread_mutex_init(&job->delta_mutex, NULL); |
5381 | 0 | blosc2_pthread_cond_init(&job->delta_cv, NULL); |
5382 | 0 | blosc2_pthread_cond_init(&job->completion_cv, NULL); |
5383 | 0 | } |
5384 | | |
5385 | 0 | static void job_group_destroy(struct blosc_job_group *job) { |
5386 | 0 | blosc2_pthread_mutex_destroy(&job->mutex); |
5387 | 0 | blosc2_pthread_mutex_destroy(&job->delta_mutex); |
5388 | 0 | blosc2_pthread_cond_destroy(&job->delta_cv); |
5389 | 0 | blosc2_pthread_cond_destroy(&job->completion_cv); |
5390 | 0 | } |
5391 | | |
5392 | 0 | static void* shared_pool_worker(void* arg) { |
5393 | 0 | struct thread_context* thcontext = (struct thread_context*)arg; |
5394 | 0 | struct blosc_shared_pool* pool = thcontext->owner_pool; |
5395 | |
|
5396 | 0 | while (1) { |
5397 | 0 | struct blosc_job_group* job = NULL; |
5398 | 0 | struct blosc_job_queue_entry* entry = NULL; |
5399 | 0 | blosc2_pthread_mutex_lock(&pool->mutex); |
5400 | 0 | while (!pool->shutdown && pool->job_queue_head == NULL) { |
5401 | 0 | blosc2_pthread_cond_wait(&pool->work_cv, &pool->mutex); |
5402 | 0 | } |
5403 | 0 | if (pool->shutdown) { |
5404 | 0 | blosc2_pthread_mutex_unlock(&pool->mutex); |
5405 | 0 | break; |
5406 | 0 | } |
5407 | 0 | entry = pool->job_queue_head; |
5408 | 0 | job = entry->job; |
5409 | 0 | pool->job_queue_head = entry->next; |
5410 | 0 | if (pool->job_queue_head == NULL) { |
5411 | 0 | pool->job_queue_tail = NULL; |
5412 | 0 | } |
5413 | 0 | blosc2_pthread_mutex_unlock(&pool->mutex); |
5414 | 0 | int32_t logical_tid = entry->logical_tid; |
5415 | 0 | my_free(entry); |
5416 | |
|
5417 | 0 | thcontext->parent_context = job->context; |
5418 | 0 | thcontext->tid = logical_tid; |
5419 | 0 | t_blosc_do_job(thcontext); |
5420 | 0 | thcontext->parent_context = NULL; |
5421 | | |
5422 | | /* Signal job completion BEFORE touching pool accounting. |
5423 | | * The job struct is stack-allocated in parallel_blosc; the main thread |
5424 | | * is allowed to destroy it as soon as completed==true is visible. |
5425 | | * Decrementing pending_workers here (under job->mutex) guarantees that |
5426 | | * every worker has finished with the job before the last decrement can |
5427 | | * reach 0 and wake the main thread. Doing the pool accounting afterwards |
5428 | | * is safe because it no longer references the job. */ |
5429 | 0 | blosc2_pthread_mutex_lock(&job->mutex); |
5430 | 0 | if (--job->pending_workers == 0) { |
5431 | 0 | job->completed = true; |
5432 | 0 | blosc2_pthread_cond_broadcast(&job->completion_cv); |
5433 | 0 | } |
5434 | 0 | blosc2_pthread_mutex_unlock(&job->mutex); |
5435 | |
|
5436 | 0 | blosc2_pthread_mutex_lock(&pool->mutex); |
5437 | 0 | pool->active_jobs--; |
5438 | 0 | if (pool->active_jobs == 0 && pool->context_refs == 0 && pool->job_queue_head == NULL) { |
5439 | 0 | blosc2_pthread_cond_broadcast(&pool->idle_cv); |
5440 | 0 | } |
5441 | 0 | blosc2_pthread_mutex_unlock(&pool->mutex); |
5442 | 0 | } |
5443 | |
|
5444 | 0 | destroy_thread_context(thcontext); |
5445 | 0 | return NULL; |
5446 | 0 | } |
5447 | | |
5448 | 0 | static struct blosc_shared_pool* find_shared_pool_locked(int16_t nthreads) { |
5449 | 0 | struct blosc_shared_pool *pool = shared_pools; |
5450 | 0 | while (pool != NULL) { |
5451 | 0 | if (pool->nthreads == nthreads) { |
5452 | 0 | return pool; |
5453 | 0 | } |
5454 | 0 | pool = pool->next; |
5455 | 0 | } |
5456 | 0 | return NULL; |
5457 | 0 | } |
5458 | | |
5459 | 0 | static int create_shared_pool(int16_t nthreads, struct blosc_shared_pool **pool_out) { |
5460 | 0 | int rc = 0; |
5461 | 0 | int rc2; |
5462 | 0 | int32_t contexts_init = 0; // per-thread contexts successfully initialized |
5463 | 0 | int32_t threads_started = 0; // worker threads successfully created |
5464 | 0 | #if !defined(_WIN32) |
5465 | 0 | bool attr_inited = false; |
5466 | 0 | #endif |
5467 | 0 | struct blosc_shared_pool *pool = (struct blosc_shared_pool *)my_malloc(sizeof(*pool)); |
5468 | 0 | BLOSC_ERROR_NULL(pool, BLOSC2_ERROR_MEMORY_ALLOC); |
5469 | 0 | memset(pool, 0, sizeof(*pool)); |
5470 | 0 | pool->nthreads = nthreads; |
5471 | 0 | blosc2_pthread_mutex_init(&pool->mutex, NULL); |
5472 | 0 | blosc2_pthread_cond_init(&pool->work_cv, NULL); |
5473 | 0 | blosc2_pthread_cond_init(&pool->idle_cv, NULL); |
5474 | 0 | pool->threads = (blosc2_pthread_t*)my_malloc(nthreads * sizeof(blosc2_pthread_t)); |
5475 | 0 | if (pool->threads == NULL) { rc = BLOSC2_ERROR_MEMORY_ALLOC; goto error; } |
5476 | 0 | pool->thread_contexts = (struct thread_context*)my_malloc((size_t)nthreads * sizeof(struct thread_context)); |
5477 | 0 | if (pool->thread_contexts == NULL) { rc = BLOSC2_ERROR_MEMORY_ALLOC; goto error; } |
5478 | 0 | memset(pool->thread_contexts, 0, (size_t)nthreads * sizeof(struct thread_context)); |
5479 | 0 | #if !defined(_WIN32) |
5480 | 0 | pthread_attr_init(&pool->ct_attr); |
5481 | 0 | pthread_attr_setdetachstate(&pool->ct_attr, PTHREAD_CREATE_JOINABLE); |
5482 | 0 | attr_inited = true; |
5483 | 0 | #endif |
5484 | 0 | for (int32_t tid = 0; tid < nthreads; ++tid) { |
5485 | 0 | rc = init_thread_context(pool->thread_contexts + tid, NULL, tid); |
5486 | 0 | if (rc < 0) { |
5487 | 0 | goto error; |
5488 | 0 | } |
5489 | 0 | contexts_init++; |
5490 | 0 | pool->thread_contexts[tid].owner_pool = pool; |
5491 | 0 | #if !defined(_WIN32) |
5492 | 0 | rc2 = blosc2_pthread_create(&pool->threads[tid], &pool->ct_attr, shared_pool_worker, |
5493 | 0 | (void*)(pool->thread_contexts + tid)); |
5494 | | #else |
5495 | | rc2 = blosc2_pthread_create(&pool->threads[tid], NULL, shared_pool_worker, |
5496 | | (void*)(pool->thread_contexts + tid)); |
5497 | | #endif |
5498 | 0 | if (rc2) { |
5499 | 0 | BLOSC_TRACE_ERROR("Return code from blosc2_pthread_create() is %d.\n\tError detail: %s\n", rc2, strerror(rc2)); |
5500 | 0 | rc = BLOSC2_ERROR_THREAD_CREATE; |
5501 | 0 | goto error; |
5502 | 0 | } |
5503 | 0 | threads_started++; |
5504 | 0 | } |
5505 | 0 | *pool_out = pool; |
5506 | 0 | return 0; |
5507 | | |
5508 | 0 | error: |
5509 | | // A worker frees its own thread context when it exits (see shared_pool_worker), |
5510 | | // so signal shutdown and join the ones already started before freeing anything. |
5511 | 0 | if (threads_started > 0) { |
5512 | 0 | blosc2_pthread_mutex_lock(&pool->mutex); |
5513 | 0 | pool->shutdown = 1; |
5514 | 0 | blosc2_pthread_cond_broadcast(&pool->work_cv); |
5515 | 0 | blosc2_pthread_mutex_unlock(&pool->mutex); |
5516 | 0 | for (int32_t t = 0; t < threads_started; ++t) { |
5517 | 0 | void *status; |
5518 | 0 | blosc2_pthread_join(pool->threads[t], &status); |
5519 | 0 | } |
5520 | 0 | } |
5521 | | // Contexts that were initialized but never got a running worker have no owner |
5522 | | // to free them; tear those down here (the started ones did it themselves). |
5523 | 0 | for (int32_t t = threads_started; t < contexts_init; ++t) { |
5524 | 0 | destroy_thread_context(pool->thread_contexts + t); |
5525 | 0 | } |
5526 | 0 | #if !defined(_WIN32) |
5527 | 0 | if (attr_inited) { |
5528 | 0 | pthread_attr_destroy(&pool->ct_attr); |
5529 | 0 | } |
5530 | 0 | #endif |
5531 | 0 | if (pool->thread_contexts != NULL) { |
5532 | 0 | my_free(pool->thread_contexts); |
5533 | 0 | } |
5534 | 0 | if (pool->threads != NULL) { |
5535 | 0 | my_free(pool->threads); |
5536 | 0 | } |
5537 | 0 | blosc2_pthread_cond_destroy(&pool->idle_cv); |
5538 | 0 | blosc2_pthread_cond_destroy(&pool->work_cv); |
5539 | 0 | blosc2_pthread_mutex_destroy(&pool->mutex); |
5540 | 0 | my_free(pool); |
5541 | 0 | return rc; |
5542 | 0 | } |
5543 | | |
5544 | 0 | static void destroy_shared_pool(struct blosc_shared_pool *pool) { |
5545 | 0 | void *status; |
5546 | |
|
5547 | 0 | blosc2_pthread_mutex_lock(&pool->mutex); |
5548 | 0 | pool->shutdown = 1; |
5549 | 0 | blosc2_pthread_cond_broadcast(&pool->work_cv); |
5550 | 0 | blosc2_pthread_mutex_unlock(&pool->mutex); |
5551 | |
|
5552 | 0 | for (int32_t t = 0; t < pool->nthreads; ++t) { |
5553 | 0 | blosc2_pthread_join(pool->threads[t], &status); |
5554 | 0 | } |
5555 | 0 | #if !defined(_WIN32) |
5556 | 0 | pthread_attr_destroy(&pool->ct_attr); |
5557 | 0 | #endif |
5558 | 0 | my_free(pool->threads); |
5559 | 0 | my_free(pool->thread_contexts); |
5560 | 0 | blosc2_pthread_cond_destroy(&pool->idle_cv); |
5561 | 0 | blosc2_pthread_cond_destroy(&pool->work_cv); |
5562 | 0 | blosc2_pthread_mutex_destroy(&pool->mutex); |
5563 | 0 | my_free(pool); |
5564 | 0 | } |
5565 | | |
5566 | 0 | static int attach_shared_pool(blosc2_context *context) { |
5567 | 0 | struct blosc_shared_pool *pool; |
5568 | |
|
5569 | 0 | if (!g_initlib) blosc2_init(); |
5570 | 0 | blosc2_pthread_mutex_lock(&pool_registry_mutex); |
5571 | 0 | pool = find_shared_pool_locked(context->nthreads); |
5572 | 0 | if (pool == NULL) { |
5573 | 0 | int rc = create_shared_pool(context->nthreads, &pool); |
5574 | 0 | if (rc < 0) { |
5575 | 0 | blosc2_pthread_mutex_unlock(&pool_registry_mutex); |
5576 | 0 | return rc; |
5577 | 0 | } |
5578 | 0 | pool->next = shared_pools; |
5579 | 0 | shared_pools = pool; |
5580 | 0 | } |
5581 | 0 | pool->context_refs++; |
5582 | 0 | blosc2_pthread_mutex_unlock(&pool_registry_mutex); |
5583 | |
|
5584 | 0 | context->thread_pool = pool; |
5585 | 0 | context->thread_backend = BLOSC_BACKEND_SHARED_POOL; |
5586 | 0 | context->threads_started = context->nthreads; |
5587 | 0 | context->pool_epoch = g_destroy_count; |
5588 | 0 | return 0; |
5589 | 0 | } |
5590 | | |
5591 | | #if defined(_WIN32) |
5592 | | /* Per-context worker thread for Windows (BLOSC_BACKEND_PER_CONTEXT). |
5593 | | * Sleeps between jobs using a job_seq counter; wakes when main increments |
5594 | | * job_seq and broadcasts jobs_ready. The last worker to finish signals |
5595 | | * jobs_done so main can return from parallel_blosc. */ |
5596 | | static void* t_blosc_win(void* arg) { |
5597 | | struct thread_context* thcontext = (struct thread_context*)arg; |
5598 | | blosc2_context* context = thcontext->parent_context; |
5599 | | |
5600 | | blosc2_pthread_mutex_lock(&context->jobs_mutex); |
5601 | | while (1) { |
5602 | | /* Sleep until main posts a new job or signals shutdown. */ |
5603 | | while (!context->end_threads && thcontext->my_job_seq == context->job_seq) { |
5604 | | blosc2_pthread_cond_wait(&context->jobs_ready, &context->jobs_mutex); |
5605 | | } |
5606 | | if (context->end_threads) { |
5607 | | break; |
5608 | | } |
5609 | | thcontext->my_job_seq = context->job_seq; |
5610 | | blosc2_pthread_mutex_unlock(&context->jobs_mutex); |
5611 | | |
5612 | | t_blosc_do_job(thcontext); |
5613 | | |
5614 | | blosc2_pthread_mutex_lock(&context->jobs_mutex); |
5615 | | if (--context->active_workers == 0) { |
5616 | | blosc2_pthread_cond_signal(&context->jobs_done); |
5617 | | } |
5618 | | } |
5619 | | blosc2_pthread_mutex_unlock(&context->jobs_mutex); |
5620 | | free_thread_context(thcontext); |
5621 | | return NULL; |
5622 | | } |
5623 | | |
5624 | | static int init_threadpool(blosc2_context *context) { |
5625 | | int16_t nthreads = context->nthreads; |
5626 | | |
5627 | | blosc2_pthread_mutex_init(&context->count_mutex, NULL); |
5628 | | blosc2_pthread_mutex_init(&context->delta_mutex, NULL); |
5629 | | blosc2_pthread_cond_init(&context->delta_cv, NULL); |
5630 | | blosc2_pthread_mutex_init(&context->jobs_mutex, NULL); |
5631 | | blosc2_pthread_cond_init(&context->jobs_ready, NULL); |
5632 | | blosc2_pthread_cond_init(&context->jobs_done, NULL); |
5633 | | context->end_threads = 0; |
5634 | | context->job_seq = 0; |
5635 | | context->active_workers = 0; |
5636 | | context->thread_giveup_code = 1; |
5637 | | context->thread_nblock = -1; |
5638 | | |
5639 | | context->threads = (blosc2_pthread_t *)my_malloc((size_t)nthreads * sizeof(blosc2_pthread_t)); |
5640 | | if (context->threads == NULL) { |
5641 | | goto oom_threads; |
5642 | | } |
5643 | | |
5644 | | int16_t created = 0; |
5645 | | for (int16_t tid = 0; tid < nthreads; tid++) { |
5646 | | struct thread_context *thread_context = create_thread_context(context, tid); |
5647 | | if (thread_context == NULL) { |
5648 | | goto create_error; |
5649 | | } |
5650 | | thread_context->my_job_seq = 0; |
5651 | | int rc = blosc2_pthread_create(&context->threads[tid], NULL, t_blosc_win, thread_context); |
5652 | | if (rc != 0) { |
5653 | | BLOSC_TRACE_ERROR("blosc2_pthread_create() failed: %d\n", rc); |
5654 | | free_thread_context(thread_context); |
5655 | | goto create_error; |
5656 | | } |
5657 | | created++; |
5658 | | } |
5659 | | |
5660 | | context->thread_backend = BLOSC_BACKEND_PER_CONTEXT; |
5661 | | context->threads_started = nthreads; |
5662 | | return 0; |
5663 | | |
5664 | | create_error: |
5665 | | blosc2_pthread_mutex_lock(&context->jobs_mutex); |
5666 | | context->end_threads = 1; |
5667 | | blosc2_pthread_cond_broadcast(&context->jobs_ready); |
5668 | | blosc2_pthread_mutex_unlock(&context->jobs_mutex); |
5669 | | for (int16_t t = 0; t < created; t++) { |
5670 | | blosc2_pthread_join(context->threads[t], NULL); |
5671 | | } |
5672 | | my_free(context->threads); |
5673 | | context->threads = NULL; |
5674 | | oom_threads: |
5675 | | blosc2_pthread_cond_destroy(&context->jobs_done); |
5676 | | blosc2_pthread_cond_destroy(&context->jobs_ready); |
5677 | | blosc2_pthread_mutex_destroy(&context->jobs_mutex); |
5678 | | blosc2_pthread_cond_destroy(&context->delta_cv); |
5679 | | blosc2_pthread_mutex_destroy(&context->delta_mutex); |
5680 | | blosc2_pthread_mutex_destroy(&context->count_mutex); |
5681 | | return BLOSC2_ERROR_THREAD_CREATE; |
5682 | | } |
5683 | | #endif /* _WIN32 */ |
5684 | | |
5685 | | |
5686 | 0 | static int init_callback_threads(blosc2_context *context) { |
5687 | 0 | int32_t tid; |
5688 | |
|
5689 | 0 | blosc2_pthread_mutex_init(&context->count_mutex, NULL); |
5690 | 0 | blosc2_pthread_mutex_init(&context->delta_mutex, NULL); |
5691 | 0 | blosc2_pthread_cond_init(&context->delta_cv, NULL); |
5692 | 0 | context->thread_contexts = (struct thread_context *)my_malloc( |
5693 | 0 | context->nthreads * sizeof(struct thread_context)); |
5694 | 0 | BLOSC_ERROR_NULL(context->thread_contexts, BLOSC2_ERROR_MEMORY_ALLOC); |
5695 | 0 | for (tid = 0; tid < context->nthreads; tid++) { |
5696 | 0 | init_thread_context(context->thread_contexts + tid, context, tid); |
5697 | 0 | } |
5698 | 0 | context->thread_backend = BLOSC_BACKEND_CALLBACK; |
5699 | 0 | context->threads_started = context->nthreads; |
5700 | 0 | return 0; |
5701 | 0 | } |
5702 | | |
5703 | 64.9k | static int release_thread_backend(blosc2_context *context) { |
5704 | 64.9k | if (context->thread_backend == BLOSC_BACKEND_CALLBACK && context->threads_started > 0) { |
5705 | 0 | for (int32_t t = 0; t < context->threads_started; t++) { |
5706 | 0 | destroy_thread_context(context->thread_contexts + t); |
5707 | 0 | } |
5708 | 0 | my_free(context->thread_contexts); |
5709 | 0 | context->thread_contexts = NULL; |
5710 | 0 | blosc2_pthread_mutex_destroy(&context->count_mutex); |
5711 | 0 | blosc2_pthread_mutex_destroy(&context->delta_mutex); |
5712 | 0 | blosc2_pthread_cond_destroy(&context->delta_cv); |
5713 | 0 | } |
5714 | | #if defined(_WIN32) |
5715 | | else if (context->thread_backend == BLOSC_BACKEND_PER_CONTEXT && context->threads_started > 0) { |
5716 | | /* Signal all workers to exit. */ |
5717 | | blosc2_pthread_mutex_lock(&context->jobs_mutex); |
5718 | | context->end_threads = 1; |
5719 | | blosc2_pthread_cond_broadcast(&context->jobs_ready); |
5720 | | blosc2_pthread_mutex_unlock(&context->jobs_mutex); |
5721 | | |
5722 | | for (int32_t t = 0; t < context->threads_started; t++) { |
5723 | | blosc2_pthread_join(context->threads[t], NULL); |
5724 | | } |
5725 | | my_free(context->threads); |
5726 | | context->threads = NULL; |
5727 | | blosc2_pthread_cond_destroy(&context->jobs_done); |
5728 | | blosc2_pthread_cond_destroy(&context->jobs_ready); |
5729 | | blosc2_pthread_mutex_destroy(&context->jobs_mutex); |
5730 | | blosc2_pthread_cond_destroy(&context->delta_cv); |
5731 | | blosc2_pthread_mutex_destroy(&context->delta_mutex); |
5732 | | blosc2_pthread_mutex_destroy(&context->count_mutex); |
5733 | | } |
5734 | | #endif /* _WIN32 */ |
5735 | 64.9k | else if (context->thread_backend == BLOSC_BACKEND_SHARED_POOL && context->thread_pool != NULL) { |
5736 | 0 | struct blosc_shared_pool *pool = context->thread_pool; |
5737 | 0 | struct blosc_shared_pool **prev; |
5738 | 0 | bool destroy_pool = false; |
5739 | |
|
5740 | 0 | if (context->pool_epoch != g_destroy_count) { |
5741 | | /* blosc2_destroy() already freed this pool and tore down pool_registry_mutex. |
5742 | | * Just clear the dangling pointer; nothing else to do. */ |
5743 | 0 | context->thread_pool = NULL; |
5744 | 0 | context->threads_started = 0; |
5745 | 0 | context->thread_backend = BLOSC_BACKEND_SERIAL; |
5746 | 0 | return 0; |
5747 | 0 | } |
5748 | | |
5749 | 0 | blosc2_pthread_mutex_lock(&pool_registry_mutex); |
5750 | 0 | pool->context_refs--; |
5751 | 0 | if (pool->context_refs == 0) { |
5752 | | /* Check pool-internal state under the pool's own mutex */ |
5753 | 0 | blosc2_pthread_mutex_lock(&pool->mutex); |
5754 | 0 | bool idle = (pool->active_jobs == 0 && pool->job_queue_head == NULL); |
5755 | 0 | blosc2_pthread_mutex_unlock(&pool->mutex); |
5756 | 0 | if (idle) { |
5757 | 0 | prev = &shared_pools; |
5758 | 0 | while (*prev != NULL && *prev != pool) { |
5759 | 0 | prev = &(*prev)->next; |
5760 | 0 | } |
5761 | 0 | if (*prev == pool) { |
5762 | 0 | *prev = pool->next; |
5763 | 0 | } |
5764 | 0 | destroy_pool = true; |
5765 | 0 | } |
5766 | 0 | } |
5767 | 0 | blosc2_pthread_mutex_unlock(&pool_registry_mutex); |
5768 | 0 | if (destroy_pool) { |
5769 | 0 | destroy_shared_pool(pool); |
5770 | 0 | } |
5771 | 0 | context->thread_pool = NULL; |
5772 | 0 | } |
5773 | | |
5774 | 64.9k | context->threads_started = 0; |
5775 | 64.9k | context->thread_backend = BLOSC_BACKEND_SERIAL; |
5776 | 64.9k | return 0; |
5777 | 64.9k | } |
5778 | | |
5779 | 0 | static int parallel_blosc(blosc2_context* context) { |
5780 | | #if defined(_WIN32) |
5781 | | /* Windows: per-context threads using WAIT_INIT/WAIT_FINISH barriers. |
5782 | | * No job-group struct; workers read/write context fields directly. */ |
5783 | | context->job = NULL; |
5784 | | context->thread_giveup_code = 1; |
5785 | | context->thread_nblock = -1; |
5786 | | |
5787 | | if (context->thread_backend == BLOSC_BACKEND_CALLBACK) { |
5788 | | threads_callback(threads_callback_data, t_blosc_do_job, |
5789 | | context->nthreads, sizeof(struct thread_context), (void*) context->thread_contexts); |
5790 | | } |
5791 | | else { |
5792 | | /* BLOSC_BACKEND_PER_CONTEXT: post job to sleeping workers and wait. */ |
5793 | | blosc2_pthread_mutex_lock(&context->jobs_mutex); |
5794 | | context->active_workers = context->nthreads; |
5795 | | context->job_seq++; |
5796 | | blosc2_pthread_cond_broadcast(&context->jobs_ready); |
5797 | | while (context->active_workers > 0) { |
5798 | | blosc2_pthread_cond_wait(&context->jobs_done, &context->jobs_mutex); |
5799 | | } |
5800 | | blosc2_pthread_mutex_unlock(&context->jobs_mutex); |
5801 | | } |
5802 | | |
5803 | | if (context->thread_giveup_code <= 0) { |
5804 | | return context->thread_giveup_code; |
5805 | | } |
5806 | | return (int)context->output_bytes; |
5807 | | |
5808 | | #else /* !_WIN32 */ |
5809 | |
|
5810 | 0 | struct blosc_job_group job; |
5811 | |
|
5812 | 0 | job_group_init(&job, context); |
5813 | 0 | context->job = &job; |
5814 | |
|
5815 | 0 | if (context->thread_backend == BLOSC_BACKEND_CALLBACK) { |
5816 | 0 | blosc2_pthread_mutex_lock(&job.mutex); |
5817 | 0 | job.active_workers = context->nthreads; |
5818 | 0 | job.pending_workers = 0; |
5819 | 0 | blosc2_pthread_mutex_unlock(&job.mutex); |
5820 | 0 | threads_callback(threads_callback_data, t_blosc_do_job, |
5821 | 0 | context->nthreads, sizeof(struct thread_context), (void*) context->thread_contexts); |
5822 | 0 | } |
5823 | 0 | else { |
5824 | 0 | struct blosc_shared_pool *pool = context->thread_pool; |
5825 | 0 | blosc2_pthread_mutex_lock(&pool->mutex); |
5826 | 0 | int32_t enqueued = 0; |
5827 | | /* More workers than blocks cannot add useful parallel work. */ |
5828 | 0 | int32_t workers = context->nthreads < context->nblocks ? context->nthreads : context->nblocks; |
5829 | 0 | for (int32_t tid = 0; tid < workers; ++tid) { |
5830 | 0 | struct blosc_job_queue_entry *entry = (struct blosc_job_queue_entry *)my_malloc(sizeof(*entry)); |
5831 | 0 | if (entry == NULL) { |
5832 | | /* Drain already-enqueued entries so they don't reference the |
5833 | | stack-allocated job after we return. */ |
5834 | 0 | struct blosc_job_queue_entry **pp = &pool->job_queue_head; |
5835 | 0 | while (*pp != NULL) { |
5836 | 0 | if ((*pp)->job == &job) { |
5837 | 0 | struct blosc_job_queue_entry *victim = *pp; |
5838 | 0 | *pp = victim->next; |
5839 | 0 | my_free(victim); |
5840 | 0 | pool->active_jobs--; |
5841 | 0 | } else { |
5842 | 0 | pp = &(*pp)->next; |
5843 | 0 | } |
5844 | 0 | } |
5845 | 0 | pool->job_queue_tail = NULL; |
5846 | | /* Recompute tail */ |
5847 | 0 | for (struct blosc_job_queue_entry *e = pool->job_queue_head; e != NULL; e = e->next) { |
5848 | 0 | pool->job_queue_tail = e; |
5849 | 0 | } |
5850 | 0 | blosc2_pthread_mutex_unlock(&pool->mutex); |
5851 | 0 | context->job = NULL; |
5852 | 0 | job_group_destroy(&job); |
5853 | 0 | return BLOSC2_ERROR_MEMORY_ALLOC; |
5854 | 0 | } |
5855 | 0 | memset(entry, 0, sizeof(*entry)); |
5856 | 0 | entry->job = &job; |
5857 | 0 | entry->logical_tid = tid; |
5858 | 0 | if (pool->job_queue_tail != NULL) { |
5859 | 0 | pool->job_queue_tail->next = entry; |
5860 | 0 | } |
5861 | 0 | else { |
5862 | 0 | pool->job_queue_head = entry; |
5863 | 0 | } |
5864 | 0 | pool->job_queue_tail = entry; |
5865 | 0 | pool->active_jobs++; |
5866 | 0 | enqueued++; |
5867 | 0 | } |
5868 | | /* Set active_workers to the actual number enqueued */ |
5869 | 0 | blosc2_pthread_mutex_lock(&job.mutex); |
5870 | 0 | job.active_workers = enqueued; |
5871 | 0 | job.pending_workers = enqueued; |
5872 | 0 | blosc2_pthread_mutex_unlock(&job.mutex); |
5873 | | /* Avoid waking idle pool workers that have no queue entry for this job. */ |
5874 | 0 | if (enqueued < pool->nthreads) { |
5875 | 0 | for (int32_t i = 0; i < enqueued; ++i) { |
5876 | 0 | blosc2_pthread_cond_signal(&pool->work_cv); |
5877 | 0 | } |
5878 | 0 | } |
5879 | 0 | else { |
5880 | 0 | blosc2_pthread_cond_broadcast(&pool->work_cv); |
5881 | 0 | } |
5882 | 0 | blosc2_pthread_mutex_unlock(&pool->mutex); |
5883 | |
|
5884 | 0 | blosc2_pthread_mutex_lock(&job.mutex); |
5885 | 0 | while (!job.completed) { |
5886 | 0 | blosc2_pthread_cond_wait(&job.completion_cv, &job.mutex); |
5887 | 0 | } |
5888 | 0 | blosc2_pthread_mutex_unlock(&job.mutex); |
5889 | 0 | } |
5890 | | |
5891 | 0 | context->job = NULL; |
5892 | 0 | context->output_bytes = job.output_bytes; |
5893 | 0 | context->thread_giveup_code = job.giveup_code; |
5894 | 0 | job_group_destroy(&job); |
5895 | |
|
5896 | 0 | if (context->thread_giveup_code <= 0) { |
5897 | 0 | return context->thread_giveup_code; |
5898 | 0 | } |
5899 | 0 | return (int)context->output_bytes; |
5900 | |
|
5901 | 0 | #endif /* _WIN32 */ |
5902 | 0 | } |
5903 | | |
5904 | | int16_t blosc2_get_nthreads(void) |
5905 | 18.0k | { |
5906 | 18.0k | int16_t nthreads; |
5907 | 18.0k | if (!g_initlib) { |
5908 | 0 | blosc2_init(); |
5909 | 0 | } |
5910 | 18.0k | blosc2_pthread_mutex_lock(&global_comp_mutex); |
5911 | 18.0k | nthreads = g_nthreads; |
5912 | 18.0k | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
5913 | 18.0k | return nthreads; |
5914 | 18.0k | } |
5915 | | |
5916 | 44.0k | int16_t blosc2_set_nthreads(int16_t nthreads) { |
5917 | 44.0k | int16_t ret; /* the previous number of threads */ |
5918 | | |
5919 | | /* Check whether the library should be initialized */ |
5920 | 44.0k | if (!g_initlib) blosc2_init(); |
5921 | | |
5922 | 44.0k | blosc2_pthread_mutex_lock(&global_comp_mutex); |
5923 | 44.0k | ret = g_nthreads; |
5924 | 44.0k | if (nthreads != ret) { |
5925 | 0 | int16_t old_new_nthreads = g_global_context->new_nthreads; |
5926 | 0 | int16_t old_nthreads = g_global_context->nthreads; |
5927 | 0 | g_nthreads = nthreads; |
5928 | 0 | g_global_context->new_nthreads = nthreads; |
5929 | 0 | int16_t ret2 = check_nthreads(g_global_context, true); |
5930 | 0 | if (ret2 < 0) { |
5931 | 0 | g_nthreads = ret; |
5932 | 0 | g_global_context->new_nthreads = old_new_nthreads; |
5933 | 0 | g_global_context->nthreads = old_nthreads; |
5934 | 0 | check_nthreads(g_global_context, true); |
5935 | 0 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
5936 | 0 | return ret2; |
5937 | 0 | } |
5938 | 0 | } |
5939 | 44.0k | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
5940 | | |
5941 | 44.0k | return ret; |
5942 | 44.0k | } |
5943 | | |
5944 | | |
5945 | | const char* blosc1_get_compressor(void) |
5946 | 0 | { |
5947 | 0 | const char* compname; |
5948 | 0 | if (!g_initlib) { |
5949 | 0 | blosc2_init(); |
5950 | 0 | } |
5951 | 0 | blosc2_pthread_mutex_lock(&global_comp_mutex); |
5952 | 0 | blosc2_compcode_to_compname(g_compressor, &compname); |
5953 | 0 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
5954 | |
|
5955 | 0 | return compname; |
5956 | 0 | } |
5957 | | |
5958 | 21.0k | int blosc1_set_compressor(const char* compname) { |
5959 | 21.0k | int code = blosc2_compname_to_compcode(compname); |
5960 | 21.0k | if (code >= BLOSC_LAST_CODEC) { |
5961 | 0 | BLOSC_TRACE_ERROR("User defined codecs cannot be set here. Use Blosc2 mechanism instead."); |
5962 | 0 | BLOSC_ERROR(BLOSC2_ERROR_CODEC_SUPPORT); |
5963 | 0 | } |
5964 | | /* Check whether the library should be initialized */ |
5965 | 21.0k | if (!g_initlib) blosc2_init(); |
5966 | | |
5967 | 21.0k | blosc2_pthread_mutex_lock(&global_comp_mutex); |
5968 | 21.0k | g_compressor = code; |
5969 | 21.0k | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
5970 | | |
5971 | 21.0k | return code; |
5972 | 21.0k | } |
5973 | | |
5974 | 0 | void blosc2_set_delta(int dodelta) { |
5975 | | /* Check whether the library should be initialized */ |
5976 | 0 | if (!g_initlib) blosc2_init(); |
5977 | |
|
5978 | 0 | blosc2_pthread_mutex_lock(&global_comp_mutex); |
5979 | 0 | g_delta = dodelta; |
5980 | 0 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
5981 | 0 | } |
5982 | | |
5983 | 0 | const char* blosc2_list_compressors(void) { |
5984 | 0 | static int compressors_list_done = 0; |
5985 | 0 | static char ret[256]; |
5986 | |
|
5987 | 0 | if (compressors_list_done) return ret; |
5988 | 0 | ret[0] = '\0'; |
5989 | 0 | size_t _avail = sizeof(ret); |
5990 | 0 | char *_p = ret; |
5991 | | /* Start with the first compressor name. */ |
5992 | 0 | int _sw = snprintf(_p, _avail, "%s", BLOSC_BLOSCLZ_COMPNAME); |
5993 | 0 | if (_sw < 0) { |
5994 | 0 | ret[0] = '\0'; |
5995 | 0 | compressors_list_done = 1; |
5996 | 0 | return ret; |
5997 | 0 | } |
5998 | 0 | if ((size_t)_sw >= _avail) { |
5999 | | /* Truncated; ensure terminated and return */ |
6000 | 0 | ret[sizeof(ret)-1] = '\0'; |
6001 | 0 | compressors_list_done = 1; |
6002 | 0 | return ret; |
6003 | 0 | } |
6004 | 0 | _p += _sw; _avail -= (size_t)_sw; |
6005 | |
|
6006 | 0 | #define APPEND_COMP(NAME) do { \ |
6007 | 0 | if (_avail > 1) { \ |
6008 | 0 | int __w = snprintf(_p, _avail, ",%s", (NAME)); \ |
6009 | 0 | if (__w < 0) { break; } \ |
6010 | 0 | if ((size_t)__w >= _avail) { _p += _avail - 1; _avail = 1; *_p = '\0'; break; } \ |
6011 | 0 | _p += __w; _avail -= (size_t)__w; \ |
6012 | 0 | } \ |
6013 | 0 | } while(0) |
6014 | |
|
6015 | 0 | APPEND_COMP(BLOSC_LZ4_COMPNAME); |
6016 | 0 | APPEND_COMP(BLOSC_LZ4HC_COMPNAME); |
6017 | 0 | #if defined(HAVE_ZLIB) |
6018 | 0 | APPEND_COMP(BLOSC_ZLIB_COMPNAME); |
6019 | 0 | #endif /* HAVE_ZLIB */ |
6020 | 0 | #if defined(HAVE_ZSTD) |
6021 | 0 | APPEND_COMP(BLOSC_ZSTD_COMPNAME); |
6022 | 0 | #endif /* HAVE_ZSTD */ |
6023 | 0 | #undef APPEND_COMP |
6024 | 0 | compressors_list_done = 1; |
6025 | 0 | return ret; |
6026 | 0 | } |
6027 | | |
6028 | | |
6029 | 0 | const char* blosc2_get_version_string(void) { |
6030 | 0 | return BLOSC2_VERSION_STRING; |
6031 | 0 | } |
6032 | | |
6033 | | |
6034 | 0 | int blosc2_get_complib_info(const char* compname, char** complib, char** version) { |
6035 | 0 | int clibcode; |
6036 | 0 | const char* clibname; |
6037 | 0 | const char* clibversion = "unknown"; |
6038 | 0 | char sbuffer[256]; |
6039 | |
|
6040 | 0 | clibcode = compname_to_clibcode(compname); |
6041 | 0 | clibname = clibcode_to_clibname(clibcode); |
6042 | | |
6043 | | /* complib version */ |
6044 | 0 | if (clibcode == BLOSC_BLOSCLZ_LIB) { |
6045 | 0 | clibversion = BLOSCLZ_VERSION_STRING; |
6046 | 0 | } |
6047 | 0 | else if (clibcode == BLOSC_LZ4_LIB) { |
6048 | 0 | int _sn = snprintf(sbuffer, sizeof(sbuffer), "%d.%d.%d", |
6049 | 0 | LZ4_VERSION_MAJOR, LZ4_VERSION_MINOR, LZ4_VERSION_RELEASE); |
6050 | 0 | if (_sn >= 0 && (size_t)_sn < sizeof(sbuffer)) { |
6051 | 0 | clibversion = sbuffer; |
6052 | 0 | } |
6053 | 0 | } |
6054 | 0 | #if defined(HAVE_ZLIB) |
6055 | 0 | else if (clibcode == BLOSC_ZLIB_LIB) { |
6056 | | #ifdef ZLIB_COMPAT |
6057 | | clibversion = ZLIB_VERSION; |
6058 | | #elif defined(HAVE_ZLIB_NG) |
6059 | 0 | clibversion = ZLIBNG_VERSION; |
6060 | | #else |
6061 | | clibversion = ZLIB_VERSION; |
6062 | | #endif |
6063 | 0 | } |
6064 | 0 | #endif /* HAVE_ZLIB */ |
6065 | 0 | #if defined(HAVE_ZSTD) |
6066 | 0 | else if (clibcode == BLOSC_ZSTD_LIB) { |
6067 | 0 | int _sn_zstd = snprintf(sbuffer, sizeof(sbuffer), "%d.%d.%d", |
6068 | 0 | ZSTD_VERSION_MAJOR, ZSTD_VERSION_MINOR, ZSTD_VERSION_RELEASE); |
6069 | 0 | if (_sn_zstd >= 0 && (size_t)_sn_zstd < sizeof(sbuffer)) { |
6070 | 0 | clibversion = sbuffer; |
6071 | 0 | } |
6072 | 0 | } |
6073 | 0 | #endif /* HAVE_ZSTD */ |
6074 | |
|
6075 | | #ifdef _MSC_VER |
6076 | | *complib = _strdup(clibname); |
6077 | | *version = _strdup(clibversion); |
6078 | | #else |
6079 | 0 | *complib = strdup(clibname); |
6080 | 0 | *version = strdup(clibversion); |
6081 | 0 | #endif |
6082 | 0 | return clibcode; |
6083 | 0 | } |
6084 | | |
6085 | | /* Return `nbytes`, `cbytes` and `blocksize` from a compressed buffer. */ |
6086 | 28.1k | void blosc1_cbuffer_sizes(const void* cbuffer, size_t* nbytes, size_t* cbytes, size_t* blocksize) { |
6087 | 28.1k | int32_t nbytes32, cbytes32, blocksize32; |
6088 | 28.1k | blosc2_cbuffer_sizes(cbuffer, &nbytes32, &cbytes32, &blocksize32); |
6089 | 28.1k | *nbytes = nbytes32; |
6090 | 28.1k | *cbytes = cbytes32; |
6091 | 28.1k | *blocksize = blocksize32; |
6092 | 28.1k | } |
6093 | | |
6094 | 22.0M | int blosc2_cbuffer_sizes(const void* cbuffer, int32_t* nbytes, int32_t* cbytes, int32_t* blocksize) { |
6095 | 22.0M | blosc_header header; |
6096 | 22.0M | int rc = read_chunk_header((uint8_t *) cbuffer, BLOSC_MIN_HEADER_LENGTH, false, &header); |
6097 | 22.0M | if (rc < 0) { |
6098 | | /* Return zeros if error reading header */ |
6099 | 99 | memset(&header, 0, sizeof(header)); |
6100 | 99 | } |
6101 | | |
6102 | | /* Read the interesting values */ |
6103 | 22.0M | if (nbytes != NULL) |
6104 | 22.0M | *nbytes = header.nbytes; |
6105 | 22.0M | if (cbytes != NULL) |
6106 | 22.0M | *cbytes = header.cbytes; |
6107 | 22.0M | if (blocksize != NULL) |
6108 | 53.4k | *blocksize = header.blocksize; |
6109 | 22.0M | return rc; |
6110 | 22.0M | } |
6111 | | |
6112 | 16.7k | int blosc1_cbuffer_validate(const void* cbuffer, size_t cbytes, size_t* nbytes) { |
6113 | 16.7k | int32_t header_cbytes; |
6114 | 16.7k | int32_t header_nbytes; |
6115 | 16.7k | if (cbytes < BLOSC_MIN_HEADER_LENGTH) { |
6116 | | /* Compressed data should contain enough space for header */ |
6117 | 0 | *nbytes = 0; |
6118 | 0 | return BLOSC2_ERROR_WRITE_BUFFER; |
6119 | 0 | } |
6120 | 16.7k | int rc = blosc2_cbuffer_sizes(cbuffer, &header_nbytes, &header_cbytes, NULL); |
6121 | 16.7k | if (rc < 0) { |
6122 | 0 | *nbytes = 0; |
6123 | 0 | return rc; |
6124 | 0 | } |
6125 | 16.7k | *nbytes = header_nbytes; |
6126 | 16.7k | if (header_cbytes != (int32_t)cbytes) { |
6127 | | /* Compressed size from header does not match `cbytes` */ |
6128 | 0 | *nbytes = 0; |
6129 | 0 | return BLOSC2_ERROR_INVALID_HEADER; |
6130 | 0 | } |
6131 | 16.7k | if (*nbytes > BLOSC2_MAX_BUFFERSIZE) { |
6132 | | /* Uncompressed size is larger than allowed */ |
6133 | 47 | *nbytes = 0; |
6134 | 47 | return BLOSC2_ERROR_MEMORY_ALLOC; |
6135 | 47 | } |
6136 | 16.7k | return 0; |
6137 | 16.7k | } |
6138 | | |
6139 | | /* Return `typesize` and `flags` from a compressed buffer. */ |
6140 | 0 | void blosc1_cbuffer_metainfo(const void* cbuffer, size_t* typesize, int* flags) { |
6141 | 0 | blosc_header header; |
6142 | 0 | int rc = read_chunk_header((uint8_t *) cbuffer, BLOSC_MIN_HEADER_LENGTH, false, &header); |
6143 | 0 | if (rc < 0) { |
6144 | 0 | *typesize = *flags = 0; |
6145 | 0 | return; |
6146 | 0 | } |
6147 | | |
6148 | | /* Read the interesting values */ |
6149 | 0 | *flags = header.flags; |
6150 | 0 | *typesize = header.typesize; |
6151 | 0 | } |
6152 | | |
6153 | | |
6154 | | /* Return version information from a compressed buffer. */ |
6155 | 0 | void blosc2_cbuffer_versions(const void* cbuffer, int* version, int* versionlz) { |
6156 | 0 | blosc_header header; |
6157 | 0 | int rc = read_chunk_header((uint8_t *) cbuffer, BLOSC_MIN_HEADER_LENGTH, false, &header); |
6158 | 0 | if (rc < 0) { |
6159 | 0 | *version = *versionlz = 0; |
6160 | 0 | return; |
6161 | 0 | } |
6162 | | |
6163 | | /* Read the version info */ |
6164 | 0 | *version = header.version; |
6165 | 0 | *versionlz = header.versionlz; |
6166 | 0 | } |
6167 | | |
6168 | | |
6169 | | /* Return the compressor library/format used in a compressed buffer. */ |
6170 | 0 | const char* blosc2_cbuffer_complib(const void* cbuffer) { |
6171 | 0 | blosc_header header; |
6172 | 0 | int clibcode; |
6173 | 0 | const char* complib; |
6174 | 0 | int rc = read_chunk_header((uint8_t *) cbuffer, BLOSC_MIN_HEADER_LENGTH, false, &header); |
6175 | 0 | if (rc < 0) { |
6176 | 0 | return NULL; |
6177 | 0 | } |
6178 | | |
6179 | | /* Read the compressor format/library info */ |
6180 | 0 | clibcode = (header.flags & 0xe0) >> 5; |
6181 | 0 | complib = clibcode_to_clibname(clibcode); |
6182 | 0 | return complib; |
6183 | 0 | } |
6184 | | |
6185 | | |
6186 | | /* Get the internal blocksize to be used during compression. 0 means |
6187 | | that an automatic blocksize is computed internally. */ |
6188 | | int blosc1_get_blocksize(void) |
6189 | 0 | { |
6190 | 0 | int blocksize; |
6191 | 0 | if (!g_initlib) { |
6192 | 0 | blosc2_init(); |
6193 | 0 | } |
6194 | 0 | blosc2_pthread_mutex_lock(&global_comp_mutex); |
6195 | 0 | blocksize = (int)g_force_blocksize; |
6196 | 0 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
6197 | 0 | return blocksize; |
6198 | 0 | } |
6199 | | |
6200 | | |
6201 | | /* Force the use of a specific blocksize. If 0, an automatic |
6202 | | blocksize will be used (the default). */ |
6203 | 3.82k | void blosc1_set_blocksize(size_t blocksize) { |
6204 | 3.82k | if (!g_initlib) { |
6205 | 0 | blosc2_init(); |
6206 | 0 | } |
6207 | 3.82k | blosc2_pthread_mutex_lock(&global_comp_mutex); |
6208 | 3.82k | g_force_blocksize = (int32_t)blocksize; |
6209 | 3.82k | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
6210 | 3.82k | } |
6211 | | |
6212 | | |
6213 | | /* Force the use of a specific split mode. */ |
6214 | | void blosc1_set_splitmode(int mode) |
6215 | 0 | { |
6216 | 0 | if (!g_initlib) { |
6217 | 0 | blosc2_init(); |
6218 | 0 | } |
6219 | 0 | blosc2_pthread_mutex_lock(&global_comp_mutex); |
6220 | 0 | g_splitmode = mode; |
6221 | 0 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
6222 | 0 | } |
6223 | | |
6224 | | |
6225 | | /* Set pointer to super-chunk. If NULL, no super-chunk will be |
6226 | | reachable (the default). */ |
6227 | 0 | void blosc_set_schunk(blosc2_schunk* schunk) { |
6228 | 0 | if (!g_initlib) { |
6229 | 0 | blosc2_init(); |
6230 | 0 | } |
6231 | 0 | blosc2_pthread_mutex_lock(&global_comp_mutex); |
6232 | 0 | g_schunk = schunk; |
6233 | 0 | if (g_global_context != NULL) { |
6234 | 0 | g_global_context->schunk = schunk; |
6235 | 0 | } |
6236 | 0 | blosc2_pthread_mutex_unlock(&global_comp_mutex); |
6237 | 0 | } |
6238 | | |
6239 | | blosc2_io *blosc2_io_global = NULL; |
6240 | | blosc2_io_cb BLOSC2_IO_CB_DEFAULTS; |
6241 | | blosc2_io_cb BLOSC2_IO_CB_MMAP; |
6242 | | |
6243 | | int _blosc2_register_io_cb(const blosc2_io_cb *io); |
6244 | | |
6245 | 29.0k | void blosc2_init(void) { |
6246 | | /* Return if Blosc is already initialized */ |
6247 | 29.0k | if (g_initlib) return; |
6248 | | |
6249 | 29.0k | BLOSC2_IO_CB_DEFAULTS.id = BLOSC2_IO_FILESYSTEM; |
6250 | 29.0k | BLOSC2_IO_CB_DEFAULTS.name = "filesystem"; |
6251 | 29.0k | BLOSC2_IO_CB_DEFAULTS.is_allocation_necessary = true; |
6252 | 29.0k | BLOSC2_IO_CB_DEFAULTS.open = (blosc2_open_cb) blosc2_stdio_open; |
6253 | 29.0k | BLOSC2_IO_CB_DEFAULTS.close = (blosc2_close_cb) blosc2_stdio_close; |
6254 | 29.0k | BLOSC2_IO_CB_DEFAULTS.size = (blosc2_size_cb) blosc2_stdio_size; |
6255 | 29.0k | BLOSC2_IO_CB_DEFAULTS.write = (blosc2_write_cb) blosc2_stdio_write; |
6256 | 29.0k | BLOSC2_IO_CB_DEFAULTS.read = (blosc2_read_cb) blosc2_stdio_read; |
6257 | 29.0k | BLOSC2_IO_CB_DEFAULTS.truncate = (blosc2_truncate_cb) blosc2_stdio_truncate; |
6258 | 29.0k | BLOSC2_IO_CB_DEFAULTS.destroy = (blosc2_destroy_cb) blosc2_stdio_destroy; |
6259 | | |
6260 | 29.0k | _blosc2_register_io_cb(&BLOSC2_IO_CB_DEFAULTS); |
6261 | | |
6262 | 29.0k | BLOSC2_IO_CB_MMAP.id = BLOSC2_IO_FILESYSTEM_MMAP; |
6263 | 29.0k | BLOSC2_IO_CB_MMAP.name = "filesystem_mmap"; |
6264 | 29.0k | BLOSC2_IO_CB_MMAP.is_allocation_necessary = false; |
6265 | 29.0k | BLOSC2_IO_CB_MMAP.open = (blosc2_open_cb) blosc2_stdio_mmap_open; |
6266 | 29.0k | BLOSC2_IO_CB_MMAP.close = (blosc2_close_cb) blosc2_stdio_mmap_close; |
6267 | 29.0k | BLOSC2_IO_CB_MMAP.read = (blosc2_read_cb) blosc2_stdio_mmap_read; |
6268 | 29.0k | BLOSC2_IO_CB_MMAP.size = (blosc2_size_cb) blosc2_stdio_mmap_size; |
6269 | 29.0k | BLOSC2_IO_CB_MMAP.write = (blosc2_write_cb) blosc2_stdio_mmap_write; |
6270 | 29.0k | BLOSC2_IO_CB_MMAP.truncate = (blosc2_truncate_cb) blosc2_stdio_mmap_truncate; |
6271 | 29.0k | BLOSC2_IO_CB_MMAP.destroy = (blosc2_destroy_cb) blosc2_stdio_mmap_destroy; |
6272 | | |
6273 | 29.0k | _blosc2_register_io_cb(&BLOSC2_IO_CB_MMAP); |
6274 | | |
6275 | 29.0k | g_ncodecs = 0; |
6276 | 29.0k | g_nfilters = 0; |
6277 | 29.0k | g_ntuners = 0; |
6278 | | |
6279 | 29.0k | #if defined(HAVE_PLUGINS) |
6280 | 29.0k | #include "blosc2/blosc2-common.h" |
6281 | 29.0k | #include "blosc2/blosc2-stdio.h" |
6282 | 29.0k | register_codecs(); |
6283 | 29.0k | register_filters(); |
6284 | 29.0k | register_tuners(); |
6285 | 29.0k | #endif |
6286 | 29.0k | blosc2_pthread_mutex_init(&global_comp_mutex, NULL); |
6287 | 29.0k | blosc2_pthread_mutex_init(&pool_registry_mutex, NULL); |
6288 | | /* Create a global context */ |
6289 | 29.0k | g_global_context = (blosc2_context*)my_malloc(sizeof(blosc2_context)); |
6290 | 29.0k | memset(g_global_context, 0, sizeof(blosc2_context)); |
6291 | 29.0k | g_global_context->nthreads = g_nthreads; |
6292 | 29.0k | g_global_context->new_nthreads = g_nthreads; |
6293 | 29.0k | blosc2_pthread_mutex_init(&g_global_context->nchunk_mutex, NULL); |
6294 | 29.0k | g_initlib = 1; |
6295 | 29.0k | } |
6296 | | |
6297 | | |
6298 | 29.0k | int blosc2_free_resources(void) { |
6299 | | /* Return if Blosc is not initialized */ |
6300 | 29.0k | if (!g_initlib) return BLOSC2_ERROR_FAILURE; |
6301 | | |
6302 | 29.0k | return release_thread_backend(g_global_context); |
6303 | 29.0k | } |
6304 | | |
6305 | | |
6306 | 29.0k | void blosc2_destroy(void) { |
6307 | | /* Return if Blosc is not initialized */ |
6308 | 29.0k | if (!g_initlib) return; |
6309 | | |
6310 | 29.0k | blosc2_free_resources(); |
6311 | 29.0k | g_initlib = 0; |
6312 | 29.0k | blosc2_free_ctx(g_global_context); |
6313 | | |
6314 | | /* Bump the epoch so any live context can detect its pool is now stale. |
6315 | | * Do this before freeing the pools so that release_thread_backend callers |
6316 | | * that race with us will take the "skip" path rather than locking the |
6317 | | * soon-to-be-destroyed pool_registry_mutex. */ |
6318 | 29.0k | g_destroy_count++; |
6319 | | |
6320 | | /* Tear down any remaining shared pools */ |
6321 | 29.0k | struct blosc_shared_pool *pool = shared_pools; |
6322 | 29.0k | while (pool != NULL) { |
6323 | 0 | struct blosc_shared_pool *next = pool->next; |
6324 | 0 | destroy_shared_pool(pool); |
6325 | 0 | pool = next; |
6326 | 0 | } |
6327 | 29.0k | shared_pools = NULL; |
6328 | 29.0k | blosc2_pthread_mutex_destroy(&pool_registry_mutex); |
6329 | | |
6330 | 29.0k | blosc2_pthread_mutex_destroy(&global_comp_mutex); |
6331 | 29.0k | } |
6332 | | |
6333 | | |
6334 | | /* Contexts */ |
6335 | | |
6336 | | /* Create a context for compression */ |
6337 | 14.9k | blosc2_context* blosc2_create_cctx(blosc2_cparams cparams) { |
6338 | 14.9k | blosc2_context* context = (blosc2_context*)my_malloc(sizeof(blosc2_context)); |
6339 | 14.9k | BLOSC_ERROR_NULL(context, NULL); |
6340 | | |
6341 | | /* Populate the context, using zeros as default values */ |
6342 | 14.9k | memset(context, 0, sizeof(blosc2_context)); |
6343 | 14.9k | context->do_compress = 1; /* meant for compression */ |
6344 | 14.9k | context->use_dict = cparams.use_dict; |
6345 | 14.9k | if (cparams.instr_codec) { |
6346 | 0 | context->blosc2_flags = BLOSC2_INSTR_CODEC; |
6347 | 0 | } |
6348 | | |
6349 | 104k | for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
6350 | 89.3k | context->filters[i] = cparams.filters[i]; |
6351 | 89.3k | context->filters_meta[i] = cparams.filters_meta[i]; |
6352 | | |
6353 | 89.3k | if (context->filters[i] >= BLOSC_LAST_FILTER && context->filters[i] <= BLOSC2_DEFINED_FILTERS_STOP) { |
6354 | 1 | BLOSC_TRACE_ERROR("filter (%d) is not yet defined", |
6355 | 1 | context->filters[i]); |
6356 | 1 | free(context); |
6357 | 1 | return NULL; |
6358 | 1 | } |
6359 | 89.3k | if (context->filters[i] > BLOSC_LAST_REGISTERED_FILTER && context->filters[i] <= BLOSC2_GLOBAL_REGISTERED_FILTERS_STOP) { |
6360 | 1 | BLOSC_TRACE_ERROR("filter (%d) is not yet defined", |
6361 | 1 | context->filters[i]); |
6362 | 1 | free(context); |
6363 | 1 | return NULL; |
6364 | 1 | } |
6365 | 89.3k | } |
6366 | | |
6367 | 14.8k | #if defined(HAVE_ZFP) |
6368 | 14.8k | #include "blosc2/codecs-registry.h" |
6369 | 14.8k | if ((context->compcode >= BLOSC_CODEC_ZFP_FIXED_ACCURACY) && (context->compcode <= BLOSC_CODEC_ZFP_FIXED_RATE)) { |
6370 | 0 | for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) { |
6371 | 0 | if ((context->filters[i] == BLOSC_SHUFFLE) || (context->filters[i] == BLOSC_BITSHUFFLE)) { |
6372 | 0 | BLOSC_TRACE_ERROR("ZFP cannot be run in presence of SHUFFLE / BITSHUFFLE"); |
6373 | 0 | return NULL; |
6374 | 0 | } |
6375 | 0 | } |
6376 | 0 | } |
6377 | 14.8k | #endif /* HAVE_ZFP */ |
6378 | | |
6379 | | /* Check for a BLOSC_SHUFFLE environment variable */ |
6380 | 14.8k | int doshuffle = -1; |
6381 | 14.8k | char* envvar = getenv("BLOSC_SHUFFLE"); |
6382 | 14.8k | if (envvar != NULL) { |
6383 | 0 | if (strcmp(envvar, "NOSHUFFLE") == 0) { |
6384 | 0 | doshuffle = BLOSC_NOSHUFFLE; |
6385 | 0 | } |
6386 | 0 | else if (strcmp(envvar, "SHUFFLE") == 0) { |
6387 | 0 | doshuffle = BLOSC_SHUFFLE; |
6388 | 0 | } |
6389 | 0 | else if (strcmp(envvar, "BITSHUFFLE") == 0) { |
6390 | 0 | doshuffle = BLOSC_BITSHUFFLE; |
6391 | 0 | } |
6392 | 0 | else { |
6393 | 0 | BLOSC_TRACE_WARNING("BLOSC_SHUFFLE environment variable '%s' not recognized\n", envvar); |
6394 | 0 | } |
6395 | 0 | } |
6396 | | /* Check for a BLOSC_DELTA environment variable */ |
6397 | 14.8k | int dodelta = BLOSC_NOFILTER; |
6398 | 14.8k | envvar = getenv("BLOSC_DELTA"); |
6399 | 14.8k | if (envvar != NULL) { |
6400 | 0 | if (strcmp(envvar, "1") == 0) { |
6401 | 0 | dodelta = BLOSC_DELTA; |
6402 | 0 | } else if (strcmp(envvar, "0") == 0){ |
6403 | 0 | dodelta = BLOSC_NOFILTER; |
6404 | 0 | } |
6405 | 0 | else { |
6406 | 0 | BLOSC_TRACE_WARNING("BLOSC_DELTA environment variable '%s' not recognized\n", envvar); |
6407 | 0 | } |
6408 | 0 | } |
6409 | | /* Check for a BLOSC_TYPESIZE environment variable */ |
6410 | 14.8k | context->typesize = cparams.typesize; |
6411 | 14.8k | envvar = getenv("BLOSC_TYPESIZE"); |
6412 | 14.8k | if (envvar != NULL) { |
6413 | 0 | int32_t value; |
6414 | 0 | errno = 0; /* To distinguish success/failure after call */ |
6415 | 0 | value = (int32_t) strtol(envvar, NULL, 10); |
6416 | 0 | if ((errno != EINVAL) && (value > 0)) { |
6417 | 0 | context->typesize = value; |
6418 | 0 | } |
6419 | 0 | else { |
6420 | 0 | BLOSC_TRACE_WARNING("BLOSC_TYPESIZE environment variable '%s' not recognized\n", envvar); |
6421 | 0 | } |
6422 | 0 | } |
6423 | 14.8k | build_filters(doshuffle, dodelta, context->typesize, context->filters); |
6424 | | |
6425 | 14.8k | context->clevel = cparams.clevel; |
6426 | | /* Check for a BLOSC_CLEVEL environment variable */ |
6427 | 14.8k | envvar = getenv("BLOSC_CLEVEL"); |
6428 | 14.8k | if (envvar != NULL) { |
6429 | 0 | int value; |
6430 | 0 | errno = 0; /* To distinguish success/failure after call */ |
6431 | 0 | value = (int)strtol(envvar, NULL, 10); |
6432 | 0 | if ((errno != EINVAL) && (value >= 0)) { |
6433 | 0 | context->clevel = value; |
6434 | 0 | } |
6435 | 0 | else { |
6436 | 0 | BLOSC_TRACE_WARNING("BLOSC_CLEVEL environment variable '%s' not recognized\n", envvar); |
6437 | 0 | } |
6438 | 0 | } |
6439 | | |
6440 | 14.8k | context->compcode = cparams.compcode; |
6441 | | /* Check for a BLOSC_COMPRESSOR environment variable */ |
6442 | 14.8k | envvar = getenv("BLOSC_COMPRESSOR"); |
6443 | 14.8k | if (envvar != NULL) { |
6444 | 0 | int codec = blosc2_compname_to_compcode(envvar); |
6445 | 0 | if (codec >= BLOSC_LAST_CODEC) { |
6446 | 0 | BLOSC_TRACE_ERROR("User defined codecs cannot be set here. Use Blosc2 mechanism instead."); |
6447 | 0 | return NULL; |
6448 | 0 | } |
6449 | 0 | context->compcode = codec; |
6450 | 0 | } |
6451 | 14.8k | context->compcode_meta = cparams.compcode_meta; |
6452 | | |
6453 | 14.8k | context->blocksize = cparams.blocksize; |
6454 | | /* Check for a BLOSC_BLOCKSIZE environment variable */ |
6455 | 14.8k | envvar = getenv("BLOSC_BLOCKSIZE"); |
6456 | 14.8k | if (envvar != NULL) { |
6457 | 0 | int32_t blocksize; |
6458 | 0 | errno = 0; /* To distinguish success/failure after call */ |
6459 | 0 | blocksize = (int32_t) strtol(envvar, NULL, 10); |
6460 | 0 | if ((errno != EINVAL) && (blocksize > 0)) { |
6461 | 0 | context->blocksize = blocksize; |
6462 | 0 | } |
6463 | 0 | else { |
6464 | 0 | BLOSC_TRACE_WARNING("BLOSC_BLOCKSIZE environment variable '%s' not recognized\n", envvar); |
6465 | 0 | } |
6466 | 0 | } |
6467 | | |
6468 | 14.8k | context->nthreads = cparams.nthreads; |
6469 | | /* Check for a BLOSC_NTHREADS environment variable */ |
6470 | 14.8k | envvar = getenv("BLOSC_NTHREADS"); |
6471 | 14.8k | if (envvar != NULL) { |
6472 | 0 | errno = 0; /* To distinguish success/failure after call */ |
6473 | 0 | int16_t nthreads = (int16_t) strtol(envvar, NULL, 10); |
6474 | 0 | if ((errno != EINVAL) && (nthreads > 0)) { |
6475 | 0 | context->nthreads = nthreads; |
6476 | 0 | } |
6477 | 0 | else { |
6478 | 0 | BLOSC_TRACE_WARNING("BLOSC_NTHREADS environment variable '%s' not recognized\n", envvar); |
6479 | 0 | } |
6480 | 0 | } |
6481 | 14.8k | context->new_nthreads = context->nthreads; |
6482 | | |
6483 | 14.8k | context->splitmode = cparams.splitmode; |
6484 | | /* Check for a BLOSC_SPLITMODE environment variable */ |
6485 | 14.8k | envvar = getenv("BLOSC_SPLITMODE"); |
6486 | 14.8k | if (envvar != NULL) { |
6487 | 0 | int32_t splitmode = -1; |
6488 | 0 | if (strcmp(envvar, "ALWAYS") == 0) { |
6489 | 0 | splitmode = BLOSC_ALWAYS_SPLIT; |
6490 | 0 | } |
6491 | 0 | else if (strcmp(envvar, "NEVER") == 0) { |
6492 | 0 | splitmode = BLOSC_NEVER_SPLIT; |
6493 | 0 | } |
6494 | 0 | else if (strcmp(envvar, "AUTO") == 0) { |
6495 | 0 | splitmode = BLOSC_AUTO_SPLIT; |
6496 | 0 | } |
6497 | 0 | else if (strcmp(envvar, "FORWARD_COMPAT") == 0) { |
6498 | 0 | splitmode = BLOSC_FORWARD_COMPAT_SPLIT; |
6499 | 0 | } |
6500 | 0 | else { |
6501 | 0 | BLOSC_TRACE_WARNING("BLOSC_SPLITMODE environment variable '%s' not recognized\n", envvar); |
6502 | 0 | } |
6503 | 0 | if (splitmode >= 0) { |
6504 | 0 | context->splitmode = splitmode; |
6505 | 0 | } |
6506 | 0 | } |
6507 | | |
6508 | 14.8k | context->threads_started = 0; |
6509 | 14.8k | blosc2_pthread_mutex_init(&context->nchunk_mutex, NULL); |
6510 | 14.8k | context->schunk = cparams.schunk; |
6511 | | |
6512 | 14.8k | if (cparams.prefilter != NULL) { |
6513 | 0 | context->prefilter = cparams.prefilter; |
6514 | 0 | context->preparams = (blosc2_prefilter_params*)my_malloc(sizeof(blosc2_prefilter_params)); |
6515 | 0 | BLOSC_ERROR_NULL(context->preparams, NULL); |
6516 | 0 | memcpy(context->preparams, cparams.preparams, sizeof(blosc2_prefilter_params)); |
6517 | 0 | } |
6518 | | |
6519 | 14.8k | if (cparams.tuner_id <= 0) { |
6520 | 14.8k | cparams.tuner_id = g_tuner; |
6521 | 14.8k | } else { |
6522 | 0 | for (int i = 0; i < g_ntuners; ++i) { |
6523 | 0 | if (g_tuners[i].id == cparams.tuner_id) { |
6524 | 0 | if (g_tuners[i].init == NULL) { |
6525 | 0 | if (fill_tuner(&g_tuners[i]) < 0) { |
6526 | 0 | BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
6527 | 0 | return NULL; |
6528 | 0 | } |
6529 | 0 | } |
6530 | 0 | if (g_tuners[i].init(cparams.tuner_params, context, NULL) < 0) { |
6531 | 0 | BLOSC_TRACE_ERROR("Error in user-defined tuner %d init function\n", cparams.tuner_id); |
6532 | 0 | return NULL; |
6533 | 0 | } |
6534 | 0 | goto urtunersuccess; |
6535 | 0 | } |
6536 | 0 | } |
6537 | 0 | BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", cparams.tuner_id); |
6538 | 0 | return NULL; |
6539 | 0 | } |
6540 | 14.8k | urtunersuccess:; |
6541 | | |
6542 | 14.8k | context->tuner_id = cparams.tuner_id; |
6543 | | |
6544 | 14.8k | context->codec_params = cparams.codec_params; |
6545 | 14.8k | memcpy(context->filter_params, cparams.filter_params, BLOSC2_MAX_FILTERS * sizeof(void*)); |
6546 | | |
6547 | 14.8k | return context; |
6548 | 14.8k | } |
6549 | | |
6550 | | /* Create a context for decompression */ |
6551 | 20.9k | blosc2_context* blosc2_create_dctx(blosc2_dparams dparams) { |
6552 | 20.9k | blosc2_context* context = (blosc2_context*)my_malloc(sizeof(blosc2_context)); |
6553 | 20.9k | BLOSC_ERROR_NULL(context, NULL); |
6554 | | |
6555 | | /* Populate the context, using zeros as default values */ |
6556 | 20.9k | memset(context, 0, sizeof(blosc2_context)); |
6557 | 20.9k | context->do_compress = 0; /* Meant for decompression */ |
6558 | | |
6559 | 20.9k | context->nthreads = dparams.nthreads; |
6560 | 20.9k | char* envvar = getenv("BLOSC_NTHREADS"); |
6561 | 20.9k | if (envvar != NULL) { |
6562 | 0 | errno = 0; /* To distinguish success/failure after call */ |
6563 | 0 | long nthreads = strtol(envvar, NULL, 10); |
6564 | 0 | if ((errno != EINVAL) && (nthreads > 0)) { |
6565 | 0 | context->nthreads = (int16_t) nthreads; |
6566 | 0 | } |
6567 | 0 | } |
6568 | 20.9k | context->new_nthreads = context->nthreads; |
6569 | | |
6570 | 20.9k | context->threads_started = 0; |
6571 | 20.9k | context->block_maskout = NULL; |
6572 | 20.9k | context->block_maskout_nitems = 0; |
6573 | 20.9k | blosc2_pthread_mutex_init(&context->nchunk_mutex, NULL); |
6574 | 20.9k | context->schunk = dparams.schunk; |
6575 | | |
6576 | 20.9k | if (dparams.postfilter != NULL) { |
6577 | 0 | context->postfilter = dparams.postfilter; |
6578 | 0 | context->postparams = (blosc2_postfilter_params*)my_malloc(sizeof(blosc2_postfilter_params)); |
6579 | 0 | BLOSC_ERROR_NULL(context->postparams, NULL); |
6580 | 0 | memcpy(context->postparams, dparams.postparams, sizeof(blosc2_postfilter_params)); |
6581 | 0 | } |
6582 | | |
6583 | 20.9k | return context; |
6584 | 20.9k | } |
6585 | | |
6586 | | |
6587 | 64.9k | void blosc2_free_ctx(blosc2_context* context) { |
6588 | 64.9k | if (g_initlib || |
6589 | 29.0k | context->thread_backend == BLOSC_BACKEND_PER_CONTEXT || |
6590 | 35.8k | context->thread_backend == BLOSC_BACKEND_CALLBACK) { |
6591 | 35.8k | release_thread_backend(context); |
6592 | 35.8k | } |
6593 | 64.9k | if (context->serial_context != NULL) { |
6594 | 30.3k | free_thread_context(context->serial_context); |
6595 | 30.3k | } |
6596 | 64.9k | blosc2_pthread_mutex_destroy(&context->nchunk_mutex); |
6597 | 64.9k | release_context_dict_buffer(context); |
6598 | 64.9k | if (context->dict_cdict != NULL) { |
6599 | 0 | if (context->compcode == BLOSC_LZ4) { |
6600 | 0 | LZ4_freeStream((LZ4_stream_t*)context->dict_cdict); |
6601 | 0 | } else if (context->compcode == BLOSC_LZ4HC) { |
6602 | 0 | LZ4_freeStreamHC((LZ4_streamHC_t*)context->dict_cdict); |
6603 | 0 | } |
6604 | 0 | #ifdef HAVE_ZSTD |
6605 | 0 | else if (context->compcode == BLOSC_ZSTD) { |
6606 | 0 | ZSTD_freeCDict(context->dict_cdict); |
6607 | 0 | } |
6608 | 0 | #endif |
6609 | 0 | } |
6610 | 64.9k | if (context->dict_ddict != NULL) { |
6611 | 6.12k | #ifdef HAVE_ZSTD |
6612 | 6.12k | ZSTD_freeDDict(context->dict_ddict); |
6613 | 6.12k | #endif |
6614 | 6.12k | } |
6615 | 64.9k | if (context->tuner_params != NULL) { |
6616 | 0 | int rc; |
6617 | 0 | if (context->tuner_id < BLOSC_LAST_TUNER && context->tuner_id == BLOSC_STUNE) { |
6618 | 0 | rc = blosc_stune_free(context); |
6619 | 0 | } else { |
6620 | 0 | for (int i = 0; i < g_ntuners; ++i) { |
6621 | 0 | if (g_tuners[i].id == context->tuner_id) { |
6622 | 0 | if (g_tuners[i].free == NULL) { |
6623 | 0 | if (fill_tuner(&g_tuners[i]) < 0) { |
6624 | 0 | BLOSC_TRACE_ERROR("Could not load tuner %d.", g_tuners[i].id); |
6625 | 0 | return; |
6626 | 0 | } |
6627 | 0 | } |
6628 | 0 | rc = g_tuners[i].free(context); |
6629 | 0 | goto urtunersuccess; |
6630 | 0 | } |
6631 | 0 | } |
6632 | 0 | BLOSC_TRACE_ERROR("User-defined tuner %d not found\n", context->tuner_id); |
6633 | 0 | return; |
6634 | 0 | urtunersuccess:; |
6635 | 0 | } |
6636 | 0 | if (rc < 0) { |
6637 | 0 | BLOSC_TRACE_ERROR("Error in user-defined tuner free function\n"); |
6638 | 0 | return; |
6639 | 0 | } |
6640 | 0 | } |
6641 | | /* May be needed if codec_params ever contains nested objects |
6642 | | if (context->codec_params != NULL) { |
6643 | | int rc; |
6644 | | for (int i = 0; i < g_ncodecs; ++i) { |
6645 | | if (g_codecs[i].compcode == context->compcode) { |
6646 | | if (g_codecs[i].free == NULL) { |
6647 | | // Dynamically load codec plugin |
6648 | | if (fill_codec(&g_codecs[i]) < 0) { |
6649 | | BLOSC_TRACE_ERROR("Could not load codec %d.", g_codecs[i].compcode); |
6650 | | return BLOSC2_ERROR_CODEC_SUPPORT; |
6651 | | } |
6652 | | } |
6653 | | if (g_codecs[i].free == NULL){ |
6654 | | // no free func, codec_params is simple |
6655 | | my_free(context->codec_params); |
6656 | | } |
6657 | | else{ // has free function for codec_params (e.g. openzl) |
6658 | | rc = g_codecs[i].free(context->codec_params); |
6659 | | goto urcodecsuccess; |
6660 | | } |
6661 | | } |
6662 | | } |
6663 | | BLOSC_TRACE_ERROR("User-defined compressor codec %d not found", context->compcode); |
6664 | | return BLOSC2_ERROR_CODEC_SUPPORT; |
6665 | | urcodecsuccess:; |
6666 | | if (rc < 0) { |
6667 | | BLOSC_TRACE_ERROR("Error in user-defined codec free function\n"); |
6668 | | return; |
6669 | | } |
6670 | | } |
6671 | | */ |
6672 | 64.9k | if (context->prefilter != NULL) { |
6673 | 0 | my_free(context->preparams); |
6674 | 0 | } |
6675 | 64.9k | if (context->postfilter != NULL) { |
6676 | 0 | my_free(context->postparams); |
6677 | 0 | } |
6678 | | |
6679 | 64.9k | if (context->block_maskout != NULL) { |
6680 | 0 | free(context->block_maskout); |
6681 | 0 | } |
6682 | 64.9k | if (context->blocknbytes != NULL) { |
6683 | 288 | free(context->blocknbytes); |
6684 | 288 | } |
6685 | 64.9k | if (context->blockoffsets != NULL) { |
6686 | 288 | free(context->blockoffsets); |
6687 | 288 | } |
6688 | 64.9k | if (context->blockcbytes != NULL) { |
6689 | 288 | free(context->blockcbytes); |
6690 | 288 | } |
6691 | 64.9k | my_free(context); |
6692 | 64.9k | } |
6693 | | |
6694 | | |
6695 | 0 | int blosc2_ctx_get_cparams(blosc2_context *ctx, blosc2_cparams *cparams) { |
6696 | 0 | cparams->compcode = ctx->compcode; |
6697 | 0 | cparams->compcode_meta = ctx->compcode_meta; |
6698 | 0 | cparams->clevel = ctx->clevel; |
6699 | 0 | cparams->use_dict = ctx->use_dict; |
6700 | 0 | cparams->instr_codec = ctx->blosc2_flags & BLOSC2_INSTR_CODEC; |
6701 | 0 | cparams->typesize = ctx->typesize; |
6702 | 0 | cparams->nthreads = ctx->nthreads; |
6703 | 0 | cparams->blocksize = ctx->blocksize; |
6704 | 0 | cparams->splitmode = ctx->splitmode; |
6705 | 0 | cparams->schunk = ctx->schunk; |
6706 | 0 | for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) { |
6707 | 0 | cparams->filters[i] = ctx->filters[i]; |
6708 | 0 | cparams->filters_meta[i] = ctx->filters_meta[i]; |
6709 | 0 | } |
6710 | 0 | cparams->prefilter = ctx->prefilter; |
6711 | 0 | cparams->preparams = ctx->preparams; |
6712 | 0 | cparams->tuner_id = ctx->tuner_id; |
6713 | 0 | cparams->codec_params = ctx->codec_params; |
6714 | |
|
6715 | 0 | return BLOSC2_ERROR_SUCCESS; |
6716 | 0 | } |
6717 | | |
6718 | | |
6719 | 54.1k | int blosc2_ctx_get_dparams(blosc2_context *ctx, blosc2_dparams *dparams) { |
6720 | 54.1k | dparams->nthreads = ctx->nthreads; |
6721 | 54.1k | dparams->schunk = ctx->schunk; |
6722 | 54.1k | dparams->postfilter = ctx->postfilter; |
6723 | 54.1k | dparams->postparams = ctx->postparams; |
6724 | 54.1k | dparams->typesize = ctx->typesize; |
6725 | | |
6726 | 54.1k | return BLOSC2_ERROR_SUCCESS; |
6727 | 54.1k | } |
6728 | | |
6729 | | |
6730 | | /* Set a maskout in decompression context */ |
6731 | 0 | int blosc2_set_maskout(blosc2_context *ctx, bool *maskout, int nblocks) { |
6732 | |
|
6733 | 0 | if (ctx->block_maskout != NULL) { |
6734 | | // Get rid of a possible mask here |
6735 | 0 | free(ctx->block_maskout); |
6736 | 0 | } |
6737 | |
|
6738 | 0 | bool *maskout_ = malloc(nblocks); |
6739 | 0 | BLOSC_ERROR_NULL(maskout_, BLOSC2_ERROR_MEMORY_ALLOC); |
6740 | 0 | memcpy(maskout_, maskout, nblocks); |
6741 | 0 | ctx->block_maskout = maskout_; |
6742 | 0 | ctx->block_maskout_nitems = nblocks; |
6743 | |
|
6744 | 0 | return 0; |
6745 | 0 | } |
6746 | | |
6747 | | |
6748 | | /* Create a chunk made of zeros */ |
6749 | 1.71k | int blosc2_chunk_zeros(blosc2_cparams cparams, const int32_t nbytes, void* dest, int32_t destsize) { |
6750 | 1.71k | if (destsize < BLOSC_EXTENDED_HEADER_LENGTH) { |
6751 | 0 | BLOSC_TRACE_ERROR("dest buffer is not long enough"); |
6752 | 0 | return BLOSC2_ERROR_DATA; |
6753 | 0 | } |
6754 | | |
6755 | 1.71k | if ((nbytes > 0) && (nbytes % cparams.typesize)) { |
6756 | 2 | BLOSC_TRACE_ERROR("nbytes must be a multiple of typesize"); |
6757 | 2 | return BLOSC2_ERROR_DATA; |
6758 | 2 | } |
6759 | | |
6760 | 1.71k | blosc_header header; |
6761 | 1.71k | blosc2_context* context = blosc2_create_cctx(cparams); |
6762 | 1.71k | if (context == NULL) { |
6763 | 0 | BLOSC_TRACE_ERROR("Error while creating the compression context"); |
6764 | 0 | return BLOSC2_ERROR_NULL_POINTER; |
6765 | 0 | } |
6766 | | |
6767 | 1.71k | int error = initialize_context_compression( |
6768 | 1.71k | context, NULL, nbytes, dest, destsize, |
6769 | 1.71k | context->clevel, context->filters, context->filters_meta, |
6770 | 1.71k | context->typesize, context->compcode, context->blocksize, |
6771 | 1.71k | context->new_nthreads, context->nthreads, context->splitmode, |
6772 | 1.71k | context->tuner_id, context->tuner_params, context->schunk); |
6773 | 1.71k | if (error <= 0) { |
6774 | 0 | blosc2_free_ctx(context); |
6775 | 0 | return error; |
6776 | 0 | } |
6777 | | |
6778 | 1.71k | memset(&header, 0, sizeof(header)); |
6779 | 1.71k | header.version = BLOSC2_VERSION_FORMAT_STABLE; |
6780 | 1.71k | header.versionlz = BLOSC_BLOSCLZ_VERSION_FORMAT; |
6781 | 1.71k | header.flags = BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE; // extended header |
6782 | 1.71k | header.typesize = context->typesize; |
6783 | 1.71k | header.nbytes = (int32_t)nbytes; |
6784 | 1.71k | header.blocksize = context->blocksize; |
6785 | 1.71k | header.cbytes = BLOSC_EXTENDED_HEADER_LENGTH; |
6786 | 1.71k | header.blosc2_flags = BLOSC2_SPECIAL_ZERO << 4; // mark chunk as all zeros |
6787 | 1.71k | memcpy((uint8_t *)dest, &header, sizeof(header)); |
6788 | | |
6789 | 1.71k | blosc2_free_ctx(context); |
6790 | | |
6791 | 1.71k | return BLOSC_EXTENDED_HEADER_LENGTH; |
6792 | 1.71k | } |
6793 | | |
6794 | | |
6795 | | /* Create a chunk made of uninitialized values */ |
6796 | 1.03k | int blosc2_chunk_uninit(blosc2_cparams cparams, const int32_t nbytes, void* dest, int32_t destsize) { |
6797 | 1.03k | if (destsize < BLOSC_EXTENDED_HEADER_LENGTH) { |
6798 | 0 | BLOSC_TRACE_ERROR("dest buffer is not long enough"); |
6799 | 0 | return BLOSC2_ERROR_DATA; |
6800 | 0 | } |
6801 | | |
6802 | 1.03k | if (nbytes % cparams.typesize) { |
6803 | 5 | BLOSC_TRACE_ERROR("nbytes must be a multiple of typesize"); |
6804 | 5 | return BLOSC2_ERROR_DATA; |
6805 | 5 | } |
6806 | | |
6807 | 1.02k | blosc_header header; |
6808 | 1.02k | blosc2_context* context = blosc2_create_cctx(cparams); |
6809 | 1.02k | if (context == NULL) { |
6810 | 0 | BLOSC_TRACE_ERROR("Error while creating the compression context"); |
6811 | 0 | return BLOSC2_ERROR_NULL_POINTER; |
6812 | 0 | } |
6813 | 1.02k | int error = initialize_context_compression( |
6814 | 1.02k | context, NULL, nbytes, dest, destsize, |
6815 | 1.02k | context->clevel, context->filters, context->filters_meta, |
6816 | 1.02k | context->typesize, context->compcode, context->blocksize, |
6817 | 1.02k | context->new_nthreads, context->nthreads, context->splitmode, |
6818 | 1.02k | context->tuner_id, context->tuner_params, context->schunk); |
6819 | 1.02k | if (error <= 0) { |
6820 | 0 | blosc2_free_ctx(context); |
6821 | 0 | return error; |
6822 | 0 | } |
6823 | | |
6824 | 1.02k | memset(&header, 0, sizeof(header)); |
6825 | 1.02k | header.version = BLOSC2_VERSION_FORMAT_STABLE; |
6826 | 1.02k | header.versionlz = BLOSC_BLOSCLZ_VERSION_FORMAT; |
6827 | 1.02k | header.flags = BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE; // extended header |
6828 | 1.02k | header.typesize = context->typesize; |
6829 | 1.02k | header.nbytes = (int32_t)nbytes; |
6830 | 1.02k | header.blocksize = context->blocksize; |
6831 | 1.02k | header.cbytes = BLOSC_EXTENDED_HEADER_LENGTH; |
6832 | 1.02k | header.blosc2_flags = BLOSC2_SPECIAL_UNINIT << 4; // mark chunk as uninitialized |
6833 | 1.02k | memcpy((uint8_t *)dest, &header, sizeof(header)); |
6834 | | |
6835 | 1.02k | blosc2_free_ctx(context); |
6836 | | |
6837 | 1.02k | return BLOSC_EXTENDED_HEADER_LENGTH; |
6838 | 1.02k | } |
6839 | | |
6840 | | |
6841 | | /* Create a chunk made of nans */ |
6842 | 123 | int blosc2_chunk_nans(blosc2_cparams cparams, const int32_t nbytes, void* dest, int32_t destsize) { |
6843 | 123 | if (destsize < BLOSC_EXTENDED_HEADER_LENGTH) { |
6844 | 0 | BLOSC_TRACE_ERROR("dest buffer is not long enough"); |
6845 | 0 | return BLOSC2_ERROR_DATA; |
6846 | 0 | } |
6847 | | |
6848 | 123 | if (nbytes % cparams.typesize) { |
6849 | 5 | BLOSC_TRACE_ERROR("nbytes must be a multiple of typesize"); |
6850 | 5 | return BLOSC2_ERROR_DATA; |
6851 | 5 | } |
6852 | | |
6853 | 118 | blosc_header header; |
6854 | 118 | blosc2_context* context = blosc2_create_cctx(cparams); |
6855 | 118 | if (context == NULL) { |
6856 | 0 | BLOSC_TRACE_ERROR("Error while creating the compression context"); |
6857 | 0 | return BLOSC2_ERROR_NULL_POINTER; |
6858 | 0 | } |
6859 | | |
6860 | 118 | int error = initialize_context_compression( |
6861 | 118 | context, NULL, nbytes, dest, destsize, |
6862 | 118 | context->clevel, context->filters, context->filters_meta, |
6863 | 118 | context->typesize, context->compcode, context->blocksize, |
6864 | 118 | context->new_nthreads, context->nthreads, context->splitmode, |
6865 | 118 | context->tuner_id, context->tuner_params, context->schunk); |
6866 | 118 | if (error <= 0) { |
6867 | 0 | blosc2_free_ctx(context); |
6868 | 0 | return error; |
6869 | 0 | } |
6870 | | |
6871 | 118 | memset(&header, 0, sizeof(header)); |
6872 | 118 | header.version = BLOSC2_VERSION_FORMAT_STABLE; |
6873 | 118 | header.versionlz = BLOSC_BLOSCLZ_VERSION_FORMAT; |
6874 | 118 | header.flags = BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE; // extended header |
6875 | 118 | header.typesize = context->typesize; |
6876 | 118 | header.nbytes = (int32_t)nbytes; |
6877 | 118 | header.blocksize = context->blocksize; |
6878 | 118 | header.cbytes = BLOSC_EXTENDED_HEADER_LENGTH; |
6879 | 118 | header.blosc2_flags = BLOSC2_SPECIAL_NAN << 4; // mark chunk as all NaNs |
6880 | 118 | memcpy((uint8_t *)dest, &header, sizeof(header)); |
6881 | | |
6882 | 118 | blosc2_free_ctx(context); |
6883 | | |
6884 | 118 | return BLOSC_EXTENDED_HEADER_LENGTH; |
6885 | 118 | } |
6886 | | |
6887 | | |
6888 | | /* Create a chunk made of repeated values */ |
6889 | | int blosc2_chunk_repeatval(blosc2_cparams cparams, const int32_t nbytes, |
6890 | 0 | void* dest, int32_t destsize, const void* repeatval) { |
6891 | 0 | if (destsize < BLOSC_EXTENDED_HEADER_LENGTH + cparams.typesize) { |
6892 | 0 | BLOSC_TRACE_ERROR("dest buffer is not long enough"); |
6893 | 0 | return BLOSC2_ERROR_DATA; |
6894 | 0 | } |
6895 | | |
6896 | 0 | if (nbytes % cparams.typesize) { |
6897 | 0 | BLOSC_TRACE_ERROR("nbytes must be a multiple of typesize"); |
6898 | 0 | return BLOSC2_ERROR_DATA; |
6899 | 0 | } |
6900 | | |
6901 | 0 | blosc_header header; |
6902 | 0 | blosc2_context* context = blosc2_create_cctx(cparams); |
6903 | 0 | if (context == NULL) { |
6904 | 0 | BLOSC_TRACE_ERROR("Error while creating the compression context"); |
6905 | 0 | return BLOSC2_ERROR_NULL_POINTER; |
6906 | 0 | } |
6907 | | |
6908 | 0 | int error = initialize_context_compression( |
6909 | 0 | context, NULL, nbytes, dest, destsize, |
6910 | 0 | context->clevel, context->filters, context->filters_meta, |
6911 | 0 | context->typesize, context->compcode, context->blocksize, |
6912 | 0 | context->new_nthreads, context->nthreads, context->splitmode, |
6913 | 0 | context->tuner_id, context->tuner_params, context->schunk); |
6914 | 0 | if (error <= 0) { |
6915 | 0 | blosc2_free_ctx(context); |
6916 | 0 | return error; |
6917 | 0 | } |
6918 | | |
6919 | 0 | memset(&header, 0, sizeof(header)); |
6920 | 0 | header.version = BLOSC2_VERSION_FORMAT_STABLE; |
6921 | 0 | header.versionlz = BLOSC_BLOSCLZ_VERSION_FORMAT; |
6922 | 0 | header.flags = BLOSC_DOSHUFFLE | BLOSC_DOBITSHUFFLE; // extended header |
6923 | 0 | header.typesize = context->typesize; |
6924 | 0 | header.nbytes = (int32_t)nbytes; |
6925 | 0 | header.blocksize = context->blocksize; |
6926 | 0 | header.cbytes = BLOSC_EXTENDED_HEADER_LENGTH + cparams.typesize; |
6927 | 0 | header.blosc2_flags = BLOSC2_SPECIAL_VALUE << 4; // mark chunk as all repeated value |
6928 | 0 | memcpy((uint8_t *)dest, &header, sizeof(header)); |
6929 | 0 | memcpy((uint8_t *)dest + sizeof(header), repeatval, cparams.typesize); |
6930 | |
|
6931 | 0 | blosc2_free_ctx(context); |
6932 | |
|
6933 | 0 | return BLOSC_EXTENDED_HEADER_LENGTH + cparams.typesize; |
6934 | 0 | } |
6935 | | |
6936 | | |
6937 | | /* Register filters */ |
6938 | | |
6939 | 145k | int register_filter_private(blosc2_filter *filter) { |
6940 | 145k | BLOSC_ERROR_NULL(filter, BLOSC2_ERROR_INVALID_PARAM); |
6941 | 145k | if (g_nfilters == UINT8_MAX) { |
6942 | 0 | BLOSC_TRACE_ERROR("Can not register more filters"); |
6943 | 0 | return BLOSC2_ERROR_CODEC_SUPPORT; |
6944 | 0 | } |
6945 | 145k | if (filter->id < BLOSC2_GLOBAL_REGISTERED_FILTERS_START) { |
6946 | 0 | BLOSC_TRACE_ERROR("The id must be greater or equal than %d", BLOSC2_GLOBAL_REGISTERED_FILTERS_START); |
6947 | 0 | return BLOSC2_ERROR_FAILURE; |
6948 | 0 | } |
6949 | | /* This condition can never be fulfilled |
6950 | | if (filter->id > BLOSC2_USER_REGISTERED_FILTERS_STOP) { |
6951 | | BLOSC_TRACE_ERROR("The id must be less than or equal to %d", BLOSC2_USER_REGISTERED_FILTERS_STOP); |
6952 | | return BLOSC2_ERROR_FAILURE; |
6953 | | } |
6954 | | */ |
6955 | | |
6956 | 436k | for (uint64_t i = 0; i < g_nfilters; ++i) { |
6957 | 290k | if (g_filters[i].id == filter->id) { |
6958 | 0 | if (strcmp(g_filters[i].name, filter->name) != 0) { |
6959 | 0 | BLOSC_TRACE_ERROR("The filter (ID: %d) plugin is already registered with name: %s." |
6960 | 0 | " Choose another one !", filter->id, g_filters[i].name); |
6961 | 0 | return BLOSC2_ERROR_FAILURE; |
6962 | 0 | } |
6963 | 0 | else { |
6964 | | // Already registered, so no more actions needed |
6965 | 0 | return BLOSC2_ERROR_SUCCESS; |
6966 | 0 | } |
6967 | 0 | } |
6968 | 290k | } |
6969 | | |
6970 | 145k | blosc2_filter *filter_new = &g_filters[g_nfilters++]; |
6971 | 145k | memcpy(filter_new, filter, sizeof(blosc2_filter)); |
6972 | | |
6973 | 145k | return BLOSC2_ERROR_SUCCESS; |
6974 | 145k | } |
6975 | | |
6976 | | |
6977 | 0 | int blosc2_register_filter(blosc2_filter *filter) { |
6978 | 0 | if (filter->id < BLOSC2_USER_REGISTERED_FILTERS_START) { |
6979 | 0 | BLOSC_TRACE_ERROR("The id must be greater or equal to %d", BLOSC2_USER_REGISTERED_FILTERS_START); |
6980 | 0 | return BLOSC2_ERROR_FAILURE; |
6981 | 0 | } |
6982 | | |
6983 | 0 | return register_filter_private(filter); |
6984 | 0 | } |
6985 | | |
6986 | | |
6987 | | /* Register codecs */ |
6988 | | |
6989 | 261k | int register_codec_private(blosc2_codec *codec) { |
6990 | 261k | BLOSC_ERROR_NULL(codec, BLOSC2_ERROR_INVALID_PARAM); |
6991 | 261k | if (g_ncodecs == UINT8_MAX) { |
6992 | 0 | BLOSC_TRACE_ERROR("Can not register more codecs"); |
6993 | 0 | return BLOSC2_ERROR_CODEC_SUPPORT; |
6994 | 0 | } |
6995 | 261k | if (codec->compcode < BLOSC2_GLOBAL_REGISTERED_CODECS_START) { |
6996 | 0 | BLOSC_TRACE_ERROR("The id must be greater or equal than %d", BLOSC2_GLOBAL_REGISTERED_CODECS_START); |
6997 | 0 | return BLOSC2_ERROR_FAILURE; |
6998 | 0 | } |
6999 | | /* This condition can never be fulfilled |
7000 | | if (codec->compcode > BLOSC2_USER_REGISTERED_CODECS_STOP) { |
7001 | | BLOSC_TRACE_ERROR("The id must be less or equal to %d", BLOSC2_USER_REGISTERED_CODECS_STOP); |
7002 | | return BLOSC2_ERROR_FAILURE; |
7003 | | } |
7004 | | */ |
7005 | | |
7006 | 1.30M | for (int i = 0; i < g_ncodecs; ++i) { |
7007 | 1.04M | if (g_codecs[i].compcode == codec->compcode) { |
7008 | 0 | if (strcmp(g_codecs[i].compname, codec->compname) != 0) { |
7009 | 0 | BLOSC_TRACE_ERROR("The codec (ID: %d) plugin is already registered with name: %s." |
7010 | 0 | " Choose another one !", codec->compcode, codec->compname); |
7011 | 0 | return BLOSC2_ERROR_CODEC_PARAM; |
7012 | 0 | } |
7013 | 0 | else { |
7014 | | // Already registered, so no more actions needed |
7015 | 0 | return BLOSC2_ERROR_SUCCESS; |
7016 | 0 | } |
7017 | 0 | } |
7018 | 1.04M | } |
7019 | | |
7020 | 261k | blosc2_codec *codec_new = &g_codecs[g_ncodecs++]; |
7021 | 261k | memcpy(codec_new, codec, sizeof(blosc2_codec)); |
7022 | | |
7023 | 261k | return BLOSC2_ERROR_SUCCESS; |
7024 | 261k | } |
7025 | | |
7026 | | |
7027 | 0 | int blosc2_register_codec(blosc2_codec *codec) { |
7028 | 0 | if (codec->compcode < BLOSC2_USER_REGISTERED_CODECS_START) { |
7029 | 0 | BLOSC_TRACE_ERROR("The compcode must be greater or equal than %d", BLOSC2_USER_REGISTERED_CODECS_START); |
7030 | 0 | return BLOSC2_ERROR_CODEC_PARAM; |
7031 | 0 | } |
7032 | | |
7033 | 0 | return register_codec_private(codec); |
7034 | 0 | } |
7035 | | |
7036 | | |
7037 | | /* Register tuners */ |
7038 | | |
7039 | 29.0k | int register_tuner_private(blosc2_tuner *tuner) { |
7040 | 29.0k | BLOSC_ERROR_NULL(tuner, BLOSC2_ERROR_INVALID_PARAM); |
7041 | 29.0k | if (g_ntuners == UINT8_MAX) { |
7042 | 0 | BLOSC_TRACE_ERROR("Can not register more tuners"); |
7043 | 0 | return BLOSC2_ERROR_CODEC_SUPPORT; |
7044 | 0 | } |
7045 | 29.0k | if (tuner->id < BLOSC2_GLOBAL_REGISTERED_TUNER_START) { |
7046 | 0 | BLOSC_TRACE_ERROR("The id must be greater or equal than %d", BLOSC2_GLOBAL_REGISTERED_TUNER_START); |
7047 | 0 | return BLOSC2_ERROR_FAILURE; |
7048 | 0 | } |
7049 | | |
7050 | 29.0k | for (int i = 0; i < g_ntuners; ++i) { |
7051 | 0 | if (g_tuners[i].id == tuner->id) { |
7052 | 0 | if (strcmp(g_tuners[i].name, tuner->name) != 0) { |
7053 | 0 | BLOSC_TRACE_ERROR("The tuner (ID: %d) plugin is already registered with name: %s." |
7054 | 0 | " Choose another one !", tuner->id, g_tuners[i].name); |
7055 | 0 | return BLOSC2_ERROR_FAILURE; |
7056 | 0 | } |
7057 | 0 | else { |
7058 | | // Already registered, so no more actions needed |
7059 | 0 | return BLOSC2_ERROR_SUCCESS; |
7060 | 0 | } |
7061 | 0 | } |
7062 | 0 | } |
7063 | | |
7064 | 29.0k | blosc2_tuner *tuner_new = &g_tuners[g_ntuners++]; |
7065 | 29.0k | memcpy(tuner_new, tuner, sizeof(blosc2_tuner)); |
7066 | | |
7067 | 29.0k | return BLOSC2_ERROR_SUCCESS; |
7068 | 29.0k | } |
7069 | | |
7070 | | |
7071 | 0 | int blosc2_register_tuner(blosc2_tuner *tuner) { |
7072 | 0 | if (tuner->id < BLOSC2_USER_REGISTERED_TUNER_START) { |
7073 | 0 | BLOSC_TRACE_ERROR("The id must be greater or equal to %d", BLOSC2_USER_REGISTERED_TUNER_START); |
7074 | 0 | return BLOSC2_ERROR_FAILURE; |
7075 | 0 | } |
7076 | | |
7077 | 0 | return register_tuner_private(tuner); |
7078 | 0 | } |
7079 | | |
7080 | | |
7081 | 58.1k | int _blosc2_register_io_cb(const blosc2_io_cb *io) { |
7082 | | |
7083 | 87.2k | for (uint64_t i = 0; i < g_nio; ++i) { |
7084 | 87.2k | if (g_ios[i].id == io->id) { |
7085 | 58.1k | if (strcmp(g_ios[i].name, io->name) != 0) { |
7086 | 0 | BLOSC_TRACE_ERROR("The IO (ID: %d) plugin is already registered with name: %s." |
7087 | 0 | " Choose another one !", io->id, g_ios[i].name); |
7088 | 0 | return BLOSC2_ERROR_PLUGIN_IO; |
7089 | 0 | } |
7090 | 58.1k | else { |
7091 | | // Already registered, so no more actions needed |
7092 | 58.1k | return BLOSC2_ERROR_SUCCESS; |
7093 | 58.1k | } |
7094 | 58.1k | } |
7095 | 87.2k | } |
7096 | | |
7097 | 8 | blosc2_io_cb *io_new = &g_ios[g_nio++]; |
7098 | 8 | memcpy(io_new, io, sizeof(blosc2_io_cb)); |
7099 | | |
7100 | 8 | return BLOSC2_ERROR_SUCCESS; |
7101 | 58.1k | } |
7102 | | |
7103 | 0 | int blosc2_register_io_cb(const blosc2_io_cb *io) { |
7104 | 0 | BLOSC_ERROR_NULL(io, BLOSC2_ERROR_INVALID_PARAM); |
7105 | 0 | if (g_nio == UINT8_MAX) { |
7106 | 0 | BLOSC_TRACE_ERROR("Can not register more codecs"); |
7107 | 0 | return BLOSC2_ERROR_PLUGIN_IO; |
7108 | 0 | } |
7109 | | |
7110 | 0 | if (io->id < BLOSC2_IO_REGISTERED) { |
7111 | 0 | BLOSC_TRACE_ERROR("The compcode must be greater or equal than %d", BLOSC2_IO_REGISTERED); |
7112 | 0 | return BLOSC2_ERROR_PLUGIN_IO; |
7113 | 0 | } |
7114 | | |
7115 | 0 | return _blosc2_register_io_cb(io); |
7116 | 0 | } |
7117 | | |
7118 | 51.8k | blosc2_io_cb *blosc2_get_io_cb(uint8_t id) { |
7119 | | // If g_initlib is not set by blosc2_init() this function will try to read |
7120 | | // uninitialized memory. We should therefore always return NULL in that case |
7121 | 51.8k | if (!g_initlib) { |
7122 | 0 | return NULL; |
7123 | 0 | } |
7124 | 51.8k | for (uint64_t i = 0; i < g_nio; ++i) { |
7125 | 51.8k | if (g_ios[i].id == id) { |
7126 | 51.8k | return &g_ios[i]; |
7127 | 51.8k | } |
7128 | 51.8k | } |
7129 | 0 | if (id == BLOSC2_IO_FILESYSTEM) { |
7130 | 0 | if (_blosc2_register_io_cb(&BLOSC2_IO_CB_DEFAULTS) < 0) { |
7131 | 0 | BLOSC_TRACE_ERROR("Error registering the default IO API"); |
7132 | 0 | return NULL; |
7133 | 0 | } |
7134 | 0 | return blosc2_get_io_cb(id); |
7135 | 0 | } |
7136 | 0 | else if (id == BLOSC2_IO_FILESYSTEM_MMAP) { |
7137 | 0 | if (_blosc2_register_io_cb(&BLOSC2_IO_CB_MMAP) < 0) { |
7138 | 0 | BLOSC_TRACE_ERROR("Error registering the mmap IO API"); |
7139 | 0 | return NULL; |
7140 | 0 | } |
7141 | 0 | return blosc2_get_io_cb(id); |
7142 | 0 | } |
7143 | 0 | return NULL; |
7144 | 0 | } |
7145 | | |
7146 | 0 | void blosc2_unidim_to_multidim(uint8_t ndim, int64_t *shape, int64_t i, int64_t *index) { |
7147 | 0 | if (ndim == 0) { |
7148 | 0 | return; |
7149 | 0 | } |
7150 | 0 | assert(ndim <= B2ND_MAX_DIM); |
7151 | 0 | int64_t strides[B2ND_MAX_DIM]; |
7152 | |
|
7153 | 0 | strides[ndim - 1] = 1; |
7154 | 0 | for (int j = ndim - 2; j >= 0; --j) { |
7155 | 0 | strides[j] = shape[j + 1] * strides[j + 1]; |
7156 | 0 | } |
7157 | |
|
7158 | 0 | index[0] = i / strides[0]; |
7159 | 0 | for (int j = 1; j < ndim; ++j) { |
7160 | 0 | index[j] = (i % strides[j - 1]) / strides[j]; |
7161 | 0 | } |
7162 | 0 | } |
7163 | | |
7164 | 0 | void blosc2_multidim_to_unidim(const int64_t *index, int8_t ndim, const int64_t *strides, int64_t *i) { |
7165 | 0 | *i = 0; |
7166 | 0 | for (int j = 0; j < ndim; ++j) { |
7167 | 0 | *i += index[j] * strides[j]; |
7168 | 0 | } |
7169 | 0 | } |
7170 | | |
7171 | 0 | int64_t blosc2_get_slice_nchunks(blosc2_schunk* schunk, int64_t *start, int64_t *stop, int64_t **chunks_idx) { |
7172 | 0 | BLOSC_ERROR_NULL(schunk, BLOSC2_ERROR_NULL_POINTER); |
7173 | 0 | if (blosc2_meta_exists(schunk, "b2nd") < 0) { |
7174 | | // Try with a caterva metalayer; we are meant to be backward compatible with it |
7175 | 0 | if (blosc2_meta_exists(schunk, "caterva") < 0) { |
7176 | 0 | return schunk_get_slice_nchunks(schunk, *start, *stop, chunks_idx); |
7177 | 0 | } |
7178 | 0 | } |
7179 | | |
7180 | 0 | b2nd_array_t *array; |
7181 | 0 | int rc = b2nd_from_schunk(schunk, &array); |
7182 | 0 | if (rc < 0) { |
7183 | 0 | BLOSC_TRACE_ERROR("Could not get b2nd array from schunk."); |
7184 | 0 | return rc; |
7185 | 0 | } |
7186 | 0 | int64_t nchunks = b2nd_get_slice_nchunks(array, start, stop, chunks_idx); |
7187 | 0 | array->sc = NULL; // Free only array struct |
7188 | 0 | b2nd_free(array); |
7189 | |
|
7190 | 0 | return nchunks; |
7191 | 0 | } |
7192 | | |
7193 | 0 | blosc2_cparams blosc2_get_blosc2_cparams_defaults(void) { |
7194 | 0 | return BLOSC2_CPARAMS_DEFAULTS; |
7195 | 0 | }; |
7196 | | |
7197 | 0 | blosc2_dparams blosc2_get_blosc2_dparams_defaults(void) { |
7198 | 0 | return BLOSC2_DPARAMS_DEFAULTS; |
7199 | 0 | }; |
7200 | | |
7201 | 0 | blosc2_storage blosc2_get_blosc2_storage_defaults(void) { |
7202 | 0 | return BLOSC2_STORAGE_DEFAULTS; |
7203 | 0 | }; |
7204 | | |
7205 | 0 | blosc2_io blosc2_get_blosc2_io_defaults(void) { |
7206 | 0 | return BLOSC2_IO_DEFAULTS; |
7207 | 0 | }; |
7208 | | |
7209 | 0 | blosc2_stdio_mmap blosc2_get_blosc2_stdio_mmap_defaults(void) { |
7210 | 0 | return BLOSC2_STDIO_MMAP_DEFAULTS; |
7211 | 0 | }; |
7212 | | |
7213 | 41 | const char *blosc2_error_string(int error_code) { |
7214 | 41 | switch (error_code) { |
7215 | 22 | case BLOSC2_ERROR_FAILURE: |
7216 | 22 | return "Generic failure"; |
7217 | 0 | case BLOSC2_ERROR_STREAM: |
7218 | 0 | return "Bad stream"; |
7219 | 16 | case BLOSC2_ERROR_DATA: |
7220 | 16 | return "Invalid data"; |
7221 | 0 | case BLOSC2_ERROR_MEMORY_ALLOC: |
7222 | 0 | return "Memory alloc/realloc failure"; |
7223 | 0 | case BLOSC2_ERROR_READ_BUFFER: |
7224 | 0 | return "Not enough space to read"; |
7225 | 3 | case BLOSC2_ERROR_WRITE_BUFFER: |
7226 | 3 | return "Not enough space to write"; |
7227 | 0 | case BLOSC2_ERROR_CODEC_SUPPORT: |
7228 | 0 | return "Codec not supported"; |
7229 | 0 | case BLOSC2_ERROR_CODEC_PARAM: |
7230 | 0 | return "Invalid parameter supplied to codec"; |
7231 | 0 | case BLOSC2_ERROR_CODEC_DICT: |
7232 | 0 | return "Codec dictionary error"; |
7233 | 0 | case BLOSC2_ERROR_VERSION_SUPPORT: |
7234 | 0 | return "Version not supported"; |
7235 | 0 | case BLOSC2_ERROR_INVALID_HEADER: |
7236 | 0 | return "Invalid value in header"; |
7237 | 0 | case BLOSC2_ERROR_INVALID_PARAM: |
7238 | 0 | return "Invalid parameter supplied to function"; |
7239 | 0 | case BLOSC2_ERROR_FILE_READ: |
7240 | 0 | return "File read failure"; |
7241 | 0 | case BLOSC2_ERROR_FILE_WRITE: |
7242 | 0 | return "File write failure"; |
7243 | 0 | case BLOSC2_ERROR_FILE_OPEN: |
7244 | 0 | return "File open failure"; |
7245 | 0 | case BLOSC2_ERROR_NOT_FOUND: |
7246 | 0 | return "Not found"; |
7247 | 0 | case BLOSC2_ERROR_RUN_LENGTH: |
7248 | 0 | return "Bad run length encoding"; |
7249 | 0 | case BLOSC2_ERROR_FILTER_PIPELINE: |
7250 | 0 | return "Filter pipeline error"; |
7251 | 0 | case BLOSC2_ERROR_CHUNK_INSERT: |
7252 | 0 | return "Chunk insert failure"; |
7253 | 0 | case BLOSC2_ERROR_CHUNK_APPEND: |
7254 | 0 | return "Chunk append failure"; |
7255 | 0 | case BLOSC2_ERROR_CHUNK_UPDATE: |
7256 | 0 | return "Chunk update failure"; |
7257 | 0 | case BLOSC2_ERROR_2GB_LIMIT: |
7258 | 0 | return "Sizes larger than 2gb not supported"; |
7259 | 0 | case BLOSC2_ERROR_SCHUNK_COPY: |
7260 | 0 | return "Super-chunk copy failure"; |
7261 | 0 | case BLOSC2_ERROR_FRAME_TYPE: |
7262 | 0 | return "Wrong type for frame"; |
7263 | 0 | case BLOSC2_ERROR_FILE_TRUNCATE: |
7264 | 0 | return "File truncate failure"; |
7265 | 0 | case BLOSC2_ERROR_LOCK: |
7266 | 0 | return "Frame lock failure"; |
7267 | 0 | case BLOSC2_ERROR_THREAD_CREATE: |
7268 | 0 | return "Thread or thread context creation failure"; |
7269 | 0 | case BLOSC2_ERROR_POSTFILTER: |
7270 | 0 | return "Postfilter failure"; |
7271 | 0 | case BLOSC2_ERROR_FRAME_SPECIAL: |
7272 | 0 | return "Special frame failure"; |
7273 | 0 | case BLOSC2_ERROR_SCHUNK_SPECIAL: |
7274 | 0 | return "Special super-chunk failure"; |
7275 | 0 | case BLOSC2_ERROR_PLUGIN_IO: |
7276 | 0 | return "IO plugin error"; |
7277 | 0 | case BLOSC2_ERROR_FILE_REMOVE: |
7278 | 0 | return "Remove file failure"; |
7279 | 0 | case BLOSC2_ERROR_NULL_POINTER: |
7280 | 0 | return "Pointer is null"; |
7281 | 0 | case BLOSC2_ERROR_INVALID_INDEX: |
7282 | 0 | return "Invalid index"; |
7283 | 0 | case BLOSC2_ERROR_METALAYER_NOT_FOUND: |
7284 | 0 | return "Metalayer has not been found"; |
7285 | 0 | case BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED: |
7286 | 0 | return "Maximum buffersize exceeded"; |
7287 | 0 | case BLOSC2_ERROR_TUNER: |
7288 | 0 | return "Tuner failure"; |
7289 | 0 | default: |
7290 | 0 | return "Unknown error"; |
7291 | 41 | } |
7292 | 41 | } |