core-legacy/util.h
author Asher Baker <asherkin@limetech.org>
Sun May 27 02:44:19 2012 +0100 (2012-05-27)
changeset 785 11aa8bb24378
parent 661 952a16ae1d2b
permissions -rw-r--r--
Backed out changeset: ba8cd35102b1 (bug 5079)
     1 /* ======== SourceMM ========
     2  * Copyright (C) 2004-2008 Metamod:Source Development Team
     3  * No warranties of any kind
     4  *
     5  * License: zlib/libpng
     6  *
     7  * Author(s): David "BAILOPAN" Anderson
     8  * ============================
     9  */
    10 
    11 #ifndef _INCLUDE_UTIL_H
    12 #define _INCLUDE_UTIL_H
    13 
    14 #include <stdarg.h>
    15 #include "sourcehook/sourcehook.h"
    16 
    17 /**
    18  * @brief Utility functions
    19  * @file util.h
    20  */
    21 
    22 #define IA32_JMP_IMM32 '\xE9'
    23 
    24 /**
    25  * @brief Returns a pointer to the extension in a file name.
    26  */
    27 const char *UTIL_GetExtension(const char *file);
    28 
    29 /**
    30  * @brief Removes whitespace characters from left side of string.
    31  */
    32 void UTIL_TrimLeft(char *buffer);
    33 
    34 /**
    35  * @brief Removes whitespace characters from right side of string.
    36  */
    37 void UTIL_TrimRight(char *buffer);
    38 
    39 /**
    40  * @brief Compares two file paths.
    41  */
    42 bool UTIL_PathCmp(const char *path1, const char *path2);
    43 
    44 /**
    45  * @brief Same as snprintf except that it ensures the string buffer is null terminated.
    46  */
    47 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    48 
    49 /**
    50  * @brief Same as vsnprintf except that it ensures the string buffer is null terminated.
    51  */
    52 size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params);
    53 
    54 /**
    55  * @brief Forms a relative path given two absolute paths.
    56  *
    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.
    63  */
    64 bool UTIL_Relatize(char buffer[],
    65 				   size_t maxlength,
    66 				   const char *relTo,
    67 				   const char *relFrom);
    68 
    69 /**
    70  * @brief Compares memory address against a signature.
    71  *
    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.
    76  */
    77 bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len);
    78 
    79 /**
    80  * @brief Returns the original function address of a given virtual function.
    81  *
    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.
    85  */
    86 template <class MFP, class Iface>
    87 char *UTIL_GetOrigFunction(MFP vfunc, Iface *ptr)
    88 {
    89 	SourceHook::MemFuncInfo info = {true, -1, 0, 0};
    90 	SourceHook::GetFuncInfo(vfunc, info);
    91 
    92 	/* Get address of original GetUserMessageInfo() */
    93 	char *func = reinterpret_cast<char *>(SH_GET_ORIG_VFNPTR_ENTRY(ptr, vfunc));
    94 
    95 	/* Check for relative jumps */
    96 	if (func[0] == IA32_JMP_IMM32)
    97 	{
    98 		/* Get address from displacement...
    99 		 *
   100 		 * Add 5 because it's relative to next instruction:
   101 		 * Opcode <1 byte> + 32-bit displacement <4 bytes> 
   102 		 */
   103 		func += *reinterpret_cast<unsigned long *>(func + 1) + 5;
   104 	}
   105 
   106 	return func;
   107 }
   108 
   109 #endif //_INCLUDE_UTIL_H