Coverage Report

Created: 2025-10-28 06:12

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 (shape[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-dim case
620
0
  if (ndim == 0) {
621
0
    *chunks_idx = malloc(1 * sizeof(int64_t));
622
0
    *chunks_idx[0] = 0;
623
0
    return 1;
624
0
  }
625
626
0
  int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
627
0
  for (int i = 0; i < ndim; ++i) {
628
0
    chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
629
0
  }
630
631
0
  int64_t chunks_in_array_strides[B2ND_MAX_DIM];
632
0
  chunks_in_array_strides[ndim - 1] = 1;
633
0
  for (int i = ndim - 2; i >= 0; --i) {
634
0
    chunks_in_array_strides[i] = chunks_in_array_strides[i + 1] * chunks_in_array[i + 1];
635
0
  }
636
637
  // Compute the number of chunks to update
638
0
  int64_t update_start[B2ND_MAX_DIM];
639
0
  int64_t update_shape[B2ND_MAX_DIM];
640
641
0
  int64_t update_nchunks = 1;
642
0
  for (int i = 0; i < ndim; ++i) {
643
0
    int64_t pos = 0;
644
0
    while (pos <= start[i]) {
645
0
      pos += array->chunkshape[i];
646
0
    }
647
0
    update_start[i] = pos / array->chunkshape[i] - 1;
648
0
    while (pos < stop[i]) {
649
0
      pos += array->chunkshape[i];
650
0
    }
651
0
    update_shape[i] = pos / array->chunkshape[i] - update_start[i];
652
0
    update_nchunks *= update_shape[i];
653
0
  }
654
655
0
  int nchunks = 0;
656
  // Initially we do not know the number of chunks that will be affected
657
0
  *chunks_idx = malloc(array->sc->nchunks * sizeof(int64_t));
658
0
  int64_t *ptr = *chunks_idx;
659
0
  for (int update_nchunk = 0; update_nchunk < update_nchunks; ++update_nchunk) {
660
0
    int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
661
0
    blosc2_unidim_to_multidim(ndim, update_shape, update_nchunk, nchunk_ndim);
662
0
    for (int i = 0; i < ndim; ++i) {
663
0
      nchunk_ndim[i] += update_start[i];
664
0
    }
665
0
    int64_t nchunk;
666
0
    blosc2_multidim_to_unidim(nchunk_ndim, ndim, chunks_in_array_strides, &nchunk);
667
668
    // Check if the chunk is inside the slice domain
669
0
    int64_t chunk_start[B2ND_MAX_DIM] = {0};
670
0
    int64_t chunk_stop[B2ND_MAX_DIM] = {0};
671
0
    for (int i = 0; i < ndim; ++i) {
672
0
      chunk_start[i] = nchunk_ndim[i] * array->chunkshape[i];
673
0
      chunk_stop[i] = chunk_start[i] + array->chunkshape[i];
674
0
      if (chunk_stop[i] > array->shape[i]) {
675
0
        chunk_stop[i] = array->shape[i];
676
0
      }
677
0
    }
678
0
    bool chunk_empty = false;
679
0
    for (int i = 0; i < ndim; ++i) {
680
0
      chunk_empty |= (chunk_stop[i] <= start[i] || chunk_start[i] >= stop[i]);
681
0
    }
682
0
    if (chunk_empty) {
683
0
      continue;
684
0
    }
685
686
0
    ptr[nchunks] = nchunk;
687
0
    nchunks++;
688
0
  }
689
690
0
  if (nchunks < array->sc->nchunks) {
691
0
    *chunks_idx = realloc(ptr, nchunks * sizeof(int64_t));
692
0
  }
693
694
0
  return nchunks;
695
0
}
696
697
698
// Check whether the slice defined by start and stop is a single chunk and contiguous
699
// in the C order. This is a fast path for the get_slice and set_slice functions.
700
int64_t nchunk_fastpath(const b2nd_array_t *array, const int64_t *start,
701
0
                        const int64_t *stop, const int64_t slice_size) {
702
0
  if (slice_size != array->chunknitems) {
703
0
    return -1;
704
0
  }
705
706
0
  int ndim = (int) array->ndim;
707
708
0
  int k = 0;
709
0
  for (int i = 0; i < ndim; ++i) {
710
    // The slice needs to correspond to a whole chunk (without padding)
711
0
    if (start[i] % array->chunkshape[i] != 0) {
712
0
      return -1;
713
0
    }
714
0
    if (stop[i] - start[i] != array->chunkshape[i]) {
715
0
      return -1;
716
0
    }
717
718
    // There needs to exist 0 <= k <= ndim such that:
719
    // - for i < k, blockshape[i] == 1
720
    // - for i == k, blockshape[i] divides chunkshape[i]
721
    // - for i > k, blockshape[i] == chunkshape[i]
722
0
    if (array->chunkshape[i] % array->blockshape[i] != 0) {
723
0
      return -1;
724
0
    }
725
0
    if (i > k && array->chunkshape[i] != array->blockshape[i]) {
726
0
      return -1;
727
0
    }
728
0
    if (i == k && array->blockshape[i] == 1) {
729
0
      k++;
730
0
    }
731
0
  }
732
  // Compute the chunk number
733
0
  int64_t *chunks_idx;
734
0
  int nchunks = b2nd_get_slice_nchunks(array, start, stop, &chunks_idx);
735
0
  if (nchunks != 1) {
736
0
    free(chunks_idx);
737
0
    BLOSC_TRACE_ERROR("The number of chunks to read is not 1; go fix the code");
738
0
    BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
739
0
  }
740
0
  int64_t nchunk = chunks_idx[0];
741
0
  free(chunks_idx);
742
743
0
  return nchunk;
744
0
}
745
746
747
// Setting and getting slices
748
int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const int64_t *stop,
749
0
                  const int64_t *shape, b2nd_array_t *array, bool set_slice) {
750
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
751
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
752
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
753
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
754
0
  if (buffersize < 0) {
755
0
    BLOSC_TRACE_ERROR("buffersize is < 0");
756
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
757
0
  }
758
759
0
  uint8_t *buffer_b = buffer;
760
0
  int8_t ndim = array->ndim;
761
762
  // 0-dim case
763
0
  if (ndim == 0) {
764
0
    if (set_slice) {
765
0
      int32_t chunk_size = array->sc->typesize + BLOSC2_MAX_OVERHEAD;
766
0
      uint8_t *chunk = malloc(chunk_size);
767
0
      BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
768
0
      if (blosc2_compress_ctx(array->sc->cctx, buffer_b, array->sc->typesize, chunk, chunk_size) < 0) {
769
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
770
0
      }
771
0
      if (blosc2_schunk_update_chunk(array->sc, 0, chunk, false) < 0) {
772
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
773
0
      }
774
775
0
    } else {
776
0
      if (blosc2_schunk_decompress_chunk(array->sc, 0, buffer_b, array->sc->typesize) < 0) {
777
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
778
0
      }
779
0
    }
780
0
    return BLOSC2_ERROR_SUCCESS;
781
0
  }
782
783
0
  if (array->nitems == 0) {
784
0
    return BLOSC2_ERROR_SUCCESS;
785
0
  }
786
787
0
  int64_t nelems_slice = 1;
788
0
  for (int i = 0; i < array->ndim; ++i) {
789
0
    if (stop[i] - start[i] > shape[i]) {
790
0
      BLOSC_TRACE_ERROR("The buffer shape can not be smaller than the slice shape");
791
0
      return BLOSC2_ERROR_INVALID_PARAM;
792
0
    }
793
0
    nelems_slice *= stop[i] - start[i];
794
0
  }
795
0
  int64_t slice_nbytes = nelems_slice * array->sc->typesize;
796
0
  int32_t data_nbytes = (int32_t) array->extchunknitems * array->sc->typesize;
797
798
0
  if (buffersize < slice_nbytes) {
799
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
800
0
  }
801
802
  // Check for fast path for aligned slices with chunks and blocks (only 1 chunk is supported)
803
0
  int64_t nchunk = nchunk_fastpath(array, start, stop, nelems_slice);
804
0
  if (nchunk >= 0) {
805
0
    if (set_slice) {
806
      // Fast path for set. Let's set the chunk buffer straight into the array.
807
      // Compress the chunk
808
0
      int32_t chunk_nbytes = data_nbytes + BLOSC2_MAX_OVERHEAD;
809
0
      uint8_t *chunk = malloc(chunk_nbytes);
810
0
      BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
811
0
      int brc;
812
      // Update current_chunk in case a prefilter is applied
813
0
      array->sc->current_nchunk = nchunk;
814
0
      brc = blosc2_compress_ctx(array->sc->cctx, buffer, data_nbytes, chunk, chunk_nbytes);
815
0
      if (brc < 0) {
816
0
        BLOSC_TRACE_ERROR("Blosc can not compress the data");
817
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
818
0
      }
819
0
      int64_t brc_ = blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
820
0
      if (brc_ < 0) {
821
0
        BLOSC_TRACE_ERROR("Blosc can not update the chunk");
822
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
823
0
      }
824
      // We are done
825
0
      return BLOSC2_ERROR_SUCCESS;
826
0
    }
827
0
    else {
828
      // Fast path for get. Let's read the chunk straight into the buffer.
829
0
      if (blosc2_schunk_decompress_chunk(array->sc, nchunk, buffer, (int32_t) slice_nbytes) < 0) {
830
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
831
0
      }
832
0
      return BLOSC2_ERROR_SUCCESS;
833
0
    }
834
0
  }
835
836
  // Slow path for set and get
837
838
0
  uint8_t *data = malloc(data_nbytes);
839
0
  BLOSC_ERROR_NULL(data, BLOSC2_ERROR_MEMORY_ALLOC);
840
841
0
  int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
842
0
  for (int i = 0; i < ndim; ++i) {
843
0
    chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
844
0
  }
845
846
0
  int64_t chunks_in_array_strides[B2ND_MAX_DIM];
847
0
  chunks_in_array_strides[ndim - 1] = 1;
848
0
  for (int i = ndim - 2; i >= 0; --i) {
849
0
    chunks_in_array_strides[i] = chunks_in_array_strides[i + 1] * chunks_in_array[i + 1];
850
0
  }
851
852
0
  int64_t blocks_in_chunk[B2ND_MAX_DIM] = {0};
853
0
  for (int i = 0; i < ndim; ++i) {
854
0
    blocks_in_chunk[i] = array->extchunkshape[i] / array->blockshape[i];
855
0
  }
856
857
  // Compute the number of chunks to update
858
0
  int64_t update_start[B2ND_MAX_DIM];
859
0
  int64_t update_shape[B2ND_MAX_DIM];
860
861
0
  int64_t update_nchunks = 1;
862
0
  for (int i = 0; i < ndim; ++i) {
863
0
    int64_t pos = 0;
864
0
    while (pos <= start[i]) {
865
0
      pos += array->chunkshape[i];
866
0
    }
867
0
    update_start[i] = pos / array->chunkshape[i] - 1;
868
0
    while (pos < stop[i]) {
869
0
      pos += array->chunkshape[i];
870
0
    }
871
0
    update_shape[i] = pos / array->chunkshape[i] - update_start[i];
872
0
    update_nchunks *= update_shape[i];
873
0
  }
874
875
0
  for (int update_nchunk = 0; update_nchunk < update_nchunks; ++update_nchunk) {
876
0
    int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
877
0
    blosc2_unidim_to_multidim(ndim, update_shape, update_nchunk, nchunk_ndim);
878
0
    for (int i = 0; i < ndim; ++i) {
879
0
      nchunk_ndim[i] += update_start[i];
880
0
    }
881
0
    int64_t nchunk;
882
0
    blosc2_multidim_to_unidim(nchunk_ndim, ndim, chunks_in_array_strides, &nchunk);
883
884
    // Check if the chunk needs to be updated
885
0
    int64_t chunk_start[B2ND_MAX_DIM] = {0};
886
0
    int64_t chunk_stop[B2ND_MAX_DIM] = {0};
887
0
    for (int i = 0; i < ndim; ++i) {
888
0
      chunk_start[i] = nchunk_ndim[i] * array->chunkshape[i];
889
0
      chunk_stop[i] = chunk_start[i] + array->chunkshape[i];
890
0
      if (chunk_stop[i] > array->shape[i]) {
891
0
        chunk_stop[i] = array->shape[i];
892
0
      }
893
0
    }
894
0
    bool chunk_empty = false;
895
0
    for (int i = 0; i < ndim; ++i) {
896
0
      chunk_empty |= (chunk_stop[i] <= start[i] || chunk_start[i] >= stop[i]);
897
0
    }
898
0
    if (chunk_empty) {
899
0
      continue;
900
0
    }
901
902
0
    int32_t nblocks = (int32_t) array->extchunknitems / array->blocknitems;
903
0
    if (set_slice) {
904
      // Check if all the chunk is going to be updated and avoid the decompression
905
0
      bool decompress_chunk = false;
906
0
      for (int i = 0; i < ndim; ++i) {
907
0
        decompress_chunk |= (chunk_start[i] < start[i] || chunk_stop[i] > stop[i]);
908
0
      }
909
910
0
      if (decompress_chunk) {
911
0
        int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
912
0
        if (err < 0) {
913
0
          BLOSC_TRACE_ERROR("Error decompressing chunk");
914
0
          BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
915
0
        }
916
0
      } else {
917
        // Avoid writing non zero padding from previous chunk
918
0
        memset(data, 0, data_nbytes);
919
0
      }
920
0
    } else {
921
0
      bool *block_maskout = malloc(nblocks);
922
0
      BLOSC_ERROR_NULL(block_maskout, BLOSC2_ERROR_MEMORY_ALLOC);
923
0
      for (int nblock = 0; nblock < nblocks; ++nblock) {
924
0
        int64_t nblock_ndim[B2ND_MAX_DIM] = {0};
925
0
        blosc2_unidim_to_multidim(ndim, blocks_in_chunk, nblock, nblock_ndim);
926
927
        // Check if the block needs to be updated
928
0
        int64_t block_start[B2ND_MAX_DIM] = {0};
929
0
        int64_t block_stop[B2ND_MAX_DIM] = {0};
930
0
        for (int i = 0; i < ndim; ++i) {
931
0
          block_start[i] = nblock_ndim[i] * array->blockshape[i];
932
0
          block_stop[i] = block_start[i] + array->blockshape[i];
933
0
          block_start[i] += chunk_start[i];
934
0
          block_stop[i] += chunk_start[i];
935
936
0
          if (block_start[i] > chunk_stop[i]) {
937
0
            block_start[i] = chunk_stop[i];
938
0
          }
939
0
          if (block_stop[i] > chunk_stop[i]) {
940
0
            block_stop[i] = chunk_stop[i];
941
0
          }
942
0
        }
943
944
0
        bool block_empty = false;
945
0
        for (int i = 0; i < ndim; ++i) {
946
0
          block_empty |= (block_stop[i] <= start[i] || block_start[i] >= stop[i]);
947
0
        }
948
0
        block_maskout[nblock] = block_empty ? true : false;
949
0
      }
950
951
0
      if (blosc2_set_maskout(array->sc->dctx, block_maskout, nblocks) != BLOSC2_ERROR_SUCCESS) {
952
0
        BLOSC_TRACE_ERROR("Error setting the maskout");
953
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
954
0
      }
955
956
0
      int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
957
0
      if (err < 0) {
958
0
        BLOSC_TRACE_ERROR("Error decompressing chunk");
959
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
960
0
      }
961
962
0
      free(block_maskout);
963
0
    }
964
965
    // Iterate over blocks
966
967
0
    for (int nblock = 0; nblock < nblocks; ++nblock) {
968
0
      int64_t nblock_ndim[B2ND_MAX_DIM] = {0};
969
0
      blosc2_unidim_to_multidim(ndim, blocks_in_chunk, nblock, nblock_ndim);
970
971
      // Check if the block needs to be updated
972
0
      int64_t block_start[B2ND_MAX_DIM] = {0};
973
0
      int64_t block_stop[B2ND_MAX_DIM] = {0};
974
0
      for (int i = 0; i < ndim; ++i) {
975
0
        block_start[i] = nblock_ndim[i] * array->blockshape[i];
976
0
        block_stop[i] = block_start[i] + array->blockshape[i];
977
0
        block_start[i] += chunk_start[i];
978
0
        block_stop[i] += chunk_start[i];
979
980
0
        if (block_start[i] > chunk_stop[i]) {
981
0
          block_start[i] = chunk_stop[i];
982
0
        }
983
0
        if (block_stop[i] > chunk_stop[i]) {
984
0
          block_stop[i] = chunk_stop[i];
985
0
        }
986
0
      }
987
0
      int64_t block_shape[B2ND_MAX_DIM] = {0};
988
0
      for (int i = 0; i < ndim; ++i) {
989
0
        block_shape[i] = block_stop[i] - block_start[i];
990
0
      }
991
0
      bool block_empty = false;
992
0
      for (int i = 0; i < ndim; ++i) {
993
0
        block_empty |= (block_stop[i] <= start[i] || block_start[i] >= stop[i]);
994
0
      }
995
0
      if (block_empty) {
996
0
        continue;
997
0
      }
998
999
      // compute the start of the slice inside the block
1000
0
      int64_t slice_start[B2ND_MAX_DIM] = {0};
1001
0
      for (int i = 0; i < ndim; ++i) {
1002
0
        if (block_start[i] < start[i]) {
1003
0
          slice_start[i] = start[i] - block_start[i];
1004
0
        } else {
1005
0
          slice_start[i] = 0;
1006
0
        }
1007
0
        slice_start[i] += block_start[i];
1008
0
      }
1009
1010
0
      int64_t slice_stop[B2ND_MAX_DIM] = {0};
1011
0
      for (int i = 0; i < ndim; ++i) {
1012
0
        if (block_stop[i] > stop[i]) {
1013
0
          slice_stop[i] = block_shape[i] - (block_stop[i] - stop[i]);
1014
0
        } else {
1015
0
          slice_stop[i] = block_stop[i] - block_start[i];
1016
0
        }
1017
0
        slice_stop[i] += block_start[i];
1018
0
      }
1019
1020
0
      int64_t slice_shape[B2ND_MAX_DIM] = {0};
1021
0
      for (int i = 0; i < ndim; ++i) {
1022
0
        slice_shape[i] = slice_stop[i] - slice_start[i];
1023
0
      }
1024
1025
0
      uint8_t *src = &buffer_b[0];
1026
1027
0
      int64_t src_start[B2ND_MAX_DIM] = {0};
1028
0
      int64_t src_stop[B2ND_MAX_DIM] = {0};
1029
0
      for (int i = 0; i < ndim; ++i) {
1030
0
        src_start[i] = slice_start[i] - start[i];
1031
0
        src_stop[i] = slice_stop[i] - start[i];
1032
0
      }
1033
1034
0
      uint8_t *dst = &data[nblock * array->blocknitems * array->sc->typesize];
1035
0
      int64_t dst_pad_shape[B2ND_MAX_DIM];
1036
0
      for (int i = 0; i < ndim; ++i) {
1037
0
        dst_pad_shape[i] = array->blockshape[i];
1038
0
      }
1039
1040
0
      int64_t dst_start[B2ND_MAX_DIM] = {0};
1041
0
      int64_t dst_stop[B2ND_MAX_DIM] = {0};
1042
0
      for (int i = 0; i < ndim; ++i) {
1043
0
        dst_start[i] = slice_start[i] - block_start[i];
1044
0
        dst_stop[i] = dst_start[i] + slice_shape[i];
1045
0
      }
1046
1047
0
      if (set_slice) {
1048
0
        b2nd_copy_buffer2(ndim, array->sc->typesize,
1049
0
                          src, shape, src_start, src_stop,
1050
0
                          dst, dst_pad_shape, dst_start);
1051
0
      } else {
1052
0
        b2nd_copy_buffer2(ndim, array->sc->typesize,
1053
0
                          dst, dst_pad_shape, dst_start, dst_stop,
1054
0
                          src, shape, src_start);
1055
0
      }
1056
0
    }
1057
1058
0
    if (set_slice) {
1059
      // Recompress the data
1060
0
      int32_t chunk_nbytes = data_nbytes + BLOSC2_MAX_OVERHEAD;
1061
0
      uint8_t *chunk = malloc(chunk_nbytes);
1062
0
      BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
1063
0
      int brc;
1064
      // Update current_chunk in case a prefilter is applied
1065
0
      array->sc->current_nchunk = nchunk;
1066
0
      brc = blosc2_compress_ctx(array->sc->cctx, data, data_nbytes, chunk, chunk_nbytes);
1067
0
      if (brc < 0) {
1068
0
        BLOSC_TRACE_ERROR("Blosc can not compress the data");
1069
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1070
0
      }
1071
0
      int64_t brc_ = blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
1072
0
      if (brc_ < 0) {
1073
0
        BLOSC_TRACE_ERROR("Blosc can not update the chunk");
1074
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1075
0
      }
1076
0
    }
1077
0
  }
1078
1079
0
  free(data);
1080
1081
0
  return BLOSC2_ERROR_SUCCESS;
1082
0
}
1083
1084
1085
int b2nd_get_slice_cbuffer(const b2nd_array_t *array, const int64_t *start, const int64_t *stop,
1086
0
                           void *buffer, const int64_t *buffershape, int64_t buffersize) {
1087
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1088
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1089
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1090
0
  BLOSC_ERROR_NULL(buffershape, BLOSC2_ERROR_NULL_POINTER);
1091
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1092
1093
0
  BLOSC_ERROR(get_set_slice(buffer, buffersize, start, stop, buffershape, (b2nd_array_t *)array, false));
1094
1095
0
  return BLOSC2_ERROR_SUCCESS;
1096
0
}
1097
1098
1099
int b2nd_set_slice_cbuffer(const void *buffer, const int64_t *buffershape, int64_t buffersize,
1100
                           const int64_t *start, const int64_t *stop,
1101
0
                           b2nd_array_t *array) {
1102
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1103
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1104
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1105
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1106
1107
0
  BLOSC_ERROR(get_set_slice((void*)buffer, buffersize, start, stop, (int64_t *)buffershape, array, true));
1108
1109
0
  return BLOSC2_ERROR_SUCCESS;
1110
0
}
1111
1112
1113
int b2nd_get_slice(b2nd_context_t *ctx, b2nd_array_t **array, const b2nd_array_t *src, const int64_t *start,
1114
0
                   const int64_t *stop) {
1115
0
  BLOSC_ERROR_NULL(src, BLOSC2_ERROR_NULL_POINTER);
1116
0
  BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1117
0
  BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1118
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1119
1120
0
  ctx->ndim = src->ndim;
1121
0
  for (int i = 0; i < src->ndim; ++i) {
1122
0
    ctx->shape[i] = stop[i] - start[i];
1123
0
  }
1124
1125
  // Add data
1126
0
  BLOSC_ERROR(b2nd_empty(ctx, array));
1127
1128
0
  if ((*array)->nitems == 0) {
1129
0
    return BLOSC2_ERROR_SUCCESS;
1130
0
  }
1131
1132
0
  int8_t ndim = (*array)->ndim;
1133
0
  int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
1134
0
  for (int i = 0; i < ndim; ++i) {
1135
0
    chunks_in_array[i] = (*array)->extshape[i] / (*array)->chunkshape[i];
1136
0
  }
1137
0
  int64_t nchunks = (*array)->sc->nchunks;
1138
0
  for (int nchunk = 0; nchunk < nchunks; ++nchunk) {
1139
0
    int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1140
0
    blosc2_unidim_to_multidim(ndim, chunks_in_array, nchunk, nchunk_ndim);
1141
1142
    // Check if the chunk needs to be updated
1143
0
    int64_t chunk_start[B2ND_MAX_DIM] = {0};
1144
0
    int64_t chunk_stop[B2ND_MAX_DIM] = {0};
1145
0
    int64_t chunk_shape[B2ND_MAX_DIM] = {0};
1146
0
    for (int i = 0; i < ndim; ++i) {
1147
0
      chunk_start[i] = nchunk_ndim[i] * (*array)->chunkshape[i];
1148
0
      chunk_stop[i] = chunk_start[i] + (*array)->chunkshape[i];
1149
0
      if (chunk_stop[i] > (*array)->shape[i]) {
1150
0
        chunk_stop[i] = (*array)->shape[i];
1151
0
      }
1152
0
      chunk_shape[i] = chunk_stop[i] - chunk_start[i];
1153
0
    }
1154
1155
0
    int64_t src_start[B2ND_MAX_DIM] = {0};
1156
0
    int64_t src_stop[B2ND_MAX_DIM] = {0};
1157
0
    for (int i = 0; i < ndim; ++i) {
1158
0
      src_start[i] = chunk_start[i] + start[i];
1159
0
      src_stop[i] = chunk_stop[i] + start[i];
1160
0
    }
1161
0
    int64_t buffersize = ctx->b2_storage->cparams->typesize;
1162
0
    for (int i = 0; i < ndim; ++i) {
1163
0
      buffersize *= chunk_shape[i];
1164
0
    }
1165
0
    uint8_t *buffer = malloc(buffersize);
1166
0
    BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_MEMORY_ALLOC);
1167
0
    BLOSC_ERROR(b2nd_get_slice_cbuffer(src, src_start, src_stop, buffer, chunk_shape,
1168
0
                                       buffersize));
1169
0
    BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, chunk_shape, buffersize, chunk_start,
1170
0
                                       chunk_stop, *array));
