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

htxbe.cc

Go to the documentation of this file.
00001 /*
00002  *      HT Editor
00003  *      htxbe.cc
00004  *
00005  *      Copyright (C) 2003 Stefan Esser
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 "log.h"
00022 #include "htendian.h"
00023 #include "htxbe.h"
00024 #include "htxbehead.h"
00025 #include "htxbeimg.h"
00026 #include "stream.h"
00027 #include "tools.h"
00028 
00029 #include <stdlib.h>
00030 #include <string.h>
00031 
00032 static format_viewer_if *htxbe_ifs[] = {
00033         &htxbeheader_if,
00034         &htxbeimports_if,
00035         &htxbeimage_if,
00036         0
00037 };
00038 
00039 static ht_view *htxbe_init(bounds *b, ht_streamfile *file, ht_format_group *format_group)
00040 {
00041         byte xbemagic[4];
00042 
00043         file->seek(0);
00044         file->read(xbemagic, 4);
00045         if ((xbemagic[0]!=XBE_MAGIC0) || (xbemagic[1]!=XBE_MAGIC1) ||
00046            (xbemagic[2]!=XBE_MAGIC2) || (xbemagic[3]!=XBE_MAGIC3)) return 0;
00047 
00048         ht_xbe *g=new ht_xbe();
00049         g->init(b, file, htxbe_ifs, format_group, 0);
00050         return g;
00051 }
00052 
00053 format_viewer_if htxbe_if = {
00054         htxbe_init,
00055         0
00056 };
00057 
00058 /*
00059  *      CLASS ht_xbe
00060  */
00061 void ht_xbe::init(bounds *b, ht_streamfile *file, format_viewer_if **ifs, ht_format_group *format_group, FILEOFS header_ofs)
00062 {
00063         ht_format_group::init(b, VO_BROWSABLE | VO_SELECTABLE | VO_RESIZE, DESC_XBE, file, false, true, 0, format_group);
00064         VIEW_DEBUG_NAME("ht_xbe");
00065 
00066         ht_xbe_shared_data *xbe_shared = (ht_xbe_shared_data*)malloc(sizeof (ht_xbe_shared_data));
00067         shared_data = xbe_shared;
00068 
00069         xbe_shared->imports.funcs = new ht_clist();
00070         xbe_shared->imports.funcs->init();
00071 
00072         xbe_shared->v_image = NULL;
00073         xbe_shared->v_imports = NULL;
00074         xbe_shared->v_header = NULL;
00075 
00076 /* read header */
00077         file->seek(0);
00078         file->read(&xbe_shared->header, sizeof xbe_shared->header);
00079         create_host_struct(&xbe_shared->header.base_address, XBE_IMAGE_HEADER_struct, little_endian);
00080         
00081         /* decode entrypoint - XXX: only RETAILs*/
00082         xbe_shared->header.entry_point ^= 0xA8FC57AB;
00083         xbe_shared->header.kernel_image_thunk_address ^= 0x5B6D40B6;
00084 
00085 /* read headerspace XXX: UGLY*/
00086         file->seek(0);
00087         xbe_shared->headerspace=(char *)malloc(xbe_shared->header.size_of_headers+4);
00088         file->read(xbe_shared->headerspace, xbe_shared->header.size_of_headers);
00089         xbe_shared->headerspace[xbe_shared->header.size_of_headers]=0;
00090         xbe_shared->headerspace[xbe_shared->header.size_of_headers+1]=0;
00091         xbe_shared->headerspace[xbe_shared->header.size_of_headers+2]=0;
00092         xbe_shared->headerspace[xbe_shared->header.size_of_headers+3]=0;
00093 
00094 /* read certificate */  
00095         file->seek(xbe_shared->header.certificate_address-xbe_shared->header.base_address);
00096         file->read(&xbe_shared->certificate, sizeof xbe_shared->certificate);
00097         create_host_struct(&xbe_shared->certificate, XBE_CERTIFICATE_struct, little_endian);
00098 
00099 /* read library versions */
00100         file->seek(xbe_shared->header.library_versions_address-xbe_shared->header.base_address);
00101         
00102         xbe_shared->libraries=(XBE_LIBRARY_VERSION*)malloc(xbe_shared->header.number_of_library_versions * sizeof *xbe_shared->libraries);
00103         file->read(xbe_shared->libraries, xbe_shared->header.number_of_library_versions * sizeof *xbe_shared->libraries);
00104 
00105         for (UINT i=0; i<xbe_shared->header.number_of_library_versions; i++) {
00106                 create_host_struct(&xbe_shared->libraries[i], XBE_LIBRARY_VERSION_struct, little_endian);
00107         }
00108 
00109 /* read section headers */
00110         file->seek(xbe_shared->header.section_header_address-xbe_shared->header.base_address);
00111         
00112         xbe_shared->sections.sections=(XBE_SECTION_HEADER*)malloc(xbe_shared->header.number_of_sections * sizeof *xbe_shared->sections.sections);
00113         file->read(xbe_shared->sections.sections, xbe_shared->header.number_of_sections * sizeof *xbe_shared->sections.sections);
00114 
00115         xbe_shared->sections.number_of_sections=xbe_shared->header.number_of_sections;
00116         xbe_shared->sections.base_address=xbe_shared->header.base_address;
00117 
00118         for (UINT i=0; i<xbe_shared->header.number_of_sections; i++) {
00119                 create_host_struct(&xbe_shared->sections.sections[i], XBE_SECTION_HEADER_struct, little_endian);
00120                 
00121                 // XXX: this is crashable!!!
00122                 xbe_shared->sections.sections[i].section_name_address += (UINT) xbe_shared->headerspace - xbe_shared->header.base_address;
00123                 xbe_shared->sections.sections[i].virtual_address -= xbe_shared->header.base_address;
00124         }
00125 
00126         shared_data = xbe_shared;
00127         
00128         xbe_shared->header.tls_address -= xbe_shared->header.base_address;
00129 
00130         ht_format_group::init_ifs(ifs);
00131 }
00132 
00133 void ht_xbe::done()
00134 {
00135         ht_format_group::done();
00136 
00137         ht_xbe_shared_data *xbe_shared = (ht_xbe_shared_data*)shared_data;
00138 
00139 /*
00140         if (pe_shared->exports.funcs) {
00141                 pe_shared->exports.funcs->destroy();
00142                 delete pe_shared->exports.funcs;
00143         }
00144         if (pe_shared->dimports.funcs) {
00145                 pe_shared->dimports.funcs->destroy();
00146                 delete pe_shared->dimports.funcs;
00147         }
00148         if (pe_shared->dimports.libs) {
00149                 pe_shared->dimports.libs->destroy();
00150                 delete pe_shared->dimports.libs;
00151         }
00152         if (pe_shared->imports.funcs) {
00153                 pe_shared->imports.funcs->destroy();
00154                 delete pe_shared->imports.funcs;
00155         }
00156         if (pe_shared->imports.libs) {
00157                 pe_shared->imports.libs->destroy();
00158                 delete pe_shared->imports.libs;
00159         }
00160 */
00161         free(xbe_shared->sections.sections);
00162 
00163         free(shared_data);
00164 }
00165 
00166 void ht_xbe::loc_enum_start()
00167 {
00168 /*
00169         ht_pe_shared_data *sh=(ht_pe_shared_data*)shared_data;
00170         if (sh->opt_magic==COFF_OPTMAGIC_PE32) {
00171                 loc_enum=1;
00172         } else {
00173                 loc_enum=0;
00174         }
00175 */
00176 }
00177 
00178 bool ht_xbe::loc_enum_next(ht_format_loc *loc)
00179 {
00180         return false;
00181 }
00182 
00183 /*
00184  *      rva conversion routines
00185  */
00186 
00187 bool xbe_rva_to_ofs(xbe_section_headers *section_headers, RVA rva, FILEOFS *ofs)
00188 {
00189         XBE_SECTION_HEADER *s=section_headers->sections;
00190         
00191         for (UINT i=0; i<section_headers->number_of_sections; i++) {
00192                 if ((rva>=s->virtual_address) &&
00193                 (rva<s->virtual_address+s->raw_size)) {
00194                         *ofs=rva-s->virtual_address+s->raw_address;
00195                         return true;
00196                 }
00197                 s++;
00198         }
00199         return false;
00200 }
00201 
00202 bool xbe_rva_to_section(xbe_section_headers *section_headers, RVA rva, int *section)
00203 {
00204         XBE_SECTION_HEADER *s=section_headers->sections;
00205 
00206         for (UINT i=0; i<section_headers->number_of_sections; i++) {
00207                 if ((rva>=s->virtual_address) &&
00208                 (rva<s->virtual_address+MAX(s->virtual_size, s->raw_size))) {
00209                         *section=i;
00210                         return true;
00211                 }
00212                 s++;
00213         }
00214         return false;
00215 }
00216 
00217 bool xbe_rva_is_valid(xbe_section_headers *section_headers, RVA rva)
00218 {
00219         XBE_SECTION_HEADER *s=section_headers->sections;
00220         for (UINT i=0; i<section_headers->number_of_sections; i++) {
00221                 if ((rva>=s->virtual_address) &&
00222                 (rva<s->virtual_address+MAX(s->virtual_size, s->raw_size))) {
00223                         return true;
00224                 }
00225                 s++;
00226         }
00227         return false;
00228 }
00229 
00230 bool xbe_rva_is_physical(xbe_section_headers *section_headers, RVA rva)
00231 {
00232         XBE_SECTION_HEADER *s=section_headers->sections;
00233 
00234         for (UINT i=0; i<section_headers->number_of_sections; i++) {
00235                 if ((rva>=s->virtual_address) &&
00236                 (rva<s->virtual_address+s->raw_size)) {
00237                         return true;
00238                 }
00239                 s++;
00240         }
00241         return false;
00242 }
00243 
00244 /*
00245  *      ofs conversion routines
00246  */
00247 
00248 bool xbe_ofs_to_rva(xbe_section_headers *section_headers, FILEOFS ofs, RVA *rva)
00249 {
00250         XBE_SECTION_HEADER *s=section_headers->sections;
00251         for (UINT i=0; i<section_headers->number_of_sections; i++) {
00252                 if ((ofs>=s->raw_address) &&
00253                 (ofs<s->raw_address+s->raw_size)) {
00254                         *rva=ofs-s->raw_address+s->virtual_address;
00255                         return true;
00256                 }
00257                 s++;
00258         }
00259         return false;
00260 }
00261 
00262 bool xbe_ofs_to_section(xbe_section_headers *section_headers, FILEOFS ofs, int *section)
00263 {
00264         XBE_SECTION_HEADER *s=section_headers->sections;
00265         for (UINT i=0; i<section_headers->number_of_sections; i++) {
00266                 if ((ofs>=s->raw_address) &&
00267                 (ofs<s->raw_address+s->raw_size)) {
00268                         *section=i;
00269                         return true;
00270                 }
00271                 s++;
00272         }
00273         return false;
00274 }
00275 
00276 bool xbe_ofs_to_rva_and_section(xbe_section_headers *section_headers, FILEOFS ofs, RVA *rva, int *section)
00277 {
00278         bool r = xbe_ofs_to_rva(section_headers, ofs, rva);
00279         if (r) {
00280                 r = xbe_ofs_to_section(section_headers, ofs, section);
00281         }
00282         return r;
00283 }
00284 
00285 bool xbe_ofs_is_valid(xbe_section_headers *section_headers, FILEOFS ofs)
00286 {
00287         RVA rva;
00288         return xbe_ofs_to_rva(section_headers, ofs, &rva);
00289 }
00290 
00291 /*
00292  *
00293  */
00294  
00295 bool xbe_section_name_to_section(xbe_section_headers *section_headers, const char *name, int *section)
00296 {
00297         XBE_SECTION_HEADER *s = section_headers->sections;
00298         int slen = strlen(name);
00299 
00300         for (UINT i=0; i < section_headers->number_of_sections; i++) {
00301                 if (strncmp(name, (char *)s->section_name_address, slen) == 0) {
00302                         *section = i;
00303                         return true;
00304                 }
00305                 s++;
00306         }
00307         return false;
00308 }

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