File: htpefimp.cc

package info (click to toggle)
ht 2.0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,324 kB
  • sloc: cpp: 97,563; ansic: 17,183; sh: 3,811; lex: 226; makefile: 213; yacc: 127
file content (301 lines) | stat: -rw-r--r-- 7,467 bytes parent folder | download | duplicates (8)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 *	HT Editor
 *	htpefimp.cc
 *
 *	Copyright (C) 1999-2002 Stefan Weyergraf
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "formats.h"
#include "htanaly.h"
#include "htctrl.h"
#include "data.h"
#include "endianess.h"
#include "htiobox.h"
#include "htpal.h"
#include "htpef.h"
#include "htpefimp.h"
#include "stream.h"
#include "strtools.h"
#include "httag.h"
#include "log.h"
#include "pef_analy.h"
#include "snprintf.h"
#include "tools.h"

#include <stdlib.h>
#include <string.h>

ht_view *htpefimports_init(Bounds *b, File *file, ht_format_group *group)
{
	ht_pef_shared_data *pef_shared=(ht_pef_shared_data *)group->get_shared_data();

	if (!pef_shared->loader_info_header_ofs 
	|| !pef_shared->loader_info_header.importedLibraryCount 
	|| !pef_shared->loader_info_header.totalImportedSymbolCount) return NULL;
	
	FileOfs nametable = pef_shared->loader_info_header_ofs + pef_shared->loader_info_header.loaderStringsOffset;
	FileOfs functions_offset = pef_shared->loader_info_header_ofs 
			+ sizeof pef_shared->loader_info_header
			+ pef_shared->loader_info_header.importedLibraryCount
				* sizeof(PEF_ImportedLibrary);

	ht_group *g;
	Bounds c;

	c=*b;
	g=new ht_group();
	g->init(&c, VO_RESIZE, DESC_PEF_IMPORTS"-g");
	ht_statictext *head;

	int lib_count = pef_shared->loader_info_header.importedLibraryCount;
	int function_count = pef_shared->loader_info_header.totalImportedSymbolCount;

	c.y++;
	c.h--;
	ht_pef_import_viewer *v=new ht_pef_import_viewer();
	v->init(&c, DESC_PEF_IMPORTS, group);

	c.y--;
	c.h=1;

	int symbol_num = 0;
	for (uint i=0; i < pef_shared->loader_info_header.importedLibraryCount; i++) {
		file->seek(pef_shared->loader_info_header_ofs + sizeof pef_shared->loader_info_header
			+ i*sizeof(PEF_ImportedLibrary));

		PEF_ImportedLibrary lib;
		file->read(&lib, sizeof lib);
		createHostStruct(&lib, PEF_ImportedLibrary_struct, pef_shared->byte_order);

		file->seek(nametable + lib.nameOffset);
		char *libname = file->fgetstrz();

		ht_pef_import_library *library=new ht_pef_import_library(libname);
		pef_shared->imports.libs->insert(library);

		for (uint j=0; j < lib.importedSymbolCount; j++) {
			file->seek(functions_offset + 4 * (lib.firstImportedSymbol+j));
			uint32 entry;
			file->read(&entry, 4);
			entry = createHostInt(&entry, 4, pef_shared->byte_order);
			
			uint32 symbol_ofs = entry & 0x00ffffff;
			uint32 symbol_class = entry >> 24;

			file->seek(nametable+symbol_ofs);
			char *symbol_name = file->fgetstrz();

			ht_pef_import_function *func = new ht_pef_import_function(i, symbol_num, symbol_name, symbol_class);
			pef_shared->imports.funcs->insert(func);

			free(symbol_name);
			symbol_num++;
		}
		
		free(libname);
	}

	char iline[1024];
	ht_snprintf(iline, sizeof iline, "* PEF import library description at offset %08qx (%d functions from %d libraries)", 
		pef_shared->loader_info_header_ofs + sizeof pef_shared->loader_info_header, 
		function_count, lib_count);
		
	head=new ht_statictext();
	head->init(&c, iline, align_left);

	g->insert(head);
	g->insert(v);
	//
	for (uint i=0; i < pef_shared->imports.funcs->count(); i++) {
		ht_pef_import_function *func = (ht_pef_import_function*)(*pef_shared->imports.funcs)[i];
		assert(func);
		ht_pef_import_library *lib = (ht_pef_import_library*)(*pef_shared->imports.libs)[func->libidx];
		assert(lib);
		char addr[32], name[256];
		ht_snprintf(addr, sizeof addr, "%d", func->num);
		ht_snprintf(name, sizeof name, "%s", func->name);
		v->insert_str(i, lib->name, addr, name);
	}
	//
	v->update();

	g->setpalette(palkey_generic_window_default);

	pef_shared->v_imports = v;
	return g;
pef_read_error:
	String fn;
	errorbox("%y: PEF import library description seems to be corrupted.", &file->getFilename(fn));
	g->done();
	delete g;
	v->done();
	delete v;
	return NULL;
}