1171
0
    free(buffer);
1172
0
  }
1173
1174
0
  return BLOSC2_ERROR_SUCCESS;
1175
0
}
1176
1177
/**
1178
 * @brief Return a view of a b2nd array.
1179
 *
1180
 * @param array The memory pointer of the array which will be viewed.
1181
 * @param view The memory pointer where the view will be created.
1182
 * @param ctx1 The b2nd context for the new array, containing new shape and other metadata.
1183
 *
1184
 * @return An error code.
1185
 *
1186
 * @note This doesn't support slices of arrays and is only useful for adding (or removing) dimensions.
1187
 *
1188
 */
1189
0
int view_new(const b2nd_array_t *array, b2nd_array_t **view, b2nd_context_t *ctx1) {
1190
1191
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1192
0
  BLOSC_ERROR_NULL(view, BLOSC2_ERROR_NULL_POINTER);
1193
1194
  // The view is not contiguous (points to the original contiguous cframe which has different shape)
1195
  // so we set contiguous to false, which forces a copy when calling to_cframe
1196
0
  ctx1->b2_storage->contiguous = false;
1197
1198
1199
  /* Fill view with zeros */
1200
0
  BLOSC_ERROR(b2nd_zeros(ctx1, view));
1201
  // Free the chunks in base array
1202
0
  for (int i = 0; i < (*view)->sc->nchunks; i++) {
1203
0
    free((*view)->sc->data[i]);
1204
0
  }
1205
0
  free((*view)->sc->data);
1206
0
  (*view)->sc->view = true;
1207
0
  (*view)->sc->data = array->sc->data; // point view to the same data
1208
0
  (*view)->sc->frame = array->sc->frame; // if original array is contiguous, point to frame
1209
0
  (*view)->sc->nvlmetalayers = array->sc->nvlmetalayers; //
1210
0
  for (int i = 0; i< array->sc->nvlmetalayers; i++) {
1211
0
    (*view)->sc->vlmetalayers[i] = array->sc->vlmetalayers[i]; // add ptrs to vlmetalayers
1212
0
  }
1213
1214
0
  return BLOSC2_ERROR_SUCCESS;
1215
0
}
1216
1217
0
int b2nd_expand_dims(const b2nd_array_t *array, b2nd_array_t **view, const bool *axis, const uint8_t final_dims) {
1218
0
  for (int i = 0; i < array->sc->nmetalayers; ++i) {
1219
0
    if (strcmp(array->sc->metalayers[i]->name, "b2nd") != 0) {
1220
0
      BLOSC_TRACE_ERROR("Cannot expand dimensions of an array with non-b2nd metalayers");
1221
0
      return BLOSC2_ERROR_INVALID_PARAM;
1222
0
    }
1223
0
  }
1224
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1225
0
  BLOSC_ERROR_NULL(view, BLOSC2_ERROR_NULL_POINTER);
1226
1227
0
  uint8_t old_idx = 0;
1228
0
  int64_t newshape[B2ND_MAX_DIM];
1229
0
  int32_t newchunkshape[B2ND_MAX_DIM];
1230
0
  int32_t newblockshape[B2ND_MAX_DIM];
1231
1232
0
  for (int i = 0; i < final_dims; ++i) {
1233
0
    if (axis[i] == true) {
1234
0
      newshape[i] = 1;
1235
0
      newchunkshape[i] = 1;
1236
0
      newblockshape[i] = 1;
1237
0
    }
1238
0
    else {
1239
0
      if (old_idx == array->ndim) {
1240
0
        BLOSC_TRACE_ERROR("Error in axis list: original array has fewer dimensions than the axis list implies!");
1241
0
        return BLOSC2_ERROR_INVALID_PARAM;
1242
0
      }
1243
0
      newshape[i] = array->shape[old_idx];
1244
0
      newchunkshape[i] = array->chunkshape[old_idx];
1245
0
      newblockshape[i] = array->blockshape[old_idx];
1246
0
      old_idx++;
1247
0
    }
1248
0
  }
1249
1250
  //views only deal with cparams/dparams; storage is always in-memory (ephemeral).
1251
0
  blosc2_cparams cparams = *(array->sc->storage->cparams);
1252
0
  blosc2_dparams dparams = *(array->sc->storage->dparams);
1253
0
  blosc2_storage b2_storage1 = {.cparams=&cparams, .dparams=&dparams};
1254
1255
0
  b2nd_context_t *ctx1 = b2nd_create_ctx(&b2_storage1, final_dims, newshape,
1256
0
                                        newchunkshape, newblockshape, array->dtype,
1257
0
                                        array->dtype_format, NULL, 0);
1258
1259
0
  view_new(array, view, ctx1);
1260
0
  b2nd_free_ctx(ctx1);
1261
1262
0
  return BLOSC2_ERROR_SUCCESS;
1263
0
}
1264
1265
1266
0
int b2nd_squeeze(b2nd_array_t *array, b2nd_array_t **view) {
1267
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1268
0
  BLOSC_ERROR_NULL(view, BLOSC2_ERROR_NULL_POINTER);
1269
1270
0
  bool index[B2ND_MAX_DIM];
1271
1272
0
  for (int i = 0; i < array->ndim; ++i) {
1273
0
    if (array->shape[i] != 1) {
1274
0
      index[i] = false;
1275
0
    } else {
1276
0
      index[i] = true;
1277
0
    }
1278
0
  }
1279
0
  BLOSC_ERROR(b2nd_squeeze_index(array, view, index));
1280
1281
0
  return BLOSC2_ERROR_SUCCESS;
1282
0
}
1283
1284
1285
0
int b2nd_squeeze_index(b2nd_array_t *array, b2nd_array_t **view, const bool *index) {
1286
0
  for (int i = 0; i < array->sc->nmetalayers; ++i) {
1287
0
    if (strcmp(array->sc->metalayers[i]->name, "b2nd") != 0) {
1288
0
      BLOSC_TRACE_ERROR("Cannot squeeze dimensions of an array with non-b2nd metalayers");
1289
0
      return BLOSC2_ERROR_INVALID_PARAM;
1290
0
    }
1291
0
  }
1292
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1293
0
  BLOSC_ERROR_NULL(view, BLOSC2_ERROR_NULL_POINTER);
1294
1295
0
  uint8_t nones = 0;
1296
0
  int64_t newshape[B2ND_MAX_DIM];
1297
0
  int32_t newchunkshape[B2ND_MAX_DIM];
1298
0
  int32_t newblockshape[B2ND_MAX_DIM];
1299
1300
0
  for (int i = 0; i < array->ndim; ++i) {
1301
0
    if (index[i] == true) {
1302
0
      if (array->shape[i] != 1) {
1303
0
        BLOSC_ERROR(BLOSC2_ERROR_INVALID_INDEX);
1304
0
      }
1305
0
    } else {
1306
0
      newshape[nones] = array->shape[i];
1307
0
      newchunkshape[nones] = array->chunkshape[i];
1308
0
      newblockshape[nones] = array->blockshape[i];
1309
0
      nones += 1;
1310
0
    }
1311
0
  }
1312
1313
  //views only deal with cparams/dparams; storage is always in-memory (ephemeral).
1314
0
  blosc2_cparams cparams = *(array->sc->storage->cparams);
1315
0
  blosc2_dparams dparams = *(array->sc->storage->dparams);
1316
0
  blosc2_storage b2_storage1 = {.cparams=&cparams, .dparams=&dparams};
1317
1318
0
  b2nd_context_t *ctx1 = b2nd_create_ctx(&b2_storage1, nones, newshape,
1319
0
                                        newchunkshape, newblockshape, array->dtype,
1320
0
                                        array->dtype_format, NULL, 0);
1321
1322
0
  view_new(array, view, ctx1);
1323
0
  b2nd_free_ctx(ctx1);
1324
1325
0
  return BLOSC2_ERROR_SUCCESS;
1326
0
}
1327
1328
1329
0
int b2nd_copy(b2nd_context_t *ctx, const b2nd_array_t *src, b2nd_array_t **array) {
1330
0
  BLOSC_ERROR_NULL(src, BLOSC2_ERROR_NULL_POINTER);
1331
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1332
1333
0
  ctx->ndim = src->ndim;
1334
1335
0
  for (int i = 0; i < src->ndim; ++i) {
1336
0
    ctx->shape[i] = src->shape[i];
1337
0
  }
1338
1339
0
  bool equals = true;
1340
0
  for (int i = 0; i < src->ndim; ++i) {
1341
0
    if (src->chunkshape[i] != ctx->chunkshape[i]) {
1342
0
      equals = false;
1343
0
      break;
1344
0
    }
1345
0
    if (src->blockshape[i] != ctx->blockshape[i]) {
1346
0
      equals = false;
1347
0
      break;
1348
0
    }
1349
0
  }
1350
1351
0
  if (equals) {
1352
0
    BLOSC_ERROR(array_without_schunk(ctx, array));
1353
1354
0
    blosc2_schunk *new_sc = blosc2_schunk_copy(src->sc, ctx->b2_storage);
1355
1356
0
    if (new_sc == NULL) {
1357
0
      return BLOSC2_ERROR_FAILURE;
1358
0
    }
1359
0
    (*array)->sc = new_sc;
1360
1361
0
  } else {
1362
0
    int64_t start[B2ND_MAX_DIM] = {0};
1363
1364
0
    int64_t stop[B2ND_MAX_DIM];
1365
0
    for (int i = 0; i < src->ndim; ++i) {
1366
0
      stop[i] = src->shape[i];
1367
0
    }
1368
    // Copy metalayers
1369
0
    b2nd_context_t params_meta;
1370
0
    memcpy(&params_meta, ctx, sizeof(params_meta));
1371
0
    int j = 0;
1372
1373
0
    for (int i = 0; i < src->sc->nmetalayers; ++i) {
1374
0
      if (strcmp(src->sc->metalayers[i]->name, "b2nd") == 0) {
1375
0
        continue;
1376
0
      }
1377
0
      blosc2_metalayer *meta = &params_meta.metalayers[j];
1378
0
      meta->name = src->sc->metalayers[i]->name;
1379
0
      meta->content = src->sc->metalayers[i]->content;
1380
0
      meta->content_len = src->sc->metalayers[i]->content_len;
1381
0
      j++;
1382
0
    }
1383
0
    params_meta.nmetalayers = j;
1384
1385
    // Copy data
1386
0
    BLOSC_ERROR(b2nd_get_slice(&params_meta, array, src, start, stop));
1387
1388
    // Copy vlmetayers
1389
0
    for (int i = 0; i < src->sc->nvlmetalayers; ++i) {
1390
0
      uint8_t *content;
1391
0
      int32_t content_len;
1392
0
      if (blosc2_vlmeta_get(src->sc, src->sc->vlmetalayers[i]->name, &content,
1393
0
                            &content_len) < 0) {
1394
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1395
0
      }
1396
0
      BLOSC_ERROR(blosc2_vlmeta_add((*array)->sc, src->sc->vlmetalayers[i]->name, content, content_len,
1397
0
                                      (*array)->sc->storage->cparams));
1398
0
      free(content);
1399
0
    }
1400
0
  }
1401
0
  return BLOSC2_ERROR_SUCCESS;
1402
0
}
1403
1404
1405
int b2nd_concatenate(b2nd_context_t *ctx, const b2nd_array_t *src1, const b2nd_array_t *src2,
1406
0
                     int8_t axis, bool copy, b2nd_array_t **array) {
1407
0
  BLOSC_ERROR_NULL(src1, BLOSC2_ERROR_NULL_POINTER);
1408
0
  BLOSC_ERROR_NULL(src2, BLOSC2_ERROR_NULL_POINTER);
1409
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1410
1411
  // Validate the axis parameter
1412
0
  if (axis < 0 || axis >= src1->ndim) {
1413
0
    BLOSC_TRACE_ERROR("axis parameter is out of bounds: axis=%d, expected range=[0, %d)", axis, src1->ndim - 1);
1414
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1415
0
  }
1416
1417
  // typesize must be the same for both arrays
1418
0
  if (src1->sc->typesize != src2->sc->typesize) {
1419
0
    BLOSC_TRACE_ERROR("The two arrays must have the same typesize");
1420
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1421
0
  }
1422
1423
  // Keep the src1 shape for later use
1424
0
  int64_t src1_shape[B2ND_MAX_DIM];
1425
0
  for (int i = 0; i < src1->ndim; ++i) {
1426
0
    src1_shape[i] = src1->shape[i];
1427
0
  }
1428
1429
  // Support for 0-dim arrays is not implemented
1430
0
  if (src1->ndim == 0 || src2->ndim == 0) {
1431
0
    BLOSC_TRACE_ERROR("Concatenation of 0-dim arrays is not supported");
1432
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1433
0
  }
1434
1435
  // Check that the shapes are compatible for concatenation
1436
0
  if (src1->ndim != src2->ndim) {
1437
0
    BLOSC_TRACE_ERROR("The two arrays must have the same number of dimensions");
1438
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1439
0
  }
1440
  // Compute the new shape
1441
0
  int64_t newshape[B2ND_MAX_DIM];
1442
0
  for (int8_t i = 0; i < src1->ndim; ++i) {
1443
0
    if (i == axis) {
1444
0
      newshape[i] = src1->shape[i] + src2->shape[i];
1445
0
    } else {
1446
0
      if (src1->shape[i] != src2->shape[i]) {
1447
0
        BLOSC_TRACE_ERROR("The two arrays must have the same shape in all dimensions except the concatenation axis");
1448
0
        BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1449
0
      }
1450
0
      newshape[i] = src1->shape[i];
1451
0
    }
1452
0
  }
1453
1454
0
  if (copy) {
1455
0
    BLOSC_ERROR(b2nd_copy(ctx, src1, array));
1456
0
  }
1457
0
  else {
1458
0
    *array = (b2nd_array_t *)src1;
1459
0
  }
1460
1461
  // Extend the array, we don't need to specify the start in resize, as we are extending the shape from the end
1462
0
  BLOSC_ERROR(b2nd_resize(*array, newshape, NULL));
1463
1464
  // Copy the data from the second array
1465
0
  int64_t start[B2ND_MAX_DIM];
1466
0
  int64_t stop[B2ND_MAX_DIM];
1467
1468
  // Check if the chunk is aligned with dest chunks, and has the same blockshape
1469
0
  bool aligned = true;
1470
0
  for (int8_t i = 0; i < src2->ndim; ++i) {
1471
0
    if (src1->chunkshape[i] != src2->chunkshape[i] ||
1472
0
        src2->blockshape[i] != (*array)->blockshape[i] ||
1473
0
        (i == axis && (src1_shape[i]) % (*array)->chunkshape[i] != 0)
1474
0
        ) {
1475
0
      aligned = false;
1476
0
      break;
1477
0
        }
1478
0
  }
1479
  // ...and get the chunk index in the dest array if aligned
1480
0
  int64_t chunks_in_array_strides[B2ND_MAX_DIM];
1481
  // Calculate strides for destination array
1482
0
  chunks_in_array_strides[(*array)->ndim - 1] = 1;
1483
0
  for (int i = (*array)->ndim - 2; i >= 0; --i) {
1484
0
    chunks_in_array_strides[i] = chunks_in_array_strides[i + 1] *
1485
0
                                ((*array)->extshape[i + 1] / (*array)->chunkshape[i + 1]);
1486
0
  }
1487
1488
  // Copy chunk by chunk
1489
0
  void *buffer = malloc(src2->sc->typesize * src2->extchunknitems);
1490
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_MEMORY_ALLOC);
1491
0
  for (int64_t nchunk = 0; nchunk < src2->sc->nchunks; ++nchunk) {
1492
    // Get multidimensional chunk position
1493
0
    int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1494
0
    int64_t chunkshape[B2ND_MAX_DIM] = {0};
1495
0
    for (int8_t i = 0; i < src2->ndim; ++i) {
1496
0
      chunkshape[i] = src2->chunkshape[i];
1497
0
    }
1498
0
    int64_t chunks_in_dim[B2ND_MAX_DIM] = {0};
1499
0
    for (int8_t i = 0; i < src2->ndim; ++i) {
1500
0
      chunks_in_dim[i] = src2->extshape[i] / src2->chunkshape[i];
1501
0
    }
1502
0
    blosc2_unidim_to_multidim(src2->ndim, chunks_in_dim, nchunk, nchunk_ndim);
1503
1504
0
    if (aligned) {
1505
      // Get the uncompressed chunk buffer from the source array
1506
0
      bool needs_free = false;
1507
0
      uint8_t *chunk;
1508
0
      int32_t cbytes = blosc2_schunk_get_chunk(src2->sc, nchunk, &chunk, &needs_free);
1509
0
      if (cbytes < 0) {
1510
0
        BLOSC_TRACE_ERROR("Error getting chunk from source array");
1511
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1512
0
      }
1513
      // Update the chunk in the destination array
1514
      // We need to free only if needs_free is true or copy is false
1515
      // bool needs_copy = !needs_free || copy;
1516
      // BLOSC_ERROR(blosc2_schunk_update_chunk((*array)->sc, nchunk_dest, chunk, needs_copy));
1517
      // if (needs_free && !copy) {
1518
      //   free(chunk);
1519
      // }
1520
      // TODO: the above makes some tests to crash, so always force a copy; try to optimize this later
1521
0
      int64_t nchunk_dest = 0;
1522
0
      nchunk_ndim[axis] += src1_shape[axis] / (*array)->chunkshape[axis];
1523
0
      for ( int i =0; i< src2->ndim; i++) {
1524
0
        nchunk_dest += nchunk_ndim[i] * chunks_in_array_strides[i];
1525
0
      }
1526
0
      BLOSC_ERROR(blosc2_schunk_update_chunk((*array)->sc, nchunk_dest, chunk, true));
1527
0
      if (needs_free) {
1528
0
        free(chunk);
1529
0
      }
1530
0
    }
1531
0
    else {
1532
1533
      // Set positions for each dimension
1534
0
      for (int8_t i = 0; i < src2->ndim; ++i) {
1535
0
        start[i] = nchunk_ndim[i] * src2->chunkshape[i];
1536
0
        stop[i] = start[i] + src2->chunkshape[i];
1537
0
        if (stop[i] > src2->shape[i]) {
1538
0
          stop[i] = src2->shape[i];  // Handle boundary chunks
1539
0
        }
1540
0
      }
1541
      // Load chunk into buffer
1542
0
      BLOSC_ERROR(b2nd_get_slice_cbuffer(src2, start, stop, buffer, chunkshape, src2->sc->chunksize));
1543
1544
      // Apply chunk offset only for concatenation axis
1545
0
      start[axis] += src1_shape[axis];
1546
0
      stop[axis] += src1_shape[axis];
1547
1548
      // Copy the chunk to the correct position
1549
0
      BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, chunkshape,
1550
0
                                         src2->sc->typesize * src2->extchunknitems,
1551
0
                                         start, stop, *array));
