Coverage Report

Created: 2026-02-22 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/blosc/b2nd.c
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
#include "b2nd.h"
12
#include "context.h"
13
#include "blosc2/blosc2-common.h"
14
#include "blosc2.h"
15
16
#include <inttypes.h>
17
#include <stdlib.h>
18
#include <stdint.h>
19
#include <string.h>
20
21
22
int b2nd_serialize_meta(int8_t ndim, const int64_t *shape, const int32_t *chunkshape,
23
                        const int32_t *blockshape, const char *dtype, int8_t dtype_format,
24
0
                        uint8_t **smeta) {
25
0
  if (dtype == NULL) {
26
0
    dtype = B2ND_DEFAULT_DTYPE;
27
0
  }
28
  // dtype checks
29
0
  if (dtype_format < 0) {
30
0
    BLOSC_TRACE_ERROR("dtype_format cannot be negative");
31
0
    BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
32
0
  }
33
0
  size_t dtype_len0 = strlen(dtype);
34
0
  if (dtype_len0 > INT32_MAX) {
35
0
    BLOSC_TRACE_ERROR("dtype is too large (len > %d)", INT32_MAX);
36
0
    BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
37
0
  }
38
0
  const int32_t dtype_len = (int32_t) dtype_len0;
39
  // Allocate space for b2nd metalayer
40
0
  int32_t max_smeta_len = (int32_t) (1 + 1 + 1 + (1 + ndim * (1 + sizeof(int64_t))) +
41
0
                                     (1 + ndim * (1 + sizeof(int32_t))) + (1 + ndim * (1 + sizeof(int32_t))) +
42
0
                                     1 + 1 + sizeof(int32_t) + dtype_len);
43
0
  *smeta = malloc((size_t) max_smeta_len);
44
0
  BLOSC_ERROR_NULL(*smeta, BLOSC2_ERROR_MEMORY_ALLOC);
45
0
  uint8_t *pmeta = *smeta;
46
47
  // Build an array with 7 entries (version, ndim, shape, chunkshape, blockshape, dtype_format, dtype)
48
0
  *pmeta++ = 0x90 + 7;
49
50
  // version entry
51
0
  *pmeta++ = B2ND_METALAYER_VERSION;  // positive fixnum (7-bit positive integer)
52
53
  // ndim entry
54
0
  *pmeta++ = (uint8_t) ndim;  // positive fixnum (7-bit positive integer)
55
56
  // shape entry
57
0
  *pmeta++ = (uint8_t) (0x90) + ndim;  // fix array with ndim elements
58
0
  for (uint8_t i = 0; i < ndim; i++) {
59
0
    *pmeta++ = 0xd3;  // int64
60
0
    swap_store(pmeta, shape + i, sizeof(int64_t));
61
0
    pmeta += sizeof(int64_t);
62
0
  }
63
64
  // chunkshape entry
65
0
  *pmeta++ = (uint8_t) (0x90) + ndim;  // fix array with ndim elements
66
0
  for (uint8_t i = 0; i < ndim; i++) {
67
0
    *pmeta++ = 0xd2;  // int32
68
0
    swap_store(pmeta, chunkshape + i, sizeof(int32_t));
69
0
    pmeta += sizeof(int32_t);
70
0
  }
71
72
  // blockshape entry
73
0
  *pmeta++ = (uint8_t) (0x90) + ndim;  // fix array with ndim elements
74
0
  for (uint8_t i = 0; i < ndim; i++) {
75
0
    *pmeta++ = 0xd2;  // int32
76
0
    swap_store(pmeta, blockshape + i, sizeof(int32_t));
77
0
    pmeta += sizeof(int32_t);
78
0
  }
79
80
  // dtype entry
81
0
  *pmeta++ = dtype_format;  // positive fixint (7-bit positive integer)
82
0
  *pmeta++ = (uint8_t) (0xdb);  // str with up to 2^31 elements
83
0
  swap_store(pmeta, &dtype_len, sizeof(int32_t));
84
0
  pmeta += sizeof(int32_t);
85
0
  memcpy(pmeta, dtype, dtype_len);
86
0
  pmeta += dtype_len;
87
88
0
  int32_t slen = (int32_t) (pmeta - *smeta);
89
0
  if (max_smeta_len != slen) {
90
0
    BLOSC_TRACE_ERROR("meta length is inconsistent!");
91
0
    return BLOSC2_ERROR_FAILURE;
92
0
  }
93
94
0
  return (int)slen;
95
0
}
96
97
98
int b2nd_deserialize_meta(const uint8_t *smeta, int32_t smeta_len, int8_t *ndim, int64_t *shape,
99
0
                          int32_t *chunkshape, int32_t *blockshape, char **dtype, int8_t *dtype_format) {
100
0
  const uint8_t *pmeta = smeta;
101
102
  // Check that we have an array with 7 entries (version, ndim, shape, chunkshape, blockshape, dtype_format, dtype)
103
0
  pmeta += 1;
104
105
  // version entry
106
  // int8_t version = (int8_t)pmeta[0];  // positive fixnum (7-bit positive integer) commented to avoid warning
107
0
  pmeta += 1;
108
109
  // ndim entry
110
0
  *ndim = (int8_t) pmeta[0];
111
0
  int8_t ndim_aux = *ndim;  // positive fixnum (7-bit positive integer)
112
0
  pmeta += 1;
113
114
  // shape entry
115
  // Initialize to ones, as required by b2nd
116
0
  for (int i = 0; i < ndim_aux; i++) shape[i] = 1;
117
0
  pmeta += 1;
118
0
  for (int8_t i = 0; i < ndim_aux; i++) {
119
0
    pmeta += 1;
120
0
    swap_store(shape + i, pmeta, sizeof(int64_t));
121
0
    pmeta += sizeof(int64_t);
122
0
  }
123
124
  // chunkshape entry
125
  // Initialize to ones, as required by b2nd
126
0
  for (int i = 0; i < ndim_aux; i++) chunkshape[i] = 1;
127
0
  pmeta += 1;
128
0
  for (int8_t i = 0; i < ndim_aux; i++) {
129
0
    pmeta += 1;
130
0
    swap_store(chunkshape + i, pmeta, sizeof(int32_t));
131
0
    pmeta += sizeof(int32_t);
132
0
  }
133
134
  // blockshape entry
135
  // Initialize to ones, as required by b2nd
136
0
  for (int i = 0; i < ndim_aux; i++) blockshape[i] = 1;
137
0
  pmeta += 1;
138
0
  for (int8_t i = 0; i < ndim_aux; i++) {
139
0
    pmeta += 1;
140
0
    swap_store(blockshape + i, pmeta, sizeof(int32_t));
141
0
    pmeta += sizeof(int32_t);
142
0
  }
143
144
  // dtype entry
145
0
  if (dtype_format == NULL || dtype == NULL) {
146
0
    return (int32_t)(pmeta - smeta);
147
0
  }
148
0
  if (pmeta - smeta < smeta_len) {
149
    // dtype info is here
150
0
    *dtype_format = (int8_t) *(pmeta++);
151
0
    pmeta += 1;
152
0
    int dtype_len;
153
0
    swap_store(&dtype_len, pmeta, sizeof(int32_t));
154
0
    pmeta += sizeof(int32_t);
155
0
    *dtype = (char*)malloc(dtype_len + 1);
156
0
    char* dtype_ = *dtype;
157
0
    memcpy(dtype_, (char*)pmeta, dtype_len);
158
0
    dtype_[dtype_len] = '\0';
159
0
    pmeta += dtype_len;
160
0
  }
161
0
  else {
162
    // dtype is mandatory in b2nd metalayer, but this is mainly meant as
163
    // a fall-back for deprecated caterva headers
164
0
    *dtype = NULL;
165
0
    *dtype_format = 0;
166
0
  }
167
168
0
  int32_t slen = (int32_t) (pmeta - smeta);
169
0
  return (int)slen;
170
0
}
171
172
173
174
int update_shape(b2nd_array_t *array, int8_t ndim, const int64_t *shape,
175
0
                 const int32_t *chunkshape, const int32_t *blockshape) {
176
0
  array->ndim = ndim;
177
0
  array->nitems = 1;
178
0
  array->extnitems = 1;
179
0
  array->extchunknitems = 1;
180
0
  array->chunknitems = 1;
181
0
  array->blocknitems = 1;
182
0
  for (int i = 0; i < B2ND_MAX_DIM; ++i) {
183
0
    if (i < ndim) {
184
0
      array->shape[i] = shape[i];
185
0
      array->chunkshape[i] = chunkshape[i];
186
0
      array->blockshape[i] = blockshape[i];
187
0
      if (array->chunkshape[i] != 0) {
188
0
        if (shape[i] % array->chunkshape[i] == 0) {
189
0
          array->extshape[i] = shape[i];
190
0
        } else {
191
0
          array->extshape[i] = shape[i] + chunkshape[i] - shape[i] % chunkshape[i];
192
0
        }
193
0
        if (chunkshape[i] % blockshape[i] == 0) {
194
0
          array->extchunkshape[i] = chunkshape[i];
195
0
        } else {
196
0
          array->extchunkshape[i] =
197
0
                  chunkshape[i] + blockshape[i] - chunkshape[i] % blockshape[i];
198
0
        }
199
0
      } else {
200
0
        array->extchunkshape[i] = chunkshape[i];
201
0
        array->extshape[i] = 0;
202
0
      }
203
0
    } else {
204
0
      array->blockshape[i] = 1;
205
0
      array->chunkshape[i] = 1;
206
0
      array->extshape[i] = 1;
207
0
      array->extchunkshape[i] = 1;
208
0
      array->shape[i] = 1;
209
0
    }
210
0
    array->nitems *= array->shape[i];
211
0
    array->extnitems *= array->extshape[i];
212
0
    array->extchunknitems *= array->extchunkshape[i];
213
0
    array->chunknitems *= array->chunkshape[i];
214
0
    array->blocknitems *= array->blockshape[i];
215
0
  }
216
217
  // Compute strides
218
0
  if (ndim > 0) {
219
0
    array->item_array_strides[ndim - 1] = 1;
220
0
    array->item_extchunk_strides[ndim - 1] = 1;
221
0
    array->item_chunk_strides[ndim - 1] = 1;
222
0
    array->item_block_strides[ndim - 1] = 1;
223
0
    array->block_chunk_strides[ndim - 1] = 1;
224
0
    array->chunk_array_strides[ndim - 1] = 1;
225
0
  }
226
0
  for (int i = ndim - 2; i >= 0; --i) {
227
0
    if (shape[i + 1] != 0) {
228
0
      array->item_array_strides[i] = array->item_array_strides[i + 1] * array->shape[i + 1];
229
0
      array->item_extchunk_strides[i] =
230
0
              array->item_extchunk_strides[i + 1] * array->extchunkshape[i + 1];
231
0
      array->item_chunk_strides[i] =
232
0
              array->item_chunk_strides[i + 1] * array->chunkshape[i + 1];
233
0
      array->item_block_strides[i] =
234
0
              array->item_block_strides[i + 1] * array->blockshape[i + 1];
235
0
      array->block_chunk_strides[i] = array->block_chunk_strides[i + 1] *
236
0
                                      (array->extchunkshape[i + 1] /
237
0
                                       array->blockshape[i + 1]);
238
0
      array->chunk_array_strides[i] = array->chunk_array_strides[i + 1] *
239
0
                                      (array->extshape[i + 1] * array->chunkshape[i + 1]);
240
0
    } else {
241
0
      array->item_array_strides[i] = 0;
242
0
      array->item_extchunk_strides[i] = 0;
243
0
      array->item_chunk_strides[i] = 0;
244
0
      array->item_block_strides[i] = 0;
245
0
      array->block_chunk_strides[i] = 0;
246
0
      array->chunk_array_strides[i] = 0;
247
0
    }
248
0
  }
249
0
  if (array->sc) {
250
0
    uint8_t *smeta = NULL;
251
    // Serialize the dimension info ...
252
0
    int32_t smeta_len =
253
0
            b2nd_serialize_meta(array->ndim, array->shape, array->chunkshape, array->blockshape,
254
0
                                array->dtype, array->dtype_format, &smeta);
255
0
    if (smeta_len < 0) {
256
0
      BLOSC_TRACE_ERROR("Error during serializing dims info for Blosc2 NDim");
257
0
      BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
258
0
    }
259
    // ... and update it in its metalayer
260
0
    if (blosc2_meta_exists(array->sc, "b2nd") < 0) {
261
0
      if (blosc2_meta_add(array->sc, "b2nd", smeta, smeta_len) < 0) {
262
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
263
0
      }
264
0
    } else {
265
0
      if (blosc2_meta_update(array->sc, "b2nd", smeta, smeta_len) < 0) {
266
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
267
0
      }
268
0
    }
269
0
    free(smeta);
270
0
  }
271
272
0
  return BLOSC2_ERROR_SUCCESS;
273
0
}
274
275
276
0
int array_without_schunk(b2nd_context_t *ctx, b2nd_array_t **array) {
277
  /* Create a b2nd_array_t buffer */
278
0
  (*array) = (b2nd_array_t *) malloc(sizeof(b2nd_array_t));
279
0
  BLOSC_ERROR_NULL(*array, BLOSC2_ERROR_MEMORY_ALLOC);
280
281
0
  (*array)->sc = NULL;
282
283
0
  (*array)->ndim = ctx->ndim;
284
0
  int64_t *shape = ctx->shape;
285
0
  int32_t *chunkshape = ctx->chunkshape;
286
0
  int32_t *blockshape = ctx->blockshape;
287
0
  BLOSC_ERROR(update_shape(*array, ctx->ndim, shape, chunkshape, blockshape));
288
289
0
  if (ctx->dtype != NULL) {
290
0
    (*array)->dtype = malloc(strlen(ctx->dtype) + 1);
291
0
    strcpy((*array)->dtype, ctx->dtype);
292
0
  } else {
293
0
    (*array)->dtype = NULL;
294
0
  }
295
296
0
  (*array)->dtype_format = ctx->dtype_format;
297
298
  // The partition cache (empty initially)
299
0
  (*array)->chunk_cache.data = NULL;
300
0
  (*array)->chunk_cache.nchunk = -1;  // means no valid cache yet
301
302
0
  return BLOSC2_ERROR_SUCCESS;
303
0
}
304
305
306
0
int array_new(b2nd_context_t *ctx, int special_value, b2nd_array_t **array) {
307
0
  BLOSC_ERROR(array_without_schunk(ctx, array));
308
309
0
  blosc2_schunk *sc = blosc2_schunk_new(ctx->b2_storage);
310
0
  if (sc == NULL) {
311
0
    BLOSC_TRACE_ERROR("Pointer is NULL");
312
0
    return BLOSC2_ERROR_FAILURE;
313
0
  }
314
  // Set the chunksize for the schunk, as it cannot be derived from storage
315
0
  int32_t chunksize = (int32_t) (*array)->extchunknitems * sc->typesize;
316
0
  sc->chunksize = chunksize;
317
318
  // Serialize the dimension info
319
0
  if (sc->nmetalayers >= BLOSC2_MAX_METALAYERS) {
320
0
    BLOSC_TRACE_ERROR("the number of metalayers for this schunk has been exceeded");
321
0
    return BLOSC2_ERROR_FAILURE;
322
0
  }
323
0
  uint8_t *smeta = NULL;
324
0
  int32_t smeta_len = b2nd_serialize_meta(ctx->ndim,
325
0
                                          (*array)->shape,
326
0
                                          (*array)->chunkshape,
327
0
                                          (*array)->blockshape,
328
0
                                          (*array)->dtype,
329
0
                                          (*array)->dtype_format,
330
0
                                          &smeta);
331
0
  if (smeta_len < 0) {
332
0
    BLOSC_TRACE_ERROR("error during serializing dims info for Blosc2 NDim");
333
0
    return BLOSC2_ERROR_FAILURE;
334
0
  }
335
336
  // And store it in b2nd metalayer
337
0
  if (blosc2_meta_add(sc, "b2nd", smeta, smeta_len) < 0) {
338
0
    return BLOSC2_ERROR_FAILURE;
339
0
  }
340
341
0
  free(smeta);
342
343
0
  for (int i = 0; i < ctx->nmetalayers; ++i) {
344
0
    char *name = ctx->metalayers[i].name;
345
0
    uint8_t *data = ctx->metalayers[i].content;
346
0
    int32_t size = ctx->metalayers[i].content_len;
347
0
    if (blosc2_meta_add(sc, name, data, size) < 0) {
348
0
      BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
349
0
    }
350
0
  }
351
352
0
  if ((*array)->extchunknitems * sc->typesize > BLOSC2_MAX_BUFFERSIZE){
353
0
    BLOSC_TRACE_ERROR("Chunksize exceeds maximum of %d", BLOSC2_MAX_BUFFERSIZE);
354
0
    return BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED;
355
0
  }
356
  // Fill schunk with uninit values
357
0
  if ((*array)->nitems != 0) {
358
0
    int64_t nchunks = (*array)->extnitems / (*array)->chunknitems;
359
0
    int64_t nitems = nchunks * (*array)->extchunknitems;
360
0
    BLOSC_ERROR(blosc2_schunk_fill_special(sc, nitems, special_value, chunksize));
361
0
  }
362
0
  (*array)->sc = sc;
363
364
0
  return BLOSC2_ERROR_SUCCESS;
365
0
}
366
367
368
0
int b2nd_uninit(b2nd_context_t *ctx, b2nd_array_t **array) {
369
0
  BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
370
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
371
372
0
  BLOSC_ERROR(array_new(ctx, BLOSC2_SPECIAL_UNINIT, array));
373
374
0
  return BLOSC2_ERROR_SUCCESS;
375
0
}
376
377
378
0
int b2nd_empty(b2nd_context_t *ctx, b2nd_array_t **array) {
379
0
  BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
380
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
381
382
  // Fill with zeros to avoid variable cratios
383
0
  BLOSC_ERROR(array_new(ctx, BLOSC2_SPECIAL_ZERO, array));
384
385
0
  return BLOSC2_ERROR_SUCCESS;
386
0
}
387
388
389
0
int b2nd_zeros(b2nd_context_t *ctx, b2nd_array_t **array) {
390
0
  BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
391
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
392
393
0
  BLOSC_ERROR(array_new(ctx, BLOSC2_SPECIAL_ZERO, array));
394
395
0
  return BLOSC2_ERROR_SUCCESS;
396
0
}
397
398
399
0
int b2nd_nans(b2nd_context_t *ctx, b2nd_array_t **array) {
400
0
  BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
401
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
402
403
0
  BLOSC_ERROR(array_new(ctx, BLOSC2_SPECIAL_NAN, array));
404
405
0
  const int32_t typesize = (*array)->sc->typesize;
406
0
  if (typesize != 4 && typesize != 8)
407
0
  {
408
0
    BLOSC_TRACE_ERROR("Unsupported typesize for NaN");
409
0
    return BLOSC2_ERROR_DATA;
410
0
  }
411
412
0
  return BLOSC2_ERROR_SUCCESS;
413
0
}
414
415
416
0
int b2nd_full(b2nd_context_t *ctx, b2nd_array_t **array, const void *fill_value) {
417
0
  BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
418
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
419
420
0
  BLOSC_ERROR(b2nd_empty(ctx, array));
421
422
0
  int32_t chunkbytes = (int32_t) (*array)->extchunknitems * (*array)->sc->typesize;
423
424
0
  blosc2_cparams *cparams;
425
0
  if (blosc2_schunk_get_cparams((*array)->sc, &cparams) != 0) {
426
0
    BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
427
0
  }
428
429
0
  int32_t chunksize = BLOSC_EXTENDED_HEADER_LENGTH + (*array)->sc->typesize;
430
0
  uint8_t *chunk = malloc(chunksize);
431
0
  BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
432
0
  if (blosc2_chunk_repeatval(*cparams, chunkbytes, chunk, chunksize, fill_value) < 0) {
433
0
    BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
434
0
  }
435
0
  free(cparams);
436
437
0
  for (int i = 0; i < (*array)->sc->nchunks; ++i) {
438
0
    if (blosc2_schunk_update_chunk((*array)->sc, i, chunk, true) < 0) {
439
0
      BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
440
0
    }
441
0
  }
442
0
  free(chunk);
443
444
0
  return BLOSC2_ERROR_SUCCESS;
445
0
}
446
447
448
0
int b2nd_from_schunk(blosc2_schunk *schunk, b2nd_array_t **array) {
449
0
  BLOSC_ERROR_NULL(schunk, BLOSC2_ERROR_NULL_POINTER);
450
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
451
452
0
  if (schunk == NULL) {
453
0
    BLOSC_TRACE_ERROR("Schunk is null");
454
0
    return BLOSC2_ERROR_NULL_POINTER;
455
0
  }
456
457
0
  blosc2_cparams *cparams;
458
0
  if (blosc2_schunk_get_cparams(schunk, &cparams) < 0) {
459
0
    BLOSC_TRACE_ERROR("Blosc error");
460
0
    return BLOSC2_ERROR_NULL_POINTER;
461
0
  }
462
0
  free(cparams);
463
464
0
  b2nd_context_t params = {0};
465
0
  params.b2_storage = schunk->storage;
466
467
  // Deserialize the b2nd metalayer
468
0
  uint8_t *smeta;
469
0
  int32_t smeta_len;
470
0
  if (blosc2_meta_get(schunk, "b2nd", &smeta, &smeta_len) < 0) {
471
    // Try with a caterva metalayer; we are meant to be backward compatible with it
472
0
    if (blosc2_meta_get(schunk, "caterva", &smeta, &smeta_len) < 0) {
473
0
      BLOSC_ERROR(BLOSC2_ERROR_METALAYER_NOT_FOUND);
474
0
    }
475
0
  }
476
0
  BLOSC_ERROR(b2nd_deserialize_meta(smeta, smeta_len, &params.ndim, params.shape,
477
0
                                    params.chunkshape, params.blockshape, &params.dtype,
478
0
                                    &params.dtype_format));
479
0
  free(smeta);
480
481
0
  BLOSC_ERROR(array_without_schunk(&params, array));
482
0
  free(params.dtype);
483
484
0
  (*array)->sc = schunk;
485
486
0
  if ((*array) == NULL) {
487
0
    BLOSC_TRACE_ERROR("Error creating a b2nd container from a frame");
488
0
    return BLOSC2_ERROR_NULL_POINTER;
489
0
  }
490
491
0
  return BLOSC2_ERROR_SUCCESS;
492
0
}
493
494
495
int b2nd_to_cframe(const b2nd_array_t *array, uint8_t **cframe, int64_t *cframe_len,
496
0
                   bool *needs_free) {
497
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
498
0
  BLOSC_ERROR_NULL(cframe, BLOSC2_ERROR_NULL_POINTER);
499
0
  BLOSC_ERROR_NULL(cframe_len, BLOSC2_ERROR_NULL_POINTER);
500
0
  BLOSC_ERROR_NULL(needs_free, BLOSC2_ERROR_NULL_POINTER);
501
502
0
  *cframe_len = blosc2_schunk_to_buffer(array->sc, cframe, needs_free);
503
0
  if (*cframe_len <= 0) {
504
0
    BLOSC_TRACE_ERROR("Error serializing the b2nd array");
505
0
    return BLOSC2_ERROR_FAILURE;
506
0
  }
507
0
  return BLOSC2_ERROR_SUCCESS;
508
0
}
509
510
511
0
int b2nd_from_cframe(uint8_t *cframe, int64_t cframe_len, bool copy, b2nd_array_t **array) {
512
0
  BLOSC_ERROR_NULL(cframe, BLOSC2_ERROR_NULL_POINTER);
513
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
514
515
0
  blosc2_schunk *sc = blosc2_schunk_from_buffer(cframe, cframe_len, copy);
516
0
  if (sc == NULL) {
517
0
    BLOSC_TRACE_ERROR("Blosc error");
518
0
    return BLOSC2_ERROR_FAILURE;
519
0
  }
520
  // ...and create a b2nd array out of it
521
0
  BLOSC_ERROR(b2nd_from_schunk(sc, array));
522
523
0
  return BLOSC2_ERROR_SUCCESS;
524
0
}
525
526
527
0
int b2nd_open(const char *urlpath, b2nd_array_t **array) {
528
0
  BLOSC_ERROR_NULL(urlpath, BLOSC2_ERROR_NULL_POINTER);
529
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
530
531
0
  blosc2_schunk *sc = blosc2_schunk_open(urlpath);
532
533
  // ...and create a b2nd array out of it
534
0
  BLOSC_ERROR(b2nd_from_schunk(sc, array));
535
536
0
  return BLOSC2_ERROR_SUCCESS;
537
0
}
538
539
540
0
int b2nd_open_offset(const char *urlpath, b2nd_array_t **array, int64_t offset) {
541
0
  BLOSC_ERROR_NULL(urlpath, BLOSC2_ERROR_NULL_POINTER);
542
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
543
544
0
  blosc2_schunk *sc = blosc2_schunk_open_offset(urlpath, offset);
545
546
  // ...and create a b2nd array out of it
547
0
  BLOSC_ERROR(b2nd_from_schunk(sc, array));
548
549
0
  return BLOSC2_ERROR_SUCCESS;
550
0
}
551
552
553
0
int b2nd_free(b2nd_array_t *array) {
554
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
555
556
0
  if (array) {
557
0
    if (array->sc != NULL) {
558
0
      blosc2_schunk_free(array->sc);
559
0
    }
560
0
    free(array->dtype);
561
0
    free(array);
562
0
  }
563
0
  return BLOSC2_ERROR_SUCCESS;
564
0
}
565
566
567
0
int b2nd_from_cbuffer(b2nd_context_t *ctx, b2nd_array_t **array, const void *buffer, int64_t buffersize) {
568
0
  BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
569
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
570
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
571
572
0
  BLOSC_ERROR(b2nd_empty(ctx, array));
573
574
0
  if (buffersize < (int64_t) (*array)->nitems * (*array)->sc->typesize) {
575
0
    BLOSC_TRACE_ERROR("The buffersize (%lld) is smaller than the array size (%lld)",
576
0
                        (long long) buffersize, (long long) (*array)->nitems * (*array)->sc->typesize);
577
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
578
0
  }
579
580
0
  if ((*array)->nitems == 0) {
581
0
    return BLOSC2_ERROR_SUCCESS;
582
0
  }
583
584
0
  int64_t start[B2ND_MAX_DIM] = {0};
585
0
  int64_t *stop = (*array)->shape;
586
0
  int64_t *shape = (*array)->shape;
587
0
  BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, shape, buffersize, start, stop, *array));
