From dbc12b6fb59433f3153005c62e4a228e87aa9534 Mon Sep 17 00:00:00 2001 From: 8dcc <8dcc.git@gmail.com> Date: Wed, 26 Jul 2023 13:35:11 +0200 Subject: [PATCH] Sync detour.h with upstream Add GET_ORIGINAL() --- src/include/detour.h | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/include/detour.h b/src/include/detour.h index 107336c..be759ac 100644 --- a/src/include/detour.h +++ b/src/include/detour.h @@ -34,15 +34,31 @@ bool detour_del(detour_data_t* d); /*----------------------------------------------------------------------------*/ /* Declare the type for the original function */ -#define DECL_DETOUR_TYPE(funcRet, funcName, ...) \ - typedef funcRet (*funcName##_t)(__VA_ARGS__); +#define DECL_DETOUR_TYPE(funcRet, newTypeName, ...) \ + typedef funcRet (*newTypeName##_t)(__VA_ARGS__); -/* Reset original bytes, call original, detour again. detourData is NOT a ptr */ -#define CALL_ORIGINAL(detourData, funcName, ...) \ +/* Reset original bytes, call original, detour again. + * Keep in mind that: + * - The returned value of the original function is not stored. If the + * function is not void, and you care about the return value, use + * GET_ORIGINAL() instead. + * - detourData is NOT a pointer, it expects the full struct + * - funcType should be the same name passed to DECL_DETOUR_TYPE, without the + * ending added by the macro ("_t") */ +#define CALL_ORIGINAL(detourData, funcType, ...) \ { \ detour_del(&detourData); \ - ((funcName##_t)detourData.orig)(__VA_ARGS__); \ + ((funcType##_t)detourData.orig)(__VA_ARGS__); \ detour_add(&detourData); \ } +/* Same as CALL_ORIGINAL, but accepts an extra parameter for storing the + * returned value of the original function */ +#define GET_ORIGINAL(detourData, returnVar, funcType, ...) \ + { \ + detour_del(&detourData); \ + returnVar = ((funcType##_t)detourData.orig)(__VA_ARGS__); \ + detour_add(&detourData); \ + } + #endif /* DETOUR_H_ */