UnivesalDisassembler(2003)

binonly.cc

Go to the documentation of this file.
00001 // binonly.cc
00002 //
00003 // plugin for uda - universal disassembler
00004 // Date: 2003-05-04
00005 //
00006 // Copyright (c) 2002-2003 Ales Smrcka 
00007 //
00008 // This library is licensed under GNU Library GPL. See the file COPYING.
00009 //
00010 // This library makes possible to read binary only format.
00011 //
00012 
00013 #include <iostream>
00014 #include <string>
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017 #include <fcntl.h>
00018 #include <unistd.h>
00019 #include "shared.h"
00020 using namespace std;
00021 
00022 #define pluginname "binonly.so"
00023 
00024 Parameters *Params;
00025 
00026 extern const char *_syntax;
00027 
00028 extern "C" {
00029 
00030 void plugin_init(int &type, const char *&ident, const char *&syntax, Parameters *p)
00031 {
00032     Params=p;
00033     type=PLUGIN_SECTIONS;
00034     ident="Universal disassembler plugin for binary only format";
00035     syntax=_syntax;
00036 }
00037 
00038 int is_file_defined(const char *file)
00039 {
00040     debug(pluginname<<": is_file_defined...\n");
00041     int result=pdefined("-bof");
00042     if (result)
00043         debug(pluginname<<": is_file_defined...yes\n");
00044     else
00045         debug(pluginname<<": is_file_defined...no\n");
00046     return result;
00047 }
00048 
00049 unsigned hex2uint(const string hex)
00050 {
00051     const char *c=hex.c_str();
00052     if (hex.substr(0, 2)=="0x")
00053         c+=2;
00054     unsigned result=0;
00055     while (*c)
00056     {
00057         if (isdigit(*c))
00058             result=result<<4 | (*c-'0');
00059         else
00060         if (isxdigit(*c))
00061             result=result<<4 | (lower(*c)-'a'+10);
00062         else
00063             return 0;
00064         c++;
00065     }
00066     return result;
00067 }
00068 
00069 unsigned number2uint(const string num)
00070 {
00071     const char *c=num.c_str();
00072     if (num.substr(0, 2)=="0x")
00073         return hex2uint(num);
00074     unsigned result=0;
00075     while (*c)
00076     {
00077         if (isdigit(*c))
00078             result=result*10 + (*c-'0');
00079         else
00080             return 0;
00081         c++;
00082     }
00083     return result;
00084 }
00085 
00086 int load_sections(BinFileData &bfdata)
00087 {
00088     debug(pluginname<<": load_sections...\n");
00089     bfdata.entry=number2uint((*Params)["-bofentry"]);
00090     int in=open(bfdata.name.c_str(), O_RDONLY);
00091     if (in!=-1)
00092     {
00093         struct stat buf;
00094         if (fstat(in, &buf))
00095         {
00096             close(in);
00097             return 0;
00098         }
00099         SectionData *sd = new SectionData;
00100         sd->addr=bfdata.entry;
00101         sd->size=buf.st_size;
00102         sd->executable=!pdefined("-bofnoexec");
00103         sd->name=".bof";
00104         sd->content = new byte[sd->size];
00105         read(in, sd->content, sd->size);
00106         close(in);
00107         bfdata.Sections.push_back(sd);
00108         debug(pluginname<<": load_sections "<<1<<"...ok\n");
00109         return 1;
00110     }
00111     return 0;
00112 }
00113 
00114 } // extern "C"
00115 
00116 const char *_syntax = "\n\
00117     -bof            Activate this plugin for any binary file. Load all binary\n\
00118                     into one section .bof\n\
00119     -bofentry address\n\
00120                     set entry of section .bof\n\
00121     -bofnoexec      unset flag executable at section .bof";