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