| 1 |
/********************************************************************* |
| 2 |
Blosc - Blocked Shuffling and Compression Library |
| 3 |
|
| 4 |
Copyright (c) 2021 Blosc Development Team <[email protected]> |
| 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 "frame.h" |
| 12 |
#include "sframe.h" |
| 13 |
#include "context.h" |
| 14 |
#include "blosc-private.h" |
| 15 |
#include "blosc2.h" |
| 16 |
|
| 17 |
#include <sys/stat.h> |
| 18 |
#if defined(_WIN32) |
| 19 |
#include <windows.h> |
| 20 |
#include <malloc.h> |
| 21 |
// See https://github.com/Blosc/python-blosc2/issues/359#issuecomment-2625380236 |
| 22 |
#define stat _stat64 |
| 23 |
#endif /* _WIN32 */ |
| 24 |
|
| 25 |
#include <inttypes.h> |
| 26 |
#include <stdbool.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdint.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
/* If C11 is supported, use it's built-in aligned allocation. */ |
| 33 |
#if __STDC_VERSION__ >= 201112L |
| 34 |
#include <stdalign.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
/* Create a new (empty) frame */ |
| 39 |
blosc2_frame_s* frame_new(const char* urlpath) { |
| 40 |
blosc2_frame_s* new_frame = calloc(1, sizeof(blosc2_frame_s)); |
| 41 |
if (urlpath != NULL) { |
| 42 |
char* new_urlpath = malloc(strlen(urlpath) + 1); // + 1 for the trailing NULL |
| 43 |
new_frame->urlpath = strcpy(new_urlpath, urlpath); |
| 44 |
new_frame->file_offset = 0; |
| 45 |
} |
| 46 |
return new_frame; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/* Free memory from a frame. */ |
| 51 |
int frame_free(blosc2_frame_s* frame) { |
| 52 |
|
| 53 |
if (frame->cframe != NULL && !frame->avoid_cframe_free) { |
| 54 |
free(frame->cframe); |
| 55 |
} |
| 56 |
|
| 57 |
if (frame->coffsets != NULL && frame->coffsets_needs_free) { |
| 58 |
free(frame->coffsets); |
| 59 |
} |
| 60 |
|
| 61 |
if (frame->urlpath != NULL) { |
| 62 |
free(frame->urlpath); |
| 63 |
} |
| 64 |
|
| 65 |
free(frame); |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
void *new_header_frame(blosc2_schunk *schunk, blosc2_frame_s *frame) { |
| 72 |
if (frame == NULL) { |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
uint8_t* h2 = calloc(FRAME_HEADER_MINLEN, 1); |
| 76 |
uint8_t* h2p = h2; |
| 77 |
|
| 78 |
// The msgpack header starts here |
| 79 |
*h2p = 0x90; // fixarray... |
| 80 |
*h2p += 14; // ...with 13 elements |
| 81 |
h2p += 1; |
| 82 |
|
| 83 |
// Magic number |
| 84 |
*h2p = 0xa0 + 8; // str with 8 elements |
| 85 |
h2p += 1; |
| 86 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
strcpy((char*)h2p, "b2frame"); |
| 90 |
h2p += 8; |
| 91 |
|
| 92 |
// Header size |
| 93 |
*h2p = 0xd2; // int32 |
| 94 |
h2p += 1 + 4; |
| 95 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
// Total frame size |
| 100 |
*h2p = 0xcf; // uint64 |
| 101 |
// Fill it with frame->len which is known *after* the creation of the frame (e.g. when updating the header) |
| 102 |
int64_t flen = frame->len; |
| 103 |
to_big(h2 + FRAME_LEN, &flen, sizeof(flen)); |
| 104 |
h2p += 1 + 8; |
| 105 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
// Flags |
| 110 |
*h2p = 0xa0 + 4; // str with 4 elements |
| 111 |
h2p += 1; |
| 112 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
// General flags |
| 116 |
*h2p = BLOSC2_VERSION_FRAME_FORMAT; // version |
| 117 |
*h2p += 0x10; // 64-bit offsets. We only support this for now. |
| 118 |
h2p += 1; |
| 119 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
// Frame type |
| 124 |
// We only support contiguous and sparse directories frames currently |
| 125 |
*h2p = frame->sframe ? 1 : 0; |
| 126 |
h2p += 1; |
| 127 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
|
| 131 |
// Codec flags |
| 132 |
*h2p = schunk->compcode; |
| 133 |
if (schunk->compcode >= BLOSC_LAST_CODEC) { |
| 134 |
*h2p = BLOSC_UDCODEC_FORMAT; |
| 135 |
} |
| 136 |
*h2p += (schunk->clevel) << 4u; // clevel |
| 137 |
h2p += 1; |
| 138 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
|
| 142 |
// Other flags |
| 143 |
*h2p = schunk->splitmode - 1; |
| 144 |
h2p += 1; |
| 145 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 146 |
return NULL; |
| 147 |
} |
| 148 |
|
| 149 |
// Uncompressed size |
| 150 |
*h2p = 0xd3; // int64 |
| 151 |
h2p += 1; |
| 152 |
int64_t nbytes = schunk->nbytes; |
| 153 |
to_big(h2p, &nbytes, sizeof(nbytes)); |
| 154 |
h2p += 8; |
| 155 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
// Compressed size |
| 160 |
*h2p = 0xd3; // int64 |
| 161 |
h2p += 1; |
| 162 |
int64_t cbytes = schunk->cbytes; |
| 163 |
to_big(h2p, &cbytes, sizeof(cbytes)); |
| 164 |
h2p += 8; |
| 165 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
|
| 169 |
// Type size |
| 170 |
*h2p = 0xd2; // int32 |
| 171 |
h2p += 1; |
| 172 |
int32_t typesize = schunk->typesize; |
| 173 |
to_big(h2p, &typesize, sizeof(typesize)); |
| 174 |
h2p += 4; |
| 175 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
|
| 179 |
// Block size |
| 180 |
*h2p = 0xd2; // int32 |
| 181 |
h2p += 1; |
| 182 |
int32_t blocksize = schunk->blocksize; |
| 183 |
to_big(h2p, &blocksize, sizeof(blocksize)); |
| 184 |
h2p += 4; |
| 185 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
|
| 189 |
// Chunk size |
| 190 |
*h2p = 0xd2; // int32 |
| 191 |
h2p += 1; |
| 192 |
int32_t chunksize = schunk->chunksize; |
| 193 |
to_big(h2p, &chunksize, sizeof(chunksize)); |
| 194 |
h2p += 4; |
| 195 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
// Number of threads for compression |
| 200 |
*h2p = 0xd1; // int16 |
| 201 |
h2p += 1; |
| 202 |
int16_t nthreads = (int16_t)schunk->cctx->nthreads; |
| 203 |
to_big(h2p, &nthreads, sizeof(nthreads)); |
| 204 |
h2p += 2; |
| 205 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 206 |
return NULL; |
| 207 |
} |
| 208 |
|
| 209 |
// Number of threads for decompression |
| 210 |
*h2p = 0xd1; // int16 |
| 211 |
h2p += 1; |
| 212 |
nthreads = (int16_t)schunk->dctx->nthreads; |
| 213 |
to_big(h2p, &nthreads, sizeof(nthreads)); |
| 214 |
h2p += 2; |
| 215 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
|
| 219 |
// The boolean for variable-length metalayers |
| 220 |
*h2p = (schunk->nvlmetalayers > 0) ? (uint8_t)0xc3 : (uint8_t)0xc2; |
| 221 |
h2p += 1; |
| 222 |
if (h2p - h2 >= FRAME_HEADER_MINLEN) { |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
|
| 226 |
// The space for FRAME_FILTER_PIPELINE |
| 227 |
*h2p = 0xd8; // fixext 16 |
| 228 |
h2p += 1; |
| 229 |
if (BLOSC2_MAX_FILTERS > FRAME_FILTER_PIPELINE_MAX) { |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
// Store the filter pipeline in header |
| 233 |
uint8_t* mp_filters = h2 + FRAME_FILTER_PIPELINE + 1; |
| 234 |
uint8_t* mp_meta = h2 + FRAME_FILTER_PIPELINE + 1 + FRAME_FILTER_PIPELINE_MAX; |
| 235 |
for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) { |
| 236 |
mp_filters[i] = schunk->filters[i]; |
| 237 |
mp_meta[i] = schunk->filters_meta[i]; |
| 238 |
} |
| 239 |
*h2p = (uint8_t) BLOSC2_MAX_FILTERS; |
| 240 |
h2p += 1; |
| 241 |
h2p += 16; |
| 242 |
|
| 243 |
// User-defined codec and codec metadata |
| 244 |
uint8_t* udcodec = h2 + FRAME_UDCODEC; |
| 245 |
*udcodec = schunk->compcode; |
| 246 |
uint8_t* codec_meta = h2 + FRAME_CODEC_META; |
| 247 |
*codec_meta = schunk->compcode_meta; |
| 248 |
|
| 249 |
if (h2p - h2 != FRAME_HEADER_MINLEN) { |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 253 |
int32_t hsize = FRAME_HEADER_MINLEN; |
| 254 |
|
| 255 |
// Now, deal with metalayers |
| 256 |
uint16_t nmetalayers = schunk->nmetalayers; |
| 257 |
if (nmetalayers > BLOSC2_MAX_METALAYERS) { |
| 258 |
return NULL; |
| 259 |
} |
| 260 |
|
| 261 |
// Make space for the header of metalayers (array marker, size, map of offsets) |
| 262 |
h2 = realloc(h2, (size_t)hsize + 1 + 1 + 2 + 1 + 2); |
| 263 |
h2p = h2 + hsize; |
| 264 |
|
| 265 |
// The msgpack header for the metalayers (array_marker, size, map of offsets, list of metalayers) |
| 266 |
*h2p = 0x90 + 3; // array with 3 elements |
| 267 |
h2p += 1; |
| 268 |
|
| 269 |
// Size for the map (index) of offsets, including this uint16 size (to be filled out later on) |
| 270 |
*h2p = 0xcd; // uint16 |
| 271 |
h2p += 1 + 2; |
| 272 |
|
| 273 |
// Map (index) of offsets for optional metalayers |
| 274 |
*h2p = 0xde; // map 16 with N keys |
| 275 |
h2p += 1; |
| 276 |
to_big(h2p, &nmetalayers, sizeof(nmetalayers)); |
| 277 |
h2p += sizeof(nmetalayers); |
| 278 |
int32_t current_header_len = (int32_t)(h2p - h2); |
| 279 |
int32_t *offtooff = malloc(nmetalayers * sizeof(int32_t)); |
| 280 |
for (int nmetalayer = 0; nmetalayer < nmetalayers; nmetalayer++) { |
| 281 |
if (frame == NULL) { |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
blosc2_metalayer *metalayer = schunk->metalayers[nmetalayer]; |
| 285 |
uint8_t namelen = (uint8_t) strlen(metalayer->name); |
| 286 |
h2 = realloc(h2, (size_t)current_header_len + 1 + namelen + 1 + 4); |
| 287 |
h2p = h2 + current_header_len; |
| 288 |
// Store the metalayer |
| 289 |
if (namelen >= (1U << 5U)) { // metalayer strings cannot be longer than 32 bytes |
| 290 |
free(offtooff); |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
*h2p = (uint8_t)0xa0 + namelen; // str |
| 294 |
h2p += 1; |
| 295 |
memcpy(h2p, metalayer->name, namelen); |
| 296 |
h2p += namelen; |
| 297 |
// Space for storing the offset for the value of this metalayer |
| 298 |
*h2p = 0xd2; // int32 |
| 299 |
h2p += 1; |
| 300 |
offtooff[nmetalayer] = (int32_t)(h2p - h2); |
| 301 |
h2p += 4; |
| 302 |
current_header_len += 1 + namelen + 1 + 4; |
| 303 |
} |
| 304 |
int32_t hsize2 = (int32_t)(h2p - h2); |
| 305 |
if (hsize2 != current_header_len) { // sanity check |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
// Map size + int16 size |
| 310 |
if ((uint32_t) (hsize2 - hsize) >= (1U << 16U)) { |
| 311 |
return NULL; |
| 312 |
} |
| 313 |
uint16_t map_size = (uint16_t) (hsize2 - hsize); |
| 314 |
to_big(h2 + FRAME_IDX_SIZE, &map_size, sizeof(map_size)); |
| 315 |
|
| 316 |
// Make space for an (empty) array |
| 317 |
hsize = (int32_t)(h2p - h2); |
| 318 |
h2 = realloc(h2, (size_t)hsize + 2 + 1 + 2); |
| 319 |
h2p = h2 + hsize; |
| 320 |
|
| 321 |
// Now, store the values in an array |
| 322 |
*h2p = 0xdc; // array 16 with N elements |
| 323 |
h2p += 1; |
| 324 |
to_big(h2p, &nmetalayers, sizeof(nmetalayers)); |
| 325 |
h2p += sizeof(nmetalayers); |
| 326 |
current_header_len = (int32_t)(h2p - h2); |
| 327 |
for (int nmetalayer = 0; nmetalayer < nmetalayers; nmetalayer++) { |
| 328 |
if (frame == NULL) { |
| 329 |
return NULL; |
| 330 |
} |
| 331 |
blosc2_metalayer *metalayer = schunk->metalayers[nmetalayer]; |
| 332 |
h2 = realloc(h2, (size_t)current_header_len + 1 + 4 + metalayer->content_len); |
| 333 |
h2p = h2 + current_header_len; |
| 334 |
// Store the serialized contents for this metalayer |
| 335 |
*h2p = 0xc6; // bin32 |
| 336 |
h2p += 1; |
| 337 |
to_big(h2p, &(metalayer->content_len), sizeof(metalayer->content_len)); |
| 338 |
h2p += 4; |
| 339 |
memcpy(h2p, metalayer->content, metalayer->content_len); // buffer, no need to swap |
| 340 |
h2p += metalayer->content_len; |
| 341 |
// Update the offset now that we know it |
| 342 |
to_big(h2 + offtooff[nmetalayer], ¤t_header_len, sizeof(current_header_len)); |
| 343 |
current_header_len += 1 + 4 + metalayer->content_len; |
| 344 |
} |
| 345 |
free(offtooff); |
| 346 |
hsize = (int32_t)(h2p - h2); |
| 347 |
if (hsize != current_header_len) { // sanity check |
| 348 |
return NULL; |
| 349 |
} |
| 350 |
|
| 351 |
// Set the length of the whole header now that we know it |
| 352 |
to_big(h2 + FRAME_HEADER_LEN, &hsize, sizeof(hsize)); |
| 353 |
|
| 354 |
return h2; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
int get_header_info(blosc2_frame_s *frame, int32_t *header_len, int64_t *frame_len, int64_t *nbytes, int64_t *cbytes, |
| 359 |
int32_t *blocksize, int32_t *chunksize, int64_t *nchunks, int32_t *typesize, uint8_t *compcode, |
| 360 |
uint8_t *compcode_meta, uint8_t *clevel, uint8_t *filters, uint8_t *filters_meta, |
| 361 |
uint8_t *splitmode, const blosc2_io *io) { |
| 362 |
uint8_t* framep = frame->cframe; |
| 363 |
uint8_t* header_ptr; |
| 364 |
uint8_t header[FRAME_HEADER_MINLEN]; |
| 365 |
|
| 366 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(io->id); |
| 367 |
if (io_cb == NULL) { |
| 368 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 369 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 370 |
} |
| 371 |
|
| 372 |
if (frame->len <= 0) { |
| 373 |
return BLOSC2_ERROR_READ_BUFFER; |
| 374 |
} |
| 375 |
|
| 376 |
if (frame->cframe == NULL) { |
| 377 |
int64_t rbytes = 0; |
| 378 |
void* fp = NULL; |
| 379 |
int64_t io_pos = 0; |
| 380 |
if (frame->sframe) { |
| 381 |
fp = sframe_open_index(frame->urlpath, "rb", io); |
| 382 |
if (fp == NULL) { |
| 383 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 384 |
return BLOSC2_ERROR_FILE_OPEN; |
| 385 |
} |
| 386 |
} |
| 387 |
else { |
| 388 |
fp = io_cb->open(frame->urlpath, "rb", io->params); |
| 389 |
if (fp == NULL) { |
| 390 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 391 |
return BLOSC2_ERROR_FILE_OPEN; |
| 392 |
} |
| 393 |
io_pos = frame->file_offset; |
| 394 |
} |
| 395 |
if (io_cb->is_allocation_necessary) |
| 396 |
header_ptr = header; |
| 397 |
rbytes = io_cb->read((void**)&header_ptr, 1, FRAME_HEADER_MINLEN, io_pos, fp); |
| 398 |
io_cb->close(fp); |
| 399 |
if (rbytes != FRAME_HEADER_MINLEN) { |
| 400 |
return BLOSC2_ERROR_FILE_READ; |
| 401 |
} |
| 402 |
framep = header_ptr; |
| 403 |
} |
| 404 |
|
| 405 |
// Consistency check for frame type |
| 406 |
uint8_t frame_type = framep[FRAME_TYPE]; |
| 407 |
if (frame->sframe) { |
| 408 |
if (frame_type != FRAME_DIRECTORY_TYPE) { |
| 409 |
return BLOSC2_ERROR_FRAME_TYPE; |
| 410 |
} |
| 411 |
} else { |
| 412 |
if (frame_type != FRAME_CONTIGUOUS_TYPE) { |
| 413 |
return BLOSC2_ERROR_FRAME_TYPE; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
// Fetch some internal lengths |
| 418 |
from_big(header_len, framep + FRAME_HEADER_LEN, sizeof(*header_len)); |
| 419 |
if (*header_len < FRAME_HEADER_MINLEN) { |
| 420 |
BLOSC_TRACE_ERROR("Header length is zero or smaller than min allowed."); |
| 421 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 422 |
} |
| 423 |
from_big(frame_len, framep + FRAME_LEN, sizeof(*frame_len)); |
| 424 |
if (*header_len > *frame_len) { |
| 425 |
BLOSC_TRACE_ERROR("Header length exceeds length of the frame."); |
| 426 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 427 |
} |
| 428 |
from_big(nbytes, framep + FRAME_NBYTES, sizeof(*nbytes)); |
| 429 |
from_big(cbytes, framep + FRAME_CBYTES, sizeof(*cbytes)); |
| 430 |
from_big(blocksize, framep + FRAME_BLOCKSIZE, sizeof(*blocksize)); |
| 431 |
if (chunksize != NULL) { |
| 432 |
from_big(chunksize, framep + FRAME_CHUNKSIZE, sizeof(*chunksize)); |
| 433 |
} |
| 434 |
if (typesize != NULL) { |
| 435 |
from_big(typesize, framep + FRAME_TYPESIZE, sizeof(*typesize)); |
| 436 |
if (*typesize <= 0) { |
| 437 |
BLOSC_TRACE_ERROR("`typesize` cannot be zero or negative."); |
| 438 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
// Codecs |
| 443 |
uint8_t frame_codecs = framep[FRAME_CODECS]; |
| 444 |
if (clevel != NULL) { |
| 445 |
*clevel = frame_codecs >> 4u; |
| 446 |
} |
| 447 |
if (compcode != NULL) { |
| 448 |
*compcode = frame_codecs & 0xFu; |
| 449 |
if (*compcode == BLOSC_UDCODEC_FORMAT) { |
| 450 |
from_big(compcode, framep + FRAME_UDCODEC, sizeof(*compcode)); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
// Other flags |
| 455 |
uint8_t other_flags = framep[FRAME_OTHER_FLAGS]; |
| 456 |
if (splitmode != NULL) { |
| 457 |
*splitmode = other_flags & 0x4u; |
| 458 |
from_big(splitmode, framep + FRAME_OTHER_FLAGS, sizeof(*splitmode)); |
| 459 |
*splitmode += 1; |
| 460 |
} |
| 461 |
|
| 462 |
if (compcode_meta != NULL) { |
| 463 |
from_big(compcode_meta, framep + FRAME_CODEC_META, sizeof(*compcode_meta)); |
| 464 |
} |
| 465 |
|
| 466 |
// Filters |
| 467 |
if (filters != NULL && filters_meta != NULL) { |
| 468 |
uint8_t nfilters = framep[FRAME_FILTER_PIPELINE]; |
| 469 |
if (nfilters > BLOSC2_MAX_FILTERS) { |
| 470 |
BLOSC_TRACE_ERROR("The number of filters in frame header are too large for Blosc2."); |
| 471 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 472 |
} |
| 473 |
uint8_t *filters_ = framep + FRAME_FILTER_PIPELINE + 1; |
| 474 |
uint8_t *filters_meta_ = framep + FRAME_FILTER_PIPELINE + 1 + FRAME_FILTER_PIPELINE_MAX; |
| 475 |
for (int i = 0; i < nfilters; i++) { |
| 476 |
filters[i] = filters_[i]; |
| 477 |
filters_meta[i] = filters_meta_[i]; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if (*nbytes > 0 && *chunksize > 0) { |
| 482 |
// We can compute the number of chunks only when the frame has actual data |
| 483 |
*nchunks = *nbytes / *chunksize; |
| 484 |
if (*nbytes % *chunksize > 0) { |
| 485 |
if (*nchunks == INT32_MAX) { |
| 486 |
BLOSC_TRACE_ERROR("Number of chunks exceeds maximum allowed."); |
| 487 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 488 |
} |
| 489 |
*nchunks += 1; |
| 490 |
} |
| 491 |
|
| 492 |
// Sanity check for compressed sizes |
| 493 |
if ((*cbytes < 0) || ((int64_t)*nchunks * *chunksize < *nbytes)) { |
| 494 |
BLOSC_TRACE_ERROR("Invalid compressed size in frame header."); |
| 495 |
return BLOSC2_ERROR_INVALID_HEADER; |
| 496 |
} |
| 497 |
} else { |
| 498 |
*nchunks = 0; |
| 499 |
} |
| 500 |
|
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
int64_t get_trailer_offset(blosc2_frame_s *frame, int32_t header_len, bool has_coffsets) { |
| 506 |
if (!has_coffsets) { |
| 507 |
// No data chunks yet |
| 508 |
return header_len; |
| 509 |
} |
| 510 |
return frame->len - frame->trailer_len; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
// Update the length in the header |
| 515 |
int update_frame_len(blosc2_frame_s* frame, int64_t len) { |
| 516 |
int rc = 1; |
| 517 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 518 |
if (io_cb == NULL) { |
| 519 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 520 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 521 |
} |
| 522 |
|
| 523 |
if (frame->cframe != NULL) { |
| 524 |
to_big(frame->cframe + FRAME_LEN, &len, sizeof(int64_t)); |
| 525 |
} |
| 526 |
else { |
| 527 |
void* fp = NULL; |
| 528 |
if (frame->sframe) { |
| 529 |
fp = sframe_open_index(frame->urlpath, "rb+", |
| 530 |
frame->schunk->storage->io); |
| 531 |
} |
| 532 |
else { |
| 533 |
fp = io_cb->open(frame->urlpath, "rb+", frame->schunk->storage->io->params); |
| 534 |
} |
| 535 |
if (fp == NULL) { |
| 536 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 537 |
return BLOSC2_ERROR_FILE_OPEN; |
| 538 |
} |
| 539 |
int64_t io_pos = frame->file_offset + FRAME_LEN; |
| 540 |
int64_t swap_len; |
| 541 |
to_big(&swap_len, &len, sizeof(int64_t)); |
| 542 |
int64_t wbytes = io_cb->write(&swap_len, 1, sizeof(int64_t), io_pos, fp); |
| 543 |
io_cb->close(fp); |
| 544 |
if (wbytes != sizeof(int64_t)) { |
| 545 |
BLOSC_TRACE_ERROR("Cannot write the frame length in header."); |
| 546 |
return BLOSC2_ERROR_FILE_WRITE; |
| 547 |
} |
| 548 |
} |
| 549 |
return rc; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
int frame_update_trailer(blosc2_frame_s* frame, blosc2_schunk* schunk) { |
| 554 |
if (frame != NULL && frame->len == 0) { |
| 555 |
BLOSC_TRACE_ERROR("The trailer cannot be updated on empty frames."); |
| 556 |
} |
| 557 |
|
| 558 |
// Create the trailer in msgpack (see the frame format document) |
| 559 |
uint32_t trailer_len = FRAME_TRAILER_MINLEN; |
| 560 |
uint8_t* trailer = (uint8_t*)calloc((size_t)trailer_len, 1); |
| 561 |
uint8_t* ptrailer = trailer; |
| 562 |
*ptrailer = 0x90 + 4; // fixarray with 4 elements |
| 563 |
ptrailer += 1; |
| 564 |
// Trailer format version |
| 565 |
*ptrailer = FRAME_TRAILER_VERSION; |
| 566 |
ptrailer += 1; |
| 567 |
|
| 568 |
int32_t current_trailer_len = (int32_t)(ptrailer - trailer); |
| 569 |
|
| 570 |
// Now, deal with variable-length metalayers |
| 571 |
int16_t nvlmetalayers = schunk->nvlmetalayers; |
| 572 |
if (nvlmetalayers < 0 || nvlmetalayers > BLOSC2_MAX_METALAYERS) { |
| 573 |
return -1; |
| 574 |
} |
| 575 |
|
| 576 |
// Make space for the header of metalayers (array marker, size, map of offsets) |
| 577 |
trailer = realloc(trailer, (size_t) current_trailer_len + 1 + 1 + 2 + 1 + 2); |
| 578 |
ptrailer = trailer + current_trailer_len; |
| 579 |
|
| 580 |
// The msgpack header for the metalayers (array_marker, size, map of offsets, list of metalayers) |
| 581 |
*ptrailer = 0x90 + 3; // array with 3 elements |
| 582 |
ptrailer += 1; |
| 583 |
|
| 584 |
int32_t tsize = (int32_t)(ptrailer - trailer); |
| 585 |
|
| 586 |
// Size for the map (index) of metalayer offsets, including this uint16 size (to be filled out later on) |
| 587 |
*ptrailer = 0xcd; // uint16 |
| 588 |
ptrailer += 1 + 2; |
| 589 |
|
| 590 |
// Map (index) of offsets for optional metalayers |
| 591 |
*ptrailer = 0xde; // map 16 with N keys |
| 592 |
ptrailer += 1; |
| 593 |
to_big(ptrailer, &nvlmetalayers, sizeof(nvlmetalayers)); |
| 594 |
ptrailer += sizeof(nvlmetalayers); |
| 595 |
current_trailer_len = (int32_t)(ptrailer - trailer); |
| 596 |
int32_t *offtodata = malloc(nvlmetalayers * sizeof(int32_t)); |
| 597 |
for (int nvlmetalayer = 0; nvlmetalayer < nvlmetalayers; nvlmetalayer++) { |
| 598 |
if (frame == NULL) { |
| 599 |
return -1; |
| 600 |
} |
| 601 |
blosc2_metalayer *vlmetalayer = schunk->vlmetalayers[nvlmetalayer]; |
| 602 |
uint8_t name_len = (uint8_t) strlen(vlmetalayer->name); |
| 603 |
trailer = realloc(trailer, (size_t)current_trailer_len + 1 + name_len + 1 + 4); |
| 604 |
ptrailer = trailer + current_trailer_len; |
| 605 |
// Store the vlmetalayer |
| 606 |
if (name_len >= (1U << 5U)) { // metalayer strings cannot be longer than 32 bytes |
| 607 |
free(offtodata); |
| 608 |
return -1; |
| 609 |
} |
| 610 |
*ptrailer = (uint8_t)0xa0 + name_len; // str |
| 611 |
ptrailer += 1; |
| 612 |
memcpy(ptrailer, vlmetalayer->name, name_len); |
| 613 |
ptrailer += name_len; |
| 614 |
// Space for storing the offset for the value of this vlmetalayer |
| 615 |
*ptrailer = 0xd2; // int32 |
| 616 |
ptrailer += 1; |
| 617 |
offtodata[nvlmetalayer] = (int32_t)(ptrailer - trailer); |
| 618 |
ptrailer += 4; |
| 619 |
current_trailer_len += 1 + name_len + 1 + 4; |
| 620 |
} |
| 621 |
int32_t tsize2 = (int32_t)(ptrailer - trailer); |
| 622 |
if (tsize2 != current_trailer_len) { // sanity check |
| 623 |
return -1; |
| 624 |
} |
| 625 |
|
| 626 |
// Map size + int16 size |
| 627 |
if ((uint32_t) (tsize2 - tsize) >= (1U << 16U)) { |
| 628 |
return -1; |
| 629 |
} |
| 630 |
uint16_t map_size = (uint16_t) (tsize2 - tsize); |
| 631 |
to_big(trailer + 4, &map_size, sizeof(map_size)); |
| 632 |
|
| 633 |
// Make space for an (empty) array |
| 634 |
tsize = (int32_t)(ptrailer - trailer); |
| 635 |
trailer = realloc(trailer, (size_t) tsize + 2 + 1 + 2); |
| 636 |
ptrailer = trailer + tsize; |
| 637 |
|
| 638 |
// Now, store the values in an array |
| 639 |
*ptrailer = 0xdc; // array 16 with N elements |
| 640 |
ptrailer += 1; |
| 641 |
to_big(ptrailer, &nvlmetalayers, sizeof(nvlmetalayers)); |
| 642 |
ptrailer += sizeof(nvlmetalayers); |
| 643 |
current_trailer_len = (int32_t)(ptrailer - trailer); |
| 644 |
for (int nvlmetalayer = 0; nvlmetalayer < nvlmetalayers; nvlmetalayer++) { |
| 645 |
if (frame == NULL) { |
| 646 |
return -1; |
| 647 |
} |
| 648 |
blosc2_metalayer *vlmetalayer = schunk->vlmetalayers[nvlmetalayer]; |
| 649 |
trailer = realloc(trailer, (size_t)current_trailer_len + 1 + 4 + vlmetalayer->content_len); |
| 650 |
ptrailer = trailer + current_trailer_len; |
| 651 |
// Store the serialized contents for this vlmetalayer |
| 652 |
*ptrailer = 0xc6; // bin32 |
| 653 |
ptrailer += 1; |
| 654 |
to_big(ptrailer, &(vlmetalayer->content_len), sizeof(vlmetalayer->content_len)); |
| 655 |
ptrailer += 4; |
| 656 |
memcpy(ptrailer, vlmetalayer->content, vlmetalayer->content_len); // buffer, no need to swap |
| 657 |
ptrailer += vlmetalayer->content_len; |
| 658 |
// Update the offset now that we know it |
| 659 |
to_big(trailer + offtodata[nvlmetalayer], ¤t_trailer_len, sizeof(current_trailer_len)); |
| 660 |
current_trailer_len += 1 + 4 + vlmetalayer->content_len; |
| 661 |
} |
| 662 |
free(offtodata); |
| 663 |
tsize = (int32_t)(ptrailer - trailer); |
| 664 |
if (tsize != current_trailer_len) { // sanity check |
| 665 |
return -1; |
| 666 |
} |
| 667 |
|
| 668 |
trailer = realloc(trailer, (size_t)current_trailer_len + 23); |
| 669 |
ptrailer = trailer + current_trailer_len; |
| 670 |
trailer_len = (ptrailer - trailer) + 23; |
| 671 |
|
| 672 |
// Trailer length |
| 673 |
*ptrailer = 0xce; // uint32 |
| 674 |
ptrailer += 1; |
| 675 |
to_big(ptrailer, &trailer_len, sizeof(uint32_t)); |
| 676 |
ptrailer += sizeof(uint32_t); |
| 677 |
// Up to 16 bytes for frame fingerprint (using XXH3 included in https://github.com/Cyan4973/xxHash) |
| 678 |
// Maybe someone would need 256-bit in the future, but for the time being 128-bit seems like a good tradeoff |
| 679 |
*ptrailer = 0xd8; // fixext 16 |
| 680 |
ptrailer += 1; |
| 681 |
*ptrailer = 0; // fingerprint type: 0 -> no fp; 1 -> 32-bit; 2 -> 64-bit; 3 -> 128-bit |
| 682 |
ptrailer += 1; |
| 683 |
|
| 684 |
// Remove call to memset when we compute an actual fingerprint |
| 685 |
memset(ptrailer, 0, 16); |
| 686 |
// Uncomment call to memcpy when we compute an actual fingerprint |
| 687 |
// memcpy(ptrailer, xxh3_fingerprint, sizeof(xxh3_fingerprint)); |
| 688 |
ptrailer += 16; |
| 689 |
|
| 690 |
// Sanity check |
| 691 |
if (ptrailer - trailer != trailer_len) { |
| 692 |
return BLOSC2_ERROR_DATA; |
| 693 |
} |
| 694 |
|
| 695 |
int32_t header_len; |
| 696 |
int64_t frame_len; |
| 697 |
int64_t nbytes; |
| 698 |
int64_t cbytes; |
| 699 |
int32_t blocksize; |
| 700 |
int32_t chunksize; |
| 701 |
int64_t nchunks; |
| 702 |
int ret = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 703 |
&blocksize, &chunksize, &nchunks, |
| 704 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 705 |
frame->schunk->storage->io); |
| 706 |
if (ret < 0) { |
| 707 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 708 |
return ret; |
| 709 |
} |
| 710 |
|
| 711 |
int64_t trailer_offset = get_trailer_offset(frame, header_len, nbytes > 0); |
| 712 |
|
| 713 |
if (trailer_offset < BLOSC_EXTENDED_HEADER_LENGTH) { |
| 714 |
BLOSC_TRACE_ERROR("Unable to get trailer offset in frame."); |
| 715 |
return BLOSC2_ERROR_READ_BUFFER; |
| 716 |
} |
| 717 |
|
| 718 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 719 |
if (io_cb == NULL) { |
| 720 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 721 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 722 |
} |
| 723 |
// Update the trailer. As there are no internal offsets to the trailer section, |
| 724 |
// and it is always at the end of the frame, we can just write (or overwrite) it |
| 725 |
// at the end of the frame. |
| 726 |
if (frame->cframe != NULL) { |
| 727 |
frame->cframe = realloc(frame->cframe, (size_t)(trailer_offset + trailer_len)); |
| 728 |
if (frame->cframe == NULL) { |
| 729 |
BLOSC_TRACE_ERROR("Cannot realloc space for the frame."); |
| 730 |
return BLOSC2_ERROR_MEMORY_ALLOC; |
| 731 |
} |
| 732 |
memcpy(frame->cframe + trailer_offset, trailer, trailer_len); |
| 733 |
} |
| 734 |
else { |
| 735 |
void* fp = NULL; |
| 736 |
if (frame->sframe) { |
| 737 |
fp = sframe_open_index(frame->urlpath, "rb+", |
| 738 |
frame->schunk->storage->io); |
| 739 |
} |
| 740 |
else { |
| 741 |
fp = io_cb->open(frame->urlpath, "rb+", frame->schunk->storage->io->params); |
| 742 |
} |
| 743 |
if (fp == NULL) { |
| 744 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 745 |
return BLOSC2_ERROR_FILE_OPEN; |
| 746 |
} |
| 747 |
int64_t io_pos = frame->file_offset + trailer_offset; |
| 748 |
int64_t wbytes = io_cb->write(trailer, 1, trailer_len, io_pos, fp); |
| 749 |
if (wbytes != trailer_len) { |
| 750 |
BLOSC_TRACE_ERROR("Cannot write the trailer length in trailer."); |
| 751 |
return BLOSC2_ERROR_FILE_WRITE; |
| 752 |
} |
| 753 |
if (io_cb->truncate(fp, trailer_offset + trailer_len) != 0) { |
| 754 |
BLOSC_TRACE_ERROR("Cannot truncate the frame."); |
| 755 |
return BLOSC2_ERROR_FILE_TRUNCATE; |
| 756 |
} |
| 757 |
io_cb->close(fp); |
| 758 |
|
| 759 |
} |
| 760 |
free(trailer); |
| 761 |
|
| 762 |
int rc = update_frame_len(frame, trailer_offset + trailer_len); |
| 763 |
if (rc < 0) { |
| 764 |
return rc; |
| 765 |
} |
| 766 |
frame->len = trailer_offset + trailer_len; |
| 767 |
frame->trailer_len = trailer_len; |
| 768 |
|
| 769 |
return 1; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
// Remove a file:/// prefix |
| 774 |
// This is a temporary workaround for allowing to use proper URLs for local files/dirs |
| 775 |
static char* normalize_urlpath(const char* urlpath) { |
| 776 |
char* localpath = strstr(urlpath, "file:///"); |
| 777 |
if (localpath == urlpath) { |
| 778 |
// There is a file:/// prefix. Get rid of it. |
| 779 |
localpath += strlen("file:///"); |
| 780 |
} |
| 781 |
else { |
| 782 |
localpath = (char*)urlpath; |
| 783 |
} |
| 784 |
return localpath; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
/* Initialize a frame out of a file */ |
| 789 |
blosc2_frame_s* frame_from_file_offset(const char* urlpath, const blosc2_io *io, int64_t offset) { |
| 790 |
// Get the length of the frame |
| 791 |
uint8_t* header_ptr; |
| 792 |
uint8_t header[FRAME_HEADER_MINLEN]; |
| 793 |
uint8_t* trailer_ptr; |
| 794 |
uint8_t trailer[FRAME_TRAILER_MINLEN]; |
| 795 |
|
| 796 |
void* fp = NULL; |
| 797 |
bool sframe = false; |
| 798 |
struct stat path_stat; |
| 799 |
|
| 800 |
urlpath = normalize_urlpath(urlpath); |
| 801 |
|
| 802 |
if(stat(urlpath, &path_stat) < 0) { |
| 803 |
BLOSC_TRACE_ERROR("Cannot get information about the path %s.", urlpath); |
| 804 |
return NULL; |
| 805 |
} |
| 806 |
|
| 807 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(io->id); |
| 808 |
if (io_cb == NULL) { |
| 809 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 810 |
return NULL; |
| 811 |
} |
| 812 |
|
| 813 |
char* urlpath_cpy; |
| 814 |
if (path_stat.st_mode & S_IFDIR) { |
| 815 |
urlpath_cpy = malloc(strlen(urlpath) + 1); |
| 816 |
strcpy(urlpath_cpy, urlpath); |
| 817 |
char last_char = urlpath[strlen(urlpath) - 1]; |
| 818 |
if (last_char == '\\' || last_char == '/') { |
| 819 |
urlpath_cpy[strlen(urlpath) - 1] = '\0'; |
| 820 |
} |
| 821 |
else { |
| 822 |
} |
| 823 |
fp = sframe_open_index(urlpath_cpy, "rb", io); |
| 824 |
sframe = true; |
| 825 |
} |
| 826 |
else { |
| 827 |
urlpath_cpy = malloc(strlen(urlpath) + 1); |
| 828 |
strcpy(urlpath_cpy, urlpath); |
| 829 |
fp = io_cb->open(urlpath, "rb", io->params); |
| 830 |
} |
| 831 |
if (fp == NULL) { |
| 832 |
BLOSC_TRACE_ERROR("Error opening file in: %s", urlpath); |
| 833 |
return NULL; |
| 834 |
} |
| 835 |
|
| 836 |
if (io_cb->is_allocation_necessary) |
| 837 |
header_ptr = header; |
| 838 |
int64_t io_pos = offset; |
| 839 |
int64_t rbytes = io_cb->read((void**)&header_ptr, 1, FRAME_HEADER_MINLEN, io_pos, fp); |
| 840 |
if (rbytes != FRAME_HEADER_MINLEN) { |
| 841 |
BLOSC_TRACE_ERROR("Cannot read from file '%s'.", urlpath); |
| 842 |
io_cb->close(fp); |
| 843 |
free(urlpath_cpy); |
| 844 |
return NULL; |
| 845 |
} |
| 846 |
int64_t frame_len; |
| 847 |
to_big(&frame_len, header_ptr + FRAME_LEN, sizeof(frame_len)); |
| 848 |
|
| 849 |
blosc2_frame_s* frame = calloc(1, sizeof(blosc2_frame_s)); |
| 850 |
frame->urlpath = urlpath_cpy; |
| 851 |
frame->len = frame_len; |
| 852 |
frame->sframe = sframe; |
| 853 |
frame->file_offset = offset; |
| 854 |
|
| 855 |
// Now, the trailer length |
| 856 |
if (io_cb->is_allocation_necessary) |
| 857 |
trailer_ptr = trailer; |
| 858 |
io_pos = offset + frame_len - FRAME_TRAILER_MINLEN; |
| 859 |
rbytes = io_cb->read((void**)&trailer_ptr, 1, FRAME_TRAILER_MINLEN, io_pos, fp); |
| 860 |
io_cb->close(fp); |
| 861 |
if (rbytes != FRAME_TRAILER_MINLEN) { |
| 862 |
BLOSC_TRACE_ERROR("Cannot read from file '%s'.", urlpath); |
| 863 |
free(urlpath_cpy); |
| 864 |
free(frame); |
| 865 |
return NULL; |
| 866 |
} |
| 867 |
int trailer_offset = FRAME_TRAILER_MINLEN - FRAME_TRAILER_LEN_OFFSET; |
| 868 |
if (trailer_ptr[trailer_offset - 1] != 0xce) { |
| 869 |
free(urlpath_cpy); |
| 870 |
free(frame); |
| 871 |
return NULL; |
| 872 |
} |
| 873 |
uint32_t trailer_len; |
| 874 |
to_big(&trailer_len, trailer_ptr + trailer_offset, sizeof(trailer_len)); |
| 875 |
frame->trailer_len = trailer_len; |
| 876 |
|
| 877 |
return frame; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
/* Initialize a frame out of a contiguous frame buffer */ |
| 882 |
blosc2_frame_s* frame_from_cframe(uint8_t *cframe, int64_t len, bool copy) { |
| 883 |
// Get the length of the frame |
| 884 |
const uint8_t* header = cframe; |
| 885 |
int64_t frame_len; |
| 886 |
if (len < FRAME_HEADER_MINLEN) { |
| 887 |
return NULL; |
| 888 |
} |
| 889 |
|
| 890 |
from_big(&frame_len, header + FRAME_LEN, sizeof(frame_len)); |
| 891 |
if (frame_len != len) { // sanity check |
| 892 |
return NULL; |
| 893 |
} |
| 894 |
|
| 895 |
blosc2_frame_s* frame = calloc(1, sizeof(blosc2_frame_s)); |
| 896 |
frame->len = frame_len; |
| 897 |
frame->file_offset = 0; |
| 898 |
|
| 899 |
// Now, the trailer length |
| 900 |
const uint8_t* trailer = cframe + frame_len - FRAME_TRAILER_MINLEN; |
| 901 |
int trailer_offset = FRAME_TRAILER_MINLEN - FRAME_TRAILER_LEN_OFFSET; |
| 902 |
if (trailer[trailer_offset - 1] != 0xce) { |
| 903 |
free(frame); |
| 904 |
return NULL; |
| 905 |
} |
| 906 |
uint32_t trailer_len; |
| 907 |
from_big(&trailer_len, trailer + trailer_offset, sizeof(trailer_len)); |
| 908 |
frame->trailer_len = trailer_len; |
| 909 |
|
| 910 |
if (copy) { |
| 911 |
frame->cframe = malloc((size_t)len); |
| 912 |
memcpy(frame->cframe, cframe, (size_t)len); |
| 913 |
} |
| 914 |
else { |
| 915 |
frame->cframe = cframe; |
| 916 |
frame->avoid_cframe_free = true; |
| 917 |
} |
| 918 |
|
| 919 |
return frame; |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
/* Create a frame out of a super-chunk. */ |
| 924 |
int64_t frame_from_schunk(blosc2_schunk *schunk, blosc2_frame_s *frame) { |
| 925 |
frame->file_offset = 0; |
| 926 |
int64_t nchunks = schunk->nchunks; |
| 927 |
int64_t cbytes = schunk->cbytes; |
| 928 |
int32_t chunk_cbytes; |
| 929 |
int32_t chunk_nbytes; |
| 930 |
void* fp = NULL; |
| 931 |
int rc; |
| 932 |
|
| 933 |
uint8_t* h2 = new_header_frame(schunk, frame); |
| 934 |
if (h2 == NULL) { |
| 935 |
return BLOSC2_ERROR_DATA; |
| 936 |
} |
| 937 |
uint32_t h2len; |
| 938 |
from_big(&h2len, h2 + FRAME_HEADER_LEN, sizeof(h2len)); |
| 939 |
// Build the offsets chunk |
| 940 |
int32_t chunksize = -1; |
| 941 |
int32_t off_cbytes = 0; |
| 942 |
uint64_t coffset = 0; |
| 943 |
int32_t off_nbytes = (int32_t) (nchunks * sizeof(int64_t)); |
| 944 |
uint64_t* data_tmp = malloc(off_nbytes); |
| 945 |
bool needs_free = false; |
| 946 |
for (int i = 0; i < nchunks; i++) { |
| 947 |
uint8_t* data_chunk; |
| 948 |
data_chunk = schunk->data[i]; |
| 949 |
rc = blosc2_cbuffer_sizes(data_chunk, &chunk_nbytes, &chunk_cbytes, NULL); |
| 950 |
if (rc < 0) { |
| 951 |
return rc; |
| 952 |
} |
| 953 |
data_tmp[i] = coffset; |
| 954 |
coffset += chunk_cbytes; |
| 955 |
int32_t chunksize_ = chunk_nbytes; |
| 956 |
if (i == 0) { |
| 957 |
chunksize = chunksize_; |
| 958 |
} |
| 959 |
else if (chunksize != chunksize_) { |
| 960 |
// Variable size // TODO: update flags for this (or do not use them at all) |
| 961 |
chunksize = 0; |
| 962 |
} |
| 963 |
if (needs_free) { |
| 964 |
free(data_chunk); |
| 965 |
} |
| 966 |
} |
| 967 |
if ((int64_t)coffset != cbytes) { |
| 968 |
free(data_tmp); |
| 969 |
return BLOSC2_ERROR_DATA; |
| 970 |
} |
| 971 |
uint8_t *off_chunk = NULL; |
| 972 |
if (nchunks > 0) { |
| 973 |
// Compress the chunk of offsets |
| 974 |
off_chunk = malloc(off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 975 |
blosc2_context *cctx = blosc2_create_cctx(BLOSC2_CPARAMS_DEFAULTS); |
| 976 |
if (cctx == NULL) { |
| 977 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 978 |
return BLOSC2_ERROR_NULL_POINTER; |
| 979 |
} |
| 980 |
cctx->typesize = sizeof(int64_t); |
| 981 |
off_cbytes = blosc2_compress_ctx(cctx, data_tmp, off_nbytes, off_chunk, |
| 982 |
off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 983 |
blosc2_free_ctx(cctx); |
| 984 |
if (off_cbytes < 0) { |
| 985 |
free(off_chunk); |
| 986 |
free(h2); |
| 987 |
return off_cbytes; |
| 988 |
} |
| 989 |
} |
| 990 |
else { |
| 991 |
off_cbytes = 0; |
| 992 |
} |
| 993 |
free(data_tmp); |
| 994 |
|
| 995 |
// Now that we know them, fill the chunksize and frame length in header |
| 996 |
to_big(h2 + FRAME_CHUNKSIZE, &chunksize, sizeof(chunksize)); |
| 997 |
frame->len = h2len + cbytes + off_cbytes + FRAME_TRAILER_MINLEN; |
| 998 |
if (frame->sframe) { |
| 999 |
frame->len = h2len + off_cbytes + FRAME_TRAILER_MINLEN; |
| 1000 |
} |
| 1001 |
int64_t tbytes = frame->len; |
| 1002 |
to_big(h2 + FRAME_LEN, &tbytes, sizeof(tbytes)); |
| 1003 |
|
| 1004 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 1005 |
if (io_cb == NULL) { |
| 1006 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 1007 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 1008 |
} |
| 1009 |
|
| 1010 |
// Create the frame and put the header at the beginning |
| 1011 |
int64_t io_pos = 0; |
| 1012 |
if (frame->urlpath == NULL) { |
| 1013 |
frame->cframe = malloc((size_t)frame->len); |
| 1014 |
memcpy(frame->cframe, h2, h2len); |
| 1015 |
} |
| 1016 |
else { |
| 1017 |
if (frame->sframe) { |
| 1018 |
fp = sframe_open_index(frame->urlpath, "wb", |
| 1019 |
frame->schunk->storage->io); |
| 1020 |
} |
| 1021 |
else { |
| 1022 |
fp = io_cb->open(frame->urlpath, "wb", frame->schunk->storage->io->params); |
| 1023 |
} |
| 1024 |
if (fp == NULL) { |
| 1025 |
BLOSC_TRACE_ERROR("Error creating file in: %s", frame->urlpath); |
| 1026 |
return BLOSC2_ERROR_FILE_OPEN; |
| 1027 |
} |
| 1028 |
io_cb->write(h2, h2len, 1, io_pos, fp); |
| 1029 |
io_pos += h2len; |
| 1030 |
} |
| 1031 |
free(h2); |
| 1032 |
|
| 1033 |
// Fill the frame with the actual data chunks |
| 1034 |
if (!frame->sframe) { |
| 1035 |
coffset = 0; |
| 1036 |
for (int i = 0; i < nchunks; i++) { |
| 1037 |
uint8_t* data_chunk = schunk->data[i]; |
| 1038 |
rc = blosc2_cbuffer_sizes(data_chunk, NULL, &chunk_cbytes, NULL); |
| 1039 |
if (rc < 0) { |
| 1040 |
return rc; |
| 1041 |
} |
| 1042 |
if (frame->urlpath == NULL) { |
| 1043 |
memcpy(frame->cframe + h2len + coffset, data_chunk, (size_t)chunk_cbytes); |
| 1044 |
} else { |
| 1045 |
io_cb->write(data_chunk, chunk_cbytes, 1, io_pos, fp); |
| 1046 |
io_pos += chunk_cbytes; |
| 1047 |
} |
| 1048 |
coffset += chunk_cbytes; |
| 1049 |
} |
| 1050 |
if ((int64_t)coffset != cbytes) { |
| 1051 |
return BLOSC2_ERROR_FAILURE; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
// Copy the offsets chunk at the end of the frame |
| 1056 |
if (frame->urlpath == NULL) { |
| 1057 |
memcpy(frame->cframe + h2len + cbytes, off_chunk, off_cbytes); |
| 1058 |
} |
| 1059 |
else { |
| 1060 |
io_cb->write(off_chunk, off_cbytes, 1, io_pos, fp); |
| 1061 |
io_cb->close(fp); |
| 1062 |
} |
| 1063 |
free(off_chunk); |
| 1064 |
rc = frame_update_trailer(frame, schunk); |
| 1065 |
if (rc < 0) { |
| 1066 |
return rc; |
| 1067 |
} |
| 1068 |
|
| 1069 |
return frame->len; |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
// Get the compressed data offsets |
| 1074 |
uint8_t* get_coffsets(blosc2_frame_s *frame, int32_t header_len, int64_t cbytes, |
| 1075 |
int64_t nchunks, int32_t *off_cbytes) { |
| 1076 |
int32_t chunk_cbytes; |
| 1077 |
int rc; |
| 1078 |
|
| 1079 |
if (frame->coffsets != NULL) { |
| 1080 |
if (off_cbytes != NULL) { |
| 1081 |
rc = blosc2_cbuffer_sizes(frame->coffsets, NULL, &chunk_cbytes, NULL); |
| 1082 |
if (rc < 0) { |
| 1083 |
return NULL; |
| 1084 |
} |
| 1085 |
*off_cbytes = (int32_t)chunk_cbytes; |
| 1086 |
} |
| 1087 |
return frame->coffsets; |
| 1088 |
} |
| 1089 |
if (frame->cframe != NULL) { |
| 1090 |
int64_t off_pos = header_len; |
| 1091 |
if (cbytes < INT64_MAX - header_len) { |
| 1092 |
off_pos += cbytes; |
| 1093 |
} |
| 1094 |
// Check that there is enough room to read Blosc header |
| 1095 |
if (off_pos < 0 || off_pos > INT64_MAX - BLOSC_EXTENDED_HEADER_LENGTH || |
| 1096 |
off_pos + BLOSC_EXTENDED_HEADER_LENGTH > frame->len) { |
| 1097 |
BLOSC_TRACE_ERROR("Cannot read the offsets outside of frame boundary."); |
| 1098 |
return NULL; |
| 1099 |
} |
| 1100 |
// For in-memory frames, the coffset is just one pointer away |
| 1101 |
uint8_t* off_start = frame->cframe + off_pos; |
| 1102 |
if (off_cbytes != NULL) { |
| 1103 |
int32_t chunk_nbytes; |
| 1104 |
int32_t chunk_blocksize; |
| 1105 |
rc = blosc2_cbuffer_sizes(off_start, &chunk_nbytes, &chunk_cbytes, &chunk_blocksize); |
| 1106 |
if (rc < 0) { |
| 1107 |
return NULL; |
| 1108 |
} |
| 1109 |
*off_cbytes = (int32_t)chunk_cbytes; |
| 1110 |
if (*off_cbytes < 0 || off_pos + *off_cbytes > frame->len) { |
| 1111 |
BLOSC_TRACE_ERROR("Cannot read the cbytes outside of frame boundary."); |
| 1112 |
return NULL; |
| 1113 |
} |
| 1114 |
if ((uint64_t)chunk_nbytes != nchunks * sizeof(int64_t)) { |
| 1115 |
BLOSC_TRACE_ERROR("The number of chunks in offset idx " |
| 1116 |
"does not match the ones in the header frame."); |
| 1117 |
return NULL; |
| 1118 |
} |
| 1119 |
|
| 1120 |
} |
| 1121 |
return off_start; |
| 1122 |
} |
| 1123 |
|
| 1124 |
int64_t trailer_offset = get_trailer_offset(frame, header_len, true); |
| 1125 |
|
| 1126 |
if (trailer_offset < BLOSC_EXTENDED_HEADER_LENGTH || trailer_offset + FRAME_TRAILER_MINLEN > frame->len) { |
| 1127 |
BLOSC_TRACE_ERROR("Cannot read the trailer out of the frame."); |
| 1128 |
return NULL; |
| 1129 |
} |
| 1130 |
|
| 1131 |
int32_t coffsets_cbytes; |
| 1132 |
if (frame->sframe) { |
| 1133 |
coffsets_cbytes = (int32_t)(trailer_offset - (header_len + 0)); |
| 1134 |
} |
| 1135 |
else { |
| 1136 |
coffsets_cbytes = (int32_t)(trailer_offset - (header_len + cbytes)); |
| 1137 |
} |
| 1138 |
|
| 1139 |
if (off_cbytes != NULL) { |
| 1140 |
*off_cbytes = coffsets_cbytes; |
| 1141 |
} |
| 1142 |
|
| 1143 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 1144 |
if (io_cb == NULL) { |
| 1145 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 1146 |
return NULL; |
| 1147 |
} |
| 1148 |
|
| 1149 |
void* fp = NULL; |
| 1150 |
uint8_t* coffsets; |
| 1151 |
if (io_cb->is_allocation_necessary) { |
| 1152 |
coffsets = malloc((size_t)coffsets_cbytes); |
| 1153 |
frame->coffsets_needs_free = true; |
| 1154 |
} |
| 1155 |
else { |
| 1156 |
frame->coffsets_needs_free = false; |
| 1157 |
} |
| 1158 |
|
| 1159 |
int64_t io_pos = 0; |
| 1160 |
if (frame->sframe) { |
| 1161 |
fp = sframe_open_index(frame->urlpath, "rb", |
| 1162 |
frame->schunk->storage->io); |
| 1163 |
if (fp == NULL) { |
| 1164 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1165 |
return NULL; |
| 1166 |
} |
| 1167 |
io_pos = header_len + 0; |
| 1168 |
} |
| 1169 |
else { |
| 1170 |
fp = io_cb->open(frame->urlpath, "rb", frame->schunk->storage->io->params); |
| 1171 |
if (fp == NULL) { |
| 1172 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1173 |
return NULL; |
| 1174 |
} |
| 1175 |
io_pos = frame->file_offset + header_len + cbytes; |
| 1176 |
} |
| 1177 |
int64_t rbytes = io_cb->read((void**)&coffsets, 1, coffsets_cbytes, io_pos, fp); |
| 1178 |
io_cb->close(fp); |
| 1179 |
if (rbytes != coffsets_cbytes) { |
| 1180 |
BLOSC_TRACE_ERROR("Cannot read the offsets out of the frame."); |
| 1181 |
if (frame->coffsets_needs_free) |
| 1182 |
free(coffsets); |
| 1183 |
return NULL; |
| 1184 |
} |
| 1185 |
frame->coffsets = coffsets; |
| 1186 |
return coffsets; |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
// Get the data offsets from a frame |
| 1191 |
int64_t* blosc2_frame_get_offsets(blosc2_schunk *schunk) { |
| 1192 |
if (schunk->frame == NULL) { |
| 1193 |
BLOSC_TRACE_ERROR("This function needs a frame."); |
| 1194 |
return NULL; |
| 1195 |
} |
| 1196 |
blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame; |
| 1197 |
|
| 1198 |
// Get header info |
| 1199 |
int32_t header_len; |
| 1200 |
int64_t frame_len; |
| 1201 |
int64_t nbytes; |
| 1202 |
int64_t cbytes; |
| 1203 |
int32_t blocksize; |
| 1204 |
int32_t chunksize; |
| 1205 |
int64_t nchunks; |
| 1206 |
int ret = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 1207 |
&blocksize, &chunksize, &nchunks, |
| 1208 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 1209 |
frame->schunk->storage->io); |
| 1210 |
if (ret < 0) { |
| 1211 |
BLOSC_TRACE_ERROR("Cannot get the header info for the frame."); |
| 1212 |
return NULL; |
| 1213 |
} |
| 1214 |
|
| 1215 |
int32_t off_nbytes = (int32_t) (nchunks * sizeof(int64_t)); |
| 1216 |
int64_t* offsets = (int64_t *) malloc((size_t)off_nbytes); |
| 1217 |
|
| 1218 |
int32_t coffsets_cbytes = 0; |
| 1219 |
uint8_t *coffsets = get_coffsets(frame, header_len, cbytes, nchunks, &coffsets_cbytes); |
| 1220 |
// Decompress offsets |
| 1221 |
blosc2_dparams off_dparams = BLOSC2_DPARAMS_DEFAULTS; |
| 1222 |
blosc2_context *dctx = blosc2_create_dctx(off_dparams); |
| 1223 |
if (dctx == NULL) { |
| 1224 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 1225 |
return NULL; |
| 1226 |
} |
| 1227 |
int32_t prev_nbytes = blosc2_decompress_ctx(dctx, coffsets, coffsets_cbytes, |
| 1228 |
offsets, off_nbytes); |
| 1229 |
blosc2_free_ctx(dctx); |
| 1230 |
if (prev_nbytes < 0) { |
| 1231 |
free(offsets); |
| 1232 |
BLOSC_TRACE_ERROR("Cannot decompress the offsets chunk."); |
| 1233 |
return NULL; |
| 1234 |
} |
| 1235 |
return offsets; |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
int frame_update_header(blosc2_frame_s* frame, blosc2_schunk* schunk, bool new) { |
| 1240 |
uint8_t* framep = frame->cframe; |
| 1241 |
uint8_t* header_ptr; |
| 1242 |
uint8_t header[FRAME_HEADER_MINLEN]; |
| 1243 |
|
| 1244 |
if (frame->len <= 0) { |
| 1245 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 1246 |
} |
| 1247 |
|
| 1248 |
if (new && schunk->cbytes > 0) { |
| 1249 |
BLOSC_TRACE_ERROR("New metalayers cannot be added after actual data " |
| 1250 |
"has been appended."); |
| 1251 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 1252 |
} |
| 1253 |
|
| 1254 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 1255 |
if (io_cb == NULL) { |
| 1256 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 1257 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 1258 |
} |
| 1259 |
|
| 1260 |
if (frame->cframe == NULL) { |
| 1261 |
int64_t rbytes = 0; |
| 1262 |
void* fp = NULL; |
| 1263 |
int64_t io_pos = 0; |
| 1264 |
if (frame->sframe) { |
| 1265 |
fp = sframe_open_index(frame->urlpath, "rb+", |
| 1266 |
frame->schunk->storage->io); |
| 1267 |
if (fp == NULL) { |
| 1268 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1269 |
return BLOSC2_ERROR_FILE_OPEN; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
else { |
| 1273 |
fp = io_cb->open(frame->urlpath, "rb", frame->schunk->storage->io->params); |
| 1274 |
if (fp == NULL) { |
| 1275 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1276 |
return BLOSC2_ERROR_FILE_OPEN; |
| 1277 |
} |
| 1278 |
io_pos = frame->file_offset; |
| 1279 |
} |
| 1280 |
if (fp != NULL) { |
| 1281 |
if (io_cb->is_allocation_necessary) |
| 1282 |
header_ptr = header; |
| 1283 |
rbytes = io_cb->read((void**)&header_ptr, 1, FRAME_HEADER_MINLEN, io_pos, fp); |
| 1284 |
io_cb->close(fp); |
| 1285 |
} |
| 1286 |
(void) rbytes; |
| 1287 |
if (rbytes != FRAME_HEADER_MINLEN) { |
| 1288 |
return BLOSC2_ERROR_FILE_WRITE; |
| 1289 |
} |
| 1290 |
framep = header_ptr; |
| 1291 |
} |
| 1292 |
uint32_t prev_h2len; |
| 1293 |
from_big(&prev_h2len, framep + FRAME_HEADER_LEN, sizeof(prev_h2len)); |
| 1294 |
|
| 1295 |
// Build a new header |
| 1296 |
uint8_t* h2 = new_header_frame(schunk, frame); |
| 1297 |
uint32_t h2len; |
| 1298 |
from_big(&h2len, h2 + FRAME_HEADER_LEN, sizeof(h2len)); |
| 1299 |
|
| 1300 |
// The frame length is outdated when adding a new metalayer, so update it |
| 1301 |
if (new) { |
| 1302 |
int64_t frame_len = h2len; // at adding time, we only have to worry of the header for now |
| 1303 |
to_big(h2 + FRAME_LEN, &frame_len, sizeof(frame_len)); |
| 1304 |
frame->len = frame_len; |
| 1305 |
} |
| 1306 |
|
| 1307 |
if (!new && prev_h2len != h2len) { |
| 1308 |
BLOSC_TRACE_ERROR("The new metalayer sizes should be equal the existing ones."); |
| 1309 |
return BLOSC2_ERROR_DATA; |
| 1310 |
} |
| 1311 |
|
| 1312 |
void* fp = NULL; |
| 1313 |
if (frame->cframe == NULL) { |
| 1314 |
// Write updated header down to file |
| 1315 |
if (frame->sframe) { |
| 1316 |
fp = sframe_open_index(frame->urlpath, "rb+", |
| 1317 |
frame->schunk->storage->io); |
| 1318 |
} |
| 1319 |
else { |
| 1320 |
fp = io_cb->open(frame->urlpath, "rb+", frame->schunk->storage->io->params); |
| 1321 |
} |
| 1322 |
if (fp == NULL) { |
| 1323 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1324 |
return BLOSC2_ERROR_FILE_OPEN; |
| 1325 |
} |
| 1326 |
int64_t io_pos = frame->file_offset; |
| 1327 |
io_cb->write(h2, h2len, 1, io_pos, fp); |
| 1328 |
io_cb->close(fp); |
| 1329 |
} |
| 1330 |
else { |
| 1331 |
if (new) { |
| 1332 |
frame->cframe = realloc(frame->cframe, h2len); |
| 1333 |
} |
| 1334 |
memcpy(frame->cframe, h2, h2len); |
| 1335 |
} |
| 1336 |
free(h2); |
| 1337 |
|
| 1338 |
return 1; |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
static int get_meta_from_header(blosc2_frame_s* frame, blosc2_schunk* schunk, uint8_t* header, |
| 1343 |
int32_t header_len) { |
| 1344 |
BLOSC_UNUSED_PARAM(frame); |
| 1345 |
int64_t header_pos = FRAME_IDX_SIZE; |
| 1346 |
|
| 1347 |
// Get the size for the index of metalayers |
| 1348 |
uint16_t idx_size; |
| 1349 |
header_pos += sizeof(idx_size); |
| 1350 |
if (header_len < header_pos) { |
| 1351 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1352 |
} |
| 1353 |
from_big(&idx_size, header + FRAME_IDX_SIZE, sizeof(idx_size)); |
| 1354 |
|
| 1355 |
// Get the actual index of metalayers |
| 1356 |
uint8_t* metalayers_idx = header + FRAME_IDX_SIZE + 2; |
| 1357 |
header_pos += 1; |
| 1358 |
if (header_len < header_pos) { |
| 1359 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1360 |
} |
| 1361 |
if (metalayers_idx[0] != 0xde) { // sanity check |
| 1362 |
return BLOSC2_ERROR_DATA; |
| 1363 |
} |
| 1364 |
uint8_t* idxp = metalayers_idx + 1; |
| 1365 |
uint16_t nmetalayers; |
| 1366 |
header_pos += sizeof(nmetalayers); |
| 1367 |
if (header_len < header_pos) { |
| 1368 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1369 |
} |
| 1370 |
from_big(&nmetalayers, idxp, sizeof(uint16_t)); |
| 1371 |
idxp += 2; |
| 1372 |
if (nmetalayers > BLOSC2_MAX_METALAYERS) { |
| 1373 |
return BLOSC2_ERROR_DATA; |
| 1374 |
} |
| 1375 |
schunk->nmetalayers = nmetalayers; |
| 1376 |
|
| 1377 |
// Populate the metalayers and its serialized values |
| 1378 |
for (int nmetalayer = 0; nmetalayer < nmetalayers; nmetalayer++) { |
| 1379 |
header_pos += 1; |
| 1380 |
if (header_len < header_pos) { |
| 1381 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1382 |
} |
| 1383 |
if ((*idxp & 0xe0u) != 0xa0u) { // sanity check |
| 1384 |
return BLOSC2_ERROR_DATA; |
| 1385 |
} |
| 1386 |
blosc2_metalayer* metalayer = calloc(1, sizeof(blosc2_metalayer)); |
| 1387 |
schunk->metalayers[nmetalayer] = metalayer; |
| 1388 |
|
| 1389 |
// Populate the metalayer string |
| 1390 |
uint8_t nslen = *idxp & (uint8_t)0x1F; |
| 1391 |
idxp += 1; |
| 1392 |
header_pos += nslen; |
| 1393 |
if (header_len < header_pos) { |
| 1394 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1395 |
} |
| 1396 |
char* ns = malloc((size_t)nslen + 1); |
| 1397 |
memcpy(ns, idxp, nslen); |
| 1398 |
ns[nslen] = '\0'; |
| 1399 |
idxp += nslen; |
| 1400 |
metalayer->name = ns; |
| 1401 |
|
| 1402 |
// Populate the serialized value for this metalayer |
| 1403 |
// Get the offset |
| 1404 |
header_pos += 1; |
| 1405 |
if (header_len < header_pos) { |
| 1406 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1407 |
} |
| 1408 |
if ((*idxp & 0xffu) != 0xd2u) { // sanity check |
| 1409 |
return BLOSC2_ERROR_DATA; |
| 1410 |
} |
| 1411 |
idxp += 1; |
| 1412 |
int32_t offset; |
| 1413 |
header_pos += sizeof(offset); |
| 1414 |
if (header_len < header_pos) { |
| 1415 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1416 |
} |
| 1417 |
from_big(&offset, idxp, sizeof(offset)); |
| 1418 |
idxp += 4; |
| 1419 |
if (offset < 0 || offset >= header_len) { |
| 1420 |
// Offset is less than zero or exceeds header length |
| 1421 |
return BLOSC2_ERROR_DATA; |
| 1422 |
} |
| 1423 |
// Go to offset and see if we have the correct marker |
| 1424 |
uint8_t* content_marker = header + offset; |
| 1425 |
if (header_len < offset + 1 + 4) { |
| 1426 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1427 |
} |
| 1428 |
if (*content_marker != 0xc6) { |
| 1429 |
return BLOSC2_ERROR_DATA; |
| 1430 |
} |
| 1431 |
|
| 1432 |
// Read the size of the content |
| 1433 |
int32_t content_len; |
| 1434 |
from_big(&content_len, content_marker + 1, sizeof(content_len)); |
| 1435 |
if (content_len < 0) { |
| 1436 |
return BLOSC2_ERROR_DATA; |
| 1437 |
} |
| 1438 |
metalayer->content_len = content_len; |
| 1439 |
|
| 1440 |
// Finally, read the content |
| 1441 |
if (header_len < offset + 1 + 4 + content_len) { |
| 1442 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1443 |
} |
| 1444 |
char* content = malloc((size_t)content_len); |
| 1445 |
memcpy(content, content_marker + 1 + 4, (size_t)content_len); |
| 1446 |
metalayer->content = (uint8_t*)content; |
| 1447 |
} |
| 1448 |
|
| 1449 |
return 1; |
| 1450 |
} |
| 1451 |
|
| 1452 |
int frame_get_metalayers(blosc2_frame_s* frame, blosc2_schunk* schunk) { |
| 1453 |
int32_t header_len; |
| 1454 |
int64_t frame_len; |
| 1455 |
int64_t nbytes; |
| 1456 |
int64_t cbytes; |
| 1457 |
int32_t blocksize; |
| 1458 |
int32_t chunksize; |
| 1459 |
int64_t nchunks; |
| 1460 |
int ret = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 1461 |
&blocksize, &chunksize, &nchunks, |
| 1462 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 1463 |
schunk->storage->io); |
| 1464 |
if (ret < 0) { |
| 1465 |
BLOSC_TRACE_ERROR("Unable to get the header info from frame."); |
| 1466 |
return ret; |
| 1467 |
} |
| 1468 |
|
| 1469 |
// Get the header |
| 1470 |
uint8_t* header = NULL; |
| 1471 |
bool needs_free = false; |
| 1472 |
if (frame->cframe != NULL) { |
| 1473 |
header = frame->cframe; |
| 1474 |
} else { |
| 1475 |
int64_t rbytes = 0; |
| 1476 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 1477 |
if (io_cb == NULL) { |
| 1478 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 1479 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 1480 |
} |
| 1481 |
|
| 1482 |
if (io_cb->is_allocation_necessary) { |
| 1483 |
header = malloc(header_len); |
| 1484 |
needs_free = true; |
| 1485 |
} |
| 1486 |
else { |
| 1487 |
needs_free = false; |
| 1488 |
} |
| 1489 |
|
| 1490 |
void* fp = NULL; |
| 1491 |
int64_t io_pos = 0; |
| 1492 |
if (frame->sframe) { |
| 1493 |
fp = sframe_open_index(frame->urlpath, "rb", |
| 1494 |
frame->schunk->storage->io); |
| 1495 |
if (fp == NULL) { |
| 1496 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1497 |
return BLOSC2_ERROR_FILE_OPEN; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
else { |
| 1501 |
fp = io_cb->open(frame->urlpath, "rb", frame->schunk->storage->io->params); |
| 1502 |
if (fp == NULL) { |
| 1503 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1504 |
return BLOSC2_ERROR_FILE_OPEN; |
| 1505 |
} |
| 1506 |
io_pos = frame->file_offset; |
| 1507 |
} |
| 1508 |
if (fp != NULL) { |
| 1509 |
rbytes = io_cb->read((void**)&header, 1, header_len, io_pos, fp); |
| 1510 |
io_cb->close(fp); |
| 1511 |
} |
| 1512 |
if (rbytes != header_len) { |
| 1513 |
BLOSC_TRACE_ERROR("Cannot access the header out of the frame."); |
| 1514 |
if (needs_free) |
| 1515 |
free(header); |
| 1516 |
return BLOSC2_ERROR_FILE_READ; |
| 1517 |
} |
| 1518 |
} |
| 1519 |
|
| 1520 |
ret = get_meta_from_header(frame, schunk, header, header_len); |
| 1521 |
|
| 1522 |
if (frame->cframe == NULL && needs_free) { |
| 1523 |
free(header); |
| 1524 |
} |
| 1525 |
|
| 1526 |
return ret; |
| 1527 |
} |
| 1528 |
|
| 1529 |
static int get_vlmeta_from_trailer(blosc2_frame_s* frame, blosc2_schunk* schunk, uint8_t* trailer, |
| 1530 |
int32_t trailer_len) { |
| 1531 |
|
| 1532 |
BLOSC_UNUSED_PARAM(frame); |
| 1533 |
int64_t trailer_pos = FRAME_TRAILER_VLMETALAYERS + 2; |
| 1534 |
uint8_t* idxp = trailer + trailer_pos; |
| 1535 |
|
| 1536 |
// Get the size for the index of metalayers |
| 1537 |
trailer_pos += 2; |
| 1538 |
if (trailer_len < trailer_pos) { |
| 1539 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1540 |
} |
| 1541 |
uint16_t idx_size; |
| 1542 |
from_big(&idx_size, idxp, sizeof(idx_size)); |
| 1543 |
idxp += 2; |
| 1544 |
|
| 1545 |
trailer_pos += 1; |
| 1546 |
// Get the actual index of metalayers |
| 1547 |
if (trailer_len < trailer_pos) { |
| 1548 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1549 |
} |
| 1550 |
if (idxp[0] != 0xde) { // sanity check |
| 1551 |
return BLOSC2_ERROR_DATA; |
| 1552 |
} |
| 1553 |
idxp += 1; |
| 1554 |
|
| 1555 |
int16_t nmetalayers; |
| 1556 |
trailer_pos += sizeof(nmetalayers); |
| 1557 |
if (trailer_len < trailer_pos) { |
| 1558 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1559 |
} |
| 1560 |
from_big(&nmetalayers, idxp, sizeof(uint16_t)); |
| 1561 |
idxp += 2; |
| 1562 |
if (nmetalayers > BLOSC2_MAX_VLMETALAYERS) { |
| 1563 |
return BLOSC2_ERROR_DATA; |
| 1564 |
} |
| 1565 |
schunk->nvlmetalayers = nmetalayers; |
| 1566 |
|
| 1567 |
// Populate the metalayers and its serialized values |
| 1568 |
for (int nmetalayer = 0; nmetalayer < nmetalayers; nmetalayer++) { |
| 1569 |
trailer_pos += 1; |
| 1570 |
if (trailer_len < trailer_pos) { |
| 1571 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1572 |
} |
| 1573 |
if ((*idxp & 0xe0u) != 0xa0u) { // sanity check |
| 1574 |
return BLOSC2_ERROR_DATA; |
| 1575 |
} |
| 1576 |
blosc2_metalayer* metalayer = calloc(1, sizeof(blosc2_metalayer)); |
| 1577 |
schunk->vlmetalayers[nmetalayer] = metalayer; |
| 1578 |
|
| 1579 |
// Populate the metalayer string |
| 1580 |
uint8_t nslen = *idxp & (uint8_t)0x1F; |
| 1581 |
idxp += 1; |
| 1582 |
trailer_pos += nslen; |
| 1583 |
if (trailer_len < trailer_pos) { |
| 1584 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1585 |
} |
| 1586 |
char* ns = malloc((size_t)nslen + 1); |
| 1587 |
memcpy(ns, idxp, nslen); |
| 1588 |
ns[nslen] = '\0'; |
| 1589 |
idxp += nslen; |
| 1590 |
metalayer->name = ns; |
| 1591 |
|
| 1592 |
// Populate the serialized value for this metalayer |
| 1593 |
// Get the offset |
| 1594 |
trailer_pos += 1; |
| 1595 |
if (trailer_len < trailer_pos) { |
| 1596 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1597 |
} |
| 1598 |
if ((*idxp & 0xffu) != 0xd2u) { // sanity check |
| 1599 |
return BLOSC2_ERROR_DATA; |
| 1600 |
} |
| 1601 |
idxp += 1; |
| 1602 |
int32_t offset; |
| 1603 |
trailer_pos += sizeof(offset); |
| 1604 |
if (trailer_len < trailer_pos) { |
| 1605 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1606 |
} |
| 1607 |
from_big(&offset, idxp, sizeof(offset)); |
| 1608 |
idxp += 4; |
| 1609 |
if (offset < 0 || offset >= trailer_len) { |
| 1610 |
// Offset is less than zero or exceeds trailer length |
| 1611 |
return BLOSC2_ERROR_DATA; |
| 1612 |
} |
| 1613 |
// Go to offset and see if we have the correct marker |
| 1614 |
uint8_t* content_marker = trailer + offset; |
| 1615 |
if (trailer_len < offset + 1 + 4) { |
| 1616 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1617 |
} |
| 1618 |
if (*content_marker != 0xc6) { |
| 1619 |
return BLOSC2_ERROR_DATA; |
| 1620 |
} |
| 1621 |
|
| 1622 |
// Read the size of the content |
| 1623 |
int32_t content_len; |
| 1624 |
from_big(&content_len, content_marker + 1, sizeof(content_len)); |
| 1625 |
if (content_len < 0) { |
| 1626 |
return BLOSC2_ERROR_DATA; |
| 1627 |
} |
| 1628 |
metalayer->content_len = content_len; |
| 1629 |
|
| 1630 |
// Finally, read the content |
| 1631 |
if (trailer_len < offset + 1 + 4 + content_len) { |
| 1632 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1633 |
} |
| 1634 |
char* content = malloc((size_t)content_len); |
| 1635 |
memcpy(content, content_marker + 1 + 4, (size_t)content_len); |
| 1636 |
metalayer->content = (uint8_t*)content; |
| 1637 |
} |
| 1638 |
return 1; |
| 1639 |
} |
| 1640 |
|
| 1641 |
int frame_get_vlmetalayers(blosc2_frame_s* frame, blosc2_schunk* schunk) { |
| 1642 |
int32_t header_len; |
| 1643 |
int64_t frame_len; |
| 1644 |
int64_t nbytes; |
| 1645 |
int64_t cbytes; |
| 1646 |
int32_t blocksize; |
| 1647 |
int32_t chunksize; |
| 1648 |
int64_t nchunks; |
| 1649 |
int ret = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 1650 |
&blocksize, &chunksize, &nchunks, |
| 1651 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 1652 |
schunk->storage->io); |
| 1653 |
if (ret < 0) { |
| 1654 |
BLOSC_TRACE_ERROR("Unable to get the trailer info from frame."); |
| 1655 |
return ret; |
| 1656 |
} |
| 1657 |
|
| 1658 |
int64_t trailer_offset = get_trailer_offset(frame, header_len, nbytes > 0); |
| 1659 |
int32_t trailer_len = (int32_t) frame->trailer_len; |
| 1660 |
|
| 1661 |
if (trailer_offset < BLOSC_EXTENDED_HEADER_LENGTH || trailer_offset + trailer_len > frame->len) { |
| 1662 |
BLOSC_TRACE_ERROR("Cannot access the trailer out of the frame."); |
| 1663 |
return BLOSC2_ERROR_READ_BUFFER; |
| 1664 |
} |
| 1665 |
|
| 1666 |
// Get the trailer |
| 1667 |
uint8_t* trailer = NULL; |
| 1668 |
bool needs_free = false; |
| 1669 |
if (frame->cframe != NULL) { |
| 1670 |
trailer = frame->cframe + trailer_offset; |
| 1671 |
} else { |
| 1672 |
int64_t rbytes = 0; |
| 1673 |
|
| 1674 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 1675 |
if (io_cb == NULL) { |
| 1676 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 1677 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 1678 |
} |
| 1679 |
|
| 1680 |
if (io_cb->is_allocation_necessary) { |
| 1681 |
trailer = malloc(trailer_len); |
| 1682 |
needs_free = true; |
| 1683 |
} |
| 1684 |
else { |
| 1685 |
needs_free = false; |
| 1686 |
} |
| 1687 |
|
| 1688 |
void* fp = NULL; |
| 1689 |
int64_t io_pos = 0; |
| 1690 |
if (frame->sframe) { |
| 1691 |
char* eframe_name = malloc(strlen(frame->urlpath) + strlen("/chunks.b2frame") + 1); |
| 1692 |
sprintf(eframe_name, "%s/chunks.b2frame", frame->urlpath); |
| 1693 |
fp = io_cb->open(eframe_name, "rb", frame->schunk->storage->io->params); |
| 1694 |
if (fp == NULL) { |
| 1695 |
BLOSC_TRACE_ERROR("Error opening file in: %s", eframe_name); |
| 1696 |
free(eframe_name); |
| 1697 |
return BLOSC2_ERROR_FILE_OPEN; |
| 1698 |
} |
| 1699 |
free(eframe_name); |
| 1700 |
io_pos = trailer_offset; |
| 1701 |
} |
| 1702 |
else { |
| 1703 |
fp = io_cb->open(frame->urlpath, "rb", frame->schunk->storage->io->params); |
| 1704 |
if (fp == NULL) { |
| 1705 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1706 |
return BLOSC2_ERROR_FILE_OPEN; |
| 1707 |
} |
| 1708 |
io_pos = frame->file_offset + trailer_offset; |
| 1709 |
} |
| 1710 |
if (fp != NULL) { |
| 1711 |
rbytes = io_cb->read((void**)&trailer, 1, trailer_len, io_pos, fp); |
| 1712 |
io_cb->close(fp); |
| 1713 |
} |
| 1714 |
if (rbytes != trailer_len) { |
| 1715 |
BLOSC_TRACE_ERROR("Cannot access the trailer out of the fileframe."); |
| 1716 |
if (needs_free) |
| 1717 |
free(trailer); |
| 1718 |
return BLOSC2_ERROR_FILE_READ; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
|
| 1722 |
ret = get_vlmeta_from_trailer(frame, schunk, trailer, trailer_len); |
| 1723 |
|
| 1724 |
if (frame->cframe == NULL && needs_free) { |
| 1725 |
free(trailer); |
| 1726 |
} |
| 1727 |
|
| 1728 |
return ret; |
| 1729 |
} |
| 1730 |
|
| 1731 |
|
| 1732 |
blosc2_storage* get_new_storage(const blosc2_storage* storage, |
| 1733 |
const blosc2_cparams* cdefaults, |
| 1734 |
const blosc2_dparams* ddefaults, |
| 1735 |
const blosc2_io* iodefaults) { |
| 1736 |
|
| 1737 |
blosc2_storage* new_storage = (blosc2_storage*)calloc(1, sizeof(blosc2_storage)); |
| 1738 |
memcpy(new_storage, storage, sizeof(blosc2_storage)); |
| 1739 |
if (storage->urlpath != NULL) { |
| 1740 |
char* urlpath = normalize_urlpath(storage->urlpath); |
| 1741 |
new_storage->urlpath = malloc(strlen(urlpath) + 1); |
| 1742 |
strcpy(new_storage->urlpath, urlpath); |
| 1743 |
} |
| 1744 |
|
| 1745 |
// cparams |
| 1746 |
blosc2_cparams* cparams = malloc(sizeof(blosc2_cparams)); |
| 1747 |
if (storage->cparams != NULL) { |
| 1748 |
memcpy(cparams, storage->cparams, sizeof(blosc2_cparams)); |
| 1749 |
} else { |
| 1750 |
memcpy(cparams, cdefaults, sizeof(blosc2_cparams)); |
| 1751 |
} |
| 1752 |
new_storage->cparams = cparams; |
| 1753 |
|
| 1754 |
// dparams |
| 1755 |
blosc2_dparams* dparams = malloc(sizeof(blosc2_dparams)); |
| 1756 |
if (storage->dparams != NULL) { |
| 1757 |
memcpy(dparams, storage->dparams, sizeof(blosc2_dparams)); |
| 1758 |
} |
| 1759 |
else { |
| 1760 |
memcpy(dparams, ddefaults, sizeof(blosc2_dparams)); |
| 1761 |
} |
| 1762 |
new_storage->dparams = dparams; |
| 1763 |
|
| 1764 |
// iodefaults |
| 1765 |
blosc2_io* udio = malloc(sizeof(blosc2_io)); |
| 1766 |
if (storage->io != NULL) { |
| 1767 |
memcpy(udio, storage->io, sizeof(blosc2_io)); |
| 1768 |
} |
| 1769 |
else { |
| 1770 |
memcpy(udio, iodefaults, sizeof(blosc2_io)); |
| 1771 |
} |
| 1772 |
new_storage->io = udio; |
| 1773 |
|
| 1774 |
return new_storage; |
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
/* Get a super-chunk out of a frame */ |
| 1779 |
blosc2_schunk* frame_to_schunk(blosc2_frame_s* frame, bool copy, const blosc2_io *udio) { |
| 1780 |
int32_t header_len; |
| 1781 |
int64_t frame_len; |
| 1782 |
int rc; |
| 1783 |
blosc2_schunk* schunk = calloc(1, sizeof(blosc2_schunk)); |
| 1784 |
schunk->frame = (blosc2_frame*)frame; |
| 1785 |
frame->schunk = schunk; |
| 1786 |
|
| 1787 |
rc = get_header_info(frame, &header_len, &frame_len, &schunk->nbytes, |
| 1788 |
&schunk->cbytes, &schunk->blocksize, |
| 1789 |
&schunk->chunksize, &schunk->nchunks, &schunk->typesize, |
| 1790 |
&schunk->compcode, &schunk->compcode_meta, &schunk->clevel, schunk->filters, |
| 1791 |
schunk->filters_meta, &schunk->splitmode, udio); |
| 1792 |
if (rc < 0) { |
| 1793 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 1794 |
blosc2_schunk_free(schunk); |
| 1795 |
return NULL; |
| 1796 |
} |
| 1797 |
int64_t nchunks = schunk->nchunks; |
| 1798 |
int64_t nbytes = schunk->nbytes; |
| 1799 |
(void) nbytes; |
| 1800 |
int64_t cbytes = schunk->cbytes; |
| 1801 |
|
| 1802 |
// Compression and decompression contexts |
| 1803 |
blosc2_cparams *cparams; |
| 1804 |
blosc2_schunk_get_cparams(schunk, &cparams); |
| 1805 |
schunk->cctx = blosc2_create_cctx(*cparams); |
| 1806 |
if (schunk->cctx == NULL) { |
| 1807 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 1808 |
return NULL; |
| 1809 |
} |
| 1810 |
blosc2_dparams *dparams; |
| 1811 |
blosc2_schunk_get_dparams(schunk, &dparams); |
| 1812 |
schunk->dctx = blosc2_create_dctx(*dparams); |
| 1813 |
if (schunk->dctx == NULL) { |
| 1814 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 1815 |
return NULL; |
| 1816 |
} |
| 1817 |
blosc2_storage storage = {.contiguous = copy ? false : true}; |
| 1818 |
schunk->storage = get_new_storage(&storage, cparams, dparams, udio); |
| 1819 |
free(cparams); |
| 1820 |
free(dparams); |
| 1821 |
if (!copy) { |
| 1822 |
goto out; |
| 1823 |
} |
| 1824 |
|
| 1825 |
// We are not attached to a frame anymore |
| 1826 |
schunk->frame = NULL; |
| 1827 |
|
| 1828 |
if (nchunks == 0) { |
| 1829 |
frame->schunk = NULL; |
| 1830 |
goto out; |
| 1831 |
} |
| 1832 |
|
| 1833 |
// Get the compressed offsets |
| 1834 |
int32_t coffsets_cbytes = 0; |
| 1835 |
uint8_t* coffsets = get_coffsets(frame, header_len, cbytes, nchunks, &coffsets_cbytes); |
| 1836 |
if (coffsets == NULL) { |
| 1837 |
blosc2_schunk_free(schunk); |
| 1838 |
BLOSC_TRACE_ERROR("Cannot get the offsets for the frame."); |
| 1839 |
return NULL; |
| 1840 |
} |
| 1841 |
|
| 1842 |
// Decompress offsets |
| 1843 |
blosc2_dparams off_dparams = BLOSC2_DPARAMS_DEFAULTS; |
| 1844 |
blosc2_context *dctx = blosc2_create_dctx(off_dparams); |
| 1845 |
if (dctx == NULL) { |
| 1846 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 1847 |
return NULL; |
| 1848 |
} |
| 1849 |
int64_t* offsets = (int64_t *) malloc((size_t)nchunks * sizeof(int64_t)); |
| 1850 |
int32_t off_nbytes = blosc2_decompress_ctx(dctx, coffsets, coffsets_cbytes, |
| 1851 |
offsets, (int32_t)(nchunks * sizeof(int64_t))); |
| 1852 |
blosc2_free_ctx(dctx); |
| 1853 |
if (off_nbytes < 0) { |
| 1854 |
free(offsets); |
| 1855 |
blosc2_schunk_free(schunk); |
| 1856 |
BLOSC_TRACE_ERROR("Cannot decompress the offsets chunk."); |
| 1857 |
return NULL; |
| 1858 |
} |
| 1859 |
|
| 1860 |
// We want the contiguous schunk, so create the actual data chunks (and, while doing this, |
| 1861 |
// get a guess at the blocksize used in this frame) |
| 1862 |
int64_t acc_nbytes = 0; |
| 1863 |
int64_t acc_cbytes = 0; |
| 1864 |
int32_t blocksize = 0; |
| 1865 |
int32_t chunk_nbytes; |
| 1866 |
int32_t chunk_cbytes; |
| 1867 |
int32_t chunk_blocksize; |
| 1868 |
size_t prev_alloc = BLOSC_EXTENDED_HEADER_LENGTH; |
| 1869 |
uint8_t* data_chunk = NULL; |
| 1870 |
bool needs_free = false; |
| 1871 |
const blosc2_io_cb *io_cb = blosc2_get_io_cb(udio->id); |
| 1872 |
if (io_cb == NULL) { |
| 1873 |
blosc2_schunk_free(schunk); |
| 1874 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 1875 |
return NULL; |
| 1876 |
} |
| 1877 |
|
| 1878 |
void* fp = NULL; |
| 1879 |
if (frame->cframe == NULL) { |
| 1880 |
if (io_cb->is_allocation_necessary) { |
| 1881 |
data_chunk = malloc((size_t)prev_alloc); |
| 1882 |
needs_free = true; |
| 1883 |
} |
| 1884 |
else { |
| 1885 |
needs_free = false; |
| 1886 |
} |
| 1887 |
|
| 1888 |
if (!frame->sframe) { |
| 1889 |
// If not the chunks won't be in the frame |
| 1890 |
fp = io_cb->open(frame->urlpath, "rb", udio->params); |
| 1891 |
if (fp == NULL) { |
| 1892 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 1893 |
rc = BLOSC2_ERROR_FILE_OPEN; |
| 1894 |
goto end; |
| 1895 |
} |
| 1896 |
} |
| 1897 |
} |
| 1898 |
schunk->data = malloc(nchunks * sizeof(void*)); |
| 1899 |
for (int i = 0; i < nchunks; i++) { |
| 1900 |
if (frame->cframe != NULL) { |
| 1901 |
if (needs_free) { |
| 1902 |
free(data_chunk); |
| 1903 |
} |
| 1904 |
if (offsets[i] < 0) { |
| 1905 |
int64_t rbytes = frame_get_chunk(frame, i, &data_chunk, &needs_free); |
| 1906 |
if (rbytes < 0) { |
| 1907 |
break; |
| 1908 |
} |
| 1909 |
} |
| 1910 |
else { |
| 1911 |
data_chunk = frame->cframe + header_len + offsets[i]; |
| 1912 |
} |
| 1913 |
rc = blosc2_cbuffer_sizes(data_chunk, NULL, &chunk_cbytes, NULL); |
| 1914 |
if (rc < 0) { |
| 1915 |
break; |
| 1916 |
} |
| 1917 |
} |
| 1918 |
else { |
| 1919 |
int64_t rbytes; |
| 1920 |
if (frame->sframe) { |
| 1921 |
if (needs_free) { |
| 1922 |
free(data_chunk); |
| 1923 |
} |
| 1924 |
rbytes = frame_get_chunk(frame, i, &data_chunk, &needs_free); |
| 1925 |
if (rbytes < 0) { |
| 1926 |
break; |
| 1927 |
} |
| 1928 |
} |
| 1929 |
else { |
| 1930 |
int64_t io_pos = frame->file_offset + header_len + offsets[i]; |
| 1931 |
rbytes = io_cb->read((void**)&data_chunk, 1, BLOSC_EXTENDED_HEADER_LENGTH, io_pos, fp); |
| 1932 |
} |
| 1933 |
if (rbytes != BLOSC_EXTENDED_HEADER_LENGTH) { |
| 1934 |
rc = BLOSC2_ERROR_READ_BUFFER; |
| 1935 |
break; |
| 1936 |
} |
| 1937 |
rc = blosc2_cbuffer_sizes(data_chunk, NULL, &chunk_cbytes, NULL); |
| 1938 |
if (rc < 0) { |
| 1939 |
break; |
| 1940 |
} |
| 1941 |
if (chunk_cbytes > (int32_t)prev_alloc) { |
| 1942 |
if (io_cb->is_allocation_necessary) |
| 1943 |
data_chunk = realloc(data_chunk, chunk_cbytes); |
| 1944 |
if (data_chunk == NULL) { |
| 1945 |
BLOSC_TRACE_ERROR("Cannot realloc space for the data_chunk."); |
| 1946 |
rc = BLOSC2_ERROR_MEMORY_ALLOC; |
| 1947 |
break; |
| 1948 |
} |
| 1949 |
prev_alloc = chunk_cbytes; |
| 1950 |
} |
| 1951 |
if (!frame->sframe) { |
| 1952 |
int64_t io_pos = frame->file_offset + header_len + offsets[i]; |
| 1953 |
rbytes = io_cb->read((void**)&data_chunk, 1, chunk_cbytes, io_pos, fp); |
| 1954 |
if (rbytes != chunk_cbytes) { |
| 1955 |
rc = BLOSC2_ERROR_READ_BUFFER; |
| 1956 |
break; |
| 1957 |
} |
| 1958 |
} |
| 1959 |
} |
| 1960 |
uint8_t* new_chunk = malloc(chunk_cbytes); |
| 1961 |
memcpy(new_chunk, data_chunk, chunk_cbytes); |
| 1962 |
schunk->data[i] = new_chunk; |
| 1963 |
rc = blosc2_cbuffer_sizes(data_chunk, &chunk_nbytes, NULL, &chunk_blocksize); |
| 1964 |
if (rc < 0) { |
| 1965 |
break; |
| 1966 |
} |
| 1967 |
acc_nbytes += chunk_nbytes; |
| 1968 |
acc_cbytes += chunk_cbytes; |
| 1969 |
if (i == 0) { |
| 1970 |
blocksize = chunk_blocksize; |
| 1971 |
} |
| 1972 |
else if (blocksize != chunk_blocksize) { |
| 1973 |
// Blocksize varies |
| 1974 |
blocksize = 0; |
| 1975 |
} |
| 1976 |
} |
| 1977 |
|
| 1978 |
// We are not attached to a schunk anymore |
| 1979 |
frame->schunk = NULL; |
| 1980 |
|
| 1981 |
end: |
| 1982 |
if (needs_free) { |
| 1983 |
free(data_chunk); |
| 1984 |
} |
| 1985 |
if (frame->cframe == NULL) { |
| 1986 |
if (!frame->sframe) { |
| 1987 |
io_cb->close(fp); |
| 1988 |
} |
| 1989 |
} |
| 1990 |
free(offsets); |
| 1991 |
|
| 1992 |
// cframes and sframes have different ways to store chunks with special values: |
| 1993 |
// 1) cframes represent special chunks as negative offsets |
| 1994 |
// 2) sframes does not have the concept of offsets, but rather of data pointers (.data) |
| 1995 |
// so they always have a pointer to a special chunk |
| 1996 |
// This is why cframes and sframes have different cbytes and hence, we cannot enforce acc_bytes == schunk->cbytes |
| 1997 |
// In the future, maybe we could provide special meanings for .data[i] > 0x7FFFFFFF, but not there yet |
| 1998 |
// if (rc < 0 || acc_nbytes != nbytes || acc_cbytes != cbytes) { |
| 1999 |
if (rc < 0 || acc_nbytes != nbytes) { |
| 2000 |
blosc2_schunk_free(schunk); |
| 2001 |
return NULL; |
| 2002 |
} |
| 2003 |
// Update counters |
| 2004 |
schunk->cbytes = acc_cbytes; |
| 2005 |
schunk->blocksize = blocksize; |
| 2006 |
|
| 2007 |
out: |
| 2008 |
rc = frame_get_metalayers(frame, schunk); |
| 2009 |
if (rc < 0) { |
| 2010 |
blosc2_schunk_free(schunk); |
| 2011 |
BLOSC_TRACE_ERROR("Cannot access the metalayers."); |
| 2012 |
return NULL; |
| 2013 |
} |
| 2014 |
|
| 2015 |
rc = frame_get_vlmetalayers(frame, schunk); |
| 2016 |
if (rc < 0) { |
| 2017 |
blosc2_schunk_free(schunk); |
| 2018 |
BLOSC_TRACE_ERROR("Cannot access the vlmetalayers."); |
| 2019 |
return NULL; |
| 2020 |
} |
| 2021 |
|
| 2022 |
return schunk; |
| 2023 |
} |
| 2024 |
|
| 2025 |
|
| 2026 |
void frame_avoid_cframe_free(blosc2_frame_s* frame, bool avoid_cframe_free) { |
| 2027 |
frame->avoid_cframe_free = avoid_cframe_free; |
| 2028 |
} |
| 2029 |
|
| 2030 |
|
| 2031 |
struct csize_idx { |
| 2032 |
int32_t val; |
| 2033 |
int32_t idx; |
| 2034 |
}; |
| 2035 |
|
| 2036 |
// Helper function for qsorting block offsets |
| 2037 |
int sort_offset(const void* a, const void* b) { |
| 2038 |
int32_t a_ = ((struct csize_idx*)a)->val; |
| 2039 |
int32_t b_ = ((struct csize_idx*)b)->val; |
| 2040 |
return a_ - b_; |
| 2041 |
} |
| 2042 |
|
| 2043 |
|
| 2044 |
int get_coffset(blosc2_frame_s* frame, int32_t header_len, int64_t cbytes, |
| 2045 |
int64_t nchunk, int64_t nchunks, int64_t *offset) { |
| 2046 |
int32_t off_cbytes; |
| 2047 |
// Get the offset to nchunk |
| 2048 |
uint8_t *coffsets = get_coffsets(frame, header_len, cbytes, nchunks, &off_cbytes); |
| 2049 |
if (coffsets == NULL) { |
| 2050 |
BLOSC_TRACE_ERROR("Cannot get the offset for chunk %" PRId64 " for the frame.", nchunk); |
| 2051 |
return BLOSC2_ERROR_DATA; |
| 2052 |
} |
| 2053 |
|
| 2054 |
// Get the 64-bit offset |
| 2055 |
int rc = blosc2_getitem(coffsets, off_cbytes, (int32_t)nchunk, 1, offset, (int32_t)sizeof(int64_t)); |
| 2056 |
if (rc < 0) { |
| 2057 |
BLOSC_TRACE_ERROR("Problems retrieving a chunk offset."); |
| 2058 |
} else if (!frame->sframe && *offset > frame->len) { |
| 2059 |
BLOSC_TRACE_ERROR("Cannot read chunk %" PRId64 " outside of frame boundary.", nchunk); |
| 2060 |
rc = BLOSC2_ERROR_READ_BUFFER; |
| 2061 |
} |
| 2062 |
|
| 2063 |
return rc; |
| 2064 |
} |
| 2065 |
|
| 2066 |
|
| 2067 |
// Detect and return a chunk with special values in offsets (only zeros, NaNs and non initialized) |
| 2068 |
int frame_special_chunk(int64_t special_value, int32_t nbytes, int32_t typesize, int32_t blocksize, |
| 2069 |
uint8_t** chunk, int32_t cbytes, bool *needs_free) { |
| 2070 |
int rc = 0; |
| 2071 |
*chunk = malloc(cbytes); |
| 2072 |
*needs_free = true; |
| 2073 |
|
| 2074 |
// Detect the kind of special value |
| 2075 |
uint64_t zeros_mask = (uint64_t) BLOSC2_SPECIAL_ZERO << (8 * 7); // chunk of zeros |
| 2076 |
uint64_t nans_mask = (uint64_t) BLOSC2_SPECIAL_NAN << (8 * 7); // chunk of NaNs |
| 2077 |
uint64_t uninit_mask = (uint64_t) BLOSC2_SPECIAL_UNINIT << (8 * 7); // chunk of uninit values |
| 2078 |
|
| 2079 |
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS; |
| 2080 |
cparams.typesize = typesize; |
| 2081 |
cparams.blocksize = blocksize; |
| 2082 |
if (special_value & zeros_mask) { |
| 2083 |
rc = blosc2_chunk_zeros(cparams, nbytes, *chunk, cbytes); |
| 2084 |
if (rc < 0) { |
| 2085 |
BLOSC_TRACE_ERROR("Error creating a zero chunk"); |
| 2086 |
} |
| 2087 |
} |
| 2088 |
else if (special_value & uninit_mask) { |
| 2089 |
rc = blosc2_chunk_uninit(cparams, nbytes, *chunk, cbytes); |
| 2090 |
if (rc < 0) { |
| 2091 |
BLOSC_TRACE_ERROR("Error creating a non initialized chunk"); |
| 2092 |
} |
| 2093 |
} |
| 2094 |
else if (special_value & nans_mask) { |
| 2095 |
rc = blosc2_chunk_nans(cparams, nbytes, *chunk, cbytes); |
| 2096 |
if (rc < 0) { |
| 2097 |
BLOSC_TRACE_ERROR("Error creating a nan chunk"); |
| 2098 |
} |
| 2099 |
} |
| 2100 |
else { |
| 2101 |
BLOSC_TRACE_ERROR("Special value not recognized: %" PRId64 "", special_value); |
| 2102 |
rc = BLOSC2_ERROR_DATA; |
| 2103 |
} |
| 2104 |
|
| 2105 |
if (rc < 0) { |
| 2106 |
free(*chunk); |
| 2107 |
*needs_free = false; |
| 2108 |
*chunk = NULL; |
| 2109 |
} |
| 2110 |
|
| 2111 |
return rc; |
| 2112 |
} |
| 2113 |
|
| 2114 |
|
| 2115 |
/* Return a compressed chunk that is part of a frame in the `chunk` parameter. |
| 2116 |
* If the frame is disk-based, a buffer is allocated for the (compressed) chunk, |
| 2117 |
* and hence a free is needed. You can check if the chunk requires a free with the `needs_free` |
| 2118 |
* parameter. |
| 2119 |
* If the chunk does not need a free, it means that a pointer to the location in frame is returned |
| 2120 |
* in the `chunk` parameter. |
| 2121 |
* |
| 2122 |
* The size of the (compressed) chunk is returned. If some problem is detected, a negative code |
| 2123 |
* is returned instead. |
| 2124 |
*/ |
| 2125 |
int frame_get_chunk(blosc2_frame_s *frame, int64_t nchunk, uint8_t **chunk, bool *needs_free) { |
| 2126 |
int32_t header_len; |
| 2127 |
int64_t frame_len; |
| 2128 |
int64_t nbytes; |
| 2129 |
int64_t cbytes; |
| 2130 |
int32_t blocksize; |
| 2131 |
int32_t chunksize; |
| 2132 |
int64_t nchunks; |
| 2133 |
int32_t typesize; |
| 2134 |
int64_t offset; |
| 2135 |
int32_t chunk_cbytes; |
| 2136 |
int rc; |
| 2137 |
|
| 2138 |
*chunk = NULL; |
| 2139 |
*needs_free = false; |
| 2140 |
rc = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 2141 |
&blocksize, &chunksize, &nchunks, |
| 2142 |
&typesize, NULL, NULL, NULL, NULL, NULL, NULL, |
| 2143 |
frame->schunk->storage->io); |
| 2144 |
if (rc < 0) { |
| 2145 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 2146 |
return rc; |
| 2147 |
} |
| 2148 |
|
| 2149 |
if (nchunk >= nchunks) { |
| 2150 |
BLOSC_TRACE_ERROR("nchunk ('%" PRId64 "') exceeds the number of chunks " |
| 2151 |
"('%" PRId64 "') in frame.", nchunk, nchunks); |
| 2152 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2153 |
} |
| 2154 |
|
| 2155 |
// Get the offset to nchunk |
| 2156 |
rc = get_coffset(frame, header_len, cbytes, nchunk, nchunks, &offset); |
| 2157 |
if (rc < 0) { |
| 2158 |
BLOSC_TRACE_ERROR("Unable to get offset to chunk %" PRId64 ".", nchunk); |
| 2159 |
return rc; |
| 2160 |
} |
| 2161 |
|
| 2162 |
if (offset < 0) { |
| 2163 |
// Special value |
| 2164 |
chunk_cbytes = BLOSC_EXTENDED_HEADER_LENGTH; |
| 2165 |
int32_t chunksize_ = chunksize; |
| 2166 |
if ((nchunk == nchunks - 1) && (nbytes % chunksize)) { |
| 2167 |
// Last chunk is incomplete. Compute its actual size. |
| 2168 |
chunksize_ = (int32_t) (nbytes % chunksize); |
| 2169 |
} |
| 2170 |
rc = frame_special_chunk(offset, chunksize_, typesize, blocksize, chunk, chunk_cbytes, needs_free); |
| 2171 |
if (rc < 0) { |
| 2172 |
return rc; |
| 2173 |
} |
| 2174 |
goto end; |
| 2175 |
} |
| 2176 |
|
| 2177 |
if (frame->sframe) { |
| 2178 |
// Sparse on-disk |
| 2179 |
nchunk = offset; |
| 2180 |
return sframe_get_chunk(frame, nchunk, chunk, needs_free); |
| 2181 |
} |
| 2182 |
|
| 2183 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 2184 |
if (io_cb == NULL) { |
| 2185 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 2186 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 2187 |
} |
| 2188 |
|
| 2189 |
if (frame->cframe == NULL) { |
| 2190 |
uint8_t* header_ptr; |
| 2191 |
uint8_t header[BLOSC_EXTENDED_HEADER_LENGTH]; |
| 2192 |
void* fp = io_cb->open(frame->urlpath, "rb", frame->schunk->storage->io->params); |
| 2193 |
if (fp == NULL) { |
| 2194 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 2195 |
return BLOSC2_ERROR_FILE_OPEN; |
| 2196 |
} |
| 2197 |
if (io_cb->is_allocation_necessary) |
| 2198 |
header_ptr = header; |
| 2199 |
int64_t io_pos = frame->file_offset + header_len + offset; |
| 2200 |
int64_t rbytes = io_cb->read((void**)&header_ptr, 1, sizeof(header), io_pos, fp); |
| 2201 |
if (rbytes != BLOSC_EXTENDED_HEADER_LENGTH) { |
| 2202 |
BLOSC_TRACE_ERROR("Cannot read the cbytes for chunk in the frame."); |
| 2203 |
io_cb->close(fp); |
| 2204 |
return BLOSC2_ERROR_FILE_READ; |
| 2205 |
} |
| 2206 |
rc = blosc2_cbuffer_sizes(header_ptr, NULL, &chunk_cbytes, NULL); |
| 2207 |
if (rc < 0) { |
| 2208 |
BLOSC_TRACE_ERROR("Cannot read the cbytes for chunk in the frame."); |
| 2209 |
io_cb->close(fp); |
| 2210 |
return rc; |
| 2211 |
} |
| 2212 |
if (io_cb->is_allocation_necessary) { |
| 2213 |
*chunk = malloc(chunk_cbytes); |
| 2214 |
*needs_free = true; |
| 2215 |
} |
| 2216 |
else { |
| 2217 |
*needs_free = false; |
| 2218 |
} |
| 2219 |
|
| 2220 |
io_pos = frame->file_offset + header_len + offset; |
| 2221 |
rbytes = io_cb->read((void**)chunk, 1, chunk_cbytes, io_pos, fp); |
| 2222 |
io_cb->close(fp); |
| 2223 |
if (rbytes != chunk_cbytes) { |
| 2224 |
BLOSC_TRACE_ERROR("Cannot read the chunk out of the frame."); |
| 2225 |
return BLOSC2_ERROR_FILE_READ; |
| 2226 |
} |
| 2227 |
|
| 2228 |
} else { |
| 2229 |
// The chunk is in memory and just one pointer away |
| 2230 |
*chunk = frame->cframe + header_len + offset; |
| 2231 |
rc = blosc2_cbuffer_sizes(*chunk, NULL, &chunk_cbytes, NULL); |
| 2232 |
if (rc < 0) { |
| 2233 |
return rc; |
| 2234 |
} |
| 2235 |
} |
| 2236 |
|
| 2237 |
end: |
| 2238 |
return (int32_t)chunk_cbytes; |
| 2239 |
} |
| 2240 |
|
| 2241 |
|
| 2242 |
/* Return a compressed chunk that is part of a frame in the `chunk` parameter. |
| 2243 |
* If the frame is disk-based, a buffer is allocated for the (lazy) chunk, |
| 2244 |
* and hence a free is needed. You can check if the chunk requires a free with the `needs_free` |
| 2245 |
* parameter. |
| 2246 |
* If the chunk does not need a free, it means that the frame is in memory and that just a |
| 2247 |
* pointer to the location of the chunk in memory is returned. |
| 2248 |
* |
| 2249 |
* The size of the (compressed, potentially lazy) chunk is returned. If some problem is detected, |
| 2250 |
* a negative code is returned instead. |
| 2251 |
*/ |
| 2252 |
int frame_get_lazychunk(blosc2_frame_s *frame, int64_t nchunk, uint8_t **chunk, bool *needs_free) { |
| 2253 |
int32_t header_len; |
| 2254 |
int64_t frame_len; |
| 2255 |
int64_t nbytes; |
| 2256 |
int64_t cbytes; |
| 2257 |
int32_t blocksize; |
| 2258 |
int32_t chunksize; |
| 2259 |
int64_t nchunks; |
| 2260 |
int32_t typesize; |
| 2261 |
int32_t lazychunk_cbytes; |
| 2262 |
int64_t offset; |
| 2263 |
void* fp = NULL; |
| 2264 |
|
| 2265 |
*chunk = NULL; |
| 2266 |
*needs_free = false; |
| 2267 |
int rc = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 2268 |
&blocksize, &chunksize, &nchunks, |
| 2269 |
&typesize, NULL, NULL, NULL, NULL, NULL, NULL, |
| 2270 |
frame->schunk->storage->io); |
| 2271 |
if (rc < 0) { |
| 2272 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 2273 |
return rc; |
| 2274 |
} |
| 2275 |
|
| 2276 |
if (nchunk >= nchunks) { |
| 2277 |
BLOSC_TRACE_ERROR("nchunk ('%" PRId64 "') exceeds the number of chunks " |
| 2278 |
"('%" PRId64 "') in frame.", nchunk, nchunks); |
| 2279 |
return BLOSC2_ERROR_INVALID_PARAM; |
| 2280 |
} |
| 2281 |
|
| 2282 |
// Get the offset to nchunk |
| 2283 |
rc = get_coffset(frame, header_len, cbytes, nchunk, nchunks, &offset); |
| 2284 |
if (rc < 0) { |
| 2285 |
BLOSC_TRACE_ERROR("Unable to get offset to chunk %" PRId64 ".", nchunk); |
| 2286 |
return rc; |
| 2287 |
} |
| 2288 |
|
| 2289 |
if (offset < 0) { |
| 2290 |
// Special value |
| 2291 |
lazychunk_cbytes = BLOSC_EXTENDED_HEADER_LENGTH; |
| 2292 |
int32_t chunksize_ = chunksize; |
| 2293 |
if ((nchunk == nchunks - 1) && (nbytes % chunksize)) { |
| 2294 |
// Last chunk is incomplete. Compute its actual size. |
| 2295 |
chunksize_ = (int32_t) (nbytes % chunksize); |
| 2296 |
} |
| 2297 |
rc = frame_special_chunk(offset, chunksize_, typesize, blocksize, chunk, |
| 2298 |
(int32_t)lazychunk_cbytes, needs_free); |
| 2299 |
goto end; |
| 2300 |
} |
| 2301 |
|
| 2302 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 2303 |
if (io_cb == NULL) { |
| 2304 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 2305 |
rc = BLOSC2_ERROR_PLUGIN_IO; |
| 2306 |
goto end; |
| 2307 |
} |
| 2308 |
|
| 2309 |
if (frame->cframe == NULL) { |
| 2310 |
// TODO: make this portable across different endianness |
| 2311 |
// Get info for building a lazy chunk |
| 2312 |
int32_t chunk_nbytes; |
| 2313 |
int32_t chunk_cbytes; |
| 2314 |
int32_t chunk_blocksize; |
| 2315 |
uint8_t* header_ptr; |
| 2316 |
uint8_t header[BLOSC_EXTENDED_HEADER_LENGTH]; |
| 2317 |
int64_t io_pos = 0; |
| 2318 |
if (frame->sframe) { |
| 2319 |
// The chunk is not in the frame |
| 2320 |
fp = sframe_open_chunk(frame->urlpath, offset, "rb", |
| 2321 |
frame->schunk->storage->io); |
| 2322 |
if (fp == NULL) { |
| 2323 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 2324 |
return BLOSC2_ERROR_FILE_OPEN; |
| 2325 |
} |
| 2326 |
} |
| 2327 |
else { |
| 2328 |
fp = io_cb->open(frame->urlpath, "rb", frame->schunk->storage->io->params); |
| 2329 |
if (fp == NULL) { |
| 2330 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 2331 |
return BLOSC2_ERROR_FILE_OPEN; |
| 2332 |
} |
| 2333 |
io_pos = frame->file_offset + header_len + offset; |
| 2334 |
} |
| 2335 |
if (io_cb->is_allocation_necessary) |
| 2336 |
header_ptr = header; |
| 2337 |
int64_t rbytes = io_cb->read((void**)&header_ptr, 1, BLOSC_EXTENDED_HEADER_LENGTH, io_pos, fp); |
| 2338 |
if (rbytes != BLOSC_EXTENDED_HEADER_LENGTH) { |
| 2339 |
BLOSC_TRACE_ERROR("Cannot read the header for chunk in the frame."); |
| 2340 |
rc = BLOSC2_ERROR_FILE_READ; |
| 2341 |
goto end; |
| 2342 |
} |
| 2343 |
rc = blosc2_cbuffer_sizes(header_ptr, &chunk_nbytes, &chunk_cbytes, &chunk_blocksize); |
| 2344 |
if (rc < 0) { |
| 2345 |
goto end; |
| 2346 |
} |
| 2347 |
size_t nblocks = chunk_nbytes / chunk_blocksize; |
| 2348 |
size_t leftover_block = chunk_nbytes % chunk_blocksize; |
| 2349 |
nblocks = leftover_block ? nblocks + 1 : nblocks; |
| 2350 |
// Allocate space for the lazy chunk |
| 2351 |
int32_t trailer_len; |
| 2352 |
int32_t special_type = (header_ptr[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK; |
| 2353 |
int memcpyed = header_ptr[BLOSC2_CHUNK_FLAGS] & (uint8_t) BLOSC_MEMCPYED; |
| 2354 |
|
| 2355 |
int32_t trailer_offset = BLOSC_EXTENDED_HEADER_LENGTH; |
| 2356 |
size_t streams_offset = BLOSC_EXTENDED_HEADER_LENGTH; |
| 2357 |
if (special_type == 0) { |
| 2358 |
// Regular values have offsets for blocks |
| 2359 |
trailer_offset += (int32_t) (nblocks * sizeof(int32_t)); |
| 2360 |
if (memcpyed) { |
| 2361 |
streams_offset += 0; |
| 2362 |
} else { |
| 2363 |
streams_offset += nblocks * sizeof(int32_t); |
| 2364 |
} |
| 2365 |
trailer_len = (int32_t) (sizeof(int32_t) + sizeof(int64_t) + nblocks * sizeof(int32_t)); |
| 2366 |
lazychunk_cbytes = trailer_offset + trailer_len; |
| 2367 |
} |
| 2368 |
else if (special_type == BLOSC2_SPECIAL_VALUE) { |
| 2369 |
trailer_offset += typesize; |
| 2370 |
streams_offset += typesize; |
| 2371 |
trailer_len = 0; |
| 2372 |
lazychunk_cbytes = trailer_offset + trailer_len; |
| 2373 |
} |
| 2374 |
else { |
| 2375 |
rc = BLOSC2_ERROR_INVALID_HEADER; |
| 2376 |
goto end; |
| 2377 |
} |
| 2378 |
|
| 2379 |
// Read just the full header and bstarts section too (lazy partial length) |
| 2380 |
if (frame->sframe) { |
| 2381 |
io_pos = 0; |
| 2382 |
} |
| 2383 |
else { |
| 2384 |
io_pos = frame->file_offset + header_len + offset; |
| 2385 |
} |
| 2386 |
|
| 2387 |
// The case here is a bit special because more memory is allocated than read from the file |
| 2388 |
// and the chunk is modified after reading. Due to the modification, we cannot directly use |
| 2389 |
// the memory provided by the io |
| 2390 |
*chunk = malloc(lazychunk_cbytes); |
| 2391 |
*needs_free = true; |
| 2392 |
|
| 2393 |
if (io_cb->is_allocation_necessary) { |
| 2394 |
rbytes = io_cb->read((void**)chunk, 1, (int64_t)streams_offset, io_pos, fp); |
| 2395 |
} |
| 2396 |
else { |
| 2397 |
uint8_t* chunk_ptr; |
| 2398 |
rbytes = io_cb->read((void**)&chunk_ptr, 1, (int64_t)streams_offset, io_pos, fp); |
| 2399 |
memcpy(*chunk, chunk_ptr, streams_offset); |
| 2400 |
} |
| 2401 |
|
| 2402 |
if (rbytes != (int64_t)streams_offset) { |
| 2403 |
BLOSC_TRACE_ERROR("Cannot read the (lazy) chunk out of the frame."); |
| 2404 |
rc = BLOSC2_ERROR_FILE_READ; |
| 2405 |
goto end; |
| 2406 |
} |
| 2407 |
if (special_type == BLOSC2_SPECIAL_VALUE) { |
| 2408 |
// Value runlen is not returning a lazy chunk. We are done. |
| 2409 |
goto end; |
| 2410 |
} |
| 2411 |
|
| 2412 |
// Mark chunk as lazy |
| 2413 |
uint8_t* blosc2_flags = *chunk + BLOSC2_CHUNK_BLOSC2_FLAGS; |
| 2414 |
*blosc2_flags |= 0x08U; |
| 2415 |
|
| 2416 |
// Add the trailer (currently, nchunk + offset + block_csizes) |
| 2417 |
if (frame->sframe) { |
| 2418 |
*(int32_t*)(*chunk + trailer_offset) = (int32_t)offset; // offset is nchunk for sframes |
| 2419 |
*(int64_t*)(*chunk + trailer_offset + sizeof(int32_t)) = offset; |
| 2420 |
} |
| 2421 |
else { |
| 2422 |
*(int32_t*)(*chunk + trailer_offset) = (int32_t)nchunk; |
| 2423 |
*(int64_t*)(*chunk + trailer_offset + sizeof(int32_t)) = header_len + offset; |
| 2424 |
} |
| 2425 |
|
| 2426 |
int32_t* block_csizes = malloc(nblocks * sizeof(int32_t)); |
| 2427 |
|
| 2428 |
if (memcpyed) { |
| 2429 |
// When memcpyed the blocksizes are trivial to compute |
| 2430 |
for (int i = 0; i < (int)nblocks - 1; i++) { |
| 2431 |
block_csizes[i] = (int)chunk_blocksize; |
| 2432 |
} |
| 2433 |
// The last block could be incomplete, mainly due to the fact that the block size is not divisible |
| 2434 |
// by the typesize |
| 2435 |
block_csizes[nblocks - 1] = (int32_t)leftover_block ? (int32_t)leftover_block : chunk_blocksize; |
| 2436 |
} |
| 2437 |
else { |
| 2438 |
// In regular, compressed chunks, we need to sort the bstarts (they can be out |
| 2439 |
// of order because of multi-threading), and get a reverse index too. |
| 2440 |
memcpy(block_csizes, *chunk + BLOSC_EXTENDED_HEADER_LENGTH, nblocks * sizeof(int32_t)); |
| 2441 |
// Helper structure to keep track of original indexes |
| 2442 |
struct csize_idx *csize_idx = malloc(nblocks * sizeof(struct csize_idx)); |
| 2443 |
for (int n = 0; n < (int)nblocks; n++) { |
| 2444 |
csize_idx[n].val = block_csizes[n]; |
| 2445 |
csize_idx[n].idx = n; |
| 2446 |
} |
| 2447 |
qsort(csize_idx, nblocks, sizeof(struct csize_idx), &sort_offset); |
| 2448 |
// Compute the actual csizes |
| 2449 |
int idx; |
| 2450 |
for (int n = 0; n < (int)nblocks - 1; n++) { |
| 2451 |
idx = csize_idx[n].idx; |
| 2452 |
block_csizes[idx] = csize_idx[n + 1].val - csize_idx[n].val; |
| 2453 |
} |
| 2454 |
idx = csize_idx[nblocks - 1].idx; |
| 2455 |
block_csizes[idx] = (int)chunk_cbytes - csize_idx[nblocks - 1].val; |
| 2456 |
free(csize_idx); |
| 2457 |
} |
| 2458 |
// Copy the csizes at the end of the trailer |
| 2459 |
void *trailer_csizes = *chunk + lazychunk_cbytes - nblocks * sizeof(int32_t); |
| 2460 |
memcpy(trailer_csizes, block_csizes, nblocks * sizeof(int32_t)); |
| 2461 |
free(block_csizes); |
| 2462 |
} else { |
| 2463 |
// The chunk is in memory and just one pointer away |
| 2464 |
int64_t chunk_header_offset = header_len + offset; |
| 2465 |
int64_t chunk_cbytes_offset = chunk_header_offset + BLOSC_MIN_HEADER_LENGTH; |
| 2466 |
|
| 2467 |
*chunk = frame->cframe + chunk_header_offset; |
| 2468 |
|
| 2469 |
if (chunk_cbytes_offset > frame->len) { |
| 2470 |
BLOSC_TRACE_ERROR("Cannot read the header for chunk in the (contiguous) frame."); |
| 2471 |
rc = BLOSC2_ERROR_READ_BUFFER; |
| 2472 |
} else { |
| 2473 |
rc = blosc2_cbuffer_sizes(*chunk, NULL, &lazychunk_cbytes, NULL); |
| 2474 |
if (rc && chunk_cbytes_offset + lazychunk_cbytes > frame_len) { |
| 2475 |
BLOSC_TRACE_ERROR("Compressed bytes exceed beyond frame length."); |
| 2476 |
rc = BLOSC2_ERROR_READ_BUFFER; |
| 2477 |
} |
| 2478 |
} |
| 2479 |
} |
| 2480 |
|
| 2481 |
end: |
| 2482 |
if (fp != NULL) { |
| 2483 |
io_cb->close(fp); |
| 2484 |
} |
| 2485 |
if (rc < 0) { |
| 2486 |
if (*needs_free) { |
| 2487 |
free(*chunk); |
| 2488 |
*chunk = NULL; |
| 2489 |
*needs_free = false; |
| 2490 |
} |
| 2491 |
return rc; |
| 2492 |
} |
| 2493 |
|
| 2494 |
return (int)lazychunk_cbytes; |
| 2495 |
} |
| 2496 |
|
| 2497 |
|
| 2498 |
/* Fill an empty frame with special values (fast path). */ |
| 2499 |
int64_t frame_fill_special(blosc2_frame_s* frame, int64_t nitems, int special_value, |
| 2500 |
int32_t chunksize, blosc2_schunk* schunk) { |
| 2501 |
int32_t header_len; |
| 2502 |
int64_t frame_len; |
| 2503 |
int64_t nbytes; |
| 2504 |
int64_t cbytes; |
| 2505 |
int32_t blocksize; |
| 2506 |
int32_t typesize; |
| 2507 |
int64_t nchunks; |
| 2508 |
|
| 2509 |
int rc = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, &blocksize, NULL, |
| 2510 |
&nchunks, &typesize, NULL, NULL, NULL, NULL, NULL, NULL, |
| 2511 |
schunk->storage->io); |
| 2512 |
if (rc < 0) { |
| 2513 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 2514 |
return BLOSC2_ERROR_DATA; |
| 2515 |
} |
| 2516 |
|
| 2517 |
if (nitems == 0) { |
| 2518 |
return frame_len; |
| 2519 |
} |
| 2520 |
|
| 2521 |
if ((nitems / chunksize) > INT_MAX) { |
| 2522 |
BLOSC_TRACE_ERROR("nitems is too large. Try increasing the chunksize."); |
| 2523 |
return BLOSC2_ERROR_FRAME_SPECIAL; |
| 2524 |
} |
| 2525 |
|
| 2526 |
if ((nbytes > 0) || (cbytes > 0)) { |
| 2527 |
BLOSC_TRACE_ERROR("Filling with special values only works on empty frames"); |
| 2528 |
return BLOSC2_ERROR_FRAME_SPECIAL; |
| 2529 |
} |
| 2530 |
|
| 2531 |
// Compute the number of chunks and the length of the offsets chunk |
| 2532 |
int32_t chunkitems = chunksize / typesize; |
| 2533 |
nchunks = nitems / chunkitems; |
| 2534 |
int32_t leftover_items = (int32_t)(nitems % chunkitems); |
| 2535 |
if (leftover_items) { |
| 2536 |
nchunks += 1; |
| 2537 |
} |
| 2538 |
|
| 2539 |
blosc2_cparams* cparams; |
| 2540 |
blosc2_schunk_get_cparams(schunk, &cparams); |
| 2541 |
|
| 2542 |
// Build the offsets with a special chunk |
| 2543 |
int new_off_cbytes = BLOSC_EXTENDED_HEADER_LENGTH + sizeof(int64_t); |
| 2544 |
uint8_t* off_chunk = malloc(new_off_cbytes); |
| 2545 |
uint64_t offset_value = ((uint64_t)1 << 63); |
| 2546 |
uint8_t* sample_chunk = malloc(BLOSC_EXTENDED_HEADER_LENGTH); |
| 2547 |
int csize; |
| 2548 |
switch (special_value) { |
| 2549 |
case BLOSC2_SPECIAL_ZERO: |
| 2550 |
offset_value += (uint64_t) BLOSC2_SPECIAL_ZERO << (8 * 7); |
| 2551 |
csize = blosc2_chunk_zeros(*cparams, chunksize, sample_chunk, BLOSC_EXTENDED_HEADER_LENGTH); |
| 2552 |
break; |
| 2553 |
case BLOSC2_SPECIAL_UNINIT: |
| 2554 |
offset_value += (uint64_t) BLOSC2_SPECIAL_UNINIT << (8 * 7); |
| 2555 |
csize = blosc2_chunk_uninit(*cparams, chunksize, sample_chunk, BLOSC_EXTENDED_HEADER_LENGTH); |
| 2556 |
break; |
| 2557 |
case BLOSC2_SPECIAL_NAN: |
| 2558 |
offset_value += (uint64_t)BLOSC2_SPECIAL_NAN << (8 * 7); |
| 2559 |
csize = blosc2_chunk_nans(*cparams, chunksize, sample_chunk, BLOSC_EXTENDED_HEADER_LENGTH); |
| 2560 |
break; |
| 2561 |
default: |
| 2562 |
BLOSC_TRACE_ERROR("Only zeros, NaNs or non-initialized values are supported."); |
| 2563 |
return BLOSC2_ERROR_FRAME_SPECIAL; |
| 2564 |
} |
| 2565 |
if (csize < 0) { |
| 2566 |
BLOSC_TRACE_ERROR("Error creating sample chunk"); |
| 2567 |
return BLOSC2_ERROR_FRAME_SPECIAL; |
| 2568 |
} |
| 2569 |
cparams->typesize = sizeof(int64_t); // change it to offsets typesize |
| 2570 |
// cparams->blocksize = 0; // automatic blocksize |
| 2571 |
cparams->blocksize = 8 * 2 * 1024; // based on experiments with create_frame.c bench |
| 2572 |
cparams->clevel = 5; |
| 2573 |
cparams->compcode = BLOSC_BLOSCLZ; |
| 2574 |
int32_t special_nbytes = (int32_t) (nchunks * sizeof(int64_t)); |
| 2575 |
rc = blosc2_chunk_repeatval(*cparams, special_nbytes, off_chunk, new_off_cbytes, &offset_value); |
| 2576 |
free(cparams); |
| 2577 |
if (rc < 0) { |
| 2578 |
BLOSC_TRACE_ERROR("Error creating a special offsets chunk"); |
| 2579 |
return BLOSC2_ERROR_DATA; |
| 2580 |
} |
| 2581 |
|
| 2582 |
// Get the blocksize associated to the sample chunk |
| 2583 |
blosc2_cbuffer_sizes(sample_chunk, NULL, NULL, &blocksize); |
| 2584 |
free(sample_chunk); |
| 2585 |
// and use it for the super-chunk |
| 2586 |
schunk->blocksize = blocksize; |
| 2587 |
// schunk->blocksize = 0; // for experimenting with automatic blocksize |
| 2588 |
|
| 2589 |
// We have the new offsets; update the frame. |
| 2590 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 2591 |
if (io_cb == NULL) { |
| 2592 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 2593 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 2594 |
} |
| 2595 |
|
| 2596 |
int64_t new_frame_len = header_len + new_off_cbytes + frame->trailer_len; |
| 2597 |
void* fp = NULL; |
| 2598 |
if (frame->cframe != NULL) { |
| 2599 |
uint8_t* framep = frame->cframe; |
| 2600 |
/* Make space for the new chunk and copy it */ |
| 2601 |
frame->cframe = framep = realloc(framep, (size_t)new_frame_len); |
| 2602 |
if (framep == NULL) { |
| 2603 |
BLOSC_TRACE_ERROR("Cannot realloc space for the frame."); |
| 2604 |
return BLOSC2_ERROR_FRAME_SPECIAL; |
| 2605 |
} |
| 2606 |
/* Copy the offsets */ |
| 2607 |
memcpy(framep + header_len, off_chunk, (size_t)new_off_cbytes); |
| 2608 |
} |
| 2609 |
else { |
| 2610 |
size_t wbytes; |
| 2611 |
int64_t io_pos = 0; |
| 2612 |
if (frame->sframe) { |
| 2613 |
// Update the offsets chunk in the chunks frame |
| 2614 |
fp = sframe_open_index(frame->urlpath, "rb+", frame->schunk->storage->io); |
| 2615 |
if (fp == NULL) { |
| 2616 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 2617 |
return BLOSC2_ERROR_FILE_OPEN; |
| 2618 |
} |
| 2619 |
io_pos = frame->file_offset + header_len; |
| 2620 |
} |
| 2621 |
else { |
| 2622 |
// Regular frame |
| 2623 |
fp = io_cb->open(frame->urlpath, "rb+", schunk->storage->io->params); |
| 2624 |
if (fp == NULL) { |
| 2625 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 2626 |
return BLOSC2_ERROR_FILE_OPEN; |
| 2627 |
} |
| 2628 |
io_pos = frame->file_offset + header_len + cbytes; |
| 2629 |
} |
| 2630 |
wbytes = io_cb->write(off_chunk, 1, new_off_cbytes, io_pos, fp); // the new offsets |
| 2631 |
io_cb->close(fp); |
| 2632 |
if (wbytes != (size_t)new_off_cbytes) { |
| 2633 |
BLOSC_TRACE_ERROR("Cannot write the offsets to frame."); |
| 2634 |
return BLOSC2_ERROR_FRAME_SPECIAL; |
| 2635 |
} |
| 2636 |
} |
| 2637 |
|
| 2638 |
// Invalidate the cache for chunk offsets |
| 2639 |
if (frame->coffsets != NULL) { |
| 2640 |
if (frame->coffsets_needs_free) |
| 2641 |
free(frame->coffsets); |
| 2642 |
frame->coffsets = NULL; |
| 2643 |
} |
| 2644 |
free(off_chunk); |
| 2645 |
|
| 2646 |
frame->len = new_frame_len; |
| 2647 |
rc = frame_update_header(frame, schunk, false); |
| 2648 |
if (rc < 0) { |
| 2649 |
return BLOSC2_ERROR_FRAME_SPECIAL; |
| 2650 |
} |
| 2651 |
|
| 2652 |
rc = frame_update_trailer(frame, schunk); |
| 2653 |
if (rc < 0) { |
| 2654 |
return BLOSC2_ERROR_FRAME_SPECIAL; |
| 2655 |
} |
| 2656 |
|
| 2657 |
return frame->len; |
| 2658 |
} |
| 2659 |
|
| 2660 |
|
| 2661 |
/* Append an existing chunk into a frame. */ |
| 2662 |
void* frame_append_chunk(blosc2_frame_s* frame, void* chunk, blosc2_schunk* schunk) { |
| 2663 |
int8_t* chunk_ = chunk; |
| 2664 |
int32_t header_len; |
| 2665 |
int64_t frame_len; |
| 2666 |
int64_t nbytes; |
| 2667 |
int64_t cbytes; |
| 2668 |
int32_t blocksize; |
| 2669 |
int32_t chunksize; |
| 2670 |
int64_t nchunks; |
| 2671 |
int rc = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, &blocksize, &chunksize, |
| 2672 |
&nchunks, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 2673 |
frame->schunk->storage->io); |
| 2674 |
if (rc < 0) { |
| 2675 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 2676 |
return NULL; |
| 2677 |
} |
| 2678 |
|
| 2679 |
/* The uncompressed and compressed sizes start at byte 4 and 12 */ |
| 2680 |
int32_t chunk_nbytes; |
| 2681 |
int32_t chunk_cbytes; |
| 2682 |
rc = blosc2_cbuffer_sizes(chunk, &chunk_nbytes, &chunk_cbytes, NULL); |
| 2683 |
if (rc < 0) { |
| 2684 |
return NULL; |
| 2685 |
} |
| 2686 |
|
| 2687 |
if ((nchunks > 0) && (chunk_nbytes > chunksize)) { |
| 2688 |
BLOSC_TRACE_ERROR("Appending chunks with a larger chunksize than frame is " |
| 2689 |
"not allowed yet %d != %d.", chunk_nbytes, chunksize); |
| 2690 |
return NULL; |
| 2691 |
} |
| 2692 |
|
| 2693 |
// Check that we are not appending a small chunk after another small chunk |
| 2694 |
int32_t chunk_nbytes_last; |
| 2695 |
if (chunksize == 0 && (nchunks > 0) && (chunk_nbytes < chunksize)) { |
| 2696 |
uint8_t* last_chunk; |
| 2697 |
bool needs_free; |
| 2698 |
rc = frame_get_lazychunk(frame, nchunks - 1, &last_chunk, &needs_free); |
| 2699 |
if (rc < 0) { |
| 2700 |
BLOSC_TRACE_ERROR("Cannot get the last chunk (in position %" PRId64 ").", nchunks - 1); |
| 2701 |
} else { |
| 2702 |
rc = blosc2_cbuffer_sizes(last_chunk, &chunk_nbytes_last, NULL, NULL); |
| 2703 |
} |
| 2704 |
if (needs_free) { |
| 2705 |
free(last_chunk); |
| 2706 |
} |
| 2707 |
if (rc < 0) { |
| 2708 |
return NULL; |
| 2709 |
} |
| 2710 |
if ((chunk_nbytes_last < chunksize) && (nbytes < chunksize)) { |
| 2711 |
BLOSC_TRACE_ERROR("Appending two consecutive chunks with a chunksize smaller " |
| 2712 |
"than the frame chunksize is not allowed yet: %d != %d.", |
| 2713 |
chunk_nbytes, chunksize); |
| 2714 |
return NULL; |
| 2715 |
} |
| 2716 |
} |
| 2717 |
|
| 2718 |
// Get the current offsets and add one more |
| 2719 |
int32_t off_nbytes = (int32_t) ((nchunks + 1) * sizeof(int64_t)); |
| 2720 |
int64_t* offsets = (int64_t *) malloc((size_t)off_nbytes); |
| 2721 |
if (nchunks > 0) { |
| 2722 |
int32_t coffsets_cbytes; |
| 2723 |
uint8_t *coffsets = get_coffsets(frame, header_len, cbytes, nchunks, &coffsets_cbytes); |
| 2724 |
if (coffsets == NULL) { |
| 2725 |
BLOSC_TRACE_ERROR("Cannot get the offsets for the frame."); |
| 2726 |
free(offsets); |
| 2727 |
return NULL; |
| 2728 |
} |
| 2729 |
if (coffsets_cbytes == 0) { |
| 2730 |
coffsets_cbytes = (int32_t)cbytes; |
| 2731 |
} |
| 2732 |
|
| 2733 |
// Decompress offsets |
| 2734 |
blosc2_dparams off_dparams = BLOSC2_DPARAMS_DEFAULTS; |
| 2735 |
blosc2_context *dctx = blosc2_create_dctx(off_dparams); |
| 2736 |
if (dctx == NULL) { |
| 2737 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 2738 |
return NULL; |
| 2739 |
} |
| 2740 |
int32_t prev_nbytes = blosc2_decompress_ctx(dctx, coffsets, coffsets_cbytes, offsets, |
| 2741 |
off_nbytes); |
| 2742 |
blosc2_free_ctx(dctx); |
| 2743 |
if (prev_nbytes < 0) { |
| 2744 |
free(offsets); |
| 2745 |
BLOSC_TRACE_ERROR("Cannot decompress the offsets chunk."); |
| 2746 |
return NULL; |
| 2747 |
} |
| 2748 |
} |
| 2749 |
|
| 2750 |
// Add the new offset |
| 2751 |
int64_t sframe_chunk_id = -1; |
| 2752 |
int special_value = (chunk_[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK; |
| 2753 |
uint64_t offset_value = ((uint64_t)1 << 63); |
| 2754 |
switch (special_value) { |
| 2755 |
case BLOSC2_SPECIAL_ZERO: |
| 2756 |
// Zero chunk. Code it in a special way. |
| 2757 |
offset_value += (uint64_t) BLOSC2_SPECIAL_ZERO << (8 * 7); // chunk of zeros |
| 2758 |
to_little(offsets + nchunks, &offset_value, sizeof(uint64_t)); |
| 2759 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 2760 |
break; |
| 2761 |
case BLOSC2_SPECIAL_UNINIT: |
| 2762 |
// Non initizalized values chunk. Code it in a special way. |
| 2763 |
offset_value += (uint64_t) BLOSC2_SPECIAL_UNINIT << (8 * 7); // chunk of uninit values |
| 2764 |
to_little(offsets + nchunks, &offset_value, sizeof(uint64_t)); |
| 2765 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 2766 |
break; |
| 2767 |
case BLOSC2_SPECIAL_NAN: |
| 2768 |
// NaN chunk. Code it in a special way. |
| 2769 |
offset_value += (uint64_t)BLOSC2_SPECIAL_NAN << (8 * 7); // chunk of NANs |
| 2770 |
to_little(offsets + nchunks, &offset_value, sizeof(uint64_t)); |
| 2771 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 2772 |
break; |
| 2773 |
default: |
| 2774 |
if (frame->sframe) { |
| 2775 |
// Compute the sframe_chunk_id value |
| 2776 |
for (int i = 0; i < nchunks; ++i) { |
| 2777 |
if (offsets[i] > sframe_chunk_id) { |
| 2778 |
sframe_chunk_id = offsets[i]; |
| 2779 |
} |
| 2780 |
} |
| 2781 |
offsets[nchunks] = ++sframe_chunk_id; |
| 2782 |
} |
| 2783 |
else { |
| 2784 |
offsets[nchunks] = cbytes; |
| 2785 |
} |
| 2786 |
} |
| 2787 |
|
| 2788 |
// Re-compress the offsets again |
| 2789 |
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS; |
| 2790 |
cparams.splitmode = BLOSC_NEVER_SPLIT; |
| 2791 |
cparams.typesize = sizeof(int64_t); |
| 2792 |
cparams.blocksize = 16 * 1024; // based on experiments with create_frame.c bench |
| 2793 |
cparams.nthreads = 4; // 4 threads seems a decent default for nowadays CPUs |
| 2794 |
cparams.compcode = BLOSC_BLOSCLZ; |
| 2795 |
blosc2_context* cctx = blosc2_create_cctx(cparams); |
| 2796 |
if (cctx == NULL) { |
| 2797 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 2798 |
return NULL; |
| 2799 |
} |
| 2800 |
cctx->typesize = sizeof(int64_t); // override a possible BLOSC_TYPESIZE env variable (or chaos may appear) |
| 2801 |
void* off_chunk = malloc((size_t)off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 2802 |
int32_t new_off_cbytes = blosc2_compress_ctx(cctx, offsets, off_nbytes, |
| 2803 |
off_chunk, off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 2804 |
blosc2_free_ctx(cctx); |
| 2805 |
free(offsets); |
| 2806 |
if (new_off_cbytes < 0) { |
| 2807 |
free(off_chunk); |
| 2808 |
return NULL; |
| 2809 |
} |
| 2810 |
// printf("%f\n", (double) off_nbytes / new_off_cbytes); |
| 2811 |
|
| 2812 |
int64_t new_cbytes = cbytes + chunk_cbytes; |
| 2813 |
int64_t new_frame_len; |
| 2814 |
if (frame->sframe) { |
| 2815 |
new_frame_len = header_len + 0 + new_off_cbytes + frame->trailer_len; |
| 2816 |
} |
| 2817 |
else { |
| 2818 |
new_frame_len = header_len + new_cbytes + new_off_cbytes + frame->trailer_len; |
| 2819 |
} |
| 2820 |
|
| 2821 |
void* fp = NULL; |
| 2822 |
if (frame->cframe != NULL) { |
| 2823 |
uint8_t* framep = frame->cframe; |
| 2824 |
/* Make space for the new chunk and copy it */ |
| 2825 |
frame->cframe = framep = realloc(framep, (size_t)new_frame_len); |
| 2826 |
if (framep == NULL) { |
| 2827 |
BLOSC_TRACE_ERROR("Cannot realloc space for the frame."); |
| 2828 |
return NULL; |
| 2829 |
} |
| 2830 |
/* Copy the chunk */ |
| 2831 |
memcpy(framep + header_len + cbytes, chunk, (size_t)chunk_cbytes); |
| 2832 |
/* Copy the offsets */ |
| 2833 |
memcpy(framep + header_len + new_cbytes, off_chunk, (size_t)new_off_cbytes); |
| 2834 |
} |
| 2835 |
else { |
| 2836 |
int64_t wbytes; |
| 2837 |
int64_t io_pos = 0; |
| 2838 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 2839 |
if (io_cb == NULL) { |
| 2840 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 2841 |
return NULL; |
| 2842 |
} |
| 2843 |
|
| 2844 |
if (frame->sframe) { |
| 2845 |
// Update the offsets chunk in the chunks frame |
| 2846 |
if (chunk_cbytes != 0) { |
| 2847 |
if (sframe_chunk_id < 0) { |
| 2848 |
BLOSC_TRACE_ERROR("The chunk id (%" PRId64 ") is not correct", sframe_chunk_id); |
| 2849 |
return NULL; |
| 2850 |
} |
| 2851 |
if (sframe_create_chunk(frame, chunk, sframe_chunk_id, chunk_cbytes) == NULL) { |
| 2852 |
BLOSC_TRACE_ERROR("Cannot write the full chunk."); |
| 2853 |
return NULL; |
| 2854 |
} |
| 2855 |
} |
| 2856 |
fp = sframe_open_index(frame->urlpath, "rb+", |
| 2857 |
frame->schunk->storage->io); |
| 2858 |
if (fp == NULL) { |
| 2859 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 2860 |
return NULL; |
| 2861 |
} |
| 2862 |
io_pos = frame->file_offset + header_len; |
| 2863 |
} |
| 2864 |
else { |
| 2865 |
// Regular frame |
| 2866 |
fp = io_cb->open(frame->urlpath, "rb+", frame->schunk->storage->io->params); |
| 2867 |
if (fp == NULL) { |
| 2868 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 2869 |
return NULL; |
| 2870 |
} |
| 2871 |
io_pos = frame->file_offset + header_len + cbytes; |
| 2872 |
wbytes = io_cb->write(chunk, 1, chunk_cbytes, io_pos, fp); // the new chunk |
| 2873 |
io_pos += chunk_cbytes; |
| 2874 |
if (wbytes != chunk_cbytes) { |
| 2875 |
BLOSC_TRACE_ERROR("Cannot write the full chunk to frame."); |
| 2876 |
io_cb->close(fp); |
| 2877 |
return NULL; |
| 2878 |
} |
| 2879 |
} |
| 2880 |
wbytes = io_cb->write(off_chunk, 1, new_off_cbytes, io_pos, fp); // the new offsets |
| 2881 |
io_cb->close(fp); |
| 2882 |
if (wbytes != new_off_cbytes) { |
| 2883 |
BLOSC_TRACE_ERROR("Cannot write the offsets to frame."); |
| 2884 |
return NULL; |
| 2885 |
} |
| 2886 |
} |
| 2887 |
// Invalidate the cache for chunk offsets |
| 2888 |
if (frame->coffsets != NULL) { |
| 2889 |
if (frame->coffsets_needs_free) |
| 2890 |
free(frame->coffsets); |
| 2891 |
frame->coffsets = NULL; |
| 2892 |
} |
| 2893 |
free(chunk); // chunk has always to be a copy when reaching here... |
| 2894 |
free(off_chunk); |
| 2895 |
|
| 2896 |
frame->len = new_frame_len; |
| 2897 |
rc = frame_update_header(frame, schunk, false); |
| 2898 |
if (rc < 0) { |
| 2899 |
return NULL; |
| 2900 |
} |
| 2901 |
|
| 2902 |
rc = frame_update_trailer(frame, schunk); |
| 2903 |
if (rc < 0) { |
| 2904 |
return NULL; |
| 2905 |
} |
| 2906 |
|
| 2907 |
return frame; |
| 2908 |
} |
| 2909 |
|
| 2910 |
|
| 2911 |
void* frame_insert_chunk(blosc2_frame_s* frame, int64_t nchunk, void* chunk, blosc2_schunk* schunk) { |
| 2912 |
uint8_t* chunk_ = chunk; |
| 2913 |
int32_t header_len; |
| 2914 |
int64_t frame_len; |
| 2915 |
int64_t nbytes; |
| 2916 |
int64_t cbytes; |
| 2917 |
int32_t blocksize; |
| 2918 |
int32_t chunksize; |
| 2919 |
int64_t nchunks; |
| 2920 |
int rc = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 2921 |
&blocksize, &chunksize, &nchunks, |
| 2922 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 2923 |
frame->schunk->storage->io); |
| 2924 |
if (rc < 0) { |
| 2925 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 2926 |
return NULL; |
| 2927 |
} |
| 2928 |
int32_t chunk_cbytes; |
| 2929 |
rc = blosc2_cbuffer_sizes(chunk_, NULL, &chunk_cbytes, NULL); |
| 2930 |
if (rc < 0) { |
| 2931 |
return NULL; |
| 2932 |
} |
| 2933 |
|
| 2934 |
// Get the current offsets |
| 2935 |
int32_t off_nbytes = (int32_t) ((nchunks + 1) * sizeof(int64_t)); |
| 2936 |
int64_t* offsets = (int64_t *) malloc((size_t)off_nbytes); |
| 2937 |
if (nchunks > 0) { |
| 2938 |
int32_t coffsets_cbytes = 0; |
| 2939 |
uint8_t *coffsets = get_coffsets(frame, header_len, cbytes, nchunks, &coffsets_cbytes); |
| 2940 |
if (coffsets == NULL) { |
| 2941 |
BLOSC_TRACE_ERROR("Cannot get the offsets for the frame."); |
| 2942 |
return NULL; |
| 2943 |
} |
| 2944 |
if (coffsets_cbytes == 0) { |
| 2945 |
coffsets_cbytes = (int32_t)cbytes; |
| 2946 |
} |
| 2947 |
|
| 2948 |
// Decompress offsets |
| 2949 |
blosc2_dparams off_dparams = BLOSC2_DPARAMS_DEFAULTS; |
| 2950 |
blosc2_context *dctx = blosc2_create_dctx(off_dparams); |
| 2951 |
if (dctx == NULL) { |
| 2952 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 2953 |
return NULL; |
| 2954 |
} |
| 2955 |
int32_t prev_nbytes = blosc2_decompress_ctx(dctx, coffsets, coffsets_cbytes, offsets, off_nbytes); |
| 2956 |
blosc2_free_ctx(dctx); |
| 2957 |
if (prev_nbytes < 0) { |
| 2958 |
free(offsets); |
| 2959 |
BLOSC_TRACE_ERROR("Cannot decompress the offsets chunk."); |
| 2960 |
return NULL; |
| 2961 |
} |
| 2962 |
} |
| 2963 |
|
| 2964 |
// TODO: Improvement: Check if new chunk is smaller than previous one |
| 2965 |
|
| 2966 |
// Move offsets |
| 2967 |
for (int64_t i = nchunks; i > nchunk; i--) { |
| 2968 |
offsets[i] = offsets[i - 1]; |
| 2969 |
} |
| 2970 |
// Add the new offset |
| 2971 |
int64_t sframe_chunk_id = -1; |
| 2972 |
int special_value = (chunk_[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK; |
| 2973 |
uint64_t offset_value = ((uint64_t)1 << 63); |
| 2974 |
switch (special_value) { |
| 2975 |
case BLOSC2_SPECIAL_ZERO: |
| 2976 |
// Zero chunk. Code it in a special way. |
| 2977 |
offset_value += (uint64_t)BLOSC2_SPECIAL_ZERO << (8 * 7); // indicate a chunk of zeros |
| 2978 |
to_little(offsets + nchunk, &offset_value, sizeof(uint64_t)); |
| 2979 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 2980 |
break; |
| 2981 |
case BLOSC2_SPECIAL_UNINIT: |
| 2982 |
// Non initizalized values chunk. Code it in a special way. |
| 2983 |
offset_value += (uint64_t) BLOSC2_SPECIAL_UNINIT << (8 * 7); // chunk of uninit values |
| 2984 |
to_little(offsets + nchunk, &offset_value, sizeof(uint64_t)); |
| 2985 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 2986 |
break; |
| 2987 |
case BLOSC2_SPECIAL_NAN: |
| 2988 |
// NaN chunk. Code it in a special way. |
| 2989 |
offset_value += (uint64_t)BLOSC2_SPECIAL_NAN << (8 * 7); // indicate a chunk of NANs |
| 2990 |
to_little(offsets + nchunk, &offset_value, sizeof(uint64_t)); |
| 2991 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 2992 |
break; |
| 2993 |
default: |
| 2994 |
if (frame->sframe) { |
| 2995 |
for (int i = 0; i <= nchunks; ++i) { |
| 2996 |
// offsets[nchunk] is still uninitialized here |
| 2997 |
if (i != nchunk && offsets[i] > sframe_chunk_id) { |
| 2998 |
sframe_chunk_id = offsets[i]; |
| 2999 |
} |
| 3000 |
} |
| 3001 |
offsets[nchunk] = ++sframe_chunk_id; |
| 3002 |
} |
| 3003 |
else { |
| 3004 |
offsets[nchunk] = cbytes; |
| 3005 |
} |
| 3006 |
} |
| 3007 |
|
| 3008 |
// Re-compress the offsets again |
| 3009 |
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS; |
| 3010 |
cparams.splitmode = BLOSC_NEVER_SPLIT; |
| 3011 |
cparams.typesize = sizeof(int64_t); |
| 3012 |
cparams.blocksize = 16 * 1024; // based on experiments with create_frame.c bench |
| 3013 |
cparams.nthreads = 4; // 4 threads seems a decent default for nowadays CPUs |
| 3014 |
cparams.compcode = BLOSC_BLOSCLZ; |
| 3015 |
blosc2_context* cctx = blosc2_create_cctx(cparams); |
| 3016 |
if (cctx == NULL) { |
| 3017 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 3018 |
return NULL; |
| 3019 |
} |
| 3020 |
void* off_chunk = malloc((size_t)off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 3021 |
int32_t new_off_cbytes = blosc2_compress_ctx(cctx, offsets, off_nbytes, |
| 3022 |
off_chunk, off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 3023 |
blosc2_free_ctx(cctx); |
| 3024 |
|
| 3025 |
free(offsets); |
| 3026 |
if (new_off_cbytes < 0) { |
| 3027 |
free(off_chunk); |
| 3028 |
return NULL; |
| 3029 |
} |
| 3030 |
|
| 3031 |
int64_t new_cbytes = cbytes + chunk_cbytes; |
| 3032 |
|
| 3033 |
int64_t new_frame_len; |
| 3034 |
if (frame->sframe) { |
| 3035 |
new_frame_len = header_len + 0 + new_off_cbytes + frame->trailer_len; |
| 3036 |
} |
| 3037 |
else { |
| 3038 |
new_frame_len = header_len + new_cbytes + new_off_cbytes + frame->trailer_len; |
| 3039 |
} |
| 3040 |
|
| 3041 |
// Add the chunk and update meta |
| 3042 |
void* fp = NULL; |
| 3043 |
if (frame->cframe != NULL) { |
| 3044 |
uint8_t* framep = frame->cframe; |
| 3045 |
/* Make space for the new chunk and copy it */ |
| 3046 |
frame->cframe = framep = realloc(framep, (size_t)new_frame_len); |
| 3047 |
if (framep == NULL) { |
| 3048 |
BLOSC_TRACE_ERROR("Cannot realloc space for the frame."); |
| 3049 |
return NULL; |
| 3050 |
} |
| 3051 |
/* Copy the chunk */ |
| 3052 |
memcpy(framep + header_len + cbytes, chunk, (size_t)chunk_cbytes); |
| 3053 |
/* Copy the offsets */ |
| 3054 |
memcpy(framep + header_len + new_cbytes, off_chunk, (size_t)new_off_cbytes); |
| 3055 |
} else { |
| 3056 |
int64_t wbytes; |
| 3057 |
|
| 3058 |
int64_t io_pos = 0; |
| 3059 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 3060 |
if (io_cb == NULL) { |
| 3061 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 3062 |
return NULL; |
| 3063 |
} |
| 3064 |
|
| 3065 |
if (frame->sframe) { |
| 3066 |
if (chunk_cbytes != 0) { |
| 3067 |
if (sframe_chunk_id < 0) { |
| 3068 |
BLOSC_TRACE_ERROR("The chunk id (%" PRId64 ") is not correct", sframe_chunk_id); |
| 3069 |
return NULL; |
| 3070 |
} |
| 3071 |
if (sframe_create_chunk(frame, chunk, sframe_chunk_id, chunk_cbytes) == NULL) { |
| 3072 |
BLOSC_TRACE_ERROR("Cannot write the full chunk."); |
| 3073 |
return NULL; |
| 3074 |
} |
| 3075 |
} |
| 3076 |
// Update the offsets chunk in the chunks frame |
| 3077 |
fp = sframe_open_index(frame->urlpath, "rb+", |
| 3078 |
frame->schunk->storage->io); |
| 3079 |
if (fp == NULL) { |
| 3080 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 3081 |
return NULL; |
| 3082 |
} |
| 3083 |
io_pos = frame->file_offset + header_len + 0; |
| 3084 |
} |
| 3085 |
else { |
| 3086 |
// Regular frame |
| 3087 |
fp = io_cb->open(frame->urlpath, "rb+", frame->schunk->storage->io->params); |
| 3088 |
if (fp == NULL) { |
| 3089 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 3090 |
return NULL; |
| 3091 |
} |
| 3092 |
io_pos = frame->file_offset + header_len + cbytes; |
| 3093 |
wbytes = io_cb->write(chunk, 1, chunk_cbytes, io_pos, fp); // the new chunk |
| 3094 |
io_pos += chunk_cbytes; |
| 3095 |
if (wbytes != chunk_cbytes) { |
| 3096 |
BLOSC_TRACE_ERROR("Cannot write the full chunk to frame."); |
| 3097 |
io_cb->close(fp); |
| 3098 |
return NULL; |
| 3099 |
} |
| 3100 |
} |
| 3101 |
wbytes = io_cb->write(off_chunk, 1, new_off_cbytes, io_pos, fp); // the new offsets |
| 3102 |
io_cb->close(fp); |
| 3103 |
if (wbytes != new_off_cbytes) { |
| 3104 |
BLOSC_TRACE_ERROR("Cannot write the offsets to frame."); |
| 3105 |
return NULL; |
| 3106 |
} |
| 3107 |
// Invalidate the cache for chunk offsets |
| 3108 |
if (frame->coffsets != NULL) { |
| 3109 |
if (frame->coffsets_needs_free) |
| 3110 |
free(frame->coffsets); |
| 3111 |
frame->coffsets = NULL; |
| 3112 |
} |
| 3113 |
} |
| 3114 |
free(chunk); // chunk has always to be a copy when reaching here... |
| 3115 |
free(off_chunk); |
| 3116 |
|
| 3117 |
frame->len = new_frame_len; |
| 3118 |
rc = frame_update_header(frame, schunk, false); |
| 3119 |
if (rc < 0) { |
| 3120 |
return NULL; |
| 3121 |
} |
| 3122 |
|
| 3123 |
rc = frame_update_trailer(frame, schunk); |
| 3124 |
if (rc < 0) { |
| 3125 |
return NULL; |
| 3126 |
} |
| 3127 |
|
| 3128 |
return frame; |
| 3129 |
} |
| 3130 |
|
| 3131 |
|
| 3132 |
void* frame_update_chunk(blosc2_frame_s* frame, int64_t nchunk, void* chunk, blosc2_schunk* schunk) { |
| 3133 |
uint8_t *chunk_ = (uint8_t *) chunk; |
| 3134 |
int32_t header_len; |
| 3135 |
int64_t frame_len; |
| 3136 |
int64_t nbytes; |
| 3137 |
int64_t cbytes; |
| 3138 |
int32_t blocksize; |
| 3139 |
int32_t chunksize; |
| 3140 |
int64_t nchunks; |
| 3141 |
int rc = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 3142 |
&blocksize, &chunksize, &nchunks, |
| 3143 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 3144 |
frame->schunk->storage->io); |
| 3145 |
if (rc < 0) { |
| 3146 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 3147 |
return NULL; |
| 3148 |
} |
| 3149 |
if (nchunk >= nchunks) { |
| 3150 |
BLOSC_TRACE_ERROR("The chunk must already exist."); |
| 3151 |
return NULL; |
| 3152 |
} |
| 3153 |
|
| 3154 |
int32_t chunk_cbytes; |
| 3155 |
rc = blosc2_cbuffer_sizes(chunk, NULL, &chunk_cbytes, NULL); |
| 3156 |
if (rc < 0) { |
| 3157 |
return NULL; |
| 3158 |
} |
| 3159 |
|
| 3160 |
// Get the current offsets |
| 3161 |
int32_t off_nbytes = (int32_t) (nchunks * sizeof(int64_t)); |
| 3162 |
int64_t* offsets = (int64_t *) malloc((size_t)off_nbytes); |
| 3163 |
if (nchunks > 0) { |
| 3164 |
int32_t coffsets_cbytes = 0; |
| 3165 |
uint8_t *coffsets = get_coffsets(frame, header_len, cbytes, nchunks, &coffsets_cbytes); |
| 3166 |
if (coffsets == NULL) { |
| 3167 |
BLOSC_TRACE_ERROR("Cannot get the offsets for the frame."); |
| 3168 |
return NULL; |
| 3169 |
} |
| 3170 |
if (coffsets_cbytes == 0) { |
| 3171 |
coffsets_cbytes = (int32_t)cbytes; |
| 3172 |
} |
| 3173 |
|
| 3174 |
// Decompress offsets |
| 3175 |
blosc2_dparams off_dparams = BLOSC2_DPARAMS_DEFAULTS; |
| 3176 |
blosc2_context *dctx = blosc2_create_dctx(off_dparams); |
| 3177 |
if (dctx == NULL) { |
| 3178 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 3179 |
return NULL; |
| 3180 |
} |
| 3181 |
int32_t prev_nbytes = blosc2_decompress_ctx(dctx, coffsets, coffsets_cbytes, offsets, off_nbytes); |
| 3182 |
blosc2_free_ctx(dctx); |
| 3183 |
if (prev_nbytes < 0) { |
| 3184 |
free(offsets); |
| 3185 |
BLOSC_TRACE_ERROR("Cannot decompress the offsets chunk."); |
| 3186 |
return NULL; |
| 3187 |
} |
| 3188 |
} |
| 3189 |
int32_t cbytes_old = 0; |
| 3190 |
int64_t old_offset = 0; |
| 3191 |
if (!frame->sframe) { |
| 3192 |
// See how big would be the space |
| 3193 |
old_offset = offsets[nchunk]; |
| 3194 |
bool needs_free; |
| 3195 |
uint8_t *chunk_old; |
| 3196 |
int err = blosc2_schunk_get_chunk(schunk, nchunk, &chunk_old, &needs_free); |
| 3197 |
if (err < 0) { |
| 3198 |
BLOSC_TRACE_ERROR("%" PRId64 " chunk can not be obtained from schunk.", nchunk); |
| 3199 |
return NULL; |
| 3200 |
} |
| 3201 |
|
| 3202 |
if (chunk_old == NULL) { |
| 3203 |
cbytes_old = 0; |
| 3204 |
} |
| 3205 |
else { |
| 3206 |
cbytes_old = sw32_(chunk_old + BLOSC2_CHUNK_CBYTES); |
| 3207 |
if (cbytes_old == BLOSC2_MAX_OVERHEAD) { |
| 3208 |
cbytes_old = 0; |
| 3209 |
} |
| 3210 |
} |
| 3211 |
if (needs_free) { |
| 3212 |
free(chunk_old); |
| 3213 |
} |
| 3214 |
} |
| 3215 |
|
| 3216 |
// Add the new offset |
| 3217 |
int64_t sframe_chunk_id = -1; |
| 3218 |
if (frame->sframe) { |
| 3219 |
if (offsets[nchunk] < 0) { |
| 3220 |
sframe_chunk_id = -1; |
| 3221 |
} |
| 3222 |
else { |
| 3223 |
// In case there was a reorder in a sframe |
| 3224 |
sframe_chunk_id = offsets[nchunk]; |
| 3225 |
} |
| 3226 |
} |
| 3227 |
int special_value = (chunk_[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK; |
| 3228 |
uint64_t offset_value = ((uint64_t)1 << 63); |
| 3229 |
switch (special_value) { |
| 3230 |
case BLOSC2_SPECIAL_ZERO: |
| 3231 |
// Zero chunk. Code it in a special way. |
| 3232 |
offset_value += (uint64_t)BLOSC2_SPECIAL_ZERO << (8 * 7); // indicate a chunk of zeros |
| 3233 |
to_little(offsets + nchunk, &offset_value, sizeof(uint64_t)); |
| 3234 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 3235 |
break; |
| 3236 |
case BLOSC2_SPECIAL_UNINIT: |
| 3237 |
// Non initizalized values chunk. Code it in a special way. |
| 3238 |
offset_value += (uint64_t)BLOSC2_SPECIAL_UNINIT << (8 * 7); // indicate a chunk of uninit values |
| 3239 |
to_little(offsets + nchunk, &offset_value, sizeof(uint64_t)); |
| 3240 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 3241 |
break; |
| 3242 |
case BLOSC2_SPECIAL_NAN: |
| 3243 |
// NaN chunk. Code it in a special way. |
| 3244 |
offset_value += (uint64_t)BLOSC2_SPECIAL_NAN << (8 * 7); // indicate a chunk of NANs |
| 3245 |
to_little(offsets + nchunk, &offset_value, sizeof(uint64_t)); |
| 3246 |
chunk_cbytes = 0; // we don't need to store the chunk |
| 3247 |
break; |
| 3248 |
default: |
| 3249 |
if (frame->sframe) { |
| 3250 |
if (sframe_chunk_id < 0) { |
| 3251 |
for (int i = 0; i < nchunks; ++i) { |
| 3252 |
if (offsets[i] > sframe_chunk_id) { |
| 3253 |
sframe_chunk_id = offsets[i]; |
| 3254 |
} |
| 3255 |
} |
| 3256 |
offsets[nchunk] = ++sframe_chunk_id; |
| 3257 |
} |
| 3258 |
} |
| 3259 |
else { |
| 3260 |
// Add the new offset |
| 3261 |
offsets[nchunk] = cbytes; |
| 3262 |
} |
| 3263 |
} |
| 3264 |
|
| 3265 |
if (!frame->sframe && chunk_cbytes != 0 && cbytes_old >= chunk_cbytes) { |
| 3266 |
offsets[nchunk] = old_offset; |
| 3267 |
cbytes = old_offset; |
| 3268 |
} |
| 3269 |
// Re-compress the offsets again |
| 3270 |
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS; |
| 3271 |
cparams.splitmode = BLOSC_NEVER_SPLIT; |
| 3272 |
cparams.typesize = sizeof(int64_t); |
| 3273 |
cparams.blocksize = 16 * 1024; // based on experiments with create_frame.c bench |
| 3274 |
cparams.nthreads = 4; // 4 threads seems a decent default for nowadays CPUs |
| 3275 |
cparams.compcode = BLOSC_BLOSCLZ; |
| 3276 |
blosc2_context* cctx = blosc2_create_cctx(cparams); |
| 3277 |
if (cctx == NULL) { |
| 3278 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 3279 |
return NULL; |
| 3280 |
} |
| 3281 |
void* off_chunk = malloc((size_t)off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 3282 |
int32_t new_off_cbytes = blosc2_compress_ctx(cctx, offsets, off_nbytes, |
| 3283 |
off_chunk, off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 3284 |
blosc2_free_ctx(cctx); |
| 3285 |
|
| 3286 |
free(offsets); |
| 3287 |
if (new_off_cbytes < 0) { |
| 3288 |
free(off_chunk); |
| 3289 |
return NULL; |
| 3290 |
} |
| 3291 |
|
| 3292 |
int64_t new_cbytes = schunk->cbytes; |
| 3293 |
int64_t new_frame_len; |
| 3294 |
if (frame->sframe) { |
| 3295 |
// The chunk is not stored in the frame |
| 3296 |
new_frame_len = header_len + 0 + new_off_cbytes + frame->trailer_len; |
| 3297 |
} |
| 3298 |
else { |
| 3299 |
new_frame_len = header_len + new_cbytes + new_off_cbytes + frame->trailer_len; |
| 3300 |
} |
| 3301 |
|
| 3302 |
void* fp = NULL; |
| 3303 |
if (frame->cframe != NULL) { |
| 3304 |
uint8_t* framep = frame->cframe; |
| 3305 |
/* Make space for the new chunk and copy it */ |
| 3306 |
frame->cframe = framep = realloc(framep, (size_t)new_frame_len); |
| 3307 |
if (framep == NULL) { |
| 3308 |
BLOSC_TRACE_ERROR("Cannot realloc space for the frame."); |
| 3309 |
return NULL; |
| 3310 |
} |
| 3311 |
/* Copy the chunk */ |
| 3312 |
memcpy(framep + header_len + cbytes, chunk, (size_t)chunk_cbytes); |
| 3313 |
/* Copy the offsets */ |
| 3314 |
memcpy(framep + header_len + new_cbytes, off_chunk, (size_t)new_off_cbytes); |
| 3315 |
} else { |
| 3316 |
int64_t wbytes; |
| 3317 |
|
| 3318 |
int64_t io_pos = 0; |
| 3319 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 3320 |
if (io_cb == NULL) { |
| 3321 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 3322 |
return NULL; |
| 3323 |
} |
| 3324 |
|
| 3325 |
if (frame->sframe) { |
| 3326 |
// Create the chunks file, if it's a special value this will delete its old content |
| 3327 |
if (sframe_chunk_id >= 0) { |
| 3328 |
if (sframe_create_chunk(frame, chunk, sframe_chunk_id, chunk_cbytes) == NULL) { |
| 3329 |
BLOSC_TRACE_ERROR("Cannot write the full chunk."); |
| 3330 |
return NULL; |
| 3331 |
} |
| 3332 |
} |
| 3333 |
// Update the offsets chunk in the chunks frame |
| 3334 |
fp = sframe_open_index(frame->urlpath, "rb+", |
| 3335 |
frame->schunk->storage->io); |
| 3336 |
if (fp == NULL) { |
| 3337 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 3338 |
return NULL; |
| 3339 |
} |
| 3340 |
io_pos = frame->file_offset + header_len + 0; |
| 3341 |
} |
| 3342 |
else { |
| 3343 |
// Regular frame |
| 3344 |
fp = io_cb->open(frame->urlpath, "rb+", frame->schunk->storage->io->params); |
| 3345 |
if (fp == NULL) { |
| 3346 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 3347 |
return NULL; |
| 3348 |
} |
| 3349 |
io_pos = frame->file_offset + header_len + cbytes; |
| 3350 |
wbytes = io_cb->write(chunk, 1, chunk_cbytes, io_pos, fp); // the new chunk |
| 3351 |
io_pos += chunk_cbytes; |
| 3352 |
if (wbytes != chunk_cbytes) { |
| 3353 |
BLOSC_TRACE_ERROR("Cannot write the full chunk to frame."); |
| 3354 |
io_cb->close(fp); |
| 3355 |
return NULL; |
| 3356 |
} |
| 3357 |
io_pos = frame->file_offset + header_len + new_cbytes; |
| 3358 |
} |
| 3359 |
wbytes = io_cb->write(off_chunk, 1, new_off_cbytes, io_pos, fp); // the new offsets |
| 3360 |
io_cb->close(fp); |
| 3361 |
if (wbytes != new_off_cbytes) { |
| 3362 |
BLOSC_TRACE_ERROR("Cannot write the offsets to frame."); |
| 3363 |
return NULL; |
| 3364 |
} |
| 3365 |
// Invalidate the cache for chunk offsets |
| 3366 |
if (frame->coffsets != NULL) { |
| 3367 |
if (frame->coffsets_needs_free) |
| 3368 |
free(frame->coffsets); |
| 3369 |
frame->coffsets = NULL; |
| 3370 |
} |
| 3371 |
} |
| 3372 |
free(chunk); // chunk has always to be a copy when reaching here... |
| 3373 |
free(off_chunk); |
| 3374 |
|
| 3375 |
frame->len = new_frame_len; |
| 3376 |
rc = frame_update_header(frame, schunk, false); |
| 3377 |
if (rc < 0) { |
| 3378 |
return NULL; |
| 3379 |
} |
| 3380 |
|
| 3381 |
rc = frame_update_trailer(frame, schunk); |
| 3382 |
if (rc < 0) { |
| 3383 |
return NULL; |
| 3384 |
} |
| 3385 |
|
| 3386 |
return frame; |
| 3387 |
} |
| 3388 |
|
| 3389 |
|
| 3390 |
void* frame_delete_chunk(blosc2_frame_s* frame, int64_t nchunk, blosc2_schunk* schunk) { |
| 3391 |
int32_t header_len; |
| 3392 |
int64_t frame_len; |
| 3393 |
int64_t nbytes; |
| 3394 |
int64_t cbytes; |
| 3395 |
int32_t blocksize; |
| 3396 |
int32_t chunksize; |
| 3397 |
int64_t nchunks; |
| 3398 |
int rc = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 3399 |
&blocksize, &chunksize, &nchunks, |
| 3400 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, frame->schunk->storage->io); |
| 3401 |
if (rc < 0) { |
| 3402 |
BLOSC_TRACE_ERROR("Unable to get meta info from frame."); |
| 3403 |
return NULL; |
| 3404 |
} |
| 3405 |
|
| 3406 |
// Get the current offsets |
| 3407 |
int32_t off_nbytes = (int32_t) (nchunks * sizeof(int64_t)); |
| 3408 |
int64_t* offsets = (int64_t *) malloc((size_t)off_nbytes); |
| 3409 |
if (nchunks > 0) { |
| 3410 |
int32_t coffsets_cbytes = 0; |
| 3411 |
uint8_t *coffsets = get_coffsets(frame, header_len, cbytes, nchunks, &coffsets_cbytes); |
| 3412 |
if (coffsets == NULL) { |
| 3413 |
BLOSC_TRACE_ERROR("Cannot get the offsets for the frame."); |
| 3414 |
return NULL; |
| 3415 |
} |
| 3416 |
if (coffsets_cbytes == 0) { |
| 3417 |
coffsets_cbytes = (int32_t)cbytes; |
| 3418 |
} |
| 3419 |
|
| 3420 |
// Decompress offsets |
| 3421 |
blosc2_dparams off_dparams = BLOSC2_DPARAMS_DEFAULTS; |
| 3422 |
blosc2_context *dctx = blosc2_create_dctx(off_dparams); |
| 3423 |
if (dctx == NULL) { |
| 3424 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 3425 |
return NULL; |
| 3426 |
} |
| 3427 |
int32_t prev_nbytes = blosc2_decompress_ctx(dctx, coffsets, coffsets_cbytes, offsets, off_nbytes); |
| 3428 |
blosc2_free_ctx(dctx); |
| 3429 |
if (prev_nbytes < 0) { |
| 3430 |
free(offsets); |
| 3431 |
BLOSC_TRACE_ERROR("Cannot decompress the offsets chunk."); |
| 3432 |
return NULL; |
| 3433 |
} |
| 3434 |
} |
| 3435 |
|
| 3436 |
// Delete the new offset |
| 3437 |
for (int64_t i = nchunk; i < nchunks - 1; i++) { |
| 3438 |
offsets[i] = offsets[i + 1]; |
| 3439 |
} |
| 3440 |
offsets[nchunks - 1] = 0; |
| 3441 |
|
| 3442 |
// Re-compress the offsets again |
| 3443 |
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS; |
| 3444 |
cparams.splitmode = BLOSC_NEVER_SPLIT; |
| 3445 |
cparams.typesize = sizeof(int64_t); |
| 3446 |
cparams.blocksize = 16 * 1024; // based on experiments with create_frame.c bench |
| 3447 |
cparams.nthreads = 4; // 4 threads seems a decent default for nowadays CPUs |
| 3448 |
cparams.compcode = BLOSC_BLOSCLZ; |
| 3449 |
blosc2_context* cctx = blosc2_create_cctx(cparams); |
| 3450 |
if (cctx == NULL) { |
| 3451 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 3452 |
return NULL; |
| 3453 |
} |
| 3454 |
void* off_chunk = malloc((size_t)off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 3455 |
int32_t new_off_cbytes = blosc2_compress_ctx(cctx, offsets, off_nbytes - (int32_t)sizeof(int64_t), |
| 3456 |
off_chunk, off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 3457 |
blosc2_free_ctx(cctx); |
| 3458 |
|
| 3459 |
free(offsets); |
| 3460 |
if (new_off_cbytes < 0) { |
| 3461 |
free(off_chunk); |
| 3462 |
return NULL; |
| 3463 |
} |
| 3464 |
|
| 3465 |
int64_t new_cbytes = cbytes; |
| 3466 |
|
| 3467 |
int64_t new_frame_len; |
| 3468 |
if (frame->sframe) { |
| 3469 |
new_frame_len = header_len + 0 + new_off_cbytes + frame->trailer_len; |
| 3470 |
} |
| 3471 |
else { |
| 3472 |
new_frame_len = header_len + new_cbytes + new_off_cbytes + frame->trailer_len; |
| 3473 |
} |
| 3474 |
|
| 3475 |
// Add the chunk and update meta |
| 3476 |
FILE* fp = NULL; |
| 3477 |
if (frame->cframe != NULL) { |
| 3478 |
uint8_t* framep = frame->cframe; |
| 3479 |
/* Make space for the new chunk and copy it */ |
| 3480 |
frame->cframe = framep = realloc(framep, (size_t)new_frame_len); |
| 3481 |
if (framep == NULL) { |
| 3482 |
BLOSC_TRACE_ERROR("Cannot realloc space for the frame."); |
| 3483 |
return NULL; |
| 3484 |
} |
| 3485 |
/* Copy the offsets */ |
| 3486 |
memcpy(framep + header_len + new_cbytes, off_chunk, (size_t)new_off_cbytes); |
| 3487 |
} else { |
| 3488 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 3489 |
if (io_cb == NULL) { |
| 3490 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 3491 |
return NULL; |
| 3492 |
} |
| 3493 |
|
| 3494 |
size_t wbytes; |
| 3495 |
int64_t io_pos = 0; |
| 3496 |
if (frame->sframe) { |
| 3497 |
int64_t offset; |
| 3498 |
rc = get_coffset(frame, header_len, cbytes, nchunk, nchunks, &offset); |
| 3499 |
if (rc < 0) { |
| 3500 |
BLOSC_TRACE_ERROR("Unable to get offset to chunk %" PRId64 ".", nchunk); |
| 3501 |
return NULL; |
| 3502 |
} |
| 3503 |
if (offset >= 0){ |
| 3504 |
// Remove the chunk file only if it is not a special value chunk |
| 3505 |
int err = sframe_delete_chunk(frame->urlpath, offset); |
| 3506 |
if (err != 0) { |
| 3507 |
BLOSC_TRACE_ERROR("Unable to delete chunk!"); |
| 3508 |
return NULL; |
| 3509 |
} |
| 3510 |
} |
| 3511 |
// Update the offsets chunk in the chunks frame |
| 3512 |
fp = sframe_open_index(frame->urlpath, "rb+", frame->schunk->storage->io); |
| 3513 |
if (fp == NULL) { |
| 3514 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 3515 |
return NULL; |
| 3516 |
} |
| 3517 |
io_pos = frame->file_offset + header_len + 0; |
| 3518 |
} |
| 3519 |
else { |
| 3520 |
// Regular frame |
| 3521 |
fp = io_cb->open(frame->urlpath, "rb+", frame->schunk->storage->io); |
| 3522 |
if (fp == NULL) { |
| 3523 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 3524 |
return NULL; |
| 3525 |
} |
| 3526 |
io_pos = frame->file_offset + header_len + cbytes; |
| 3527 |
} |
| 3528 |
wbytes = io_cb->write(off_chunk, 1, new_off_cbytes, io_pos, fp); // the new offsets |
| 3529 |
io_cb->close(fp); |
| 3530 |
if (wbytes != (size_t)new_off_cbytes) { |
| 3531 |
BLOSC_TRACE_ERROR("Cannot write the offsets to frame."); |
| 3532 |
return NULL; |
| 3533 |
} |
| 3534 |
// Invalidate the cache for chunk offsets |
| 3535 |
if (frame->coffsets != NULL) { |
| 3536 |
if (frame->coffsets_needs_free) |
| 3537 |
free(frame->coffsets); |
| 3538 |
frame->coffsets = NULL; |
| 3539 |
} |
| 3540 |
} |
| 3541 |
free(off_chunk); |
| 3542 |
|
| 3543 |
frame->len = new_frame_len; |
| 3544 |
rc = frame_update_header(frame, schunk, false); |
| 3545 |
if (rc < 0) { |
| 3546 |
return NULL; |
| 3547 |
} |
| 3548 |
|
| 3549 |
rc = frame_update_trailer(frame, schunk); |
| 3550 |
if (rc < 0) { |
| 3551 |
return NULL; |
| 3552 |
} |
| 3553 |
|
| 3554 |
return frame; |
| 3555 |
} |
| 3556 |
|
| 3557 |
|
| 3558 |
int frame_reorder_offsets(blosc2_frame_s* frame, const int64_t* offsets_order, blosc2_schunk* schunk) { |
| 3559 |
// Get header info |
| 3560 |
int32_t header_len; |
| 3561 |
int64_t frame_len; |
| 3562 |
int64_t nbytes; |
| 3563 |
int64_t cbytes; |
| 3564 |
int32_t blocksize; |
| 3565 |
int32_t chunksize; |
| 3566 |
int64_t nchunks; |
| 3567 |
int ret = get_header_info(frame, &header_len, &frame_len, &nbytes, &cbytes, |
| 3568 |
&blocksize, &chunksize, &nchunks, |
| 3569 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 3570 |
frame->schunk->storage->io); |
| 3571 |
if (ret < 0) { |
| 3572 |
BLOSC_TRACE_ERROR("Cannot get the header info for the frame."); |
| 3573 |
return ret; |
| 3574 |
} |
| 3575 |
|
| 3576 |
// Get the current offsets and add one more |
| 3577 |
int32_t off_nbytes = (int32_t) (nchunks * sizeof(int64_t)); |
| 3578 |
int64_t* offsets = (int64_t *) malloc((size_t)off_nbytes); |
| 3579 |
|
| 3580 |
int32_t coffsets_cbytes = 0; |
| 3581 |
uint8_t *coffsets = get_coffsets(frame, header_len, cbytes, nchunks, &coffsets_cbytes); |
| 3582 |
if (coffsets == NULL) { |
| 3583 |
BLOSC_TRACE_ERROR("Cannot get the offsets for the frame."); |
| 3584 |
free(offsets); |
| 3585 |
return BLOSC2_ERROR_DATA; |
| 3586 |
} |
| 3587 |
|
| 3588 |
// Decompress offsets |
| 3589 |
blosc2_dparams off_dparams = BLOSC2_DPARAMS_DEFAULTS; |
| 3590 |
blosc2_context *dctx = blosc2_create_dctx(off_dparams); |
| 3591 |
if (dctx == NULL) { |
| 3592 |
BLOSC_TRACE_ERROR("Error while creating the decompression context"); |
| 3593 |
return BLOSC2_ERROR_NULL_POINTER; |
| 3594 |
} |
| 3595 |
int32_t prev_nbytes = blosc2_decompress_ctx(dctx, coffsets, coffsets_cbytes, |
| 3596 |
offsets, off_nbytes); |
| 3597 |
blosc2_free_ctx(dctx); |
| 3598 |
if (prev_nbytes < 0) { |
| 3599 |
free(offsets); |
| 3600 |
BLOSC_TRACE_ERROR("Cannot decompress the offsets chunk."); |
| 3601 |
return prev_nbytes; |
| 3602 |
} |
| 3603 |
|
| 3604 |
// Make a copy of the chunk offsets and reorder it |
| 3605 |
int64_t *offsets_copy = malloc(prev_nbytes); |
| 3606 |
memcpy(offsets_copy, offsets, prev_nbytes); |
| 3607 |
|
| 3608 |
for (int i = 0; i < nchunks; ++i) { |
| 3609 |
offsets[i] = offsets_copy[offsets_order[i]]; |
| 3610 |
} |
| 3611 |
free(offsets_copy); |
| 3612 |
|
| 3613 |
// Re-compress the offsets again |
| 3614 |
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS; |
| 3615 |
cparams.splitmode = BLOSC_NEVER_SPLIT; |
| 3616 |
cparams.typesize = sizeof(int64_t); |
| 3617 |
cparams.blocksize = 16 * 1024; // based on experiments with create_frame.c bench |
| 3618 |
cparams.nthreads = 4; // 4 threads seems a decent default for nowadays CPUs |
| 3619 |
cparams.compcode = BLOSC_BLOSCLZ; |
| 3620 |
blosc2_context* cctx = blosc2_create_cctx(cparams); |
| 3621 |
if (cctx == NULL) { |
| 3622 |
BLOSC_TRACE_ERROR("Error while creating the compression context"); |
| 3623 |
return BLOSC2_ERROR_NULL_POINTER; |
| 3624 |
} |
| 3625 |
void* off_chunk = malloc((size_t)off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 3626 |
int32_t new_off_cbytes = blosc2_compress_ctx(cctx, offsets, off_nbytes, |
| 3627 |
off_chunk, off_nbytes + BLOSC2_MAX_OVERHEAD); |
| 3628 |
blosc2_free_ctx(cctx); |
| 3629 |
|
| 3630 |
if (new_off_cbytes < 0) { |
| 3631 |
free(offsets); |
| 3632 |
free(off_chunk); |
| 3633 |
return new_off_cbytes; |
| 3634 |
} |
| 3635 |
free(offsets); |
| 3636 |
int64_t new_frame_len; |
| 3637 |
if (frame->sframe) { |
| 3638 |
// The chunks are not in the frame |
| 3639 |
new_frame_len = header_len + 0 + new_off_cbytes + frame->trailer_len; |
| 3640 |
} |
| 3641 |
else { |
| 3642 |
new_frame_len = header_len + cbytes + new_off_cbytes + frame->trailer_len; |
| 3643 |
} |
| 3644 |
|
| 3645 |
if (frame->cframe != NULL) { |
| 3646 |
uint8_t* framep = frame->cframe; |
| 3647 |
/* Make space for the new chunk and copy it */ |
| 3648 |
frame->cframe = framep = realloc(framep, (size_t)new_frame_len); |
| 3649 |
if (framep == NULL) { |
| 3650 |
BLOSC_TRACE_ERROR("Cannot realloc space for the frame."); |
| 3651 |
return BLOSC2_ERROR_MEMORY_ALLOC; |
| 3652 |
} |
| 3653 |
/* Copy the offsets */ |
| 3654 |
memcpy(framep + header_len + cbytes, off_chunk, (size_t)new_off_cbytes); |
| 3655 |
} |
| 3656 |
else { |
| 3657 |
void* fp = NULL; |
| 3658 |
|
| 3659 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 3660 |
if (io_cb == NULL) { |
| 3661 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 3662 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 3663 |
} |
| 3664 |
|
| 3665 |
int64_t io_pos = 0; |
| 3666 |
if (frame->sframe) { |
| 3667 |
// Update the offsets chunk in the chunks frame |
| 3668 |
fp = sframe_open_index(frame->urlpath, "rb+", |
| 3669 |
frame->schunk->storage->io); |
| 3670 |
if (fp == NULL) { |
| 3671 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 3672 |
return BLOSC2_ERROR_FILE_OPEN; |
| 3673 |
} |
| 3674 |
io_pos = frame->file_offset + header_len + 0; |
| 3675 |
} |
| 3676 |
else { |
| 3677 |
// Regular frame |
| 3678 |
fp = io_cb->open(frame->urlpath, "rb+", frame->schunk->storage->io->params); |
| 3679 |
if (fp == NULL) { |
| 3680 |
BLOSC_TRACE_ERROR("Error opening file in: %s", frame->urlpath); |
| 3681 |
return BLOSC2_ERROR_FILE_OPEN; |
| 3682 |
} |
| 3683 |
io_pos = frame->file_offset + header_len + cbytes; |
| 3684 |
} |
| 3685 |
int64_t wbytes = io_cb->write(off_chunk, 1, new_off_cbytes, io_pos, fp); // the new offsets |
| 3686 |
io_cb->close(fp); |
| 3687 |
if (wbytes != new_off_cbytes) { |
| 3688 |
BLOSC_TRACE_ERROR("Cannot write the offsets to frame."); |
| 3689 |
return BLOSC2_ERROR_FILE_WRITE; |
| 3690 |
} |
| 3691 |
} |
| 3692 |
|
| 3693 |
// Invalidate the cache for chunk offsets |
| 3694 |
if (frame->coffsets != NULL) { |
| 3695 |
if (frame->coffsets_needs_free) |
| 3696 |
free(frame->coffsets); |
| 3697 |
frame->coffsets = NULL; |
| 3698 |
} |
| 3699 |
free(off_chunk); |
| 3700 |
|
| 3701 |
frame->len = new_frame_len; |
| 3702 |
int rc = frame_update_header(frame, schunk, false); |
| 3703 |
if (rc < 0) { |
| 3704 |
return rc; |
| 3705 |
} |
| 3706 |
|
| 3707 |
rc = frame_update_trailer(frame, schunk); |
| 3708 |
if (rc < 0) { |
| 3709 |
return rc; |
| 3710 |
} |
| 3711 |
|
| 3712 |
return 0; |
| 3713 |
} |
| 3714 |
|
| 3715 |
|
| 3716 |
/* Decompress and return a chunk that is part of a frame. */ |
| 3717 |
int frame_decompress_chunk(blosc2_context *dctx, blosc2_frame_s* frame, int64_t nchunk, void *dest, int32_t nbytes) { |
| 3718 |
uint8_t* src; |
| 3719 |
bool needs_free; |
| 3720 |
int32_t chunk_nbytes; |
| 3721 |
int32_t chunk_cbytes; |
| 3722 |
int rc; |
| 3723 |
|
| 3724 |
// Use a lazychunk here in order to do a potential parallel read. |
| 3725 |
rc = frame_get_lazychunk(frame, nchunk, &src, &needs_free); |
| 3726 |
if (rc < 0) { |
| 3727 |
BLOSC_TRACE_ERROR("Cannot get the chunk in position %" PRId64 ".", nchunk); |
| 3728 |
goto end; |
| 3729 |
} |
| 3730 |
chunk_cbytes = rc; |
| 3731 |
if (chunk_cbytes < (signed)sizeof(int32_t)) { |
| 3732 |
/* Not enough input to read `nbytes` */ |
| 3733 |
rc = BLOSC2_ERROR_READ_BUFFER; |
| 3734 |
} |
| 3735 |
|
| 3736 |
rc = blosc2_cbuffer_sizes(src, &chunk_nbytes, &chunk_cbytes, NULL); |
| 3737 |
if (rc < 0) { |
| 3738 |
goto end; |
| 3739 |
} |
| 3740 |
|
| 3741 |
/* Create a buffer for destination */ |
| 3742 |
if (chunk_nbytes > nbytes) { |
| 3743 |
BLOSC_TRACE_ERROR("Not enough space for decompressing in dest."); |
| 3744 |
rc = BLOSC2_ERROR_WRITE_BUFFER; |
| 3745 |
goto end; |
| 3746 |
} |
| 3747 |
/* And decompress it */ |
| 3748 |
dctx->header_overhead = BLOSC_EXTENDED_HEADER_LENGTH; |
| 3749 |
int chunksize = rc = blosc2_decompress_ctx(dctx, src, chunk_cbytes, dest, nbytes); |
| 3750 |
if (chunksize < 0 || chunksize != chunk_nbytes) { |
| 3751 |
BLOSC_TRACE_ERROR("Error in decompressing chunk."); |
| 3752 |
if (chunksize >= 0) |
| 3753 |
rc = BLOSC2_ERROR_FAILURE; |
| 3754 |
} |
| 3755 |
end: |
| 3756 |
if (needs_free) { |
| 3757 |
free(src); |
| 3758 |
} |
| 3759 |
return rc; |
| 3760 |
} |