Commit ad5f0ac1 authored by Øyvind Rønningstad's avatar Øyvind Rønningstad Committed by Jamie
Browse files

zcbor: Copy source and header files

from zcbor 0.8.0

Signed-off-by: default avatarØyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
parent c3a72e9d
Loading
Loading
Loading
Loading
+173 −93
Changes for boot/zcbor/include/zcbor_common.h: 173 added lines, 93 removed lines.
Original line number Diff line number Diff line
/*
 * This file has been copied from the zcbor library.
 * Commit zcbor 0.7.0
 */

/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
@@ -15,6 +10,8 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "zcbor_tags.h"

#ifdef __cplusplus
extern "C" {
@@ -43,54 +40,25 @@ struct zcbor_string_fragment {
/** Size to use in struct zcbor_string_fragment when the real size is unknown. */
#define ZCBOR_STRING_FRAGMENT_UNKNOWN_LENGTH SIZE_MAX

#ifdef ZCBOR_VERBOSE
#include <zephyr/sys/printk.h>
#define zcbor_trace() (printk("bytes left: %zu, byte: 0x%x, elem_count: 0x%" PRIxFAST32 ", err: %d, %s:%d\n",\
	(size_t)state->payload_end - (size_t)state->payload, *state->payload, state->elem_count, \
	state->constant_state ? state->constant_state->error : 0, __FILE__, __LINE__))

#define zcbor_print_assert(expr, ...) \
do { \
	printk("ASSERTION \n  \"" #expr \
		"\"\nfailed at %s:%d with message:\n  ", \
		__FILE__, __LINE__); \
	printk(__VA_ARGS__);\
} while(0)
#define zcbor_print(...) printk(__VA_ARGS__)
#else
#define zcbor_trace() ((void)state)
#define zcbor_print_assert(...)
#define zcbor_print(...)
#endif

#ifdef ZCBOR_ASSERTS
#define zcbor_assert(expr, ...) \
do { \
	if (!(expr)) { \
		zcbor_print_assert(expr, __VA_ARGS__); \
		ZCBOR_FAIL(); \
	} \
} while(0)
#define zcbor_assert_state(expr, ...) \
do { \
	if (!(expr)) { \
		zcbor_print_assert(expr, __VA_ARGS__); \
		ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \
	} \
} while(0)
#else
#define zcbor_assert(expr, ...)
#define zcbor_assert_state(expr, ...)
#endif

#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif

#ifndef MAX
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
#endif

#ifndef ZCBOR_ARRAY_SIZE
#define ZCBOR_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif

/* Endian-dependent offset of smaller integer in a bigger one. */
#ifdef ZCBOR_BIG_ENDIAN
#define ZCBOR_ECPY_OFFS(dst_len, src_len) ((dst_len) - (src_len))
#else
#define ZCBOR_ECPY_OFFS(dst_len, src_len) (0)
#endif /* ZCBOR_BIG_ENDIAN */

