Skip to content

Commit

Permalink
Merge pull request #14 from moul/dev/moul/fix-parser
Browse files Browse the repository at this point in the history
fix: use alternate method to get all manifest fields
  • Loading branch information
moul committed Sep 29, 2020
2 parents 50ca8b8 + 5e37db4 commit 26c4ca9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
33 changes: 23 additions & 10 deletions main.go
Expand Up @@ -24,16 +24,9 @@ func run(args []string) error {
root := &ffcli.Command{
ShortUsage: `pkgman SUBCOMMAND`,
Subcommands: []*ffcli.Command{
{
Name: "ipa-plist-json",
ShortUsage: "ipa-plist-json PATH",
Exec: runIpaPlistJSON,
},
{
Name: "apk-manifest",
ShortUsage: "apk-manifest",
Exec: runApkManifest,
},
{Name: "ipa-plist-json", ShortUsage: "ipa-plist-json PATH", Exec: runIpaPlistJSON},
{Name: "apk-manifest", ShortUsage: "apk-manifest PATH", Exec: runApkManifest},
{Name: "apk-manifest-xml", ShortUsage: "apk-manifest-xml PATH", Exec: runApkManifestXML},
},
Exec: func(context.Context, []string) error { return flag.ErrHelp },
}
Expand Down Expand Up @@ -81,3 +74,23 @@ func runApkManifest(_ context.Context, args []string) error {
}
return nil
}

func runApkManifestXML(_ context.Context, args []string) error {
if len(args) < 1 {
return flag.ErrHelp
}
for _, arg := range args {
pkg, err := apk.Open(arg)
if err != nil {
return err
}
defer pkg.Close()

manifest, err := pkg.ManifestXML()
if err != nil {
return err
}
fmt.Println(manifest)
}
return nil
}
20 changes: 14 additions & 6 deletions pkg/apk/apk.go
Expand Up @@ -14,15 +14,16 @@ import (
)

type Package struct {
r *zip.ReadCloser
r *zip.ReadCloser
path string
}

func Open(path string) (*Package, error) {
r, err := zip.OpenReader(path)
if err != nil {
return nil, fmt.Errorf("open path: %w", err)
}
return &Package{r: r}, nil
return &Package{r: r, path: path}, nil
}

func (p *Package) Close() error {
Expand Down Expand Up @@ -57,17 +58,24 @@ func (p Package) FileBytes(name string) ([]byte, error) {
}

func (p Package) ManifestXML() (string, error) {
b, err := p.FileBytes("AndroidManifest.xml")
// this method is a little bit stupid, since it reopens a new zip,
// but when using apkparser.ParseXML on bytes, we lose some fields.
apkReader, err := apkparser.OpenZip(p.path)
if err != nil {
return "", err
}
binbuf := bytes.NewBuffer(b)
defer apkReader.Close()

xmlbuf := bytes.Buffer{}
xmlwriter := bufio.NewWriter(&xmlbuf)
enc := xml.NewEncoder(xmlwriter)
enc.Indent(" ", " ")
err = apkparser.ParseXml(binbuf, enc, nil)
enc.Indent("", " ")
parser, err := apkparser.NewParser(apkReader, enc)
if err != nil {
return "", err
}

err = parser.ParseXml("AndroidManifest.xml")
if err != nil {
return "", err
}
Expand Down

0 comments on commit 26c4ca9

Please sign in to comment.