BinClone/CloneSearcherLib/CSAssemblyLine.cpp
// 
// Parse the line into tokens.
//
Parse the line into tokens.
Parse the line into tokens .
bool CCSAssemblyLine::addTokens()
{
    CBFStrParser tokenParser(m_lineStr, CS_ASSEMBLYFILE_DELIMETER);
    CString tokenStr;
    bool bFirstToken = true, bOffsetToken = false, CallorJMP = false;
    while (tokenParser.getNext(tokenStr)) {
        CBFStrHelper::trim(tokenStr);
        if (tokenStr.IsEmpty())
            continue;
        if (tokenStr.Right(1).CompareNoCase(_T(",")) == 0)
            tokenStr.Truncate(tokenStr.GetLength() - 1);
        CCSAssemblyToken* pNewToken = NULL;
        if (bFirstToken) {
            tokenStr = tokenStr.MakeLower();
            if (CCSAssemblyToken::isMnemonic(tokenStr.GetString())) {
                pNewToken = new CCSAssemblyToken(tokenStr, CSTOKEN_TYPE_MNEMONIC);
					CallorJMP = false;
				if (CCSAssemblyToken::isCallorJMPReference(tokenStr.GetString())) {
					Add(pNewToken); 
					CallorJMP = true;
				}
			}
            else {
				 pNewToken = new CCSAssemblyToken(tokenStr, CSTOKEN_TYPE_UNKNOWN);
				 CallorJMP = false;
			}	
            bFirstToken = false;
        }
        else {
            if (CCSAssemblyToken::isMemoryReference(tokenStr))
                pNewToken = new CCSAssemblyToken(tokenStr, CSTOKEN_TYPE_OPMEM);
            else if (CCSAssemblyToken::isRegisterReference(tokenStr))
                pNewToken = new CCSAssemblyToken(tokenStr, CSTOKEN_TYPE_OPREG);
            else if (CCSAssemblyToken::isConstantValue(tokenStr) || bOffsetToken) {
                pNewToken = new CCSAssemblyToken(tokenStr, CSTOKEN_TYPE_OPVAL);
                bOffsetToken = false;
            }
            else {
                pNewToken = new CCSAssemblyToken(tokenStr, CSTOKEN_TYPE_UNKNOWN);
            }
            bOffsetToken = tokenStr.CompareNoCase(CS_ASSEMBLYFILE_OFFSET) == 0;
        }
		if (!CallorJMP) {
			try {
				Add(pNewToken);
			}
			catch (CMemoryException&) {
				tcout << _T("CCSAssemblyLine: failed to add token from line: ") << m_lineStr.GetString() << endl;
				ASSERT(false);
				return false;
			}
		}
    }
    return true;
}
bool CCSAssemblyLine :: addTokens ( ) { CBFStrParser tokenParser ( m_lineStr , CS_ASSEMBLYFILE_DELIMETER ) ; CString tokenStr ; bool bFirstToken = true , bOffsetToken = false , CallorJMP = false ; while ( tokenParser . getNext ( tokenStr ) ) { CBFStrHelper :: trim ( tokenStr ) ; if ( tokenStr . IsEmpty ( ) ) continue ; if ( tokenStr . Right ( 1 ) . CompareNoCase ( _T ( " " ) ) == 0 ) tokenStr . Truncate ( tokenStr . GetLength ( ) - 1 ) ; CCSAssemblyToken * pNewToken = NULL ; if ( bFirstToken ) { tokenStr = tokenStr . MakeLower ( ) ; if ( CCSAssemblyToken :: isMnemonic ( tokenStr . GetString ( ) ) ) { pNewToken = new CCSAssemblyToken ( tokenStr , CSTOKEN_TYPE_MNEMONIC ) ; CallorJMP = false ; if ( CCSAssemblyToken :: isCallorJMPReference ( tokenStr . GetString ( ) ) ) { Add ( pNewToken ) ; CallorJMP = true ; } } else { pNewToken = new CCSAssemblyToken ( tokenStr , CSTOKEN_TYPE_UNKNOWN ) ; CallorJMP = false ; } bFirstToken = false ; } else { if ( CCSAssemblyToken :: isMemoryReference ( tokenStr ) ) pNewToken = new CCSAssemblyToken ( tokenStr , CSTOKEN_TYPE_OPMEM ) ; else if ( CCSAssemblyToken :: isRegisterReference ( tokenStr ) ) pNewToken = new CCSAssemblyToken ( tokenStr , CSTOKEN_TYPE_OPREG ) ; else if ( CCSAssemblyToken :: isConstantValue ( tokenStr ) || bOffsetToken ) { pNewToken = new CCSAssemblyToken ( tokenStr , CSTOKEN_TYPE_OPVAL ) ; bOffsetToken = false ; } else { pNewToken = new CCSAssemblyToken ( tokenStr , CSTOKEN_TYPE_UNKNOWN ) ; } bOffsetToken = tokenStr . CompareNoCase ( CS_ASSEMBLYFILE_OFFSET ) == 0 ; } if ( ! CallorJMP ) { try { Add ( pNewToken ) ; } catch ( CMemoryException & ) { tcout << _T ( " " ) << m_lineStr . GetString ( ) << endl ; ASSERT ( false ) ; return false ; } } } return true ; }
Parse the line into tokens.
Parse the line into tokens .
// Trim comma // Mnemonic // add the mnemonics but skip the operand // Operands //tcout << _T("Unknown token string: ") << tokenStr.GetString() << endl; // Is offset string?