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) ⇒ Integer
Returns the number of bytes needed to pad the given size to the given alignment.
-
.round(value, round) ⇒ Integer
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
30 31 32 33 |
# File 'lib/macho/utils.rb', line 30 def self.specialize_format(format, endianness) modifier = endianness == :big ? ">" : "<" format.tr("=", modifier) end |