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 #include "common/byte_array.h"
13 #include "common/memcpy_s.h"
14 #include "common/oscore_edhoc_error.h"
15
16 uint8_t EMPTY_STRING[] = { "" };
17 struct byte_array EMPTY_ARRAY = {
18 .len = 0,
19 .ptr = EMPTY_STRING,
20 };
21
22 struct byte_array NULL_ARRAY = {
23 .len = 0,
24 .ptr = NULL,
25 };
26
byte_array_append(struct byte_array * dest,const struct byte_array * source,uint32_t capacity)27 enum err byte_array_append(struct byte_array *dest,
28 const struct byte_array *source, uint32_t capacity)
29 {
30 if (source->len + dest->len > capacity) {
31 return buffer_to_small;
32 }
33 TRY(_memcpy_s(dest->ptr + dest->len, capacity - dest->len, source->ptr,
34 source->len));
35 dest->len += source->len;
36 return ok;
37 }
38
byte_array_cpy(struct byte_array * dest,const struct byte_array * src,const uint32_t dest_max_len)39 enum err byte_array_cpy(struct byte_array *dest, const struct byte_array *src,
40 const uint32_t dest_max_len)
41 {
42 TRY(_memcpy_s(dest->ptr, dest_max_len, src->ptr, src->len));
43 dest->len = src->len;
44 return ok;
45 }
46
array_equals(const struct byte_array * left,const struct byte_array * right)47 bool array_equals(const struct byte_array *left, const struct byte_array *right)
48 {
49 if (left->len != right->len) {
50 return false;
51 }
52 for (uint32_t i = 0; i < left->len; i++) {
53 if (left->ptr[i] != right->ptr[i]) {
54 return false;
55 }
56 }
57 return true;
58 }
59