module Ronin::Support::Encoding::URI

Contains methods for encoding/decoding escaping/unescaping URI data.

## Features

## Core-Ext Methods

@api public

Public Class Methods

decode(data) click to toggle source

Alias for {unescape}.

@param [String] data

The URI escaped data to decode.

@return [String]

The decoded URI form of the String.

@see unescape

# File lib/ronin/support/encoding/uri.rb, line 240
def self.decode(data)
  unescape(data)
end
encode(data,**kwargs) click to toggle source

URI encodes the String.

@param [String] data

The data to URI encode.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI encoded form of the String.

@example

Encoding::URI.encode("plain text")
# => "%70%6C%61%69%6E%20%74%65%78%74"

@example Lowercase encoding:

Encoding::URI.escape("plain text", case: :lower)
# => "%70%6c%61%69%6e%20%74%65%78%74"
# File lib/ronin/support/encoding/uri.rb, line 219
def self.encode(data,**kwargs)
  encoded = String.new

  data.each_byte do |byte|
    encoded << encode_byte(byte,**kwargs)
  end

  return encoded
end
encode_byte(byte,**kwargs) click to toggle source

URI encodes the byte.

@param [Integer] byte

The byte to URI encode.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI encoded byte.

@raise [ArgumentError]

The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.

@raise [RangeError]

The byte value is negative or greater than 255.

@example

Encoding::URI.encode_byte(0x41)
# => "%41"

@example Lowercase encoding:

Encoding::URI.encode_byte(0xff, case: :lower)
# => "%ff"
# File lib/ronin/support/encoding/uri.rb, line 78
def self.encode_byte(byte,**kwargs)
  if (byte >= 0) && (byte <= 0xff)
    case kwargs[:case]
    when :lower
      "%%%.2x" % byte
    when :upper, nil
      "%%%.2X" % byte
    else
      raise(ArgumentError,"case (#{kwargs[:case].inspect}) keyword argument must be either :lower, :upper, or nil")
    end
  else
    raise(RangeError,"#{byte.inspect} out of char range")
  end
end
escape(data,**kwargs) click to toggle source

URI escapes the String.

@param [String] data

The data to URI escape.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI escaped form of the String.

@raise [ArgumentError]

The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.

@example

Encoding::URI.escape("x > y")
# => "x%20%3E%20y"

@example Lowercase encoding:

Encoding::URI.escape("x > y", case: :lower)
# => "x%20%3e%20y"

@api public

# File lib/ronin/support/encoding/uri.rb, line 166
def self.escape(data,**kwargs)
  escaped = String.new

  data.each_byte do |byte|
    escaped << escape_byte(byte,**kwargs)
  end

  return escaped
end
escape_byte(byte,**kwargs) click to toggle source

URI escapes the byte.

@param [Integer] byte

The byte to URI escape.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI escaped byte.

@raise [ArgumentError]

The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.

@raise [RangeError]

The byte value is negative or greater than 255.

@example

Encoding::URI.escape_byte(0x41)
# => "A"
Encoding::URI.escape_byte(0x3d)
# => "%3D"

@example Lowercase encoding:

Encoding::URI.escape_byte(0xff, case: :lower)
# => "%ff"
# File lib/ronin/support/encoding/uri.rb, line 125
def self.escape_byte(byte,**kwargs)
  if (byte >= 0) && (byte <= 0xff)
    if (byte == 33) || (byte == 36) || (byte == 38) || ((byte >= 39) && (byte <= 59)) || (byte == 61) || ((byte >= 63) && (byte <= 91)) || (byte == 93) || (byte == 95) || ((byte >= 97) && (byte <= 122)) || (byte == 126)
      byte.chr
    else
      encode_byte(byte,**kwargs)
    end
  else
    raise(RangeError,"#{byte.inspect} out of char range")
  end
end
unescape(data) click to toggle source

URI unescapes the String.

@param [String] data

The URI escaped data to unescape.

@return [String]

The unescaped URI form of the String.

@example

Encoding::URI.unescape("sweet%20%26%20sour")
# => "sweet & sour"
# File lib/ronin/support/encoding/uri.rb, line 189
def self.unescape(data)
  data.gsub(/%[A-Fa-f0-9]{2}/) do |escaped_char|
    escaped_char[1..].to_i(16).chr
  end
end