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 17 #ifndef TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_ 18 #define TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_ 19 20 #include <set> 21 #include <string> 22 23 #include "ResourceMetadata.pb.h" 24 #include "ResourceTable.h" 25 #include "android-base/function_ref.h" 26 #include "android-base/macros.h" 27 #include "cmd/Optimize.h" 28 #include "format/binary/TableFlattener.h" 29 #include "process/IResourceTableConsumer.h" 30 31 namespace aapt { 32 33 class ResourceTable; 34 35 // Maps resources in the apk to shortened paths. 36 class Obfuscator : public IResourceTableConsumer { 37 public: 38 explicit Obfuscator(OptimizeOptions& optimizeOptions); 39 40 bool Consume(IAaptContext* context, ResourceTable* table) override; 41 42 bool WriteObfuscationMap(const std::string& file_path) const; 43 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 56 protected: 57 virtual std::string ShortenFileName(android::StringPiece file_path, int output_length); 58 59 private: 60 bool HandleShortenFilePaths(ResourceTable* table, 61 std::map<std::string, std::string>& shortened_path_map, 62 const std::set<ResourceName>& path_shorten_exemptions); 63 64 TableFlattenerOptions& options_; 65 const bool shorten_resource_paths_; 66 const bool collapse_key_stringpool_; 67 DISALLOW_COPY_AND_ASSIGN(Obfuscator); 68 }; 69 70 } // namespace aapt 71 72 #endif // TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_ 73