Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 17 | #ifndef TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_ |
| 18 | #define TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_ |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 19 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 20 | #include <set> |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 21 | #include <string> |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 22 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 23 | #include "ResourceTable.h" |
| 24 | #include "android-base/function_ref.h" |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 25 | #include "android-base/macros.h" |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 26 | #include "cmd/Optimize.h" |
| 27 | #include "format/binary/TableFlattener.h" |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 28 | #include "process/IResourceTableConsumer.h" |
| 29 | |
| 30 | namespace aapt { |
| 31 | |
| 32 | class ResourceTable; |
| 33 | |
| 34 | // Maps resources in the apk to shortened paths. |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 35 | class Obfuscator : public IResourceTableConsumer { |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 36 | public: |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 37 | explicit Obfuscator(OptimizeOptions& optimizeOptions); |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 38 | |
| 39 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 40 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 41 | bool IsEnabled() const; |
| 42 | |
| 43 | enum class Result { Obfuscated, Keep_ExemptionList, Keep_Overlayable }; |
| 44 | |
| 45 | // hardcoded string uses characters which make it an invalid resource name |
| 46 | static constexpr char kObfuscatedResourceName[] = "0_resource_name_obfuscated"; |
| 47 | |
| 48 | static void ObfuscateResourceName( |
| 49 | const bool collapse_key_stringpool, const std::set<ResourceName>& name_collapse_exemptions, |
| 50 | const ResourceNamedType& type_name, const ResourceTableEntryView& entry, |
| 51 | const android::base::function_ref<void(Result, const ResourceName&)> onObfuscate); |
| 52 | |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 53 | private: |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame^] | 54 | TableFlattenerOptions& options_; |
| 55 | const bool shorten_resource_paths_; |
| 56 | const bool collapse_key_stringpool_; |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 57 | DISALLOW_COPY_AND_ASSIGN(Obfuscator); |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 58 | }; |
| 59 | |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 60 | } // namespace aapt |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 61 | |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 62 | #endif // TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_ |