Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/ronin/formatting/extensions/binary/integer.rb,
lib/ronin/formatting/extensions/http/integer.rb,
lib/ronin/formatting/extensions/html/integer.rb
Overview
Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com)
This file is part of Ronin Support.
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 <www.gnu.org/licenses/>.
Constant Summary
- 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)
-
- (Array) bytes(address_length, endian = :little)
Extracts a sequence of bytes which represent the Integer.
-
- (String) format_html
Formats the Integer as a HTML String.
-
- (String) format_http
Formats the byte for HTTP.
-
- (String) format_js
Formats the Integer as a JavaScript escaped String.
-
- (String) hex_escape
The hex escaped version of the Integer.
-
- (String) html_escape
Escapes the Integer as an HTML String.
-
- (String) js_escape
Escapes the Integer as a JavaScript String.
-
- (String) pack(arch, address_length = nil)
Packs the Integer into a String, for a specific architecture and address-length.
-
- (String) uri_encode
URI encodes the byte.
-
- (String) uri_escape
URI escapes the byte.
Instance Method Details
- (Array) bytes(address_length, endian = :little)
Extracts a sequence of bytes which represent the Integer.
56 57 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 |
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 56 def bytes(address_length,endian=:little) endian = endian.to_sym buffer = [] case endian when :little mask = 0xff shift = 0 address_length.times do |i| buffer << ((self & mask) >> shift) mask <<= 8 shift += 8 end when :big, :net shift = ((address_length - 1) * 8) mask = (0xff << shift) address_length.times do |i| buffer << ((self & mask) >> shift) mask >>= 8 shift -= 8 end else raise(ArgumentError,"invalid endian #{endian}") end return buffer end |
- (String) format_html
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 |
- (String) format_http
Formats the byte for HTTP.
65 66 67 |
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 65 def format_http "%%%X" % self end |
- (String) format_js
Formats the Integer as a JavaScript escaped String.
138 139 140 141 142 143 144 |
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 138 def format_js if self > 0xff "\\u%.2X%.2X" % [(self & 0xff00) >> 8, (self & 0xff)] else "\\x%.2X" % self end end |
- (String) hex_escape
The hex escaped version of the Integer.
158 159 160 |
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 158 def hex_escape "\\x%.2x" % self end |
- (String) html_escape
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 |
- (String) js_escape
Escapes the Integer as a JavaScript String.
116 117 118 119 120 121 122 |
# File 'lib/ronin/formatting/extensions/html/integer.rb', line 116 def js_escape if self > 0xff format_js else JS_ESCAPE_BYTES.fetch(self,chr) end end |
- (String) pack(arch, address_length = nil)
Packs the Integer into a String, for a specific architecture and address-length.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 127 def pack(arch,address_length=nil) if arch.kind_of?(String) return [self].pack(arch) end 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 end |
- (String) uri_encode
URI encodes the byte.
33 34 35 |
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 33 def uri_encode URI.encode(chr) end |
- (String) uri_escape
URI escapes the byte.
49 50 51 |
# File 'lib/ronin/formatting/extensions/http/integer.rb', line 49 def uri_escape CGI.escape(chr) end |