1 /*
2  * Copyright (c) 2022 Eriptic Technologies.
3  *
4  * SPDX-License-Identifier: Apache-2.0 or MIT
5  */
6 
7 #include "zcbor_common.h"
8 #include "cbor/edhoc_encode_bstr_type.h"
9 #include "cbor/edhoc_decode_bstr_type.h"
10 
11 #include "common/oscore_edhoc_error.h"
12 #include "common/print_util.h"
13 #include "common/memcpy_s.h"
14 #include "common/byte_array.h"
15 
encode_bstr(const struct byte_array * in,struct byte_array * out)16 enum err encode_bstr(const struct byte_array *in, struct byte_array *out)
17 {
18 	size_t payload_len_out;
19 	struct zcbor_string tmp;
20 	tmp.value = in->ptr;
21 	tmp.len = in->len;
22 	TRY_EXPECT(cbor_encode_bstr_type_b_str(out->ptr, out->len, &tmp,
23 					       &payload_len_out),
24 		   0);
25 	out->len = (uint32_t)payload_len_out;
26 	return ok;
27 }
28 
decode_bstr(const struct byte_array * in,struct byte_array * out)29 enum err decode_bstr(const struct byte_array *in, struct byte_array *out)
30 {
31 	struct zcbor_string str;
32 	size_t decode_len = 0;
33 
34 	TRY_EXPECT(cbor_decode_bstr_type_b_str(in->ptr, in->len, &str,
35 					       &decode_len),
36 		   0);
37 
38 	TRY(_memcpy_s(out->ptr, out->len, str.value, (uint32_t)str.len));
39 	out->len = (uint32_t)str.len;
40 
41 	return ok;
42 }
43