Skip to content
Snippets Groups Projects
Commit 2d8863a9 authored by Sascha Schirra's avatar Sascha Schirra
Browse files

added bindings for ruby

parent 4bbe172a
No related branches found
No related tags found
No related merge requests found
Showing
with 371 additions and 0 deletions
...@@ -47,6 +47,24 @@ template = { ...@@ -47,6 +47,24 @@ template = {
'comment_open': '//', 'comment_open': '//',
'comment_close': '', 'comment_close': '',
}, },
'ruby': {
'header': "# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.rb]\n\nmodule Keystone\n",
'footer': "end",
'line_format': '\tKS_%s = %s\n',
'out_file': './ruby/keystone_gem/lib/keystone/%s_const.rb',
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'arm',
'arm64.h': 'arm64',
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
'systemz.h': 'systemz',
'ppc.h': 'ppc',
'hexagon.h': 'hexagon',
'keystone.h': 'keystone',
'comment_open': '#',
'comment_close': '',
},
} }
# markup for comments to be added to autogen files # markup for comments to be added to autogen files
......
# Ruby binding for Unicorn engine. Sascha Schirra <sashs@scoding.de>
.PHONY: gen_const
install:
$(MAKE) gen_const
C_INCLUDE_PATH=/usr/local/ cd keystone_gem && rake build
cd keystone_gem && gem install --local pkg/keystone-1.0.0.gem
gen_const:
cd .. && python const_generator.py ruby
# Installation
## Software requirements
### Linux
- ruby >= 1.9.3
- rubygems
- make
- gcc
### Mac OS
- ruby >= 1.9.3
- rubygems
- make
- XCode
## Install keystone
* cd path_to_keystone
* ./make.sh install
## Install ruby binding
* cd bindings/ruby
* make install
\ No newline at end of file
source 'https://rubygems.org'
gemspec
require "bundler/gem_tasks"
task :default => :spec
require 'mkmf'
extension_name = 'keystone'
dir_config(extension_name)
have_library('keystone')
create_makefile(extension_name)
\ No newline at end of file
/*
Ruby bindings for the Keystone Engine
Copyright(c) 2016 Sascha Schirra
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ruby.h"
#include <keystone/keystone.h>
#include "keystone.h"
VALUE KeystoneModule = Qnil;
VALUE KsClass = Qnil;
VALUE KsError = Qnil;
void Init_keystone() {
rb_require("keystone/keystone_const");
KeystoneModule = rb_define_module("Keystone");
KsError = rb_define_class_under(KeystoneModule, "KsError", rb_eStandardError);
KsClass = rb_define_class_under(KeystoneModule, "Ks", rb_cObject);
rb_define_method(KsClass, "initialize", m_ks_initialize, 2);
rb_define_method(KsClass, "asm", m_ks_asm, -1);
rb_define_method(KsClass, "syntax", m_ks_get_syntax, 0);
rb_define_method(KsClass, "syntax=", m_ks_set_syntax, 1);
}
VALUE m_ks_initialize(VALUE self, VALUE arch, VALUE mode) {
ks_engine *_ks;
ks_err err;
err = ks_open(NUM2INT(arch), NUM2INT(mode), &_ks);
if (err != KS_ERR_OK) {
rb_raise(KsError, "%d", err);
}
VALUE ks = Data_Wrap_Struct(KsClass, 0, ks_close, _ks);
rb_iv_set(self, "@ksh", ks);
if(NUM2INT(arch) == KS_ARCH_X86){
rb_iv_set(self, "@syntax", INT2NUM(KS_OPT_SYNTAX_INTEL));
}
else{
rb_iv_set(self, "@syntax", Qnil);
}
return self;
}
VALUE m_ks_asm(int argc, VALUE* argv, VALUE self){
VALUE string;
VALUE addr;
size_t count;
unsigned char *encode;
size_t size;
ks_err err;
ks_engine *_ks;
VALUE to_return = rb_ary_new();
Data_Get_Struct(rb_iv_get(self,"@ksh"), ks_engine, _ks);
rb_scan_args(argc, argv, "11", &string, &addr);
if (NIL_P(addr))
addr = INT2NUM(0);
err = ks_asm(_ks, StringValuePtr(string), NUM2INT(addr), &encode, &size, &count);
if (err != KS_ERR_OK) {
rb_raise(KsError, "%d", err);
}
if (count == 0){
rb_ary_store(to_return, 0, Qnil);
rb_ary_store(to_return, 1, INT2NUM(0));
}
else{
rb_ary_store(to_return, 0, rb_str_new(encode, size));
rb_ary_store(to_return, 1, INT2NUM(count));
}
ks_free(encode);
return to_return;
}
VALUE m_ks_get_syntax(VALUE self){
return rb_iv_get(self, "@syntax");
}
VALUE m_ks_set_syntax(VALUE self, VALUE val){
ks_err err;
ks_engine *_ks;
Data_Get_Struct(rb_iv_get(self,"@ksh"), ks_engine, _ks);
ks_option(_ks, KS_OPT_SYNTAX, NUM2INT(val));
rb_iv_set(self, "@syntax", val);
return Qnil;
}
/*
Ruby bindings for the Keystone Engine
Copyright(c) 2016 Sascha Schirra
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
VALUE m_ks_initialize(VALUE self, VALUE arch, VALUE mode);
VALUE m_ks_asm(int argc, VALUE* argv, VALUE self);
VALUE m_ks_get_syntax(VALUE self);
VALUE m_ks_set_syntax(VALUE self, VALUE val);
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'keystone/version'
Gem::Specification.new do |spec|
spec.name = "keystone"
spec.version = Keystone::VERSION
spec.authors = ["Sascha Schirra"]
spec.email = ["sashs@scoding.de"]
spec.license = 'GPL-2.0'
spec.summary = %q{Ruby binding for Keystone}
spec.description = %q{Ruby binding for Keystone <Keystone-engine.org>}
spec.homepage = "https://keystone-engine.org"
spec.files = Dir["lib/keystone/*.rb"] + Dir["ext/keystone.c"] + Dir["ext/keystone.h"] + Dir["ext/extconf.rb"]
spec.require_paths = ["lib","ext"]
spec.extensions = ["ext/extconf.rb"]
spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
end
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [arm64_const.rb]
module Keystone
KS_ERR_ASM_ARM64_INVALIDOPERAND = 512
KS_ERR_ASM_ARM64_MISSINGFEATURE = 513
KS_ERR_ASM_ARM64_MNEMONICFAIL = 514
end
\ No newline at end of file
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [arm_const.rb]
module Keystone
KS_ERR_ASM_ARM_INVALIDOPERAND = 512
KS_ERR_ASM_ARM_MISSINGFEATURE = 513
KS_ERR_ASM_ARM_MNEMONICFAIL = 514
end
\ No newline at end of file
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [hexagon_const.rb]
module Keystone
KS_ERR_ASM_HEXAGON_INVALIDOPERAND = 512
KS_ERR_ASM_HEXAGON_MISSINGFEATURE = 513
KS_ERR_ASM_HEXAGON_MNEMONICFAIL = 514
end
\ No newline at end of file
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [keystone_const.rb]
module Keystone
KS_API_MAJOR = 1
KS_API_MINOR = 0
KS_ARCH_ARM = 1
KS_ARCH_ARM64 = 2
KS_ARCH_MIPS = 3
KS_ARCH_X86 = 4
KS_ARCH_PPC = 5
KS_ARCH_SPARC = 6
KS_ARCH_SYSTEMZ = 7
KS_ARCH_HEXAGON = 8
KS_ARCH_MAX = 9
KS_MODE_LITTLE_ENDIAN = 0
KS_MODE_BIG_ENDIAN = 1073741824
KS_MODE_ARM = 1
KS_MODE_THUMB = 16
KS_MODE_V8 = 64
KS_MODE_MICRO = 16
KS_MODE_MIPS3 = 32
KS_MODE_MIPS32R6 = 64
KS_MODE_MIPS32 = 4
KS_MODE_MIPS64 = 8
KS_MODE_16 = 2
KS_MODE_32 = 4
KS_MODE_64 = 8
KS_MODE_PPC32 = 4
KS_MODE_PPC64 = 8
KS_MODE_QPX = 16
KS_MODE_SPARC32 = 4
KS_MODE_SPARC64 = 8
KS_MODE_V9 = 16
KS_ERR_ASM = 128
KS_ERR_ASM_ARCH = 512
KS_ERR_OK = 0
KS_ERR_NOMEM = 1
KS_ERR_ARCH = 2
KS_ERR_HANDLE = 3
KS_ERR_MODE = 4
KS_ERR_VERSION = 5
KS_ERR_OPT_INVALID = 6
KS_ERR_ASM_EXPR_TOKEN = 128
KS_ERR_ASM_DIRECTIVE_VALUE_RANGE = 129
KS_ERR_ASM_DIRECTIVE_ID = 130
KS_ERR_ASM_DIRECTIVE_TOKEN = 131
KS_ERR_ASM_DIRECTIVE_STR = 132
KS_ERR_ASM_DIRECTIVE_COMMA = 133
KS_ERR_ASM_DIRECTIVE_RELOC_NAME = 134
KS_ERR_ASM_DIRECTIVE_RELOC_TOKEN = 135
KS_ERR_ASM_DIRECTIVE_FPOINT = 136
KS_ERR_ASM_VARIANT_INVALID = 137
KS_ERR_ASM_EXPR_BRACKET = 138
KS_ERR_ASM_SYMBOL_MODIFIER = 139
KS_ERR_ASM_RPAREN = 140
KS_ERR_ASM_STAT_TOKEN = 141
KS_ERR_ASM_UNSUPPORTED = 142
KS_ERR_ASM_MACRO_TOKEN = 143
KS_ERR_ASM_MACRO_PAREN = 144
KS_ERR_ASM_MACRO_EQU = 145
KS_ERR_ASM_MACRO_ARGS = 146
KS_ERR_ASM_MACRO_LEVELS_EXCEED = 147
KS_ERR_ASM_ESC_BACKSLASH = 148
KS_ERR_ASM_ESC_OCTAL = 149
KS_ERR_ASM_ESC_SEQUENCE = 150
KS_ERR_ASM_INVALIDOPERAND = 512
KS_ERR_ASM_MISSINGFEATURE = 513
KS_ERR_ASM_MNEMONICFAIL = 514
KS_OPT_SYNTAX = 1
KS_OPT_SYNTAX_INTEL = 1
KS_OPT_SYNTAX_ATT = 2
KS_OPT_SYNTAX_NASM = 4
KS_OPT_SYNTAX_MASM = 8
KS_OPT_SYNTAX_GAS = 16
end
\ No newline at end of file
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [mips_const.rb]
module Keystone
KS_ERR_ASM_MIPS_INVALIDOPERAND = 512
KS_ERR_ASM_MIPS_MISSINGFEATURE = 513
KS_ERR_ASM_MIPS_MNEMONICFAIL = 514
end
\ No newline at end of file
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [ppc_const.rb]
module Keystone
KS_ERR_ASM_PPC_INVALIDOPERAND = 512
KS_ERR_ASM_PPC_MISSINGFEATURE = 513
KS_ERR_ASM_PPC_MNEMONICFAIL = 514
end
\ No newline at end of file
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [sparc_const.rb]
module Keystone
KS_ERR_ASM_SPARC_INVALIDOPERAND = 512
KS_ERR_ASM_SPARC_MISSINGFEATURE = 513
KS_ERR_ASM_SPARC_MNEMONICFAIL = 514
end
\ No newline at end of file
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [systemz_const.rb]
module Keystone
KS_ERR_ASM_SYSTEMZ_INVALIDOPERAND = 512
KS_ERR_ASM_SYSTEMZ_MISSINGFEATURE = 513
KS_ERR_ASM_SYSTEMZ_MNEMONICFAIL = 514
end
\ No newline at end of file
module Keystone
VERSION = "1.0.0"
end
# For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [x86_const.rb]
module Keystone
KS_ERR_ASM_X86_INVALIDOPERAND = 512
KS_ERR_ASM_X86_MISSINGFEATURE = 513
KS_ERR_ASM_X86_MNEMONICFAIL = 514
end
\ No newline at end of file
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.gem
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment