Coverage Report

Created: 2024-07-27 06:20

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