add old go tooling
This commit is contained in:
58
macho-go/internal/wrapper/ofile/fatfile.go
Normal file
58
macho-go/internal/wrapper/ofile/fatfile.go
Normal file
@ -0,0 +1,58 @@
|
||||
package ofile
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"ios-wrapper/pkg/ios/fat"
|
||||
)
|
||||
|
||||
type FatFile struct {
|
||||
machos []*MachoFile
|
||||
files []string
|
||||
tmp_file string
|
||||
}
|
||||
|
||||
func (ff *FatFile) Machos() []*MachoFile {
|
||||
return ff.machos
|
||||
}
|
||||
|
||||
func (ff *FatFile) Cleanup() {
|
||||
for _, macho := range ff.machos {
|
||||
macho.Cleanup()
|
||||
}
|
||||
os.Remove(ff.tmp_file)
|
||||
}
|
||||
|
||||
func NewFatFile(f string) *FatFile {
|
||||
// create a tmp working file
|
||||
tmp, _ := ioutil.TempFile("", "bcell_*")
|
||||
data, _ := ioutil.ReadFile(f)
|
||||
ioutil.WriteFile(tmp.Name(), data, 0644)
|
||||
|
||||
var machos []*MachoFile
|
||||
var files []string
|
||||
splitted_files, err := fat.FatSplit(tmp.Name())
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"err": err,
|
||||
"splitted_ok": splitted_files,
|
||||
}).Panic("Cannot split Fat binary")
|
||||
}
|
||||
for _, splitted_file := range splitted_files {
|
||||
macho := NewMachoFile(splitted_file)
|
||||
machos = append(machos, macho)
|
||||
files = append(files, macho.tmp_file)
|
||||
|
||||
// NewMachoFile creates another tmp file, remove this temp splitted file
|
||||
os.Remove(splitted_file)
|
||||
}
|
||||
|
||||
return &FatFile{
|
||||
machos: machos,
|
||||
tmp_file: tmp.Name(),
|
||||
files: files,
|
||||
}
|
||||
}
|
56
macho-go/internal/wrapper/ofile/machofile.go
Normal file
56
macho-go/internal/wrapper/ofile/machofile.go
Normal file
@ -0,0 +1,56 @@
|
||||
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(),
|
||||
},
|
||||
}
|
||||
}
|
47
macho-go/internal/wrapper/ofile/ofile.go
Normal file
47
macho-go/internal/wrapper/ofile/ofile.go
Normal file
@ -0,0 +1,47 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user