Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/core_ext/integer.rb,
lib/ronin/support/binary/core_ext/integer.rb,
lib/ronin/support/encoding/c/core_ext/integer.rb,
lib/ronin/support/encoding/js/core_ext/integer.rb,
lib/ronin/support/encoding/hex/core_ext/integer.rb,
lib/ronin/support/encoding/uri/core_ext/integer.rb,
lib/ronin/support/encoding/xml/core_ext/integer.rb,
lib/ronin/support/encoding/html/core_ext/integer.rb,
lib/ronin/support/encoding/http/core_ext/integer.rb,
lib/ronin/support/encoding/shell/core_ext/integer.rb,
lib/ronin/support/binary/bit_flip/core_ext/integer.rb,
lib/ronin/support/encoding/powershell/core_ext/integer.rb

Overview

Copyright (c) 2006-2023 Hal Brodigan (postmodern.mod3 at gmail.com)

ronin-support is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

ronin-support is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with ronin-support. If not, see https://www.gnu.org/licenses/.

Instance Method Summary collapse

Instance Method Details

#bit_flips(bits) ⇒ Array<Integer> Also known as: flip_bits

Returns every bit flip in the integer.

Examples:

bit-flip all eight bits:

0x41.bit_flips(8)

bit-flip bits 8-16:

0xffff.bit_flips(8...16)

Parameters:

  • bits (Integer, Range(Integer))

    The number of bits to flip or a range of bit indexes to flip.

Returns:

Raises:

  • (ArgumentError)

    The given bits must be either a Range or an Integer.



73
74
75
# File 'lib/ronin/support/binary/bit_flip/core_ext/integer.rb', line 73

def bit_flips(bits)
  Ronin::Support::Binary::BitFlip::Integer.bit_flips(self,bits)
end

#c_encodeString

Formats the Integer as a C escaped String.

Examples:

0x41.c_encode
# => "\\x41"
0x100.c_encode
# => "\\u1000"
0x10000.c_encode
# => "\\U000100000"

Returns:

  • (String)

    The escaped C character.

See Also:

Since:

  • 1.0.0



78
79
80
# File 'lib/ronin/support/encoding/c/core_ext/integer.rb', line 78

def c_encode
  Ronin::Support::Encoding::C.encode_byte(self)
end

#c_escapeString Also known as: c_char

Escapes the Integer as a C character.

Examples:

0x41.c_escape
# => "A"
0x22.c_escape
# => "\\\""
0x7f.c_escape
# => "\\x7F"

Escaping unicode characters:

0xffff.c_escape
# => "\\uFFFF"
0x10000.c_escape
# => "\\U000100000"

Returns:

  • (String)

    The escaped C character.

Raises:

  • (RangeError)

    The integer value is negative.

See Also:

Since:

  • 1.0.0



52
53
54
# File 'lib/ronin/support/encoding/c/core_ext/integer.rb', line 52

def c_escape
  Ronin::Support::Encoding::C.escape_byte(self)
end

#each_bit_flip(bits) {|int| ... } ⇒ Enumerator

Enumerates over every bit flip in the integer.

Examples:

bit-flip all eight bits:

0x41.each_bit_flip(8) { |int| puts "%.8b" % int }

bit-flip bits 8-16:

0xffff.each_bit_flip(8...16) { |int| puts "%.16b" % int }

Parameters:

  • bits (Integer, Range(Integer))

    The number of bits to flip or a range of bit indexes to flip.

Yields:

  • (int)

    If a block is given, it will be passed each bit-flipped integer.

Yield Parameters:

  • int (Integer)

    The integer but with one of it's bits flipped.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.

Raises:

  • (ArgumentError)

    The given bits must be either a Range or an Integer.



49
50
51
# File 'lib/ronin/support/binary/bit_flip/core_ext/integer.rb', line 49

def each_bit_flip(bits,&block)
  Ronin::Support::Binary::BitFlip::Integer.each_bit_flip(self,bits,&block)
end

#hex_encodeString Also known as: to_hex

Converts the integer into hex format.

Examples:

0x41.hex_encode
# => "41"