format_viewer_if htpefimports_if = {
	htpefimports_init,
	NULL
};

/*
 *	class ht_pef_import_library
 */

ht_pef_import_library::ht_pef_import_library(char *n)
{
	name = ht_strdup(n);
}

ht_pef_import_library::~ht_pef_import_library()
{
	free(name);
}

/*
 *	class ht_pe_import_function
 */

ht_pef_import_function::ht_pef_import_function(uint aLibidx, int aNum, const char *aName, uint aSym_class)
{
	libidx = aLibidx;
	name = ht_strdup(aName);
	num = aNum;
	sym_class = aSym_class;
}

ht_pef_import_function::~ht_pef_import_function()
{
	free(name);
}

/*
 *	CLASS ht_pef_import_viewer
 */

void ht_pef_import_viewer::init(Bounds *b, const char *Desc, ht_format_group *fg)
{
	ht_text_listbox::init(b, 3, 2, LISTBOX_QUICKFIND);
	options |= VO_BROWSABLE;
	desc = strdup(Desc);
	format_group = fg;
	grouplib = false;
	sortby = 1;
	dosort();
}

void	ht_pef_import_viewer::done()
{
	ht_text_listbox::done();
}

void ht_pef_import_viewer::dosort()
{
	ht_text_listbox_sort_order sortord[2];
	uint l, s;
	if (grouplib) {
		l = 0;
		s = 1;
	} else {
		l = 1;
		s = 0;
	}
	sortord[l].col = 0;
	sortord[l].compare_func = strcmp;
	sortord[s].col = sortby;
	sortord[s].compare_func = strcmp;
	sort(2, sortord);
}

const char *ht_pef_import_viewer::func(uint i, bool execute)
{
	switch (i) {
		case 2:
			if (execute) {
				grouplib = !grouplib;
				dosort();
			}
			return grouplib ? (char*)"nbylib" : (char*)"bylib";
		case 5:
			if (execute) {
				if (sortby != 2) {
					sortby = 2;
					dosort();
				}
			}
			return "byname";
	}
	return NULL;
}

void ht_pef_import_viewer::handlemsg(htmsg *msg)
{
	switch (msg->msg) {
	case msg_funcexec:
		if (func(msg->data1.integer, 1)) {
			clearmsg(msg);
			return;
		}
		break;
	case msg_funcquery: {
		const char *s=func(msg->data1.integer, 0);
		if (s) {
			msg->msg=msg_retval;
			msg->data1.cstr=s;
		}
		break;
	}
	case msg_keypressed: {
		if (msg->data1.integer == K_Return) {
			select_entry(e_cursor);
			clearmsg(msg);
		}
		break;
	}
	}
	ht_text_listbox::handlemsg(msg);
}

bool ht_pef_import_viewer::select_entry(void *entry)
{
/*	ht_text_listbox_item *i = (ht_text_listbox_item *)entry;

	ht_pef_shared_data *pef_shared=(ht_pef_shared_data *)format_group->get_shared_data();

	ht_pef_import_function *e = (ht_pef_import_function*)pef_shared->imports.funcs->get(i->id);
	if (!e) return true;
	if (pef_shared->v_image) {
		ht_aviewer *av = (ht_aviewer*)pef_shared->v_image;
		PEFAnalyser *a = (PEFAnalyser*)av->analy;
		Address *addr;
		if (pef_shared->opt_magic == COFF_OPTMAGIC_PE32) {
			addr = a->createAddress32(e->address+pe_shared->pe32.header_nt.image_base);
		} else {
			addr = a->createAddress64(to_qword(e->address)+pe_shared->pe64.header_nt.image_base);
		}
		if (av->gotoAddress(addr, NULL)) {
			app->focus(av);
			vstate_save();
		} else {
			global_analyser_address_string_format = ADDRESS_STRING_FORMAT_COMPACT | ADDRESS_STRING_FORMAT_ADD_0X;
			errorbox("can't follow: %s %y is not valid!", "import address", addr);
		}
		delete addr;
	} else errorbox("can't follow: no image viewer");*/
	return true;
}