#if SIZE_MAX <= UINT64_MAX
/** The ZCBOR_SUPPORTS_SIZE_T will be defined if processing of size_t type variables directly
 * with zcbor_size_ functions is supported.
@@ -102,6 +70,7 @@ do { \

struct zcbor_state_constant;

/** The zcbor_state_t structure is used for both encoding and decoding. */
typedef struct {
union {
	uint8_t *payload_mut;
@@ -110,7 +79,7 @@ union {
	                             processed. */
};
	uint8_t const *payload_bak; /**< Temporary backup of payload. */
	uint_fast32_t elem_count; /**< The current element is part of a LIST or a MAP,
	size_t elem_count; /**< The current element is part of a LIST or a MAP,
	                        and this keeps count of how many elements are
	                        expected. This will be checked before processing
	                        and decremented if the element is correctly
@@ -118,23 +87,51 @@ union {
	uint8_t const *payload_end; /**< The end of the payload. This will be
	                                 checked against payload before
	                                 processing each element. */
	bool indefinite_length_array; /**< Is set to true if the decoder is currently
	                                   decoding the contents of an indefinite-
	                                   length array. */
	bool payload_moved; /**< Is set to true while the state is stored as a backup
	                         if @ref zcbor_update_state is called, since that function
	                         updates the payload_end of all backed-up states. */

/* This is the "decode state", the part of zcbor_state_t that is only used by zcbor_decode.c. */
struct {
	bool indefinite_length_array; /**< Is set to true if the decoder is currently
	                                   decoding the contents of an indefinite-
	                                   length array. */
	bool counting_map_elems; /**< Is set to true while the number of elements of the
	                              current map are being counted. */
#ifdef ZCBOR_MAP_SMART_SEARCH
	uint8_t *map_search_elem_state; /**< Optional flags to use when searching unordered
	                                     maps. If this is not NULL and map_elem_count
	                                     is non-zero, this consists of one flag per element
	                                     in the current map. The n-th bit can be set to 0
	                                     to indicate that the n-th element in the
	                                     map should not be searched. These are manipulated
	                                     via zcbor_elem_processed() or
	                                     zcbor_unordered_map_search(), and should not be
	                                     manipulated directly. */
#else
	size_t map_elems_processed; /**< The number of elements of an unordered map
	                                 that have been processed. */
#endif
	size_t map_elem_count; /**< Number of elements in the current unordered map.
	                            This also serves as the number of bits (not bytes)
	                            in the map_search_elem_state array (when applicable). */
} decode_state;
	struct zcbor_state_constant *constant_state; /**< The part of the state that is
	                                                  not backed up and duplicated. */
} zcbor_state_t;

struct zcbor_state_constant {
	zcbor_state_t *backup_list;
	uint_fast32_t current_backup;
	uint_fast32_t num_backups;
	size_t current_backup;
	size_t num_backups;
	int error;
#ifdef ZCBOR_STOP_ON_ERROR
	bool stop_on_error;
#endif
	bool manually_process_elem; /**< Whether an (unordered map) element should be automatically
	                                 marked as processed when found via @ref zcbor_search_map_key. */
#ifdef ZCBOR_MAP_SMART_SEARCH
	uint8_t *map_search_elem_state_end; /**< The end of the @ref map_search_elem_state buffer. */
#endif
};

@@ -159,20 +156,35 @@ typedef enum
	ZCBOR_MAJOR_TYPE_LIST   = 4, ///! List
	ZCBOR_MAJOR_TYPE_MAP    = 5, ///! Map
	ZCBOR_MAJOR_TYPE_TAG    = 6, ///! Semantic Tag
	ZCBOR_MAJOR_TYPE_PRIM = 7, ///! Primitive Type
	ZCBOR_MAJOR_TYPE_SIMPLE = 7, ///! Simple values and floats
} zcbor_major_type_t;

/** Extract the major type, i.e. the first 3 bits of the header byte. */
#define ZCBOR_MAJOR_TYPE(header_byte) ((zcbor_major_type_t)(((header_byte) >> 5) & 0x7))

/** Extract the additional info, i.e. the last 5 bits of the header byte. */
#define ZCBOR_ADDITIONAL(header_byte) ((header_byte) & 0x1F)

/** Convenience macro for failing out of a decoding/encoding function.
*/
#define ZCBOR_FAIL() \
do {\
	zcbor_trace(); \
	zcbor_log("ZCBOR_FAIL "); \
	zcbor_trace_file(state); \
	return false; \
} while(0)

