From e4a55a9e55f658139d767efa4736d4229b0c0c6f Mon Sep 17 00:00:00 2001 From: blacktop Date: Sat, 4 Apr 2026 17:35:42 -0600 Subject: [PATCH] chore: replace inline regex with precompiled variables for improved performance and readability --- internal/search/search.go | 12 +++++++++--- pkg/disass/colors.go | 15 +++++++++------ pkg/emu/disass.go | 11 +++++++---- pkg/ota/aa.go | 21 +++++++++++++++------ 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/internal/search/search.go b/internal/search/search.go index d71cf25b5..594629f11 100644 --- a/internal/search/search.go +++ b/internal/search/search.go @@ -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 { diff --git a/pkg/disass/colors.go b/pkg/disass/colors.go index 9dc777be7..15906c47f 100644 --- a/pkg/disass/colors.go +++ b/pkg/disass/colors.go @@ -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) diff --git a/pkg/emu/disass.go b/pkg/emu/disass.go index 675c02411..10876a377 100644 --- a/pkg/emu/disass.go +++ b/pkg/emu/disass.go @@ -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:]) }) } diff --git a/pkg/ota/aa.go b/pkg/ota/aa.go index 6ca1108b6..ba8987891 100644 --- a/pkg/ota/aa.go +++ b/pkg/ota/aa.go @@ -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