Skip to content

Commit

Permalink
Renamed UnboundedArrayType to FlexibleArrayType (closes #537).
Browse files Browse the repository at this point in the history
* This more closely matches the C99 definition.
  See https://en.wikipedia.org/wiki/Flexible_array_member
  • Loading branch information
postmodern committed Nov 21, 2024
1 parent 187888a commit 8a39af1
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 63 deletions.
4 changes: 2 additions & 2 deletions lib/ronin/support/binary/ctypes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
require 'ronin/support/binary/ctypes/os'

require 'ronin/support/binary/ctypes/array_type'
require 'ronin/support/binary/ctypes/unbounded_array_type'
require 'ronin/support/binary/ctypes/flexible_array_type'
require 'ronin/support/binary/ctypes/struct_type'

require 'ronin/support/binary/ctypes/array_object_type'
Expand Down Expand Up @@ -65,7 +65,7 @@ module Binary
# `1.79769313486231570E+308`)
# * Aggregate Types:
# * {CTypes::ArrayType Array} (ex: `{1,2,3}`)
# * {CTypes::UnboundedArrayType unbounded Array} (ex: `char c[] = {...}`)
# * {CTypes::FlexibleArrayType unbounded Array} (ex: `char c[] = {...}`)
# * {CTypes::StructType struct} (ex: `struct s = {1, 'c', ...}`)
# * {CTypes::UnionType union} (ex: `union u = {1234}`)
# * Object Types:
Expand Down
6 changes: 3 additions & 3 deletions lib/ronin/support/binary/ctypes/array_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#

require 'ronin/support/binary/ctypes/aggregate_type'
require 'ronin/support/binary/ctypes/unbounded_array_type'
require 'ronin/support/binary/ctypes/flexible_array_type'

module Ronin
module Support
Expand Down Expand Up @@ -60,8 +60,8 @@ class ArrayType < AggregateType
# Custom type alignment to override the type's alignment.
#
def initialize(type,length, alignment: nil)
if type.kind_of?(UnboundedArrayType)
raise(ArgumentError,"cannot initialize an #{self.class} of #{UnboundedArrayType}")
if type.kind_of?(FlexibleArrayType)
raise(ArgumentError,"cannot initialize an #{self.class} of #{FlexibleArrayType}")
end

@type = type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ module CTypes
#
# @api private
#
# @since 1.0.0
# @since 1.2.0
#
class UnboundedArrayType < AggregateType
class FlexibleArrayType < AggregateType

# The type of each element in the unbounded array type.
#
Expand All @@ -44,11 +44,11 @@ class UnboundedArrayType < AggregateType
# The type of each element in the unbounded array type.
#
# @raise [ArgumentError]
# Cannot initialize a nested {UnboundedArrayType}.
# Cannot initialize a nested {FlexibleArrayType}.
#
def initialize(type, alignment: nil)
if type.kind_of?(UnboundedArrayType)
raise(ArgumentError,"cannot initialize a nested #{UnboundedArrayType}")
if type.kind_of?(FlexibleArrayType)
raise(ArgumentError,"cannot initialize a nested #{FlexibleArrayType}")
end

@type = type
Expand Down
4 changes: 2 additions & 2 deletions lib/ronin/support/binary/ctypes/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ def initialize(pack_string: )
# @param [Integer, nil] length
# The length of the Array.
#
# @return [ArrayType, UnboundedArrayType]
# @return [ArrayType, FlexibleArrayType]
# The new Array type or an unbounded Array type if `length` was not
# given.
#
def [](length=nil)
if length then ArrayType.new(self,length)
else UnboundedArrayType.new(self)
else FlexibleArrayType.new(self)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ronin/support/binary/ctypes/type_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def resolve_array(type_signature)
#
# @param [Range] type_signature
#
# @return [UnboundedArrayType]
# @return [FlexibleArrayType]
#
def resolve_range(type_signature)
range = type_signature
Expand Down
4 changes: 2 additions & 2 deletions lib/ronin/support/binary/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ module Binary
# struct.pack
# # => "\x00\x00\x00\x00\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00"
#
# ### Unbounded Array Fields
# ### Flexible Array Fields
#
# class MyStruct < Ronin::Support::Binary::Struct
#
Expand Down Expand Up @@ -517,7 +517,7 @@ def self.read_from(io)
def [](name)
if (member = @type.members[name])
case member.type
when CTypes::UnboundedArrayType
when CTypes::FlexibleArrayType
# XXX: but how do we handle an unbounded array of structs?
@cache[name] ||= begin
offset = member.offset
Expand Down
2 changes: 1 addition & 1 deletion lib/ronin/support/binary/union.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module Binary
# union.pack
# # => "\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00"
#
# ### Unbounded Array Fields
# ### Flexible Array Fields
#
# class MyUnion < Ronin::Support::Binary::Union
#
Expand Down
4 changes: 2 additions & 2 deletions spec/binary/ctypes/array_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
end
end

context "when the given type is an UnboundedArrayType" do
context "when the given type is an FlexibleArrayType" do
let(:type) do
Ronin::Support::Binary::CTypes::UnboundedArrayType.new(super())
Ronin::Support::Binary::CTypes::FlexibleArrayType.new(super())
end

it do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'spec_helper'
require 'ronin/support/binary/ctypes/unbounded_array_type'
require 'ronin/support/binary/ctypes/flexible_array_type'
require 'ronin/support/binary/ctypes/int32_type'
require 'ronin/support/binary/ctypes/array_type'
require 'ronin/support/binary/ctypes/struct_type'
require 'ronin/support/binary/ctypes'

describe Ronin::Support::Binary::CTypes::UnboundedArrayType do
describe Ronin::Support::Binary::CTypes::FlexibleArrayType do
let(:endian) { :little }
let(:pack_string) { 'L<' }

Expand Down Expand Up @@ -48,9 +48,9 @@
end
end

context "when the given type is an UnboundedArrayType" do
context "when the given type is an FlexibleArrayType" do
let(:type) do
Ronin::Support::Binary::CTypes::UnboundedArrayType.new(super())
Ronin::Support::Binary::CTypes::FlexibleArrayType.new(super())
end

it do
Expand Down
14 changes: 7 additions & 7 deletions spec/binary/ctypes/struct_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
let(:members) do
{
a: Ronin::Support::Binary::CTypes::INT32,
b: Ronin::Support::Binary::CTypes::UnboundedArrayType.new(
b: Ronin::Support::Binary::CTypes::FlexibleArrayType.new(
Ronin::Support::Binary::CTypes::INT32[3]
)
}
Expand All @@ -202,7 +202,7 @@
end
end

context "when one of the members is a Ronin::Support::Binary::CTypes::UnboundedArrayType" do
context "when one of the members is a Ronin::Support::Binary::CTypes::FlexibleArrayType" do
let(:members) do
{
a: Ronin::Support::Binary::CTypes::INT16,
Expand All @@ -211,7 +211,7 @@
}
end

it "must omit the UnboundedArrayType member size from #size" do
it "must omit the FlexibleArrayType member size from #size" do
expect(subject.size).to eq(
members[:a].size + members[:b].size
)
Expand Down Expand Up @@ -366,7 +366,7 @@
end
end

context "when the last value in #members is an UnboundedArrayType" do
context "when the last value in #members is an FlexibleArrayType" do
let(:members) do
{
a: Ronin::Support::Binary::CTypes::CHAR,
Expand Down Expand Up @@ -539,7 +539,7 @@
end
end

context "when the last value in #members is an UnboundedArrayType" do
context "when the last value in #members is an FlexibleArrayType" do
let(:members) do
{
a: Ronin::Support::Binary::CTypes::CHAR,
Expand Down Expand Up @@ -734,7 +734,7 @@
end
end

context "when the last value in #members is an UnboundedArrayType" do
context "when the last value in #members is an FlexibleArrayType" do
let(:members) do
{
a: Ronin::Support::Binary::CTypes::CHAR,
Expand Down Expand Up @@ -861,7 +861,7 @@
end
end

context "when the last value in #members is an UnboundedArrayType" do
context "when the last value in #members is an FlexibleArrayType" do
let(:members) do
{
a: Ronin::Support::Binary::CTypes::CHAR,
Expand Down
6 changes: 3 additions & 3 deletions spec/binary/ctypes/type_examples.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rspec'
require 'ronin/support/binary/ctypes/array_type'
require 'ronin/support/binary/ctypes/unbounded_array_type'
require 'ronin/support/binary/ctypes/flexible_array_type'

shared_examples_for "Type examples" do
describe "#[]" do
Expand All @@ -21,8 +21,8 @@
end

context "when no argument is given" do
it "must return an UnboundedArrayType" do
expect(subject[]).to be_kind_of(Ronin::Support::Binary::CTypes::UnboundedArrayType)
it "must return an FlexibleArrayType" do
expect(subject[]).to be_kind_of(Ronin::Support::Binary::CTypes::FlexibleArrayType)
end

it "must have a #type of self" do
Expand Down
14 changes: 7 additions & 7 deletions spec/binary/ctypes/type_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ class TestUnionWithAlignment < Ronin::Support::Binary::Union

context "when given a Range" do
context "and it starts with a Symbol" do
it "must return an UnboundedArrayType containing the resolved Type" do
it "must return an FlexibleArrayType containing the resolved Type" do
type = subject.resolve(type_name..)

expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::UnboundedArrayType)
expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::FlexibleArrayType)
expect(type.type).to eq(types[type_name])
end
end
Expand All @@ -243,7 +243,7 @@ class TestUnionWithAlignment < Ronin::Support::Binary::Union
it "must return an ArrayObjectType containing the resolved Type and length" do
type = subject.resolve([type_name, length]..)

expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::UnboundedArrayType)
expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::FlexibleArrayType)
expect(type.type).to be_kind_of(Ronin::Support::Binary::CTypes::ArrayObjectType)
expect(type.type.type).to eq(types[type_name])
expect(type.type.length).to eq(length)
Expand All @@ -257,7 +257,7 @@ class TestUnionWithAlignment < Ronin::Support::Binary::Union
it "must return an ArrayObjectType containing an ArrayObjectType and the length" do
type = subject.resolve([[type_name, length2], length1]..)

expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::UnboundedArrayType)
expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::FlexibleArrayType)
expect(type.type).to be_kind_of(Ronin::Support::Binary::CTypes::ArrayObjectType)
expect(type.type.type).to be_kind_of(Ronin::Support::Binary::CTypes::ArrayObjectType)
expect(type.type.length).to eq(length1)
Expand All @@ -273,7 +273,7 @@ class TestUnionWithAlignment < Ronin::Support::Binary::Union
it "must return an ArrayObjectType containing a StructObjectType and the length" do
type = subject.resolve([struct_class, length]..)

expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::UnboundedArrayType)
expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::FlexibleArrayType)
expect(type.type).to be_kind_of(Ronin::Support::Binary::CTypes::ArrayObjectType)
expect(type.type.type).to be_kind_of(Ronin::Support::Binary::CTypes::StructObjectType)
expect(type.type.length).to eq(length)
Expand All @@ -300,7 +300,7 @@ class TestUnionWithAlignment < Ronin::Support::Binary::Union
it "must return an ArrayObjectType containing a UnionObjectType and the length" do
type = subject.resolve([union_class, length]..)

expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::UnboundedArrayType)
expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::FlexibleArrayType)
expect(type.type).to be_kind_of(Ronin::Support::Binary::CTypes::ArrayObjectType)
expect(type.type.type).to be_kind_of(Ronin::Support::Binary::CTypes::UnionObjectType)
expect(type.type.length).to eq(length)
Expand All @@ -327,7 +327,7 @@ class TestUnionWithAlignment < Ronin::Support::Binary::Union
it "must return an ArrayObjectType containing the Type and length" do
type = subject.resolve([type_object, length]..)

expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::UnboundedArrayType)
expect(type).to be_kind_of(Ronin::Support::Binary::CTypes::FlexibleArrayType)
expect(type.type).to be_kind_of(Ronin::Support::Binary::CTypes::ArrayObjectType)
expect(type.type.type).to eq(type_object)
expect(type.type.length).to eq(length)
Expand Down
10 changes: 5 additions & 5 deletions spec/binary/ctypes/union_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
expect(subject.pack_string).to be(nil)
end

