tools.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package aapt
  2. import (
  3. "archive/zip"
  4. "log"
  5. "strings"
  6. "notabug.org/Umnik/GoAndroidSDK/v2/components/misc"
  7. )
  8. func findApkSignature(apkFile string) ([]string, error) {
  9. zipReader, err := zip.OpenReader(apkFile)
  10. if err != nil {
  11. return nil, err
  12. }
  13. defer func(zipReader *zip.ReadCloser) {
  14. if e := zipReader.Close(); e != nil {
  15. log.Print(e)
  16. }
  17. }(zipReader)
  18. var (
  19. rsaFiles []string
  20. sfFiles []string
  21. otherFiles []string
  22. )
  23. for _, zf := range zipReader.File {
  24. switch {
  25. case zf.FileInfo().IsDir():
  26. continue
  27. case strings.HasPrefix(zf.Name, "META-INF"):
  28. f := strings.ToLower(zf.Name)
  29. switch {
  30. case strings.HasSuffix(f, ".rsa"):
  31. rsaFiles = append(rsaFiles, zf.Name)
  32. case strings.HasSuffix(f, ".sf"):
  33. sfFiles = append(sfFiles, zf.Name)
  34. case strings.HasSuffix(f, ".mf"):
  35. otherFiles = append(otherFiles, zf.Name)
  36. }
  37. }
  38. }
  39. return append(rsaFiles, append(sfFiles, otherFiles...)...), nil
  40. }
  41. func processPackageLine(line string) (result [3]string) {
  42. parts := strings.Split(misc.TwoSpotValue(line), " ")
  43. for _, part := range parts {
  44. part = strings.ReplaceAll(part, "=", ":")
  45. switch {
  46. case strings.HasPrefix(part, "name"):
  47. result[0] = misc.RemoveQuoteMark(misc.TwoSpotValue(part))
  48. case strings.HasPrefix(part, "versionCode"):
  49. result[1] = misc.RemoveQuoteMark(misc.TwoSpotValue(part))
  50. case strings.HasPrefix(part, "versionName"):
  51. result[2] = misc.RemoveQuoteMark(misc.TwoSpotValue(part))
  52. }
  53. }
  54. return
  55. }
  56. func extractPermission(line string) string {
  57. return misc.RemoveQuoteMark(strings.SplitN(line, "=", 2)[1])
  58. }
  59. func extractLabel(line string) (key, value string) {
  60. val := "application-label"
  61. parts := strings.SplitN(line, ":", 2)
  62. value = misc.RemoveQuoteMark(parts[1])
  63. if parts[0] == val {
  64. key = "NoLocale"
  65. return
  66. }
  67. key = parts[0][len(val)+1:]
  68. return
  69. }
  70. func extractMetaData(line string) (res [2]string) {
  71. parts := strings.SplitN(line, " ", 3)
  72. res[0] = misc.EqualSignRexValue(misc.RemoveQuoteMark(parts[1]))
  73. res[1] = misc.EqualSignRexValue(misc.RemoveQuoteMark(parts[2]))
  74. return
  75. }
  76. func extractLaunchActivity(line string) string {
  77. for _, part := range strings.Split(line, " ") {
  78. if strings.HasPrefix(part, "name='") {
  79. return strings.Split(part, "'")[1]
  80. }
  81. }
  82. return ""
  83. }
  84. func extractNativeCode(line string) map[string]struct{} {
  85. result := make(map[string]struct{})
  86. parts := strings.Split(misc.TwoSpotValue(line), " ")
  87. for _, val := range parts {
  88. result[misc.RemoveQuoteMark(val)] = struct{}{}
  89. }
  90. return result
  91. }
  92. func resArrName(line string) string {
  93. rawName := strings.SplitN(line, ":", 3)[1]
  94. return strings.SplitN(rawName, "/", 2)[1]
  95. }
  96. func resStringName(line string) string {
  97. return resArrName(line)
  98. }
  99. func resArrValues(source []string) [][2]string {
  100. result := make([][2]string, len(source))
  101. for i, val := range source {
  102. line := strings.TrimSpace(val)
  103. if !strings.HasPrefix(line, "#") {
  104. panic("output format was changed")
  105. }
  106. parts := strings.SplitN(line, " ", 4)
  107. values := [2]string{misc.RemoveBraces(parts[2]), misc.RemoveQuoteMark(parts[3])}
  108. result[i] = values
  109. }
  110. return result
  111. }
  112. func resStringValue(line string) (result [2]string) {
  113. line = strings.TrimSpace(line)
  114. if !strings.HasPrefix(line, "(") {
  115. panic("out format was changed")
  116. }
  117. parts := strings.SplitN(line, " ", 2)
  118. result[0] = misc.RemoveBraces(parts[0])
  119. result[1] = misc.RemoveQuoteMark(parts[1])
  120. return
  121. }