588
589
0
  return BLOSC2_ERROR_SUCCESS;
590
0
}
591
592
593
int b2nd_to_cbuffer(const b2nd_array_t *array, void *buffer,
594
0
                    int64_t buffersize) {
595
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
596
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
597
598
0
  if (buffersize < (int64_t) array->nitems * array->sc->typesize) {
599
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
600
0
  }
601
602
0
  if (array->nitems == 0) {
603
0
    return BLOSC2_ERROR_SUCCESS;
604
0
  }
605
606
0
  int64_t start[B2ND_MAX_DIM] = {0};
607
0
  const int64_t *stop = array->shape;
608
0
  BLOSC_ERROR(b2nd_get_slice_cbuffer(array, start, stop, buffer, array->shape, buffersize));
609
0
  return BLOSC2_ERROR_SUCCESS;
610
0
}
611
612
0
int b2nd_get_slice_nchunks(const b2nd_array_t *array, const int64_t *start, const int64_t *stop, int64_t **chunks_idx) {
613
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
614
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
615
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
616
617
0
  int8_t ndim = array->ndim;
618
619
0
  if (array->nitems == 0){  
620
0
    *chunks_idx = NULL;
621
0
    return 0;
622
0
  }
623
624
  // 0-dim case
625
0
  if (ndim == 0) {
626
0
    *chunks_idx = malloc(1 * sizeof(int64_t));
627
0
    *chunks_idx[0] = 0;
628
0
    return 1;
629
0
  }
630
631
0
  int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
632
0
  for (int i = 0; i < ndim; ++i) {
633
0
    chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
634
0
  }
635
636
0
  int64_t chunks_in_array_strides[B2ND_MAX_DIM];
637
0
  chunks_in_array_strides[ndim - 1] = 1;
638
0
  for (int i = ndim - 2; i >= 0; --i) {
639
0
    chunks_in_array_strides[i] = chunks_in_array_strides[i + 1] * chunks_in_array[i + 1];
640
0
  }
641
642
  // Compute the number of chunks to update
643
0
  int64_t update_start[B2ND_MAX_DIM];
644
0
  int64_t update_shape[B2ND_MAX_DIM];
645
646
0
  int64_t update_nchunks = 1;
647
0
  for (int i = 0; i < ndim; ++i) {
648
0
    int64_t pos = 0;
649
0
    while (pos <= start[i]) {
650
0
      pos += array->chunkshape[i];
651
0
    }
652
0
    update_start[i] = pos / array->chunkshape[i] - 1;
653
0
    while (pos < stop[i]) {
654
0
      pos += array->chunkshape[i];
655
0
    }
656
0
    update_shape[i] = pos / array->chunkshape[i] - update_start[i];
657
0
    update_nchunks *= update_shape[i];
658
0
  }
659
660
0
  int nchunks = 0;
661
  // Initially we do not know the number of chunks that will be affected
662
0
  *chunks_idx = malloc(array->sc->nchunks * sizeof(int64_t));
663
0
  int64_t *ptr = *chunks_idx;
664
0
  for (int update_nchunk = 0; update_nchunk < update_nchunks; ++update_nchunk) {
665
0
    int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
666
0
    blosc2_unidim_to_multidim(ndim, update_shape, update_nchunk, nchunk_ndim);
667
0
    for (int i = 0; i < ndim; ++i) {
668
0
      nchunk_ndim[i] += update_start[i];
669
0
    }
670
0
    int64_t nchunk;
671
0
    blosc2_multidim_to_unidim(nchunk_ndim, ndim, chunks_in_array_strides, &nchunk);
672
673
    // Check if the chunk is inside the slice domain
674
0
    int64_t chunk_start[B2ND_MAX_DIM] = {0};
675
0
    int64_t chunk_stop[B2ND_MAX_DIM] = {0};
676
0
    for (int i = 0; i < ndim; ++i) {
677
0
      chunk_start[i] = nchunk_ndim[i] * array->chunkshape[i];
678
0
      chunk_stop[i] = chunk_start[i] + array->chunkshape[i];
679
0
      if (chunk_stop[i] > array->shape[i]) {
680
0
        chunk_stop[i] = array->shape[i];
681
0
      }
682
0
    }
683
0
    bool chunk_empty = false;
684
0
    for (int i = 0; i < ndim; ++i) {
685
0
      chunk_empty |= (chunk_stop[i] <= start[i] || chunk_start[i] >= stop[i]);
686
0
    }
687
0
    if (chunk_empty) {
688
0
      continue;
689
0
    }
690
691
0
    ptr[nchunks] = nchunk;
692
0
    nchunks++;
693
0
  }
694
695
0
  if (nchunks < array->sc->nchunks) {
696
0
    *chunks_idx = realloc(ptr, nchunks * sizeof(int64_t));
697
0
  }
698
699
0
  return nchunks;
700
0
}
701
702
703
// Check whether the slice defined by start and stop is a single chunk and contiguous
704
// in the C order. This is a fast path for the get_slice and set_slice functions.
705
int64_t nchunk_fastpath(const b2nd_array_t *array, const int64_t *start,
706
0
                        const int64_t *stop, const int64_t slice_size) {
707
0
  if (slice_size != array->chunknitems) {
708
0
    return -1;
709
0
  }
710
711
0
  int ndim = (int) array->ndim;
712
713
0
  int k = 0;
714
0
  for (int i = 0; i < ndim; ++i) {
715
    // The slice needs to correspond to a whole chunk (without padding)
716
0
    if (start[i] % array->chunkshape[i] != 0) {
717
0
      return -1;
718
0
    }
719
0
    if (stop[i] - start[i] != array->chunkshape[i]) {
720
0
      return -1;
721
0
    }
722
723
    // There needs to exist 0 <= k <= ndim such that:
724
    // - for i < k, blockshape[i] == 1
725
    // - for i == k, blockshape[i] divides chunkshape[i]
726
    // - for i > k, blockshape[i] == chunkshape[i]
727
0
    if (array->chunkshape[i] % array->blockshape[i] != 0) {
728
0
      return -1;
729
0
    }
730
0
    if (i > k && array->chunkshape[i] != array->blockshape[i]) {
731
0
      return -1;
732
0
    }
733
0
    if (i == k && array->blockshape[i] == 1) {
734
0
      k++;
735
0
    }
736
0
  }
737
  // Compute the chunk number
738
0
  int64_t *chunks_idx;
739
0
  int nchunks = b2nd_get_slice_nchunks(array, start, stop, &chunks_idx);
740
0
  if (nchunks != 1) {
741
0
    free(chunks_idx);
742
0
    BLOSC_TRACE_ERROR("The number of chunks to read is not 1; go fix the code");
743
0
    BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
744
0
  }
745
0
  int64_t nchunk = chunks_idx[0];
746
0
  free(chunks_idx);
747
748
0
  return nchunk;
749
0
}
750
751
752
// Setting and getting slices
753
int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const int64_t *stop,
754
0
                  const int64_t *shape, b2nd_array_t *array, bool set_slice) {
755
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
756
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
757
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
758
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
759
0
  if (buffersize < 0) {
760
0
    BLOSC_TRACE_ERROR("buffersize is < 0");
761
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
762
0
  }
763
764
0
  uint8_t *buffer_b = buffer;
765
0
  int8_t ndim = array->ndim;
766
767
  // 0-dim case
768
0
  if (ndim == 0) {
769
0
    if (set_slice) {
770
0
      int32_t chunk_size = array->sc->typesize + BLOSC2_MAX_OVERHEAD;
771
0
      uint8_t *chunk = malloc(chunk_size);
772
0
      BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
773
0
      if (blosc2_compress_ctx(array->sc->cctx, buffer_b, array->sc->typesize, chunk, chunk_size) < 0) {
774
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
775
0
      }
776
0
      if (blosc2_schunk_update_chunk(array->sc, 0, chunk, false) < 0) {
777
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
778
0
      }
779
780
0
    } else {
781
0
      if (blosc2_schunk_decompress_chunk(array->sc, 0, buffer_b, array->sc->typesize) < 0) {
782
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
783
0
      }
784
0
    }
785
0
    return BLOSC2_ERROR_SUCCESS;
786
0
  }
787
788
0
  if (array->nitems == 0) {
789
0
    return BLOSC2_ERROR_SUCCESS;
790
0
  }
791
792
0
  int64_t nelems_slice = 1;
793
0
  for (int i = 0; i < array->ndim; ++i) {
794
0
    if (stop[i] - start[i] > shape[i]) {
795
0
      BLOSC_TRACE_ERROR("The buffer shape can not be smaller than the slice shape");
796
0
      return BLOSC2_ERROR_INVALID_PARAM;
797
0
    }
798
0
    nelems_slice *= stop[i] - start[i];
799
0
  }
800
0
  int64_t slice_nbytes = nelems_slice * array->sc->typesize;
801
0
  int32_t data_nbytes = (int32_t) array->extchunknitems * array->sc->typesize;
802
803
0
  if (buffersize < slice_nbytes) {
804
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
805
0
  }
806
807
  // Check for fast path for aligned slices with chunks and blocks (only 1 chunk is supported)
808
0
  int64_t nchunk = nchunk_fastpath(array, start, stop, nelems_slice);
809
0
  if (nchunk >= 0) {
810
0
    if (set_slice) {
811
      // Fast path for set. Let's set the chunk buffer straight into the array.
812
      // Compress the chunk
813
0
      int32_t chunk_nbytes = data_nbytes + BLOSC2_MAX_OVERHEAD;
814
0
      uint8_t *chunk = malloc(chunk_nbytes);
815
0
      BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
816
0
      int brc;
817
      // Update current_chunk in case a prefilter is applied
818
0
      array->sc->current_nchunk = nchunk;
819
0
      brc = blosc2_compress_ctx(array->sc->cctx, buffer, data_nbytes, chunk, chunk_nbytes);
820
0
      if (brc < 0) {
821
0
        BLOSC_TRACE_ERROR("Blosc can not compress the data");
822
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
823
0
      }
824
0
      int64_t brc_ = blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
825
0
      if (brc_ < 0) {
826
0
        BLOSC_TRACE_ERROR("Blosc can not update the chunk");
827
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
828
0
      }
829
      // We are done
830
0
      return BLOSC2_ERROR_SUCCESS;
831
0
    }
832
0
    else {
833
      // Fast path for get. Let's read the chunk straight into the buffer.
834
0
      if (blosc2_schunk_decompress_chunk(array->sc, nchunk, buffer, (int32_t) slice_nbytes) < 0) {
835
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
836
0
      }
837
0
      return BLOSC2_ERROR_SUCCESS;
838
0
    }
839
0
  }
840
841
  // Slow path for set and get
842
843
0
  uint8_t *data = malloc(data_nbytes);
844
0
  BLOSC_ERROR_NULL(data, BLOSC2_ERROR_MEMORY_ALLOC);
845
846
0
  int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
847
0
  for (int i = 0; i < ndim; ++i) {
848
0
    chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
849
0
  }
850
851
0
  int64_t chunks_in_array_strides[B2ND_MAX_DIM];
852
0
  chunks_in_array_strides[ndim - 1] = 1;
853
0
  for (int i = ndim - 2; i >= 0; --i) {
854
0
    chunks_in_array_strides[i] = chunks_in_array_strides[i + 1] * chunks_in_array[i + 1];
855
0
  }
856
857
0
  int64_t blocks_in_chunk[B2ND_MAX_DIM] = {0};
858
0
  for (int i = 0; i < ndim; ++i) {
859
0
    blocks_in_chunk[i] = array->extchunkshape[i] / array->blockshape[i];
860
0
  }
861
862
  // Compute the number of chunks to update
863
0
  int64_t update_start[B2ND_MAX_DIM];
864
0
  int64_t update_shape[B2ND_MAX_DIM];
865
866
0
  int64_t update_nchunks = 1;
867
0
  for (int i = 0; i < ndim; ++i) {
868
0
    int64_t pos = 0;
869
0
    while (pos <= start[i]) {
870
0
      pos += array->chunkshape[i];
871
0
    }
872
0
    update_start[i] = pos / array->chunkshape[i] - 1;
873
0
    while (pos < stop[i]) {
874
0
      pos += array->chunkshape[i];
875
0
    }
876
0
    update_shape[i] = pos / array->chunkshape[i] - update_start[i];
877
0
    update_nchunks *= update_shape[i];
878
0
  }
879
880
0
  for (int update_nchunk = 0; update_nchunk < update_nchunks; ++update_nchunk) {
881
0
    int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
882
0
    blosc2_unidim_to_multidim(ndim, update_shape, update_nchunk, nchunk_ndim);
883
0
    for (int i = 0; i < ndim; ++i) {
884
0
      nchunk_ndim[i] += update_start[i];
885
0
    }
886
0
    int64_t nchunk;
887
0
    blosc2_multidim_to_unidim(nchunk_ndim, ndim, chunks_in_array_strides, &nchunk);
888
889
    // Check if the chunk needs to be updated
890
0
    int64_t chunk_start[B2ND_MAX_DIM] = {0};
891
0
    int64_t chunk_stop[B2ND_MAX_DIM] = {0};
892
0
    for (int i = 0; i < ndim; ++i) {
893
0
      chunk_start[i] = nchunk_ndim[i] * array->chunkshape[i];
894
0
      chunk_stop[i] = chunk_start[i] + array->chunkshape[i];
895
0
      if (chunk_stop[i] > array->shape[i]) {
896
0
        chunk_stop[i] = array->shape[i];
897
0
      }
898
0
    }
899
0
    bool chunk_empty = false;
900
0
    for (int i = 0; i < ndim; ++i) {
901
0
      chunk_empty |= (chunk_stop[i] <= start[i] || chunk_start[i] >= stop[i]);
902
0
    }
903
0
    if (chunk_empty) {
904
0
      continue;
905
0
    }
906
907
0
    int32_t nblocks = (int32_t) array->extchunknitems / array->blocknitems;
908
0
    if (set_slice) {
909
      // Check if all the chunk is going to be updated and avoid the decompression
910
0
      bool decompress_chunk = false;
911
0
      for (int i = 0; i < ndim; ++i) {
912
0
        decompress_chunk |= (chunk_start[i] < start[i] || chunk_stop[i] > stop[i]);
913
0
      }
914
915
0
      if (decompress_chunk) {
916
0
        int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
917
0
        if (err < 0) {
918
0
          BLOSC_TRACE_ERROR("Error decompressing chunk");
919
0
          BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
920
0
        }
921
0
      } else {
922
        // Avoid writing non zero padding from previous chunk
923
0
        memset(data, 0, data_nbytes);
924
0
      }
925
0
    } else {
926
0
      bool *block_maskout = malloc(nblocks);
927
0
      BLOSC_ERROR_NULL(block_maskout, BLOSC2_ERROR_MEMORY_ALLOC);
928
0
      for (int nblock = 0; nblock < nblocks; ++nblock) {
929
0
        int64_t nblock_ndim[B2ND_MAX_DIM] = {0};
930
0
        blosc2_unidim_to_multidim(ndim, blocks_in_chunk, nblock, nblock_ndim);
931
932
        // Check if the block needs to be updated
933
0
        int64_t block_start[B2ND_MAX_DIM] = {0};
934
0
        int64_t block_stop[B2ND_MAX_DIM] = {0};
935
0
        for (int i = 0; i < ndim; ++i) {
936
0
          block_start[i] = nblock_ndim[i] * array->blockshape[i];
937
0
          block_stop[i] = block_start[i] + array->blockshape[i];
938
0
          block_start[i] += chunk_start[i];
939
0
          block_stop[i] += chunk_start[i];
940
941
0
          if (block_start[i] > chunk_stop[i]) {
942
0
            block_start[i] = chunk_stop[i];
943
0
          }
944
0
          if (block_stop[i] > chunk_stop[i]) {
945
0
            block_stop[i] = chunk_stop[i];
946
0
          }
947
0
        }
948
949
0
        bool block_empty = false;
950
0
        for (int i = 0; i < ndim; ++i) {
951
0
          block_empty |= (block_stop[i] <= start[i] || block_start[i] >= stop[i]);
952
0
        }
953
0
        block_maskout[nblock] = block_empty ? true : false;
954
0
      }
955
956
0
      if (blosc2_set_maskout(array->sc->dctx, block_maskout, nblocks) != BLOSC2_ERROR_SUCCESS) {
957
0
        BLOSC_TRACE_ERROR("Error setting the maskout");
958
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
959
0
      }
960
961
0
      int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
962
0
      if (err < 0) {
963
0
        BLOSC_TRACE_ERROR("Error decompressing chunk");
964
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
965
0
      }
966
967
0
      free(block_maskout);
968
0
    }
969
970
    // Iterate over blocks
971
972
0
    for (int nblock = 0; nblock < nblocks; ++nblock) {
973
0
      int64_t nblock_ndim[B2ND_MAX_DIM] = {0};
974
0
      blosc2_unidim_to_multidim(ndim, blocks_in_chunk, nblock, nblock_ndim);
975
976
      // Check if the block needs to be updated
977
0
      int64_t block_start[B2ND_MAX_DIM] = {0};
978
0
      int64_t block_stop[B2ND_MAX_DIM] = {0};
979
0
      for (int i = 0; i < ndim; ++i) {
980
0
        block_start[i] = nblock_ndim[i] * array->blockshape[i];
981
0
        block_stop[i] = block_start[i] + array->blockshape[i];
982
0
        block_start[i] += chunk_start[i];
983
0
        block_stop[i] += chunk_start[i];
984
985
0
        if (block_start[i] > chunk_stop[i]) {
986
0
          block_start[i] = chunk_stop[i];
987
0
        }
988
0
        if (block_stop[i] > chunk_stop[i]) {
989
0
          block_stop[i] = chunk_stop[i];
990
0
        }
991
0
      }
992
0
      int64_t block_shape[B2ND_MAX_DIM] = {0};
993
0
      for (int i = 0; i < ndim; ++i) {
994
0
        block_shape[i] = block_stop[i] - block_start[i];
995
0
      }
996
0
      bool block_empty = false;
997
0
      for (int i = 0; i < ndim; ++i) {
998
0
        block_empty |= (block_stop[i] <= start[i] || block_start[i] >= stop[i]);
999
0
      }
1000
0
      if (block_empty) {
1001
0
        continue;
1002
0
      }
1003
1004
      // compute the start of the slice inside the block
1005
0
      int64_t slice_start[B2ND_MAX_DIM] = {0};
1006
0
      for (int i = 0; i < ndim; ++i) {
1007
0
        if (block_start[i] < start[i]) {
1008
0
          slice_start[i] = start[i] - block_start[i];
1009
0
        } else {
1010
0
          slice_start[i] = 0;
1011
0
        }
1012
0
        slice_start[i] += block_start[i];
1013
0
      }
1014
1015
0
      int64_t slice_stop[B2ND_MAX_DIM] = {0};
1016
0
      for (int i = 0; i < ndim; ++i) {
1017
0
        if (block_stop[i] > stop[i]) {
1018
0
          slice_stop[i] = block_shape[i] - (block_stop[i] - stop[i]);
1019
0
        } else {
1020
0
          slice_stop[i] = block_stop[i] - block_start[i];
1021
0
        }
1022
0
        slice_stop[i] += block_start[i];
1023
0
      }
1024
1025
0
      int64_t slice_shape[B2ND_MAX_DIM] = {0};
1026
0
      for (int i = 0; i < ndim; ++i) {
1027
0
        slice_shape[i] = slice_stop[i] - slice_start[i];
1028
0
      }
1029
1030
0
      uint8_t *src = &buffer_b[0];
1031
1032
0
      int64_t src_start[B2ND_MAX_DIM] = {0};
1033
0
      int64_t src_stop[B2ND_MAX_DIM] = {0};
1034
0
      for (int i = 0; i < ndim; ++i) {
1035
0
        src_start[i] = slice_start[i] - start[i];
1036
0
        src_stop[i] = slice_stop[i] - start[i];
1037
0
      }
1038
1039
0
      uint8_t *dst = &data[nblock * array->blocknitems * array->sc->typesize];
1040
0
      int64_t dst_pad_shape[B2ND_MAX_DIM];
1041
0
      for (int i = 0; i < ndim; ++i) {
1042
0
        dst_pad_shape[i] = array->blockshape[i];
1043
0
      }
1044
1045
0
      int64_t dst_start[B2ND_MAX_DIM] = {0};
1046
0
      int64_t dst_stop[B2ND_MAX_DIM] = {0};
1047
0
      for (int i = 0; i < ndim; ++i) {
1048
0
        dst_start[i] = slice_start[i] - block_start[i];
1049
0
        dst_stop[i] = dst_start[i] + slice_shape[i];
1050
0
      }
1051
1052
0
      if (set_slice) {
1053
0
        b2nd_copy_buffer2(ndim, array->sc->typesize,
1054
0
                          src, shape, src_start, src_stop,
1055
0
                          dst, dst_pad_shape, dst_start);
1056
0
      } else {
1057
0
        b2nd_copy_buffer2(ndim, array->sc->typesize,
1058
0
                          dst, dst_pad_shape, dst_start, dst_stop,
1059
0
                          src, shape, src_start);
1060
0
      }
1061
0
    }
1062
1063
0
    if (set_slice) {
1064
      // Recompress the data
1065
0
      int32_t chunk_nbytes = data_nbytes + BLOSC2_MAX_OVERHEAD;
1066
0
      uint8_t *chunk = malloc(chunk_nbytes);
1067
0
      BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
1068
0
      int brc;
1069
      // Update current_chunk in case a prefilter is applied
1070
0
      array->sc->current_nchunk = nchunk;
1071
0
      brc = blosc2_compress_ctx(array->sc->cctx, data, data_nbytes, chunk, chunk_nbytes);
1072
0
      if (brc < 0) {
1073
0
        BLOSC_TRACE_ERROR("Blosc can not compress the data");
1074
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1075
0
      }
1076
0
      int64_t brc_ = blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
1077
0
      if (brc_ < 0) {
1078
0
        BLOSC_TRACE_ERROR("Blosc can not update the chunk");
1079
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1080
0
      }
1081
0
    }
1082
0
  }
1083
1084
0
  free(data);
1085
1086
0
  return BLOSC2_ERROR_SUCCESS;
1087
0
}
1088
1089
1090
int b2nd_get_slice_cbuffer(const b2nd_array_t *array, const int64_t *start, const int64_t *stop,
1091
0
                           void *buffer, const int64_t *buffershape, int64_t buffersize) {
1092
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1093
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1094
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1095
0
  BLOSC_ERROR_NULL(buffershape, BLOSC2_ERROR_NULL_POINTER);
1096
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1097
1098
0
  BLOSC_ERROR(get_set_slice(buffer, buffersize, start, stop, buffershape, (b2nd_array_t *)array, false));
1099
1100
0
  return BLOSC2_ERROR_SUCCESS;
1101
0
}
1102
1103
1104
int b2nd_set_slice_cbuffer(const void *buffer, const int64_t *buffershape, int64_t buffersize,
1105
                           const int64_t *start, const int64_t *stop,
1106
0
                           b2nd_array_t *array) {
1107
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1108
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1109
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1110
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1111
1112
0
  BLOSC_ERROR(get_set_slice((void*)buffer, buffersize, start, stop, (int64_t *)buffershape, array, true));
1113
1114
0
  return BLOSC2_ERROR_SUCCESS;
1115
0
}
1116
1117
1118
int b2nd_get_slice(b2nd_context_t *ctx, b2nd_array_t **array, const b2nd_array_t *src, const int64_t *start,
1119
0
                   const int64_t *stop) {
1120
0
  BLOSC_ERROR_NULL(src, BLOSC2_ERROR_NULL_POINTER);
1121
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1122
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1123
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1124
1125
0
  ctx->ndim = src->ndim;
1126
0
  for (int i = 0; i < src->ndim; ++i) {
1127
0
    ctx->shape[i] = stop[i] - start[i];
1128
0
  }
1129
1130
  // Add data
1131
0
  BLOSC_ERROR(b2nd_empty(ctx, array));
1132
1133
0
  if ((*array)->nitems == 0) {
1134
0
    return BLOSC2_ERROR_SUCCESS;
1135
0
  }
1136
1137
0
  int8_t ndim = (*array)->ndim;
1138
0
  int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
1139
0
  for (int i = 0; i < ndim; ++i) {
1140
0
    chunks_in_array[i] = (*array)->extshape[i] / (*array)->chunkshape[i];
1141
0
  }
1142
0
  int64_t nchunks = (*array)->sc->nchunks;
1143
0
  for (int nchunk = 0; nchunk < nchunks; ++nchunk) {
1144
0
    int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1145
0
    blosc2_unidim_to_multidim(ndim, chunks_in_array, nchunk, nchunk_ndim);
1146
1147
    // Check if the chunk needs to be updated
1148
0
    int64_t chunk_start[B2ND_MAX_DIM] = {0};
1149
0
    int64_t chunk_stop[B2ND_MAX_DIM] = {0};
1150
0
    int64_t chunk_shape[B2ND_MAX_DIM] = {0};
1151
0
    for (int i = 0; i < ndim; ++i) {
1152
0
      chunk_start[i] = nchunk_ndim[i] * (*array)->chunkshape[i];
1153
0
      chunk_stop[i] = chunk_start[i] + (*array)->chunkshape[i];
1154
0
      if (chunk_stop[i] > (*array)->shape[i]) {
1155
0
        chunk_stop[i] = (*array)->shape[i];
1156
0
      }
1157
0
      chunk_shape[i] = chunk_stop[i] - chunk_start[i];
1158
0
    }
1159
1160
0
    int64_t src_start[B2ND_MAX_DIM] = {0};
1161
0
    int64_t src_stop[B2ND_MAX_DIM] = {0};
1162
0
    for (int i = 0; i < ndim; ++i) {
1163
0
      src_start[i] = chunk_start[i] + start[i];
1164
0
      src_stop[i] = chunk_stop[i] + start[i];
1165
0
    }
1166
0
    int64_t buffersize = ctx->b2_storage->cparams->typesize;
1167
0
    for (int i = 0; i < ndim; ++i) {
1168
0
      buffersize *= chunk_shape[i];
1169
0
    }
1170
0
    uint8_t *buffer = malloc(buffersize);
1171
0
    BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_MEMORY_ALLOC);
1172
0
    BLOSC_ERROR(b2nd_get_slice_cbuffer(src, src_start, src_stop, buffer, chunk_shape,
1173
0
                                       buffersize));
1174
0
    BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, chunk_shape, buffersize, chunk_start,
1175
0
                                       chunk_stop, *array));
