-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathblosc.c
2303 lines (1991 loc) · 68.1 KB
/
blosc.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
Author: Francesc Alted <[email protected]>
Creation date: 2009-05-20
See LICENSE.txt for details about copyright and rights to use.
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#include "fastcopy.h"
#if defined(USING_CMAKE)
#include "config.h"
#endif /* USING_CMAKE */
#include "blosc.h"
#include "shuffle.h"
#include "blosclz.h"
#if defined(HAVE_LZ4)
#include "lz4.h"
#include "lz4hc.h"
#endif /* HAVE_LZ4 */
#if defined(HAVE_SNAPPY)
#include "snappy-c.h"
#endif /* HAVE_SNAPPY */
#if defined(HAVE_ZLIB)
#include "zlib.h"
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
#include "zstd.h"
#endif /* HAVE_ZSTD */
#if defined(_WIN32) && !defined(__MINGW32__)
#include <windows.h>
#include <malloc.h>
/* stdint.h only available in VS2010 (VC++ 16.0) and newer */
#if defined(_MSC_VER) && _MSC_VER < 1600
#include "win32/stdint-windows.h"
#else
#include <stdint.h>
#endif
#include <process.h>
#define getpid _getpid
#else
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h>
#endif /* _WIN32 */
/* Include the win32/pthread.h library for all the Windows builds. See #224. */
#if defined(_WIN32)
#include "win32/pthread.h"
#include "win32/pthread.c"
#else
#include <pthread.h>
#endif
/* Some useful units */
#define KB 1024
#define MB (1024 * (KB))
/* Minimum buffer size to be compressed */
#define MIN_BUFFERSIZE 128 /* Cannot be smaller than 66 */
/* The maximum number of splits in a block for compression */
#define MAX_SPLITS 16 /* Cannot be larger than 128 */
/* The size of L1 cache. 32 KB is quite common nowadays. */
#define L1 (32 * (KB))
/* Have problems using posix barriers when symbol value is 200112L */
/* This requires more investigation, but will work for the moment */
#if defined(_POSIX_BARRIERS) && ( (_POSIX_BARRIERS - 20012L) >= 0 && _POSIX_BARRIERS != 200112L)
#define _POSIX_BARRIERS_MINE
#endif
/* Synchronization variables */
struct blosc_context {
int32_t compress; /* 1 if we are doing compression 0 if decompress */
const uint8_t* src;
uint8_t* dest; /* The current pos in the destination buffer */
uint8_t* header_flags; /* Flags for header */
int compversion; /* Compressor version byte, only used during decompression */
int32_t sourcesize; /* Number of bytes in source buffer (or uncompressed bytes in compressed file) */
int32_t compressedsize; /* Number of bytes of compressed data (only used when decompressing) */
int32_t nblocks; /* Number of total blocks in buffer */
int32_t leftover; /* Extra bytes at end of buffer */
int32_t blocksize; /* Length of the block in bytes */
int32_t typesize; /* Type size */
int32_t num_output_bytes; /* Counter for the number of output bytes */
int32_t destsize; /* Maximum size for destination buffer */
uint8_t* bstarts; /* Start of the buffer past header info */
int32_t compcode; /* Compressor code to use */
int clevel; /* Compression level (1-9) */
/* Function to use for decompression. Only used when decompression */
int (*decompress_func)(const void* input, int compressed_length, void* output,
int maxout);
/* Threading */
int32_t numthreads;
int32_t threads_started;
int32_t end_threads;
pthread_t threads[BLOSC_MAX_THREADS];
int32_t tids[BLOSC_MAX_THREADS];
pthread_mutex_t count_mutex;
#ifdef _POSIX_BARRIERS_MINE
pthread_barrier_t barr_init;
pthread_barrier_t barr_finish;
#else
int32_t count_threads;
pthread_mutex_t count_threads_mutex;
pthread_cond_t count_threads_cv;
#endif
#if !defined(_WIN32)
pthread_attr_t ct_attr; /* creation time attrs for threads */
#endif
int32_t thread_giveup_code; /* error code when give up */
int32_t thread_nblock; /* block counter */
};
struct thread_context {
struct blosc_context* parent_context;
int32_t tid;
uint8_t* tmp;
uint8_t* tmp2;
uint8_t* tmp3;
int32_t tmpblocksize; /* Used to keep track of how big the temporary buffers are */
};
/* Global context for non-contextual API */
static struct blosc_context* g_global_context;
static pthread_mutex_t* global_comp_mutex;
static int32_t g_compressor = BLOSC_BLOSCLZ; /* the compressor to use by default */
static int32_t g_threads = 1;
static int32_t g_force_blocksize = 0;
static int32_t g_initlib = 0;
static int32_t g_atfork_registered = 0;
static int32_t g_splitmode = BLOSC_FORWARD_COMPAT_SPLIT;
/* Wrapped function to adjust the number of threads used by blosc */
int blosc_set_nthreads_(struct blosc_context*);
/* Releases the global threadpool */
int blosc_release_threadpool(struct blosc_context* context);
/* Macros for synchronization */
/* Wait until all threads are initialized */
#ifdef _POSIX_BARRIERS_MINE
#define WAIT_INIT(RET_VAL, CONTEXT_PTR) \
rc = pthread_barrier_wait(&CONTEXT_PTR->barr_init); \
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) { \
printf("Could not wait on barrier (init): %d\n", rc); \
return((RET_VAL)); \
}
#else
#define WAIT_INIT(RET_VAL, CONTEXT_PTR) \
pthread_mutex_lock(&CONTEXT_PTR->count_threads_mutex); \
if (CONTEXT_PTR->count_threads < CONTEXT_PTR->numthreads) { \
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);
#endif
/* Wait for all threads to finish */
#ifdef _POSIX_BARRIERS_MINE
#define WAIT_FINISH(RET_VAL, CONTEXT_PTR) \
rc = pthread_barrier_wait(&CONTEXT_PTR->barr_finish); \
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) { \
printf("Could not wait on barrier (finish)\n"); \
return((RET_VAL)); \
}
#else
#define WAIT_FINISH(RET_VAL, CONTEXT_PTR) \
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);
#endif
/* 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) {
printf("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 */
}
/* Copy 4 bytes from `*pa` to int32_t, changing endianness if necessary. */
static int32_t sw32_(const uint8_t *pa)
{
int32_t idest;
uint8_t *dest = (uint8_t *)&idest;
int i = 1; /* for big/little endian detection */
char *p = (char *)&i;
if (p[0] != 1) {
/* big endian */
dest[0] = pa[3];
dest[1] = pa[2];
dest[2] = pa[1];
dest[3] = pa[0];
}
else {
/* little endian */
dest[0] = pa[0];
dest[1] = pa[1];
dest[2] = pa[2];
dest[3] = pa[3];
}
return idest;
}
/* Copy 4 bytes from `*pa` to `*dest`, changing endianness if necessary. */
static void _sw32(uint8_t* dest, int32_t a)
{
uint8_t *pa = (uint8_t *)&a;
int i = 1; /* for big/little endian detection */
char *p = (char *)&i;
if (p[0] != 1) {
/* big endian */
dest[0] = pa[3];
dest[1] = pa[2];
dest[2] = pa[1];
dest[3] = pa[0];
}
else {
/* little endian */
dest[0] = pa[0];
dest[1] = pa[1];
dest[2] = pa[2];
dest[3] = pa[3];
}
}
/*
* 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_SNAPPY_COMPNAME) == 0)
return BLOSC_SNAPPY_LIB;
if (strcmp(compname, BLOSC_ZLIB_COMPNAME) == 0)
return BLOSC_ZLIB_LIB;
if (strcmp(compname, BLOSC_ZSTD_COMPNAME) == 0)
return BLOSC_ZSTD_LIB;
return -1;
}
/* 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_SNAPPY_LIB) return BLOSC_SNAPPY_LIBNAME;
if (clibcode == BLOSC_ZLIB_LIB) return BLOSC_ZLIB_LIBNAME;
if (clibcode == BLOSC_ZSTD_LIB) return BLOSC_ZSTD_LIBNAME;
return NULL; /* should never happen */
}
/*
* Conversion routines between compressor names and compressor codes
*/
/* Get the compressor name associated with the compressor code */
int blosc_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_SNAPPY)
name = BLOSC_SNAPPY_COMPNAME;
else if (compcode == BLOSC_ZLIB)
name = BLOSC_ZLIB_COMPNAME;
else if (compcode == BLOSC_ZSTD)
name = BLOSC_ZSTD_COMPNAME;
*compname = name;
/* Guess if there is support for this code */
if (compcode == BLOSC_BLOSCLZ)
code = BLOSC_BLOSCLZ;
#if defined(HAVE_LZ4)
else if (compcode == BLOSC_LZ4)
code = BLOSC_LZ4;
else if (compcode == BLOSC_LZ4HC)
code = BLOSC_LZ4HC;
#endif /* HAVE_LZ4 */
#if defined(HAVE_SNAPPY)
else if (compcode == BLOSC_SNAPPY)
code = BLOSC_SNAPPY;
#endif /* HAVE_SNAPPY */
#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 */
return code;
}
/* Get the compressor code for the compressor name. -1 if it is not available */
int blosc_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;
}
#if defined(HAVE_LZ4)
else if (strcmp(compname, BLOSC_LZ4_COMPNAME) == 0) {
code = BLOSC_LZ4;
}
else if (strcmp(compname, BLOSC_LZ4HC_COMPNAME) == 0) {
code = BLOSC_LZ4HC;
}
#endif /* HAVE_LZ4 */
#if defined(HAVE_SNAPPY)
else if (strcmp(compname, BLOSC_SNAPPY_COMPNAME) == 0) {
code = BLOSC_SNAPPY;
}
#endif /* HAVE_SNAPPY */
#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 */
return code;
}
#if defined(HAVE_LZ4)
static int lz4_wrap_compress(const char* input, size_t input_length,
char* output, size_t maxout, int accel)
{
int cbytes;
cbytes = LZ4_compress_fast(input, output, (int)input_length, (int)maxout,
accel);
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 -1; /* input larger than 2 GB is not supported */
/* 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 void* input, int compressed_length,
void* output, int maxout)
{
return LZ4_decompress_safe(input, output, compressed_length, maxout);
}
#endif /* HAVE_LZ4 */
#if defined(HAVE_SNAPPY)
static int snappy_wrap_compress(const char* input, size_t input_length,
char* output, size_t maxout)
{
snappy_status status;
size_t cl = maxout;
status = snappy_compress(input, input_length, output, &cl);
if (status != SNAPPY_OK){
return 0;
}
return (int)cl;
}
static int snappy_wrap_decompress(const void* input, int compressed_length,
void* output, int maxout)
{
snappy_status status;
size_t ul = maxout;
status = snappy_uncompress(input, compressed_length, output, &ul);
if (status != SNAPPY_OK){
return 0;
}
return (int)ul;
}
#endif /* HAVE_SNAPPY */
#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;
uLongf cl = maxout;
status = compress2(
(Bytef*)output, &cl, (Bytef*)input, (uLong)input_length, clevel);
if (status != Z_OK){
return 0;
}
return (int)cl;
}
static int zlib_wrap_decompress(const void* input, int compressed_length,
void* output, int maxout) {
int status;
uLongf ul = maxout;
status = uncompress(
(Bytef*)output, &ul, (Bytef*)input, (uLong)compressed_length);
if (status != Z_OK){
return 0;
}
return (int)ul;
}
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
static int zstd_wrap_compress(const char* input, size_t input_length,
char* output, size_t maxout, int clevel) {
size_t code;
clevel = (clevel < 9) ? clevel * 2 - 1 : ZSTD_maxCLevel();
/* Make the level 8 close enough to maxCLevel */
if (clevel == 8) clevel = ZSTD_maxCLevel() - 2;
code = ZSTD_compress(
(void*)output, maxout, (void*)input, input_length, clevel);
if (ZSTD_isError(code)) {
return 0;
}
return (int)code;
}
static int zstd_wrap_decompress(const void* input, int compressed_length,
void* output, int maxout) {
size_t code;
code = ZSTD_decompress(
(void*)output, maxout, (void*)input, compressed_length);
if (ZSTD_isError(code)) {
return 0;
}
return (int)code;
}
#endif /* HAVE_ZSTD */
static int initialize_decompress_func(struct blosc_context* context) {
int8_t header_flags = *(context->header_flags);
int32_t compformat = (header_flags & 0xe0) >> 5;
int compversion = context->compversion;
if (compformat == BLOSC_BLOSCLZ_FORMAT) {
if (compversion != BLOSC_BLOSCLZ_VERSION_FORMAT) {
return -9;
}
context->decompress_func = &blosclz_decompress;
return 0;
}
#if defined(HAVE_LZ4)
if (compformat == BLOSC_LZ4_FORMAT) {
if (compversion != BLOSC_LZ4_VERSION_FORMAT) {
return -9;
}
context->decompress_func = &lz4_wrap_decompress;
return 0;
}
#endif /* HAVE_LZ4 */
#if defined(HAVE_SNAPPY)
if (compformat == BLOSC_SNAPPY_FORMAT) {
if (compversion != BLOSC_SNAPPY_VERSION_FORMAT) {
return -9;
}
context->decompress_func = &snappy_wrap_decompress;
return 0;
}
#endif /* HAVE_SNAPPY */
#if defined(HAVE_ZLIB)
if (compformat == BLOSC_ZLIB_FORMAT) {
if (compversion != BLOSC_ZLIB_VERSION_FORMAT) {
return -9;
}
context->decompress_func = &zlib_wrap_decompress;
return 0;
}
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
if (compformat == BLOSC_ZSTD_FORMAT) {
if (compversion != BLOSC_ZSTD_VERSION_FORMAT) {
return -9;
}
context->decompress_func = &zstd_wrap_decompress;
return 0;
}
#endif /* HAVE_ZSTD */
return -5; /* signals no decompression support */
}
/* Compute acceleration for blosclz */
static int get_accel(const struct blosc_context* context) {
int32_t 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;
}
/* Shuffle & compress a single block */
static int blosc_c(const struct blosc_context* context, int32_t blocksize,
int32_t leftoverblock, int32_t ntbytes, int32_t maxbytes,
const uint8_t *src, uint8_t *dest, uint8_t *tmp,
uint8_t *tmp2)
{
int8_t header_flags = *(context->header_flags);
int dont_split = (header_flags & 0x10) >> 4;
int32_t j, neblock, nsplits;
int32_t cbytes; /* number of compressed bytes in split */
int32_t ctbytes = 0; /* number of compressed bytes in block */
int32_t maxout;
int32_t typesize = context->typesize;
const uint8_t *_tmp = src;
const char *compname;
int accel;
int bscount;
int doshuffle = (header_flags & BLOSC_DOSHUFFLE) && (typesize > 1);
int dobitshuffle = ((header_flags & BLOSC_DOBITSHUFFLE) &&
(blocksize >= typesize));
if (doshuffle) {
/* Byte shuffling only makes sense if typesize > 1 */
blosc_internal_shuffle(typesize, blocksize, src, tmp);
_tmp = tmp;
}
/* We don't allow more than 1 filter at the same time (yet) */
else if (dobitshuffle) {
bscount = blosc_internal_bitshuffle(typesize, blocksize, src, tmp, tmp2);
if (bscount < 0)
return bscount;
_tmp = tmp;
}
/* Calculate acceleration for different compressors */
accel = get_accel(context);
/* The number of splits for this block */
if (!dont_split && !leftoverblock) {
nsplits = typesize;
}
else {
nsplits = 1;
}
neblock = blocksize / nsplits;
for (j = 0; j < nsplits; j++) {
dest += sizeof(int32_t);
ntbytes += (int32_t)sizeof(int32_t);
ctbytes += (int32_t)sizeof(int32_t);
maxout = neblock;
#if defined(HAVE_SNAPPY)
if (context->compcode == BLOSC_SNAPPY) {
/* TODO perhaps refactor this to keep the value stashed somewhere */
maxout = snappy_max_compressed_length(neblock);
}
#endif /* HAVE_SNAPPY */
if (ntbytes+maxout > maxbytes) {
maxout = maxbytes - ntbytes; /* avoid buffer overrun */
if (maxout <= 0) {
return 0; /* non-compressible block */
}
}
if (context->compcode == BLOSC_BLOSCLZ) {
cbytes = blosclz_compress(context->clevel, _tmp+j*neblock, neblock,
dest, maxout, !dont_split);
}
#if defined(HAVE_LZ4)
else if (context->compcode == BLOSC_LZ4) {
cbytes = lz4_wrap_compress((char *)_tmp+j*neblock, (size_t)neblock,
(char *)dest, (size_t)maxout, accel);
}
else if (context->compcode == BLOSC_LZ4HC) {
cbytes = lz4hc_wrap_compress((char *)_tmp+j*neblock, (size_t)neblock,
(char *)dest, (size_t)maxout,
context->clevel);
}
#endif /* HAVE_LZ4 */
#if defined(HAVE_SNAPPY)
else if (context->compcode == BLOSC_SNAPPY) {
cbytes = snappy_wrap_compress((char *)_tmp+j*neblock, (size_t)neblock,
(char *)dest, (size_t)maxout);
}
#endif /* HAVE_SNAPPY */
#if defined(HAVE_ZLIB)
else if (context->compcode == BLOSC_ZLIB) {
cbytes = zlib_wrap_compress((char *)_tmp+j*neblock, (size_t)neblock,
(char *)dest, (size_t)maxout,
context->clevel);
}
#endif /* HAVE_ZLIB */
#if defined(HAVE_ZSTD)
else if (context->compcode == BLOSC_ZSTD) {
cbytes = zstd_wrap_compress((char*)_tmp + j * neblock, (size_t)neblock,
(char*)dest, (size_t)maxout, context->clevel);
}
#endif /* HAVE_ZSTD */
else {
blosc_compcode_to_compname(context->compcode, &compname);
if (compname == NULL) {
compname = "(null)";
}
fprintf(stderr, "Blosc has not been compiled with '%s' ", compname);
fprintf(stderr, "compression support. Please use one having it.");
return -5; /* signals no compression support */
}
if (cbytes > maxout) {
/* Buffer overrun caused by compression (should never happen) */
return -1;
}
else if (cbytes < 0) {
/* cbytes should never be negative */
return -2;
}
else if (cbytes == 0 || cbytes == neblock) {
/* The compressor has been unable to compress data at all. */
/* Before doing the copy, check that we are not running into a
buffer overflow. */
if ((ntbytes+neblock) > maxbytes) {
return 0; /* Non-compressible data */
}
fastcopy(dest, _tmp + j * neblock, neblock);
cbytes = neblock;
}
_sw32(dest - 4, cbytes);
dest += cbytes;
ntbytes += cbytes;
ctbytes += cbytes;
} /* Closes j < nsplits */
return ctbytes;
}
/* Decompress & unshuffle a single block */
static int blosc_d(struct blosc_context* context, int32_t blocksize,
int32_t leftoverblock, const uint8_t* base_src,
int32_t src_offset, uint8_t* dest, uint8_t* tmp,
uint8_t* tmp2) {
int8_t header_flags = *(context->header_flags);
int dont_split = (header_flags & 0x10) >> 4;
int32_t j, neblock, nsplits;
int32_t nbytes; /* number of decompressed bytes in split */
const int32_t compressedsize = context->compressedsize;
int32_t cbytes; /* number of compressed bytes in split */
int32_t ntbytes = 0; /* number of uncompressed bytes in block */
uint8_t *_tmp = dest;
int32_t typesize = context->typesize;
int bscount;
int doshuffle = (header_flags & BLOSC_DOSHUFFLE) && (typesize > 1);
int dobitshuffle = ((header_flags & BLOSC_DOBITSHUFFLE) &&
(blocksize >= typesize));
const uint8_t* src;
if (doshuffle || dobitshuffle) {
_tmp = tmp;
}
/* The number of splits for this block */
if (!dont_split &&
/* For compatibility with before the introduction of the split flag */
((typesize <= MAX_SPLITS) && (blocksize/typesize) >= MIN_BUFFERSIZE) &&
!leftoverblock) {
nsplits = typesize;
}
else {
nsplits = 1;
}
neblock = blocksize / nsplits;
for (j = 0; j < nsplits; j++) {
/* Validate src_offset */
if (src_offset < 0 || src_offset > compressedsize - sizeof(int32_t)) {
return -1;
}
cbytes = sw32_(base_src + src_offset); /* amount of compressed bytes */
src_offset += sizeof(int32_t);
/* Validate cbytes */
if (cbytes < 0 || cbytes > context->compressedsize - src_offset) {
return -1;
}
src = base_src + src_offset;
/* Uncompress */
if (cbytes == neblock) {
fastcopy(_tmp, src, neblock);
nbytes = neblock;
}
else {
nbytes = context->decompress_func(src, cbytes, _tmp, neblock);
/* Check that decompressed bytes number is correct */
if (nbytes != neblock) {
return -2;
}
}
src_offset += cbytes;
_tmp += nbytes;
ntbytes += nbytes;
} /* Closes j < nsplits */
if (doshuffle) {
blosc_internal_unshuffle(typesize, blocksize, tmp, dest);
}
else if (dobitshuffle) {
bscount = blosc_internal_bitunshuffle(typesize, blocksize, tmp, dest, tmp2);
if (bscount < 0)
return bscount;
}
/* Return the number of uncompressed bytes */
return ntbytes;
}
/* Serial version for compression/decompression */
static int serial_blosc(struct blosc_context* context)
{
int32_t j, bsize, leftoverblock;
int32_t cbytes;
int32_t ebsize = context->blocksize + context->typesize * (int32_t)sizeof(int32_t);
int32_t ntbytes = context->num_output_bytes;
uint8_t *tmp = my_malloc(context->blocksize + ebsize);
uint8_t *tmp2 = tmp + context->blocksize;
for (j = 0; j < context->nblocks; j++) {
if (context->compress && !(*(context->header_flags) & BLOSC_MEMCPYED)) {
_sw32(context->bstarts + j * 4, ntbytes);
}
bsize = context->blocksize;
leftoverblock = 0;
if ((j == context->nblocks - 1) && (context->leftover > 0)) {
bsize = context->leftover;
leftoverblock = 1;
}
if (context->compress) {
if (*(context->header_flags) & BLOSC_MEMCPYED) {
/* We want to memcpy only */
fastcopy(context->dest + BLOSC_MAX_OVERHEAD + j * context->blocksize,
context->src + j * context->blocksize, bsize);
cbytes = bsize;
}
else {
/* Regular compression */
cbytes = blosc_c(context, bsize, leftoverblock, ntbytes,
context->destsize, context->src+j*context->blocksize,
context->dest+ntbytes, tmp, tmp2);
if (cbytes == 0) {
ntbytes = 0; /* incompressible data */
break;
}
}
}
else {
if (*(context->header_flags) & BLOSC_MEMCPYED) {
/* We want to memcpy only */
fastcopy(context->dest + j * context->blocksize,
context->src + BLOSC_MAX_OVERHEAD + j * context->blocksize, bsize);
cbytes = bsize;
}
else {
/* Regular decompression */
cbytes = blosc_d(context, bsize, leftoverblock, context->src,
sw32_(context->bstarts + j * 4),
context->dest + j * context->blocksize, tmp, tmp2);
}
}
if (cbytes < 0) {
ntbytes = cbytes; /* error in blosc_c or blosc_d */
break;
}
ntbytes += cbytes;
}
/* Free temporaries */
my_free(tmp);
return ntbytes;
}
/* Threaded version for compression/decompression */
static int parallel_blosc(struct blosc_context* context)
{
int rc;
(void)rc; // just to avoid 'unused-variable' warning
/* Check whether we need to restart threads */
if (blosc_set_nthreads_(context) < 0) {
return -1;
}
/* Set sentinels */
context->thread_giveup_code = 1;
context->thread_nblock = -1;
/* Synchronization point for all threads (wait for initialization) */
WAIT_INIT(-1, context);
/* Synchronization point for all threads (wait for finalization) */
WAIT_FINISH(-1, context);
if (context->thread_giveup_code > 0) {
/* Return the total bytes (de-)compressed in threads */
return context->num_output_bytes;
}
else {
/* Compression/decompression gave up. Return error code. */
return context->thread_giveup_code;
}
}
/* Do the compression or decompression of the buffer depending on the
global params. */
static int do_job(struct blosc_context* context)
{
int32_t ntbytes;
/* Run the serial version when nthreads is 1 or when the buffers are
not much larger than blocksize */
if (context->numthreads == 1 || (context->sourcesize / context->blocksize) <= 1) {
ntbytes = serial_blosc(context);
}
else {
ntbytes = parallel_blosc(context);
}
return ntbytes;
}
/* Whether a codec is meant for High Compression Ratios */
#define HCR(codec) ( \
((codec) == BLOSC_LZ4HC) || \
((codec) == BLOSC_ZLIB) || \
((codec) == BLOSC_ZSTD) ? 1 : 0 )
/* Conditions for splitting a block before compressing with a codec. */
static int split_block(int compcode, int typesize, int blocksize) {
int splitblock = -1;
switch (g_splitmode) {
case BLOSC_ALWAYS_SPLIT:
splitblock = 1;
break;
case BLOSC_NEVER_SPLIT:
splitblock = 0;
break;
case BLOSC_AUTO_SPLIT:
/* Normally all the compressors designed for speed benefit from a
split. However, in conducted benchmarks LZ4 seems that it runs
faster if we don't split, which is quite surprising. */
splitblock= (((compcode == BLOSC_BLOSCLZ) ||
(compcode == BLOSC_SNAPPY)) &&
(typesize <= MAX_SPLITS) &&
(blocksize / typesize) >= MIN_BUFFERSIZE);
break;
case BLOSC_FORWARD_COMPAT_SPLIT:
/* The zstd support was introduced at the same time than the split flag, so
* there should be not a problem with not splitting bloscks with it */
splitblock = ((compcode != BLOSC_ZSTD) &&
(typesize <= MAX_SPLITS) &&
(blocksize / typesize) >= MIN_BUFFERSIZE);
break;
default:
fprintf(stderr, "Split mode %d not supported", g_splitmode);
}
return splitblock;
}
static int32_t compute_blocksize(struct blosc_context* context, int32_t clevel,
int32_t typesize, int32_t nbytes,
int32_t forced_blocksize)
{
int32_t blocksize;
/* Protection against very small buffers */
if (nbytes < (int32_t)typesize) {
return 1;
}
blocksize = nbytes; /* Start by a whole buffer as blocksize */
if (forced_blocksize) {
blocksize = forced_blocksize;
/* Check that forced blocksize is not too small */
if (blocksize < MIN_BUFFERSIZE) {
blocksize = MIN_BUFFERSIZE;
}
/* Check that forced blocksize is not too large */
if (blocksize > BLOSC_MAX_BLOCKSIZE) {
blocksize = BLOSC_MAX_BLOCKSIZE;
}
}
else if (nbytes >= L1) {
blocksize = L1;
/* For HCR codecs, increase the block sizes by a factor of 2 because they
are meant for compressing large blocks (i.e. they show a big overhead
when compressing small ones). */
if (HCR(context->compcode)) {
blocksize *= 2;
}
switch (clevel) {
case 0:
/* Case of plain copy */
blocksize /= 4;
break;