blob: 6b11de759d2d5df490b8c8f43bd3cb7a0daf2975 [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
17#include "optimize/ResourcePathShortener.h"
18
19#include <math.h>
20#include <unordered_set>
21
22#include "androidfw/StringPiece.h"
23
24#include "ResourceTable.h"
25#include "ValueVisitor.h"
Mohamed Heikal61d528f2019-07-08 15:06:39 -040026#include "util/Util.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050027
28
29static const std::string base64_chars =
30 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
31 "abcdefghijklmnopqrstuvwxyz"
32 "0123456789-_";
33
34namespace aapt {
35
36ResourcePathShortener::ResourcePathShortener(
37 std::map<std::string, std::string>& path_map_out)
38 : path_map_(path_map_out) {
39}
40
41std::string ShortenFileName(const android::StringPiece& file_path, int output_length) {
42 std::size_t hash_num = std::hash<android::StringPiece>{}(file_path);
43 std::string result = "";
44 // Convert to (modified) base64 so that it is a proper file path.
45 for (int i = 0; i < output_length; i++) {
46 uint8_t sextet = hash_num & 0x3f;
47 hash_num >>= 6;
48 result += base64_chars[sextet];
49 }
50 return result;
51}
52
53
54// Calculate the optimal hash length such that an average of 10% of resources
55// collide in their shortened path.
56// Reference: http://matt.might.net/articles/counting-hash-collisions/
57int OptimalShortenedLength(int num_resources) {
58 int num_chars = 2;
59 double N = 64*64; // hash space when hash is 2 chars long
60 double max_collisions = num_resources * 0.1;
61 while (num_resources - N + N * pow((N - 1) / N, num_resources) > max_collisions) {
62 N *= 64;
63 num_chars++;
64 }
65 return num_chars;
66}
67
68std::string GetShortenedPath(const android::StringPiece& shortened_filename,
69 const android::StringPiece& extension, int collision_count) {
70 std::string shortened_path = "res/" + shortened_filename.to_string();
71 if (collision_count > 0) {
72 shortened_path += std::to_string(collision_count);
73 }
74 shortened_path += extension;
75 return shortened_path;
76}
77
78bool ResourcePathShortener::Consume(IAaptContext* context, ResourceTable* table) {
79 // used to detect collisions
80 std::unordered_set<std::string> shortened_paths;
81 std::unordered_set<FileReference*> file_refs;
82 for (auto& package : table->packages) {
83 for (auto& type : package->types) {
84 for (auto& entry : type->entries) {
85 for (auto& config_value : entry->values) {
86 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
87 if (file_ref) {
88 file_refs.insert(file_ref);
89 }
90 }
91 }
92 }
93 }
94 int num_chars = OptimalShortenedLength(file_refs.size());
95 for (auto& file_ref : file_refs) {
96 android::StringPiece res_subdir, actual_filename, extension;
97 util::ExtractResFilePathParts(*file_ref->path, &res_subdir, &actual_filename, &extension);
98
Mohamed Heikal61d528f2019-07-08 15:06:39 -040099 // Android detects ColorStateLists via pathname, skip res/color*
100 if (util::StartsWith(res_subdir, "res/color"))
Mohamed Heikal7c757302019-04-25 17:39:43 -0400101 continue;
102
Mohamed Heikalc7694032018-11-07 16:49:02 -0500103 std::string shortened_filename = ShortenFileName(*file_ref->path, num_chars);
104 int collision_count = 0;
105 std::string shortened_path = GetShortenedPath(shortened_filename, extension, collision_count);
106 while (shortened_paths.find(shortened_path) != shortened_paths.end()) {
107 collision_count++;
108 shortened_path = GetShortenedPath(shortened_filename, extension, collision_count);
109 }
110 shortened_paths.insert(shortened_path);
111 path_map_.insert({*file_ref->path, shortened_path});
112 file_ref->path = table->string_pool.MakeRef(shortened_path, file_ref->path.GetContext());
113 }
114 return true;
115}
116
117} // namespace aapt