libose
Loading...
Searching...
No Matches
ose_test_ctosc.h
Go to the documentation of this file.
1
17/* zero or more zeros for padding */
18#define Z0
19#define Z1 0
20#define Z2 0, 0
21#define Z3 0, 0, 0
22#define Z4 0, 0, 0, 0
23
24/* unpack a string into a comma separated list of chars */
25#define STOC1(s) s[0]
26#define STOC2(s) s[0], STOC1((s + 1))
27#define STOC3(s) s[0], STOC2((s + 1))
28#define STOC4(s) s[0], STOC3((s + 1))
29
30/* NULL padded strings */
31#define S0N(s, z) z
32#define S1N(s, z) STOC1(s), z
33#define S2N(s, z) STOC2(s), z
34#define S3N(s, z) STOC3(s), z
35#define S4N(s, z) STOC4(s), z
36#define S5N(s, z) STOC4(s), STOC1((s + 4)), z
37#define S6N(s, z) STOC4(s), STOC2((s + 4)), z
38#define S7N(s, z) STOC4(s), STOC3((s + 4)), z
39#define S8N(s, z) STOC4(s), STOC4((s + 4)), z
40#define S9N(s, z) STOC4(s), STOC4((s + 4)), STOC1((s + 8)), z
41#define S10N(s, z) STOC4(s), STOC4((s + 4)), STOC2((s + 8)), z
42#define S11N(s, z) STOC4(s), STOC4((s + 4)), STOC3((s + 8)), z
43#define S12N(s, z) STOC4(s), STOC4((s + 4)), STOC4((s + 8)), z
44#define S13N(s, z) STOC4(s), STOC4((s + 4)), STOC4((s + 8)), \
45 STOC1((s + 12)), z
46#define S14N(s, z) STOC4(s), STOC4((s + 4)), STOC4((s + 8)), \
47 STOC2((s + 12)), z
48#define S15N(s, z) STOC4(s), STOC4((s + 4)), STOC4((s + 8)), \
49 STOC3((s + 12)), z
50#define S16N(s, z) STOC4(s), STOC4((s + 4)), STOC4((s + 8)), \
51 STOC4((s + 12)), z
52
53/* general string type, used for addresses and strings */
54#define S0(s) S0N(s, Z4)
55#define S1(s) S1N(s, Z3)
56#define S2(s) S2N(s, Z2)
57#define S3(s) S3N(s, Z1)
58#define S4(s) S4N(s, Z4)
59#define S5(s) S5N(s, Z3)
60#define S6(s) S6N(s, Z2)
61#define S7(s) S7N(s, Z1)
62#define S8(s) S8N(s, Z4)
63#define S9(s) S9N(s, Z3)
64#define S10(s) S10N(s, Z2)
65#define S11(s) S11N(s, Z1)
66#define S12(s) S12N(s, Z4)
67#define S13(s) S13N(s, Z3)
68#define S14(s) S14N(s, Z2)
69#define S15(s) S15N(s, Z1)
70#define S16(s) S16N(s, Z4)
71
72/* typetags */
73#define T0(s) S0N(s, Z3)
74#define T1(s) S1N(s, Z2)
75#define T2(s) S2N(s, Z1)
76#define T3(s) S3N(s, Z4)
77#define T4(s) S4N(s, Z3)
78#define T5(s) S5N(s, Z2)
79#define T6(s) S6N(s, Z1)
80#define T7(s) S7N(s, Z4)
81#define T8(s) S8N(s, Z3)
82#define T9(s) S9N(s, Z2)
83#define T10(s) S10N(s, Z1)
84#define T11(s) S11N(s, Z4)
85#define T12(s) S12N(s, Z3)
86#define T13(s) S13N(s, Z2)
87#define T14(s) S14N(s, Z1)
88#define T15(s) S15N(s, Z4)
89#define T16(s) S16N(s, Z3)
90
// DOXYGEN_SKIP
92
102#define A(n, a) S ## n (a)
103
113#define T(n, s) ',', T ## n (s)
114
122#define Di(i) \
123 ((i & 0xFF000000) >> 24), \
124 ((i & 0x00FF0000) >> 16), \
125 ((i & 0x0000FF00) >> 8), \
126 (i & 0x000000FF)
127
129/* data: float32 */
130union flint
131{
132 int32_t i;
133 float f;
134};
// DOXYGEN_SKIP
136
144#define Df(ff) \
145 (((union flint){.f = ff}.i & 0xFF000000) >> 24), \
146 (((union flint){.f = ff}.i & 0x00FF0000) >> 16), \
147 (((union flint){.f = ff}.i & 0x0000FF00) >> 8), \
148 ((union flint){.f = ff}.i & 0x000000FF)
149
151/* data: 64 bit, as a double or as an integer */
152union dblint
153{
154 int64_t i;
155 double d;
156};
// DOXYGEN_SKIP
158
169#define Dl(ll) \
170 ((char)(((int64_t)(ll) >> 56) & 0xFF)), \
171 ((char)(((int64_t)(ll) >> 48) & 0xFF)), \
172 ((char)(((int64_t)(ll) >> 40) & 0xFF)), \
173 ((char)(((int64_t)(ll) >> 32) & 0xFF)), \
174 ((char)(((int64_t)(ll) >> 24) & 0xFF)), \
175 ((char)(((int64_t)(ll) >> 16) & 0xFF)), \
176 ((char)(((int64_t)(ll) >> 8) & 0xFF)), \
177 ((char)((int64_t)(ll) & 0xFF))
178
186#define Dd(dd) Dl(((union dblint){.d = (dd)}.i))
187
199#define Dt(sec, fsec) Di(((int32_t)(sec))), Di(((int32_t)(fsec)))
200
210#define Ds(n, s) S ## n (s)
211
213/* data: blob */
214#define BS(i) 0, 0, 0, i,
215
216#define B0(b) Z4
217#define B1(b) BS(1) S1N(b, Z3)
218#define B2(b) BS(2) S2N(b, Z2)
219#define B3(b) BS(3) S3N(b, Z1)
220#define B4(b) BS(4) S4N(b, Z0)
221#define B5(b) BS(5) S5N(b, Z3)
222#define B6(b) BS(6) S6N(b, Z2)
223#define B7(b) BS(7) S7N(b, Z1)
224#define B8(b) BS(8) S8N(b, Z0)
225#define B9(b) BS(9) S9N(b, Z3)
226#define B10(b) BS(10) S10N(b, Z2)
227#define B11(b) BS(11) S11N(b, Z1)
228#define B12(b) BS(12) S12N(b, Z0)
229#define B13(b) BS(13) S9N(b, Z3)
230#define B14(b) BS(14) S10N(b, Z2)
231#define B15(b) BS(15) S11N(b, Z1)
232#define B16(b) BS(16) S12N(b, Z0)
// DOXYGEN_SKIP
234
244#define Db(n, b) B ## n (b)
245
247/* bundle elements */
248/*
249 Emit a size as four big-endian bytes.
250
251 Each byte must be SHIFTED as well as masked. Storing
252 (n & 0xFF000000) into a char keeps only the low 8 bits of that value,
253 which are always zero -- so before this was fixed the top three bytes
254 were always 0 and any size of 256 or more was silently truncated to its
255 low byte. Nothing caught it because every test bundle was small enough
256 that only the low byte mattered.
257*/
258#define BE_SIZE(...) \
259 ((sizeof((char []){__VA_ARGS__}) >> 24) & 0xFF), \
260 ((sizeof((char []){__VA_ARGS__}) >> 16) & 0xFF), \
261 ((sizeof((char []){__VA_ARGS__}) >> 8) & 0xFF), \
262 (sizeof((char []){__VA_ARGS__}) & 0xFF),
// DOXYGEN_SKIP
264
272#define BE_M(...) \
273 BE_SIZE(__VA_ARGS__) \
274 __VA_ARGS__
275
284#define BE_B(...) \
285 BE_SIZE('#', 'b', 'u', 'n', 'd', 'l', 'e', 0, \
286 0, 0, 0, 0, 0, 0, 0, 0, \
287 __VA_ARGS__) \
288 '#', 'b', 'u', 'n', 'd', 'l', 'e', 0, \
289 0, 0, 0, 0, 0, 0, 0, 0, \
290 __VA_ARGS__
291
292/* necessary in order to avoid a trailing comma */
298#define BE_B0() \
299 BE_SIZE('#', 'b', 'u', 'n', 'd', 'l', 'e', 0, \
300 0, 0, 0, 0, 0, 0, 0, 0) \
301 '#', 'b', 'u', 'n', 'd', 'l', 'e', 0, \
302 0, 0, 0, 0, 0, 0, 0, 0
303
321#define M(...) ((char []){ BE_M(__VA_ARGS__) })
322
339#define B(...) ((char []){ BE_B(__VA_ARGS__) })
340
341/*
342Example:
343
344char b[] = B(BE_M(A(0, ""), T(0, "")),
345 BE_B(BE_M(A(2, "/x"),
346 T(2, "ii"),
347 Di(33),
348 Di(44)),
349 BE_M(A(8, "/foo/bar"),
350 T(4, "ifsb"),
351 Di(10),
352 Df(3.14159),
353 Ds(3, "foo"),
354 Db(3, "\x0\x1\x2"))));
355 */