Class: Ronin::Support::Binary::Template
- Inherits:
-
Object
- Object
- Ronin::Support::Binary::Template
- Includes:
- CTypes::Mixin
- Defined in:
- lib/ronin/support/binary/template.rb
Overview
Provides a translation layer between C-types and Ruby Array#pack
codes.
Supported Types
:uint8- unsigned 8-bit integer.:uint16- unsigned 16-bit integer.:uint32- unsigned 32-bit integer.:uint64- unsigned 64-bit integer.:int8- signed 8-bit integer.:int16- signed 16-bit integer.:int32- signed 32-bit integer.:int64- signed 64-bit integer.:uchar- unsigned character.:ushort- unsigned short integer, native endian.:uint- unsigned integer, native endian.:ulong- unsigned long integer, native endian.:ulong_long- unsigned quad integer, native endian.:char- signed character.:short- signed short integer, native endian.:int- signed integer, native endian.:long- signed long integer, native endian.:long_long- signed quad integer, native endian.:float- single-precision float, native format.:double- double-precision float, native format.:float_le- single-precision float, little endian.:double_le- double-precision float, little endian.:float_be- single-precision float, big endian.:double_be- double-precision float, big endian.:byte- signed byte.:string- binary String,\0terminated.:uint16_le- unsigned 16-bit integer, little endian.:uint32_le- unsigned 32-bit integer, little endian.:uint64_le- unsigned 64-bit integer, little endian.:int16_le- signed 16-bit integer, little endian.:int32_le- signed 32-bit integer, little endian.:int64_le- signed 64-bit integer, little endian.:uint16_be- unsigned 16-bit integer, big endian.:uint32_be- unsigned 32-bit integer, big endian.:uint64_be- unsigned 64-bit integer, big endian.:int16_be- signed 16-bit integer, big endian.:int32_be- signed 32-bit integer, big endian.:int64_be- signed 64-bit integer, big endian.:ushort_le- unsigned short integer, little endian.:uint_le- unsigned integer, little endian.:ulong_le- unsigned long integer, little endian.:ulong_long_le- unsigned quad integer, little endian.:short_le- signed short integer, little endian.:int_le- signed integer, little endian.:long_le- signed long integer, little endian.:long_long_le- signed quad integer, little endian.:ushort_be- unsigned short integer, little endian.:uint_be- unsigned integer, little endian.:ulong_be- unsigned long integer, little endian.:ulong_long_be- unsigned quad integer, little endian.:short_be- signed short integer, little endian.:int_be- signed integer, little endian.:long_be- signed long integer, little endian.:long_long_be- signed quad integer, little endian.
Instance Attribute Summary collapse
-
#fields ⇒ ::Array<Symbol, (Symbol, Integer), Range(Symbol)>
readonly
The fields of the binary template.
-
#pack_string ⇒ String
readonly
The
Array#packstring for the binary template. -
#types ⇒ ::Array<CTypes::Type, CTypes::ArrayType, CTypes::UnboundArrayType>
readonly
The field types of the binary template.
Attributes included from CTypes::Mixin
#arch, #endian, #os, #type_resolver, #type_system
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(fields, **kwargs) ⇒ Template
constructor
Creates a new Binary template.
-
#pack(*arguments) ⇒ String
Packs the data.
-
#to_s ⇒ String
(also: #to_str)
Converts the template to a
Array#packtemplate String. -
#unpack(data) ⇒ ::Array
Unpacks the string.
Methods included from CTypes::Mixin
Constructor Details
#initialize(fields, **kwargs) ⇒ Template
Creates a new Binary template.
The desired architecture for the values within the template.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ronin/support/binary/template.rb', line 145 def initialize(fields, **kwargs) initialize_type_system(**kwargs) @fields = [] @types = [] @pack_string = String.new fields.each do |field| type = @type_resolver.resolve(field) @fields << field @types << type if @pack_string if type.pack_string then @pack_string << type.pack_string else @pack_string = nil end end end end |
Instance Attribute Details
#fields ⇒ ::Array<Symbol, (Symbol, Integer), Range(Symbol)> (readonly)
The fields of the binary template.
97 98 99 |
# File 'lib/ronin/support/binary/template.rb', line 97 def fields @fields end |
#pack_string ⇒ String (readonly)
The Array#pack string for the binary template.
113 114 115 |
# File 'lib/ronin/support/binary/template.rb', line 113 def pack_string @pack_string end |
#types ⇒ ::Array<CTypes::Type, CTypes::ArrayType, CTypes::UnboundArrayType> (readonly)
The field types of the binary template.
106 107 108 |
# File 'lib/ronin/support/binary/template.rb', line 106 def types @types end |
Class Method Details
.[](*fields, **kwargs) ⇒ Object
179 180 181 |
# File 'lib/ronin/support/binary/template.rb', line 179 def self.[](*fields,**kwargs) new(fields,**kwargs) end |
Instance Method Details
#pack(*arguments) ⇒ String
Packs the data.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ronin/support/binary/template.rb', line 197 def pack(*arguments) if @pack_string values = [] @types.each do |type| # shift off the next value(s) and enqueue them type.enqueue_value(values,arguments.shift) end values.pack(@pack_string) else buffer = String.new @types.each do |type| # shift off the next value and pack it buffer << type.pack(arguments.shift) end return buffer end end |
#to_s ⇒ String Also known as: to_str
Converts the template to a Array#pack template String.
272 273 274 |
# File 'lib/ronin/support/binary/template.rb', line 272 def to_s @pack_string end |
#unpack(data) ⇒ ::Array
Unpacks the string.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/ronin/support/binary/template.rb', line 233 def unpack(data) if @pack_string values = data.unpack(@pack_string) @types.map do |type| type.dequeue_value(values) end else array = [] offset = 0 @types.each do |type| slice = if type.size == Float::INFINITY data.byteslice(offset..) else data.byteslice(offset,type.size) end array << type.unpack(slice) offset += type.size end return array end end |