blob: d4ea6c05c9b02c38c0f7fa62e256e21b7e1069ed [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
17#include "ResourceTable.h"
18#include "ResourceValues.h"
19#include "ValueVisitor.h"
20
21#include "flatten/ChunkWriter.h"
22#include "flatten/ResourceTypeExtensions.h"
23#include "flatten/TableFlattener.h"
24#include "util/BigBuffer.h"
25
Elliott Hughese7d2cc32015-12-08 08:57:14 -080026#include <android-base/macros.h>
Adam Lesinski803c7c82016-04-06 16:09:43 -070027#include <algorithm>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028#include <numeric>
Adam Lesinskid0f116b2016-07-08 15:00:32 -070029#include <sstream>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include <type_traits>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031
32using namespace android;
33
34namespace aapt {
35
36namespace {
37
38template <typename T>
39static bool cmpIds(const T* a, const T* b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 return a->id.value() < b->id.value();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041}
42
43static void strcpy16_htod(uint16_t* dst, size_t len, const StringPiece16& src) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 if (len == 0) {
45 return;
46 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 size_t i;
49 const char16_t* srcData = src.data();
50 for (i = 0; i < len - 1 && i < src.size(); i++) {
51 dst[i] = util::hostToDevice16((uint16_t)srcData[i]);
52 }
53 dst[i] = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054}
55
Adam Lesinski59e04c62016-02-04 15:59:23 -080056static bool cmpStyleEntries(const Style::Entry& a, const Style::Entry& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 if (a.key.id) {
58 if (b.key.id) {
59 return a.key.id.value() < b.key.id.value();
60 }
61 return true;
62 } else if (!b.key.id) {
63 return a.key.name.value() < b.key.name.value();
64 }
65 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -080066}
67
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068struct FlatEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 ResourceEntry* entry;
70 Value* value;
Adam Lesinski3b4cd942015-10-30 16:31:42 -070071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 // The entry string pool index to the entry's name.
73 uint32_t entryKey;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074};
75
Adam Lesinski59e04c62016-02-04 15:59:23 -080076class MapFlattenVisitor : public RawValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 public:
78 using RawValueVisitor::visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 MapFlattenVisitor(ResTable_entry_ext* outEntry, BigBuffer* buffer)
81 : mOutEntry(outEntry), mBuffer(buffer) {}
82
83 void visit(Attribute* attr) override {
84 {
85 Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE));
86 BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->typeMask);
87 flattenEntry(&key, &val);
Adam Lesinski28cacf02015-11-23 14:22:47 -080088 }
89
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 if (attr->minInt != std::numeric_limits<int32_t>::min()) {
91 Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN));
92 BinaryPrimitive val(Res_value::TYPE_INT_DEC,
93 static_cast<uint32_t>(attr->minInt));
94 flattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095 }
96
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 if (attr->maxInt != std::numeric_limits<int32_t>::max()) {
98 Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX));
99 BinaryPrimitive val(Res_value::TYPE_INT_DEC,
100 static_cast<uint32_t>(attr->maxInt));
101 flattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102 }
103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 for (Attribute::Symbol& s : attr->symbols) {
105 BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value);
106 flattenEntry(&s.symbol, &val);
107 }
108 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 void visit(Style* style) override {
111 if (style->parent) {
112 const Reference& parentRef = style->parent.value();
113 assert(parentRef.id && "parent has no ID");
114 mOutEntry->parent.ident = util::hostToDevice32(parentRef.id.value().id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115 }
116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 // Sort the style.
118 std::sort(style->entries.begin(), style->entries.end(), cmpStyleEntries);
119
120 for (Style::Entry& entry : style->entries) {
121 flattenEntry(&entry.key, entry.value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 void visit(Styleable* styleable) override {
126 for (auto& attrRef : styleable->entries) {
127 BinaryPrimitive val(Res_value{});
128 flattenEntry(&attrRef, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800131
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 void visit(Array* array) override {
133 for (auto& item : array->items) {
134 ResTable_map* outEntry = mBuffer->nextBlock<ResTable_map>();
135 flattenValue(item.get(), outEntry);
136 outEntry->value.size = util::hostToDevice16(sizeof(outEntry->value));
137 mEntryCount++;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800138 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 void visit(Plural* plural) override {
142 const size_t count = plural->values.size();
143 for (size_t i = 0; i < count; i++) {
144 if (!plural->values[i]) {
145 continue;
146 }
147
148 ResourceId q;
149 switch (i) {
150 case Plural::Zero:
151 q.id = android::ResTable_map::ATTR_ZERO;
152 break;
153
154 case Plural::One:
155 q.id = android::ResTable_map::ATTR_ONE;
156 break;
157
158 case Plural::Two:
159 q.id = android::ResTable_map::ATTR_TWO;
160 break;
161
162 case Plural::Few:
163 q.id = android::ResTable_map::ATTR_FEW;
164 break;
165
166 case Plural::Many:
167 q.id = android::ResTable_map::ATTR_MANY;
168 break;
169
170 case Plural::Other:
171 q.id = android::ResTable_map::ATTR_OTHER;
172 break;
173
174 default:
175 assert(false);
176 break;
177 }
178
179 Reference key(q);
180 flattenEntry(&key, plural->values[i].get());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800181 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 /**
185 * Call this after visiting a Value. This will finish any work that
186 * needs to be done to prepare the entry.
187 */
188 void finish() { mOutEntry->count = util::hostToDevice32(mEntryCount); }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800189
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 private:
191 void flattenKey(Reference* key, ResTable_map* outEntry) {
192 assert(key->id && "key has no ID");
193 outEntry->name.ident = util::hostToDevice32(key->id.value().id);
194 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 void flattenValue(Item* value, ResTable_map* outEntry) {
197 bool result = value->flatten(&outEntry->value);
198 assert(result && "flatten failed");
199 }
200
201 void flattenEntry(Reference* key, Item* value) {
202 ResTable_map* outEntry = mBuffer->nextBlock<ResTable_map>();
203 flattenKey(key, outEntry);
204 flattenValue(value, outEntry);
205 outEntry->value.size = util::hostToDevice16(sizeof(outEntry->value));
206 mEntryCount++;
207 }
208
209 ResTable_entry_ext* mOutEntry;
210 BigBuffer* mBuffer;
211 size_t mEntryCount = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212};
213
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700214class PackageFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 public:
216 PackageFlattener(IDiagnostics* diag, ResourceTablePackage* package)
217 : mDiag(diag), mPackage(package) {}
218
219 bool flattenPackage(BigBuffer* buffer) {
220 ChunkWriter pkgWriter(buffer);
221 ResTable_package* pkgHeader =
222 pkgWriter.startChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE);
223 pkgHeader->id = util::hostToDevice32(mPackage->id.value());
224
225 if (mPackage->name.size() >= arraysize(pkgHeader->name)) {
226 mDiag->error(DiagMessage() << "package name '" << mPackage->name
227 << "' is too long");
228 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700229 }
230
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 // Copy the package name in device endianness.
232 strcpy16_htod(pkgHeader->name, arraysize(pkgHeader->name),
233 util::utf8ToUtf16(mPackage->name));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 // Serialize the types. We do this now so that our type and key strings
236 // are populated. We write those first.
237 BigBuffer typeBuffer(1024);
238 flattenTypes(&typeBuffer);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 pkgHeader->typeStrings = util::hostToDevice32(pkgWriter.size());
241 StringPool::flattenUtf16(pkgWriter.getBuffer(), mTypePool);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700242
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 pkgHeader->keyStrings = util::hostToDevice32(pkgWriter.size());
244 StringPool::flattenUtf8(pkgWriter.getBuffer(), mKeyPool);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700245
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 // Append the types.
247 buffer->appendBuffer(std::move(typeBuffer));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700248
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 pkgWriter.finish();
250 return true;
251 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700252
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 private:
254 IDiagnostics* mDiag;
255 ResourceTablePackage* mPackage;
256 StringPool mTypePool;
257 StringPool mKeyPool;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 template <typename T, bool IsItem>
260 T* writeEntry(FlatEntry* entry, BigBuffer* buffer) {
261 static_assert(std::is_same<ResTable_entry, T>::value ||
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262 std::is_same<ResTable_entry_ext, T>::value,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 "T must be ResTable_entry or ResTable_entry_ext");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700264
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 T* result = buffer->nextBlock<T>();
266 ResTable_entry* outEntry = (ResTable_entry*)(result);
267 if (entry->entry->symbolStatus.state == SymbolState::kPublic) {
268 outEntry->flags |= ResTable_entry::FLAG_PUBLIC;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269 }
270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 if (entry->value->isWeak()) {
272 outEntry->flags |= ResTable_entry::FLAG_WEAK;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700273 }
274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 if (!IsItem) {
276 outEntry->flags |= ResTable_entry::FLAG_COMPLEX;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700277 }
278
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 outEntry->flags = util::hostToDevice16(outEntry->flags);
280 outEntry->key.index = util::hostToDevice32(entry->entryKey);
281 outEntry->size = util::hostToDevice16(sizeof(T));
282 return result;
283 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 bool flattenValue(FlatEntry* entry, BigBuffer* buffer) {
286 if (Item* item = valueCast<Item>(entry->value)) {
287 writeEntry<ResTable_entry, true>(entry, buffer);
288 Res_value* outValue = buffer->nextBlock<Res_value>();
289 bool result = item->flatten(outValue);
290 assert(result && "flatten failed");
291 outValue->size = util::hostToDevice16(sizeof(*outValue));
292 } else {
293 ResTable_entry_ext* outEntry =
294 writeEntry<ResTable_entry_ext, false>(entry, buffer);
295 MapFlattenVisitor visitor(outEntry, buffer);
296 entry->value->accept(&visitor);
297 visitor.finish();
298 }
299 return true;
300 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 bool flattenConfig(const ResourceTableType* type,
303 const ConfigDescription& config,
304 std::vector<FlatEntry>* entries, BigBuffer* buffer) {
305 ChunkWriter typeWriter(buffer);
306 ResTable_type* typeHeader =
307 typeWriter.startChunk<ResTable_type>(RES_TABLE_TYPE_TYPE);
308 typeHeader->id = type->id.value();
309 typeHeader->config = config;
310 typeHeader->config.swapHtoD();
311
312 auto maxAccum = [](uint32_t max,
313 const std::unique_ptr<ResourceEntry>& a) -> uint32_t {
314 return std::max(max, (uint32_t)a->id.value());
315 };
316
317 // Find the largest entry ID. That is how many entries we will have.
318 const uint32_t entryCount =
319 std::accumulate(type->entries.begin(), type->entries.end(), 0,
320 maxAccum) +
321 1;
322
323 typeHeader->entryCount = util::hostToDevice32(entryCount);
324 uint32_t* indices = typeWriter.nextBlock<uint32_t>(entryCount);
325
326 assert((size_t)entryCount <= std::numeric_limits<uint16_t>::max() + 1);
327 memset(indices, 0xff, entryCount * sizeof(uint32_t));
328
329 typeHeader->entriesStart = util::hostToDevice32(typeWriter.size());
330
331 const size_t entryStart = typeWriter.getBuffer()->size();
332 for (FlatEntry& flatEntry : *entries) {
333 assert(flatEntry.entry->id.value() < entryCount);
334 indices[flatEntry.entry->id.value()] =
335 util::hostToDevice32(typeWriter.getBuffer()->size() - entryStart);
336 if (!flattenValue(&flatEntry, typeWriter.getBuffer())) {
337 mDiag->error(DiagMessage()
338 << "failed to flatten resource '"
339 << ResourceNameRef(mPackage->name, type->type,
340 flatEntry.entry->name)
341 << "' for configuration '" << config << "'");
342 return false;
343 }
344 }
345 typeWriter.finish();
346 return true;
347 }
348
349 std::vector<ResourceTableType*> collectAndSortTypes() {
350 std::vector<ResourceTableType*> sortedTypes;
351 for (auto& type : mPackage->types) {
352 if (type->type == ResourceType::kStyleable) {
353 // Styleables aren't real Resource Types, they are represented in the
354 // R.java
355 // file.
356 continue;
357 }
358
359 assert(type->id && "type must have an ID set");
360
361 sortedTypes.push_back(type.get());
362 }
363 std::sort(sortedTypes.begin(), sortedTypes.end(),
364 cmpIds<ResourceTableType>);
365 return sortedTypes;
366 }
367
368 std::vector<ResourceEntry*> collectAndSortEntries(ResourceTableType* type) {
369 // Sort the entries by entry ID.
370 std::vector<ResourceEntry*> sortedEntries;
371 for (auto& entry : type->entries) {
372 assert(entry->id && "entry must have an ID set");
373 sortedEntries.push_back(entry.get());
374 }
375 std::sort(sortedEntries.begin(), sortedEntries.end(),
376 cmpIds<ResourceEntry>);
377 return sortedEntries;
378 }
379
380 bool flattenTypeSpec(ResourceTableType* type,
381 std::vector<ResourceEntry*>* sortedEntries,
382 BigBuffer* buffer) {
383 ChunkWriter typeSpecWriter(buffer);
384 ResTable_typeSpec* specHeader =
385 typeSpecWriter.startChunk<ResTable_typeSpec>(RES_TABLE_TYPE_SPEC_TYPE);
386 specHeader->id = type->id.value();
387
388 if (sortedEntries->empty()) {
389 typeSpecWriter.finish();
390 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700391 }
392
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 // We can't just take the size of the vector. There may be holes in the
394 // entry ID space.
395 // Since the entries are sorted by ID, the last one will be the biggest.
396 const size_t numEntries = sortedEntries->back()->id.value() + 1;
397
398 specHeader->entryCount = util::hostToDevice32(numEntries);
399
400 // Reserve space for the masks of each resource in this type. These
401 // show for which configuration axis the resource changes.
402 uint32_t* configMasks = typeSpecWriter.nextBlock<uint32_t>(numEntries);
403
404 const size_t actualNumEntries = sortedEntries->size();
405 for (size_t entryIndex = 0; entryIndex < actualNumEntries; entryIndex++) {
406 ResourceEntry* entry = sortedEntries->at(entryIndex);
407
408 // Populate the config masks for this entry.
409
410 if (entry->symbolStatus.state == SymbolState::kPublic) {
411 configMasks[entry->id.value()] |=
412 util::hostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
413 }
414
415 const size_t configCount = entry->values.size();
416 for (size_t i = 0; i < configCount; i++) {
417 const ConfigDescription& config = entry->values[i]->config;
418 for (size_t j = i + 1; j < configCount; j++) {
419 configMasks[entry->id.value()] |=
420 util::hostToDevice32(config.diff(entry->values[j]->config));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700421 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700423 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 typeSpecWriter.finish();
425 return true;
426 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700427
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 bool flattenTypes(BigBuffer* buffer) {
429 // Sort the types by their IDs. They will be inserted into the StringPool in
430 // this order.
431 std::vector<ResourceTableType*> sortedTypes = collectAndSortTypes();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700432
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 size_t expectedTypeId = 1;
434 for (ResourceTableType* type : sortedTypes) {
435 // If there is a gap in the type IDs, fill in the StringPool
436 // with empty values until we reach the ID we expect.
437 while (type->id.value() > expectedTypeId) {
438 std::stringstream typeName;
439 typeName << "?" << expectedTypeId;
440 mTypePool.makeRef(typeName.str());
441 expectedTypeId++;
442 }
443 expectedTypeId++;
444 mTypePool.makeRef(toString(type->type));
445
446 std::vector<ResourceEntry*> sortedEntries = collectAndSortEntries(type);
447
448 if (!flattenTypeSpec(type, &sortedEntries, buffer)) {
449 return false;
450 }
451
452 // The binary resource table lists resource entries for each
453 // configuration.
454 // We store them inverted, where a resource entry lists the values for
455 // each
456 // configuration available. Here we reverse this to match the binary
457 // table.
458 std::map<ConfigDescription, std::vector<FlatEntry>> configToEntryListMap;
459 for (ResourceEntry* entry : sortedEntries) {
460 const uint32_t keyIndex =
461 (uint32_t)mKeyPool.makeRef(entry->name).getIndex();
462
463 // Group values by configuration.
464 for (auto& configValue : entry->values) {
465 configToEntryListMap[configValue->config].push_back(
466 FlatEntry{entry, configValue->value.get(), keyIndex});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700467 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700469
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 // Flatten a configuration value.
471 for (auto& entry : configToEntryListMap) {
472 if (!flattenConfig(type, entry.first, &entry.second, buffer)) {
473 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700474 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700476 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 return true;
478 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700479};
480
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700482
483bool TableFlattener::consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 // We must do this before writing the resources, since the string pool IDs may
485 // change.
486 table->stringPool.sort(
487 [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700488 int diff = a.context.priority - b.context.priority;
489 if (diff < 0) return true;
490 if (diff > 0) return false;
491 diff = a.context.config.compare(b.context.config);
492 if (diff < 0) return true;
493 if (diff > 0) return false;
494 return a.value < b.value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 });
496 table->stringPool.prune();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700497
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 // Write the ResTable header.
499 ChunkWriter tableWriter(mBuffer);
500 ResTable_header* tableHeader =
501 tableWriter.startChunk<ResTable_header>(RES_TABLE_TYPE);
502 tableHeader->packageCount = util::hostToDevice32(table->packages.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700503
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 // Flatten the values string pool.
505 StringPool::flattenUtf8(tableWriter.getBuffer(), table->stringPool);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700506
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 BigBuffer packageBuffer(1024);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700508
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 // Flatten each package.
510 for (auto& package : table->packages) {
511 PackageFlattener flattener(context->getDiagnostics(), package.get());
512 if (!flattener.flattenPackage(&packageBuffer)) {
513 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700514 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700516
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 // Finally merge all the packages into the main buffer.
518 tableWriter.getBuffer()->appendBuffer(std::move(packageBuffer));
519 tableWriter.finish();
520 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521}
522
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523} // namespace aapt