loader/utility.cpp
author David Anderson <dvander@alliedmods.net>
Wed Nov 26 00:19:30 2008 -0600 (2008-11-26)
changeset 608 30b546cc4e03
parent 586 ed3320c98dd7
child 705 a1001281dcb1
permissions -rw-r--r--
Updated changelog, added license headers.
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     9  * This software is provided 'as-is', without any express or implied warranty.
    10  * In no event will the authors be held liable for any damages arising from 
    11  * the use of this software.
    12  * 
    13  * Permission is granted to anyone to use this software for any purpose, 
    14  * including commercial applications, and to alter it and redistribute it 
    15  * freely, subject to the following restrictions:
    16  *
    17  * 1. The origin of this software must not be misrepresented; you must not 
    18  * claim that you wrote the original software. If you use this software in a 
    19  * product, an acknowledgment in the product documentation would be 
    20  * appreciated but is not required.
    21  * 2. Altered source versions must be plainly marked as such, and must not be
    22  * misrepresented as being the original software.
    23  * 3. This notice may not be removed or altered from any source distribution.
    24  *
    25  * Version: $Id$
    26  */
    27 
    28 #include <stdio.h>
    29 #include <assert.h>
    30 #include <string.h>
    31 #include <ctype.h>
    32 #include <stdarg.h>
    33 #include <stdlib.h>
    34 #include "loader.h"
    35 #include "utility.h"
    36 
    37 #if defined _WIN32
    38 static void
    39 mm_GetPlatformError(char *buffer, size_t maxlength)
    40 {
    41 	DWORD dw = GetLastError();
    42 	FormatMessageA(
    43 		FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
    44 		NULL,
    45 		dw,
    46 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    47 		(LPSTR)buffer,
    48 		maxlength,
    49 		NULL);
    50 }
    51 #endif
    52 
    53 
    54 size_t
    55 mm_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
    56 {
    57 	size_t len = vsnprintf(buffer, maxlength, fmt, params);
    58 
    59 	if (len >= maxlength)
    60 	{
    61 		len = maxlength - 1;
    62 		buffer[len] = '\0';
    63 	}
    64 
    65 	return len;
    66 }
    67 
    68 size_t
    69 mm_Format(char *buffer, size_t maxlength, const char *fmt, ...)
    70 {
    71 	size_t len;
    72 	va_list ap;
    73 
    74 	va_start(ap, fmt);
    75 	len = mm_FormatArgs(buffer, maxlength, fmt, ap);
    76 	va_end(ap);
    77 
    78 	return len;
    79 }
    80 
    81 size_t
    82 mm_PathFormat(char *buffer, size_t maxlen, const char *fmt, ...)
    83 {
    84 	size_t len;
    85 	va_list ap;
    86 
    87 	va_start(ap, fmt);
    88 	len = mm_FormatArgs(buffer, maxlen, fmt, ap);
    89 	va_end(ap);
    90 
    91 	for (size_t i = 0; i < len; i++)
    92 	{
    93 		if (buffer[i] == ALT_SEP_CHAR)
    94 			buffer[i] = PATH_SEP_CHAR;
    95 	}
    96 
    97 	return len;
    98 }
    99 
   100 void
   101 mm_TrimLeft(char *buffer)
   102 {
   103 	/* Let's think of this as our iterator */
   104 	char *i = buffer;
   105 
   106 	/* Make sure the buffer isn't null */
   107 	if (i && *i)
   108 	{
   109 		/* Add up number of whitespace characters */
   110 		while(isspace((unsigned char) *i))
   111 			i++;
   112 
   113 		/* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
   114 		if (i != buffer)
   115 			memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
   116 	}
   117 }
   118 
   119 void
   120 mm_TrimRight(char *buffer)
   121 {
   122 	/* Make sure buffer isn't null */
   123 	if (buffer)
   124 	{
   125 		size_t len = strlen(buffer);
   126 
   127 		/* Loop through buffer backwards while replacing whitespace chars with null chars */
   128 		for (size_t i = len - 1; i >= 0; i--)
   129 		{
   130 			if (isspace((unsigned char) buffer[i]))
   131 				buffer[i] = '\0';
   132 			else
   133 				break;
   134 		}
   135 	}
   136 }
   137 
   138 /* :TODO: this should skip string literals */
   139 void
   140 mm_TrimComments(char *buffer)
   141 {
   142 	int num_sc = 0;
   143 	size_t len = strlen(buffer);
   144 	if (buffer)
   145 	{
   146 		for (int i = len - 1; i >= 0; i--)
   147 		{
   148 			if (buffer[i] == '/')
   149 			{
   150 				if (++num_sc >= 2 && i==0)
   151 				{
   152 					buffer[i] = '\0';
   153 					return;
   154 				}
   155 			}
   156 			else
   157 			{
   158 				if (num_sc >= 2)
   159 				{
   160 					buffer[i] = '\0';
   161 					return;
   162 				}
   163 				num_sc = 0;
   164 			}
   165 			/* size_t won't go below 0, manually break out */
   166 			if (i == 0)
   167 				break;
   168 			
   169 		}
   170 	}
   171 }
   172 
   173 void
   174 mm_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
   175 {
   176 	size_t start;
   177 	size_t len = strlen(str);
   178 
   179 	for (start = 0; start < len; start++)
   180 	{
   181 		if (!isspace(str[start]))
   182 			break;
   183 	}
   184 
   185 	size_t end;
   186 	for (end = start; end < len; end++)
   187 	{
   188 		if (isspace(str[end]))
   189 			break;
   190 	}
   191 	
   192 	size_t i, c = 0;
   193 	for (i = start; i < end; i++, c++)
   194 	{
   195 		if (c >= len1)
   196 			break;
   197 		buf1[c] = str[i];
   198 	}
   199 	buf1[c] = '\0';
   200 
   201 	for (start = end; start < len; start++)
   202 	{
   203 		if (!isspace(str[start]))
   204 			break;
   205 	}
   206 
   207 	for (c = 0; start < len; start++, c++)
   208 	{
   209 		if (c >= len2)
   210 			break;
   211 		buf2[c] = str[start];
   212 	}
   213 	buf2[c] = '\0';
   214 }
   215 
   216 bool
   217 mm_PathCmp(const char *path1, const char *path2)
   218 {
   219 	size_t pos1 = 0, pos2 = 0;
   220 
   221 	while (true)
   222 	{
   223 		if (path1[pos1] == '\0' || path2[pos2] == '\0')
   224 			return (path1[pos1] == path2[pos2]);
   225 
   226 		if (path1[pos1] == PATH_SEP_CHAR)
   227 		{
   228 			if (path2[pos2] != PATH_SEP_CHAR)
   229 				return false;
   230 
   231 			/* Look for extra path chars */
   232 			while (path1[++pos1])
   233 			{
   234 				if (path1[pos1] != PATH_SEP_CHAR)
   235 					break;
   236 			}
   237 			while (path2[++pos2])
   238 			{
   239 				if (path2[pos2] != PATH_SEP_CHAR)
   240 					break;
   241 			}
   242 			continue;
   243 		}
   244 
   245 		/* If we're at a different non-alphanumeric, the next character MUST match */
   246 		if ((((unsigned)path1[pos1] & 0x80) && path1[pos1] != path2[pos2])
   247 			||
   248 			!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2])
   249 			)
   250 		{
   251 			return false;
   252 		}
   253 
   254 	#ifdef WIN32
   255 		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   256 	#else
   257 		if (path1[pos1] != path2[pos2])
   258 	#endif
   259 		{
   260 			return false;
   261 		}
   262 
   263 		pos1++;
   264 		pos2++;
   265 	}
   266 }
   267 
   268 bool
   269 mm_ResolvePath(const char *path, char *buffer, size_t maxlength)
   270 {
   271 #if defined _WIN32
   272 	return _fullpath(buffer, path, maxlength) != NULL;
   273 #elif defined __linux__
   274 	assert(maxlength >= PATH_MAX);
   275 	return realpath(path, buffer) != NULL;
   276 #endif
   277 }
   278 
   279 void *
   280 mm_LoadLibrary(const char *path, char *buffer, size_t maxlength)
   281 {
   282 	void *lib;
   283 
   284 #if defined _WIN32
   285 	lib = (void*)LoadLibrary(path);
   286 
   287 	if (lib == NULL)
   288 	{
   289 		mm_GetPlatformError(buffer, maxlength);
   290 		return NULL;
   291 	}
   292 #elif defined __linux__
   293 	lib = dlopen(path, RTLD_NOW);
   294 
   295 	if (lib == NULL)
   296 	{
   297 		mm_Format(buffer, maxlength, "%s", dlerror());
   298 		return NULL;
   299 	}
   300 #endif
   301 
   302 	return lib;
   303 }
   304 
   305 void *
   306 mm_GetLibAddress(void *lib, const char *name)
   307 {
   308 #if defined _WIN32
   309 	return GetProcAddress((HMODULE)lib, name);
   310 #elif defined __linux__
   311 	return dlsym(lib, name);
   312 #endif
   313 }
   314 
   315 void
   316 mm_UnloadLibrary(void *lib)
   317 {
   318 #if defined _WIN32
   319 	FreeLibrary((HMODULE)lib);
   320 #else
   321 	dlclose(lib);
   322 #endif
   323 }
   324 
   325 bool
   326 mm_GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
   327 {
   328 #if defined _WIN32
   329 	MEMORY_BASIC_INFORMATION mem;
   330 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
   331 		return false;
   332 	if (mem.AllocationBase == NULL)
   333 		return false;
   334 	HMODULE dll = (HMODULE)mem.AllocationBase;
   335 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
   336 #elif defined __linux__
   337 	Dl_info info;
   338 	if (!dladdr(pAddr, &info))
   339 		return false;
   340 	if (!info.dli_fbase || !info.dli_fname)
   341 		return false;
   342 	const char *dllpath = info.dli_fname;
   343 	snprintf(buffer, maxlength, "%s", dllpath);
   344 #endif
   345 	return true;
   346 }
   347