Skip to content

Commit ad5f0ac

Browse files
oyvindronningstadnordicjm
authored andcommitted
zcbor: Copy source and header files
from zcbor 0.8.0 Signed-off-by: Øyvind Rønningstad <[email protected]>
1 parent c3a72e9 commit ad5f0ac

File tree

9 files changed

+2079
-971
lines changed

9 files changed

+2079
-971
lines changed

boot/zcbor/include/zcbor_common.h

Lines changed: 185 additions & 105 deletions
Large diffs are not rendered by default.

boot/zcbor/include/zcbor_debug.h

Lines changed: 0 additions & 69 deletions
This file was deleted.

boot/zcbor/include/zcbor_decode.h

Lines changed: 294 additions & 197 deletions
Large diffs are not rendered by default.

boot/zcbor/include/zcbor_encode.h

Lines changed: 104 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* This file has been copied from the zcbor library.
3-
* Commit zcbor 0.7.0
4-
*/
5-
61
/*
72
* Copyright (c) 2020 Nordic Semiconductor ASA
83
*
@@ -21,137 +16,80 @@
2116
extern "C" {
2217
#endif
2318

19+
2420
/** The zcbor_encode library provides functions for encoding CBOR data elements.
2521
*
2622
* See The README for an introduction to CBOR, including the meaning of pint,
2723
* nint, bstr etc.
2824
*/
2925

3026

31-
/** The following param and retval docs apply to all single value encoding functions
32-
*
33-
* @param[inout] state The current state of the encoding.
34-
* @param[in] input The value to encode.
35-
*
36-
* @retval true Everything is ok.
37-
* @retval false If the payload is exhausted. Or an unexpected error happened.
38-
*/
39-
40-
/** Encode a pint/nint. */
41-
bool zcbor_int32_put(zcbor_state_t *state, int32_t input);
42-
bool zcbor_int64_put(zcbor_state_t *state, int64_t input);
43-
bool zcbor_uint32_put(zcbor_state_t *state, uint32_t input);
44-
bool zcbor_uint64_put(zcbor_state_t *state, uint64_t input);
45-
bool zcbor_size_put(zcbor_state_t *state, size_t input);
46-
47-
/** Encode a pint/nint from a pointer.
48-
*
49-
* Can be used for bulk encoding with @ref zcbor_multi_encode.
50-
*/
51-
bool zcbor_int_encode(zcbor_state_t *state, const void *input_int, size_t int_size);
52-
bool zcbor_int32_encode(zcbor_state_t *state, const int32_t *input);
53-
bool zcbor_int64_encode(zcbor_state_t *state, const int64_t *input);
54-
bool zcbor_uint32_encode(zcbor_state_t *state, const uint32_t *input);
55-
bool zcbor_uint64_encode(zcbor_state_t *state, const uint64_t *input);
56-
bool zcbor_size_encode(zcbor_state_t *state, const size_t *input);
57-
58-
/** Encode a bstr. */
59-
bool zcbor_bstr_encode(zcbor_state_t *state, const struct zcbor_string *input);
60-
/** Encode a tstr. */
61-
bool zcbor_tstr_encode(zcbor_state_t *state, const struct zcbor_string *input);
62-
63-
/** Encode a pointer to a string as a bstr/tstr.
64-
*
65-
* @param[inout] state The current state of the encoding.
66-
* @param[in] string The value to encode. A pointer to the string
67-
* @param[in] len The length of the string pointed to by @p string.
68-
*/
69-
static inline bool zcbor_bstr_encode_ptr(zcbor_state_t *state, const char *ptr, size_t len)
70-
{
71-
const struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
72-
73-
return zcbor_bstr_encode(state, &zs);
74-
}
75-
static inline bool zcbor_tstr_encode_ptr(zcbor_state_t *state, const char *ptr, size_t len)
76-
{
77-
const struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
78-
79-
return zcbor_tstr_encode(state, &zs);
80-
}
81-
82-
/** Encode a string literal as a bstr/tstr.
83-
*
84-
* @param[inout] state The current state of the encoding.
85-
* @param[in] string The value to encode. A string literal, e.g. "Foo", so
86-
* that sizeof(string) - 1 is the length of the string.
87-
*/
88-
#define zcbor_bstr_put_lit(state, string) \
89-
zcbor_bstr_encode_ptr(state, string, sizeof(string) - 1)
90-
#define zcbor_tstr_put_lit(state, string) \
91-
zcbor_tstr_encode_ptr(state, string, sizeof(string) - 1)
27+
/** See @ref zcbor_new_state() */
28+
void zcbor_new_encode_state(zcbor_state_t *state_array, size_t n_states,
29+
uint8_t *payload, size_t payload_len, size_t elem_count);
9230

