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
|
#include "formats.h"
#include "htapp.h"
#include "htctrl.h"
#include "htdialog.h"
#include "endianess.h"
#include "hthex.h"
#include "htiobox.h"
#include "keyb.h"
#include "htnewexe.h"
#include "htobj.h"
#include "htpe.h"
#include "htperes.h"
#include "strtools.h"
#include "httree.h"
#include "log.h"
#include "pestruct.h"
#include "stream.h"
#include "snprintf.h"
#include <string.h>
static int_hash restypes[] = {
{0x0001, "cursors"},
{0x0002, "bitmaps"},
{0x0003, "icons"},
{0x0004, "menus"},
{0x0005, "dialogs"},
{0x0006, "string tables"},
{0x0007, "font directories"},
{0x0008, "fonts"},
{0x0009, "accelerators"},
{0x000a, "custom resource data"},
{0x000b, "message tables"},
{0x000c, "group cursors"},
{0x000e, "group icons"},
{0x0010, "version information"},
{0x0011, "dialog includes"},
{0x0013, "pnp"},
{0x0014, "vxd"},
{0x0015, "animated cursors"},
{ 0 }
};
static int_hash languages[] = {
{7, "german"},
{9, "english"},
{10, "spanish"},
{12, "french"},
{16, "italian"},
{ 0 }
};
class ht_pe_resource_leaf: public Object {
public:
uint32 offset;
uint32 size;
};
static File *peresource_file;
static FileOfs peresource_dir_ofs;
static ht_static_treeview *peresource_tree;
static char peresource_string[128];
static pe_section_headers *peresource_section_headers;
static void read_resource_dir(void *node, int ofs, int level)
{
if (level>2) return;
PE_RESOURCE_DIRECTORY dir;
peresource_file->seek(peresource_dir_ofs+ofs);
if (peresource_file->read(&dir, sizeof dir) != (sizeof dir)) return;
createHostStruct(&dir, PE_RESOURCE_DIRECTORY_struct, little_endian);
PE_RESOURCE_DIRECTORY_ENTRY entry;
for (int i=0; i<dir.name_count+dir.id_count; i++) {
peresource_file->seek(peresource_dir_ofs+ofs+sizeof dir+i*8);
peresource_file->read(&entry, sizeof entry);
createHostStruct(&entry, PE_RESOURCE_DIRECTORY_ENTRY_struct, little_endian);
if (entry.offset_to_directory & 0x80000000) {
bool hasname = entry.name & 0x80000000;
PE_RESOURCE_DIRECTORY subdir;
peresource_file->seek(peresource_dir_ofs + (entry.offset_to_directory & 0x7fffffff));
peresource_file->read(&subdir, sizeof subdir);
createHostStruct(&subdir, PE_RESOURCE_DIRECTORY_struct, little_endian);
if (hasname) {
peresource_file->seek(peresource_dir_ofs + (entry.name & 0x7fffffff));
char *name = peresource_file->readstrw();
ht_snprintf(peresource_string, sizeof peresource_string, "%s [%d]", name, subdir.name_count+subdir.id_count);
free(name);
} else {
const char *s = (!level) ? matchhash(entry.name & 0xffff, restypes) : NULL;
if (s) {
ht_snprintf(peresource_string, sizeof peresource_string, "ID %04x, %s [%d]", entry.name & 0xffff, s, subdir.name_count+subdir.id_count);
} else {
ht_snprintf(peresource_string, sizeof peresource_string, "ID %04x [%d]", entry.name & 0xffff, subdir.name_count+subdir.id_count);
}
}
void *n = peresource_tree->add_child(node, peresource_string);
read_resource_dir(n, entry.offset_to_directory & 0x7fffffff, level+1);
} else {
char *rm = peresource_string;
char *rm_end = rm + sizeof peresource_string;
const char *s = matchhash((char)entry.name, languages);
if (s) {
rm += ht_snprintf(rm, rm_end-rm, "resource, %s (%04x) ", s, entry.name & 0xffff);
} else {
rm += ht_snprintf(rm, rm_end-rm, "resource, unknown language (%04x) ", entry.name & 0xffff);
}
PE_RESOURCE_DATA_ENTRY data;
peresource_file->seek(peresource_dir_ofs+entry.offset_to_directory);
peresource_file->read(&data, sizeof data);
createHostStruct(&data, PE_RESOURCE_DATA_ENTRY_struct, little_endian);
ht_pe_resource_leaf *xdata = NULL;
FileOfs dofs=0;
if (pe_rva_to_ofs(peresource_section_headers, data.offset_to_data, &dofs)) {
xdata = new ht_pe_resource_leaf();
xdata->offset = dofs;
xdata->size = data.size;
rm += ht_snprintf(rm, rm_end-rm, "offset %08qx", dofs);
} else {
rm += ht_snprintf(rm, rm_end-rm, "offset? (rva %08x, corrupt)", data.offset_to_data);
}
ht_snprintf(rm, rm_end-rm, " size %08x", data.size);
peresource_tree->add_child(node, peresource_string, xdata);
}
}
}
static ht_view *htperesources_init(Bounds *b, File *file, ht_format_group *group)
{
ht_pe_shared_data *pe_shared=(ht_pe_shared_data *)group->get_shared_data();
if (pe_shared->opt_magic!=COFF_OPTMAGIC_PE32 && pe_shared->opt_magic!=COFF_OPTMAGIC_PE64) return NULL;
bool pe32 = (pe_shared->opt_magic==COFF_OPTMAGIC_PE32);
uint32 sec_rva, sec_size;
if (pe32) {
sec_rva = pe_shared->pe32.header_nt.directory[PE_DIRECTORY_ENTRY_RESOURCE].address;
sec_size = pe_shared->pe32.header_nt.directory[PE_DIRECTORY_ENTRY_RESOURCE].size;
} else {
sec_rva = pe_shared->pe64.header_nt.directory[PE_DIRECTORY_ENTRY_RESOURCE].address;
sec_size = pe_shared->pe64.header_nt.directory[PE_DIRECTORY_ENTRY_RESOURCE].size;
}
if (!sec_rva || !sec_size) return NULL;
ht_pe_resource_viewer *t=new ht_pe_resource_viewer();
t->init(b, DESC_PE_RESOURCES);
void *root;
String fn;
FileOfs iofs;
uint32 irva;
if (pe32) {
irva=pe_shared->pe32.header_nt.directory[PE_DIRECTORY_ENTRY_RESOURCE].address;
} else {
irva=pe_shared->pe64.header_nt.directory[PE_DIRECTORY_ENTRY_RESOURCE].address;
}
if (!pe_rva_to_ofs(&pe_shared->sections, irva, &iofs)) goto pe_read_error;
LOG("%y: PE: reading resource directory at offset 0x%08qx, rva %08x", &file->getFilename(fn), iofs, irva);
peresource_file = file;
peresource_dir_ofs = iofs;
peresource_tree = t;
peresource_section_headers = &pe_shared->sections;
root=t->add_child(0, "pe resources");
read_resource_dir(root, 0, 0);
t->adjust(root, true);
t->update();
pe_shared->v_resources = t;
return t;
pe_read_error:
errorbox("%y: PE resource directory seems to be corrupted.", &file->getFilename(fn));
t->done();
delete t;
return NULL;
}
format_viewer_if htperesources_if = {
htperesources_init,
0
};
void ht_pe_resource_viewer::init(Bounds *b, const char *desc)
{
ht_static_treeview::init(b, desc);
VIEW_DEBUG_NAME("ht_pe_resource_viewer");
}
void ht_pe_resource_viewer::done()
{
ht_static_treeview::done();
}
void ht_pe_resource_viewer::handlemsg(htmsg *msg)
{
switch (msg->msg) {
case msg_vstate_restore:
vstate_restore((Object*)msg->data1.ptr);
clearmsg(msg);
return;
}
ht_static_treeview::handlemsg(msg);
}
void ht_pe_resource_viewer::select_node(void *node)
{
static_node *s=(static_node*)node;
if (s->data) {
ht_group *vr_group=group;
while (strcmp(vr_group->desc, VIEWERGROUP_NAME)) vr_group=vr_group->group;
ht_view *c=vr_group->getfirstchild();
ht_format_viewer *hexv = NULL;
while (c) {
if (c->desc && (strcmp(c->desc, DESC_HEX)==0)) {
hexv=(ht_format_viewer*)c;
break;
}
c=c->next;
}
if (hexv) {
vstate_save();
hexv->goto_offset(((ht_pe_resource_leaf*)s->data)->offset, false);
hexv->pselect_set(((ht_pe_resource_leaf*)s->data)->offset,
((ht_pe_resource_leaf*)s->data)->offset+((ht_pe_resource_leaf*)s->data)->size);
app->focus(hexv);
}
}
}
class ht_pe_resource_viewer_vstate: public Object {
public:
void *node;
};
Object *ht_pe_resource_viewer::vstate_create()
{
ht_pe_resource_viewer_vstate *v = new ht_pe_resource_viewer_vstate();
v->node = get_cursor_node();
return v;
}
bool ht_pe_resource_viewer::vstate_save()
{
Object *vs = vstate_create();
if (vs) {
htmsg m;
m.msg = msg_vstate_save;
m.type = mt_empty;
m.data1.ptr = vs;
m.data2.ptr = this;
app->sendmsg(&m);
return true;
}
return false;
}
void ht_pe_resource_viewer::vstate_restore(Object *d)
{
ht_pe_resource_viewer_vstate *v = new ht_pe_resource_viewer_vstate();
goto_node(v->node);
}
|