void qtDLGAssembler::InsertNewInstructions()
{
	if(lineEdit->text().length() <= 0)
	{
		close();
		return;
	}

	QMap<quint64,DisAsDataRow>::const_iterator i = m_pCurrentDisassembler->SectionDisAs.constFind(m_instructionOffset);
	if(i == m_pCurrentDisassembler->SectionDisAs.constEnd()) 
	{
		close();
		return;
	}

	QString oldOpcodes = i.value().OpCodes;
	DWORD oldOpcodeLen = oldOpcodes.replace(" ", "").length() / 2,
		newOpcodeLen = NULL;
		
	QFile tempOutput("nanomite.asm");
	tempOutput.open(QIODevice::WriteOnly | QIODevice::Text);
	QTextStream out(&tempOutput);

	if(m_is64Bit)
		out << "BITS 64\n";
	else
		out << "BITS 32\n";

	out << "org 0x" << hex << i.value().Offset << "\r\n";
	out << lineEdit->text();
	tempOutput.close();

	QProcess nasm;
	nasm.setReadChannel(QProcess::StandardOutput);
    nasm.setProcessChannelMode(QProcess::MergedChannels);
    nasm.start("nasm.exe -o nanomite.bin nanomite.asm");
    if (!nasm.waitForStarted())
	{
		QMessageBox::critical(this, "Nanomite", "Unable to launch assembler!", QMessageBox::Ok, QMessageBox::Ok);
		close();
		return;
	}

	while(nasm.state() != QProcess::NotRunning)
	{
        nasm.waitForReadyRead();
		QString errorMessage = nasm.readAll();

		if(errorMessage.contains("nanomite.asm:3:"))
		{
			errorMessage.replace("nanomite.asm:3:","");
			QMessageBox::critical(this, "Nanomite", errorMessage, QMessageBox::Ok, QMessageBox::Ok);
			lineEdit->clear();
			return;
		}
    }
	DeleteFile(L"nanomite.asm");

	HANDLE hFile = CreateFileW(L"nanomite.bin",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
	if(hFile == INVALID_HANDLE_VALUE)
	{
		lineEdit->clear();
		return;
	}

	int iLen = GetFileSize(hFile,NULL);
	LPVOID pFileBuffer = clsMemManager::CAlloc(iLen);
	DWORD BytesRead = NULL;	
	if(!ReadFile(hFile,pFileBuffer,iLen,&BytesRead,NULL))
	{
		CloseHandle(hFile);
		DeleteFile(L"nanomite.bin");
		clsMemManager::CFree(pFileBuffer);
		QMessageBox::critical(this,"Nanomite","no valid opcodes found!",QMessageBox::Ok,QMessageBox::Ok);
		lineEdit->clear();
		return;
	}
	CloseHandle(hFile);
	DeleteFile(L"nanomite.bin");


	if(BytesRead <= 0)
	{
		clsMemManager::CFree(pFileBuffer);
		QMessageBox::critical(this,"Nanomite","no valid opcodes found!",QMessageBox::Ok,QMessageBox::Ok);
		lineEdit->clear();
		return;
	}

	if(oldOpcodeLen >= BytesRead)
		newOpcodeLen = oldOpcodeLen;
	else if(oldOpcodeLen < BytesRead)
	{
		newOpcodeLen = oldOpcodeLen;
		while(newOpcodeLen < BytesRead)
		{
			++i;
			if(i == m_pCurrentDisassembler->SectionDisAs.constEnd()) return;
			oldOpcodes = i.value().OpCodes;
			newOpcodeLen += oldOpcodes.replace(" ", "").length() / 2;
		}
	}

	LPVOID pBuffer = clsMemManager::CAlloc(newOpcodeLen);
	memset(pBuffer,0x90,newOpcodeLen);
	memcpy(pBuffer,pFileBuffer,BytesRead);

	qtDLGPatchManager::AddNewPatch(GetProcessId(m_processHandle), m_processHandle, m_instructionOffset, newOpcodeLen, pBuffer);

	clsMemManager::CFree(pBuffer);
	clsMemManager::CFree(pFileBuffer);

	lineEdit->clear();
	close();
}
void qtDLGAssembler::InsertNewInstructions()
{
	if(lineEdit->text().length() <= 0)
	{
		close();
		return;
	}

	QMap<QString,DisAsDataRow>::const_iterator i = m_pCurrentDisassembler->SectionDisAs.constFind(QString("%1").arg(m_instructionOffset,16,16,QChar('0')).toUpper());
	if((QMapData::Node *)i == (QMapData::Node *)m_pCurrentDisassembler->SectionDisAs.constEnd()) 
	{
		close();
		return;
	}

	QString oldOpcodes = i.value().OpCodes;
	DWORD oldOpcodeLen = oldOpcodes.replace(" ", "").length() / 2,
		newOpcodeLen = NULL;
		
	QFile tempOutput("nanomite.asm");
	tempOutput.open(QIODevice::WriteOnly | QIODevice::Text);
	QTextStream out(&tempOutput);

	if(m_is64Bit)
		out << "BITS 64\r\n";
	else
		out << "BITS 32\r\n";
	out << lineEdit->text();
	tempOutput.close();


	STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si,sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi,sizeof(pi));
	TCHAR szCommandLine[] = L"nasm.exe -o nanomite.bin nanomite.asm";

	if(!CreateProcess(NULL,szCommandLine,NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,&si,&pi)) 
    {
        MessageBoxW(NULL,L"Error, unable to launch assembler!",L"Nanomite",MB_OK);
		close();
		return;
    }

    WaitForSingleObject(pi.hProcess,INFINITE);
	CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
	DeleteFile(L"nanomite.asm");

	HANDLE hFile = CreateFileW(L"nanomite.bin",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
	if(hFile == INVALID_HANDLE_VALUE)
	{
		close();
		return;
	}

	int iLen = GetFileSize(hFile,NULL);
	LPVOID pFileBuffer = clsMemManager::CAlloc(iLen);
	DWORD BytesRead = NULL;	
	if(!ReadFile(hFile,pFileBuffer,iLen,&BytesRead,NULL))
	{
		CloseHandle(hFile);
		DeleteFile(L"nanomite.bin");
		clsMemManager::CFree(pFileBuffer);
		MessageBoxW(NULL,L"Error, no valid opcodes found!",L"Nanomite",MB_OK);
		close();
		return;
	}
	CloseHandle(hFile);
	DeleteFile(L"nanomite.bin");


	if(BytesRead <= 0)
	{
		clsMemManager::CFree(pFileBuffer);
		MessageBoxW(NULL,L"Error, no valid opcodes found!",L"Nanomite",MB_OK);
		close();
		return;
	}

	if(oldOpcodeLen >= BytesRead)
		newOpcodeLen = oldOpcodeLen;
	else if(oldOpcodeLen < BytesRead)
	{
		newOpcodeLen = oldOpcodeLen;
		while(newOpcodeLen < BytesRead)
		{
			++i;
			if((QMapData::Node *)i == (QMapData::Node *)m_pCurrentDisassembler->SectionDisAs.constEnd()) return;
			oldOpcodes = i.value().OpCodes;
			newOpcodeLen += oldOpcodes.replace(" ", "").length() / 2;
		}
	}

	LPVOID pBuffer = clsMemManager::CAlloc(newOpcodeLen);
	memset(pBuffer,0x90,newOpcodeLen);
	memcpy(pBuffer,pFileBuffer,BytesRead);

	qtDLGPatchManager::AddNewPatch(0,m_processHandle,m_instructionOffset,newOpcodeLen,pBuffer);

	clsMemManager::CFree(pBuffer);
	clsMemManager::CFree(pFileBuffer);

	m_pCurrentDisassembler->SectionDisAs.clear();
	lineEdit->clear();
	close();
}
Exemple #3
0
int createHTKData(const string& lipiRootPath,const string& featureFilePath,const string& tempDir,int& outNumFeatures)
{

	string htkFeatFilesDestDir;
	string strNumFeatures;
	string statisticsFilePath;
	string cmdLine;

	string htkInstallationDir;

	#ifdef WIN32

		htkInstallationDir = lipiRootPath + SEPARATOR + "externaltools" + SEPARATOR + "htk" + SEPARATOR + "windows";
	#else
		htkInstallationDir = lipiRootPath + SEPARATOR + "externaltools" + SEPARATOR + "htk" + SEPARATOR + "linux";
	#endif
	
	
	string htkConfigFilePath = htkInstallationDir+string(SEPARATOR)+"config";

	ifstream checkHTKConfigExists(htkConfigFilePath.c_str());

	if(!checkHTKConfigExists)
	{
		cout<<"Unable to access HTK's config file:"<<htkConfigFilePath<<endl;
		return 1;
	}

	checkHTKConfigExists.close();

	cmdLine = "mkdir "+tempDir;

	system(cmdLine.c_str());

	statisticsFilePath = tempDir + string(SEPARATOR) + "class-train-statistics.txt";

	ofstream doesExist(statisticsFilePath.c_str());

	if(!doesExist)
	{

		cout<<"Unable to create files at:"<<tempDir<<endl;
		return 1;
	}

	doesExist.close();


	htkFeatFilesDestDir = tempDir+string(SEPARATOR)+"htktraindata";

	cmdLine = "mkdir "+htkFeatFilesDestDir;

	system(cmdLine.c_str());

	ifstream input(featureFilePath.c_str());

	if(input)
	{
		string eachLine;
		getline(input,eachLine,'\n'); //simply reading the first line of the feature file

		bool isSecondLine = true;

		int classId;

		int numPoints;

		int lineNumber = 1;

		while(input)
		{
			eachLine = "";

			++lineNumber;

			getline(input,eachLine,'\n');

			if(!eachLine.empty())
			{
				vector<string> tokens;
				LTKStringUtil::tokenizeString(eachLine," ",tokens);

				if(tokens.size()!=2)
				{
					cout<<"Error reading feature file at line number:"<<lineNumber<<endl;
					return 1;
				}

				classId  =atoi(tokens[0].c_str());

				vector<string> strFeaturePointsVec;
				LTKStringUtil::tokenizeString(tokens[1],"|",strFeaturePointsVec);

				numPoints = strFeaturePointsVec.size();
								

				if(numPoints <=0)
				{
					cout<<"Error reading feature file at line number:"<<lineNumber<<endl;
					return 1;
				}

				if(isSecondLine)
				{
					vector<string> tempTokens;
					LTKStringUtil::tokenizeString(strFeaturePointsVec[0],",",tempTokens);
					char buff[50];
					sprintf(buff,"%d",tempTokens.size()-1); //-1 to ignore the pen-up bit feature
					strNumFeatures = string(buff);
					outNumFeatures = atoi(strNumFeatures.c_str());
					isSecondLine = false;
				}

				if(classStatistics.find(classId)!=classStatistics.end())
				{
					  classStatistics[classId].numSamples+=1;
					  classStatistics[classId].totalNumPoints+=numPoints;

				}
				else
				{
					 ClassDetails cd;
					 cd.numSamples=1;
					 cd.totalNumPoints=numPoints;
					 classStatistics[classId]=cd;
					 string classDir=htkFeatFilesDestDir+SEPARATOR+tokens[0];
					 
					 cmdLine = "mkdir "+classDir;
					 system(cmdLine.c_str());
				}
				string tempFilePath = tempDir+string(SEPARATOR)+"tmp.txt";

				ofstream tempOutput(tempFilePath.c_str());

				for(int i=0;i<strFeaturePointsVec.size();++i)
				{
					vector<string> tempTokens;
					LTKStringUtil::tokenizeString(strFeaturePointsVec[i],",",tempTokens);

					for(int ii=0;ii<(tempTokens.size()-1);++ii) //-1 to ignore the pen-up bit feature
					{
						tempOutput<<tempTokens[ii]<<" ";
					}
				}
				tempOutput<<endl;
				tempOutput.close();

				char buffer[100];
				sprintf( buffer,"%03d",(classStatistics[classId].numSamples)-1);
				string strSampleIndex(buffer);

				char buff[100];
				sprintf(buff,"%d",strFeaturePointsVec.size());
				string strNumPoints(buff);

				string htkFeatFilePath=htkFeatFilesDestDir+SEPARATOR+tokens[0]+string(SEPARATOR)+"train_class"+tokens[0]+"_"+strSampleIndex;


				cmdLine=htkInstallationDir+string(SEPARATOR)+"ascii_htk "+tempFilePath+" "+strNumFeatures+" "+strNumPoints+" "+htkFeatFilePath;
				system(cmdLine.c_str());
				remove(tempFilePath.c_str());


			}
		}
	}
	else
	{
		cout<<"Unable to acess feature file:"<<featureFilePath<<endl;
		return 1;
	}

	input.close();

   ofstream output(statisticsFilePath.c_str());
   output<<"#Class_ID AVG_NUM_POINTS NUM_SAMPLES"<<endl;
   for(map<int,ClassDetails>::iterator iter=classStatistics.begin();iter!=classStatistics.end();++iter)
   {
	output<<iter->first<<" ";
	int numOfSamples=(iter->second).numSamples;
	int avgNumPoints=(iter->second).totalNumPoints/numOfSamples;
	output<<avgNumPoints<<" "<<numOfSamples<<endl;
   }
   output.close();
	
   return 0;

}