BinClone/CloneSearcherLib/CSAssemblyFile.cpp
//
// Normalize each line of code based on
//
Normalize each line of code based on
Normalize each line of code based on
bool CCSAssemblyFile::normalizeCode(const CCSParam& param)
{
    CCSAssemblyFunction* pFcn = NULL;
    CCSAssemblyLine* pLine = NULL;
    CCSAssemblyToken* pToken = NULL;
    for (int lineIdx = 0; lineIdx < GetSize(); ++lineIdx) {
        pLine = GetAt(lineIdx);
        for (int tIdx = 0; tIdx < pLine->GetSize(); ++tIdx) {
            pToken = pLine->GetAt(tIdx);
            int opIdx = -1;
            CString newTokenStr;
			if (pToken->getTokenType() == CSTOKEN_TYPE_MNEMONIC) {
					continue;   
			}
            else if (pToken->getTokenType() == CSTOKEN_TYPE_OPMEM) {
                opIdx = m_pAssemblyFileMgr->getOpMems()->getOperandIdx(pToken->getTokenStr());
                newTokenStr = CS_ASSEMBLYTOKEN_OPMEM;
            }
            else if (pToken->getTokenType() == CSTOKEN_TYPE_OPREG) {
                opIdx = m_pAssemblyFileMgr->getOpRegs()->getOperandIdx(pToken->getTokenStr());
                newTokenStr = CCSAssemblyToken::normalizeRegister(pToken->getTokenStr(), param.m_regNormLevel);
            }
            else if (pToken->getTokenType() == CSTOKEN_TYPE_OPVAL) {
                opIdx = m_pAssemblyFileMgr->getOpVals()->getOperandIdx(pToken->getTokenStr());
                newTokenStr = CS_ASSEMBLYTOKEN_OPVAL;
            }
            else if (pToken->getTokenType() == CSTOKEN_TYPE_UNKNOWN)
                continue;
            else {
                tcout << _T("Error: normalizeCode(): unknown token type for token string: ") << pToken->getTokenStr().GetString() << endl;
                ASSERT(false);
                return false;
            }
            if (opIdx == -1) {
                tcout << _T("Error: normalizeCode(): failed to get operand index.") << endl;
                ASSERT(false);
                return false;
            }
            pToken->setTokenStr(newTokenStr); 
        }
    }
    return true;
}
bool CCSAssemblyFile :: normalizeCode ( const CCSParam & param ) { CCSAssemblyFunction * pFcn = NULL ; CCSAssemblyLine * pLine = NULL ; CCSAssemblyToken * pToken = NULL ; for ( int lineIdx = 0 ; lineIdx < GetSize ( ) ; ++ lineIdx ) { pLine = GetAt ( lineIdx ) ; for ( int tIdx = 0 ; tIdx < pLine -> GetSize ( ) ; ++ tIdx ) { pToken = pLine -> GetAt ( tIdx ) ; int opIdx = -1 ; CString newTokenStr ; if ( pToken -> getTokenType ( ) == CSTOKEN_TYPE_MNEMONIC ) { continue ; } else if ( pToken -> getTokenType ( ) == CSTOKEN_TYPE_OPMEM ) { opIdx = m_pAssemblyFileMgr -> getOpMems ( ) -> getOperandIdx ( pToken -> getTokenStr ( ) ) ; newTokenStr = CS_ASSEMBLYTOKEN_OPMEM ; } else if ( pToken -> getTokenType ( ) == CSTOKEN_TYPE_OPREG ) { opIdx = m_pAssemblyFileMgr -> getOpRegs ( ) -> getOperandIdx ( pToken -> getTokenStr ( ) ) ; newTokenStr = CCSAssemblyToken :: normalizeRegister ( pToken -> getTokenStr ( ) , param . m_regNormLevel ) ; } else if ( pToken -> getTokenType ( ) == CSTOKEN_TYPE_OPVAL ) { opIdx = m_pAssemblyFileMgr -> getOpVals ( ) -> getOperandIdx ( pToken -> getTokenStr ( ) ) ; newTokenStr = CS_ASSEMBLYTOKEN_OPVAL ; } else if ( pToken -> getTokenType ( ) == CSTOKEN_TYPE_UNKNOWN ) continue ; else { tcout << _T ( " " ) << pToken -> getTokenStr ( ) . GetString ( ) << endl ; ASSERT ( false ) ; return false ; } if ( opIdx == -1 ) { tcout << _T ( " " ) << endl ; ASSERT ( false ) ; return false ; } pToken -> setTokenStr ( newTokenStr ) ; } } return true ; }
Normalize each line of code based on
Normalize each line of code based on
// For each line in the function // For each token // Get the index of the operand // do nothing for mnemonic (mnemonics expect Call and JMP) // e.g., REG or REGGen32