home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / pedasm.zip / DasmBase.cpp < prev    next >
C/C++ Source or Header  |  1998-10-23  |  5KB  |  191 lines

  1. /*        fichier DasmBase.cpp : fichier implementation
  2.  *
  3.  *    descr : classe service communs automate desassembleur code donnes
  4.  *    projet : PEDasm
  5.  *    
  6.  *    rq:
  7.  *    Ce programme est libre de droits. Il peut etre distribue et/ou modifie
  8.  *  selon les termes de la licence 'GNU General Public License version 2'.
  9.  *    
  10.  *    Ce programme est distribue sans aucunes garanties, y compris d'utilite 
  11.  *    ni de risques encouru, quelle que soit son utilisation.
  12.  *
  13.  *    lire le fichier licence.txt fourni ou bien ecrire a :
  14.  *    the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  *    pour recevoir une copie de la licence.
  16.  *
  17.  *    Copyright (C) 1997 - 1998 Nicolas Witczak <witczak@geocities.com>
  18.  */
  19.  
  20. #include "Config.h"
  21.  
  22. #include <assert.h>
  23.  
  24. #include "DasmBase.h"
  25. #include "DasmData.h"
  26. #include "DasmCode.h"
  27.  
  28. ////////////////////////////////////////////////////////////////////////
  29. // class CDasm : desassemblage d'une portion de code
  30.  
  31. CDasm::CDasm()
  32. {
  33.     memset( this , 0 , sizeof(CDasm) ) ;
  34. }
  35.  
  36. CDasm& CDasm::SetWindow( CSection* pSection )
  37. {
  38.     m_pSection = pSection ;
  39.     return *this ;
  40. }
  41.  
  42. CDasm& CDasm::SetFile( FILE* pFile ) 
  43. {
  44.     m_pFileOut = pFile ;
  45.     return *this ;
  46. }
  47.  
  48. void CDasm::Run( int iPass  ) 
  49. {
  50.     m_iPass = iPass ;
  51.  
  52. // choix du mode par defaut
  53.     if( m_pSection->IsCode() )
  54.         m_iMode = cteCodeMode ;
  55.     else
  56.         m_iMode = cteDataMode ;
  57. // condition initiale
  58.     m_iIBeginIP = m_pSection->va_begin() ;    
  59.     m_iIP = m_iIBeginIP ;
  60.     m_iLastIP = m_iIP ;
  61.     
  62.     m_pSymbCur = 0 ;
  63.     m_pSymbNext =  GetExe()->FindSymbol( m_iIP ) ;
  64.     if( ( m_pSymbNext != 0 ) && ( m_pSymbNext->m_VirtAdress == m_iIP ) )
  65.     {
  66.         m_pSymbCur = m_pSymbNext ;
  67.         m_pSymbNext = GetExe()->FindSymbol( m_iIP + 1  ) ;
  68.     }
  69.     if( ( m_pSymbNext != 0 ) && ( m_pSymbNext->m_VirtAdress < va_end() ) )
  70.         m_iNextIP = m_pSymbNext->m_VirtAdress ; 
  71.     else
  72.         m_iNextIP = va_end() ;
  73.  
  74.     m_pvBegin = GetExe()->va2ptr( m_iIBeginIP ) ;
  75.     m_pvNext = GetExe()->va2ptr( m_iNextIP ) ;    
  76.     m_pvCur = m_pvBegin ;
  77.  
  78.  
  79.     while( m_iIP < va_end() )
  80.     {            
  81.         Reset();
  82.         if( ( m_iPass == ctePassEcho ) && ( m_pSymbCur != 0 ) && ( m_pSymbCur->m_VirtAdress < va_end() ) )
  83.             fprintf( m_pFileOut ,"\n%s", m_pSymbCur->GetDeclaration() ) ;    
  84.     // choix du mode en fct de la nature du symbol 
  85.         if( m_pSymbCur != 0 )
  86.         {
  87.             if( ( m_pSymbCur->m_Attrib & fSymCode ) != 0 )
  88.                 m_iMode = cteCodeMode ;
  89.             else
  90.                 m_iMode = cteDataMode ;
  91.         }
  92.  
  93.         while( m_iIP < m_iNextIP )
  94.         {
  95.             m_iLastIP = m_iIP ;
  96.             if( ScanNext() && ( m_iIP <= m_iNextIP ) )
  97.             {
  98.                 if( m_iPass == ctePassEcho )
  99.                 {
  100.                     PrintInstr();
  101.                     if( GetExe()->m_bLineNum || GetExe()->m_bRawData )
  102.                         fprintf( m_pFileOut ,"\t\t\t;" ) ;
  103.                     if( GetExe()->m_bLineNum )
  104.                         fprintf( m_pFileOut , "0x%08x :\t" , m_iLastIP ) ;
  105.                     if( GetExe()->m_bRawData )
  106.                     {
  107.                         BYTE* pCur = GetExe()->va2ptr( m_iLastIP );
  108.                         BYTE* pEnd = GetExe()->va2ptr( m_iIP );
  109.                         if( ( pEnd - pCur ) < 32 )
  110.                         {
  111.                             for( ; pCur < pEnd ; ++ pCur )
  112.                             {
  113.                                 fprintf( m_pFileOut ,"%02x" , *pCur );
  114.                             }
  115.                         }
  116.                     }
  117.                     fprintf( m_pFileOut ,"\n" ) ;
  118.                 }
  119.             }
  120.             else
  121.             {    // on change de mode
  122.                 m_iMode = 1 - m_iMode ;
  123.                 m_iIP = m_iLastIP ;
  124.                 m_pvCur = GetExe()->va2ptr( m_iLastIP );            
  125.                 Reset();
  126.                 continue ;
  127.             }
  128.         }
  129.     // iteration bloc inter symbol
  130.         m_iIBeginIP = m_iNextIP ;
  131.         m_iIP = m_iIBeginIP ;
  132.         m_pSymbCur = m_pSymbNext ;
  133.         m_pSymbNext = GetExe()->FindSymbol( m_iIP + 1 ) ;
  134.         if( ( m_pSymbNext != 0 ) && ( m_pSymbNext->m_VirtAdress < va_end() ) )
  135.             m_iNextIP = m_pSymbNext->m_VirtAdress ; 
  136.         else
  137.             m_iNextIP = va_end() ;    
  138.         m_pvBegin = GetExe()->va2ptr( m_iIBeginIP ) ;
  139.         m_pvCur = m_pvBegin  ;
  140.         m_pvNext = GetExe()->va2ptr( m_iNextIP ) ;
  141.     }
  142. }
  143.  
  144. unsigned int CDasm::va_end()
  145. {
  146.     unsigned int uRet ;
  147.     if( m_pSection->IsCode() )
  148.     {
  149.         uRet = m_pSection->va_init_end() ;
  150.         if( uRet == m_pSection->va_begin() ) 
  151.             uRet = m_pSection->va_end() ;
  152.     }
  153.     else
  154.         uRet = m_pSection->va_end() ;
  155.     return uRet  ;
  156. }
  157.  
  158. void CDasm::Reset()
  159. {
  160.     if( m_iMode == cteCodeMode )
  161.         static_cast< CDasmCode* >(this)->Reset() ;
  162.     else
  163.         static_cast< CDasmData* >(this)->Reset() ;
  164. }
  165.  
  166. bool CDasm::ScanNext( ) 
  167. {
  168.     if( m_iMode == cteCodeMode )
  169.         return static_cast< CDasmCode* >(this)->ScanNext(  ) ;
  170.     else
  171.         return static_cast< CDasmData* >(this)->ScanNext(  ) ;
  172. }
  173.  
  174. void CDasm::PrintInstr( ) 
  175. {
  176.     if( m_iMode == cteCodeMode )
  177.         static_cast< CDasmCode* >(this)->PrintInstr( ) ;
  178.     else
  179.         static_cast< CDasmData* >(this)->PrintInstr( ) ;
  180. }
  181.  
  182. //////////////////////////////////////////////////////////////////
  183. // fonctions globales
  184. CDasm* CreateCDasm()
  185. {
  186.     BYTE* pBuff = new BYTE[ max( sizeof(CDasmData) , sizeof(CDasmCode) ) ] ;
  187.     CDasm* pDasm = new( pBuff ) CDasm ;
  188.     return pDasm ;
  189. }
  190.  
  191.