package fat import ( "fmt" "io" "os" "strings" ) // Split a Fat binary into multiple Mach-O binaries // @ifile: path to Fat binary // returns a list of file path of splited Mach-O binaries func CreateMachosFromFat(ifile string) ([]string, error) { f, err := os.OpenFile(ifile, os.O_RDONLY, 0644) if err != nil { fmt.Println("Cannot open file") return []string{}, err } var fc FatContext fc.ParseFile(f) var r []string for _, arch := range fc.Machos() { offset := arch.Offset size := arch.Size buf := make([]byte, size) filename := fmt.Sprintf( "%s_%x", strings.ReplaceAll(ifile, " ", "_"), offset, ) var err error _, err = f.Seek(int64(offset), io.SeekStart) if err != nil { return r, err } _, err = f.Read(buf) if err != nil { return r, err } outfile, err := os.OpenFile( filename, os.O_CREATE|os.O_WRONLY, 0777, ) if err != nil { return r, err } _, err = outfile.Write(buf) if err != nil { return r, err } r = append(r, filename) // everything is find, append new file path } return r, nil }