Skip to content

Commit c1f6005

Browse files
Added documentation for new structure dsl
1 parent 97c3bfd commit c1f6005

File tree

5 files changed

+74
-17
lines changed

5 files changed

+74
-17
lines changed

lib/macho/headers.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,13 +751,13 @@ class PrelinkedKernelHeader < MachOStructure
751751
field :prelink_version, :uint32, :endian => :big
752752

753753
# @return [void]
754-
field :reserved, :binary, :size => 40, :unpack => "L>10"
754+
field :reserved, :varbyte, :size => 40, :unpack => "L>10"
755755

756756
# @return [void]
757-
field :platform_name, :binary, :size => 64
757+
field :platform_name, :varbyte, :size => 64
758758

759759
# @return [void]
760-
field :root_path, :binary, :size => 256
760+
field :root_path, :varbyte, :size => 256
761761

762762
# @return [Boolean] whether this prelinked kernel supports KASLR
763763
def kaslr?

lib/macho/load_commands.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def initialize(endianness, alignment)
367367
# LC_UUID.
368368
class UUIDCommand < LoadCommand
369369
# @return [Array<Integer>] the UUID
370-
field :uuid, :binary, :size => 16, :unpack => "C16"
370+
field :uuid, :varbyte, :size => 16, :unpack => "C16"
371371

372372
# @return [String] a string representation of the UUID
373373
def uuid_string
@@ -398,7 +398,7 @@ def to_h
398398
# the task's address space. Corresponds to LC_SEGMENT.
399399
class SegmentCommand < LoadCommand
400400
# @return [String] the name of the segment
401-
field :segname, :string, :size => 16, :to_s => true
401+
field :segname, :varchar, :size => 16, :to_s => true
402402

403403
# @return [Integer] the memory address of the segment
404404
field :vmaddr, :uint32
@@ -1325,7 +1325,7 @@ def to_h
13251325
# Corresponds to LC_NOTE.
13261326
class NoteCommand < LoadCommand
13271327
# @return [String] the name of the owner for this note
1328-
field :data_owner, :string, :size => 16, :to_s => true
1328+
field :data_owner, :varchar, :size => 16, :to_s => true
13291329

13301330
# @return [Integer] the offset, within the file, of the note
13311331
field :offset, :uint64

lib/macho/sections.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ module Sections
8989
# Represents a section of a segment for 32-bit architectures.
9090
class Section < MachOStructure
9191
# @return [String] the name of the section, including null pad bytes
92-
field :sectname, :string, :size => 16
92+
field :sectname, :varchar, :size => 16
9393

9494
# @return [String] the name of the segment's section, including null
9595
# pad bytes
96-
field :segname, :string, :size => 16
96+
field :segname, :varchar, :size => 16
9797

9898
# @return [Integer] the memory address of the section
9999
field :addr, :uint32

lib/macho/structure.rb

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,62 @@
11
# frozen_string_literal: true
22

3+
# MachOStructure DSL Documentation:
4+
# The MachOStructure class makes it easy to describe
5+
# binary chunks by using the #field method. This method
6+
# generates the byte size and format strings necessary
7+
# to parse a chunk of binary data. It also automatically
8+
# generates the constructor and readers for all fields
9+
# as well.
10+
# The fields are created in order so you will be
11+
# expected to pass those arguements to the constructor
12+
# in the same order. Fields with no arguments should
13+
# be defined last and fields with default arguments
14+
# should be defined right before them.
15+
# The type and options of inherited fields can be changed
16+
# but their argument position and the number of arguments
17+
# (used to calculate min_args) will also not change.
18+
# Usually, endianness is handled by the
19+
# Utils#specialize_format method but occasionally a
20+
# field needs to specifiy that beforehand. That is
21+
# what the :endian option is for. If not specified,
22+
# a placeholder is used so that can be specified later.
23+
#
24+
# Syntax:
25+
# field [field name], [field type], [option => value]*
26+
#
27+
# Ex:
28+
# class AllFields < MachO::MachOStructure
29+
# field :name1, :varbyte, :size => 16
30+
# field :name2, :varchar, :size => 32
31+
# field :name3, :int32
32+
# field :name4, :uint32
33+
# field :name5, :uint64
34+
# field :name6, :view
35+
# field :name7, :lcstr
36+
# field :name8, :two_level_hints_table
37+
# field :name9, :tool_entries
38+
# end
39+
#
40+
# Field types:
41+
# :varbyte = a null terminated binary chunk
42+
# :varchar = a null terminated binary string (cstring)
43+
# :int32 = signed 32 bit integer
44+
# :uint32 = unsigned 32 bit integer
45+
# :uint64 = unsigned 64 bit integer
46+
# :view = class (must be instantiated before passed to constructor)
47+
# :lcstr = class (will be instantiated in constructor)
48+
# :two_level_hints_table = no arg class (will be instantiated in constructor)
49+
# :tool_entries = class (will be instantiated in constructor)
50+
#
51+
# Option types:
52+
# Exclusive (only one can be used at a time):
53+
# :mask [Integer] bitmask to be applied to field
54+
# :unpack [String] binary unpack string used for further unpacking of :varbyte
55+
# :default [Value] default field value
56+
# Inclusive (can be used with other options):
57+
# :to_s [Boolean] generate #to_s method based on field
58+
# :endian [Symbol] optionally specify :big or :little endian
59+
360
module MachO
461
# A general purpose pseudo-structure.
562
# @abstract
@@ -15,8 +72,8 @@ module Fields
1572
# @api private
1673
BYTE_SIZE = {
1774
# Binary slices
18-
:binary => nil,
19-
:string => nil,
75+
:varbyte => nil,
76+
:varchar => nil,
2077
:int32 => 4,
2178
:uint32 => 4,
2279
:uint64 => 8,
@@ -36,8 +93,8 @@ module Fields
3693
# @api private
3794
FORMAT_CODE = {
3895
# Binary slices
39-
:binary => "a",
40-
:string => "Z",
96+
:varbyte => "a",
97+
:varchar => "Z",
4198
:int32 => "l=",
4299
:uint32 => "L=",
43100
:uint64 => "Q=",

test/test_structure_dsl.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class MachOStructureTest < Minitest::Test
77
# that information is reflected in the bytesize, min_args
88
# and format.
99
class AllFields < MachO::MachOStructure
10-
field :binary, :binary, :size => 16
11-
field :string, :string, :size => 32
10+
field :varbyte, :varbyte, :size => 16
11+
field :varchar, :varchar, :size => 32
1212
field :int32, :int32
1313
field :uint32, :uint32
1414
field :uint64, :uint64
@@ -19,8 +19,8 @@ class AllFields < MachO::MachOStructure
1919
end
2020

2121
def test_all_field_types
22-
assert_includes AllFields.instance_methods, :binary
23-
assert_includes AllFields.instance_methods, :string
22+
assert_includes AllFields.instance_methods, :varbyte
23+
assert_includes AllFields.instance_methods, :varchar
2424
assert_includes AllFields.instance_methods, :int32
2525
assert_includes AllFields.instance_methods, :uint32
2626
assert_includes AllFields.instance_methods, :uint64
@@ -66,7 +66,7 @@ def test_mask_option
6666
end
6767

6868
class UnpackCmd < MachO::MachOStructure
69-
field :unpack_field, :binary, :size => 8, :unpack => "L>2"
69+
field :unpack_field, :varbyte, :size => 8, :unpack => "L>2"
7070
end
7171

7272
def test_unpack_option

0 commit comments

Comments
 (0)