blob: 5ccf54383aae87b13443b51c64d8cb7eb39bd7bb [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
Mohamed Heikalc7694032018-11-07 16:49:02 -050056 private:
felkachang8ceb39c2022-09-13 10:58:49 +080057 TableFlattenerOptions& options_;
58 const bool shorten_resource_paths_;
59 const bool collapse_key_stringpool_;
felkachang4bdd3ac2022-09-13 10:58:49 +080060 DISALLOW_COPY_AND_ASSIGN(Obfuscator);
Mohamed Heikalc7694032018-11-07 16:49:02 -050061};
62
felkachang4bdd3ac2022-09-13 10:58:49 +080063} // namespace aapt
Mohamed Heikalc7694032018-11-07 16:49:02 -050064
felkachang4bdd3ac2022-09-13 10:58:49 +080065#endif // TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_