static wbool outputMatch( char *where, size_t read, wbool disp ) { wbool done; char *endrec; int i; done = FALSE; if( where >= &Buff[read] ) { done = TRUE; // this is the string we added to buffer! we're done buffer } else if( disp ) { if( PrtFn ) { if( FOut != NULL ) { fprintf( FOut, FOutFmt, FName ); fprintf( FOut, "\n" ); } if( PrtFiles && !PrtAll ) { printFileName(); } PrtFn = FALSE; } MatchCount++; TotalMatchCount++; if( PrtMatch ) { i = Context + 1; for( ;; ) { // scan back to beginning of the record if( where == Buff ) break; if( *where == '\n' ) { if( --i == 0 ) { ++where; break; } } --where; } endrec = where; i = Context * 2 + 1; while( --i >= 0 ) { do { // find the end of record ++endrec; } while( *endrec != '\n' && endrec != (Buff + read + 1) ); } if( PrtLines ) { printf( "%.5u: ", Recs ); if( Context ) printf( "\r\n" ); } dumpMatch( where, endrec - where + 1 ); } if( QuitFirst || OnePerFile ) { done = TRUE; ExitStatus = 1; } } return( done ); }
static void performSearch( char *fn ) { int io; int retries; // number of times we read the pipe and got nothing int red; int sav; size_t size; unsigned frag; char *locn; unsigned saveRecs; char **currs; char **nexts; char *probe; struct stat buf; MatchCount = 0; if( strcmp( fn, "@@" ) == 0 ) { fn = "stdin"; io = STDIN_FILENO; setmode( io, O_BINARY ); } else { if( FileMode != 0 ) { if( stat( fn, &buf ) != -1 ) { if( (buf.st_mode & FileMode) != 0 ) { return; } } } io = open( fn, O_RDONLY | O_BINARY ); if( io == -1 ) { Warning( "Unable to open", fn ); return; } } FName = fn; retries = 0; // number of times we read the pipe and got nothing if( PrtAll ) { printFileName(); } PrtFn = TRUE; red = readFile( io, Buff, BSize ); if( red == -1 ) { // Warning( "Error reading file", fn ); // removed for 0 length files if( io != STDIN_FILENO ) { close( io ); } return; } // look for two '\n's with no preceeding '\r' probe = (char *)memchr( Buff, '\n', red ); // look for a newline if( ( probe == Buff || ( probe != NULL && probe[-1] != '\r' ) ) && red > 1 ) { probe = (char *)memchr( probe+1, '\n', red - ( probe+1 - Buff ) ); if( probe != NULL && probe[-1] != '\n' ) { OutMode( O_TEXT ); // we'll assume file has no \r's in it } else { OutMode( O_BINARY ); // assume file has \r\n pairs } } else { OutMode( O_BINARY ); } Recs = 1; locn = Buff; size = BSize; for(;;) { retries = 0; // we actually got something sav = red; // preserve old value Buff[ sav + 0 ] = '\0'; Buff[ sav + 1 ] = '\n'; // put this here as a sentinel // (required by searchBuffer) while( red != 0 && Buff[red-1] != '\n' ) { // break on record boundary --red; } if( red == 0 ) red = sav; // if we did not find NL restore red currs = SrchStrings; saveRecs = Recs; // reset line number for each search string while( *currs ) { Recs = saveRecs; strcpy( Buff + sav + 2, *currs ); searchBuffer( *currs, red ); // - search the buffer if( ExitStatus && QuitFirst ) { nexts = currs; for( ;; ) { nexts[ 0 ] = nexts[ 1 ]; if( nexts[ 0 ] == NULL ) break; ++nexts; } if( *SrchStrings ) { ExitStatus = 0; } } else { ++currs; } } if( ExitStatus ) break; frag = (unsigned)(sav - red); if( frag != 0) { memcpy( Buff, &Buff[red], frag ); } locn = &Buff[frag]; size = BSize - frag; red = readFile( io, locn, size ); // read a big hunk of the file if( red == -1 ) { // if nothing read if( frag == 0 ) break; // - and no fragment quit red = 0; // - did not read anything } // endif red += frag; // include size of piece moved } if( io != STDIN_FILENO ) { close( io ); } if( PrtCount && (MatchCount != 0) ) { printf( "Lines: %d\r\n", MatchCount ); } }
void ExportPatch(const std::wstring & templateContent, DBGPATCHINFO* patchList, size_t numPatches) { printFileName(); size_t idx_template = templateContent.find(L"$TEMPLATE_PREFIX:"); size_t idx_module = templateContent.find(L"$MODULE_PREFIX:"); size_t idx_patch = templateContent.find(L"$PATCH:"); size_t idx_module_suffix = templateContent.find(L"$MODULE_SUFFIX:"); size_t idx_template_suffix = templateContent.find(L"$TEMPLATE_SUFFIX:"); if(idx_template == std::wstring::npos || idx_module == std::wstring::npos || idx_patch == std::wstring::npos || idx_module_suffix == std::wstring::npos || idx_template_suffix == std::wstring::npos) { MessageBox(hwndDlg, LoadWideString(IDS_INVALID_PATCH).c_str(), LoadWideString(IDS_PLUGNAME).c_str(), MB_ICONERROR); return; } HANDLE hProcess; wchar_t ProcessName[1024]; memset(ProcessName, 0, sizeof(ProcessName)); hProcess = (HANDLE)DbgValFromString("$hProcess"); if(GetModuleBaseNameW(hProcess, 0, ProcessName, sizeof(ProcessName) / sizeof(wchar_t)) == 0) { MessageBox(hwndDlg, LoadWideString(IDS_HPROCESSFAIL).c_str(), LoadWideString(IDS_PLUGNAME).c_str(), MB_ICONERROR); return; } std::wstring text = templateContent.substr(idx_template + int(wcslen(L"$TEMPLATE_PREFIX:")), idx_module - idx_template - int(wcslen(L"$TEMPLATE_PREFIX:"))); std::wstring modulePrefix = templateContent.substr(idx_module + int(wcslen(L"$MODULE_PREFIX:")), idx_patch - idx_module - int(wcslen(L"$MODULE_PREFIX:"))); std::wstring patchText = templateContent.substr(idx_patch + int(wcslen(L"$PATCH:")), idx_module_suffix - idx_patch - int(wcslen(L"$PATCH:"))); std::wstring moduleSuffix = templateContent.substr(idx_module_suffix + int(wcslen(L"$MODULE_SUFFIX:")), idx_template_suffix - idx_module_suffix - int(wcslen(L"$MODULE_SUFFIX:"))); std::wstring templateSuffix = templateContent.substr(idx_template_suffix + int(wcslen(L"$TEMPLATE_SUFFIX:"))); std::vector<std::pair<std::wstring, unsigned int>> modules; std::string firstModuleUTF8(patchList[0].mod); std::wstring firstModuleUTF16; unsigned int currentModuleCount = 1; utf8::utf8to16(firstModuleUTF8.begin(), firstModuleUTF8.end(), std::back_inserter(firstModuleUTF16)); modules.push_back(std::make_pair(firstModuleUTF16, 1)); for(duint i = 1; i < numPatches; i++) { firstModuleUTF8 = std::string(patchList[i].mod); firstModuleUTF16.clear(); utf8::utf8to16(firstModuleUTF8.begin(), firstModuleUTF8.end(), std::back_inserter(firstModuleUTF16)); if(firstModuleUTF16.compare(modules.back().first) != 0) { modules.back().second = currentModuleCount; currentModuleCount = 1; modules.push_back(std::make_pair(firstModuleUTF16, 1)); } else currentModuleCount++; } modules.back().second = currentModuleCount; currentModuleCount = 0; duint patches = 0; duint modbase; unsigned int currentModule = 0; std::wstring moduleText; for(duint i = 0; i < numPatches; i++) { if(currentModuleCount == 0) { moduleText += modulePrefix + L"\r\n"; modbase = DbgFunctions()->ModBaseFromName(patchList[i].mod); } std::wstring patchText2(patchText); std::wstring newByteText(printByte(patchList[i].newbyte)); ReplaceWString(patchText2, L"$rva", printHex(patchList[i].addr - modbase)); ReplaceWString(patchText2, L"$newByte", newByteText); ReplaceWString(patchText2, L"$patchIndex", printInto(++patches)); moduleText += patchText2 + L"\r\n"; if(currentModuleCount == modules.at(currentModule).second - 1) { moduleText += moduleSuffix + L"\r\n"; ReplaceWString(moduleText, L"$moduleName", modules.at(currentModule).first); ReplaceWString(moduleText, L"$numPatches", printInto(modules.at(currentModule).second)); text += moduleText; moduleText.clear(); currentModuleCount = 0; patches = 0; currentModule++; } else currentModuleCount++; } text.append(templateSuffix); ReplaceWString(text, L"$numPatches", printInto(numPatches)); ReplaceWString(text, L"$exeName", std::wstring(ProcessName)); ReplaceWString(text, L"$date", printTime()); std::wstring compiledate; std::string compiledateASCII(__DATE__); utf8::utf8to16(compiledateASCII.begin(), compiledateASCII.end(), std::back_inserter(compiledate)); ReplaceWString(text, L"$compiledate", compiledate); // save if(SaveFile(exportedname, text)) _plugin_logputs(LoadUTF8String(IDS_SAVESUCCESS).c_str()); else _plugin_logputs(LoadUTF8String(IDS_SAVEFAIL).c_str()); }