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_magic32?(num) ⇒ Boolean
Compares the given number to valid 32-bit Fat magic numbers.
-
.fat_magic64?(num) ⇒ Boolean
Compares the given number to valid 64-bit Fat 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.
-
.nullpad(size) ⇒ String
Returns a string of null bytes of the requested (non-negative) size.
-
.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.
119 120 121 |
# File 'lib/macho/utils.rb', line 119 def self.big_magic?(num) [Headers::MH_MAGIC, Headers::MH_MAGIC_64].include? num end |
.fat_magic32?(num) ⇒ Boolean
Compares the given number to valid 32-bit Fat magic numbers.
84 85 86 |
# File 'lib/macho/utils.rb', line 84 def self.fat_magic32?(num) num == Headers::FAT_MAGIC end |
.fat_magic64?(num) ⇒ Boolean
Compares the given number to valid 64-bit Fat magic numbers.
91 92 93 |
# File 'lib/macho/utils.rb', line 91 def self.fat_magic64?(num) num == Headers::FAT_MAGIC_64 end |
.fat_magic?(num) ⇒ Boolean
Compares the given number to valid Fat magic numbers.
77 78 79 |
# File 'lib/macho/utils.rb', line 77 def self.fat_magic?(num) [Headers::FAT_MAGIC, Headers::FAT_MAGIC_64].include? num end |
.little_magic?(num) ⇒ Boolean
Compares the given number to valid little-endian magic numbers.
112 113 114 |
# File 'lib/macho/utils.rb', line 112 def self.little_magic?(num) [Headers::MH_CIGAM, Headers::MH_CIGAM_64].include? num end |
.magic32?(num) ⇒ Boolean
Compares the given number to valid 32-bit Mach-O magic numbers.
98 99 100 |
# File 'lib/macho/utils.rb', line 98 def self.magic32?(num) [Headers::MH_MAGIC, Headers::MH_CIGAM].include? num end |
.magic64?(num) ⇒ Boolean
Compares the given number to valid 64-bit Mach-O magic numbers.
105 106 107 |
# File 'lib/macho/utils.rb', line 105 def self.magic64?(num) [Headers::MH_MAGIC_64, Headers::MH_CIGAM_64].include? num end |
.magic?(num) ⇒ Boolean
Compares the given number to valid Mach-O magic numbers.
70 71 72 |
# File 'lib/macho/utils.rb', line 70 def self.magic?(num) Headers::MH_MAGICS.key?(num) end |
.nullpad(size) ⇒ String
Returns a string of null bytes of the requested (non-negative) size
29 30 31 32 33 |
# File 'lib/macho/utils.rb', line 29 def self.nullpad(size) raise ArgumentError, "size < 0: #{size}" if size.negative? "\x00" * size end |
.pack_strings(fixed_offset, alignment, strings = {}) ⇒ Array<String, Hash>
Packs tagged strings into an aligned payload.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/macho/utils.rb', line 51 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 << Utils.nullpad(1) next_offset += string.bytesize + 1 end payload << Utils.nullpad(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.
40 41 42 43 |
# File 'lib/macho/utils.rb', line 40 def self.specialize_format(format, endianness) modifier = endianness == :big ? ">" : "<" format.tr("=", modifier) end |