Class: Integer

Inherits:
Object show all
Defined in:
lib/ronin/formatting/extensions/binary/integer.rb,
lib/ronin/formatting/extensions/html/integer.rb,
lib/ronin/formatting/extensions/http/integer.rb

Overview

Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)

This file is part of Ronin Support.

Ronin Support 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 Support 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 Support. If not, see http://www.gnu.org/licenses/.

Constant Summary collapse

JS_ESCAPE_BYTES =

Special JavaScript bytes and their escaped Strings.

{
  0x00 => '\u0000',
  0x01 => '\u0001',
  0x02 => '\u0002',
  0x03 => '\u0003',
  0x04 => '\u0004',
  0x05 => '\u0005',
  0x06 => '\u0006',
  0x07 => '\u0007',
  0x08 =>  '\b',
  0x09 =>  '\t',
  0x0a =>  '\n',
  0x0b => '\u000b',
  0x0c =>  '\f',
  0x0d =>  '\r',
  0x0e => '\u000e',
  0x0f => '\u000f',
  0x10 => '\u0010',
  0x11 => '\u0011',
  0x12 => '\u0012',
  0x13 => '\u0013',
  0x14 => '\u0014',
  0x15 => '\u0015',
  0x16 => '\u0016',
  0x17 => '\u0017',
  0x18 => '\u0018',
  0x19 => '\u0019',
  0x1a => '\u001a',
  0x1b => '\u001b',
  0x1c => '\u001c',
  0x1d => '\u001d',
  0x1e => '\u001e',
  0x1f => '\u001f',
  0x22 =>  '\"',
  0x5c =>  '\\\\',
}

Instance Method Summary collapse

Instance Method Details

#bytes(address_length, endian = :little) ⇒ Array

Extracts a sequence of bytes which represent the Integer.

Examples:

0xff41.bytes(2)
# => [65, 255]
0xff41.bytes(4, :big)
# => [0, 0, 255, 65]

Parameters:

  • address_length (Integer)

    The number of bytes to decode from the Integer.

  • endian (Symbol, String) (defaults to: :little)

    The endianness to use while decoding the bytes of the Integer. May be one of:

    • :big / "big"
    • :little / "little"
    • :net / "net"

Returns:

  • (Array)

    The bytes decoded from the Integer.

Raises:

  • (ArgumentError)

    The given endian was not one of:

    • :little / "little"
    • :net / "net"
    • :big / "big"


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 56

def bytes(address_length,endian=:little)
  endian = endian.to_sym
  buffer = []

  case endian
  when :little, :net
    mask  = 0xff
    shift = 0

    address_length.times do |i|
      buffer << ((self & mask) >> shift)

      mask <<= 8
      shift += 8
    end
  when :big
    shift = ((address_length - 1) * 8)
    mask  = (0xff << shift)

    address_length.times do |i|
      buffer << ((self & mask) >> shift)

      mask >>= 8
      shift -= 8
    end
  else
    raise(ArgumentError,"invalid endian #{endian}")
  end

  return buffer
end

#format_htmlString

Formats the Integer as a HTML String.

Examples:

0x41.format_html
# => "&#65;"

Returns:

  • (String)

    The HTML String.

Since:

  • 0.2.0



94
95
96
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 94

def format_html
  "&#%d;" % self
end

#format_httpString

Formats the byte for HTTP.

Examples:

0x41.format_http
# => "%41"

Returns:

  • (String)

    The formatted byte.



65
66
67
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 65

def format_http
  "%%%X" % self
end

#format_jsString

Formats the Integer as a JavaScript escaped String.

Examples:

0x41.format_js
# => "%41"

Returns:

  • (String)

    The escaped JavaScript String.

Since:

  • 0.2.0



138
139
140
141
142
143
144
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 138

def format_js
  if self > 0xff
    "\\u%.2X%.2X" % [(self & 0xff00) >> 8, (self & 0xff)]
  else
    "\\x%.2X" % self
  end
end

#hex_escapeString

Returns The hex escaped version of the Integer.

Examples:

42.hex_escape
# => "\\x2a"

Returns:

  • (String)

    The hex escaped version of the Integer.



158
159
160
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 158

def hex_escape
  "\\x%.2x" % self
end

#html_escapeString

Escapes the Integer as an HTML String.

Examples:

0x26.html_escape
# => "&amp;"

Returns:

  • (String)

    The escaped HTML String.

Since:

  • 0.2.0



76
77
78
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 76

def html_escape
  CGI.escapeHTML(chr)
end

#js_escapeString

Escapes the Integer as a JavaScript String.

Examples:

0x22.js_escape
# => "\\\""
0x7f.js_escape
# => "\x7F"

Returns:

  • (String)

    The escaped JavaScript String.

Since:

  • 0.2.0



116
117
118
119
120
121
122
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 116

def js_escape
  if self > 0xff
    format_js
  else
    JS_ESCAPE_BYTES.fetch(self,chr)
  end
end

#pack(arch, address_length = nil) ⇒ String

Packs the Integer into a String, for a specific architecture and address-length.

Examples:

using archs other than Ronin::Arch.

arch = OpenStruct.new(:endian => :little, :address_length => 4)

0x41.pack(arch)
# => "A\0\0\0"

using a Ronin::Arch arch.

0x41.pack(Arch.i686)
# => "A\0\0\0"

specifying a custom address-length.

0x41.pack(Arch.ppc,2)
# => "\0A"

using a Array#pack template String for the arch.

0x41.pack('L')
# => "A\0\0\0"

Parameters:

  • arch (Ronin::Arch, #endian, #address_length, String)

    The architecture to pack the Integer for.

  • address_length (Integer) (defaults to: nil)

    The number of bytes to pack.

Returns:

  • (String)

    The packed Integer.

Raises:

  • (ArgumentError)

    The given arch does not respond to the endian or address_length methods.

See Also:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 127

def pack(arch,address_length=nil)
  if arch.kind_of?(String)
    return [self].pack(arch)
  end

  unless arch.respond_to?(:address_length)
    raise(ArgumentError,"first argument to Ineger#pack must respond to address_length")
  end

  unless arch.respond_to?(:endian)
    raise(ArgumentError,"first argument to Ineger#pack must respond to endian")
  end

  address_length ||= arch.address_length

  integer_bytes = bytes(address_length,arch.endian)
  integer_bytes.map! { |b| b.chr }

  return integer_bytes.join
end

#uri_encodeString

URI encodes the byte.

Returns:

  • (String)

    The URI encoded byte.



33
34
35
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 33

def uri_encode
  URI.encode(chr)
end

#uri_escapeString

URI escapes the byte.

Examples:

0x3d.uri_escape
# => "%3D"

Returns:

  • (String)

    The URI escaped byte.



49
50
51
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 49

def uri_escape
  CGI.escape(chr)
end