93-
/** Encode null-terminated string as a bstr/tstr.
31+
/** Convenience macro for declaring and initializing an encoding state with backups.
9432
*
95-
* @param[inout] state The current state of the encoding.
96-
* @param[in] string The value to encode. Must be a null-terminated string,
97-
* so that strlen can be used.
98-
*/
99-
#define zcbor_bstr_put_term(state, string) \
100-
zcbor_bstr_encode_ptr(state, string, strlen(string))
101-
#define zcbor_tstr_put_term(state, string) \
102-
zcbor_tstr_encode_ptr(state, string, strlen(string))
103-
104-
/** Encode a char array literal as a bstr/tstr.
33+
* This gives you a state variable named @p name. The variable functions like
34+
* a pointer.
10535
*
106-
* @param[inout] state The current state of the encoding.
107-
* @param[in] string The value to encode. An array literal, e.g. {'F', 'o', 'o'},
108-
* so that sizeof(string) is the length of the string.
36+
* @param[in] name The name of the new state variable.
37+
* @param[in] num_backups The number of backup slots to keep in the state.
38+
* @param[in] payload The payload to work on.
39+
* @param[in] payload_size The size (in bytes) of @p payload.
40+
* @param[in] elem_count The starting elem_count (typically 1).
10941
*/
110-
#define zcbor_bstr_put_arr(state, string) \
111-
zcbor_bstr_encode_ptr(state, string, sizeof(string))
112-
#define zcbor_tstr_put_arr(state, string) \
113-
zcbor_tstr_encode_ptr(state, string, sizeof(string))
114-
115-
/** Encode a tag. Must be called before encoding the value being tagged. */
116-
bool zcbor_tag_encode(zcbor_state_t *state, uint32_t input);
117-
118-
/** Encode a boolean primitive value. */
119-
bool zcbor_bool_put(zcbor_state_t *state, bool input);
120-
bool zcbor_bool_encode(zcbor_state_t *state, const bool *input);
42+
#define ZCBOR_STATE_E(name, num_backups, payload, payload_size, elem_count) \
43+
zcbor_state_t name[((num_backups) + 2)]; \
44+
do { \
45+
zcbor_new_encode_state(name, ZCBOR_ARRAY_SIZE(name), payload, payload_size, elem_count); \
46+
} while(0)
12147

122-
/** Encode a float */
123-
bool zcbor_float32_put(zcbor_state_t *state, float input);
124-
bool zcbor_float32_encode(zcbor_state_t *state, const float *input);
125-
bool zcbor_float64_put(zcbor_state_t *state, double input);
126-
bool zcbor_float64_encode(zcbor_state_t *state, const double *input);
12748