Returns:

  • (String)

    The hex encoded version of the Integer.

See Also:

Since:

  • 0.6.0



37
38
39
# File 'lib/ronin/support/encoding/hex/core_ext/integer.rb', line 37

def hex_encode
  Ronin::Support::Encoding::Hex.encode_byte(self)
end

#hex_escapeString Also known as: hex_char

Converts the integer into an escaped hex character.

Examples:

42.hex_char
# => "\\x2a"

Returns:

  • (String)

    The hex escaped version of the Integer.

Raises:

  • (RangeError)

    The integer value is negative.

See Also:



60
61
62
# File 'lib/ronin/support/encoding/hex/core_ext/integer.rb', line 60

def hex_escape
  Ronin::Support::Encoding::Hex.escape_byte(self)
end

#hex_intString

Encodes the number as a 0xXX hex integer.

Examples:

42.hex_int
# => "0x2e"

Returns:

  • (String)

    The hex encoded integer.



78
79
80
# File 'lib/ronin/support/encoding/hex/core_ext/integer.rb', line 78

def hex_int
  "0x%.2x" % self
end

#html_encode(**kwargs) ⇒ String

Encodes the Integer as a HTML String.

Examples:

0x41.html_encode
# => "&#65;"

Zero-padding:

0x41.html_encode(zero_pad: true)
# => "&#0000065;"

Hexadecimal escaped characters:

0x41.html_encode(format: :hex)
# => "&#x41;"

Uppercase hexadecimal escaped characters:

0xff.html_encode(format: :hex, case: :upper)
# => "&#XFF;"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :format (:decimal, :hex) — default: :decimal

    The numeric format for the escaped characters.

  • :zero_pad (Boolean)

    Controls whether the escaped characters will be left-padded with up to seven 0 characters.

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase XML special characters. Defaults to lowercase hexadecimal.

Returns:

  • (String)

    The encoded HTML String.

Raises:

  • (ArgumentError)

    The format: or case: keyword argument is invalid.

See Also:

Since:

  • 0.2.0



102
103
104
# File 'lib/ronin/support/encoding/html/core_ext/integer.rb', line 102

def html_encode(**kwargs)
  Ronin::Support::Encoding::HTML.encode_byte(self,**kwargs)
end

#html_escape(**kwargs) ⇒ String

Escapes the Integer as an HTML String.

Examples:

0x26.html_escape
# => "&amp;"

Uppercase encoding:

0x26.html_escape(case: :upper)
# => "&AMP;"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase XML special characters. Defaults to lowercase hexadecimal.

Returns:

  • (String)

    The escaped HTML String.

Raises:

  • (ArgumentError)

    The case: keyword argument is invalid.

See Also:

Since:

  • 0.2.0



53
54
55
# File 'lib/ronin/support/encoding/html/core_ext/integer.rb', line 53

def html_escape(**kwargs)
  Ronin::Support::Encoding::HTML.escape_byte(self,**kwargs)
end

#http_encode(**kwargs) ⇒ String

Encodes the byte as an escaped HTTP decimal character.

Examples:

0x41.http_encode
# => "%41"

Lowercase encoding:

0xff.http_encode(case: :lower)
# => "%ff"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The encoded HTTP byte.

Raises:

  • (ArgumentError)

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

  • (RangeError)

    The byte value is negative or greater than 255.

See Also:



54
55
56
# File 'lib/ronin/support/encoding/http/core_ext/integer.rb', line 54

def http_encode(**kwargs)
  Ronin::Support::Encoding::HTTP.encode_byte(self,**kwargs)
end

#http_escape(**kwargs) ⇒ String

HTTP escapes the Integer.

Examples:

62.http_escape
# => "%3E"

Lowercase encoding:

0xff.http_escape(case: :lower)
# => "%ff"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The HTTP escaped form of the Integer.

Raises:

  • (ArgumentError)

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

  • (RangeError)

    The byte value is negative or greater than 255.

See Also:

Since:

  • 0.6.0



91
92
93
# File 'lib/ronin/support/encoding/http/core_ext/integer.rb', line 91

