module Ronin::Support::Encoding::URI::Form

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

@api public

Public Class Methods

decode(data) click to toggle source

Alias for {unescape}.

@param [String] data

The URI Form escaped data to decode.

@return [String]

The URI Form decoded String.

@see unescape

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

URI Form encodes every character in the given data.

@param [String] data

The given data to URI Form 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 Form encoded String.

@example

Encoding::URI::Form.encode("hello world")
# => "%68%65%6C%6C%6F+%77%6F%72%6C%64"

@example Lowercase encoding:

Encoding::URI::Form.encode("hello world", case: :lower)
# => "%68%65%6c%6c%6f+%77%6f%72%6c%64"
# File lib/ronin/support/encoding/uri.rb, line 409
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 Form encodes the given byte.

@param [Integer] byte

The byte to URI Form 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 Form encoded byte.

@raise [RangeError]

The byte value is negative or greater than 255.

@example

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

@example Lowercase encoding:

Encoding::URI::Form.encode_byte(0xff, case: :lower)
# => "%ff"
# File lib/ronin/support/encoding/uri.rb, line 280
def self.encode_byte(byte,**kwargs)
  if byte == 0x20 then '+'
  else                 URI.encode_byte(byte,**kwargs)
  end
end
escape(data,**kwargs) click to toggle source

URI Form escapes the String.

@param [String] data

The data to URI Form 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 Form escaped String.

@example

Encoding::URI::Form.escape("hello world")
# => "hello+world"
Encoding::URI::Form.escape("hello\0world")
# => "hello%00world"

@example Lowercase encoding:

Encoding::URI::Form.escape("hello\xffworld", case: :lower)
# => "hello%ffworld"

@see www.w3.org/TR/2013/CR-html5-20130806/forms.html#url-encoded-form-data

# File lib/ronin/support/encoding/uri.rb, line 350
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 Form escapes the given byte.

@param [Integer] byte

The byte to URI Form 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 Form escaped byte.

@raise [RangeError]

The byte value is negative or greater than 255.

@example

Encoding::URI::Form.escape_byte(0x41)
# => "A"
Encoding::URI::Form.escape_byte(0x20)
# => "+"

@example Lowercase encoding:

Encoding::URI::Form.escape_byte(0xff, case: :lower)
# => "%ff"
# File lib/ronin/support/encoding/uri.rb, line 315
def self.escape_byte(byte,**kwargs)
  if (byte == 42) || (byte == 45) || (byte == 46) || ((byte >= 48) && (byte <= 57)) || ((byte >= 65) && (byte <= 90)) || (byte == 95) || ((byte >= 97) && (byte <= 122))
    byte.chr
  else
    encode_byte(byte,**kwargs)
  end
end
unescape(data) click to toggle source

URI Form unescapes the String.

@param [String] data

The URI Form escaped data to unescape.

@return [String]

The URI Form unescaped String.

@example

Encoding::URI::Form.unescape("hello+world")
# => "hello world"
Encoding::URI::Form.unescape("hello%00world")
# => "hello\u0000world"
# File lib/ronin/support/encoding/uri.rb, line 375
def self.unescape(data)
  data.gsub(/(?:\+|%[A-Fa-f0-9]{2})/) do |escaped_char|
    if escaped_char == '+'
      ' '
    else
      escaped_char[1..].to_i(16).chr
    end
  end
end