Class: Ronin::Binary::Template
Overview
Provides a translation layer between C-types and Ruby Array#pack
codes.
Types
:uint8
(C
) - unsigned 8-bit integer.:uint16
(S
) - unsigned 16-bit integer.:uint32
(L
) - unsigned 32-bit integer.:uint64
(Q
) - unsigned 64-bit integer.:int8
(c
) - signed 8-bit integer.:int16
(s
) - signed 16-bit integer.:int32
(l
) - signed 32-bit integer.:int64
(q
) - signed 64-bit integer.:uint16_le
(v
) - unsigned 16-bit integer, little endian.:uint32_le
(V
) - unsigned 32-bit integer, little endian.:uint16_be
(n
) - unsigned 16-bit integer, big endian.:uint32_be
(N
) - unsigned 32-bit integer, big endian.:uchar
(Z
) - unsigned character.:ushort
(S!
) - unsigned short integer, native endian.:uint
(I!
) - unsigned integer, native endian.:ulong
(L!
) - unsigned long integer, native endian.:ulong_long
(Q
) - unsigned quad integer, native endian.:char
(Z
) - signed character.:short
(s!
) - signed short integer, native endian.:int
(i!
) - signed integer, native endian.:long
(l!
) - signed long integer, native endian.:long_long
(q
) - signed quad integer, native endian.:utf8
(U
) - UTF8 character.:float
(F
) - single-precision float, native format.:double
(D
) - double-precision float, native format.:float_le
(e
) - single-precision float, little endian.:double_le
(E
) - double-precision float, little endian.:float_be
(g
) - single-precision float, big endian.: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 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.:int32_le
(l<
) - signed 32-bit integer, little endian.:int64_le
(q<
) - signed 64-bit integer, little endian.:uint16_be
(S>
) - unsigned 16-bit integer, big endian.:uint32_be
(L>
) - unsigned 32-bit integer, big endian.:uint64_be
(Q>
) - unsigned 64-bit integer, big endian.:int16_be
(s>
) - signed 16-bit integer, big endian.:int32_be
(l>
) - signed 32-bit integer, big endian.:int64_be
(q>
) - signed 64-bit integer, big endian.:ushort_le
(S<
) - unsigned short integer, little endian.:uint_le
(I<
) - unsigned integer, little endian.:ulong_le
(L<
) - unsigned long integer, little endian.:ulong_long_le
(Q<
) - unsigned quad integer, little endian.:short_le
(s<
) - signed short integer, little endian.:int_le
(i<
) - signed integer, little endian.:long_le
(l<
) - signed long integer, little endian.:long_long_le
(q<
) - signed quad integer, little endian.:ushort_be
(S>
) - unsigned short integer, little endian.:uint_be
(I>
) - unsigned integer, little endian.:ulong_be
(L>
) - unsigned long integer, little endian.:ulong_long_be
(Q>
) - unsigned quad integer, little endian.:short_be
(s>
) - signed short integer, little endian.:int_be
(i>
) - signed integer, little endian.:long_be
(l>
) - signed long integer, little endian.:long_long_be
(q>
) - signed quad integer, little endian.
Constant Summary collapse
- TYPES =
Supported C-types and corresponding
Array#pack
codes. { :uint8 => 'C', :uint16 => 'S', :uint32 => 'L', :uint64 => 'Q', :int8 => 'c', :int16 => 's', :int32 => 'l', :int64 => 'q', :uint16_le => 'v', :uint32_le => 'V', :uint16_be => 'n', :uint32_be => 'N', :uchar => 'Z', :ushort => 'S!', :uint => 'I!', :ulong => 'L!', :ulong_long => 'Q', :char => 'Z', :short => 's!', :int => 'i!', :long => 'l!', :long_long => 'q', :utf8 => 'U', :float => 'F', :double => 'D', :float_le => 'e', :double_le => 'E', :float_be => 'g', :double_be => 'G', :ubyte => 'C', :byte => 'c', :string => 'Z*' }
- INT_TYPES =
Integer C-types
Set[ :uint8, :uint16, :uint32, :uint64, :int8, :int16, :int32, :int64, :ubyte, :ushort, :uint, :ulong, :ulong_long, :byte, :short, :int, :long, :long_long, :uint16_le, :uint32_le, :uint64_le, :int16_le, :int32_le, :int64_le, :ushort_le, :uint_le, :ulong_le, :ulong_long_le, :short_le, :int_le, :long_le, :long_long_le, :uint16_be, :uint32_be, :uint64_be, :int16_be, :int32_be, :int64_be, :ushort_be, :uint_be, :ulong_be, :ulong_long_be, :short_be, :int_be, :long_be, :long_long_be ]
- FLOAT_TYPES =
Float C-types
Set[ :float, :double, :float_le, :double_le, :float_be, :double_be ]
- CHAR_TYPES =
Character C-types
Set[:uchar, :char]
- STRING_TYPES =
String C-types
CHAR_TYPES + Set[:string]
- ENDIAN_TYPES =
Types which have little and big endian forms
Set[ :uint16, :uint32, :uint64, :int16, :int32, :int64, :ushort, :uint, :ulong, :ulong_long, :short, :int, :long, :long_long, :float, :double ]
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
The fields of the template.
Class Method Summary collapse
-
.[](*fields) ⇒ Object
-
.compile(types, options = {}) ⇒ String
Compiles C-types into an
Array#pack
/String#unpack
template. -
.translate(type, options = {}) ⇒ Symbol
Translates the type of the field.
Instance Method Summary collapse
-
#initialize(fields, options = {}) ⇒ Template
constructor
Creates a new Binary Template.
-
#inspect ⇒ String
Inspects the template.
-
#pack(*data) ⇒ String
Packs the data.
-
#to_s ⇒ String
Converts the template to a
Array#pack
template String. -
#unpack(string) ⇒ Array
Unpacks the string.
Constructor Details
#initialize(fields, options = {}) ⇒ Template
The following C-types are not supported on Ruby 1.8:
:uint16_le
:uint32_le
:uint64_le
:int16_le
:int32_le
:int64_le
:uint16_be
:uint32_be
:uint64_be
:int16_be
:int32_be
:int64_be
:ushort_le
:uint_le
:ulong_le
:ulong_long_le
:short_le
:int_le
:long_le
:long_long_le
:ushort_be
:uint_be
:ulong_be
:ulong_long_be
:short_be
:int_be
:long_be
:long_long_be
Creates a new Binary Template.
324 325 326 327 |
# File 'lib/ronin/binary/template.rb', line 324 def initialize(fields,={}) @fields = fields @template = self.class.compile(@fields,) end |
Instance Attribute Details
#fields ⇒ Object (readonly)
The fields of the template
272 273 274 |
# File 'lib/ronin/binary/template.rb', line 272 def fields @fields end |
Class Method Details
.[](*fields) ⇒ Object
332 333 334 |
# File 'lib/ronin/binary/template.rb', line 332 def self.[](*fields) new(fields) end |
.compile(types, options = {}) ⇒ String
Compiles C-types into an Array#pack
/ String#unpack
template.
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/ronin/binary/template.rb', line 386 def self.compile(types,={}) string = '' types.each do |(type,length)| type = translate(type,) unless (code = TYPES[type]) raise(ArgumentError,"#{type.inspect} not supported") end string << code << length.to_s end return string end |
.translate(type, options = {}) ⇒ Symbol
Translates the type of the field.
354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/ronin/binary/template.rb', line 354 def self.translate(type,={}) if ([:endian] && ENDIAN_TYPES.include?(type)) type = case [:endian] when :little then :"#{type}_le" when :big, :network then :"#{type}_be" else raise(ArgumentError,"unknown endianness: #{type}") end end return type end |
Instance Method Details
#inspect ⇒ String
Inspects the template.
448 449 450 |
# File 'lib/ronin/binary/template.rb', line 448 def inspect "<#{self.class}: #{@fields.inspect}>" end |
#pack(*data) ⇒ String
Packs the data.
411 412 413 |
# File 'lib/ronin/binary/template.rb', line 411 def pack(*data) data.pack(@template) end |
#to_s ⇒ String
Converts the template to a Array#pack
template String.
436 437 438 |
# File 'lib/ronin/binary/template.rb', line 436 def to_s @template end |
#unpack(string) ⇒ Array
Unpacks the string.
424 425 426 |
# File 'lib/ronin/binary/template.rb', line 424 def unpack(string) string.unpack(@template) end |