128-
/** Encode a "nil"/"undefined" primitive value. @p unused should be NULL.
49+
/** The following applies to all _put and _encode functions listed directly below.
12950
*
130-
* @param[inout] state The current state of the encoding.
131-
* @param[in] unused Unused parameter to maintain signature parity with
132-
* @ref zcbor_encoder_t.
133-
*/
134-
bool zcbor_nil_put(zcbor_state_t *state, const void *unused);
135-
bool zcbor_undefined_put(zcbor_state_t *state, const void *unused);
136-
137-
/** Encode a bstr header.
51+
* The difference between _put and _encode is only in the argument type,
52+
* but when a @ref zcbor_encoder_t is needed, such as for @ref zcbor_multi_encode,
53+
* the _encode variant must be used.
13854
*
139-
* The rest of the string can be encoded as CBOR.
140-
* A state backup is created to keep track of the element count.
141-
* Call @ref zcbor_bstr_end_encode when done encoding the contents of the bstr.
142-
*
143-
* @param[inout] state The current state of the encoding.
144-
*
145-
* @retval true Header encoded correctly
146-
* @retval false Header encoded incorrectly, or backup failed.
147-
*/
148-
bool zcbor_bstr_start_encode(zcbor_state_t *state);
149-
150-
/** Finalize encoding a CBOR-encoded bstr.
55+
* @param[inout] state The current state of the encoding.
56+
* @param[in] input The value to encode.
15157
*
152-
* Restore element count from backup.
153-
*/
154-
bool zcbor_bstr_end_encode(zcbor_state_t *state, struct zcbor_string *result);
58+
* @retval true Everything is ok.
59+
* @retval false If the payload is exhausted. Or an unexpected error happened.
60+
* Use zcbor_peek_error() to see the error code.
61+
*/
62+
bool zcbor_int32_put(zcbor_state_t *state, int32_t input); /* pint/nint */
63+
bool zcbor_int64_put(zcbor_state_t *state, int64_t input); /* pint/nint */
64+
bool zcbor_uint32_put(zcbor_state_t *state, uint32_t input); /* pint */
65+
bool zcbor_uint64_put(zcbor_state_t *state, uint64_t input); /* pint */
66+
bool zcbor_size_put(zcbor_state_t *state, size_t input); /* pint */
67+
bool zcbor_tag_put(zcbor_state_t *state, uint32_t tag); /* CBOR tag */
68+
bool zcbor_simple_put(zcbor_state_t *state, uint8_t input); /* CBOR simple value */
69+
bool zcbor_bool_put(zcbor_state_t *state, bool input); /* boolean CBOR simple value */
70+
bool zcbor_nil_put(zcbor_state_t *state, const void *unused); /* 'nil' CBOR simple value */
71+
bool zcbor_undefined_put(zcbor_state_t *state, const void *unused); /* 'undefined' CBOR simple value */
72+
bool zcbor_float16_put(zcbor_state_t *state, float input); /* IEEE754 float16 */
73+
bool zcbor_float16_bytes_put(zcbor_state_t *state, uint16_t input); /* IEEE754 float16 raw bytes */
74+
bool zcbor_float32_put(zcbor_state_t *state, float input); /* IEEE754 float32 */
75+
bool zcbor_float64_put(zcbor_state_t *state, double input); /* IEEE754 float64 */
76+
77+
bool zcbor_int32_encode(zcbor_state_t *state, const int32_t *input); /* pint/nint */
78+
bool zcbor_int64_encode(zcbor_state_t *state, const int64_t *input); /* pint/nint */
79+
bool zcbor_uint32_encode(zcbor_state_t *state, const uint32_t *input); /* pint */
80+
bool zcbor_uint64_encode(zcbor_state_t *state, const uint64_t *input); /* pint */
81+
bool zcbor_size_encode(zcbor_state_t *state, const size_t *input); /* pint */
82+
bool zcbor_int_encode(zcbor_state_t *state, const void *input_int, size_t int_size);
83+
bool zcbor_uint_encode(zcbor_state_t *state, const void *input_uint, size_t uint_size);
84+
bool zcbor_bstr_encode(zcbor_state_t *state, const struct zcbor_string *input); /* bstr */
85+
bool zcbor_tstr_encode(zcbor_state_t *state, const struct zcbor_string *input); /* tstr */
86+
bool zcbor_tag_encode(zcbor_state_t *state, uint32_t *tag); /* CBOR tag. Note that zcbor_tag_encode()'s argument was changed to be a pointer. See also zcbor_tag_put(). */
87+
bool zcbor_simple_encode(zcbor_state_t *state, uint8_t *input); /* CBOR simple value */
88+
bool zcbor_bool_encode(zcbor_state_t *state, const bool *input); /* boolean CBOR simple value */
89+
bool zcbor_float16_encode(zcbor_state_t *state, const float *input); /* IEEE754 float16 */
90+
bool zcbor_float16_bytes_encode(zcbor_state_t *state, const uint16_t *input); /* IEEE754 float16 raw bytes */
91+
bool zcbor_float32_encode(zcbor_state_t *state, const float *input); /* IEEE754 float32 */
92+
bool zcbor_float64_encode(zcbor_state_t *state, const double *input); /* IEEE754 float64 */
15593

