-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
89 lines (78 loc) · 1.6 KB
/
pipe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os/exec"
"strings"
"sync"
)
type Rizin struct {
pipe *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
mutex sync.Mutex
project string
}
func RizinInfo(rizinbin string) ([]string, error) {
out, err := exec.Command(rizinbin, "-version").Output()
if err != nil {
return nil, err
}
return strings.Split(string(out), "\n"), nil
}
func NewRizin(rizinbin, file, project string) *Rizin {
args := []string{
"-2",
"-0",
"-e",
"scr.color=3",
"-p",
project,
file,
}
pipe := exec.Command(rizinbin, args...)
stdin, err := pipe.StdinPipe()
if err != nil {
fmt.Println("pipe error:", err)
return nil
}
stdout, err := pipe.StdoutPipe()
if err != nil {
fmt.Println("pipe error:", err)
return nil
}
if err := pipe.Start(); err != nil {
fmt.Println("pipe error:", err)
return nil
}
rizin := &Rizin{pipe: pipe, stdin: stdin, stdout: stdout, project: project}
go func(r *Rizin) {
r.mutex.Lock()
if _, err := bufio.NewReader(r.stdout).ReadString('\x00'); err != nil {
fmt.Println("pipe error:", err)
}
r.mutex.Unlock()
}(rizin)
return rizin
}
func (r *Rizin) exec(cmd string) (string, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
if _, err := fmt.Fprintln(r.stdin, cmd); err != nil {
fmt.Println("pipe error:", err)
return "", err
}
buf, err := bufio.NewReader(r.stdout).ReadString('\x00')
if err != nil && err != io.EOF {
fmt.Println("pipe error:", err)
return "", err
}
buf = string(bytes.Trim([]byte(buf), "\x00"))
return buf, nil
}
func (r *Rizin) close() {
r.exec("Ps " + r.project)
r.exec("q!")
}