-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
198 lines (158 loc) · 3.98 KB
/
client.go
File metadata and controls
198 lines (158 loc) · 3.98 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package rizingo
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
)
// A Pipe represents a communication interface with rizin that will be used to
// execute commands and obtain their results.
type Pipe struct {
File string
rzcmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
Core *struct{}
cmd CmdDelegate
close CloseDelegate
}
type (
CmdDelegate func(*Pipe, string) (string, error)
CloseDelegate func(*Pipe) error
)
// NewPipe returns a new rizin pipe and initializes an rizin core that will try to
// load the provided file or URI. If file is an empty string, the env vars
// RZPIPE_{IN,OUT} will be used as file descriptors for input and output, this
// is the case when rz-pipe is called within rizin.
func NewPipe(file string) (*Pipe, error) {
if file == "" {
return newPipeFd()
}
return newPipeCmd(file)
}
func newPipeFd() (*Pipe, error) {
rzpipeIn := os.Getenv("RZ_PIPE_IN")
rzpipeOut := os.Getenv("RZ_PIPE_OUT")
if rzpipeIn == "" || rzpipeOut == "" {
return nil, fmt.Errorf("missing RZ_PIPE_{IN,OUT} vars")
}
rzpipeInFd, err := strconv.Atoi(rzpipeIn)
if err != nil {
return nil, fmt.Errorf("failed to convert IN into file descriptor")
}
rzpipeOutFd, err := strconv.Atoi(rzpipeOut)
if err != nil {
return nil, fmt.Errorf("failed to convert OUT into file descriptor")
}
stdout := os.NewFile(uintptr(rzpipeInFd), "RZ_PIPE_IN")
stdin := os.NewFile(uintptr(rzpipeOutFd), "RZ_PIPE_OUT")
rzp := &Pipe{
File: "",
rzcmd: nil,
stdin: stdin,
stdout: stdout,
Core: nil,
}
return rzp, nil
}
func newPipeCmd(file string) (*Pipe, error) {
rzcmd := exec.Command("rizin", "-q0", file)
stdin, err := rzcmd.StdinPipe()
if err != nil {
return nil, err
}
stdout, err := rzcmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := rzcmd.Start(); err != nil {
return nil, err
}
// Read initial data
if _, err := bufio.NewReader(stdout).ReadString('\x00'); err != nil {
return nil, err
}
rzp := &Pipe{
File: file,
rzcmd: rzcmd,
stdin: stdin,
stdout: stdout,
Core: nil,
}
return rzp, nil
}
// Write implements the standard Write interface: it writes data to the rizin
// pipe, blocking until rizin have consumed all the data.
func (rzp *Pipe) Write(p []byte) (n int, err error) {
return rzp.stdin.Write(p)
}
// Read implements the standard Read interface: it reads data from the rizin
// pipe, blocking until the previously issued commands have finished.
func (rzp *Pipe) Read(p []byte) (n int, err error) {
return rzp.stdout.Read(p)
}
// Cmd is a helper that allows to run rizin commands and receive their output.
func (rzp *Pipe) Cmd(cmd string) (string, error) {
if rzp.Core != nil {
if rzp.cmd != nil {
return rzp.cmd(rzp, cmd)
}
return "", nil
}
if _, err := fmt.Fprintln(rzp, cmd); err != nil {
return "", err
}
buf, err := bufio.NewReader(rzp).ReadString('\x00')
if err != nil {
return "", err
}
return strings.TrimRight(buf, "\n\x00"), nil
}
// Cmdj acts like Cmd but interprets the output of the command as json. It
// returns the parsed json keys and values.
func (rzp *Pipe) Cmdj(cmd string) (json.RawMessage, error) {
if _, err := fmt.Fprintln(rzp, cmd); err != nil {
return nil, err
}
buf, err := bufio.NewReader(rzp).ReadBytes('\x00')
if err != nil {
return nil, err
}
buf = bytes.TrimRight(buf, "\n\x00")
var output json.RawMessage
if err := json.Unmarshal(buf, &output); err != nil {
return nil, err
}
return output, nil
}
// Close shuts down rz, closing the created pipe.
func (rzp *Pipe) Close() error {
if rzp.close != nil {
return rzp.close(rzp)
}
if rzp.File == "" {
return nil
}
if _, err := rzp.Cmd("q"); err != nil {
return err
}
return rzp.rzcmd.Wait()
}
// Forcing shutdown of rz, closing the created pipe.
func (rzp *Pipe) ForceClose() error {
if rzp.close != nil {
return rzp.close(rzp)
}
if rzp.File == "" {
return nil
}
if _, err := rzp.Cmd("q!"); err != nil {
return err
}
return rzp.rzcmd.Wait()
}