summaryrefslogtreecommitdiffhomepage
path: root/src/modules/pe_get_module_handle.c
blob: 78afaa26b2d8638f20b80aad42f6c70de830184e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*****************************************************************************/
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
/*  Copyright (C) 2013--2020  SysDeer Technologies, LLC                      */
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
/*****************************************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pe_consts.h>
#include <pemagine/pe_structs.h>
#include <pemagine/pemagine.h>
#include "pe_impl.h"


void * pe_get_first_module_handle(void)
{
	struct pe_peb_ldr_data *	peb_ldr_data;
	struct pe_list_entry *   	peb_list_entry;
	intptr_t 			peb_tbl_addr;
	struct pe_ldr_tbl_entry *	peb_ldr_tbl_entry;

	peb_ldr_data		= (struct pe_peb_ldr_data *)pe_get_peb_ldr_data_address();
	peb_list_entry		= (struct pe_list_entry *)peb_ldr_data->in_load_order_module_list.flink;
	peb_tbl_addr		= (intptr_t)peb_list_entry - IN_LOAD_ORDER_MODULE_LIST_OFFSET;
	peb_ldr_tbl_entry	= (struct pe_ldr_tbl_entry *)peb_tbl_addr;

	return peb_ldr_tbl_entry->dll_base;
}


void * pe_get_module_handle(const wchar16_t * name)
{
	wchar16_t * src;
	wchar16_t * dst;

	size_t match;
	size_t len;

	struct pe_peb_ldr_data *	peb_ldr_data;
	struct pe_list_entry *		plist_head;

	struct pe_list_entry *		plist_current;
	struct pe_ldr_tbl_entry *	ldr_tbl_entry = 0;

	peb_ldr_data  = (struct pe_peb_ldr_data *)pe_get_peb_ldr_data_address();
	plist_head    = (struct pe_list_entry *)peb_ldr_data->in_load_order_module_list.flink;
	plist_current = plist_head;

	len = pe_impl_strlen_utf16(name);

	do {
		ldr_tbl_entry = (struct pe_ldr_tbl_entry *)plist_current - IN_LOAD_ORDER_MODULE_LIST_OFFSET;

		if (ldr_tbl_entry->base_dll_name.strlen == len)
			dst = (wchar16_t *)ldr_tbl_entry->base_dll_name.buffer;

		else if (ldr_tbl_entry->full_dll_name.strlen == len)
			dst = (wchar16_t *)ldr_tbl_entry->full_dll_name.buffer;

                else
                        dst = (wchar16_t *)0;


		if ((intptr_t)(dst)) {
		        src   = (wchar16_t *)name;
			match = 0;

			while ((match < len)
					&& ((pe_impl_utf16_char_to_lower(*src))
					== (pe_impl_utf16_char_to_lower(*dst)))) {
				src = (wchar16_t *)pe_va_from_rva(src,sizeof(wchar16_t));
				dst = (wchar16_t *)pe_va_from_rva(dst,sizeof(wchar16_t));
				match+=sizeof(wchar16_t);
			}


			if (match == len)
				return ldr_tbl_entry->dll_base;
		}

		plist_current = plist_current->flink;
	} while (plist_current != plist_head);

	return 0;
}