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

rawdump.cpp

Go to the documentation of this file.
00001 /*
00002 PeRdr - PE file disassembler
00003 Copyright (C) 1999-2002 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 "signfile.hpp"
00032 #include "rawdump.h"
00033 #include "codeglob.h"
00034 
00035 using namespace std;
00036 
00037 // !!! funzione copiata da codeprn.cpp !!!
00038 // file generico di dump ??
00039 static void WriteBinary(uint32_t address,const uint8_t* ibuffer,int _len)
00040 {
00041   _PRG_ASSERT(ibuffer != NULL);
00042   _PRG_ASSERT(_len>0);
00043   // remove warning
00044   unsigned len = (unsigned)_len;
00045   const unsigned nCarInRow = 16;
00046   char buffer[3*nCarInRow+nCarInRow+10];
00047   for(unsigned n=0;;++n)
00048   {
00049     unsigned part = n%nCarInRow;
00050     if (part==0)
00051     {
00052       if (n>=nCarInRow)
00053       {
00054         buffer[nCarInRow*3+nCarInRow+1] = '\0';
00055         printf("%08X %s\n",static_cast<uint32_t>(address+n-nCarInRow),buffer);
00056         if (n>=len)
00057           return;
00058       }
00059       // inizializza buffer
00060       memset(buffer,' ',sizeof(buffer));
00061     }
00062     if (n<len)
00063     {
00064       uint8_t car = *ibuffer++;
00065 
00066       // scrive il carattere in esadecimale e carattere relativo
00067       sprintf(buffer+part*3,"%02X",car);
00068       buffer[part*3+2] = part==(nCarInRow/2-1)?'-':' ';
00069       // !!! settare come opzione carattere ?
00070       buffer[nCarInRow*3+1+part] = isprint(car)?car:'.';
00071     }
00072   }
00073 }
00074 
00075 void RawDump(CSignFile& file,uint32_t imageBase,const RVAFileTranslator& rva)
00076 {
00077   // get file size
00078   file.Seek(0,SEEK_END);
00079   uint32_t fileSize = (uint32_t)file.Tell();
00080 
00081   uint32_t startFile = 0;
00082   uint32_t endFile;
00083 
00084   printf("\nRaw dump\n");
00085 
00086   for(unsigned n=0; ; )
00087   {
00088     const PeSection* section = rva.GetSection(n);
00089     uint32_t writeStart = startFile;
00090     int currSection = -1;
00091     if (!section)
00092     {
00093       // no more sections, write last part of file
00094       endFile = fileSize;
00095     }
00096     else
00097     {
00098       if (startFile == section->RawAddress )
00099       {
00100         // write this section
00101         endFile = section->RawAddress + section->RawSize;
00102         writeStart = imageBase + section->RvaAddress;
00103         currSection = n;
00104         ++n;
00105       }
00106       else if (startFile > section->RawAddress )
00107       {
00108         // skip section (probably not file section)
00109         ++n;
00110         continue;
00111       }
00112       else
00113       {
00114         // write part before section
00115         endFile = section->RawAddress;
00116       }
00117     }
00118 
00119     // normalize
00120     if (endFile > fileSize)
00121       endFile = fileSize;
00122 
00123     // write dump of [startFile, endFile)
00124     // exclude empty sections
00125     if (startFile != endFile)
00126     {
00127       if (currSection == -1)
00128         printf("\nFile section\n");
00129       else
00130         printf("\nSection %d\n",currSection);
00131       uint8_t buffer[256];
00132       file.Seek(startFile);
00133       while ( startFile < endFile)
00134       {
00135         unsigned left = (endFile - startFile) > 256 ? 256 : (endFile - startFile);
00136         file.RawRead(buffer,left*sizeof(uint8_t));
00137         WriteBinary(writeStart,buffer,left);
00138         startFile += left;
00139         writeStart += left;
00140       }
00141     }
00142 
00143     startFile = endFile;
00144 
00145     if (endFile == fileSize)
00146       break;
00147   }
00148 }
00149 

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