Coverage Report

Created: 2025-06-20 06:13

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