mirror of
https://github.com/radareorg/r2pipe-go.git
synced 2026-11-07 06:25:58 -10:00
Implement the API backend and minor tweaks for the native one
This commit is contained in:
1 parent
7e955ca2e2
commit
d79271d656
5 files changed
+66
-9
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user