lib/ronin/formatting/extensions/binary/string.rb in ronin-support-0.2.0 vs lib/ronin/formatting/extensions/binary/string.rb in ronin-support-0.3.0
- old
+ new
@@ -82,37 +82,40 @@
unless arch.respond_to?(:endian)
raise(ArgumentError,"first argument to Ineger#pack must respond to endian")
end
+ endian = arch.endian.to_sym
address_length ||= arch.address_length
integer = 0x0
byte_index = 0
- case arch.endian
- when :little, 'little'
+ case endian
+ when :little
mask = lambda { |b| b << (byte_index * 8) }
- when :big, 'big'
+ when :big
mask = lambda { |b|
b << ((address_length - byte_index - 1) * 8)
}
else
raise(ArgumentError,"invalid endian #{arch.endian.inspect}")
end
- self.each_byte do |b|
+ each_byte do |b|
break if byte_index >= address_length
integer |= mask.call(b)
byte_index += 1
end
return integer
end
#
+ # Hex-escapes characters in the String.
+ #
# @return [String]
# The hex escaped version of the String.
#
# @example
# "hello".hex_escape
@@ -205,30 +208,39 @@
# # => "=$\x8d9.\xc14&\x80</"
#
# @api public
#
def xor(key)
- key = if key.kind_of?(Integer)
+ key = case key
+ when Integer
[key]
- elsif key.kind_of?(String)
+ when String
key.bytes
else
key
end
- key = key.cycle
+ key = key.cycle
result = ''
- self.bytes.inject('') { |result,b| result << (b ^ key.next).chr }
+ bytes.each do |b|
+ result << (b ^ key.next).chr
+ end
+
+ return result
end
#
# Base64 encodes a string.
#
# @return [String]
# The base64 encoded form of the string.
#
+ # @example
+ # "hello".base64_encode
+ # # => "aGVsbG8=\n"
+ #
# @api public
#
def base64_encode
Base64.encode64(self)
end
@@ -237,10 +249,14 @@
# Base64 decodes a string.
#
# @return [String]
# The base64 decoded form of the string.
#
+ # @example
+ # "aGVsbG8=\n"
+ # # => "hello"
+ #
# @api public
#
def base64_decode
Base64.decode64(self)
end
@@ -249,10 +265,14 @@
# Zlib inflate a string.
#
# @return [String]
# The Zlib inflated form of the string.
#
+ # @example
+ # "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15".zlib_inflate
+ # # => "hello"
+ #
# @api public
#
def zlib_inflate
Zlib::Inflate.inflate(self)
end
@@ -261,10 +281,14 @@
# Zlib deflate a string.
#
# @return [String]
# The Zlib deflated form of the string.
#
+ # @example
+ # "hello".zlib_deflate
+ # # => "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15"
+ #
# @api public
#
def zlib_deflate
Zlib::Deflate.deflate(self)
end
@@ -280,10 +304,11 @@
# `:hexdump`.
#
# @option options [Symbol] :encoding
# Denotes the encoding used for the bytes within the hexdump.
# Must be one of the following:
+ #
# * `:binary`
# * `:octal`
# * `:octal_bytes`
# * `:octal_shorts`
# * `:octal_ints`
@@ -307,18 +332,21 @@
# @api public
#
def unhexdump(options={})
case (format = options[:format])
when :od
- address_base = base = 8
- word_size = 2
+ address_base = 8
+ base = 8
+ word_size = 2
when :hexdump
- address_base = base = 16
- word_size = 2
+ address_base = 16
+ base = 16
+ word_size = 2
else
- address_base = base = 16
- word_size = 1
+ address_base = 16
+ base = 16
+ word_size = 1
end
case options[:encoding]
when :binary
base = 2
@@ -377,15 +405,15 @@
else
segment += word.to_i(base).bytes(word_size)
end
end
- segment = segment[0...segment_length]
+ segment = segment[0,segment_length]
buffer += segment
last_addr = current_addr
end
end
- return buffer[0...(last_addr - first_addr)]
+ return buffer[0,(last_addr - first_addr)]
end
end