1 /*
2    Copyright (c) 2021 Fraunhofer AISEC. See the COPYRIGHT
3    file at the top-level directory of this distribution.
4 
5    Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6    http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7    <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8    option. This file may not be copied, modified, or distributed
9    except according to those terms.
10 */
11 
12 #ifndef BYTE_ARRAY_H
13 #define BYTE_ARRAY_H
14 
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <stdint.h>
18 
19 #include "oscore_edhoc_error.h"
20 #include "memcpy_s.h"
21 
22 /* Array with pointer and length.*/
23 struct byte_array {
24 	uint32_t len;
25 	uint8_t *ptr;
26 };
27 
28 struct const_byte_array {
29 	uint32_t len;
30 	const uint8_t *ptr;
31 };
32 
33 /* Empty Array with len=0 but with a non-null pointer.*/
34 extern struct byte_array EMPTY_ARRAY;
35 
36 /* Null Array with len=0 and a null pointer.*/
37 extern struct byte_array NULL_ARRAY;
38 
39 /**
40  * @brief			Compares if the given two arrays have equal
41  * 				content.
42  *
43  * @param[in] a 		Array "a".
44  * @param[in] b 		Array "b".
45  * @return  			True if the contents of both arrays is equal.
46  */
47 bool array_equals(const struct byte_array *a, const struct byte_array *b);
48 
49 /**
50  * @brief 			Creates a copy of a byte array.
51  *
52  * @param[out] dest 		The destination byte array.
53  * @param[in] src		The source byte array.
54  * @param dest_max_len 		The maximal length of the destination array.
55  * @return enum err 		Ok or error code.
56  */
57 enum err byte_array_cpy(struct byte_array *dest, const struct byte_array *src,
58 			const uint32_t dest_max_len);
59 
60 /**
61  * @brief   			Initializes a byte array variable with a
62  * 				pointer to a buffer and length of the buffer.
63  *
64  * @param PTR			pointer
65  * @param LEN			Length of the buffer in bytes
66  */
67 #define BYTE_ARRAY_INIT(PTR, LEN)                                              \
68 	{                                                                      \
69 		.len = LEN, .ptr = PTR                                         \
70 	}
71 
72 /**
73  * @brief   Creates a variable of type byte_array.
74  *          In addition a buffer is created to hold the data.
75  *          If Variable Length Array (VLA) is NOT used, before the creation of
76  *          the buffer it is checked if the size of the buffer (BUF_SIZE) will
77  *          be sufficient for the size of the byte_array (SIZE).
78 */
79 #ifdef VLA
80 #define BYTE_ARRAY_NEW(NAME, BUF_SIZE, SIZE)                                   \
81 	if (SIZE < 0 || SIZE > BUF_SIZE) {                                     \
82 		return vla_insufficient_size;                                  \
83 	}                                                                      \
84 	struct byte_array NAME;                                                \
85 	uint8_t NAME##_buf[SIZE];                                              \
86 	if (SIZE == 0) {                                                       \
87 		NAME = NULL_ARRAY;                                             \
88 	} else {                                                               \
89 		NAME.ptr = NAME##_buf;                                         \
90 		NAME.len = SIZE;                                               \
91 	};
92 
93 #else
94 #define BYTE_ARRAY_NEW(NAME, BUF_SIZE, SIZE)                                   \
95 	TRY(check_buffer_size(BUF_SIZE, SIZE));                                \
96 	struct byte_array NAME;                                                \
97 	uint8_t NAME##_buf[BUF_SIZE];                                          \
98 	if (SIZE == 0) {                                                       \
99 		NAME = NULL_ARRAY;                                             \
100 	} else {                                                               \
101 		NAME.ptr = NAME##_buf;                                         \
102 		NAME.len = SIZE;                                               \
103 	};
104 #endif
105 
106 #endif //BYTE_ARRAY_H
107