def http_escape(**kwargs)
  Ronin::Support::Encoding::HTTP.escape_byte(self,**kwargs)
end

#js_encodeString

Encodes the Integer as a JavaScript character.

Examples:

0x41.js_encode
# => "\\x41"

Returns:

  • (String)

    The encoded JavaScript character.

See Also:

Since:

  • 1.0.0



63
64
65
# File 'lib/ronin/support/encoding/js/core_ext/integer.rb', line 63

def js_encode
  Ronin::Support::Encoding::JS.encode_byte(self)
end

#js_escapeString

Escapes the Integer as a JavaScript character.

Examples:

0x41.js_escape
# => "A"
0x22.js_escape
# => "\\\""
0x7f.js_escape
# => "\\x7F"

Returns:

  • (String)

    The escaped JavaScript character.

See Also:

Since:

  • 0.2.0



43
44
45
# File 'lib/ronin/support/encoding/js/core_ext/integer.rb', line 43

def js_escape
  Ronin::Support::Encoding::JS.escape_byte(self)
end

#pack(argument, **kwargs) ⇒ String

Packs the Integer into a String.

The desired architecture of the binary format.

The Operating System name to lookup.

Examples:

using a Array#pack format string:

0x41.pack('V')
# => "A\0\0\0"

0x41.pack(:uint32_le)
# => "A\x00\x00\x00"

specifying the endian-ness:

0x41.pack(:uint32, endian: :big)
# => "\x00\x00\x00A"

specifying the architecture:

0x41.pack(:ulong, arch: :arm64)
# => "A\x00\x00\x00\x00\x00\x00\x00"

specifying the architecture and Operating System (OS):

0x41.pack(:size_t, arch: :arm64, os: :linux)
# => "A\x00\x00\x00\x00\x00\x00\x00"

Parameters:

Options Hash (**kwargs):

  • :endian (:little, :big, :net, nil)

    The desired endianness of the binary format.

  • :arch (:x86, :x86_64, :ppc, :ppc64, :mips, :mips_le, :mips_be, :mips64, :mips64_le, :mips64_be, :arm, :arm_le, :arm_be, :arm64, :arm64_le, :arm64_be)
  • :os (:linux, :macos, :windows, :bsd, :freebsd, :openbsd, :netbsd)

Returns:

  • (String)

    The packed Integer.

Raises:

  • (ArgumentError)

    The given argument was not a String, Symbol, or valid type name.

See Also:



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 79

def pack(argument, **kwargs)
  case argument
  when String
    [self].pack(argument)
  when Symbol
    types = Ronin::Support::Binary::CTypes.platform(**kwargs)
    type  = types[argument]
    type.pack(self)
  else
    raise(ArgumentError,"invalid pack argument: #{argument}")
  end
end

#powershell_encodeString Also known as: psh_encode

Encodes the Integer as a PowerShell character.

Examples:

0x41.powershell_encode
# => "[char]0x41"
0x0a.powershell_encode
# => "`n"

Encoding unicode characters:

1001.powershell_escape
# => "`u{1001}"

Returns:

  • (String)

    The encoded PowerShell character.

Raises:

  • (RangeError)

    The integer value is negative.

See Also:

Since:

  • 1.0.0



48
49
50
# File 'lib/ronin/support/encoding/powershell/core_ext/integer.rb', line 48

def powershell_encode
  Ronin::Support::Encoding::PowerShell.encode_byte(self)
end

#powershell_escapeString Also known as: psh_escape

Escapes the Integer as a PowerShell character.

Examples:

0x41.powershell_escape
# => "A"
0x08.powershell_escape
# => "`b"
0xff.powershell_escape
# => "[char]0xff"

Escaping unicode characters:

1001.powershell_escape
# => "`u{1001}"

Returns:

  • (String)

    The escaped PowerShell character.

Raises:

  • (RangeError)

    The integer value is negative.

See Also:

Since:

  • 1.0.0



81
82
83
# File 'lib/ronin/support/encoding/powershell/core_ext/integer.rb', line 81