15694
/** Encode a list/map header.
15795
*
@@ -169,8 +107,8 @@ bool zcbor_bstr_end_encode(zcbor_state_t *state, struct zcbor_string *result);
169107
* call.
170108
* Only used when ZCBOR_CANONICAL is defined.
171109
*/
172-
bool zcbor_list_start_encode(zcbor_state_t *state, uint_fast32_t max_num);
173-
bool zcbor_map_start_encode(zcbor_state_t *state, uint_fast32_t max_num);
110+
bool zcbor_list_start_encode(zcbor_state_t *state, size_t max_num);
111+
bool zcbor_map_start_encode(zcbor_state_t *state, size_t max_num);
174112

175113
/** Encode the end of a list/map. Do some checks and deallocate backup.
176114
*
@@ -189,8 +127,8 @@ bool zcbor_map_start_encode(zcbor_state_t *state, uint_fast32_t max_num);
189127
* @ref zcbor_list_start_encode call.
190128
* Only used when ZCBOR_CANONICAL is defined.
191129
*/
192-
bool zcbor_list_end_encode(zcbor_state_t *state, uint_fast32_t max_num);
193-
bool zcbor_map_end_encode(zcbor_state_t *state, uint_fast32_t max_num);
130+
bool zcbor_list_end_encode(zcbor_state_t *state, size_t max_num);
131+
bool zcbor_map_end_encode(zcbor_state_t *state, size_t max_num);
194132
bool zcbor_list_map_end_force_encode(zcbor_state_t *state);
195133

196134
/** Encode 0 or more elements with the same type and constraints.
@@ -241,49 +179,59 @@ bool zcbor_list_map_end_force_encode(zcbor_state_t *state);
241179
* @retval false If @p encoder failed before having encoded @p min_encode
242180
* values.
243181
*/
244-
bool zcbor_multi_encode(uint_fast32_t num_encode,
245-
zcbor_encoder_t encoder,
246-
zcbor_state_t *state,
247-
const void *input,
248-
uint_fast32_t result_len);
182+
bool zcbor_multi_encode(size_t num_encode, zcbor_encoder_t encoder,
183+
zcbor_state_t *state, const void *input, size_t result_len);
249184

250185
/** Works like @ref zcbor_multi_encode
251186
*
252187
* But first checks that @p num_encode is between @p min_encode and @p max_encode.
253188
*/
254-
bool zcbor_multi_encode_minmax(uint_fast32_t min_encode, uint_fast32_t max_encode, const uint_fast32_t *num_encode,
255-
zcbor_encoder_t encoder, zcbor_state_t *state, const void *input,
256-
uint_fast32_t input_len);
189+
bool zcbor_multi_encode_minmax(size_t min_encode, size_t max_encode,
190+
const size_t *num_encode, zcbor_encoder_t encoder,
191+
zcbor_state_t *state, const void *input, size_t input_len);
257192

