blob: 0b08c3276cb5908af7da66a3133fd734678f1d42 [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"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Lesinski769de982015-04-10 19:43:55 -070025namespace aapt {
26
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027struct NameManglerPolicy {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 /**
29 * Represents the package we are trying to build. References pointing
30 * to this package are not mangled, and mangled references inherit this
31 * package name.
32 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 std::string target_package_name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 /**
36 * We must know which references to mangle, and which to keep (android vs.
37 * com.android.support).
38 */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070039 std::set<std::string, std::less<>> packages_to_mangle;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040};
41
42class NameMangler {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 explicit NameMangler(NameManglerPolicy policy) : policy_(policy) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045
Ryan Mitchell4382e442021-07-14 12:53:01 -070046 std::optional<ResourceName> MangleName(const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 if (policy_.target_package_name == name.package ||
48 policy_.packages_to_mangle.count(name.package) == 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050 }
51
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 std::string mangled_entry_name = MangleEntry(name.package, name.entry);
53 return ResourceName(policy_.target_package_name, name.type,
54 mangled_entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070057 bool ShouldMangle(std::string_view package) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 if (package.empty() || policy_.target_package_name == package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return false;
60 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 return policy_.packages_to_mangle.count(package) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
63
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080064 const std::string& GetTargetPackageName() const { return policy_.target_package_name; }
65
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 /**
67 * Returns a mangled name that is a combination of `name` and `package`.
68 * The mangled name should contain symbols that are illegal to define in XML,
69 * so that there will never be name mangling collisions.
70 */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070071 static std::string MangleEntry(std::string_view package, std::string_view name) {
72 return (std::string(package) += '$') += name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 }
74
75 /**
76 * Unmangles the name in `outName`, storing the correct name back in `outName`
77 * and the package in `outPackage`. Returns true if the name was unmangled or
78 * false if the name was never mangled to begin with.
79 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 static bool Unmangle(std::string* out_name, std::string* out_package) {
81 size_t pivot = out_name->find('$');
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 if (pivot == std::string::npos) {
83 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 }
85
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 out_package->assign(out_name->data(), pivot);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070087 std::string new_name = out_name->substr(pivot + 1);
88 *out_name = std::move(new_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 return true;
90 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091
92 private:
93 NameManglerPolicy policy_;
Adam Lesinski769de982015-04-10 19:43:55 -070094};
95
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096} // namespace aapt
Adam Lesinski769de982015-04-10 19:43:55 -070097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098#endif // AAPT_NAME_MANGLER_H