This repository was archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApkName.cpp
More file actions
223 lines (193 loc) · 6.22 KB
/
ApkName.cpp
File metadata and controls
223 lines (193 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// Copyright 2006 The Android Open Source Project
//
// Android Asset Packaging Tool main entry point.
//
// Trimmed AAPT to only return apk packange name and application label.
// Edited by @Devil7DK on 01-06-2018
#include <cstdlib>
#include <getopt.h>
#include <cassert>
#include <errno.h>
#include <fcntl.h>
#include <iostream>
#include <string>
#include <sstream>
#include "AaptXml.h"
#include "ApkBuilder.h"
#include "Bundle.h"
#include "Images.h"
#include "Main.h"
#include "ResourceFilter.h"
#include "ResourceTable.h"
#include "XMLNode.h"
bool containsOnlyASCII(const std::string& text) {
for (auto c: text) {
if (static_cast<unsigned char>(c) > 127) {
return false;
}
}
return true;
}
std::string trim(std::string s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
int main(int argc, char* const argv[])
{
if (argc < 2){
fprintf(stderr, "ERROR: no file supplied");
return 1;
}
std::string package_name = "unknown";
std::string app_name = "unknown";
const char* filename = argv[1];
AssetManager assets;
int32_t assetsCookie;
assets.addAssetPath(String8(filename), &assetsCookie);
// Make a dummy config for retrieving resources... we need to supply
// non-default values for some configs so that we can retrieve resources
// in the app that don't have a default. The most important of these is
// the API version because key resources like icons will have an implicit
// version if they are using newer config types like density.
ResTable_config config;
memset(&config, 0, sizeof(ResTable_config));
config.language[0] = 'e';
config.language[1] = 'n';
config.country[0] = 'U';
config.country[1] = 'S';
config.orientation = ResTable_config::ORIENTATION_PORT;
config.density = ResTable_config::DENSITY_MEDIUM;
config.sdkVersion = 10000; // Very high.
config.screenWidthDp = 320;
config.screenHeightDp = 480;
config.smallestScreenWidthDp = 320;
config.screenLayout |= ResTable_config::SCREENSIZE_NORMAL;
assets.setConfiguration(config);
const ResTable& res = assets.getResources(false);
// The dynamicRefTable can be null if there are no resources for this asset cookie.
// This fine.
const DynamicRefTable* dynamicRefTable = res.getDynamicRefTableForCookie(assetsCookie);
Asset* asset = NULL;
asset = assets.openNonAsset(assetsCookie, "AndroidManifest.xml", Asset::ACCESS_BUFFER);
ResXMLTree tree(dynamicRefTable);
tree.setTo(asset->getBuffer(true), asset->getLength());
tree.restart();
Vector<String8> locales;
res.getLocales(&locales);
Vector<ResTable_config> configs;
res.getConfigurations(&configs);
SortedVector<int> densities;
const size_t NC = configs.size();
for (size_t i=0; i<NC; i++) {
int dens = configs[i].density;
if (dens == 0) {
dens = 160;
}
densities.add(dens);
}
size_t len;
ResXMLTree::event_code_t code;
int depth = 0;
String8 error;
bool withinActivity = false;
bool isMainActivity = false;
bool isLauncherActivity = false;
bool isLeanbackLauncherActivity = false;
bool withinApplication = false;
bool withinSupportsInput = false;
bool withinFeatureGroup = false;
bool withinReceiver = false;
bool withinService = false;
bool withinProvider = false;
bool hasOtherActivities = false;
bool hasOtherReceivers = false;
bool hasOtherServices = false;
bool hasIntentFilter = false;
bool hasPaymentService = false;
bool actHostApduService = false;
bool actOffHostApduService = false;
bool hasMetaHostPaymentCategory = false;
bool hasMetaOffHostPaymentCategory = false;
bool hasBindNfcServicePermission = false;
String8 activityName;
String8 activityLabel;
String8 activityIcon;
String8 activityBanner;
String8 receiverName;
String8 serviceName;
Vector<String8> supportedInput;
while ((code=tree.next()) != ResXMLTree::END_DOCUMENT &&
code != ResXMLTree::BAD_DOCUMENT) {
if (code == ResXMLTree::END_TAG) {
depth--;
if (depth < 2) {
withinApplication = false;
withinSupportsInput = false;
withinFeatureGroup = false;
} else if (depth < 3) {
if (!hasIntentFilter) {
hasOtherActivities |= withinActivity;
hasOtherReceivers |= withinReceiver;
hasOtherServices |= withinService;
} else {
if (withinService) {
hasPaymentService |= (actHostApduService && hasMetaHostPaymentCategory &&
hasBindNfcServicePermission);
hasPaymentService |= (actOffHostApduService && hasMetaOffHostPaymentCategory &&
hasBindNfcServicePermission);
}
}
withinActivity = false;
withinService = false;
withinReceiver = false;
withinProvider = false;
hasIntentFilter = false;
isMainActivity = isLauncherActivity = isLeanbackLauncherActivity = false;
}
continue;
}
if (code != ResXMLTree::START_TAG) {
continue;
}
depth++;
const char16_t* ctag16 = tree.getElementName(&len);
String8 tag(ctag16);
if (depth == 1) {
package_name = AaptXml::getAttribute(tree, NULL, "package", NULL);;
} else if (depth == 2) {
withinApplication = false;
if (tag == "application") {
withinApplication = true;
String8 label;
const size_t NL = locales.size();
for (size_t i=0; i<NL; i++) {
const char* localeStr = locales[i].string();
assets.setConfiguration(config, localeStr != NULL ? localeStr : "");
String8 llabel = AaptXml::getResolvedAttribute(res, tree, 0x01010001, &error);
if (llabel != "") {
if (localeStr == NULL || strlen(localeStr) == 0) {
label = llabel;
} else {
bool label_isASCII = containsOnlyASCII(ResTable::normalizeForOutput(label.string()).string());
bool llabel_isASCII = containsOnlyASCII(ResTable::normalizeForOutput(llabel.string()).string());
if (label == "" || (label_isASCII == false && llabel_isASCII == true)) {
label = llabel;
}
}
}
}
assets.setConfiguration(config);
app_name = label;
}
}
}
if (app_name == "" || containsOnlyASCII(app_name) == false) {
app_name = package_name;
}
printf("%s=%s\n",ResTable::normalizeForOutput(package_name.c_str()).string(),ResTable::normalizeForOutput(app_name.c_str()).string());
return 0;
}