Implement the API backend and minor tweaks for the native one

This commit is contained in:
pancake authored and pancake committed 2021-11-23 12:12:48 +01:00
1 parent 7e955ca2e2
commit d79271d656
5 files changed
+66 -9

No files matched your search

+2
View File
@@ -1,5 +1,7 @@
all:
go test
ex:
cd example ; go run example.go
sync:
+48
View File
@@ -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 <stdio.h>
// #include <stdlib.h>
// 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
}
+3
View File
@@ -0,0 +1,3 @@
module github.com/radareorg/r2pipe-go
go 1.13
+11 -8
View File
@@ -2,22 +2,23 @@
package r2pipe
//#cgo linux LDFLAGS: -ldl
//#include <stdio.h>
//#include <dlfcn.h>
// #include <stdlib.h>
// 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)
}
+2 -1
View File
@@ -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
}