bool getFileMd5(unsigned char *md5, int len) { if (len < MD5_LEN) { return false; } #if IDA_SDK_VERSION >= 500 retrieve_input_file_md5(md5); #else #define RIDX_MD5 1302 //MD5 of the input file if (RootNode.supval(RIDX_MD5, md5, MD5_LEN) != MD5_LEN) { char buf[512]; get_input_file_path(buf, sizeof(buf)); FILE *f = qfopen(buf, "rb"); if (f) { MD5Context ctx; MD5Init(&ctx); int len; while ((len = qfread(f, buf, sizeof(buf))) > 0) { MD5Update(&ctx, (unsigned char*)buf, len); } MD5Final(md5, &ctx); RootNode.supset(RIDX_MD5, md5, MD5_LEN); qfclose(f); } else { //failed to open input file return false; } } #endif return true; }
void simulation::get_results(uint32 id, uint32& n_total_faults, uint32& n_faults) { int ret; char *str = read_file(get_output_file_path(id).c_str()); char *str_s = strstr (str,"0,"); n_total_faults = 0; n_faults = 0; ret = sscanf(str_s, "0,%d.0\n1,%d.0\n", (int*)&n_total_faults, (int*)&n_faults); if (ret != 2) LOG("sim_events", "simulation", "parsing error... ret is %d\n", ret); LOG("sim_events", "simulation", "#total_faults = %d", n_total_faults); LOG("sim_events", "simulation", "#faults_detected = %d\n", n_faults); remove(get_output_file_path(id).c_str()); remove(get_input_file_path(id).c_str()); delete[] str; }
bool simulation::setup_input_file(string dna, uint32 id) { fstream sim_file; sim_file.open(get_input_file_path(id).c_str(), fstream::out); if (!sim_file.is_open()) { perror("input_file"); exit(0); } int clocks = GetStrRowSize(dna); int inputs = GetStrColSize(dna); //LOG("sim_events", "simulation", "clocks: %d", clocks); //LOG("sim_events", "simulation", "inputs: %d", inputs); stringstream out; out << clocks; sim_file << out.str(); sim_file << ";" << endl; for (uint32 i = 0 ; i < inputs; i++) { for (uint32 j = 0 ; j < clocks; j++) { sim_file << dna.c_str()[i * (clocks +1) + j]; if (j != (clocks - 1)) sim_file << ","; } sim_file << ";" << endl; } sim_file.close(); return true; }
static void idaapi run(int /* arg */) { static char mapFileName[_MAX_PATH] = { 0 }; // If user press shift key, show options dialog if (GetAsyncKeyState(VK_SHIFT) & 0x8000) { ShowOptionsDlg(); } ulong numOfSegs = (ulong) get_segm_qty(); if (0 == numOfSegs) { warning("Not found any segments"); return; } if ('\0' == mapFileName[0]) { // First run strncpy(mapFileName, get_input_file_path(), sizeof(mapFileName)); WIN32CHECK(PathRenameExtension(mapFileName, ".map")); } // Show open map file dialog char *fname = askfile_c(0, mapFileName, "Open MAP file"); if (NULL == fname) { msg("LoadMap: User cancel\n"); return; } // Open the map file LPSTR pMapStart = NULL; DWORD mapSize = INVALID_FILE_SIZE; MAP_OPEN_ERROR eRet = MapFileOpen(fname, pMapStart, mapSize); switch (eRet) { case WIN32_ERROR: warning("Could not open file '%s'.\nWin32 Error Code = 0x%08X", fname, GetLastError()); return; case FILE_EMPTY_ERROR: warning("File '%s' is empty, zero size", fname); return; case FILE_BINARY_ERROR: warning("File '%s' seem to be a binary or Unicode file", fname); return; case OPEN_NO_ERROR: default: break; } bool foundHdr = false; ulong validSyms = 0; ulong invalidSyms = 0; // The mark pointer to the end of memory map file // all below code must not read or write at and over it LPSTR pMapEnd = pMapStart + mapSize; show_wait_box("Parsing and applying symbols from the Map file '%s'", fname); __try { LPSTR pLine = pMapStart; LPSTR pEOL = pMapStart; while (pLine < pMapEnd) { // Skip the spaces, '\r', '\n' characters, blank lines, seek to the // non space character at the beginning of a non blank line pLine = SkipSpaces(pEOL, pMapEnd); // Find the EOL '\r' or '\n' characters pEOL = FindEOL(pLine, pMapEnd); size_t lineLen = (size_t) (pEOL - pLine); if (lineLen < g_minLineLen) { continue; } if (!foundHdr) { if ((0 == strnicmp(pLine, VC_HDR_START , lineLen)) || (0 == strnicmp(pLine, BL_HDR_NAME_START , lineLen)) || (0 == strnicmp(pLine, BL_HDR_VALUE_START, lineLen))) { foundHdr = true; } } else { ulong seg = SREG_NUM; ulong addr = BADADDR; char name[MAXNAMELEN + 1]; char fmt[80]; name[0] = '\0'; fmt[0] = '\0'; // Get segment number, address, name, by pass spaces at beginning, // between ':' character, between address and name int ret = _snscanf(pLine, min(lineLen, MAXNAMELEN + g_minLineLen), " %04X : %08X %s", &seg, &addr, name); if (3 != ret) { // we have parsed to end of value/name symbols table or reached EOF _snprintf(fmt, sizeof(fmt), "Parsing finished at line: '%%.%ds'.\n", lineLen); ShowMsg(fmt, pLine); break; } else if ((0 == seg) || (--seg >= numOfSegs) || (BADADDR == addr) || ('\0' == name[0])) { sprintf(fmt, "Invalid map line: %%.%ds.\n", lineLen); ShowMsg(fmt, pLine); invalidSyms++; } else { // Ensure name is NULL terminated name[MAXNAMELEN] = '\0'; // Determine the DeDe map file bool bNameApply = g_options.bNameApply; char *pname = name; if (('<' == pname[0]) && ('-' == pname[1])) { // Functions indicator symbol of DeDe map pname += 2; bNameApply = true; } else if ('*' == pname[0]) { // VCL controls indicator symbol of DeDe map pname++; bNameApply = false; } else if (('-' == pname[0]) && ('>' == pname[1])) { // VCL methods indicator symbol of DeDe map pname += 2; bNameApply = false; } ulong la = addr + getnseg((int) seg)->startEA; flags_t f = getFlags(la); if (bNameApply) // Apply symbols for name { // Add name if there's no meaningful name assigned. if (g_options.bReplace || (!has_name(f) || has_dummy_name(f) || has_auto_name(f))) { if (set_name(la, pname, SN_NOWARN)) { ShowMsg("%04X:%08X - Change name to '%s' successed\n", seg, la, pname); validSyms++; } else { ShowMsg("%04X:%08X - Change name to '%s' failed\n", seg, la, pname); invalidSyms++; } } } else if (g_options.bReplace || !has_cmt(f)) { // Apply symbols for comment if (set_cmt(la, pname, false)) { ShowMsg("%04X:%08X - Change comment to '%s' successed\n", seg, la, pname); validSyms++; } else { ShowMsg("%04X:%08X - Change comment to '%s' failed\n", seg, la, pname); invalidSyms++; } } } } } } __finally { MapFileClose(pMapStart); hide_wait_box(); } if (!foundHdr) { warning("File '%s' is not a valid Map file", fname); } else { // Save file name for next askfile_c dialog strncpy(mapFileName, fname, sizeof(mapFileName)); // Show the result msg("Result of loading and parsing the Map file '%s'\n" " Number of Symbols applied: %d\n" " Number of Invalid Symbols: %d\n\n", fname, validSyms, invalidSyms); } }
//-------------------------------------------------------------------------- static int idaapi callback( void * /*user_data*/, int notification_code, va_list va) { static int stage = 0; static bool is_dll; static char needed_file[QMAXPATH]; switch ( notification_code ) { case dbg_process_start: case dbg_process_attach: get_input_file_path(needed_file, sizeof(needed_file)); // no break case dbg_library_load: if ( stage == 0 ) { const debug_event_t *pev = va_arg(va, const debug_event_t *); if ( !strieq(pev->modinfo.name, needed_file) ) break; if ( notification_code == dbg_library_load ) is_dll = true; // remember the current module bounds if ( pev->modinfo.rebase_to != BADADDR ) curmod.startEA = pev->modinfo.rebase_to; else curmod.startEA = pev->modinfo.base; curmod.endEA = curmod.startEA + pev->modinfo.size; deb(IDA_DEBUG_PLUGIN, "UUNP: module space %a-%a\n", curmod.startEA, curmod.endEA); ++stage; } break; case dbg_library_unload: if ( stage != 0 && is_dll ) { const debug_event_t *pev = va_arg(va, const debug_event_t *); if ( curmod.startEA == pev->modinfo.base || curmod.startEA == pev->modinfo.rebase_to ) { deb(IDA_DEBUG_PLUGIN, "UUNP: unload unpacked module\n"); if ( stage > 2 ) enable_step_trace(false); stage = 0; curmod.startEA = 0; curmod.endEA = 0; _hide_wait_box(); } } break; case dbg_run_to: // Parameters: const debug_event_t *event dbg->stopped_at_debug_event(true); bp_gpa = get_name_ea(BADADDR, "kernel32_GetProcAddress"); #ifndef __X64__ if( (LONG)GetVersion() < 0 ) // win9x mode -- use thunk's { is_9x = true; win9x_resolve_gpa_thunk(); } #endif if ( bp_gpa == BADADDR ) { bring_debugger_to_front(); warning("Sorry, could not find kernel32.GetProcAddress"); FORCE_STOP: stage = 4; // last stage clear_requests_queue(); request_exit_process(); run_requests(); break; } else if( !my_add_bpt(bp_gpa) ) { bring_debugger_to_front(); warning("Sorry, can not set bpt to kernel32.GetProcAddress"); goto FORCE_STOP; } else { ++stage; set_wait_box("Waiting for a call to GetProcAddress()"); } continue_process(); break; case dbg_bpt: // A user defined breakpoint was reached. // Parameters: thid_t tid // ea_t breakpoint_ea // int *warn = -1 // Return (in *warn): // -1 - to display a breakpoint warning dialog // if the process is suspended. // 0 - to never display a breakpoint warning dialog. // 1 - to always display a breakpoint warning dialog. { thid_t tid = va_arg(va, thid_t); qnotused(tid); ea_t ea = va_arg(va, ea_t); //int *warn = va_arg(va, int*); if ( stage == 2 ) { if ( ea == bp_gpa ) { regval_t rv; if ( get_reg_val(REGNAME_ESP, &rv) ) { ea_t esp = ea_t(rv.ival); invalidate_dbgmem_contents(esp, 1024); ea_t gpa_caller = getPtr(esp); if ( !is_library_entry(gpa_caller) ) { ea_t nameaddr; if ( ptrSz == 4 ) { nameaddr = get_long(esp+8); } else { get_reg_val(REGNAME_ECX, &rv); nameaddr = ea_t(rv.ival); } invalidate_dbgmem_contents(nameaddr, 1024); char name[MAXSTR]; size_t len = get_max_ascii_length(nameaddr, ASCSTR_C, ALOPT_IGNHEADS); name[0] = '\0'; get_ascii_contents2(nameaddr, len, ASCSTR_C, name, sizeof(name)); if ( !ignore_win32_api(name) ) { deb(IDA_DEBUG_PLUGIN, "%a: found a call to GetProcAddress(%s)\n", gpa_caller, name); if ( !my_del_bpt(bp_gpa) || !my_add_bpt(gpa_caller) ) error("Can not modify breakpoint"); } } } } else if ( ea == bpt_ea ) { my_del_bpt(ea); if ( !is_library_entry(ea) ) { msg("Uunp: reached unpacker code at %a, switching to trace mode\n", ea); enable_step_trace(true); ++stage; uint64 eax; if ( get_reg_val(REGNAME_EAX, &eax) ) an_imported_func = ea_t(eax); set_wait_box("Waiting for the unpacker to finish"); } else { warning("%a: bpt in library code", ea); // how can it be? my_add_bpt(bp_gpa); } } // not our bpt? skip it else { // hide the wait box to allow others plugins to properly stop _hide_wait_box(); break; } } } // while continue_process() would work here too, request+run is more universal // because they do not ignore the request queue request_continue_process(); run_requests(); break; case dbg_trace: // A step occured (one instruction was executed). This event // notification is only generated if step tracing is enabled. // Parameter: none if ( stage == 3 ) { thid_t tid = va_arg(va, thid_t); qnotused(tid); ea_t ip = va_arg(va, ea_t); // ip reached the OEP range? if ( oep_area.contains(ip) ) { // stop the trace mode enable_step_trace(false); msg("Uunp: reached OEP %a\n", ip); set_wait_box("Reanalyzing the unpacked code"); // reanalyze the unpacked code do_unknown_range(oep_area.startEA, oep_area.size(), DOUNK_EXPAND); auto_make_code(ip); // plan to make code noUsed(oep_area.startEA, oep_area.endEA); // plan to reanalyze auto_mark_range(oep_area.startEA, oep_area.endEA, AU_FINAL); // plan to analyze move_entry(ip); // mark the program's entry point _hide_wait_box(); // inform the user bring_debugger_to_front(); if ( askyn_c(1, "HIDECANCEL\n" "The universal unpacker has finished its work.\n" "Do you want to take a memory snapshot and stop now?\n" "(you can do it yourself if you want)\n") > 0 ) { set_wait_box("Recreating the import table"); invalidate_dbgmem_config(); if ( is_9x ) find_thunked_imports(); create_impdir(); set_wait_box("Storing resources to 'resource.res'"); if ( resfile[0] != '\0' ) extract_resource(resfile); _hide_wait_box(); if ( take_memory_snapshot(true) ) goto FORCE_STOP; } suspend_process(); unhook_from_notification_point(HT_DBG, callback, NULL); } } break; case dbg_process_exit: { stage = 0; // stop the tracing _hide_wait_box(); unhook_from_notification_point(HT_DBG, callback, NULL); if ( success ) jumpto(inf.beginEA, -1); else tell_about_failure(); } break; case dbg_exception:// Parameters: const debug_event_t *event // int *warn = -1 // Return (in *warn): // -1 - to display an exception warning dialog // if the process is suspended. // 0 - to never display an exception warning dialog. // 1 - to always display an exception warning dialog. { // const debug_event_t *event = va_arg(va, const debug_event_t *); // int *warn = va_arg(va, int *); // FIXME: handle code which uses SEH to unpack itself if ( askyn_c(1, "AUTOHIDE DATABASE\n" "HIDECANCEL\n" "An exception occurred in the program.\n" "UUNP does not support exceptions yet.\n" "The execution has been suspended.\n" "Do you want to continue the unpacking?") <= 0 ) { _hide_wait_box(); stage = 0; enable_step_trace(false); // stop the trace mode suspend_process(); } else { continue_process(); } } break; case dbg_request_error: // An error occured during the processing of a request. // Parameters: ui_notification_t failed_command // dbg_notification_t failed_dbg_notification { ui_notification_t failed_cmd = va_arg(va, ui_notification_t); dbg_notification_t failed_dbg_notification = va_arg(va, dbg_notification_t); _hide_wait_box(); stage = 0; warning("dbg request error: command: %d notification: %d", failed_cmd, failed_dbg_notification); } break; } return 0; }
int __stdcall FilePath(char *buf){ return get_input_file_path(buf,255); }
void idaapi run(int arg) { char buf[MAXSTR]; char cmt[MAXSTR]; char *valid_cmt = NULL; char ext[0x20]; FILE *f = NULL; short checkboxes = OPTION_NAMES | OPTION_COMMENTS; sval_t bank = 1; // default bool first = true; bool hasName = false; flags_t flags; ea_t ea = 0x0; if( AskUsingForm_c( madnes_options, &checkboxes, &bank ) != 1 || checkboxes == 0 ) return; // prepare filename for namelist (.nl) file get_input_file_path( buf, sizeof( buf ) ); qsnprintf( ext, sizeof( ext ),".%X.nl",--bank ); qstrncat( buf, ext, sizeof( buf )-strlen( buf ) ); // (always) create file f = qfopen( buf, "w" ); if( f == NULL ) { warning( "%s could not be created!", buf ); return; } msg( "Writing to file %s..", buf ); while( ea <= 0xFFFF ) { hasName = false; // get flags if( isCode( getFlags( ea ) ) ) flags = getFlags( ea ); else flags = getFlags( get_item_head( ea ) ); // if user either chose to export names or anynames if( ( ( checkboxes & OPTION_NAMES ) && has_name( flags ) ) || ( ( checkboxes & OPTION_ANYNAME ) && has_any_name( flags ) ) ) { // if current item is code or if current item is head of item if( isCode( flags ) || ea==get_item_head( ea ) ) { // get name get_name( ea, ea, buf, sizeof( buf ) ); // write to file qfprintf( f, "$%04X#%s#", ea, buf ); } else // if not code or not head of item (must be an array) { // get name of item start get_name( get_item_head( ea ), get_item_head( ea ), buf, sizeof( buf ) ); // calc displacement, write to file (example: "password+$04") qfprintf( f, "$%04X#%s+$%X#", ea, buf, ea-get_item_head( ea ) ); } hasName = true; } // if user chose to export cross references if( checkboxes & OPTION_XREFS ) { xrefblk_t xb; first = true; // cycle through all xrefs except ordinary flow xrefs for ( bool ok=xb.first_to( ea, XREF_FAR/*XREF_ALL*/); ok; ok=xb.next_to() ) { if( first ) // if first xref { if( !hasName ) // if this location hasn't a name yet, add symbol stub { qfprintf( f, "$%04X##", ea ); hasName = true; } qfprintf( f, "XREFS:\n\\"); // append XREFS first = false; } qfprintf( f, " $%04X\n\\", xb.from ); } } // if user chose to export comments if( checkboxes & OPTION_COMMENTS ) { if( has_cmt( flags ) ) // if current item has comment { // get comment // workaround for get_any_indeted_cmt() // -> unresolved external symbol "char * __stdcall get_any_indented_cmt(unsigned long,unsigned char *)" (?get_any_indented_cmt@@YGPADKPAE@Z) if( get_cmt( ea, false, cmt, sizeof( cmt ) ) == -1 ) get_cmt( ea, true, cmt, sizeof( cmt ) ); // validate comment (replace invalid chars, add room for additional chars) valid_cmt = validate_comment( cmt ); if( valid_cmt != NULL ) { if( !hasName ) { qfprintf( f, "$%04X##", ea ); // add symbol stub if no name yet hasName = true; } qfprintf( f, "%s", valid_cmt ); // write comment to file qfree( valid_cmt ); } } } if( hasName) qfprintf( f, "\n" ); ea++; // get name of each byte } qfclose( f ); msg( "done.\n" ); }
INT_PTR CALLBACK CFileGroups::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { // OutputDebugString("%s(%08X, %s, %08X, %08X)", __FUNCTION__, hwndDlg, // GetMessageName(uMsg), wParam, lParam); static filegroups_t *pfile_groups(0); static HWND hwndTVCtrl, hwndEditCtrl; OPENFILENAME ofn; TVINSERTSTRUCT tvis; TVITEMEX tvi; HTREEITEM hti, hParent, hGroup, hChild, hSel; char text[MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR], path[QMAXPATH], ext[_MAX_EXT]; BOOL hasSelection; switch (uMsg) { case WM_INITDIALOG: { _ASSERTE(lParam != 0); if (lParam == 0) { EndDialog(hwndDlg, IDCANCEL); return FALSE; } pfile_groups = reinterpret_cast<filegroups_t *>(lParam); hwndTVCtrl = GetDlgItem(hwndDlg, IDC_GROUPVWR); _ASSERTE(hwndTVCtrl != 0); if (hwndTVCtrl == NULL) { EndDialog(hwndDlg, IDCANCEL); return FALSE; } hwndEditCtrl = NULL; for (filegroups_t::const_iterator i = pfile_groups->begin(); i != pfile_groups->end(); ++i) { tvis.hParent = TVI_ROOT; tvis.hInsertAfter = TVI_SORT; tvis.itemex.mask = TVIF_STATE | TVIF_TEXT; tvis.itemex.state = 0; //TVIS_EXPANDED; tvis.itemex.stateMask = 0; tvis.itemex.pszText = const_cast<LPTSTR>(i->first.c_str()); hParent = TreeView_InsertItem(hwndTVCtrl, &tvis); _ASSERTE(hParent != NULL); if (hParent == NULL) continue; for (filegroups_t::mapped_type::const_iterator j = i->second.begin(); j != i->second.end(); ++j) { tvis.hParent = hParent; tvis.hInsertAfter = TVI_SORT; tvis.itemex.mask = TVIF_TEXT; tvis.itemex.pszText = const_cast<LPTSTR>(j->c_str()); hti = TreeView_InsertItem(hwndTVCtrl, &tvis); _ASSERTE(hti != NULL); } EnableDlgItem(hwndDlg, IDADD, TreeView_GetCount(hwndTVCtrl) > 0); EnableDlgItem(hwndDlg, IDREMOVE, TreeView_GetSelection(hwndTVCtrl) != 0); } static const tooltip_item_t tooltips[] = { IDC_GROUPVWR, "You can edit group names or filenames inplace by left clicking or pressing F2 on selected item", }; HWND hwndTT(CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP | TTS_BALLOON, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwndDlg, NULL, hInstance, NULL)); SetWindowPos(hwndTT, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); SendMessage(hwndTT, TTM_SETMAXTIPWIDTH, 0, static_cast<LPARAM>(400)); SendMessage(hwndTT, TTM_SETDELAYTIME, static_cast<WPARAM>(TTDT_AUTOPOP), static_cast<LPARAM>(20000)); TOOLINFO tt; memset(&tt, 0, sizeof tt); tt.cbSize = sizeof tt; tt.uFlags = TTF_SUBCLASS | TTF_IDISHWND | TTF_TRANSPARENT; tt.hwnd = hwndDlg; tt.hinst = hInstance; for (UINT j = 0; j < qnumber(tooltips); ++j) { tt.uId = reinterpret_cast<UINT_PTR>(GetDlgItem(hwndDlg, tooltips[j].uID)); tt.lpszText = const_cast<LPTSTR>(tooltips[j].lpText); SendMessage(hwndTT, TTM_ADDTOOL, 0, reinterpret_cast<LPARAM>(&tt)); } return TRUE; } case WM_CLOSE: EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; case WM_COMMAND: //if (hwndEditCtrl != NULL) return FALSE; switch (HIWORD(wParam)) { case BN_CLICKED: switch (LOWORD(wParam)) { case IDOK: if (hwndEditCtrl != NULL) { TreeView_EndEditLabelNow(hwndTVCtrl, FALSE); SetWindowLong(hwndDlg, DWL_MSGRESULT, TRUE); return TRUE; } _ASSERTE(pfile_groups != 0); pfile_groups->clear(); for (hGroup = TreeView_GetRoot(hwndTVCtrl); hGroup != NULL; hGroup = TreeView_GetNextSibling(hwndTVCtrl, hGroup)) { tvi.mask = TVIF_HANDLE | TVIF_TEXT; tvi.hItem = hGroup; tvi.pszText = text; tvi.cchTextMax = sizeof text; if (TreeView_GetItem(hwndTVCtrl, &tvi)) { filegroups_t::iterator i(pfile_groups->insert(pfile_groups->begin(), filegroups_t::value_type(text, filegroups_t::mapped_type()))); _ASSERTE(i != pfile_groups->end()); filegroups_t::mapped_type * pcurrentset(i != pfile_groups->end() ? &i->second : 0); if (pcurrentset != 0) for (hChild = TreeView_GetChild(hwndTVCtrl, hGroup); hChild != NULL; hChild = TreeView_GetNextSibling(hwndTVCtrl, hChild)) { tvi.mask = TVIF_HANDLE | TVIF_TEXT; tvi.hItem = hChild; tvi.pszText = text; tvi.cchTextMax = sizeof text; if (TreeView_GetItem(hwndTVCtrl, &tvi)) { /*if (qfileexist(text)) */pcurrentset->insert(text); } #ifdef _DEBUG else _RPTF3(_CRT_ERROR, "%s(%08X, WM_COMMAND, ...): TreeView_GetItem(%08X, ...) returned NULL\n", __FUNCTION__, hwndDlg, hwndTVCtrl); #endif // _DEBUG } } #ifdef _DEBUG else _RPTF3(_CRT_ERROR, "%s(%08X, WM_COMMAND, ...): TreeView_GetItem(%08X, ...) returned NULL\n", __FUNCTION__, hwndDlg, hwndTVCtrl); #endif // _DEBUG } case IDCANCEL: if (hwndEditCtrl != NULL) TreeView_EndEditLabelNow(hwndTVCtrl, TRUE); else EndDialog(hwndDlg, LOWORD(wParam)); SetWindowLong(hwndDlg, DWL_MSGRESULT, 0); return TRUE; case IDADD: { hSel = TreeView_GetSelection(hwndTVCtrl); if (hSel == NULL) hSel = TreeView_GetRoot(hwndTVCtrl); if (hSel != NULL) { hGroup = TreeView_GetParent(hwndTVCtrl, hSel); if (hGroup == NULL) hGroup = hSel; memset(&ofn, 0, sizeof ofn); ofn.lStructSize = sizeof ofn; ofn.hInstance = hInstance; ofn.hwndOwner = hwndDlg; ofn.lpstrTitle = CFileGroups::lpstrTitle; boost::scoped_array<char> FileName(new char[0x10000]); if (!FileName) { _RPTF2(_CRT_ERROR, "%s(...): failed to allocate new string of size 0x%X\n", __FUNCTION__, 0x10000); SetWindowLong(hwndDlg, DWL_MSGRESULT, 0); throw std::bad_alloc(); //break; } FileName[0] = 0; ofn.lpstrFile = FileName.get(); ofn.nMaxFile = 0x10000; ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_FORCESHOWHIDDEN | OFN_LONGNAMES | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT; ofn.lpstrFilter = CFileGroups::lpstrFilter; ofn.nFilterIndex = CFileGroups::nFilterIndex; ofn.lpstrDefExt = CFileGroups::lpstrDefExt; get_input_file_path(CPY(path)); _splitpath(path, drive, dir, 0, 0); _makepath(path, drive, dir, 0, 0); ofn.lpstrInitialDir = path; ofn.nMaxFile = 0x10000; if (GetOpenFileName(&ofn)) if (ofn.nFileOffset > strlen(ofn.lpstrFile)) while (*(ofn.lpstrFile + ofn.nFileOffset)) { AddFile(hwndTVCtrl, hGroup, _sprintf("%s\\%s", ofn.lpstrFile, ofn.lpstrFile + ofn.nFileOffset).c_str()); ofn.nFileOffset += strlen(ofn.lpstrFile + ofn.nFileOffset) + 1; } else AddFile(hwndTVCtrl, hGroup, ofn.lpstrFile); EnableDlgItem(hwndDlg, IDADD, TRUE); EnableDlgItem(hwndDlg, IDREMOVE, TreeView_GetSelection(hwndTVCtrl) != 0); } SetWindowLong(hwndDlg, DWL_MSGRESULT, 0); return TRUE; } case IDADDGROUP: { char *newgroup(askstr(HIST_IDENT, NULL, "New group name")); if (newgroup != 0 && strlen(newgroup) > 0) { if (strlen(newgroup) >= MAX_PATH) newgroup[MAX_PATH - 1] = 0; for (hGroup = TreeView_GetRoot(hwndTVCtrl); hGroup != NULL; hGroup = TreeView_GetNextSibling(hwndTVCtrl, hGroup)) { tvi.mask = TVIF_HANDLE | TVIF_TEXT; tvi.hItem = hGroup; tvi.pszText = text; tvi.cchTextMax = sizeof text; if (TreeView_GetItem(hwndTVCtrl, &tvi)) { if (strcmp(newgroup, text) == 0) { warning("Group with this name already exists"); SetWindowLong(hwndDlg, DWL_MSGRESULT, 0); return FALSE; } } #ifdef _DEBUG else _RPTF3(_CRT_ERROR, "%s(%08X, WM_COMMAND, ...): TreeView_GetItem(%08X, ...) returned NULL\n", __FUNCTION__, hwndDlg, hwndTVCtrl); #endif // _DEBUG } tvis.hParent = TVI_ROOT; tvis.hInsertAfter = TVI_SORT; tvis.itemex.mask = TVIF_STATE | TVIF_TEXT; tvis.itemex.state = TVIS_EXPANDED; tvis.itemex.stateMask = 0; tvis.itemex.pszText = newgroup; hti = TreeView_InsertItem(hwndTVCtrl, &tvis); _ASSERTE(hti != NULL); if (hti != NULL) { TreeView_SelectItem(hwndTVCtrl, hti); TreeView_Expand(hwndTVCtrl, hti, TVE_EXPAND); EnableDlgItem(hwndDlg, IDADD, TRUE); EnableDlgItem(hwndDlg, IDREMOVE, TRUE); } } SetWindowLong(hwndDlg, DWL_MSGRESULT, 0); return TRUE; } case IDREMOVE: if ((hSel = TreeView_GetSelection(hwndTVCtrl)) != NULL) { if ((hParent = TreeView_GetParent(hwndTVCtrl, hSel)) == NULL && TreeView_GetChild(hwndTVCtrl, hSel) != NULL) { // is group std::string msg("Are you sure to delete group '"); tvi.mask = TVIF_HANDLE | TVIF_TEXT; tvi.hItem = hSel; tvi.pszText = text; tvi.cchTextMax = sizeof text; if (TreeView_GetItem(hwndTVCtrl, &tvi)) msg.append(text); msg.append("' and all it's files?"); if (MessageBox(hwndDlg, msg.c_str(), "libnames matching", MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1) == IDYES) TreeView_DeleteItem(hwndTVCtrl, hSel); } else // is file or group has no files - delete without confirmation TreeView_DeleteItem(hwndTVCtrl, hSel); if (TreeView_GetCount(hwndTVCtrl) <= 0) EnableDlgItem(hwndDlg, IDADD, FALSE); EnableDlgItem(hwndDlg, IDREMOVE, TreeView_GetSelection(hwndTVCtrl) != NULL); } SetWindowLong(hwndDlg, DWL_MSGRESULT, 0); return TRUE; } break; } break; case WM_NOTIFY: _ASSERTE(lParam != NULL); if (lParam != NULL) { // OutputDebugString("%s(%08X, WM_NOTIFY, ...): hwndFrom=%08X idFrom=%u code=0x%X", // __FUNCTION__, hwndDlg, reinterpret_cast<LPNMHDR>(lParam)->hwndFrom, reinterpret_cast<LPNMHDR>(lParam)->idFrom, // reinterpret_cast<LPNMHDR>(lParam)->code); switch (reinterpret_cast<LPNMHDR>(lParam)->idFrom) { case IDC_GROUPVWR: switch (reinterpret_cast<LPNMHDR>(lParam)->code) { case TVN_KEYDOWN: switch (reinterpret_cast<LPNMTVKEYDOWN>(lParam)->wVKey) { case VK_F2: if ((hSel = TreeView_GetSelection(hwndTVCtrl)) != NULL) TreeView_EditLabel(hwndTVCtrl, hSel); SetWindowLong(hwndDlg, DWL_MSGRESULT, TRUE); return TRUE; case VK_INSERT: SendMessage(hwndDlg, WM_COMMAND, (hSel = TreeView_GetSelection(hwndTVCtrl)) != NULL && TreeView_GetParent(hwndTVCtrl, hSel) != NULL ? MAKELONG(IDADD, BN_CLICKED) : MAKELONG(IDADDGROUP, BN_CLICKED), reinterpret_cast<LPARAM>(hwndTVCtrl)); SetWindowLong(hwndDlg, DWL_MSGRESULT, TRUE); return TRUE; case VK_DELETE: SendMessage(hwndDlg, WM_COMMAND, MAKELONG(IDREMOVE, BN_CLICKED), reinterpret_cast<LPARAM>(hwndTVCtrl)); SetWindowLong(hwndDlg, DWL_MSGRESULT, TRUE); return TRUE; } break; case TVN_BEGINLABELEDIT: { if ((hSel = TreeView_GetSelection(hwndTVCtrl)) != NULL && TreeView_GetParent(hwndTVCtrl, hSel) != NULL) { tvi.mask = TVIF_HANDLE | TVIF_TEXT; tvi.hItem = hSel; tvi.pszText = text; tvi.cchTextMax = sizeof text; if (TreeView_GetItem(hwndTVCtrl, &tvi)) { memset(&ofn, 0, sizeof ofn); ofn.lStructSize = sizeof ofn; ofn.lpstrFile = text; ofn.nMaxFile = sizeof text; _splitpath(text, drive, dir, 0, ext); _makepath(path, drive, dir, 0, 0); ofn.lpstrInitialDir = path; ofn.hwndOwner = hwndDlg; ofn.hInstance = hInstance; ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_FORCESHOWHIDDEN | OFN_LONGNAMES | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrTitle = "Change file's location"; ofn.lpstrFilter = "all files\0*.*\0"; ofn.nFilterIndex = 1; ofn.lpstrDefExt = ext; if (GetOpenFileName(&ofn)) { tvi.mask = TVIF_HANDLE | TVIF_TEXT; TreeView_SetItem(hwndTVCtrl, &tvi); } SetWindowLong(hwndDlg, DWL_MSGRESULT, TRUE); return TRUE; } #ifdef _DEBUG else _RPTF2(_CRT_WARN, "%s(...): TreeView_GetItem(%08X, ...) returned NULL", __FUNCTION__, hwndTVCtrl); #endif // _DEBUG } hwndEditCtrl = TreeView_GetEditControl(hwndTVCtrl); _ASSERTE(hwndEditCtrl != NULL); SendMessage(hwndEditCtrl, EM_LIMITTEXT, MAX_PATH - 1, 0); SetWindowLong(hwndDlg, DWL_MSGRESULT, FALSE); return TRUE; } case TVN_ENDLABELEDIT: hwndEditCtrl = NULL; SetWindowLong(hwndDlg, DWL_MSGRESULT, reinterpret_cast<LPNMTVDISPINFO>(lParam)->item.pszText != NULL && strlen(reinterpret_cast<LPNMTVDISPINFO>(lParam)->item.pszText) > 0 && strlen(reinterpret_cast<LPNMTVDISPINFO>(lParam)->item.pszText) < MAX_PATH); return TRUE; case TVN_SELCHANGED: EnableDlgItem(hwndDlg, IDREMOVE, TreeView_GetSelection(hwndTVCtrl) != 0); return TRUE; //case WM_LBUTTONDBLCLCK: // break; } // switch code break; } // switch idFrom } // lParam != NULL break; } // main switch return 0; }
/* Dumb algorithm of filename searching: * 1) get filename of currently disassembled filename * 2) chomp ext * 3) add our ext and try to open * 4) if failed - get name and add our exe * 5) and apply standard IDA file search mechanism (over getsysfile) * That`s all folks */ char * RP_mapping::find_path(const char *my_ext) { int my_ext_len = 0; /* lets in beginning cut all initial dots from proposed extentsion */ if ( my_ext != NULL ) { while ( *my_ext && *my_ext == '.' ) my_ext++; if ( *my_ext ) my_ext_len = strlen(my_ext); } /* try to find filename of current database */ char own_name[_MAX_PATH]; get_input_file_path(own_name, sizeof(own_name)); if ( NULL == own_name ) { return NULL; } /* main processing */ char *path = NULL, *file = NULL, *ext = NULL; qsplitpath(own_name, &path, &file); if ( file == NULL ) return NULL; char *filebase = NULL; qsplitfile(file, NULL, &ext); if ( ext != NULL ) { int base_len = ext - file; filebase = (char *)qalloc(base_len); strncpy(filebase, file, base_len - 1); filebase[base_len - 1] = 0x0; } else filebase = qstrdup(file); int filebase_len = strlen(filebase); /* O`k lets try p 1) */ if ( NULL != path ) { int need_len; int base_len = file - own_name; need_len = base_len + 3 + filebase_len + my_ext_len; char *res = (char *)qalloc(need_len); strncpy(res, path, base_len); res[base_len - 1] = '\\'; strcpy(res + base_len, filebase); if ( my_ext != NULL ) { strcat(res, "."); strcat(res, my_ext); } struct stat st; #ifdef RP_DEBUG msg("Try %s\n", res); #endif if ( -1 != stat(res, &st) ) { #ifdef RP_DEBUG msg("Find %s (time %X)\n", res, st.st_mtime ); #endif qfree(filebase); return res; } qfree(res); } /* next part do same thing as qmakefile missed by Ilfak */ char *st_buf = filebase; if ( my_ext_len ) { st_buf = (char *)qalloc(2 + my_ext_len + filebase_len); strcpy(st_buf, filebase); st_buf[filebase_len] = '.'; strcpy(st_buf + filebase_len + 1, my_ext); } #ifdef RP_DEBUG msg("st_buf '%s', my_ext_len %d, filebase_len %d, my_ext %s\n", st_buf, my_ext_len, filebase_len, my_ext); #endif char *res = (char *)qalloc(_MAX_PATH + 1); char *ret_val = getsysfile(res, _MAX_PATH, st_buf, CFG_SUBDIR); if ( NULL == ret_val ) qfree(res); if ( st_buf != filebase ) qfree(st_buf); if ( filebase != NULL ) qfree(filebase); return ret_val; }
NTHeader::NTHeader() { char inputFilePath[QMAXPATH]; get_input_file_path(inputFilePath, sizeof(inputFilePath)); parsePEFile(inputFilePath); }