1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /******************************************************************************* 3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG 4 * All rights reserved. 5 ******************************************************************************/ 6 #ifndef AUX_UTIL_H 7 #define AUX_UTIL_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #define SAFE_FREE(S) if((S) != NULL) {free((void*) (S)); (S)=NULL;} 14 15 #define TPM2_ERROR_FORMAT "%s%s (0x%08x)" 16 #define TPM2_ERROR_TEXT(r) "Error", "Code", r 17 18 #define return_if_error(r,msg) \ 19 if (r != TSS2_RC_SUCCESS) { \ 20 LOG_ERROR("%s " TPM2_ERROR_FORMAT, msg, TPM2_ERROR_TEXT(r)); \ 21 return r; \ 22 } 23 24 #define return_state_if_error(r,s,msg) \ 25 if (r != TSS2_RC_SUCCESS) { \ 26 LOG_ERROR("%s " TPM2_ERROR_FORMAT, msg, TPM2_ERROR_TEXT(r)); \ 27 esysContext->state = s; \ 28 return r; \ 29 } 30 31 #define return_error(r,msg) \ 32 { \ 33 LOG_ERROR("%s " TPM2_ERROR_FORMAT, msg, TPM2_ERROR_TEXT(r)); \ 34 return r; \ 35 } 36 37 #define goto_state_if_error(r,s,msg,label) \ 38 if (r != TSS2_RC_SUCCESS) { \ 39 LOG_ERROR("%s " TPM2_ERROR_FORMAT, msg, TPM2_ERROR_TEXT(r)); \ 40 esysContext->state = s; \ 41 goto label; \ 42 } 43 44 #define goto_if_null(p,msg,ec,label) \ 45 if ((p) == NULL) { \ 46 LOG_ERROR("%s ", (msg)); \ 47 r = (ec); \ 48 goto label; \ 49 } 50 51 #define goto_if_error(r,msg,label) \ 52 if (r != TSS2_RC_SUCCESS) { \ 53 LOG_ERROR("%s " TPM2_ERROR_FORMAT, msg, TPM2_ERROR_TEXT(r)); \ 54 goto label; \ 55 } 56 57 #define goto_error(r,v,msg,label, ...) \ 58 { r = v; \ 59 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(r), ## __VA_ARGS__); \ 60 goto label; \ 61 } 62 63 #define return_if_null(p,msg,ec) \ 64 if (p == NULL) { \ 65 LOG_ERROR("%s ", msg); \ 66 return ec; \ 67 } 68 69 #define return_if_notnull(p,msg,ec) \ 70 if (p != NULL) { \ 71 LOG_ERROR("%s ", msg); \ 72 return ec; \ 73 } 74 75 #define exit_if_error(r,msg) \ 76 if (r != TSS2_RC_SUCCESS) { \ 77 LOG_ERROR("%s " TPM2_ERROR_FORMAT, msg, TPM2_ERROR_TEXT(r)); \ 78 exit(1); \ 79 } 80 81 #define set_return_code(r_max, r, msg) \ 82 if (r != TSS2_RC_SUCCESS) { \ 83 LOG_ERROR("%s " TPM2_ERROR_FORMAT, msg, TPM2_ERROR_TEXT(r)); \ 84 r_max = r; \ 85 } 86 87 #ifdef __cplusplus 88 } /* extern "C" */ 89 #endif 90 91 #endif /* AUX_UTIL_H */ 92