Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

htpefimp.cc

Go to the documentation of this file.
00001 /*
00002  *      HT Editor
00003  *      htpefimp.cc
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "formats.h"
00022 #include "htanaly.h"
00023 #include "htctrl.h"
00024 #include "htdata.h"
00025 #include "htendian.h"
00026 #include "htiobox.h"
00027 #include "htpal.h"
00028 #include "htpef.h"
00029 #include "htpefimp.h"
00030 #include "stream.h"
00031 #include "htstring.h"
00032 #include "httag.h"
00033 #include "log.h"
00034 #include "pef_analy.h"
00035 #include "snprintf.h"
00036 #include "tools.h"
00037 
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00041 ht_view *htpefimports_init(bounds *b, ht_streamfile *file, ht_format_group *group)
00042 {
00043         ht_pef_shared_data *pef_shared=(ht_pef_shared_data *)group->get_shared_data();
00044 
00045         if (!pef_shared->loader_info_header_ofs 
00046         || !pef_shared->loader_info_header.importedLibraryCount 
00047         || !pef_shared->loader_info_header.totalImportedSymbolCount) return NULL;
00048         
00049         FILEOFS nametable = pef_shared->loader_info_header_ofs + pef_shared->loader_info_header.loaderStringsOffset;
00050         FILEOFS functions_offset = pef_shared->loader_info_header_ofs 
00051                         + sizeof pef_shared->loader_info_header
00052                         + pef_shared->loader_info_header.importedLibraryCount
00053                                 * sizeof(PEF_ImportedLibrary);
00054 
00055         ht_group *g;
00056         bounds c;
00057 
00058         c=*b;
00059         g=new ht_group();
00060         g->init(&c, VO_RESIZE, DESC_PEF_IMPORTS"-g");
00061         ht_statictext *head;
00062 
00063         int lib_count = pef_shared->loader_info_header.importedLibraryCount;
00064         int function_count = pef_shared->loader_info_header.totalImportedSymbolCount;
00065 
00066         c.y++;
00067         c.h--;
00068         ht_pef_import_viewer *v=new ht_pef_import_viewer();
00069         v->init(&c, DESC_PEF_IMPORTS, group);
00070 
00071         c.y--;
00072         c.h=1;
00073 
00074         int symbol_num = 0;
00075         for (uint i=0; i < pef_shared->loader_info_header.importedLibraryCount; i++) {
00076                 file->seek(pef_shared->loader_info_header_ofs + sizeof pef_shared->loader_info_header
00077                         + i*sizeof(PEF_ImportedLibrary));
00078 
00079                 PEF_ImportedLibrary lib;
00080                 file->read(&lib, sizeof lib);
00081                 create_host_struct(&lib, PEF_ImportedLibrary_struct, pef_shared->byte_order);
00082 
00083                 file->seek(nametable + lib.nameOffset);
00084                 char *libname = fgetstrz(file);
00085 
00086                 ht_pef_import_library *library=new ht_pef_import_library(libname);
00087                 pef_shared->imports.libs->insert(library);
00088 
00089                 for (uint j=0; j < lib.importedSymbolCount; j++) {
00090                         file->seek(functions_offset + 4 * (lib.firstImportedSymbol+j));
00091                         uint32 entry;
00092                         file->read(&entry, 4);
00093                         entry = create_host_int(&entry, 4, pef_shared->byte_order);
00094                         
00095                         uint32 symbol_ofs = entry & 0x00ffffff;
00096                         uint32 symbol_class = entry >> 24;
00097 
00098                         file->seek(nametable+symbol_ofs);
00099                         char *symbol_name = fgetstrz(file);
00100 
00101                         ht_pef_import_function *func = new ht_pef_import_function(i, symbol_num, symbol_name, symbol_class);
00102                         pef_shared->imports.funcs->insert(func);
00103 
00104                         free(symbol_name);
00105                         symbol_num++;
00106                 }
00107                 
00108                 free(libname);
00109         }
00110 
00111         char iline[1024];
00112         ht_snprintf(iline, sizeof iline, "* PEF import library description at offset %08x (%d functions from %d libraries)", 
00113                 pef_shared->loader_info_header_ofs + sizeof pef_shared->loader_info_header, 
00114                 function_count, lib_count);
00115                 
00116         head=new ht_statictext();
00117         head->init(&c, iline, align_left);
00118 
00119         g->insert(head);
00120         g->insert(v);
00121         //
00122         for (UINT i=0; i < pef_shared->imports.funcs->count(); i++) {
00123                 ht_pef_import_function *func = (ht_pef_import_function*)pef_shared->imports.funcs->get(i);
00124                 assert(func);
00125                 ht_pef_import_library *lib = (ht_pef_import_library*)pef_shared->imports.libs->get(func->libidx);
00126                 assert(lib);
00127                 char addr[32], name[256];
00128                 ht_snprintf(addr, sizeof addr, "%d", func->num);
00129                 ht_snprintf(name, sizeof name, "%s", func->name);
00130                 v->insert_str(i, lib->name, addr, name);
00131         }
00132         //
00133         v->update();
00134 
00135         g->setpalette(palkey_generic_window_default);
00136 
00137         pef_shared->v_imports = v;
00138         return g;
00139 pef_read_error:
00140         errorbox("%s: PEF import library description seems to be corrupted.", file->get_filename());
00141         g->done();
00142         delete g;
00143         v->done();
00144         delete v;
00145         return NULL;
00146 }
00147 
00148 format_viewer_if htpefimports_if = {
00149         htpefimports_init,
00150         NULL
00151 };
00152 
00153 /*
00154  *      class ht_pef_import_library
00155  */
00156 
00157 ht_pef_import_library::ht_pef_import_library(char *n)
00158 {
00159         name = ht_strdup(n);
00160 }
00161 
00162 ht_pef_import_library::~ht_pef_import_library()
00163 {
00164         if (name) free(name);
00165 }
00166 
00167 /*
00168  *      class ht_pe_import_function
00169  */
00170 
00171 ht_pef_import_function::ht_pef_import_function(UINT aLibidx, int aNum, const char *aName, UINT aSym_class)
00172 {
00173         libidx = aLibidx;
00174         name = ht_strdup(aName);
00175         num = aNum;
00176         sym_class = aSym_class;
00177 }
00178 
00179 ht_pef_import_function::~ht_pef_import_function()
00180 {
00181         free(name);
00182 }
00183 
00184 /*
00185  *      CLASS ht_pef_import_viewer
00186  */
00187 
00188 void ht_pef_import_viewer::init(bounds *b, char *Desc, ht_format_group *fg)
00189 {
00190         ht_text_listbox::init(b, 3, 2, LISTBOX_QUICKFIND);
00191         options |= VO_BROWSABLE;
00192         desc = strdup(Desc);
00193         format_group = fg;
00194         grouplib = false;
00195         sortby = 1;
00196         dosort();
00197 }
00198 
00199 void    ht_pef_import_viewer::done()
00200 {
00201         ht_text_listbox::done();
00202 }
00203 
00204 void ht_pef_import_viewer::dosort()
00205 {
00206         ht_text_listbox_sort_order sortord[2];
00207         UINT l, s;
00208         if (grouplib) {
00209                 l = 0;
00210                 s = 1;
00211         } else {
00212                 l = 1;
00213                 s = 0;
00214         }
00215         sortord[l].col = 0;
00216         sortord[l].compare_func = strcmp;
00217         sortord[s].col = sortby;
00218         sortord[s].compare_func = strcmp;
00219         sort(2, sortord);
00220 }
00221 
00222 char *ht_pef_import_viewer::func(UINT i, bool execute)
00223 {
00224         switch (i) {
00225                 case 2:
00226                         if (execute) {
00227                                 grouplib = !grouplib;
00228                                 dosort();
00229                         }
00230                         return grouplib ? (char*)"nbylib" : (char*)"bylib";
00231                 case 5:
00232                         if (execute) {
00233                                 if (sortby != 2) {
00234                                         sortby = 2;
00235                                         dosort();
00236                                 }
00237                         }
00238                         return "byname";
00239         }
00240         return NULL;
00241 }
00242 
00243 void ht_pef_import_viewer::handlemsg(htmsg *msg)
00244 {
00245         switch (msg->msg) {
00246                 case msg_funcexec:
00247                         if (func(msg->data1.integer, 1)) {
00248                                 clearmsg(msg);
00249                                 return;
00250                         }
00251                         break;
00252                 case msg_funcquery: {
00253                         char *s=func(msg->data1.integer, 0);
00254                         if (s) {
00255                                 msg->msg=msg_retval;
00256                                 msg->data1.str=s;
00257                         }
00258                         break;
00259                 }
00260 /*              case msg_get_scrollinfo:
00261                         switch (msg->data1.integer) {
00262                                 case gsi_pindicator: {
00263                                         strcpy((char*)msg->data2.ptr, " Enter to view, Backspace to return here");
00264                                         clearmsg(msg);
00265                                         return;
00266                                 }
00267                         }
00268                         break;*/
00269                 case msg_keypressed: {
00270                         if (msg->data1.integer == K_Return) {
00271                                 select_entry(e_cursor);
00272                                 clearmsg(msg);
00273                         }
00274                         break;
00275                 }
00276         }
00277         ht_text_listbox::handlemsg(msg);
00278 }
00279 
00280 bool ht_pef_import_viewer::select_entry(void *entry)
00281 {
00282 /*      ht_text_listbox_item *i = (ht_text_listbox_item *)entry;
00283 
00284         ht_pef_shared_data *pef_shared=(ht_pef_shared_data *)format_group->get_shared_data();
00285 
00286         ht_pef_import_function *e = (ht_pef_import_function*)pef_shared->imports.funcs->get(i->id);
00287         if (!e) return true;
00288         if (pef_shared->v_image) {
00289                 ht_aviewer *av = (ht_aviewer*)pef_shared->v_image;
00290                 PEFAnalyser *a = (PEFAnalyser*)av->analy;
00291                 Address *addr;
00292                 if (pef_shared->opt_magic == COFF_OPTMAGIC_PE32) {
00293                         addr = a->createAddress32(e->address+pe_shared->pe32.header_nt.image_base);
00294                 } else {
00295                         addr = a->createAddress64(to_qword(e->address)+pe_shared->pe64.header_nt.image_base);
00296                 }
00297                 if (av->gotoAddress(addr, NULL)) {
00298                         app->focus(av);
00299                         vstate_save();
00300                 } else {
00301                         global_analyser_address_string_format = ADDRESS_STRING_FORMAT_COMPACT | ADDRESS_STRING_FORMAT_ADD_0X;
00302                         errorbox("can't follow: %s %y is not valid !", "import address", addr);
00303                 }
00304                 delete addr;
00305         } else errorbox("can't follow: no image viewer");*/
00306         return true;
00307 }
00308 
00309 

Generated on Fri May 7 21:15:36 2004 by doxygen 1.3.5