core-legacy/util.h
changeset 545 07e5df11f9fc
parent 541 35a8cda28485
child 580 783e91c5f445
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core-legacy/util.h	Fri Nov 14 05:02:00 2008 -0600
     1.3 @@ -0,0 +1,130 @@
     1.4 +/* ======== SourceMM ========
     1.5 + * Copyright (C) 2004-2008 Metamod:Source Development Team
     1.6 + * No warranties of any kind
     1.7 + *
     1.8 + * License: zlib/libpng
     1.9 + *
    1.10 + * Author(s): David "BAILOPAN" Anderson
    1.11 + * ============================
    1.12 + */
    1.13 +
    1.14 +#ifndef _INCLUDE_UTIL_H
    1.15 +#define _INCLUDE_UTIL_H
    1.16 +
    1.17 +#include <stdarg.h>
    1.18 +#include <sourcehook/sourcehook.h>
    1.19 +
    1.20 +/**
    1.21 + * @brief Utility functions
    1.22 + * @file util.h
    1.23 + */
    1.24 +
    1.25 +#define IA32_JMP_IMM32 '\xE9'
    1.26 +
    1.27 +/**
    1.28 + * @brief Returns a pointer to the extension in a file name.
    1.29 + */
    1.30 +const char *UTIL_GetExtension(const char *file);
    1.31 +
    1.32 +/**
    1.33 + * @brief Removes C-style comments from string.
    1.34 + */
    1.35 +void UTIL_TrimComments(char *buffer);
    1.36 +
    1.37 +/**
    1.38 + * @brief Removes whitespace characters from left side of string.
    1.39 + */
    1.40 +void UTIL_TrimLeft(char *buffer);
    1.41 +
    1.42 +/**
    1.43 + * @brief Removes whitespace characters from right side of string.
    1.44 + */
    1.45 +void UTIL_TrimRight(char *buffer);
    1.46 +
    1.47 +/**
    1.48 + * @brief Breaks a string at the first space until it reaches a non-space.
    1.49 + */
    1.50 +void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2);
    1.51 +
    1.52 +/**
    1.53 + * @brief Compares two file paths.
    1.54 + */
    1.55 +bool UTIL_PathCmp(const char *path1, const char *path2);
    1.56 +
    1.57 +/**
    1.58 + * @brief Same as snprintf except that it ensures the string buffer is null terminated.
    1.59 + */
    1.60 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    1.61 +
    1.62 +/**
    1.63 + * @brief Same as vsnprintf except that it ensures the string buffer is null terminated.
    1.64 + */
    1.65 +size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params);
    1.66 +
    1.67 +/**
    1.68 + * @brief Forms a relative path given two absolute paths.
    1.69 + *
    1.70 + * @param buffer		Buffer to store relative path in.
    1.71 + * @param maxlength		Maximum length of the output buffer.
    1.72 + * @param relTo			Destination folder to use as a working directory.
    1.73 + *						Final folder name should not be pathchar-terminated.
    1.74 + * @param relFrom		Source file or folder to use as a target.
    1.75 + * @return				True on success, false on failure.
    1.76 + */
    1.77 +bool UTIL_Relatize(char buffer[],
    1.78 +				   size_t maxlength,
    1.79 +				   const char *relTo,
    1.80 +				   const char *relFrom);
    1.81 +
    1.82 +/**
    1.83 + * @brief Compares memory address against a signature.
    1.84 + *
    1.85 + * @param addr			Memory address to check.
    1.86 + * @param sig			Signature used to check against memory address. Accept 0x2A as wildcard.
    1.87 + * @param len			Length of signature.
    1.88 + * @return				True if signature was verified, false otherwise.
    1.89 + */
    1.90 +bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len);
    1.91 +
    1.92 +/**
    1.93 + * @brief Returns the original function address of a given virtual function.
    1.94 + *
    1.95 + * @param mfp			Member function pointer to virtual function.
    1.96 + * @param ptr			Pointer to interface in which the virtual function belongs.
    1.97 + * @param cls			A CallClass for the interface in which the virtual function belongs.
    1.98 + * @return				Address of function originally pointed to by the virtual function.
    1.99 + */
   1.100 +template <class MFP, class Iface>
   1.101 +char *UTIL_GetOrigFunction(MFP vfunc, Iface *ptr, SourceHook::CallClass<Iface> *cls)
   1.102 +{
   1.103 +	SourceHook::MemFuncInfo info = {true, -1, 0, 0};
   1.104 +	SourceHook::GetFuncInfo(vfunc, info);
   1.105 +
   1.106 +	/* Get address of original GetUserMessageInfo() */
   1.107 +	char *func = reinterpret_cast<char *>(cls->GetOrigFunc(info.vtbloffs, info.vtblindex));
   1.108 +
   1.109 +	/* If we can't get original function, that means there's no hook */
   1.110 +	if (func == NULL)
   1.111 +	{
   1.112 +		/* Get virtual function address 'manually' then */
   1.113 +		char *adjustedptr = reinterpret_cast<char *>(ptr) + info.vtbloffs + info.vtbloffs;
   1.114 +		char **vtable = *reinterpret_cast<char ***>(adjustedptr);
   1.115 +
   1.116 +		func = vtable[info.vtblindex];
   1.117 +	}
   1.118 +
   1.119 +	/* Check for relative jumps */
   1.120 +	if (func[0] == IA32_JMP_IMM32)
   1.121 +	{
   1.122 +		/* Get address from displacement...
   1.123 +		 *
   1.124 +		 * Add 5 because it's relative to next instruction:
   1.125 +		 * Opcode <1 byte> + 32-bit displacement <4 bytes> 
   1.126 +		 */
   1.127 +		func += *reinterpret_cast<unsigned long *>(func + 1) + 5;
   1.128 +	}
   1.129 +
   1.130 +	return func;
   1.131 +}
   1.132 +
   1.133 +#endif //_INCLUDE_UTIL_H