Module: Ronin::Support::Encoding::Ruby

Defined in:
lib/ronin/support/encoding/ruby.rb

Overview

Contains methods for encoding/decoding escaping/unescaping Ruby strings.

Core-Ext Methods

Since:

  • 1.0.0

Constant Summary collapse

UNESCAPE_CHARS =

Common escaped characters.

Since:

  • 1.0.0

Hash.new do |hash,char|
  if char[0] == '\\' then char[1]
  else                    char
  end
end

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ String

Decodes the Ruby encoded data.

Parameters:

  • data (String)

    The given Ruby data to decode.

Returns:

  • (String)

    The decoded data.

See Also:

Since:

  • 1.0.0



130
131
132
# File 'lib/ronin/support/encoding/ruby.rb', line 130

def self.decode(data)
  unescape(data)
end

.encode(data) ⇒ String

Encodes each byte of the given data as a Ruby escaped character.

Examples:

Encoding::Ruby.encode("hello")
# => "\\x68\\x65\\x6c\\x6c\\x6f"

Parameters:

  • data (String)

    The given data to encode.

Returns:

  • (String)

    The encoded Ruby String.

Since:

  • 1.0.0



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ronin/support/encoding/ruby.rb', line 103

def self.encode(data)
  encoded = String.new

  if data.valid_encoding?
    data.each_codepoint do |codepoint|
      encoded << encode_byte(codepoint)
    end
  else
    data.each_byte do |byte|
      encoded << encode_byte(byte)
    end
  end

  return encoded
end

.encode_byte(byte) ⇒ String

Encodes a byte as a Ruby escaped character.

Examples:

Encoding::Ruby.encode_byte(0x41)
# => "\\x41"
Encoding::Ruby.encode_byte(0x100)
# => "\\u100"
Encoding::Ruby.encode_byte(0x10000)
# => "\\u{10000}"

Parameters:

  • byte (Integer)

    The byte value to encode.

Returns:

  • (String)

    The escaped Ruby character.

Since:

  • 1.0.0



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ronin/support/encoding/ruby.rb', line 56

def self.encode_byte(byte)
  if byte >= 0x00 && byte <= 0xff
    "\\x%.2X" % byte
  elsif byte >= 0x100 && byte <= 0xffff
    "\\u%.4X" % byte
  elsif byte >= 0x10000 && byte <= 0x10ffff
    "\\u{%.X}" % byte
  else
    raise(RangeError,"#{byte.inspect} out of char range")
  end
end

.escape(data) ⇒ String

Escapes the Ruby string.

Parameters:

  • data (String)

    The data to escape.

Returns:

  • (String)

    The Ruby escaped String.

Since:

  • 1.0.0



143
144
145
# File 'lib/ronin/support/encoding/ruby.rb', line 143

def self.escape(data)
  data.dump[1..-2]
end

.escape_byte(byte) ⇒ String

Escapes a byte as a Ruby character.

Parameters:

  • byte (Integer)

    The byte value to escape.

Returns:

  • (String)

    The escaped Ruby character.

Raises:

  • (RangeError)

    The byte value isn't a valid ASCII or UTF byte.

Since:

  • 1.0.0



80
81
82
83
84
85
86
87
88
# File 'lib/ronin/support/encoding/ruby.rb', line 80

def self.escape_byte(byte)
  char = if byte >= 0x00 && byte <= 0xff
           byte.chr
         else
           byte.chr(Encoding::UTF_8)
         end

  return char.dump[1..-2]
end

.quote(data) ⇒ String

Escapes and quotes the given data as a Ruby string.

Examples:

Encoding::Ruby.quote("hello\nworld\n")
# => "\"hello\\nworld\\n\""

Parameters:

  • data (String)

    The given data to escape and quote.

Returns:

  • (String)

    The quoted Ruby string.

Since:

  • 1.0.0



210
211
212
# File 'lib/ronin/support/encoding/ruby.rb', line 210

def self.quote(data)
  data.dump
end

.unescape(data) ⇒ String

Unescapes the escaped String.

Examples:

Encoding::Ruby.unescape("\\x68\\x65\\x6c\\x6c\\x6f")
# => "hello"

Parameters:

  • data (String)

    The given Ruby escaped data.

Returns:

  • (String)

    The unescaped version of the hex escaped String.

Since:

  • 1.0.0



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ronin/support/encoding/ruby.rb', line 176

def self.unescape(data)
  unescaped = String.new(encoding: Encoding::UTF_8)
  scanner   = StringScanner.new(data)

  until scanner.eos?
    unescaped << if (unicode_escape = scanner.scan(/\\(?:[0-7]{1,3}|[0-7])/))
                   unicode_escape[1,3].to_i(8).chr
                 elsif (hex_escape = scanner.scan(/\\u[0-9a-fA-F]{4,8}/))
                   hex_escape[2..].to_i(16).chr(Encoding::UTF_8)
                 elsif (hex_escape = scanner.scan(/\\x[0-9a-fA-F]{1,2}/))
                   hex_escape[2..].to_i(16).chr
                 elsif (escape     = scanner.scan(/\\./))
                   UNESCAPE_CHARS[escape]
                 else
                   scanner.getch
                 end
  end

  return unescaped
end

.unquote(data) ⇒ String

Removes the quotes an unescapes a quoted string.

Examples:

Encoding::Ruby.unquote("\"hello\\nworld\"")
# => "hello\nworld"
Encoding::Ruby.unquote("'hello\\'world'")
# => "hello'world"

Parameters:

  • data (String)

    The given Ruby string.

Returns:

  • (String)

    The un-quoted String if the String begins and ends with quotes, or the same String if it is not quoted.

Since:

  • 1.0.0



230
231
232
233
234
235
236
237
238
# File 'lib/ronin/support/encoding/ruby.rb', line 230

def self.unquote(data)
  if (data[0] == '"' && data[-1] == '"')
    unescape(data[1..-2])
  elsif (data[0] == "'" && data[-1] == "'")
    data[1..-2].gsub(/\\(['\\])/,'\1')
  else
    data
  end
end