Backed out changeset: ba8cd35102b1 (
bug 5079)
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2008 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
11 #ifndef _INCLUDE_UTIL_H
12 #define _INCLUDE_UTIL_H
15 #include "sourcehook/sourcehook.h"
18 * @brief Utility functions
22 #define IA32_JMP_IMM32 '\xE9'
25 * @brief Returns a pointer to the extension in a file name.
27 const char *UTIL_GetExtension(const char *file);
30 * @brief Removes whitespace characters from left side of string.
32 void UTIL_TrimLeft(char *buffer);
35 * @brief Removes whitespace characters from right side of string.
37 void UTIL_TrimRight(char *buffer);
40 * @brief Compares two file paths.
42 bool UTIL_PathCmp(const char *path1, const char *path2);
45 * @brief Same as snprintf except that it ensures the string buffer is null terminated.
47 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
50 * @brief Same as vsnprintf except that it ensures the string buffer is null terminated.
52 size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params);
55 * @brief Forms a relative path given two absolute paths.
57 * @param buffer Buffer to store relative path in.
58 * @param maxlength Maximum length of the output buffer.
59 * @param relTo Destination folder to use as a working directory.
60 * Final folder name should not be pathchar-terminated.
61 * @param relFrom Source file or folder to use as a target.
62 * @return True on success, false on failure.
64 bool UTIL_Relatize(char buffer[],
70 * @brief Compares memory address against a signature.
72 * @param addr Memory address to check.
73 * @param sig Signature used to check against memory address. Accept 0x2A as wildcard.
74 * @param len Length of signature.
75 * @return True if signature was verified, false otherwise.
77 bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len);
80 * @brief Returns the original function address of a given virtual function.
82 * @param mfp Member function pointer to virtual function.
83 * @param ptr Pointer to interface in which the virtual function belongs.
84 * @return Address of function originally pointed to by the virtual function.
86 template <class MFP, class Iface>
87 char *UTIL_GetOrigFunction(MFP vfunc, Iface *ptr)
89 SourceHook::MemFuncInfo info = {true, -1, 0, 0};
90 SourceHook::GetFuncInfo(vfunc, info);
92 /* Get address of original GetUserMessageInfo() */
93 char *func = reinterpret_cast<char *>(SH_GET_ORIG_VFNPTR_ENTRY(ptr, vfunc));
95 /* Check for relative jumps */
96 if (func[0] == IA32_JMP_IMM32)
98 /* Get address from displacement...
100 * Add 5 because it's relative to next instruction:
101 * Opcode <1 byte> + 32-bit displacement <4 bytes>
103 func += *reinterpret_cast<unsigned long *>(func + 1) + 5;
109 #endif //_INCLUDE_UTIL_H