Blame src/modules/pe_get_module_handle.c

feffc7
/*****************************************************************************/
feffc7
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
6dda52
/*  Copyright (C) 2013--2020  SysDeer Technologies, LLC                      */
feffc7
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
feffc7
/*****************************************************************************/
feffc7
feffc7
#include <psxtypes/psxtypes.h>
feffc7
#include <pemagine/pe_consts.h>
feffc7
#include <pemagine/pe_structs.h>
feffc7
#include <pemagine/pemagine.h>
feffc7
#include "pe_impl.h"
feffc7
b3ca43
feffc7
void * pe_get_first_module_handle(void)
feffc7
{
feffc7
	struct pe_peb_ldr_data *	peb_ldr_data;
feffc7
	struct pe_list_entry *   	peb_list_entry;
feffc7
	intptr_t 			peb_tbl_addr;
feffc7
	struct pe_ldr_tbl_entry *	peb_ldr_tbl_entry;
feffc7
feffc7
	peb_ldr_data		= (struct pe_peb_ldr_data *)pe_get_peb_ldr_data_address();
feffc7
	peb_list_entry		= (struct pe_list_entry *)peb_ldr_data->in_load_order_module_list.flink;
feffc7
	peb_tbl_addr		= (intptr_t)peb_list_entry - IN_LOAD_ORDER_MODULE_LIST_OFFSET;
feffc7
	peb_ldr_tbl_entry	= (struct pe_ldr_tbl_entry *)peb_tbl_addr;
feffc7
feffc7
	return peb_ldr_tbl_entry->dll_base;
feffc7
}
feffc7
53d5a6
feffc7
void * pe_get_module_handle(const wchar16_t * name)
feffc7
{
feffc7
	wchar16_t * src;
feffc7
	wchar16_t * dst;
feffc7
feffc7
	size_t match;
feffc7
	size_t len;
feffc7
feffc7
	struct pe_peb_ldr_data *	peb_ldr_data;
feffc7
	struct pe_list_entry *		plist_head;
feffc7
feffc7
	struct pe_list_entry *		plist_current;
feffc7
	struct pe_ldr_tbl_entry *	ldr_tbl_entry = 0;
feffc7
feffc7
	peb_ldr_data  = (struct pe_peb_ldr_data *)pe_get_peb_ldr_data_address();
feffc7
	plist_head    = (struct pe_list_entry *)peb_ldr_data->in_load_order_module_list.flink;
feffc7
	plist_current = plist_head;
feffc7
feffc7
	len = pe_impl_strlen_utf16(name);
feffc7
feffc7
	do {
feffc7
		ldr_tbl_entry = (struct pe_ldr_tbl_entry *)plist_current - IN_LOAD_ORDER_MODULE_LIST_OFFSET;
feffc7
feffc7
		if (ldr_tbl_entry->base_dll_name.strlen == len)
feffc7
			dst = (wchar16_t *)ldr_tbl_entry->base_dll_name.buffer;
53d5a6
feffc7
		else if (ldr_tbl_entry->full_dll_name.strlen == len)
feffc7
			dst = (wchar16_t *)ldr_tbl_entry->full_dll_name.buffer;
53d5a6
feffc7
                else
feffc7
                        dst = (wchar16_t *)0;
feffc7
53d5a6
feffc7
		if ((intptr_t)(dst)) {
feffc7
		        src   = (wchar16_t *)name;
feffc7
			match = 0;
feffc7
feffc7
			while ((match < len)
53d5a6
					&& ((pe_impl_utf16_char_to_lower(*src))
53d5a6
					== (pe_impl_utf16_char_to_lower(*dst)))) {
53d5a6
				src = (wchar16_t *)pe_va_from_rva(src,sizeof(wchar16_t));
53d5a6
				dst = (wchar16_t *)pe_va_from_rva(dst,sizeof(wchar16_t));
53d5a6
				match+=sizeof(wchar16_t);
53d5a6
			}
53d5a6
feffc7
feffc7
			if (match == len)
feffc7
				return ldr_tbl_entry->dll_base;
feffc7
		}
feffc7
feffc7
		plist_current = plist_current->flink;
feffc7
	} while (plist_current != plist_head);
feffc7
feffc7
	return 0;
feffc7
}