macho/macho-go/internal/wrapper/ofile/ofile.go

48 lines
828 B
Go
Raw Permalink Normal View History

2023-05-31 16:17:03 +07:00
package ofile
import (
"bufio"
"bytes"
"encoding/binary"
"os"
log "github.com/sirupsen/logrus"
. "ios-wrapper/pkg/ios"
)
type OFile interface {
Cleanup()
}
func NewOFile(f string) OFile {
file, err := os.Open(f)
if err != nil {
return nil
}
r := bufio.NewReader(file)
var magic uint32
magic_buf, _ := r.Peek(4)
magic_r := bytes.NewReader(magic_buf)
binary.Read(magic_r, binary.LittleEndian, &magic)
if magic != Magic32 && magic != Magic64 && magic != Cigam32 && magic != Cigam64 && magic != MagicFat &&
magic != CigamFat {
log.Panic("Magic does not match %x", magic)
return nil
}
if magic == Magic32 || magic == Magic64 || magic == Cigam32 ||
magic == Cigam64 {
return NewMachoFile(f)
}
if magic == MagicFat || magic == CigamFat {
return NewFatFile(f)
}
// not likely
return nil
}