add old go tooling

This commit is contained in:
2023-05-31 16:17:03 +07:00
commit 54f1f3eb38
57 changed files with 5791 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package action
import (
. "ios-wrapper/internal/wrapper/ofile"
)
// Actions on MachoFile
type Action interface {
withMacho(mf *MachoFile) error
withFat(ff *FatFile) error
// TODO: error context
}
func RunAction(action Action, ofile OFile) error {
switch ofile.(type) {
case *MachoFile:
return action.withMacho(ofile.(*MachoFile))
case *FatFile:
return action.withFat(ofile.(*FatFile))
}
// not likely
return nil
}
func defaultWithFat(action Action, ff *FatFile) error {
for _, macho := range ff.Machos() {
err := action.withMacho(macho)
if err != nil {
return err
}
}
return nil
}

View File

@ -0,0 +1,31 @@
package action
import (
log "github.com/sirupsen/logrus"
. "ios-wrapper/internal/wrapper/ofile"
)
type addDylib struct {
dylib_to_add []string
}
func (action *addDylib) withMacho(mf *MachoFile) error {
for _, dylib := range action.dylib_to_add {
mf.Context().AddDylib(dylib)
log.WithFields(log.Fields{
"dylib": dylib,
}).Info("Add Load Dylib Command")
}
return nil
}
func (action *addDylib) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewAddDylibAction(dylib_to_add []string) *addDylib {
return &addDylib{
dylib_to_add,
}
}

View File

@ -0,0 +1,31 @@
package action
import (
log "github.com/sirupsen/logrus"
. "ios-wrapper/internal/wrapper/ofile"
)
type addRpath struct {
rpath []string
}
func (action *addRpath) withMacho(mf *MachoFile) error {
for _, rpath := range action.rpath {
mf.Context().AddRPath(rpath)
log.WithFields(log.Fields{
"rpath": rpath,
}).Info("Add Rpath Command")
}
return nil
}
func (action *addRpath) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewAddRpathAction(rpath []string) *addRpath {
return &addRpath{
rpath,
}
}

View File

@ -0,0 +1,20 @@
package action
import (
. "ios-wrapper/internal/wrapper/ofile"
)
type removeClassicSymbol struct{}
func (action *removeClassicSymbol) withMacho(mf *MachoFile) error {
mf.Context().RemoveClassicSymbol()
return nil
}
func (action *removeClassicSymbol) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewRemoveClassicSymbolAction() *removeClassicSymbol {
return &removeClassicSymbol{}
}

View File

@ -0,0 +1,20 @@
package action
import (
. "ios-wrapper/internal/wrapper/ofile"
)
type removeCodeSignature struct{}
func (action *removeCodeSignature) withMacho(mf *MachoFile) error {
mf.Context().RemoveCodesign()
return nil
}
func (action *removeCodeSignature) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewRemoveCodeSignatureAction() *removeCodeSignature {
return &removeCodeSignature{}
}

View File

@ -0,0 +1,20 @@
package action
import (
. "ios-wrapper/internal/wrapper/ofile"
)
type removeInitPointer struct{}
func (action *removeInitPointer) withMacho(mf *MachoFile) error {
mf.Context().RemoveInitFunctions()
return nil
}
func (action *removeInitPointer) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewRemoveInitPointerAction() *removeInitPointer {
return &removeInitPointer{}
}

View File

@ -0,0 +1,20 @@
package action
import (
. "ios-wrapper/internal/wrapper/ofile"
)
type removeUnnecessaryInfo struct{}
func (action *removeUnnecessaryInfo) withMacho(mf *MachoFile) error {
mf.Context().RemoveUnnecessaryInfo()
return nil
}
func (action *removeUnnecessaryInfo) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewRemoveUnnecessaryInfoAction() *removeUnnecessaryInfo {
return &removeUnnecessaryInfo{}
}

View File

@ -0,0 +1,68 @@
package action
import (
"fmt"
"io/ioutil"
"google.golang.org/protobuf/proto"
. "ios-wrapper/internal/wrapper/ofile"
"ios-wrapper/pkg/protomodel"
)
type saveBcellFile struct {
bcellfile string
content *protomodel.BcellFile
signed bool
}
func (action *saveBcellFile) addMacho(mf *MachoFile) {
action.content.MachoInfos[mf.Context().ArchName()] = mf.Info()
}
func (action *saveBcellFile) writeFile() error {
outfile := fmt.Sprintf(action.bcellfile)
content, err := proto.Marshal(action.content)
if err != nil {
return err
}
if action.signed {
blocks := []*protomodel.Block{
{
Key: 0,
Value: content,
},
}
signed_data := protomodel.SignedData{
Blocks: blocks,
}
content, err = proto.Marshal(&signed_data)
}
return ioutil.WriteFile(outfile, content, 0644)
}
func (action *saveBcellFile) withMacho(mf *MachoFile) error {
action.addMacho(mf)
return action.writeFile()
}
func (action *saveBcellFile) withFat(ff *FatFile) error {
for _, macho := range ff.Machos() {
action.addMacho(macho)
}
return action.writeFile()
}
func NewSaveBcellFileAction(userconfig *protomodel.Config, bcellfile string, signed bool) *saveBcellFile {
content := &protomodel.BcellFile{
BcellConfig: userconfig,
MachoInfos: make(map[string]*protomodel.MachoInfo),
}
return &saveBcellFile{
bcellfile,
content,
signed,
}
}

View File

@ -0,0 +1,34 @@
package action
import (
log "github.com/sirupsen/logrus"
. "ios-wrapper/internal/wrapper/ofile"
"ios-wrapper/pkg/protomodel"
)
type saveInitPointer struct{}
func (action *saveInitPointer) withMacho(mf *MachoFile) error {
init_pointers := []*protomodel.MachoInfo_InitPointer{}
for _, ptr := range mf.Context().InitFunctions() {
init_pointers = append(init_pointers,
&protomodel.MachoInfo_InitPointer{
Offset: ptr.Offset(),
Value: ptr.Value(),
})
}
log.WithFields(log.Fields{
"pointers": init_pointers,
}).Info("Saved Init Pointers")
mf.Info().InitPointers = init_pointers
return nil
}
func (action *saveInitPointer) withFat(ff *FatFile) error {
return defaultWithFat(action, ff)
}
func NewSaveInitPointerAction() *saveInitPointer {
return &saveInitPointer{}
}

View File

@ -0,0 +1,32 @@
package action
import (
"io/ioutil"
. "ios-wrapper/internal/wrapper/ofile"
"ios-wrapper/pkg/ios/fat"
)
type writeFile struct {
outfile string
}
func (action *writeFile) withMacho(mf *MachoFile) error {
data, _ := ioutil.ReadFile(mf.TmpFile())
return ioutil.WriteFile(action.outfile, data, 0644)
}
func (action *writeFile) withFat(ff *FatFile) error {
var files []string
for _, macho := range ff.Machos() {
files = append(files, macho.TmpFile())
}
fat.FatJoin(files, action.outfile)
return nil
}
func NewWriteFileAction(outfile string) *writeFile {
return &writeFile{
outfile,
}
}