1 /*
2 * This file has been copied from the zcbor library.
3 * Commit zcbor 0.8.1
4 */
5
6 /*
7 * Copyright (c) 2023 Nordic Semiconductor ASA
8 *
9 * SPDX-License-Identifier: Apache-2.0
10 */
11
12 #ifndef ZCBOR_PRINT_H__
13 #define ZCBOR_PRINT_H__
14
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #ifndef ZCBOR_PRINT_FUNC
21 #include <stdio.h>
22 #define zcbor_do_print(...) printf(__VA_ARGS__)
23 #else
24 #define zcbor_do_print(...) ZCBOR_PRINT_FUNC(__VA_ARGS__)
25 #endif
26
27 #ifdef ZCBOR_VERBOSE
28 #define zcbor_trace_raw(state) (zcbor_do_print("rem: %zu, cur: 0x%x, ec: 0x%zx, err: %d",\
29 (size_t)state->payload_end - (size_t)state->payload, *state->payload, state->elem_count, \
30 state->constant_state ? state->constant_state->error : 0))
31 #define zcbor_trace(state, appendix) do { \
32 zcbor_trace_raw(state); \
33 zcbor_do_print(", %s\n", appendix); \
34 } while(0)
35 #define zcbor_trace_file(state) do { \
36 zcbor_trace_raw(state); \
37 zcbor_do_print(", %s:%d\n", __FILE__, __LINE__); \
38 } while(0)
39
40 #define zcbor_log_assert(expr, ...) \
41 do { \
42 zcbor_do_print("ASSERTION \n \"" #expr \
43 "\"\nfailed at %s:%d with message:\n ", \
44 __FILE__, __LINE__); \
45 zcbor_do_print(__VA_ARGS__);\
46 } while(0)
47 #define zcbor_log(...) zcbor_do_print(__VA_ARGS__)
48 #else
49 #define zcbor_trace(state, appendix)
50 #define zcbor_trace_file(state) ((void)state)
51 #define zcbor_log_assert(...)
52 #define zcbor_log(...)
53 #endif
54
55 #ifdef ZCBOR_ASSERTS
56 #define zcbor_assert(expr, ...) \
57 do { \
58 if (!(expr)) { \
59 zcbor_log_assert(expr, __VA_ARGS__); \
60 ZCBOR_FAIL(); \
61 } \
62 } while(0)
63 #define zcbor_assert_state(expr, ...) \
64 do { \
65 if (!(expr)) { \
66 zcbor_log_assert(expr, __VA_ARGS__); \
67 ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \
68 } \
69 } while(0)
70 #else
71 #define zcbor_assert(expr, ...)
72 #define zcbor_assert_state(expr, ...)
73 #endif
74
75 __attribute__((used))
zcbor_print_compare_lines(const uint8_t * str1,const uint8_t * str2,size_t size)76 static void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, size_t size)
77 {
78 for (size_t j = 0; j < size; j++) {
79 zcbor_do_print("%x ", str1[j]);
80 }
81 zcbor_do_print("\r\n");
82 for (size_t j = 0; j < size; j++) {
83 zcbor_do_print("%x ", str2[j]);
84 }
85 zcbor_do_print("\r\n");
86 for (size_t j = 0; j < size; j++) {
87 zcbor_do_print("%x ", str1[j] != str2[j]);
88 }
89 zcbor_do_print("\r\n");
90 zcbor_do_print("\r\n");
91 }
92
93 __attribute__((used))
zcbor_print_compare_strings(const uint8_t * str1,const uint8_t * str2,size_t size)94 static void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, size_t size)
95 {
96 const size_t col_width = 16;
97
98 for (size_t i = 0; i <= size / col_width; i++) {
99 zcbor_do_print("line %zu (char %zu)\r\n", i, i*col_width);
100 zcbor_print_compare_lines(&str1[i*col_width], &str2[i*col_width],
101 MIN(col_width, (size - i*col_width)));
102 }
103 zcbor_do_print("\r\n");
104 }
105
106 __attribute__((used))
zcbor_print_compare_strings_diff(const uint8_t * str1,const uint8_t * str2,size_t size)107 static void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, size_t size)
108 {
109 const size_t col_width = 16;
110 bool printed = false;
111
112 for (size_t i = 0; i <= size / col_width; i++) {
113 if (memcmp(&str1[i*col_width], &str2[i*col_width], MIN(col_width, (size - i*col_width))) != 0) {
114 zcbor_do_print("line %zu (char %zu)\r\n", i, i*col_width);
115 zcbor_print_compare_lines(&str1[i*col_width], &str2[i*col_width],
116 MIN(col_width, (size - i*col_width)));
117 printed = true;
118 }
119 }
120 if (printed) {
121 zcbor_do_print("\r\n");
122 }
123 }
124
125 __attribute__((used))
zcbor_error_str(int error)126 static const char *zcbor_error_str(int error)
127 {
128 #define ZCBOR_ERR_CASE(err) case err: \
129 return #err; /* The literal is static per C99 6.4.5 paragraph 5. */\
130
131 switch(error) {
132 ZCBOR_ERR_CASE(ZCBOR_SUCCESS)
133 ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_MEM)
134 ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_ACTIVE)
135 ZCBOR_ERR_CASE(ZCBOR_ERR_LOW_ELEM_COUNT)
136 ZCBOR_ERR_CASE(ZCBOR_ERR_HIGH_ELEM_COUNT)
137 ZCBOR_ERR_CASE(ZCBOR_ERR_INT_SIZE)
138 ZCBOR_ERR_CASE(ZCBOR_ERR_FLOAT_SIZE)
139 ZCBOR_ERR_CASE(ZCBOR_ERR_ADDITIONAL_INVAL)
140 ZCBOR_ERR_CASE(ZCBOR_ERR_NO_PAYLOAD)
141 ZCBOR_ERR_CASE(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED)
142 ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_TYPE)
143 ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_VALUE)
144 ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_RANGE)
145 ZCBOR_ERR_CASE(ZCBOR_ERR_ITERATIONS)
146 ZCBOR_ERR_CASE(ZCBOR_ERR_ASSERTION)
147 ZCBOR_ERR_CASE(ZCBOR_ERR_PAYLOAD_OUTDATED)
148 ZCBOR_ERR_CASE(ZCBOR_ERR_ELEM_NOT_FOUND)
149 ZCBOR_ERR_CASE(ZCBOR_ERR_MAP_MISALIGNED)
150 ZCBOR_ERR_CASE(ZCBOR_ERR_ELEMS_NOT_PROCESSED)
151 ZCBOR_ERR_CASE(ZCBOR_ERR_NOT_AT_END)
152 ZCBOR_ERR_CASE(ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE)
153 ZCBOR_ERR_CASE(ZCBOR_ERR_INVALID_VALUE_ENCODING)
154 }
155 #undef ZCBOR_ERR_CASE
156
157 return "ZCBOR_ERR_UNKNOWN";
158 }
159
160 __attribute__((used))
zcbor_print_error(int error)161 static void zcbor_print_error(int error)
162 {
163 zcbor_do_print("%s\r\n", zcbor_error_str(error));
164 }
165
166 #ifdef __cplusplus
167 }
168 #endif
169
170 #endif /* ZCBOR_PRINT_H__ */
171