Module: MachO::Utils
- Defined in:
- lib/macho/utils.rb
Overview
A collection of utility functions used throughout ruby-macho.
Class Method Summary collapse
-
.big_magic?(num) ⇒ Boolean
Compares the given number to valid big-endian magic numbers.
-
.fat_magic?(num) ⇒ Boolean
Compares the given number to valid Fat magic numbers.
-
.little_magic?(num) ⇒ Boolean
Compares the given number to valid little-endian magic numbers.
-
.magic32?(num) ⇒ Boolean
Compares the given number to valid 32-bit Mach-O magic numbers.
-
.magic64?(num) ⇒ Boolean
Compares the given number to valid 64-bit Mach-O magic numbers.
-
.magic?(num) ⇒ Boolean
Compares the given number to valid Mach-O magic numbers.
-
.pack_strings(fixed_offset, alignment, strings = {}) ⇒ Array<String, Hash>
Packs tagged strings into an aligned payload.
-
.padding_for(size, alignment) ⇒ Fixnum
Returns the number of bytes needed to pad the given size to the given alignment.
-
.round(value, round) ⇒ Fixnum
Rounds a value to the next multiple of the given round.
-
.specialize_format(format, endianness) ⇒ String
Converts an abstract (native-endian) String#unpack format to big or little.
Class Method Details
.big_magic?(num) ⇒ Boolean
Compares the given number to valid big-endian magic numbers.
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.
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.
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.
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.
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.
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.
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.
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.
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.
28 29 30 31 |
# File 'lib/macho/utils.rb', line 28 def self.specialize_format(format, endianness) modifier = endianness == :big ? ">" : "<" format.tr("=", modifier) end |