Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split the code in index.ts into separate files #28

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions typescript/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
TSC=node_modules/.bin/tsc

all: node_modules
$(TSC) --declaration index.ts
$(TSC) --declaration r2papi.ts
$(TSC) -m node16 --target es2020 shell.ts && cp -f shell.js shell.r2.js
$(TSC) -m node16 --target es2020 esil.ts && cp -f esil.js esil.r2.js
$(TSC) -m node16 --target es2020 opt.ts && cp -f opt.js opt.r2.js
$(TSC) -m node16 --target es2020 index.ts && cp -f index.js index.r2.js
$(TSC) -m node16 --target es2020 r2papi.ts && cp -f r2papi.js index.r2.js

pdq: node_modules
$(TSC) -m node16 --target es2020 pdq.ts
Expand All @@ -21,7 +21,7 @@ uninstall user-uninstall:
rm -f $(PLUGDIR)/pdq.qjs

one:
cat shell.r2.js esil.r2.js index.r2.js pdq.r2.js |grep -v require > one.r2.js
cat shell.r2.js esil.r2.js base64.r2.js r2pipe.r2.js ai.r2.js r2papi.r2.js pdq.r2.js |grep -v require > one.r2.js
R2_DEBUG_NOPAPI=1 r2 -i one.r2.js /bin/ls

test:
Expand Down
57 changes: 57 additions & 0 deletions typescript/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { R, Module, Process, Thread } from "./r2papi.js"
import { r2, R2Pipe } from "./r2pipe.js";

export class R2AI {
available : boolean = false;
model : string = "";
constructor (num?: number, model?: string) {
this.available = r2.cmd('r2ai -h').trim() !== "";
if (this.available) {
if (num) {
r2.call(`r2ai -n ${num}`)
}
// r2.call('r2ai -e DEBUG=1')
if (model) {
this.model = model;
}
} else {
console.error("ERROR: r2ai is not installed");
}
}
reset() {
if (this.available) {
r2.call('r2ai -R')
}
}
setRole(msg: string) {
if (this.available) {
r2.call(`r2ai -r ${msg}`)
}
}
setModel(modelName: string) {
if (this.available) {
r2.call(`r2ai -m ${this.model}`)
}
}
getModel() : string {
if (this.available) {
return r2.call("r2ai -m");
}
return this.model;
}
listModels() : string[] {
if (this.available) {
return r2.call("r2ai -M").trim().split(/\n/g);
}
return [];
}
query(msg: string) : string {
if (!this.available || msg == '') {
return '';
}
const fmsg = msg.trim().replace(/\n/g, '.');
return r2.call(`r2ai ${fmsg}`)
}
}


14 changes: 14 additions & 0 deletions typescript/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export class Base64 {
static encode(x: string) : string {
return b64(x);
}
static decode(x: string) : string {
return b64(x, true);
}
}

interface base64Interface {
(message: string, decode?: boolean): string;
}

export declare var b64: base64Interface;
2 changes: 1 addition & 1 deletion typescript/esil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { R2Pipe } from "./index";
import { R2Pipe } from "./r2pipe.js";

declare var console: any;
declare var r2: R2Pipe;
Expand Down
19 changes: 14 additions & 5 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"typescript": "5.0.4"
},
"scripts": {
"build": "tsc -m node16 --target es2020 --declaration index.ts esil.ts shell.ts",
"build": "tsc -m node16 --target es2020 --declaration r2pipe.ts base64.ts ai.ts r2papi.ts esil.ts shell.ts",
"test": "echo \"Error: no test specified\" && exit 1",
"doc": "typedoc index.ts shell.ts esil.ts"
"doc": "typedoc r2pipe.ts ai.ts r2papi.ts base64.ts shell.ts esil.ts"
},
"repository": {
"type": "git",
Expand All @@ -34,15 +34,24 @@
],
"files": [
"README.md",
"ai.js",
"ai.ts",
"ai.d.ts",
"base64.js",
"base64.ts",
"base64.d.js",
"r2pipe.js",
"r2pipe.ts",
"r2pipe.d.ts",
"esil.js",
"esil.ts",
"esil.d.ts",
"shell.js",
"shell.ts",
"shell.d.ts",
"index.js",
"index.ts",
"index.d.ts"
"r2papi.js",
"r2papi.ts",
"r2papi.d.ts"
],
"license": "MIT"
}
138 changes: 5 additions & 133 deletions typescript/index.ts → typescript/r2papi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// r2papi main file

// import { R, Module, Process, Thread } from "./global.js"
import { R2PapiShell } from "./shell.js";
import { r2, R2Pipe } from "./r2pipe.js";

