blob: 79d7e088d1cc8e3f69bd2cbfa260413ead0c5908 [file] [log] [blame]
Mohamed Heikalc7694032018-11-07 16:49:02 -05001/*
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
felkachang4bdd3ac2022-09-13 10:58:49 +080017#ifndef TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_
18#define TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_
Mohamed Heikalc7694032018-11-07 16:49:02 -050019
felkachang8ceb39c2022-09-13 10:58:49 +080020#include <set>
felkachang4bdd3ac2022-09-13 10:58:49 +080021#include <string>
Mohamed Heikalc7694032018-11-07 16:49:02 -050022
felkachang78a8d372022-09-14 15:17:29 +080023#include "ResourceMetadata.pb.h"
felkachang8ceb39c2022-09-13 10:58:49 +080024#include "ResourceTable.h"
25#include "android-base/function_ref.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050026#include "android-base/macros.h"
felkachang8ceb39c2022-09-13 10:58:49 +080027#include "cmd/Optimize.h"
28#include "format/binary/TableFlattener.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050029#include "process/IResourceTableConsumer.h"
30
31namespace aapt {
32
33class ResourceTable;
34
35// Maps resources in the apk to shortened paths.
felkachang4bdd3ac2022-09-13 10:58:49 +080036class Obfuscator : public IResourceTableConsumer {
Mohamed Heikalc7694032018-11-07 16:49:02 -050037 public:
felkachang8ceb39c2022-09-13 10:58:49 +080038 explicit Obfuscator(OptimizeOptions& optimizeOptions);
Mohamed Heikalc7694032018-11-07 16:49:02 -050039
40 bool Consume(IAaptContext* context, ResourceTable* table) override;
41
felkachang78a8d372022-09-14 15:17:29 +080042 bool WriteObfuscationMap(const std::string& file_path) const;
43
felkachang8ceb39c2022-09-13 10:58:49 +080044 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 Punzalan19e1d2052023-07-11 20:37:45 +000056 protected:
57 virtual std::string ShortenFileName(android::StringPiece file_path, int output_length);
58
Mohamed Heikalc7694032018-11-07 16:49:02 -050059 private:
Mark Punzalan19e1d2052023-07-11 20:37:45 +000060 bool HandleShortenFilePaths(ResourceTable* table,
61 std::map<std::string, std::string>& shortened_path_map,
62 const std::set<ResourceName>& path_shorten_exemptions);
63
felkachang8ceb39c2022-09-13 10:58:49 +080064 TableFlattenerOptions& options_;
65 const bool shorten_resource_paths_;
66 const bool collapse_key_stringpool_;
felkachang4bdd3ac2022-09-13 10:58:49 +080067 DISALLOW_COPY_AND_ASSIGN(Obfuscator);
Mohamed Heikalc7694032018-11-07 16:49:02 -050068};
69
felkachang4bdd3ac2022-09-13 10:58:49 +080070} // namespace aapt
Mohamed Heikalc7694032018-11-07 16:49:02 -050071
felkachang4bdd3ac2022-09-13 10:58:49 +080072#endif // TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_