Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "Resource.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "util/Maybe.h" |
| 22 | |
| 23 | #include <set> |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | struct NameManglerPolicy { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 29 | /** |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | * Represents the package we are trying to build. References pointing |
| 31 | * to this package are not mangled, and mangled references inherit this package name. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 32 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 33 | std::string targetPackageName; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * We must know which references to mangle, and which to keep (android vs. com.android.support). |
| 37 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 38 | std::set<std::string> packagesToMangle; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class NameMangler { |
| 42 | private: |
| 43 | NameManglerPolicy mPolicy; |
| 44 | |
| 45 | public: |
| 46 | NameMangler(NameManglerPolicy policy) : mPolicy(policy) { |
| 47 | } |
| 48 | |
| 49 | Maybe<ResourceName> mangleName(const ResourceName& name) { |
| 50 | if (mPolicy.targetPackageName == name.package || |
| 51 | mPolicy.packagesToMangle.count(name.package) == 0) { |
| 52 | return {}; |
| 53 | } |
| 54 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 55 | std::string mangledEntryName = mangleEntry(name.package, name.entry); |
| 56 | return ResourceName(mPolicy.targetPackageName, name.type, mangledEntryName); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 59 | bool shouldMangle(const std::string& package) const { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | if (package.empty() || mPolicy.targetPackageName == package) { |
| 61 | return false; |
| 62 | } |
| 63 | return mPolicy.packagesToMangle.count(package) != 0; |
| 64 | } |
| 65 | |
| 66 | /** |
| 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 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 71 | static std::string mangleEntry(const std::string& package, const std::string& name) { |
| 72 | return package + "$" + name; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 73 | } |
| 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 80 | static bool unmangle(std::string* outName, std::string* outPackage) { |
| 81 | size_t pivot = outName->find('$'); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 82 | if (pivot == std::string::npos) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | outPackage->assign(outName->data(), pivot); |
| 87 | outName->assign(outName->data() + pivot + 1, outName->size() - (pivot + 1)); |
| 88 | return true; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | } // namespace aapt |
| 93 | |
| 94 | #endif // AAPT_NAME_MANGLER_H |