lib/ronin/formatting/extensions/binary/integer.rb in ronin-support-0.5.0 vs lib/ronin/formatting/extensions/binary/integer.rb in ronin-support-0.5.1
- old
+ new
@@ -96,11 +96,12 @@
#
# @return [String]
# The packed Integer.
#
# @raise [ArgumentError]
- # The arguments were not a String, Symbol or Architecture object.
+ # The given Symbol could not be found in
+ # {Ronin::Binary::Template::INT_TYPES}.
#
# @example using a `Array#pack` template:
# 0x41.pack('V')
# # => "A\0\0\0"
#
@@ -120,26 +121,26 @@
# @example specifying a custom address-length (**deprecated**):
# 0x41.pack(Arch.ppc,2)
# # => "\0A"
#
# @see http://rubydoc.info/stdlib/core/Array:pack
+ # @see Ronin::Binary::Template
#
# @api public
#
def pack(*arguments)
- argument = arguments.first
+ 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
- case argument
- when String
- [self].pack(argument)
- when Symbol
- unless Ronin::Binary::Template::INT_TYPES.include?(argument)
- raise(ArgumentError,"unsupported integer type: #{argument}")
+ unless Ronin::Binary::Template::INT_TYPES.include?(type)
+ raise(ArgumentError,"unsupported integer type: #{type}")
end
- [self].pack(Ronin::Binary::Template::TYPES[argument])
- else
+ [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")
@@ -153,9 +154,11 @@
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
#
# @return [String]