lib/ronin/formatting/extensions/binary/string.rb in ronin-support-0.5.0 vs lib/ronin/formatting/extensions/binary/string.rb in ronin-support-0.5.1

- old
+ new

@@ -34,41 +34,39 @@ alias unpack_original unpack # # Unpacks the String. # - # @param [String, Array<Symbol>] arguments + # @param [String, Array<Symbol, (Symbol, Integer)>] arguments # The `String#unpack` template or a list of {Ronin::Binary::Template} types. # # @return [Array] # The values unpacked from the String. # # @raise [ArgumentError] - # The arguments were not a String or a list of Symbols. + # One of the arguments was not a known {Ronin::Binary::Template} type. # # @example using {Ronin::Binary::Template} types: # "A\0\0\0hello\0".unpack(:uint32_le, :string) # # => [10, "hello"] # # @example using a `String#unpack` template: # "A\0\0\0".unpack('V') # # => 65 # # @see http://rubydoc.info/stdlib/core/String:unpack + # @see Ronin::Binary::Template # # @since 0.5.0 # # @api public # def unpack(*arguments) - case arguments.first - when String + if (arguments.length == 1 && arguments.first.kind_of?(String)) unpack_original(arguments.first) - when Symbol - unpack_original(Ronin::Binary::Template.compile(arguments)) else - raise(ArgumentError,"first argument to String#unpack must be a String or Symbol") + unpack_original(Ronin::Binary::Template.compile(arguments)) end end # # Unpacks the String into an Integer. @@ -188,16 +186,13 @@ # # @api public # def xor(key) key = case key - when Integer - [key] - when String - key.bytes - else - key + when Integer then [key] + when String then key.bytes + else key end key = key.cycle result = '' @@ -227,16 +222,13 @@ # # @api public # def base64_encode(mode=nil) case mode - when :strict - Base64.strict_encode64(self) - when :url, :urlsafe - Base64.urlsafe_encode64(self) - else - Base64.encode64(self) + when :strict then Base64.strict_encode64(self) + when :url, :urlsafe then Base64.urlsafe_encode64(self) + else Base64.encode64(self) end end # # Base64 decodes a string. @@ -260,15 +252,12 @@ # # @api public # def base64_decode(mode=nil) case mode - when :strict - Base64.strict_decode64(self) - when :url, :urlsafe - Base64.urlsafe_decode64(self) - else - Base64.decode64(self) + when :strict then Base64.strict_decode64(self) + when :url, :urlsafe then Base64.urlsafe_decode64(self) + else Base64.decode64(self) end end # # Zlib inflate a string.