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
Constant Summary collapse
- UNESCAPE_CHARS =
Common escaped characters.
Hash.new do |hash,char| if char[0] == '\\' then char[1] else char end end
Class Method Summary collapse
-
.decode(data) ⇒ String
Decodes the Ruby encoded data.
-
.encode(data) ⇒ String
Encodes each byte of the given data as a Ruby escaped character.
-
.encode_byte(byte) ⇒ String
Encodes a byte as a Ruby escaped character.
-
.escape(data) ⇒ String
Escapes the Ruby string.
-
.escape_byte(byte) ⇒ String
Escapes a byte as a Ruby character.
-
.quote(data) ⇒ String
Escapes and quotes the given data as a Ruby string.
-
.unescape(data) ⇒ String
Unescapes the escaped String.
-
.unquote(data) ⇒ String
Removes the quotes an unescapes a quoted string.
Class Method Details
.decode(data) ⇒ String
Decodes the Ruby encoded data.
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.
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.
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.
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.
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.
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.
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.
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 |