#define ZCBOR_FAIL_IF(expr) \
do {\
	if (expr) { \
		zcbor_log("ZCBOR_FAIL_IF(" #expr ") "); \
		ZCBOR_FAIL(); \
	} \
} while(0)

#define ZCBOR_ERR(err) \
do { \
	zcbor_log("ZCBOR_ERR(%d) ", err); \
	zcbor_error(state, err); \
	ZCBOR_FAIL(); \
} while(0)
@@ -180,6 +192,7 @@ do { \
#define ZCBOR_ERR_IF(expr, err) \
do {\
	if (expr) { \
		zcbor_log("ZCBOR_ERR_IF(" #expr ", %d) ", err); \
		ZCBOR_ERR(err); \
	} \
} while(0)
@@ -205,11 +218,12 @@ do { \
#define ZCBOR_VALUE_IS_8_BYTES 27 ///! The next 8 bytes contain the value.
#define ZCBOR_VALUE_IS_INDEFINITE_LENGTH 31 ///! The list or map has indefinite length, and will instead be terminated by a 0xFF token.

#define ZCBOR_BOOL_TO_PRIM ((uint8_t)20) ///! In CBOR, false/true have the values 20/21
#define ZCBOR_BOOL_TO_SIMPLE ((uint8_t)20) ///! In CBOR, false/true have the values 20/21

#define ZCBOR_FLAG_RESTORE 1UL ///! Restore from the backup. Overwrite the current state with the state from the backup.
#define ZCBOR_FLAG_CONSUME 2UL ///! Consume the backup. Remove the backup from the stack of backups.
#define ZCBOR_FLAG_TRANSFER_PAYLOAD 4UL ///! Keep the pre-restore payload after restoring.
#define ZCBOR_FLAG_KEEP_PAYLOAD 4UL ///! Keep the pre-restore payload after restoring.
#define ZCBOR_FLAG_KEEP_DECODE_STATE 8UL ///! Keep the pre-restore decode state (everything only used for decoding)

#define ZCBOR_SUCCESS 0
#define ZCBOR_ERR_NO_BACKUP_MEM 1
@@ -226,47 +240,30 @@ do { \
#define ZCBOR_ERR_WRONG_RANGE 12
#define ZCBOR_ERR_ITERATIONS 13
#define ZCBOR_ERR_ASSERTION 14
#define ZCBOR_ERR_PAYLOAD_OUTDATED 15 ///! Because of a call to @ref zcbor_update_state
#define ZCBOR_ERR_ELEM_NOT_FOUND 16
#define ZCBOR_ERR_MAP_MISALIGNED 17
#define ZCBOR_ERR_ELEMS_NOT_PROCESSED 18
#define ZCBOR_ERR_NOT_AT_END 19
#define ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE 20
#define ZCBOR_ERR_INVALID_VALUE_ENCODING 21 ///! When ZCBOR_CANONICAL is defined, and the incoming data is not encoded with minimal length.
#define ZCBOR_ERR_UNKNOWN 31

/** The largest possible elem_count. */
#ifdef UINT_FAST32_MAX
#define ZCBOR_MAX_ELEM_COUNT UINT_FAST32_MAX
#else
#define ZCBOR_MAX_ELEM_COUNT ((uint_fast32_t)(-1L))
#endif
#define ZCBOR_MAX_ELEM_COUNT SIZE_MAX

/** Initial value for elem_count for when it just needs to be large. */
#define ZCBOR_LARGE_ELEM_COUNT (ZCBOR_MAX_ELEM_COUNT - 16)


/** Values defined by RFC8949 via www.iana.org/assignments/cbor-tags/cbor-tags.xhtml */
enum zcbor_rfc8949_tag {
	ZCBOR_TAG_TIME_TSTR =       0, ///! text string       Standard date/time string
	ZCBOR_TAG_TIME_NUM =        1, ///! integer or float  Epoch-based date/time
	ZCBOR_TAG_UBIGNUM_BSTR =    2, ///! byte string       Unsigned bignum
	ZCBOR_TAG_BIGNUM_BSTR =     3, ///! byte string       Negative bignum
	ZCBOR_TAG_DECFRAC_ARR =     4, ///! array             Decimal fraction
	ZCBOR_TAG_BIGFLOAT_ARR =    5, ///! array             Bigfloat
	ZCBOR_TAG_2BASE64URL =     21, ///! (any)             Expected conversion to base64url encoding
	ZCBOR_TAG_2BASE64 =        22, ///! (any)             Expected conversion to base64 encoding
	ZCBOR_TAG_2BASE16 =        23, ///! (any)             Expected conversion to base16 encoding
	ZCBOR_TAG_BSTR =           24, ///! byte string       Encoded CBOR data item
	ZCBOR_TAG_URI_TSTR =       32, ///! text string       URI
	ZCBOR_TAG_BASE64URL_TSTR = 33, ///! text string       base64url
	ZCBOR_TAG_BASE64_TSTR =    34, ///! text string       base64
	ZCBOR_TAG_MIME_TSTR =      36, ///! text string       MIME message
	ZCBOR_TAG_CBOR =        55799, ///! (any)             Self-described CBOR
};
#define ZCBOR_LARGE_ELEM_COUNT (ZCBOR_MAX_ELEM_COUNT - 15)


/** Take a backup of the current state. Overwrite the current elem_count. */
bool zcbor_new_backup(zcbor_state_t *state, uint_fast32_t new_elem_count);
bool zcbor_new_backup(zcbor_state_t *state, size_t new_elem_count);

/** Consult the most recent backup. In doing so, check whether elem_count is
 *  less than or equal to max_elem_count.
 *  Also, take action based on the flags (See ZCBOR_FLAG_*).
 */
bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags, uint_fast32_t max_elem_count);
bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags, size_t max_elem_count);

/** Convenience function for starting encoding/decoding of a union.
 *
@@ -294,14 +291,27 @@ bool zcbor_union_end_code(zcbor_state_t *state);
 *  If there is no struct zcbor_state_constant (n_states == 1), error codes are
 *  not available.
 *  This means that you get a state with (n_states - 2) backups.
 *  payload, payload_len, and elem_count are used to initialize the first state.
 *  in the array, which is the state that can be passed to cbor functions.
 *  payload, payload_len, elem_count, and elem_state are used to initialize the first state.
 *  The elem_state is only needed for unordered maps, when ZCBOR_MAP_SMART_SEARCH is enabled.
 *  It is ignored otherwise.
 */
void zcbor_new_state(zcbor_state_t *state_array, size_t n_states,
		const uint8_t *payload, size_t payload_len, size_t elem_count,
		uint8_t *elem_state, size_t elem_state_bytes);

/** Do boilerplate entry function procedure.
 *  Initialize states, call function, and check the result.
 */
void zcbor_new_state(zcbor_state_t *state_array, uint_fast32_t n_states,
		const uint8_t *payload, size_t payload_len, uint_fast32_t elem_count);
int zcbor_entry_function(const uint8_t *payload, size_t payload_len,
	void *result, size_t *payload_len_out, zcbor_state_t *state, zcbor_decoder_t func,
	size_t n_states, size_t elem_count);

#ifdef ZCBOR_STOP_ON_ERROR
/** Check stored error and fail if present, but only if stop_on_error is true. */
/** Check stored error and fail if present, but only if stop_on_error is true.
 *
 * @retval true   No error found
 * @retval false  An error was found
 */
static inline bool zcbor_check_error(const zcbor_state_t *state)
{
	struct zcbor_state_constant *cs = state->constant_state;
@@ -355,9 +365,9 @@ static inline bool zcbor_payload_at_end(const zcbor_state_t *state)
 *  For use when the payload is divided into multiple chunks.
 *
 *  This function also updates all backups to the new payload_end.
 *  This sets a flag so that if a backup is processed with the flag
 *  @ref ZCBOR_FLAG_RESTORE, but without the flag
 *  @ref ZCBOR_FLAG_TRANSFER_PAYLOAD since this would cause an invalid state.
 *  This sets a flag so that @ref zcbor_process_backup fails if a backup is
 *  processed with the flag @ref ZCBOR_FLAG_RESTORE, but without the flag
 *  @ref ZCBOR_FLAG_KEEP_PAYLOAD since this would cause an invalid state.
 *
 *  @param[inout]  state              The current state, will be updated with
 *                                    the new payload pointer.
@@ -382,7 +392,7 @@ void zcbor_update_state(zcbor_state_t *state,
 *                  found, or if any fragment value is NULL.
 */
bool zcbor_validate_string_fragments(struct zcbor_string_fragment *fragments,
		uint_fast32_t num_fragments);
		size_t num_fragments);

/** Assemble the fragments into a single string.
 *
@@ -401,7 +411,77 @@ bool zcbor_validate_string_fragments(struct zcbor_string_fragment *fragments,
 *                  The buffer might still be written to.
 */
bool zcbor_splice_string_fragments(struct zcbor_string_fragment *fragments,
		uint_fast32_t num_fragments, uint8_t *result, size_t *result_len);
		size_t num_fragments, uint8_t *result, size_t *result_len);

/** Compare two struct zcbor_string instances.
 *
 *  @param[in] str1  A string
 *  @param[in] str2  A string to compare to @p str1
 *
 *  @retval true   if the strings are identical
 *  @retval false  if length or contents don't match, or one one or both strings is NULL.
 */
bool zcbor_compare_strings(const struct zcbor_string *str1,
		const struct zcbor_string *str2);

/** Calculate the length of a CBOR string, list, or map header.
 *
 *  This can be used to find the start of the CBOR object when you have a
 *  pointer to the start of the contents. The function assumes that the header
 *  will be the shortest it can be.
 *
 *  @param[in] num_elems  The number of elements in the string, list, or map.
 *
 *  @return  The length of the header in bytes (1-9).
 */
size_t zcbor_header_len(uint64_t value);

/** Like @ref zcbor_header_len but for integer of any size <= 8. */
size_t zcbor_header_len_ptr(const void *const value, size_t value_len);

/** Convert a float16 value to float32.
 *
 *  @param[in] input  The float16 value stored in a uint16_t.
 *
 *  @return  The resulting float32 value.
 */
float zcbor_float16_to_32(uint16_t input);

/** Convert a float32 value to float16.
 *
 *  @param[in] input  The float32 value.
 *
 *  @return  The resulting float16 value as a uint16_t.
 */
uint16_t zcbor_float32_to_16(float input);

#ifdef ZCBOR_MAP_SMART_SEARCH
static inline size_t zcbor_round_up(size_t x, size_t align)
{
	return (((x) + (align) - 1) / (align) * (align));
}

#define ZCBOR_BITS_PER_BYTE 8
/** Calculate the number of bytes needed to hold @p num_flags 1 bit flags
 */
static inline size_t zcbor_flags_to_bytes(size_t num_flags)
{
	return zcbor_round_up(num_flags, ZCBOR_BITS_PER_BYTE) / ZCBOR_BITS_PER_BYTE;
}

/** Calculate the number of zcbor_state_t instances needed to hold @p num_flags 1 bit flags
 */
static inline size_t zcbor_flags_to_states(size_t num_flags)
{
	return zcbor_round_up(num_flags, sizeof(zcbor_state_t) * ZCBOR_BITS_PER_BYTE)
			/ (sizeof(zcbor_state_t) * ZCBOR_BITS_PER_BYTE);
}

#define ZCBOR_FLAG_STATES(n_flags) zcbor_flags_to_states(n_flags)

#else
#define ZCBOR_FLAG_STATES(n_flags) 0
#endif

#ifdef __cplusplus
}

boot/zcbor/include/zcbor_debug.h

deleted100644 → 0
+0 −69
Changes for boot/zcbor/include/zcbor_debug.h: 0 added lines, 69 removed lines.
Original line number Diff line number Diff line
/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef ZCBOR_DEBUG_H__
#define ZCBOR_DEBUG_H__

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "zcbor_common.h"

#ifdef __cplusplus
extern "C" {
#endif

__attribute__((used))
static void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, uint32_t size)
{
	for (uint32_t j = 0; j < size; j++) {
		printk ("%x ", str1[j]);
	}
	printk("\r\n");
	for (uint32_t j = 0; j < size; j++) {
		printk ("%x ", str2[j]);
	}
	printk("\r\n");
	for (uint32_t j = 0; j < size; j++) {
		printk ("%x ", str1[j] != str2[j]);
	}
	printk("\r\n");
	printk("\r\n");
}

__attribute__((used))
static void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, uint32_t size)
{
	for (uint32_t i = 0; i <= size / 16; i++) {
		printk("line %d (char %d)\r\n", i, i*16);
		zcbor_print_compare_lines(&str1[i*16], &str2[i*16],
			MIN(16, (size - i*16)));
	}
	printk("\r\n");
}

__attribute__((used))
static void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, uint32_t size)
{
	bool printed = false;
	for (uint32_t i = 0; i <= size / 16; i++) {
		if (memcmp(&str1[i*16], &str2[i*16], MIN(16, (size - i*16)) != 0)) {
			printk("line %d (char %d)\r\n", i, i*16);
			zcbor_print_compare_lines(&str1[i*16], &str2[i*16],
				MIN(16, (size - i*16)));
			printed = true;
		}
	}
	if (printed) {
		printk("\r\n");
	}
}

