Coverage Report

Created: 2025-09-04 06:36

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