258-
/** Runs @p encoder on @p state and @p input if @p present is true.
259-
*
260-
* Calls @ref zcbor_multi_encode under the hood.
261-
*/
262-
bool zcbor_present_encode(const uint_fast32_t *present,
263-
zcbor_encoder_t encoder,
264-
zcbor_state_t *state,
265-
const void *input);
266193

267-
/** See @ref zcbor_new_state() */
268-
void zcbor_new_encode_state(zcbor_state_t *state_array, uint_fast32_t n_states,
269-
uint8_t *payload, size_t payload_len, uint_fast32_t elem_count);
194+
/* Supplementary string (bstr/tstr) encoding functions: */
270195

271-
/** Convenience macro for declaring and initializing a state with backups.
196+
/** Encode a char/uint8_t pointer as a bstr/tstr.
272197
*
273-
* This gives you a state variable named @p name. The variable functions like
274-
* a pointer.
198+
* @param[inout] state The current state of the encoding.
199+
* @param[in] str The value to encode. A pointer to the string/array.
200+
* _term() uses strnlen(), so @p str must be null-terminated.
201+
* _lit() uses sizeof()-1, so @p str must be a (null-terminated) string literal.
202+
* _arr() uses sizeof(), so @p str must be a uint8_t array (not null-terminated).
203+
* @param[in] len (if present) The length of the string pointed to by @p str
204+
* @param[in] maxlen (if present) The maximum length of the string pointed to by @p str.
205+
* This value is passed to strnlen.
206+
*/
207+
bool zcbor_bstr_encode_ptr(zcbor_state_t *state, const char *str, size_t len);
208+
bool zcbor_tstr_encode_ptr(zcbor_state_t *state, const char *str, size_t len);
209+
bool zcbor_bstr_put_term(zcbor_state_t *state, char const *str, size_t maxlen);
210+
bool zcbor_tstr_put_term(zcbor_state_t *state, char const *str, size_t maxlen);
211+
#define zcbor_bstr_put_lit(state, str) zcbor_bstr_encode_ptr(state, str, sizeof(str) - 1)
212+
#define zcbor_tstr_put_lit(state, str) zcbor_tstr_encode_ptr(state, str, sizeof(str) - 1)
213+
#define zcbor_bstr_put_arr(state, str) zcbor_bstr_encode_ptr(state, str, sizeof(str))
214+
#define zcbor_tstr_put_arr(state, str) zcbor_tstr_encode_ptr(state, str, sizeof(str))
215+
216+
/** Encode a bstr header.
275217
*
276-
* @param[in] name The name of the new state variable.
277-
* @param[in] num_backups The number of backup slots to keep in the state.
278-
* @param[in] payload The payload to work on.
279-
* @param[in] payload_size The size (in bytes) of @p payload.
280-
* @param[in] elem_count The starting elem_count (typically 1).
218+
* The rest of the string can be encoded as CBOR.
219+
* A state backup is created to keep track of the element count.
220+
* Call @ref zcbor_bstr_end_encode when done encoding the contents of the bstr.
221+
*
222+
* @param[inout] state The current state of the encoding.
223+
*
224+
* @retval true Header encoded correctly
225+
* @retval false Header encoded incorrectly, or backup failed.
281226
*/
282-
#define ZCBOR_STATE_E(name, num_backups, payload, payload_size, elem_count) \
283-
zcbor_state_t name[((num_backups) + 2)]; \
284-
do { \
285-
zcbor_new_encode_state(name, ZCBOR_ARRAY_SIZE(name), payload, payload_size, elem_count); \
286-
} while(0)
227+
bool zcbor_bstr_start_encode(zcbor_state_t *state);
228+
229+
/** Finalize encoding a CBOR-encoded bstr.
230+
*
231+
* This writes the final size of the bstr to the header.
232+
* Restore element count from backup.
233+
*/
234+
bool zcbor_bstr_end_encode(zcbor_state_t *state, struct zcbor_string *result);
287235

288236
#ifdef __cplusplus
289237
}

0 commit comments

Comments
 (0)