Coverage Report

Created: 2026-11-19 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/blosc/blosc-private.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
#ifndef BLOSC_BLOSC_PRIVATE_H
13
#define BLOSC_BLOSC_PRIVATE_H
14
15
#include "blosc2/blosc2-common.h"
16
#include "blosc2.h"
17
18
#include <stdbool.h>
19
#include <stdlib.h>
20
#include <stdint.h>
21
#include <ctype.h>
22
23
/*********************************************************************
24
25
  Utility functions meant to be used internally.
26
27
*********************************************************************/
28
29
int blosc2_decompress_block_ctx(blosc2_context* context, const void* src,
30
                                int32_t srcsize, int32_t nblock, void* dest,
31
                                int32_t destsize);
32
int blosc2_run_parallel(int16_t nthreads, void (*dojob)(void *),
33
                        size_t jobdata_elsize, void *jobdata);
34
35
377k
#define to_little(dest, src, itemsize)    endian_handler(true, dest, src, itemsize)
36
#define from_little(dest, src, itemsize)  endian_handler(true, dest, src, itemsize)
37
20.9M
#define to_big(dest, src, itemsize)       endian_handler(false, dest, src, itemsize)
38
36.0M
#define from_big(dest, src, itemsize)     endian_handler(false, dest, src, itemsize)
39
40
41
// Return true if platform is little endian; else false
42
134M
static bool is_little_endian(void) {
43
134M
  static const int i = 1;
44
134M
  char* p = (char*)&i;
45
46
134M
  if (p[0] == 1) {
47
134M
    return true;
48
134M
  }
49
0
  else {
50
0
    return false;
51
0
  }
52
134M
}
blosc2.c:is_little_endian
Line
Count
Source
42
76.7M
static bool is_little_endian(void) {
43
76.7M
  static const int i = 1;
44
76.7M
  char* p = (char*)&i;
45
46
76.7M
  if (p[0] == 1) {
47
76.7M
    return true;
48
76.7M
  }
49
0
  else {
50
    return false;
51
0
  }
52
76.7M
}
schunk.c:is_little_endian
Line
Count
Source
42
34.9k
static bool is_little_endian(void) {
43
34.9k
  static const int i = 1;
44
34.9k
  char* p = (char*)&i;
45
46
34.9k
  if (p[0] == 1) {
47
34.9k
    return true;
48
34.9k
  }
49
0
  else {
50
    return false;
51
0
  }
52
34.9k
}
frame.c:is_little_endian
Line
Count
Source
42
57.2M
static bool is_little_endian(void) {
43
57.2M
  static const int i = 1;
44
57.2M
  char* p = (char*)&i;
45
46
57.2M
  if (p[0] == 1) {
47
57.2M
    return true;
48
57.2M
  }
49
0
  else {
50
    return false;
51
0
  }
52
57.2M
}
Unexecuted instantiation: prefetch.c:is_little_endian
Unexecuted instantiation: b2nd.c:is_little_endian
Unexecuted instantiation: codecs-registry.c:is_little_endian
Unexecuted instantiation: tuners-registry.c:is_little_endian
Unexecuted instantiation: filters-registry.c:is_little_endian
53
54
55
static inline void endian_handler(bool little, void *dest, const void *pa, int size)
56
57.2M
{
57
57.2M
  bool little_endian = is_little_endian();
58
57.2M
  if (little_endian == little) {
59
377k
    memcpy(dest, pa, size);
60
377k
  }
61
56.9M
  else {
62
56.9M
    uint8_t* pa_ = (uint8_t*)pa;
63
56.9M
    uint8_t pa2_[8];
64
56.9M
    switch (size) {
65
19.9M
      case 8:
66
19.9M
        pa2_[0] = pa_[7];
67
19.9M
        pa2_[1] = pa_[6];
68
19.9M
        pa2_[2] = pa_[5];
69
19.9M
        pa2_[3] = pa_[4];
70
19.9M
        pa2_[4] = pa_[3];
71
19.9M
        pa2_[5] = pa_[2];
72
19.9M
        pa2_[6] = pa_[1];
73
19.9M
        pa2_[7] = pa_[0];
74
19.9M
        break;
75
28.1M
      case 4:
76
28.1M
        pa2_[0] = pa_[3];
77
28.1M
        pa2_[1] = pa_[2];
78
28.1M
        pa2_[2] = pa_[1];
79
28.1M
        pa2_[3] = pa_[0];
80
28.1M
        break;
81
8.84M
      case 2:
82
8.84M
        pa2_[0] = pa_[1];
83
8.84M
        pa2_[1] = pa_[0];
84
8.84M
        break;
85
10.1k
      case 1:
86
10.1k
        pa2_[0] = pa_[0];
87
10.1k
        break;
88
0
      default:
89
0
        BLOSC_TRACE_ERROR("Unhandled size: %d.", size);
90
56.9M
    }
91
56.9M
    memcpy(dest, pa2_, size);
92
56.9M
  }
93
57.2M
}
Unexecuted instantiation: blosc2.c:endian_handler
Unexecuted instantiation: schunk.c:endian_handler
frame.c:endian_handler
Line
Count
Source
56
57.2M
{
57
57.2M
  bool little_endian = is_little_endian();
58
57.2M
  if (little_endian == little) {
59
377k
    memcpy(dest, pa, size);
60
377k
  }
61
56.9M
  else {
62
56.9M
    uint8_t* pa_ = (uint8_t*)pa;
63
56.9M
    uint8_t pa2_[8];
64
56.9M
    switch (size) {
65
19.9M
      case 8:
66
19.9M
        pa2_[0] = pa_[7];
67
19.9M
        pa2_[1] = pa_[6];
68
19.9M
        pa2_[2] = pa_[5];
69
19.9M
        pa2_[3] = pa_[4];
70
19.9M
        pa2_[4] = pa_[3];
71
19.9M
        pa2_[5] = pa_[2];
72
19.9M
        pa2_[6] = pa_[1];
73
19.9M
        pa2_[7] = pa_[0];
74
19.9M
        break;
75
28.1M
      case 4:
76
28.1M
        pa2_[0] = pa_[3];
77
28.1M
        pa2_[1] = pa_[2];
78
28.1M
        pa2_[2] = pa_[1];
79
28.1M
        pa2_[3] = pa_[0];
80
28.1M
        break;
81
8.84M
      case 2:
82
8.84M
        pa2_[0] = pa_[1];
83
8.84M
        pa2_[1] = pa_[0];
84
8.84M
        break;
85
10.1k
      case 1:
86
10.1k
        pa2_[0] = pa_[0];
87
10.1k
        break;
88
0
      default:
89
0
        BLOSC_TRACE_ERROR("Unhandled size: %d.", size);
90
56.9M
    }
91
56.9M
    memcpy(dest, pa2_, size);
92
56.9M
  }
93
57.2M
}
Unexecuted instantiation: prefetch.c:endian_handler
Unexecuted instantiation: b2nd.c:endian_handler
Unexecuted instantiation: codecs-registry.c:endian_handler
Unexecuted instantiation: tuners-registry.c:endian_handler
Unexecuted instantiation: filters-registry.c:endian_handler
94
95
/*
96
 * Convert a chunk count to the serialized offsets payload size.
97
 *
98
 * Security rationale: frame metadata can be attacker-controlled, and the offsets payload
99
 * eventually flows into APIs that accept int32 byte lengths. This helper enforces a
100
 * single overflow-checked conversion so callers cannot accidentally truncate
101
 * nchunks * sizeof(int64_t) into a smaller signed length.
102
 */
