Branch data Line data Source code
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 : :
13 : 6 : bool c_x_is_encoded_int(const struct byte_array *c_i)
14 : : {
15 [ + - + - ]: 6 : if (c_i->len == 1 && ((0x00 <= c_i->ptr[0] && c_i->ptr[0] < 0x18) ||
16 [ + + + - ]: 6 : (0x1F < c_i->ptr[0] && c_i->ptr[0] <= 0x37))) {
17 : 5 : return true;
18 : : } else {
19 : 1 : return false;
20 : : }
21 : : }
22 : :
23 : :
24 : 6 : bool c_r_is_raw_int(const struct byte_array *c_r)
25 : : {
26 [ + - - + ]: 6 : if (c_r->len == 1 && c_r->ptr[0] >= -24 && c_r->ptr[0] <= 23) {
27 : 0 : return true;
28 : : } else {
29 : 6 : return false;
30 : : }
31 : : }
32 : :
33 : :
34 : 2 : enum err encode_int(const int32_t *in, uint32_t in_len, struct byte_array *out)
35 : : {
36 : : size_t payload_len_out;
37 [ - + ]: 2 : TRY_EXPECT(cbor_encode_int_type_i(out->ptr, out->len, in,
38 : : &payload_len_out),
39 : : 0);
40 : 2 : out->len = (uint32_t)payload_len_out;
41 : 2 : return ok;
42 : : }
43 : :
44 : 3 : enum err decode_int(const struct byte_array *in, int32_t *out)
45 : : {
46 : 3 : size_t decode_len = 0;
47 [ - + ]: 3 : TRY_EXPECT(cbor_decode_int_type_i(in->ptr, in->len, out, &decode_len),
48 : : 0);
49 [ - + ]: 3 : if (decode_len != 1) {
50 : 0 : return cbor_decoding_error;
51 : : }
52 : 3 : return ok;
53 : : }
|