context "when one of the fields is a Ronin::Support::Binary::CTypes::UnboundedArrayType" do
context "when one of the fields is a Ronin::Support::Binary::CTypes::FlexibleArrayType" do
let(:members) do
{
a: Ronin::Support::Binary::CTypes::CHAR,
Expand All @@ -135,7 +135,7 @@
}
end

it "must omit the UnboundedArrayType member size from #size" do
it "must omit the FlexibleArrayType member size from #size" do
expect(subject.size).to eq(
[members[:a].size, members[:b].size].max
)
Expand Down Expand Up @@ -257,7 +257,7 @@
end
end

context "and when the member type is an UnboundedArrayType" do
context "and when the member type is an FlexibleArrayType" do
let(:members) do
{
a: Ronin::Support::Binary::CTypes::CHAR,
Expand All @@ -270,7 +270,7 @@
let(:value) { [*0x00..0x10] }
let(:hash) { {key => value} }

it "must pack the value using the member's UnboundedArrayType" do
it "must pack the value using the member's FlexibleArrayType" do
expect(subject.pack(hash)).to eq(type.pack(value))
end
end
Expand Down Expand Up @@ -455,7 +455,7 @@
end
end

context "when the last value in #members is an UnboundedArrayType" do
context "when the last value in #members is an FlexibleArrayType" do
let(:members) do
{
a: Ronin::Support::Binary::CTypes::CHAR,
Expand Down
2 changes: 1 addition & 1 deletion spec/binary/struct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StructWithAnArray < Ronin::Support::Binary::Struct
member :baz, :uint64
end

class StructWithUnboundedArray < Ronin::Support::Binary::Struct
class StructWithFlexibleArray < Ronin::Support::Binary::Struct
member :foo, :uint16
member :bar, :int32
member :baz, (:uint64..)
Expand Down
Loading

0 comments on commit 8a39af1

Please sign in to comment.