103
9.89M
static inline bool blosc2_nchunks_to_offsets_nbytes(int64_t nchunks, int32_t *off_nbytes) {
104
9.89M
  const int64_t max_nchunks = INT32_MAX / (int64_t)sizeof(int64_t);
105
9.89M
  if (nchunks < 0 || nchunks > max_nchunks) {
106
54
    return false;
107
54
  }
108
9.89M
  if (off_nbytes != NULL) {
109
9.89M
    *off_nbytes = (int32_t)(nchunks * (int64_t)sizeof(int64_t));
110
9.89M
  }
111
9.89M
  return true;
112
9.89M
}
Unexecuted instantiation: blosc2.c:blosc2_nchunks_to_offsets_nbytes
schunk.c:blosc2_nchunks_to_offsets_nbytes
Line
Count
Source
103
3.49k
static inline bool blosc2_nchunks_to_offsets_nbytes(int64_t nchunks, int32_t *off_nbytes) {
104
3.49k
  const int64_t max_nchunks = INT32_MAX / (int64_t)sizeof(int64_t);
105
3.49k
  if (nchunks < 0 || nchunks > max_nchunks) {
106
0
    return false;
107
0
  }
108
3.49k
  if (off_nbytes != NULL) {
109
0
    *off_nbytes = (int32_t)(nchunks * (int64_t)sizeof(int64_t));
110
0
  }
111
  return true;
112
3.49k
}
frame.c:blosc2_nchunks_to_offsets_nbytes
Line
Count
Source
103
9.89M
static inline bool blosc2_nchunks_to_offsets_nbytes(int64_t nchunks, int32_t *off_nbytes) {
104
9.89M
  const int64_t max_nchunks = INT32_MAX / (int64_t)sizeof(int64_t);
105
9.89M
  if (nchunks < 0 || nchunks > max_nchunks) {
106
54
    return false;
107
54
  }
108
9.89M
  if (off_nbytes != NULL) {
109
9.89M
    *off_nbytes = (int32_t)(nchunks * (int64_t)sizeof(int64_t));
110
9.89M
  }
111
  return true;
112
9.89M
}
Unexecuted instantiation: prefetch.c:blosc2_nchunks_to_offsets_nbytes
Unexecuted instantiation: b2nd.c:blosc2_nchunks_to_offsets_nbytes
Unexecuted instantiation: codecs-registry.c:blosc2_nchunks_to_offsets_nbytes
Unexecuted instantiation: tuners-registry.c:blosc2_nchunks_to_offsets_nbytes
Unexecuted instantiation: filters-registry.c:blosc2_nchunks_to_offsets_nbytes
113
114
/* The flags2 byte lives in the extended header, at offset 0x1e, so it only
115
   exists when the chunk actually carries one.  Whether it does is recorded in
116
   the flags byte, the same way read_chunk_header() decides it -- not in the
117
   chunk size: a Blosc1-style chunk with 16 or more bytes of payload is 32 bytes
118
   or longer, yet offset 0x1e is compressed data there, and reading it as flags2
119
   can mark a schunk as using variable-length blocks out of thin air.  The size
120
   check stays as the bound that keeps a short chunk from being over-read. */