#ifdef __cplusplus
}
#endif

#endif /* ZCBOR_DEBUG_H__ */
+290 −193

File changed.

Preview size limit exceeded, changes collapsed.

+103 −155

File changed.

Preview size limit exceeded, changes collapsed.

+161 −0
Changes for boot/zcbor/include/zcbor_print.h: 161 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * Copyright (c) 2023 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef ZCBOR_PRINT_H__
#define ZCBOR_PRINT_H__


#ifdef __cplusplus
extern "C" {
#endif

#ifndef ZCBOR_PRINT_FUNC
#include <stdio.h>
#define zcbor_do_print(...) printf(__VA_ARGS__)
#else
#define zcbor_do_print(...) ZCBOR_PRINT_FUNC(__VA_ARGS__)
#endif

#ifdef ZCBOR_VERBOSE
#define zcbor_trace_raw(state) (zcbor_do_print("rem: %zu, cur: 0x%x, ec: 0x%zx, err: %d",\
	(size_t)state->payload_end - (size_t)state->payload, *state->payload, state->elem_count, \
	state->constant_state ? state->constant_state->error : 0))
#define zcbor_trace(state, appendix) do { \
	zcbor_trace_raw(state); \
	zcbor_do_print(", %s\n", appendix); \
} while(0)
#define zcbor_trace_file(state) do { \
	zcbor_trace_raw(state); \
	zcbor_do_print(", %s:%d\n", __FILE__, __LINE__); \
} while(0)

