From 08f44fa326c3802905c5c189d1827ef630b37050 Mon Sep 17 00:00:00 2001 From: Xu Jingtao Date: Wed, 30 Sep 2015 16:34:55 +0800 Subject: [PATCH] add r2pipe lisp bindings for radare2. --- lisp/main.lisp | 19 +++++++++++++++++++ lisp/r2pipe.lisp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 lisp/main.lisp create mode 100644 lisp/r2pipe.lisp diff --git a/lisp/main.lisp b/lisp/main.lisp new file mode 100644 index 0000000..ca30af6 --- /dev/null +++ b/lisp/main.lisp @@ -0,0 +1,19 @@ +;;; -*- encoding:utf-8 Mode: LISP; Syntax: COMMON-LISP; Base: 10 -*- --- +;; +;; Filename: main.lisp +;; Description: +;; Author: Jingtao Xu +;; Created: 2015.09.29 17:43:05(+0800) +;; Last-Updated: 2015.09.30 16:33:26(+0800) +;; Update #: 6 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;;; Commentary: +;; +;; +;;(load "r2pipe.lisp") +(setf pipe (r2pipe "/bin/bash")) +(format t "~s~%" (r2-cmd pipe "pi 5")) +(format t "~s~%" (r2-json (r2-cmd pipe "pij 1"))) +(format t "~s~%" (r2-cmd pipe "px 64")) +(r2-quit pipe) diff --git a/lisp/r2pipe.lisp b/lisp/r2pipe.lisp new file mode 100644 index 0000000..889d62e --- /dev/null +++ b/lisp/r2pipe.lisp @@ -0,0 +1,45 @@ +;;; -*- encoding:utf-8 Mode: LISP; Syntax: COMMON-LISP; Base: 10 -*- --- +;; +;; Filename: r2pipe.lisp +;; Description: +;; Author: Jingtao Xu +;; Created: 2015.09.29 17:42:43(+0800) +;; Last-Updated: 2015.09.30 16:30:25(+0800) +;; Update #: 38 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;;; Commentary: +;; +;; +(in-package :cl-user) + +(asdf:oos 'asdf:load-op :cl-json) + +(defvar *r2-bin-path* "/usr/bin/r2") +(defun r2-read-pipe (pipe) + (with-output-to-string (stream) + (loop for c = (read-char pipe) + until (= 0 (char-code c)) + do (write-char c stream)))) + +(defun r2pipe (file) + (let ((pipe #-lispworks(error "r2pipe is not implemented for current lisp platform.") + #+lispworks(sys:open-pipe `(,*r2-bin-path* "-q" "-0" ,file) :direction :io))) + (values pipe (r2-read-pipe pipe)))) + +(defun r2-cmd (pipe cmd) + (write-line (format nil "~a" cmd) pipe) + (finish-output pipe) + (r2-read-pipe pipe)) + +(defun r2-quit (pipe) + (r2-cmd pipe "q!")) + +(defun r2-json (str) + (json:decode-json-from-string + (with-output-to-string (*standard-output*) + (with-input-from-string (*standard-input* str) + (loop for c = (read-char *standard-input* nil nil) then (read-char *standard-input* nil nil) + until (null c) + unless (find c '(#\Newline #\Return)) + do (write-char c))))))