1176
0
    free(buffer);
1177
0
  }
1178
1179
0
  return BLOSC2_ERROR_SUCCESS;
1180
0
}
1181
1182
/**
1183
 * @brief Return a view of a b2nd array.
1184
 *
1185
 * @param array The memory pointer of the array which will be viewed.
1186
 * @param view The memory pointer where the view will be created.
1187
 * @param ctx1 The b2nd context for the new array, containing new shape and other metadata.
1188
 *
1189
 * @return An error code.
1190
 *
1191
 * @note This doesn't support slices of arrays and is only useful for adding (or removing) dimensions.
1192
 *
1193
 */
1194
0
int view_new(const b2nd_array_t *array, b2nd_array_t **view, b2nd_context_t *ctx1) {
1195
1196
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1197
0
  BLOSC_ERROR_NULL(view, BLOSC2_ERROR_NULL_POINTER);
1198
1199
  // The view is not contiguous (points to the original contiguous cframe which has different shape)
1200
  // so we set contiguous to false, which forces a copy when calling to_cframe
1201
0
  ctx1->b2_storage->contiguous = false;
1202
1203
1204
  /* Fill view with zeros */
1205
0
  BLOSC_ERROR(b2nd_zeros(ctx1, view));
1206
  // Free the chunks in base array
1207
0
  for (int i = 0; i < (*view)->sc->nchunks; i++) {
1208
0
    free((*view)->sc->data[i]);
1209
0
  }
1210
0
  free((*view)->sc->data);
1211
0
  (*view)->sc->view = true;
1212
0
  (*view)->sc->data = array->sc->data; // point view to the same data
1213
0
  (*view)->sc->frame = array->sc->frame; // if original array is contiguous, point to frame
1214
0
  (*view)->sc->nvlmetalayers = array->sc->nvlmetalayers; //
1215
0
  for (int i = 0; i< array->sc->nvlmetalayers; i++) {
1216
0
    (*view)->sc->vlmetalayers[i] = array->sc->vlmetalayers[i]; // add ptrs to vlmetalayers
1217
0
  }
1218
1219
0
  return BLOSC2_ERROR_SUCCESS;
1220
0
}
1221
1222
0
int b2nd_expand_dims(const b2nd_array_t *array, b2nd_array_t **view, const bool *axis, const uint8_t final_dims) {
1223
0
  for (int i = 0; i < array->sc->nmetalayers; ++i) {
1224
0
    if (strcmp(array->sc->metalayers[i]->name, "b2nd") != 0) {
1225
0
      BLOSC_TRACE_ERROR("Cannot expand dimensions of an array with non-b2nd metalayers");
1226
0
      return BLOSC2_ERROR_INVALID_PARAM;
1227
0
    }
1228
0
  }
1229
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1230
0
  BLOSC_ERROR_NULL(view, BLOSC2_ERROR_NULL_POINTER);
1231
1232
0
  uint8_t old_idx = 0;
1233
0
  int64_t newshape[B2ND_MAX_DIM];
1234
0
  int32_t newchunkshape[B2ND_MAX_DIM];
1235
0
  int32_t newblockshape[B2ND_MAX_DIM];
1236
1237
0
  for (int i = 0; i < final_dims; ++i) {
1238
0
    if (axis[i] == true) {
1239
0
      newshape[i] = 1;
1240
0
      newchunkshape[i] = 1;
1241
0
      newblockshape[i] = 1;
1242
0
    }
1243
0
    else {
1244
0
      if (old_idx == array->ndim) {
1245
0
        BLOSC_TRACE_ERROR("Error in axis list: original array has fewer dimensions than the axis list implies!");
1246
0
        return BLOSC2_ERROR_INVALID_PARAM;
1247
0
      }
1248
0
      newshape[i] = array->shape[old_idx];
1249
0
      newchunkshape[i] = array->chunkshape[old_idx];
1250
0
      newblockshape[i] = array->blockshape[old_idx];
1251
0
      old_idx++;
1252
0
    }
1253
0
  }
1254
1255
  //views only deal with cparams/dparams; storage is always in-memory (ephemeral).
1256
0
  blosc2_cparams cparams = *(array->sc->storage->cparams);
1257
0
  blosc2_dparams dparams = *(array->sc->storage->dparams);
1258
0
  blosc2_storage b2_storage1 = {.cparams=&cparams, .dparams=&dparams};
1259
1260
0
  b2nd_context_t *ctx1 = b2nd_create_ctx(&b2_storage1, final_dims, newshape,
1261
0
                                        newchunkshape, newblockshape, array->dtype,
1262
0
                                        array->dtype_format, NULL, 0);
1263
1264
0
  view_new(array, view, ctx1);
1265
0
  b2nd_free_ctx(ctx1);
1266
1267
0
  return BLOSC2_ERROR_SUCCESS;
1268
0
}
1269
1270
1271
0
int b2nd_squeeze(b2nd_array_t *array, b2nd_array_t **view) {
1272
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1273
0
  BLOSC_ERROR_NULL(view, BLOSC2_ERROR_NULL_POINTER);
1274
1275
0
  bool index[B2ND_MAX_DIM];
1276
1277
0
  for (int i = 0; i < array->ndim; ++i) {
1278
0
    if (array->shape[i] != 1) {
1279
0
      index[i] = false;
1280
0
    } else {
1281
0
      index[i] = true;
1282
0
    }
1283
0
  }
1284
0
  BLOSC_ERROR(b2nd_squeeze_index(array, view, index));
1285
1286
0
  return BLOSC2_ERROR_SUCCESS;
1287
0
}
1288
1289
1290
0
int b2nd_squeeze_index(b2nd_array_t *array, b2nd_array_t **view, const bool *index) {
1291
0
  for (int i = 0; i < array->sc->nmetalayers; ++i) {
1292
0
    if (strcmp(array->sc->metalayers[i]->name, "b2nd") != 0) {
1293
0
      BLOSC_TRACE_ERROR("Cannot squeeze dimensions of an array with non-b2nd metalayers");
1294
0
      return BLOSC2_ERROR_INVALID_PARAM;
1295
0
    }
1296
0
  }
1297
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1298
0
  BLOSC_ERROR_NULL(view, BLOSC2_ERROR_NULL_POINTER);
1299
1300
0
  uint8_t nones = 0;
1301
0
  int64_t newshape[B2ND_MAX_DIM];
1302
0
  int32_t newchunkshape[B2ND_MAX_DIM];
1303
0
  int32_t newblockshape[B2ND_MAX_DIM];
1304
1305
0
  for (int i = 0; i < array->ndim; ++i) {
1306
0
    if (index[i] == true) {
1307
0
      if (array->shape[i] != 1) {
1308
0
        BLOSC_ERROR(BLOSC2_ERROR_INVALID_INDEX);
1309
0
      }
1310
0
    } else {
1311
0
      newshape[nones] = array->shape[i];
1312
0
      newchunkshape[nones] = array->chunkshape[i];
1313
0
      newblockshape[nones] = array->blockshape[i];
1314
0
      nones += 1;
1315
0
    }
1316
0
  }
1317
1318
  //views only deal with cparams/dparams; storage is always in-memory (ephemeral).
1319
0
  blosc2_cparams cparams = *(array->sc->storage->cparams);
1320
0
  blosc2_dparams dparams = *(array->sc->storage->dparams);
1321
0
  blosc2_storage b2_storage1 = {.cparams=&cparams, .dparams=&dparams};
1322
1323
0
  b2nd_context_t *ctx1 = b2nd_create_ctx(&b2_storage1, nones, newshape,
1324
0
                                        newchunkshape, newblockshape, array->dtype,
1325
0
                                        array->dtype_format, NULL, 0);
1326
1327
0
  view_new(array, view, ctx1);
1328
0
  b2nd_free_ctx(ctx1);
1329
1330
0
  return BLOSC2_ERROR_SUCCESS;
1331
0
}
1332
1333
1334
0
int b2nd_copy(b2nd_context_t *ctx, const b2nd_array_t *src, b2nd_array_t **array) {
1335
0
  BLOSC_ERROR_NULL(src, BLOSC2_ERROR_NULL_POINTER);
1336
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1337
1338
0
  ctx->ndim = src->ndim;
1339
1340
0
  for (int i = 0; i < src->ndim; ++i) {
1341
0
    ctx->shape[i] = src->shape[i];
1342
0
  }
1343
1344
0
  bool equals = true;
1345
0
  for (int i = 0; i < src->ndim; ++i) {
1346
0
    if (src->chunkshape[i] != ctx->chunkshape[i]) {
1347
0
      equals = false;
1348
0
      break;
1349
0
    }
1350
0
    if (src->blockshape[i] != ctx->blockshape[i]) {
1351
0
      equals = false;
1352
0
      break;
1353
0
    }
1354
0
  }
1355
1356
0
  if (equals) {
1357
0
    BLOSC_ERROR(array_without_schunk(ctx, array));
1358
1359
0
    blosc2_schunk *new_sc = blosc2_schunk_copy(src->sc, ctx->b2_storage);
1360
1361
0
    if (new_sc == NULL) {
1362
0
      return BLOSC2_ERROR_FAILURE;
1363
0
    }
1364
0
    (*array)->sc = new_sc;
1365
1366
0
  } else {
1367
0
    int64_t start[B2ND_MAX_DIM] = {0};
1368
1369
0
    int64_t stop[B2ND_MAX_DIM];
1370
0
    for (int i = 0; i < src->ndim; ++i) {
1371
0
      stop[i] = src->shape[i];
1372
0
    }
1373
    // Copy metalayers
1374
0
    b2nd_context_t params_meta;
1375
0
    memcpy(&params_meta, ctx, sizeof(params_meta));
1376
0
    int j = 0;
1377
1378
0
    for (int i = 0; i < src->sc->nmetalayers; ++i) {
1379
0
      if (strcmp(src->sc->metalayers[i]->name, "b2nd") == 0) {
1380
0
        continue;
1381
0
      }
1382
0
      blosc2_metalayer *meta = &params_meta.metalayers[j];
1383
0
      meta->name = src->sc->metalayers[i]->name;
1384
0
      meta->content = src->sc->metalayers[i]->content;
1385
0
      meta->content_len = src->sc->metalayers[i]->content_len;
1386
0
      j++;
1387
0
    }
1388
0
    params_meta.nmetalayers = j;
1389
1390
    // Copy data
1391
0
    BLOSC_ERROR(b2nd_get_slice(&params_meta, array, src, start, stop));
1392
1393
    // Copy vlmetayers
1394
0
    for (int i = 0; i < src->sc->nvlmetalayers; ++i) {
1395
0
      uint8_t *content;
1396
0
      int32_t content_len;
1397
0
      if (blosc2_vlmeta_get(src->sc, src->sc->vlmetalayers[i]->name, &content,
1398
0
                            &content_len) < 0) {
1399
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1400
0
      }
1401
0
      BLOSC_ERROR(blosc2_vlmeta_add((*array)->sc, src->sc->vlmetalayers[i]->name, content, content_len,
1402
0
                                      (*array)->sc->storage->cparams));
1403
0
      free(content);
1404
0
    }
1405
0
  }
1406
0
  return BLOSC2_ERROR_SUCCESS;
1407
0
}
1408
1409
1410
int b2nd_concatenate(b2nd_context_t *ctx, const b2nd_array_t *src1, const b2nd_array_t *src2,
1411
0
                     int8_t axis, bool copy, b2nd_array_t **array) {
1412
0
  BLOSC_ERROR_NULL(src1, BLOSC2_ERROR_NULL_POINTER);
1413
0
  BLOSC_ERROR_NULL(src2, BLOSC2_ERROR_NULL_POINTER);
1414
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1415
1416
  // Validate the axis parameter
1417
0
  if (axis < 0 || axis >= src1->ndim) {
1418
0
    BLOSC_TRACE_ERROR("axis parameter is out of bounds: axis=%d, expected range=[0, %d)", axis, src1->ndim - 1);
1419
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1420
0
  }
1421
1422
  // typesize must be the same for both arrays
1423
0
  if (src1->sc->typesize != src2->sc->typesize) {
1424
0
    BLOSC_TRACE_ERROR("The two arrays must have the same typesize");
1425
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1426
0
  }
1427
1428
  // Keep the src1 shape for later use
1429
0
  int64_t src1_shape[B2ND_MAX_DIM];
1430
0
  for (int i = 0; i < src1->ndim; ++i) {
1431
0
    src1_shape[i] = src1->shape[i];
1432
0
  }
1433
1434
  // Support for 0-dim arrays is not implemented
1435
0
  if (src1->ndim == 0 || src2->ndim == 0) {
1436
0
    BLOSC_TRACE_ERROR("Concatenation of 0-dim arrays is not supported");
1437
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1438
0
  }
1439
1440
  // Check that the shapes are compatible for concatenation
1441
0
  if (src1->ndim != src2->ndim) {
1442
0
    BLOSC_TRACE_ERROR("The two arrays must have the same number of dimensions");
1443
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1444
0
  }
1445
  // Compute the new shape
1446
0
  int64_t newshape[B2ND_MAX_DIM];
1447
0
  for (int8_t i = 0; i < src1->ndim; ++i) {
1448
0
    if (i == axis) {
1449
0
      newshape[i] = src1->shape[i] + src2->shape[i];
1450
0
    } else {
1451
0
      if (src1->shape[i] != src2->shape[i]) {
1452
0
        BLOSC_TRACE_ERROR("The two arrays must have the same shape in all dimensions except the concatenation axis");
1453
0
        BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1454
0
      }
1455
0
      newshape[i] = src1->shape[i];
1456
0
    }
1457
0
  }
1458
1459
0
  if (copy) {
1460
0
    BLOSC_ERROR(b2nd_copy(ctx, src1, array));
1461
0
  }
1462
0
  else {
1463
0
    *array = (b2nd_array_t *)src1;
1464
0
  }
1465
1466
  // Extend the array, we don't need to specify the start in resize, as we are extending the shape from the end
1467
0
  BLOSC_ERROR(b2nd_resize(*array, newshape, NULL));
1468
1469
  // Copy the data from the second array
1470
0
  int64_t start[B2ND_MAX_DIM];
1471
0
  int64_t stop[B2ND_MAX_DIM];
1472
1473
  // Check if the chunk is aligned with dest chunks, and has the same blockshape
1474
0
  bool aligned = true;
1475
0
  for (int8_t i = 0; i < src2->ndim; ++i) {
1476
0
    if (src1->chunkshape[i] != src2->chunkshape[i] ||
1477
0
        src2->blockshape[i] != (*array)->blockshape[i] ||
1478
0
        (i == axis && (src1_shape[i]) % (*array)->chunkshape[i] != 0)
1479
0
        ) {
1480
0
      aligned = false;
1481
0
      break;
1482
0
        }
1483
0
  }
1484
  // ...and get the chunk index in the dest array if aligned
1485
0
  int64_t chunks_in_array_strides[B2ND_MAX_DIM];
1486
  // Calculate strides for destination array
1487
0
  chunks_in_array_strides[(*array)->ndim - 1] = 1;
1488
0
  for (int i = (*array)->ndim - 2; i >= 0; --i) {
1489
0
    chunks_in_array_strides[i] = chunks_in_array_strides[i + 1] *
1490
0
                                ((*array)->extshape[i + 1] / (*array)->chunkshape[i + 1]);
1491
0
  }
1492
1493
  // Copy chunk by chunk
1494
0
  void *buffer = malloc(src2->sc->typesize * src2->extchunknitems);
1495
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_MEMORY_ALLOC);
1496
0
  for (int64_t nchunk = 0; nchunk < src2->sc->nchunks; ++nchunk) {
1497
    // Get multidimensional chunk position
1498
0
    int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1499
0
    int64_t chunkshape[B2ND_MAX_DIM] = {0};
1500
0
    for (int8_t i = 0; i < src2->ndim; ++i) {
1501
0
      chunkshape[i] = src2->chunkshape[i];
1502
0
    }
1503
0
    int64_t chunks_in_dim[B2ND_MAX_DIM] = {0};
1504
0
    for (int8_t i = 0; i < src2->ndim; ++i) {
1505
0
      chunks_in_dim[i] = src2->extshape[i] / src2->chunkshape[i];
1506
0
    }
1507
0
    blosc2_unidim_to_multidim(src2->ndim, chunks_in_dim, nchunk, nchunk_ndim);
1508
1509
0
    if (aligned) {
1510
      // Get the uncompressed chunk buffer from the source array
1511
0
      bool needs_free = false;
1512
0
      uint8_t *chunk;
1513
0
      int32_t cbytes = blosc2_schunk_get_chunk(src2->sc, nchunk, &chunk, &needs_free);
1514
0
      if (cbytes < 0) {
1515
0
        BLOSC_TRACE_ERROR("Error getting chunk from source array");
1516
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1517
0
      }
1518
      // Update the chunk in the destination array
1519
      // We need to free only if needs_free is true or copy is false
1520
      // bool needs_copy = !needs_free || copy;
1521
      // BLOSC_ERROR(blosc2_schunk_update_chunk((*array)->sc, nchunk_dest, chunk, needs_copy));
1522
      // if (needs_free && !copy) {
1523
      //   free(chunk);
1524
      // }
1525
      // TODO: the above makes some tests to crash, so always force a copy; try to optimize this later
1526
0
      int64_t nchunk_dest = 0;
1527
0
      nchunk_ndim[axis] += src1_shape[axis] / (*array)->chunkshape[axis];
1528
0
      for ( int i =0; i< src2->ndim; i++) {
1529
0
        nchunk_dest += nchunk_ndim[i] * chunks_in_array_strides[i];
1530
0
      }
1531
0
      BLOSC_ERROR(blosc2_schunk_update_chunk((*array)->sc, nchunk_dest, chunk, true));
1532
0
      if (needs_free) {
1533
0
        free(chunk);
1534
0
      }
1535
0
    }
1536
0
    else {
1537
1538
      // Set positions for each dimension
1539
0
      for (int8_t i = 0; i < src2->ndim; ++i) {
1540
0
        start[i] = nchunk_ndim[i] * src2->chunkshape[i];
1541
0
        stop[i] = start[i] + src2->chunkshape[i];
1542
0
        if (stop[i] > src2->shape[i]) {
1543
0
          stop[i] = src2->shape[i];  // Handle boundary chunks
1544
0
        }
1545
0
      }
1546
      // Load chunk into buffer
1547
0
      BLOSC_ERROR(b2nd_get_slice_cbuffer(src2, start, stop, buffer, chunkshape, src2->sc->chunksize));
1548
1549
      // Apply chunk offset only for concatenation axis
1550
0
      start[axis] += src1_shape[axis];
1551
0
      stop[axis] += src1_shape[axis];
1552
1553
      // Copy the chunk to the correct position
1554
0
      BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, chunkshape,
1555
0
                                         src2->sc->typesize * src2->extchunknitems,
1556
0
                                         start, stop, *array));