1552
0
    }
1553
0
  }
1554
1555
0
  free(buffer);
1556
1557
0
  return BLOSC2_ERROR_SUCCESS;
1558
0
}
1559
1560
0
int b2nd_save(const b2nd_array_t *array, char *urlpath) {
1561
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1562
0
  BLOSC_ERROR_NULL(urlpath, BLOSC2_ERROR_NULL_POINTER);
1563
1564
0
  b2nd_array_t *tmp;
1565
0
  blosc2_storage b2_storage = BLOSC2_STORAGE_DEFAULTS;
1566
0
  b2nd_context_t params = {.b2_storage=&b2_storage};
1567
0
  b2_storage.urlpath = urlpath;
1568
0
  b2_storage.contiguous = array->sc->storage->contiguous;
1569
1570
0
  for (int i = 0; i < array->ndim; ++i) {
1571
0
    params.chunkshape[i] = array->chunkshape[i];
1572
0
    params.blockshape[i] = array->blockshape[i];
1573
0
  }
1574
1575
0
  BLOSC_ERROR(b2nd_copy(&params, array, &tmp));
1576
0
  BLOSC_ERROR(b2nd_free(tmp));
1577
1578
0
  return BLOSC2_ERROR_SUCCESS;
1579
0
}
1580
1581
0
int64_t b2nd_save_append(const b2nd_array_t *array, const char *urlpath) {
1582
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1583
0
  return blosc2_schunk_append_file(array->sc, urlpath);
1584
0
}
1585
1586
0
int b2nd_print_meta(const b2nd_array_t *array) {
1587
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1588
0
  int8_t ndim;
1589
0
  int64_t shape[B2ND_MAX_DIM];
1590
0
  int32_t chunkshape[B2ND_MAX_DIM];
1591
0
  int32_t blockshape[B2ND_MAX_DIM];
1592
0
  char *dtype;
1593
0
  int8_t dtype_format;
1594
0
  uint8_t *smeta;
1595
0
  int32_t smeta_len;
1596
0
  if (blosc2_meta_get(array->sc, "b2nd", &smeta, &smeta_len) < 0) {
1597
    // Try with a caterva metalayer; we are meant to be backward compatible with it
1598
0
    if (blosc2_meta_get(array->sc, "caterva", &smeta, &smeta_len) < 0) {
1599
0
      BLOSC_ERROR(BLOSC2_ERROR_METALAYER_NOT_FOUND);
1600
0
    }
1601
0
  }
1602
0
  BLOSC_ERROR(b2nd_deserialize_meta(smeta, smeta_len, &ndim, shape, chunkshape, blockshape,
1603
0
                                    &dtype, &dtype_format));
1604
0
  free(smeta);
1605
1606
0
  printf("b2nd metalayer parameters:\n Ndim:       %d", ndim);
1607
0
  printf("\n shape:      %" PRId64 "", shape[0]);
1608
0
  for (int i = 1; i < ndim; ++i) {
1609
0
    printf(", %" PRId64 "", shape[i]);
1610
0
  }
1611
0
  printf("\n chunkshape: %d", chunkshape[0]);
1612
0
  for (int i = 1; i < ndim; ++i) {
1613
0
    printf(", %d", chunkshape[i]);
1614
0
  }
1615
0
  if (dtype != NULL) {
1616
0
    printf("\n dtype: %s", dtype);
1617
0
    free(dtype);
1618
0
  }
1619
1620
0
  printf("\n blockshape: %d", blockshape[0]);
1621
0
  for (int i = 1; i < ndim; ++i) {
1622
0
    printf(", %d", blockshape[i]);
1623
0
  }
1624
0
  printf("\n");
1625
1626
0
  return BLOSC2_ERROR_SUCCESS;
1627
0
}
1628
1629
1630
0
int extend_shape(b2nd_array_t *array, const int64_t *new_shape, const int64_t *start) {
1631
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1632
0
  BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1633
1634
0
  int8_t ndim = array->ndim;
1635
0
  int64_t diffs_shape[B2ND_MAX_DIM];
1636
0
  int64_t diffs_sum = 0;
1637
0
  for (int i = 0; i < ndim; i++) {
1638
0
    diffs_shape[i] = new_shape[i] - array->shape[i];
1639
0
    diffs_sum += diffs_shape[i];
1640
0
    if (diffs_shape[i] < 0) {
1641
0
      BLOSC_TRACE_ERROR("The new shape must be greater than the old one");
1642
0
      BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1643
0
    }
1644
0
    if (array->shape[i] == INT64_MAX) {
1645
0
      BLOSC_TRACE_ERROR("Cannot extend array with shape[%d] = %" PRId64 "d", i, INT64_MAX);
1646
0
      BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1647
0
    }
1648
0
  }
1649
0
  if (diffs_sum == 0) {
1650
    // Shapes are equal. Do nothing.
1651
0
    return BLOSC2_ERROR_SUCCESS;
1652
0
  }
1653
1654
0
  int64_t old_nchunks = array->sc->nchunks;
1655
  // aux array to keep old shapes
1656
0
  b2nd_array_t *aux = malloc(sizeof(b2nd_array_t));
1657
0
  BLOSC_ERROR_NULL(aux, BLOSC2_ERROR_MEMORY_ALLOC);
1658
0
  aux->sc = NULL;
1659
0
  BLOSC_ERROR(update_shape(aux, ndim, array->shape, array->chunkshape, array->blockshape));
1660
1661
0
  BLOSC_ERROR(update_shape(array, ndim, new_shape, array->chunkshape, array->blockshape));
1662
1663
0
  int64_t nchunks = array->extnitems / array->chunknitems;
1664
0
  int64_t nchunks_;
1665
0
  int64_t nchunk_ndim[B2ND_MAX_DIM];
1666
0
  blosc2_cparams *cparams;
1667
0
  BLOSC_ERROR(blosc2_schunk_get_cparams(array->sc, &cparams));
1668
0
  void *chunk;
1669
0
  int64_t csize;
1670
0
  if (nchunks != old_nchunks) {
1671
0
    if (start == NULL) {
1672
0
      start = aux->shape;
1673
0
    }
1674
0
    int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
1675
0
    for (int i = 0; i < ndim; ++i) {
1676
0
      chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
1677
0
    }
1678
0
    for (int i = 0; i < nchunks; ++i) {
1679
0
      blosc2_unidim_to_multidim(ndim, chunks_in_array, i, nchunk_ndim);
1680
0
      for (int j = 0; j < ndim; ++j) {
1681
0
        if (start[j] <= (array->chunkshape[j] * nchunk_ndim[j])
1682
0
            && (array->chunkshape[j] * nchunk_ndim[j]) < (start[j] + new_shape[j] - aux->shape[j])) {
1683
0
          chunk = malloc(BLOSC_EXTENDED_HEADER_LENGTH);
1684
0
          BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
1685
0
          csize = blosc2_chunk_zeros(*cparams, array->sc->chunksize, chunk, BLOSC_EXTENDED_HEADER_LENGTH);
1686
0
          if (csize < 0) {
1687
0
            free(aux);
1688
0
            free(cparams);
1689
0
            BLOSC_TRACE_ERROR("Blosc error when creating a chunk");
1690
0
            return BLOSC2_ERROR_FAILURE;
1691
0
          }
1692
0
          nchunks_ = blosc2_schunk_insert_chunk(array->sc, i, chunk, false);
1693
0
          if (nchunks_ < 0) {
1694
0
            free(aux);
1695
0
            free(cparams);
1696
0
            BLOSC_TRACE_ERROR("Blosc error when inserting a chunk");
1697
0
            return BLOSC2_ERROR_FAILURE;
1698
0
          }
1699
0
          break;
1700
0
        }
1701
0
      }
1702
0
    }
1703
0
  }
1704
0
  free(aux);
1705
0
  free(cparams);
1706
1707
0
  return BLOSC2_ERROR_SUCCESS;
1708
0
}
1709
1710
1711
0
int shrink_shape(b2nd_array_t *array, const int64_t *new_shape, const int64_t *start) {
1712
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1713
0
  BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1714
1715
0
  int8_t ndim = array->ndim;
1716
0
  int64_t diffs_shape[B2ND_MAX_DIM];
1717
0
  int64_t diffs_sum = 0;
1718
0
  for (int i = 0; i < ndim; i++) {
1719
0
    diffs_shape[i] = new_shape[i] - array->shape[i];
1720
0
    diffs_sum += diffs_shape[i];
1721
0
    if (diffs_shape[i] > 0) {
1722
0
      BLOSC_TRACE_ERROR("The new shape must be smaller than the old one");
1723
0
      BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1724
0
    }
1725
0
    if (array->shape[i] == 0) {
1726
0
      continue;
1727
0
    }
1728
0
  }
1729
0
  if (diffs_sum == 0) {
1730
    // Shapes are equal. Do nothing.
1731
0
    return BLOSC2_ERROR_SUCCESS;
1732
0
  }
1733
1734
0
  int64_t old_nchunks = array->sc->nchunks;
1735
  // aux array to keep old shapes
1736
0
  b2nd_array_t *aux = malloc(sizeof(b2nd_array_t));
1737
0
  BLOSC_ERROR_NULL(aux, BLOSC2_ERROR_MEMORY_ALLOC);
1738
0
  aux->sc = NULL;
1739
0
  BLOSC_ERROR(update_shape(aux, ndim, array->shape, array->chunkshape, array->blockshape));
1740
1741
0
  BLOSC_ERROR(update_shape(array, ndim, new_shape, array->chunkshape, array->blockshape));
1742
1743
  // Delete chunks if needed
1744
0
  int64_t chunks_in_array_old[B2ND_MAX_DIM] = {0};
1745
0
  for (int i = 0; i < ndim; ++i) {
1746
0
    chunks_in_array_old[i] = aux->extshape[i] / aux->chunkshape[i];
1747
0
  }
1748
0
  if (start == NULL) {
1749
0
    start = new_shape;
1750
0
  }
1751
1752
0
  int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1753
0
  int64_t nchunks_;
1754
0
  for (int i = (int) old_nchunks - 1; i >= 0; --i) {
1755
0
    blosc2_unidim_to_multidim(ndim, chunks_in_array_old, i, nchunk_ndim);
1756
0
    for (int j = 0; j < ndim; ++j) {
1757
0
      if (start[j] <= (array->chunkshape[j] * nchunk_ndim[j])
1758
0
          && (array->chunkshape[j] * nchunk_ndim[j]) < (start[j] + aux->shape[j] - new_shape[j])) {
1759
0
        nchunks_ = blosc2_schunk_delete_chunk(array->sc, i);
1760
0
        if (nchunks_ < 0) {
1761
0
          free(aux);
1762
0
          BLOSC_TRACE_ERROR("Blosc error when deleting a chunk");
1763
0
          return BLOSC2_ERROR_FAILURE;
1764
0
        }
1765
0
        break;
1766
0
      }
1767
0
    }
1768
0
  }
1769
0
  free(aux);
1770
1771
0
  return BLOSC2_ERROR_SUCCESS;
1772
0
}
1773
1774
1775
int b2nd_resize(b2nd_array_t *array, const int64_t *new_shape,
1776
0
                const int64_t *start) {
1777
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1778
0
  BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1779
1780
0
  if (start != NULL) {
1781
0
    for (int i = 0; i < array->ndim; ++i) {
1782
0
      if (start[i] > array->shape[i]) {
1783
0
        BLOSC_TRACE_ERROR("`start` must be lower or equal than old array shape in all dims");
1784
0
        BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1785
0
      }
1786
0
      if ((new_shape[i] > array->shape[i] && start[i] != array->shape[i])
1787
0
          || (new_shape[i] < array->shape[i]
1788
0
              && (start[i] + array->shape[i] - new_shape[i]) != array->shape[i])) {
1789
        // Chunks cannot be cut unless they are in the last position
1790
0
        if (start[i] % array->chunkshape[i] != 0) {
1791
0
          BLOSC_TRACE_ERROR("If array end is not being modified "
1792
0
                              "`start` must be a multiple of chunkshape in all dims");
1793
0
          BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1794
0
        }
1795
0
        if ((new_shape[i] - array->shape[i]) % array->chunkshape[i] != 0) {
1796
0
          BLOSC_TRACE_ERROR("If array end is not being modified "
1797
0
                              "`(new_shape - shape)` must be multiple of chunkshape in all dims");
1798
0
          BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1799
0
        }
1800
0
      }
1801
0
    }
1802
0
  }
1803
1804
  // Get shrunk shape
1805
0
  int64_t shrunk_shape[B2ND_MAX_DIM] = {0};
1806
0
  for (int i = 0; i < array->ndim; ++i) {
1807
0
    if (new_shape[i] <= array->shape[i]) {
1808
0
      shrunk_shape[i] = new_shape[i];
1809
0
    } else {
1810
0
      shrunk_shape[i] = array->shape[i];
1811
0
    }
1812
0
  }
1813
1814
0
  BLOSC_ERROR(shrink_shape(array, shrunk_shape, start));
1815
0
  BLOSC_ERROR(extend_shape(array, new_shape, start));
1816
1817
0
  return BLOSC2_ERROR_SUCCESS;
1818
0
}
1819
1820
1821
int b2nd_insert(b2nd_array_t *array, const void *buffer, int64_t buffersize,
1822
0
                int8_t axis, int64_t insert_start) {
1823
1824
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1825
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1826
1827
0
  if (axis >= array->ndim) {
1828
0
    BLOSC_TRACE_ERROR("`axis` cannot be greater than the number of dimensions");
1829
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1830
0
  }
1831
1832
0
  int64_t axis_size = array->sc->typesize;
1833
0
  int64_t buffershape[B2ND_MAX_DIM];
1834
0
  for (int i = 0; i < array->ndim; ++i) {
1835
0
    if (i != axis) {
1836
0
      axis_size *= array->shape[i];
1837
0
      buffershape[i] = array->shape[i];
1838
0
    }
1839
0
  }
1840
0
  if (buffersize % axis_size != 0) {
1841
0
    BLOSC_TRACE_ERROR("`buffersize` must be multiple of the array");
1842
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1843
0
  }
1844
0
  int64_t newshape[B2ND_MAX_DIM];
1845
0
  memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1846
0
  newshape[axis] += buffersize / axis_size;
1847
0
  buffershape[axis] = newshape[axis] - array->shape[axis];
1848
0
  int64_t start[B2ND_MAX_DIM] = {0};
1849
0
  start[axis] = insert_start;
1850
1851
0
  if (insert_start == array->shape[axis]) {
1852
0
    BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1853
0
  } else {
1854
0
    BLOSC_ERROR(b2nd_resize(array, newshape, start));
1855
0
  }
1856
1857
0
  int64_t stop[B2ND_MAX_DIM];
1858
0
  memcpy(stop, array->shape, sizeof(int64_t) * array->ndim);
1859
0
  stop[axis] = start[axis] + buffershape[axis];
1860
0
  BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, buffershape, buffersize, start, stop, array));
