Sha256: 89e730f41e3d3266dcdb6b4067c05e2a46f225e515f58976a454539d7260166f

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

#
#--
# Ronin - A Ruby platform designed for information security and data
# exploration tasks.
#
# Copyright (c) 2006-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 St, Fifth Floor, Boston, MA  02110-1301  USA
#++
#

require 'ronin/arch'

class Integer

  #
  # Packs the integer using the specified _arch_ and the given
  # _address_length_. The _address_length_ will default to the address
  # length of the _arch_.
  #
  #   0x41.pack(Arch.i686) # => "A\000\000\000"
  #
  #   0x41.pack(Arch.ppc,2) # => "\000A"
  #
  def pack(arch,address_length=arch.address_length)
    buffer = ""

    if arch.endian=='little'
      mask = 0xff

      address_length.times do |i|
        buffer+=((self & mask) >> (i*8)).chr
        mask <<= 8
      end
    elsif arch.endian=='big'
      mask = (0xff << ((address_length-1)*8))

      address_length.times do |i|
        buffer+=((self & mask) >> ((address_length-i-1)*8)).chr
        mask >>= 8
      end
    end

    return buffer
  end

  #
  # Returns the hex escaped form of the integer.
  #
  #   42.hex_escape
  #   # => "\\x2a"
  #
  def hex_escape
    "\\x%.2x" % self
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ronin-0.2.1 lib/ronin/formatting/extensions/binary/integer.rb
ronin-0.2.0 lib/ronin/formatting/extensions/binary/integer.rb