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 /* Empty Array with len=0 but with a non-null pointer.*/ 29 extern struct byte_array EMPTY_ARRAY; 30 31 /* Null Array with len=0 and a null pointer.*/ 32 extern struct byte_array NULL_ARRAY; 33 34 /** 35 * @brief Compares if the given two arrays have an equal content. 36 * 37 * Handles null-arrays correctly 38 * @param left first array 39 * @param right second array 40 * @return if the contents of given arrays are equal 41 */ 42 bool array_equals(const struct byte_array *left, 43 const struct byte_array *right); 44 45 enum err byte_array_cpy(struct byte_array *dest, const struct byte_array *src, 46 const uint32_t dest_max_len); 47 48 /** 49 * @brief Sets the pointer and the length of a byte_array variable to a given array 50 */ 51 #define BYTE_ARRAY_INIT(PTR, SIZE) { .ptr = PTR, .len = SIZE } 52 53 /** 54 * @brief Creates a variable of type byte_array. 55 * In addition a buffer is created to hold the data. 56 * Before the creation of the buffer it is checked if the size of the 57 * buffer (BUF_SIZE) will be sufficient for the size of the byte_array 58 * (SIZE). 59 */ 60 #define BYTE_ARRAY_NEW(NAME, BUF_SIZE, SIZE) \ 61 TRY(check_buffer_size(BUF_SIZE, SIZE)); \ 62 uint8_t NAME##_buf[BUF_SIZE]; \ 63 struct byte_array NAME = BYTE_ARRAY_INIT(NAME##_buf, SIZE); 64 65 #endif 66