diff --git a/Makefile b/Makefile index 4a72feb..5dd762a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ all: go test + +ex: cd example ; go run example.go sync: diff --git a/api.go b/api.go new file mode 100644 index 0000000..e0d4015 --- /dev/null +++ b/api.go @@ -0,0 +1,48 @@ +// radare - LGPL - Copyright 2021 - pancake + +package r2pipe + +// #cgo CFLAGS: -I/usr/local/include/libr +// #cgo CFLAGS: -I/usr/local/include/libr/sdb +// #cgo LDFLAGS: -L/usr/local/lib -lr_core +// #include +// #include +// extern void r_core_free(void *); +// extern void *r_core_new(void); +// extern char *r_core_cmd_str(void*, const char *); +// +import "C" + +import ( + "unsafe" +) + + +func (r2p *Pipe) ApiCmd(cmd string) (string, error) { + res := C.r_core_cmd_str(r2p.Core, C.CString(cmd)) + return C.GoString(res), nil +} + +func (r2p *Pipe) ApiClose() error { + C.r_core_free(unsafe.Pointer(r2p.Core)) + r2p.Core = nil + return nil +} + +func NewApiPipe(file string) (*Pipe, error) { + r2 := C.r_core_new() + r2p := &Pipe{ + File: file, + Core: r2, + cmd: func(r2p *Pipe, cmd string) (string, error) { + return r2p.ApiCmd(cmd) + }, + close: func(r2p *Pipe) error { + return r2p.ApiClose() + }, + } + if file != "" { + r2p.ApiCmd("o " + file) + } + return r2p, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..eaac975 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/radareorg/r2pipe-go + +go 1.13 diff --git a/native.go b/native.go index ef81029..4cf7be1 100644 --- a/native.go +++ b/native.go @@ -2,22 +2,23 @@ package r2pipe +//#cgo linux LDFLAGS: -ldl //#include //#include // #include -// void *r_core_new(void *f) { +// void *gor_core_new(void *f) { // void *(*rcn)(); // rcn = (void *(*)())f; // return rcn(); // } // -// void r_core_free(void *f, void *arg) { +// void gor_core_free(void *f, void *arg) { // void (*fr)(void *); // fr = (void (*)(void *))f; // fr(arg); // } // -// char *r_core_cmd_str(void *f, void *arg, char *arg2) { +// char *gor_core_cmd_str(void *f, void *arg, char *arg2) { // char *(*cmdstr)(void *, char *); // cmdstr = (char *(*)(void *, char *))f; // return cmdstr(arg, arg2); @@ -25,12 +26,14 @@ package r2pipe import "C" import ( + "fmt" "errors" "runtime" "unsafe" ) -type Ptr = *struct{} +type Ptr = unsafe.Pointer +// *struct{} var ( lib Ptr = nil @@ -63,7 +66,7 @@ func dlOpen(path string) (*DL, error) { ret.name = path C.free(unsafe.Pointer(cpath)) if ret.handle == nil { - return nil, errors.New("Failed to open dl") + return nil, errors.New(fmt.Sprintf("Failed to open %s", path)) } return &ret, nil } @@ -92,17 +95,17 @@ func NativeLoad() error { } handle, _ := dlSym(lib, "r_core_new") r_core_new = func() Ptr { - a := (Ptr)(C.r_core_new(handle)) + a := (Ptr)(C.gor_core_new(handle)) return a } handle, _ = dlSym(lib, "r_core_free") r_core_free = func(p Ptr) { - C.r_core_free(handle, unsafe.Pointer(p)) + C.gor_core_free(handle, unsafe.Pointer(p)) } handle, _ = dlSym(lib, "r_core_cmd_str") r_core_cmd_str = func(p Ptr, s string) string { a := C.CString(s) - b := C.r_core_cmd_str(handle, unsafe.Pointer(p), a) + b := C.gor_core_cmd_str(handle, unsafe.Pointer(p), a) C.free(unsafe.Pointer(a)) return C.GoString(b) } diff --git a/r2pipe.go b/r2pipe.go index fcc533d..03b4206 100644 --- a/r2pipe.go +++ b/r2pipe.go @@ -34,6 +34,7 @@ package r2pipe import ( "bufio" + "unsafe" "bytes" "encoding/json" "fmt" @@ -51,7 +52,7 @@ type Pipe struct { r2cmd *exec.Cmd stdin io.WriteCloser stdout io.ReadCloser - Core *struct{} + Core unsafe.Pointer cmd CmdDelegate close CloseDelegate }