Class: Ronin::Support::Binary::CTypes::ArrayType Private

Inherits:
AggregateType show all
Defined in:
lib/ronin/support/binary/ctypes/array_type.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents a bounded array type.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Attributes inherited from Type

#pack_string

Instance Method Summary collapse

Methods inherited from Type

#[]

Constructor Details

#initialize(type, length, alignment: nil) ⇒ ArrayType

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the array type.

Parameters:

  • type (Type)

    The type of each element in the array type.

  • length (Integer)

    The length of the array type.

  • alignment (Integer, nil) (defaults to: nil)

    Custom type alignment to override the type's alignment.

Since:

  • 1.0.0



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 62

def initialize(type,length, alignment: nil)
  if type.kind_of?(UnboundedArrayType)
    raise(ArgumentError,"cannot initialize an #{self.class} of #{UnboundedArrayType}")
  end

  @type   = type
  @length = length

  @size      = @type.size * @length
  @alignment = alignment

  super(
    pack_string: if @type.pack_string
                   @type.pack_string * @length
                 end
  )
end

Instance Attribute Details

#lengthInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The length of the array type.

Returns:

Since:

  • 1.0.0



43
44
45
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 43

def length
  @length
end

#sizeInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The size of the array type.

Returns:

Since:

  • 1.0.0



48
49
50
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 48

def size
  @size
end

#typeType (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The type of each element in the array type.

Returns:

Since:

  • 1.0.0



38
39
40
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 38

def type
  @type
end

Instance Method Details

#align(new_alignment) ⇒ ScalarType

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a copy of the array type with a different #alignment.

Parameters:

  • new_alignment (Integer)

    The new alignment for the new array type.

Returns:

Since:

  • 1.0.0



118
119
120
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 118

def align(new_alignment)
  self.class.new(@type,@length, alignment: new_alignment)
end

#alignmentInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The alignment, in bytes, for the array type.

Returns:

Since:

  • 1.0.0



105
106
107
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 105

def alignment
  @alignment || @type.alignment
end

#dequeue_value(values) ⇒ ::Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Dequeues an array from the flat list of values.

Parameters:

  • values (::Array)

    The flat array of values.

Returns:

  • (::Array)

    The dequeued array.

Since:

  • 1.0.0



222
223
224
225
226
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 222

def dequeue_value(values)
  ::Array.new(@length) do
    @type.dequeue_value(values)
  end
end

#endian:little, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The endianness of each element in the bounded array type.

Returns:

  • (:little, :big, nil)

    Indicates whether each element is little-endian, big-endian, or nil if each element has no endianness.

Since:

  • 1.0.0



96
97
98
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 96

def endian
  @type.endian
end

#enqueue_value(values, array) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enqueues an array of values onto the flat list of values.

Parameters:

  • values (::Array)

    The flat array of values.

  • array (::Array)

    The array to enqueue.

Since:

  • 1.0.0



203
204
205
206
207
208
209
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 203

def enqueue_value(values,array)
  @length.times do |index|
    value = array[index] || @type.uninitialized_value

    @type.enqueue_value(values,value)
  end
end

#pack(array) ⇒ String

Packs an array of values into the type's binary format.

Parameters:

Returns:

  • (String)

    The packed binary data.

Since:

  • 1.0.0



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 151

def pack(array)
  if @pack_string
    super(array)
  else
    buffer = String.new

    @length.times do |index|
      value = array[index] || @type.uninitialized_value

      buffer << @type.pack(value)
    end

    return buffer
  end
end

#signed?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indicates whether each element is signed.

Returns:

  • (Boolean)

Since:

  • 1.0.0



127
128
129
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 127

def signed?
  @type.signed?
end

#uninitialized_value::Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes an Array of uninitialized values.

Returns:

Since:

  • 1.0.0



85
86
87
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 85

def uninitialized_value
  ::Array.new(@length) { @type.uninitialized_value }
end

#unpack(data) ⇒ ::Array<Integer, Float, String, nil>

Unpacks an array of binary data.

Parameters:

  • data (String)

    The binary data to unpack.

Returns:

Since:

  • 1.0.0



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 178

def unpack(data)
  if @pack_string
    super(data)
  else
    type_size = @type.size

    ::Array.new(@length) do |index|
      offset = index * type_size

      @type.unpack(data.byteslice(offset,type_size))
    end
  end
end

#unsigned?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indicates whether each element is unsigned.

Returns:

  • (Boolean)

Since:

  • 1.0.0



136
137
138
# File 'lib/ronin/support/binary/ctypes/array_type.rb', line 136

def unsigned?
  @type.unsigned?
end