Coverage Report

Created: 2025-06-20 06:08

/src/c-blosc2/include/blosc2.h
Line
Count
Source (jump to first uncovered line)
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
  @file blosc2.h
13
  @brief Blosc2 header file.
14
15
  This file contains Blosc2 public API and the structures needed to use it.
16
  @author Blosc Development Team <blosc@blosc.org>
17
**********************************************************************/
18
19
#ifndef BLOSC_BLOSC2_H
20
#define BLOSC_BLOSC2_H
21
22
#include "blosc2/blosc2-export.h"
23
#include "blosc2/blosc2-common.h"
24
#include "blosc2/blosc2-stdio.h"
25
26
#if defined(_WIN32) && !defined(__MINGW32__)
27
#include <windows.h>
28
#include <malloc.h>
29
#include <process.h>
30
#define getpid _getpid
31
#endif
32
33
#include <limits.h>
34
#include <stdlib.h>
35
#include <stdint.h>
36
#include <stdbool.h>
37
#include <stdio.h>
38
39
#ifdef __cplusplus
40
extern "C" {
41
#endif
42
43
// For compatibility with the Blosc 1.x series
44
#ifdef BLOSC1_COMPAT
45
  // Blosc2 symbols that should be accessible from Blosc 1.x API
46
  #define BLOSC_VERSION_MAJOR BLOSC2_VERSION_MAJOR
47
  #define BLOSC_VERSION_MINOR BLOSC2_VERSION_MINOR
48
  #define BLOSC_VERSION_RELEASE BLOSC2_VERSION_RELEASE
49
  #define BLOSC_VERSION_STRING BLOSC2_VERSION_STRING
50
  #define BLOSC_VERSION_DATE BLOSC2_VERSION_DATE
51
  #define BLOSC_MAX_OVERHEAD BLOSC2_MAX_OVERHEAD
52
  #define BLOSC_MAX_BUFFERSIZE BLOSC2_MAX_BUFFERSIZE
53
54
  // API that changed to blosc1_ prefix
55
  #define blosc_compress blosc1_compress
56
  #define blosc_decompress blosc1_decompress
57
  #define blosc_getitem blosc1_getitem
58
  #define blosc_get_compressor blosc1_get_compressor
59
  #define blosc_set_compressor blosc1_set_compressor
60
  #define blosc_cbuffer_sizes blosc1_cbuffer_sizes
61
  #define blosc_cbuffer_validate blosc1_cbuffer_validate
62
  #define blosc_cbuffer_metainfo blosc1_cbuffer_metainfo
63
  #define blosc_get_blocksize blosc1_get_blocksize
64
  #define blosc_set_blocksize blosc1_set_blocksize
65
  #define blosc_set_splitmode blosc1_set_splitmode
66
67
  // API that changed to blosc2_ prefix
68
  #define blosc_init blosc2_init
69
  #define blosc_destroy blosc2_destroy
70
  #define blosc_free_resources blosc2_free_resources
71
  #define blosc_get_nthreads blosc2_get_nthreads
72
  #define blosc_set_nthreads blosc2_set_nthreads
73
  #define blosc_compcode_to_compname blosc2_compcode_to_compname
74
  #define blosc_compname_to_compcode blosc2_compname_to_compcode
75
  #define blosc_list_compressors blosc2_list_compressors
76
  #define blosc_get_version_string blosc2_get_version_string
77
  #define blosc_get_complib_info blosc2_get_complib_info
78
  #define blosc_cbuffer_versions blosc2_cbuffer_versions
79
  #define blosc_cbuffer_complib blosc2_cbuffer_complib
80
#endif
81
82
83
/* Version numbers */
84
#define BLOSC2_VERSION_MAJOR    2    /* for major interface/format changes  */
85
#define BLOSC2_VERSION_MINOR    18   /* for minor interface/format changes  */
86
#define BLOSC2_VERSION_RELEASE  1    /* for tweaks, bug-fixes, or development */
87
88
0
#define BLOSC2_VERSION_STRING   "2.18.1.dev"  /* string version.  Sync with above! */
89
#define BLOSC2_VERSION_DATE     "$Date:: 2025-06-13 #$"    /* date version year-month-day */
90
91
92
/* The maximum number of dimensions for Blosc2 NDim arrays */
93
#define BLOSC2_MAX_DIM 8
94
95
96
/* Tracing macros */
97
2.61k
#define BLOSC_TRACE_ERROR(msg, ...) BLOSC_TRACE(error, msg, ##__VA_ARGS__)
98
3
#define BLOSC_TRACE_WARNING(msg, ...) BLOSC_TRACE(warning, msg, ##__VA_ARGS__)
99
0
#define BLOSC_TRACE_INFO(msg, ...) BLOSC_TRACE(info, msg, ##__VA_ARGS__)
100
#define BLOSC_TRACE(cat, msg, ...)                  \
101
2.61k
    do {                                            \
102
2.61k
        const char *__e = getenv("BLOSC_TRACE");    \
103
2.61k
        if (!__e) { break; }                        \
104
2.61k
        fprintf(stderr, "[%s] - " msg " (%s:%d)\n", #cat, ##__VA_ARGS__, __FILE__, __LINE__); \
105
0
    } while(0)
106
107
#define BLOSC_ERROR_NULL(pointer, rc)               \
108
72.5k
    do {                                            \
109
72.5k
        if ((pointer) == NULL) {                    \
110
0
            BLOSC_TRACE_ERROR("Pointer is null");   \
111
0
            return (rc);                            \
112
0
        }                                           \
113
72.5k
    } while (0)
114
#define BLOSC_ERROR(rc)                             \
115
3
    do {                                            \
116
3
        int rc_ = (rc);                             \
117
3
        if (rc_ < BLOSC2_ERROR_SUCCESS) {           \
118
3
            char *error_msg = print_error(rc_);     \
119
3
            BLOSC_TRACE_ERROR("%s", error_msg);     \
120
3
            return rc_;                             \
121
3
        }                                           \
122
3
    } while (0)
123
124
#define BLOSC_INFO(msg, ...)                        \
125
0
    do {                                            \
126
0
        const char *__e = getenv("BLOSC_INFO");     \
127
0
        if (!__e) { break; }                        \
128
0
        fprintf(stderr, "[INFO] - " msg "\n", ##__VA_ARGS__); \
129
0
    } while(0)
130
131
132
/* The VERSION_FORMAT symbols below should be just 1-byte long */
133
enum {
134
  /* Blosc format version, starting at 1
135
     1 -> Blosc pre-1.0
136
     2 -> Blosc 1.x stable series
137
     3 -> Blosc 2-alpha.x series
138
     4 -> Blosc 2.x beta.1 series
139
     5 -> Blosc 2.x stable series
140
     */
141
  BLOSC1_VERSION_FORMAT_PRE1 = 1,
142
  BLOSC1_VERSION_FORMAT = 2,
143
  BLOSC2_VERSION_FORMAT_ALPHA = 3,
144
  BLOSC2_VERSION_FORMAT_BETA1 = 4,
145
  BLOSC2_VERSION_FORMAT_STABLE = 5,
146
  BLOSC2_VERSION_FORMAT = BLOSC2_VERSION_FORMAT_STABLE,
147
};
148
149
150
/* The FRAME_FORMAT_VERSION symbols below should be just 4-bit long */
151
enum {
152
  /* Blosc format version
153
   *  1 -> First version (introduced in beta.2)
154
   *  2 -> Second version (introduced in rc.1)
155
   *
156
   */
157
  BLOSC2_VERSION_FRAME_FORMAT_BETA2 = 1,  // for 2.0.0-beta2 and after
158
  BLOSC2_VERSION_FRAME_FORMAT_RC1 = 2,    // for 2.0.0-rc1 and after
159
  BLOSC2_VERSION_FRAME_FORMAT = BLOSC2_VERSION_FRAME_FORMAT_RC1,
160
};
161
162
163
//!< Struct for storing data from instrumentation of codecs
164
// This can be flexible because it is typically used mainly for development
165
typedef struct {
166
  float cratio;
167
  float cspeed;
168
  float filter_speed;
169
  //float memory;
170
  //float power;
171
  uint8_t flags[4];
172
} blosc2_instr;
173
174
175
enum {
176
#ifndef BLOSC_H
177
  BLOSC_MIN_HEADER_LENGTH = 16,
178
  //!< Minimum header length (Blosc1)
179
#endif // BLOSC_H
180
  BLOSC_EXTENDED_HEADER_LENGTH = 32,
181
  //!< Extended header length (Blosc2, see README_HEADER)
182
  BLOSC2_MAX_OVERHEAD = BLOSC_EXTENDED_HEADER_LENGTH,
183
  //!< The maximum overhead during compression in bytes. This equals
184
  //!< to @ref BLOSC_EXTENDED_HEADER_LENGTH now, but can be higher in future
185
  //!< implementations.
186
  BLOSC2_MAX_BUFFERSIZE = (INT_MAX - BLOSC2_MAX_OVERHEAD),
187
  //!< Maximum source buffer size to be compressed
188
#ifndef BLOSC_H
189
  BLOSC_MAX_TYPESIZE = UINT8_MAX,
190
  //!< Maximum typesize before considering source buffer as a stream of bytes.
191
  //!< Cannot be larger than 255.
192
#endif // BLOSC_H
193
  BLOSC_MIN_BUFFERSIZE = 32,
194
  //!< Minimum buffer size to be compressed.
195
};
196
197
enum {
198
    BLOSC2_DEFINED_TUNER_START = 0,
199
    BLOSC2_DEFINED_TUNER_STOP = 31,
200
    //!< Blosc-defined tuners must be between 0 - 31.
201
    BLOSC2_GLOBAL_REGISTERED_TUNER_START = 32,
202
    BLOSC2_GLOBAL_REGISTERED_TUNER_STOP = 159,
203
    //!< Blosc-registered tuners must be between 31 - 159.
204
    BLOSC2_GLOBAL_REGISTERED_TUNERS = 0,
205
    //!< Number of Blosc-registered tuners at the moment.
206
    BLOSC2_USER_REGISTERED_TUNER_START = 160,
207
    BLOSC2_USER_REGISTERED_TUNER_STOP = 255,
208
    //!< User-defined tuners must be between 160 - 255.
209
};
210
211
/**
212
 * @brief Codes for the different tuners shipped with Blosc
213
 */
214
enum {
215
    BLOSC_STUNE = 0,
216
    BLOSC_LAST_TUNER = 1,
217
    //!< Determine the last tuner defined by Blosc.
218
    BLOSC_LAST_REGISTERED_TUNE = BLOSC2_GLOBAL_REGISTERED_TUNER_START + BLOSC2_GLOBAL_REGISTERED_TUNERS - 1,
219
    //!< Determine the last registered tuner. It is used to check if a tuner is registered or not.
220
};
221
222
enum {
223
  BLOSC2_DEFINED_FILTERS_START = 0,
224
  BLOSC2_DEFINED_FILTERS_STOP = 31,
225
  //!< Blosc-defined filters must be between 0 - 31.
226
  BLOSC2_GLOBAL_REGISTERED_FILTERS_START = 32,
227
  BLOSC2_GLOBAL_REGISTERED_FILTERS_STOP = 159,
228
  //!< Blosc-registered filters must be between 32 - 159.
229
  BLOSC2_GLOBAL_REGISTERED_FILTERS = 5,
230
  //!< Number of Blosc-registered filters at the moment.
231
  BLOSC2_USER_REGISTERED_FILTERS_START = 160,
232
  BLOSC2_USER_REGISTERED_FILTERS_STOP = 255,
233
  //!< User-defined filters must be between 128 - 255.
234
  BLOSC2_MAX_FILTERS = 6,
235
  //!< Maximum number of filters in the filter pipeline
236
  BLOSC2_MAX_UDFILTERS = 16,
237
  //!< Maximum number of filters that a user can register.
238
};
239
240
241
/**
242
 * @brief Codes for filters.
243
 *
244
 * @sa #blosc1_compress
245
 */
246
enum {
247
#ifndef BLOSC_H
248
  BLOSC_NOSHUFFLE = 0,
249
  //!< No shuffle (for compatibility with Blosc1).
250
  BLOSC_NOFILTER = 0,
251
  //!< No filter.
252
  BLOSC_SHUFFLE = 1,
253
  //!< Byte-wise shuffle. `filters_meta` does not have any effect here.
254
  BLOSC_BITSHUFFLE = 2,
255
  //!< Bit-wise shuffle. `filters_meta` does not have any effect here.
256
#endif // BLOSC_H
257
  BLOSC_DELTA = 3,
258
  //!< Delta filter. `filters_meta` does not have any effect here.
259
  BLOSC_TRUNC_PREC = 4,
260
  //!< Truncate mantissa precision.
261
  //!< Positive values in `filters_meta` will keep bits; negative values will zero bits.
262
  BLOSC_LAST_FILTER = 5,
263
  //!< sentinel
264
  BLOSC_LAST_REGISTERED_FILTER = BLOSC2_GLOBAL_REGISTERED_FILTERS_START + BLOSC2_GLOBAL_REGISTERED_FILTERS - 1,
265
  //!< Determine the last registered filter. It is used to check if a filter is registered or not.
266
};
267
268
/**
269
 * @brief Codes for internal flags (see blosc1_cbuffer_metainfo)
270
 */
271
enum {
272
#ifndef BLOSC_H
273
  BLOSC_DOSHUFFLE = 0x1,     //!< byte-wise shuffle
274
  BLOSC_MEMCPYED = 0x2,      //!< plain copy
275
  BLOSC_DOBITSHUFFLE = 0x4,  //!< bit-wise shuffle
276
#endif // BLOSC_H
277
  BLOSC_DODELTA = 0x8,       //!< delta coding
278
};
279
280
/**
281
 * @brief Codes for new internal flags in Blosc2
282
 */
283
enum {
284
  BLOSC2_USEDICT = 0x1,          //!< use dictionaries with codec
285
  BLOSC2_BIGENDIAN = 0x2,        //!< data is in big-endian ordering
286
  BLOSC2_INSTR_CODEC = 0x80,     //!< codec is instrumented (mainly for development)
287
};
288
289
/**
290
 * @brief Values for different Blosc2 capabilities
291
 */
292
enum {
293
  BLOSC2_MAXDICTSIZE = 128 * 1024, //!< maximum size for compression dicts
294
  BLOSC2_MAXBLOCKSIZE = 536866816, //!< maximum size for blocks
295
  BLOSC2_MAXTYPESIZE = BLOSC2_MAXBLOCKSIZE, //!< maximum size for types
296
};
297
298
299
enum {
300
  BLOSC2_DEFINED_CODECS_START = 0,
301
  BLOSC2_DEFINED_CODECS_STOP = 31,
302
  //!< Blosc-defined codecs must be between 0 - 31.
303
  BLOSC2_GLOBAL_REGISTERED_CODECS_START = 32,
304
  BLOSC2_GLOBAL_REGISTERED_CODECS_STOP = 159,
305
  //!< Blosc-registered codecs must be between 31 - 159.
306
  BLOSC2_GLOBAL_REGISTERED_CODECS = 5,
307
    //!< Number of Blosc-registered codecs at the moment.
308
  BLOSC2_USER_REGISTERED_CODECS_START = 160,
309
  BLOSC2_USER_REGISTERED_CODECS_STOP = 255,
310
  //!< User-defined codecs must be between 160 - 255.
311
};
312
313
/**
314
 * @brief Codes for the different compressors shipped with Blosc
315
 */
316
enum {
317
#ifndef BLOSC_H
318
  BLOSC_BLOSCLZ = 0,
319
  BLOSC_LZ4 = 1,
320
  BLOSC_LZ4HC = 2,
321
  BLOSC_ZLIB = 4,
322
  BLOSC_ZSTD = 5,
323
#endif // BLOSC_H
324
  BLOSC_LAST_CODEC = 6,
325
  //!< Determine the last codec defined by Blosc.
326
  BLOSC_LAST_REGISTERED_CODEC = BLOSC2_GLOBAL_REGISTERED_CODECS_START + BLOSC2_GLOBAL_REGISTERED_CODECS - 1,
327
  //!< Determine the last registered codec. It is used to check if a codec is registered or not.
328
};
329
330
331
// Names for the different compressors shipped with Blosc
332
333
#ifndef BLOSC_H
334
0
#define BLOSC_BLOSCLZ_COMPNAME   "blosclz"
335
0
#define BLOSC_LZ4_COMPNAME       "lz4"
336
0
#define BLOSC_LZ4HC_COMPNAME     "lz4hc"
337
0
#define BLOSC_ZLIB_COMPNAME      "zlib"
338
0
#define BLOSC_ZSTD_COMPNAME      "zstd"
339
#endif // BLOSC_H
340
341
/**
342
 * @brief Codes for compression libraries shipped with Blosc (code must be < 8)
343
 */
344
enum {
345
#ifndef BLOSC_H
346
  BLOSC_BLOSCLZ_LIB = 0,
347
  BLOSC_LZ4_LIB = 1,
348
  BLOSC_ZLIB_LIB = 3,
349
  BLOSC_ZSTD_LIB = 4,
350
#endif // BLOSC_H
351
  BLOSC_UDCODEC_LIB = 6,
352
  BLOSC_SCHUNK_LIB = 7,   //!< compressor library in super-chunk header
353
};
354
355
/**
356
 * @brief Names for the different compression libraries shipped with Blosc
357
 */
358
#ifndef BLOSC_H
359
0
#define BLOSC_BLOSCLZ_LIBNAME   "BloscLZ"
360
0
#define BLOSC_LZ4_LIBNAME       "LZ4"
361
0
#define BLOSC_ZLIB_LIBNAME      "Zlib"
362
0
#define BLOSC_ZSTD_LIBNAME      "Zstd"
363
#endif // BLOSC_H
364
365
/**
366
 * @brief The codes for compressor formats shipped with Blosc
367
 */
368
enum {
369
#ifndef BLOSC_H
370
  BLOSC_BLOSCLZ_FORMAT = BLOSC_BLOSCLZ_LIB,
371
  BLOSC_LZ4_FORMAT = BLOSC_LZ4_LIB,
372
  //!< LZ4HC and LZ4 share the same format
373
  BLOSC_LZ4HC_FORMAT = BLOSC_LZ4_LIB,
374
  BLOSC_ZLIB_FORMAT = BLOSC_ZLIB_LIB,
375
  BLOSC_ZSTD_FORMAT = BLOSC_ZSTD_LIB,
376
#endif // BLOSC_H
377
  BLOSC_UDCODEC_FORMAT = BLOSC_UDCODEC_LIB,
378
};
379
380
/**
381
 * @brief The version formats for compressors shipped with Blosc.
382
 * All versions here starts at 1
383
 */
384
enum {
385
#ifndef BLOSC_H
386
  BLOSC_BLOSCLZ_VERSION_FORMAT = 1,
387
  BLOSC_LZ4_VERSION_FORMAT = 1,
388
  BLOSC_LZ4HC_VERSION_FORMAT = 1,  /* LZ4HC and LZ4 share the same format */
389
  BLOSC_ZLIB_VERSION_FORMAT = 1,
390
  BLOSC_ZSTD_VERSION_FORMAT = 1,
391
#endif // BLOSC_H
392
  BLOSC_UDCODEC_VERSION_FORMAT = 1,
393
};
394
395
/**
396
 * @brief Split mode for blocks.
397
 * NEVER and ALWAYS are for experimenting with compression ratio.
398
 * AUTO for nearly optimal behaviour (based on heuristics).
399
 * FORWARD_COMPAT provides best forward compatibility (default).
400
 */
401
#ifndef BLOSC_H
402
enum {
403
  BLOSC_ALWAYS_SPLIT = 1,
404
  BLOSC_NEVER_SPLIT = 2,
405
  BLOSC_AUTO_SPLIT = 3,
406
  BLOSC_FORWARD_COMPAT_SPLIT = 4,
407
};
408
#endif // BLOSC_H
409
410
/**
411
 * @brief Offsets for fields in Blosc2 chunk header.
412
 */
413
enum {
414
  BLOSC2_CHUNK_VERSION = 0x0,       //!< the version for the chunk format
415
  BLOSC2_CHUNK_VERSIONLZ = 0x1,     //!< the version for the format of internal codec
416
  BLOSC2_CHUNK_FLAGS = 0x2,         //!< flags and codec info
417
  BLOSC2_CHUNK_TYPESIZE = 0x3,      //!< (uint8) the number of bytes of the atomic type
418
  BLOSC2_CHUNK_NBYTES = 0x4,        //!< (int32) uncompressed size of the buffer (this header is not included)
419
  BLOSC2_CHUNK_BLOCKSIZE = 0x8,     //!< (int32) size of internal blocks
420
  BLOSC2_CHUNK_CBYTES = 0xc,        //!< (int32) compressed size of the buffer (including this header)
421
  BLOSC2_CHUNK_FILTER_CODES = 0x10, //!< the codecs for the filter pipeline (1 byte per code)
422
  BLOSC2_CHUNK_FILTER_META = 0x18,  //!< meta info for the filter pipeline (1 byte per code)
423
  BLOSC2_CHUNK_BLOSC2_FLAGS = 0x1F, //!< flags specific for Blosc2 functionality
424
};
425
426
/**
427
 * @brief Run lengths for special values for chunks/frames
428
 */
429
enum {
430
  BLOSC2_NO_SPECIAL = 0x0,       //!< no special value
431
  BLOSC2_SPECIAL_ZERO = 0x1,     //!< zero special value
432
  BLOSC2_SPECIAL_NAN = 0x2,      //!< NaN special value
433
  BLOSC2_SPECIAL_VALUE = 0x3,    //!< repeated special value
434
  BLOSC2_SPECIAL_UNINIT = 0x4,   //!< non initialized values
435
  BLOSC2_SPECIAL_LASTID = 0x4,   //!< last valid ID for special value (update this adequately)
436
  BLOSC2_SPECIAL_MASK = 0x7      //!< special value mask (prev IDs cannot be larger than this)
437
};
438
439
/**
440
 * @brief Error codes
441
 * Each time an error code is added here, its corresponding message error should be added in
442
 * print_error()
443
 */
444
enum {
445
  BLOSC2_ERROR_SUCCESS = 0,           //<! Success
446
  BLOSC2_ERROR_FAILURE = -1,          //<! Generic failure
447
  BLOSC2_ERROR_STREAM = -2,           //<! Bad stream
448
  BLOSC2_ERROR_DATA = -3,             //<! Invalid data
449
  BLOSC2_ERROR_MEMORY_ALLOC = -4,     //<! Memory alloc/realloc failure
450
  BLOSC2_ERROR_READ_BUFFER = -5,      //!< Not enough space to read
451
  BLOSC2_ERROR_WRITE_BUFFER = -6,     //!< Not enough space to write
452
  BLOSC2_ERROR_CODEC_SUPPORT = -7,    //!< Codec not supported
453
  BLOSC2_ERROR_CODEC_PARAM = -8,      //!< Invalid parameter supplied to codec
454
  BLOSC2_ERROR_CODEC_DICT = -9,       //!< Codec dictionary error
455
  BLOSC2_ERROR_VERSION_SUPPORT = -10, //!< Version not supported
456
  BLOSC2_ERROR_INVALID_HEADER = -11,  //!< Invalid value in header
457
  BLOSC2_ERROR_INVALID_PARAM = -12,   //!< Invalid parameter supplied to function
458
  BLOSC2_ERROR_FILE_READ = -13,       //!< File read failure
459
  BLOSC2_ERROR_FILE_WRITE = -14,      //!< File write failure
460
  BLOSC2_ERROR_FILE_OPEN = -15,       //!< File open failure
461
  BLOSC2_ERROR_NOT_FOUND = -16,       //!< Not found
462
  BLOSC2_ERROR_RUN_LENGTH = -17,      //!< Bad run length encoding
463
  BLOSC2_ERROR_FILTER_PIPELINE = -18, //!< Filter pipeline error
464
  BLOSC2_ERROR_CHUNK_INSERT = -19,    //!< Chunk insert failure
465
  BLOSC2_ERROR_CHUNK_APPEND = -20,    //!< Chunk append failure
466
  BLOSC2_ERROR_CHUNK_UPDATE = -21,    //!< Chunk update failure
467
  BLOSC2_ERROR_2GB_LIMIT = -22,       //!< Sizes larger than 2gb not supported
468
  BLOSC2_ERROR_SCHUNK_COPY = -23,     //!< Super-chunk copy failure
469
  BLOSC2_ERROR_FRAME_TYPE = -24,      //!< Wrong type for frame
470
  BLOSC2_ERROR_FILE_TRUNCATE = -25,   //!< File truncate failure
471
  BLOSC2_ERROR_THREAD_CREATE = -26,   //!< Thread or thread context creation failure
472
  BLOSC2_ERROR_POSTFILTER = -27,      //!< Postfilter failure
473
  BLOSC2_ERROR_FRAME_SPECIAL = -28,   //!< Special frame failure
474
  BLOSC2_ERROR_SCHUNK_SPECIAL = -29,  //!< Special super-chunk failure
475
  BLOSC2_ERROR_PLUGIN_IO = -30,       //!< IO plugin error
476
  BLOSC2_ERROR_FILE_REMOVE = -31,     //!< Remove file failure
477
  BLOSC2_ERROR_NULL_POINTER = -32,    //!< Pointer is null
478
  BLOSC2_ERROR_INVALID_INDEX = -33,   //!< Invalid index
479
  BLOSC2_ERROR_METALAYER_NOT_FOUND = -34,   //!< Metalayer has not been found
480
  BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED = -35,  //!< Max buffer size exceeded
481
  BLOSC2_ERROR_TUNER = -36,           //!< Tuner failure
482
};
483
484
485
#ifdef __GNUC__
486
#define BLOSC_ATTRIBUTE_UNUSED __attribute__((unused))
487
#else
488
#define BLOSC_ATTRIBUTE_UNUSED
489
#endif
490
491
static char *print_error(int rc) BLOSC_ATTRIBUTE_UNUSED;
492
3
static char *print_error(int rc) {
493
3
  switch (rc) {
494
3
    case BLOSC2_ERROR_FAILURE:
495
3
      return (char *) "Generic failure";
496
0
    case BLOSC2_ERROR_STREAM:
497
0
      return (char *) "Bad stream";
498
0
    case BLOSC2_ERROR_DATA:
499
0
      return (char *) "Invalid data";
500
0
    case BLOSC2_ERROR_MEMORY_ALLOC:
501
0
      return (char *) "Memory alloc/realloc failure";
502
0
    case BLOSC2_ERROR_READ_BUFFER:
503
0
      return (char *) "Not enough space to read";
504
0
    case BLOSC2_ERROR_WRITE_BUFFER:
505
0
      return (char *) "Not enough space to write";
506
0
    case BLOSC2_ERROR_CODEC_SUPPORT:
507
0
      return (char *) "Codec not supported";
508
0
    case BLOSC2_ERROR_CODEC_PARAM:
509
0
      return (char *) "Invalid parameter supplied to codec";
510
0
    case BLOSC2_ERROR_CODEC_DICT:
511
0
      return (char *) "Codec dictionary error";
512
0
    case BLOSC2_ERROR_VERSION_SUPPORT:
513
0
      return (char *) "Version not supported";
514
0
    case BLOSC2_ERROR_INVALID_HEADER:
515
0
      return (char *) "Invalid value in header";
516
0
    case BLOSC2_ERROR_INVALID_PARAM:
517
0
      return (char *) "Invalid parameter supplied to function";
518
0
    case BLOSC2_ERROR_FILE_READ:
519
0
      return (char *) "File read failure";
520
0
    case BLOSC2_ERROR_FILE_WRITE:
521
0
      return (char *) "File write failure";
522
0
    case BLOSC2_ERROR_FILE_OPEN:
523
0
      return (char *) "File open failure";
524
0
    case BLOSC2_ERROR_NOT_FOUND:
525
0
      return (char *) "Not found";
526
0
    case BLOSC2_ERROR_RUN_LENGTH:
527
0
      return (char *) "Bad run length encoding";
528
0
    case BLOSC2_ERROR_FILTER_PIPELINE:
529
0
      return (char *) "Filter pipeline error";
530
0
    case BLOSC2_ERROR_CHUNK_INSERT:
531
0
      return (char *) "Chunk insert failure";
532
0
    case BLOSC2_ERROR_CHUNK_APPEND:
533
0
      return (char *) "Chunk append failure";
534
0
    case BLOSC2_ERROR_CHUNK_UPDATE:
535
0
      return (char *) "Chunk update failure";
536
0
    case BLOSC2_ERROR_2GB_LIMIT:
537
0
      return (char *) "Sizes larger than 2gb not supported";
538
0
    case BLOSC2_ERROR_SCHUNK_COPY:
539
0
      return (char *) "Super-chunk copy failure";
540
0
    case BLOSC2_ERROR_FRAME_TYPE:
541
0
      return (char *) "Wrong type for frame";
542
0
    case BLOSC2_ERROR_FILE_TRUNCATE:
543
0
      return (char *) "File truncate failure";
544
0
    case BLOSC2_ERROR_THREAD_CREATE:
545
0
      return (char *) "Thread or thread context creation failure";
546
0
    case BLOSC2_ERROR_POSTFILTER:
547
0
      return (char *) "Postfilter failure";
548
0
    case BLOSC2_ERROR_FRAME_SPECIAL:
549
0
      return (char *) "Special frame failure";
550
0
    case BLOSC2_ERROR_SCHUNK_SPECIAL:
551
0
      return (char *) "Special super-chunk failure";
552
0
    case BLOSC2_ERROR_PLUGIN_IO:
553
0
      return (char *) "IO plugin error";
554
0
    case BLOSC2_ERROR_FILE_REMOVE:
555
0
      return (char *) "Remove file failure";
556
0
    case BLOSC2_ERROR_NULL_POINTER:
557
0
      return (char *) "Pointer is null";
558
0
    case BLOSC2_ERROR_INVALID_INDEX:
559
0
      return (char *) "Invalid index";
560
0
    case BLOSC2_ERROR_METALAYER_NOT_FOUND:
561
0
      return (char *) "Metalayer has not been found";
562
0
    case BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED:
563
0
      return (char *) "Maximum buffersize exceeded";
564
0
    default:
565
0
      return (char *) "Unknown error";
566
3
  }
567
3
}
Unexecuted instantiation: fuzz_decompress_chunk.c:print_error
Unexecuted instantiation: blosc2.c:print_error
Unexecuted instantiation: blosclz.c:print_error
Unexecuted instantiation: schunk.c:print_error
Unexecuted instantiation: frame.c:print_error
Unexecuted instantiation: stune.c:print_error
Unexecuted instantiation: trunc-prec.c:print_error
Unexecuted instantiation: timestamp.c:print_error
Unexecuted instantiation: sframe.c:print_error
Unexecuted instantiation: blosc2-stdio.c:print_error
Unexecuted instantiation: b2nd.c:print_error
Unexecuted instantiation: b2nd_utils.c:print_error
Unexecuted instantiation: shuffle.c:print_error
Unexecuted instantiation: blosc2-zfp.c:print_error
Unexecuted instantiation: codecs-registry.c:print_error
Unexecuted instantiation: tuners-registry.c:print_error
Unexecuted instantiation: filters-registry.c:print_error
Unexecuted instantiation: ndlz.c:print_error
Unexecuted instantiation: ndlz4x4.c:print_error
Unexecuted instantiation: ndlz8x8.c:print_error
Unexecuted instantiation: ndcell.c:print_error
Unexecuted instantiation: ndmean.c:print_error
bytedelta.c:print_error
Line
Count
Source
492
3
static char *print_error(int rc) {
493
3
  switch (rc) {
494
3
    case BLOSC2_ERROR_FAILURE:
495
3
      return (char *) "Generic failure";
496
0
    case BLOSC2_ERROR_STREAM:
497
0
      return (char *) "Bad stream";
498
0
    case BLOSC2_ERROR_DATA:
499
0
      return (char *) "Invalid data";
500
0
    case BLOSC2_ERROR_MEMORY_ALLOC:
501
0
      return (char *) "Memory alloc/realloc failure";
502
0
    case BLOSC2_ERROR_READ_BUFFER:
503
0
      return (char *) "Not enough space to read";
504
0
    case BLOSC2_ERROR_WRITE_BUFFER:
505
0
      return (char *) "Not enough space to write";
506
0
    case BLOSC2_ERROR_CODEC_SUPPORT:
507
0
      return (char *) "Codec not supported";
508
0
    case BLOSC2_ERROR_CODEC_PARAM:
509
0
      return (char *) "Invalid parameter supplied to codec";
510
0
    case BLOSC2_ERROR_CODEC_DICT:
511
0
      return (char *) "Codec dictionary error";
512
0
    case BLOSC2_ERROR_VERSION_SUPPORT:
513
0
      return (char *) "Version not supported";
514
0
    case BLOSC2_ERROR_INVALID_HEADER:
515
0
      return (char *) "Invalid value in header";
516
0
    case BLOSC2_ERROR_INVALID_PARAM:
517
0
      return (char *) "Invalid parameter supplied to function";
518
0
    case BLOSC2_ERROR_FILE_READ:
519
0
      return (char *) "File read failure";
520
0
    case BLOSC2_ERROR_FILE_WRITE:
521
0
      return (char *) "File write failure";
522
0
    case BLOSC2_ERROR_FILE_OPEN:
523
0
      return (char *) "File open failure";
524
0
    case BLOSC2_ERROR_NOT_FOUND:
525
0
      return (char *) "Not found";
526
0
    case BLOSC2_ERROR_RUN_LENGTH:
527
0
      return (char *) "Bad run length encoding";
528
0
    case BLOSC2_ERROR_FILTER_PIPELINE:
529
0
      return (char *) "Filter pipeline error";
530
0
    case BLOSC2_ERROR_CHUNK_INSERT:
531
0
      return (char *) "Chunk insert failure";
532
0
    case BLOSC2_ERROR_CHUNK_APPEND:
533
0
      return (char *) "Chunk append failure";
534
0
    case BLOSC2_ERROR_CHUNK_UPDATE:
535
0
      return (char *) "Chunk update failure";
536
0
    case BLOSC2_ERROR_2GB_LIMIT:
537
0
      return (char *) "Sizes larger than 2gb not supported";
538
0
    case BLOSC2_ERROR_SCHUNK_COPY:
539
0
      return (char *) "Super-chunk copy failure";
540
0
    case BLOSC2_ERROR_FRAME_TYPE:
541
0
      return (char *) "Wrong type for frame";
542
0
    case BLOSC2_ERROR_FILE_TRUNCATE:
543
0
      return (char *) "File truncate failure";
544
0
    case BLOSC2_ERROR_THREAD_CREATE:
545
0
      return (char *) "Thread or thread context creation failure";
546
0
    case BLOSC2_ERROR_POSTFILTER:
547
0
      return (char *) "Postfilter failure";
548
0
    case BLOSC2_ERROR_FRAME_SPECIAL:
549
0
      return (char *) "Special frame failure";
550
0
    case BLOSC2_ERROR_SCHUNK_SPECIAL:
551
0
      return (char *) "Special super-chunk failure";
552
0
    case BLOSC2_ERROR_PLUGIN_IO:
553
0
      return (char *) "IO plugin error";
554
0
    case BLOSC2_ERROR_FILE_REMOVE:
555
0
      return (char *) "Remove file failure";
556
0
    case BLOSC2_ERROR_NULL_POINTER:
557
0
      return (char *) "Pointer is null";
558
0
    case BLOSC2_ERROR_INVALID_INDEX:
559
0
      return (char *) "Invalid index";
560
0
    case BLOSC2_ERROR_METALAYER_NOT_FOUND:
561
0
      return (char *) "Metalayer has not been found";
562
0
    case BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED:
563
0
      return (char *) "Maximum buffersize exceeded";
564
0
    default:
565
0
      return (char *) "Unknown error";
566
3
  }
567
3
}
Unexecuted instantiation: int_trunc.c:print_error
568
569
570
/**
571
 * @brief Initialize the Blosc library environment.
572
 *
573
 * You must call this previous to any other Blosc call, unless you want
574
 * Blosc to be used simultaneously in a multi-threaded environment, in
575
 * which case you can use the #blosc2_compress_ctx #blosc2_decompress_ctx pair.
576
 *
577
 * @sa #blosc2_destroy
578
 */
579
BLOSC_EXPORT void blosc2_init(void);
580
581
582
/**
583
 * @brief Destroy the Blosc library environment.
584
 *
585
 * You must call this after to you are done with all the Blosc calls,
586
 * unless you have not used blosc2_init() before.
587
 *
588
 * @sa #blosc2_init
589
 */
590
BLOSC_EXPORT void blosc2_destroy(void);
591
592
593
/**
594
 * @brief Compress a block of data in the @p src buffer and returns the size of
595
 * compressed block.
596
 *
597
 * @remark Compression is memory safe and guaranteed not to write @p dest
598
 * more than what is specified in @p destsize.
599
 * There is not a minimum for @p src buffer size @p nbytes.
600
 * Equivalent to #blosc2_compress.
601
 *
602
 * @warning The @p src buffer and the @p dest buffer can not overlap.
603
 *
604
 * @param clevel The desired compression level and must be a number
605
 * between 0 (no compression) and 9 (maximum compression).
606
 * @param doshuffle Specifies whether the shuffle compression preconditioner
607
 * should be applied or not. #BLOSC_NOFILTER means not applying filters,
608
 * #BLOSC_SHUFFLE means applying shuffle at a byte level and
609
 * #BLOSC_BITSHUFFLE at a bit level (slower but *may* achieve better
610
 * compression).
611
 * @param typesize Is the number of bytes for the atomic type in binary
612
 * @p src buffer.  This is mainly useful for the shuffle preconditioner.
613
 * For implementation reasons, only a 1 < typesize < 256 will allow the
614
 * shuffle filter to work.  When typesize is not in this range, shuffle
615
 * will be silently disabled.
616
 * @param nbytes The number of bytes to compress in the @p src buffer.
617
 * @param src The buffer containing the data to compress.
618
 * @param dest The buffer where the compressed data will be put,
619
 * must have at least the size of @p destsize.
620
 * @param destsize The size of the dest buffer. Blosc
621
 * guarantees that if you set @p destsize to, at least,
622
 * (@p nbytes + #BLOSC2_MAX_OVERHEAD), the compression will always succeed.
623
 *
624
 * @return The number of bytes compressed.
625
 * If @p src buffer cannot be compressed into @p destsize, the return
626
 * value is zero and you should discard the contents of the @p dest
627
 * buffer. A negative return value means that an internal error happened. This
628
 * should never happen. If you see this, please report it back
629
 * together with the buffer data causing this and compression settings.
630
 *
631
 *
632
 * @par Environment variables
633
 * @parblock
634
 *
635
 * This function honors different environment variables to control
636
 * internal parameters without the need of doing that programmatically.
637
 * Here are the ones supported:
638
 *
639
 * * **BLOSC_CLEVEL=(INTEGER)**: This will overwrite the @p clevel parameter
640
 * before the compression process starts.
641
 *
642
 * * **BLOSC_SHUFFLE=[NOSHUFFLE | SHUFFLE | BITSHUFFLE]**: This will
643
 * overwrite the @p doshuffle parameter before the compression process
644
 * starts.
645
 *
646
 * * **BLOSC_DELTA=(1|0)**: This will call #blosc2_set_delta() before the
647
 * compression process starts.
648
 *
649
 * * **BLOSC_TYPESIZE=(INTEGER)**: This will overwrite the @p typesize
650
 * parameter before the compression process starts.
651
 *
652
 * * **BLOSC_COMPRESSOR=[BLOSCLZ | LZ4 | LZ4HC | ZLIB | ZSTD]**:
653
 * This will call #blosc1_set_compressor before the compression process starts.
654
 *
655
 * * **BLOSC_NTHREADS=(INTEGER)**: This will call
656
 * #blosc2_set_nthreads before the compression process starts.
657
 *
658
 * * **BLOSC_SPLITMODE=(ALWAYS | NEVER | AUTO | FORWARD_COMPAT)**:
659
 * This will call #blosc1_set_splitmode() before the compression process starts.
660
 *
661
 * * **BLOSC_BLOCKSIZE=(INTEGER)**: This will call
662
 * #blosc1_set_blocksize before the compression process starts.
663
 * *NOTE:* The *blocksize* is a critical parameter with
664
 * important restrictions in the allowed values, so use this with care.
665
 *
666
 * * **BLOSC_NOLOCK=(ANY VALUE)**: This will call #blosc2_compress_ctx under
667
 * the hood, with the *compressor*, *blocksize* and
668
 * *numinternalthreads* parameters set to the same as the last calls to
669
 * #blosc1_set_compressor, #blosc1_set_blocksize and
670
 * #blosc2_set_nthreads. *BLOSC_CLEVEL*, *BLOSC_SHUFFLE*, *BLOSC_DELTA* and
671
 * *BLOSC_TYPESIZE* environment vars will also be honored.
672
 *
673
 * @endparblock
674
 *
675
 * @sa #blosc1_decompress
676
 */
677
BLOSC_EXPORT int blosc1_compress(int clevel, int doshuffle, size_t typesize,
678
                                 size_t nbytes, const void* src, void* dest,
679
                                 size_t destsize);
680
681
682
/**
683
 * @brief Decompress a block of compressed data in @p src, put the result in
684
 * @p dest and returns the size of the decompressed block.
685
 *
686
 * @warning The @p src buffer and the @p dest buffer can not overlap.
687
 *
688
 * @remark Decompression is memory safe and guaranteed not to write the @p dest
689
 * buffer more than what is specified in @p destsize.
690
 * Similar to #blosc2_decompress.
691
 *
692
 * @remark In case you want to keep under control the number of bytes read from
693
 * source, you can call #blosc1_cbuffer_sizes first to check whether the
694
 * @p nbytes (i.e. the number of bytes to be read from @p src buffer by this
695
 * function) in the compressed buffer is ok with you.
696
 *
697
 * @param src The buffer to be decompressed.
698
 * @param dest The buffer where the decompressed data will be put.
699
 * @param destsize The size of the @p dest buffer.
700
 *
701
 * @return The number of bytes decompressed.
702
 * If an error occurs, e.g. the compressed data is corrupted or the
703
 * output buffer is not large enough, then a negative value
704
 * will be returned instead.
705
 *
706
 * @par Environment variables
707
 * @parblock
708
 * This function honors different environment variables to control
709
 * internal parameters without the need of doing that programmatically.
710
 * Here are the ones supported:
711
 *
712
 * * **BLOSC_NTHREADS=(INTEGER)**: This will call
713
 * #blosc2_set_nthreads before the proper decompression
714
 * process starts.
715
 *
716
 * * **BLOSC_NOLOCK=(ANY VALUE)**: This will call #blosc2_decompress_ctx
717
 * under the hood, with the *numinternalthreads* parameter set to the
718
 * same value as the last call to #blosc2_set_nthreads.
719
 *
720
 * @endparblock
721
 *
722
 * @sa #blosc1_compress
723
 */
724
BLOSC_EXPORT int blosc1_decompress(const void* src, void* dest, size_t destsize);
725
726
727
/**
728
 * @brief Get @p nitems (of @p typesize size) in @p src buffer starting in @p start.
729
 * The items are returned in @p dest buffer, which has to have enough
730
 * space for storing all items.
731
 *
732
 * @remark The function #blosc2_getitem is a more complete and secure version.
733
 *
734
 * @param src The compressed buffer from data will be decompressed.
735
 * @param start The position of the first item (of @p typesize size) from where data
736
 * will be retrieved.
737
 * @param nitems The number of items (of @p typesize size) that will be retrieved.
738
 * @param dest The buffer where the decompressed data retrieved will be put.
739
 *
740
 * @return The number of bytes copied to @p dest or a negative value if
741
 * some error happens.
742
 */
743
BLOSC_EXPORT int blosc1_getitem(const void* src, int start, int nitems, void* dest);
744
745
/**
746
 * @brief Get @p nitems (of @p typesize size) in @p src buffer starting in @p start.
747
 * The items are returned in @p dest buffer. The dest buffer should have enough space
748
 * for storing all items. This function is a more secure version of #blosc1_getitem.
749
 *
750
 * @param src The compressed buffer holding the data to be retrieved.
751
 * @param srcsize Size of the compressed buffer.
752
 * @param start The position of the first item (of @p typesize size) from where data
753
 * will be retrieved.
754
 * @param nitems The number of items (of @p typesize size) that will be retrieved.
755
 * @param dest The buffer where the retrieved data will be stored decompressed.
756
 * @param destsize Size of the buffer where retrieved data will be stored.
757
 *
758
 * @return The number of bytes copied to @p dest or a negative value if
759
 * some error happens.
760
 */
761
BLOSC_EXPORT int blosc2_getitem(const void* src, int32_t srcsize, int start, int nitems,
762
                                void* dest, int32_t destsize);
763
764
/**
765
  Pointer to a callback function that executes `dojob(jobdata + i*jobdata_elsize)` for `i = 0 to numjobs-1`,
766
  possibly in parallel threads (but not returning until all `dojob` calls have returned).   This allows the
767
  caller to provide a custom threading backend as an alternative to the default Blosc-managed threads.
768
  `callback_data` is passed through from `blosc2_set_threads_callback`.
769
 */
770
typedef void (*blosc_threads_callback)(void *callback_data, void (*dojob)(void *), int numjobs, size_t jobdata_elsize, void *jobdata);
771
772
/**
773
  * @brief Set the threading backend for parallel compression/decompression to use @p callback to execute work
774
  * instead of using the Blosc-managed threads.  The @p callback_data argument is passed through to the callback.
775
776
  * @param callback: the callback to use. Passing `NULL` uses the default Blosc threading backend.
777
  * @param callback_data: the callback data.
778
  *
779
  * @warning This function is *not* thread-safe and should be called
780
  * before any other Blosc function: it affects all Blosc contexts.
781
  * @sa https://github.com/Blosc/c-blosc2/pull/81
782
  */
783
BLOSC_EXPORT void blosc2_set_threads_callback(blosc_threads_callback callback, void *callback_data);
784
785
786
/**
787
 * @brief Returns the current number of threads that are used for
788
 * compression/decompression.
789
 */
790
BLOSC_EXPORT int16_t blosc2_get_nthreads(void);
791
792
793
/**
794
 * @brief Initialize a pool of threads for compression/decompression. If
795
 * @p nthreads is 1, then the serial version is chosen and a possible
796
 * previous existing pool is ended. If this is not called, @p nthreads
797
 * is set to 1 internally.
798
 *
799
 * @param nthreads The number of threads to use.
800
 *
801
 * @return The previous number of threads.
802
 */
803
BLOSC_EXPORT int16_t blosc2_set_nthreads(int16_t nthreads);
804
805
806
/**
807
 * @brief Get the current compressor that is used for compression.
808
 *
809
 * @return The string identifying the compressor being used.
810
 */
811
BLOSC_EXPORT const char* blosc1_get_compressor(void);
812
813
814
/**
815
 * @brief Select the compressor to be used. The supported ones are "blosclz",
816
 * "lz4", "lz4hc", "zlib" and "ztsd". If this function is not
817
 * called, then "blosclz" will be used.
818
 *
819
 * @param compname The name identifier of the compressor to be set.
820
 *
821
 * @return The code for the compressor (>=0). In case the compressor
822
 * is not recognized, or there is not support for it in this build,
823
 * it returns a -1.
824
 */
825
BLOSC_EXPORT int blosc1_set_compressor(const char* compname);
826
827
828
/**
829
 * @brief Select the delta coding filter to be used.
830
 *
831
 * @param dodelta A value >0 will activate the delta filter.
832
 * If 0, it will be de-activated
833
 *
834
 * This call should always succeed.
835
 */
836
BLOSC_EXPORT void blosc2_set_delta(int dodelta);
837
838
839
/**
840
 * @brief Get the compressor name associated with the compressor code.
841
 *
842
 * @param compcode The code identifying the compressor
843
 * @param compname The pointer to a string where the compressor name will be put.
844
 *
845
 * @return The compressor code. If the compressor code is not recognized,
846
 * or there is not support for it in this build, -1 is returned.
847
 */
848
BLOSC_EXPORT int blosc2_compcode_to_compname(int compcode, const char** compname);
849
850
851
/**
852
 * @brief Get the compressor code associated with the compressor name.
853
 *
854
 * @param compname The string containing the compressor name.
855
 *
856
 * @return The compressor code. If the compressor name is not recognized,
857
 * or there is not support for it in this build, -1 is returned instead.
858
 */
859
BLOSC_EXPORT int blosc2_compname_to_compcode(const char* compname);
860
861
862
/**
863
 * @brief Get a list of compressors supported in the current build.
864
 *
865
 * @return The comma separated string with the list of compressor names
866
 * supported.
867
 *
868
 * This function does not leak, so you should not free() the returned
869
 * list.
870
 *
871
 * This function should always succeed.
872
 */
873
BLOSC_EXPORT const char* blosc2_list_compressors(void);
874
875
876
/**
877
 * @brief Get the version of Blosc in string format.
878
 *
879
 * @return The string with the current Blosc version.
880
 * Useful for dynamic libraries.
881
 */
882
BLOSC_EXPORT const char* blosc2_get_version_string(void);
883
884
885
/**
886
 * @brief Get info from compression libraries included in the current build.
887
 *
888
 * @param compname The compressor name that you want info from.
889
 * @param complib The pointer to a string where the
890
 * compression library name, if available, will be put.
891
 * @param version The pointer to a string where the
892
 * compression library version, if available, will be put.
893
 *
894
 * @warning You are in charge of the @p complib and @p version strings,
895
 * you should free() them so as to avoid leaks.
896
 *
897
 * @return The code for the compression library (>=0). If it is not supported,
898
 * this function returns -1.
899
 */
900
BLOSC_EXPORT int blosc2_get_complib_info(const char* compname, char** complib,
901
                                         char** version);
902
903
904
/**
905
 * @brief Free possible memory temporaries and thread resources. Use this
906
 * when you are not going to use Blosc for a long while.
907
 *
908
 * @return A 0 if succeeds, in case of problems releasing the resources,
909
 * it returns a negative number.
910
 */
911
BLOSC_EXPORT int blosc2_free_resources(void);
912
913
914
/**
915
 * @brief Get information about a compressed buffer, namely the number of
916
 * uncompressed bytes (@p nbytes) and compressed (@p cbytes). It also
917
 * returns the @p blocksize (which is used internally for doing the
918
 * compression by blocks).
919
 *
920
 * @remark Equivalent to function #blosc2_cbuffer_sizes.
921
 *
922
 * @param cbuffer The buffer of compressed data.
923
 * @param nbytes The pointer where the number of uncompressed bytes will be put.
924
 * @param cbytes The pointer where the number of compressed bytes will be put.
925
 * @param blocksize The pointer where the block size will be put.
926
 *
927
 * You only need to pass the first BLOSC_MIN_HEADER_LENGTH bytes of a
928
 * compressed buffer for this call to work.
929
 *
930
 * This function should always succeed.
931
 */
932
BLOSC_EXPORT void blosc1_cbuffer_sizes(const void* cbuffer, size_t* nbytes,
933
                                       size_t* cbytes, size_t* blocksize);
934
/**
935
 * @brief Get information about a compressed buffer, namely the number of
936
 * uncompressed bytes (@p nbytes) and compressed (@p cbytes). It also
937
 * returns the @p blocksize (which is used internally for doing the
938
 * compression by blocks).
939
 *
940
 * @param cbuffer The buffer of compressed data.
941
 * @param nbytes The pointer where the number of uncompressed bytes will be put.
942
 * @param cbytes The pointer where the number of compressed bytes will be put.
943
 * @param blocksize The pointer where the block size will be put.
944
 *
945
 * @note: if any of the nbytes, cbytes or blocksize is NULL, it will not be returned.
946
 *
947
 * You only need to pass the first BLOSC_MIN_HEADER_LENGTH bytes of a
948
 * compressed buffer for this call to work.
949
 *
950
 * @return On failure, returns negative value.
951
 */
952
BLOSC_EXPORT int blosc2_cbuffer_sizes(const void* cbuffer, int32_t* nbytes,
953
                                      int32_t* cbytes, int32_t* blocksize);
954
955
/**
956
 * @brief Checks that the compressed buffer starting at @p cbuffer of length @p cbytes
957
 * may contain valid blosc compressed data, and that it is safe to call
958
 * blosc1_decompress/blosc1_getitem.
959
 * On success, returns 0 and sets @p nbytes to the size of the uncompressed data.
960
 * This does not guarantee that the decompression function won't return an error,
961
 * but does guarantee that it is safe to attempt decompression.
962
 *
963
 * @param cbuffer The buffer of compressed data.
964
 * @param cbytes The number of compressed bytes.
965
 * @param nbytes The pointer where the number of uncompressed bytes will be put.
966
 *
967
 * @return On failure, returns negative value.
968
 */
969
BLOSC_EXPORT int blosc1_cbuffer_validate(const void* cbuffer, size_t cbytes,
970
                                         size_t* nbytes);
971
972
/**
973
 * @brief Get information about a compressed buffer, namely the type size
974
 * (@p typesize), as well as some internal @p flags.
975
 *
976
 * @param cbuffer The buffer of compressed data.
977
 * @param typesize The pointer where the type size will be put.
978
 * @param flags The pointer of the integer where the additional info is encoded.
979
 * The @p flags is a set of bits, where the currently used ones are:
980
 *   * bit 0: whether the shuffle filter has been applied or not
981
 *   * bit 1: whether the internal buffer is a pure memcpy or not
982
 *   * bit 2: whether the bitshuffle filter has been applied or not
983
 *   * bit 3: whether the delta coding filter has been applied or not
984
 *
985
 * You can use the @p BLOSC_DOSHUFFLE, @p BLOSC_DOBITSHUFFLE, @p BLOSC_DODELTA
986
 * and @p BLOSC_MEMCPYED symbols for extracting the interesting bits
987
 * (e.g. @p flags & @p BLOSC_DOSHUFFLE says whether the buffer is byte-shuffled
988
 * or not).
989
 *
990
 * This function should always succeed.
991
 */
992
BLOSC_EXPORT void blosc1_cbuffer_metainfo(const void* cbuffer, size_t* typesize,
993
                                          int* flags);
994
995
996
/**
997
 * @brief Get information about a compressed buffer, namely the internal
998
 * Blosc format version (@p version) and the format for the internal
999
 * Lempel-Ziv compressor used (@p versionlz).
1000
 *
1001
 * @param cbuffer The buffer of compressed data.
1002
 * @param version The pointer where the Blosc format version will be put.
1003
 * @param versionlz The pointer where the Lempel-Ziv version will be put.
1004
 *
1005
 * This function should always succeed.
1006
 */
1007
BLOSC_EXPORT void blosc2_cbuffer_versions(const void* cbuffer, int* version,
1008
                                          int* versionlz);
1009
1010
1011
/**
1012
 * @brief Get the compressor library/format used in a compressed buffer.
1013
 *
1014
 * @param cbuffer The buffer of compressed data.
1015
 *
1016
 * @return The string identifying the compressor library/format used.
1017
 *
1018
 * This function should always succeed.
1019
 */
1020
BLOSC_EXPORT const char* blosc2_cbuffer_complib(const void* cbuffer);
1021
1022
/*********************************************************************
1023
  Structures and functions related with user-defined input/output.
1024
*********************************************************************/
1025
1026
enum {
1027
  BLOSC2_IO_FILESYSTEM = 0,
1028
  BLOSC2_IO_FILESYSTEM_MMAP = 1,
1029
  BLOSC_IO_LAST_BLOSC_DEFINED = 2,  // sentinel
1030
  BLOSC_IO_LAST_REGISTERED = 32,  // sentinel
1031
};
1032
1033
enum {
1034
  BLOSC2_IO_BLOSC_DEFINED = 32,
1035
  BLOSC2_IO_REGISTERED = 160,
1036
  BLOSC2_IO_USER_DEFINED = 256
1037
};
1038
1039
typedef void*   (*blosc2_open_cb)(const char *urlpath, const char *mode, void *params);
1040
typedef int     (*blosc2_close_cb)(void *stream);
1041
typedef int64_t (*blosc2_size_cb)(void *stream);
1042
typedef int64_t (*blosc2_write_cb)(const void *ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
1043
typedef int64_t (*blosc2_read_cb)(void **ptr, int64_t size, int64_t nitems, int64_t position, void *stream);
1044
typedef int     (*blosc2_truncate_cb)(void *stream, int64_t size);
1045
typedef int     (*blosc2_destroy_cb)(void *params);
1046
1047
1048
/*
1049
 * Input/Output callbacks.
1050
 */
1051
typedef struct {
1052
  uint8_t id;
1053
  //!< The IO identifier.
1054
  char* name;
1055
  //!< The IO name.
1056
  bool is_allocation_necessary;
1057
  //!< If true, the caller needs to allocate data for the read function (ptr argument). If false, the read function
1058
  //!< takes care of memory allocation and stores the address in the allocated_ptr argument.
1059
  blosc2_open_cb open;
1060
  //!< The IO open callback.
1061
  blosc2_close_cb close;
1062
  //!< The IO close callback.
1063
  blosc2_size_cb size;
1064
  //!< The IO size callback.
1065
  blosc2_write_cb write;
1066
  //!< The IO write callback.
1067
  blosc2_read_cb read;
1068
  //!< The IO read callback.
1069
  blosc2_truncate_cb truncate;
1070
  //!< The IO truncate callback.
1071
  blosc2_destroy_cb destroy;
1072
  //!< The IO destroy callback (called in the end when finished with the schunk).
1073
} blosc2_io_cb;
1074
1075
1076
/*
1077
 * Input/Output parameters.
1078
 */
1079
typedef struct {
1080
  uint8_t id;
1081
  const char *name;
1082
  //!< The IO identifier.
1083
  void *params;
1084
  //!< The IO parameters.
1085
} blosc2_io;
1086
1087
static const blosc2_io BLOSC2_IO_DEFAULTS = {
1088
  /* .id = */ BLOSC2_IO_FILESYSTEM,
1089
  /* .name = */ "filesystem",
1090
  /* .params = */ NULL,
1091
};
1092
1093
1094
/**
1095
 * @brief Register a user-defined input/output callbacks in Blosc.
1096
 *
1097
 * @param io The callbacks API to register.
1098
 *
1099
 * @return 0 if succeeds. Else a negative code is returned.
1100
 */
1101
BLOSC_EXPORT int blosc2_register_io_cb(const blosc2_io_cb *io);
1102
1103
/**
1104
 * @brief Get a user-defined input/output callback in Blosc.
1105
 *
1106
 * @param id The id of the callback to get.
1107
 *
1108
 * @return A pointer containing the desired callback if success. Else a NULL pointer is returned.
1109
 */
1110
BLOSC_EXPORT blosc2_io_cb *blosc2_get_io_cb(uint8_t id);
1111
1112
/*********************************************************************
1113
  Structures and functions related with contexts.
1114
*********************************************************************/
1115
1116
typedef struct blosc2_context_s blosc2_context;   /* opaque type */
1117
1118
typedef struct {
1119
  int (*init)(void * config, blosc2_context* cctx, blosc2_context* dctx);
1120
  //!< Initialize tuner. Keep in mind dctx may be NULL. This should memcpy the cctx->tuner_params.
1121
  int (*next_blocksize)(blosc2_context * context);
1122
  //!< Only compute the next blocksize. Only it is executed if tuner is not initialized.
1123
  int (*next_cparams)(blosc2_context * context);
1124
  //!< Compute the next cparams. Only is executed if tuner is initialized.
1125
  int (*update)(blosc2_context * context, double ctime);
1126
  //!< Update the tuner parameters.
1127
  int (*free)(blosc2_context * context);
1128
  //!< Free the tuner.
1129
  int id;
1130
  //!< The tuner id
1131
  char *name;
1132
  //!< The tuner name
1133
} blosc2_tuner;
1134
1135
1136
/**
1137
 * @brief Register locally a user-defined tuner in Blosc.
1138
 *
1139
 * @param tuner The tuner to register.
1140
 *
1141
 * @return 0 if succeeds. Else a negative code is returned.
1142
 */
1143
BLOSC_EXPORT int blosc2_register_tuner(blosc2_tuner *tuner);
1144
1145
1146
/**
1147
 * @brief The parameters for a prefilter function.
1148
 *
1149
 */
1150
typedef struct {
1151
  void *user_data;  // user-provided info (optional)
1152
  const uint8_t *input;  // the input buffer
1153
  uint8_t *output;  // the output buffer
1154
  int32_t output_size;  // the output size (in bytes)
1155
  int32_t output_typesize;  // the output typesize
1156
  int32_t output_offset; // offset to reach the start of the output buffer
1157
  int64_t nchunk;  // the current nchunk in associated schunk (if exists; if not -1)
1158
  int32_t nblock;  // the current nblock in associated chunk
1159
  int32_t tid;  // thread id
1160
  uint8_t *ttmp;  // a temporary that is able to hold several blocks for the output and is private for each thread
1161
  size_t ttmp_nbytes;  // the size of the temporary in bytes
1162
  blosc2_context *ctx;  // the compression context
1163
} blosc2_prefilter_params;
1164
1165
/**
1166
 * @brief The parameters for a postfilter function.
1167
 *
1168
 */
1169
typedef struct {
1170
  void *user_data;  // user-provided info (optional)
1171
  const uint8_t *input;  // the input buffer
1172
  uint8_t *output;  // the output buffer
1173
  int32_t size;  // the input size (in bytes)
1174
  int32_t typesize;  // the input typesize
1175
  int32_t offset;  // offset to reach the start of the input buffer
1176
  int64_t nchunk;  // the current nchunk in associated schunk (if exists; if not -1)
1177
  int32_t nblock;  // the current nblock in associated chunk
1178
  int32_t tid;  // thread id
1179
  uint8_t *ttmp;  // a temporary that is able to hold several blocks for the output and is private for each thread
1180
  size_t ttmp_nbytes;  // the size of the temporary in bytes
1181
  blosc2_context *ctx;  // the decompression context
1182
} blosc2_postfilter_params;
1183
1184
/**
1185
 * @brief The type of the prefilter function.
1186
 *
1187
 * If the function call is successful, the return value should be 0; else, a negative value.
1188
 */
1189
typedef int (*blosc2_prefilter_fn)(blosc2_prefilter_params* params);
1190
1191
/**
1192
 * @brief The type of the postfilter function.
1193
 *
1194
 * If the function call is successful, the return value should be 0; else, a negative value.
1195
 */
1196
typedef int (*blosc2_postfilter_fn)(blosc2_postfilter_params* params);
1197
1198
/**
1199
 * @brief The parameters for creating a context for compression purposes.
1200
 *
1201
 * In parenthesis it is shown the default value used internally when a 0
1202
 * (zero) in the fields of the struct is passed to a function.
1203
 */
1204
typedef struct {
1205
  uint8_t compcode;
1206
  //!< The compressor codec.
1207
  uint8_t compcode_meta;
1208
  //!< The metadata for the compressor codec.
1209
  uint8_t clevel;
1210
  //!< The compression level (5).
1211
  int use_dict;
1212
  //!< Use dicts or not when compressing (only for ZSTD).
1213
  int32_t typesize;
1214
  //!< The type size (8).
1215
  int16_t nthreads;
1216
  //!< The number of threads to use internally (1).
1217
  int32_t blocksize;
1218
  //!< The requested size of the compressed blocks (0 means automatic).
1219
  int32_t splitmode;
1220
  //!< Whether the blocks should be split or not.
1221
  void* schunk;
1222
  //!< The associated schunk, if any (NULL).
1223
  uint8_t filters[BLOSC2_MAX_FILTERS];
1224
  //!< The (sequence of) filters.
1225
  uint8_t filters_meta[BLOSC2_MAX_FILTERS];
1226
  //!< The metadata for filters.
1227
  blosc2_prefilter_fn prefilter;
1228
  //!< The prefilter function.
1229
  blosc2_prefilter_params *preparams;
1230
  //!< The prefilter parameters.
1231
  void *tuner_params;
1232
  //!< Tune configuration.
1233
  int tuner_id;
1234
  //!< The tuner id.
1235
  bool instr_codec;
1236
  //!< Whether the codec is instrumented or not
1237
  void *codec_params;
1238
  //!< User defined parameters for the codec
1239
  void *filter_params[BLOSC2_MAX_FILTERS];
1240
  //!< User defined parameters for the filters
1241
} blosc2_cparams;
1242
1243
/**
1244
 * @brief Default struct for compression params meant for user initialization.
1245
 */
1246
static const blosc2_cparams BLOSC2_CPARAMS_DEFAULTS = {
1247
        BLOSC_BLOSCLZ, 0, 5, 0, 8, 1, 0,
1248
        BLOSC_FORWARD_COMPAT_SPLIT, NULL,
1249
        {0, 0, 0, 0, 0, BLOSC_SHUFFLE},
1250
        {0, 0, 0, 0, 0, 0},
1251
        NULL, NULL, NULL, 0, 0,
1252
        NULL, {NULL, NULL, NULL, NULL, NULL, NULL}
1253
        };
1254
1255
1256
/**
1257
  @brief The parameters for creating a context for decompression purposes.
1258
1259
  In parenthesis it is shown the default value used internally when a 0
1260
  (zero) in the fields of the struct is passed to a function.
1261
 */
1262
typedef struct {
1263
  int16_t nthreads;
1264
  //!< The number of threads to use internally (1).
1265
  void* schunk;
1266
  //!< The associated schunk, if any (NULL).
1267
  blosc2_postfilter_fn postfilter;
1268
  //!< The postfilter function.
1269
  blosc2_postfilter_params *postparams;
1270
  //!< The postfilter parameters.
1271
} blosc2_dparams;
1272
1273
/**
1274
 * @brief Default struct for decompression params meant for user initialization.
1275
 */
1276
static const blosc2_dparams BLOSC2_DPARAMS_DEFAULTS = {1, NULL, NULL, NULL};
1277
1278
/**
1279
 * @brief Create a context for @a *_ctx() compression functions.
1280
 *
1281
 * @param cparams The blosc2_cparams struct with the compression parameters.
1282
 *
1283
 * @return A pointer to the new context. NULL is returned if this fails.
1284
 *
1285
 * @note This supports the same environment variables than #blosc2_compress
1286
 * for overriding the programmatic compression values.
1287
 *
1288
 * @sa #blosc2_compress
1289
 */
1290
BLOSC_EXPORT blosc2_context* blosc2_create_cctx(blosc2_cparams cparams);
1291
1292
/**
1293
 * @brief Create a context for *_ctx() decompression functions.
1294
 *
1295
 * @param dparams The blosc2_dparams struct with the decompression parameters.
1296
 *
1297
 * @return A pointer to the new context. NULL is returned if this fails.
1298
 *
1299
 * @note This supports the same environment variables than #blosc2_decompress
1300
 * for overriding the programmatic decompression values.
1301
 *
1302
 * @sa #blosc2_decompress
1303
 *
1304
 */
1305
BLOSC_EXPORT blosc2_context* blosc2_create_dctx(blosc2_dparams dparams);
1306
1307
/**
1308
 * @brief Free the resources associated with a context.
1309
 *
1310
 * @param context The context to free.
1311
 *
1312
 * This function should always succeed and is valid for contexts meant for
1313
 * both compression and decompression.
1314
 */
1315
BLOSC_EXPORT void blosc2_free_ctx(blosc2_context* context);
1316
1317
/**
1318
 * @brief Create a @p cparams associated to a context.
1319
 *
1320
 * @param ctx The context from where to extract the compression parameters.
1321
 * @param cparams The pointer where the compression params will be stored.
1322
 *
1323
 * @return 0 if succeeds. Else a negative code is returned.
1324
 */
1325
BLOSC_EXPORT int blosc2_ctx_get_cparams(blosc2_context *ctx, blosc2_cparams *cparams);
1326
1327
/**
1328
 * @brief Create a @p dparams associated to a context.
1329
 *
1330
 * @param ctx The context from where to extract the decompression parameters.
1331
 * @param dparams The pointer where the decompression params will be stored.
1332
 *
1333
 * @return 0 if succeeds. Else a negative code is returned.
1334
 */
1335
BLOSC_EXPORT int blosc2_ctx_get_dparams(blosc2_context *ctx, blosc2_dparams *dparams);
1336
1337
/**
1338
 * @brief Set a maskout so as to avoid decompressing specified blocks.
1339
 *
1340
 * @param ctx The decompression context to update.
1341
 *
1342
 * @param maskout The boolean mask for the blocks where decompression
1343
 * is to be avoided.
1344
 *
1345
 * @remark The maskout is valid for contexts *only* meant for decompressing
1346
 * a chunk via #blosc2_decompress_ctx.  Once a call to #blosc2_decompress_ctx
1347
 * is done, this mask is reset so that next call to #blosc2_decompress_ctx
1348
 * will decompress the whole chunk.
1349
 *
1350
 * @param nblocks The number of blocks in maskout above.
1351
 *
1352
 * @return If success, a 0 is returned.  An error is signaled with a negative int.
1353
 *
1354
 */
1355
BLOSC_EXPORT int blosc2_set_maskout(blosc2_context *ctx, bool *maskout, int nblocks);
1356
1357
/**
1358
 * @brief Compress a block of data in the @p src buffer and returns the size of
1359
 * compressed block.
1360
 *
1361
 * @remark Compression is memory safe and guaranteed not to write @p dest
1362
 * more than what is specified in @p destsize.
1363
 * There is not a minimum for @p src buffer size @p nbytes.
1364
 *
1365
 * @warning The @p src buffer and the @p dest buffer can not overlap.
1366
 *
1367
 * @param clevel The desired compression level and must be a number
1368
 * between 0 (no compression) and 9 (maximum compression).
1369
 * @param doshuffle Specifies whether the shuffle compression preconditioner
1370
 * should be applied or not. #BLOSC_NOFILTER means not applying filters,
1371
 * #BLOSC_SHUFFLE means applying shuffle at a byte level and
1372
 * #BLOSC_BITSHUFFLE at a bit level (slower but *may* achieve better
1373
 * compression).
1374
 * @param typesize Is the number of bytes for the atomic type in binary
1375
 * @p src buffer.  This is mainly useful for the shuffle preconditioner.
1376
 * For implementation reasons, only a 1 < typesize < 256 will allow the
1377
 * shuffle filter to work.  When typesize is not in this range, shuffle
1378
 * will be silently disabled.
1379
 * @param src The buffer containing the data to compress.
1380
 * @param srcsize The number of bytes to compress in the @p src buffer.
1381
 * @param dest The buffer where the compressed data will be put,
1382
 * must have at least the size of @p destsize.
1383
 * @param destsize The size of the dest buffer. Blosc
1384
 * guarantees that if you set @p destsize to, at least,
1385
 * (@p nbytes + #BLOSC2_MAX_OVERHEAD), the compression will always succeed.
1386
 *
1387
 * @return The number of bytes compressed.
1388
 * If @p src buffer cannot be compressed into @p destsize, the return
1389
 * value is zero and you should discard the contents of the @p dest
1390
 * buffer. A negative return value means that either a parameter is not correct
1391
 * or that an internal error happened. Set the BLOSC_TRACE environment variable
1392
 * for getting more info on what is happening. If the error is not related with
1393
 * wrong params, please report it back together with the buffer data causing this,
1394
 * as well as the compression params used.
1395
*/
1396
/*
1397
 * Environment variables
1398
 * _____________________
1399
 *
1400
 * *blosc2_compress()* honors different environment variables to control
1401
 * internal parameters without the need of doing that programmatically.
1402
 * Here are the ones supported:
1403
 *
1404
 * **BLOSC_CLEVEL=(INTEGER)**: This will overwrite the @p clevel parameter
1405
 * before the compression process starts.
1406
 *
1407
 * **BLOSC_SHUFFLE=[NOSHUFFLE | SHUFFLE | BITSHUFFLE]**: This will
1408
 * overwrite the *doshuffle* parameter before the compression process
1409
 * starts.
1410
 *
1411
 * **BLOSC_DELTA=(1|0)**: This will call *blosc2_set_delta()^* before the
1412
 * compression process starts.
1413
 *
1414
 * **BLOSC_TYPESIZE=(INTEGER)**: This will overwrite the *typesize*
1415
 * parameter before the compression process starts.
1416
 *
1417
 * **BLOSC_COMPRESSOR=[BLOSCLZ | LZ4 | LZ4HC | ZLIB | ZSTD]**:
1418
 * This will call #blosc_set_compressor before the compression process starts.
1419
 *
1420
 * **BLOSC_NTHREADS=(INTEGER)**: This will call
1421
 * #blosc_set_nthreads before the compression process
1422
 * starts.
1423
 *
1424
 * **BLOSC_SPLITMODE=(ALWAYS | NEVER | AUTO | FORWARD_COMPAT)**:
1425
 * This will call #blosc1_set_splitmode() before the compression process starts.
1426
 *
1427
 * **BLOSC_BLOCKSIZE=(INTEGER)**: This will call
1428
 * #blosc_set_blocksize before the compression process starts.
1429
 * *NOTE:* The blocksize is a critical parameter with
1430
 * important restrictions in the allowed values, so use this with care.
1431
 *
1432
 * **BLOSC_NOLOCK=(ANY VALUE)**: This will call #blosc2_compress_ctx under
1433
 * the hood, with the *compressor*, *blocksize* and
1434
 * *numinternalthreads* parameters set to the same as the last calls to
1435
 * #blosc1_set_compressor, #blosc1_set_blocksize and
1436
 * #blosc2_set_nthreads. *BLOSC_CLEVEL*, *BLOSC_SHUFFLE*, *BLOSC_DELTA* and
1437
 * *BLOSC_TYPESIZE* environment vars will also be honored.
1438
 *
1439
 */
1440
BLOSC_EXPORT int blosc2_compress(int clevel, int doshuffle, int32_t typesize,
1441
                                 const void* src, int32_t srcsize, void* dest,
1442
                                 int32_t destsize);
1443
1444
1445
/**
1446
 * @brief Decompress a block of compressed data in @p src, put the result in
1447
 * @p dest and returns the size of the decompressed block.
1448
 *
1449
 * @warning The @p src buffer and the @p dest buffer can not overlap.
1450
 *
1451
 * @remark Decompression is memory safe and guaranteed not to write the @p dest
1452
 * buffer more than what is specified in @p destsize.
1453
 *
1454
 * @remark In case you want to keep under control the number of bytes read from
1455
 * source, you can call #blosc1_cbuffer_sizes first to check whether the
1456
 * @p nbytes (i.e. the number of bytes to be read from @p src buffer by this
1457
 * function) in the compressed buffer is ok with you.
1458
 *
1459
 * @param src The buffer to be decompressed.
1460
 * @param srcsize The size of the buffer to be decompressed.
1461
 * @param dest The buffer where the decompressed data will be put.
1462
 * @param destsize The size of the @p dest buffer.
1463
 *
1464
 * @return The number of bytes decompressed.
1465
 * If an error occurs, e.g. the compressed data is corrupted or the
1466
 * output buffer is not large enough, then a negative value
1467
 * will be returned instead.
1468
*/
1469
/*
1470
 * Environment variables
1471
 * _____________________
1472
 *
1473
 * *blosc1_decompress* honors different environment variables to control
1474
 * internal parameters without the need of doing that programmatically.
1475
 * Here are the ones supported:
1476
 *
1477
 * **BLOSC_NTHREADS=(INTEGER)**: This will call
1478
 * *blosc_set_nthreads(BLOSC_NTHREADS)* before the proper decompression
1479
 * process starts.
1480
 *
1481
 * **BLOSC_NOLOCK=(ANY VALUE)**: This will call *blosc2_decompress_ctx*
1482
 * under the hood, with the *numinternalthreads* parameter set to the
1483
 * same value as the last call to *blosc2_set_nthreads*.
1484
 *
1485
 */
1486
BLOSC_EXPORT int blosc2_decompress(const void* src, int32_t srcsize,
1487
                                   void* dest, int32_t destsize);
1488
1489
/**
1490
 * @brief Context interface to Blosc compression. This does not require a call
1491
 * to #blosc2_init and can be called from multithreaded applications
1492
 * without the global lock being used, so allowing Blosc be executed
1493
 * simultaneously in those scenarios.
1494
 *
1495
 * @param context A blosc2_context struct with the different compression params.
1496
 * @param src The buffer containing the data to be compressed.
1497
 * @param srcsize The number of bytes to be compressed from the @p src buffer.
1498
 * @param dest The buffer where the compressed data will be put.
1499
 * @param destsize The size in bytes of the @p dest buffer.
1500
 *
1501
 * @return The number of bytes compressed.
1502
 * If @p src buffer cannot be compressed into @p destsize, the return
1503
 * value is zero and you should discard the contents of the @p dest
1504
 * buffer.  A negative return value means that an internal error happened.
1505
 * It could happen that context is not meant for compression (which is stated in stderr).
1506
 * Otherwise, please report it back together with the buffer data causing this
1507
 * and compression settings.
1508
 */
1509
BLOSC_EXPORT int blosc2_compress_ctx(
1510
        blosc2_context* context, const void* src, int32_t srcsize, void* dest,
1511
        int32_t destsize);
1512
1513
1514
/**
1515
 * @brief Context interface to Blosc decompression. This does not require a
1516
 * call to #blosc2_init and can be called from multithreaded
1517
 * applications without the global lock being used, so allowing Blosc
1518
 * be executed simultaneously in those scenarios.
1519
 *
1520
 * @param context The blosc2_context struct with the different compression params.
1521
 * @param src The buffer of compressed data.
1522
 * @param srcsize The length of buffer of compressed data.
1523
 * @param dest The buffer where the decompressed data will be put.
1524
 * @param destsize The size in bytes of the @p dest buffer.
1525
 *
1526
 * @warning The @p src buffer and the @p dest buffer can not overlap.
1527
 *
1528
 * @remark Decompression is memory safe and guaranteed not to write the @p dest
1529
 * buffer more than what is specified in @p destsize.
1530
 *
1531
 * @remark In case you want to keep under control the number of bytes read from
1532
 * source, you can call #blosc1_cbuffer_sizes first to check the @p nbytes
1533
 * (i.e. the number of bytes to be read from @p src buffer by this function)
1534
 * in the compressed buffer.
1535
 *
1536
 * @remark If #blosc2_set_maskout is called prior to this function, its
1537
 * @p block_maskout parameter will be honored for just *one single* shot;
1538
 * i.e. the maskout in context will be automatically reset to NULL, so
1539
 * mask won't be used next time (unless #blosc2_set_maskout is called again).
1540
 *
1541
 * @return The number of bytes decompressed (i.e. the maskout blocks are not
1542
 * counted). If an error occurs, e.g. the compressed data is corrupted,
1543
 * @p destsize is not large enough or context is not meant for decompression,
1544
 * then a negative value will be returned instead.
1545
 */
1546
BLOSC_EXPORT int blosc2_decompress_ctx(blosc2_context* context, const void* src,
1547
                                       int32_t srcsize, void* dest, int32_t destsize);
1548
1549
/**
1550
 * @brief Create a chunk made of zeros.
1551
 *
1552
 * @param cparams The compression parameters.
1553
 * @param nbytes The size (in bytes) of the chunk.
1554
 * @param dest The buffer where the data chunk will be put.
1555
 * @param destsize The size (in bytes) of the @p dest buffer;
1556
 * must be BLOSC_EXTENDED_HEADER_LENGTH at least.
1557
 *
1558
 * @return The number of bytes compressed (BLOSC_EXTENDED_HEADER_LENGTH).
1559
 * If negative, there has been an error and @p dest is unusable.
1560
 * */
1561
BLOSC_EXPORT int blosc2_chunk_zeros(blosc2_cparams cparams, int32_t nbytes,
1562
                                    void* dest, int32_t destsize);
1563
1564
1565
/**
1566
 * @brief Create a chunk made of nans.
1567
 *
1568
 * @param cparams The compression parameters;
1569
 * only 4 bytes (float) and 8 bytes (double) are supported.
1570
 * @param nbytes The size (in bytes) of the chunk.
1571
 * @param dest The buffer where the data chunk will be put.
1572
 * @param destsize The size (in bytes) of the @p dest buffer;
1573
 * must be BLOSC_EXTENDED_HEADER_LENGTH at least.
1574
 *
1575
 * @note Whether the NaNs are floats or doubles will be given by the typesize.
1576
 *
1577
 * @return The number of bytes compressed (BLOSC_EXTENDED_HEADER_LENGTH).
1578
 * If negative, there has been an error and @p dest is unusable.
1579
 * */
1580
BLOSC_EXPORT int blosc2_chunk_nans(blosc2_cparams cparams, int32_t nbytes,
1581
                                   void* dest, int32_t destsize);
1582
1583
1584
/**
1585
 * @brief Create a chunk made of repeated values.
1586
 *
1587
 * @param cparams The compression parameters.
1588
 * @param nbytes The size (in bytes) of the chunk.
1589
 * @param dest The buffer where the data chunk will be put.
1590
 * @param destsize The size (in bytes) of the @p dest buffer.
1591
 * @param repeatval A pointer to the repeated value (little endian).
1592
 * The size of the value is given by @p cparams.typesize param.
1593
 *
1594
 * @return The number of bytes compressed (BLOSC_EXTENDED_HEADER_LENGTH + typesize).
1595
 * If negative, there has been an error and @p dest is unusable.
1596
 * */
1597
BLOSC_EXPORT int blosc2_chunk_repeatval(blosc2_cparams cparams, int32_t nbytes,
1598
                                        void* dest, int32_t destsize, const void* repeatval);
1599
1600
1601
/**
1602
 * @brief Create a chunk made of uninitialized values.
1603
 *
1604
 * @param cparams The compression parameters.
1605
 * @param nbytes The size (in bytes) of the chunk.
1606
 * @param dest The buffer where the data chunk will be put.
1607
 * @param destsize The size (in bytes) of the @p dest buffer;
1608
 * must be BLOSC_EXTENDED_HEADER_LENGTH at least.
1609
 *
1610
 * @return The number of bytes compressed (BLOSC_EXTENDED_HEADER_LENGTH).
1611
 * If negative, there has been an error and @p dest is unusable.
1612
 * */
1613
BLOSC_EXPORT int blosc2_chunk_uninit(blosc2_cparams cparams, int32_t nbytes,
1614
                                     void* dest, int32_t destsize);
1615
1616
1617
/**
1618
 * @brief Context interface counterpart for #blosc1_getitem.
1619
 *
1620
 * @param context Context pointer.
1621
 * @param src The compressed buffer from data will be decompressed.
1622
 * @param srcsize Compressed buffer length.
1623
 * @param start The position of the first item (of @p typesize size) from where data
1624
 * will be retrieved.
1625
 * @param nitems The number of items (of @p typesize size) that will be retrieved.
1626
 * @param dest The buffer where the decompressed data retrieved will be put.
1627
 * @param destsize Output buffer length.
1628
 *
1629
 * @return The number of bytes copied to @p dest or a negative value if
1630
 * some error happens.
1631
 */
1632
BLOSC_EXPORT int blosc2_getitem_ctx(blosc2_context* context, const void* src,
1633
                                    int32_t srcsize, int start, int nitems, void* dest,
1634
                                    int32_t destsize);
1635
1636
1637
/*********************************************************************
1638
  Super-chunk related structures and functions.
1639
*********************************************************************/
1640
1641
0
#define BLOSC2_MAX_METALAYERS 16
1642
3
#define BLOSC2_METALAYER_NAME_MAXLEN 31
1643
1644
// Allow for a reasonable number of vl metalayers
1645
// max is 64 * 1024 due to msgpack map 16 in frame
1646
// mem usage 8 * 1024 entries for blosc2_schunk.vlmetalayers[] is 64 KB
1647
0
#define BLOSC2_MAX_VLMETALAYERS (8 * 1024)
1648
#define BLOSC2_VLMETALAYERS_NAME_MAXLEN BLOSC2_METALAYER_NAME_MAXLEN
1649
1650
/**
1651
 * @brief This struct is meant for holding storage parameters for a
1652
 * for a blosc2 container, allowing to specify, for example, how to interpret
1653
 * the contents included in the schunk.
1654
 */
1655
typedef struct {
1656
    bool contiguous;
1657
    //!< Whether the chunks are contiguous or sparse.
1658
    char* urlpath;
1659
    //!< The path for persistent storage. If NULL, that means in-memory.
1660
    blosc2_cparams* cparams;
1661
    //!< The compression params when creating a schunk.
1662
    //!< If NULL, sensible defaults are used depending on the context.
1663
    blosc2_dparams* dparams;
1664
    //!< The decompression params when creating a schunk.
1665
    //!< If NULL, sensible defaults are used depending on the context.
1666
    blosc2_io *io;
1667
    //!< Input/output backend.
1668
} blosc2_storage;
1669
1670
/**
1671
 * @brief Default struct for #blosc2_storage meant for user initialization.
1672
 */
1673
static const blosc2_storage BLOSC2_STORAGE_DEFAULTS = {false, NULL, NULL, NULL, NULL};
1674
1675
/**
1676
 * @brief Get default struct for compression params meant for user initialization.
1677
 */
1678
BLOSC_EXPORT blosc2_cparams blosc2_get_blosc2_cparams_defaults(void);
1679
1680
/**
1681
 * @brief Get default struct for decompression params meant for user initialization.
1682
 */
1683
BLOSC_EXPORT blosc2_dparams blosc2_get_blosc2_dparams_defaults(void);
1684
1685
/**
1686
 * @brief Get default struct for #blosc2_storage meant for user initialization.
1687
 */
1688
BLOSC_EXPORT blosc2_storage blosc2_get_blosc2_storage_defaults(void);
1689
1690
/**
1691
 * @brief Get default struct for #blosc2_io meant for user initialization.
1692
 */
1693
BLOSC_EXPORT blosc2_io blosc2_get_blosc2_io_defaults(void);
1694
1695
/**
1696
 * @brief Get default struct for #blosc2_stdio_mmap meant for user initialization.
1697
 */
1698
BLOSC_EXPORT blosc2_stdio_mmap blosc2_get_blosc2_stdio_mmap_defaults(void);
1699
1700
typedef struct blosc2_frame_s blosc2_frame;   /* opaque type */
1701
1702
/**
1703
 * @brief This struct is meant to store metadata information inside
1704
 * a #blosc2_schunk, allowing to specify, for example, how to interpret
1705
 * the contents included in the schunk.
1706
 */
1707
typedef struct blosc2_metalayer {
1708
  char* name;          //!< The metalayer identifier for Blosc client (e.g. Blosc2 NDim).
1709
  uint8_t* content;    //!< The serialized (msgpack preferably) content of the metalayer.
1710
  int32_t content_len; //!< The length in bytes of the content.
1711
} blosc2_metalayer;
1712
1713
/**
1714
 * @brief This struct is the standard container for Blosc 2 compressed data.
1715
 *
1716
 * This is essentially a container for Blosc 1 chunks of compressed data,
1717
 * and it allows to overcome the 32-bit limitation in Blosc 1. Optionally,
1718
 * a #blosc2_frame can be attached so as to store the compressed chunks contiguously.
1719
 */
1720
typedef struct blosc2_schunk {
1721
  uint8_t version;
1722
  uint8_t compcode;
1723
  //!< The default compressor. Each chunk can override this.
1724
  uint8_t compcode_meta;
1725
  //!< The default compressor metadata. Each chunk can override this.
1726
  uint8_t clevel;
1727
  //!< The compression level and other compress params.
1728
  uint8_t splitmode;
1729
  //!< The split mode.
1730
  int32_t typesize;
1731
  //!< The type size.
1732
  int32_t blocksize;
1733
  //!< The requested size of the compressed blocks (0; meaning automatic).
1734
  int32_t chunksize;
1735
  //!< Size of each chunk. 0 if not a fixed chunksize.
1736
  uint8_t filters[BLOSC2_MAX_FILTERS];
1737
  //!< The (sequence of) filters.  8-bit per filter.
1738
  uint8_t filters_meta[BLOSC2_MAX_FILTERS];
1739
  //!< Metadata for filters. 8-bit per meta-slot.
1740
  int64_t nchunks;
1741
  //!< Number of chunks in super-chunk.
1742
  int64_t current_nchunk;
1743
  //!< The current chunk that is being accessed
1744
  int64_t nbytes;
1745
  //!< The data size (uncompressed).
1746
  int64_t cbytes;
1747
  //!< The data size + chunks header size (compressed).
1748
  uint8_t** data;
1749
  //!< Pointer to chunk data pointers buffer.
1750
  size_t data_len;
1751
  //!< Length of the chunk data pointers buffer.
1752
  blosc2_storage* storage;
1753
  //!< Pointer to storage info.
1754
  blosc2_frame* frame;
1755
  //!< Pointer to frame used as store for chunks.
1756
  //!<uint8_t* ctx;
1757
  //!< Context for the thread holder. NULL if not acquired.
1758
  blosc2_context* cctx;
1759
  //!< Context for compression
1760
  blosc2_context* dctx;
1761
  //!< Context for decompression.
1762
  struct blosc2_metalayer *metalayers[BLOSC2_MAX_METALAYERS];
1763
  //!< The array of metalayers.
1764
  uint16_t nmetalayers;
1765
  //!< The number of metalayers in the super-chunk
1766
  struct blosc2_metalayer *vlmetalayers[BLOSC2_MAX_VLMETALAYERS];
1767
  //<! The array of variable-length metalayers.
1768
  int16_t nvlmetalayers;
1769
  //!< The number of variable-length metalayers.
1770
  void *tuner_params;
1771
  //!< Tune configuration.
1772
  int tuner_id;
1773
  //<! Id for tuner
1774
  int8_t ndim;
1775
  //<! The ndim (mainly for ZFP usage)
1776
  int64_t *blockshape;
1777
  //<! The blockshape (mainly for ZFP usage)
1778
  bool view;
1779
  //<! Whether the schunk is a view or not.
1780
} blosc2_schunk;
1781
1782
1783
/**
1784
 * @brief Create a new super-chunk.
1785
 *
1786
 * @param storage The storage properties.
1787
 *
1788
 * @remark In case that storage.urlpath is not NULL, the data is stored
1789
 * on-disk.  If the data file(s) exist, they are *overwritten*.
1790
 *
1791
 * @return The new super-chunk.
1792
 */
1793
BLOSC_EXPORT blosc2_schunk* blosc2_schunk_new(blosc2_storage *storage);
1794
1795
/**
1796
 * Create a copy of a super-chunk.
1797
 *
1798
 * @param schunk The super-chunk to be copied.
1799
 * @param storage The storage properties.
1800
 *
1801
 * @return The new super-chunk.
1802
 */
1803
BLOSC_EXPORT blosc2_schunk* blosc2_schunk_copy(blosc2_schunk *schunk, blosc2_storage *storage);
1804
1805
/**
1806
 * @brief Create a super-chunk out of a contiguous frame buffer.
1807
 *
1808
 * @param cframe The buffer of the in-memory frame.
1809
 * @param copy Whether the super-chunk should make a copy of
1810
 * the @p cframe data or not.  The copy will be made to an internal
1811
 * sparse frame.
1812
 *
1813
 * @remark If copy is false, the @p cframe buffer passed will be owned
1814
 * by the super-chunk and will be automatically freed when
1815
 * blosc2_schunk_free() is called.  If the user frees it after the
1816
 * opening, bad things will happen.  Don't do that (or set @p copy).
1817
 *
1818
 * @param len The length of the buffer (in bytes).
1819
 *
1820
 * @return The new super-chunk.
1821
 */
1822
BLOSC_EXPORT blosc2_schunk* blosc2_schunk_from_buffer(uint8_t *cframe, int64_t len, bool copy);
1823
1824
/**
1825
 * @brief Set the private `avoid_cframe_free` field in a frame.
1826
 *
1827
 * @param schunk The super-chunk referencing the frame.
1828
 * @param avoid_cframe_free The value to set in the blosc2_frame_s structure.
1829
 *
1830
 * @warning If you set it to `true` you will be responsible of freeing it.
1831
 */
1832
BLOSC_EXPORT void blosc2_schunk_avoid_cframe_free(blosc2_schunk *schunk, bool avoid_cframe_free);
1833
1834
/**
1835
 * @brief Open an existing super-chunk that is on-disk (frame). No in-memory copy is made.
1836
 *
1837
 * @param urlpath The file name.
1838
 *
1839
 * @return The new super-chunk.  NULL if not found or not in frame format.
1840
 */
1841
BLOSC_EXPORT blosc2_schunk* blosc2_schunk_open(const char* urlpath);
1842
1843
/**
1844
 * @brief Open an existing super-chunk that is on-disk (frame). No in-memory copy is made.
1845
 *
1846
 * @param urlpath The file name.
1847
 *
1848
 * @param offset The frame offset.
1849
 *
1850
 * @return The new super-chunk.  NULL if not found or not in frame format.
1851
 */
1852
BLOSC_EXPORT blosc2_schunk* blosc2_schunk_open_offset(const char* urlpath, int64_t offset);
1853
1854
/**
1855
 * @brief Open an existing super-chunk (no copy is made) using a user-defined I/O interface.
1856
 *
1857
 * @param urlpath The file name.
1858
 *
1859
 * @param udio The user-defined I/O interface.
1860
 *
1861
 * @return The new super-chunk.
1862
 */
1863
BLOSC_EXPORT blosc2_schunk* blosc2_schunk_open_udio(const char* urlpath, const blosc2_io *udio);
1864
1865
/**
1866
 * @brief Open an existing super-chunk (no copy is made) using a user-defined I/O interface.
1867
 *
1868
 * @param urlpath The file name.
1869
 *
1870
 * @param offset The frame offset.
1871
 *
1872
 * @param udio The user-defined I/O interface.
1873
 *
1874
 * @return The new super-chunk.
1875
 */
1876
BLOSC_EXPORT blosc2_schunk* blosc2_schunk_open_offset_udio(const char* urlpath, int64_t offset, const blosc2_io *udio);
1877
1878
/* @brief Convert a super-chunk into a contiguous frame buffer.
1879
 *
1880
 * @param schunk The super-chunk to convert.
1881
 * @param cframe The address of the destination buffer (output).
1882
 * @param needs_free The pointer to a boolean indicating if it is the user's
1883
 * responsibility to free the resulting @p cframe buffer or not.
1884
 *
1885
 * @return If successful, return the size of the (frame) @p cframe buffer.
1886
 * Else, a negative value.
1887
 */
1888
BLOSC_EXPORT int64_t blosc2_schunk_to_buffer(blosc2_schunk* schunk, uint8_t** cframe, bool* needs_free);
1889
1890
/* @brief Store a super-chunk into a file.
1891
 *
1892
 * @param schunk The super-chunk to write.
1893
 * @param urlpath The path for persistent storage.
1894
 *
1895
 * @return If successful, return the size of the (fileframe) in @p urlpath.
1896
 * Else, a negative value.
1897
 */
1898
BLOSC_EXPORT int64_t blosc2_schunk_to_file(blosc2_schunk* schunk, const char* urlpath);
1899
1900
/* @brief Append a super-chunk into a file.
1901
 *
1902
 * @param schunk The super-chunk to write.
1903
 * @param urlpath The path for persistent storage.
1904
 *
1905
 * @return If successful, return the offset where @p schunk has been appended in @p urlpath.
1906
 * Else, a negative value.
1907
 */
1908
BLOSC_EXPORT int64_t blosc2_schunk_append_file(blosc2_schunk* schunk, const char* urlpath);
1909
1910
/**
1911
 * @brief Release resources from a super-chunk.
1912
 *
1913
 * @param schunk The super-chunk to be freed.
1914
 *
1915
 * @remark All the memory resources attached to the super-chunk are freed.
1916
 * If the super-chunk is on-disk, the data continues there for a later
1917
 * re-opening.
1918
 *
1919
 * @return 0 if success.
1920
 */
1921
BLOSC_EXPORT int blosc2_schunk_free(blosc2_schunk *schunk);
1922
1923
/**
1924
 * @brief Append an existing @p chunk to a super-chunk.
1925
 *
1926
 * @param schunk The super-chunk where the chunk will be appended.
1927
 * @param chunk The @p chunk to append.  An internal copy is made, so @p chunk can be reused or
1928
 * freed if desired.
1929
 * @param copy Whether the chunk should be copied internally or can be used as-is.
1930
 *
1931
 * @return The number of chunks in super-chunk. If some problem is
1932
 * detected, this number will be negative.
1933
 */
1934
BLOSC_EXPORT int64_t blosc2_schunk_append_chunk(blosc2_schunk *schunk, uint8_t *chunk, bool copy);
1935
1936
/**
1937
  * @brief Update a chunk at a specific position in a super-chunk.
1938
  *
1939
  * @param schunk The super-chunk where the chunk will be updated.
1940
  * @param nchunk The position where the chunk will be updated.
1941
  * @param chunk The new @p chunk. If an internal copy is made, the @p chunk can be reused or
1942
  * freed if desired.
1943
  * @param copy Whether the chunk should be copied internally or can be used as-is.
1944
  *
1945
  * @return The number of chunks in super-chunk. If some problem is
1946
  * detected, this number will be negative.
1947
  */
1948
BLOSC_EXPORT int64_t blosc2_schunk_update_chunk(blosc2_schunk *schunk, int64_t nchunk, uint8_t *chunk, bool copy);
1949
1950
/**
1951
 * @brief Insert a chunk at a specific position in a super-chunk.
1952
 *
1953
 * @param schunk The super-chunk where the chunk will be appended.
1954
 * @param nchunk The position where the chunk will be inserted.
1955
 * @param chunk The @p chunk to insert. If an internal copy is made, the @p chunk can be reused or
1956
 * freed if desired.
1957
 * @param copy Whether the chunk should be copied internally or can be used as-is.
1958
 *
1959
 * @return The number of chunks in super-chunk. If some problem is
1960
 * detected, this number will be negative.
1961
 */
1962
BLOSC_EXPORT int64_t blosc2_schunk_insert_chunk(blosc2_schunk *schunk, int64_t nchunk, uint8_t *chunk, bool copy);
1963
1964
/**
1965
 * @brief Delete a chunk at a specific position in a super-chunk.
1966
 *
1967
 * @param schunk The super-chunk where the chunk will be deleted.
1968
 * @param nchunk The position where the chunk will be deleted.
1969
 *
1970
 * @return The number of chunks in super-chunk. If some problem is
1971
 * detected, this number will be negative.
1972
 */
1973
BLOSC_EXPORT int64_t blosc2_schunk_delete_chunk(blosc2_schunk *schunk, int64_t nchunk);
1974
1975
/**
1976
 * @brief Append a @p src data buffer to a super-chunk.
1977
 *
1978
 * @param schunk The super-chunk where data will be appended.
1979
 * @param src The buffer of data to compress.
1980
 * @param nbytes The size of the @p src buffer.
1981
 *
1982
 * @return The number of chunks in super-chunk. If some problem is
1983
 * detected, this number will be negative.
1984
 */
1985
BLOSC_EXPORT int64_t blosc2_schunk_append_buffer(blosc2_schunk *schunk, const void *src, int32_t nbytes);
1986
1987
/**
1988
 * @brief Decompress and return the @p nchunk chunk of a super-chunk.
1989
 *
1990
 * If the chunk is uncompressed successfully, it is put in the @p *dest
1991
 * pointer.
1992
 *
1993
 * @param schunk The super-chunk from where the chunk will be decompressed.
1994
 * @param nchunk The chunk to be decompressed (0 indexed).
1995
 * @param dest The buffer where the decompressed data will be put.
1996
 * @param nbytes The size of the area pointed by @p *dest.
1997
 *
1998
 * @warning You must make sure that you have enough space to store the
1999
 * uncompressed data.
2000
 *
2001
 * @return The size of the decompressed chunk or 0 if it is non-initialized. If some problem is
2002
 * detected, a negative code is returned instead.
2003
 */
2004
BLOSC_EXPORT int blosc2_schunk_decompress_chunk(blosc2_schunk *schunk, int64_t nchunk, void *dest, int32_t nbytes);
2005
2006
/**
2007
 * @brief Return a compressed chunk that is part of a super-chunk in the @p chunk parameter.
2008
 *
2009
 * @param schunk The super-chunk from where to extract a chunk.
2010
 * @param nchunk The chunk to be extracted (0 indexed).
2011
 * @param chunk The pointer to the chunk of compressed data.
2012
 * @param needs_free The pointer to a boolean indicating if it is the user's
2013
 * responsibility to free the chunk returned or not.
2014
 *
2015
 * @warning If the super-chunk is backed by a frame that is disk-based, a buffer is allocated for the
2016
 * (compressed) chunk, and hence a free is needed.
2017
 * You can check whether the chunk requires a free with the @p needs_free parameter.
2018
 * If the chunk does not need a free, it means that a pointer to the location in the super-chunk
2019
 * (or the backing in-memory frame) is returned in the @p chunk parameter.
2020
 *
2021
 * @return The size of the (compressed) chunk or 0 if it is non-initialized. If some problem is
2022
 * detected, a negative code is returned instead.
2023
 */
2024
BLOSC_EXPORT int blosc2_schunk_get_chunk(blosc2_schunk *schunk, int64_t nchunk, uint8_t **chunk,
2025
                                         bool *needs_free);
2026
2027
/**
2028
 * @brief Return a (lazy) compressed chunk that is part of a super-chunk in the @p chunk parameter.
2029
 *
2030
 * @param schunk The super-chunk from where to extract a chunk.
2031
 * @param nchunk The chunk to be extracted (0 indexed).
2032
 * @param chunk The pointer to the (lazy) chunk of compressed data.
2033
 * @param needs_free The pointer to a boolean indicating if it is the user's
2034
 * responsibility to free the chunk returned or not.
2035
 *
2036
 * @note For disk-based frames, a lazy chunk is always returned.
2037
 *
2038
 * @warning Currently, a lazy chunk can only be used by #blosc2_decompress_ctx and #blosc2_getitem_ctx.
2039
 *
2040
 * @warning If the super-chunk is backed by a frame that is disk-based, a buffer is allocated for the
2041
 * (compressed) chunk, and hence a free is needed.
2042
 * You can check whether requires a free with the @p needs_free parameter.
2043
 * If the chunk does not need a free, it means that a pointer to the location in the super-chunk
2044
 * (or the backing in-memory frame) is returned in the @p chunk parameter.  In this case the returned
2045
 * chunk is not lazy.
2046
 *
2047
 * @return The size of the (compressed) chunk or 0 if it is non-initialized. If some problem is
2048
 * detected, a negative code is returned instead.  Note that a lazy chunk is somewhat larger than
2049
 * a regular chunk because of the trailer section (for details see `README_CHUNK_FORMAT.rst`).
2050
 */
2051
BLOSC_EXPORT int blosc2_schunk_get_lazychunk(blosc2_schunk *schunk, int64_t nchunk, uint8_t **chunk,
2052
                                             bool *needs_free);
2053
2054
/**
2055
 * @brief Fill buffer with a schunk slice.
2056
 *
2057
 * @param schunk The super-chunk from where to extract a slice.
2058
 * @param start Index (0-based) where the slice begins.
2059
 * @param stop The first index (0-based) that is not in the selected slice.
2060
 * @param buffer The buffer where the data will be stored.
2061
 *
2062
 * @warning You must make sure that you have enough space in buffer to store the
2063
 * uncompressed data.
2064
 *
2065
 * @return An error code.
2066
 */
2067
BLOSC_EXPORT int blosc2_schunk_get_slice_buffer(blosc2_schunk *schunk, int64_t start, int64_t stop, void *buffer);
2068
2069
/**
2070
 * @brief Update a schunk slice from buffer.
2071
 *
2072
 * @param schunk The super-chunk where to set the slice.
2073
 * @param start Index (0-based) where the slice begins.
2074
 * @param stop The first index (0-based) that is not in the selected slice.
2075
 * @param buffer The buffer containing the data to set.
2076
 *
2077
 *
2078
 * @return An error code.
2079
 */
2080
BLOSC_EXPORT int blosc2_schunk_set_slice_buffer(blosc2_schunk *schunk, int64_t start, int64_t stop, void *buffer);
2081
2082
/**
2083
 * @brief Return the @p cparams associated to a super-chunk.
2084
 *
2085
 * @param schunk The super-chunk from where to extract the compression parameters.
2086
 * @param cparams The pointer where the compression params will be returned.
2087
 *
2088
 * @warning A new struct is allocated, and the user should free it after use.
2089
 *
2090
 * @return 0 if succeeds. Else a negative code is returned.
2091
 */
2092
BLOSC_EXPORT int blosc2_schunk_get_cparams(blosc2_schunk *schunk, blosc2_cparams **cparams);
2093
2094
/**
2095
 * @brief Return the @p dparams struct associated to a super-chunk.
2096
 *
2097
 * @param schunk The super-chunk from where to extract the decompression parameters.
2098
 * @param dparams The pointer where the decompression params will be returned.
2099
 *
2100
 * @warning A new struct is allocated, and the user should free it after use.
2101
 *
2102
 * @return 0 if succeeds. Else a negative code is returned.
2103
 */
2104
BLOSC_EXPORT int blosc2_schunk_get_dparams(blosc2_schunk *schunk, blosc2_dparams **dparams);
2105
2106
/**
2107
 * @brief Reorder the chunk offsets of an existing super-chunk.
2108
 *
2109
 * @param schunk The super-chunk whose chunk offsets are to be reordered.
2110
 * @param offsets_order The new order of the chunk offsets.
2111
 *
2112
 * @return 0 if succeeds. Else a negative code is returned.
2113
 */
2114
BLOSC_EXPORT int blosc2_schunk_reorder_offsets(blosc2_schunk *schunk, int64_t *offsets_order);
2115
2116
/**
2117
 * @brief Get the length (in bytes) of the internal frame of the super-chunk.
2118
 *
2119
 * @param schunk The super-chunk.
2120
 *
2121
 * @return The length (in bytes) of the internal frame.
2122
 * If there is not an internal frame, an estimate of the length is provided.
2123
 */
2124
BLOSC_EXPORT int64_t blosc2_schunk_frame_len(blosc2_schunk* schunk);
2125
2126
/**
2127
 * @brief Quickly fill an empty frame with special values (zeros, NaNs, uninit).
2128
 *
2129
 * @param schunk The super-chunk to be filled.  This must be empty initially.
2130
 * @param nitems The number of items to fill.
2131
 * @param special_value The special value to use for filling.  The only values
2132
 * supported for now are BLOSC2_SPECIAL_ZERO, BLOSC2_SPECIAL_NAN and BLOSC2_SPECIAL_UNINIT.
2133
 * @param chunksize The chunksize for the chunks that are to be added to the super-chunk.
2134
 *
2135
 * @return The total number of chunks that have been added to the super-chunk.
2136
 * If there is an error, a negative value is returned.
2137
 */
2138
BLOSC_EXPORT int64_t blosc2_schunk_fill_special(blosc2_schunk* schunk, int64_t nitems,
2139
                                                int special_value, int32_t chunksize);
2140
2141
2142
/*********************************************************************
2143
  Functions related with fixed-length metalayers.
2144
*********************************************************************/
2145
2146
/**
2147
 * @brief Find whether the schunk has a metalayer or not.
2148
 *
2149
 * @param schunk The super-chunk from which the metalayer will be checked.
2150
 * @param name The name of the metalayer to be checked.
2151
 *
2152
 * @return If successful, return the index of the metalayer. Else, return a negative value.
2153
 */
2154
3
static inline int blosc2_meta_exists(blosc2_schunk *schunk, const char *name) {
2155
3
  if (strlen(name) > BLOSC2_METALAYER_NAME_MAXLEN) {
2156
0
    BLOSC_TRACE_ERROR("Metalayers cannot be larger than %d chars.", BLOSC2_METALAYER_NAME_MAXLEN);
2157
0
    return BLOSC2_ERROR_INVALID_PARAM;
2158
0
  }
2159
2160
3
  if (schunk == NULL) {
2161
3
    BLOSC_TRACE_ERROR("Schunk must not be NUll.");
2162
0
    return BLOSC2_ERROR_INVALID_PARAM;
2163
3
  }
2164
2165
0
  for (int nmetalayer = 0; nmetalayer < schunk->nmetalayers; nmetalayer++) {
2166
0
    if (strcmp(name, schunk->metalayers[nmetalayer]->name) == 0) {
2167
0
      return nmetalayer;
2168
0
    }
2169
0
  }
2170
0
  return BLOSC2_ERROR_NOT_FOUND;
2171
0
}
Unexecuted instantiation: fuzz_decompress_chunk.c:blosc2_meta_exists
Unexecuted instantiation: blosc2.c:blosc2_meta_exists
Unexecuted instantiation: blosclz.c:blosc2_meta_exists
Unexecuted instantiation: schunk.c:blosc2_meta_exists
Unexecuted instantiation: frame.c:blosc2_meta_exists
Unexecuted instantiation: stune.c:blosc2_meta_exists
Unexecuted instantiation: trunc-prec.c:blosc2_meta_exists
Unexecuted instantiation: timestamp.c:blosc2_meta_exists
Unexecuted instantiation: sframe.c:blosc2_meta_exists
Unexecuted instantiation: blosc2-stdio.c:blosc2_meta_exists
Unexecuted instantiation: b2nd.c:blosc2_meta_exists
Unexecuted instantiation: b2nd_utils.c:blosc2_meta_exists
Unexecuted instantiation: shuffle.c:blosc2_meta_exists
Unexecuted instantiation: blosc2-zfp.c:blosc2_meta_exists
Unexecuted instantiation: codecs-registry.c:blosc2_meta_exists
Unexecuted instantiation: tuners-registry.c:blosc2_meta_exists
Unexecuted instantiation: filters-registry.c:blosc2_meta_exists
Unexecuted instantiation: ndlz.c:blosc2_meta_exists
Unexecuted instantiation: ndlz4x4.c:blosc2_meta_exists
Unexecuted instantiation: ndlz8x8.c:blosc2_meta_exists
ndcell.c:blosc2_meta_exists
Line
Count
Source
2154
1
static inline int blosc2_meta_exists(blosc2_schunk *schunk, const char *name) {
2155
1
  if (strlen(name) > BLOSC2_METALAYER_NAME_MAXLEN) {
2156
0
    BLOSC_TRACE_ERROR("Metalayers cannot be larger than %d chars.", BLOSC2_METALAYER_NAME_MAXLEN);
2157
0
    return BLOSC2_ERROR_INVALID_PARAM;
2158
0
  }
2159
2160
1
  if (schunk == NULL) {
2161
1
    BLOSC_TRACE_ERROR("Schunk must not be NUll.");
2162
0
    return BLOSC2_ERROR_INVALID_PARAM;
2163
1
  }
2164
2165
0
  for (int nmetalayer = 0; nmetalayer < schunk->nmetalayers; nmetalayer++) {
2166
0
    if (strcmp(name, schunk->metalayers[nmetalayer]->name) == 0) {
2167
0
      return nmetalayer;
2168
0
    }
2169
0
  }
2170
0
  return BLOSC2_ERROR_NOT_FOUND;
2171
0
}
ndmean.c:blosc2_meta_exists
Line
Count
Source
2154
2
static inline int blosc2_meta_exists(blosc2_schunk *schunk, const char *name) {
2155
2
  if (strlen(name) > BLOSC2_METALAYER_NAME_MAXLEN) {
2156
0
    BLOSC_TRACE_ERROR("Metalayers cannot be larger than %d chars.", BLOSC2_METALAYER_NAME_MAXLEN);
2157
0
    return BLOSC2_ERROR_INVALID_PARAM;
2158
0
  }
2159
2160
2
  if (schunk == NULL) {
2161
2
    BLOSC_TRACE_ERROR("Schunk must not be NUll.");
2162
0
    return BLOSC2_ERROR_INVALID_PARAM;
2163
2
  }
2164
2165
0
  for (int nmetalayer = 0; nmetalayer < schunk->nmetalayers; nmetalayer++) {
2166
0
    if (strcmp(name, schunk->metalayers[nmetalayer]->name) == 0) {
2167
0
      return nmetalayer;
2168
0
    }
2169
0
  }
2170
0
  return BLOSC2_ERROR_NOT_FOUND;
2171
0
}
Unexecuted instantiation: bytedelta.c:blosc2_meta_exists
Unexecuted instantiation: int_trunc.c:blosc2_meta_exists
2172
2173
/**
2174
 * @brief Add content into a new metalayer.
2175
 *
2176
 * @param schunk The super-chunk to which the metalayer should be added.
2177
 * @param name The name of the metalayer.
2178
 * @param content The content of the metalayer.
2179
 * @param content_len The length of the content.
2180
 *
2181
 * @return If successful, the index of the new metalayer. Else, return a negative value.
2182
 */
2183
BLOSC_EXPORT int blosc2_meta_add(blosc2_schunk *schunk, const char *name, uint8_t *content,
2184
                                 int32_t content_len);
2185
2186
/**
2187
 * @brief Update the content of an existing metalayer.
2188
 *
2189
 * @param schunk The frame containing the metalayer.
2190
 * @param name The name of the metalayer to be updated.
2191
 * @param content The new content of the metalayer.
2192
 * @param content_len The length of the content.
2193
 *
2194
 * @note Contrarily to #blosc2_meta_add the updates to metalayers
2195
 * are automatically serialized into a possible attached frame.
2196
 *
2197
 * @return If successful, the index of the metalayer. Else, return a negative value.
2198
 */
2199
BLOSC_EXPORT int blosc2_meta_update(blosc2_schunk *schunk, const char *name, uint8_t *content,
2200
                                    int32_t content_len);
2201
2202
/**
2203
 * @brief Get the content out of a metalayer.
2204
 *
2205
 * @param schunk The frame containing the metalayer.
2206
 * @param name The name of the metalayer.
2207
 * @param content The pointer where the content will be put.
2208
 * @param content_len The length of the content.
2209
 *
2210
 * @warning The @p **content receives a malloc'ed copy of the content.
2211
 * The user is responsible of freeing it.
2212
 *
2213
 * @note This function is inlined and available even when not linking with libblosc2.
2214
 *
2215
 * @return If successful, the index of the new metalayer. Else, return a negative value.
2216
 */
2217
static inline int blosc2_meta_get(blosc2_schunk *schunk, const char *name, uint8_t **content,
2218
3
                                  int32_t *content_len) {
2219
3
  int nmetalayer = blosc2_meta_exists(schunk, name);
2220
3
  if (nmetalayer < 0) {
2221
3
    BLOSC_TRACE_WARNING("Metalayer \"%s\" not found.", name);
2222
0
    return nmetalayer;
2223
3
  }
2224
0
  *content_len = schunk->metalayers[nmetalayer]->content_len;
2225
0
  *content = (uint8_t*)malloc((size_t)*content_len);
2226
0
  memcpy(*content, schunk->metalayers[nmetalayer]->content, (size_t)*content_len);
2227
0
  return nmetalayer;
2228
3
}
Unexecuted instantiation: fuzz_decompress_chunk.c:blosc2_meta_get
Unexecuted instantiation: blosc2.c:blosc2_meta_get
Unexecuted instantiation: blosclz.c:blosc2_meta_get
Unexecuted instantiation: schunk.c:blosc2_meta_get
Unexecuted instantiation: frame.c:blosc2_meta_get
Unexecuted instantiation: stune.c:blosc2_meta_get
Unexecuted instantiation: trunc-prec.c:blosc2_meta_get
Unexecuted instantiation: timestamp.c:blosc2_meta_get
Unexecuted instantiation: sframe.c:blosc2_meta_get
Unexecuted instantiation: blosc2-stdio.c:blosc2_meta_get
Unexecuted instantiation: b2nd.c:blosc2_meta_get
Unexecuted instantiation: b2nd_utils.c:blosc2_meta_get
Unexecuted instantiation: shuffle.c:blosc2_meta_get
Unexecuted instantiation: blosc2-zfp.c:blosc2_meta_get
Unexecuted instantiation: codecs-registry.c:blosc2_meta_get
Unexecuted instantiation: tuners-registry.c:blosc2_meta_get
Unexecuted instantiation: filters-registry.c:blosc2_meta_get
Unexecuted instantiation: ndlz.c:blosc2_meta_get
Unexecuted instantiation: ndlz4x4.c:blosc2_meta_get
Unexecuted instantiation: ndlz8x8.c:blosc2_meta_get
ndcell.c:blosc2_meta_get
Line
Count
Source
2218
1
                                  int32_t *content_len) {
2219
1
  int nmetalayer = blosc2_meta_exists(schunk, name);
2220
1
  if (nmetalayer < 0) {
2221
1
    BLOSC_TRACE_WARNING("Metalayer \"%s\" not found.", name);
2222
0
    return nmetalayer;
2223
1
  }
2224
0
  *content_len = schunk->metalayers[nmetalayer]->content_len;
2225
0
  *content = (uint8_t*)malloc((size_t)*content_len);
2226
0
  memcpy(*content, schunk->metalayers[nmetalayer]->content, (size_t)*content_len);
2227
0
  return nmetalayer;
2228
1
}
ndmean.c:blosc2_meta_get
Line
Count
Source
2218
2
                                  int32_t *content_len) {
2219
2
  int nmetalayer = blosc2_meta_exists(schunk, name);
2220
2
  if (nmetalayer < 0) {
2221
2
    BLOSC_TRACE_WARNING("Metalayer \"%s\" not found.", name);
2222
0
    return nmetalayer;
2223
2
  }
2224
0
  *content_len = schunk->metalayers[nmetalayer]->content_len;
2225
0
  *content = (uint8_t*)malloc((size_t)*content_len);
2226
0
  memcpy(*content, schunk->metalayers[nmetalayer]->content, (size_t)*content_len);
2227
0
  return nmetalayer;
2228
2
}
Unexecuted instantiation: bytedelta.c:blosc2_meta_get
Unexecuted instantiation: int_trunc.c:blosc2_meta_get
2229
2230
2231
/*********************************************************************
2232
  Variable-length metalayers functions.
2233
*********************************************************************/
2234
2235
/**
2236
 * @brief Find whether the schunk has a variable-length metalayer or not.
2237
 *
2238
 * @param schunk The super-chunk from which the variable-length metalayer will be checked.
2239
 * @param name The name of the variable-length metalayer to be checked.
2240
 *
2241
 * @return If successful, return the index of the variable-length metalayer. Else, return a negative value.
2242
 */
2243
BLOSC_EXPORT int blosc2_vlmeta_exists(blosc2_schunk *schunk, const char *name);
2244
2245
/**
2246
 * @brief Add content into a new variable-length metalayer.
2247
 *
2248
 * @param schunk The super-chunk to which the variable-length metalayer should be added.
2249
 * @param name The name of the variable-length metalayer.
2250
 * @param content The content to be added.
2251
 * @param content_len The length of the content.
2252
 * @param cparams The parameters for compressing the variable-length metalayer content. If NULL,
2253
 * the `BLOSC2_CPARAMS_DEFAULTS` will be used.
2254
 *
2255
 * @return If successful, the index of the new variable-length metalayer. Else, return a negative value.
2256
 */
2257
BLOSC_EXPORT int blosc2_vlmeta_add(blosc2_schunk *schunk, const char *name,
2258
                                   uint8_t *content, int32_t content_len,
2259
                                   blosc2_cparams *cparams);
2260
2261
/**
2262
 * @brief Update the content of an existing variable-length metalayer.
2263
 *
2264
 * @param schunk The super-chunk containing the variable-length metalayer.
2265
 * @param name The name of the variable-length metalayer to be updated.
2266
 * @param content The new content of the variable-length metalayer.
2267
 * @param content_len The length of the content.
2268
 * @param cparams The parameters for compressing the variable-length metalayer content. If NULL,
2269
 * the `BLOSC2_CPARAMS_DEFAULTS` will be used.
2270
 *
2271
 * @return If successful, the index of the variable-length metalayer. Else, return a negative value.
2272
 */
2273
BLOSC_EXPORT int blosc2_vlmeta_update(blosc2_schunk *schunk, const char *name,
2274
                                      uint8_t *content, int32_t content_len,
2275
                                      blosc2_cparams *cparams);
2276
2277
/**
2278
 * @brief Get the content out of a variable-length metalayer.
2279
 *
2280
 * @param schunk The super-chunk containing the variable-length metalayer.
2281
 * @param name The name of the variable-length metalayer.
2282
 * @param content The pointer where the content will be put.
2283
 * @param content_len The pointer where the length of the content will be put.
2284
 *
2285
 * @warning The @p **content receives a malloc'ed copy of the content.
2286
 * The user is responsible of freeing it.
2287
 *
2288
 * @return If successful, the index of the new variable-length metalayer. Else, return a negative value.
2289
 */
2290
BLOSC_EXPORT int blosc2_vlmeta_get(blosc2_schunk *schunk, const char *name,
2291
                                   uint8_t **content, int32_t *content_len);
2292
2293
/**
2294
 * @brief Delete the variable-length metalayer from the super-chunk.
2295
 *
2296
 * @param schunk The super-chunk containing the variable-length metalayer.
2297
 * @param name The name of the variable-length metalayer.
2298
 *
2299
 * @return If successful, the number of the variable-length metalayers in the super-chunk. Else, return a negative value.
2300
 */
2301
BLOSC_EXPORT int blosc2_vlmeta_delete(blosc2_schunk *schunk, const char *name);
2302
2303
/**
2304
 * @brief Get a list of all the variable-length metalayer names.
2305
 *
2306
 * @param schunk The super-chunk containing the variable-length metalayers.
2307
 * @param names The pointer to a char** to store the name pointers. This should
2308
 * be of size *schunk->nvlmetalayers * sizeof(char*).
2309
 *
2310
 * @return The number of the variable-length metalayers in the super-chunk.
2311
 * This cannot fail unless the user does not pass a @p names which is large enough to
2312
 * keep pointers to all names, in which case funny things (seg faults and such) will happen.
2313
 */
2314
BLOSC_EXPORT int blosc2_vlmeta_get_names(blosc2_schunk *schunk, char **names);
2315
2316
2317
/*********************************************************************
2318
  Time measurement utilities.
2319
*********************************************************************/
2320
2321
#if defined(_WIN32)
2322
/* For QueryPerformanceCounter(), etc. */
2323
  #include <windows.h>
2324
#elif defined(__MACH__)
2325
#include <mach/clock.h>
2326
#include <mach/mach.h>
2327
#include <time.h>
2328
#elif defined(__unix__)
2329
#if defined(__linux__)
2330
    #include <time.h>
2331
  #else
2332
    #include <sys/time.h>
2333
  #endif
2334
#else
2335
  #error Unable to detect platform.
2336
#endif
2337
2338
/* The type of timestamp used on this system. */
2339
#if defined(_WIN32)
2340
typedef LARGE_INTEGER blosc_timestamp_t;
2341
#else
2342
typedef struct timespec blosc_timestamp_t;
2343
#endif
2344
2345
/*
2346
 * @brief Set a timestamp.
2347
 *
2348
 * @param timestamp
2349
 *
2350
 */
2351
BLOSC_EXPORT void blosc_set_timestamp(blosc_timestamp_t* timestamp);
2352
2353
/*
2354
 * @brief Get the nanoseconds between 2 timestamps.
2355
 *
2356
 * @param start_time
2357
 * @param end_time
2358
 *
2359
 * @return The nanoseconds between start_time and end_time.
2360
 */
2361
BLOSC_EXPORT double blosc_elapsed_nsecs(blosc_timestamp_t start_time,
2362
                                        blosc_timestamp_t end_time);
2363
2364
/*
2365
 * @brief Get the seconds between 2 timestamps.
2366
 *
2367
 * @param start_time
2368
 * @param end_time
2369
 *
2370
 * @return The seconds between start_time and end_time.
2371
 */
2372
BLOSC_EXPORT double blosc_elapsed_secs(blosc_timestamp_t start_time,
2373
                                       blosc_timestamp_t end_time);
2374
2375
2376
/*********************************************************************
2377
  Low-level functions follows.  Use them only if you are an expert!
2378
*********************************************************************/
2379
2380
/**
2381
 * @brief Get the internal blocksize to be used during compression. 0 means
2382
 * that an automatic blocksize is computed internally.
2383
 *
2384
 * @return The size in bytes of the internal block size.
2385
 */
2386
BLOSC_EXPORT int blosc1_get_blocksize(void);
2387
2388
/**
2389
 * @brief Force the use of a specific blocksize. If 0, an automatic
2390
 * blocksize will be used (the default).
2391
 *
2392
 * @warning The blocksize is a critical parameter with important
2393
 * restrictions in the allowed values, so use this with care.
2394
 */
2395
BLOSC_EXPORT void blosc1_set_blocksize(size_t blocksize);
2396
2397
2398
/**
2399
  * @brief Set the split mode.
2400
2401
  * @param splitmode It can take the next values:
2402
  *  BLOSC_FORWARD_COMPAT_SPLIT
2403
  *  BLOSC_AUTO_SPLIT
2404
  *  BLOSC_NEVER_SPLIT
2405
  *  BLOSC_ALWAYS_SPLIT
2406
  *
2407
  * BLOSC_FORWARD_COMPAT offers reasonably forward compatibility,
2408
  * BLOSC_AUTO_SPLIT is for nearly optimal results (based on heuristics),
2409
  * BLOSC_NEVER_SPLIT and BLOSC_ALWAYS_SPLIT are for the user experimenting
2410
  *  when trying to get best compression ratios and/or speed.
2411
  *
2412
  * If not called, the default mode is BLOSC_FORWARD_COMPAT_SPLIT.
2413
  *
2414
  * This function should always succeed.
2415
 */
2416
BLOSC_EXPORT void blosc1_set_splitmode(int splitmode);
2417
2418
2419
/**
2420
 * @brief Get the offsets of a frame in a super-chunk.
2421
 *
2422
 * @param schunk The super-chunk containing the frame.
2423
 *
2424
 * @return If successful, return a pointer to a buffer of the decompressed offsets.
2425
 * The number of offsets is equal to schunk->nchunks; the user is
2426
 * responsible to free this buffer. Else, return a NULL value.
2427
 */
2428
BLOSC_EXPORT int64_t* blosc2_frame_get_offsets(blosc2_schunk *schunk);
2429
2430
2431
/*********************************************************************
2432
  Structures and functions related with compression codecs.
2433
*********************************************************************/
2434
2435
typedef int (* blosc2_codec_encoder_cb) (const uint8_t *input, int32_t input_len, uint8_t *output, int32_t output_len,
2436
            uint8_t meta, blosc2_cparams *cparams, const void* chunk);
2437
typedef int (* blosc2_codec_decoder_cb) (const uint8_t *input, int32_t input_len, uint8_t *output, int32_t output_len,
2438
            uint8_t meta, blosc2_dparams *dparams, const void* chunk);
2439
2440
typedef struct {
2441
  uint8_t compcode;
2442
  //!< The codec identifier.
2443
  char *compname;
2444
  //!< The codec name.
2445
  uint8_t complib;
2446
  //!< The codec library format.
2447
  uint8_t version;
2448
  //!< The codec version.
2449
  blosc2_codec_encoder_cb encoder;
2450
  //!< The codec encoder that is used during compression.
2451
  blosc2_codec_decoder_cb decoder;
2452
  //!< The codec decoder that is used during decompression.
2453
} blosc2_codec;
2454
2455
/**
2456
 * @brief Register locally a user-defined codec in Blosc.
2457
 *
2458
 * @param codec The codec to register.
2459
 *
2460
 * @return 0 if succeeds. Else a negative code is returned.
2461
 */
2462
BLOSC_EXPORT int blosc2_register_codec(blosc2_codec *codec);
2463
2464
2465
/*********************************************************************
2466
  Structures and functions related with filters plugins.
2467
*********************************************************************/
2468
2469
typedef int (* blosc2_filter_forward_cb)  (const uint8_t *, uint8_t *, int32_t, uint8_t, blosc2_cparams *,
2470
                                           uint8_t);
2471
typedef int (* blosc2_filter_backward_cb) (const uint8_t *, uint8_t *, int32_t, uint8_t, blosc2_dparams *,
2472
                                           uint8_t);
2473
2474
/**
2475
 * @brief The parameters for a user-defined filter.
2476
 */
2477
typedef struct {
2478
  uint8_t id;
2479
  //!< The filter identifier.
2480
  char * name;
2481
  //!< The filter name.
2482
  uint8_t version;
2483
  //!< The filter version.
2484
  blosc2_filter_forward_cb forward;
2485
  //!< The filter function that is used during compression.
2486
  blosc2_filter_backward_cb backward;
2487
  //!< The filter function that is used during decompression.
2488
} blosc2_filter;
2489
2490
/**
2491
 * @brief Register locally a user-defined filter in Blosc.
2492
 *
2493
 * @param filter The filter to register.
2494
 *
2495
 * @return 0 if succeeds. Else a negative code is returned.
2496
 */
2497
BLOSC_EXPORT int blosc2_register_filter(blosc2_filter *filter);
2498
2499
/*********************************************************************
2500
  Directory utilities.
2501
*********************************************************************/
2502
2503
/*
2504
 * @brief Remove a directory and its files.
2505
 *
2506
 * @param path The directory to remove.
2507
 *
2508
 * @return 0 if succeeds. Else a negative code is returned.
2509
 */
2510
BLOSC_EXPORT int blosc2_remove_dir(const char *path);
2511
2512
/*
2513
 * @brief Remove a file or a directory given by path.
2514
 *
2515
 * @param path The file or directory to remove.
2516
 *
2517
 * @return 0 if succeeds. Else a negative code is returned.
2518
 */
2519
BLOSC_EXPORT int blosc2_remove_urlpath(const char *path);
2520
2521
/*
2522
 * @brief Rename a file or a directory given by old_urlpath to new_path.
2523
 *
2524
 * @param old_urlpath The original path to the directory or file.
2525
 * @param new_path The new path to the directory or file.
2526
 *
2527
 * @return 0 if succeeds. Else a negative code is returned.
2528
 */
2529
BLOSC_EXPORT int blosc2_rename_urlpath(char* old_urlpath, char* new_path);
2530
2531
2532
/*********************************************************************
2533
  Index utilities.
2534
*********************************************************************/
2535
2536
/*
2537
 * @brief Convert a sequential index into a multidimensional index
2538
 */
2539
BLOSC_EXPORT void blosc2_unidim_to_multidim(uint8_t ndim, int64_t *shape, int64_t i, int64_t *index);
2540
2541
/*
2542
 * @brief Convert a multidimensional index into a sequential index
2543
 */
2544
BLOSC_EXPORT void blosc2_multidim_to_unidim(const int64_t *index, int8_t ndim, const int64_t *strides, int64_t *i);
2545
2546
/*
2547
 * @brief Get the unidimensional chunk indexes needed to get a slice of a schunk or a b2nd array
2548
 *
2549
 * @param schunk The super-chunk (of b2nd array or not).
2550
 * @param start Index (0-based if it is a schunk) where the slice begins.
2551
 * @param stop The first index (0-based if it is a schunk) that is not in the selected slice.
2552
 * @param chunks_idx The pointer to the buffer where the indexes will be written. It is the user responsibility
2553
 * to free the buffer.
2554
 *
2555
 * @return The number of chunks needed to get the slice. If some problem is
2556
 * detected, a negative code is returned instead.
2557
 */
2558
BLOSC_EXPORT int blosc2_get_slice_nchunks(blosc2_schunk* schunk, int64_t *start, int64_t *stop, int64_t **chunks_idx);
2559
2560
2561
/*********************************************************************
2562
  Private functions, these are here for convenience,
2563
  and are not meant to be included in public docs
2564
*********************************************************************/
2565
2566
// Private function needed in b2nd.h for deserializing meta
2567
0
static inline void swap_store(void *dest, const void *pa, int size) {
2568
0
  uint8_t *pa_ = (uint8_t *) pa;
2569
0
  uint8_t *pa2_ = (uint8_t*)malloc((size_t) size);
2570
0
  int i = 1; /* for big/little endian detection */
2571
0
  char *p = (char *) &i;
2572
2573
0
  if (p[0] == 1) {
2574
    /* little endian */
2575
0
    switch (size) {
2576
0
      case 8:
2577
0
        pa2_[0] = pa_[7];
2578
0
        pa2_[1] = pa_[6];
2579
0
        pa2_[2] = pa_[5];
2580
0
        pa2_[3] = pa_[4];
2581
0
        pa2_[4] = pa_[3];
2582
0
        pa2_[5] = pa_[2];
2583
0
        pa2_[6] = pa_[1];
2584
0
        pa2_[7] = pa_[0];
2585
0
        break;
2586
0
      case 4:
2587
0
        pa2_[0] = pa_[3];
2588
0
        pa2_[1] = pa_[2];
2589
0
        pa2_[2] = pa_[1];
2590
0
        pa2_[3] = pa_[0];
2591
0
        break;
2592
0
      case 2:
2593
0
        pa2_[0] = pa_[1];
2594
0
        pa2_[1] = pa_[0];
2595
0
        break;
2596
0
      case 1:
2597
0
        pa2_[0] = pa_[0];
2598
0
        break;
2599
0
      default:
2600
0
        fprintf(stderr, "Unhandled nitems: %d\n", size);
2601
0
    }
2602
0
  }
2603
0
  memcpy(dest, pa2_, size);
2604
0
  free(pa2_);
2605
0
}
Unexecuted instantiation: fuzz_decompress_chunk.c:swap_store
Unexecuted instantiation: blosc2.c:swap_store
Unexecuted instantiation: blosclz.c:swap_store
Unexecuted instantiation: schunk.c:swap_store
Unexecuted instantiation: frame.c:swap_store
Unexecuted instantiation: stune.c:swap_store
Unexecuted instantiation: trunc-prec.c:swap_store
Unexecuted instantiation: timestamp.c:swap_store
Unexecuted instantiation: sframe.c:swap_store
Unexecuted instantiation: blosc2-stdio.c:swap_store
Unexecuted instantiation: b2nd.c:swap_store
Unexecuted instantiation: b2nd_utils.c:swap_store
Unexecuted instantiation: shuffle.c:swap_store
Unexecuted instantiation: blosc2-zfp.c:swap_store
Unexecuted instantiation: codecs-registry.c:swap_store
Unexecuted instantiation: tuners-registry.c:swap_store
Unexecuted instantiation: filters-registry.c:swap_store
Unexecuted instantiation: ndlz.c:swap_store
Unexecuted instantiation: ndlz4x4.c:swap_store
Unexecuted instantiation: ndlz8x8.c:swap_store
Unexecuted instantiation: ndcell.c:swap_store
Unexecuted instantiation: ndmean.c:swap_store
Unexecuted instantiation: bytedelta.c:swap_store
Unexecuted instantiation: int_trunc.c:swap_store
2606
2607
#ifdef __cplusplus
2608
}
2609
#endif
2610
2611
#endif /* BLOSC_BLOSC2_H */