1 /*
2  * Copyright (C) 2015 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 #ifndef TOOLS_AAPT2_FORMAT_BINARY_TABLEFLATTENER_H_
18 #define TOOLS_AAPT2_FORMAT_BINARY_TABLEFLATTENER_H_
19 
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <unordered_map>
24 
25 #include "Resource.h"
26 #include "ResourceTable.h"
27 #include "android-base/macros.h"
28 #include "androidfw/BigBuffer.h"
29 #include "process/IResourceTableConsumer.h"
30 
31 namespace aapt {
32 
33 // The percentage of used entries for a type for which using a sparse encoding is
34 // preferred.
35 constexpr const size_t kSparseEncodingThreshold = 60;
36 
37 enum class SparseEntriesMode {
38   // Disables sparse encoding for entries.
39   Disabled,
40   // Enables sparse encoding for all entries for APKs with O+ minSdk. For APKs with minSdk less
41   // than O only applies sparse encoding for resource configuration available on O+.
42   Enabled,
43   // Enables sparse encoding for all entries regardless of minSdk.
44   Forced,
45 };
46 
47 struct TableFlattenerOptions {
48   // When enabled, types for configurations with a sparse set of entries are encoded
49   // as a sparse map of entry ID and offset to actual data.
50   SparseEntriesMode sparse_entries = SparseEntriesMode::Disabled;
51 
52   // When true, use compact entries for simple data
53   bool use_compact_entries = false;
54 
55   // When true, the key string pool in the final ResTable
56   // is collapsed to a single entry. All resource entries
57   // have name indices that point to this single value
58   bool collapse_key_stringpool = false;
59 
60   // Set of resources to avoid collapsing to a single entry in key stringpool.
61   std::set<ResourceName> name_collapse_exemptions;
62 
63   // Set of resources to avoid path shortening.
64   std::set<ResourceName> path_shorten_exemptions;
65 
66   // Map from original resource paths to shortened resource paths.
67   std::map<std::string, std::string> shortened_path_map;
68 
69   // When enabled, only unique pairs of entry and value are stored in type chunks.
70   //
71   // By default, all such pairs are unique because a reference to resource name in the string pool
72   // is a part of the pair. But when resource names are collapsed (using 'collapse_key_stringpool'
73   // flag or manually) the same data might be duplicated multiple times in the same type chunk.
74   //
75   // For example: an application has 3 boolean resources with collapsed names and 3 'true' values
76   // are defined for these resources in 'default' configuration. All pairs of entry and value for
77   // these resources will have the same binary representation and stored only once in type chunk
78   // instead of three times when this flag is disabled.
79   //
80   // This applies only to simple entries (entry->flags & ResTable_entry::FLAG_COMPLEX == 0).
81   bool deduplicate_entry_values = false;
82 
83   // Map from original resource ids to obfuscated names.
84   std::unordered_map<uint32_t, std::string> id_resource_map;
85 };
86 
87 class TableFlattener : public IResourceTableConsumer {
88  public:
TableFlattener(const TableFlattenerOptions & options,android::BigBuffer * buffer)89   explicit TableFlattener(const TableFlattenerOptions& options, android::BigBuffer* buffer)
90       : options_(options), buffer_(buffer) {
91   }
92 
93   bool Consume(IAaptContext* context, ResourceTable* table) override;
94 
95  private:
96   TableFlattenerOptions options_;
97   android::BigBuffer* buffer_;
98 
99   DISALLOW_COPY_AND_ASSIGN(TableFlattener);
100 };
101 
102 }  // namespace aapt
103 
104 #endif  // TOOLS_AAPT2_FORMAT_BINARY_TABLEFLATTENER_H_
105