Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2010
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 109.54 KB | None | 0 0
  1. #include <string.h>
  2. // Temp storage
  3. char buffer[65535] = {0};
  4.  
  5. // -------------------------------------------------------------------------------
  6. // COMMANDS
  7. // -------------------------------------------------------------------------------
  8. bool OllyLang::DoADD(string args)
  9. {
  10.     string ops[2];
  11.  
  12.     if(!CreateOperands(args, ops, 2))
  13.         return false;
  14.  
  15.     ulong dw1, dw2 ;
  16.     string str1, str2;
  17.  
  18.     if (GetDWOpValue(ops[0], dw1)
  19.         && GetDWOpValue(ops[1], dw2))
  20.     {
  21.         // "0" to force hexa constant
  22.         args = ops[0] + ",0" + ultoa(dw1 + dw2, buffer, 16);
  23.         nIgnoreNextValuesHist++;
  24.         return DoMOV(args);
  25.     }
  26.     else if (GetSTROpValue(ops[0], str1)
  27.         && GetDWOpValue(ops[1], dw2))
  28.     {
  29.         //concate string with ultoa or buffer with hex value
  30.         var v1=str1, v2=dw2;
  31.         v1+=v2;
  32.  
  33.         args = ops[0] + ", \"" + v1.str +"\"";
  34.         nIgnoreNextValuesHist++;
  35.         return DoMOV(args);
  36.     }
  37.     else if (GetSTROpValue(ops[0], str1)
  38.              && GetANYOpValue(ops[1], str2))
  39.     {
  40.         //Class var for buffer/str concate support
  41.         var v1=str1, v2=str2;
  42.         v1+=v2;
  43.  
  44.         args = ops[0] + ", " + "\"" + v1.str + "\"";
  45.         nIgnoreNextValuesHist++;
  46.         return DoMOV(args);
  47.     }
  48.     else if (GetANYOpValue(ops[0], str1)
  49.              && GetANYOpValue(ops[1], str2))
  50.     {
  51.         //Class var for buffer/str concate support
  52.         var v1=str1, v2=str2;
  53.         v1+=v2;
  54.  
  55.         args = ops[0] + ", " + "\"" + v1.str + "\"";
  56.         nIgnoreNextValuesHist++;
  57.         return DoMOV(args);
  58.     }
  59.     return false;
  60. }
  61.  
  62. bool OllyLang::DoAI(string args)
  63. {
  64.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 1, 0, VK_F7);
  65.     require_ollyloop = 1;
  66.     return true;
  67. }
  68.  
  69. bool OllyLang::DoALLOC(string args)
  70. {
  71.     string ops[1];
  72.  
  73.     if(!CreateOperands(args, ops, 1))
  74.         return false;
  75.  
  76.     ulong addr, size;
  77.     if(GetDWOpValue(ops[0], size))
  78.     {
  79.  
  80.         HANDLE hDbgPrc = (HANDLE) Plugingetvalue(VAL_HPROCESS);
  81.         addr = (DWORD) VirtualAllocEx(hDbgPrc,NULL,size,MEM_RESERVE|MEM_COMMIT,PAGE_EXECUTE_READWRITE);
  82.         variables["$RESULT"] = addr;
  83.         //Refresh Memory Window
  84.         Listmemory();
  85.         require_ollyloop=1;
  86.  
  87.         regBlockToFree((void *)addr, size, false);
  88.         return true;
  89.     }
  90.     return false;
  91. }
  92.  
  93. bool OllyLang::DoAN(string args)
  94. {
  95.     string ops[1];
  96.  
  97.     if(!CreateOperands(args, ops, 1))
  98.         return false;
  99.  
  100.     ulong dw;
  101.  
  102.     if(GetDWOpValue(ops[0], dw))
  103.     {
  104.         Analysecode(Findmodule(dw));
  105.         require_ollyloop = 1;
  106.         return true;
  107.     }
  108.     return false;
  109. }
  110.  
  111. bool OllyLang::DoAND(string args)
  112. {
  113.     string ops[2];
  114.  
  115.     if(!CreateOperands(args, ops, 2))
  116.         return false;
  117.  
  118.     ulong dw1, dw2;
  119.     if (GetDWOpValue(ops[0], dw1)
  120.         && GetDWOpValue(ops[1], dw2))
  121.     {
  122.         args = ops[0] + ", 0" + ultoa(dw1 & dw2, buffer, 16);
  123.         nIgnoreNextValuesHist++;
  124.         return DoMOV(args);
  125.     }
  126.     return false;
  127. }
  128.  
  129. bool OllyLang::DoAO(string args)
  130. {
  131.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 1, 0, VK_F8);
  132.     require_ollyloop = 1;
  133.     return true;
  134. }
  135.  
  136. bool OllyLang::DoASK(string args)
  137. {
  138.     string ops[1];
  139.     string title;
  140.  
  141.     //Reset $RESULT, (when dialog closed)
  142.     variables["$RESULT"] = 0;
  143.     variables["$RESULT_1"] = 0;
  144.  
  145.     if(!CreateOperands(args, ops, 1))
  146.         return false;
  147.  
  148.     if(GetSTROpValue(ops[0], title))
  149.     {
  150.         if (wndProg.hw!=NULL)
  151.             InvalidateRect(wndProg.hw, NULL, FALSE);
  152.  
  153.         HWND hw = 0; //not modal but dialog need to be closed on plugin close.
  154.         char* returned_buffer = (char*)DialogBoxParam(hinstModule(), MAKEINTRESOURCE(IDD_INPUT), hw, (DLGPROC) InputDialogProc, (LPARAM)title.c_str());
  155.         if ((BYTE)returned_buffer != NULL)
  156.         {
  157.             string returned = returned_buffer;
  158.             delete[256] returned_buffer;
  159.            
  160.             if(is_hex(returned))
  161.             {
  162.                 variables["$RESULT"] = strtoul(returned.c_str(), 0, 16);
  163.                 variables["$RESULT_1"] = (int) (returned.length() / 2);            //size
  164.             }
  165.             else
  166.             {
  167.                 UnquoteString(returned, '"', '"'); // To Accept input like "FFF" (forces string)
  168.                 variables["$RESULT"] = returned.c_str();
  169.                 variables["$RESULT_1"] = (int) returned.length();
  170.             }
  171.         }
  172.         else
  173.         {
  174.             Pause();
  175.         }
  176.         return true;
  177.     }
  178.     return false;
  179. }
  180.  
  181. bool OllyLang::DoASM(string args)
  182. {
  183.     string ops[3],cmd;
  184.     ulong addr, attempt=0;
  185.  
  186.     if(!CreateOperands(args, ops, 3))
  187.         if(!CreateOperands(args, ops, 2))
  188.             return false;
  189.  
  190.     t_asmmodel model={0};
  191.     char error[255] = {0};
  192.     int len = 0;
  193.  
  194.     if(GetDWOpValue(ops[0], addr)
  195.         && GetSTROpValue(ops[1], cmd))
  196.     {
  197.         cmd=FormatAsmDwords(cmd);
  198.  
  199.         GetDWOpValue(ops[2], attempt);
  200.  
  201.         if((len = Assemble((char*) cmd.c_str(), addr, &model, attempt, 3, error)) <= 0)
  202.         {
  203.             errorstr = error;
  204.             return false;
  205.         }
  206.         else
  207.         {
  208.             Writememory(model.code, addr, len, MM_SILENT|MM_DELANAL);
  209.             Broadcast(WM_USER_CHALL, 0, 0);
  210.             variables["$RESULT"] = len;
  211.             require_ollyloop = 1;
  212.             return true;
  213.         }
  214.     }
  215.  
  216.     return false;
  217. }
  218.  
  219. bool OllyLang::DoASMTXT(string args)
  220. {
  221.     string ops[2];
  222.  
  223.     if (!CreateOp(args, ops, 2))
  224.        return false;
  225.  
  226.     FILE *fp = NULL;
  227.     t_asmmodel model={0};
  228.     string asmfile;
  229.     char opcode[4096]={0},error[256]={0};
  230.     long len = 0,line = 0;
  231.     ulong addr, p, lens = 0,attempt = 0;
  232.  
  233.  
  234.     if(GetDWOpValue(ops[0], addr)
  235.         && GetSTROpValue(ops[1],asmfile))
  236.     {
  237.         if(fopen(asmfile.c_str(), "rb")){
  238.            
  239.             string asmline,asmdoc;
  240.             std::ifstream fin;
  241.             (asmfile.c_str(), ios::in);
  242.             fin.open(asmfile.c_str());
  243.  
  244.             p = (ulong) opcode;
  245.             while(getline(fin, asmline))
  246.             {
  247.                 line++;
  248.                 asmline=FormatAsmDwords(asmline);
  249.                 len=Assemble((char*) asmline.c_str(), addr+lens, &model, attempt, 3, error);                    
  250.                 memcpy((void*) (p + lens),&model.code,len);
  251.                 lens += len;
  252.             }
  253.             Writememory(opcode, addr, lens, MM_DELANAL);
  254.             Broadcast(WM_USER_CHALL, 0, 0);
  255.             variables["$RESULT"] = lens;
  256.             variables["$RESULT_1"] = line;
  257.             require_ollyloop = 1;
  258.             fin.close();
  259.         }  
  260.         return true;
  261.     }
  262.     return false;
  263. }
  264.  
  265. bool OllyLang::DoATOI(string args)
  266. {
  267.     string ops[2];
  268.  
  269.     if (!CreateOperands(args, ops, 2))
  270.     {
  271.         ops[1] = "10"; //10h=(16.) : Default Hexa number
  272.         if (!CreateOperands(args, ops, 1))
  273.             return false;
  274.     }
  275.  
  276.     string str;
  277.     ulong base, dw;
  278.  
  279.     if(GetSTROpValue(ops[0], str)
  280.         && GetDWOpValue(ops[1], base) )
  281.     {
  282.         variables["$RESULT"] = strtoul(str.c_str(),0,base);
  283.         return true;
  284.     }
  285.     return false;
  286. }
  287.  
  288. bool OllyLang::DoBACKUP(string args)
  289. {
  290.     if (DoOPENDUMP(args)) {
  291.  
  292.         HWND wnd = (HWND) variables["$RESULT"].dw;
  293.         t_dump * dump = dumpWindows[wnd];
  294.  
  295.         if (dump) {
  296.             Dumpbackup(dump, BKUP_CREATE);
  297.             //Dumpbackup(dump, BKUP_VIEWCOPY);
  298.             return true;
  299.         }
  300.     }
  301.  
  302.     return false;
  303. }
  304.  
  305. bool OllyLang::DoBC(string args)
  306. {
  307.     if (args=="")
  308.         return DoBCA(args);
  309.  
  310.     string ops[1];
  311.  
  312.     if (!CreateOperands(args, ops, 1))
  313.         return false;
  314.  
  315.     ulong dw;
  316.     if (GetDWOpValue(ops[0], dw))
  317.     {
  318.         Deletebreakpoints(dw, dw + 1, true);
  319.         Broadcast(WM_USER_CHALL, 0, 0);
  320.         require_ollyloop = 1;
  321.         return true;
  322.     }
  323.     return false;
  324. }
  325.  
  326. //clear all loaded breakpoints
  327. bool OllyLang::DoBCA(string args)
  328. {
  329.     t_table *bptable;
  330.     t_bpoint *bpoint;
  331.     bptable=(t_table *)Plugingetvalue(VAL_BREAKPOINTS);
  332.     if (bptable!=NULL)
  333.     {
  334.         for (int b=bptable->data.n-1; b>=0;b--) {
  335.             bpoint=(t_bpoint *)Getsortedbyselection(&(bptable->data),b);
  336.             if (bpoint!=NULL) {
  337.                 Deletesorteddata(&(bptable->data),bpoint->addr);
  338.             }
  339.         }
  340.     }
  341.     Broadcast(WM_USER_CHALL, 0, 0);
  342.     require_ollyloop = 1;
  343.     return true;
  344. }
  345.  
  346. bool OllyLang::DoBD(string args)
  347. {
  348.     if (args=="")
  349.         return DoBDA(args);
  350.  
  351.     string ops[1];
  352.     if (!CreateOperands(args, ops, 1))
  353.         return false;
  354.  
  355.     ulong dw;
  356.     if(GetDWOpValue(ops[0], dw))
  357.     {
  358.         Setbreakpoint(dw, TY_DISABLED, 0);
  359.         Broadcast(WM_USER_CHALL, 0, 0);
  360.         require_ollyloop = 1;
  361.         return true;
  362.     }
  363.     return false;
  364. }
  365.  
  366. //disable all loaded breakpoints
  367. bool OllyLang::DoBDA(string args)
  368. {
  369.     t_table *bptable;
  370.     t_bpoint *bpoint;
  371.     bptable=(t_table *)Plugingetvalue(VAL_BREAKPOINTS);
  372.     if (bptable!=NULL) {
  373.         for (int b=bptable->data.n-1; b>=0;b--) {
  374.             bpoint=(t_bpoint *)Getsortedbyselection(&(bptable->data),b);
  375.             if (bpoint!=NULL) {
  376.               Setbreakpoint(bpoint->addr, TY_DISABLED, bpoint->cmd);
  377.             }
  378.         }
  379.     }
  380.     Broadcast(WM_USER_CHALL, 0, 0);
  381.     require_ollyloop = 1;
  382.     return true;
  383. }
  384.  
  385. bool OllyLang::DoBEGINSEARCH(string args)
  386. {
  387.     string ops[1];
  388.     ulong start;
  389.  
  390.     if (!CreateOperands(args, ops, 1))
  391.         start=0;
  392.     else
  393.         if (!GetDWOpValue(ops[0], start))
  394.             return false;
  395.  
  396.     t_memory* tm;
  397.     t_table* tt;
  398.  
  399.     tt=(t_table*) Plugingetvalue(VAL_MEMORY);
  400.     if (tt==NULL)
  401.         return false;
  402.  
  403.     if (start==0) {
  404.         //get first mem block addr.
  405.         tm=(t_memory*) Getsortedbyselection(&tt->data, 0);
  406.         if (tm==NULL)
  407.             return false;
  408.         start=tm->base;
  409.     } else {
  410.         //get block at addr
  411.         tm=(t_memory*) Findmemory(start);
  412.         if (tm==NULL)
  413.             return false;
  414.     }
  415.  
  416.     //Last Memory Bloc
  417.  
  418.     tm=(t_memory*) Getsortedbyselection(&tt->data, tt->data.n-1);
  419.     if (tm==NULL)
  420.         return false;
  421.  
  422.     int fullsize = tm->base+tm->size - start;
  423.  
  424.     search_buffer = new unsigned char[fullsize];
  425.     fullsize = Readmemory(search_buffer,start,fullsize,MM_SILENT);
  426.     Havecopyofmemory(search_buffer,start,fullsize);
  427.     //Havecopyofmemory(search_buffer,tm->base,tm->size);
  428.     return true;
  429. }
  430.  
  431. bool OllyLang::DoENDSEARCH(string args)
  432. {
  433.     if (search_buffer!=NULL) {
  434.         Havecopyofmemory(NULL,0,0);
  435.         delete [] search_buffer;
  436.         search_buffer=NULL;
  437.     }
  438.     return true;
  439. }
  440.  
  441. bool OllyLang::DoBP(string args)
  442. {
  443.     string ops[1];
  444.  
  445.     if (!CreateOperands(args, ops, 1))
  446.         return false;
  447.  
  448.     ulong dw;
  449.     if(GetDWOpValue(ops[0], dw))
  450.     {
  451.         Setbreakpoint(dw, TY_ACTIVE, 0);
  452.         Broadcast(WM_USER_CHALL, 0, 0);
  453.         require_ollyloop = 1;
  454.         return true;
  455.     }
  456.     return false;
  457. }
  458.  
  459. bool OllyLang::DoBPCND(string args)
  460. {
  461.     string ops[2];
  462.  
  463.     if(!CreateOperands(args, ops, 2))
  464.         return false;
  465.  
  466.     ulong addr = 0;
  467.     string condition;
  468.     if(GetDWOpValue(ops[0], addr)
  469.         && GetSTROpValue(ops[1], condition))
  470.     {
  471.         char* buffer = new char[condition.length() + 1];
  472.         strcpy(buffer, condition.c_str());
  473.         Setbreakpoint(addr, TY_ACTIVE, 0);
  474.         Insertname(addr, NM_BREAK, buffer);
  475.         Deletenamerange(addr, addr + 1, NM_BREAKEXPL);
  476.         Deletenamerange(addr, addr + 1, NM_BREAKEXPR);
  477.         Broadcast(WM_USER_CHALL,0,0);
  478.         delete buffer;
  479.         return true;
  480.     }
  481.     return false;
  482. }
  483.  
  484. bool OllyLang::DoBPD(string args)
  485. {
  486.     string ops[1];
  487.  
  488.     if (!CreateOp(args, ops, 1))
  489.         return false;
  490.  
  491.     return DoBPX(ops[0] + ", 1");
  492.  
  493. }
  494.  
  495. //Set On Breakpoint Goto Script Label (addr, label)
  496. bool OllyLang::DoBPGOTO(string args)
  497. {
  498.     string ops[2];
  499.  
  500.     if(!CreateOperands(args, ops, 2))
  501.         return false;
  502.  
  503.     ulong addr = 0;
  504.     if(GetDWOpValue(ops[0], addr))
  505.     {
  506.         if(labels.find(ops[1]) != labels.end()) {
  507.             AddBPJump(addr,labels[ops[1]]);
  508.             return true;
  509.         }
  510.     }
  511.     return false;
  512. }
  513.  
  514. bool OllyLang::DoBPHWCA(string args)
  515. {
  516.     int i = 0;
  517.     while(i < 4)
  518.     {
  519.         if (Deletehardwarebreakpoint(i) == -1)
  520.         {return false;}
  521.         i++;
  522.     }
  523.     require_ollyloop = 1;
  524.     return true;
  525. }
  526.  
  527. bool OllyLang::DoBPHWC(string args)
  528. {
  529.     if (args=="")
  530.         return DoBPHWCA(args);
  531.  
  532.     string ops[1];
  533.  
  534.     if (!CreateOperands(args, ops, 1))
  535.         return false;
  536.  
  537.     ulong dw1;
  538.     if(GetDWOpValue(ops[0], dw1))
  539.     {
  540.         Deletehardwarebreakbyaddr(dw1);
  541.         require_ollyloop = 1;
  542.         return true;
  543.     }
  544.     return false;
  545. }
  546.  
  547. bool OllyLang::DoBPHWS(string args)
  548. {
  549.     string ops[2];
  550.  
  551.     if(!CreateOperands(args, ops, 2)){
  552.         if(!CreateOperands(args, ops, 1))
  553.             return false;
  554.         else ops[1]="\"x\"";
  555.     }
  556.  
  557.     ulong dw1;
  558.     string str1;
  559.     if(GetDWOpValue(ops[0], dw1)
  560.         && GetSTROpValue(ops[1], str1))
  561.     {
  562.         int type;
  563.         if(str1 == "r")
  564.             type = HB_ACCESS;
  565.         else if(str1 == "w")
  566.             type = HB_WRITE;
  567.         else if(str1 == "x")
  568.             type = HB_CODE;
  569.         else
  570.             return false;
  571.  
  572.         Sethardwarebreakpoint(dw1, 1, type);
  573.         require_ollyloop = 1;
  574.         return true;
  575.     }
  576.     return false;
  577. }
  578.  
  579. bool OllyLang::DoBPL(string args)
  580. {
  581.     string ops[2];
  582.  
  583.     if(!CreateOperands(args, ops, 2))
  584.         return false;
  585.  
  586.     ulong addr = 0;
  587.     string expression;
  588.     if(GetDWOpValue(ops[0], addr)
  589.         && GetSTROpValue(ops[1], expression))
  590.     {
  591.         sprintf(buffer, "%c%s", 0x45/*COND_NOBREAK | COND_LOGALWAYS | COND_ARGALWAYS | COND_FILLING*/, expression.c_str());
  592.         Setbreakpoint(addr, TY_ACTIVE, 0);
  593.         Deletenamerange(addr, addr + 1, NM_BREAK);
  594.         Deletenamerange(addr, addr + 1, NM_BREAKEXPL);
  595.         Insertname(addr, NM_BREAKEXPR, buffer);
  596.         Broadcast(WM_USER_CHALL,0,0);
  597.         return true;
  598.     }
  599.     return false;
  600. }
  601.  
  602. bool OllyLang::DoBPLCND(string args)
  603. {
  604.     string ops[3];
  605.  
  606.     if(!CreateOperands(args, ops, 3))
  607.         return false;
  608.  
  609.     ulong addr = 0;
  610.     string expression, condition;
  611.     if (GetDWOpValue(ops[0], addr)
  612.         && GetSTROpValue(ops[1], expression)
  613.         && GetSTROpValue(ops[2], condition))
  614.     {
  615.         Setbreakpoint(addr, TY_ACTIVE, 0);
  616.         Deletenamerange(addr, addr + 1, NM_BREAKEXPL);
  617.         sprintf(buffer, "%s", condition.c_str());
  618.         Insertname(addr, NM_BREAK, buffer);
  619.         sprintf(buffer, "%c%s", 0x43, expression.c_str());
  620.         Insertname(addr, NM_BREAKEXPR, buffer);
  621.         Broadcast(WM_USER_CHALL,0,0);
  622.         return true;
  623.     }
  624.     return false;
  625. }
  626.  
  627. bool OllyLang::DoBPMC(string args)
  628. {
  629.     Setmembreakpoint(0, 0, 0);
  630.     return true;
  631. }
  632.  
  633. bool OllyLang::DoBPRM(string args)
  634. {
  635.     string ops[2];
  636.  
  637.     if(!CreateOperands(args, ops, 2))
  638.         return false;
  639.  
  640.     ulong addr, size;
  641.     if(GetDWOpValue(ops[0], addr)
  642.         && GetDWOpValue(ops[1], size))
  643.     {
  644.         Setmembreakpoint(MEMBP_READ, addr, size);
  645.         return true;
  646.     }
  647.     return false;
  648. }
  649.  
  650. bool OllyLang::DoBPWM(string args)
  651. {
  652.     string ops[2];
  653.  
  654.     if(!CreateOperands(args, ops, 2))
  655.         return false;
  656.  
  657.     ulong addr, size;
  658.     if(GetDWOpValue(ops[0], addr)
  659.         && GetDWOpValue(ops[1], size))
  660.     {
  661.         Setmembreakpoint(MEMBP_WRITE, addr, size);
  662.         return true;
  663.     }
  664.     return false;
  665. }
  666.  
  667. //Breakpoint on DLL Calls in current module
  668. //ex: BPX "GetModuleHandleA"
  669. bool OllyLang::DoBPX(string args)
  670. {
  671.  
  672.     string ops[2];
  673.     int i;
  674.     int bpnmb=0;
  675.     ulong del=0;
  676.     string callname;
  677.     string module;
  678.     bool closeWindow=false;
  679.  
  680.     if(CreateOp(args, ops, 2))
  681.     {
  682.         GetDWOpValue(ops[1], del);
  683.     }
  684.     else
  685.     if(!CreateOp(args, ops, 1))
  686.         return false;
  687.  
  688.  
  689.     if (GetSTROpValue(ops[0],callname))
  690.     {
  691.         t_table *reftable;
  692.         t_ref *pref;
  693.  
  694.         char findname[TEXTLEN]={0};
  695.        
  696.         variables["$RESULT"]=0;
  697.  
  698.         if(callname.length()==0)
  699.         {
  700.             errorstr="Function name missed";
  701.             return false;
  702.         }
  703.        
  704.         if (callname.find(".") != string::npos)
  705.         {
  706.             module = callname.substr(0, callname.find("."));
  707.             callname = callname.substr(callname.find(".")+1);
  708.         }
  709.  
  710.         reftable=(t_table *)Plugingetvalue(VAL_REFERENCES);
  711.  
  712.         //Hide window after if was closed
  713.         if(reftable->hw == 0)
  714.             closeWindow = true;
  715.  
  716.         Findalldllcalls((t_dump *)Plugingetvalue(VAL_CPUDASM),0,"Intermodular calls");
  717.         reftable=(t_table *)Plugingetvalue(VAL_REFERENCES);
  718.  
  719.         if(reftable==NULL || reftable->data.n==0)
  720.         {
  721.             errorstr="No references";
  722.             return false;
  723.         }
  724.  
  725.         if(reftable->data.itemsize<sizeof(t_ref))
  726.         {
  727.             errorstr="Old version of OllyDbg";
  728.             return false;
  729.         }
  730.  
  731.         for(i=0; i<reftable->data.n; i++)
  732.         {
  733.             // The safest way: size of t_ref may change in the future!
  734.             pref=(t_ref *)((char *)reftable->data.data+reftable->data.itemsize*i);
  735.  
  736.             if(Findlabel(pref->dest,findname)==0)
  737.             {// Unnamed destination
  738.                 continue;
  739.             }
  740.    
  741.             if(stricmp(callname.c_str(),findname)!=0)
  742.             {      // Different function
  743.                 continue;
  744.             }
  745.  
  746.             if(!del)
  747.             {                     // Set breakpoint
  748.                 Setbreakpoint(pref->addr,TY_ACTIVE,0);
  749.                 Deletenamerange(pref->addr,pref->addr+1,NM_BREAK);
  750.                 Deletenamerange(pref->addr,pref->addr+1,NM_BREAKEXPL);
  751.                 Deletenamerange(pref->addr,pref->addr+1,NM_BREAKEXPR);
  752.                 bpnmb++;
  753.             }
  754.             else
  755.             {
  756.                  Deletebreakpoints(pref->addr,pref->addr+1,true);
  757.                  bpnmb++;
  758.             }
  759.         }
  760.         variables["$RESULT"]=bpnmb;
  761.  
  762.         if (closeWindow && reftable->hw != 0)
  763.             DestroyWindow(reftable->hw);
  764.  
  765.         Broadcast(WM_USER_CHALL,0,0);
  766.         return true;
  767.     }
  768.    return false;
  769. }
  770.  
  771. bool OllyLang::DoBUF(string args)
  772. {
  773.     string op[1];
  774.  
  775.     if(!CreateOp(args, op, 1))
  776.         return false;
  777.  
  778.     if (is_variable(op[0])) {
  779.         if (variables[op[0]].vt == STR) {
  780.  
  781.             if (!variables[op[0]].isbuf)
  782.                 variables[op[0]] = "#"+variables[op[0]].strbuffhex()+"#";
  783.  
  784.             return true;
  785.  
  786.         } else if (variables[op[0]].vt == DW) {
  787.  
  788.             var v; v="##";
  789.             v+=variables[op[0]].dw;
  790.             variables[op[0]]=v;
  791.  
  792.             return true;
  793.  
  794.         }
  795.     }
  796.     return false;
  797. }
  798.  
  799. bool OllyLang::DoCALL(string args)
  800. {
  801.     if (args=="")
  802.         return false;
  803.  
  804.     if (labels.find(args)!=labels.end())
  805.     {
  806.         calls.push_back(script_pos);
  807.         return DoJMP(args);
  808.     }
  809.     errorstr="Label not found";
  810.     return false;
  811. }
  812.  
  813. bool OllyLang::DoCLOSE(string args)
  814. {
  815.  
  816.     string ops[1];
  817.  
  818.     if(!CreateOperands(args, ops, 1))
  819.         return false;
  820.  
  821.     HWND hwnd = 0;
  822.     string str;
  823.     bool bOk = false;
  824.  
  825.     t_table * tbl = NULL;
  826.     t_dump * dmp = NULL;
  827.  
  828.     if( GetSTROpValue("\""+ops[0]+"\"", str) )
  829.     {
  830.         transform(str.begin(), str.end(), str.begin(), toupper);
  831.        
  832.         if(str == "SCRIPT")
  833.         {          
  834.             hwnd = wndProg.hw;
  835.             bOk = true;
  836.         }
  837.         else if(str == "SCRIPTLOG")
  838.         {
  839.             hwnd = wndLog.hw;
  840.             bOk = true;
  841.         }
  842.         else if(str == "MODULES")
  843.         {
  844.             tbl = (t_table *) Plugingetvalue(VAL_MODULES);
  845.             hwnd = tbl->hw;
  846.         }
  847.         else if(str == "MEMORY")
  848.         {
  849.             tbl = (t_table *) Plugingetvalue(VAL_MEMORY);
  850.             hwnd = tbl->hw;
  851.         }
  852.         else if(str == "THREADS")
  853.         {
  854.             tbl = (t_table *) Plugingetvalue(VAL_THREADS);
  855.             hwnd = tbl->hw;
  856.         }
  857.         else if(str == "BREAKPOINTS")
  858.         {
  859.             tbl = (t_table *) Plugingetvalue(VAL_BREAKPOINTS);
  860.             hwnd = tbl->hw;
  861.         }
  862.         else if(str == "REFERENCES")
  863.         {
  864.             tbl = (t_table *) Plugingetvalue(VAL_REFERENCES);
  865.             hwnd = tbl->hw;
  866.         }
  867.         else if(str == "SOURCELIST")
  868.         {
  869.             tbl = (t_table *) Plugingetvalue(VAL_SOURCELIST);
  870.             hwnd = tbl->hw;
  871.         }
  872.         else if(str == "WATCHES")
  873.         {
  874.             tbl = (t_table *) Plugingetvalue(VAL_WATCHES);
  875.             hwnd = tbl->hw;
  876.         }
  877.         else if(str == "PATCHES")
  878.         {
  879.             tbl = (t_table *) Plugingetvalue(VAL_PATCHES);
  880.             hwnd = tbl->hw;
  881.         }
  882.         else if(str == "CPU")
  883.         {
  884.             dmp = (t_dump *) Plugingetvalue(VAL_CPUDASM);
  885.             hwnd = dmp->table.hw;
  886.             if (hwnd != 0) hwnd = GetParent(hwnd);
  887.         }
  888.         else if(str == "RUNTRACE")
  889.         {                      
  890.             hwnd = GetODBGWindow(NULL,"ARTRACE");
  891.             bOk = true;
  892.         }
  893.         else if(str == "WINDOWS")
  894.         {
  895.             hwnd = GetODBGWindow(NULL,"AWINDOWS");
  896.             bOk = true;
  897.         }
  898.         else if(str == "CALLSTACK")
  899.         {
  900.             hwnd = GetODBGWindow(NULL,"ACALLSTK");
  901.             bOk = true;
  902.         }
  903.         else if(str == "LOG")
  904.         {
  905.             hwnd = GetODBGWindow(NULL,"ALIST");
  906.             bOk = true;
  907.         }
  908.         else if(str == "TEXT")
  909.         {
  910.             hwnd = GetODBGWindow(NULL,"ASHOWTEXT");
  911.             bOk = true;
  912.         }
  913.         else if(str == "FILE")
  914.         {
  915.             hwnd = GetODBGWindow(NULL,"ADUMPFILE");
  916.             bOk = true;
  917.         }
  918.         else if(str == "HANDLES")
  919.         {
  920.             hwnd = GetODBGWindow(NULL,"AHANDLES");
  921.             bOk = true;
  922.         }
  923.         else if(str == "SEH")
  924.         {
  925.             hwnd = GetODBGWindow(NULL,"ASEH");
  926.             bOk = true;
  927.         }
  928.         else if(str == "SOURCE")
  929.         {
  930.             hwnd = GetODBGWindow(NULL,"ASOURCE");
  931.             bOk = true;
  932.         }
  933.  
  934.         if (hwnd != 0) {
  935.             DestroyWindow(hwnd);
  936.         }
  937.         if (tbl || dmp || hwnd || bOk)
  938.             return true;
  939.     }
  940.  
  941.     if(hwnd==0 && GetDWOpValue(ops[0], (ulong &) hwnd) ) {
  942.         if (hwnd != 0) {
  943.             DestroyWindow(hwnd);
  944.             return true;
  945.         }
  946.     }
  947.  
  948.     errorstr = "Bad operand";
  949.     return false;
  950. }
  951.  
  952. bool OllyLang::DoCMP(string args)
  953. {
  954.     string ops[3];
  955.     ulong dw1=0, dw2=0, size=0;
  956.     string s1, s2;
  957.  
  958.     if(!CreateOperands(args, ops, 3)) {
  959.         if(!CreateOperands(args, ops, 2))
  960.             return false;
  961.     } else {
  962.         GetDWOpValue(ops[2], size);
  963.     }
  964.  
  965.     if(GetDWOpValue(ops[0], dw1)
  966.         && GetDWOpValue(ops[1], dw2))
  967.     {
  968.         if (size==1) {
  969.             dw1&=0xFF;
  970.             dw2&=0xFF;
  971.         }
  972.         else if (size==2) {
  973.             dw1&=0xFFFF;
  974.             dw2&=0xFFFF;
  975.         }
  976.  
  977.         if(dw1 == dw2)
  978.         {
  979.             zf = 1;
  980.             cf = 0;
  981.         }
  982.         else if(dw1 > dw2)
  983.         {
  984.             zf = 0;
  985.             cf = 0;
  986.         }
  987.         else if(dw1 < dw2)
  988.         {
  989.             zf = 0;
  990.             cf = 1;
  991.         }
  992.         return true;
  993.     }
  994.     else if(GetANYOpValue(ops[0], s1, true) // see also SCMP command, code is not finished here...
  995.             && GetANYOpValue(ops[1], s2, true))
  996.     {
  997.         var v1=s1,v2=s2;
  998.         if (size>0) {
  999.             v1.resize(size);
  1000.             v2.resize(size);
  1001.         }
  1002.         int res = v1.compare(v2); //Error if -2 (type mismatch)
  1003.         if(res == 0)
  1004.         {
  1005.             zf = 1;
  1006.             cf = 0;
  1007.         }
  1008.         else if(res > 0)
  1009.         {
  1010.             zf = 0;
  1011.             cf = 0;
  1012.         }
  1013.         else if(res < 0)
  1014.         {
  1015.             zf = 0;
  1016.             cf = 1;
  1017.         }
  1018.         return true;
  1019.     }
  1020.     return false;
  1021. }
  1022.  
  1023. bool OllyLang::DoCMT(string args)
  1024. {
  1025.     string ops[2];
  1026.  
  1027.     if(!CreateOperands(args, ops, 2))
  1028.         return false;
  1029.  
  1030.     string cmt;
  1031.     ulong addr;
  1032.  
  1033.     if(GetDWOpValue(ops[0], addr)
  1034.         && GetSTROpValue(ops[1], cmt))
  1035.     {
  1036.         if(cmt != "")
  1037.         {
  1038.             char cmtt[1024]={0};
  1039.             strcpy(cmtt, cmt.c_str());
  1040.             Insertname(addr, NM_COMMENT, cmtt);
  1041.             Broadcast(WM_USER_CHALL, 0, 0);
  1042.         }
  1043.         else
  1044.         {
  1045.             Deletenamerange(addr, addr + 1, NM_COMMENT);
  1046.             Broadcast(WM_USER_CHALL, 0, 0);
  1047.         }
  1048.         return true;
  1049.     }
  1050.     return false;
  1051. }
  1052.  
  1053. bool OllyLang::DoCOB(string args)
  1054. {
  1055.     EOB_row = -1;
  1056.     return true;
  1057. }
  1058.  
  1059. bool OllyLang::DoCOE(string args)
  1060. {
  1061.     EOE_row = -1;
  1062.     return true;
  1063. }
  1064.  
  1065. bool OllyLang::DoDBH(string args)
  1066. {
  1067.     t_thread* thr = Findthread(Getcputhreadid());
  1068.     BYTE buf[4];
  1069.     ulong fs = thr->reg.limit[2]; // BUG IN ODBG!!!
  1070.     fs += 0x30;
  1071.     Readmemory(buf, fs, 4, MM_RESTORE);
  1072.     fs = *((ulong*)buf);
  1073.     fs += 2;
  1074.     buffer[0] = 0;
  1075.     Writememory(buf, fs, 1, MM_RESTORE);
  1076.     return true;  
  1077. }
  1078.  
  1079. bool OllyLang::DoDBS(string args)
  1080. {
  1081.     t_thread* thr = Findthread(Getcputhreadid());
  1082.     BYTE buffer[4];
  1083.     ulong fs = thr->reg.limit[2]; // BUG IN ODBG!!!
  1084.     fs += 0x30;
  1085.     Readmemory(buffer, fs, 4, MM_RESTORE);
  1086.     fs = *((ulong*)buffer);
  1087.     fs += 2;
  1088.     buffer[0] = 1;
  1089.     Writememory(buffer, fs, 1, MM_RESTORE);
  1090.     return true;  
  1091. }
  1092.  
  1093. bool OllyLang::DoDEC(string args)
  1094. {
  1095.     string ops[1];
  1096.  
  1097.     if(!CreateOperands(args, ops, 1))
  1098.         return false;
  1099.  
  1100.     return DoSUB(ops[0] + ", 1");
  1101. }
  1102.  
  1103. bool OllyLang::DoDIV(string args)
  1104. {
  1105.     string ops[2];
  1106.  
  1107.     if(!CreateOperands(args, ops, 2))
  1108.         return false;
  1109.  
  1110.     ulong dw1, dw2;
  1111.     if(GetDWOpValue(ops[0], dw1) && GetDWOpValue(ops[1], dw2))
  1112.     {
  1113.         if(dw2==0)
  1114.         {
  1115.             errorstr = "Division by 0";
  1116.             return false;
  1117.         }
  1118.         args = ops[0] + ", 0" + ultoa(dw1 / dw2, buffer, 16);
  1119.         nIgnoreNextValuesHist++;
  1120.         return DoMOV(args);
  1121.     }
  1122.     return false;
  1123. }
  1124.  
  1125.  
  1126.  
  1127.  
  1128. bool OllyLang::DoDM(string args)
  1129. {
  1130.     string ops[3];
  1131.     if(!CreateOperands(args, ops, 3))
  1132.         return false;
  1133.  
  1134.     char spath[MAX_PATH];
  1135.     strcpy(spath, (char*)Plugingetvalue(VAL_EXEFILENAME));
  1136.     string path = spath;
  1137.  
  1138.     path = path.substr(0, path.rfind('\\') + 1);
  1139.    
  1140.     ulong addr, size;
  1141.     string filename;
  1142.     if (GetDWOpValue(ops[0], addr)
  1143.         && GetDWOpValue(ops[1], size)
  1144.         && GetSTROpValue(ops[2], filename))
  1145.     {
  1146.         if (filename.find(":\\") != string::npos)
  1147.             path = filename;
  1148.         else
  1149.             path += filename;
  1150.         char* membuf = new char[size];
  1151.         int memlen = Readmemory(membuf, addr, size, MM_RESILENT);
  1152.  
  1153.         ofstream fout;
  1154.         fout.open(path.c_str(), ios::binary);
  1155.         if(!fout.fail())
  1156.         {
  1157.             for(int i = 0; i < memlen; i++)
  1158.                 fout << membuf[i];
  1159.             fout.close();
  1160.             delete [] membuf;
  1161.             variables["$RESULT"] = memlen;
  1162.             return true;
  1163.         }
  1164.         else
  1165.         {
  1166.             errorstr = "Couldn't create file!";
  1167.             delete [] membuf;
  1168.             return false;
  1169.         }
  1170.     }
  1171.     return false;
  1172. }
  1173.  
  1174. bool OllyLang::DoDMA(string args)
  1175. {
  1176.     string ops[3];
  1177.     if(!CreateOperands(args, ops, 3))
  1178.         return false;
  1179.  
  1180.     char spath[MAX_PATH];
  1181.     strcpy(spath, (char*)Plugingetvalue(VAL_EXEFILENAME));
  1182.     string path = spath;
  1183.  
  1184.     path = path.substr(0, path.rfind('\\') + 1);
  1185.  
  1186.     ulong addr, size;
  1187.     string filename;
  1188.     if(GetDWOpValue(ops[0], addr)
  1189.         && GetDWOpValue(ops[1], size)
  1190.         && GetSTROpValue(ops[2], filename))
  1191.     {
  1192.         char* membuf = new char[size];
  1193.         int memlen = Readmemory(membuf, addr, size, MM_RESILENT);
  1194.  
  1195.         if (filename.find(":\\") != string::npos)
  1196.             path = filename;
  1197.         else
  1198.             path += filename;
  1199.         ofstream fout;
  1200.         fout.open(path.c_str(), ios::app | ios::binary);
  1201.         if(!fout.fail())
  1202.         {
  1203.             for(int i = 0; i < memlen; i++)
  1204.                 fout << membuf[i];
  1205.             fout.close();
  1206.             delete [] membuf;
  1207.             variables["$RESULT"] = memlen;
  1208.             return true;
  1209.         }
  1210.         else
  1211.         {
  1212.             errorstr = "Couldn't create file!";
  1213.             delete [] membuf;
  1214.             return false;
  1215.         }
  1216.     }
  1217.     return false;
  1218. }
  1219.  
  1220. bool OllyLang::DoDPE(string args)
  1221. {
  1222.     string ops[2];
  1223.  
  1224.     if(!CreateOperands(args, ops, 2))
  1225.         return false;
  1226.  
  1227.     char spath[MAX_PATH];
  1228.     strcpy(spath, (char*)Plugingetvalue(VAL_EXEFILENAME));
  1229.     string path = spath;
  1230.  
  1231.     path = path.substr(0, path.rfind('\\') + 1);
  1232.  
  1233.     string filename;
  1234.     ulong ep;
  1235.     if(GetSTROpValue(ops[0], filename)
  1236.         && GetDWOpValue(ops[1], ep))
  1237.     {
  1238.         if (filename.find(":\\") != string::npos)
  1239.             path = filename;
  1240.         else
  1241.             path += filename;
  1242.         bool result = false;
  1243.         result = GetPEInfo(ep);
  1244.         if (!result)
  1245.             return result;
  1246.         result = SaveDump(path, ep);
  1247.         return result;
  1248.     }
  1249.     return false;
  1250. }
  1251.  
  1252. bool OllyLang::DoENDE(string args)
  1253. {
  1254.     // Free memory
  1255.     if (pmemforexec!=NULL)
  1256.         DelProcessMemoryBloc((DWORD) pmemforexec);
  1257.     pmemforexec=NULL;
  1258.     return true;
  1259. }
  1260.  
  1261. bool OllyLang::DoERUN(string args)
  1262. {
  1263.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 0, 1, VK_F9);
  1264.     require_ollyloop = 1;
  1265.     return true;
  1266. }
  1267.  
  1268. bool OllyLang::DoESTI(string args)
  1269. {
  1270.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 0, 1, VK_F7);
  1271.     require_ollyloop = 1;
  1272.     return true;
  1273. }
  1274.  
  1275. bool OllyLang::DoESTEP(string args)
  1276. {
  1277.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 0, 1, VK_F8);
  1278.     require_ollyloop = 1;
  1279.     return true;
  1280. }
  1281.  
  1282. bool OllyLang::DoEOB(string args)
  1283. {
  1284.     string ops[1];
  1285.  
  1286.     if(!CreateOperands(args, ops, 1))
  1287.         return false;
  1288.  
  1289.     if(ops[0] == "") // Go interactive
  1290.         EOB_row = -1;
  1291.     else if(labels.find(ops[0]) != labels.end()) // Set label to go to
  1292.         EOB_row = labels[ops[0]];
  1293.     else
  1294.         return false;
  1295.     return true;
  1296. }
  1297.  
  1298. bool OllyLang::DoEOE(string args)
  1299. {
  1300.     string ops[1];
  1301.  
  1302.     if(!CreateOperands(args, ops, 1))
  1303.         return false;
  1304.  
  1305.     if(ops[0] == "") // Go interactive
  1306.         EOE_row = -1;
  1307.     else if(labels.find(ops[0]) != labels.end()) // Set label to go to
  1308.         EOE_row = labels[ops[0]];
  1309.     else
  1310.         return false;
  1311.     return true;
  1312. }
  1313.  
  1314. bool OllyLang::DoEVAL(string args)
  1315. {
  1316.     string ops[1];
  1317.  
  1318.     if (!CreateOperands(args, ops, 1))
  1319.         return false;
  1320.  
  1321.     string to_eval;
  1322.  
  1323.     if (GetSTROpValue(ops[0], to_eval))
  1324.     {
  1325.         string res = ResolveVarsForExec(to_eval,false);
  1326.         variables["$RESULT"] = res;
  1327.         return true;
  1328.     }
  1329.     return false;
  1330. }
  1331.  
  1332. bool OllyLang::DoEXEC(string args)
  1333. {
  1334.     // Old eip
  1335.     ulong eip;
  1336.     // For Assemble API
  1337.     t_asmmodel model;
  1338.     char error[255] = {0};
  1339.     char buffer[255] = {0};
  1340.     // Bytes assembled
  1341.     int len = 0, totallen = 0;
  1342.     bool res = true;
  1343.     // Temp storage
  1344.     string tmp;
  1345.  
  1346.     // Get process handle and save eip
  1347.     HANDLE hDebugee = (HANDLE)Plugingetvalue(VAL_HPROCESS);
  1348.     t_thread* thr = Findthread(Getcputhreadid());
  1349.     eip = thr->reg.ip;
  1350.  
  1351.     // Allocate memory for code
  1352.     pmemforexec = VirtualAllocEx(hDebugee, NULL, 0x1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  1353.  
  1354.     // Pass EXECs
  1355.     script_pos++;
  1356.     setProgLineResult(script_pos,variables["$RESULT"]);
  1357.  
  1358.     // Assemble and write code to memory (everything between EXEC and ENDE)
  1359.     while(ToLower(script[script_pos]) != "ende")
  1360.     {
  1361.         tmp = ResolveVarsForExec(script[script_pos],true);
  1362.         sprintf(buffer, tmp.c_str());
  1363.         len = Assemble(buffer, (ulong)pmemforexec + totallen, &model, 0, 0, error);
  1364.         if (len < 0) {
  1365.             errorstr = tmp+"\n"+error;
  1366.             return false;
  1367.         } else {
  1368.             WriteProcessMemory(hDebugee, (LPVOID)((ulong)pmemforexec + totallen), model.code, len, 0);
  1369.             totallen += len;
  1370.         }
  1371.         script_pos++;
  1372.         setProgLineEIP(script_pos,eip);
  1373.         setProgLineResult(script_pos,variables["$RESULT"]);
  1374.         if (script_pos>script.size()) {
  1375.             DoENDE("");
  1376.             errorstr = "EXEC needs ENDE command !";
  1377.             return false;
  1378.         }
  1379.     }
  1380.  
  1381.     //return before ENDE command
  1382.     script_pos--;
  1383.  
  1384.     // Assemble and write jump to original EIP
  1385.     sprintf(buffer, "JMP %lX", eip);
  1386.     len = Assemble(buffer, (ulong)pmemforexec + totallen, &model, 0, 0, error);
  1387.     if (len < 0) {
  1388.         errorstr = error;
  1389.         return false;
  1390.     } else {
  1391.         WriteProcessMemory(hDebugee, (LPVOID)((ulong)pmemforexec + totallen), model.code, len, 0);
  1392.     }
  1393.  
  1394.     // Set new eip and run to the original one
  1395.     thr->reg.ip = (ulong)pmemforexec;
  1396.     thr->reg.modified = 1;
  1397.     thr->regvalid = 1;
  1398.  
  1399.     // tell to odbgscript to ignore next breakpoint
  1400.     bInternalBP=true;
  1401.  
  1402.     Broadcast(WM_USER_CHREG, 0, 0);
  1403.     Go(Getcputhreadid(), eip, STEP_RUN, 0, 1);
  1404.    
  1405.     t_dbgmemblock block={0};
  1406.     block.hmem = pmemforexec;
  1407.     block.size = 0x1000;
  1408.     block.script_pos = script_pos;
  1409.     block.free_at_eip = eip;
  1410.     block.autoclean = true;
  1411.  
  1412.     regBlockToFree(block);
  1413.     require_addonaction = 1;
  1414.     require_ollyloop = 1;
  1415.  
  1416.     return true;
  1417. }
  1418.  
  1419. bool OllyLang::DoFILL(string args)
  1420. {
  1421.     string ops[3];
  1422.  
  1423.     if(!CreateOperands(args, ops, 3))
  1424.         return false;
  1425.     ulong start, len, val;
  1426.  
  1427.     if(GetDWOpValue(ops[0], start)
  1428.         && GetDWOpValue(ops[1], len)
  1429.         && GetDWOpValue(ops[2], val))
  1430.     {
  1431.         BYTE* buffer = new BYTE[len];
  1432.         FillMemory(buffer,len,val);
  1433. /*      BYTE b = 0 + val;
  1434.         for(ulong i = 0; i < len; i++)
  1435.             buffer[i] = b;*/
  1436.         Writememory(buffer, start, len, MM_SILENT);
  1437.         delete []buffer;
  1438.         Broadcast(WM_USER_CHALL, 0, 0);
  1439.         require_ollyloop = 1;
  1440.         return true;
  1441.     }
  1442.     return false;
  1443. }
  1444.  
  1445. bool OllyLang::DoFIND(string args)
  1446. {
  1447.     string ops[3];
  1448.     ulong maxsize=0;
  1449.     if(CreateOp(args, ops, 3)){
  1450.         GetDWOpValue(ops[2], maxsize);
  1451.     }
  1452.     else if(!CreateOp(args, ops, 2))
  1453.         return false;
  1454.  
  1455.     ulong addr,dw;
  1456.     string data=ops[1];
  1457.     string finddata=ops[1];
  1458.  
  1459.     if(UnquoteString(ops[1], '#', '#'))
  1460.     {
  1461.         if(ops[1].length() % 2 != 0)
  1462.         {
  1463.             errorstr = "Hex string must have an even number of characters!";
  1464.             return false;
  1465.         }
  1466.         finddata=ops[1];
  1467.     }
  1468.     else if (UnquoteString(ops[1], '"', '"'))
  1469.     {
  1470.        string wilddata;
  1471.        wilddata=ops[1];
  1472.        Str2Hex(ops[1],finddata);
  1473.        ReplaceString(finddata,"3f","??");
  1474.     }
  1475.     else if(GetDWOpValue(ops[1], dw) && !is_variable(ops[1]))
  1476.     {
  1477.         itoa(rev(dw),buffer,16);
  1478.         string data1=buffer;
  1479.         transform( data1.begin(),data1.end(), data1.begin(),(int(*)(int)) toupper );
  1480.         while(data1.length() < data.length())
  1481.             data1.insert(0,"0");
  1482.         while (data1.length() > data.length())
  1483.             data1.erase(data1.length()-1,1);
  1484.        
  1485.         finddata=data1;    
  1486.     }
  1487.     else if(is_variable(ops[1]))
  1488.     {  
  1489.         GetANYOpValue(ops[1], data);
  1490.         if (UnquoteString(data, '#', '#'))
  1491.         {
  1492.           finddata=data;   
  1493.         }
  1494.         else if(GetSTROpValue(ops[1],data))
  1495.         {
  1496.           Str2Hex(data,finddata);
  1497.         }
  1498.         else if(GetDWOpValue(ops[1], dw))
  1499.         {
  1500.             itoa(rev(dw),buffer,16);
  1501.             string data1=buffer;
  1502.             while(data1.length() < data.length())
  1503.                 data1.insert(0,"0");
  1504.             while (data1.length() > data.length())
  1505.                 data1.erase(data1.length()-1,1);
  1506.  
  1507.             finddata=data1;
  1508.         }
  1509.     }
  1510.  
  1511.     if(GetDWOpValue(ops[0], addr) && is_hexwild(finddata))
  1512.     {
  1513.  
  1514.         t_memory* tmem = Findmemory(addr);
  1515.  
  1516.         if (tmem==NULL) {
  1517.             // search in current mem block
  1518.             variables["$RESULT"] = 0;
  1519.             return true;
  1520.         }
  1521.  
  1522.         if(finddata.find('?') != -1)
  1523.         {
  1524.             // Wildcard search
  1525.             char *membuf = 0;
  1526.             int memlen = tmem->size - (addr - tmem->base);
  1527.             membuf = new char[memlen];
  1528.  
  1529.             memlen = Readmemory(membuf, addr, memlen, MM_RESILENT);
  1530.             int pos;
  1531.  
  1532.             if (maxsize && maxsize <= memlen)
  1533.                 pos = FindWithWildcards(membuf, finddata.c_str(), maxsize);
  1534.             else
  1535.                 pos = FindWithWildcards(membuf, finddata.c_str(), memlen);
  1536.  
  1537.             delete [] membuf;
  1538.  
  1539.             if(pos > -1)
  1540.                 variables["$RESULT"] = addr + pos;
  1541.             else
  1542.                 variables["$RESULT"] = 0;
  1543.         }
  1544.         else
  1545.         {
  1546.             // Regular search
  1547.             char *membuf = 0;
  1548.             int memlen = tmem->size - (addr - tmem->base);
  1549.  
  1550.             int len = Str2Rgch(finddata, buffer, sizeof(buffer));
  1551.             if (maxsize && maxsize <= memlen)
  1552.             {
  1553.                 memlen = maxsize;
  1554.             }
  1555.             membuf = new char[memlen];
  1556.             memlen = Readmemory(membuf, addr, memlen, MM_RESILENT);
  1557.             char *p = search(membuf, membuf + memlen, buffer, buffer + len);
  1558.  
  1559.             delete[] membuf;
  1560.  
  1561.             if(p < membuf + memlen)
  1562.                 variables["$RESULT"] = addr + p - membuf;
  1563.             else
  1564.                 variables["$RESULT"] = 0;
  1565.         }
  1566.         return true;
  1567.     }
  1568.     return false;
  1569. }
  1570.  
  1571. bool OllyLang::DoFINDCALLS(string args)
  1572. {
  1573.     string ops[2];
  1574.     if(!CreateOp(args, ops, 2))
  1575.         if (args=="")
  1576.             return false;
  1577.  
  1578.     bool bRefVisible=false,bResetDisam=false;
  1579.     ulong addr,base,size,disamsel=0;
  1580.  
  1581.     Getdisassemblerrange(&base,&size);
  1582.  
  1583.     //Get initial Ref Window State
  1584.     t_table* tt;
  1585.     tt=(t_table*) Plugingetvalue(VAL_REFERENCES);
  1586.     if (tt!=NULL)
  1587.         bRefVisible=(tt->hw!=0);
  1588.  
  1589.     t_dump* td;
  1590.     td=(t_dump*) Plugingetvalue(VAL_CPUDASM);
  1591.     if (td==NULL)
  1592.         return false;
  1593.  
  1594.     if (GetDWOpValue(ops[0], addr))
  1595.     {
  1596.         if (addr<base || addr>=(base+size)) {
  1597.             //outside debugger window range
  1598.             disamsel=td->sel0;
  1599.             Setdisasm(addr,0,0);
  1600.             bResetDisam=true;
  1601.         }
  1602.  
  1603.         variables["$RESULT"]=0;
  1604.         if (Findalldllcalls(td,addr,NULL)>0) {
  1605.            
  1606.             if (tt==NULL)
  1607.                 tt=(t_table*) Plugingetvalue(VAL_REFERENCES);
  1608.  
  1609.             if (tt!=NULL)
  1610.             {
  1611.                 t_ref* tr;
  1612.  
  1613.                 if (tt->data.n > 1)
  1614.                 {
  1615.                     //Filter results
  1616.                     string filter;
  1617.                     if (GetSTROpValue(ops[1], filter) && filter!="") {
  1618.                         //filter=ToLower(filter);
  1619.                         (char*) buffer[TEXTLEN+1];
  1620.                         for (int nref=tt->data.n-1;nref>0;nref--) {
  1621.                             tr=(t_ref*) Getsortedbyselection(&tt->data, nref);
  1622.                             if (tr!=NULL && tr->dest!=0) {
  1623.                                 //ZeroMemory(buffer,TEXTLEN+1);
  1624.                                 //Decodename(tr->dest,NM_LABEL,buffer);
  1625.                                 Findlabel(tr->dest,buffer);
  1626.                                 if (stricmp(buffer,filter.c_str())!=0)
  1627.                                     Deletesorteddata(&tt->data,tr->addr);
  1628.                             }
  1629.                         }
  1630.                     }
  1631.  
  1632.                     tr=(t_ref*) Getsortedbyselection(&tt->data, 1); //0 is CPU initial
  1633.                     if (tr!=NULL)
  1634.                         variables["$RESULT"]=tr->addr;
  1635.                 }
  1636.  
  1637.                 if (tt->hw && !bRefVisible) {
  1638.                     DestroyWindow(tt->hw);
  1639.                     tt->hw=0;
  1640.                 }
  1641.             }
  1642.         }
  1643.         if (bResetDisam)
  1644.             Setdisasm(disamsel,0,0);
  1645.         return true;
  1646.     }
  1647.     return false;
  1648. }
  1649.  
  1650. //search for asm command in disasm window
  1651. bool OllyLang::DoFINDCMD(string args)
  1652. {
  1653.     string ops[2];
  1654.     if(!CreateOp(args, ops, 2))
  1655.         return false;
  1656.  
  1657.     bool bRefVisible=false,bResetDisam=false;
  1658.     string cmd, cmds;
  1659.     int len,pos;
  1660.     ulong addr,base,size,attempt,opsize=3,disamsel=0;
  1661.     int startadr=0,endadr=0,lps=0,length,ncmd=0,cmdpos=0;
  1662.     char error[256]={0};
  1663.  
  1664.     Getdisassemblerrange(&base,&size);
  1665.  
  1666.     //Get initial Ref Window State
  1667.     t_table* tt;
  1668.     tt=(t_table*) Plugingetvalue(VAL_REFERENCES);
  1669.     if (tt!=NULL)
  1670.         bRefVisible=(tt->hw!=0);
  1671.  
  1672.     t_dump* td;
  1673.     td=(t_dump*) Plugingetvalue(VAL_CPUDASM);
  1674.     if (td==NULL)
  1675.         return false;
  1676.  
  1677.     ulong tmpaddr=AddProcessMemoryBloc(0x100,PAGE_EXECUTE_READWRITE);
  1678.  
  1679.     if (GetDWOpValue(ops[0], addr)
  1680.         && GetSTROpValue(ops[1], cmds))
  1681.     {
  1682.         if (addr<base || addr>=(base+size)) {
  1683.             //outside debugger window range
  1684.             disamsel=td->sel0;
  1685.             Setdisasm(addr,0,0);
  1686.             bResetDisam=true;
  1687.         }
  1688.  
  1689.         t_asmmodel model={0};
  1690.         t_extmodel models[NSEQ][NMODELS]={0};
  1691.  
  1692.         length = cmds.length();
  1693.         while (cmdpos<length && ncmd<NSEQ)
  1694.         {
  1695.  
  1696.             endadr= cmds.find(";",startadr);
  1697.             if (endadr==-1)
  1698.             {
  1699.                 endadr=length;
  1700.             }
  1701.             lps=endadr-startadr;
  1702.  
  1703.             cmd=cmds.substr(startadr,lps);
  1704.            
  1705.             attempt=0;
  1706.             strcpy(buffer, cmd.c_str());
  1707.  
  1708.             do {
  1709.  
  1710.                 if((len = Assemble(buffer, tmpaddr, &model, attempt, opsize, error)) <= 0)
  1711.                 {
  1712.                     if (attempt!=0) {
  1713.                         break;
  1714.                     }
  1715.  
  1716.                     pos=(cmd.length()+len);
  1717.                     if (pos>=0 && pos<cmd.length())
  1718.                         errorstr = "\nFINDCMD error at \""+cmd.substr(pos,cmd.length()-pos)+"\"!\n\n";
  1719.                     else
  1720.                         errorstr = "\nFINDCMD error !\n\n";
  1721.                     errorstr.append(error);
  1722.                     goto return_false;
  1723.                 }
  1724.                 memcpy(&models[ncmd][attempt],&model,sizeof(model));
  1725.                 attempt++;
  1726.  
  1727.             } while (len>0 && attempt<NMODELS);
  1728.  
  1729.             startadr=endadr+1;
  1730.             cmdpos+=lps+1;
  1731.  
  1732.             ncmd++;
  1733.         }
  1734.  
  1735.         variables["$RESULT"]=0;
  1736.         if (Findallsequences(td,models,addr,NULL)>0) {
  1737.            
  1738.             if (tt==NULL)
  1739.                 tt=(t_table*) Plugingetvalue(VAL_REFERENCES);
  1740.  
  1741.             if (tt!=NULL)
  1742.             {
  1743.                 t_ref* tr;
  1744.                 if (tt->data.n > 1)
  1745.                 {
  1746.                     tr=(t_ref*) Getsortedbyselection(&tt->data, 1); //0 is CPU initial
  1747.                     if (tr!=NULL)
  1748.                         variables["$RESULT"]=tr->addr;
  1749.                 }
  1750.  
  1751.                 if (tt->hw && !bRefVisible) {
  1752.                     DestroyWindow(tt->hw);
  1753.                     tt->hw=0;
  1754.                 }
  1755.             }
  1756.         }
  1757.         DelProcessMemoryBloc(tmpaddr);
  1758.         if (bResetDisam)
  1759.             Setdisasm(disamsel,0,0);
  1760.         return true;
  1761.  
  1762.     }
  1763. return_false:
  1764.     if (bResetDisam)
  1765.         Setdisasm(disamsel,0,0);
  1766.     DelProcessMemoryBloc(tmpaddr);
  1767.     return false;
  1768. }
  1769. /*
  1770. //Buggy, could assemble different command code bytes, (from chinese code)
  1771. bool OllyLang::DoFINDCMDS(string args)
  1772. {
  1773.  
  1774.     string ops[2];
  1775.     t_asmmodel model;
  1776.     ulong addr;
  1777.     string cmds,args1,cmd;
  1778.     char opcode[256]={0},buff[32]={0},tmp[64]={0},error[64]={0};
  1779.     int i,pos,len=0,length=0,startadr=0,endadr=0,lps=0,codelen=0;
  1780.     int attempt=0,opsize=3;
  1781.  
  1782.     if(!CreateOp(args, ops, 2))
  1783.         return false;
  1784.  
  1785.     if (GetDWOpValue(ops[0], addr)
  1786.         && GetSTROpValue(ops[1], cmds))
  1787.     {
  1788.  
  1789.       if (cmds.find(";")==-1)
  1790.       {
  1791.         nIgnoreNextValuesHist++;
  1792.         return DoFINDoneCMD(args);
  1793.       }
  1794.  
  1795.       length = cmds.length();
  1796.  
  1797.       ulong tmpaddr=AddProcessMemoryBloc(0x100,PAGE_EXECUTE_READWRITE);
  1798.  
  1799.       while (len<length)
  1800.       {
  1801.         endadr= cmds.find(";",startadr);
  1802.         if (endadr==-1)
  1803.         {
  1804.             endadr=length;
  1805.         }
  1806.         lps=endadr-startadr;
  1807.         cmd=cmds.substr(startadr,lps);
  1808.        
  1809.         strcpy(buffer, cmd.c_str());
  1810.         if((codelen = Assemble(buffer, tmpaddr, &model, attempt, opsize, error)) <= 0)
  1811.         {
  1812.             pos=(cmd.length()+codelen);
  1813.             if (pos>=0 && pos<cmd.length())
  1814.                 errorstr = "\nFINDCMDS error on \""+cmd.substr(pos,cmd.length()-pos)+"\"!\n\n";
  1815.             else
  1816.                 errorstr = "\nFINDCMDS error !\n\n";
  1817.             errorstr.append(error);
  1818.             DelProcessMemoryBloc(tmpaddr);
  1819.             return false;
  1820.         }
  1821.         else
  1822.         {
  1823.             sprintf(buff, "%s", (model.code));
  1824.         }
  1825.  
  1826.         i=0;
  1827.         while(i<codelen)
  1828.         {
  1829.             itoa(buff[i],tmp,16);
  1830.             i++;
  1831.             strcat(opcode,tmp);
  1832.         }
  1833.  
  1834.         startadr=endadr+1;
  1835.         len=len+lps+1;
  1836.       }
  1837.       DelProcessMemoryBloc(tmpaddr);
  1838.  
  1839.       args1 = ops[0] + ", " + "#" + opcode + "#";
  1840.       nIgnoreNextValuesHist++;
  1841.       return DoFIND(args1);
  1842.     }
  1843.     return false;
  1844. }
  1845. */
  1846.  
  1847. bool OllyLang::DoFINDMEM(string args)
  1848. {
  1849.  
  1850.     if (args=="")
  1851.         return false;
  1852.  
  1853.     string ops[2];
  1854.  
  1855.     if (args.find(',') == -1)
  1856.         args+=",0";
  1857.  
  1858.     if(!CreateOperands(args, ops, 2))
  1859.         return false;
  1860.  
  1861.     t_table* tt;
  1862.     tt=(t_table*) Plugingetvalue(VAL_MEMORY);
  1863.     if (tt==NULL)
  1864.         return false;
  1865.  
  1866.     t_memory* tm;
  1867.     char szBase[9];
  1868.     ulong start=0;
  1869.     string sArgs;
  1870.     bool bSkip;
  1871.  
  1872.     if (!GetDWOpValue(ops[1], start))
  1873.         return false;
  1874.  
  1875.     for (int m=0; m < tt->data.n; m++) {
  1876.         tm=(t_memory*) Getsortedbyselection(&tt->data, m);
  1877.         if (tm==NULL) {
  1878.             return false;
  1879.         }
  1880.  
  1881.         itoa(tm->base,szBase,16);
  1882.  
  1883.         bSkip=false;
  1884.  
  1885.         if (start > tm->base + tm->size)
  1886.             bSkip=true;
  1887.         else if (start >= tm->base)
  1888.             itoa(start,szBase,16);
  1889.  
  1890.         if (!bSkip) {
  1891.             sArgs="";
  1892.             sArgs.append(szBase);
  1893.             sArgs+=","+ops[0];
  1894.             if (DoFIND(sArgs)) {
  1895.                 if (variables["$RESULT"].dw!=0 || variables["$RESULT"].str!="") {
  1896.                     var_logging=true;
  1897.                     return true;
  1898.                 }
  1899.             }
  1900.             //Display value in ollyscript wdw only once
  1901.             var_logging=false;
  1902.         }
  1903.     }
  1904.  
  1905.     var_logging=true;
  1906.     variables["$RESULT"] = 0;
  1907.     return true;
  1908. }
  1909.  
  1910. bool OllyLang::DoFINDOP(string args)
  1911. {
  1912.     string ops[3];
  1913.     ulong maxsize=0;
  1914.     if(CreateOp(args, ops, 3)){
  1915.         GetDWOpValue(ops[2], maxsize);
  1916.     }
  1917.     else if(!CreateOp(args, ops, 2))
  1918.         return false;
  1919.  
  1920.     ulong addr, dw, endaddr;
  1921.     string data=ops[1];
  1922.     string finddata=ops[1];
  1923.  
  1924.     if (UnquoteString(ops[1], '#', '#'))
  1925.     {
  1926.         if(ops[1].length() % 2 != 0)
  1927.         {
  1928.             errorstr = "Hex string must have an even number of characters!";
  1929.             return false;
  1930.         }
  1931.     }
  1932.     else if (GetDWOpValue(ops[1], dw) && !is_variable(ops[1]))
  1933.     {
  1934. //      DoREV(ops[1]);
  1935.         itoa(dw,buffer,16);
  1936.         string data1=buffer;
  1937.         while(data1.length() < data.length())
  1938.             data1.insert(0,"0");
  1939.         while (data1.length() > data.length())
  1940.             data1.erase(data1.length()-1,1);
  1941.  
  1942.         ops[1]=data1;
  1943.     }
  1944.     else if(is_variable(ops[1]))
  1945.     {  
  1946.         GetANYOpValue(ops[1], data);
  1947.         if (UnquoteString(data, '#', '#'))
  1948.         {
  1949.           ops[1]=data; 
  1950.          }
  1951.         else if(GetSTROpValue(ops[1],data))
  1952.         {
  1953.             errorstr = "findop: incorrect data !";
  1954.             return false;
  1955.         }
  1956.         else if(GetDWOpValue(ops[1], dw))
  1957.         {
  1958.     //      DoREV(ops[1]);
  1959.             itoa(dw,buffer,16);
  1960.             string data1=buffer;
  1961.             while(data1.length() < data.length())
  1962.                 data1.insert(0,"0");
  1963.             while (data1.length() > data.length())
  1964.                 data1.erase(data1.length()-1,1);
  1965.            
  1966.             ops[1]=data1;
  1967.         }
  1968.     }
  1969.  
  1970.     int result = -1;
  1971.     int ok = 0;
  1972.     ulong addrsize;
  1973.     if(GetDWOpValue(ops[0], addr) && is_hexwild(ops[1]))
  1974.     {
  1975.         t_memory* tmem = Findmemory(addr);
  1976.         char cmd[MAXCMDSIZE] = {0};
  1977.  
  1978.         if(maxsize && maxsize <= tmem->size)
  1979.         {
  1980.             tmem->base = addr;
  1981.             tmem->size = maxsize;
  1982.         }
  1983.  
  1984.         do
  1985.         {
  1986.             ulong nextaddr = Disassembleforward(0, tmem->base, tmem->size, addr, 1, 0);
  1987. //          endaddr = Disassembleforward(0, tmem->base, tmem->size, addr, 1, 0);
  1988.             ok = Readcommand(addr, cmd);
  1989.  
  1990.             addrsize = nextaddr -addr;
  1991.             addr=nextaddr;
  1992.  
  1993.             if(addr >= tmem->base + tmem->size)
  1994.                 ok = 0;
  1995.  
  1996.             if(ok)
  1997.                 result = FindWithWildcards(cmd, ops[1].c_str(),addrsize);
  1998.  
  1999.         } while(result != 0 && ok != 0);
  2000.     }
  2001.     if(ok != 0){
  2002.         variables["$RESULT"] = addr - addrsize;
  2003.         variables["$RESULT_1"] = addrsize;
  2004.     }
  2005.  
  2006.  
  2007.     else
  2008.         variables["$RESULT"] = 0;
  2009.     return true;
  2010. }
  2011.  
  2012. bool OllyLang::DoFINDOPREV(string args)
  2013. {
  2014.     string ops[2];
  2015.  
  2016.     if(!CreateOperands(args, ops, 2))
  2017.         return false;
  2018.  
  2019.     if(ops[1].length() % 2 != 0)
  2020.     {
  2021.         errorstr = "Hex string must have an even number of characters!";
  2022.         return false;
  2023.     }
  2024.  
  2025.     DWORD addr, endaddr;
  2026.     int result = -1;
  2027.     int ok = 0;
  2028.  
  2029.     if(GetDWOpValue(ops[0], addr) && UnquoteString(ops[1], '#', '#'))
  2030.     {
  2031.         t_memory* tmem = Findmemory(addr);
  2032.         char cmd[MAXCMDSIZE] = {0};
  2033.         do
  2034.         {
  2035.             addr = Disassembleback(0, tmem->base, tmem->size, addr, 1, 0);
  2036.             endaddr = Disassembleback(0, tmem->base, tmem->size, addr, 1, 0);
  2037.             ok = Readcommand(addr, cmd);
  2038.  
  2039.             if(addr == tmem->base + tmem->size)
  2040.                 ok = 0;
  2041.  
  2042.             if(ok)
  2043.                 result = FindWithWildcards(cmd, ops[1].c_str(), endaddr - addr);
  2044.         } while(result != 0 && ok != 0);
  2045.     }
  2046.     if(ok != 0)
  2047.         variables["$RESULT"] = addr;
  2048.     else
  2049.         variables["$RESULT"] = 0;
  2050.     return true;
  2051.  
  2052. }
  2053.  
  2054.  
  2055. bool OllyLang::DoFREE(string args)
  2056. {
  2057.     string ops[2];
  2058.  
  2059.     if(!CreateOperands(args, ops, 2)) {
  2060.         ops[1]="0";
  2061.         if(!CreateOperands(args, ops, 1))
  2062.         return false;
  2063.     }
  2064.  
  2065.     void * hMem;
  2066.     ulong addr, size;
  2067.  
  2068.     if(GetDWOpValue(ops[0], addr) && GetDWOpValue(ops[1], size))
  2069.     {
  2070.         hMem = (void*)addr;
  2071.         //To Refresh Memory Window
  2072.         require_ollyloop=1;
  2073.         if (size==0) {
  2074.             if (DelProcessMemoryBloc(addr)) {
  2075.                 unregMemBlock(hMem);
  2076.                 Listmemory();
  2077.                 return true;
  2078.             }
  2079.         }
  2080.         else {
  2081.            
  2082.             HANDLE hDbgPrc = (HANDLE) Plugingetvalue(VAL_HPROCESS);
  2083.             variables["$RESULT"] = (DWORD) VirtualFreeEx(hDbgPrc,hMem,size,MEM_DECOMMIT);
  2084.             unregMemBlock(hMem);
  2085.             Listmemory();
  2086.             return true;
  2087.         }
  2088.     }
  2089.     return false;
  2090. }
  2091.  
  2092. bool OllyLang::DoGAPI(string args)
  2093. {
  2094.     string ops[1];
  2095.     ulong addr,size,test,addr2;
  2096.  
  2097.     if(!CreateOp(args, ops, 1))
  2098.         return false;
  2099.  
  2100.     if(GetDWOpValue(ops[0], addr)){
  2101.         BYTE buffer[MAXCMDSIZE];
  2102.         //size=Readmemory(buffer, addr, MAXCMDSIZE, MM_SILENT);
  2103.         size=Readcommand(addr,(char *) buffer);
  2104.  
  2105.        
  2106.         if (size>0) {
  2107.             t_disasm disasm;
  2108.             size=Disasm(buffer,size,addr,NULL,&disasm,DISASM_CODE,NULL);
  2109.             test=disasm.jmpaddr;
  2110.  
  2111.             if (size>0) {
  2112. //              variables["$RESULT"] = disasm.result; //asm command text
  2113. //              variables["$RESULT_1"] = disasm.dump;     //command bytes
  2114.                 variables["$RESULT_3"] = disasm.addrdata;
  2115.                 variables["$RESULT_4"] = disasm.jmpaddr;
  2116.  
  2117.             }
  2118.         }
  2119.         if (test!=NULL)   {
  2120.             t_disasm disasm;
  2121.             size=Disasm(buffer,size,addr,NULL,&disasm,DISASM_CODE,NULL);
  2122.             char sym[4096] = {0};
  2123.             char buf[TEXTLEN] = {0};
  2124.             addr2 = disasm.addrdata;
  2125.             int res = Decodeaddress(addr2, 0, ADC_JUMP | ADC_STRING | ADC_ENTRY | ADC_OFFSET | ADC_SYMBOL, sym, 4096, buf);
  2126.             if(res)
  2127.             {
  2128.             variables["$RESULT"] = sym;
  2129.             char *tmp = strstr(sym, ".");
  2130.             if(tmp)
  2131.             {
  2132.                 strtok(sym, ">");                          //buxfix
  2133.                 *tmp = '\0';
  2134.                 variables["$RESULT_1"] = sym + 2;          //bugfix
  2135.                 variables["$RESULT_2"] = tmp + 1;
  2136.             }
  2137.         }
  2138.             return true;
  2139.         }
  2140.         variables["$RESULT"] = NULL;
  2141.         return true;
  2142.     }
  2143.     return false;
  2144. }
  2145.  
  2146. bool OllyLang::DoGBPM(string args)
  2147. {
  2148.     variables["$RESULT"]=break_memaddr;
  2149.     return true;
  2150. }
  2151.  
  2152. //Get Breakpoint Reason
  2153. bool OllyLang::DoGBPR(string args)
  2154. {
  2155.     string ops[1];
  2156.  
  2157.     if (break_reason == PP_MEMBREAK) {
  2158.         //Mem Breakpoint Reason
  2159.  
  2160.         break_memaddr=0;
  2161.         t_thread* t;
  2162.         t = Findthread(Getcputhreadid());
  2163.         CONTEXT context;
  2164.         context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
  2165.         GetThreadContext(t->thread, &context);
  2166.  
  2167.         ulong tHwBpAddr[4];
  2168.         tHwBpAddr[0]=context.Dr0;
  2169.         tHwBpAddr[1]=context.Dr1;
  2170.         tHwBpAddr[2]=context.Dr2;
  2171.         tHwBpAddr[3]=context.Dr3;
  2172.  
  2173.         BYTE buffer[MAXCMDSIZE];
  2174.         ulong size=Readmemory(buffer, t->reg.ip, MAXCMDSIZE, MM_SILENT);
  2175.         if (size>0) {
  2176.             t_disasm disasm;
  2177.             size=Disasm(buffer,size,t->reg.ip,NULL,&disasm,DISASM_DATA,NULL);
  2178.             if (size>0) {
  2179.                 char i,j;
  2180.                 for (j=0;j<3;j++)
  2181.                     if (disasm.opaddr[j]!=0 && disasm.opgood[j])
  2182.                     {
  2183.                         break_memaddr=disasm.opaddr[j];
  2184.                         for (i=0;i<4;i++)
  2185.                         if (tHwBpAddr[i]==disasm.opaddr[j])
  2186.                             break;
  2187.                     }
  2188.             }
  2189.         }
  2190.     }
  2191.  
  2192.     if(CreateOp(args, ops, 1))
  2193.     {
  2194.         if (is_variable(ops[0])) {
  2195.             variables[ops[0]] = break_reason;
  2196.             return true;
  2197.         }
  2198.     }
  2199.     variables["$RESULT"] = break_reason;
  2200.     return true;
  2201. }
  2202.  
  2203. //Get Code Information
  2204. bool OllyLang::DoGCI(string args)
  2205. {
  2206.     string ops[2], param;
  2207.     ulong addr,size;
  2208.  
  2209.     if(!CreateOp(args, ops, 2))
  2210.         return false;
  2211.  
  2212.     if ( GetDWOpValue(ops[0], addr)
  2213.        && GetSTROpValue("\""+ops[1]+"\"", param) )
  2214.     {
  2215.  
  2216.         transform(param.begin(), param.end(), param.begin(), toupper);
  2217.  
  2218.         BYTE buffer[MAXCMDSIZE];
  2219. //      size=Readmemory(buffer, addr, MAXCMDSIZE, MM_SILENT);
  2220.         size=Readcommand(addr,(char *) buffer);
  2221.  
  2222.         if (size>0)
  2223.         {
  2224.             t_disasm disasm;
  2225.             size=Disasm(buffer,size,addr,NULL,&disasm,DISASM_CODE,NULL);
  2226.  
  2227.             if (size<=0)
  2228.                 return false;
  2229.             else if (param == "COMMAND")
  2230.             {
  2231.                 string s;
  2232.                 s.assign(disasm.result);
  2233.                 while (s.find("  ") != string::npos) ReplaceString(s,"  "," ");
  2234.                 variables["$RESULT"] = s;
  2235.                 return true;
  2236.             }
  2237.             else if (param == "CONDITION")
  2238.             {
  2239.                 variables["$RESULT"] = disasm.condition;
  2240.                 return true;
  2241.             }
  2242.             else if (param == "DESTINATION")
  2243.             {
  2244.                 variables["$RESULT"] = disasm.jmpaddr;
  2245.                 return true;
  2246.             }
  2247.             else if (param == "SIZE")
  2248.             {
  2249.                 variables["$RESULT"] = size;
  2250.                 return true;
  2251.             }
  2252.             else if (param == "TYPE")
  2253.             {
  2254.                 variables["$RESULT"] = disasm.cmdtype;
  2255.                 return true;
  2256.             }
  2257.         }
  2258.     }
  2259.     return false;
  2260. }
  2261.  
  2262. bool OllyLang::DoGCMT(string args)
  2263. {
  2264.     string comment;
  2265.     ulong addr,size;
  2266.  
  2267.     if(GetDWOpValue(args, addr))
  2268.     {
  2269.         if(addr != 0)
  2270.         {
  2271.             size = Findname(addr, NM_COMMENT, buffer);
  2272.             if (size==0)
  2273.                 size = Findname(addr, NM_LIBCOMM, buffer);
  2274.             if (size==0)
  2275.                 size = Findname(addr, NM_ANALYSE, buffer);     
  2276.             comment = buffer;
  2277.             if(comment==""){
  2278.                variables["$RESULT"] = " ";
  2279.                return true;
  2280.             }  
  2281.             variables["$RESULT"] = comment;
  2282.             return true;
  2283.         }
  2284.     }
  2285.     return false;
  2286. }
  2287.  
  2288. bool OllyLang::DoGFO(string args)
  2289. {
  2290.     string comment;
  2291.     ulong addr;
  2292.  
  2293.     if(GetDWOpValue(args, addr))
  2294.     {
  2295.         if(addr != 0)
  2296.         {
  2297.             t_module* mod = (t_module*) Findmodule(addr);
  2298.             if (mod!=NULL) {
  2299.                 variables["$RESULT"] = Findfileoffset(mod, addr);
  2300.             } else {
  2301.                 variables["$RESULT"] = 0;
  2302.             }
  2303.             return true;
  2304.         }
  2305.     }
  2306.     return false;
  2307. }
  2308.  
  2309. // Get Label
  2310. // returns label from address
  2311. // glbl
  2312. bool OllyLang::DoGLBL ( string args )
  2313. {
  2314.     string comment;
  2315.     ulong addr, type;
  2316.     char buffer[TEXTLEN]={0};
  2317.  
  2318.     if (!GetDWOpValue(args, addr))
  2319.         return false;
  2320.     {
  2321.         variables["$RESULT"] = 0;
  2322.  
  2323.         if (addr != 0)
  2324.         {
  2325.             if (Findlabel ( addr, buffer ) != NM_LABEL)
  2326.                 variables["$RESULT"] = 0;
  2327.  
  2328.             comment.assign(buffer);
  2329.             if (comment == "")
  2330.                 variables["$RESULT"] = 0;
  2331.  
  2332.             variables["$RESULT"] = comment;
  2333.         }
  2334.     }
  2335.     return true;
  2336. }
  2337.  
  2338. bool OllyLang::DoGMA(string args)
  2339. {
  2340.     string str,ops[2];
  2341.  
  2342.     if(!CreateOperands(args, ops, 2))
  2343.         return false;
  2344.  
  2345.     if(GetSTROpValue(ops[0], str)) {
  2346.  
  2347.         if (str.length() > 8)
  2348.             str = str.substr(0,8);
  2349.  
  2350.         //Module names : Spaces to underscores
  2351.         while(str.find(" ")!=string::npos)
  2352.             str.replace(str.find(" "),1,"_");
  2353.  
  2354.         Listmemory();
  2355.  
  2356.         int t=Plugingetvalue(VAL_MEMORY);
  2357.         if (t<=0)
  2358.             return false;
  2359.  
  2360.         t_table* ttab=(t_table*) t;
  2361.         t_memory* mem;
  2362.         t_module* mod;
  2363.         string sMod;
  2364.  
  2365.         for (int n=0;n<ttab->data.n;n++) {
  2366.    
  2367.             mem = (t_memory*) Getsortedbyselection(&ttab->data,n);
  2368.             mod = (t_module*) Findmodule(mem->base);
  2369.             if (mod!=NULL) {
  2370.                 sMod.assign(mod->name,SHORTLEN);
  2371.                 if (stricmp(sMod.c_str(),str.c_str())==0) {
  2372.                     Int2Hex(mem->base, str);
  2373.                     nIgnoreNextValuesHist++;
  2374.                     return DoGMI(str+","+ops[1]);      
  2375.                 }
  2376.             }
  2377.         }
  2378.     }
  2379.  
  2380.     variables["$RESULT"] = 0;
  2381.  
  2382.     return true;
  2383.  
  2384. }
  2385.  
  2386. bool OllyLang::DoGMEMI(string args)
  2387. {
  2388.     string ops[2];
  2389.  
  2390.     if(!CreateOperands(args, ops, 2))
  2391.         return false;
  2392.  
  2393.     ulong addr = 0;
  2394.     string str;
  2395.  
  2396.     if(GetDWOpValue(ops[0], addr) && GetSTROpValue("\""+ops[1]+"\"", str) )
  2397.     {
  2398.         transform(str.begin(), str.end(), str.begin(), toupper);
  2399.  
  2400.         t_memory* mem = Findmemory(addr);
  2401.         if(str == "MEMORYBASE" && mem != NULL)
  2402.         {
  2403.             variables["$RESULT"] = (DWORD)mem->base;
  2404.             return true;
  2405.         }
  2406.         else if(str == "MEMORYSIZE" && mem != NULL)
  2407.         {
  2408.             variables["$RESULT"] = (DWORD)mem->size;
  2409.             return true;
  2410.         }
  2411.         else if(str == "MEMORYOWNER" && mem != NULL)
  2412.         {
  2413.             variables["$RESULT"] = (DWORD)mem->owner;
  2414.             return true;
  2415.         }
  2416.         else if(mem == NULL)
  2417.         {
  2418.             variables["$RESULT"] = 0;
  2419.             return true;
  2420.         }
  2421.         else
  2422.         {          
  2423.             //DoGMI(Int2Str(mem->base)+" ops[1]);
  2424.             variables["$RESULT"] = 0;
  2425.             errorstr = "Second operand bad";
  2426.             return false;
  2427.         }
  2428.     }
  2429.  
  2430.     errorstr = "Bad operand";
  2431.     return false;
  2432. }
  2433.  
  2434. bool OllyLang::DoNAMES(string args)
  2435. {
  2436.     string ops[1];
  2437.     ulong addr;
  2438.     if(!CreateOperands(args, ops, 1))
  2439.         return false;
  2440.  
  2441.     if(GetDWOpValue(ops[0], addr)) {
  2442.         t_module* mod = Findmodule(addr);
  2443.         Setdisasm(mod->codebase,1,0);
  2444.         Sendshortcut(PM_DISASM, 0, WM_KEYDOWN, 1, 0, 'N');  //"Ctrl + N"
  2445.         require_ollyloop = 1;
  2446.         return true;
  2447.     }
  2448.  
  2449.     return false;
  2450. }
  2451.  
  2452. bool OllyLang::DoGMEXP(string args)
  2453. {
  2454.     string ops[3];
  2455.  
  2456.     if(!CreateOperands(args, ops, 3)) {
  2457.         ops[2]="0";
  2458.         if(!CreateOperands(args, ops, 2))
  2459.             return false;
  2460.     }
  2461.  
  2462.     ulong i, num, addr, count=0;
  2463.     string str;
  2464.     bool cache=false, cached=false;
  2465.  
  2466.     if(GetDWOpValue(ops[0], addr) && GetSTROpValue("\""+ops[1]+"\"", str) && GetDWOpValue(ops[2], num) )
  2467.     {
  2468.         transform(str.begin(), str.end(), str.begin(), toupper);
  2469.  
  2470.         t_module * mod = Findmodule(addr);
  2471.         if (!mod) {
  2472.             variables["$RESULT"] = 0;
  2473.             return true;
  2474.         }
  2475.  
  2476.         t_export exp={0};
  2477.  
  2478.         if (str == "COUNT") {
  2479.             cache = true;
  2480.             tExportsCache.clear();
  2481.             exportsCacheAddr = addr;
  2482.         } else {
  2483.             if (exportsCacheAddr == addr && num < tExportsCache.size()) {
  2484.                 exp = tExportsCache[num];
  2485.                 count = tExportsCache.size();
  2486.                 cached = true;
  2487.             }
  2488.         }
  2489.  
  2490.         if (!cached)
  2491.         for(i = 0; i < mod->codesize ; i++) {
  2492.             if (Findname(mod->codebase + i, NM_EXPORT, exp.label))
  2493.             {
  2494.                 count++;
  2495.                 exp.addr=mod->codebase + i;
  2496.                 if (count==num && !cache) break;
  2497.                 if (cache) {
  2498.                     tExportsCache.push_back(exp);
  2499.                 }
  2500.             }
  2501.         }
  2502.  
  2503.         if (num > count) //no more
  2504.         {
  2505.             variables["$RESULT"] = 0;
  2506.             return true;
  2507.         }
  2508.  
  2509.         if(str == "COUNT")
  2510.         {
  2511.             variables["$RESULT"] = count;
  2512.             return true;
  2513.         }
  2514.         else if(str == "ADDRESS")
  2515.         {
  2516.             variables["$RESULT"] = exp.addr;
  2517.             return true;
  2518.         }
  2519.         else if(str == "LABEL")
  2520.         {
  2521.             variables["$RESULT"] = exp.label;
  2522.             return true;
  2523.         }
  2524.         else
  2525.         {
  2526.             variables["$RESULT"] = 0;
  2527.             errorstr = "Second operand bad";
  2528.             return false;
  2529.         }
  2530.     }
  2531.  
  2532.     errorstr = "Bad operand";
  2533.     return false;
  2534. }
  2535.  
  2536. bool OllyLang::DoGMI(string args)
  2537. {
  2538.     string ops[2];
  2539.  
  2540.     if(!CreateOperands(args, ops, 2))
  2541.         return false;
  2542.  
  2543.     ulong addr = 0;
  2544.     string str;
  2545.  
  2546.     if(GetDWOpValue(ops[0], addr) && GetSTROpValue("\""+ops[1]+"\"", str) )
  2547.     {
  2548.         t_module* mod = Findmodule(addr);
  2549.  
  2550.         transform(str.begin(), str.end(), str.begin(), toupper);
  2551.        
  2552.         if(mod == NULL)
  2553.         {
  2554.             variables["$RESULT"] = 0;
  2555.             return true;
  2556.         }
  2557.         else if(str == "MODULEBASE")
  2558.         {
  2559.             variables["$RESULT"] = (DWORD)mod->base;
  2560.             return true;
  2561.         }
  2562.         else if(str == "MODULESIZE")
  2563.         {
  2564.             variables["$RESULT"] = (DWORD)mod->size;
  2565.             return true;
  2566.         }
  2567.         else if(str == "CODEBASE")
  2568.         {
  2569.             variables["$RESULT"] = (DWORD)mod->codebase;
  2570.             return true;
  2571.         }
  2572.         else if(str == "CODESIZE")
  2573.         {
  2574.             variables["$RESULT"] = (DWORD)mod->origcodesize;
  2575.             return true;
  2576.         }
  2577.         else if(str == "ENTRY")
  2578.         {
  2579.             variables["$RESULT"] = (DWORD)mod->entry;
  2580.             return true;
  2581.         }
  2582.         else if(str == "NSECT")
  2583.         {
  2584.             variables["$RESULT"] = (DWORD)mod->nsect;
  2585.             return true;
  2586.         }
  2587.         else if(str == "DATABASE")
  2588.         {
  2589.             variables["$RESULT"] = (DWORD)mod->database;
  2590.             return true;
  2591.         }
  2592.         else if(str == "EDATATABLE")
  2593.         {
  2594.             variables["$RESULT"] = (DWORD)mod->edatatable;
  2595.             return true;
  2596.         }
  2597.         else if(str == "EDATASIZE")
  2598.         {
  2599.             variables["$RESULT"] = (DWORD)mod->edatasize;
  2600.             return true;
  2601.         }
  2602.         else if(str == "IDATABASE")
  2603.         {
  2604.             variables["$RESULT"] = (DWORD)mod->idatabase;
  2605.             return true;
  2606.         }
  2607.         else if(str == "IDATATABLE")
  2608.         {
  2609.             variables["$RESULT"] = (DWORD)mod->idatatable;
  2610.             return true;
  2611.         }
  2612.         else if(str == "RESBASE")
  2613.         {
  2614.             variables["$RESULT"] = (DWORD)mod->resbase;
  2615.             return true;
  2616.         }
  2617.         else if(str == "RESSIZE")
  2618.         {
  2619.             variables["$RESULT"] = (DWORD)mod->ressize;
  2620.             return true;
  2621.         }
  2622.         else if(str == "RELOCTABLE")
  2623.         {
  2624.             variables["$RESULT"] = (DWORD)mod->reloctable;
  2625.             return true;
  2626.         }
  2627.         else if(str == "RELOCSIZE")
  2628.         {
  2629.             variables["$RESULT"] = (DWORD)mod->relocsize;
  2630.             return true;
  2631.         }
  2632.         else if(str == "NAME")
  2633.         {
  2634.             variables["$RESULT"] = (char*)mod->name;
  2635.             if (variables["$RESULT"].str.length() > SHORTLEN)
  2636.                 variables["$RESULT"].str = variables["$RESULT"].str.substr(0,SHORTLEN);
  2637.             return true;
  2638.         }
  2639.         else if(str == "PATH")
  2640.         {
  2641.             variables["$RESULT"] = (char[MAX_PATH])mod->path;
  2642.             return true;
  2643.         }
  2644.         else if(str == "VERSION")
  2645.         {
  2646.             variables["$RESULT"] = (char*)mod->version;
  2647.             return true;
  2648.         }
  2649.         else
  2650.         {
  2651.             variables["$RESULT"] = 0;
  2652.             errorstr = "Second operand bad";
  2653.             return false;
  2654.         }
  2655.     }
  2656.     errorstr = "Bad operand";
  2657.     return false;
  2658. }
  2659.  
  2660. bool OllyLang::DoGMIMP(string args)
  2661. {
  2662.     string ops[3];
  2663.  
  2664.     if(!CreateOperands(args, ops, 3)) {
  2665.         ops[2]="0";
  2666.         if(!CreateOperands(args, ops, 2))
  2667.             return false;
  2668.     }
  2669.  
  2670.     ulong i, num, addr, count=0;
  2671.     string str;
  2672.     bool cache=false, cached=false;
  2673.  
  2674.     if(GetDWOpValue(ops[0], addr) && GetSTROpValue("\""+ops[1]+"\"", str) && GetDWOpValue(ops[2], num) )
  2675.     {
  2676.         transform(str.begin(), str.end(), str.begin(), toupper);
  2677.  
  2678.         t_module * mod = Findmodule(addr);
  2679.         if (!mod) {
  2680.             variables["$RESULT"] = 0;
  2681.             return true;
  2682.         }
  2683.  
  2684.         t_export exp={0};
  2685.  
  2686.         if (str == "COUNT") {
  2687.             cache = true;
  2688.             tImportsCache.clear();
  2689.             importsCacheAddr = addr;
  2690.         } else {
  2691.             if (importsCacheAddr == addr && num < tImportsCache.size()) {
  2692.                 exp = tImportsCache[num];
  2693.                 count = tImportsCache.size();
  2694.                 cached = true;
  2695.             }
  2696.         }
  2697.  
  2698.         if (!cached)
  2699.         for(i = 0; i < mod->codesize ; i++) {
  2700.             if (Findname(mod->codebase + i, NM_IMPORT, exp.label))
  2701.             {
  2702.                 count++;
  2703.                 exp.addr=mod->codebase + i;
  2704.                 if (count==num && !cache) break;
  2705.                 if (cache) {
  2706.                     tImportsCache.push_back(exp);
  2707.                 }
  2708.             }
  2709.         }
  2710.  
  2711.         if (num > count) //no more
  2712.         {
  2713.             variables["$RESULT"] = 0;
  2714.             return true;
  2715.         }
  2716.  
  2717.         if(str == "COUNT")
  2718.         {
  2719.             variables["$RESULT"] = count;
  2720.             return true;
  2721.         }
  2722.         else if(str == "ADDRESS")
  2723.         {
  2724.             variables["$RESULT"] = exp.addr;
  2725.             return true;
  2726.         }
  2727.         else if(str == "LABEL")
  2728.         {
  2729.             variables["$RESULT"] = exp.label;
  2730.             return true;
  2731.         }
  2732.         else if(str == "NAME")
  2733.         {
  2734.             string s = exp.label;
  2735.             if (s.find(".") != string::npos) {
  2736.                 variables["$RESULT"] = s.substr(s.find(".")+1);
  2737.             }
  2738.             else
  2739.                 variables["$RESULT"] = exp.label;
  2740.  
  2741.             return true;
  2742.         }
  2743.         else if(str == "MODULE")
  2744.         {
  2745.             string s = exp.label;
  2746.             if (s.find(".") != string::npos) {
  2747.                 variables["$RESULT"] = s.substr(0,s.find("."));
  2748.             }
  2749.             else
  2750.                 variables["$RESULT"] = "";
  2751.             return true;
  2752.         }
  2753.         else
  2754.         {
  2755.             variables["$RESULT"] = 0;
  2756.             errorstr = "Second operand bad";
  2757.             return false;
  2758.         }
  2759.     }
  2760.  
  2761.     errorstr = "Bad operand";
  2762.     return false;
  2763. }
  2764.  
  2765. bool OllyLang::DoGN(string args)
  2766. {
  2767.     string ops[1];
  2768.     ulong addr;
  2769.  
  2770.     if(!CreateOperands(args, ops, 1))
  2771.         return false;
  2772.  
  2773.     if(GetDWOpValue(ops[0], addr))
  2774.     {
  2775.         char sym[4096] = {0};
  2776.         char buf[TEXTLEN] = {0};
  2777. //      int res = Decodeaddress(addr, 0, ADC_STRING, sym, 4096, buf);
  2778.         int res = Decodeaddress(addr, 0, ADC_JUMP | ADC_STRING | ADC_ENTRY | ADC_OFFSET | ADC_SYMBOL, sym, 4096, buf);
  2779.         if(res)
  2780.         {
  2781.             variables["$RESULT"] = sym;
  2782.             char* tmp = strstr(sym, ".");
  2783.             if(tmp)
  2784.             {
  2785.                 strtok(sym, ">");                      
  2786.                 *tmp = '\0';
  2787.                 variables["$RESULT_1"] = sym; //+ 2 ... not a bug Charset pb ?
  2788.                 variables["$RESULT_2"] = tmp + 1;
  2789.             }
  2790.         }
  2791.         else
  2792.         {
  2793.             variables["$RESULT"] = 0;
  2794.             variables["$RESULT_1"] = 0;
  2795.             variables["$RESULT_2"] = 0;
  2796.         }
  2797.  
  2798.         return true;
  2799.     }
  2800.  
  2801.     return false;
  2802. }
  2803.  
  2804. bool OllyLang::DoGO(string args)
  2805. {
  2806.     string ops[1];
  2807.     ulong addr;
  2808.  
  2809.     if(!CreateOperands(args, ops, 1))
  2810.         return false;
  2811.  
  2812.     if(GetDWOpValue(ops[0], addr))
  2813.     {
  2814.         Go(Getcputhreadid(), addr, STEP_RUN, 1, 1);
  2815.         require_ollyloop = 1;
  2816.       return true;
  2817.     }
  2818.  
  2819.     return false;
  2820. }
  2821.  
  2822. //Get Code Information
  2823. bool OllyLang::DoGOPI(string args)
  2824. {
  2825.     string ops[3], param;
  2826.     ulong addr,size,index;
  2827.  
  2828.     if(!CreateOp(args, ops, 3))
  2829.         return false;
  2830.  
  2831.     if ( GetDWOpValue(ops[0], addr)
  2832.        && GetDWOpValue(ops[1], index)
  2833.        && GetSTROpValue("\""+ops[2]+"\"", param) )
  2834.     {
  2835.  
  2836.         index--;
  2837.         if (index < 0 || index > 2) {
  2838.             errorstr = "Bad operand index (1-3) !";
  2839.             return false;
  2840.         }
  2841.  
  2842.         transform(param.begin(), param.end(), param.begin(), toupper);
  2843.  
  2844.         BYTE buffer[MAXCMDSIZE]={0};
  2845. //      size=Readmemory(buffer, addr, MAXCMDSIZE, MM_SILENT);
  2846.         size=Readcommand(addr,(char *) buffer);
  2847.  
  2848.         if (size>0)
  2849.         {
  2850.             t_disasm disasm;
  2851.             size=Disasm(buffer,size,addr,NULL,&disasm,DISASM_ALL,Getcputhreadid());
  2852.  
  2853.             if (size<=0)
  2854.                 return false;
  2855.             else if (param == "TYPE")
  2856.             {
  2857.                 variables["$RESULT"] = disasm.optype[index]; // Type of operand (extended set DEC_xxx)
  2858.                 return true;
  2859.             }
  2860.             else if (param == "SIZE")
  2861.             {
  2862.                 variables["$RESULT"] = disasm.opsize[index]; // Size of operand, bytes
  2863.                 return true;
  2864.             }
  2865.             else if (param == "GOOD")
  2866.             {
  2867.                 variables["$RESULT"] = disasm.opgood[index]; // Whether address and data valid
  2868.                 return true;
  2869.             }
  2870.             else if (param == "ADDR")
  2871.             {
  2872.                 variables["$RESULT"] = disasm.opaddr[index]; // Address if memory, index if register
  2873.                 return true;
  2874.             }
  2875.             else if (param == "DATA")
  2876.             {
  2877.                 variables["$RESULT"] = disasm.opdata[index]; // Actual value (only integer operands)
  2878.                 return true;
  2879.             }
  2880.         }
  2881.     }
  2882.     return false;
  2883. }
  2884.  
  2885.  
  2886. //gpa "LoadLibraryA","kernel32.dll"
  2887. bool OllyLang::DoGPA(string args)
  2888. {
  2889.     string ops[3];
  2890.     ulong dontfree=0;
  2891.  
  2892.     if(CreateOperands(args, ops, 3))
  2893.         GetDWOpValue(ops[2], dontfree);
  2894.     else
  2895.         if(!CreateOperands(args, ops, 2))
  2896.             return false;
  2897.  
  2898.     variables["$RESULT"] = 0;
  2899.     DropVariable("$RESULT_1");
  2900.     DropVariable("$RESULT_2");
  2901.  
  2902.     string proc, lib;
  2903.     FARPROC p;
  2904.  
  2905.     if(GetSTROpValue(ops[0], proc) && GetSTROpValue(ops[1], lib))
  2906.     {
  2907.         HMODULE hMod=LoadLibraryEx(lib.c_str(),NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
  2908.  
  2909.         if(hMod==0) {
  2910.             variables["$RESULT"] = 0;
  2911.             errorstr = "GPA: "+StrLastError();
  2912.  
  2913.             if (dontfree) {
  2914.  
  2915.                 nIgnoreNextValuesHist++;
  2916.                 DoGPA("\"LoadLibraryA\",\"kernel32.dll\"");
  2917.  
  2918.                 HANDLE hDebugee = (HANDLE)Plugingetvalue(VAL_HPROCESS);
  2919.  
  2920.                 ulong pmem = AddProcessMemoryBloc(lib);
  2921.  
  2922.                 string tmp;
  2923.                 sprintf(buffer,"%09X",pmem);
  2924.                 tmp.assign(buffer);
  2925.  
  2926.                 if (DoPUSH(tmp)) {
  2927.  
  2928.                     sprintf(buffer,"%09X",variables["$RESULT"].dw);
  2929.  
  2930.                     ExecuteASM("call "+tmp);
  2931.                
  2932.                 }
  2933.  
  2934.                 regBlockToFree((void*)pmem,0x1000,true);
  2935.                 //VirtualFreeEx(hDebugee, pmem, 0x1000, MEM_DECOMMIT);
  2936.  
  2937.             }
  2938.             return true;
  2939.         }
  2940.  
  2941.         variables["$RESULT_1"] = lib;
  2942.  
  2943.         //if (proc != "")
  2944.         p = GetProcAddress(hMod, proc.c_str());
  2945.         //else
  2946.         //: GetModu
  2947.  
  2948.         if (!dontfree) {
  2949.             FreeLibrary(hMod);
  2950.             require_ollyloop=1;
  2951.         }
  2952.  
  2953.         if(p == 0) {
  2954.             errorstr = "GPA: No such procedure: " + proc;
  2955.             return true;
  2956.         }
  2957.  
  2958.         variables["$RESULT"] = (DWORD) p;
  2959.         variables["$RESULT_2"] = proc;
  2960.         return true;
  2961.     }
  2962.     return false;
  2963. }
  2964.  
  2965. bool OllyLang::DoGPI(string args)
  2966. {
  2967.     string ops[1];
  2968.  
  2969.     if(!CreateOperands(args, ops, 1))
  2970.         return false;
  2971.  
  2972.     string str;
  2973.  
  2974.     if(GetSTROpValue("\""+ops[0]+"\"", str))
  2975.     {
  2976.  
  2977.         if(str == "HPROCESS") //(HANDLE) Handle of debugged process
  2978.         {
  2979.             variables["$RESULT"] = (DWORD)Plugingetvalue(VAL_HPROCESS);
  2980.             return true;
  2981.         }
  2982.         else if(str == "PROCESSID") //Process ID of debugged process
  2983.         {
  2984.             variables["$RESULT"] = (DWORD)Plugingetvalue(VAL_PROCESSID);
  2985.             return true;
  2986.         }
  2987.         else if(str == "HMAINTHREAD") //(HANDLE) Handle of main thread of debugged process
  2988.         {
  2989.             variables["$RESULT"] = (DWORD)Plugingetvalue(VAL_HMAINTHREAD);
  2990.             return true;
  2991.         }
  2992.         else if(str == "MAINTHREADID") //Thread ID of main thread of debugged process
  2993.         {
  2994.             variables["$RESULT"] = (DWORD)Plugingetvalue(VAL_MAINTHREADID);
  2995.             return true;
  2996.         }
  2997.         else if(str == "MAINBASE") //Base of main module in the debugged process
  2998.         {
  2999.             variables["$RESULT"] = (DWORD)Plugingetvalue(VAL_MAINBASE);
  3000.             return true;
  3001.         }
  3002.         else if(str == "PROCESSNAME") //Name of the debugged process
  3003.         {
  3004.             variables["$RESULT"] = (char *)Plugingetvalue(VAL_PROCESSNAME);
  3005.             return true;
  3006.         }
  3007.         else if(str == "EXEFILENAME") //Name of the main debugged file
  3008.         {
  3009.             variables["$RESULT"] = (char *)Plugingetvalue(VAL_EXEFILENAME);
  3010.             return true;
  3011.         }
  3012.         else if(str == "CURRENTDIR") //Current directory for debugged process
  3013.         {
  3014.             variables["$RESULT"] = (char *)Plugingetvalue(VAL_CURRENTDIR);
  3015.             if (variables["$RESULT"].str.length()==0) {
  3016.                 char spath[MAX_PATH];
  3017.                 strcpy(spath, (char*)Plugingetvalue(VAL_EXEFILENAME));
  3018.                 string path = spath;
  3019.                 variables["$RESULT"] = path.substr(0, path.rfind('\\') + 1);               
  3020.             }
  3021.             return true;
  3022.         }
  3023.         else if(str == "SYSTEMDIR") //Windows system directory
  3024.         {
  3025.             variables["$RESULT"] = (char *)Plugingetvalue(VAL_SYSTEMDIR);
  3026.             return true;
  3027.         }
  3028.     }
  3029.     errorstr = "Bad operand";
  3030.     return false;
  3031. }
  3032.  
  3033. //in dev... i try to find API parameters count and types
  3034. bool OllyLang::DoGPP(string args)
  3035. {
  3036.     if (!DoGPA(args))
  3037.         return false;
  3038.  
  3039.     ulong addr = variables["$RESULT"].dw;
  3040.     if (addr==0)
  3041.         return false;
  3042.  
  3043.     string sAddr = itoa(addr,buffer,16);
  3044.     if (!DoREF(sAddr))
  3045.         return false;
  3046.  
  3047.     int t=Plugingetvalue(VAL_REFERENCES);
  3048.     if (t<=0)
  3049.         return false;
  3050.  
  3051.     int size;
  3052.     t_table* tref=(t_table*) t;
  3053.     for (int n=0;n<tref->data.n;n++) {
  3054.    
  3055.         t_ref* ref= (t_ref*) Getsortedbyselection(&tref->data,n);
  3056.            
  3057.         if (ref->addr == addr)
  3058.             continue;
  3059.  
  3060.         //Disasm origin to get comments
  3061.         BYTE buffer[MAXCMDSIZE];
  3062.         size=Readmemory(buffer, ref->addr, MAXCMDSIZE, MM_SILENT);                 
  3063.         if (size>0) {
  3064.  
  3065.             t_disasm disasm;
  3066.             t_module* mod = Findmodule(ref->addr);
  3067.             Analysecode(mod);
  3068.  
  3069.             size=Disasm(buffer,size,ref->addr,NULL,&disasm,DISASM_ALL,NULL);
  3070.             DbgMsg(disasm.nregstack,disasm.comment);
  3071.  
  3072.             if (size>0) {
  3073.                 variables["$RESULT"] = ref->addr;
  3074.                 variables["$RESULT_1"] = disasm.result; //command text
  3075.                 variables["$RESULT_2"] = disasm.comment;
  3076.                 return true;
  3077.             }
  3078.         }
  3079.     }
  3080.     return true;
  3081. }
  3082.  
  3083. //Get Reference Window Address at Offset
  3084. bool OllyLang::DoGREF(string args)
  3085. {
  3086.     string ops[1];
  3087.     ulong line;
  3088.  
  3089.     CreateOperands(args, ops, 1);
  3090.  
  3091.     t_table* tt;
  3092.     tt=(t_table*) Plugingetvalue(VAL_REFERENCES);
  3093.  
  3094.     variables["$RESULT"] = 0;
  3095.  
  3096.     //Get Ref. Addr
  3097.     if (tt!=NULL)
  3098.     {
  3099.         if(ops[0]!="" && GetDWOpValue(ops[0], line))
  3100.         {
  3101.             if (line < tt->data.n)
  3102.             {
  3103.                 t_ref* tr;
  3104.                 tr=(t_ref*) Getsortedbyselection(&tt->data, line); //0 is CPU initial sel.
  3105.                 if (tr!=NULL)
  3106.                     variables["$RESULT"]=tr->addr;
  3107.             }
  3108.         } else {
  3109.             //Get Ref. Count
  3110.             variables["$RESULT"] = tt->data.n-1;
  3111.         }
  3112.         return true;
  3113.     }
  3114.     return false;
  3115. }
  3116.  
  3117. //Get Relative Offset
  3118. bool OllyLang::DoGRO(string args)
  3119. {
  3120.     string ops[1];
  3121.     ulong addr;
  3122.  
  3123.     if(!CreateOperands(args, ops, 1))
  3124.         return false;
  3125.  
  3126.     if(GetDWOpValue(ops[0], addr))
  3127.     {
  3128.         char sym[4096] = {0};
  3129.  
  3130.         int size = Decoderelativeoffset(addr, ADC_NONTRIVIAL, sym, 4096);
  3131.         if (size > 0)
  3132.         {
  3133.             variables["$RESULT"] = sym;
  3134.         }
  3135.         else
  3136.         {
  3137.             variables["$RESULT"] = 0;
  3138.         }
  3139.  
  3140.         return true;
  3141.     }
  3142.  
  3143.     return false;
  3144. }
  3145.  
  3146. // Get Selection Limits
  3147. // returns START/END address from currently selected line in CPUASM | DUMP | STACK window in $RESULT & $RESULT_1
  3148. // arg can be either : CPUDASM, CPUDUMP, CPUSTACK
  3149. // ex       :   gsl CPUDUMP
  3150. bool OllyLang::DoGSL ( string args )
  3151. {
  3152.     string str;
  3153.     t_dump* td;
  3154.  
  3155.     variables["$RESULT"] = 0;
  3156.     variables["$RESULT_1"] = 0;
  3157.     variables["$RESULT_2"] = 0;
  3158.    
  3159.     GetSTROpValue("\""+args+"\"", str);
  3160.     if (str == "") str = "CPUDASM";
  3161.  
  3162.     transform(str.begin(), str.end(), str.begin(), toupper);
  3163.  
  3164.     if(str == "CPUDASM")
  3165.         td = (t_dump*) Plugingetvalue (VAL_CPUDASM);
  3166.     else if (str == "CPUDUMP")
  3167.         td = (t_dump*) Plugingetvalue(VAL_CPUDDUMP);
  3168.     else if (str == "CPUSTACK")
  3169.         td = (t_dump*) Plugingetvalue(VAL_CPUDSTACK);
  3170.     else
  3171.         return false;
  3172.    
  3173.     if (td)
  3174.     {
  3175.         variables["$RESULT"] = (DWORD)td->sel0;
  3176.         variables["$RESULT_1"] =  (DWORD)td->sel1-1;
  3177.         variables["$RESULT_2"] =  (DWORD)td->sel1-(DWORD)td->sel0;
  3178.     }
  3179.     else
  3180.     {
  3181.         variables["$RESULT"] = 0;
  3182.         variables["$RESULT_1"] = 0;
  3183.         variables["$RESULT_2"] = 0;
  3184.     }
  3185.  
  3186.     return true;
  3187. }
  3188.  
  3189. // Get String
  3190. // returns a null terminated string from addr, the string is at least arg1 charachters
  3191. // gstr addr, [arg1]
  3192. // returns in   :
  3193. // - $RESULT    : the string
  3194. // - $RESULT_1  : len of string
  3195. //
  3196. // ex       : gstr 401000     ; arg1 in this case is set to default (5 chars)
  3197. //          : gstr 401000, 20 ; must be at least 20 chars
  3198. bool OllyLang::DoGSTR(string args)
  3199. {
  3200.     string str;
  3201.     char buf[TEXTLEN]={0};
  3202.     ulong addr, size, tmpSize;
  3203.     char c;
  3204.     bool bUseDef = false;
  3205.     uint iDashNum;
  3206.  
  3207.     string ops[2];
  3208.  
  3209.  
  3210.     if (!CreateOperands(args, ops, 2))
  3211.     {
  3212.         if (!CreateOperands(args, ops, 1))
  3213.             return false;
  3214.         else
  3215.             bUseDef = true;
  3216.     }
  3217.  
  3218.     if (GetDWOpValue (ops[0], addr))
  3219.     {
  3220.         variables["$RESULT"] = 0;
  3221.         variables["$RESULT_1"] = 0;
  3222.         if (addr != 0)
  3223.         {
  3224.             buffer[0] = '\0';
  3225.             tmpSize = Decodeascii(addr, buf, TEXTLEN, DASC_ASCII);
  3226.             if (tmpSize > 2 && buf[0]=='"') {
  3227.                 tmpSize -= 2;
  3228.                 lstrcpyn(buffer, (char *)(buf+1), tmpSize+1);
  3229.             }
  3230.             else {
  3231.                 return true;
  3232.             }
  3233.  
  3234.             //tmpSize = Readmemory(buf, addr, TEXTLEN, MM_RESILENT);
  3235.             //if (!tmpSize)
  3236.             //  return true;
  3237.  
  3238.             //lstrcpy(buffer, buf);
  3239.             tmpSize = lstrlen(buffer);
  3240.  
  3241.             if (!bUseDef)
  3242.                 GetDWOpValue(ops[1], size);
  3243.             else
  3244.                 size = 2;
  3245.  
  3246.             if (tmpSize < size)
  3247.                 return true;
  3248. /*
  3249.             int i;
  3250.             for (iDashNum=0,i=0; i < tmpSize; i++)
  3251.             {
  3252.                 c = buffer[i];
  3253.                 if (isalnum(c))
  3254.                     continue;
  3255.                 else
  3256.                 {
  3257.                     buffer[i] = '_';
  3258.                     iDashNum++;
  3259.                 }
  3260.             }
  3261.  
  3262.             if (iDashNum >= tmpSize / 2)
  3263.                 return true;
  3264. */
  3265.             str.assign(buffer);
  3266.             variables["$RESULT"] = str;
  3267.             variables["$RESULT_1"] = tmpSize;          
  3268.         }
  3269.         return true;
  3270.     }
  3271.     return false;
  3272. }
  3273.  
  3274. // Get String
  3275. // returns a null terminated string from addr, the string is at least arg1 charachters
  3276. // gstr addr, [arg1]
  3277. // returns in   :
  3278. // - $RESULT    : the string
  3279. // - $RESULT_1  : len of string
  3280. //
  3281. // ex       : gstr 401000     ; arg1 in this case is set to default (5 chars)
  3282. //          : gstr 401000, 20 ; must be at least 20 chars
  3283. bool OllyLang::DoGSTRW(string args)
  3284. {
  3285.     string str,wstr;
  3286.     char buf[TEXTLEN*2]={0};
  3287.     ulong addr, size, tmpSize;
  3288.     char c;
  3289.     bool bUseDef = false;
  3290.     uint iDashNum;
  3291.  
  3292.     string ops[2];
  3293.  
  3294.  
  3295.     if (!CreateOperands(args, ops, 2))
  3296.     {
  3297.         if (!CreateOperands(args, ops, 1))
  3298.             return false;
  3299.         else
  3300.             bUseDef = true;
  3301.     }
  3302.  
  3303.     if (GetDWOpValue (ops[0], addr))
  3304.     {
  3305.         variables["$RESULT"] = 0;
  3306.         variables["$RESULT_1"] = 0;
  3307.         if (addr != 0)
  3308.         {
  3309.             buffer[0] = '\0';
  3310.             tmpSize = Decodeunicode(addr, buf, TEXTLEN*2);
  3311.             if (tmpSize > 2 && buf[0]=='"') {
  3312.                 tmpSize -= 2;
  3313.                 lstrcpyn(buffer, (char *)(buf+1), tmpSize+1);              
  3314.             }
  3315.             else {
  3316.                 return true;
  3317.             }
  3318.  
  3319.             str.assign(buffer);
  3320.             tmpSize = str.length();//lstrlen(buffer);
  3321.  
  3322.             if (tmpSize < Readmemory(buf, addr, TEXTLEN*2, MM_RESILENT)) {
  3323.                 wcsncpy((wchar_t *)buffer, (wchar_t *) buf, tmpSize);
  3324.                 wstr.assign(buffer,tmpSize*2);
  3325.             }
  3326.  
  3327.             if (!bUseDef)
  3328.                 GetDWOpValue(ops[1], size);
  3329.             else
  3330.                 size = 2;
  3331.  
  3332.             if (tmpSize < size)
  3333.                 return true;
  3334. /*
  3335.             int i;
  3336.             for (iDashNum=0,i=0; i < tmpSize; i++)
  3337.             {
  3338.                 c = buffer[i];
  3339.                 if (isalnum(c))
  3340.                     continue;
  3341.                 else
  3342.                 {
  3343.                     buffer[i] = '_';
  3344.                     iDashNum++;
  3345.                 }
  3346.             }
  3347.  
  3348.             if (iDashNum >= tmpSize / 2)
  3349.                 return true;
  3350. */
  3351.            
  3352.             variables["$RESULT"] = str;
  3353.             variables["$RESULT_1"] = tmpSize;
  3354.             variables["$RESULT_2"] = wstr;
  3355.         }
  3356.         return true;
  3357.     }
  3358.     return false;
  3359. }
  3360.  
  3361. bool OllyLang::DoHANDLE(string args)
  3362. {
  3363.     string ops[3];
  3364.     bool useCaption=true;
  3365.  
  3366.     if(!CreateOperands(args, ops, 3)) {
  3367.             return false;
  3368.     }
  3369.  
  3370.     string sClassName;
  3371.     ulong x,y;
  3372.     ulong thid = Plugingetvalue(VAL_MAINTHREADID);
  3373.  
  3374.     if(GetDWOpValue(ops[0], x)
  3375.         && GetDWOpValue(ops[1], y)
  3376.         && GetSTROpValue(ops[2], sClassName))
  3377.     {
  3378.         variables["$RESULT"] = (DWORD) FindHandle(thid, sClassName, x, y);
  3379.         return true;
  3380.     }
  3381.  
  3382.     return false;
  3383.  
  3384. }
  3385.  
  3386. bool OllyLang::DoINC(string args)
  3387. {
  3388.     string ops[1];
  3389.  
  3390.     if(!CreateOperands(args, ops, 1))
  3391.         return false;
  3392.  
  3393.     return DoADD(ops[0] + ", 1");
  3394. }
  3395.  
  3396. bool OllyLang::DoHISTORY(string args)
  3397. {
  3398.     string ops[1];
  3399.     ulong dw;
  3400.  
  3401.     if(!CreateOperands(args, ops, 1))
  3402.         return false;
  3403.  
  3404.     if(GetDWOpValue(ops[0], dw)) {
  3405.         showVarHistory=(dw!=0);
  3406.         return true;
  3407.     }
  3408.  
  3409.     return false;
  3410. }
  3411.  
  3412. bool OllyLang::DoITOA(string args)
  3413. {
  3414.     string ops[2];
  3415.  
  3416.     if(!CreateOperands(args, ops, 2)) {
  3417.         ops[1] = "16.";
  3418.         if(!CreateOperands(args, ops, 1))
  3419.             return false;
  3420.     }
  3421.  
  3422.     ulong base, dw;
  3423.  
  3424.     if(GetDWOpValue(ops[0], dw)
  3425.         && GetDWOpValue(ops[1], base) )
  3426.     {
  3427.         char buffer [20]={0};
  3428.         itoa(dw,buffer,base);
  3429.         variables["$RESULT"] = buffer;
  3430.         return true;
  3431.     }
  3432.     return false;
  3433. }
  3434.  
  3435. bool OllyLang::DoJA(string args)
  3436. {
  3437.     if(zf == 0 && cf == 0)
  3438.         return DoJMP(args);
  3439.     return true;
  3440. }
  3441.  
  3442. bool OllyLang::DoJAE(string args)
  3443. {
  3444.     if(cf == 0)
  3445.         return DoJMP(args);
  3446.     return true;
  3447. }
  3448.  
  3449. bool OllyLang::DoJB(string args)
  3450. {
  3451.     if(cf == 1)
  3452.         return DoJMP(args);
  3453.     return true;
  3454. }
  3455.  
  3456. bool OllyLang::DoJBE(string args)
  3457. {
  3458.     if(zf == 1 || cf == 1)
  3459.         return DoJMP(args);
  3460.     return true;
  3461. }
  3462.  
  3463. bool OllyLang::DoJE(string args)
  3464. {
  3465.     if(zf == 1)
  3466.         return DoJMP(args);
  3467.     return true;
  3468. }
  3469.  
  3470. bool OllyLang::DoJMP(string args)
  3471. {
  3472.     string ops[1];
  3473.  
  3474.     if(!CreateOperands(args, ops, 1))
  3475.         return false;
  3476.  
  3477.     if(labels.find(ops[0]) == labels.end())
  3478.         return false;
  3479.  
  3480.     //Store jump destination to display it
  3481.     t_wndprog_data *pline;
  3482.     pline = (t_wndprog_data *) Getsortedbyselection(&wndProg.data,pgr_scriptpos);
  3483.     if (pline != NULL) {
  3484.         pline->jumpto = labels[ops[0]];
  3485.     }
  3486.  
  3487.     script_pos = labels[ops[0]];
  3488.     return true;
  3489. }
  3490.  
  3491. bool OllyLang::DoJNE(string args)
  3492. {
  3493.     if(zf == 0)
  3494.         return DoJMP(args);
  3495.     return true;
  3496. }
  3497.  
  3498.  
  3499. bool OllyLang::DoKEY(string args)
  3500. {
  3501.     string ops[3];
  3502.     ulong key,shift,ctrl;
  3503.  
  3504.     if(!CreateOperands(args, ops, 3)) {
  3505.         ops[2]="0";
  3506.         if(!CreateOperands(args, ops, 2)) {
  3507.             ops[1]="0";
  3508.             if(!CreateOperands(args, ops, 1))
  3509.                 return false;
  3510.         }
  3511.     }
  3512.    
  3513.     if(GetDWOpValue(ops[0], key)
  3514.         && GetDWOpValue(ops[1], shift)
  3515.         && GetDWOpValue(ops[2], ctrl))
  3516.     {
  3517.         Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, ctrl, shift, key);
  3518.         require_ollyloop = 1;
  3519.         return true;
  3520.     }
  3521.     return false;
  3522. }
  3523.  
  3524. bool OllyLang::DoLBL(string args)
  3525. {
  3526.     string ops[2];
  3527.  
  3528.     if(!CreateOperands(args, ops, 2))
  3529.         return false;
  3530.  
  3531.     string lbl;
  3532.     ulong addr;
  3533.  
  3534.     if(GetDWOpValue(ops[0], addr)
  3535.         && GetSTROpValue(ops[1], lbl))
  3536.     {
  3537.         if(lbl != "")
  3538.         {
  3539.             strcpy(buffer, lbl.c_str());
  3540.             Insertname(addr, NM_LABEL, buffer);
  3541.             Broadcast(WM_USER_CHALL, 0, 0);
  3542.         }
  3543.         else
  3544.         {
  3545.             Deletenamerange(addr, addr + 1, NM_COMMENT);
  3546.             Broadcast(WM_USER_CHALL, 0, 0);
  3547.         }
  3548.         return true;
  3549.     }
  3550.     return false;
  3551. }
  3552.  
  3553. bool OllyLang::DoLM(string args)
  3554. {
  3555.     string ops[3];
  3556.     if(!CreateOperands(args, ops, 3))
  3557.         return false;
  3558.  
  3559.     streamsize sum = 0;
  3560.  
  3561.     char spath[MAX_PATH];
  3562.     strcpy(spath, (char*)Plugingetvalue(VAL_EXEFILENAME));
  3563.     string path = spath;
  3564.  
  3565.     path = path.substr(0, path.rfind('\\') + 1);
  3566.  
  3567.     ulong addr, size;
  3568.     string filename;
  3569.     if(GetDWOpValue(ops[0], addr)
  3570.         && GetDWOpValue(ops[1], size)
  3571.         && GetSTROpValue(ops[2], filename))
  3572.     {
  3573.  
  3574.         if (filename.find(":\\") != string::npos)
  3575.             path = filename;
  3576.         else
  3577.             path += filename;
  3578.  
  3579.         std::ifstream fin(path.c_str(),ios::in | ios::binary);
  3580.  
  3581.         if(fin.fail()) {
  3582.             variables["$RESULT"] = 0;
  3583.             errorstr = "Couldn't open file!";
  3584.             fin.close();
  3585.             return false;
  3586.         }
  3587.  
  3588.         char buf[4096];
  3589.         int gotten;
  3590.         while (!fin.eof() && (sum<size || size==0) )
  3591.         {
  3592.             if (size==0)
  3593.                 fin.read(buf, sizeof buf);         
  3594.             else
  3595.                 fin.read(buf, min(size-sum, sizeof buf));
  3596.             gotten=fin.gcount();
  3597.             sum += Writememory(buf, addr+sum, gotten, MM_RESILENT);
  3598.         }
  3599.         fin.close();
  3600.  
  3601.         if (sum) {
  3602.             variables["$RESULT"] = sum;
  3603.             return true;
  3604.         }
  3605.         else
  3606.         {
  3607.             errorstr = "Couldn't load file!";
  3608.             return false;
  3609.         }
  3610.     }
  3611.     return false;
  3612. }
  3613.  
  3614. bool OllyLang::DoLC(string args)
  3615. {
  3616.     //Clear Main Log Window
  3617.  
  3618.     HWND hwndLog=Createlistwindow();
  3619.     if (hwndLog==0)
  3620.         return false;
  3621.  
  3622.     //open context menu to clear main log wdw
  3623.     PostMessage(hwndLog,WM_USER+101,17,0);
  3624.     PostMessage(hwndLog,WM_KEYDOWN,'C',0);
  3625.  
  3626.     return true;
  3627. }
  3628.  
  3629. bool OllyLang::DoLCLR(string args)
  3630. {
  3631.     clearLogLines();
  3632.     return true;
  3633. }
  3634.  
  3635. bool OllyLang::DoLEN(string args)
  3636. {
  3637.     string ops[1],str;
  3638.  
  3639.     if(!CreateOperands(args, ops, 1))
  3640.         return false;
  3641.  
  3642.     if(!GetSTROpValue(ops[0], str))
  3643.         return false;
  3644.  
  3645.     variables["$RESULT"] = (int) str.length();
  3646.     return true;
  3647. }
  3648.  
  3649. bool OllyLang::DoLOADLIB(string args)
  3650. {
  3651.     string ops[1],str;
  3652.  
  3653.     if(!CreateOperands(args, ops, 1))
  3654.         return false;
  3655.  
  3656.     if(!GetSTROpValue(ops[0], str))
  3657.         return false;
  3658.  
  3659.     ulong fnload;
  3660.  
  3661.     SaveRegisters(true);
  3662.     variables["$RESULT"] = 0;
  3663.  
  3664.     DoGPA("\"LoadLibraryA\",\"kernel32.dll\"");
  3665.     fnload = variables["$RESULT"].dw;
  3666.  
  3667.     //alloc memory bloc to store DLL name
  3668.     HANDLE hDbgPrc = (HANDLE) Plugingetvalue(VAL_HPROCESS);
  3669.     void * hMem = VirtualAllocEx(hDbgPrc,NULL,0x1000,MEM_RESERVE|MEM_COMMIT,PAGE_EXECUTE_READWRITE);
  3670.  
  3671.     char bfdlladdr[10]={0};
  3672.     sprintf(bfdlladdr, "%09lX", hMem);
  3673.    
  3674.     Writememory((void*)str.c_str(), (ulong) hMem, str.length(), MM_DELANAL|MM_SILENT);
  3675.  
  3676.     if (DoPUSH(bfdlladdr)) {
  3677.  
  3678.         char bffnloadlib[10]={0};
  3679.         sprintf(bffnloadlib,"%09X",fnload);
  3680.         string libPtrToLoad = bffnloadlib;
  3681.  
  3682.         ExecuteASM("call "+libPtrToLoad);  
  3683.  
  3684.         variables["$RESULT"] = 0;
  3685.  
  3686.         // result returned after process
  3687.         // variables["$RESULT"] = pt->reg.r[REG_EAX];
  3688.         t_dbgmemblock block={0};
  3689.         block.hmem = hMem;
  3690.         block.size = 0x1000;
  3691.         block.script_pos = script_pos;
  3692.         block.free_at_eip = reg_backup.eip;
  3693.         block.result_register = true;
  3694.         block.reg_to_return = REG_EAX;
  3695.         block.restore_registers = true;
  3696.         block.listmemory = true;
  3697.         block.autoclean = true;
  3698.  
  3699.         // Free memory block after next ollyloop
  3700.         regBlockToFree(block);
  3701.         require_addonaction = 1;
  3702.         require_ollyloop = 1;
  3703.        
  3704.         return true;
  3705.     }
  3706.    
  3707.  
  3708.     return false;
  3709. }
  3710.  
  3711. bool OllyLang::DoLOG(string args)
  3712. {
  3713.     string prefix, ops[2];
  3714.  
  3715.     if(CreateOp(args, ops, 2))
  3716.     {
  3717.         if (!GetSTROpValue(ops[1], prefix))
  3718.             return false;
  3719.     }
  3720.     else
  3721.     {
  3722.         prefix="DEFAULT";
  3723.         if(!CreateOperands(args, ops, 1))
  3724.             return false;
  3725.     }
  3726.  
  3727.     ulong dw=0;
  3728.     string str;
  3729.     long double flt=0;
  3730.  
  3731.     if(UnquoteString(ops[0], '"', '"'))
  3732.     {
  3733.         //string constant
  3734.         if (prefix.compare("DEFAULT") == 0)
  3735.         {
  3736.             strncpy(buffer, ops[0].c_str(),TEXTLEN-1);
  3737.         }
  3738.         else
  3739.         {
  3740.             prefix = prefix + ops[0];
  3741.             strncpy(buffer, prefix.c_str(),TEXTLEN-1);
  3742.         }
  3743.         buffer[TEXTLEN-1]=0;
  3744.         Infoline(buffer);
  3745.         Addtolist(0, 1, buffer);
  3746.         add2log(buffer);
  3747.         return true;
  3748.     }
  3749.     else
  3750.     {
  3751.         vtype vt=EMP;
  3752.         if (is_variable(ops[0]))
  3753.             vt = variables[ops[0]].vt;
  3754.  
  3755.         if (vt==DW || vt==EMP) {
  3756.             if(GetDWOpValue(ops[0], dw))
  3757.             {
  3758.                 //ulong constant
  3759.                 if (prefix.compare("DEFAULT") == 0) {
  3760.  
  3761.                     char sym[4096] = {0};
  3762.                     char buf[TEXTLEN-1] = {0};
  3763.                     int res = Decodeaddress(dw, 0, ADC_JUMP | ADC_STRING | ADC_ENTRY | ADC_OFFSET | ADC_SYMBOL, sym, 4096, buf);
  3764.                     sprintf(buffer, "%s: %08lX", ops[0].c_str(), dw);
  3765.  
  3766.                     if(strcmp(buf, ""))
  3767.                         sprintf(buffer, "%s | %s", buffer, buf);
  3768.  
  3769.                     if(strcmp(sym, "") && dw !=  strtoul(sym, 0, 16))
  3770.                         sprintf(buffer, "%s | %s", buffer, sym);
  3771.                
  3772.                 }
  3773.                 else
  3774.                 {
  3775.                     sprintf(buffer, "%s%08lX", prefix.c_str(), dw);
  3776.                 }
  3777.  
  3778.                 buffer[TEXTLEN-1]=0;
  3779.                 str.assign(buffer);
  3780.                 //Infoline and Addtolist need parameters if %s %d... is present in string
  3781.                 if (str.find("%") == string::npos) {
  3782.                     Infoline(buffer);
  3783.                     Addtolist(0, 1, buffer);
  3784.                 }
  3785.                 add2log(buffer);
  3786.                 return true;
  3787.             }
  3788.         }
  3789.  
  3790.         if (vt==FLT || vt==EMP) {
  3791.             if(GetFLTOpValue(ops[0], flt))
  3792.             {
  3793.                 //ulong constant
  3794.                 if (prefix.compare("DEFAULT") == 0) {
  3795.                     sprintf(buffer, "%s: %2lf", ops[0].c_str(), flt);              
  3796.                 } else {
  3797.                     sprintf(buffer, "%s%2lf", prefix.c_str(), flt);
  3798.                 }
  3799.  
  3800.                 buffer[TEXTLEN-1]=0;
  3801.                 Infoline(buffer);
  3802.                 Addtolist(0, 1, buffer);
  3803.                 add2log(buffer);
  3804.                 return true;
  3805.             }
  3806.         }
  3807.  
  3808.         if (vt==STR || vt==EMP) {      
  3809.             if(GetSTROpValue(ops[0], str))
  3810.             {
  3811.                 var v=str;
  3812.  
  3813.                 if(v.isbuf) {
  3814.  
  3815.                     //log a buffer
  3816.                     if (prefix.compare("DEFAULT") == 0)
  3817.                     {
  3818.                         if (str.length()+ops[0].length() < 4094)
  3819.                             sprintf(buffer, "%s: %s", ops[0].c_str(), str.c_str());
  3820.                         else
  3821.                             strncpy(buffer, variables[ops[0]].str.c_str(), TEXTLEN-1);
  3822.                     }
  3823.                     else
  3824.                     {
  3825.                         prefix = prefix + str;
  3826.                         strncpy(buffer, prefix.c_str(), TEXTLEN-1);
  3827.                     }
  3828.                 }
  3829.                 else
  3830.                 {
  3831.  
  3832.                     //log a string
  3833.                     if (prefix.compare("DEFAULT") == 0)
  3834.                     {
  3835.                         if (variables[ops[0]].str.length()+ops[0].length() < 4094)
  3836.                             sprintf(buffer, "%s: %s", ops[0].c_str(), variables[ops[0]].strclean().c_str());
  3837.                         else
  3838.                             strncpy(buffer, variables[ops[0]].strclean().c_str(), TEXTLEN-1);
  3839.                     }
  3840.                     else
  3841.                     {
  3842.                         prefix = prefix + CleanString(str);
  3843.                         strncpy(buffer, prefix.c_str(), TEXTLEN-1);
  3844.                     }
  3845.                 }
  3846.                 buffer[TEXTLEN-1]=0;
  3847.                 str.assign(buffer);
  3848.                 //Infoline and Addtolist need parameters if %s %d... is present in string
  3849.                 if (str.find("%") == string::npos) {
  3850.                     Addtolist(0, 1, buffer);
  3851.                 }
  3852.                 add2log(str);
  3853.                 return true;
  3854.             }
  3855.         }
  3856.     }
  3857.     return false;
  3858. }
  3859.  
  3860. bool OllyLang::DoLOGBUF(string args)
  3861. {
  3862.     string sSep,ops[3];
  3863.  
  3864.     if(!CreateOperands(args, ops, 3)) {
  3865.         ops[2]="\" \"";
  3866.         if(!CreateOperands(args, ops, 2)) {
  3867.             ops[0]=args;
  3868.             ops[1]="0";
  3869.         }
  3870.     }
  3871.  
  3872.     GetSTROpValue(ops[2], sSep);
  3873.  
  3874.     if (!is_variable(ops[0]))
  3875.         return false;
  3876.    
  3877.     ulong dw;
  3878.     if(GetDWOpValue(ops[1], dw))
  3879.     {
  3880.         if (dw==0) dw=16;
  3881.  
  3882.         string sLine="";
  3883.         string data = variables[ops[0]].strbuffhex();
  3884.         for (int n=0; n < variables[ops[0]].size; n++)
  3885.         {
  3886.             sLine=sLine+data.substr(n*2,2)+sSep;
  3887.             if (n>0 && !((n+1) % dw)) {
  3888.                 DoLOG("\""+sLine+"\",\"\"");
  3889.                 sLine="";
  3890.             }
  3891.         }
  3892.         if (sLine!="") DoLOG("\""+sLine+"\",\"\"");
  3893.         return true;
  3894.     }
  3895.     return false;
  3896. }
  3897.  
  3898. bool OllyLang::DoMEMCOPY(string args)
  3899. {
  3900.     string ops[3];
  3901.  
  3902.     if(!CreateOperands(args, ops, 3))
  3903.         return false;
  3904.  
  3905.     args="["+ops[0]+"],["+ops[1]+"],"+ops[2];
  3906.  
  3907.     return DoMOV(args);
  3908. }
  3909.  
  3910. bool OllyLang::DoMOV(string args)
  3911. {
  3912.     string ops[3];
  3913.     bool bDeclared=false;
  3914.     ulong addr=0,maxsize=0;
  3915.  
  3916.     if(CreateOperands(args, ops, 3)){
  3917.         GetDWOpValue(ops[2], maxsize);
  3918.     }
  3919.     else
  3920.         if(!CreateOperands(args, ops, 2))
  3921.             return false;
  3922.  
  3923.     //resolve address with operands
  3924.     if(UnquoteString(ops[1], '[', ']')) {
  3925.         if (!CreateOperands(ops[1],&ops[1],1))
  3926.             return false;
  3927.         ops[1]="["+ops[1]+"]";
  3928.     }
  3929.  
  3930.     // Check source
  3931.     ulong dw = 0; addr=0;
  3932.     string str = "";
  3933.     string tmpops=ops[0];
  3934.     long double flt;
  3935.  
  3936.     // Used to retry after automatic variable declaration
  3937.     retry_DoMOV:
  3938.  
  3939.     // Check destination
  3940.     if(is_variable(ops[0]))
  3941.     {
  3942.         // Dest is variable
  3943.         if(maxsize <= 4 && variables[ops[0]].vt != STR && GetDWOpValue(ops[1], dw) )
  3944.         {
  3945.             // DW to DW/FLT var
  3946.             if (maxsize==0) maxsize=4;
  3947.             dw = resizeDW(dw,maxsize);
  3948.             variables[ops[0]] = dw;
  3949.             variables[ops[0]].size = maxsize;
  3950.         }
  3951.         else if(GetSTROpValue(ops[1], str, maxsize))
  3952.         {
  3953.             // STR to any var
  3954.             variables[ops[0]] = str;
  3955.         }
  3956.         else if(maxsize <= 4 && GetDWOpValue(ops[1], dw) )
  3957.         {
  3958.             // DW to STR var
  3959.             if (maxsize==0) maxsize=4;
  3960.             dw = resizeDW(dw,maxsize);
  3961.             variables[ops[0]] = dw;
  3962.             variables[ops[0]].size = maxsize;
  3963.         }
  3964.         else if(GetFLTOpValue(ops[1], flt))
  3965.         {
  3966.             variables[ops[0]] = flt;
  3967.         }
  3968.         else
  3969.             return false;
  3970.  
  3971.         return true;
  3972.     }
  3973.     else if(is_register(ops[0]))
  3974.     {
  3975.         // Dest is register
  3976.         if(GetDWOpValue(ops[1], dw))
  3977.         {
  3978.             t_thread* pt = Findthread(Getcputhreadid());
  3979.             int regnr = GetRegNr(ops[0]);          
  3980.             if(regnr != -1) {
  3981.                 if (ops[0].length()==2)
  3982.                 {
  3983.                     if (ops[0][1] == 'l') {
  3984.                         //R8
  3985.                         dw &= 0xFF;
  3986.                         pt->reg.r[regnr] &= 0xFFFFFF00;
  3987.                         pt->reg.r[regnr] |= dw;
  3988.                     }
  3989.                     else if ( ops[0][1] == 'h')
  3990.                     {
  3991.                         //R8
  3992.                         dw &= 0xFF;
  3993.                         pt->reg.r[regnr] &= 0xFFFF00FF;
  3994.                         pt->reg.r[regnr] |= dw * 0x100;
  3995.                     }
  3996.                     else
  3997.                     {
  3998.                         //R16
  3999.                         dw &= 0xFFFF;
  4000.                         pt->reg.r[regnr] &= 0xFFFF0000;
  4001.                         pt->reg.r[regnr] |= dw;
  4002.                     }
  4003.                 }
  4004.                 else
  4005.                 {
  4006.                         //R32
  4007.                         pt->reg.r[regnr] = dw;
  4008.                 }
  4009.             }
  4010.             else
  4011.                 if(ops[0] == "eip") {
  4012.                     pt->reg.ip = dw;
  4013.                     //synch disasm window position
  4014.                     Setdisasm(dw,1,CPU_ASMHIST);
  4015.                 }
  4016.  
  4017.             pt->reg.modified = 1;
  4018.             pt->regvalid = 1;
  4019.             Broadcast(WM_USER_CHREG, 0, 0);
  4020.             require_ollyloop = 1;
  4021.             dw = resizeDW(dw,maxsize);
  4022.             return true;
  4023.         }
  4024.         return false;
  4025.     }
  4026.     else if(is_flag(ops[0]))
  4027.     {
  4028.         // Dest is flag
  4029.         if(GetDWOpValue(ops[1], dw))
  4030.         {
  4031.             if(dw != 0 && dw != 1)
  4032.             {
  4033.                 errorstr = "Invalid flag value";
  4034.                 return false;
  4035.             }
  4036.  
  4037.             eflags flags;
  4038.             ZeroMemory(&flags, sizeof DWORD);
  4039.             t_thread* pt = Findthread(Getcputhreadid());
  4040.             flags.dwFlags = pt->reg.flags;
  4041.  
  4042.             if(stricmp(ops[0].c_str(), "!af") == 0)
  4043.                 flags.bitFlags.AF = dw;
  4044.             else if(stricmp(ops[0].c_str(), "!cf") == 0)
  4045.                 flags.bitFlags.CF = dw;
  4046.             else if(stricmp(ops[0].c_str(), "!df") == 0)
  4047.                 flags.bitFlags.DF = dw;
  4048.             else if(stricmp(ops[0].c_str(), "!of") == 0)
  4049.                 flags.bitFlags.OF = dw;
  4050.             else if(stricmp(ops[0].c_str(), "!pf") == 0)
  4051.                 flags.bitFlags.PF = dw;
  4052.             else if(stricmp(ops[0].c_str(), "!sf") == 0)
  4053.                 flags.bitFlags.SF = dw;
  4054.             else if(stricmp(ops[0].c_str(), "!zf") == 0)
  4055.                 flags.bitFlags.ZF = dw;
  4056.  
  4057.             pt->reg.flags = flags.dwFlags;
  4058.             pt->reg.modified = 1;
  4059.             pt->regvalid = 1;
  4060.             Broadcast(WM_USER_CHREG, 0, 0);
  4061.             require_ollyloop = 1;
  4062.             return true;
  4063.         }
  4064.         return false;
  4065.     }
  4066.     else if(is_floatreg(ops[0]))
  4067.     {
  4068.         // Dest is float register
  4069.         if(GetFLTOpValue(ops[1], flt))
  4070.         {
  4071.             t_thread* pt = Findthread(Getcputhreadid());
  4072.             pt->reg.f[(ops[0][3]-0x30)] = flt;
  4073.             pt->reg.modified = 1;
  4074.             pt->regvalid = 1;
  4075.             Broadcast(WM_USER_CHREG, 0, 0);
  4076.             require_ollyloop = 1;
  4077.             return true;
  4078.         }
  4079.         return false;
  4080.     }
  4081.     else if(UnquoteString(ops[0], '[', ']'))
  4082.     {
  4083.         // Destination is memory address
  4084.         // Get Address from Operators (+_*...)
  4085.         nIgnoreNextValuesHist++;
  4086.         CreateOperands(ops[0], &ops[0], 1);
  4087.  
  4088.         if(GetDWOpValue(ops[0], addr))
  4089.         {
  4090.             if (addr==0)
  4091.             {
  4092.                 DoLOG("\"WARNING: writing to address 0 !\"");
  4093.                 return true;
  4094.             }
  4095.  
  4096.             tmpops=ops[1];
  4097.             if (maxsize > 8 && UnquoteString(ops[1], '[', ']'))
  4098.             {
  4099.                 //Get Addr from Operators
  4100.                 CreateOperands(ops[1], &ops[1], 1);
  4101.  
  4102.                 //Optimized Mem Copy
  4103.                 ulong src;
  4104.                 if (!GetDWOpValue(ops[1], src) || src==0) {
  4105.                     DoLOG("\"WARNING: copy from address 0 !\"");
  4106.                     return true;
  4107.                 }
  4108.                 char* copybuffer= new char[maxsize];
  4109.                 if (maxsize != Readmemory((void*) copybuffer, src, maxsize, MM_RESTORE)) {
  4110.                     delete[] copybuffer;
  4111.                     return false;
  4112.                 }
  4113.                 Writememory((void*) copybuffer, addr, maxsize, MM_DELANAL);
  4114.                 delete[] copybuffer;
  4115.                 Broadcast(WM_USER_CHALL, 0, 0);
  4116.             }
  4117.             else if (GetDWOpValue(ops[1], dw) && maxsize <= 4)
  4118.             {
  4119.                 if (maxsize==0) maxsize=4;
  4120.                 dw = resizeDW(dw,maxsize);
  4121.                 Writememory(&dw, addr, maxsize, MM_DELANAL|MM_SILENT);
  4122.                 Broadcast(WM_USER_CHALL, 0, 0);
  4123.             }
  4124.             else if (GetSTROpValue(ops[1], str, maxsize))
  4125.             {
  4126.                 var v=str;
  4127.                 if (maxsize==0) maxsize=v.size;
  4128.                 maxsize=min(maxsize,v.size);
  4129.                 Writememory((void*)v.strbuff().c_str(), addr, maxsize, MM_DELANAL|MM_SILENT);          
  4130.                 Broadcast(WM_USER_CHALL, 0, 0);
  4131.             }
  4132.             else if (GetFLTOpValue(ops[1], flt))
  4133.             {
  4134.                 Writememory(&flt, addr, 8, MM_DELANAL|MM_SILENT);              
  4135.                 Broadcast(WM_USER_CHALL, 0, 0);
  4136.             }
  4137.             else
  4138.             {
  4139.                 errorstr = "Bad operator \"" + ops[1] + "\"";
  4140.                 return false;
  4141.             }
  4142.             Broadcast(WM_USER_CHMEM, 0, 0);
  4143.             return true;
  4144.         }
  4145.         return false;
  4146.  
  4147.     }
  4148.     else if (!bDeclared && ops[0][0] >= 'A' )
  4149.     {
  4150.  
  4151.         bDeclared=true;
  4152.         DoVAR(ops[0]);
  4153.         //DoLOG("\"automatic declaration of variable "+ops[0]+"\"");
  4154.         goto retry_DoMOV;
  4155.  
  4156.     }
  4157.  
  4158.     //errorstr = "Variable '" + ops[0] + "' is not declared";
  4159.     return false;
  4160. }
  4161.  
  4162. bool OllyLang::DoMSG(string args)
  4163. {
  4164.     string ops[1];
  4165.  
  4166.     if(!CreateOp(args, ops, 1))
  4167.         return false;
  4168.  
  4169.     string msg;
  4170.     if(GetANYOpValue(ops[0], msg))
  4171.     {
  4172.         if (wndProg.hw!=NULL)
  4173.             InvalidateRect(wndProg.hw, NULL, FALSE);
  4174.        
  4175.         //hwndOllyDbg() or 0: modal or not
  4176.         int ret = MessageBox(0, msg.c_str(), "MSG ODbgScript", MB_ICONINFORMATION | MB_OKCANCEL | MB_TOPMOST | MB_SETFOREGROUND);
  4177.         FocusProgWindow();
  4178.         if(ret == IDCANCEL) {
  4179.             return Pause();
  4180.         }
  4181.         return true;
  4182.     }
  4183.     return false;
  4184. }
  4185.  
  4186. bool OllyLang::DoMSGYN(string args)
  4187. {
  4188.     string ops[1];
  4189.  
  4190.     if(!CreateOperands(args, ops, 1))
  4191.         return false;
  4192.  
  4193.     string msg;
  4194.     if(GetSTROpValue(ops[0], msg))
  4195.     {
  4196.         if (wndProg.hw!=NULL)
  4197.             InvalidateRect(wndProg.hw, NULL, FALSE);
  4198.  
  4199.         int ret = MessageBox(0, msg.c_str(), "MSG ODbgScript", MB_ICONQUESTION | MB_YESNOCANCEL | MB_TOPMOST | MB_SETFOREGROUND);
  4200.         FocusProgWindow();
  4201.         if(ret == IDCANCEL)
  4202.         {
  4203.             variables["$RESULT"] = 2;
  4204.             return Pause();
  4205.         }
  4206.         else if(ret == IDYES)
  4207.             variables["$RESULT"] = 1;
  4208.         else
  4209.             variables["$RESULT"] = 0;
  4210.  
  4211.         return true;
  4212.     }
  4213.     return false;
  4214. }
  4215.  
  4216. bool OllyLang::DoOLLY(string args)
  4217. {
  4218.     string ops[1], param;
  4219.     ulong value;
  4220.  
  4221.     if(!CreateOp(args, ops, 1))
  4222.         return false;
  4223.  
  4224.     if ( GetSTROpValue("\""+ops[0]+"\"", param) )
  4225.     {
  4226.  
  4227.         transform(param.begin(), param.end(), param.begin(), toupper);
  4228.  
  4229.         if (param == "PID")
  4230.         {
  4231.             value = GetCurrentProcessId();
  4232.             variables["$RESULT"] = value;
  4233.             return true;
  4234.         }
  4235.         else if (param == "HWND")
  4236.         {
  4237.             value = (ulong) hwndOllyDbg();
  4238.             variables["$RESULT"] = value;
  4239.             return true;
  4240.         }
  4241.        
  4242.  
  4243.     }
  4244.     return false;
  4245. }
  4246.  
  4247.  
  4248. bool OllyLang::DoOR(string args)
  4249. {
  4250.     string ops[2];
  4251.  
  4252.     if(!CreateOperands(args, ops, 2))
  4253.         return false;
  4254.  
  4255.     ulong dw1, dw2;
  4256.     if(GetDWOpValue(ops[0], dw1) && GetDWOpValue(ops[1], dw2))
  4257.     {
  4258.         args = ops[0] + ", 0" + ultoa(dw1 | dw2, buffer, 16);
  4259.         nIgnoreNextValuesHist++;
  4260.         return DoMOV(args);
  4261.     }
  4262.     return false;
  4263. }
  4264.  
  4265. bool OllyLang::DoMUL(string args)
  4266. {
  4267.     string ops[2];
  4268.  
  4269.     if(!CreateOperands(args, ops, 2))
  4270.         return false;
  4271.  
  4272.     ulong dw1, dw2;
  4273.     if(GetDWOpValue(ops[0], dw1) && GetDWOpValue(ops[1], dw2))
  4274.     {
  4275.         args = ops[0] + ", 0" + ultoa(dw1 * dw2, buffer, 16);
  4276.         nIgnoreNextValuesHist++;
  4277.         return DoMOV(args);
  4278.     }
  4279.     return false;
  4280. }
  4281.  
  4282. bool OllyLang::DoNEG(string args)
  4283. {
  4284.     string ops[1];
  4285.  
  4286.     if(!CreateOperands(args, ops, 1))
  4287.         return false;
  4288.  
  4289.     ulong dw1;
  4290.     if(GetDWOpValue(ops[0], dw1))
  4291.     {
  4292.         __asm
  4293.         {
  4294.             push eax
  4295.             mov eax,dw1
  4296.             neg eax
  4297.             mov dw1,eax
  4298.             pop eax
  4299.         }
  4300.         args = ops[0] + ", " +ultoa(dw1, buffer, 16);
  4301.         nIgnoreNextValuesHist++;
  4302.         return DoMOV(args);
  4303.     }
  4304.     return false;
  4305. }
  4306.  
  4307.  
  4308. bool OllyLang::DoNOT(string args)
  4309. {
  4310.     string ops[1];
  4311.  
  4312.     if(!CreateOperands(args, ops, 1))
  4313.         return false;
  4314.  
  4315.     ulong dw1;
  4316.     if(GetDWOpValue(ops[0], dw1))
  4317.     {
  4318.         __asm
  4319.         {
  4320.             push eax
  4321.             mov eax,dw1
  4322.             not eax
  4323.             mov dw1,eax
  4324.             pop eax
  4325.         }
  4326.         args = ops[0] + ", " +ultoa(dw1, buffer, 16);
  4327.         nIgnoreNextValuesHist++;
  4328.         return DoMOV(args);
  4329.     }
  4330.     return false;
  4331. }
  4332.  
  4333. //see also GCI
  4334. bool OllyLang::DoOPCODE(string args)
  4335. {
  4336.     string ops[1];
  4337.     if(!CreateOperands(args, ops, 1))
  4338.         return false;
  4339.  
  4340.     ulong addr,size;
  4341.     if(GetDWOpValue(ops[0], addr))
  4342.     {
  4343.         BYTE buffer[MAXCMDSIZE];
  4344. //      size=Readmemory(buffer, addr, MAXCMDSIZE, MM_SILENT);
  4345.         size=Readcommand(addr,(char *) buffer);
  4346.        
  4347.         if (size>0)
  4348.         {
  4349.             t_disasm disasm;
  4350.             size=Disasm(buffer,size,addr,NULL,&disasm,DISASM_CODE,NULL);
  4351.  
  4352.             if (size>0)
  4353.             {
  4354.                 variables["$RESULT"] = disasm.dump;     //command bytes
  4355.                 variables["$RESULT_1"] = disasm.result; //asm command text
  4356.                 variables["$RESULT_2"] = size;
  4357.                 return true;
  4358.             }
  4359.         }
  4360.         variables["$RESULT"] = 0;
  4361.         variables["$RESULT_1"] = 0;
  4362.         variables["$RESULT_2"] = 0;
  4363.         return true;
  4364.  
  4365.     }
  4366.     return false;
  4367. }
  4368.  
  4369. bool OllyLang::DoOPENDUMP(string args)
  4370. {
  4371.     string ops[3];
  4372.  
  4373.     if(!CreateOperands(args, ops, 3))
  4374.     if(!CreateOperands(args, ops, 2))
  4375.     if(!CreateOperands(args, ops, 1))
  4376.         return false;
  4377.  
  4378.     ulong addr, base, size;
  4379.     if(!GetDWOpValue(ops[0], addr))
  4380.         return false;
  4381.     if(!GetDWOpValue(ops[1], base))
  4382.         return false;
  4383.     if(!GetDWOpValue(ops[2], size))
  4384.         return false;
  4385.  
  4386.     variables["$RESULT"] = 0;
  4387.  
  4388.     if (addr==0)
  4389.         return true; //do nothing
  4390.  
  4391.     t_memory* mem = Findmemory(addr);
  4392.  
  4393.     if (base==0) base=mem->base;
  4394.     if (size==0) size=mem->size;
  4395.    
  4396.     HWND wndDump = Createdumpwindow(NULL,base,size,addr,0x01081,NULL);
  4397.  
  4398. //dont work: need to ask Olleh to have a function
  4399. //  t_dump * dump = (t_dump *) &wndDump;
  4400.  
  4401.     //So i created a special key message to grab it
  4402.     if (wndDump!=NULL) {
  4403.         ulong res = SendMessage(wndDump,WM_KEYDOWN,VK_F5,0);
  4404.         //can now grab (t_dump *) dumpWindows[wndDump];
  4405.         variables["$RESULT"] = (ulong) wndDump;    
  4406.     }
  4407.     return true;
  4408.  
  4409.     return false;
  4410. }
  4411.  
  4412. bool OllyLang::DoOPENTRACE(string args)
  4413. {
  4414.     ulong threadid = Getcputhreadid();
  4415.     if(threadid == 0)
  4416.         threadid = Plugingetvalue(VAL_MAINTHREADID);
  4417.     t_thread* pthr = Findthread(threadid);
  4418.     if(pthr != NULL)
  4419.         Startruntrace(&(pthr->reg));
  4420.  
  4421.     //Show Trace Window
  4422.     HWND wndTrace = Creatertracewindow();
  4423.     //SetWindowPos(wndTrace,HWND_TOP,0,0,0,0,SWP_NOSIZE+SWP_NOMOVE);
  4424.  
  4425.     return true;
  4426. }
  4427.  
  4428. bool OllyLang::DoPAUSE(string args)
  4429. {
  4430.     Pause();
  4431.     FocusProgWindow();
  4432.     return true;
  4433. }
  4434.  
  4435. bool OllyLang::DoPOP(string args)
  4436. {
  4437.     string ops[1];
  4438.  
  4439.     nIgnoreNextValuesHist++;
  4440.     var_logging=false;
  4441.     if(!CreateOperands(args, ops, 1))
  4442.         return false;
  4443.  
  4444.     args = ops[0] + ", [esp], 4";
  4445.     nIgnoreNextValuesHist+=2;
  4446.     DoMOV(args);
  4447.  
  4448.     args = "esp, esp+4";
  4449.     nIgnoreNextValuesHist+=1;
  4450.     return DoMOV(args);
  4451. }
  4452.  
  4453. bool OllyLang::DoPOPA(string args)
  4454. {
  4455.     bool result = RestoreRegisters(true);
  4456.     if (result)
  4457.         require_ollyloop=1;
  4458.     return result;
  4459. }
  4460.  
  4461. bool OllyLang::DoPREOP(string args)
  4462. {
  4463.     string ops[1];
  4464.  
  4465.     CreateOperands(args, ops, 1);
  4466.     if (ops[0]=="")
  4467.         ops[0]="eip";
  4468.  
  4469.     ulong addr,base;
  4470.     if(!GetDWOpValue(ops[0], addr))
  4471.         return false;
  4472.  
  4473.     t_memory* mem = Findmemory(addr);
  4474.     if(mem != NULL)
  4475.     {
  4476.         variables["$RESULT"] = Disassembleback(NULL,mem->base,mem->size,addr,1,true);
  4477.         return true;
  4478.     }
  4479.     else
  4480.     {
  4481.         variables["$RESULT"] = 0;
  4482.         return true;
  4483.     }
  4484.     return false;
  4485. }
  4486.  
  4487. bool OllyLang::DoPUSH(string args)
  4488. {
  4489.     string ops[1];
  4490.  
  4491.     if(!CreateOperands(args, ops, 1))
  4492.         return false;
  4493.  
  4494.     ulong dw,dw1;
  4495.  
  4496.     //var_logging=false;
  4497.     if(GetDWOpValue(ops[0], dw1))
  4498.     {
  4499.         args = "esp, esp-4";
  4500.         nIgnoreNextValuesHist+=2;
  4501.         DoMOV(args);
  4502.  
  4503.         args = "[esp], " + ops[0];
  4504.         nIgnoreNextValuesHist++;
  4505.         DoMOV(args);
  4506.  
  4507.         t_thread* pt = Findthread(Getcputhreadid());   
  4508.         dw1 = pt->reg.r[4];
  4509.         Readmemory(&dw, dw1, 4, MM_DELANAL|MM_SILENT);
  4510.         if (nIgnoreNextValuesHist==0)
  4511.             setProgLineValue(script_pos+1,dw);
  4512.         return true;
  4513.     }
  4514.     return false;
  4515. }
  4516.  
  4517. bool OllyLang::DoPUSHA(string args)
  4518. {
  4519.     return SaveRegisters(true);
  4520. }
  4521.  
  4522. bool OllyLang::DoREADSTR(string args)
  4523. {
  4524.     string ops[2],s;
  4525.     ulong maxsize=0;
  4526.  
  4527.     ulong dw = 0, addr = 0;
  4528.     string str = "";
  4529.     if(CreateOperands(args, ops, 2))
  4530.     {
  4531.         GetDWOpValue(ops[1], maxsize);
  4532.         GetSTROpValue(ops[0], str, maxsize);
  4533.  
  4534.         variables["$RESULT"] = str;
  4535.         return true;
  4536.     }
  4537.     return false;
  4538. }
  4539.  
  4540. // Restore Break Points
  4541. // restores all hardware and software breakpoints
  4542. // (if arg1 == 'STRICT', all soft bp set by script will be deleted and only those have been set before it runs
  4543. // will be restored
  4544. // if no argument set, previous soft bp will be appended to those set by script)
  4545.  
  4546. // rbp [arg1]
  4547. // arg1 = may be STRICT or nothing
  4548.  
  4549. // return in:
  4550. // - $RESULT number of restored swbp
  4551. // - $RESULT_1 number of restored hwbp
  4552.  
  4553. // ex     : rbp
  4554. //        : rbp STRICT
  4555. bool OllyLang::DoRBP ( string args )
  4556. {
  4557.     t_table* bpt = 0;
  4558.     t_bpoint* bpoint = 0;
  4559.     uint n,i=0;
  4560.     string ops[1];
  4561.  
  4562.     CreateOperands ( args, ops, 1 );
  4563.    
  4564.     variables["$RESULT"] = 0;
  4565.  
  4566.     if ( saved_bp )
  4567.     {
  4568.         bpt = ( t_table * ) Plugingetvalue ( VAL_BREAKPOINTS );
  4569.         if ( bpt != NULL )
  4570.         {
  4571.             bpoint = ( t_bpoint * ) bpt->data.data;
  4572.  
  4573.             if ( ops[0] == "STRICT" )
  4574.             {
  4575.                 int dummy;
  4576.                 dummy = bpt->data.n;
  4577.                 for ( n = 0; n < dummy; n++ )
  4578.                 {
  4579.                     Deletebreakpoints ( bpoint->addr, ( bpoint->addr ) + 1, 1 );
  4580.                 }
  4581.             }
  4582.  
  4583.             for ( n=0; n < sortedsoftbp_t.n; n++ )
  4584.                 Setbreakpoint ( softbp_t[n].addr, ( softbp_t[n].type | TY_KEEPCODE ) ^ TY_KEEPCODE, 0 );
  4585.  
  4586.             variables["$RESULT"] = ( DWORD ) sortedsoftbp_t.n;
  4587.  
  4588.             Broadcast ( WM_USER_CHALL, 0, 0 );
  4589.         }
  4590.  
  4591.     }
  4592.  
  4593.     //Hardware Bps
  4594.     for ( n=0; n < 4; n++ ) {
  4595.         if (hwbp_t[n].addr) {
  4596.             Sethardwarebreakpoint ( hwbp_t[n].addr, hwbp_t[n].size, hwbp_t[n].type );
  4597.             i++;
  4598.         }
  4599.     }
  4600.     variables["$RESULT_1"] = ( DWORD ) i;
  4601.  
  4602.     return true;
  4603. }
  4604.  
  4605. bool OllyLang::DoREF(string args)
  4606. {
  4607.     string ops[2];
  4608.     if (!CreateOperands(args, ops, 2)){
  4609.         ops[1]="MEMORY";
  4610.         if(!CreateOperands(args, ops, 1))
  4611.         return false;
  4612.     }
  4613.  
  4614.     char title[256]="Reference to Command - ODbgScript REF";   
  4615.     ulong addr,size;
  4616.     string str;
  4617.     if(GetDWOpValue(ops[0], addr) && GetSTROpValue("\""+ops[1]+"\"", str) )
  4618.     {
  4619.         variables["$RESULT"] = 0;
  4620.         variables["$RESULT_1"] = 0; //command bytes
  4621.         variables["$RESULT_2"] = 0;
  4622.  
  4623.         if (adrREF!=addr)
  4624.         {
  4625.             curREF=-1;
  4626.  
  4627.             //Reset REF
  4628.             if (addr == 0) {
  4629.                 adrREF=0;
  4630.                 return true;
  4631.             }
  4632.  
  4633.             //Info to get destination address
  4634.             BYTE buffer[MAXCMDSIZE];
  4635. //          size=Readmemory(buffer, addr, MAXCMDSIZE, MM_SILENT);
  4636.             size=Readcommand(addr,(char *) buffer);
  4637.  
  4638.             t_disasm disasm;
  4639.             size=Disasm(buffer, size, addr, NULL, &disasm, DISASM_SIZE, NULL);
  4640.             if (size==0)
  4641.                 return true;
  4642.  
  4643.             transform(str.begin(), str.end(), str.begin(), toupper);
  4644.  
  4645.             //Search for references
  4646.             if(str == "MEMORY") // Compatibility (before v1.71) Search in the memory bloc
  4647.             {
  4648.                 t_memory* mem = Findmemory(addr);
  4649.  
  4650.                 if (Findreferences(mem->base,mem->size,addr,addr+size,addr,16,title) > 0)
  4651.                     adrREF=addr;
  4652.                 else
  4653.                     return true;
  4654.             }
  4655.             else if(str == "CODE") // Search in the code part of module
  4656.             {
  4657.                 t_module* mod = Findmodule(addr);
  4658.  
  4659.                 if (Findreferences(mod->codebase,mod->codesize,addr,addr+size,addr,16,title) > 0)
  4660.                     adrREF=addr;
  4661.                 else
  4662.                     return true;
  4663.             }
  4664.             else if(str == "MODULE") // Search in the whole module
  4665.             {
  4666.                 t_module* mod = Findmodule(addr);
  4667.  
  4668.                 if (Findreferences(mod->base,mod->size,addr,addr+size,addr,16,title) > 0)
  4669.                     adrREF=addr;
  4670.                 else
  4671.                     return true;
  4672.             }
  4673.             else
  4674.                 return false;
  4675.  
  4676.         }
  4677.         t_table* tref=(t_table*) Plugingetvalue(VAL_REFERENCES);
  4678.        
  4679.         if (tref != NULL)
  4680.         {
  4681.             ignore_origin:
  4682.             curREF++;
  4683.             if (curREF > tref->data.n)
  4684.                 adrREF=0;
  4685.             else
  4686.             {
  4687.                 t_ref* ref= (t_ref*) Getsortedbyselection(&tref->data,curREF);
  4688.                 if (ref != NULL)
  4689.                 {
  4690.  
  4691.                     if (ref->addr == addr)
  4692.                         goto ignore_origin;
  4693.  
  4694.                     //Disasm origin to get comments
  4695.                     BYTE buffer[MAXCMDSIZE];
  4696.                     size=Readmemory(buffer, ref->addr, MAXCMDSIZE, MM_SILENT);                 
  4697.                     if (size>0)
  4698.                     {
  4699.                         t_disasm disasm;
  4700.                         size=Disasm(buffer,size,ref->addr,NULL,&disasm,DISASM_ALL,NULL);
  4701.  
  4702.                         if (size>0)
  4703.                         {
  4704.                             variables["$RESULT"] = ref->addr;
  4705.                             variables["$RESULT_1"] = disasm.result; //command text
  4706.                             variables["$RESULT_2"] = disasm.comment;
  4707.                             return true;
  4708.                         }
  4709.                     }
  4710.                 }
  4711.             }          
  4712.         }
  4713.         return true;
  4714.  
  4715.     }
  4716.     return false;
  4717. }
  4718.  
  4719. bool OllyLang::DoREFRESH(string args)
  4720. {
  4721.     //Refresh Memory Map
  4722.     Listmemory();
  4723.  
  4724.     //Refresh Executable Modules (no other way found to do this)
  4725.     t_table* tt = (t_table*)Plugingetvalue(VAL_MODULES);
  4726.     if (tt->hw!=0) {
  4727.         //was visible
  4728.         Sendshortcut(PM_MAIN,0,WM_SYSKEYDOWN,0,0,'E');
  4729.     } else {
  4730.         //was hidden, hide it after
  4731.         Sendshortcut(PM_MAIN,0,WM_SYSKEYDOWN,0,0,'E');
  4732.         tt = (t_table*)Plugingetvalue(VAL_MODULES);
  4733.         if (tt->hw!=0)
  4734.             DestroyWindow(tt->hw);
  4735.     }
  4736.  
  4737.     //Refresh DISASM
  4738.     Redrawdisassembler();
  4739.  
  4740.     require_ollyloop=1;
  4741.     Broadcast(WM_USER_CHALL, 0, 0);
  4742.     return true;
  4743. }
  4744.  
  4745. bool OllyLang::DoREPL(string args)
  4746. {
  4747.     string ops[4];
  4748.     if(!CreateOperands(args, ops, 4))
  4749.         return false;
  4750.  
  4751.     if(ops[1].length() % 2 != 0 || ops[2].length() % 2 != 0)
  4752.     {
  4753.         errorstr = "Hex string must have an even number of characters!";
  4754.         return false;
  4755.     }
  4756.  
  4757.     ulong addr, len;
  4758.     if(GetDWOpValue(ops[0], addr) && UnquoteString(ops[1], '#', '#') && UnquoteString(ops[2], '#', '#') && GetDWOpValue(ops[3], len))
  4759.     {
  4760.         // Replace
  4761.         char *membuf = 0;
  4762.         t_memory* tmem = Findmemory(addr);
  4763.         membuf = new char[len];
  4764.         int memlen = Readmemory(membuf, addr, len, MM_RESILENT);
  4765.         bool replaced = false;
  4766.  
  4767.         try
  4768.         {
  4769.             int i = 0;
  4770.             while(i < len)
  4771.             {
  4772.                 replaced = Replace(membuf + i, ops[1].c_str(), ops[2].c_str(), ops[2].length()) || replaced;
  4773.                 i++;
  4774.             }
  4775.         }
  4776.         catch(char* str)
  4777.         {
  4778.             errorstr = errorstr.append(str);
  4779.             delete [] membuf;
  4780.             return false;
  4781.         }
  4782.        
  4783.         if(replaced)
  4784.         {
  4785.             Writememory(membuf, addr, len, MM_DELANAL | MM_SILENT);
  4786.             Broadcast(WM_USER_CHALL, 0, 0);
  4787.         }
  4788.  
  4789.         delete [] membuf;
  4790.  
  4791.         return true;
  4792.     }
  4793.  
  4794.     return false;
  4795. }
  4796.  
  4797. bool OllyLang::DoRESET(string args)
  4798. {
  4799.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 1, 0, VK_F2);
  4800.     return true;
  4801. }
  4802.  
  4803. bool OllyLang::DoRET(string args)
  4804. {
  4805.     if (calls.size() > 0) {
  4806.         script_pos = calls[calls.size()-1];
  4807.         calls.pop_back();
  4808.     } else {
  4809.         MessageBox(0, "Script finished", "ODbgScript", MB_ICONINFORMATION | MB_OK | MB_TOPMOST | MB_SETFOREGROUND);
  4810.         Reset();
  4811.         FocusProgWindow();
  4812.     }
  4813.     return true;
  4814. }
  4815.  
  4816. bool OllyLang::DoREV(string args)
  4817. {
  4818.     string ops[1];
  4819.     ulong dw;
  4820. //  BYTE b, tb[4];
  4821.  
  4822.     if(!CreateOperands(args, ops, 1))
  4823.         return false;
  4824.  
  4825.     if(!GetDWOpValue(ops[0], dw))
  4826.         return false;
  4827.  
  4828.     dw=rev(dw);
  4829.  
  4830. /*  memcpy(&tb[0],&dw,4);
  4831.     dw=tb[3]; tb[3]=tb[0]; tb[0]=dw;
  4832.     dw=tb[2]; tb[2]=tb[1]; tb[1]=dw;
  4833.     memcpy(&dw,&tb[0],4);
  4834. */
  4835.     variables["$RESULT"] = dw;
  4836.     return true;
  4837. }
  4838.  
  4839. bool OllyLang::DoROL(string args)
  4840. {
  4841.     string ops[2];
  4842.  
  4843.     if(!CreateOperands(args, ops, 2))
  4844.         return false;
  4845.  
  4846.     ulong dw1;
  4847.     BYTE dw2;
  4848.     if(GetDWOpValue(ops[0], dw1) && GetBYTEOpValue(ops[1], dw2))
  4849.     {
  4850.        
  4851.         __asm
  4852.         {
  4853.             push eax
  4854.             push ecx
  4855.             mov eax,dw1
  4856.             mov cl,dw2
  4857.             rol eax,cl
  4858.             mov dw1,eax
  4859.             pop ecx
  4860.             pop eax
  4861.         }
  4862.         args = ops[0] + ", 0" + ultoa(dw1, buffer, 16);
  4863.         nIgnoreNextValuesHist++;
  4864.         return DoMOV(args);
  4865.     }
  4866.     return false;
  4867. }
  4868.  
  4869. bool OllyLang::DoROR(string args)
  4870. {
  4871.     string ops[2];
  4872.  
  4873.     if(!CreateOperands(args, ops, 2))
  4874.         return false;
  4875.  
  4876.     ulong dw1;
  4877.     BYTE dw2;
  4878.     if(GetDWOpValue(ops[0], dw1)
  4879.         && GetBYTEOpValue(ops[1], dw2))
  4880.     {
  4881.        
  4882.         __asm
  4883.         {
  4884.             push eax
  4885.             push ecx
  4886.             mov eax,dw1
  4887.             mov cl,dw2
  4888.             ror eax,cl
  4889.             mov dw1,eax
  4890.             pop ecx
  4891.             pop eax
  4892.         }
  4893.         args = ops[0] + ", 0" + ultoa(dw1, buffer, 16);
  4894.         nIgnoreNextValuesHist++;
  4895.         return DoMOV(args);
  4896.     }
  4897.     return false;
  4898. }
  4899.  
  4900.  
  4901. bool OllyLang::DoRTR(string args)
  4902. {
  4903.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 1, 0, VK_F9);
  4904.     require_ollyloop = 1;
  4905.     return true;
  4906. }
  4907.  
  4908. bool OllyLang::DoRTU(string args)
  4909. {
  4910.     Sendshortcut(PM_MAIN,0,WM_SYSKEYDOWN,0,0,VK_F9);
  4911.     require_ollyloop = 1;
  4912.     return true;
  4913. }
  4914.  
  4915. bool OllyLang::DoRUN(string args)
  4916. {
  4917.     Go(Getcputhreadid(), 0, STEP_RUN, 0, 1);
  4918.     require_ollyloop = 1;
  4919.     return true;
  4920. }
  4921.  
  4922. // Store Break Points
  4923. // stores all hardware and software breakpoints
  4924.  
  4925. // return in:
  4926. // - $RESULT number of restored swbp
  4927. // - $RESULT_1 number of restored hwbp
  4928.  
  4929. // ex   : sbp
  4930. //      : no argument
  4931. bool OllyLang::DoSBP ( string args )
  4932. {
  4933.     uint n = 0, i;
  4934.     bool success;
  4935.     t_table *bpt;
  4936.     t_bpoint *bpoint;
  4937.  
  4938.     variables["$RESULT"] = 0;
  4939.     variables["$RESULT_1"] = 0;
  4940.  
  4941.     bpt = ( t_table * ) Plugingetvalue ( VAL_BREAKPOINTS );
  4942.     if ( bpt != NULL )
  4943.     {
  4944.         bpoint = ( t_bpoint * ) ( bpt->data.data );
  4945.         if ( bpoint != NULL )
  4946.         {
  4947.             n = bpt->data.n;
  4948.  
  4949.             if ( n > alloc_bp )
  4950.             {
  4951.                 //FreeBpMem();
  4952.                 success = AllocSwbpMem ( n );
  4953.             }
  4954.  
  4955.             if ( n > alloc_bp && !success ) {
  4956.                 errorstr = "Can't allocate enough memory to copy all breakpoints";
  4957.                 return false;
  4958.             }
  4959.             else if (n > 0)
  4960.             {
  4961.                 memcpy ( ( void* ) softbp_t, bpt->data.data, n*sizeof ( t_bpoint ) );
  4962.                 memcpy ( ( void* ) &sortedsoftbp_t, ( void* ) &bpt->data, sizeof ( t_sorted ) );
  4963.                
  4964.             }
  4965.  
  4966.             saved_bp = n;
  4967.             variables["$RESULT"] =  ( DWORD ) n;
  4968.         }
  4969.     }
  4970.  
  4971.     memcpy ( ( void* ) &hwbp_t, ( void* ) ( Plugingetvalue ( VAL_HINST ) +0xD8D70 ), 4 * sizeof ( t_hardbpoint ) );
  4972.  
  4973.     n = i = 0;
  4974.     while ( n < 4 )
  4975.     {
  4976.         if ( hwbp_t[n].addr )
  4977.             i++;
  4978.         n++;
  4979.     }
  4980.     variables["$RESULT_1"] =  ( DWORD ) i;
  4981.  
  4982.     return true;
  4983. }
  4984.  
  4985. bool OllyLang::DoSCMP(string args)
  4986. {
  4987.     string ops[3];
  4988.     ulong size=0;
  4989.     string s1, s2;
  4990.  
  4991.     if(!CreateOperands(args, ops, 3)) {
  4992.         if(!CreateOperands(args, ops, 2))
  4993.             return false;
  4994.     } else {
  4995.         GetDWOpValue(ops[2], size);
  4996.     }
  4997.  
  4998.     if(GetSTROpValue(ops[0], s1, size)
  4999.         && GetSTROpValue(ops[1], s2, size))
  5000.     {
  5001.         int res = s1.compare(s2);
  5002.         if(res == 0)   
  5003.         {
  5004.             zf = 1;
  5005.             cf = 0;
  5006.         }
  5007.         else if(res > 0)
  5008.         {
  5009.             zf = 0;
  5010.             cf = 0;
  5011.         }
  5012.         else //if(res < 0)
  5013.         {
  5014.             zf = 0;
  5015.             cf = 1;
  5016.         }
  5017.         return true;
  5018.     }
  5019.     return false;
  5020. }
  5021.  
  5022. bool OllyLang::DoSCMPI(string args)
  5023. {
  5024.     string ops[3];
  5025.     ulong size=0;
  5026.     string s1, s2;
  5027.  
  5028.     if(!CreateOperands(args, ops, 3)) {
  5029.         if(!CreateOperands(args, ops, 2))
  5030.             return false;
  5031.     } else {
  5032.         GetDWOpValue(ops[2], size);
  5033.     }
  5034.  
  5035.     if(GetSTROpValue(ops[0], s1, size)
  5036.         && GetSTROpValue(ops[1], s2, size) )
  5037.     {
  5038.         int res = stricmp (s1.c_str(), s2.c_str());//s1.compare(s2,false);
  5039.         if(res == 0)       
  5040.         {
  5041.             zf = 1;
  5042.             cf = 0;
  5043.         }
  5044.         else if(res > 0)
  5045.         {
  5046.             zf = 0;
  5047.             cf = 0;
  5048.         }
  5049.         else //if(res < 0)
  5050.         {
  5051.             zf = 0;
  5052.             cf = 1;
  5053.         }
  5054.         return true;
  5055.     }
  5056.     return false;
  5057. }
  5058.  
  5059. bool OllyLang::DoSETOPTION(string args)
  5060. {
  5061.     Sendshortcut(PM_MAIN,0,WM_SYSKEYDOWN,0,0,'O');
  5062.     require_ollyloop = 1;
  5063.     return true;
  5064. }
  5065.  
  5066. bool OllyLang::DoSHL(string args)
  5067. {
  5068.     string ops[2];
  5069.  
  5070.     if(!CreateOperands(args, ops, 2))
  5071.         return false;
  5072.  
  5073.     ulong dw1, dw2;
  5074.     if(GetDWOpValue(ops[0], dw1)
  5075.         && GetDWOpValue(ops[1], dw2))
  5076.     {
  5077.         args = ops[0] + ", 0" + ultoa(dw1 << dw2, buffer, 16);
  5078.         nIgnoreNextValuesHist++;
  5079.         return DoMOV(args);
  5080.     }
  5081.     return false;
  5082. }
  5083.  
  5084. bool OllyLang::DoSHR(string args)
  5085. {
  5086.     string ops[2];
  5087.  
  5088.     if(!CreateOperands(args, ops, 2))
  5089.         return false;
  5090.  
  5091.     ulong dw1, dw2;
  5092.     if(GetDWOpValue(ops[0], dw1)
  5093.         && GetDWOpValue(ops[1], dw2))
  5094.     {
  5095.         args = ops[0] + ", 0" + ultoa(dw1 >> dw2, buffer, 16);
  5096.         nIgnoreNextValuesHist++;
  5097.         return DoMOV(args);
  5098.     }
  5099.     return false;
  5100. }
  5101.  
  5102. bool OllyLang::DoSTI(string args)
  5103. {
  5104.     Go(Getcputhreadid(), 0, STEP_IN, 0, 1);
  5105.     require_ollyloop = 1;
  5106.     return true;
  5107. }
  5108.  
  5109. bool OllyLang::DoSTO(string args)
  5110. {
  5111.     Go(Getcputhreadid(), 0, STEP_OVER, 0, 1);
  5112.     require_ollyloop = 1;
  5113.     return true;
  5114. }
  5115.  
  5116. bool OllyLang::DoSTR(string args)
  5117. {
  5118.     string op[1];
  5119.  
  5120.     if(!CreateOp(args, op, 1))
  5121.         return false;
  5122.  
  5123.     if (is_variable(op[0])) {
  5124.         if (variables[op[0]].vt == STR) {
  5125.  
  5126.             if (variables[op[0]].isbuf)
  5127.                 variables[op[0]] = variables[op[0]].strbuff();
  5128.  
  5129.             return true;
  5130.  
  5131.         } else if (variables[op[0]].vt == DW) {
  5132.  
  5133.             var v; v="##";
  5134.             v+=variables[op[0]].dw;
  5135.             variables[op[0]]=v.strbuff();
  5136.  
  5137.             return true;
  5138.         }
  5139.     }
  5140.     return false;
  5141. }
  5142.  
  5143. bool OllyLang::DoSUB(string args)
  5144. {
  5145.     string ops[2];
  5146.  
  5147.     if(!CreateOperands(args, ops, 2))
  5148.         return false;
  5149.  
  5150.     ulong dw1, dw2;
  5151.     if(GetDWOpValue(ops[0], dw1)
  5152.         && GetDWOpValue(ops[1], dw2))
  5153.     {
  5154.         args = ops[0] + ", 0" + ultoa(dw1 - dw2, buffer, 16);
  5155.         nIgnoreNextValuesHist++;
  5156.         return DoMOV(args);
  5157.     }
  5158.     return false;
  5159. }
  5160.  
  5161. bool OllyLang::DoTC(string args)
  5162. {
  5163.     Deleteruntrace();
  5164.     return true;
  5165. }
  5166.  
  5167.  
  5168. bool OllyLang::DoTEST(string args)
  5169. {
  5170.     string ops[2];
  5171.  
  5172.     if(!CreateOperands(args, ops, 2))
  5173.         return false;
  5174.  
  5175.     ulong dw1, dw2;
  5176.  
  5177.     if(GetDWOpValue(ops[0], dw1) && GetDWOpValue(ops[1], dw2))
  5178.     {
  5179.         zf = 0;
  5180.  
  5181.         if (!(dw1 & dw2)) {
  5182.             zf = 1;
  5183.         }
  5184.         setProgLineValue(script_pos+1,zf);
  5185.     }
  5186.     return true;
  5187.  
  5188. }
  5189.  
  5190.  
  5191. bool OllyLang::DoTI(string args)
  5192. {
  5193.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 1, 0, VK_F11);
  5194.     require_ollyloop = 1;
  5195.     return true;
  5196. }
  5197.  
  5198. bool OllyLang::DoTICK(string args)
  5199. {
  5200.     string ops[2];
  5201.     if(!CreateOperands(args, ops, 2))
  5202.         if(args=="" || !CreateOperands(args, ops, 1)) {
  5203.             char tmpbuf[256]={0};
  5204.             sprintf(tmpbuf,"%u ms",this->tickcount/1000);
  5205.             string s; s.assign(tmpbuf);
  5206.             variables["$RESULT"]=s;
  5207.             return true;
  5208.         }
  5209.  
  5210.     if (!is_variable(ops[0])) {
  5211.         DoVAR(ops[0]);
  5212.     }
  5213.  
  5214.     ulong timeref;
  5215.     GetDWOpValue(ops[1], timeref);
  5216.  
  5217.     if (is_variable(ops[0])) {
  5218.         variables[ops[0]] = tickcount;
  5219.         if (timeref)
  5220.             variables["$RESULT"]=tickcount-timeref;
  5221.         return true;
  5222.     }
  5223.     return false;
  5224. }
  5225.  
  5226. bool OllyLang::DoTICND(string args)
  5227. {
  5228.     string ops[1];
  5229.     string condition;
  5230.  
  5231.     if(!CreateOperands(args, ops, 1))
  5232.         return false;
  5233.  
  5234.     if(GetSTROpValue(ops[0], condition))
  5235.     {
  5236.         char* buffer = new char[condition.length() + 1];
  5237.         strcpy(buffer, condition.c_str());
  5238.         if(Runtracesize() == 0)
  5239.         {
  5240.             ulong threadid = Getcputhreadid();
  5241.             if(threadid == 0)
  5242.                 threadid = Plugingetvalue(VAL_MAINTHREADID);
  5243.             t_thread* pthr = Findthread(threadid);
  5244.             if(pthr != NULL)
  5245.                 Startruntrace(&(pthr->reg));
  5246.         }
  5247.         Settracecondition(buffer, 0, 0, 0, 0, 0);
  5248.         Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 1, 0, VK_F11);
  5249.         require_ollyloop = 1;
  5250.         return true;
  5251.     }
  5252.     return false;
  5253. }
  5254.  
  5255. bool OllyLang::DoTO(string args)
  5256. {
  5257.     Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 1, 0, VK_F12);
  5258.     require_ollyloop = 1;
  5259.     return true;
  5260. }
  5261.  
  5262. bool OllyLang::DoTOCND(string args)
  5263. {
  5264.     string ops[1];
  5265.     string condition;
  5266.  
  5267.     if(!CreateOperands(args, ops, 1))
  5268.         return false;
  5269.  
  5270.     if(GetSTROpValue(ops[0], condition))
  5271.     {
  5272.         char* buffer = new char[condition.length() + 1];
  5273.         strcpy(buffer, condition.c_str());
  5274.         if(Runtracesize() == 0)
  5275.         {
  5276.             ulong threadid = Getcputhreadid();
  5277.             if(threadid == 0)
  5278.                 threadid = Plugingetvalue(VAL_MAINTHREADID);
  5279.             t_thread* pthr = Findthread(threadid);
  5280.             if(pthr != NULL)
  5281.                 Startruntrace(&(pthr->reg));
  5282.         }
  5283.         Settracecondition(buffer, 0, 0, 0, 0, 0);
  5284.         Sendshortcut(PM_MAIN, 0, WM_KEYDOWN, 1, 0, VK_F12);
  5285.         require_ollyloop = 1;
  5286.         return true;
  5287.     }
  5288.     return false;
  5289. }
  5290.  
  5291.  
  5292. bool OllyLang::DoUNICODE(string args)
  5293. {
  5294.     string ops[1];
  5295.     ulong dw;
  5296.  
  5297.     if(!CreateOperands(args, ops, 1))
  5298.         return false;
  5299.  
  5300.     if(GetDWOpValue(ops[0], dw)) {
  5301.         bUnicode=(dw!=0);
  5302.         return true;
  5303.     }
  5304.  
  5305.     return false;
  5306. }
  5307.  
  5308. bool OllyLang::DoVAR(string args)
  5309. {
  5310.     string ops[1];
  5311.  
  5312.     if(!CreateOperands(args, ops, 1))
  5313.         return false;
  5314.  
  5315.     if(reg_names.find(ops[0]) == reg_names.end()){
  5316.         variables.insert(pair<string, var> (ops[0], 0));
  5317.         return true;
  5318.     }
  5319.     errorstr = "Bad variable name: " + ops[0];
  5320.     return false;
  5321. }
  5322.  
  5323. bool OllyLang::DoXCHG(string args)
  5324. {
  5325.     string ops[2];
  5326.  
  5327.     if (!CreateOperands(args, ops, 2))
  5328.         return false;
  5329.  
  5330.     ulong dw1, dw2;
  5331.  
  5332.     if (GetDWOpValue(ops[0], dw1) && GetDWOpValue(ops[1], dw2)) {
  5333.         args = ops[0] + ", 0" + ultoa(dw2, buffer, 16);
  5334.         nIgnoreNextValuesHist++;
  5335.         DoMOV(args);
  5336.         args = ops[1] + ", 0" + ultoa(dw1, buffer, 16);
  5337.         nIgnoreNextValuesHist++;
  5338.         DoMOV(args);
  5339.         return true;
  5340.     }
  5341.     return false;
  5342.  
  5343. }
  5344.  
  5345.  
  5346. bool OllyLang::DoXOR(string args)
  5347. {
  5348.     string ops[2];
  5349.  
  5350.     if(!CreateOperands(args, ops, 2))
  5351.         return false;
  5352.  
  5353.     ulong dw1, dw2;
  5354.     if(GetDWOpValue(ops[0], dw1)
  5355.         && GetDWOpValue(ops[1], dw2))
  5356.     {
  5357.         args = ops[0] + ", 0" + ultoa(dw1 ^ dw2, buffer, 16);
  5358.         nIgnoreNextValuesHist++;
  5359.         return DoMOV(args);
  5360.     }
  5361.     return false;
  5362. }
  5363.  
  5364. bool OllyLang::DoWRT(string args)
  5365. {
  5366.     string ops[2];
  5367.  
  5368.     if(!CreateOperands(args, ops, 2))
  5369.         return false;
  5370.  
  5371.     string path;
  5372.     path.assign((char*)Plugingetvalue(VAL_EXEFILENAME));
  5373.  
  5374.     path = path.substr(0, path.rfind('\\') + 1);
  5375.  
  5376.     string filename,data;
  5377.  
  5378.     if(GetSTROpValue(ops[0], filename)
  5379.         && GetANYOpValue(ops[1], data))
  5380.     {
  5381.         if ((filename.find(":\\") != string::npos) || (filename.find("\\\\") != string::npos)) //hard disk or network
  5382.           path = filename;
  5383.         else
  5384.           path += filename;
  5385.        
  5386.         HANDLE hFile;
  5387.         ulong dwAccBytes=0;
  5388.  
  5389.         hFile = CreateFile(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  5390.         if(hFile != INVALID_HANDLE_VALUE)
  5391.         {
  5392.             //SetFilePointer(hFile, 0, 0, FILE_BEGIN);
  5393. //          if (bUnicode) {
  5394. //              if (data.length()>1 && data[1]!=0)
  5395. //                  data=Str2Unicode(data);
  5396. //              WriteFile(hFile, data.c_str(), data.length(), &dwAccBytes, NULL);
  5397. //          } else
  5398.                 WriteFile(hFile, data.c_str(), data.length(), &dwAccBytes, NULL);
  5399.             CloseHandle(hFile);
  5400.         }
  5401.         return dwAccBytes;
  5402.     }
  5403.     return false;
  5404. }
  5405.  
  5406. bool OllyLang::DoWRTA(string args)
  5407. {
  5408.     string ops[3];
  5409.  
  5410.     if(!CreateOperands(args, ops, 3)) {
  5411.         ops[2]="\"\r\n\"";
  5412.         if(!CreateOperands(args, ops, 2))
  5413.             return false;
  5414.     }
  5415.    
  5416.     string path;
  5417.     path.assign((char*)Plugingetvalue(VAL_EXEFILENAME));
  5418.  
  5419.     path = path.substr(0, path.rfind('\\') + 1);
  5420.  
  5421.     string filename,data,sSep;
  5422.  
  5423.     if(GetSTROpValue(ops[0], filename)
  5424.         && GetANYOpValue(ops[1], data))
  5425.     {
  5426.         if ((filename.find(":\\") != string::npos) || (filename.find("\\\\") != string::npos))
  5427.           path = filename;
  5428.         else
  5429.           path += filename;
  5430.        
  5431.         HANDLE hFile;
  5432.         ulong dwAccBytes=0;
  5433.  
  5434.         if (!GetSTROpValue(ops[2],sSep))
  5435.             return false;  
  5436.  
  5437.         data=sSep+data;
  5438.  
  5439.         hFile = CreateFile(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  5440.         if(hFile != INVALID_HANDLE_VALUE)
  5441.         {
  5442.             SetFilePointer(hFile, 0, 0, FILE_END);
  5443. //          if (bUnicode) {
  5444. //              if (data.length()>1 && data[1]!=0)
  5445. //                  data=Str2Unicode(data);
  5446. //              WriteFile(hFile, data.c_str(), data.length(), &dwAccBytes, NULL);
  5447. //          } else
  5448.                 WriteFile(hFile, data.c_str(), data.length(), &dwAccBytes, NULL);
  5449.  
  5450.             CloseHandle(hFile);
  5451.         }
  5452.         return dwAccBytes;
  5453.     }
  5454.     return false;
  5455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement