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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "aapt.h"
#include "command.h"
#include "print.h"
#include "util.h"
#include <regex>
const regex NS_REGEX("( *)N: ([^=]+)=(.*)");
const regex ELEMENT_REGEX("( *)E: ([^ ]+) \\(line=(\\d+)\\)");
const regex ATTR_REGEX("( *)A: ([^\\(=]+)[^=]*=\"([^\"]+)\".*");
const string ANDROID_NS("http://schemas.android.com/apk/res/android");
bool
Apk::HasActivity(const string& className)
{
string fullClassName = full_class_name(package, className);
const size_t N = activities.size();
for (size_t i=0; i<N; i++) {
if (activities[i] == fullClassName) {
return true;
}
}
return false;
}
struct Attribute {
string ns;
string name;
string value;
};
struct Element {
Element* parent;
string ns;
string name;
int lineno;
vector<Attribute> attributes;
vector<Element*> children;
/**
* Indentation in the xmltree dump. Might not be equal to the distance
* from the root because namespace rows (scopes) have their own indentation.
*/
int depth;
Element();
~Element();
string GetAttr(const string& ns, const string& name) const;
void FindElements(const string& ns, const string& name, vector<Element*>* result, bool recurse);
};
Element::Element()
{
}
Element::~Element()
{
const size_t N = children.size();
for (size_t i=0; i<N; i++) {
delete children[i];
}
}
string
Element::GetAttr(const string& ns, const string& name) const
{
const size_t N = attributes.size();
for (size_t i=0; i<N; i++) {
const Attribute& attr = attributes[i];
if (attr.ns == ns && attr.name == name) {
return attr.value;
}
}
return string();
}
void
Element::FindElements(const string& ns, const string& name, vector<Element*>* result, bool recurse)
{
const size_t N = children.size();
for (size_t i=0; i<N; i++) {
Element* child = children[i];
if (child->ns == ns && child->name == name) {
result->push_back(child);
}
if (recurse) {
child->FindElements(ns, name, result, recurse);
}
}
}
struct Scope {
Scope* parent;
int depth;
map<string,string> namespaces;
Scope(Scope* parent, int depth);
};
Scope::Scope(Scope* p, int d)
:parent(p),
depth(d)
{
if (p != NULL) {
namespaces = p->namespaces;
}
}
string
full_class_name(const string& packageName, const string& className)
{
if (className.length() == 0) {
return "";
}
if (className[0] == '.') {
return packageName + className;
}
if (className.find('.') == string::npos) {
return packageName + "." + className;
}
return className;
}
string
pretty_component_name(const string& packageName, const string& className)
{
if (starts_with(packageName, className)) {
size_t pn = packageName.length();
size_t cn = className.length();
if (cn > pn && className[pn] == '.') {
return packageName + "/" + string(className, pn, string::npos);
}
}
return packageName + "/" + className;
}
int
inspect_apk(Apk* apk, const string& filename)
{
// Load the manifest xml
Command cmd("aapt2");
cmd.AddArg("dump");
cmd.AddArg("xmltree");
cmd.AddArg(filename);
cmd.AddArg("--file");
cmd.AddArg("AndroidManifest.xml");
int err;
string output = get_command_output(cmd, &err, false);
check_error(err);
// Parse the manifest xml
Scope* scope = new Scope(NULL, -1);
Element* root = NULL;
Element* current = NULL;
vector<string> lines;
split_lines(&lines, output);
for (size_t i=0; i<lines.size(); i++) {
const string& line = lines[i];
smatch match;
if (regex_match(line, match, NS_REGEX)) {
int depth = match[1].length() / 2;
while (depth < scope->depth) {
Scope* tmp = scope;
scope = scope->parent;
delete tmp;
}
scope = new Scope(scope, depth);
scope->namespaces[match[2]] = match[3];
} else if (regex_match(line, match, ELEMENT_REGEX)) {
Element* element = new Element();
string str = match[2];
size_t colon = str.find(':');
if (colon == string::npos) {
element->name = str;
} else {
element->ns = scope->namespaces[string(str, 0, colon)];
element->name.assign(str, colon+1, string::npos);
}
element->lineno = atoi(match[3].str().c_str());
element->depth = match[1].length() / 2;
if (root == NULL) {
current = element;
root = element;
} else {
while (element->depth <= current->depth && current->parent != NULL) {
current = current->parent;
}
element->parent = current;
current->children.push_back(element);
current = element;
}
} else if (regex_match(line, match, ATTR_REGEX)) {
if (current != NULL) {
Attribute attr;
string str = match[2];
size_t colon = str.rfind(':');
if (colon == string::npos) {
attr.name = str;
} else {
attr.ns.assign(str, 0, colon);
attr.name.assign(str, colon+1, string::npos);
}
attr.value = match[3];
current->attributes.push_back(attr);
}
}
}
while (scope != NULL) {
Scope* tmp = scope;
scope = scope->parent;
delete tmp;
}
// Package name
apk->package = root->GetAttr("", "package");
if (apk->package.size() == 0) {
print_error("%s:%d: Manifest root element doesn't contain a package attribute",
filename.c_str(), root->lineno);
delete root;
return 1;
}
// Instrumentation runner
vector<Element*> instrumentation;
root->FindElements("", "instrumentation", &instrumentation, true);
if (instrumentation.size() > 0) {
// TODO: How could we deal with multiple instrumentation tags?
// We'll just pick the first one.
apk->runner = instrumentation[0]->GetAttr(ANDROID_NS, "name");
}
// Activities
vector<Element*> activities;
root->FindElements("", "activity", &activities, true);
for (size_t i=0; i<activities.size(); i++) {
string name = activities[i]->GetAttr(ANDROID_NS, "name");
if (name.size() == 0) {
continue;
}
apk->activities.push_back(full_class_name(apk->package, name));
}
delete root;
return 0;
}
|