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 (Integer)

    the number being checked

Returns:

  • (Boolean)

    whether num is a valid big-endian magic number



95
96
97
# File 'lib/macho/utils.rb', line 95

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

.fat_magic?(num) ⇒ Boolean

Compares the given number to valid Fat magic numbers.

Parameters:

  • num (Integer)

    the number being checked

Returns:

  • (Boolean)

    whether num is a valid Fat magic number



67
68
69
# File 'lib/macho/utils.rb', line 67

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

.little_magic?(num) ⇒ Boolean

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

Parameters:

  • num (Integer)

    the number being checked

Returns:

  • (Boolean)

    whether num is a valid little-endian magic number



88
89
90
# File 'lib/macho/utils.rb', line 88

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

.magic32?(num) ⇒ Boolean

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

Parameters:

  • num (Integer)

    the number being checked

Returns:

  • (Boolean)

    whether num is a valid 32-bit magic number



74
75
76
# File 'lib/macho/utils.rb', line 74

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

.magic64?(num) ⇒ Boolean

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

Parameters:

  • num (Integer)

    the number being checked

Returns:

  • (Boolean)

    whether num is a valid 64-bit magic number



81
82
83
# File 'lib/macho/utils.rb', line 81

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

.magic?(num) ⇒ Boolean

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

Parameters:

  • num (Integer)

    the number being checked

Returns:

  • (Boolean)

    whether num is a valid Mach-O magic number



60
61
62
# File 'lib/macho/utils.rb', line 60

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

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

Packs tagged strings into an aligned payload.

Parameters:

  • fixed_offset (Integer)

    the baseline offset for the first packed string

  • alignment (Integer)

    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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/macho/utils.rb', line 41

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) ⇒ Integer

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

Parameters:

  • size (Integer)

    the unpadded size

  • alignment (Integer)

    the number to alignment the size with

Returns:

  • (Integer)

    the number of pad bytes required



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

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

.round(value, round) ⇒ Integer

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

Parameters:

  • value (Integer)

    the number being rounded

  • round (Integer)

    the number being rounded with

Returns:

  • (Integer)

    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



30
31
32
33
# File 'lib/macho/utils.rb', line 30

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