Make your own free website on Tripod.com
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

data.cpp

Go to the documentation of this file.
00001 /*
00002 PeRdr - PE file disassembler
00003 Copyright (C) 1999-2003 Frediano Ziglio
00004 -----
00005 
00006 This program is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
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 INFORMATION
00022   www: http://freddy77.tripod.com/perdr/
00023   e-mail: freddy77@angelfire.com
00024 */
00025 #include "global.h"
00026 #ifdef HAVE_HDRSTOP
00027 #pragma hdrstop
00028 #endif
00029 
00030 #include <cctype>
00031 #include <algorithm>
00032 #include "codeglob.h"
00033 
00034 using namespace std;
00035 
00036 // Data analisy code
00037 // conta i caratteri della stringa e i caratteri stampabili
00038 // ritorna 0 in tutti e due i risultati se errore di lettura
00039 // !!! aggiungere stringhe Unicode
00040 void CodeParser::GetStringStats(vma_t address,unsigned& len,unsigned& num_printable)
00041 {
00042   try
00043   {
00044                 ObjectModule::DataReader reader = module->GetDataReader(address);
00045     unsigned n = 0;
00046     num_printable = 0;
00047     for (;;)
00048     {
00049       // se rilocazioni ritorna
00050       if (module->HasRelocation()
00051           && module->GetRelocationInfos().GetRelocation(reader.Tell()))
00052       {
00053         len = num_printable = 0;
00054         return;
00055       }
00056 
00057       // se codice occupato ritorna
00058       if (byteInfo.IsOccupied(reader.Tell()))
00059       {
00060         len = num_printable = 0;
00061         return;
00062       }
00063 
00064       uint8_t car = reader.ReadByte();
00065       if (isprint(car))
00066         ++num_printable;
00067       ++n;
00068       if (car == 0)
00069         break;
00070     }
00071     len = n;
00072   }
00073   catch(const ObjectModule::OutOfAddress&)
00074   {
00075     len = num_printable = 0;
00076   }
00077 }
00078 
00079 void CodeParser::GetPointerArrayStats(vma_t address,PointerArrayStat& stat)
00080 {
00081   // inizializza statistiche
00082   stat.nCodePointer = 0;
00083   stat.nPointer     = 0;
00084   stat.nNull        = 0;
00085   stat.nFirstNull   = 0;
00086   bool bFirstNull = false;
00087   try
00088   {
00089                 ObjectModule::DataReader reader = module->GetDataReader(address);
00090     for (unsigned n=0;;++n)
00091     {
00092       // testa sovrapposizione
00093       if (byteInfo.IsOccupied(reader.Tell(),addr_bytes))
00094         break;
00095       // !!!
00096       vma_t addr = reader.ReadDword();
00097       // se puntatore nullo incrementa e testa se primo
00098       if (IsNullAddress(addr))
00099       {
00100         ++stat.nNull;
00101         // se primo puntatore nullo segna posizione
00102         if (!bFirstNull)
00103           stat.nFirstNull = n;
00104         bFirstNull = true;
00105       }
00106       // fermati primo indirizzo invalido non nullo
00107       else if (!module->IsValid(addr))
00108         break;
00109       ++stat.nPointer;
00110       if (module->GetSection(addr)->IsCode())
00111         ++stat.nCodePointer;
00112     }
00113   }
00114   catch(const ObjectModule::OutOfAddress&) {}; // se errore lettura si ferma
00115   // se non trovati puntatori nulli metti il primo puntatore nullo cella dopo
00116   if (!bFirstNull)
00117     stat.nFirstNull = stat.nPointer;
00118 }
00119 
00120 void CodeParser::GetBitStat(vma_t address,unsigned numBytes,BitStat& stat)
00121 {
00122   // init counters
00123   fill(stat.bit8,stat.bit8+8,0);  
00124   fill(stat.bit16,stat.bit16+16,0);  
00125   fill(stat.bit32,stat.bit32+32,0);
00126   
00127   unsigned n;
00128   try
00129   {
00130                 ObjectModule::DataReader reader = module->GetDataReader(address);
00131     for (n=0;n<numBytes;++n)
00132     {
00133       if (byteInfo.IsOccupied(reader.Tell()))
00134         break;
00135       uint8_t byte = reader.ReadByte();
00136 
00137       // increment counters
00138       unsigned *pCount = &stat.bit32[(n%4u)*8u];
00139       for(unsigned n=1;n != (1<<8);++pCount,n<<=1)
00140         if(byte & n)
00141           ++*pCount;
00142     }
00143   }
00144   catch(const ObjectModule::OutOfAddress&) {}; // se errore lettura si ferma
00145 
00146   // group counters
00147   for(n=0;n < 16;++n)
00148     stat.bit16[n] = stat.bit32[n] + stat.bit32[n+16];
00149   for(n=0;n < 8;++n)
00150     stat.bit8[n] = stat.bit16[n] + stat.bit16[n+8];
00151 }

Generated on Mon Jan 13 22:20:34 2003 for perdr by doxygen1.2.15