chore: replace inline regex with precompiled variables for improved performance and readability

This commit is contained in:
blacktop
2026-04-04 17:35:42 -06:00
parent d3ca9d7519
commit e4a55a9e55
4 changed files with 40 additions and 19 deletions
+9 -3
View File
@@ -23,6 +23,12 @@ import (
"github.com/blacktop/ipsw/pkg/info"
)
var (
reArmFwIm4p = regexp.MustCompile(`armfw_.*.im4p$`)
reExclaveBundleIm4p = regexp.MustCompile(`.*exclavecore_bundle.*im4p$`)
reDmgAeaFile = regexp.MustCompile(`[0-9]{3}-[0-9]{5}-[0-9]{3}\.dmg(\.aea|\.trustcache)?(\.root_hash|\.trustcache|\.integrity_catalog|\.mtree)?$`)
)
// DmgInfo provides a name/path pair for a DMG contained in an IPSW.
type DmgInfo struct {
Name string
@@ -486,7 +492,7 @@ func ForEachIm4pInIPSW(ipswPath string, handler func(string, *macho.File) error)
}
for _, im4pFile := range im4ps {
if regexp.MustCompile(`armfw_.*.im4p$`).MatchString(im4pFile) {
if reArmFwIm4p.MatchString(im4pFile) {
im4p, err := img4.OpenPayload(im4pFile)
if err != nil {
return fmt.Errorf("failed to open im4p file %s: %v", im4pFile, err)
@@ -512,7 +518,7 @@ func ForEachIm4pInIPSW(ipswPath string, handler func(string, *macho.File) error)
}
}
ftab.Close()
} else if regexp.MustCompile(`.*exclavecore_bundle.*im4p$`).MatchString(im4pFile) {
} else if reExclaveBundleIm4p.MatchString(im4pFile) {
im4p, err := img4.OpenPayload(im4pFile)
if err != nil {
return fmt.Errorf("failed to open im4p file %s: %v", im4pFile, err)
@@ -660,7 +666,7 @@ func ForEachFileInIPSW(ipswPath, directory, pemDB string, handler func(string, s
continue
}
// skip DMGs/cryptexes as they always have a different name (i.e. 090-43228-337.dmg.aea)
if regexp.MustCompile(`[0-9]{3}-[0-9]{5}-[0-9]{3}\.dmg(\.aea|\.trustcache)?(\.root_hash|\.trustcache|.integrity_catalog|\.mtree)?$`).MatchString(f.Name) {
if reDmgAeaFile.MatchString(f.Name) {
continue
}
if err := scanFile("", f.Name); err != nil {
+9 -6
View File
@@ -16,18 +16,21 @@ var colorComment = color.New(color.Faint, color.FgWhite).SprintFunc()
var colorLocation = color.New(color.FgHiYellow).SprintfFunc()
var printCurLine = color.New(color.Bold, color.FgBlack, color.BgHiWhite).PrintfFunc()
var (
reColorImm = regexp.MustCompile(`#?-?0x[0-9a-z]+`)
reColorLocation = regexp.MustCompile(`\sloc_[0-9a-z]+`)
reColorRegs = regexp.MustCompile(`\W([wxvbhsdqzp][0-9]{1,2}|(c|s)psr(_c)?|pc|sl|sb|fp|ip|sp|lr|fpsid|fpscr|fpexc)`)
)
func ColorOperands(operands string) string {
if len(operands) > 0 {
immMatch := regexp.MustCompile(`#?-?0x[0-9a-z]+`)
operands = immMatch.ReplaceAllStringFunc(operands, func(s string) string {
operands = reColorImm.ReplaceAllStringFunc(operands, func(s string) string {
return colorImm(s)
})
locMatch := regexp.MustCompile(`\sloc_[0-9a-z]+`)
operands = locMatch.ReplaceAllStringFunc(operands, func(s string) string {
operands = reColorLocation.ReplaceAllStringFunc(operands, func(s string) string {
return colorLocation(s)
})
regMatch := regexp.MustCompile(`\W([wxvbhsdqzp][0-9]{1,2}|(c|s)psr(_c)?|pc|sl|sb|fp|ip|sp|lr|fpsid|fpscr|fpexc)`)
operands = regMatch.ReplaceAllStringFunc(operands, func(s string) string {
operands = reColorRegs.ReplaceAllStringFunc(operands, func(s string) string {
return string(s[0]) + colorRegs(s[1:])
})
// TODO: delete this (moved comment coloring into disass module)
+7 -4
View File
@@ -13,14 +13,17 @@ import (
"github.com/blacktop/arm64-cgo/disassemble"
)
var (
emuReColorImm = regexp.MustCompile(`#?-?0x[0-9a-z]+`)
emuReColorRegs = regexp.MustCompile(`\W([wxvbhsdqzp][0-9]{1,2}|(c|s)psr(_c)?|pc|sl|sb|fp|ip|sp|lr|fpsid|fpscr|fpexc)`)
)
func colorOperands(operands string) string {
if len(operands) > 0 {
immMatch := regexp.MustCompile(`#?-?0x[0-9a-z]+`)
operands = immMatch.ReplaceAllStringFunc(operands, func(s string) string {
operands = emuReColorImm.ReplaceAllStringFunc(operands, func(s string) string {
return colorImm(s)
})
regMatch := regexp.MustCompile(`\W([wxvbhsdqzp][0-9]{1,2}|(c|s)psr(_c)?|pc|sl|sb|fp|ip|sp|lr|fpsid|fpscr|fpexc)`)
operands = regMatch.ReplaceAllStringFunc(operands, func(s string) string {
operands = emuReColorRegs.ReplaceAllStringFunc(operands, func(s string) string {
return string(s[0]) + colorRegs(s[1:])
})
}
+15 -6
View File
@@ -35,6 +35,15 @@ import (
"golang.org/x/sys/execabs"
)
var (
reOTADeviceTreeIm4p = regexp.MustCompile(`.*DeviceTree.*im4p$`)
reOTAInfoPlist = regexp.MustCompile(`^Info\.plist$`)
reOTAAssetDataInfo = regexp.MustCompile(`^AssetData/Info\.plist$`)
reOTARestorePlist = regexp.MustCompile(`Restore\.plist$`)
reOTABuildManifest = regexp.MustCompile(`BuildManifest\.plist$`)
reOTASystemVersion = regexp.MustCompile(`SystemVersion\.plist$`)
)
type File struct {
name string
isDir bool
@@ -180,17 +189,17 @@ func (a *AA) Info() (*info.Info, error) {
var pfiles []fs.File
for _, file := range a.Files() {
switch {
case regexp.MustCompile(`.*DeviceTree.*im4p$`).MatchString(file.Name()):
case reOTADeviceTreeIm4p.MatchString(file.Name()):
fallthrough
case regexp.MustCompile(`^Info.plist$`).MatchString(file.Name()):
case reOTAInfoPlist.MatchString(file.Name()):
fallthrough
case regexp.MustCompile(`^AssetData/Info.plist$`).MatchString(file.Name()):
case reOTAAssetDataInfo.MatchString(file.Name()):
fallthrough
case regexp.MustCompile(`Restore.plist$`).MatchString(file.Name()):
case reOTARestorePlist.MatchString(file.Name()):
fallthrough
case regexp.MustCompile(`BuildManifest.plist$`).MatchString(file.Name()):
case reOTABuildManifest.MatchString(file.Name()):
fallthrough
case regexp.MustCompile(`SystemVersion.plist$`).MatchString(file.Name()):
case reOTASystemVersion.MatchString(file.Name()):
f, err := a.Open(file.Name(), true)
if err != nil {
return nil, err