1557
0
    }
1558
0
  }
1559
1560
0
  free(buffer);
1561
1562
0
  return BLOSC2_ERROR_SUCCESS;
1563
0
}
1564
1565
0
int b2nd_save(const b2nd_array_t *array, char *urlpath) {
1566
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1567
0
  BLOSC_ERROR_NULL(urlpath, BLOSC2_ERROR_NULL_POINTER);
1568
1569
0
  b2nd_array_t *tmp;
1570
0
  blosc2_storage b2_storage = BLOSC2_STORAGE_DEFAULTS;
1571
0
  b2nd_context_t params = {.b2_storage=&b2_storage};
1572
0
  b2_storage.urlpath = urlpath;
1573
0
  b2_storage.contiguous = array->sc->storage->contiguous;
1574
1575
0
  for (int i = 0; i < array->ndim; ++i) {
1576
0
    params.chunkshape[i] = array->chunkshape[i];
1577
0
    params.blockshape[i] = array->blockshape[i];
1578
0
  }
1579
1580
0
  BLOSC_ERROR(b2nd_copy(&params, array, &tmp));
1581
0
  BLOSC_ERROR(b2nd_free(tmp));
1582
1583
0
  return BLOSC2_ERROR_SUCCESS;
1584
0
}
1585
1586
0
int64_t b2nd_save_append(const b2nd_array_t *array, const char *urlpath) {
1587
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1588
0
  return blosc2_schunk_append_file(array->sc, urlpath);
1589
0
}
1590
1591
0
int b2nd_print_meta(const b2nd_array_t *array) {
1592
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1593
0
  int8_t ndim;
1594
0
  int64_t shape[B2ND_MAX_DIM];
1595
0
  int32_t chunkshape[B2ND_MAX_DIM];
1596
0
  int32_t blockshape[B2ND_MAX_DIM];
1597
0
  char *dtype;
1598
0
  int8_t dtype_format;
1599
0
  uint8_t *smeta;
1600
0
  int32_t smeta_len;
1601
0
  if (blosc2_meta_get(array->sc, "b2nd", &smeta, &smeta_len) < 0) {
1602
    // Try with a caterva metalayer; we are meant to be backward compatible with it
1603
0
    if (blosc2_meta_get(array->sc, "caterva", &smeta, &smeta_len) < 0) {
1604
0
      BLOSC_ERROR(BLOSC2_ERROR_METALAYER_NOT_FOUND);
1605
0
    }
1606
0
  }
1607
0
  BLOSC_ERROR(b2nd_deserialize_meta(smeta, smeta_len, &ndim, shape, chunkshape, blockshape,
1608
0
                                    &dtype, &dtype_format));
1609
0
  free(smeta);
1610
1611
0
  printf("b2nd metalayer parameters:\n Ndim:       %d", ndim);
1612
0
  printf("\n shape:      %" PRId64 "", shape[0]);
1613
0
  for (int i = 1; i < ndim; ++i) {
1614
0
    printf(", %" PRId64 "", shape[i]);
1615
0
  }
1616
0
  printf("\n chunkshape: %d", chunkshape[0]);
1617
0
  for (int i = 1; i < ndim; ++i) {
1618
0
    printf(", %d", chunkshape[i]);
1619
0
  }
1620
0
  if (dtype != NULL) {
1621
0
    printf("\n dtype: %s", dtype);
1622
0
    free(dtype);
1623
0
  }
1624
1625
0
  printf("\n blockshape: %d", blockshape[0]);
1626
0
  for (int i = 1; i < ndim; ++i) {
1627
0
    printf(", %d", blockshape[i]);
1628
0
  }
1629
0
  printf("\n");
1630
1631
0
  return BLOSC2_ERROR_SUCCESS;
1632
0
}
1633
1634
1635
0
int extend_shape(b2nd_array_t *array, const int64_t *new_shape, const int64_t *start) {
1636
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1637
0
  BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1638
1639
0
  int8_t ndim = array->ndim;
1640
0
  int64_t diffs_shape[B2ND_MAX_DIM];
1641
0
  int64_t diffs_sum = 0;
1642
0
  for (int i = 0; i < ndim; i++) {
1643
0
    diffs_shape[i] = new_shape[i] - array->shape[i];
1644
0
    diffs_sum += diffs_shape[i];
1645
0
    if (diffs_shape[i] < 0) {
1646
0
      BLOSC_TRACE_ERROR("The new shape must be greater than the old one");
1647
0
      BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1648
0
    }
1649
0
    if (array->shape[i] == INT64_MAX) {
1650
0
      BLOSC_TRACE_ERROR("Cannot extend array with shape[%d] = %" PRId64 "d", i, INT64_MAX);
1651
0
      BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1652
0
    }
1653
0
  }
1654
0
  if (diffs_sum == 0) {
1655
    // Shapes are equal. Do nothing.
1656
0
    return BLOSC2_ERROR_SUCCESS;
1657
0
  }
1658
1659
0
  int64_t old_nchunks = array->sc->nchunks;
1660
  // aux array to keep old shapes
1661
0
  b2nd_array_t *aux = malloc(sizeof(b2nd_array_t));
1662
0
  BLOSC_ERROR_NULL(aux, BLOSC2_ERROR_MEMORY_ALLOC);
1663
0
  aux->sc = NULL;
1664
0
  BLOSC_ERROR(update_shape(aux, ndim, array->shape, array->chunkshape, array->blockshape));
1665
1666
0
  BLOSC_ERROR(update_shape(array, ndim, new_shape, array->chunkshape, array->blockshape));
1667
1668
0
  int64_t nchunks = array->extnitems / array->chunknitems;
1669
0
  int64_t nchunks_;
1670
0
  int64_t nchunk_ndim[B2ND_MAX_DIM];
1671
0
  blosc2_cparams *cparams;
1672
0
  BLOSC_ERROR(blosc2_schunk_get_cparams(array->sc, &cparams));
1673
0
  void *chunk;
1674
0
  int64_t csize;
1675
0
  if (nchunks != old_nchunks) {
1676
0
    if (start == NULL) {
1677
0
      start = aux->shape;
1678
0
    }
1679
0
    int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
1680
0
    for (int i = 0; i < ndim; ++i) {
1681
0
      chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
1682
0
    }
1683
0
    for (int i = 0; i < nchunks; ++i) {
1684
0
      blosc2_unidim_to_multidim(ndim, chunks_in_array, i, nchunk_ndim);
1685
0
      for (int j = 0; j < ndim; ++j) {
1686
0
        if (start[j] <= (array->chunkshape[j] * nchunk_ndim[j])
1687
0
            && (array->chunkshape[j] * nchunk_ndim[j]) < (start[j] + new_shape[j] - aux->shape[j])) {
1688
0
          chunk = malloc(BLOSC_EXTENDED_HEADER_LENGTH);
1689
0
          BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
1690
0
          csize = blosc2_chunk_zeros(*cparams, array->sc->chunksize, chunk, BLOSC_EXTENDED_HEADER_LENGTH);
1691
0
          if (csize < 0) {
1692
0
            free(aux);
1693
0
            free(cparams);
1694
0
            BLOSC_TRACE_ERROR("Blosc error when creating a chunk");
1695
0
            return BLOSC2_ERROR_FAILURE;
1696
0
          }
1697
0
          nchunks_ = blosc2_schunk_insert_chunk(array->sc, i, chunk, false);
1698
0
          if (nchunks_ < 0) {
1699
0
            free(aux);
1700
0
            free(cparams);
1701
0
            BLOSC_TRACE_ERROR("Blosc error when inserting a chunk");
1702
0
            return BLOSC2_ERROR_FAILURE;
1703
0
          }
1704
0
          break;
1705
0
        }
1706
0
      }
1707
0
    }
1708
0
  }
1709
0
  free(aux);
1710
0
  free(cparams);
1711
1712
0
  return BLOSC2_ERROR_SUCCESS;
1713
0
}
1714
1715
1716
0
int shrink_shape(b2nd_array_t *array, const int64_t *new_shape, const int64_t *start) {
1717
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1718
0
  BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1719
1720
0
  int8_t ndim = array->ndim;
1721
0
  int64_t diffs_shape[B2ND_MAX_DIM];
1722
0
  int64_t diffs_sum = 0;
1723
0
  for (int i = 0; i < ndim; i++) {
1724
0
    diffs_shape[i] = new_shape[i] - array->shape[i];
1725
0
    diffs_sum += diffs_shape[i];
1726
0
    if (diffs_shape[i] > 0) {
1727
0
      BLOSC_TRACE_ERROR("The new shape must be smaller than the old one");
1728
0
      BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1729
0
    }
1730
0
    if (array->shape[i] == 0) {
1731
0
      continue;
1732
0
    }
1733
0
  }
1734
0
  if (diffs_sum == 0) {
1735
    // Shapes are equal. Do nothing.
1736
0
    return BLOSC2_ERROR_SUCCESS;
1737
0
  }
1738
1739
0
  int64_t old_nchunks = array->sc->nchunks;
1740
  // aux array to keep old shapes
1741
0
  b2nd_array_t *aux = malloc(sizeof(b2nd_array_t));
1742
0
  BLOSC_ERROR_NULL(aux, BLOSC2_ERROR_MEMORY_ALLOC);
1743
0
  aux->sc = NULL;
1744
0
  BLOSC_ERROR(update_shape(aux, ndim, array->shape, array->chunkshape, array->blockshape));
1745
1746
0
  BLOSC_ERROR(update_shape(array, ndim, new_shape, array->chunkshape, array->blockshape));
1747
1748
  // Delete chunks if needed
1749
0
  int64_t chunks_in_array_old[B2ND_MAX_DIM] = {0};
1750
0
  for (int i = 0; i < ndim; ++i) {
1751
0
    chunks_in_array_old[i] = aux->extshape[i] / aux->chunkshape[i];
1752
0
  }
1753
0
  if (start == NULL) {
1754
0
    start = new_shape;
1755
0
  }
1756
1757
0
  int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1758
0
  int64_t nchunks_;
1759
0
  for (int i = (int) old_nchunks - 1; i >= 0; --i) {
1760
0
    blosc2_unidim_to_multidim(ndim, chunks_in_array_old, i, nchunk_ndim);
1761
0
    for (int j = 0; j < ndim; ++j) {
1762
0
      if (start[j] <= (array->chunkshape[j] * nchunk_ndim[j])
1763
0
          && (array->chunkshape[j] * nchunk_ndim[j]) < (start[j] + aux->shape[j] - new_shape[j])) {
1764
0
        nchunks_ = blosc2_schunk_delete_chunk(array->sc, i);
1765
0
        if (nchunks_ < 0) {
1766
0
          free(aux);
1767
0
          BLOSC_TRACE_ERROR("Blosc error when deleting a chunk");
1768
0
          return BLOSC2_ERROR_FAILURE;
1769
0
        }
1770
0
        break;
1771
0
      }
1772
0
    }
1773
0
  }
1774
0
  free(aux);
1775
1776
0
  return BLOSC2_ERROR_SUCCESS;
1777
0
}
1778
1779
1780
int b2nd_resize(b2nd_array_t *array, const int64_t *new_shape,
1781
0
                const int64_t *start) {
1782
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1783
0
  BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1784
1785
0
  if (start != NULL) {
1786
0
    for (int i = 0; i < array->ndim; ++i) {
1787
0
      if (start[i] > array->shape[i]) {
1788
0
        BLOSC_TRACE_ERROR("`start` must be lower or equal than old array shape in all dims");
1789
0
        BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1790
0
      }
1791
0
      if ((new_shape[i] > array->shape[i] && start[i] != array->shape[i])
1792
0
          || (new_shape[i] < array->shape[i]
1793
0
              && (start[i] + array->shape[i] - new_shape[i]) != array->shape[i])) {
1794
        // Chunks cannot be cut unless they are in the last position
1795
0
        if (start[i] % array->chunkshape[i] != 0) {
1796
0
          BLOSC_TRACE_ERROR("If array end is not being modified "
1797
0
                              "`start` must be a multiple of chunkshape in all dims");
1798
0
          BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1799
0
        }
1800
0
        if ((new_shape[i] - array->shape[i]) % array->chunkshape[i] != 0) {
1801
0
          BLOSC_TRACE_ERROR("If array end is not being modified "
1802
0
                              "`(new_shape - shape)` must be multiple of chunkshape in all dims");
1803
0
          BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1804
0
        }
1805
0
      }
1806
0
    }
1807
0
  }
1808
1809
  // Get shrunk shape
1810
0
  int64_t shrunk_shape[B2ND_MAX_DIM] = {0};
1811
0
  for (int i = 0; i < array->ndim; ++i) {
1812
0
    if (new_shape[i] <= array->shape[i]) {
1813
0
      shrunk_shape[i] = new_shape[i];
1814
0
    } else {
1815
0
      shrunk_shape[i] = array->shape[i];
1816
0
    }
1817
0
  }
1818
1819
0
  BLOSC_ERROR(shrink_shape(array, shrunk_shape, start));
1820
0
  BLOSC_ERROR(extend_shape(array, new_shape, start));
1821
1822
0
  return BLOSC2_ERROR_SUCCESS;
1823
0
}
1824
1825
1826
int b2nd_insert(b2nd_array_t *array, const void *buffer, int64_t buffersize,
1827
0
                int8_t axis, int64_t insert_start) {
1828
1829
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1830
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1831
1832
0
  if (axis >= array->ndim) {
1833
0
    BLOSC_TRACE_ERROR("`axis` cannot be greater than the number of dimensions");
1834
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1835
0
  }
1836
1837
0
  int64_t axis_size = array->sc->typesize;
1838
0
  int64_t buffershape[B2ND_MAX_DIM];
1839
0
  for (int i = 0; i < array->ndim; ++i) {
1840
0
    if (i != axis) {
1841
0
      axis_size *= array->shape[i];
1842
0
      buffershape[i] = array->shape[i];
1843
0
    }
1844
0
  }
1845
0
  if (buffersize % axis_size != 0) {
1846
0
    BLOSC_TRACE_ERROR("`buffersize` must be multiple of the array");
1847
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1848
0
  }
1849
0
  int64_t newshape[B2ND_MAX_DIM];
1850
0
  memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1851
0
  newshape[axis] += buffersize / axis_size;
1852
0
  buffershape[axis] = newshape[axis] - array->shape[axis];
1853
0
  int64_t start[B2ND_MAX_DIM] = {0};
1854
0
  start[axis] = insert_start;
1855
1856
0
  if (insert_start == array->shape[axis]) {
1857
0
    BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1858
0
  } else {
1859
0
    BLOSC_ERROR(b2nd_resize(array, newshape, start));
1860
0
  }
1861
1862
0
  int64_t stop[B2ND_MAX_DIM];
1863
0
  memcpy(stop, array->shape, sizeof(int64_t) * array->ndim);
1864
0
  stop[axis] = start[axis] + buffershape[axis];
1865
0
  BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, buffershape, buffersize, start, stop, array));
