| 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 |
#ifndef BLOSC_FRAME_H |
| 12 |
#define BLOSC_FRAME_H |
| 13 |
|
| 14 |
#include "blosc2.h" |
| 15 |
|
| 16 |
#include <stdbool.h> |
| 17 |
#include <stdint.h> |
| 18 |
|
| 19 |
// Different types of frames |
| 20 |
#define FRAME_CONTIGUOUS_TYPE 0 |
| 21 |
#define FRAME_DIRECTORY_TYPE 1 |
| 22 |
|
| 23 |
|
| 24 |
// Constants for metadata placement in header |
| 25 |
#define FRAME_HEADER_MAGIC 2 |
| 26 |
#define FRAME_HEADER_LEN (FRAME_HEADER_MAGIC + 8 + 1) // 11 |
| 27 |
#define FRAME_LEN (FRAME_HEADER_LEN + 4 + 1) // 16 |
| 28 |
#define FRAME_FLAGS (FRAME_LEN + 8 + 1) // 25 |
| 29 |
#define FRAME_TYPE (FRAME_FLAGS + 1) // 26 |
| 30 |
#define FRAME_CODECS (FRAME_FLAGS + 2) // 27 |
| 31 |
#define FRAME_OTHER_FLAGS (FRAME_FLAGS + 3) // 28 |
| 32 |
#define FRAME_NBYTES (FRAME_FLAGS + 4 + 1) // 30 |
| 33 |
#define FRAME_CBYTES (FRAME_NBYTES + 8 + 1) // 39 |
| 34 |
#define FRAME_TYPESIZE (FRAME_CBYTES + 8 + 1) // 48 |
| 35 |
#define FRAME_BLOCKSIZE (FRAME_TYPESIZE + 4 + 1) // 53 |
| 36 |
#define FRAME_CHUNKSIZE (FRAME_BLOCKSIZE + 4 + 1) // 58 |
| 37 |
#define FRAME_NTHREADS_C (FRAME_CHUNKSIZE + 4 + 1) // 63 |
| 38 |
#define FRAME_NTHREADS_D (FRAME_NTHREADS_C + 2 + 1) // 66 |
| 39 |
#define FRAME_HAS_VLMETALAYERS (FRAME_NTHREADS_D + 2) // 68 |
| 40 |
#define FRAME_FILTER_PIPELINE (FRAME_HAS_VLMETALAYERS + 1 + 1) // 70 |
| 41 |
#define FRAME_UDCODEC (FRAME_FILTER_PIPELINE + 1 + 6) // 77 |
| 42 |
#define FRAME_CODEC_META (FRAME_FILTER_PIPELINE + 1 + 7) // 78 |
| 43 |
#define FRAME_HEADER_MINLEN (FRAME_FILTER_PIPELINE + 1 + 16) // 87 <- minimum length |
| 44 |
#define FRAME_METALAYERS (FRAME_HEADER_MINLEN) // 87 |
| 45 |
#define FRAME_IDX_SIZE (FRAME_METALAYERS + 1 + 1) // 89 |
| 46 |
|
| 47 |
#define FRAME_FILTER_PIPELINE_MAX (8) // the maximum number of filters that can be stored in header |
| 48 |
|
| 49 |
#define FRAME_TRAILER_VERSION_BETA2 (0U) // for beta.2 and former |
| 50 |
#define FRAME_TRAILER_VERSION (1U) // can be up to 127 |
| 51 |
|
| 52 |
#define FRAME_TRAILER_MINLEN (25) // minimum length for the trailer (msgpack overhead) |
| 53 |
#define FRAME_TRAILER_LEN_OFFSET (22) // offset to trailer length (counting from the end) |
| 54 |
#define FRAME_TRAILER_VLMETALAYERS (2) |
| 55 |
|
| 56 |
|
| 57 |
typedef struct { |
| 58 |
char* urlpath; //!< The name of the file or directory if it's an sframe; if NULL, this is in-memory |
| 59 |
uint8_t* cframe; //!< The in-memory, contiguous frame buffer |
| 60 |
bool avoid_cframe_free; //!< Whether the cframe can be freed (false) or not (true). |
| 61 |
uint8_t* coffsets; //!< Pointers to the (compressed, on-disk) chunk offsets |
| 62 |
bool coffsets_needs_free; //!< Whether the coffsets memory need to be freed or not. |
| 63 |
int64_t len; //!< The current length of the frame in (compressed) bytes |
| 64 |
int64_t maxlen; //!< The maximum length of the frame; if 0, there is no maximum |
| 65 |
uint32_t trailer_len; //!< The current length of the trailer in (compressed) bytes |
| 66 |
bool sframe; //!< Whether the frame is sparse (true) or not |
| 67 |
blosc2_schunk *schunk; //!< The schunk associated |
| 68 |
int64_t file_offset; //!< The offset where the frame starts inside the file |
| 69 |
} blosc2_frame_s; |
| 70 |
|
| 71 |
|
| 72 |
/********************************************************************* |
| 73 |
Frame struct related functions. |
| 74 |
These are rather low-level and the blosc2_schunk interface is |
| 75 |
recommended instead. |
| 76 |
*********************************************************************/ |
| 77 |
|
| 78 |
/** |
| 79 |
* @brief Create a new frame. |
| 80 |
* |
| 81 |
* @param urlpath The filename of the frame. If not persistent, pass NULL. |
| 82 |
* |
| 83 |
* @return The new frame. |
| 84 |
*/ |
| 85 |
blosc2_frame_s* frame_new(const char* urlpath); |
| 86 |
|
| 87 |
/** |
| 88 |
* @brief Create a frame from a super-chunk. |
| 89 |
* |
| 90 |
* @param schunk The super-chunk from where the frame will be created. |
| 91 |
* @param frame The pointer for the frame that will be populated. |
| 92 |
* |
| 93 |
* @note If frame->urlpath is NULL, a frame is created in-memory; else it is created |
| 94 |
* on-disk. |
| 95 |
* |
| 96 |
* @return The size in bytes of the frame. If an error occurs it returns a negative value. |
| 97 |
*/ |
| 98 |
int64_t frame_from_schunk(blosc2_schunk* schunk, blosc2_frame_s* frame); |
| 99 |
|
| 100 |
/** |
| 101 |
* @brief Set `avoid_cframe_free` from @param frame to @param avoid_cframe_free. |
| 102 |
* |
| 103 |
* @param frame The frame to set the property to. |
| 104 |
* @param avoid_cframe_free The value to set in @param frame. |
| 105 |
* @warning If you set it to `true` you will be responsible of freeing it. |
| 106 |
*/ |
| 107 |
void frame_avoid_cframe_free(blosc2_frame_s* frame, bool avoid_cframe_free); |
| 108 |
|
| 109 |
/** |
| 110 |
* @brief Free all memory from a frame. |
| 111 |
* |
| 112 |
* @param frame The frame to be freed. |
| 113 |
* |
| 114 |
* @return 0 if succeeds. |
| 115 |
*/ |
| 116 |
int frame_free(blosc2_frame_s *frame); |
| 117 |
|
| 118 |
/** |
| 119 |
* @brief Initialize a frame out of a file. |
| 120 |
* |
| 121 |
* @param urlpath The file name. |
| 122 |
* |
| 123 |
* @return The frame created from the file. |
| 124 |
*/ |
| 125 |
blosc2_frame_s* frame_from_file_offset(const char *urlpath, const blosc2_io *io_cb, int64_t offset); |
| 126 |
|
| 127 |
/** |
| 128 |
* @brief Initialize a frame out of a contiguous frame buffer. |
| 129 |
* |
| 130 |
* @param cframe The buffer for the frame. |
| 131 |
* @param len The length of buffer for the frame. |
| 132 |
* @param copy Whether the frame buffer should be copied internally or not. |
| 133 |
* |
| 134 |
* @return The frame created from the contiguous frame buffer. |
| 135 |
* |
| 136 |
* @note The user is responsible to `free` the returned frame. |
| 137 |
*/ |
| 138 |
blosc2_frame_s* frame_from_cframe(uint8_t *cframe, int64_t len, bool copy); |
| 139 |
|
| 140 |
/** |
| 141 |
* @brief Create a super-chunk from a frame. |
| 142 |
* |
| 143 |
* @param frame The frame from which the super-chunk will be created. |
| 144 |
* @param copy If true, a new frame buffer is created |
| 145 |
* internally to serve as storage for the super-chunk. Else, the |
| 146 |
* super-chunk will be backed by @p frame (i.e. no copies are made). |
| 147 |
* |
| 148 |
* @return The super-chunk corresponding to the frame. |
| 149 |
*/ |
| 150 |
blosc2_schunk* frame_to_schunk(blosc2_frame_s* frame, bool copy, const blosc2_io *udio); |
| 151 |
|
| 152 |
blosc2_storage * |
| 153 |
get_new_storage(const blosc2_storage *storage, const blosc2_cparams *cdefaults, const blosc2_dparams *ddefaults, |
| 154 |
const blosc2_io *iodefaults); |
| 155 |
|
| 156 |
void* frame_append_chunk(blosc2_frame_s* frame, void* chunk, blosc2_schunk* schunk); |
| 157 |
void* frame_insert_chunk(blosc2_frame_s* frame, int64_t nchunk, void* chunk, blosc2_schunk* schunk); |
| 158 |
void* frame_update_chunk(blosc2_frame_s* frame, int64_t nchunk, void* chunk, blosc2_schunk* schunk); |
| 159 |
void* frame_delete_chunk(blosc2_frame_s* frame, int64_t nchunk, blosc2_schunk* schunk); |
| 160 |
int frame_reorder_offsets(blosc2_frame_s *frame, const int64_t *offsets_order, blosc2_schunk* schunk); |
| 161 |
|
| 162 |
int frame_get_chunk(blosc2_frame_s* frame, int64_t nchunk, uint8_t **chunk, bool *needs_free); |
| 163 |
int frame_get_lazychunk(blosc2_frame_s* frame, int64_t nchunk, uint8_t **chunk, bool *needs_free); |
| 164 |
int frame_decompress_chunk(blosc2_context* dctx, blosc2_frame_s* frame, int64_t nchunk, |
| 165 |
void *dest, int32_t nbytes); |
| 166 |
|
| 167 |
int frame_update_header(blosc2_frame_s* frame, blosc2_schunk* schunk, bool new); |
| 168 |
int frame_update_trailer(blosc2_frame_s* frame, blosc2_schunk* schunk); |
| 169 |
|
| 170 |
int64_t frame_fill_special(blosc2_frame_s* frame, int64_t nitems, int special_value, |
| 171 |
int32_t chunksize, blosc2_schunk* schunk); |
| 172 |
|
| 173 |
#endif /* BLOSC_FRAME_H */ |