Class: Integer

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

Constant Summary

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(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]

Raises:

  • (ArgumentError)

    The given endian was not one of:

    • little
    • net
    • big


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
87
88
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 58

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

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

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

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

    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;"

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"


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"

Since:

  • 0.2.0



136
137
138
139
140
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 136

def format_js
  if self > 0xff then "\\u%.4X" % self
  else                "\\x%.2X" % self
  end
end

#hex_escapeString

Returns The hex escaped version of the Integer.

Examples:

42.hex_escape
# => "\\x2a"


174
175
176
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 174

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

#html_escapeString

Escapes the Integer as an HTML String.

Examples:

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

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"

Since:

  • 0.2.0



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

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

#pack(*arguments) ⇒ String

Packs the Integer into a String.

Examples:

using a Array#pack template:

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

0x41.pack(:uint32_le)

using archs other than Ronin::Arch (deprecated):

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

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

using a Ronin::Arch arch (deprecated):

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

specifying a custom address-length (deprecated):

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

Raises:

See Also:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 130

def pack(*arguments)
  if (arguments.length == 1 && arguments.first.kind_of?(String))
    [self].pack(arguments.first)
  elsif (arguments.length == 1 && arguments.first.kind_of?(Symbol))
    type = arguments.first

    unless Ronin::Binary::Template::INT_TYPES.include?(type)
      raise(ArgumentError,"unsupported integer type: #{type}")
    end

    [self].pack(Ronin::Binary::Template::TYPES[type])
  elsif (arguments.length == 1 || arguments.length == 2)
    # TODO: deprecate this calling convention
    arch, address_length = arguments

    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
  else
    raise(ArgumentError,"wrong number of arguments (#{arguments.length} for 1..2)")
  end
end

#uri_encodeString

URI encodes the 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"


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

def uri_escape
  CGI.escape(chr)
end