blob: f704f26bfd291714bd2ffca549cd59f1ca2881cc [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#include "optimize/Obfuscator.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050018
Mohamed Heikalb2bac122019-07-17 17:47:17 -040019#include <set>
felkachang4bdd3ac2022-09-13 10:58:49 +080020#include <string>
Mohamed Heikalc7694032018-11-07 16:49:02 -050021#include <unordered_set>
22
Mohamed Heikalc7694032018-11-07 16:49:02 -050023#include "ResourceTable.h"
24#include "ValueVisitor.h"
felkachang4bdd3ac2022-09-13 10:58:49 +080025#include "androidfw/StringPiece.h"
Mohamed Heikal61d528f2019-07-08 15:06:39 -040026#include "util/Util.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050027
felkachang4bdd3ac2022-09-13 10:58:49 +080028static const char base64_chars[] =
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
30 "abcdefghijklmnopqrstuvwxyz"
31 "0123456789-_";
Mohamed Heikalc7694032018-11-07 16:49:02 -050032
33namespace aapt {
34
felkachang4bdd3ac2022-09-13 10:58:49 +080035Obfuscator::Obfuscator(std::map<std::string, std::string>& path_map_out) : path_map_(path_map_out) {
Mohamed Heikalc7694032018-11-07 16:49:02 -050036}
37
38std::string ShortenFileName(const android::StringPiece& file_path, int output_length) {
39 std::size_t hash_num = std::hash<android::StringPiece>{}(file_path);
40 std::string result = "";
41 // Convert to (modified) base64 so that it is a proper file path.
42 for (int i = 0; i < output_length; i++) {
43 uint8_t sextet = hash_num & 0x3f;
44 hash_num >>= 6;
45 result += base64_chars[sextet];
46 }
47 return result;
48}
49
Mohamed Heikal525714a2019-07-18 11:14:31 -040050// Return the optimal hash length such that at most 10% of resources collide in
51// their shortened path.
Mohamed Heikalc7694032018-11-07 16:49:02 -050052// Reference: http://matt.might.net/articles/counting-hash-collisions/
53int OptimalShortenedLength(int num_resources) {
Mohamed Heikal525714a2019-07-18 11:14:31 -040054 if (num_resources > 4000) {
55 return 3;
56 } else {
57 return 2;
Mohamed Heikalc7694032018-11-07 16:49:02 -050058 }
Mohamed Heikalc7694032018-11-07 16:49:02 -050059}
60
61std::string GetShortenedPath(const android::StringPiece& shortened_filename,
felkachang4bdd3ac2022-09-13 10:58:49 +080062 const android::StringPiece& extension, int collision_count) {
Mohamed Heikalc7694032018-11-07 16:49:02 -050063 std::string shortened_path = "res/" + shortened_filename.to_string();
64 if (collision_count > 0) {
65 shortened_path += std::to_string(collision_count);
66 }
67 shortened_path += extension;
68 return shortened_path;
69}
70
Mohamed Heikalb2bac122019-07-17 17:47:17 -040071// implement custom comparator of FileReference pointers so as to use the
72// underlying filepath as key rather than the integer address. This is to ensure
73// determinism of output for colliding files.
74struct PathComparator {
felkachang4bdd3ac2022-09-13 10:58:49 +080075 bool operator()(const FileReference* lhs, const FileReference* rhs) const {
76 return lhs->path->compare(*rhs->path);
77 }
Mohamed Heikalb2bac122019-07-17 17:47:17 -040078};
79
felkachang4bdd3ac2022-09-13 10:58:49 +080080bool Obfuscator::Consume(IAaptContext* context, ResourceTable* table) {
Mohamed Heikalc7694032018-11-07 16:49:02 -050081 // used to detect collisions
82 std::unordered_set<std::string> shortened_paths;
Mohamed Heikalb2bac122019-07-17 17:47:17 -040083 std::set<FileReference*, PathComparator> file_refs;
Mohamed Heikalc7694032018-11-07 16:49:02 -050084 for (auto& package : table->packages) {
85 for (auto& type : package->types) {
86 for (auto& entry : type->entries) {
87 for (auto& config_value : entry->values) {
88 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
89 if (file_ref) {
90 file_refs.insert(file_ref);
91 }
92 }
93 }
94 }
95 }
96 int num_chars = OptimalShortenedLength(file_refs.size());
97 for (auto& file_ref : file_refs) {
98 android::StringPiece res_subdir, actual_filename, extension;
99 util::ExtractResFilePathParts(*file_ref->path, &res_subdir, &actual_filename, &extension);
100
Mohamed Heikal61d528f2019-07-08 15:06:39 -0400101 // Android detects ColorStateLists via pathname, skip res/color*
felkachang4bdd3ac2022-09-13 10:58:49 +0800102 if (util::StartsWith(res_subdir, "res/color")) continue;
Mohamed Heikal7c757302019-04-25 17:39:43 -0400103
Mohamed Heikalc7694032018-11-07 16:49:02 -0500104 std::string shortened_filename = ShortenFileName(*file_ref->path, num_chars);
105 int collision_count = 0;
106 std::string shortened_path = GetShortenedPath(shortened_filename, extension, collision_count);
107 while (shortened_paths.find(shortened_path) != shortened_paths.end()) {
108 collision_count++;
109 shortened_path = GetShortenedPath(shortened_filename, extension, collision_count);
110 }
111 shortened_paths.insert(shortened_path);
112 path_map_.insert({*file_ref->path, shortened_path});
113 file_ref->path = table->string_pool.MakeRef(shortened_path, file_ref->path.GetContext());
114 }
115 return true;
116}
117
118} // namespace aapt