diff --git a/libr/lang/p/qjs.c b/libr/lang/p/qjs.c index 5a12fe1ecd..9155faa41d 100644 --- a/libr/lang/p/qjs.c +++ b/libr/lang/p/qjs.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2020-2022 pancake */ +/* radare - LGPL - Copyright 2020-2023 pancake */ #include #include @@ -153,6 +153,10 @@ static JSValue b64(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst } static int r2plugin_core_call2(QjsContext *qc, RCore *core, const char *input) { + // ceprintf ("CALL2\n"); + if (!qc || !qc->ctx) { + return 0; + } JSValueConst args[1] = { JS_NewString (qc->ctx, input) }; @@ -249,14 +253,12 @@ static JSValue r2plugin_core(JSContext *ctx, JSValueConst this_val, int argc, JS // return JS_ThrowRangeError (ctx, "r2.plugin core already registered (only one exists)"); return JS_NewBool (ctx, false); } - eprintf ("ADDING LE PLUGIN BECAUSE WE DDIND FIND IT THE FUNC\n"); if (Gplug >= MAXPLUGS) { R_LOG_WARN ("Maximum number of plugins loaded! this is a limitation induced by the"); return JS_NewBool (ctx, false); } qc = qjsctx_add (core, nameptr, ctx, func); ap->call = Gcalls[Gplug]; - eprintf ("%s = %p\n", ap->name, qc); GcallsData[Gplug] = qc; Gplug++; diff --git a/shlr/qjs/js_r2papi.c b/shlr/qjs/js_r2papi.c index 43f6d1bfb0..6c42ea9afd 100644 --- a/shlr/qjs/js_r2papi.c +++ b/shlr/qjs/js_r2papi.c @@ -1,16 +1,16 @@ static const char *const js_r2papi_qjs = "" \ "Object.defineProperty(G,\"__esModule\",{value:!0}),G.Base64=G.N"\ "ativePointer=G.R2Papi=void 0;const shell_js_1=G;G.R2Papi=clas"\ - "s{constructor(t){this.r2=t}jsonToTypescript(t,s){let e=`inter"\ + "s{constructor(t){this.r2=t}jsonToTypescript(t,s){let i=`inter"\ "face ${t} {\n`;s.length&&0t.toEsil()).join(\",\")}optimizeFlags"\ - "(t){void 0!==t.rhs&&this.optimizeFlags(t.rhs),void 0!==t.lhs&"\ - "&this.optimizeFlags(t.lhs);for(let s=0;st.toString()).join(\";\\n\")}reset(){this"\ - ".nodes=[],this.stack=[],this.tokens=[],this.cur=0,this.root=n"\ - "ew EsilNode(new EsilToken(\"function\",0),\"block\")}parseRange(t"\ - ",s){let e=t;for(;et.trim()),i=th"\ - "is.tokens.length;for(let t of e){const e=new EsilToken(t,this"\ - ".tokens.length);void 0!==s&&(e.addr=s),this.tokens.push(e)}t="\ - "this.tokens.length;this.parseRange(i,t)}peek(t){return this.t"\ - "okens[t]}pushToken(t){if(this.isNumber(t)){var s=new EsilNode"\ - "(t,\"number\");this.stack.push(s),this.nodes.push(s)}else if(th"\ - "is.isInternal(t)){const s=new EsilNode(t,\"flag\");this.stack.p"\ - "ush(s),this.nodes.push(s)}else if(!this.isOperation(t)){const"\ - " s=new EsilNode(t,\"register\");this.stack.push(s),this.nodes.p"\ - "ush(s)}}isNumber(t){return!!t.toString().startsWith(\"0\")||0<+"\ - "t}isInternal(t){t=t.toString();return t.startsWith(\"$\")&&1i.push(t));e\":case\"^\":case\"&\":case\"|\":case\"+"\ - "\":case\"*\":case\"/\":case\">>=\":case\"<<=\":case\">>>=\":case\"<<<=\":c"\ - "ase\">>>>=\":case\"<<<<=\":if(!(2<=this.stack.length))throw new E"\ - "rror(\"Stack needs more items\");{const s=this.stack.pop(),e=th"\ - "is.stack.pop(),i=new EsilNode(t,\"operation\");i.setSides(s,e),"\ - "this.stack.length,this.stack.push(i),this.nodes.push(i)}retur"\ - "n!0;case\"=\":case\":=\":case\"-=\":case\"+=\":case\"==\":case\"=[1]\":ca"\ - "se\"=[2]\":case\"=[4]\":case\"=[8]\":if(!(2<=this.stack.length))thr"\ - "ow new Error(\"Stack needs more items\");{const s=this.stack.po"\ - "p(),e=this.stack.pop(),i=new EsilNode(t,\"operation\");i.setSid"\ - "es(s,e),0===this.stack.length&&this.root.children.push(i),thi"\ - "s.nodes.push(i)}return!0}return!1}};\n"; + "id 0===this.rhs)return\"\";{let t=this.lhs.toEsil();return\"\"!=="\ + "t&&(t+=\",\"),this.rhs.toEsil()+\",\"+t+this.token}}toString(){le"\ + "t t=\"\";if(\"\"!==this.token.label&&(t+=this.token.label+\":\\n\"),"\ + "this.token.addr,\"\"!==this.token.comment&&(t+=\"/*\"+this.token."\ + "comment+\"*/\\n\"),\"GOTO\"===this.token.toString()&&(0t.toEsil()).join(\",\")}optimizeFlags(t){void 0!==t"\ + ".rhs&&this.optimizeFlags(t.rhs),void 0!==t.lhs&&this.optimize"\ + "Flags(t.lhs);for(let s=0;st.toString()).join(\";\\n\")}reset(){this.nodes=[],this"\ + ".stack=[],this.tokens=[],this.cur=0,this.root=new EsilNode(ne"\ + "w EsilToken(\"function\",0),\"block\")}parseRange(t,s){let i=t;fo"\ + "r(;it."\ + "trim()),e=this.tokens.length;for(let t of i){const i=new Esil"\ + "Token(t,this.tokens.length);void 0!==s&&(i.addr=s),this.token"\ + "s.push(i)}t=this.tokens.length;this.parseRange(e,t)}peek(t){r"\ + "eturn this.tokens[t]}pushToken(t){if(this.isNumber(t)){var s="\ + "new EsilNode(t,\"number\");this.stack.push(s),this.nodes.push(s"\ + ")}else if(this.isInternal(t)){const s=new EsilNode(t,\"flag\");"\ + "this.stack.push(s),this.nodes.push(s)}else if(!this.isOperati"\ + "on(t)){const s=new EsilNode(t,\"register\");this.stack.push(s),"\ + "this.nodes.push(s)}}isNumber(t){return!!t.toString().startsWi"\ + "th(\"0\")||0<+t}isInternal(t){t=t.toString();return t.startsWit"\ + "h(\"$\")&&1e.push(t));i\":case\"^\":case\"&\":ca"\ + "se\"|\":case\"+\":case\"*\":case\"/\":case\">>=\":case\"<<=\":case\">>>=\":"\ + "case\"<<<=\":case\">>>>=\":case\"<<<<=\":if(!(2<=this.stack.length)"\ + ")throw new Error(\"Stack needs more items\");{const s=this.stac"\ + "k.pop(),i=this.stack.pop(),e=new EsilNode(t,\"operation\");e.se"\ + "tSides(s,i),this.stack.length,this.stack.push(e),this.nodes.p"\ + "ush(e)}return!0;case\"=\":case\":=\":case\"-=\":case\"+=\":case\"==\":c"\ + "ase\"=[1]\":case\"=[2]\":case\"=[4]\":case\"=[8]\":if(!(2<=this.stack"\ + ".length))throw new Error(\"Stack needs more items\");{const s=t"\ + "his.stack.pop(),i=this.stack.pop(),e=new EsilNode(t,\"operatio"\ + "n\");e.setSides(s,i),0===this.stack.length&&this.root.children"\ + ".push(e),this.nodes.push(e)}return!0}return!1}};\n"; diff --git a/shlr/qjs/js_r2papi.qjs b/shlr/qjs/js_r2papi.qjs index 7def9b2b5c..d2f58d107b 100644 --- a/shlr/qjs/js_r2papi.qjs +++ b/shlr/qjs/js_r2papi.qjs @@ -576,9 +576,10 @@ class EsilNode { if (left !== "") { left += ","; } - return `${this.rhs.toEsil()},${left}${this.token}`; + let right = this.rhs.toEsil(); + return `${right},${left}${this.token}`; } - return this.token.text; + return ''; // this.token.text; } toString() { let str = ""; @@ -695,6 +696,42 @@ class EsilParser { } // console.log("done"); } + parseFunction(addr) { + var ep = this; + function parseAmount(n) { + // console.log("PDQ "+n); + const lines = r2.cmd("pie " + n + " @e:scr.color=0").trim().split("\n"); + for (const line of lines) { + if (line.length === 0) { + console.log("Empty"); + continue; + } + // console.log("parse", r2.cmd("?v:$$")); + const kv = line.split(' '); + if (kv.length > 1) { // line != "") { + // console.log("// @ " + kv[0]); + //ep.reset (); + r2.cmd(`s ${kv[0]}`); + ep.parse(kv[1], kv[0]); + ep.optimize("flags,labels"); + //console.log(ep.toString()); + } + } + // console.log(ep.toString()); + } + const oaddr = r2.cmd("?v $$").trim(); + // const func = r2.cmdj("pdrj"); // XXX this command changes the current seek + if (addr === undefined) { + addr = oaddr; + } + const bbs = r2.cmdj(`afbj@${addr}`); // XXX this command changes the current seek + for (let bb of bbs) { + // console.log("bb_" + bb.addr + ":"); + r2.cmd(`s ${bb.addr}`); + parseAmount(bb.ninstr); + } + r2.cmd(`s ${oaddr}`); + } parse(expr, addr) { const tokens = expr.trim().split(',').map((x) => x.trim()); const from = this.tokens.length; diff --git a/shlr/qjs/package.json b/shlr/qjs/package.json index d34a3c4578..7f54ab293f 100644 --- a/shlr/qjs/package.json +++ b/shlr/qjs/package.json @@ -1,5 +1,5 @@ { "dependencies": { - "r2papi": "^0.0.22" + "r2papi": "^0.0.23" } }