blob: f1aad29a5c58a1fa49361bb0f5a85f37d124a5ec [file] [log] [blame]
Adam Lesinski769de982015-04-10 19:43:55 -07001/*
2 * Copyright (C) 2015 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#ifndef AAPT_NAME_MANGLER_H
18#define AAPT_NAME_MANGLER_H
19
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include <set>
Adam Lesinski769de982015-04-10 19:43:55 -070021#include <string>
22
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "Resource.h"
24#include "util/Maybe.h"
25
Adam Lesinski769de982015-04-10 19:43:55 -070026namespace aapt {
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028struct NameManglerPolicy {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 /**
30 * Represents the package we are trying to build. References pointing
31 * to this package are not mangled, and mangled references inherit this
32 * package name.
33 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 std::string target_package_name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 /**
37 * We must know which references to mangle, and which to keep (android vs.
38 * com.android.support).
39 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 std::set<std::string> packages_to_mangle;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041};
42
43class NameMangler {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 explicit NameMangler(NameManglerPolicy policy) : policy_(policy) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 Maybe<ResourceName> MangleName(const ResourceName& name) {
48 if (policy_.target_package_name == name.package ||
49 policy_.packages_to_mangle.count(name.package) == 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051 }
52
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 std::string mangled_entry_name = MangleEntry(name.package, name.entry);
54 return ResourceName(policy_.target_package_name, name.type,
55 mangled_entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 bool ShouldMangle(const std::string& package) const {
59 if (package.empty() || policy_.target_package_name == package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return false;
61 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 return policy_.packages_to_mangle.count(package) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 }
64
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080065 const std::string& GetTargetPackageName() const { return policy_.target_package_name; }
66
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 /**
68 * Returns a mangled name that is a combination of `name` and `package`.
69 * The mangled name should contain symbols that are illegal to define in XML,
70 * so that there will never be name mangling collisions.
71 */
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070072 static std::string MangleEntry(const std::string& package, const std::string& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 return package + "$" + name;
74 }
75
76 /**
77 * Unmangles the name in `outName`, storing the correct name back in `outName`
78 * and the package in `outPackage`. Returns true if the name was unmangled or
79 * false if the name was never mangled to begin with.
80 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 static bool Unmangle(std::string* out_name, std::string* out_package) {
82 size_t pivot = out_name->find('$');
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 if (pivot == std::string::npos) {
84 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085 }
86
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 out_package->assign(out_name->data(), pivot);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070088 std::string new_name = out_name->substr(pivot + 1);
89 *out_name = std::move(new_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 return true;
91 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092
93 private:
94 NameManglerPolicy policy_;
Adam Lesinski769de982015-04-10 19:43:55 -070095};
96
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097} // namespace aapt
Adam Lesinski769de982015-04-10 19:43:55 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099#endif // AAPT_NAME_MANGLER_H