blob: 786bf8c9ed938f14a3a33014cf023541cbfc38df [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
felkachang8ceb39c2022-09-13 10:58:49 +080023#include "ResourceTable.h"
24#include "android-base/function_ref.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050025#include "android-base/macros.h"
felkachang8ceb39c2022-09-13 10:58:49 +080026#include "cmd/Optimize.h"
27#include "format/binary/TableFlattener.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050028#include "process/IResourceTableConsumer.h"
29
30namespace aapt {
31
32class ResourceTable;
33
34// Maps resources in the apk to shortened paths.
felkachang4bdd3ac2022-09-13 10:58:49 +080035class Obfuscator : public IResourceTableConsumer {
Mohamed Heikalc7694032018-11-07 16:49:02 -050036 public:
felkachang8ceb39c2022-09-13 10:58:49 +080037 explicit Obfuscator(OptimizeOptions& optimizeOptions);
Mohamed Heikalc7694032018-11-07 16:49:02 -050038
39 bool Consume(IAaptContext* context, ResourceTable* table) override;
40
felkachang8ceb39c2022-09-13 10:58:49 +080041 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 Heikalc7694032018-11-07 16:49:02 -050053 private:
felkachang8ceb39c2022-09-13 10:58:49 +080054 TableFlattenerOptions& options_;
55 const bool shorten_resource_paths_;
56 const bool collapse_key_stringpool_;
felkachang4bdd3ac2022-09-13 10:58:49 +080057 DISALLOW_COPY_AND_ASSIGN(Obfuscator);
Mohamed Heikalc7694032018-11-07 16:49:02 -050058};
59
felkachang4bdd3ac2022-09-13 10:58:49 +080060} // namespace aapt
Mohamed Heikalc7694032018-11-07 16:49:02 -050061
felkachang4bdd3ac2022-09-13 10:58:49 +080062#endif // TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_