export type InstructionType = "mov" | "jmp" | "cmp" | "nop" | "call" | "add" | "sub";
export type InstructionFamily = "cpu" | "fpu" | "priv";
Expand Down Expand Up @@ -138,64 +140,6 @@ export interface Instruction {
stack: string; // "inc"|"dec"|"get"|"set"|"nop"|"null";
}

/**
* Generic interface to interact with radare2, abstracts the access to the associated
* instance of the tool, which could be native via rlang or remote via pipes or tcp/http.
*
* @typedef R2Pipe
*/
export interface R2Pipe {
/**
* Run a command in the associated instance of radare2
*
* @param {string} command to be executed inside radare2.
* @returns {string} The output of the command execution
*/
cmd(cmd: string): string;
/**
* Run a radare2 command in a different address. Same as `.cmd(x + '@ ' + a)`
*
* @param {string} command to be executed inside radare2.
* @returns {string} The output of the command execution
*/
cmdAt(cmd: string): string;
/**
* Run a radare2 command expecting the output to be JSON
*
* @param {string} command to be executed inside radare2. The given command should end with `j`
* @returns {object} the JSON decoded object from the output of the command
*/
cmdj(cmd: string): any;
/**
* Call a radare2 command. This is similar to `R2Pipe.cmd`, but skips all the command parsing rules,
* which is safer and faster but you cannot use any special modifier like `@`, `~`, ...
*
* See R2Pipe.callAt() to call a command on a different address
*
* @param {string} command to be executed inside radare2. The given command should end with `j`
* @returns {object} the JSON decoded object from the output of the command
*/
call(cmd: string): string;
/**
* Call a radare2 command in a different address
*
* @param {string} command to be executed inside radare2. The given command should end with `j`
* @param {NativePointer|string|number} where to seek to execute this command (previous offset is restored after executing it)
* @returns {object} the JSON decoded object from the output of the command
*/
callAt(cmd: string, at: string|number|NativePointer): string;
callj(cmd: string): any;
/**
* Log a string to the associated console. This is used internally by `console.log` in some implementations.
*
* @param {string} text to be displayed
* @returns {boolean} true if successful
*/
log(msg: string): string;
plugin(type: string, maker: any): boolean;
unload(name: string): boolean;
}

export interface Radare2 {
version: string;
}
Expand Down Expand Up @@ -778,6 +722,7 @@ export class NativeCallback {

export class NativePointer {
addr: string;

api: R2Papi;
constructor(s: string | number, api?: R2Papi) {
if (api === undefined) {
Expand Down Expand Up @@ -866,10 +811,10 @@ export class NativePointer {
return this.readPointer().compare(0);
}
toJSON() : string {
return this.api.cmd('?vi ' + this.addr.trim());
return this.api.cmd('?vi ' + this.addr.trim()).trim();
}
toString() : string {
return this.api.cmd('?v ' + this.addr.trim());
return this.api.cmd('?v ' + this.addr.trim()).trim();
}
toNumber(): number {
return parseInt(this.toString());
Expand Down Expand Up @@ -1026,72 +971,6 @@ export class NativePointer {
}
}

export class Base64 {
static encode(x: string) : string {
return b64(x);
}
static decode(x: string) : string {
return b64(x, true);
}
}

interface base64Interface {
(message: string, decode?: boolean): string;
}

class R2AI {
available : boolean = false;
model : string = "";
constructor (num, model) {
this.available = r2.cmd('r2ai -h').trim() !== "";
if (this.available) {
if (num) {
r2.call(`r2ai -n ${num}`)
}
// r2.call('r2ai -e DEBUG=1')
if (model) {
this.model = model;
}
} else {
console.error("ERROR: r2ai is not installed");
}
}
reset() {
if (this.available) {
r2.call('r2ai -R')
}
}
setRole(msg) {
if (this.available) {
r2.call(`r2ai -r ${msg}`)
}
}
setModel(modelName) {
if (this.available) {
r2.call(`r2ai -m ${this.model}`)
}
}
getModel() {
if (this.available) {
return r2.call("r2ai -m");
}
return this.model;
}
listModels() {
if (this.available) {
return r2.call("r2ai -M").trim().split(/\n/g);
}
return [];
}
query(msg) {
if (!this.available || msg == '') {
return '';
}
const fmsg = msg.trim().replace(/\n/g, '.');
return r2.call(`r2ai ${fmsg}`)
}
}


/*
// already defined by r2
Expand All @@ -1100,13 +979,6 @@ function ptr(x: string|number) {
}
*/

export declare var b64: base64Interface;
/**
* A global instance of R2Pipe associated with the current instance of radare2
*
* @type {R2Pipe}
*/
export declare var r2: R2Pipe;
export declare var R: R2Papi;
export declare var Module: ModuleClass;
export declare var Process: ProcessClass;
Expand Down
Loading
Loading