Branch data Line data Source code
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 : :
27 : 57 : enum err byte_array_append(struct byte_array *dest,
28 : : const struct byte_array *source, uint32_t capacity)
29 : : {
30 [ - + ]: 57 : if (source->len + dest->len > capacity) {
31 : 0 : return buffer_to_small;
32 : : }
33 [ - + ]: 57 : TRY(_memcpy_s(dest->ptr + dest->len, capacity - dest->len, source->ptr,
34 : : source->len));
35 : 57 : dest->len += source->len;
36 : 57 : return ok;
37 : : }
38 : :
39 : 24 : enum err byte_array_cpy(struct byte_array *dest, const struct byte_array *src,
40 : : const uint32_t dest_max_len)
41 : : {
42 [ - + ]: 24 : TRY(_memcpy_s(dest->ptr, dest_max_len, src->ptr, src->len));
43 : 24 : dest->len = src->len;
44 : 24 : return ok;
45 : : }
46 : :
47 : 9 : bool array_equals(const struct byte_array *left, const struct byte_array *right)
48 : : {
49 [ + + ]: 9 : if (left->len != right->len) {
50 : 1 : return false;
51 : : }
52 [ - + ]: 8 : for (uint32_t i = 0; i < left->len; i++) {
53 [ # # ]: 0 : if (left->ptr[i] != right->ptr[i]) {
54 : 0 : return false;
55 : : }
56 : : }
57 : 8 : return true;
58 : : }
|