Coverage Report

Created: 2026-04-12 06:05

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