Skip to content

Commit

Permalink
Added X86::RelativeOffset (closes #102).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Feb 28, 2025
1 parent b828166 commit 6940daf
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/ronin/asm/x86/relative_offset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true
#
# ronin-asm - A Ruby DSL for crafting Assembly programs and shellcode.
#
# Copyright (c) 2007-2025 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# ronin-asm is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-asm 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-asm. If not, see <https://www.gnu.org/licenses/>.
#

require_relative '../operand'

module Ronin
module ASM
module X86
#
# Represents a relative offset (`rel8` or `rel32`).
#
# @since 1.0.0
#
class RelativeOffset < Operand

# The relative offset value.
#
# @return [Integer]
attr_reader :value

# The size of the relative offset operand.
#
# @return [1, 4]
attr_reader :width

# The operand type.
#
# @return [:rel8, :rel32]
attr_reader :type

#
# Initializes the relative offset.
#
# @param [Integer] value
# The relative offset value.
#
def initialize(value)
@value = value
@width = if value.bit_length <= 8 then 1
else 4
end
@type = :"rel#{@width * 8}"
end

end
end
end
end
38 changes: 38 additions & 0 deletions spec/x86/relative_offset_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'
require 'ronin/asm/x86/relative_offset'

describe Ronin::ASM::X86::RelativeOffset do
let(:value) { 0x1000 }

subject { described_class.new(value) }

describe "#initialize" do
it "must set #value" do
expect(subject.value).to eq(value)
end

context "when the value has a bit length less or equal to 8 bits" do
let(:value) { 0x10 }

it "must set #width to 1" do
expect(subject.width).to eq(1)
end

it "must set #type to :rel8" do
expect(subject.type).to eq(:rel8)
end
end

context "when the value has a bit length less or equal to 32bits" do
let(:value) { 0x11223344 }

it "must set #width to 4" do
expect(subject.width).to eq(4)
end

it "must set #type to :rel32" do
expect(subject.type).to eq(:rel32)
end
end
end
end

0 comments on commit 6940daf

Please sign in to comment.