1866
1867
0
  return BLOSC2_ERROR_SUCCESS;
1868
0
}
1869
1870
1871
int b2nd_append(b2nd_array_t *array, const void *buffer, int64_t buffersize,
1872
0
                int8_t axis) {
1873
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1874
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1875
1876
0
  int32_t chunksize = array->sc->chunksize;
1877
0
  int64_t nchunks_append = buffersize / chunksize;
1878
  // Check whether chunkshape and blockshape are compatible with accelerated path.
1879
  // Essentially, we are checking whether the buffer is a multiple of the chunksize
1880
  // and that the chunkshape and blockshape are the same, except for the first axis.
1881
  // Also, axis needs to be the first one.
1882
0
  bool compat_chunks_blocks = true;
1883
0
  for (int i = 1; i < array->ndim; ++i) {
1884
0
    if (array->chunkshape[i] != array->blockshape[i]) {
1885
0
      compat_chunks_blocks = false;
1886
0
      break;
1887
0
    }
1888
0
  }
1889
0
  if (axis > 0) {
1890
0
    compat_chunks_blocks = false;
1891
0
  }
1892
  // General case where a buffer has a different size than the chunksize
1893
0
  if (!compat_chunks_blocks || buffersize % chunksize != 0 || nchunks_append != 1) {
1894
0
    BLOSC_ERROR(b2nd_insert(array, buffer, buffersize, axis, array->shape[axis]));
1895
0
    return BLOSC2_ERROR_SUCCESS;
1896
0
  }
1897
1898
  // Accelerated path for buffers that are of the same size as the chunksize
1899
  // printf("accelerated path\n");
1900
1901
  // Append the buffer to the underlying schunk. This is very fast, as
1902
  // it doesn't need to do internal partitioning.
1903
0
  BLOSC_ERROR(blosc2_schunk_append_buffer(array->sc, (void*)buffer, buffersize));
1904
1905
  // Finally, resize the array
1906
0
  int64_t newshape[B2ND_MAX_DIM];
1907
0
  memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1908
0
  newshape[axis] += nchunks_append * array->chunkshape[axis];
1909
0
  BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1910
1911
0
  return BLOSC2_ERROR_SUCCESS;
1912
0
}
1913
1914
1915
int b2nd_delete(b2nd_array_t *array, const int8_t axis,
1916
0
                int64_t delete_start, int64_t delete_len) {
1917
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1918
1919
0
  if (axis >= array->ndim) {
1920
0
    BLOSC_TRACE_ERROR("axis cannot be greater than the number of dimensions");
1921
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1922
0
  }
1923
1924
1925
0
  int64_t newshape[B2ND_MAX_DIM];
1926
0
  memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1927
0
  newshape[axis] -= delete_len;
1928
0
  int64_t start[B2ND_MAX_DIM] = {0};
1929
0
  start[axis] = delete_start;
1930
1931
0
  if (delete_start == (array->shape[axis] - delete_len)) {
1932
0
    BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1933
0
  } else {
1934
0
    BLOSC_ERROR(b2nd_resize(array, newshape, start));
1935
0
  }
1936
1937
0
  return BLOSC2_ERROR_SUCCESS;
1938
0
}
1939
1940
// Indexing
1941
1942
typedef struct {
1943
    int64_t value;
1944
    int64_t index;
1945
} b2nd_selection_t;
1946
1947
1948
0
int compare_selection(const void *a, const void *b) {
1949
0
  int res = (int) (((b2nd_selection_t *) a)->value - ((b2nd_selection_t *) b)->value);
1950
  // In case values are equal, sort by index
1951
0
  if (res == 0) {
1952
0
    res = (int) (((b2nd_selection_t *) a)->index - ((b2nd_selection_t *) b)->index);
1953
0
  }
1954
0
  return res;
1955
0
}
1956
1957
1958
int copy_block_buffer_data(b2nd_array_t *array,
1959
                           int8_t ndim,
1960
                           int64_t *block_selection_size,
1961
                           b2nd_selection_t **chunk_selection,
1962
                           b2nd_selection_t **p_block_selection_0,
1963
                           b2nd_selection_t **p_block_selection_1,
1964
                           uint8_t *block,
1965
                           uint8_t *buffer,
1966
                           int64_t *buffershape,
1967
                           int64_t *bufferstrides,
1968
0
                           bool get) {
1969
0
  p_block_selection_0[ndim] = chunk_selection[ndim];
1970
0
  p_block_selection_1[ndim] = chunk_selection[ndim];
1971
0
  while (p_block_selection_1[ndim] - p_block_selection_0[ndim] < block_selection_size[ndim]) {
1972
0
    if (ndim == array->ndim - 1) {
1973
1974
0
      int64_t index_in_block_n[B2ND_MAX_DIM];
1975
0
      for (int i = 0; i < array->ndim; ++i) {
1976
0
        index_in_block_n[i] = p_block_selection_1[i]->value % array->chunkshape[i] % array->blockshape[i];
1977
0
      }
1978
0
      int64_t index_in_block = 0;
1979
0
      for (int i = 0; i < array->ndim; ++i) {
1980
0
        index_in_block += index_in_block_n[i] * array->item_block_strides[i];
1981
0
      }
1982
1983
0
      int64_t index_in_buffer_n[B2ND_MAX_DIM];
1984
0
      for (int i = 0; i < array->ndim; ++i) {
1985
0
        index_in_buffer_n[i] = p_block_selection_1[i]->index;
1986
0
      }
1987
0
      int64_t index_in_buffer = 0;
1988
0
      for (int i = 0; i < array->ndim; ++i) {
1989
0
        index_in_buffer += index_in_buffer_n[i] * bufferstrides[i];
1990
0
      }
1991
0
      if (get) {
1992
0
        memcpy(&buffer[index_in_buffer * array->sc->typesize],
1993
0
               &block[index_in_block * array->sc->typesize],
1994
0
               array->sc->typesize);
1995
0
      } else {
1996
0
        memcpy(&block[index_in_block * array->sc->typesize],
1997
0
               &buffer[index_in_buffer * array->sc->typesize],
1998
0
               array->sc->typesize);
1999
0
      }
2000
0
    } else {
2001
0
      BLOSC_ERROR(copy_block_buffer_data(array, (int8_t) (ndim + 1), block_selection_size,
2002
0
                                         chunk_selection,
2003
0
                                         p_block_selection_0, p_block_selection_1, block,
2004
0
                                         buffer, buffershape, bufferstrides, get)
2005
0
      );
2006
0
    }
2007
0
    p_block_selection_1[ndim]++;
2008
0
  }
2009
0
  return BLOSC2_ERROR_SUCCESS;
2010
0
}
2011
2012
2013
int iter_block_copy(b2nd_array_t *array, int8_t ndim,
2014
                    int64_t *chunk_selection_size,
2015
                    b2nd_selection_t **ordered_selection,
2016
                    b2nd_selection_t **chunk_selection_0,
2017
                    b2nd_selection_t **chunk_selection_1,
2018
                    uint8_t *data,
2019
                    uint8_t *buffer,
2020
                    int64_t *buffershape,
2021
                    int64_t *bufferstrides,
2022
0
                    bool get) {
2023
0
  chunk_selection_0[ndim] = ordered_selection[ndim];
2024
0
  chunk_selection_1[ndim] = ordered_selection[ndim];
2025
0
  while (chunk_selection_1[ndim] - ordered_selection[ndim] < chunk_selection_size[ndim]) {
2026
0
    int64_t block_index_ndim = ((*chunk_selection_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim];
2027
0
    while (chunk_selection_1[ndim] - ordered_selection[ndim] < chunk_selection_size[ndim] &&
2028
0
           block_index_ndim == ((*chunk_selection_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim]) {
2029
0
      chunk_selection_1[ndim]++;
2030
0
    }
2031
0
    if (ndim == array->ndim - 1) {
2032
0
      int64_t block_chunk_strides[B2ND_MAX_DIM];
2033
0
      block_chunk_strides[array->ndim - 1] = 1;
2034
0
      for (int i = array->ndim - 2; i >= 0; --i) {
2035
0
        block_chunk_strides[i] = block_chunk_strides[i + 1] * (array->extchunkshape[i + 1] / array->blockshape[i + 1]);
2036
0
      }
2037
0
      int64_t block_index[B2ND_MAX_DIM];
2038
0
      for (int i = 0; i < array->ndim; ++i) {
2039
0
        block_index[i] = ((*chunk_selection_0[i]).value % array->chunkshape[i]) / array->blockshape[i];
2040
0
      }
2041
0
      int64_t nblock = 0;
2042
0
      for (int i = 0; i < array->ndim; ++i) {
2043
0
        nblock += block_index[i] * block_chunk_strides[i];
2044
0
      }
2045
0
      b2nd_selection_t **p_block_selection_0 = malloc(array->ndim * sizeof(b2nd_selection_t *));
2046
0
      BLOSC_ERROR_NULL(p_block_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
2047
0
      b2nd_selection_t **p_block_selection_1 = malloc(array->ndim * sizeof(b2nd_selection_t *));
2048
0
      BLOSC_ERROR_NULL(p_block_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
2049
0
      int64_t *block_selection_size = malloc(array->ndim * sizeof(int64_t));
2050
0
      BLOSC_ERROR_NULL(block_selection_size, BLOSC2_ERROR_MEMORY_ALLOC);
2051
0
      for (int i = 0; i < array->ndim; ++i) {
2052
0
        block_selection_size[i] = chunk_selection_1[i] - chunk_selection_0[i];
2053
0
      }
2054
2055
0
      BLOSC_ERROR(copy_block_buffer_data(array,
2056
0
                                         (int8_t) 0,
2057
0
                                         block_selection_size,
2058
0
                                         chunk_selection_0,
2059
0
                                         p_block_selection_0,
2060
0
                                         p_block_selection_1,
2061
0
                                         &data[nblock * array->blocknitems * array->sc->typesize],
2062
0
                                         buffer,
2063
0
                                         buffershape,
2064
0
                                         bufferstrides,
2065
0
                                         get)
2066
0
      );
2067
0
      free(p_block_selection_0);
2068
0
      free(p_block_selection_1);
2069
0
      free(block_selection_size);
2070
0
    } else {
2071
0
      BLOSC_ERROR(iter_block_copy(array, (int8_t) (ndim + 1), chunk_selection_size,
2072
0
                                  ordered_selection, chunk_selection_0, chunk_selection_1,
2073
0
                                  data, buffer, buffershape, bufferstrides, get)
2074
0
      );
2075
0
    }
2076
0
    chunk_selection_0[ndim] = chunk_selection_1[ndim];
2077
2078
0
  }
2079
2080
0
  return BLOSC2_ERROR_SUCCESS;
2081
0
}
2082
2083
2084
int iter_block_maskout(b2nd_array_t *array, int8_t ndim,
2085
                       int64_t *sel_block_size,
2086
                       b2nd_selection_t **o_selection,
2087
                       b2nd_selection_t **p_o_sel_block_0,
2088
                       b2nd_selection_t **p_o_sel_block_1,
2089
0
                       bool *maskout) {
2090
0
  p_o_sel_block_0[ndim] = o_selection[ndim];
2091
0
  p_o_sel_block_1[ndim] = o_selection[ndim];
2092
0
  while (p_o_sel_block_1[ndim] - o_selection[ndim] < sel_block_size[ndim]) {
2093
0
    int64_t block_index_ndim = ((*p_o_sel_block_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim];
2094
0
    while (p_o_sel_block_1[ndim] - o_selection[ndim] < sel_block_size[ndim] &&
2095
0
           block_index_ndim == ((*p_o_sel_block_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim]) {
2096
0
      p_o_sel_block_1[ndim]++;
2097
0
    }
2098
0
    if (ndim == array->ndim - 1) {
2099
0
      int64_t block_chunk_strides[B2ND_MAX_DIM];
2100
0
      block_chunk_strides[array->ndim - 1] = 1;
2101
0
      for (int i = array->ndim - 2; i >= 0; --i) {
2102
0
        block_chunk_strides[i] = block_chunk_strides[i + 1] * (array->extchunkshape[i + 1] / array->blockshape[i + 1]);
2103
0
      }
2104
0
      int64_t block_index[B2ND_MAX_DIM];
2105
0
      for (int i = 0; i < array->ndim; ++i) {
2106
0
        block_index[i] = ((*p_o_sel_block_0[i]).value % array->chunkshape[i]) / array->blockshape[i];
2107
0
      }
2108
0
      int64_t nblock = 0;
2109
0
      for (int i = 0; i < array->ndim; ++i) {
2110
0
        nblock += block_index[i] * block_chunk_strides[i];
2111
0
      }
2112
0
      maskout[nblock] = false;
2113
0
    } else {
2114
0
      BLOSC_ERROR(iter_block_maskout(array, (int8_t) (ndim + 1), sel_block_size,
2115
0
                                     o_selection, p_o_sel_block_0, p_o_sel_block_1,
2116
0
                                     maskout)
2117
0
      );
2118
0
    }
2119
0
    p_o_sel_block_0[ndim] = p_o_sel_block_1[ndim];
2120
2121
0
  }
2122
2123
0
  return BLOSC2_ERROR_SUCCESS;
2124
0
}
2125
2126
2127
int iter_chunk(b2nd_array_t *array, int8_t ndim,
2128
               int64_t *selection_size,
2129
               b2nd_selection_t **ordered_selection,
2130
               b2nd_selection_t **p_ordered_selection_0,
2131
               b2nd_selection_t **p_ordered_selection_1,
2132
               uint8_t *buffer,
2133
               int64_t *buffershape,
2134
               int64_t *bufferstrides,
2135
0
               bool get) {
2136
0
  p_ordered_selection_0[ndim] = ordered_selection[ndim];
2137
0
  p_ordered_selection_1[ndim] = ordered_selection[ndim];
2138
0
  while (p_ordered_selection_1[ndim] - ordered_selection[ndim] < selection_size[ndim]) {
2139
0
    int64_t chunk_index_ndim = (*p_ordered_selection_1[ndim]).value / array->chunkshape[ndim];
2140
0
    while (p_ordered_selection_1[ndim] - ordered_selection[ndim] < selection_size[ndim] &&
2141
0
           chunk_index_ndim == (*p_ordered_selection_1[ndim]).value / array->chunkshape[ndim]) {
2142
0
      p_ordered_selection_1[ndim]++;
2143
0
    }
2144
0
    if (ndim == array->ndim - 1) {
2145
0
      int64_t chunk_array_strides[B2ND_MAX_DIM];
2146
0
      chunk_array_strides[array->ndim - 1] = 1;
2147
0
      for (int i = array->ndim - 2; i >= 0; --i) {
2148
0
        chunk_array_strides[i] = chunk_array_strides[i + 1] *
2149
0
                                 (array->extshape[i + 1] / array->chunkshape[i + 1]);
2150
0
      }
2151
0
      int64_t chunk_index[B2ND_MAX_DIM];
2152
0
      for (int i = 0; i < array->ndim; ++i) {
2153
0
        chunk_index[i] = (*p_ordered_selection_0[i]).value / array->chunkshape[i];
2154
0
      }
2155
0
      int64_t nchunk = 0;
2156
0
      for (int i = 0; i < array->ndim; ++i) {
2157
0
        nchunk += chunk_index[i] * chunk_array_strides[i];
2158
0
      }
2159
2160
0
      int64_t nblocks = array->extchunknitems / array->blocknitems;
2161
0
      b2nd_selection_t **p_chunk_selection_0 = malloc(array->ndim * sizeof(b2nd_selection_t *));
2162
0
      BLOSC_ERROR_NULL(p_chunk_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
2163
0
      b2nd_selection_t **p_chunk_selection_1 = malloc(array->ndim * sizeof(b2nd_selection_t *));
2164
0
      BLOSC_ERROR_NULL(p_chunk_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
2165
0
      int64_t *chunk_selection_size = malloc(array->ndim * sizeof(int64_t));
2166
0
      BLOSC_ERROR_NULL(chunk_selection_size, BLOSC2_ERROR_MEMORY_ALLOC);
2167
0
      for (int i = 0; i < array->ndim; ++i) {
2168
0
        chunk_selection_size[i] = p_ordered_selection_1[i] - p_ordered_selection_0[i];
2169
0
      }
2170
2171
0
      if (get) {
2172
0
        bool *maskout = calloc(nblocks, sizeof(bool));
2173
0
        for (int i = 0; i < nblocks; ++i) {
2174
0
          maskout[i] = true;
2175
0
        }
2176
2177
0
        BLOSC_ERROR(iter_block_maskout(array, (int8_t) 0,
2178
0
                                       chunk_selection_size,
2179
0
                                       p_ordered_selection_0,
2180
0
                                       p_chunk_selection_0,
2181
0
                                       p_chunk_selection_1,
2182
0
                                       maskout));
2183
2184
0
        if (blosc2_set_maskout(array->sc->dctx, maskout, (int) nblocks) !=
2185
0
            BLOSC2_ERROR_SUCCESS) {
2186
0
          BLOSC_TRACE_ERROR("Error setting the maskout");
2187
0
          BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
2188
0
        }
2189
0
        free(maskout);
2190
0
      }
2191
0
      int data_nitems = (int) array->extchunknitems;
2192
0
      int data_nbytes = data_nitems * array->sc->typesize;
2193
0
      uint8_t *data = malloc(data_nitems * array->sc->typesize);
2194
0
      BLOSC_ERROR_NULL(data, BLOSC2_ERROR_MEMORY_ALLOC);
2195
0
      int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
2196
0
      if (err < 0) {
2197
0
        BLOSC_TRACE_ERROR("Error decompressing chunk");
2198
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
2199
0
      }
2200
0
      BLOSC_ERROR(iter_block_copy(array, 0, chunk_selection_size,
2201
0
                                  p_ordered_selection_0, p_chunk_selection_0, p_chunk_selection_1,
2202
0
                                  data, buffer, buffershape, bufferstrides, get));
2203
2204
0
      if (!get) {
2205
0
        int32_t chunk_size = data_nbytes + BLOSC_EXTENDED_HEADER_LENGTH;
2206
0
        uint8_t *chunk = malloc(chunk_size);
2207
0
        BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
2208
0
        err = blosc2_compress_ctx(array->sc->cctx, data, data_nbytes, chunk, chunk_size);
2209
0
        if (err < 0) {
2210
0
          BLOSC_TRACE_ERROR("Error compressing data");
2211
0
          BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
2212
0
        }
2213
0
        err = (int) blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
2214
0
        if (err < 0) {
2215
0
          BLOSC_TRACE_ERROR("Error updating chunk");
2216
0
          BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
2217
0
        }
2218
0
      }
2219
0
      free(data);
2220
0
      free(chunk_selection_size);
2221
0
      free(p_chunk_selection_0);
2222
0
      free(p_chunk_selection_1);
2223
0
    } else {
2224
0
      BLOSC_ERROR(iter_chunk(array, (int8_t) (ndim + 1), selection_size,
2225
0
                             ordered_selection, p_ordered_selection_0, p_ordered_selection_1,
2226
0
                             buffer, buffershape, bufferstrides, get));
2227
0
    }
2228
2229
0
    p_ordered_selection_0[ndim] = p_ordered_selection_1[ndim];
2230
0
  }
2231
0
  return BLOSC2_ERROR_SUCCESS;
2232
0
}
2233
2234
2235
int orthogonal_selection(b2nd_array_t *array, int64_t **selection, int64_t *selection_size, void *buffer,
2236
0
                         int64_t *buffershape, int64_t buffersize, bool get) {
2237
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
2238
0
  BLOSC_ERROR_NULL(selection, BLOSC2_ERROR_NULL_POINTER);
2239
0
  BLOSC_ERROR_NULL(selection_size, BLOSC2_ERROR_NULL_POINTER);
2240
2241
0
  int8_t ndim = array->ndim;
2242
2243
0
  for (int i = 0; i < ndim; ++i) {
2244
0
    BLOSC_ERROR_NULL(selection[i], BLOSC2_ERROR_NULL_POINTER);
2245
    // Check that indexes are not larger than array shape
2246
0
    for (int j = 0; j < selection_size[i]; ++j) {
2247
0
      if (selection[i][j] > array->shape[i]) {
2248
0
        BLOSC_ERROR(BLOSC2_ERROR_INVALID_INDEX);
2249
0
      }
2250
0
    }
2251
0
  }
2252
2253
  // Check buffer size
2254
0
  int64_t sel_size = array->sc->typesize;
2255
0
  for (int i = 0; i < ndim; ++i) {
2256
0
    sel_size *= selection_size[i];
2257
0
  }
2258
2259
0
  if (sel_size < buffersize) {
2260
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
2261
0
  }
2262
2263
  // Sort selections
2264
0
  b2nd_selection_t **ordered_selection = malloc(ndim * sizeof(b2nd_selection_t *));
2265
0
  BLOSC_ERROR_NULL(ordered_selection, BLOSC2_ERROR_MEMORY_ALLOC);
2266
0
  for (int i = 0; i < ndim; ++i) {
2267
0
    ordered_selection[i] = malloc(selection_size[i] * sizeof(b2nd_selection_t));
2268
0
    for (int j = 0; j < selection_size[i]; ++j) {
2269
0
      ordered_selection[i][j].index = j;
2270
0
      ordered_selection[i][j].value = selection[i][j];
2271
0
    }
2272
0
    qsort(ordered_selection[i], selection_size[i], sizeof(b2nd_selection_t), compare_selection);
2273
0
  }
2274
2275
  // Define pointers to iterate over ordered_selection data
2276
0
  b2nd_selection_t **p_ordered_selection_0 = malloc(ndim * sizeof(b2nd_selection_t *));
2277
0
  BLOSC_ERROR_NULL(p_ordered_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
2278
0
  b2nd_selection_t **p_ordered_selection_1 = malloc(ndim * sizeof(b2nd_selection_t *));
2279
0
  BLOSC_ERROR_NULL(p_ordered_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
2280
2281
0
  int64_t bufferstrides[B2ND_MAX_DIM];
2282
0
  bufferstrides[array->ndim - 1] = 1;
2283
0
  for (int i = array->ndim - 2; i >= 0; --i) {
2284
0
    bufferstrides[i] = bufferstrides[i + 1] * buffershape[i + 1];
2285
0
  }
2286
2287
0
  BLOSC_ERROR(iter_chunk(array, 0,
2288
0
                         selection_size, ordered_selection,
2289
0
                         p_ordered_selection_0,
2290
0
                         p_ordered_selection_1,
2291
0
                         buffer, buffershape, bufferstrides, get));
2292
2293
  // Free allocated memory
2294
0
  free(p_ordered_selection_0);
2295
0
  free(p_ordered_selection_1);
2296
0
  for (int i = 0; i < ndim; ++i) {
2297
0
    free(ordered_selection[i]);
2298
0
  }
2299
0
  free(ordered_selection);
2300
2301
0
  return BLOSC2_ERROR_SUCCESS;
2302
0
}
2303
2304
2305
int b2nd_get_orthogonal_selection(const b2nd_array_t *array, int64_t **selection, int64_t *selection_size, void *buffer,
2306
0
                                  int64_t *buffershape, int64_t buffersize) {
2307
0
  return orthogonal_selection((b2nd_array_t *)array, selection, selection_size, buffer, buffershape, buffersize, true);
2308
0
}
2309
2310
2311
int b2nd_set_orthogonal_selection(b2nd_array_t *array, int64_t **selection, int64_t *selection_size, const void *buffer,
2312
0
                                  int64_t *buffershape, int64_t buffersize) {
2313
0
  return orthogonal_selection(array, selection, selection_size, (void*)buffer, buffershape, buffersize, false);
2314
0
}
2315
2316
2317
b2nd_context_t *
2318
b2nd_create_ctx(const blosc2_storage *b2_storage, int8_t ndim, const int64_t *shape, const int32_t *chunkshape,
2319
                const int32_t *blockshape, const char *dtype, int8_t dtype_format, const blosc2_metalayer *metalayers,
2320
0
                int32_t nmetalayers) {
2321
0
  b2nd_context_t *ctx = malloc(sizeof(b2nd_context_t));
2322
0
  BLOSC_ERROR_NULL(ctx, NULL);
2323
0
  blosc2_storage *params_b2_storage = malloc(sizeof(blosc2_storage));
2324
0
  BLOSC_ERROR_NULL(params_b2_storage, NULL);
2325
0
  if (b2_storage == NULL) {
2326
0
    memcpy(params_b2_storage, &BLOSC2_STORAGE_DEFAULTS, sizeof(blosc2_storage));
2327
0
  }
2328
0
  else {
2329
0
    memcpy(params_b2_storage, b2_storage, sizeof(blosc2_storage));
2330
0
  }
2331
0
  blosc2_cparams *cparams = malloc(sizeof(blosc2_cparams));
2332
0
  BLOSC_ERROR_NULL(cparams, NULL);
2333
  // We need a copy of cparams mainly to be able to modify blocksize
2334
0
  if (params_b2_storage->cparams == NULL) {
2335
0
    memcpy(cparams, &BLOSC2_CPARAMS_DEFAULTS, sizeof(blosc2_cparams));
2336
0
  }
2337
0
  else {
2338
0
    memcpy(cparams, params_b2_storage->cparams, sizeof(blosc2_cparams));
2339
0
  }
2340
2341
0
  if (dtype == NULL) {
2342
    // ctx->dtype = strdup(B2ND_DEFAULT_DTYPE);
2343
0
    char buf[16] = {0};
2344
0
    snprintf(buf, sizeof(buf), "|S%d", cparams->typesize);
2345
0
    ctx->dtype = strdup(buf);
2346
0
  }
2347
0
  else {
2348
0
    ctx->dtype = strdup(dtype);
2349
0
  }
2350
0
  ctx->dtype_format = dtype_format;
2351
2352
0
  params_b2_storage->cparams = cparams;
2353
0
  ctx->b2_storage = params_b2_storage;
2354
0
  ctx->ndim = ndim;
2355
0
  int32_t blocknitems = 1;
2356
0
  for (int i = 0; i < ndim; i++) {
2357
0
    ctx->shape[i] = shape[i];
2358
0
    ctx->chunkshape[i] = chunkshape[i];
2359
0
    ctx->blockshape[i] = blockshape[i];
2360
0
    blocknitems *= ctx->blockshape[i];
2361
0
  }
2362
0
  cparams->blocksize = blocknitems * cparams->typesize;
2363
2364
0
  ctx->nmetalayers = nmetalayers;
2365
0
  for (int i = 0; i < nmetalayers; ++i) {
2366
0
    ctx->metalayers[i] = metalayers[i];
2367
0
  }
2368
2369
#if defined(HAVE_PLUGINS)
2370
  #include "blosc2/codecs-registry.h"
2371
  if ((ctx->b2_storage->cparams->compcode >= BLOSC_CODEC_ZFP_FIXED_ACCURACY) &&
2372
      (ctx->b2_storage->cparams->compcode <= BLOSC_CODEC_ZFP_FIXED_RATE)) {
2373
    for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) {
2374
      if ((ctx->b2_storage->cparams->filters[i] == BLOSC_SHUFFLE) ||
2375
          (ctx->b2_storage->cparams->filters[i] == BLOSC_BITSHUFFLE)) {
2376
        BLOSC_TRACE_ERROR("ZFP cannot be run in presence of SHUFFLE / BITSHUFFLE");
2377
        return NULL;
2378
      }
2379
    }
2380
  }
2381
#endif /* HAVE_PLUGINS */
2382
2383
0
  return ctx;
2384
0
}
2385
2386
2387
0
int b2nd_free_ctx(b2nd_context_t *ctx) {
2388
0
  ctx->b2_storage->cparams->schunk = NULL;
2389
0
  free(ctx->b2_storage->cparams);
2390
0
  free(ctx->b2_storage);
2391
0
  free(ctx->dtype);
2392
0
  free(ctx);
2393
2394
0
  return BLOSC2_ERROR_SUCCESS;
2395
0
}