1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "cmd/Util.h"
18 
19 #include <vector>
20 
21 #include "android-base/logging.h"
22 #include "androidfw/ConfigDescription.h"
23 #include "androidfw/Locale.h"
24 
25 #include "ResourceUtils.h"
26 #include "ValueVisitor.h"
27 #include "split/TableSplitter.h"
28 #include "util/Maybe.h"
29 #include "util/Util.h"
30 
31 using ::android::ConfigDescription;
32 using ::android::LocaleValue;
33 using ::android::StringPiece;
34 using ::android::base::StringPrintf;
35 
36 namespace aapt {
37 
ParseTargetDensityParameter(const StringPiece & arg,IDiagnostics * diag)38 Maybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
39   ConfigDescription preferred_density_config;
40   if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
41     diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option");
42     return {};
43   }
44 
45   // Clear the version that can be automatically added.
46   preferred_density_config.sdkVersion = 0;
47 
48   if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
49       ConfigDescription::CONFIG_DENSITY) {
50     diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. "
51                               << "Preferred density must only be a density value");
52     return {};
53   }
54   return preferred_density_config.density;
55 }
56 
ParseSplitParameter(const StringPiece & arg,IDiagnostics * diag,std::string * out_path,SplitConstraints * out_split)57 bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path,
58                          SplitConstraints* out_split) {
59   CHECK(diag != nullptr);
60   CHECK(out_path != nullptr);
61   CHECK(out_split != nullptr);
62 
63 #ifdef _WIN32
64   const char sSeparator = ';';
65 #else
66   const char sSeparator = ':';
67 #endif
68 
69   std::vector<std::string> parts = util::Split(arg, sSeparator);
70   if (parts.size() != 2) {
71     diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
72     diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator
73                              << "<config>[,<config>...].");
74     return false;
75   }
76 
77   *out_path = parts[0];
78   out_split->name = parts[1];
79   for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
80     ConfigDescription config;
81     if (!ConfigDescription::Parse(config_str, &config)) {
82       diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '"
83                                 << arg << "'");
84       return false;
85     }
86     out_split->configs.insert(config);
87   }
88   return true;
89 }
90 
ParseConfigFilterParameters(const std::vector<std::string> & args,IDiagnostics * diag)91 std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
92                                                            IDiagnostics* diag) {
93   std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
94   for (const std::string& config_arg : args) {
95     for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
96       ConfigDescription config;
97       LocaleValue lv;
98       if (lv.InitFromFilterString(config_str)) {
99         lv.WriteTo(&config);
100       } else if (!ConfigDescription::Parse(config_str, &config)) {
101         diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option");
102         return {};
103       }
104 
105       if (config.density != 0) {
106         diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option");
107       } else {
108         filter->AddConfig(config);
109       }
110     }
111   }
112   return std::move(filter);
113 }
114 
115 // Adjust the SplitConstraints so that their SDK version is stripped if it
116 // is less than or equal to the minSdk. Otherwise the resources that have had
117 // their SDK version stripped due to minSdk won't ever match.
AdjustSplitConstraintsForMinSdk(int min_sdk,const std::vector<SplitConstraints> & split_constraints)118 std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
119     int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
120   std::vector<SplitConstraints> adjusted_constraints;
121   adjusted_constraints.reserve(split_constraints.size());
122   for (const SplitConstraints& constraints : split_constraints) {
123     SplitConstraints constraint;
124     for (const ConfigDescription& config : constraints.configs) {
125       const ConfigDescription &configToInsert = (config.sdkVersion <= min_sdk)
126           ? config.CopyWithoutSdkVersion()
127           : config;
128       // only add the config if it actually selects something
129       if (configToInsert != ConfigDescription::DefaultConfig()) {
130         constraint.configs.insert(configToInsert);
131       }
132     }
133     constraint.name = constraints.name;
134     adjusted_constraints.push_back(std::move(constraint));
135   }
136   return adjusted_constraints;
137 }
138 
CreateAttributeWithId(const ResourceId & id)139 static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
140   return xml::AaptAttribute(Attribute(), id);
141 }
142 
CreateAndroidNamespaceDecl()143 static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
144   xml::NamespaceDecl decl;
145   decl.prefix = "android";
146   decl.uri = xml::kSchemaAndroid;
147   return decl;
148 }
149 
150 // Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
151 // replacing nonconforming characters with underscores.
152 //
153 // See frameworks/base/core/java/android/content/pm/PackageParser.java which
154 // checks this at runtime.
MakePackageSafeName(const std::string & name)155 std::string MakePackageSafeName(const std::string &name) {
156   std::string result(name);
157   bool first = true;
158   for (char &c : result) {
159     if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
160       first = false;
161       continue;
162     }
163     if (!first) {
164       if (c >= '0' && c <= '9') {
165         continue;
166       }
167     }
168 
169     c = '_';
170     first = false;
171   }
172   return result;
173 }
174 
GenerateSplitManifest(const AppInfo & app_info,const SplitConstraints & constraints)175 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
176                                                         const SplitConstraints& constraints) {
177   const ResourceId kVersionCode(0x0101021b);
178   const ResourceId kVersionCodeMajor(0x01010576);
179   const ResourceId kRevisionCode(0x010104d5);
180   const ResourceId kHasCode(0x0101000c);
181 
182   std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
183   manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
184   manifest_el->name = "manifest";
185   manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
186 
187   if (app_info.version_code) {
188     const uint32_t version_code = app_info.version_code.value();
189     manifest_el->attributes.push_back(xml::Attribute{
190         xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
191         CreateAttributeWithId(kVersionCode),
192         util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
193   }
194 
195   if (app_info.version_code_major) {
196     const uint32_t version_code_major = app_info.version_code_major.value();
197     manifest_el->attributes.push_back(xml::Attribute{
198         xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
199         CreateAttributeWithId(kVersionCodeMajor),
200         util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
201   }
202 
203   if (app_info.revision_code) {
204     const uint32_t revision_code = app_info.revision_code.value();
205     manifest_el->attributes.push_back(xml::Attribute{
206         xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
207         CreateAttributeWithId(kRevisionCode),
208         util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
209   }
210 
211   std::stringstream split_name;
212   if (app_info.split_name) {
213     split_name << app_info.split_name.value() << ".";
214   }
215   std::vector<std::string> sanitized_config_names;
216   for (const auto &config : constraints.configs) {
217     sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
218   }
219   split_name << "config." << util::Joiner(sanitized_config_names, "_");
220 
221   manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
222 
223   if (app_info.split_name) {
224     manifest_el->attributes.push_back(
225         xml::Attribute{"", "configForSplit", app_info.split_name.value()});
226   }
227 
228   // Splits may contain more configurations than originally desired (fall-back densities, etc.).
229   // This makes programmatic discovery of split targeting difficult. Encode the original
230   // split constraints intended for this split.
231   std::stringstream target_config_str;
232   target_config_str << util::Joiner(constraints.configs, ",");
233   manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
234 
235   std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
236   application_el->name = "application";
237   application_el->attributes.push_back(
238       xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
239                      util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
240 
241   manifest_el->AppendChild(std::move(application_el));
242 
243   std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
244   doc->root = std::move(manifest_el);
245   return doc;
246 }
247 
ExtractCompiledString(const xml::Attribute & attr,std::string * out_error)248 static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr,
249                                                 std::string* out_error) {
250   if (attr.compiled_value != nullptr) {
251     const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
252     if (compiled_str != nullptr) {
253       if (!compiled_str->value->empty()) {
254         return *compiled_str->value;
255       } else {
256         *out_error = "compiled value is an empty string";
257         return {};
258       }
259     }
260     *out_error = "compiled value is not a string";
261     return {};
262   }
263 
264   // Fallback to the plain text value if there is one.
265   if (!attr.value.empty()) {
266     return attr.value;
267   }
268   *out_error = "value is an empty string";
269   return {};
270 }
271 
ExtractCompiledInt(const xml::Attribute & attr,std::string * out_error)272 static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) {
273   if (attr.compiled_value != nullptr) {
274     const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
275     if (compiled_prim != nullptr) {
276       if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
277           compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
278         return compiled_prim->value.data;
279       }
280     }
281     *out_error = "compiled value is not an integer";
282     return {};
283   }
284 
285   // Fallback to the plain text value if there is one.
286   Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
287   if (integer) {
288     return integer;
289   }
290   std::stringstream error_msg;
291   error_msg << "'" << attr.value << "' is not a valid integer";
292   *out_error = error_msg.str();
293   return {};
294 }
295 
ExtractSdkVersion(const xml::Attribute & attr,std::string * out_error)296 static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
297   if (attr.compiled_value != nullptr) {
298     const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
299     if (compiled_prim != nullptr) {
300       if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
301           compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
302         return compiled_prim->value.data;
303       }
304       *out_error = "compiled value is not an integer or string";
305       return {};
306     }
307 
308     const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
309     if (compiled_str != nullptr) {
310       Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
311       if (sdk_version) {
312         return sdk_version;
313       }
314 
315       *out_error = "compiled string value is not a valid SDK version";
316       return {};
317     }
318     *out_error = "compiled value is not an integer or string";
319     return {};
320   }
321 
322   // Fallback to the plain text value if there is one.
323   Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
324   if (sdk_version) {
325     return sdk_version;
326   }
327   std::stringstream error_msg;
328   error_msg << "'" << attr.value << "' is not a valid SDK version";
329   *out_error = error_msg.str();
330   return {};
331 }
332 
ExtractAppInfoFromBinaryManifest(const xml::XmlResource & xml_res,IDiagnostics * diag)333 Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
334                                                 IDiagnostics* diag) {
335   // Make sure the first element is <manifest> with package attribute.
336   const xml::Element* manifest_el = xml_res.root.get();
337   if (manifest_el == nullptr) {
338     return {};
339   }
340 
341   AppInfo app_info;
342 
343   if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
344     diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
345     return {};
346   }
347 
348   const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
349   if (!package_attr) {
350     diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute");
351     return {};
352   }
353 
354   std::string error_msg;
355   Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
356   if (!maybe_package) {
357     diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
358                 << "invalid package name: " << error_msg);
359     return {};
360   }
361   app_info.package = maybe_package.value();
362 
363   if (const xml::Attribute* version_code_attr =
364           manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
365     Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
366     if (!maybe_code) {
367       diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
368                   << "invalid android:versionCode: " << error_msg);
369       return {};
370     }
371     app_info.version_code = maybe_code.value();
372   }
373 
374   if (const xml::Attribute* version_code_major_attr =
375       manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
376     Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
377     if (!maybe_code) {
378       diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
379                       << "invalid android:versionCodeMajor: " << error_msg);
380       return {};
381     }
382     app_info.version_code_major = maybe_code.value();
383   }
384 
385   if (const xml::Attribute* revision_code_attr =
386           manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
387     Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
388     if (!maybe_code) {
389       diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
390                   << "invalid android:revisionCode: " << error_msg);
391       return {};
392     }
393     app_info.revision_code = maybe_code.value();
394   }
395 
396   if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
397     Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg);
398     if (!maybe_split_name) {
399       diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
400                   << "invalid split name: " << error_msg);
401       return {};
402     }
403     app_info.split_name = maybe_split_name.value();
404   }
405 
406   if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
407     if (const xml::Attribute* min_sdk =
408             uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
409       Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
410       if (!maybe_sdk) {
411         diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
412                     << "invalid android:minSdkVersion: " << error_msg);
413         return {};
414       }
415       app_info.min_sdk_version = maybe_sdk.value();
416     }
417   }
418   return app_info;
419 }
420 
SetLongVersionCode(xml::Element * manifest,uint64_t version)421 void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
422   // Write the low bits of the version code to android:versionCode
423   auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
424   version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
425   version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
426 
427   auto version_high = (uint32_t) (version >> 32);
428   if (version_high != 0) {
429     // Write the high bits of the version code to android:versionCodeMajor
430     auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
431     version_major->value = StringPrintf("0x%08x", version_high);
432     version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
433   } else {
434     manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
435   }
436 }
437 
GetRegularExpression(const std::string & input)438 std::regex GetRegularExpression(const std::string &input) {
439   // Standard ECMAScript grammar.
440   std::regex case_insensitive(
441       input, std::regex_constants::ECMAScript);
442   return case_insensitive;
443 }
444 
445 }  // namespace aapt
446