Class: R2Pipe

Inherits:
Object
  • Object
show all
Defined in:
lib/r2pipe.rb

Overview

R2Pipe is an easy way to communicate with an r2 core through ruby

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ R2Pipe

Returns a new instance of R2Pipe.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/r2pipe.rb', line 11

def initialize(file = nil)
  @file = file
  if file == nil
    fdIn = ENV['R2PIPE_IN'].to_i
    fdOut = ENV['R2PIPE_OUT'].to_i
    if fdIn < 1 or fdOut < 1
      raise 'Cannot find R2PIPE_IN and R2PIPE_OUT environment variables'
    end
    @read = IO.new(fdIn, 'r')
    @write = IO.new(fdOut, 'w')
    @pid = -1
  else
    exec = "radare2 -q0 #{Shellwords.shellescape file} 2>/dev/null"
    write, read, wait_thr = Open3.popen2(exec)
    @read = read
    @write = write
    @pid = wait_thr.pid
    @read.gets("\0")
  end
end

Instance Method Details

#cmd(str) ⇒ Object



32
33
34
35
36
# File 'lib/r2pipe.rb', line 32

def cmd(str)
  @write.print "#{str}\n"
  @write.flush
  @read.gets("\0")[0..-2]
end

#cmdj(str) ⇒ Object



38
39
40
# File 'lib/r2pipe.rb', line 38

def cmdj(str)
  json(cmd(str))
end

#json(str) ⇒ Object



51
52
53
54
55
# File 'lib/r2pipe.rb', line 51

def json(str)
  if str != nil
    JSON.parse str.sub("\n", '').sub("\r", '')
  end
end

#quitObject



42
43
44
45
46
47
48
49
# File 'lib/r2pipe.rb', line 42

def quit
  cmd('q!')
  @read.close
  @write.close
  if @pid != -1
    ::Process.wait @pid
  end
end