smali_utils.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. import os.path
  3. def get_smali_method_count(smaliFile, allMethods):
  4. if not os.path.exists(smaliFile):
  5. return 0
  6. f = open(smaliFile, 'r', encoding='UTF-8')
  7. lines = f.readlines()
  8. f.close()
  9. classLine = lines[0]
  10. classLine.strip()
  11. if not classLine.startswith('.class'):
  12. print(f + ' not startswith .class')
  13. return 0
  14. className = parse_class(classLine)
  15. count = 0
  16. for line in lines:
  17. line = line.strip()
  18. method = None
  19. if line.startswith('.method'):
  20. method = parse_method_default(className, line)
  21. elif line.startswith('invoke-'):
  22. method = parse_method_invoke(line)
  23. if method is None:
  24. continue
  25. if method not in allMethods:
  26. count = count + 1
  27. allMethods.append(method)
  28. else:
  29. pass
  30. return count
  31. def parse_class(line):
  32. if not line.startswith('.class'):
  33. print('line parse error. not startswith .class : '+line)
  34. return None
  35. blocks = line.split()
  36. return blocks[len(blocks)-1]
  37. def parse_method_default(className, line):
  38. if not line.startswith('.method'):
  39. print('the line parse error in parse_method_default:'+line)
  40. return None
  41. blocks = line.split()
  42. return className + '->' + blocks[len(blocks)-1]
  43. def parse_method_invoke(line):
  44. if not line.startswith('invoke-'):
  45. print('the line parse error in parse_method_invoke:'+line)
  46. blocks = line.split()
  47. return blocks[len(blocks)-1]