#define zcbor_log_assert(expr, ...) \
do { \
	zcbor_do_print("ASSERTION \n  \"" #expr \
		"\"\nfailed at %s:%d with message:\n  ", \
		__FILE__, __LINE__); \
	zcbor_do_print(__VA_ARGS__);\
} while(0)
#define zcbor_log(...) zcbor_do_print(__VA_ARGS__)
#else
#define zcbor_trace(state, appendix)
#define zcbor_trace_file(state) ((void)state)
#define zcbor_log_assert(...)
#define zcbor_log(...)
#endif

#ifdef ZCBOR_ASSERTS
#define zcbor_assert(expr, ...) \
do { \
	if (!(expr)) { \
		zcbor_log_assert(expr, __VA_ARGS__); \
		ZCBOR_FAIL(); \
	} \
} while(0)
#define zcbor_assert_state(expr, ...) \
do { \
	if (!(expr)) { \
		zcbor_log_assert(expr, __VA_ARGS__); \
		ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \
	} \
} while(0)
#else
#define zcbor_assert(expr, ...)
#define zcbor_assert_state(expr, ...)
#endif

__attribute__((used))
static void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, uint32_t size)
{
	for (uint32_t j = 0; j < size; j++) {
		zcbor_do_print("%x ", str1[j]);
	}
	zcbor_do_print("\r\n");
	for (uint32_t j = 0; j < size; j++) {
		zcbor_do_print("%x ", str2[j]);
	}
	zcbor_do_print("\r\n");
	for (uint32_t j = 0; j < size; j++) {
		zcbor_do_print("%x ", str1[j] != str2[j]);
	}
	zcbor_do_print("\r\n");
	zcbor_do_print("\r\n");
}