121
3.71M
static inline uint8_t get_chunk_flags2(const uint8_t *chunk, int32_t chunk_cbytes) {
122
3.71M
  uint8_t flags = chunk[BLOSC2_CHUNK_FLAGS];
123
3.71M
  bool extended_header = (flags & BLOSC_DOSHUFFLE) && (flags & BLOSC_DOBITSHUFFLE);
124
3.71M
  if (!extended_header || chunk_cbytes < BLOSC_EXTENDED_HEADER_LENGTH) {
125
7.60k
    return 0;
126
7.60k
  }
127
3.70M
  return chunk[BLOSC2_CHUNK_BLOSC2_FLAGS2];
128
3.71M
}
Unexecuted instantiation: blosc2.c:get_chunk_flags2
schunk.c:get_chunk_flags2
Line
Count
Source
121
3.70M
static inline uint8_t get_chunk_flags2(const uint8_t *chunk, int32_t chunk_cbytes) {
122
3.70M
  uint8_t flags = chunk[BLOSC2_CHUNK_FLAGS];
123
3.70M
  bool extended_header = (flags & BLOSC_DOSHUFFLE) && (flags & BLOSC_DOBITSHUFFLE);
124
3.70M
  if (!extended_header || chunk_cbytes < BLOSC_EXTENDED_HEADER_LENGTH) {
125
6.01k
    return 0;
126
6.01k
  }
127
3.69M
  return chunk[BLOSC2_CHUNK_BLOSC2_FLAGS2];
128
3.70M
}
frame.c:get_chunk_flags2
Line
Count
Source
121
14.4k
static inline uint8_t get_chunk_flags2(const uint8_t *chunk, int32_t chunk_cbytes) {
122
14.4k
  uint8_t flags = chunk[BLOSC2_CHUNK_FLAGS];
123
14.4k
  bool extended_header = (flags & BLOSC_DOSHUFFLE) && (flags & BLOSC_DOBITSHUFFLE);
124
14.4k
  if (!extended_header || chunk_cbytes < BLOSC_EXTENDED_HEADER_LENGTH) {
125
1.59k
    return 0;
126
1.59k
  }
127
12.8k
  return chunk[BLOSC2_CHUNK_BLOSC2_FLAGS2];
128
14.4k
}
Unexecuted instantiation: prefetch.c:get_chunk_flags2
Unexecuted instantiation: b2nd.c:get_chunk_flags2
Unexecuted instantiation: codecs-registry.c:get_chunk_flags2
Unexecuted instantiation: tuners-registry.c:get_chunk_flags2
Unexecuted instantiation: filters-registry.c:get_chunk_flags2
129
130
/* Whether the filter pipeline `filters` (BLOSC2_MAX_FILTERS codes) has filter `id` */
131
4.60M
static inline bool filters_contain(const uint8_t *filters, uint8_t id) {
132
32.2M
  for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) {
133
27.6M
    if (filters[i] == id) {
134
2.52k
      return true;
135
2.52k
    }
136
27.6M
  }
137
4.60M
  return false;
138
4.60M
}
blosc2.c:filters_contain
Line
Count
Source
131
3.05M
static inline bool filters_contain(const uint8_t *filters, uint8_t id) {
132
21.3M
  for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) {
133
18.3M
    if (filters[i] == id) {
134
2.47k
      return true;
135
2.47k
    }
136
18.3M
  }
137
3.04M
  return false;
138
3.05M
}
Unexecuted instantiation: schunk.c:filters_contain
Unexecuted instantiation: frame.c:filters_contain
Unexecuted instantiation: prefetch.c:filters_contain
b2nd.c:filters_contain
Line
Count
Source
131
1.55M
static inline bool filters_contain(const uint8_t *filters, uint8_t id) {
132
10.8M
  for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) {
133
9.32M
    if (filters[i] == id) {
134
48
      return true;
135
48
    }
136
9.32M
  }
137
1.55M
  return false;
138
1.55M
}
Unexecuted instantiation: codecs-registry.c:filters_contain
Unexecuted instantiation: tuners-registry.c:filters_contain
Unexecuted instantiation: filters-registry.c:filters_contain
139
140
/* The special value (BLOSC2_SPECIAL_ZERO, _NAN or _UNINIT) that a frame codes
141
   `chunk` (`cbytes` long) with in its offset instead of storing it, or 0 if it
142
   stores the chunk.  A chunk shorter than an extended header has no special value. */