def powershell_escape
  Ronin::Support::Encoding::PowerShell.escape_byte(self)
end

#shell_encodeString Also known as: sh_encode, bash_encode

Encodes the Integer as a shell character.

Examples:

0x41.shell_encode
# => "\\x41"
0x0a.shell_encode
# => "\\n"

Encoding unicode characters:

1001.shell_encode
# => "\\u1001"

Returns:

  • (String)

    The encoded shell character.

Raises:

  • (RangeError)

    The integer value is negative.

See Also:

Since:

  • 1.0.0



48
49
50
# File 'lib/ronin/support/encoding/shell/core_ext/integer.rb', line 48

def shell_encode
  Ronin::Support::Encoding::Shell.encode_byte(self)
end

#shell_escapeString Also known as: sh_escape, bash_escape

Escapes the Integer as a shell character.

Examples:

0x41.shell_escape
# => "A"
0x08.shell_escape
# => "\b"
0xff.shell_escape
# => "\xff"

Escaping unicode characters:

1001.shell_escape
# => "\\u1001"

Returns:

  • (String)

    The escaped shell character.

Raises:

  • (RangeError)

    The integer value is negative.

See Also:

Since:

  • 1.0.0



82
83
84
# File 'lib/ronin/support/encoding/shell/core_ext/integer.rb', line 82

def shell_escape
  Ronin::Support::Encoding::Shell.escape_byte(self)
end

#to_int16Integer Also known as: to_i16

Converts the integer into an 16-bit signed integer.

Returns:

  • (Integer)

    The integer truncated to 16-bits with signed preserved.

Since:

  • 1.0.0



189
190
191
192
193
194
195
196
197
198
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 189

def to_int16
  int = self & 0xffff

  if int[15] == 1
    # interpret the new signed bit
    int -= 0x10000
  end

  return int
end

#to_int32Integer Also known as: to_i32

Converts the integer into an 32-bit signed integer.

Returns:

  • (Integer)

    The integer truncated to 32-bits with signed preserved.

Since:

  • 1.0.0



212
213
214
215
216
217
218
219
220
221
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 212

def to_int32
  int = self & 0xffffffff

  if int[31] == 1
    # interpret the new signed bit
    int -= 0x100000000
  end

  return int
end

#to_int64Integer Also known as: to_i64

Converts the integer into an 64-bit signed integer.

Returns:

  • (Integer)

    The integer truncated to 64-bits with signed preserved.

Since:

  • 1.0.0



235
236
237
238
239
240
241
242
243
244
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 235

def to_int64
  int = self & 0xffffffffffffffff

  if int[63] == 1
    # interpret the new signed bit
    int -= 0x10000000000000000
  end

  return int
end

#to_int8Integer Also known as: to_i8

Converts the integer into an 8-bit signed integer.

Returns:

  • (Integer)

    The integer truncated to 8-bits with signed preserved.

Since:

  • 1.0.0



166
167
168
169
170
171
172
173
174
175
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 166

def to_int8
  int = self & 0xff

  if int[7] == 1
    # interpret the new signed bit
    int -= 0x100
  end

  return int
end

#to_uint16Integer Also known as: to_u16

Converts the integer into an 16-bit unsigned integer.

Returns:

  • (Integer)

    The integer truncated to 16-bits.

Since:

  • 1.0.0



118
119
120
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 118

def to_uint16
  self & 0xffff
end

#to_uint32Integer Also known as: to_u32

Converts the integer into an 32-bit unsigned integer.

Returns:

  • (Integer)

    The integer truncated to 32-bits.

Since:

  • 1.0.0



134
135
136
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 134

def to_uint32
  self & 0xffffffff
end

#to_uint64Integer Also known as: to_u64

Converts the integer into an 64-bit unsigned integer.

Returns:

  • (Integer)

    The integer truncated to 64-bits.

Since:

  • 1.0.0



150
151
152
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 150

def to_uint64
  self & 0xffffffffffffffff
end

#to_uint8Integer Also known as: to_u8

Converts the integer into an 8-bit unsigned integer.

Returns:

  • (Integer)

    The integer truncated to 8-bits.

