1 /*********************************************************************
2 Blosc - Blocked Shuffling and Compression Library
3
4 Copyright (C) 2021 The Blosc Developers <blosc@blosc.org>
5 https://blosc.org
6 License: BSD 3-Clause (see LICENSE.txt)
7
8 See LICENSE.txt for details about copyright and rights to use.
9 **********************************************************************/
10
11
12
13 #ifndef NDLZ_PRIVATE_H
14 #define NDLZ_PRIVATE_H
15 #include "context.h"
16
17 #if defined (__cplusplus)
18 extern "C" {
19 #endif
20 #define XXH_INLINE_ALL
21
22 #define NDLZ_VERSION_STRING "1.0.0"
23
24 #define NDLZ_ERROR_NULL(pointer) \
25 do { \
26 if ((pointer) == NULL) { \
27 return 0; \
28 } \
29 } while (0)
30
31
swap_store(void * dest,const void * pa,int size)32 static void swap_store(void *dest, const void *pa, int size) {
33 uint8_t *pa_ = (uint8_t *) pa;
34 uint8_t pa2_[8];
35 int i = 1; /* for big/little endian detection */
36 char *p = (char *) &i;
37
38 if (p[0] == 1) {
39 /* little endian */
40 switch (size) {
41 case 8:
42 pa2_[0] = pa_[7];
43 pa2_[1] = pa_[6];
44 pa2_[2] = pa_[5];
45 pa2_[3] = pa_[4];
46 pa2_[4] = pa_[3];
47 pa2_[5] = pa_[2];
48 pa2_[6] = pa_[1];
49 pa2_[7] = pa_[0];
50 break;
51 case 4:
52 pa2_[0] = pa_[3];
53 pa2_[1] = pa_[2];
54 pa2_[2] = pa_[1];
55 pa2_[3] = pa_[0];
56 break;
57 case 2:
58 pa2_[0] = pa_[1];
59 pa2_[1] = pa_[0];
60 break;
61 case 1:
62 pa2_[0] = pa_[0];
63 break;
64 default:
65 fprintf(stderr, "Unhandled nitems: %d\n", size);
66 }
67 }
68 memcpy(dest, pa2_, size);
69 }
70
deserialize_meta(uint8_t * smeta,uint32_t smeta_len,int8_t * ndim,int64_t * shape,int32_t * chunkshape,int32_t * blockshape)71 static int32_t deserialize_meta(uint8_t *smeta, uint32_t smeta_len, int8_t *ndim, int64_t *shape,
72 int32_t *chunkshape, int32_t *blockshape) {
73 uint8_t *pmeta = smeta;
74
75 // Check that we have an array with 5 entries (version, ndim, shape, chunkshape, blockshape)
76 pmeta += 1;
77
78 // version entry
79 int8_t version = pmeta[0]; // positive fixnum (7-bit positive integer)
80 pmeta += 1;
81
82 // ndim entry
83 *ndim = pmeta[0];
84 int8_t ndim_aux = *ndim; // positive fixnum (7-bit positive integer)
85 pmeta += 1;
86
87 // shape entry
88 // Initialize to ones, as required by Caterva
89 for (int i = 0; i < 8; i++) shape[i] = 1;
90 pmeta += 1;
91 for (int8_t i = 0; i < ndim_aux; i++) {
92 pmeta += 1;
93 swap_store(shape + i, pmeta, sizeof(int64_t));
94 pmeta += sizeof(int64_t);
95 }
96
97 // chunkshape entry
98 // Initialize to ones, as required by Caterva
99 for (int i = 0; i < 8; i++) chunkshape[i] = 1;
100 pmeta += 1;
101 for (int8_t i = 0; i < ndim_aux; i++) {
102 pmeta += 1;
103 swap_store(chunkshape + i, pmeta, sizeof(int32_t));
104 pmeta += sizeof(int32_t);
105 }
106
107 // blockshape entry
108 // Initialize to ones, as required by Caterva
109 for (int i = 0; i < 8; i++) blockshape[i] = 1;
110 pmeta += 1;
111 for (int8_t i = 0; i < ndim_aux; i++) {
112 pmeta += 1;
113 swap_store(blockshape + i, pmeta, sizeof(int32_t));
114 pmeta += sizeof(int32_t);
115 }
116 uint32_t slen = (uint32_t)(pmeta - smeta);
117 return 0;
118 }
119
120 #if defined (__cplusplus)
121 }
122 #endif
123
124 #endif /* NDLZ_PRIVATE_H */
125