1 /*
2  * Copyright (c) 2022 Eriptic Technologies.
3  *
4  * SPDX-License-Identifier: Apache-2.0 or MIT
5  */
6 
7 #include "common/oscore_edhoc_error.h"
8 #include "common/byte_array.h"
9 
10 #include "cbor/edhoc_encode_int_type.h"
11 #include "cbor/edhoc_decode_int_type.h"
12 
c_x_is_encoded_int(const struct byte_array * c_i)13 bool c_x_is_encoded_int(const struct byte_array *c_i)
14 {
15 	if (c_i->len == 1 && ((0x00 <= c_i->ptr[0] && c_i->ptr[0] < 0x18) ||
16 			      (0x1F < c_i->ptr[0] && c_i->ptr[0] <= 0x37))) {
17 		return true;
18 	} else {
19 		return false;
20 	}
21 }
22 
23 
c_r_is_raw_int(const struct byte_array * c_r)24 bool c_r_is_raw_int(const struct byte_array *c_r)
25 {
26 	if (c_r->len == 1 && c_r->ptr[0] >= -24 && c_r->ptr[0] <= 23) {
27 		return true;
28 	} else {
29 		return false;
30 	}
31 }
32 
33 
encode_int(const int32_t * in,uint32_t in_len,struct byte_array * out)34 enum err encode_int(const int32_t *in, uint32_t in_len, struct byte_array *out)
35 {
36 	size_t payload_len_out;
37 	TRY_EXPECT(cbor_encode_int_type_i(out->ptr, out->len, in,
38 					  &payload_len_out),
39 		   0);
40 	out->len = (uint32_t)payload_len_out;
41 	return ok;
42 }
43 
decode_int(const struct byte_array * in,int32_t * out)44 enum err decode_int(const struct byte_array *in, int32_t *out)
45 {
46 	size_t decode_len = 0;
47 	TRY_EXPECT(cbor_decode_int_type_i(in->ptr, in->len, out, &decode_len),
48 		   0);
49 	if (decode_len != 1) {
50 		return cbor_decoding_error;
51 	}
52 	return ok;
53 }