Skip to content

Commit

Permalink
feat: add GetCStrings function to dump all IsCstringLiterals
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Jul 10, 2024
1 parent 924c976 commit 25a8f48
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sync"

"github.com/blacktop/go-dwarf"
"golang.org/x/exp/slices"

Check failure on line 20 in file.go

View workflow job for this annotation

GitHub Actions / Build

no required module provides package golang.org/x/exp/slices; to add it:

"github.com/blacktop/go-macho/internal/saferio"
"github.com/blacktop/go-macho/pkg/codesign"
Expand Down Expand Up @@ -1644,6 +1645,46 @@ func (f *File) GetCString(addr uint64) (string, error) {
return "", fmt.Errorf("string not found at address %#x", addr)
}

func (f *File) GetCStrings() ([]string, error) {
var strs []string

for _, sec := range f.Sections {
if sec.Flags.IsCstringLiterals() {
off, err := f.GetOffset(sec.Addr)
if err != nil {
return nil, fmt.Errorf("failed to get offset for %s.%s: %v", sec.Seg, sec.Name, err)
}
dat := make([]byte, sec.Size)
if _, err = f.ReadAt(dat, int64(off)); err != nil {
return nil, fmt.Errorf("failed to read cstring data in %s.%s: %v", sec.Seg, sec.Name, err)
}

csr := bytes.NewBuffer(dat)

for {
s, err := csr.ReadString('\x00')

if err == io.EOF {
break
}

if err != nil {
return nil, fmt.Errorf("failed to read string: %v", err)
}

s = strings.Trim(s, "\x00")

if len(s) > 0 {
strs = append(strs, s)
}
}
slices.Sort(strs)
}
}

return strs, nil
}

// GetCStringAtOffset returns a c-string at a given offset into the MachO
func (f *File) GetCStringAtOffset(strOffset int64) (string, error) {
if _, err := f.cr.Seek(strOffset, io.SeekStart); err != nil {
Expand Down

0 comments on commit 25a8f48

Please sign in to comment.