/src/cjose/include/cjose/error.h
Line | Count | Source |
1 | | /** |
2 | | * \file |
3 | | * \brief |
4 | | * Datatypes and functions for error reporting. |
5 | | * |
6 | | * Copyrights |
7 | | * |
8 | | * Portions created or assigned to Cisco Systems, Inc. are |
9 | | * Copyright (c) 2014-2016 Cisco Systems, Inc. All Rights Reserved. |
10 | | */ |
11 | | #ifndef CJOSE_ERROR_H |
12 | | #define CJOSE_ERROR_H |
13 | | |
14 | | #ifdef __cplusplus |
15 | | extern "C" { |
16 | | #endif |
17 | | |
18 | | /** |
19 | | * Temporarily disable compiler warnings, if possible (>=gcc-4.6). |
20 | | * |
21 | | * In some cases (particularly within macros), certain compiler warnings are |
22 | | * unavoidable. In order to allow these warnings to be treated as errors in |
23 | | * most cases, these macros will disable particular warnings only during |
24 | | * specific points in the compilation. |
25 | | */ |
26 | | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
27 | | #define GCC_END_IGNORED_WARNING _Pragma("GCC diagnostic pop") |
28 | | |
29 | | #define GCC_BEGIN_IGNORED_WARNING_ADDRESS \ |
30 | | _Pragma("GCC diagnostic push"); \ |
31 | | _Pragma("GCC diagnostic ignored \"-Waddress\"") |
32 | | #define GCC_END_IGNORED_WARNING_ADDRESS GCC_END_IGNORED_WARNING |
33 | | #else |
34 | | #define GCC_BEGIN_IGNORED_WARNING_ADDRESS |
35 | | #define GCC_END_IGNORED_WARNING_ADDRESS |
36 | | #endif /* defined(__GNUC__) && (__GNUC__ > 3) && (__GNUC_MINOR__ > 5) */ |
37 | | |
38 | | /** |
39 | | * Enumeration of defined error codes. |
40 | | */ |
41 | | typedef enum |
42 | | { |
43 | | /** No error */ |
44 | | CJOSE_ERR_NONE = 0, |
45 | | |
46 | | /** argument was invalid (beyond invariants) */ |
47 | | CJOSE_ERR_INVALID_ARG, |
48 | | |
49 | | /** context is not in a valid state */ |
50 | | CJOSE_ERR_INVALID_STATE, |
51 | | |
52 | | /** out of memory */ |
53 | | CJOSE_ERR_NO_MEMORY, |
54 | | |
55 | | /** an error returned from the crypto libraries */ |
56 | | CJOSE_ERR_CRYPTO, |
57 | | |
58 | | } cjose_errcode; |
59 | | |
60 | | /** |
61 | | * An instance of an error context. Unlike other structures, it |
62 | | * is the API user's responsibility to allocate the structure; however |
63 | | * the values provided are considered constants, and MUST NOT be |
64 | | * deallocated. |
65 | | */ |
66 | | typedef struct |
67 | | { |
68 | | /** The error code */ |
69 | | cjose_errcode code; |
70 | | |
71 | | /** The human readable message for the error code */ |
72 | | const char *message; |
73 | | |
74 | | /** The function where the error occured, or "<unknown>" |
75 | | if it cannot be determined */ |
76 | | const char *function; |
77 | | |
78 | | /** The file where the error occured */ |
79 | | const char *file; |
80 | | |
81 | | /** The line number in the file where the error occured */ |
82 | | unsigned long line; |
83 | | |
84 | | } cjose_err; |
85 | | |
86 | | /** |
87 | | * Retrieves the error message for the given error code. |
88 | | * |
89 | | * \param code The error code to lookup |
90 | | * \retval const char * The message for {code} |
91 | | */ |
92 | | const char *cjose_err_message(cjose_errcode code); |
93 | | |
94 | | /** |
95 | | * \def CJOSE_ERROR(err, code) |
96 | | * |
97 | | * Macro to initialize an error context. |
98 | | * |
99 | | * \param err The pointer to the error context, or NULL if none |
100 | | * \param errcode The error code |
101 | | */ |
102 | | #define CJOSE_ERROR(err, errcode) \ |
103 | 7.32k | GCC_BEGIN_IGNORED_WARNING_ADDRESS \ |
104 | 7.32k | if ((err) != NULL && (errcode) != CJOSE_ERR_NONE) \ |
105 | 7.32k | { \ |
106 | 7.32k | (err)->code = (errcode); \ |
107 | 7.32k | (err)->message = cjose_err_message((errcode)); \ |
108 | 7.32k | (err)->function = __func__; \ |
109 | 7.32k | (err)->file = __FILE__; \ |
110 | | (err)->line = __LINE__; \ |
111 | 7.32k | } \ |
112 | 0 | GCC_END_IGNORED_WARNING_ADDRESS |
113 | | |
114 | | #ifdef __cplusplus |
115 | | } |
116 | | #endif |
117 | | |
118 | | #endif /* CJOSE_ERROR_H */ |