class R2Pipe

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

Public Class Methods

new(file = nil) click to toggle source
# File lib/r2pipe.rb, line 11
def initialize(file = nil)
  @file = file
  if file.nil?
    fd_in, fd_out = getfds
    @read = IO.new(fd_in, 'r')
    @write = IO.new(fd_out, 'w')
    @pid = -1
  else
    execute file
  end
end

Public Instance Methods

cmd(str) click to toggle source
# File lib/r2pipe.rb, line 23
def cmd(str)
  @write.print "#{str}\n"
  @write.flush
  @read.gets("\0")[0..-2]
end
cmdj(str) click to toggle source
# File lib/r2pipe.rb, line 29
def cmdj(str)
  json(cmd(str))
end
json(str) click to toggle source
# File lib/r2pipe.rb, line 42
def json(str)
  return if str.nil?

  JSON.parse str.sub("\n", '').sub("\r", '')
end
quit() click to toggle source
# File lib/r2pipe.rb, line 33
def quit
  cmd('q!')
  @read.close
  @write.close
  return if @pid == -1

  ::Process.wait @pid
end

Private Instance Methods

execute(file) click to toggle source
# File lib/r2pipe.rb, line 60
def execute(file)
  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
getfds() click to toggle source
# File lib/r2pipe.rb, line 50
def getfds
  fd_in = ENV['R2PIPE_IN'].to_i
  fd_out = ENV['R2PIPE_OUT'].to_i
  if fd_in < 1 || fd_out < 1
    raise 'Cannot find R2PIPE_IN and R2PIPE_OUT environment variables'
  end

  [fd_in, fd_out]
end