lib/ronin/binary/template.rb in ronin-support-0.5.0 vs lib/ronin/binary/template.rb in ronin-support-0.5.1

- old
+ new

@@ -20,11 +20,11 @@ require 'set' module Ronin module Binary # - # Provides a translation layer between C types and Ruby `Array#pack` + # Provides a translation layer between C-types and Ruby `Array#pack` # codes. # # ## Types # # * `:uint8` (`C`) - unsigned 8-bit integer. @@ -58,11 +58,11 @@ # * `:double_be` (`G`) - double-precision float, big endian. # * `:ubyte` (`C`) - unsigned byte. # * `:byte` (`c`) - signed byte. # * `:string` (`Z*`) - binary String, `\0` terminated. # - # ### Ruby 1.9 specific types + # ### Ruby 1.9 specific C-types # # * `:uint16_le` (`S<`) - unsigned 16-bit integer, little endian. # * `:uint32_le` (`L<`) - unsigned 32-bit integer, little endian. # * `:uint64_le` (`Q<`) - unsigned 64-bit integer, little endian. # * `:int16_le` (`s<`) - signed 16-bit integer, little endian. @@ -97,11 +97,11 @@ # # @since 0.5.0 # class Template - # Supported types and corresponding `Array#pack` codes. + # Supported C-types and corresponding `Array#pack` codes. TYPES = { :uint8 => 'C', :uint16 => 'S', :uint32 => 'L', :uint64 => 'Q', @@ -142,11 +142,11 @@ :ubyte => 'C', :byte => 'c', :string => 'Z*' } - # Additional types, not available on Ruby 1.8: + # Additional C-types, not available on Ruby 1.8: if RUBY_VERSION > '1.9.' TYPES.merge!( :uint16_le => 'S<', :uint32_le => 'L<', :uint64_le => 'Q<', @@ -183,11 +183,11 @@ :long_be => 'l!>', :long_long_be => 'q>' ) end - # Integer types + # Integer C-types INT_TYPES = Set[ :uint8, :uint16, :uint32, :uint64, @@ -244,21 +244,21 @@ :int_be, :long_be, :long_long_be ] - # Float types + # Float C-types FLOAT_TYPES = Set[ :float, :double, :float_le, :double_le, :float_be, :double_be ] - # Character types + # Character C-types CHAR_TYPES = Set[:uchar, :char] - # String types + # String C-types STRING_TYPES = CHAR_TYPES + Set[:string] # Types which have little and big endian forms ENDIAN_TYPES = Set[ :uint16, :uint32, :uint64, @@ -273,23 +273,23 @@ # # Creates a new Binary Template. # # @param [Array<type, (type, length)>] fields - # The types which the packer will use. + # The C-types which the packer will use. # # @param [Hash] options # Template options. # # @option options [:little, :big, :network] :endian - # The endianness to apply to types. + # The endianness to apply to the C-types. # # @raise [ArgumentError] # A given type is not known. # # @note - # The following types are **not supported** on Ruby 1.8: + # The following C-types are **not supported** on Ruby 1.8: # # * `:uint16_le` # * `:uint32_le` # * `:uint64_le` # * `:int16_le` @@ -341,45 +341,43 @@ # # @param [Hash] options # Translation options. # # @option options [:little, :big, :network] :endian - # The endianness to apply to types. + # The endianness to apply to the C-types. # # @return [Symbol] # The translated type. # # @raise [ArgumentError] # The value of `:endian` is unknown. # def self.translate(type,options={}) if (options[:endian] && ENDIAN_TYPES.include?(type)) type = case options[:endian] - when :little - :"#{type}_le" - when :big, :network - :"#{type}_be" + when :little then :"#{type}_le" + when :big, :network then :"#{type}_be" else raise(ArgumentError,"unknown endianness: #{type}") end end return type end # - # Compiles binary types into an `Array#pack` / `String#unpack` + # Compiles C-types into an `Array#pack` / `String#unpack` # template. # # @param [Array<type, (type, length)>] types - # The types which the packer will use. + # The C-types which the packer will use. # # @param [Hash] options # Type options. # # @option options [:little, :big, :network] :endian - # The endianness to apply to types. + # The endianness to apply to the C-types. # # @return [String] # The `Array#pack` / `String#unpack` template. # # @raise [ArgumentError] @@ -435,9 +433,21 @@ # # @see http://rubydoc.info/stdlib/core/Array:pack # def to_s @template + end + + # + # Inspects the template. + # + # @return [String] + # The inspected template. + # + # @since 1.5.1 + # + def inspect + "<#{self.class}: #{@fields.inspect}>" end end end end