1861
1862
0
  return BLOSC2_ERROR_SUCCESS;
1863
0
}
1864
1865
1866
int b2nd_append(b2nd_array_t *array, const void *buffer, int64_t buffersize,
1867
0
                int8_t axis) {
1868
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1869
0
  BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1870
1871
0
  int32_t chunksize = array->sc->chunksize;
1872
0
  int64_t nchunks_append = buffersize / chunksize;
1873
  // Check whether chunkshape and blockshape are compatible with accelerated path.
1874
  // Essentially, we are checking whether the buffer is a multiple of the chunksize
1875
  // and that the chunkshape and blockshape are the same, except for the first axis.
1876
  // Also, axis needs to be the first one.
1877
0
  bool compat_chunks_blocks = true;
1878
0
  for (int i = 1; i < array->ndim; ++i) {
1879
0
    if (array->chunkshape[i] != array->blockshape[i]) {
1880
0
      compat_chunks_blocks = false;
1881
0
      break;
1882
0
    }
1883
0
  }
1884
0
  if (axis > 0) {
1885
0
    compat_chunks_blocks = false;
1886
0
  }
1887
  // General case where a buffer has a different size than the chunksize
1888
0
  if (!compat_chunks_blocks || buffersize % chunksize != 0 || nchunks_append != 1) {
1889
0
    BLOSC_ERROR(b2nd_insert(array, buffer, buffersize, axis, array->shape[axis]));
1890
0
    return BLOSC2_ERROR_SUCCESS;
1891
0
  }
1892
1893
  // Accelerated path for buffers that are of the same size as the chunksize
1894
  // printf("accelerated path\n");
1895
1896
  // Append the buffer to the underlying schunk. This is very fast, as
1897
  // it doesn't need to do internal partitioning.
1898
0
  BLOSC_ERROR(blosc2_schunk_append_buffer(array->sc, (void*)buffer, buffersize));
1899
1900
  // Finally, resize the array
1901
0
  int64_t newshape[B2ND_MAX_DIM];
1902
0
  memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1903
0
  newshape[axis] += nchunks_append * array->chunkshape[axis];
1904
0
  BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1905
1906
0
  return BLOSC2_ERROR_SUCCESS;
1907
0
}
1908
1909
1910
int b2nd_delete(b2nd_array_t *array, const int8_t axis,
1911
0
                int64_t delete_start, int64_t delete_len) {
1912
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1913
1914
0
  if (axis >= array->ndim) {
1915
0
    BLOSC_TRACE_ERROR("axis cannot be greater than the number of dimensions");
1916
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1917
0
  }
1918
1919
1920
0
  int64_t newshape[B2ND_MAX_DIM];
1921
0
  memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1922
0
  newshape[axis] -= delete_len;
1923
0
  int64_t start[B2ND_MAX_DIM] = {0};
1924
0
  start[axis] = delete_start;
1925
1926
0
  if (delete_start == (array->shape[axis] - delete_len)) {
1927
0
    BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1928
0
  } else {
1929
0
    BLOSC_ERROR(b2nd_resize(array, newshape, start));
1930
0
  }
1931
1932
0
  return BLOSC2_ERROR_SUCCESS;
1933
0
}
1934
1935
// Indexing
1936
1937
typedef struct {
1938
    int64_t value;
1939
    int64_t index;
1940
} b2nd_selection_t;
1941
1942
1943
0
int compare_selection(const void *a, const void *b) {
1944
0
  int res = (int) (((b2nd_selection_t *) a)->value - ((b2nd_selection_t *) b)->value);
1945
  // In case values are equal, sort by index
1946
0
  if (res == 0) {
1947
0
    res = (int) (((b2nd_selection_t *) a)->index - ((b2nd_selection_t *) b)->index);
1948
0
  }
1949
0
  return res;
1950
0
}
1951
1952
1953
int copy_block_buffer_data(b2nd_array_t *array,
1954
                           int8_t ndim,
1955
                           int64_t *block_selection_size,
1956
                           b2nd_selection_t **chunk_selection,
1957
                           b2nd_selection_t **p_block_selection_0,
1958
                           b2nd_selection_t **p_block_selection_1,
1959
                           uint8_t *block,
1960
                           uint8_t *buffer,
1961
                           int64_t *buffershape,
1962
                           int64_t *bufferstrides,
1963
0
                           bool get) {
1964
0
  p_block_selection_0[ndim] = chunk_selection[ndim];
1965
0
  p_block_selection_1[ndim] = chunk_selection[ndim];
1966
0
  while (p_block_selection_1[ndim] - p_block_selection_0[ndim] < block_selection_size[ndim]) {
1967
0
    if (ndim == array->ndim - 1) {
1968
1969
0
      int64_t index_in_block_n[B2ND_MAX_DIM];
1970
0
      for (int i = 0; i < array->ndim; ++i) {
1971
0
        index_in_block_n[i] = p_block_selection_1[i]->value % array->chunkshape[i] % array->blockshape[i];
1972
0
      }
1973
0
      int64_t index_in_block = 0;
1974
0
      for (int i = 0; i < array->ndim; ++i) {
1975
0
        index_in_block += index_in_block_n[i] * array->item_block_strides[i];
1976
0
      }
1977
1978
0
      int64_t index_in_buffer_n[B2ND_MAX_DIM];
1979
0
      for (int i = 0; i < array->ndim; ++i) {
1980
0
        index_in_buffer_n[i] = p_block_selection_1[i]->index;
1981
0
      }
1982
0
      int64_t index_in_buffer = 0;
1983
0
      for (int i = 0; i < array->ndim; ++i) {
1984
0
        index_in_buffer += index_in_buffer_n[i] * bufferstrides[i];
1985
0
      }
1986
0
      if (get) {
1987
0
        memcpy(&buffer[index_in_buffer * array->sc->typesize],
1988
0
               &block[index_in_block * array->sc->typesize],
1989
0
               array->sc->typesize);
1990
0
      } else {
1991
0
        memcpy(&block[index_in_block * array->sc->typesize],
1992
0
               &buffer[index_in_buffer * array->sc->typesize],
1993
0
               array->sc->typesize);
1994
0
      }
1995
0
    } else {
1996
0
      BLOSC_ERROR(copy_block_buffer_data(array, (int8_t) (ndim + 1), block_selection_size,
1997
0
                                         chunk_selection,
1998
0
                                         p_block_selection_0, p_block_selection_1, block,
1999
0
                                         buffer, buffershape, bufferstrides, get)
2000
0
      );
2001
0
    }
2002
0
    p_block_selection_1[ndim]++;
2003
0
  }
2004
0
  return BLOSC2_ERROR_SUCCESS;
2005
0
}
2006
2007
2008
int iter_block_copy(b2nd_array_t *array, int8_t ndim,
2009
                    int64_t *chunk_selection_size,
2010
                    b2nd_selection_t **ordered_selection,
2011
                    b2nd_selection_t **chunk_selection_0,
2012
                    b2nd_selection_t **chunk_selection_1,
2013
                    uint8_t *data,
2014
                    uint8_t *buffer,
2015
                    int64_t *buffershape,
2016
                    int64_t *bufferstrides,
2017
0
                    bool get) {
2018
0
  chunk_selection_0[ndim] = ordered_selection[ndim];
2019
0
  chunk_selection_1[ndim] = ordered_selection[ndim];
2020
0
  while (chunk_selection_1[ndim] - ordered_selection[ndim] < chunk_selection_size[ndim]) {
2021
0
    int64_t block_index_ndim = ((*chunk_selection_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim];
2022
0
    while (chunk_selection_1[ndim] - ordered_selection[ndim] < chunk_selection_size[ndim] &&
2023
0
           block_index_ndim == ((*chunk_selection_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim]) {
2024
0
      chunk_selection_1[ndim]++;
2025
0
    }
2026
0
    if (ndim == array->ndim - 1) {
2027
0
      int64_t block_chunk_strides[B2ND_MAX_DIM];
2028
0
      block_chunk_strides[array->ndim - 1] = 1;
2029
0
      for (int i = array->ndim - 2; i >= 0; --i) {
2030
0
        block_chunk_strides[i] = block_chunk_strides[i + 1] * (array->extchunkshape[i + 1] / array->blockshape[i + 1]);
2031
0
      }
2032
0
      int64_t block_index[B2ND_MAX_DIM];
2033
0
      for (int i = 0; i < array->ndim; ++i) {
2034
0
        block_index[i] = ((*chunk_selection_0[i]).value % array->chunkshape[i]) / array->blockshape[i];
2035
0
      }
2036
0
      int64_t nblock = 0;
2037
0
      for (int i = 0; i < array->ndim; ++i) {
2038
0
        nblock += block_index[i] * block_chunk_strides[i];
2039
0
      }
2040
0
      b2nd_selection_t **p_block_selection_0 = malloc(array->ndim * sizeof(b2nd_selection_t *));
2041
0
      BLOSC_ERROR_NULL(p_block_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
2042
0
      b2nd_selection_t **p_block_selection_1 = malloc(array->ndim * sizeof(b2nd_selection_t *));
2043
0
      BLOSC_ERROR_NULL(p_block_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
2044
0
      int64_t *block_selection_size = malloc(array->ndim * sizeof(int64_t));
2045
0
      BLOSC_ERROR_NULL(block_selection_size, BLOSC2_ERROR_MEMORY_ALLOC);
2046
0
      for (int i = 0; i < array->ndim; ++i) {
2047
0
        block_selection_size[i] = chunk_selection_1[i] - chunk_selection_0[i];
2048
0
      }
2049
2050
0
      BLOSC_ERROR(copy_block_buffer_data(array,
2051
0
                                         (int8_t) 0,
2052
0
                                         block_selection_size,
2053
0
                                         chunk_selection_0,
2054
0
                                         p_block_selection_0,
2055
0
                                         p_block_selection_1,
2056
0
                                         &data[nblock * array->blocknitems * array->sc->typesize],
2057
0
                                         buffer,
2058
0
                                         buffershape,
2059
0
                                         bufferstrides,
2060
0
                                         get)
2061
0
      );
2062
0
      free(p_block_selection_0);
2063
0
      free(p_block_selection_1);
2064
0
      free(block_selection_size);
2065
0
    } else {
2066
0
      BLOSC_ERROR(iter_block_copy(array, (int8_t) (ndim + 1), chunk_selection_size,
2067
0
                                  ordered_selection, chunk_selection_0, chunk_selection_1,
2068
0
                                  data, buffer, buffershape, bufferstrides, get)
2069
0
      );
2070
0
    }
2071
0
    chunk_selection_0[ndim] = chunk_selection_1[ndim];
2072
2073
0
  }
2074
2075
0
  return BLOSC2_ERROR_SUCCESS;
2076
0
}
2077
2078
2079
int iter_block_maskout(b2nd_array_t *array, int8_t ndim,
2080
                       int64_t *sel_block_size,
2081
                       b2nd_selection_t **o_selection,
2082
                       b2nd_selection_t **p_o_sel_block_0,
2083
                       b2nd_selection_t **p_o_sel_block_1,
2084
0
                       bool *maskout) {
2085
0
  p_o_sel_block_0[ndim] = o_selection[ndim];
2086
0
  p_o_sel_block_1[ndim] = o_selection[ndim];
2087
0
  while (p_o_sel_block_1[ndim] - o_selection[ndim] < sel_block_size[ndim]) {
2088
0
    int64_t block_index_ndim = ((*p_o_sel_block_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim];
2089
0
    while (p_o_sel_block_1[ndim] - o_selection[ndim] < sel_block_size[ndim] &&
2090
0
           block_index_ndim == ((*p_o_sel_block_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim]) {
2091
0
      p_o_sel_block_1[ndim]++;
2092
0
    }
2093
0
    if (ndim == array->ndim - 1) {
2094
0
      int64_t block_chunk_strides[B2ND_MAX_DIM];
2095
0
      block_chunk_strides[array->ndim - 1] = 1;
2096
0
      for (int i = array->ndim - 2; i >= 0; --i) {
2097
0
        block_chunk_strides[i] = block_chunk_strides[i + 1] * (array->extchunkshape[i + 1] / array->blockshape[i + 1]);
2098
0
      }
2099
0
      int64_t block_index[B2ND_MAX_DIM];
2100
0
      for (int i = 0; i < array->ndim; ++i) {
2101
0
        block_index[i] = ((*p_o_sel_block_0[i]).value % array->chunkshape[i]) / array->blockshape[i];
2102
0
      }
2103
0
      int64_t nblock = 0;
2104
0
      for (int i = 0; i < array->ndim; ++i) {
2105
0
        nblock += block_index[i] * block_chunk_strides[i];
2106
0
      }
2107
0
      maskout[nblock] = false;
2108
0
    } else {
2109
0
      BLOSC_ERROR(iter_block_maskout(array, (int8_t) (ndim + 1), sel_block_size,
2110
0
                                     o_selection, p_o_sel_block_0, p_o_sel_block_1,
2111
0
                                     maskout)
2112
0
      );
2113
0
    }
2114
0
    p_o_sel_block_0[ndim] = p_o_sel_block_1[ndim];
2115
2116
0
  }
2117
2118
0
  return BLOSC2_ERROR_SUCCESS;
2119
0
}
2120
2121
2122
int iter_chunk(b2nd_array_t *array, int8_t ndim,
2123
               int64_t *selection_size,
2124
               b2nd_selection_t **ordered_selection,
2125
               b2nd_selection_t **p_ordered_selection_0,
2126
               b2nd_selection_t **p_ordered_selection_1,
2127
               uint8_t *buffer,
2128
               int64_t *buffershape,
2129
               int64_t *bufferstrides,
2130
0
               bool get) {
2131
0
  p_ordered_selection_0[ndim] = ordered_selection[ndim];
2132
0
  p_ordered_selection_1[ndim] = ordered_selection[ndim];
2133
0
  while (p_ordered_selection_1[ndim] - ordered_selection[ndim] < selection_size[ndim]) {
2134
0
    int64_t chunk_index_ndim = (*p_ordered_selection_1[ndim]).value / array->chunkshape[ndim];
2135
0
    while (p_ordered_selection_1[ndim] - ordered_selection[ndim] < selection_size[ndim] &&
2136
0
           chunk_index_ndim == (*p_ordered_selection_1[ndim]).value / array->chunkshape[ndim]) {
2137
0
      p_ordered_selection_1[ndim]++;
2138
0
    }
2139
0
    if (ndim == array->ndim - 1) {
2140
0
      int64_t chunk_array_strides[B2ND_MAX_DIM];
2141
0
      chunk_array_strides[array->ndim - 1] = 1;
2142
0
      for (int i = array->ndim - 2; i >= 0; --i) {
2143
0
        chunk_array_strides[i] = chunk_array_strides[i + 1] *
2144
0
                                 (array->extshape[i + 1] / array->chunkshape[i + 1]);
2145
0
      }
2146
0
      int64_t chunk_index[B2ND_MAX_DIM];
2147
0
      for (int i = 0; i < array->ndim; ++i) {
2148
0
        chunk_index[i] = (*p_ordered_selection_0[i]).value / array->chunkshape[i];
2149
0
      }
2150
0
      int64_t nchunk = 0;
2151
0
      for (int i = 0; i < array->ndim; ++i) {
2152
0
        nchunk += chunk_index[i] * chunk_array_strides[i];
2153
0
      }
2154
2155
0
      int64_t nblocks = array->extchunknitems / array->blocknitems;
2156
0
      b2nd_selection_t **p_chunk_selection_0 = malloc(array->ndim * sizeof(b2nd_selection_t *));
2157
0
      BLOSC_ERROR_NULL(p_chunk_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
2158
0
      b2nd_selection_t **p_chunk_selection_1 = malloc(array->ndim * sizeof(b2nd_selection_t *));
2159
0
      BLOSC_ERROR_NULL(p_chunk_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
2160
0
      int64_t *chunk_selection_size = malloc(array->ndim * sizeof(int64_t));
2161
0
      BLOSC_ERROR_NULL(chunk_selection_size, BLOSC2_ERROR_MEMORY_ALLOC);
2162
0
      for (int i = 0; i < array->ndim; ++i) {
2163
0
        chunk_selection_size[i] = p_ordered_selection_1[i] - p_ordered_selection_0[i];
2164
0
      }
2165
2166
0
      if (get) {
2167
0
        bool *maskout = calloc(nblocks, sizeof(bool));
2168
0
        for (int i = 0; i < nblocks; ++i) {
2169
0
          maskout[i] = true;
2170
0
        }
2171
2172
0
        BLOSC_ERROR(iter_block_maskout(array, (int8_t) 0,
2173
0
                                       chunk_selection_size,
2174
0
                                       p_ordered_selection_0,
2175
0
                                       p_chunk_selection_0,
2176
0
                                       p_chunk_selection_1,
2177
0
                                       maskout));
2178
2179
0
        if (blosc2_set_maskout(array->sc->dctx, maskout, (int) nblocks) !=
2180
0
            BLOSC2_ERROR_SUCCESS) {
2181
0
          BLOSC_TRACE_ERROR("Error setting the maskout");
2182
0
          BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
2183
0
        }
2184
0
        free(maskout);
2185
0
      }
2186
0
      int data_nitems = (int) array->extchunknitems;
2187
0
      int data_nbytes = data_nitems * array->sc->typesize;
2188
0
      uint8_t *data = malloc(data_nitems * array->sc->typesize);
2189
0
      BLOSC_ERROR_NULL(data, BLOSC2_ERROR_MEMORY_ALLOC);
2190
0
      int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
2191
0
      if (err < 0) {
2192
0
        BLOSC_TRACE_ERROR("Error decompressing chunk");
2193
0
        BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
2194
0
      }
2195
0
      BLOSC_ERROR(iter_block_copy(array, 0, chunk_selection_size,
2196
0
                                  p_ordered_selection_0, p_chunk_selection_0, p_chunk_selection_1,
2197
0
                                  data, buffer, buffershape, bufferstrides, get));
2198
2199
0
      if (!get) {
2200
0
        int32_t chunk_size = data_nbytes + BLOSC_EXTENDED_HEADER_LENGTH;
2201
0
        uint8_t *chunk = malloc(chunk_size);
2202
0
        BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
2203
0
        err = blosc2_compress_ctx(array->sc->cctx, data, data_nbytes, chunk, chunk_size);
2204
0
        if (err < 0) {
2205
0
          BLOSC_TRACE_ERROR("Error compressing data");
2206
0
          BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
2207
0
        }
2208
0
        err = (int) blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
2209
0
        if (err < 0) {
2210
0
          BLOSC_TRACE_ERROR("Error updating chunk");
2211
0
          BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
2212
0
        }
2213
0
      }
2214
0
      free(data);
2215
0
      free(chunk_selection_size);
2216
0
      free(p_chunk_selection_0);
2217
0
      free(p_chunk_selection_1);
2218
0
    } else {
2219
0
      BLOSC_ERROR(iter_chunk(array, (int8_t) (ndim + 1), selection_size,
2220
0
                             ordered_selection, p_ordered_selection_0, p_ordered_selection_1,
2221
0
                             buffer, buffershape, bufferstrides, get));
2222
0
    }
2223
2224
0
    p_ordered_selection_0[ndim] = p_ordered_selection_1[ndim];
2225
0
  }
2226
0
  return BLOSC2_ERROR_SUCCESS;
2227
0
}
2228
2229
2230
int orthogonal_selection(b2nd_array_t *array, int64_t **selection, int64_t *selection_size, void *buffer,
2231
0
                         int64_t *buffershape, int64_t buffersize, bool get) {
2232
0
  BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
2233
0
  BLOSC_ERROR_NULL(selection, BLOSC2_ERROR_NULL_POINTER);
2234
0
  BLOSC_ERROR_NULL(selection_size, BLOSC2_ERROR_NULL_POINTER);
2235
2236
0
  int8_t ndim = array->ndim;
2237
2238
0
  for (int i = 0; i < ndim; ++i) {
2239
0
    BLOSC_ERROR_NULL(selection[i], BLOSC2_ERROR_NULL_POINTER);
2240
    // Check that indexes are not larger than array shape
2241
0
    for (int j = 0; j < selection_size[i]; ++j) {
2242
0
      if (selection[i][j] > array->shape[i]) {
2243
0
        BLOSC_ERROR(BLOSC2_ERROR_INVALID_INDEX);
2244
0
      }
2245
0
    }
2246
0
  }
2247
2248
  // Check buffer size
2249
0
  int64_t sel_size = array->sc->typesize;
2250
0
  for (int i = 0; i < ndim; ++i) {
2251
0
    sel_size *= selection_size[i];
2252
0
  }
2253
2254
0
  if (sel_size < buffersize) {
2255
0
    BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
2256
0
  }
2257
2258
  // Sort selections
2259
0
  b2nd_selection_t **ordered_selection = malloc(ndim * sizeof(b2nd_selection_t *));
2260
0
  BLOSC_ERROR_NULL(ordered_selection, BLOSC2_ERROR_MEMORY_ALLOC);
2261
0
  for (int i = 0; i < ndim; ++i) {
2262
0
    ordered_selection[i] = malloc(selection_size[i] * sizeof(b2nd_selection_t));
2263
0
    for (int j = 0; j < selection_size[i]; ++j) {
2264
0
      ordered_selection[i][j].index = j;
2265
0
      ordered_selection[i][j].value = selection[i][j];
2266
0
    }
2267
0
    qsort(ordered_selection[i], selection_size[i], sizeof(b2nd_selection_t), compare_selection);
2268
0
  }
2269
2270
  // Define pointers to iterate over ordered_selection data
2271
0
  b2nd_selection_t **p_ordered_selection_0 = malloc(ndim * sizeof(b2nd_selection_t *));
2272
0
  BLOSC_ERROR_NULL(p_ordered_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
2273
0
  b2nd_selection_t **p_ordered_selection_1 = malloc(ndim * sizeof(b2nd_selection_t *));
2274
0
  BLOSC_ERROR_NULL(p_ordered_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
2275
2276
0
  int64_t bufferstrides[B2ND_MAX_DIM];
2277
0
  bufferstrides[array->ndim - 1] = 1;
2278
0
  for (int i = array->ndim - 2; i >= 0; --i) {
2279
0
    bufferstrides[i] = bufferstrides[i + 1] * buffershape[i + 1];
2280
0
  }
2281
2282
0
  BLOSC_ERROR(iter_chunk(array, 0,
2283
0
                         selection_size, ordered_selection,
2284
0
                         p_ordered_selection_0,
2285
0
                         p_ordered_selection_1,
2286
0
                         buffer, buffershape, bufferstrides, get));
2287
2288
  // Free allocated memory
2289
0
  free(p_ordered_selection_0);
2290
0
  free(p_ordered_selection_1);
2291
0
  for (int i = 0; i < ndim; ++i) {
2292
0
    free(ordered_selection[i]);
2293
0
  }
2294
0
  free(ordered_selection);
2295
2296
0
  return BLOSC2_ERROR_SUCCESS;
2297
0
}
2298
2299
2300
int b2nd_get_orthogonal_selection(const b2nd_array_t *array, int64_t **selection, int64_t *selection_size, void *buffer,
2301
0
                                  int64_t *buffershape, int64_t buffersize) {
2302
0
  return orthogonal_selection((b2nd_array_t *)array, selection, selection_size, buffer, buffershape, buffersize, true);
2303
0
}
2304
2305
2306
int b2nd_set_orthogonal_selection(b2nd_array_t *array, int64_t **selection, int64_t *selection_size, const void *buffer,
2307
0
                                  int64_t *buffershape, int64_t buffersize) {
2308
0
  return orthogonal_selection(array, selection, selection_size, (void*)buffer, buffershape, buffersize, false);
2309
0
}
2310
2311
2312
b2nd_context_t *
2313
b2nd_create_ctx(const blosc2_storage *b2_storage, int8_t ndim, const int64_t *shape, const int32_t *chunkshape,
2314
                const int32_t *blockshape, const char *dtype, int8_t dtype_format, const blosc2_metalayer *metalayers,
2315
0
                int32_t nmetalayers) {
2316
0
  b2nd_context_t *ctx = malloc(sizeof(b2nd_context_t));
2317
0
  BLOSC_ERROR_NULL(ctx, NULL);
2318
0
  blosc2_storage *params_b2_storage = malloc(sizeof(blosc2_storage));
2319
0
  BLOSC_ERROR_NULL(params_b2_storage, NULL);
2320
0
  if (b2_storage == NULL) {
2321
0
    memcpy(params_b2_storage, &BLOSC2_STORAGE_DEFAULTS, sizeof(blosc2_storage));
2322
0
  }
2323
0
  else {
2324
0
    memcpy(params_b2_storage, b2_storage, sizeof(blosc2_storage));
2325
0
  }
2326
0
  blosc2_cparams *cparams = malloc(sizeof(blosc2_cparams));
2327
0
  BLOSC_ERROR_NULL(cparams, NULL);
2328
  // We need a copy of cparams mainly to be able to modify blocksize
2329
0
  if (params_b2_storage->cparams == NULL) {
2330
0
    memcpy(cparams, &BLOSC2_CPARAMS_DEFAULTS, sizeof(blosc2_cparams));
2331
0
  }
2332
0
  else {
2333
0
    memcpy(cparams, params_b2_storage->cparams, sizeof(blosc2_cparams));
2334
0
  }
2335
2336
0
  if (dtype == NULL) {
2337
    // ctx->dtype = strdup(B2ND_DEFAULT_DTYPE);
2338
0
    char buf[16] = {0};
2339
0
    snprintf(buf, sizeof(buf), "|S%d", cparams->typesize);
2340
0
    ctx->dtype = strdup(buf);
2341
0
  }
2342
0
  else {
2343
0
    ctx->dtype = strdup(dtype);
2344
0
  }
2345
0
  ctx->dtype_format = dtype_format;
2346
2347
0
  params_b2_storage->cparams = cparams;
2348
0
  ctx->b2_storage = params_b2_storage;
2349
0
  ctx->ndim = ndim;
2350
0
  int32_t blocknitems = 1;
2351
0
  for (int i = 0; i < ndim; i++) {
2352
0
    ctx->shape[i] = shape[i];
2353
0
    ctx->chunkshape[i] = chunkshape[i];
2354
0
    ctx->blockshape[i] = blockshape[i];
2355
0
    blocknitems *= ctx->blockshape[i];
2356
0
  }
2357
0
  cparams->blocksize = blocknitems * cparams->typesize;
2358
2359
0
  ctx->nmetalayers = nmetalayers;
2360
0
  for (int i = 0; i < nmetalayers; ++i) {
2361
0
    ctx->metalayers[i] = metalayers[i];
2362
0
  }
2363
2364
#if defined(HAVE_PLUGINS)
2365
  #include "blosc2/codecs-registry.h"
2366
  if ((ctx->b2_storage->cparams->compcode >= BLOSC_CODEC_ZFP_FIXED_ACCURACY) &&
2367
      (ctx->b2_storage->cparams->compcode <= BLOSC_CODEC_ZFP_FIXED_RATE)) {
2368
    for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) {
2369
      if ((ctx->b2_storage->cparams->filters[i] == BLOSC_SHUFFLE) ||
2370
          (ctx->b2_storage->cparams->filters[i] == BLOSC_BITSHUFFLE)) {
2371
        BLOSC_TRACE_ERROR("ZFP cannot be run in presence of SHUFFLE / BITSHUFFLE");
2372
        return NULL;
2373
      }
2374
    }
2375
  }
2376
#endif /* HAVE_PLUGINS */
2377
2378
0
  return ctx;
2379
0
}
2380
2381
2382
0
int b2nd_free_ctx(b2nd_context_t *ctx) {
2383
0
  ctx->b2_storage->cparams->schunk = NULL;
2384
0
  free(ctx->b2_storage->cparams);
2385
0
  free(ctx->b2_storage);
2386
0
  free(ctx->dtype);
2387
0
  free(ctx);
2388
2389
0
  return BLOSC2_ERROR_SUCCESS;
2390
0
}