Module: Ronin::Support::Encoding::Hex
- Defined in:
- lib/ronin/support/encoding/hex.rb
Overview
Contains methods for hex encoding/decoding and escaping/unescaping data.
Core-Ext Methods
Constant Summary collapse
- BACKSLASHED_CHARS =
Backslash escaped characters.
{ "\\" => '\\', '"' => '"', '0' => "\0", 'a' => "\a", 'b' => "\b", 't' => "\t", 'n' => "\n", 'v' => "\v", 'f' => "\f", 'r' => "\r" }
Class Method Summary collapse
-
.decode(data) ⇒ String
Hex decodes the String.
-
.encode(data) ⇒ String
Hex encodes the given data.
-
.encode_byte(byte) ⇒ String
Hex encodes the given byte.
-
.escape(data) ⇒ String
Hex-escapes the given data.
-
.escape_byte(byte) ⇒ String
Hex escapes the given byte.
-
.quote(data) ⇒ String
Converts the given data into a double-quoted hex string.
-
.unescape(data) ⇒ String
Removes the quotes an unescapes a quoted hex string.
-
.unquote(data) ⇒ String
Removes the quotes and unescapes a hex string.
Class Method Details
.decode(data) ⇒ String
Hex decodes the String.
127 128 129 130 131 132 133 134 135 |
# File 'lib/ronin/support/encoding/hex.rb', line 127 def self.decode(data) decoded = String.new data.scan(/../) do |hex| decoded << hex.to_i(16).chr end return decoded end |
.encode(data) ⇒ String
Hex encodes the given data.
104 105 106 107 108 109 110 111 112 |
# File 'lib/ronin/support/encoding/hex.rb', line 104 def self.encode(data) encoded = String.new data.each_byte do |byte| encoded << encode_byte(byte) end return encoded end |
.encode_byte(byte) ⇒ String
Hex encodes the given byte.
53 54 55 |
# File 'lib/ronin/support/encoding/hex.rb', line 53 def self.encode_byte(byte) "%.2x" % byte end |
.escape(data) ⇒ String
Hex-escapes the given data.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ronin/support/encoding/hex.rb', line 150 def self.escape(data) escaped = String.new if data.valid_encoding? data.each_codepoint do |codepoint| escaped << escape_byte(codepoint) end else data.each_byte do |byte| escaped << escape_byte(byte) end end return escaped end |
.escape_byte(byte) ⇒ String
Hex escapes the given byte.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ronin/support/encoding/hex.rb', line 73 def self.escape_byte(byte) if byte >= 0x00 && byte <= 0xff if byte == 0x22 "\\\"" elsif byte == 0x5d "\\\\" elsif byte >= 0x20 && byte <= 0x7e byte.chr else "\\x%.2x" % byte end elsif byte > 0xff "\\x%x" % byte else raise(RangeError,"#{byte.inspect} out of char range") end end |
.quote(data) ⇒ String
Converts the given data into a double-quoted hex string.
229 230 231 |
# File 'lib/ronin/support/encoding/hex.rb', line 229 def self.quote(data) "\"#{escape(data)}\"" end |
.unescape(data) ⇒ String
Removes the quotes an unescapes a quoted hex string.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/ronin/support/encoding/hex.rb', line 194 def self.unescape(data) buffer = String.new(encoding: Encoding::UTF_8) scanner = StringScanner.new(data) until scanner.eos? buffer << case (char = scanner.getch) when '\\' if (hex_escape = scanner.scan(/x[0-9a-fA-F]{4,8}/)) hex_escape[1..].to_i(16).chr(Encoding::UTF_8) elsif (hex_escape = scanner.scan(/x[0-9a-fA-F]{1,2}/)) hex_escape[1..].to_i(16).chr elsif (char = scanner.getch) BACKSLASHED_CHARS.fetch(char,char) end else char end end return buffer end |
.unquote(data) ⇒ String
Removes the quotes and unescapes a hex string.
247 248 249 250 251 252 253 254 |
# File 'lib/ronin/support/encoding/hex.rb', line 247 def self.unquote(data) if ((data[0] == '"' && data[-1] == '"') || (data[0] == "'" && data[-1] == "'")) unescape(data[1..-2]) else data end end |