1 /*
2  * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef __ITS_UTILS_H__
9 #define __ITS_UTILS_H__
10 
11 #include <stddef.h>
12 #include <stdint.h>
13 
14 #include "psa/error.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define ITS_FILE_ID_SIZE 12
21 #define ITS_DATA_SIZE_FIELD_SIZE 4
22 #define ITS_FLAG_SIZE 4
23 #define ITS_DEFAULT_EMPTY_BUFF_VAL 0
24 
25 /**
26  * \brief Macro to check, at compilation time, if data fits in data buffer
27  *
28  * \param[in] err_msg        Error message which will be displayed in first
29  *                           instance if the error is triggered
30  * \param[in] data_size      Data size to check if it fits
31  * \param[in] data_buf_size  Size of the data buffer
32  *
33  * \return  Triggers a compilation error if data_size is bigger than
34  *          data_buf_size. The compilation error should be
35  *          "... error: 'err_msg' declared as an array with a negative size"
36  */
37 #define ITS_UTILS_BOUND_CHECK(err_msg, data_size, data_buf_size) \
38 typedef char err_msg[(data_size <= data_buf_size)*2 - 1]
39 
40 /**
41  * \brief Evaluates to the minimum of the two parameters.
42  */
43 #define ITS_UTILS_MIN(x, y) (((x) < (y)) ? (x) : (y))
44 
45 /**
46  * \brief Evaluates to the maximum of the two parameters.
47  */
48 #define ITS_UTILS_MAX(x, y) (((x) > (y)) ? (x) : (y))
49 
50 /**
51  * \brief Aligns a value up to the provided alignment.
52  *
53  * \param[in] x  Value to be aligned
54  * \param[in] a  Alignment (must be a power of two)
55  *
56  * \return The least value not less than \p x that is aligned to \p a.
57  */
58 #define ITS_UTILS_ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
59 
60 /**
61  * \brief Checks that a value is aligned to the provided alignment.
62  *
63  * \param[in] x  Value to check for alignment
64  * \param[in] a  Alignment (must be a power of two)
65  *
66  * \return 1 if \p x is aligned to \p a, 0 otherwise.
67  */
68 #define ITS_UTILS_IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
69 
70 /**
71  * \brief Checks if a subset region is fully contained within a superset region.
72  *
73  * \param[in] superset_size  Size of superset region
74  * \param[in] subset_offset  Offset of start of subset region from start of
75  *                           superset region
76  * \param[in] subset_size    Size of subset region
77  *
78  * \return Returns error code as specified in \ref psa_status_t
79  *
80  * \retval PSA_SUCCESS                 The subset is contained within the
81  *                                     superset
82  * \retval PSA_ERROR_INVALID_ARGUMENT  Otherwise
83  */
84 psa_status_t its_utils_check_contained_in(size_t superset_size,
85                                           size_t subset_offset,
86                                           size_t subset_size);
87 
88 /**
89  * \brief Validates file ID
90  *
91  * \param[in] fid  File ID
92  *
93  * \return Returns error code as specified in \ref psa_status_t
94  */
95 psa_status_t its_utils_validate_fid(const uint8_t *fid);
96 
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 #endif /* __ITS_UTILS_H__ */
102