Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / third_party / om-file-format-sys / c / src / om_variable.c

om_variable.c470 lines · 17.1 KiBRaw
1//
2// om_variable.c
3// OpenMeteoApi
4//
5// Created by Patrick Zippenfenig on 16.11.2024.
6//
7
8#include "om_variable.h"
9
10const OmVariable_t* om_variable_init(const void* src) {
11 return src;
12}
13
14const char* om_variable_get_name(const OmVariable_t* variable, uint16_t* length) {
15 switch (_om_variable_memory_layout(variable)) {
16 case OM_MEMORY_LAYOUT_LEGACY: {
17 // Legacy files do not have a name field
18 *length = 0;
19 return NULL;
20 }
21 case OM_MEMORY_LAYOUT_ARRAY: {
22 // 'Name' is after dimension arrays
23 const OmVariableArrayV3_t* meta = (const OmVariableArrayV3_t*)variable;
24 const char* name = (const char *)variable + sizeof(OmVariableArrayV3_t) + 16 * meta->children_count + 16 * meta->dimension_count;
25 *length = meta->name_size;
26 return name;
27 }
28 case OM_MEMORY_LAYOUT_SCALAR: {
29 // 'Name' is after the scalar value
30 const OmVariableV3_t* meta = (const OmVariableV3_t*)variable;
31 const char* base = (const char *)variable + sizeof(OmVariableV3_t) + 16 * meta->children_count;
32 switch (meta->data_type) {
33 case DATA_TYPE_NONE:
34 *length = meta->name_size;
35 return base;
36 case DATA_TYPE_INT8:
37 case DATA_TYPE_UINT8:
38 *length = meta->name_size;
39 return base+1;
40 case DATA_TYPE_INT16:
41 case DATA_TYPE_UINT16:
42 *length = meta->name_size;
43 return base+2;
44 case DATA_TYPE_INT32:
45 case DATA_TYPE_UINT32:
46 case DATA_TYPE_FLOAT:
47 *length = meta->name_size;
48 return base+4;
49 case DATA_TYPE_INT64:
50 case DATA_TYPE_UINT64:
51 case DATA_TYPE_DOUBLE:
52 *length = meta->name_size;
53 return base+8;
54 case DATA_TYPE_STRING: {
55 // String format: uint64_t string_size + string data
56 uint64_t string_size = *(uint64_t*)base;
57 const char* name_ptr = base + sizeof(uint64_t) + string_size;
58 *length = meta->name_size;
59 return name_ptr;
60 }
61 default:
62 *length = 0;
63 return NULL;
64 }
65 }
66 }
67}
68
69OmDataType_t om_variable_get_type(const OmVariable_t* variable) {
70 switch (_om_variable_memory_layout(variable)) {
71 case OM_MEMORY_LAYOUT_LEGACY:
72 return DATA_TYPE_FLOAT_ARRAY;
73 case OM_MEMORY_LAYOUT_ARRAY:
74 case OM_MEMORY_LAYOUT_SCALAR: {
75 const OmVariableV3_t* meta = (const OmVariableV3_t*)variable;
76 return meta->data_type;
77 }
78 }
79}
80
81OmCompression_t om_variable_get_compression(const OmVariable_t* variable) {
82 switch (_om_variable_memory_layout(variable)) {
83 case OM_MEMORY_LAYOUT_LEGACY: {
84 const OmHeaderV1_t* meta = (const OmHeaderV1_t*)variable;
85 if (meta->version == 1) {
86 return COMPRESSION_PFOR_DELTA2D_INT16;
87 }
88 return meta->compression_type;
89 }
90 case OM_MEMORY_LAYOUT_ARRAY:
91 case OM_MEMORY_LAYOUT_SCALAR: {
92 const OmVariableV3_t* meta = (const OmVariableV3_t*)variable;
93 return meta->compression_type;
94 }
95 }
96}
97
98OmMemoryLayout_t _om_variable_memory_layout(const OmVariable_t* variable) {
99 const OmHeaderV3_t* meta = (const OmHeaderV3_t*)variable;
100 bool isLegacy = meta->magic_number1 == 'O' && meta->magic_number2 == 'M' && (meta->version == 1 || meta->version == 2);
101 if (isLegacy) {
102 return OM_MEMORY_LAYOUT_LEGACY;
103 }
104 const OmVariableV3_t* var = (const OmVariableV3_t*)variable;
105 bool isArray = var->data_type >= DATA_TYPE_INT8_ARRAY && var->data_type <= DATA_TYPE_DOUBLE_ARRAY;
106 return isArray ? OM_MEMORY_LAYOUT_ARRAY : OM_MEMORY_LAYOUT_SCALAR;
107}
108
109float om_variable_get_scale_factor(const OmVariable_t* variable) {
110 switch (_om_variable_memory_layout(variable)) {
111 case OM_MEMORY_LAYOUT_LEGACY:
112 return ((OmHeaderV1_t*)variable)->scale_factor;
113 case OM_MEMORY_LAYOUT_ARRAY:
114 return ((OmVariableArrayV3_t*)variable)->scale_factor;
115 case OM_MEMORY_LAYOUT_SCALAR:
116 return 1;
117 }
118}
119
120float om_variable_get_add_offset(const OmVariable_t* variable) {
121 switch (_om_variable_memory_layout(variable)) {
122 case OM_MEMORY_LAYOUT_LEGACY:
123 return 0;
124 case OM_MEMORY_LAYOUT_ARRAY:
125 return ((OmVariableArrayV3_t*)variable)->add_offset;
126 case OM_MEMORY_LAYOUT_SCALAR:
127 return 0;
128 }
129}
130
131uint64_t om_variable_get_dimensions_count(const OmVariable_t* variable) {
132 switch (_om_variable_memory_layout(variable)) {
133 case OM_MEMORY_LAYOUT_LEGACY: {
134 return 2;
135 }
136 case OM_MEMORY_LAYOUT_ARRAY: {
137 const OmVariableArrayV3_t* meta = (const OmVariableArrayV3_t*)variable;
138 return meta->dimension_count;
139 }
140 case OM_MEMORY_LAYOUT_SCALAR:
141 return 0;
142 }
143}
144
145const uint64_t* om_variable_get_dimensions(const OmVariable_t* variable) {
146 switch (_om_variable_memory_layout(variable)) {
147 case OM_MEMORY_LAYOUT_LEGACY: {
148 const OmHeaderV1_t* meta = (const OmHeaderV1_t*)variable;
149 return &meta->dim0;
150 }
151 case OM_MEMORY_LAYOUT_ARRAY: {
152 const OmVariableArrayV3_t* meta = (const OmVariableArrayV3_t*)variable;
153 return (const uint64_t *)(
154 (const char *)variable + sizeof(OmVariableArrayV3_t) + 16 * meta->children_count
155 );
156 }
157 case OM_MEMORY_LAYOUT_SCALAR:
158 return NULL;
159 }
160}
161
162const uint64_t* om_variable_get_chunks(const OmVariable_t* variable) {
163 switch (_om_variable_memory_layout(variable)) {
164 case OM_MEMORY_LAYOUT_LEGACY: {
165 const OmHeaderV1_t* meta = (const OmHeaderV1_t*)variable;
166 return &meta->chunk0;
167 }
168 case OM_MEMORY_LAYOUT_ARRAY: {
169 const OmVariableArrayV3_t* meta = (const OmVariableArrayV3_t*)variable;
170 return (const uint64_t *)(
171 (const char *)variable + sizeof(OmVariableArrayV3_t) + 16 * meta->children_count + 8 * meta->dimension_count
172 );
173 }
174 case OM_MEMORY_LAYOUT_SCALAR:
175 return NULL;
176 }
177}
178
179uint32_t om_variable_get_children_count(const OmVariable_t* variable) {
180 switch (_om_variable_memory_layout(variable)) {
181 case OM_MEMORY_LAYOUT_LEGACY:
182 return 0;
183 case OM_MEMORY_LAYOUT_ARRAY:
184 case OM_MEMORY_LAYOUT_SCALAR:
185 return ((OmVariableV3_t*)variable)->children_count;
186 }
187}
188
189bool om_variable_get_children(const OmVariable_t* variable, uint32_t child_offset, uint32_t child_count, uint64_t* child_offsets, uint64_t* child_sizes) {
190 uint64_t sizeof_variable;
191 switch (_om_variable_memory_layout(variable)) {
192 case OM_MEMORY_LAYOUT_LEGACY:
193 return false;
194 case OM_MEMORY_LAYOUT_ARRAY:
195 sizeof_variable = sizeof(OmVariableArrayV3_t);
196 break;
197 case OM_MEMORY_LAYOUT_SCALAR:
198 sizeof_variable = sizeof(OmVariableV3_t);
199 break;
200 }
201 const OmVariableV3_t* meta = (const OmVariableV3_t*)variable;
202 if (child_offset + child_count > meta->children_count) {
203 return false;
204 }
205 const uint64_t* sizes = (const uint64_t*)((const uint8_t *)variable + sizeof_variable);
206 const uint64_t* offsets = (const uint64_t*)((const uint8_t *)variable + sizeof_variable + meta->children_count * sizeof(uint64_t));
207 for (size_t n = 0; n < child_count; n++) {
208 child_offsets[n] = offsets[n+child_offset];
209 child_sizes[n] = sizes[n+child_offset];
210 }
211 return true;
212}
213
214OmError_t om_variable_get_scalar(const OmVariable_t* variable, void** value, uint64_t* size) {
215 if (_om_variable_memory_layout(variable) != OM_MEMORY_LAYOUT_SCALAR) {
216 return ERROR_INVALID_DATA_TYPE;
217 }
218
219 const OmVariableV3_t* meta = (const OmVariableV3_t*)variable;
220 const void* src = (const void*)((char *)variable + sizeof(OmVariableV3_t) + 16 * meta->children_count);
221 switch (meta->data_type) {
222 case DATA_TYPE_INT8:
223 case DATA_TYPE_UINT8:
224 *value = (void *)src;
225 *size = 1;
226 return ERROR_OK;
227 case DATA_TYPE_INT16:
228 case DATA_TYPE_UINT16:
229 *value = (void *)src;
230 *size = 2;
231 return ERROR_OK;
232 case DATA_TYPE_INT32:
233 case DATA_TYPE_UINT32:
234 case DATA_TYPE_FLOAT:
235 *value = (void *)src;
236 *size = 4;
237 return ERROR_OK;
238 case DATA_TYPE_INT64:
239 case DATA_TYPE_UINT64:
240 case DATA_TYPE_DOUBLE:
241 *value = (void *)src;
242 *size = 8;
243 return ERROR_OK;
244 case DATA_TYPE_STRING:
245 *value = (void *)((const char*)src + sizeof(uint64_t));
246 *size = *(uint64_t*)src;
247 return ERROR_OK;
248 default:
249 return ERROR_INVALID_DATA_TYPE;
250 }
251}
252
253size_t om_variable_write_scalar_size(uint16_t name_size, uint32_t children_count, OmDataType_t data_type, uint64_t string_size) {
254 size_t base = sizeof(OmVariableV3_t) + name_size + children_count * 16;
255 switch (data_type) {
256 case DATA_TYPE_NONE:
257 return base;
258 case DATA_TYPE_INT8:
259 case DATA_TYPE_UINT8:
260 return base + 1;
261 case DATA_TYPE_INT16:
262 case DATA_TYPE_UINT16:
263 return base + 2;
264 case DATA_TYPE_INT32:
265 case DATA_TYPE_UINT32:
266 case DATA_TYPE_FLOAT:
267 return base + 4;
268 case DATA_TYPE_INT64:
269 case DATA_TYPE_UINT64:
270 case DATA_TYPE_DOUBLE:
271 return base + 8;
272 case DATA_TYPE_STRING:
273 // String format: uint64_t string_size + string data
274 return base + sizeof(uint64_t) + string_size;
275 default:
276 return 0;
277 }
278}
279
280void _om_variable_write_children(uint8_t *dst, uint32_t children_count, const uint64_t* children_offsets, const uint64_t* children_sizes) {
281 uint64_t* sizes = (uint64_t*)(dst);
282 uint64_t* offsets = (uint64_t*)(dst + children_count * sizeof(uint64_t));
283
284 for (uint32_t i = 0; i<children_count; i++) {
285 sizes[i] = children_sizes[i];
286 offsets[i] = children_offsets[i];
287 }
288}
289
290
291void om_variable_write_scalar(
292 void* dst,
293 uint16_t name_size,
294 uint32_t children_count,
295 const uint64_t* children_offsets,
296 const uint64_t* children_sizes,
297 const char* name,
298 OmDataType_t data_type,
299 const void* value,
300 size_t string_size
301) {
302 *(OmVariableV3_t*)dst = (OmVariableV3_t){
303 .data_type = (uint8_t)data_type,
304 .compression_type = COMPRESSION_NONE,
305 .name_size = name_size,
306 .children_count = children_count
307 };
308
309 uint8_t *dst_ptr = (uint8_t *)dst;
310
311 /// Set children
312 _om_variable_write_children(dst_ptr + sizeof(OmVariableV3_t), children_count, children_offsets, children_sizes);
313
314 /// Set value
315 char* destValue = (char*)(dst_ptr + sizeof(OmVariableV3_t) + 16 * children_count);
316 uint64_t valueSize = 0;
317 switch (data_type) {
318 case DATA_TYPE_NONE:
319 // No value to write for DATA_TYPE_NONE
320 valueSize = 0;
321 break;
322 case DATA_TYPE_INT8:
323 case DATA_TYPE_UINT8:
324 *(int8_t *)destValue = *(int8_t*)value;
325 valueSize = 1;
326 break;
327 case DATA_TYPE_INT16:
328 case DATA_TYPE_UINT16:
329 *(int16_t *)destValue = *(int16_t*)value;
330 valueSize = 2;
331 break;
332 case DATA_TYPE_INT32:
333 case DATA_TYPE_UINT32:
334 case DATA_TYPE_FLOAT: {
335 int32_t v = *(int32_t*)value;
336 *(int32_t *)destValue = v;
337 valueSize = 4;
338 break;
339 }
340 case DATA_TYPE_INT64:
341 case DATA_TYPE_UINT64:
342 case DATA_TYPE_DOUBLE:
343 *(int64_t *)destValue = *(int64_t*)value;
344 valueSize = 8;
345 break;
346 case DATA_TYPE_STRING: {
347 const char* string = (const char*)value;
348
349 // String format: uint64_t string_size + string data
350 *(uint64_t*)destValue = string_size; // write string length to the first 64bits
351
352 char* destString = destValue + sizeof(uint64_t);
353 for (uint64_t i = 0; i < string_size; i++) {
354 destString[i] = string[i];
355 }
356
357 valueSize = sizeof(uint64_t) + string_size;
358 break;
359 }
360 default:
361 break;
362 }
363
364 /// Set name
365 char* destName = destValue + valueSize;
366 for (uint16_t i = 0; i<name_size; i++) {
367 destName[i] = name[i];
368 }
369}
370
371size_t om_variable_write_numeric_array_size(uint16_t name_size, uint32_t children_count, uint64_t dimension_count) {
372 return sizeof(OmVariableArrayV3_t) + name_size + children_count * 16 + dimension_count * 16;
373}
374
375void om_variable_write_numeric_array(void* dst, uint16_t name_size, uint32_t children_count, const uint64_t* children_offsets, const uint64_t* children_sizes, const char* name, OmDataType_t data_type, OmCompression_t compression_type, float scale_factor, float add_offset, uint64_t dimension_count, const uint64_t *dimensions, const uint64_t *chunks, uint64_t lut_size, uint64_t lut_offset) {
376
377 *(OmVariableArrayV3_t*)dst = (OmVariableArrayV3_t){
378 .data_type = (uint8_t)data_type,
379 .compression_type = (uint8_t)compression_type,
380 .name_size = name_size,
381 .children_count = children_count,
382 .add_offset = add_offset,
383 .scale_factor = scale_factor,
384 .dimension_count = dimension_count,
385 .lut_size = lut_size,
386 .lut_offset = lut_offset
387 };
388
389 uint8_t *dst_ptr = (uint8_t *)dst;
390
391 /// Set children
392 _om_variable_write_children(dst_ptr + sizeof(OmVariableArrayV3_t), children_count, children_offsets, children_sizes);
393
394 /// Set dimensions
395 uint64_t* baseDimensions = (uint64_t*)(dst_ptr + sizeof(OmVariableArrayV3_t) + 16 * children_count);
396 uint64_t* baseChunks = (uint64_t*)(dst_ptr + sizeof(OmVariableArrayV3_t) + 16 * children_count + 8 * dimension_count);
397 for (uint64_t i = 0; i<dimension_count; i++) {
398 baseDimensions[i] = dimensions[i];
399 baseChunks[i] = chunks[i];
400 }
401 /// Set name
402 char* baseName = (char*)(dst_ptr + sizeof(OmVariableArrayV3_t) + 16 * children_count + 16 * dimension_count);
403 for (uint16_t i = 0; i<name_size; i++) {
404 baseName[i] = name[i];
405 }
406}
407
408OmError_t om_variable_validate(const void* src, uint64_t buffer_size) {
409 if (buffer_size < sizeof(OmHeaderV3_t)) {
410 return ERROR_NOT_AN_OM_FILE;
411 }
412 const OmVariable_t* variable = src;
413 switch (_om_variable_memory_layout(variable)) {
414 case OM_MEMORY_LAYOUT_LEGACY: {
415 if (buffer_size < sizeof(OmHeaderV1_t)) {
416 return ERROR_OUT_OF_BOUND_READ;
417 }
418 const OmHeaderV1_t* meta = (const OmHeaderV1_t*)variable;
419 if (meta->dim0 == 0 || meta->dim1 == 0) {
420 return ERROR_INVALID_DIMENSIONS;
421 }
422 if (meta->chunk0 == 0 || meta->chunk0 > meta->dim0) {
423 return ERROR_INVALID_CHUNK_DIMENSIONS;
424 }
425 if (meta->chunk1 == 0 || meta->chunk1 > meta->dim1) {
426 return ERROR_INVALID_CHUNK_DIMENSIONS;
427 }
428 return ERROR_OK;
429 }
430 case OM_MEMORY_LAYOUT_ARRAY: {
431 if (buffer_size < sizeof(OmVariableArrayV3_t)) {
432 return ERROR_OUT_OF_BOUND_READ;
433 }
434 const OmVariableArrayV3_t* meta = (const OmVariableArrayV3_t*)variable;
435 size_t expected = om_variable_write_numeric_array_size(
436 meta->name_size, meta->children_count, meta->dimension_count
437 );
438 if ((uint64_t)expected > buffer_size) {
439 return ERROR_OUT_OF_BOUND_READ;
440 }
441 if (meta->dimension_count == 0) {
442 return ERROR_INVALID_DIMENSIONS;
443 }
444 return ERROR_OK;
445 }
446 case OM_MEMORY_LAYOUT_SCALAR: {
447 if (buffer_size < sizeof(OmVariableV3_t)) {
448 return ERROR_OUT_OF_BOUND_READ;
449 }
450 const OmVariableV3_t* meta = (const OmVariableV3_t*)variable;
451 // For string type, read the string_size from the buffer if possible
452 uint64_t string_size = 0;
453 if (meta->data_type == DATA_TYPE_STRING) {
454 uint64_t string_size_offset = sizeof(OmVariableV3_t) + 16 * (uint64_t)meta->children_count;
455 if (string_size_offset + sizeof(uint64_t) > buffer_size) {
456 return ERROR_OUT_OF_BOUND_READ;
457 }
458 string_size = *(const uint64_t*)((const char*)variable + string_size_offset);
459 }
460 size_t expected = om_variable_write_scalar_size(
461 meta->name_size, meta->children_count, meta->data_type, string_size
462 );
463 if ((uint64_t)expected > buffer_size) {
464 return ERROR_OUT_OF_BOUND_READ;
465 }
466 return ERROR_OK;
467 }
468 }
469 return ERROR_NOT_AN_OM_FILE;
470}