Class: Ronin::Support::Binary::Array
- Includes:
- Enumerable, CTypes::Mixin
- Defined in:
- lib/ronin/support/binary/array.rb
Overview
This class provides lazy memory mapped access to an underlying
Represents an Array of binary types that can be read from and written to.
buffer. This means values are decoded/encoded each time they are read or written to.
Examples
Creating an array of int32s:
array = Binary::Binary::Array.new(:int32, 4)
# => #<Ronin::Support::Binary::Binary::Array: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00">
array[0] = 0x11111111
array[1] = 0x22222222
array[2] = 0x33333333
array[3] = -1
array.to_s
# => "\x11\x11\x11\x11\"\"\"\"3333\xFF\xFF\xFF\xFF"
Creating an array from an existing String:
array = Binary::Array.new(:uint32_le, "\x41\x00\x00\x00\x42\x00\x00\x00")
# => #<Ronin::Support::Binary::Binary::Array: "A\u0000\u0000\u0000B\u0000\u0000\u0000">
array[0]
# => 65
array[1]
# => 66
Instance Attribute Summary collapse
-
#length ⇒ Integer
readonly
The number of elements in the array buffer.
-
#type ⇒ CTypes::Type
readonly
The underlying type of the data within the array buffer.
Attributes included from CTypes::Mixin
#arch, #endian, #os, #type_resolver, #type_system
Attributes inherited from Memory
Class Method Summary collapse
-
.read_from(io, type, length) ⇒ Binary::Struct
Reads the struct from the IO stream.
Instance Method Summary collapse
-
#[](index) ⇒ Integer, ...
Reads a value from the array at the given index.
-
#[]=(index, value) ⇒ Integer, ...
Writes a value to the array at the given index.
-
#each {|value| ... } ⇒ Enumerator
Enumerates over every value within the array buffer.
-
#initialize(type, length_or_string, **kwargs) ⇒ Array
constructor
Initializes the array buffer.
-
#inspect ⇒ String
Inspects the array buffer.
Methods included from Enumerable
Methods included from CTypes::Mixin
Methods inherited from Memory
#+, #byteslice, #clear, #copy_from, #copy_to, #pack, #read_from, #size
Constructor Details
#initialize(type, length_or_string, **kwargs) ⇒ Array
Initializes the array buffer.
The desired architecture for the values within the array buffer.
The desired Operating System (OS) for the values within the array buffer.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ronin/support/binary/array.rb', line 117 def initialize(type, length_or_string, **kwargs) initialize_type_system(**kwargs) @type = @type_resolver.resolve(type) @cache = [] case length_or_string when String, ByteSlice super(length_or_string) @length = size / @type.size when Integer @length = length_or_string super(@type.size * @length) else raise(ArgumentError,"first argument must be either a length (Integer) or a buffer (String): #{length_or_string.inspect}") end end |
Instance Attribute Details
#length ⇒ Integer (readonly)
The number of elements in the array buffer.
73 74 75 |
# File 'lib/ronin/support/binary/array.rb', line 73 def length @length end |
#type ⇒ CTypes::Type (readonly)
The underlying type of the data within the array buffer.
68 69 70 |
# File 'lib/ronin/support/binary/array.rb', line 68 def type @type end |
Class Method Details
.read_from(io, type, length) ⇒ Binary::Struct
Reads the struct from the IO stream.
158 159 160 |
# File 'lib/ronin/support/binary/array.rb', line 158 def self.read_from(io,type,length) new(type,length).read_from(io) end |
Instance Method Details
#[](index) ⇒ Integer, ...
Reads a value from the array at the given index.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/ronin/support/binary/array.rb', line 178 def [](index) offset = index * @type.size if (index < 0) || ((offset + @type.size) > size) raise(IndexError,"index #{index} is out of bounds: 0...#{@length}") end case @type when CTypes::ObjectType @cache[index] ||= @type.unpack(byteslice(offset,@type.size)) else data = super(offset,@type.size) @type.unpack(data) end end |
#[]=(index, value) ⇒ Integer, ...
Writes a value to the array at the given index.
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/ronin/support/binary/array.rb', line 214 def []=(index,value) offset = index * @type.size if (index < 0) || ((offset + @type.size) > size) raise(IndexError,"index #{index} is out of bounds: 0...#{@length}") end data = @type.pack(value) return super(offset,@type.size,data) end |
#each {|value| ... } ⇒ Enumerator
Enumerates over every value within the array buffer.
238 239 240 241 242 243 244 |
# File 'lib/ronin/support/binary/array.rb', line 238 def each return enum_for(__method__) unless block_given? (0...@length).each do |index| yield self[index] end end |
#inspect ⇒ String
Inspects the array buffer.
251 252 253 |
# File 'lib/ronin/support/binary/array.rb', line 251 def inspect "#<#{self.class}: #{@string.inspect}>" end |