home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / pedasm.zip / DasmData.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-24  |  5.8 KB  |  218 lines

  1. /*        fichier DasmData.cpp : fichier implementation
  2.  *
  3.  *    descr : classe automate desassemblage des constantes
  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 "DasmData.h"
  23. #include <cassert>
  24.  
  25.  
  26. ////////////////////////////////////////////////////////////////////////
  27. // class CDasmDataData : desassemblage d'une portion de code
  28.  
  29. CDasmData::CDasmData( )
  30. {}
  31.  
  32.  
  33. bool CDasmData::PassScan()
  34. {
  35.     CExeRep::CSymbolColl::iterator iter 
  36.         = GetExe()->m_arRelocSymbols.lower_bound( &CSymbol( m_iIP ) );
  37.     for( ; ( iter != GetExe()->m_arRelocSymbols.end() )
  38.         && ( (*iter)->m_VirtAdress < m_iNextIP ) ; iter ++ )
  39.     {
  40.         CSymbol* pRelocSymb = *iter ;
  41.         CExeRep::CSymbolColl::const_iterator prevSym = GetExe()->m_arSymbols.find( & CSymbol( pRelocSymb->m_VirtAdress ) ) ;
  42.         DWORD dwSymbVal = *(DWORD*)(GetExe()->va2ptr( pRelocSymb->m_VirtAdress )) ;
  43.         CSection* pCurSection = GetExe()->GetSectFromVA( dwSymbVal ) ;
  44.         if( pCurSection != 0 )
  45.         {
  46.             if( pCurSection->IsCode() )
  47.                 GetExe()->AddSymbol( dwSymbVal , pRelocSymb->m_VirtAdress , true , cSymCallBack ) ;    
  48.             else
  49.                 GetExe()->AddSymbol( dwSymbVal , pRelocSymb->m_VirtAdress , true , cSymData ) ;    
  50.             if( prevSym == GetExe()->m_arSymbols.end() )
  51.             {
  52.                 if( m_pSection->IsData() )
  53.                     GetExe()->AddSymbol( pRelocSymb->m_VirtAdress ,0,true, cSymDataPtr ) ;
  54.             }
  55.             else
  56.             {
  57.                 if( ( (*prevSym)->m_Attrib & fSymPointer ) == 0 )
  58.                     (*prevSym)->m_Attrib = cSymDataPtr ; 
  59.             }
  60.         }
  61.         else    // improbable : signifie que la relocation pointe a l'exterieur de l'espace 
  62.                 // d'adressage de l'exe cible
  63.             GetExe()->AddSymbol( dwSymbVal , pRelocSymb->m_VirtAdress , true , cSymUnknown ) ;
  64.     }
  65.     m_iIP = m_iNextIP ; 
  66.     return true ;
  67. }
  68.  
  69. bool CDasmData::PassEcho()
  70. {
  71.     static char_buff pszTemp ;
  72.     unsigned int iSymbSize ;
  73.     int iLineWrap = 8 ;
  74.     iSymbSize = m_iNextIP - m_iIP ;
  75.  
  76. // sortie
  77.     if( m_pvCur >= m_pSection->init_end() ) // partie section non initialisee
  78.         fprintf( m_pFileOut ,"\tbyte %i dup(?)\n" , iSymbSize );
  79.     else
  80.     {
  81.         iLineWrap = 8 ;
  82.     // adresse d'un autre symbol    
  83.         if( ( m_pSymbCur != 0 ) && ( m_pSymbCur->m_Attrib & fSymPointer ) != 0 ) 
  84.         {            
  85.             fprintf( m_pFileOut , "\tdword %s\n" , GetExe()->GetSymbName( *(DWORD*)m_pvCur , m_pSymbCur->m_VirtAdress  ,false ,cteDecorate | cteDecOffset ) );
  86.             m_pvCur += 4 ;
  87.         }
  88.     // cas particuliers
  89.         else if( iSymbSize == 2 )    // un seul word
  90.         {
  91.             fprintf( m_pFileOut , "\tword %s" , GetExe()->GetValue( *(WORD*)m_pvCur , 0 ) );
  92.             m_pvCur += 2 ;
  93.         }
  94.         else if( iSymbSize == 4 )    // un seul dword
  95.         {
  96.             fprintf( m_pFileOut , "\tdword %s" , GetExe()->GetValue( *(DWORD*)m_pvCur , 0 ) );
  97.             m_pvCur += 4 ;
  98.         }
  99.     // iteration octet suivant
  100.         while( m_pvCur < m_pvNext )
  101.         {
  102.             int iNumreq ;
  103.             CExeRep::symb_iterator iterReloc = GetExe()->m_arRelocSymbols.find( &CSymbol( GetExe()->ptr2va( m_pvCur ) ) ) ;
  104.             
  105.             if( iterReloc != GetExe()->m_arRelocSymbols.end() )
  106.             {
  107.                 if( iLineWrap != 8 )
  108.                     fprintf( m_pFileOut , "\n" ) ;    
  109.                 fprintf( m_pFileOut , "\tdword %s\n" , GetExe()->GetSymbName( *(DWORD*)m_pvCur , m_pSymbCur->m_VirtAdress  ,true ,cteDecorate | cteDecOffset ) );        
  110.                 m_pvCur += 4 ;
  111.                 iLineWrap = 8 ;
  112.             }
  113.             else if( ( iNumreq = ReqString( m_pvCur , m_pvNext , pszTemp ) ) > 0 )
  114.             {                    
  115.                 m_pvCur += iNumreq ;
  116.                 if( iLineWrap != 8 )
  117.                     fprintf( m_pFileOut , "\n" ) ;
  118.                 fprintf( m_pFileOut , "\tbyte %s\n" , pszTemp );                    
  119.                 iLineWrap = 8 ;
  120.             }
  121.             else
  122.             {
  123.                 iNumreq = ReqDup( m_pvCur , m_pvNext , pszTemp ) ;
  124.                 if( iNumreq > 0  )
  125.                     m_pvCur += iNumreq ;
  126.                 else
  127.                     strcpy( pszTemp , GetExe()->GetValue( *m_pvCur++ , 0 ) ) ; 
  128.                 if( iLineWrap == 0 )
  129.                 {
  130.                     iLineWrap = 8 ;
  131.                     fprintf( m_pFileOut ,"\n");
  132.                 }
  133.                 if( iLineWrap == 8 )
  134.                     fprintf( m_pFileOut ,"\tbyte %s " , pszTemp );
  135.                 else
  136.                     fprintf( m_pFileOut ," , %s ", pszTemp ) ;
  137.                 iLineWrap -- ;
  138.             }
  139.         }
  140.     };
  141. // iteration symbol suivante        
  142.     fprintf( m_pFileOut , "\n" ) ;
  143.     m_iIP = m_iNextIP ; 
  144.     return true ;
  145. }
  146.  
  147. bool CDasmData::ScanNext()
  148. {
  149.     if( m_iPass == ctePassScan )
  150.         return PassScan();
  151.     else
  152.         return PassEcho();
  153. }
  154.  
  155. void CDasmData::PrintInstr()
  156. {
  157. }
  158.  
  159. void CDasmData::Reset() 
  160. {
  161. }
  162.  
  163. int CDasmData::ReqString( BYTE* pszCur,BYTE* pszEnd , char* pszBuff )
  164. {
  165.     int iRep = 0 ;
  166.     BYTE* pszIn ;
  167.     for( pszIn = pszCur ; pszIn < pszEnd ; ++ pszIn )
  168.     {    
  169.         if( iRep > 126 )
  170.             break ;
  171.         if( ( *pszIn >= 32 ) && ( *pszIn < 127 ) )
  172.             ++ iRep ;
  173.         else 
  174.             break ;
  175.     }
  176.     if( iRep >= 4 )
  177.     {
  178.         // double quotation des '
  179.         char* pszOut = pszBuff ;
  180.         *pszOut++ = '\'' ;
  181.         for( pszIn = pszCur ; pszIn < ( pszCur + iRep ) ; ++pszIn )
  182.         {
  183.             if( *pszIn == '\'' )
  184.                 *pszOut++ = '\'' ;
  185.             *pszOut++ = *pszIn ;
  186.         }    
  187.         *pszOut++ = '\'' ;
  188.         *pszOut = '\0' ;
  189.         return iRep ;
  190.     }
  191.     else
  192.         return 0 ;
  193. }
  194.  
  195. int CDasmData::ReqDup( BYTE* pszCur,BYTE* pszEnd , char* pszBuff )
  196. {
  197.     int iRep = 0 ;
  198.     BYTE cRep ;
  199.     for( cRep = *pszCur ; pszCur < pszEnd ; ++ pszCur)
  200.     {
  201.         if( cRep == *pszCur )
  202.             ++ iRep ;
  203.         else 
  204.             break ;
  205.     }
  206.     if( iRep >= 4 )
  207.     {
  208.         sprintf( pszBuff , "%i dup(%s)" , iRep , GetExe()->GetValue( cRep , 0 ) ) ;
  209.         return iRep ;
  210.     }
  211.     else
  212.         return 0 ;
  213. }
  214.  
  215. //////////////////////////////////////////////////////////////////
  216. // fonctions globales
  217.  
  218.