__attribute__((used))
static void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, uint32_t size)
{
	for (uint32_t i = 0; i <= size / 16; i++) {
		zcbor_do_print("line %d (char %d)\r\n", i, i*16);
		zcbor_print_compare_lines(&str1[i*16], &str2[i*16],
			MIN(16, (size - i*16)));
	}
	zcbor_do_print("\r\n");
}

__attribute__((used))
static void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, uint32_t size)
{
	bool printed = false;
	for (uint32_t i = 0; i <= size / 16; i++) {
		if (memcmp(&str1[i*16], &str2[i*16], MIN(16, (size - i*16))) != 0) {
			zcbor_do_print("line %d (char %d)\r\n", i, i*16);
			zcbor_print_compare_lines(&str1[i*16], &str2[i*16],
				MIN(16, (size - i*16)));
			printed = true;
		}
	}
	if (printed) {
		zcbor_do_print("\r\n");
	}
}

__attribute__((used))
static const char *zcbor_error_str(int error)
{
	#define ZCBOR_ERR_CASE(err) case err: \
		return #err; /* The literal is static per C99 6.4.5 paragraph 5. */\

	switch(error) {
		ZCBOR_ERR_CASE(ZCBOR_SUCCESS)
		ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_MEM)
		ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_ACTIVE)
		ZCBOR_ERR_CASE(ZCBOR_ERR_LOW_ELEM_COUNT)
		ZCBOR_ERR_CASE(ZCBOR_ERR_HIGH_ELEM_COUNT)
		ZCBOR_ERR_CASE(ZCBOR_ERR_INT_SIZE)
		ZCBOR_ERR_CASE(ZCBOR_ERR_FLOAT_SIZE)
		ZCBOR_ERR_CASE(ZCBOR_ERR_ADDITIONAL_INVAL)
		ZCBOR_ERR_CASE(ZCBOR_ERR_NO_PAYLOAD)
		ZCBOR_ERR_CASE(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED)
		ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_TYPE)
		ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_VALUE)
		ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_RANGE)
		ZCBOR_ERR_CASE(ZCBOR_ERR_ITERATIONS)
		ZCBOR_ERR_CASE(ZCBOR_ERR_ASSERTION)
		ZCBOR_ERR_CASE(ZCBOR_ERR_PAYLOAD_OUTDATED)
		ZCBOR_ERR_CASE(ZCBOR_ERR_ELEM_NOT_FOUND)
		ZCBOR_ERR_CASE(ZCBOR_ERR_MAP_MISALIGNED)
		ZCBOR_ERR_CASE(ZCBOR_ERR_ELEMS_NOT_PROCESSED)
		ZCBOR_ERR_CASE(ZCBOR_ERR_NOT_AT_END)
		ZCBOR_ERR_CASE(ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE)
		ZCBOR_ERR_CASE(ZCBOR_ERR_INVALID_VALUE_ENCODING)
	}
	#undef ZCBOR_ERR_CASE

	return "ZCBOR_ERR_UNKNOWN";
}

__attribute__((used))
static void zcbor_print_error(int error)
{
	zcbor_do_print("%s\r\n", zcbor_error_str(error));
}

#ifdef __cplusplus
}
#endif

#endif /* ZCBOR_PRINT_H__ */
Loading