macho/macho-go/internal/wrapper/ofile/machofile.go
2023-05-31 16:17:03 +07:00

57 lines
1.1 KiB
Go

package ofile
import (
"io/ioutil"
"os"
// log "github.com/sirupsen/logrus"
"ios-wrapper/pkg/ios/macho"
"ios-wrapper/pkg/protomodel"
)
type MachoFile struct {
mc *macho.MachoContext
tmp_file string
info *protomodel.MachoInfo
}
func (mf *MachoFile) Context() *macho.MachoContext {
return mf.mc
}
func (mf *MachoFile) Info() *protomodel.MachoInfo {
return mf.info
}
func (mf *MachoFile) TmpFile() string {
return mf.tmp_file
}
func (mf *MachoFile) Cleanup() {
os.Remove(mf.tmp_file)
}
func NewMachoFile(f string) *MachoFile {
// create a tmp working file
tmp, _ := ioutil.TempFile("", "bcell_*")
data, _ := ioutil.ReadFile(f)
ioutil.WriteFile(tmp.Name(), data, 0644)
var mc macho.MachoContext
tmp, _ = os.OpenFile(tmp.Name(), os.O_RDWR, 0644)
mc.ParseFile(tmp, 0)
return &MachoFile{
mc: &mc,
tmp_file: tmp.Name(),
info: &protomodel.MachoInfo{
PointerSize: map[bool]protomodel.MachoInfo_PointerSize{
false: protomodel.MachoInfo_p32,
true: protomodel.MachoInfo_p64,
}[mc.Is64bit()],
ImageBase: mc.ImageBase(),
},
}
}