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 | 78a8d37 | 2022-09-14 15:17:29 +0800 | [diff] [blame] | 23 | #include "ResourceMetadata.pb.h" |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 24 | #include "ResourceTable.h" |
| 25 | #include "android-base/function_ref.h" |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 26 | #include "android-base/macros.h" |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 27 | #include "cmd/Optimize.h" |
| 28 | #include "format/binary/TableFlattener.h" |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 29 | #include "process/IResourceTableConsumer.h" |
| 30 | |
| 31 | namespace aapt { |
| 32 | |
| 33 | class ResourceTable; |
| 34 | |
| 35 | // Maps resources in the apk to shortened paths. |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 36 | class Obfuscator : public IResourceTableConsumer { |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 37 | public: |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 38 | explicit Obfuscator(OptimizeOptions& optimizeOptions); |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 39 | |
| 40 | bool Consume(IAaptContext* context, ResourceTable* table) override; |
| 41 | |
felkachang | 78a8d37 | 2022-09-14 15:17:29 +0800 | [diff] [blame] | 42 | bool WriteObfuscationMap(const std::string& file_path) const; |
| 43 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 44 | bool IsEnabled() const; |
| 45 | |
| 46 | enum class Result { Obfuscated, Keep_ExemptionList, Keep_Overlayable }; |
| 47 | |
| 48 | // hardcoded string uses characters which make it an invalid resource name |
| 49 | static constexpr char kObfuscatedResourceName[] = "0_resource_name_obfuscated"; |
| 50 | |
| 51 | static void ObfuscateResourceName( |
| 52 | const bool collapse_key_stringpool, const std::set<ResourceName>& name_collapse_exemptions, |
| 53 | const ResourceNamedType& type_name, const ResourceTableEntryView& entry, |
| 54 | const android::base::function_ref<void(Result, const ResourceName&)> onObfuscate); |
| 55 | |
Mark Punzalan | 19e1d205 | 2023-07-11 20:37:45 +0000 | [diff] [blame] | 56 | protected: |
| 57 | virtual std::string ShortenFileName(android::StringPiece file_path, int output_length); |
| 58 | |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 59 | private: |
Mark Punzalan | 19e1d205 | 2023-07-11 20:37:45 +0000 | [diff] [blame] | 60 | bool HandleShortenFilePaths(ResourceTable* table, |
| 61 | std::map<std::string, std::string>& shortened_path_map, |
| 62 | const std::set<ResourceName>& path_shorten_exemptions); |
| 63 | |
felkachang | 8ceb39c | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 64 | TableFlattenerOptions& options_; |
| 65 | const bool shorten_resource_paths_; |
| 66 | const bool collapse_key_stringpool_; |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 67 | DISALLOW_COPY_AND_ASSIGN(Obfuscator); |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 68 | }; |
| 69 | |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 70 | } // namespace aapt |
Mohamed Heikal | c769403 | 2018-11-07 16:49:02 -0500 | [diff] [blame] | 71 | |
felkachang | 4bdd3ac | 2022-09-13 10:58:49 +0800 | [diff] [blame] | 72 | #endif // TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_ |