Module: MachO::Utils

Defined in:
lib/macho/utils.rb

Overview

A collection of utility functions used throughout ruby-macho.

Class Method Summary collapse

Class Method Details

.big_magic?(num) ⇒ Boolean

Compares the given number to valid big-endian magic numbers.

Parameters:

  • num (Fixnum)

    the number being checked

Returns:

  • (Boolean)

    true if num is a valid big-endian magic number, false otherwise



92
93
94
# File 'lib/macho/utils.rb', line 92

def self.big_magic?(num)
  num == MH_CIGAM || num == MH_CIGAM_64
end

.fat_magic?(num) ⇒ Boolean

Compares the given number to valid Fat magic numbers.

Parameters:

  • num (Fixnum)

    the number being checked

Returns:

  • (Boolean)

    true if num is a valid Fat magic number, false otherwise



64
65
66
# File 'lib/macho/utils.rb', line 64

def self.fat_magic?(num)
  num == FAT_MAGIC
end

.little_magic?(num) ⇒ Boolean

Compares the given number to valid little-endian magic numbers.

Parameters:

  • num (Fixnum)

    the number being checked

Returns:

  • (Boolean)

    true if num is a valid little-endian magic number, false otherwise



85
86
87
# File 'lib/macho/utils.rb', line 85

def self.little_magic?(num)
  num == MH_CIGAM || num == MH_CIGAM_64
end

.magic32?(num) ⇒ Boolean

Compares the given number to valid 32-bit Mach-O magic numbers.

Parameters:

  • num (Fixnum)

    the number being checked

Returns:

  • (Boolean)

    true if num is a valid 32-bit magic number, false otherwise



71
72
73
# File 'lib/macho/utils.rb', line 71

def self.magic32?(num)
  num == MH_MAGIC || num == MH_CIGAM
end

.magic64?(num) ⇒ Boolean

Compares the given number to valid 64-bit Mach-O magic numbers.

Parameters:

  • num (Fixnum)

    the number being checked

Returns:

  • (Boolean)

    true if num is a valid 64-bit magic number, false otherwise



78
79
80
# File 'lib/macho/utils.rb', line 78

def self.magic64?(num)
  num == MH_MAGIC_64 || num == MH_CIGAM_64
end

.magic?(num) ⇒ Boolean

Compares the given number to valid Mach-O magic numbers.

Parameters:

  • num (Fixnum)

    the number being checked

Returns:

  • (Boolean)

    true if num is a valid Mach-O magic number, false otherwise



57
58
59
# File 'lib/macho/utils.rb', line 57

def self.magic?(num)
  MH_MAGICS.key?(num)
end

.pack_strings(fixed_offset, alignment, strings = {}) ⇒ Array<String, Hash>

Packs tagged strings into an aligned payload.

Parameters:

  • fixed_offset (Fixnum)

    the baseline offset for the first packed string

  • alignment (Fixnum)

    the alignment value to use for packing

  • strings (Hash) (defaults to: {})

    the labeled strings to pack

Returns:

  • (Array<String, Hash>)

    the packed string and labeled offsets



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/macho/utils.rb', line 38

def self.pack_strings(fixed_offset, alignment, strings = {})
  offsets = {}
  next_offset = fixed_offset
  payload = ""

  strings.each do |key, string|
    offsets[key] = next_offset
    payload << string
    payload << "\x00"
    next_offset += string.bytesize + 1
  end

  payload << "\x00" * padding_for(fixed_offset + payload.bytesize, alignment)
  [payload, offsets]
end

.padding_for(size, alignment) ⇒ Fixnum

Returns the number of bytes needed to pad the given size to the given alignment.

Parameters:

  • size (Fixnum)

    the unpadded size

  • alignment (Fixnum)

    the number to alignment the size with

Returns:

  • (Fixnum)

    the number of pad bytes required



20
21
22
# File 'lib/macho/utils.rb', line 20

def self.padding_for(size, alignment)
  round(size, alignment) - size
end

.round(value, round) ⇒ Fixnum

Rounds a value to the next multiple of the given round.

Parameters:

  • value (Fixnum)

    the number being rounded

  • round (Fixnum)

    the number being rounded with

Returns:

  • (Fixnum)

    the rounded value

See Also:



9
10
11
12
13
14
# File 'lib/macho/utils.rb', line 9

def self.round(value, round)
  round -= 1
  value += round
  value &= ~round
  value
end

.specialize_format(format, endianness) ⇒ String

Converts an abstract (native-endian) String#unpack format to big or little.

Parameters:

  • format (String)

    the format string being converted

  • endianness (Symbol)

    either :big or :little

Returns:

  • (String)

    the converted string



28
29
30
31
# File 'lib/macho/utils.rb', line 28

def self.specialize_format(format, endianness)
  modifier = endianness == :big ? ">" : "<"
  format.tr("=", modifier)
end