143
2.11M
static inline int chunk_special_in_offset(const uint8_t *chunk, int32_t cbytes) {
144
2.11M
  if (cbytes < BLOSC_EXTENDED_HEADER_LENGTH) {
145
1.59k
    return 0;
146
1.59k
  }
147
2.11M
  int special_value = (chunk[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK;
148
2.11M
  switch (special_value) {
149
753k
    case BLOSC2_SPECIAL_ZERO:
150
754k
    case BLOSC2_SPECIAL_NAN:
151
754k
    case BLOSC2_SPECIAL_UNINIT:
152
754k
      return special_value;
153
1.35M
    default:
154
1.35M
      return 0;
155
2.11M
  }
156
2.11M
}
Unexecuted instantiation: blosc2.c:chunk_special_in_offset
schunk.c:chunk_special_in_offset
Line
Count
Source
143
1.05M
static inline int chunk_special_in_offset(const uint8_t *chunk, int32_t cbytes) {
144
1.05M
  if (cbytes < BLOSC_EXTENDED_HEADER_LENGTH) {
145
306
    return 0;
146
306
  }
147
1.05M
  int special_value = (chunk[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK;
148
1.05M
  switch (special_value) {
149
376k
    case BLOSC2_SPECIAL_ZERO:
150
376k
    case BLOSC2_SPECIAL_NAN:
151
377k
    case BLOSC2_SPECIAL_UNINIT:
152
377k
      return special_value;
153
673k
    default:
154
673k
      return 0;
155
1.05M
  }
156
1.05M
}
frame.c:chunk_special_in_offset
Line
Count
Source
143
1.06M
static inline int chunk_special_in_offset(const uint8_t *chunk, int32_t cbytes) {
144
1.06M
  if (cbytes < BLOSC_EXTENDED_HEADER_LENGTH) {
145
1.28k
    return 0;
146
1.28k
  }
147
1.06M
  int special_value = (chunk[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK;
148
1.06M
  switch (special_value) {
149
376k
    case BLOSC2_SPECIAL_ZERO:
150
377k
    case BLOSC2_SPECIAL_NAN:
151
377k
    case BLOSC2_SPECIAL_UNINIT:
152
377k
      return special_value;
153
685k
    default:
154
685k
      return 0;
155
1.06M
  }
156
1.06M
}
Unexecuted instantiation: prefetch.c:chunk_special_in_offset
Unexecuted instantiation: b2nd.c:chunk_special_in_offset
Unexecuted instantiation: codecs-registry.c:chunk_special_in_offset
Unexecuted instantiation: tuners-registry.c:chunk_special_in_offset
Unexecuted instantiation: filters-registry.c:chunk_special_in_offset
157
158
/* The chunksize of a super-chunk (-1 before its first chunk, 0 once its chunks
159
   vary in size) after adding a chunk of `nbytes` bytes at the end of (or, if not
160
   `at_end`, among) its `nchunks` chunks, the last of which has `last_nbytes`: the
161
   chunks stop having one size when the last one is shorter, or the new one is
162
   longer, or is not of that size and goes in between.  `last_nbytes` is only read
163
   when `*chunksize` is not 0 and `nchunks` > 0.  Returns false, and leaves
164
   *chunksize, for a first chunk longer than a chunksize set in advance. */
165
static inline bool chunksize_after_adding(int32_t *chunksize, int64_t nchunks, int32_t last_nbytes,
166
1.09M
                                         int32_t nbytes, bool at_end) {
167
1.09M
  if (*chunksize == 0) {
168
26.5k
    return true;
169
26.5k
  }
170
1.06M
  int32_t size = (*chunksize == -1) ? nbytes : *chunksize;
171
1.06M
  if (nchunks > 0 && (last_nbytes < size || nbytes > size || (!at_end && nbytes != size))) {
172
8.49k
    *chunksize = 0;
173
8.49k
    return true;
174
8.49k
  }
175
1.05M
  if (size > 0 && nbytes > size) {
176
803
    return false;
177
803
  }
178
1.05M
  *chunksize = size;
179
1.05M
  return true;
180
1.05M
}
Unexecuted instantiation: blosc2.c:chunksize_after_adding
schunk.c:chunksize_after_adding
Line
Count
Source
166
1.07M
                                         int32_t nbytes, bool at_end) {
167
1.07M
  if (*chunksize == 0) {
168
20.9k
    return true;
169
20.9k
  }
170
1.05M
  int32_t size = (*chunksize == -1) ? nbytes : *chunksize;
171
1.05M
  if (nchunks > 0 && (last_nbytes < size || nbytes > size || (!at_end && nbytes != size))) {
172
8.35k
    *chunksize = 0;
173
8.35k
    return true;
174
8.35k
  }
175
1.05M
  if (size > 0 && nbytes > size) {
176
803
    return false;
177
803
  }
178
1.04M
  *chunksize = size;
179
  return true;
180
1.05M
}
frame.c:chunksize_after_adding
Line
Count
Source
166
14.4k
                                         int32_t nbytes, bool at_end) {
167
14.4k
  if (*chunksize == 0) {
168
5.58k
    return true;
169
5.58k
  }
170
8.84k
  int32_t size = (*chunksize == -1) ? nbytes : *chunksize;
171
8.84k
  if (nchunks > 0 && (last_nbytes < size || nbytes > size || (!at_end && nbytes != size))) {
172
144
    *chunksize = 0;
173
144
    return true;
174
144
  }
175
8.70k
  if (size > 0 && nbytes > size) {
176
0
    return false;
177
0
  }
178
8.70k
  *chunksize = size;
179
  return true;
180
8.70k
}
Unexecuted instantiation: prefetch.c:chunksize_after_adding
Unexecuted instantiation: b2nd.c:chunksize_after_adding
Unexecuted instantiation: codecs-registry.c:chunksize_after_adding
Unexecuted instantiation: tuners-registry.c:chunksize_after_adding
Unexecuted instantiation: filters-registry.c:chunksize_after_adding
181
182
/* Copy the variable-length metalayers of `schunk` into `new_schunk`, compressed
183
   with `cparams` (NULL for the defaults of blosc2_vlmeta_add()). */
184
int schunk_copy_vlmetalayers(blosc2_schunk *schunk, blosc2_schunk *new_schunk, blosc2_cparams *cparams);
185
186
/* A copy of `schunk` in `storage` with the chunks as they are stored, for serializing:
187
   unlike blosc2_schunk_copy(), it never applies a postfilter of `schunk`.  This holds
188
   because `storage->cparams` must be NULL (the cparams of `schunk` are used), so no
189
   chunk needs recompressing; with cparams, it returns NULL. */
190
blosc2_schunk* schunk_copy_as_stored(blosc2_schunk *schunk, blosc2_storage *storage);
191
192
/* blosc2_schunk_update_chunk(schunk, nchunk, chunk, true) for a chunk in private memory of the
193
   caller, which keeps it: unlike a chunk that may point anywhere, a frame writes it out without
194
   copying it first.  It must not point into the storage of any frame (in memory or mapped). */
195
int64_t schunk_update_chunk_lent(blosc2_schunk *schunk, int64_t nchunk, uint8_t *chunk);
196
197
/* A chunk-sized temporary for `schunk` (a buffer to compress a chunk into, or the scratch of a
198
   slice read or write), released with blosc2_tmp_free(ptr, *capacity).  Frames do not keep the
199
   buffers handed to them, so their temporaries cycle through the per-thread cache.  Plain
200
   in-memory super-chunks keep each new chunk, and caching their scratch changes how glibc places
201
   those chunks (its mmap threshold follows the buffers freed) for the worse; their buffers are
202
   plain malloc ones (of no capacity, which blosc2_tmp_free() frees). */
203
uint8_t *schunk_scratch_malloc(const blosc2_schunk *schunk, int32_t nbytes, size_t *capacity);
204
205
/* Decompress chunk `nchunk` of `schunk`, or `chunk` (`cbytes` long) if it is not NULL,
206
   with the decompression context of `schunk`, decoding only the blocks that `maskout`
207
   (`nblocks` items) leaves in and, if `postfilter` is not NULL, running it (with
208
   `user_data`) instead of the context's own postfilter.  The context gets its own
209
   postfilter back and loses the mask in any case.  Returns what the decompression
210
   returns. */
211
int schunk_decompress_masked(blosc2_schunk *schunk, int64_t nchunk, const uint8_t *chunk, int32_t cbytes,
212
                             const bool *maskout, int32_t nblocks, blosc2_postfilter_fn postfilter,
213
                             void *user_data, void *dest, int32_t destsize);
214
215
/* Copy 4 bytes from @p *pa to int32_t, changing endianness if necessary. */
216
12.0M
static inline int32_t sw32_(const void* pa) {
217
12.0M
  int32_t idest;
218
219
12.0M
  bool little_endian = is_little_endian();
220
12.0M
  if (little_endian) {
221
12.0M
    memcpy(&idest, pa, sizeof(idest));
222
12.0M
  }
223
0
  else {
224
0
#if defined (__GNUC__)
225
0
    return __builtin_bswap32(*(unsigned int *)pa);
226
#elif defined (_MSC_VER) /* Visual Studio */
227
    return _byteswap_ulong(*(unsigned int *)pa);
228
#else
229
    const uint8_t *pa_ = (const uint8_t *)pa;
230
    uint8_t *dest = (uint8_t *)&idest;
231
    dest[0] = pa_[3];
232
    dest[1] = pa_[2];
233
    dest[2] = pa_[1];
234
    dest[3] = pa_[0];
235
#endif
236
0
  }
237
12.0M
  return idest;
238
12.0M
}
blosc2.c:sw32_
Line
Count
Source
216
12.0M
static inline int32_t sw32_(const void* pa) {
217
12.0M
  int32_t idest;
218
219
12.0M
  bool little_endian = is_little_endian();
220
12.0M
  if (little_endian) {
221
12.0M
    memcpy(&idest, pa, sizeof(idest));
222
12.0M
  }
223
0
  else {
224
0
#if defined (__GNUC__)
225
0
    return __builtin_bswap32(*(unsigned int *)pa);
226
#elif defined (_MSC_VER) /* Visual Studio */
227
    return _byteswap_ulong(*(unsigned int *)pa);
228
#else
229
    const uint8_t *pa_ = (const uint8_t *)pa;
230
    uint8_t *dest = (uint8_t *)&idest;
231
    dest[0] = pa_[3];
232
    dest[1] = pa_[2];
233
    dest[2] = pa_[1];
234
    dest[3] = pa_[0];
235
#endif
236
0
  }
237
12.0M
  return idest;
238
12.0M
}
schunk.c:sw32_
Line
Count
Source
216
34.9k
static inline int32_t sw32_(const void* pa) {
217
34.9k
  int32_t idest;
218
219
34.9k
  bool little_endian = is_little_endian();
220
34.9k
  if (little_endian) {
221
34.9k
    memcpy(&idest, pa, sizeof(idest));
222
34.9k
  }
223
0
  else {
224
0
#if defined (__GNUC__)
225
0
    return __builtin_bswap32(*(unsigned int *)pa);
226
#elif defined (_MSC_VER) /* Visual Studio */
227
    return _byteswap_ulong(*(unsigned int *)pa);
228
#else
229
    const uint8_t *pa_ = (const uint8_t *)pa;
230
    uint8_t *dest = (uint8_t *)&idest;
231
    dest[0] = pa_[3];
232
    dest[1] = pa_[2];
233
    dest[2] = pa_[1];
234
    dest[3] = pa_[0];
235
#endif
236
0
  }
237
34.9k
  return idest;
238
34.9k
}
Unexecuted instantiation: frame.c:sw32_
Unexecuted instantiation: prefetch.c:sw32_
Unexecuted instantiation: b2nd.c:sw32_
Unexecuted instantiation: codecs-registry.c:sw32_
Unexecuted instantiation: tuners-registry.c:sw32_
Unexecuted instantiation: filters-registry.c:sw32_
239
240
/* Copy 4 bytes from int32_t to @p *dest, changing endianness if necessary. */
241
33.3M
static inline void _sw32(void* dest, int32_t a) {
242
33.3M
  uint8_t* dest_ = (uint8_t*)dest;
243
33.3M
  uint8_t* pa = (uint8_t*)&a;
244
245
33.3M
  bool little_endian = is_little_endian();
246
33.3M
  if (little_endian) {
247
33.3M
    memcpy(dest_, &a, sizeof(a));;
248
33.3M
  }
249
0
  else {
250
0
#if defined (__GNUC__)
251
0
    *(int32_t *)dest_ = __builtin_bswap32(*(unsigned int *)pa);
252
#elif defined (_MSC_VER) /* Visual Studio */
253
    *(int32_t *)dest_ = _byteswap_ulong(*(unsigned int *)pa);
254
#else
255
    dest_[0] = pa[3];
256
    dest_[1] = pa[2];
257
    dest_[2] = pa[1];
258
    dest_[3] = pa[0];
259
#endif
260
0
  }
261
33.3M
}
blosc2.c:_sw32
Line
Count
Source
241
33.3M
static inline void _sw32(void* dest, int32_t a) {
242
33.3M
  uint8_t* dest_ = (uint8_t*)dest;
243
33.3M
  uint8_t* pa = (uint8_t*)&a;
244
245
33.3M
  bool little_endian = is_little_endian();
246
33.3M
  if (little_endian) {
247
33.3M
    memcpy(dest_, &a, sizeof(a));;
248
33.3M
  }
249
0
  else {
250
0
#if defined (__GNUC__)
251
0
    *(int32_t *)dest_ = __builtin_bswap32(*(unsigned int *)pa);
252
#elif defined (_MSC_VER) /* Visual Studio */
253
    *(int32_t *)dest_ = _byteswap_ulong(*(unsigned int *)pa);
254
#else
255
    dest_[0] = pa[3];
256
    dest_[1] = pa[2];
257
    dest_[2] = pa[1];
258
    dest_[3] = pa[0];
259
#endif
260
0
  }
261
33.3M
}
Unexecuted instantiation: schunk.c:_sw32
Unexecuted instantiation: frame.c:_sw32
Unexecuted instantiation: prefetch.c:_sw32
Unexecuted instantiation: b2nd.c:_sw32
Unexecuted instantiation: codecs-registry.c:_sw32
Unexecuted instantiation: tuners-registry.c:_sw32
Unexecuted instantiation: filters-registry.c:_sw32
262
263
/* Reverse swap bits in a 32-bit integer */
264
0
static inline int32_t bswap32_(int32_t a) {
265
0
#if defined (__GNUC__)
266
0
  return __builtin_bswap32(a);
267
268
#elif defined (_MSC_VER) /* Visual Studio */
269
  return _byteswap_ulong(a);
270
#else
271
  a = ((a & 0x000000FF) << 24) |
272
      ((a & 0x0000FF00) <<  8) |
273
      ((a & 0x00FF0000) >>  8) |
274
      ((a & 0xFF000000) >> 24);
275
  return a;
276
#endif
277
0
}
Unexecuted instantiation: blosc2.c:bswap32_
Unexecuted instantiation: schunk.c:bswap32_
Unexecuted instantiation: frame.c:bswap32_
Unexecuted instantiation: prefetch.c:bswap32_
Unexecuted instantiation: b2nd.c:bswap32_
Unexecuted instantiation: codecs-registry.c:bswap32_
Unexecuted instantiation: tuners-registry.c:bswap32_
Unexecuted instantiation: filters-registry.c:bswap32_
278
279
/**
280
 * @brief Register a filter in Blosc.
281
 *
282
 * @param filter The filter to register.
283
 *
284
 * @return 0 if succeeds. Else a negative code is returned.
285
 */
286
int register_filter_private(blosc2_filter *filter);
287
288
/**
289
 * @brief Register a codec in Blosc.
290
 *
291
 * @param codec The codec to register.
292
 *
293
 * @return 0 if succeeds. Else a negative code is returned.
294
 */
295
int register_codec_private(blosc2_codec *codec);
296
297
298
/**
299
 * @brief Register a tune in Blosc.
300
 *
301
 * @param tune The tune to register.
302
 *
303
 * @return 0 if succeeds. Else a negative code is returned.
304
 */
305
int register_tuner_private(blosc2_tuner *tuner);
306
307
int fill_tuner(blosc2_tuner *tuner);
308
309
extern blosc2_tuner g_tuners[256];
310
extern int g_ntuners;
311
312
/* Large short-lived buffers (chunk scratch, compressed chunks) cycle through a
313
   small per-thread cache instead of malloc/free on every call: glibc trims
314
   freed memory at the top of the heap back to the kernel, so a buffer of
315
   several MB freed per call is page-faulted in again by the next one.
316
   blosc2_tmp_malloc() returns memory for at least `nbytes` (malloc'ed, so it
317
   may be released with plain free()) and its real size in *capacity;
318
   blosc2_tmp_free() keeps it for reuse or frees it.  At most
319
   BLOSC2_TMP_CACHE_MAX bytes stay cached per thread.  The cache also holds the
320
   thread's thread_context for serial work (see serial_context_get()).  Both
321
   are released at thread exit, or by blosc2_free_resources() and
322
   blosc2_destroy() for the calling thread. */
323
void *blosc2_tmp_malloc(size_t nbytes, size_t *capacity);
324
void blosc2_tmp_free(void *ptr, size_t capacity);
325
void blosc2_tmp_cache_clear(void);
326
/* Bytes currently held by the calling thread's cache, without its serial
327
   thread_context (for tests). */
328
size_t blosc2_tmp_cache_nbytes(void);
329
330
/* Check whether the library is currently fully initialized (not tearing down). */
331
bool blosc2_is_initialized(void);
332
333
334
#if defined(_WIN32)
335
#include <windows.h>
336
#ifndef PATH_MAX
337
#define PATH_MAX MAX_PATH
338
#endif
339
#define RTLD_LAZY   0x000
340
#define popen _popen
341
#define pclose _pclose
342
343
static struct {
344
    long lasterror;
345
    const char *err_rutin;
346
}
347
var = {
348
    0,
349
    NULL
350
};
351
352
static inline void *dlopen (const char *filename, int flags) {
353
  BLOSC_UNUSED_PARAM(flags);
354
  HINSTANCE hInst;
355
  hInst = LoadLibrary(filename);
356
  if (hInst==NULL) {
357
    var.lasterror = GetLastError();
358
    var.err_rutin = "dlopen";
359
  }
360
361
  return hInst;
362
}
363
364
static inline void *dlsym(void *handle, const char *name) {
365
  FARPROC fp;
366
  fp = GetProcAddress((HINSTANCE)handle, name);
367
  if (!fp) {
368
    var.lasterror = GetLastError ();
369
    var.err_rutin = "dlsym";
370
  }
371
  return (void *)(intptr_t)fp;
372
}
373
374
static inline int dlclose(void *handle) {
375
  bool ok = FreeLibrary((HINSTANCE)handle);
376
  if (!ok) {
377
    var.lasterror = GetLastError();
378
    var.err_rutin = "dlclose";
379
    return BLOSC2_ERROR_FAILURE;
380
  }
381
  return BLOSC2_ERROR_SUCCESS;
382
}
383
384
static inline const char *dlerror (void) {
385
  static char errstr [88];
386
  if (var.lasterror) {
387
      snprintf(errstr, sizeof(errstr), "%s error #%ld", var.err_rutin, var.lasterror);
388
      return errstr;
389
  } else {
390
      return NULL;
391
  }
392
}
393
#else
394
#include <dlfcn.h>
395
#endif
396
397
216
static inline bool blosc2_valid_plugin_name(const char *plugin_name) {
398
216
  if (plugin_name == NULL || plugin_name[0] == '\0') {
399
0
    return false;
400
0
  }
401
1.74k
  for (const unsigned char *p = (const unsigned char *)plugin_name; *p != '\0'; ++p) {
402
1.53k
    if (!isalnum(*p) && *p != '_') {
403
0
      return false;
404
0
    }
405
1.53k
  }
406
216
  return true;
407
216
}
blosc2.c:blosc2_valid_plugin_name
Line
Count
Source
397
216
static inline bool blosc2_valid_plugin_name(const char *plugin_name) {
398
216
  if (plugin_name == NULL || plugin_name[0] == '\0') {
399
0
    return false;
400
0
  }
401
1.74k
  for (const unsigned char *p = (const unsigned char *)plugin_name; *p != '\0'; ++p) {
402
1.53k
    if (!isalnum(*p) && *p != '_') {
403
0
      return false;
404
0
    }
405
1.53k
  }
406
216
  return true;
407
216
}
Unexecuted instantiation: schunk.c:blosc2_valid_plugin_name
Unexecuted instantiation: frame.c:blosc2_valid_plugin_name
Unexecuted instantiation: prefetch.c:blosc2_valid_plugin_name
Unexecuted instantiation: b2nd.c:blosc2_valid_plugin_name
Unexecuted instantiation: codecs-registry.c:blosc2_valid_plugin_name
Unexecuted instantiation: tuners-registry.c:blosc2_valid_plugin_name
Unexecuted instantiation: filters-registry.c:blosc2_valid_plugin_name
408
409
410
144
static inline int get_libpath(char *plugin_name, char *libpath, char *python_version) {
411
144
  BLOSC_TRACE_INFO("Trying to get plugin path with python%s\n", python_version);
412
144
  char python_cmd[PATH_MAX] = {0};
413
144
  if (!blosc2_valid_plugin_name(plugin_name)) {
414
0
    BLOSC_TRACE_ERROR("Invalid plugin name");
415
0
    return BLOSC2_ERROR_INVALID_PARAM;
416
0
  }
417
144
  int written = snprintf(python_cmd, sizeof(python_cmd),
418
144
                         "python%s -c \"import blosc2_%s; blosc2_%s.print_libpath()\"",
419
144
                         python_version, plugin_name, plugin_name);
420
144
  if (written < 0 || (size_t)written >= sizeof(python_cmd)) {
421
0
    BLOSC_TRACE_ERROR("Python command is too long");
422
0
    return BLOSC2_ERROR_FAILURE;
423
0
  }
424
144
  FILE *fp = popen(python_cmd, "r");
425
144
  if (fp == NULL) {
426
0
    BLOSC_TRACE_ERROR("Could not run python");
427
0
    return BLOSC2_ERROR_FAILURE;
428
0
  }
429
144
  if (fgets(libpath, PATH_MAX, fp) == NULL) {
430
144
    BLOSC_TRACE_ERROR("Could not read python output");
431
144
    pclose(fp);
432
144
    return BLOSC2_ERROR_FAILURE;
433
144
  }
434
0
  pclose(fp);
435
436
0
  return BLOSC2_ERROR_SUCCESS;
437
144
}
blosc2.c:get_libpath
Line
Count
Source
410
144
static inline int get_libpath(char *plugin_name, char *libpath, char *python_version) {
411
144
  BLOSC_TRACE_INFO("Trying to get plugin path with python%s\n", python_version);
412
144
  char python_cmd[PATH_MAX] = {0};
413
144
  if (!blosc2_valid_plugin_name(plugin_name)) {
414
0
    BLOSC_TRACE_ERROR("Invalid plugin name");
415
0
    return BLOSC2_ERROR_INVALID_PARAM;
416
0
  }
417
144
  int written = snprintf(python_cmd, sizeof(python_cmd),
418
144
                         "python%s -c \"import blosc2_%s; blosc2_%s.print_libpath()\"",
419
144
                         python_version, plugin_name, plugin_name);
420
144
  if (written < 0 || (size_t)written >= sizeof(python_cmd)) {
421
0
    BLOSC_TRACE_ERROR("Python command is too long");
422
0
    return BLOSC2_ERROR_FAILURE;
423
0
  }
424
144
  FILE *fp = popen(python_cmd, "r");
425
144
  if (fp == NULL) {
426
0
    BLOSC_TRACE_ERROR("Could not run python");
427
0
    return BLOSC2_ERROR_FAILURE;
428
0
  }
429
144
  if (fgets(libpath, PATH_MAX, fp) == NULL) {
430
144
    BLOSC_TRACE_ERROR("Could not read python output");
431
144
    pclose(fp);
432
144
    return BLOSC2_ERROR_FAILURE;
433
144
  }
434
0
  pclose(fp);
435
436
0
  return BLOSC2_ERROR_SUCCESS;
437
144
}
Unexecuted instantiation: schunk.c:get_libpath
Unexecuted instantiation: frame.c:get_libpath
Unexecuted instantiation: prefetch.c:get_libpath
Unexecuted instantiation: b2nd.c:get_libpath
Unexecuted instantiation: codecs-registry.c:get_libpath
Unexecuted instantiation: tuners-registry.c:get_libpath
Unexecuted instantiation: filters-registry.c:get_libpath
438
439
72
static inline void* load_lib(char *plugin_name, char *libpath) {
440
72
  if (!blosc2_valid_plugin_name(plugin_name)) {
441
0
    BLOSC_TRACE_ERROR("Invalid plugin name");
442
0
    return NULL;
443
0
  }
444
    // Attempt to directly load the library by name
445
#if defined(_WIN32)
446
    // Windows dynamic library (DLL) format
447
    snprintf(libpath, PATH_MAX, "blosc2_%s.dll", plugin_name);
448
#else
449
    // Unix/Linux/Mac OS dynamic library (.so) format
450
72
    snprintf(libpath, PATH_MAX, "libblosc2_%s.so", plugin_name);
451
72
#endif
452
72
    void* loaded_lib = dlopen(libpath, RTLD_LAZY);
453
72
    if (loaded_lib != NULL) {
454
0
        BLOSC_TRACE_INFO("Successfully loaded %s directly\n", libpath);
455
0
        return loaded_lib;
456
72
    } else {
457
#if defined(_WIN32)
458
        BLOSC_TRACE_INFO("Failed to load %s directly, error: %lu\n", libpath, GetLastError());
459
#else
460
72
        BLOSC_TRACE_INFO("Failed to load %s directly, error: %s\n", libpath, dlerror());
461
72
#endif
462
72
    }
463
    // If direct loading fails, fallback to using Python to find the library path
464
72
    if (get_libpath(plugin_name, libpath, "") < 0 && get_libpath(plugin_name, libpath, "3") < 0) {
465
72
        BLOSC_TRACE_ERROR("Problems when running python or python3 for getting plugin path");
466
72
        return NULL;
467
72
    }
468
469
0
    if (strlen(libpath) == 0) {
470
0
        BLOSC_TRACE_ERROR("Could not find plugin libpath");
471
0
        return NULL;
472
0
    }
473
474
    // Try to load the library again with the path from Python
475
0
    loaded_lib = dlopen(libpath, RTLD_LAZY);
476
0
    if (loaded_lib == NULL) {
477
0
        BLOSC_TRACE_ERROR("Attempt to load plugin in path '%s' failed with error: %s", libpath, dlerror());
478
0
    } else {
479
0
        BLOSC_TRACE_INFO("Successfully loaded library with Python path: %s\n", libpath);
480
0
    }
481
482
0
    return loaded_lib;
483
0
}
blosc2.c:load_lib
Line
Count
Source
439
72
static inline void* load_lib(char *plugin_name, char *libpath) {
440
72
  if (!blosc2_valid_plugin_name(plugin_name)) {
441
0
    BLOSC_TRACE_ERROR("Invalid plugin name");
442
0
    return NULL;
443
0
  }
444
    // Attempt to directly load the library by name
445
#if defined(_WIN32)
446
    // Windows dynamic library (DLL) format
447
    snprintf(libpath, PATH_MAX, "blosc2_%s.dll", plugin_name);
448
#else
449
    // Unix/Linux/Mac OS dynamic library (.so) format
450
72
    snprintf(libpath, PATH_MAX, "libblosc2_%s.so", plugin_name);
451
72
#endif
452
72
    void* loaded_lib = dlopen(libpath, RTLD_LAZY);
453
72
    if (loaded_lib != NULL) {
454
0
        BLOSC_TRACE_INFO("Successfully loaded %s directly\n", libpath);
455
0
        return loaded_lib;
456
72
    } else {
457
#if defined(_WIN32)
458
        BLOSC_TRACE_INFO("Failed to load %s directly, error: %lu\n", libpath, GetLastError());
459
#else
460
72
        BLOSC_TRACE_INFO("Failed to load %s directly, error: %s\n", libpath, dlerror());
461
72
#endif
462
72
    }
463
    // If direct loading fails, fallback to using Python to find the library path
464
72
    if (get_libpath(plugin_name, libpath, "") < 0 && get_libpath(plugin_name, libpath, "3") < 0) {
465
72
        BLOSC_TRACE_ERROR("Problems when running python or python3 for getting plugin path");
466
72
        return NULL;
467
72
    }
468
469
0
    if (strlen(libpath) == 0) {
470
0
        BLOSC_TRACE_ERROR("Could not find plugin libpath");
471
0
        return NULL;
472
0
    }
473
474
    // Try to load the library again with the path from Python
475
0
    loaded_lib = dlopen(libpath, RTLD_LAZY);
476
0
    if (loaded_lib == NULL) {
477
0
        BLOSC_TRACE_ERROR("Attempt to load plugin in path '%s' failed with error: %s", libpath, dlerror());
478
0
    } else {
479
0
        BLOSC_TRACE_INFO("Successfully loaded library with Python path: %s\n", libpath);
480
0
    }
481
482
0
    return loaded_lib;
483
0
}
Unexecuted instantiation: schunk.c:load_lib
Unexecuted instantiation: frame.c:load_lib
Unexecuted instantiation: prefetch.c:load_lib
Unexecuted instantiation: b2nd.c:load_lib
Unexecuted instantiation: codecs-registry.c:load_lib
Unexecuted instantiation: tuners-registry.c:load_lib
Unexecuted instantiation: filters-registry.c:load_lib
484
485
486
#endif /* BLOSC_BLOSC_PRIVATE_H */