-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathblosc2.c
4765 lines (4185 loc) · 154 KB
/
blosc2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
Blosc - Blocked Shuffling and Compression Library
Copyright (c) 2021 Blosc Development Team <blosc@blosc.org>
https://blosc.org
License: BSD 3-Clause (see LICENSE.txt)
See LICENSE.txt for details about copyright and rights to use.
**********************************************************************/
#include "blosc2.h"
#include "blosc-private.h"
#include "../plugins/codecs/zfp/blosc2-zfp.h"
#include "frame.h"
#include "b2nd-private.h"
#include "schunk-private.h"
#if defined(USING_CMAKE)
#include "config.h"
#endif /* USING_CMAKE */
#include "context.h"
#include "shuffle.h"
#include "delta.h"
#include "trunc-prec.h"
#include "blosclz.h"
#include "stune.h"
#include "blosc2/codecs-registry.h"
#include "blosc2/filters-registry.h"
#include "blosc2/tuners-registry.h"
#include "lz4.h"
#include "lz4hc.h"
#ifdef HAVE_IPP
#include <ipps.h>
#include <ippdc.h>
#endif
#if defined(HAVE_ZLIB_NG)
#ifdef ZLIB_COMPAT
#include "zlib.h"
#else
#include "zlib-ng.h"
#endif
#elif defined(HAVE_ZLIB)
#include "zlib.h"
#endif /* HAVE_MINIZ */
#if defined(HAVE_ZSTD)
#include "zstd.h"
#include "zstd_errors.h"
// #include "cover.h" // for experimenting with fast cover training for building dicts
#include "zdict.h"
#endif /* HAVE_ZSTD */
#if defined(_WIN32) && !defined(__MINGW32__)
#include <windows.h>
#include <malloc.h>
#include <process.h>
#define getpid _getpid
#endif /* _WIN32 */
#if defined(_WIN32) && !defined(__GNUC__)
#include "win32/pthread.c"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#include <math.h>
/* Synchronization variables */
/* Global context for non-contextual API */
static blosc2_context* g_global_context;
static pthread_mutex_t global_comp_mutex;
static int g_compressor = BLOSC_BLOSCLZ;
static int g_delta = 0;
/* The default splitmode */
static int32_t g_splitmode = BLOSC_FORWARD_COMPAT_SPLIT;
/* the compressor to use by default */
static int16_t g_nthreads = 1;
static int32_t g_force_blocksize = 0;
static int g_initlib = 0;
static blosc2_schunk* g_schunk = NULL; /* the pointer to super-chunk */
blosc2_codec g_codecs[256] = {0};
uint8_t g_ncodecs = 0;
static blosc2_filter g_filters[256] = {0};
static uint64_t g_nfilters = 0;
static blosc2_io_cb g_ios[256] = {0};
static uint64_t g_nio = 0;
blosc2_tuner g_tuners[256] = {0};
int g_ntuners = 0;
static int g_tuner = BLOSC_STUNE;
// Forward declarations
int init_threadpool(blosc2_context *context);
int release_threadpool(blosc2_context *context);
/* Macros for synchronization */
/* Wait until all threads are initialized */
#ifdef BLOSC_POSIX_BARRIERS
#define WAIT_INIT(RET_VAL, CONTEXT_PTR) \
do { \
rc = pthread_barrier_wait(&(CONTEXT_PTR)->barr_init); \
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) { \
BLOSC_TRACE_ERROR("Could not wait on barrier (init): %d", rc); \
return((RET_VAL)); \
} \
} while (0)
#else
#define WAIT_INIT(RET_VAL, CONTEXT_PTR) \
do { \
pthread_mutex_lock(&(CONTEXT_PTR)->count_threads_mutex); \
if ((CONTEXT_PTR)->count_threads < (CONTEXT_PTR)->nthreads) { \
(CONTEXT_PTR)->count_threads++; \
pthread_cond_wait(&(CONTEXT_PTR)->count_threads_cv, \
&(CONTEXT_PTR)->count_threads_mutex); \
} \
else { \
pthread_cond_broadcast(&(CONTEXT_PTR)->count_threads_cv); \
} \
pthread_mutex_unlock(&(CONTEXT_PTR)->count_threads_mutex); \
} while (0)
#endif
/* Wait for all threads to finish */
#ifdef BLOSC_POSIX_BARRIERS
#define WAIT_FINISH(RET_VAL, CONTEXT_PTR) \
do { \
rc = pthread_barrier_wait(&(CONTEXT_PTR)->barr_finish); \
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) { \
BLOSC_TRACE_ERROR("Could not wait on barrier (finish)"); \
return((RET_VAL)); \
} \
} while (0)
#else
#define WAIT_FINISH(RET_VAL, CONTEXT_PTR) \
do { \
pthread_mutex_lock(&(CONTEXT_PTR)->count_threads_mutex); \
if ((CONTEXT_PTR)->count_threads > 0) { \
(CONTEXT_PTR)->count_threads--; \
pthread_cond_wait(&(CONTEXT_PTR)->count_threads_cv, \
&(CONTEXT_PTR)->count_threads_mutex); \
} \
else { \
pthread_cond_broadcast(&(CONTEXT_PTR)->count_threads_cv); \
} \
pthread_mutex_unlock(&(CONTEXT_PTR)->count_threads_mutex); \
} while (0)
#endif
/* global variable to change threading backend from Blosc-managed to caller-managed */
static blosc_threads_callback threads_callback = 0;
static void *threads_callback_data = 0;
/* non-threadsafe function should be called before any other Blosc function in
order to change how threads are managed */
void blosc2_set_threads_callback(blosc_threads_callback callback, void *callback_data)
{
threads_callback = callback;
threads_callback_data = callback_data;
}
/* A function for aligned malloc that is portable */
static uint8_t* my_malloc(size_t size) {
void* block = NULL;
int res = 0;
/* Do an alignment to 32 bytes because AVX2 is supported */
#if defined(_WIN32)
/* A (void *) cast needed for avoiding a warning with MINGW :-/ */
block = (void *)_aligned_malloc(size, 32);
#elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
/* Platform does have an implementation of posix_memalign */
res = posix_memalign(&block, 32, size);
#else
block = malloc(size);
#endif /* _WIN32 */
if (block == NULL || res != 0) {
BLOSC_TRACE_ERROR("Error allocating memory!");
return NULL;
}
return (uint8_t*)block;
}
/* Release memory booked by my_malloc */
static void my_free(void* block) {
#if defined(_WIN32)
_aligned_free(block);
#else
free(block);
#endif /* _WIN32 */
}
/*
* Conversion routines between compressor and compression libraries
*/
/* Return the library code associated with the compressor name */
static int compname_to_clibcode(const char* compname) {
if (strcmp(compname, BLOSC_BLOSCLZ_COMPNAME) == 0)
return BLOSC_BLOSCLZ_LIB;
if (strcmp(compname, BLOSC_LZ4_COMPNAME) == 0)
return BLOSC_LZ4_LIB;
if (strcmp(compname, BLOSC_LZ4HC_COMPNAME) == 0)
return BLOSC_LZ4_LIB;
if (strcmp(compname, BLOSC_ZLIB_COMPNAME) == 0)
return BLOSC_ZLIB_LIB;
if (strcmp(compname, BLOSC_ZSTD_COMPNAME) == 0)
return BLOSC_ZSTD_LIB;
for (int i = 0; i < g_ncodecs; ++i) {
if (strcmp(compname, g_codecs[i].compname) == 0)
return g_codecs[i].complib;
}
return BLOSC2_ERROR_NOT_FOUND;
}
/* Return the library name associated with the compressor code */
static const char* clibcode_to_clibname(int clibcode) {
if (clibcode == BLOSC_BLOSCLZ_LIB) return BLOSC_BLOSCLZ_LIBNAME;
if (clibcode == BLOSC_LZ4_LIB) return BLOSC_LZ4_LIBNAME;
if (clibcode == BLOSC_ZLIB_LIB) return BLOSC_ZLIB_LIBNAME;
if (clibcode == BLOSC_ZSTD_LIB) return BLOSC_ZSTD_LIBNAME;
for (int i = 0; i < g_ncodecs; ++i) {
if (clibcode == g_codecs[i].complib)
return g_codecs[i].compname;
}
return NULL; /* should never happen */
}
/*
* Conversion routines between compressor names and compressor codes
*/
/* Get the compressor name associated with the compressor code */
int blosc2_compcode_to_compname(int compcode, const char** compname) {
int code = -1; /* -1 means non-existent compressor code */
const char* name = NULL;
/* Map the compressor code */
if (compcode == BLOSC_BLOSCLZ)
name = BLOSC_BLOSCLZ_COMPNAME;
else if (compcode == BLOSC_LZ4)
name = BLOSC_LZ4_COMPNAME;
else if (compcode == BLOSC_LZ4HC)
name = BLOSC_LZ4HC_COMPNAME;
else if (compcode == BLOSC_ZLIB)
name = BLOSC_ZLIB_COMPNAME;
else if (compcode == BLOSC_ZSTD)
name = BLOSC_ZSTD_COMPNAME;
else {
for (int i = 0; i < g_ncodecs; ++i) {
if (compcode == g_codecs[i].compcode) {
name = g_codecs[i].compname;
break;
}
}
}
*compname = name;
/* Guess if there is support for this code */
if (compcode == BLOSC_BLOSCLZ)
code = BLOSC_BLOSCLZ;
else if (compcode == BLOSC_LZ4)
code = BLOSC_LZ4;
else if (compcode == BLOSC_LZ4HC)
code = BLOSC_LZ4HC;
#if defined(HAVE_ZLIB)
else if (compcode == BLOSC_ZLIB)
code = BLOSC_ZLIB;
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
else if (compcode == BLOSC_ZSTD)
code = BLOSC_ZSTD;
#endif /* HAVE_ZSTD */
else if (compcode >= BLOSC_LAST_CODEC)
code = compcode;
return code;
}
/* Get the compressor code for the compressor name. -1 if it is not available */
int blosc2_compname_to_compcode(const char* compname) {
int code = -1; /* -1 means non-existent compressor code */
if (strcmp(compname, BLOSC_BLOSCLZ_COMPNAME) == 0) {
code = BLOSC_BLOSCLZ;
}
else if (strcmp(compname, BLOSC_LZ4_COMPNAME) == 0) {
code = BLOSC_LZ4;
}
else if (strcmp(compname, BLOSC_LZ4HC_COMPNAME) == 0) {
code = BLOSC_LZ4HC;
}
#if defined(HAVE_ZLIB)
else if (strcmp(compname, BLOSC_ZLIB_COMPNAME) == 0) {
code = BLOSC_ZLIB;
}
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
else if (strcmp(compname, BLOSC_ZSTD_COMPNAME) == 0) {
code = BLOSC_ZSTD;
}
#endif /* HAVE_ZSTD */
else{
for (int i = 0; i < g_ncodecs; ++i) {
if (strcmp(compname, g_codecs[i].compname) == 0) {
code = g_codecs[i].compcode;
break;
}
}
}
return code;
}
/* Convert compressor code to blosc compressor format code */
static int compcode_to_compformat(int compcode) {
switch (compcode) {
case BLOSC_BLOSCLZ: return BLOSC_BLOSCLZ_FORMAT;
case BLOSC_LZ4: return BLOSC_LZ4_FORMAT;
case BLOSC_LZ4HC: return BLOSC_LZ4HC_FORMAT;
#if defined(HAVE_ZLIB)
case BLOSC_ZLIB: return BLOSC_ZLIB_FORMAT;
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
case BLOSC_ZSTD: return BLOSC_ZSTD_FORMAT;
break;
#endif /* HAVE_ZSTD */
default:
return BLOSC_UDCODEC_FORMAT;
}
BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
}
/* Convert compressor code to blosc compressor format version */
static int compcode_to_compversion(int compcode) {
/* Write compressor format */
switch (compcode) {
case BLOSC_BLOSCLZ:
return BLOSC_BLOSCLZ_VERSION_FORMAT;
case BLOSC_LZ4:
return BLOSC_LZ4_VERSION_FORMAT;
case BLOSC_LZ4HC:
return BLOSC_LZ4HC_VERSION_FORMAT;
#if defined(HAVE_ZLIB)
case BLOSC_ZLIB:
return BLOSC_ZLIB_VERSION_FORMAT;
break;
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
case BLOSC_ZSTD:
return BLOSC_ZSTD_VERSION_FORMAT;
break;
#endif /* HAVE_ZSTD */
default:
for (int i = 0; i < g_ncodecs; ++i) {
if (compcode == g_codecs[i].compcode) {
return g_codecs[i].version;
}
}
}
return BLOSC2_ERROR_FAILURE;
}
static int lz4_wrap_compress(const char* input, size_t input_length,
char* output, size_t maxout, int accel, void* hash_table) {
BLOSC_UNUSED_PARAM(accel);
int cbytes;
#ifdef HAVE_IPP
if (hash_table == NULL) {
return BLOSC2_ERROR_INVALID_PARAM; // the hash table should always be initialized
}
int outlen = (int)maxout;
int inlen = (int)input_length;
// I have not found any function that uses `accel` like in `LZ4_compress_fast`, but
// the IPP LZ4Safe call does a pretty good job on compressing well, so let's use it
IppStatus status = ippsEncodeLZ4Safe_8u((const Ipp8u*)input, &inlen,
(Ipp8u*)output, &outlen, (Ipp8u*)hash_table);
if (status == ippStsDstSizeLessExpected) {
return 0; // we cannot compress in required outlen
}
else if (status != ippStsNoErr) {
return BLOSC2_ERROR_FAILURE; // an unexpected error happened
}
cbytes = outlen;
#else
BLOSC_UNUSED_PARAM(hash_table);
accel = 1; // deactivate acceleration to match IPP behaviour
cbytes = LZ4_compress_fast(input, output, (int)input_length, (int)maxout, accel);
#endif
return cbytes;
}
static int lz4hc_wrap_compress(const char* input, size_t input_length,
char* output, size_t maxout, int clevel) {
int cbytes;
if (input_length > (size_t)(UINT32_C(2) << 30))
return BLOSC2_ERROR_2GB_LIMIT;
/* clevel for lz4hc goes up to 12, at least in LZ4 1.7.5
* but levels larger than 9 do not buy much compression. */
cbytes = LZ4_compress_HC(input, output, (int)input_length, (int)maxout,
clevel);
return cbytes;
}
static int lz4_wrap_decompress(const char* input, size_t compressed_length,
char* output, size_t maxout) {
int nbytes;
#ifdef HAVE_IPP
int outlen = (int)maxout;
int inlen = (int)compressed_length;
IppStatus status;
status = ippsDecodeLZ4_8u((const Ipp8u*)input, inlen, (Ipp8u*)output, &outlen);
//status = ippsDecodeLZ4Dict_8u((const Ipp8u*)input, &inlen, (Ipp8u*)output, 0, &outlen, NULL, 1 << 16);
nbytes = (status == ippStsNoErr) ? outlen : -outlen;
#else
nbytes = LZ4_decompress_safe(input, output, (int)compressed_length, (int)maxout);
#endif
if (nbytes != (int)maxout) {
return 0;
}
return (int)maxout;
}
#if defined(HAVE_ZLIB)
/* zlib is not very respectful with sharing name space with others.
Fortunately, its names do not collide with those already in blosc. */
static int zlib_wrap_compress(const char* input, size_t input_length,
char* output, size_t maxout, int clevel) {
int status;
#if defined(HAVE_ZLIB_NG) && ! defined(ZLIB_COMPAT)
size_t cl = maxout;
status = zng_compress2(
(uint8_t*)output, &cl, (uint8_t*)input, input_length, clevel);
#else
uLongf cl = (uLongf)maxout;
status = compress2(
(Bytef*)output, &cl, (Bytef*)input, (uLong)input_length, clevel);
#endif
if (status != Z_OK) {
return 0;
}
return (int)cl;
}
static int zlib_wrap_decompress(const char* input, size_t compressed_length,
char* output, size_t maxout) {
int status;
#if defined(HAVE_ZLIB_NG) && ! defined(ZLIB_COMPAT)
size_t ul = maxout;
status = zng_uncompress(
(uint8_t*)output, &ul, (uint8_t*)input, compressed_length);
#else
uLongf ul = (uLongf)maxout;
status = uncompress(
(Bytef*)output, &ul, (Bytef*)input, (uLong)compressed_length);
#endif
if (status != Z_OK) {
return 0;
}
return (int)ul;
}
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
static int zstd_wrap_compress(struct thread_context* thread_context,
const char* input, size_t input_length,
char* output, size_t maxout, int clevel) {
size_t code;
blosc2_context* context = thread_context->parent_context;
clevel = (clevel < 9) ? clevel * 2 - 1 : ZSTD_maxCLevel();
/* Make the level 8 close enough to maxCLevel */
if (clevel == 8) clevel = ZSTD_maxCLevel() - 2;
if (thread_context->zstd_cctx == NULL) {
thread_context->zstd_cctx = ZSTD_createCCtx();
}
if (context->use_dict) {
assert(context->dict_cdict != NULL);
code = ZSTD_compress_usingCDict(
thread_context->zstd_cctx, (void*)output, maxout, (void*)input,
input_length, context->dict_cdict);
} else {
code = ZSTD_compressCCtx(thread_context->zstd_cctx,
(void*)output, maxout, (void*)input, input_length, clevel);
}
if (ZSTD_isError(code) != ZSTD_error_no_error) {
// Blosc will just memcpy this buffer
return 0;
}
return (int)code;
}
static int zstd_wrap_decompress(struct thread_context* thread_context,
const char* input, size_t compressed_length,
char* output, size_t maxout) {
size_t code;
blosc2_context* context = thread_context->parent_context;
if (thread_context->zstd_dctx == NULL) {
thread_context->zstd_dctx = ZSTD_createDCtx();
}
if (context->use_dict) {
assert(context->dict_ddict != NULL);
code = ZSTD_decompress_usingDDict(
thread_context->zstd_dctx, (void*)output, maxout, (void*)input,
compressed_length, context->dict_ddict);
} else {
code = ZSTD_decompressDCtx(thread_context->zstd_dctx,
(void*)output, maxout, (void*)input, compressed_length);
}
if (ZSTD_isError(code) != ZSTD_error_no_error) {
BLOSC_TRACE_ERROR("Error in ZSTD decompression: '%s'. Giving up.",
ZDICT_getErrorName(code));
return 0;
}
return (int)code;
}
#endif /* HAVE_ZSTD */
/* Compute acceleration for blosclz */
static int get_accel(const blosc2_context* context) {
int clevel = context->clevel;
if (context->compcode == BLOSC_LZ4) {
/* This acceleration setting based on discussions held in:
* https://groups.google.com/forum/#!topic/lz4c/zosy90P8MQw
*/
return (10 - clevel);
}
return 1;
}
int do_nothing(uint8_t filter, char cmode) {
if (cmode == 'c') {
return (filter == BLOSC_NOFILTER);
} else {
// TRUNC_PREC do not have to be applied during decompression
return ((filter == BLOSC_NOFILTER) || (filter == BLOSC_TRUNC_PREC));
}
}
int next_filter(const uint8_t* filters, int current_filter, char cmode) {
for (int i = current_filter - 1; i >= 0; i--) {
if (!do_nothing(filters[i], cmode)) {
return filters[i];
}
}
return BLOSC_NOFILTER;
}
int last_filter(const uint8_t* filters, char cmode) {
int last_index = -1;
for (int i = BLOSC2_MAX_FILTERS - 1; i >= 0; i--) {
if (!do_nothing(filters[i], cmode)) {
last_index = i;
}
}
return last_index;
}
/* Convert filter pipeline to filter flags */
static uint8_t filters_to_flags(const uint8_t* filters) {
uint8_t flags = 0;
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) {
switch (filters[i]) {
case BLOSC_SHUFFLE:
flags |= BLOSC_DOSHUFFLE;
break;
case BLOSC_BITSHUFFLE:
flags |= BLOSC_DOBITSHUFFLE;
break;
case BLOSC_DELTA:
flags |= BLOSC_DODELTA;
break;
default :
break;
}
}
return flags;
}
/* Convert filter flags to filter pipeline */
static void flags_to_filters(const uint8_t flags, uint8_t* filters) {
/* Initialize the filter pipeline */
memset(filters, 0, BLOSC2_MAX_FILTERS);
/* Fill the filter pipeline */
if (flags & BLOSC_DOSHUFFLE)
filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_SHUFFLE;
if (flags & BLOSC_DOBITSHUFFLE)
filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_BITSHUFFLE;
if (flags & BLOSC_DODELTA)
filters[BLOSC2_MAX_FILTERS - 2] = BLOSC_DELTA;
}
/* Get filter flags from header flags */
static uint8_t get_filter_flags(const uint8_t header_flags,
const int32_t typesize) {
uint8_t flags = 0;
if ((header_flags & BLOSC_DOSHUFFLE) && (typesize > 1)) {
flags |= BLOSC_DOSHUFFLE;
}
if (header_flags & BLOSC_DOBITSHUFFLE) {
flags |= BLOSC_DOBITSHUFFLE;
}
if (header_flags & BLOSC_DODELTA) {
flags |= BLOSC_DODELTA;
}
if (header_flags & BLOSC_MEMCPYED) {
flags |= BLOSC_MEMCPYED;
}
return flags;
}
typedef struct blosc_header_s {
uint8_t version;
uint8_t versionlz;
uint8_t flags;
uint8_t typesize;
int32_t nbytes;
int32_t blocksize;
int32_t cbytes;
// Extended Blosc2 header
uint8_t filters[BLOSC2_MAX_FILTERS];
uint8_t udcompcode;
uint8_t compcode_meta;
uint8_t filters_meta[BLOSC2_MAX_FILTERS];
uint8_t reserved2;
uint8_t blosc2_flags;
} blosc_header;
int read_chunk_header(const uint8_t* src, int32_t srcsize, bool extended_header, blosc_header* header)
{
memset(header, 0, sizeof(blosc_header));
if (srcsize < BLOSC_MIN_HEADER_LENGTH) {
BLOSC_TRACE_ERROR("Not enough space to read Blosc header.");
return BLOSC2_ERROR_READ_BUFFER;
}
memcpy(header, src, BLOSC_MIN_HEADER_LENGTH);
bool little_endian = is_little_endian();
if (!little_endian) {
header->nbytes = bswap32_(header->nbytes);
header->blocksize = bswap32_(header->blocksize);
header->cbytes = bswap32_(header->cbytes);
}
if (header->version > BLOSC2_VERSION_FORMAT) {
/* Version from future */
return BLOSC2_ERROR_VERSION_SUPPORT;
}
if (header->cbytes < BLOSC_MIN_HEADER_LENGTH) {
BLOSC_TRACE_ERROR("`cbytes` is too small to read min header.");
return BLOSC2_ERROR_INVALID_HEADER;
}
if (header->blocksize <= 0 || (header->nbytes > 0 && (header->blocksize > header->nbytes))) {
BLOSC_TRACE_ERROR("`blocksize` is zero or greater than uncompressed size");
return BLOSC2_ERROR_INVALID_HEADER;
}
if (header->blocksize > BLOSC2_MAXBLOCKSIZE) {
BLOSC_TRACE_ERROR("`blocksize` greater than maximum allowed");
return BLOSC2_ERROR_INVALID_HEADER;
}
if (header->typesize == 0) {
BLOSC_TRACE_ERROR("`typesize` is zero.");
return BLOSC2_ERROR_INVALID_HEADER;
}
/* Read extended header if it is wanted */
if ((extended_header) && (header->flags & BLOSC_DOSHUFFLE) && (header->flags & BLOSC_DOBITSHUFFLE)) {
if (header->cbytes < BLOSC_EXTENDED_HEADER_LENGTH) {
BLOSC_TRACE_ERROR("`cbytes` is too small to read extended header.");
return BLOSC2_ERROR_INVALID_HEADER;
}
if (srcsize < BLOSC_EXTENDED_HEADER_LENGTH) {
BLOSC_TRACE_ERROR("Not enough space to read Blosc extended header.");
return BLOSC2_ERROR_READ_BUFFER;
}
memcpy((uint8_t *)header + BLOSC_MIN_HEADER_LENGTH, src + BLOSC_MIN_HEADER_LENGTH,
BLOSC_EXTENDED_HEADER_LENGTH - BLOSC_MIN_HEADER_LENGTH);
int32_t special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK;
if (special_type != 0) {
if (special_type == BLOSC2_SPECIAL_VALUE) {
// In this case, the actual type size must be derived from the cbytes
int32_t typesize = header->cbytes - BLOSC_EXTENDED_HEADER_LENGTH;
if (typesize <= 0) {
BLOSC_TRACE_ERROR("`typesize` is zero or negative");
return BLOSC2_ERROR_INVALID_HEADER;
}
if (typesize > BLOSC2_MAXTYPESIZE) {
BLOSC_TRACE_ERROR("`typesize` is greater than maximum allowed");
return BLOSC2_ERROR_INVALID_HEADER;
}
if (typesize > header->nbytes) {
BLOSC_TRACE_ERROR("`typesize` is greater than `nbytes`");
return BLOSC2_ERROR_INVALID_HEADER;
}
if (header->nbytes % typesize != 0) {
BLOSC_TRACE_ERROR("`nbytes` is not a multiple of typesize");
return BLOSC2_ERROR_INVALID_HEADER;
}
}
else {
if (header->nbytes % header->typesize != 0) {
BLOSC_TRACE_ERROR("`nbytes` is not a multiple of typesize");
return BLOSC2_ERROR_INVALID_HEADER;
}
}
}
// The number of filters depends on the version of the header. Blosc2 alpha series
// did not initialize filters to zero beyond the max supported.
if (header->version == BLOSC2_VERSION_FORMAT_ALPHA) {
header->filters[5] = 0;
header->filters_meta[5] = 0;
}
}
else {
flags_to_filters(header->flags, header->filters);
}
return 0;
}
static inline void blosc2_calculate_blocks(blosc2_context* context) {
/* Compute number of blocks in buffer */
context->nblocks = context->sourcesize / context->blocksize;
context->leftover = context->sourcesize % context->blocksize;
context->nblocks = (context->leftover > 0) ?
(context->nblocks + 1) : context->nblocks;
}
static int blosc2_initialize_context_from_header(blosc2_context* context, blosc_header* header) {
context->header_flags = header->flags;
context->typesize = header->typesize;
context->sourcesize = header->nbytes;
context->blocksize = header->blocksize;
context->blosc2_flags = header->blosc2_flags;
context->compcode = header->flags >> 5;
if (context->compcode == BLOSC_UDCODEC_FORMAT) {
context->compcode = header->udcompcode;
}
blosc2_calculate_blocks(context);
bool is_lazy = false;
if ((context->header_flags & BLOSC_DOSHUFFLE) &&
(context->header_flags & BLOSC_DOBITSHUFFLE)) {
/* Extended header */
context->header_overhead = BLOSC_EXTENDED_HEADER_LENGTH;
memcpy(context->filters, header->filters, BLOSC2_MAX_FILTERS);
memcpy(context->filters_meta, header->filters_meta, BLOSC2_MAX_FILTERS);
context->compcode_meta = header->compcode_meta;
context->filter_flags = filters_to_flags(header->filters);
context->special_type = (header->blosc2_flags >> 4) & BLOSC2_SPECIAL_MASK;
is_lazy = (context->blosc2_flags & 0x08u);
}
else {
context->header_overhead = BLOSC_MIN_HEADER_LENGTH;
context->filter_flags = get_filter_flags(context->header_flags, context->typesize);
flags_to_filters(context->header_flags, context->filters);
}
// Some checks for malformed headers
if (!is_lazy && header->cbytes > context->srcsize) {
return BLOSC2_ERROR_INVALID_HEADER;
}
return 0;
}
int fill_filter(blosc2_filter *filter) {
char libpath[PATH_MAX];
void *lib = load_lib(filter->name, libpath);
if(lib == NULL) {
BLOSC_TRACE_ERROR("Error while loading the library");
return BLOSC2_ERROR_FAILURE;
}
filter_info *info = dlsym(lib, "info");
filter->forward = dlsym(lib, info->forward);
filter->backward = dlsym(lib, info->backward);
if (filter->forward == NULL || filter->backward == NULL){
BLOSC_TRACE_ERROR("Wrong library loaded");
dlclose(lib);
return BLOSC2_ERROR_FAILURE;
}
return BLOSC2_ERROR_SUCCESS;
}
int fill_codec(blosc2_codec *codec) {
char libpath[PATH_MAX];
void *lib = load_lib(codec->compname, libpath);
if(lib == NULL) {
BLOSC_TRACE_ERROR("Error while loading the library for codec `%s`", codec->compname);
return BLOSC2_ERROR_FAILURE;
}
codec_info *info = dlsym(lib, "info");
if (info == NULL) {
BLOSC_TRACE_ERROR("`info` symbol cannot be loaded from plugin `%s`", codec->compname);
dlclose(lib);
return BLOSC2_ERROR_FAILURE;
}
codec->encoder = dlsym(lib, info->encoder);
codec->decoder = dlsym(lib, info->decoder);
if (codec->encoder == NULL || codec->decoder == NULL) {
BLOSC_TRACE_ERROR("encoder or decoder cannot be loaded from plugin `%s`", codec->compname);
dlclose(lib);
return BLOSC2_ERROR_FAILURE;
}
return BLOSC2_ERROR_SUCCESS;
}
int fill_tuner(blosc2_tuner *tuner) {
char libpath[PATH_MAX] = {0};
void *lib = load_lib(tuner->name, libpath);
if(lib == NULL) {
BLOSC_TRACE_ERROR("Error while loading the library");
return BLOSC2_ERROR_FAILURE;
}
tuner_info *info = dlsym(lib, "info");
tuner->init = dlsym(lib, info->init);
tuner->update = dlsym(lib, info->update);
tuner->next_blocksize = dlsym(lib, info->next_blocksize);
tuner->free = dlsym(lib, info->free);
tuner->next_cparams = dlsym(lib, info->next_cparams);
if (tuner->init == NULL || tuner->update == NULL || tuner->next_blocksize == NULL || tuner->free == NULL
|| tuner->next_cparams == NULL){
BLOSC_TRACE_ERROR("Wrong library loaded");
dlclose(lib);
return BLOSC2_ERROR_FAILURE;
}
return BLOSC2_ERROR_SUCCESS;
}
static int blosc2_intialize_header_from_context(blosc2_context* context, blosc_header* header, bool extended_header) {
memset(header, 0, sizeof(blosc_header));
header->version = BLOSC2_VERSION_FORMAT;
header->versionlz = compcode_to_compversion(context->compcode);
header->flags = context->header_flags;
header->typesize = (uint8_t)context->typesize;
header->nbytes = (int32_t)context->sourcesize;
header->blocksize = (int32_t)context->blocksize;
int little_endian = is_little_endian();
if (!little_endian) {
header->nbytes = bswap32_(header->nbytes);
header->blocksize = bswap32_(header->blocksize);
// cbytes written after compression
}
if (extended_header) {
/* Store filter pipeline info at the end of the header */
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) {
header->filters[i] = context->filters[i];
header->filters_meta[i] = context->filters_meta[i];
}
header->udcompcode = context->compcode;
header->compcode_meta = context->compcode_meta;
if (!little_endian) {
header->blosc2_flags |= BLOSC2_BIGENDIAN;
}
if (context->use_dict) {
header->blosc2_flags |= BLOSC2_USEDICT;
}
if (context->blosc2_flags & BLOSC2_INSTR_CODEC) {
header->blosc2_flags |= BLOSC2_INSTR_CODEC;
}
}
return 0;
}
void _cycle_buffers(uint8_t **src, uint8_t **dest, uint8_t **tmp) {
uint8_t *tmp2 = *src;
*src = *dest;
*dest = *tmp;
*tmp = tmp2;
}
uint8_t* pipeline_forward(struct thread_context* thread_context, const int32_t bsize,
const uint8_t* src, const int32_t offset,
uint8_t* dest, uint8_t* tmp) {
blosc2_context* context = thread_context->parent_context;
uint8_t* _src = (uint8_t*)src + offset;
uint8_t* _tmp = tmp;
uint8_t* _dest = dest;
int32_t typesize = context->typesize;
uint8_t* filters = context->filters;
uint8_t* filters_meta = context->filters_meta;
bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED;
/* Prefilter function */
if (context->prefilter != NULL) {
/* Set unwritten values to zero */
memset(_dest, 0, bsize);
// Create new prefilter parameters for this block (must be private for each thread)
blosc2_prefilter_params preparams;
memcpy(&preparams, context->preparams, sizeof(preparams));
preparams.input = _src;
preparams.output = _dest;
preparams.output_size = bsize;
preparams.output_typesize = typesize;
preparams.output_offset = offset;
preparams.nblock = offset / context->blocksize;
preparams.nchunk = context->schunk != NULL ? context->schunk->current_nchunk : -1;
preparams.tid = thread_context->tid;
preparams.ttmp = thread_context->tmp;
preparams.ttmp_nbytes = thread_context->tmp_nbytes;
preparams.ctx = context;
if (context->prefilter(&preparams) != 0) {
BLOSC_TRACE_ERROR("Execution of prefilter function failed");
return NULL;
}
if (memcpyed) {
// No more filters are required
return _dest;
}
_cycle_buffers(&_src, &_dest, &_tmp);
}
/* Process the filter pipeline */
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) {
int rc = BLOSC2_ERROR_SUCCESS;
if (filters[i] <= BLOSC2_DEFINED_FILTERS_STOP) {
switch (filters[i]) {
case BLOSC_SHUFFLE:
shuffle(typesize, bsize, _src, _dest);
break;
case BLOSC_BITSHUFFLE:
if (bitshuffle(typesize, bsize, _src, _dest) < 0) {
return NULL;
}
break;
case BLOSC_DELTA:
delta_encoder(src, offset, bsize, typesize, _src, _dest);
break;
case BLOSC_TRUNC_PREC:
if (truncate_precision(filters_meta[i], typesize, bsize, _src, _dest) < 0) {