1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/*
* Copyright (c) 2018 Vasile Vilvoiu <vasi.vilvoiu@gmail.com>
*
* libecbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef _ECBOR_INTERNAL_H_
#define _ECBOR_INTERNAL_H_
#include <stdint.h>
#include <stddef.h>
/* Don't rely on <stdbool.h> for this */
#define false 0
#define true 1
/* CBOR major type 7 must not be exposed to user, but translated to other types */
enum {
ECBOR_TYPE_SPECIAL = 7
};
/* Additional value meanings */
enum {
ECBOR_ADDITIONAL_LAST_INTEGER = 23,
ECBOR_ADDITIONAL_1BYTE = 24,
ECBOR_ADDITIONAL_2BYTE = 25,
ECBOR_ADDITIONAL_4BYTE = 26,
ECBOR_ADDITIONAL_8BYTE = 27,
ECBOR_ADDITIONAL_INDEFINITE = 31
};
/* Simple value meanings */
enum {
ECBOR_SIMPLE_FALSE = 20,
ECBOR_SIMPLE_TRUE = 21,
ECBOR_SIMPLE_NULL = 22,
ECBOR_SIMPLE_UNDEFINED = 23
};
/* Static item, for various initializations */
static const ecbor_item_t null_item = {
.type = ECBOR_TYPE_NONE,
.value = {
.tag = {
.tag_value = 0,
.child = NULL
}
},
.size = 0,
.length = 0,
.is_indefinite = 0,
.parent = NULL,
.child = NULL,
.next = NULL,
.index = 0
};
/* Internal checks. Most of these macros rely on the function returning an
error code, and a 'rc' value being declared locally */
#define ECBOR_INTERNAL_CHECK_CONTEXT_PTR(c) \
{ \
if (!(c)) { \
return ECBOR_ERR_NULL_CONTEXT; \
} \
}
#define ECBOR_INTERNAL_CHECK_ITEM_PTR(i) \
{ \
if (!(i)) { \
return ECBOR_ERR_NULL_ITEM; \
} \
}
#define ECBOR_INTERNAL_CHECK_VALUE_PTR(v) \
{ \
if (!(v)) { \
return ECBOR_ERR_NULL_VALUE; \
} \
}
#define ECBOR_INTERNAL_CHECK_TYPE(t1, ref) \
{ \
if ((t1) != (ref)) { \
return ECBOR_ERR_INVALID_TYPE; \
} \
}
#define ECBOR_INTERNAL_CHECK_BOUNDS(index, limit) \
{ \
if ((index) >= (limit)) { \
return ECBOR_ERR_INDEX_OUT_OF_BOUNDS; \
} \
}
/*
* Endianness
*/
extern uint16_t
ecbor_uint16_from_big_endian (uint16_t value);
extern uint32_t
ecbor_uint32_from_big_endian (uint32_t value);
extern uint64_t
ecbor_uint64_from_big_endian (uint64_t value);
extern float
ecbor_fp32_from_big_endian (float value);
extern double
ecbor_fp64_from_big_endian (double value);
extern uint16_t
ecbor_uint16_to_big_endian (uint16_t value);
extern uint32_t
ecbor_uint32_to_big_endian (uint32_t value);
extern uint64_t
ecbor_uint64_to_big_endian (uint64_t value);
extern float
ecbor_fp32_to_big_endian (float value);
extern double
ecbor_fp64_to_big_endian (double value);
/*
* Memory
*/
extern void
ecbor_memcpy (void *dest, void *src, size_t num);
#endif
|