Since:

  • 1.0.0



102
103
104
# File 'lib/ronin/support/binary/core_ext/integer.rb', line 102

def to_uint8
  self & 0xff
end

#uri_encode(**kwargs) ⇒ String

URI encodes the byte.

Examples:

0x41.uri_encode
# => "%41"

Lowercase encoding:

0xff.uri_encode(case: :lower)
# => "%ff"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The URI encoded byte.

See Also:



48
49
50
# File 'lib/ronin/support/encoding/uri/core_ext/integer.rb', line 48

def uri_encode(**kwargs)
  Ronin::Support::Encoding::URI.encode_byte(self,**kwargs)
end

#uri_escape(**kwargs) ⇒ String

URI escapes the byte.

Examples:

0x41.uri_escape
# => "A"
0x3d.uri_escape
# => "%3D"

Lowercase encoding:

0xff.uri_escape(case: :lower)
# => "%ff"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The URI escaped byte.

See Also:



79
80
81
# File 'lib/ronin/support/encoding/uri/core_ext/integer.rb', line 79

def uri_escape(**kwargs)
  Ronin::Support::Encoding::URI.escape_byte(self,**kwargs)
end

#uri_form_encode(**kwargs) ⇒ String

URI Form encodes the Integer.

Examples:

0x41.uri_form_encode
# => "%41"
0x20.uri_form_encode
# => "+"

Lowercase encoding:

0xff.uri_form_encode(case: :lower)
# => "%ff"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The URI Form encoded Integer.

See Also:

Since:

  • 1.0.0



145
146
147
# File 'lib/ronin/support/encoding/uri/core_ext/integer.rb', line 145

def uri_form_encode(**kwargs)
  Ronin::Support::Encoding::URI::Form.encode_byte(self,**kwargs)
end

#uri_form_escape(**kwargs) ⇒ String

URI Form escapes the Integer.

Examples:

0x41.uri_form_ecape
# => "A"
0x20.uri_form_escape
# => "+"

Lowercase encoding:

0xff.uri_form_escape(case: :lower)
# => "%ff"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The URI Form escaped Integer.

See Also:

Since:

  • 1.0.0



112
113
114
# File 'lib/ronin/support/encoding/uri/core_ext/integer.rb', line 112

def uri_form_escape(**kwargs)
  Ronin::Support::Encoding::URI::Form.escape_byte(self,**kwargs)
end

#xml_encode(**kwargs) ⇒ String

Encodes the Integer as a XML String.

Examples:

0x41.xml_encode
# => "&#65;"

Zero-padding:

0x41.xml_encode(zero_pad: true)
# => "&#0000065;"

Hexadecimal escaped characters:

0x41.xml_encode(format: :hex)
# => "&#x41;"

Uppercase hexadecimal escaped characters:

0xff.xml_encode(format: :hex, case: :upper)
# => "&#XFF;"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :format (:decimal, :hex) — default: :decimal

    The numeric format for the escaped characters.

  • :zero_pad (Boolean)

    Controls whether the escaped characters will be left-padded with up to seven 0 characters.

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase XML special characters. Defaults to lowercase hexadecimal.

Returns:

  • (String)

    The XML String.

See Also:

Since:

  • 0.2.0



96
97
98
# File 'lib/ronin/support/encoding/xml/core_ext/integer.rb', line 96

def xml_encode(**kwargs)
  Ronin::Support::Encoding::XML.encode_byte(self,**kwargs)
end

#xml_escape(**kwargs) ⇒ String

Escapes the Integer as an XML String.

Examples:

0x26.xml_escape
# => "&amp;"

Uppercase encoding:

0x26.xml_escape(case: :upper)
# => "&AMP;"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase XML special characters. Defaults to lowercase hexadecimal.

Returns:

  • (String)

    The escaped XML String.

See Also:

Since:

  • 0.2.0



50
51
52
# File 'lib/ronin/support/encoding/xml/core_ext/integer.rb', line 50

def xml_escape(**kwargs)
  Ronin::Support::Encoding::XML.escape_byte(self,**kwargs)
end