blob: aa578a2a6ff4cf120cbff61e2447fd08e80870c8 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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
Adam Lesinski46708052017-09-29 14:49:15 -070017#include "format/binary/TableFlattener.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
Adam Lesinski803c7c82016-04-06 16:09:43 -070019#include <algorithm>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070020#include <numeric>
Adam Lesinskid0f116b2016-07-08 15:00:32 -070021#include <sstream>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include <type_traits>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "android-base/logging.h"
25#include "android-base/macros.h"
Adam Lesinski490595a2017-11-07 17:08:07 -080026#include "android-base/stringprintf.h"
Ryan Mitchell75e20dd2018-11-06 16:39:36 -080027#include "androidfw/ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028
29#include "ResourceTable.h"
30#include "ResourceValues.h"
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080031#include "SdkConstants.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032#include "ValueVisitor.h"
Adam Lesinski46708052017-09-29 14:49:15 -070033#include "format/binary/ChunkWriter.h"
34#include "format/binary/ResourceTypeExtensions.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080035#include "trace/TraceBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036#include "util/BigBuffer.h"
37
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038using namespace android;
39
40namespace aapt {
41
42namespace {
43
44template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045static bool cmp_ids(const T* a, const T* b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return a->id.value() < b->id.value();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047}
48
49static void strcpy16_htod(uint16_t* dst, size_t len, const StringPiece16& src) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 if (len == 0) {
51 return;
52 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 size_t i;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 const char16_t* src_data = src.data();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 for (i = 0; i < len - 1 && i < src.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 dst[i] = util::HostToDevice16((uint16_t)src_data[i]);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 }
59 dst[i] = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060}
61
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062static bool cmp_style_entries(const Style::Entry& a, const Style::Entry& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 if (a.key.id) {
64 if (b.key.id) {
65 return a.key.id.value() < b.key.id.value();
66 }
67 return true;
68 } else if (!b.key.id) {
69 return a.key.name.value() < b.key.name.value();
70 }
71 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -080072}
73
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074struct FlatEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 ResourceEntry* entry;
76 Value* value;
Adam Lesinski3b4cd942015-10-30 16:31:42 -070077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 // The entry string pool index to the entry's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 uint32_t entry_key;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080};
81
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070082class MapFlattenVisitor : public ValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070084 using ValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 MapFlattenVisitor(ResTable_entry_ext* out_entry, BigBuffer* buffer)
Adam Lesinski46708052017-09-29 14:49:15 -070087 : out_entry_(out_entry), buffer_(buffer) {
88 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 void Visit(Attribute* attr) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 {
92 Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->type_mask);
94 FlattenEntry(&key, &val);
Adam Lesinski28cacf02015-11-23 14:22:47 -080095 }
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 if (attr->min_int != std::numeric_limits<int32_t>::min()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN));
Adam Lesinski46708052017-09-29 14:49:15 -070099 BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->min_int));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 FlattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101 }
102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 if (attr->max_int != std::numeric_limits<int32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX));
Adam Lesinski46708052017-09-29 14:49:15 -0700105 BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->max_int));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 FlattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107 }
108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 for (Attribute::Symbol& s : attr->symbols) {
110 BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 FlattenEntry(&s.symbol, &val);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 }
113 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 void Visit(Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 if (style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 const Reference& parent_ref = style->parent.value();
118 CHECK(bool(parent_ref.id)) << "parent has no ID";
119 out_entry_->parent.ident = util::HostToDevice32(parent_ref.id.value().id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120 }
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 // Sort the style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 std::sort(style->entries.begin(), style->entries.end(), cmp_style_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124
125 for (Style::Entry& entry : style->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 FlattenEntry(&entry.key, entry.value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 void Visit(Styleable* styleable) override {
131 for (auto& attr_ref : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 BinaryPrimitive val(Res_value{});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 FlattenEntry(&attr_ref, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 void Visit(Array* array) override {
Ryan Mitchell0f7da5e2018-08-16 16:10:00 -0700138 const size_t count = array->elements.size();
139 for (size_t i = 0; i < count; i++) {
140 Reference key(android::ResTable_map::ATTR_MIN + i);
141 FlattenEntry(&key, array->elements[i].get());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800142 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 void Visit(Plural* plural) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 const size_t count = plural->values.size();
147 for (size_t i = 0; i < count; i++) {
148 if (!plural->values[i]) {
149 continue;
150 }
151
152 ResourceId q;
153 switch (i) {
154 case Plural::Zero:
155 q.id = android::ResTable_map::ATTR_ZERO;
156 break;
157
158 case Plural::One:
159 q.id = android::ResTable_map::ATTR_ONE;
160 break;
161
162 case Plural::Two:
163 q.id = android::ResTable_map::ATTR_TWO;
164 break;
165
166 case Plural::Few:
167 q.id = android::ResTable_map::ATTR_FEW;
168 break;
169
170 case Plural::Many:
171 q.id = android::ResTable_map::ATTR_MANY;
172 break;
173
174 case Plural::Other:
175 q.id = android::ResTable_map::ATTR_OTHER;
176 break;
177
178 default:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 LOG(FATAL) << "unhandled plural type";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 break;
181 }
182
183 Reference key(q);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 FlattenEntry(&key, plural->values[i].get());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800185 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800187
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 /**
189 * Call this after visiting a Value. This will finish any work that
190 * needs to be done to prepare the entry.
191 */
Adam Lesinski46708052017-09-29 14:49:15 -0700192 void Finish() {
193 out_entry_->count = util::HostToDevice32(entry_count_);
194 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor);
198
199 void FlattenKey(Reference* key, ResTable_map* out_entry) {
200 CHECK(bool(key->id)) << "key has no ID";
201 out_entry->name.ident = util::HostToDevice32(key->id.value().id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 void FlattenValue(Item* value, ResTable_map* out_entry) {
205 CHECK(value->Flatten(&out_entry->value)) << "flatten failed";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 }
207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 void FlattenEntry(Reference* key, Item* value) {
209 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
210 FlattenKey(key, out_entry);
211 FlattenValue(value, out_entry);
212 out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
213 entry_count_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 }
215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 ResTable_entry_ext* out_entry_;
217 BigBuffer* buffer_;
218 size_t entry_count_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219};
220
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800221struct OverlayableChunk {
222 std::string actor;
223 Source source;
224 std::map<OverlayableItem::PolicyFlags, std::set<ResourceId>> policy_ids;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800225};
226
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700227class PackageFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 public:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800229 PackageFlattener(IAaptContext* context, ResourceTablePackage* package,
Luke Nicholsonb0643302017-12-01 15:29:03 -0800230 const std::map<size_t, std::string>* shared_libs, bool use_sparse_entries,
231 bool collapse_key_stringpool, const std::set<std::string>& whitelisted_resources)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800232 : context_(context),
233 diag_(context->GetDiagnostics()),
234 package_(package),
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800235 shared_libs_(shared_libs),
Luke Nicholsonb0643302017-12-01 15:29:03 -0800236 use_sparse_entries_(use_sparse_entries),
237 collapse_key_stringpool_(collapse_key_stringpool),
238 whitelisted_resources_(whitelisted_resources) {
Adam Lesinski46708052017-09-29 14:49:15 -0700239 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 bool FlattenPackage(BigBuffer* buffer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800242 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 ChunkWriter pkg_writer(buffer);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800244 ResTable_package* pkg_header = pkg_writer.StartChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 pkg_header->id = util::HostToDevice32(package_->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246
Adam Lesinskib522f042017-04-21 16:57:59 -0700247 // AAPT truncated the package name, so do the same.
248 // Shared libraries require full package names, so don't truncate theirs.
249 if (context_->GetPackageType() != PackageType::kApp &&
250 package_->name.size() >= arraysize(pkg_header->name)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 diag_->Error(DiagMessage() << "package name '" << package_->name
Adam Lesinskib522f042017-04-21 16:57:59 -0700252 << "' is too long. "
253 "Shared libraries cannot have truncated package names");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700255 }
256
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 // Copy the package name in device endianness.
Adam Lesinskib522f042017-04-21 16:57:59 -0700258 strcpy16_htod(pkg_header->name, arraysize(pkg_header->name), util::Utf8ToUtf16(package_->name));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700259
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 // Serialize the types. We do this now so that our type and key strings
261 // are populated. We write those first.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 BigBuffer type_buffer(1024);
263 FlattenTypes(&type_buffer);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size());
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700266 StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_, diag_);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size());
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700269 StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_, diag_);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 // Append the types.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 buffer->AppendBuffer(std::move(type_buffer));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700273
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800274 // If there are libraries (or if the package ID is 0x00), encode a library chunk.
275 if (package_->id.value() == 0x00 || !shared_libs_->empty()) {
276 FlattenLibrarySpec(buffer);
277 }
278
Winsonb2d7f532019-02-04 16:32:43 -0800279 if (!FlattenOverlayable(buffer)) {
280 return false;
281 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 pkg_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 return true;
285 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700286
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 DISALLOW_COPY_AND_ASSIGN(PackageFlattener);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700289
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 template <typename T, bool IsItem>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 T* WriteEntry(FlatEntry* entry, BigBuffer* buffer) {
Adam Lesinski46708052017-09-29 14:49:15 -0700292 static_assert(
293 std::is_same<ResTable_entry, T>::value || std::is_same<ResTable_entry_ext, T>::value,
294 "T must be ResTable_entry or ResTable_entry_ext");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 T* result = buffer->NextBlock<T>();
297 ResTable_entry* out_entry = (ResTable_entry*)result;
Adam Lesinski71be7052017-12-12 16:48:07 -0800298 if (entry->entry->visibility.level == Visibility::Level::kPublic) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 out_entry->flags |= ResTable_entry::FLAG_PUBLIC;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700300 }
301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 if (entry->value->IsWeak()) {
303 out_entry->flags |= ResTable_entry::FLAG_WEAK;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304 }
305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 if (!IsItem) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 out_entry->flags |= ResTable_entry::FLAG_COMPLEX;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700308 }
309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 out_entry->flags = util::HostToDevice16(out_entry->flags);
311 out_entry->key.index = util::HostToDevice32(entry->entry_key);
312 out_entry->size = util::HostToDevice16(sizeof(T));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 return result;
314 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700315
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 bool FlattenValue(FlatEntry* entry, BigBuffer* buffer) {
317 if (Item* item = ValueCast<Item>(entry->value)) {
318 WriteEntry<ResTable_entry, true>(entry, buffer);
319 Res_value* outValue = buffer->NextBlock<Res_value>();
320 CHECK(item->Flatten(outValue)) << "flatten failed";
321 outValue->size = util::HostToDevice16(sizeof(*outValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 } else {
Adam Lesinski46708052017-09-29 14:49:15 -0700323 ResTable_entry_ext* out_entry = WriteEntry<ResTable_entry_ext, false>(entry, buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 MapFlattenVisitor visitor(out_entry, buffer);
325 entry->value->Accept(&visitor);
326 visitor.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 }
328 return true;
329 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700330
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800331 bool FlattenConfig(const ResourceTableType* type, const ConfigDescription& config,
332 const size_t num_total_entries, std::vector<FlatEntry>* entries,
333 BigBuffer* buffer) {
334 CHECK(num_total_entries != 0);
335 CHECK(num_total_entries <= std::numeric_limits<uint16_t>::max());
336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 ChunkWriter type_writer(buffer);
Adam Lesinski46708052017-09-29 14:49:15 -0700338 ResTable_type* type_header = type_writer.StartChunk<ResTable_type>(RES_TABLE_TYPE_TYPE);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 type_header->id = type->id.value();
340 type_header->config = config;
341 type_header->config.swapHtoD();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800343 std::vector<uint32_t> offsets;
344 offsets.resize(num_total_entries, 0xffffffffu);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800346 BigBuffer values_buffer(512);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 for (FlatEntry& flat_entry : *entries) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800348 CHECK(static_cast<size_t>(flat_entry.entry->id.value()) < num_total_entries);
349 offsets[flat_entry.entry->id.value()] = values_buffer.size();
350 if (!FlattenValue(&flat_entry, &values_buffer)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 diag_->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 << "failed to flatten resource '"
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800353 << ResourceNameRef(package_->name, type->type, flat_entry.entry->name)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 << "' for configuration '" << config << "'");
355 return false;
356 }
357 }
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800358
359 bool sparse_encode = use_sparse_entries_;
360
361 // Only sparse encode if the entries will be read on platforms O+.
362 sparse_encode =
363 sparse_encode && (context_->GetMinSdkVersion() >= SDK_O || config.sdkVersion >= SDK_O);
364
365 // Only sparse encode if the offsets are representable in 2 bytes.
366 sparse_encode =
367 sparse_encode && (values_buffer.size() / 4u) <= std::numeric_limits<uint16_t>::max();
368
369 // Only sparse encode if the ratio of populated entries to total entries is below some
370 // threshold.
371 sparse_encode =
372 sparse_encode && ((100 * entries->size()) / num_total_entries) < kSparseEncodingThreshold;
373
374 if (sparse_encode) {
375 type_header->entryCount = util::HostToDevice32(entries->size());
376 type_header->flags |= ResTable_type::FLAG_SPARSE;
377 ResTable_sparseTypeEntry* indices =
378 type_writer.NextBlock<ResTable_sparseTypeEntry>(entries->size());
379 for (size_t i = 0; i < num_total_entries; i++) {
380 if (offsets[i] != ResTable_type::NO_ENTRY) {
381 CHECK((offsets[i] & 0x03) == 0);
382 indices->idx = util::HostToDevice16(i);
383 indices->offset = util::HostToDevice16(offsets[i] / 4u);
384 indices++;
385 }
386 }
387 } else {
388 type_header->entryCount = util::HostToDevice32(num_total_entries);
389 uint32_t* indices = type_writer.NextBlock<uint32_t>(num_total_entries);
390 for (size_t i = 0; i < num_total_entries; i++) {
391 indices[i] = util::HostToDevice32(offsets[i]);
392 }
393 }
394
395 type_header->entriesStart = util::HostToDevice32(type_writer.size());
396 type_writer.buffer()->AppendBuffer(std::move(values_buffer));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 type_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 return true;
399 }
400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 std::vector<ResourceTableType*> CollectAndSortTypes() {
402 std::vector<ResourceTableType*> sorted_types;
403 for (auto& type : package_->types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 if (type->type == ResourceType::kStyleable) {
405 // Styleables aren't real Resource Types, they are represented in the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 // R.java file.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 continue;
408 }
409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 CHECK(bool(type->id)) << "type must have an ID set";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 sorted_types.push_back(type.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 }
Adam Lesinski46708052017-09-29 14:49:15 -0700414 std::sort(sorted_types.begin(), sorted_types.end(), cmp_ids<ResourceTableType>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 return sorted_types;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 }
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 std::vector<ResourceEntry*> CollectAndSortEntries(ResourceTableType* type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 // Sort the entries by entry ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 std::vector<ResourceEntry*> sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 CHECK(bool(entry->id)) << "entry must have an ID set";
423 sorted_entries.push_back(entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 }
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800425 std::sort(sorted_entries.begin(), sorted_entries.end(), cmp_ids<ResourceEntry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 return sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 }
428
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800429 bool FlattenOverlayable(BigBuffer* buffer) {
430 std::set<ResourceId> seen_ids;
431 std::map<std::string, OverlayableChunk> overlayable_chunks;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800432
433 CHECK(bool(package_->id)) << "package must have an ID set when flattening <overlayable>";
434 for (auto& type : package_->types) {
435 CHECK(bool(type->id)) << "type must have an ID set when flattening <overlayable>";
436 for (auto& entry : type->entries) {
437 CHECK(bool(type->id)) << "entry must have an ID set when flattening <overlayable>";
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800438 if (!entry->overlayable_item) {
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800439 continue;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800440 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800441
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800442 OverlayableItem& item = entry->overlayable_item.value();
443
444 // Resource ids should only appear once in the resource table
445 ResourceId id = android::make_resid(package_->id.value(), type->id.value(),
446 entry->id.value());
447 CHECK(seen_ids.find(id) == seen_ids.end())
448 << "multiple overlayable definitions found for resource "
449 << ResourceName(package_->name, type->type, entry->name).to_string();
450 seen_ids.insert(id);
451
452 // Find the overlayable chunk with the specified name
453 OverlayableChunk* overlayable_chunk = nullptr;
454 auto iter = overlayable_chunks.find(item.overlayable->name);
455 if (iter == overlayable_chunks.end()) {
456 OverlayableChunk chunk{item.overlayable->actor, item.overlayable->source};
457 overlayable_chunk =
458 &overlayable_chunks.insert({item.overlayable->name, chunk}).first->second;
459 } else {
460 OverlayableChunk& chunk = iter->second;
461 if (!(chunk.source == item.overlayable->source)) {
462 // The name of an overlayable set of resources must be unique
463 context_->GetDiagnostics()->Error(DiagMessage(item.overlayable->source)
464 << "duplicate overlayable name"
465 << item.overlayable->name << "'");
466 context_->GetDiagnostics()->Error(DiagMessage(chunk.source)
467 << "previous declaration here");
468 return false;
469 }
470
471 CHECK(chunk.actor == item.overlayable->actor);
472 overlayable_chunk = &chunk;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800473 }
474
Winsonb2d7f532019-02-04 16:32:43 -0800475 if (item.policies == 0) {
476 context_->GetDiagnostics()->Error(DiagMessage(item.overlayable->source)
477 << "overlayable "
478 << entry->name
479 << " does not specify policy");
480 return false;
481 }
482
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800483 uint32_t policy_flags = 0;
Winsonb2d7f532019-02-04 16:32:43 -0800484 if (item.policies & OverlayableItem::Policy::kPublic) {
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800485 policy_flags |= ResTable_overlayable_policy_header::POLICY_PUBLIC;
Winsonb2d7f532019-02-04 16:32:43 -0800486 }
487 if (item.policies & OverlayableItem::Policy::kSystem) {
488 policy_flags |= ResTable_overlayable_policy_header::POLICY_SYSTEM_PARTITION;
489 }
490 if (item.policies & OverlayableItem::Policy::kVendor) {
491 policy_flags |= ResTable_overlayable_policy_header::POLICY_VENDOR_PARTITION;
492 }
493 if (item.policies & OverlayableItem::Policy::kProduct) {
494 policy_flags |= ResTable_overlayable_policy_header::POLICY_PRODUCT_PARTITION;
495 }
496 if (item.policies & OverlayableItem::Policy::kSignature) {
497 policy_flags |= ResTable_overlayable_policy_header::POLICY_SIGNATURE;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800498 }
499
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800500 auto policy = overlayable_chunk->policy_ids.find(policy_flags);
501 if (policy != overlayable_chunk->policy_ids.end()) {
502 policy->second.insert(id);
503 } else {
504 overlayable_chunk->policy_ids.insert(
505 std::make_pair(policy_flags, std::set<ResourceId>{id}));
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800506 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800507 }
508 }
509
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800510 for (auto& overlayable_pair : overlayable_chunks) {
511 std::string name = overlayable_pair.first;
512 OverlayableChunk& overlayable = overlayable_pair.second;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800513
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800514 // Write the header of the overlayable chunk
515 ChunkWriter overlayable_writer(buffer);
516 auto* overlayable_type =
517 overlayable_writer.StartChunk<ResTable_overlayable_header>(RES_TABLE_OVERLAYABLE_TYPE);
518 if (name.size() >= arraysize(overlayable_type->name)) {
519 diag_->Error(DiagMessage() << "overlayable name '" << name
520 << "' exceeds maximum length ("
521 << arraysize(overlayable_type->name)
522 << " utf16 characters)");
523 return false;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800524 }
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800525 strcpy16_htod(overlayable_type->name, arraysize(overlayable_type->name),
526 util::Utf8ToUtf16(name));
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800527
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800528 if (overlayable.actor.size() >= arraysize(overlayable_type->actor)) {
529 diag_->Error(DiagMessage() << "overlayable name '" << overlayable.actor
530 << "' exceeds maximum length ("
531 << arraysize(overlayable_type->actor)
532 << " utf16 characters)");
533 return false;
534 }
535 strcpy16_htod(overlayable_type->actor, arraysize(overlayable_type->actor),
536 util::Utf8ToUtf16(overlayable.actor));
537
538 // Write each policy block for the overlayable
539 for (auto& policy_ids : overlayable.policy_ids) {
540 ChunkWriter policy_writer(buffer);
541 auto* policy_type = policy_writer.StartChunk<ResTable_overlayable_policy_header>(
542 RES_TABLE_OVERLAYABLE_POLICY_TYPE);
543 policy_type->policy_flags = util::HostToDevice32(static_cast<uint32_t>(policy_ids.first));
544 policy_type->entry_count = util::HostToDevice32(static_cast<uint32_t>(
545 policy_ids.second.size()));
546 // Write the ids after the policy header
547 auto* id_block = policy_writer.NextBlock<ResTable_ref>(policy_ids.second.size());
548 for (const ResourceId& id : policy_ids.second) {
549 id_block->ident = util::HostToDevice32(id.id);
550 id_block++;
551 }
552 policy_writer.Finish();
553 }
554 overlayable_writer.Finish();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800555 }
556
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800557 return true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800558 }
559
Adam Lesinski46708052017-09-29 14:49:15 -0700560 bool FlattenTypeSpec(ResourceTableType* type, std::vector<ResourceEntry*>* sorted_entries,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 BigBuffer* buffer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 ChunkWriter type_spec_writer(buffer);
563 ResTable_typeSpec* spec_header =
Adam Lesinski46708052017-09-29 14:49:15 -0700564 type_spec_writer.StartChunk<ResTable_typeSpec>(RES_TABLE_TYPE_SPEC_TYPE);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 spec_header->id = type->id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700567 if (sorted_entries->empty()) {
568 type_spec_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700570 }
571
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 // We can't just take the size of the vector. There may be holes in the
573 // entry ID space.
574 // Since the entries are sorted by ID, the last one will be the biggest.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 const size_t num_entries = sorted_entries->back()->id.value() + 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 spec_header->entryCount = util::HostToDevice32(num_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578
579 // Reserve space for the masks of each resource in this type. These
580 // show for which configuration axis the resource changes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 uint32_t* config_masks = type_spec_writer.NextBlock<uint32_t>(num_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 const size_t actual_num_entries = sorted_entries->size();
584 for (size_t entryIndex = 0; entryIndex < actual_num_entries; entryIndex++) {
585 ResourceEntry* entry = sorted_entries->at(entryIndex);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586
587 // Populate the config masks for this entry.
588
Adam Lesinski71be7052017-12-12 16:48:07 -0800589 if (entry->visibility.level == Visibility::Level::kPublic) {
Adam Lesinski46708052017-09-29 14:49:15 -0700590 config_masks[entry->id.value()] |= util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 }
592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 const size_t config_count = entry->values.size();
594 for (size_t i = 0; i < config_count; i++) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 const ConfigDescription& config = entry->values[i]->config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 for (size_t j = i + 1; j < config_count; j++) {
597 config_masks[entry->id.value()] |=
598 util::HostToDevice32(config.diff(entry->values[j]->config));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700601 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 type_spec_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 return true;
604 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700605
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 bool FlattenTypes(BigBuffer* buffer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 // Sort the types by their IDs. They will be inserted into the StringPool in
608 // this order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 std::vector<ResourceTableType*> sorted_types = CollectAndSortTypes();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700610
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 size_t expected_type_id = 1;
612 for (ResourceTableType* type : sorted_types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 // If there is a gap in the type IDs, fill in the StringPool
614 // with empty values until we reach the ID we expect.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 while (type->id.value() > expected_type_id) {
616 std::stringstream type_name;
617 type_name << "?" << expected_type_id;
618 type_pool_.MakeRef(type_name.str());
619 expected_type_id++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 expected_type_id++;
Adam Lesinski93190b72017-11-03 15:20:17 -0700622 type_pool_.MakeRef(to_string(type->type));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 std::vector<ResourceEntry*> sorted_entries = CollectAndSortEntries(type);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800625 if (sorted_entries.empty()) {
626 continue;
627 }
628
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629 if (!FlattenTypeSpec(type, &sorted_entries, buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 return false;
631 }
632
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800633 // Since the entries are sorted by ID, the last ID will be the largest.
634 const size_t num_entries = sorted_entries.back()->id.value() + 1;
635
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 // The binary resource table lists resource entries for each
637 // configuration.
638 // We store them inverted, where a resource entry lists the values for
639 // each
640 // configuration available. Here we reverse this to match the binary
641 // table.
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800642 std::map<ConfigDescription, std::vector<FlatEntry>> config_to_entry_list_map;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643
Luke Nicholsonb0643302017-12-01 15:29:03 -0800644 // hardcoded string uses characters which make it an invalid resource name
645 const std::string obfuscated_resource_name = "0_resource_name_obfuscated";
646
647 for (ResourceEntry* entry : sorted_entries) {
648 uint32_t local_key_index;
649 if (!collapse_key_stringpool_ ||
650 whitelisted_resources_.find(entry->name) != whitelisted_resources_.end()) {
651 local_key_index = (uint32_t)key_pool_.MakeRef(entry->name).index();
652 } else {
653 // resource isn't whitelisted, add it as obfuscated value
654 local_key_index = (uint32_t)key_pool_.MakeRef(obfuscated_resource_name).index();
655 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 // Group values by configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 for (auto& config_value : entry->values) {
658 config_to_entry_list_map[config_value->config].push_back(
Luke Nicholsonb0643302017-12-01 15:29:03 -0800659 FlatEntry{entry, config_value->value.get(), local_key_index});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700660 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700662
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 // Flatten a configuration value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 for (auto& entry : config_to_entry_list_map) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800665 if (!FlattenConfig(type, entry.first, num_entries, &entry.second, buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700667 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700669 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 return true;
671 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800673 void FlattenLibrarySpec(BigBuffer* buffer) {
674 ChunkWriter lib_writer(buffer);
675 ResTable_lib_header* lib_header =
676 lib_writer.StartChunk<ResTable_lib_header>(RES_TABLE_LIBRARY_TYPE);
677
678 const size_t num_entries = (package_->id.value() == 0x00 ? 1 : 0) + shared_libs_->size();
679 CHECK(num_entries > 0);
680
681 lib_header->count = util::HostToDevice32(num_entries);
682
683 ResTable_lib_entry* lib_entry = buffer->NextBlock<ResTable_lib_entry>(num_entries);
684 if (package_->id.value() == 0x00) {
685 // Add this package
686 lib_entry->packageId = util::HostToDevice32(0x00);
687 strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
688 util::Utf8ToUtf16(package_->name));
689 ++lib_entry;
690 }
691
692 for (auto& map_entry : *shared_libs_) {
693 lib_entry->packageId = util::HostToDevice32(map_entry.first);
694 strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
695 util::Utf8ToUtf16(map_entry.second));
696 ++lib_entry;
697 }
698 lib_writer.Finish();
699 }
700
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800701 IAaptContext* context_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 IDiagnostics* diag_;
703 ResourceTablePackage* package_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800704 const std::map<size_t, std::string>* shared_libs_;
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800705 bool use_sparse_entries_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 StringPool type_pool_;
707 StringPool key_pool_;
Luke Nicholsonb0643302017-12-01 15:29:03 -0800708 bool collapse_key_stringpool_;
709 const std::set<std::string>& whitelisted_resources_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700710};
711
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700713
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800715 TRACE_CALL();
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000716 // We must do this before writing the resources, since the string pool IDs may change.
717 table->string_pool.Prune();
718 table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
719 int diff = util::compare(a.priority, b.priority);
720 if (diff == 0) {
721 diff = a.config.compare(b.config);
722 }
723 return diff;
724 });
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700725
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 // Write the ResTable header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 ChunkWriter table_writer(buffer_);
Adam Lesinski4ca56972017-04-26 21:49:53 -0700728 ResTable_header* table_header = table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 table_header->packageCount = util::HostToDevice32(table->packages.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700730
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 // Flatten the values string pool.
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700732 StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool,
733 context->GetDiagnostics());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700734
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 BigBuffer package_buffer(1024);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700736
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 // Flatten each package.
738 for (auto& package : table->packages) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700739 if (context->GetPackageType() == PackageType::kApp) {
740 // Write a self mapping entry for this package if the ID is non-standard (0x7f).
741 const uint8_t package_id = package->id.value();
742 if (package_id != kFrameworkPackageId && package_id != kAppPackageId) {
Adam Lesinski490595a2017-11-07 17:08:07 -0800743 auto result = table->included_packages_.insert({package_id, package->name});
744 if (!result.second && result.first->second != package->name) {
745 // A mapping for this package ID already exists, and is a different package. Error!
746 context->GetDiagnostics()->Error(
747 DiagMessage() << android::base::StringPrintf(
748 "can't map package ID %02x to '%s'. Already mapped to '%s'", package_id,
749 package->name.c_str(), result.first->second.c_str()));
750 return false;
751 }
Adam Lesinski8780eb62017-10-31 17:44:39 -0700752 }
753 }
754
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800755 PackageFlattener flattener(context, package.get(), &table->included_packages_,
Luke Nicholsonb0643302017-12-01 15:29:03 -0800756 options_.use_sparse_entries, options_.collapse_key_stringpool,
757 options_.whitelisted_resources);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 if (!flattener.FlattenPackage(&package_buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700760 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700762
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700763 // Finally merge all the packages into the main buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700764 table_writer.buffer()->AppendBuffer(std::move(package_buffer));
765 table_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700767}
768
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769} // namespace aapt