Skip to content

Commit

Permalink
Add locale option to get_app_name() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Forgo7ten committed Apr 19, 2024
1 parent 72e29ad commit 33c99a1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
5 changes: 3 additions & 2 deletions androguard/core/apk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def get_filename(self):
"""
return self.filename

def get_app_name(self):
def get_app_name(self, **kwargs):
"""
Return the appname of the APK
Expand Down Expand Up @@ -500,9 +500,10 @@ def get_app_name(self):
return app_name

try:
config = ARSCResTableConfig(None, **kwargs) if kwargs else ARSCResTableConfig.default_config()
app_name = res_parser.get_resolved_res_configs(
res_id,
ARSCResTableConfig.default_config())[0][1]
config)[0][1]
except Exception as e:
logger.warning("Exception selecting app name: %s" % e)
return app_name
Expand Down
27 changes: 26 additions & 1 deletion androguard/core/axml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2510,7 +2510,12 @@ def __init__(self, buff=None, **kwargs):
((kwargs.pop('mcc', 0) & 0xffff) << 0) + \
((kwargs.pop('mnc', 0) & 0xffff) << 16)

self.locale = 0
temp_locale = kwargs.pop('locale', 0)
if isinstance(temp_locale, str):
self.set_language_and_region(temp_locale)
else:
self.locale = temp_locale

for char_ix, char in kwargs.pop('locale', "")[0:4]:
self.locale += (ord(char) << (char_ix * 8))

Expand Down Expand Up @@ -2563,6 +2568,26 @@ def _unpack_language_or_region(self, char_in, char_base):
char_out += chr(char_in[1])
return char_out

def _pack_language_or_region(self, char_in):
char_out = [0x00, 0x00]
if len(char_in) != 2:
return char_out
char_out[0] = ord(char_in[0])
char_out[1] = ord(char_in[1])
return char_out

def set_language_and_region(self, language_region):
try:
language, region = language_region.split("-r")
except ValueError:
language, region = language_region, None
language_bytes = self._pack_language_or_region(language)
if region:
region_bytes = self._pack_language_or_region(region)
else:
region_bytes = [0x00, 0x00]
self.locale = language_bytes[0] | (language_bytes[1] << 8) | (region_bytes[0] << 16) | (region_bytes[1] << 24)

def get_language_and_region(self):
"""
Returns the combined language+region string or \x00\x00 for the default locale
Expand Down
Binary file added tests/data/APK/multiple_locale_appname_test.apk
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,15 @@ def testShortNamesInManifest(self):
self.assertEqual(a._format_value('bla.bar.foo'), 'bla.bar.foo')
self.assertEqual(a._format_value(None), None)

def testMultipleLocaleAppName(self):
"""Test multiple locale appname"""
a = apk.APK(os.path.join(test_dir, 'data/APK/multiple_locale_appname_test.apk'))
self.assertEqual(a.get_app_name(), "values")
self.assertEqual(a.get_app_name(locale='en'), "values-en")
self.assertEqual(a.get_app_name(locale='zh-rCN'), "values-zh-rCN")
self.assertEqual(a.get_app_name(locale='zh-rTW'), "values-zh-rTW")
self.assertEqual(a.get_app_name(locale='ru-rRU'), "values-ru-rRU")


if __name__ == '__main__':
unittest.main(failfast=True)

0 comments on commit 33c99a1

Please sign in to comment.