Class: Integer
- Defined in:
- lib/ronin/formatting/extensions/html/integer.rb,
lib/ronin/formatting/extensions/http/integer.rb,
lib/ronin/formatting/extensions/binary/integer.rb
Constant Summary collapse
- JS_ESCAPE_BYTES =
Special JavaScript bytes and their escaped Strings.
{ 0x00 => '\u0000', 0x01 => '\u0001', 0x02 => '\u0002', 0x03 => '\u0003', 0x04 => '\u0004', 0x05 => '\u0005', 0x06 => '\u0006', 0x07 => '\u0007', 0x08 => '\b', 0x09 => '\t', 0x0a => '\n', 0x0b => '\u000b', 0x0c => '\f', 0x0d => '\r', 0x0e => '\u000e', 0x0f => '\u000f', 0x10 => '\u0010', 0x11 => '\u0011', 0x12 => '\u0012', 0x13 => '\u0013', 0x14 => '\u0014', 0x15 => '\u0015', 0x16 => '\u0016', 0x17 => '\u0017', 0x18 => '\u0018', 0x19 => '\u0019', 0x1a => '\u001a', 0x1b => '\u001b', 0x1c => '\u001c', 0x1d => '\u001d', 0x1e => '\u001e', 0x1f => '\u001f', 0x22 => '\"', 0x5c => '\\\\', }
Instance Method Summary collapse
-
#bytes(length, endian = :little) ⇒ Array
Extracts a sequence of bytes which represent the Integer.
-
#format_html ⇒ String
Formats the Integer as a HTML String.
-
#format_http ⇒ String
Formats the byte for HTTP.
-
#format_js ⇒ String
Formats the Integer as a JavaScript escaped String.
-
#hex_escape ⇒ String
The hex escaped version of the Integer.
-
#html_escape ⇒ String
Escapes the Integer as an HTML String.
-
#js_escape ⇒ String
Escapes the Integer as a JavaScript String.
-
#pack(*arguments) ⇒ String
Packs the Integer into a String.
-
#uri_encode ⇒ String
URI encodes the byte.
-
#uri_escape ⇒ String
URI escapes the byte.
Instance Method Details
#bytes(length, endian = :little) ⇒ Array
Extracts a sequence of bytes which represent the Integer.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 58 def bytes(length,endian=:little) endian = endian.to_sym buffer = [] case endian when :little mask = 0xff shift = 0 length.times do |i| buffer << ((self & mask) >> shift) mask <<= 8 shift += 8 end when :big, :net shift = ((length - 1) * 8) mask = (0xff << shift) length.times do |i| buffer << ((self & mask) >> shift) mask >>= 8 shift -= 8 end else raise(ArgumentError,"invalid endian #{endian}") end return buffer end |
#format_html ⇒ String
Formats the Integer as a HTML String.
94 95 96 |
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 94 def format_html "&#%d;" % self end |
#format_http ⇒ String
Formats the byte for HTTP.
65 66 67 |
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 65 def format_http "%%%X" % self end |
#format_js ⇒ String
Formats the Integer as a JavaScript escaped String.
136 137 138 139 140 |
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 136 def format_js if self > 0xff then "\\u%.4X" % self else "\\x%.2X" % self end end |
#hex_escape ⇒ String
Returns The hex escaped version of the Integer.
174 175 176 |
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 174 def hex_escape "\\x%.2x" % self end |
#html_escape ⇒ String
Escapes the Integer as an HTML String.
76 77 78 |
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 76 def html_escape CGI.escapeHTML(chr) end |
#js_escape ⇒ String
Escapes the Integer as a JavaScript String.
116 117 118 119 120 |
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 116 def js_escape if self > 0xff then format_js else JS_ESCAPE_BYTES.fetch(self,chr) end end |
#pack(*arguments) ⇒ String
Packs the Integer into a String.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 130 def pack(*arguments) if (arguments.length == 1 && arguments.first.kind_of?(String)) [self].pack(arguments.first) elsif (arguments.length == 1 && arguments.first.kind_of?(Symbol)) type = arguments.first unless Ronin::Binary::Template::INT_TYPES.include?(type) raise(ArgumentError,"unsupported integer type: #{type}") end [self].pack(Ronin::Binary::Template::TYPES[type]) elsif (arguments.length == 1 || arguments.length == 2) # TODO: deprecate this calling convention arch, address_length = arguments unless arch.respond_to?(:address_length) raise(ArgumentError,"first argument to Ineger#pack must respond to address_length") end unless arch.respond_to?(:endian) raise(ArgumentError,"first argument to Ineger#pack must respond to endian") end address_length ||= arch.address_length integer_bytes = bytes(address_length,arch.endian) integer_bytes.map! { |b| b.chr } return integer_bytes.join else raise(ArgumentError,"wrong number of arguments (#{arguments.length} for 1..2)") end end |
#uri_encode ⇒ String
URI encodes the byte.
33 34 35 |
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 33 def uri_encode URI::DEFAULT_PARSER.escape(chr) end |
#uri_escape ⇒ String
URI escapes the byte.
49 50 51 |
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 49 def uri_escape CGI.escape(chr) end |