Module: MachO

Defined in:
lib/macho.rb,
lib/macho/view.rb,
lib/macho/tools.rb,
lib/macho/utils.rb,
lib/macho/headers.rb,
lib/macho/fat_file.rb,
lib/macho/sections.rb,
lib/macho/structure.rb,
lib/macho/exceptions.rb,
lib/macho/macho_file.rb,
lib/macho/load_commands.rb

Overview

The primary namespace for ruby-macho.

Defined Under Namespace

Modules: Headers, LoadCommands, Sections, Tools, Utils Classes: CPUSubtypeError, CPUTypeError, DylibIdMissingError, DylibUnknownError, FatArchOffsetOverflowError, FatBinaryError, FatFile, FiletypeError, HeaderPadError, JavaClassFileError, LCStrMalformedError, LoadCommandCreationArityError, LoadCommandError, LoadCommandNotCreatableError, LoadCommandNotSerializableError, MachOBinaryError, MachOError, MachOFile, MachOStructure, MachOView, MagicError, ModificationError, NotAMachOError, OffsetInsertionError, RecoverableModificationError, RpathExistsError, RpathUnknownError, TruncatedFileError, UnimplementedError

Constant Summary collapse

VERSION =

release version

"2.2.0".freeze

Class Method Summary collapse

Class Method Details

.open(filename) ⇒ MachOFile, FatFile

Opens the given filename as a MachOFile or FatFile, depending on its magic.

Parameters:

  • filename (String)

    the file being opened

Returns:

  • (MachOFile)

    if the file is a Mach-O

  • (FatFile)

    if the file is a Fat file

Raises:

  • (ArgumentError)

    if the given file does not exist

  • (TruncatedFileError)

    if the file is too small to have a valid header

  • (MagicError)

    if the file's magic is not valid Mach-O magic



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/macho.rb', line 24

def self.open(filename)
  raise ArgumentError, "#{filename}: no such file" unless File.file?(filename)
  raise TruncatedFileError unless File.stat(filename).size >= 4

  magic = File.open(filename, "rb") { |f| f.read(4) }.unpack("N").first

  if Utils.fat_magic?(magic)
    file = FatFile.new(filename)
  elsif Utils.magic?(magic)
    file = MachOFile.new(filename)
  else
    raise MagicError, magic
  end

  file
end