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 #include "format/binary/TableFlattener.h"
18
19 #include <algorithm>
20 #include <numeric>
21 #include <sstream>
22 #include <type_traits>
23
24 #include "android-base/logging.h"
25 #include "android-base/macros.h"
26 #include "android-base/stringprintf.h"
27 #include "androidfw/ResourceUtils.h"
28
29 #include "ResourceTable.h"
30 #include "ResourceValues.h"
31 #include "SdkConstants.h"
32 #include "ValueVisitor.h"
33 #include "format/binary/ChunkWriter.h"
34 #include "format/binary/ResourceTypeExtensions.h"
35 #include "trace/TraceBuffer.h"
36 #include "util/BigBuffer.h"
37
38 using namespace android;
39
40 namespace aapt {
41
42 namespace {
43
44 template <typename T>
cmp_ids(const T * a,const T * b)45 static bool cmp_ids(const T* a, const T* b) {
46 return a->id.value() < b->id.value();
47 }
48
strcpy16_htod(uint16_t * dst,size_t len,const StringPiece16 & src)49 static void strcpy16_htod(uint16_t* dst, size_t len, const StringPiece16& src) {
50 if (len == 0) {
51 return;
52 }
53
54 size_t i;
55 const char16_t* src_data = src.data();
56 for (i = 0; i < len - 1 && i < src.size(); i++) {
57 dst[i] = util::HostToDevice16((uint16_t)src_data[i]);
58 }
59 dst[i] = 0;
60 }
61
cmp_style_entries(const Style::Entry * a,const Style::Entry * b)62 static bool cmp_style_entries(const Style::Entry* a, const Style::Entry* b) {
63 if (a->key.id) {
64 if (b->key.id) {
65 return cmp_ids_dynamic_after_framework(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;
72 }
73
74 struct FlatEntry {
75 const ResourceTableEntryView* entry;
76 const Value* value;
77
78 // The entry string pool index to the entry's name.
79 uint32_t entry_key;
80 };
81
82 class MapFlattenVisitor : public ConstValueVisitor {
83 public:
84 using ConstValueVisitor::Visit;
85
MapFlattenVisitor(ResTable_entry_ext * out_entry,BigBuffer * buffer)86 MapFlattenVisitor(ResTable_entry_ext* out_entry, BigBuffer* buffer)
87 : out_entry_(out_entry), buffer_(buffer) {
88 }
89
Visit(const Attribute * attr)90 void Visit(const Attribute* attr) override {
91 {
92 Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE));
93 BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->type_mask);
94 FlattenEntry(&key, &val);
95 }
96
97 if (attr->min_int != std::numeric_limits<int32_t>::min()) {
98 Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN));
99 BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->min_int));
100 FlattenEntry(&key, &val);
101 }
102
103 if (attr->max_int != std::numeric_limits<int32_t>::max()) {
104 Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX));
105 BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->max_int));
106 FlattenEntry(&key, &val);
107 }
108
109 for (const Attribute::Symbol& s : attr->symbols) {
110 BinaryPrimitive val(s.type, s.value);
111 FlattenEntry(&s.symbol, &val);
112 }
113 }
114
Visit(const Style * style)115 void Visit(const Style* style) override {
116 if (style->parent) {
117 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);
120 }
121
122 // Sort the style.
123 std::vector<const Style::Entry*> sorted_entries;
124 for (const auto& entry : style->entries) {
125 sorted_entries.emplace_back(&entry);
126 }
127
128 std::sort(sorted_entries.begin(), sorted_entries.end(), cmp_style_entries);
129
130 for (const Style::Entry* entry : sorted_entries) {
131 FlattenEntry(&entry->key, entry->value.get());
132 }
133 }
134
Visit(const Styleable * styleable)135 void Visit(const Styleable* styleable) override {
136 for (auto& attr_ref : styleable->entries) {
137 BinaryPrimitive val(Res_value{});
138 FlattenEntry(&attr_ref, &val);
139 }
140 }
141
Visit(const Array * array)142 void Visit(const Array* array) override {
143 const size_t count = array->elements.size();
144 for (size_t i = 0; i < count; i++) {
145 Reference key(android::ResTable_map::ATTR_MIN + i);
146 FlattenEntry(&key, array->elements[i].get());
147 }
148 }
149
Visit(const Plural * plural)150 void Visit(const Plural* plural) override {
151 const size_t count = plural->values.size();
152 for (size_t i = 0; i < count; i++) {
153 if (!plural->values[i]) {
154 continue;
155 }
156
157 ResourceId q;
158 switch (i) {
159 case Plural::Zero:
160 q.id = android::ResTable_map::ATTR_ZERO;
161 break;
162
163 case Plural::One:
164 q.id = android::ResTable_map::ATTR_ONE;
165 break;
166
167 case Plural::Two:
168 q.id = android::ResTable_map::ATTR_TWO;
169 break;
170
171 case Plural::Few:
172 q.id = android::ResTable_map::ATTR_FEW;
173 break;
174
175 case Plural::Many:
176 q.id = android::ResTable_map::ATTR_MANY;
177 break;
178
179 case Plural::Other:
180 q.id = android::ResTable_map::ATTR_OTHER;
181 break;
182
183 default:
184 LOG(FATAL) << "unhandled plural type";
185 break;
186 }
187
188 Reference key(q);
189 FlattenEntry(&key, plural->values[i].get());
190 }
191 }
192
193 /**
194 * Call this after visiting a Value. This will finish any work that
195 * needs to be done to prepare the entry.
196 */
Finish()197 void Finish() {
198 out_entry_->count = util::HostToDevice32(entry_count_);
199 }
200
201 private:
202 DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor);
203
FlattenKey(const Reference * key,ResTable_map * out_entry)204 void FlattenKey(const Reference* key, ResTable_map* out_entry) {
205 CHECK(bool(key->id)) << "key has no ID";
206 out_entry->name.ident = util::HostToDevice32(key->id.value().id);
207 }
208
FlattenValue(const Item * value,ResTable_map * out_entry)209 void FlattenValue(const Item* value, ResTable_map* out_entry) {
210 CHECK(value->Flatten(&out_entry->value)) << "flatten failed";
211 }
212
FlattenEntry(const Reference * key,Item * value)213 void FlattenEntry(const Reference* key, Item* value) {
214 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
215 FlattenKey(key, out_entry);
216 FlattenValue(value, out_entry);
217 out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
218 entry_count_++;
219 }
220
221 ResTable_entry_ext* out_entry_;
222 BigBuffer* buffer_;
223 size_t entry_count_ = 0;
224 };
225
226 struct OverlayableChunk {
227 std::string actor;
228 Source source;
229 std::map<PolicyFlags, std::set<ResourceId>> policy_ids;
230 };
231
232 class PackageFlattener {
233 public:
PackageFlattener(IAaptContext * context,const ResourceTablePackageView & package,const std::map<size_t,std::string> * shared_libs,bool use_sparse_entries,bool collapse_key_stringpool,const std::set<ResourceName> & name_collapse_exemptions)234 PackageFlattener(IAaptContext* context, const ResourceTablePackageView& package,
235 const std::map<size_t, std::string>* shared_libs, bool use_sparse_entries,
236 bool collapse_key_stringpool,
237 const std::set<ResourceName>& name_collapse_exemptions)
238 : context_(context),
239 diag_(context->GetDiagnostics()),
240 package_(package),
241 shared_libs_(shared_libs),
242 use_sparse_entries_(use_sparse_entries),
243 collapse_key_stringpool_(collapse_key_stringpool),
244 name_collapse_exemptions_(name_collapse_exemptions) {
245 }
246
FlattenPackage(BigBuffer * buffer)247 bool FlattenPackage(BigBuffer* buffer) {
248 TRACE_CALL();
249 ChunkWriter pkg_writer(buffer);
250 ResTable_package* pkg_header = pkg_writer.StartChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE);
251 pkg_header->id = util::HostToDevice32(package_.id.value());
252
253 // AAPT truncated the package name, so do the same.
254 // Shared libraries require full package names, so don't truncate theirs.
255 if (context_->GetPackageType() != PackageType::kApp &&
256 package_.name.size() >= arraysize(pkg_header->name)) {
257 diag_->Error(DiagMessage() << "package name '" << package_.name
258 << "' is too long. "
259 "Shared libraries cannot have truncated package names");
260 return false;
261 }
262
263 // Copy the package name in device endianness.
264 strcpy16_htod(pkg_header->name, arraysize(pkg_header->name), util::Utf8ToUtf16(package_.name));
265
266 // Serialize the types. We do this now so that our type and key strings
267 // are populated. We write those first.
268 BigBuffer type_buffer(1024);
269 FlattenTypes(&type_buffer);
270
271 pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size());
272 StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_, diag_);
273
274 pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size());
275 StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_, diag_);
276
277 // Append the types.
278 buffer->AppendBuffer(std::move(type_buffer));
279
280 // If there are libraries (or if the package ID is 0x00), encode a library chunk.
281 if (package_.id.value() == 0x00 || !shared_libs_->empty()) {
282 FlattenLibrarySpec(buffer);
283 }
284
285 if (!FlattenOverlayable(buffer)) {
286 return false;
287 }
288
289 if (!FlattenAliases(buffer)) {
290 return false;
291 }
292
293 pkg_writer.Finish();
294 return true;
295 }
296
297 private:
298 DISALLOW_COPY_AND_ASSIGN(PackageFlattener);
299
300 template <typename T, bool IsItem>
WriteEntry(FlatEntry * entry,BigBuffer * buffer)301 T* WriteEntry(FlatEntry* entry, BigBuffer* buffer) {
302 static_assert(
303 std::is_same<ResTable_entry, T>::value || std::is_same<ResTable_entry_ext, T>::value,
304 "T must be ResTable_entry or ResTable_entry_ext");
305
306 T* result = buffer->NextBlock<T>();
307 ResTable_entry* out_entry = (ResTable_entry*)result;
308 if (entry->entry->visibility.level == Visibility::Level::kPublic) {
309 out_entry->flags |= ResTable_entry::FLAG_PUBLIC;
310 }
311
312 if (entry->value->IsWeak()) {
313 out_entry->flags |= ResTable_entry::FLAG_WEAK;
314 }
315
316 if (!IsItem) {
317 out_entry->flags |= ResTable_entry::FLAG_COMPLEX;
318 }
319
320 out_entry->flags = util::HostToDevice16(out_entry->flags);
321 out_entry->key.index = util::HostToDevice32(entry->entry_key);
322 out_entry->size = util::HostToDevice16(sizeof(T));
323 return result;
324 }
325
FlattenValue(FlatEntry * entry,BigBuffer * buffer)326 bool FlattenValue(FlatEntry* entry, BigBuffer* buffer) {
327 if (const Item* item = ValueCast<Item>(entry->value)) {
328 WriteEntry<ResTable_entry, true>(entry, buffer);
329 Res_value* outValue = buffer->NextBlock<Res_value>();
330 CHECK(item->Flatten(outValue)) << "flatten failed";
331 outValue->size = util::HostToDevice16(sizeof(*outValue));
332 } else {
333 ResTable_entry_ext* out_entry = WriteEntry<ResTable_entry_ext, false>(entry, buffer);
334 MapFlattenVisitor visitor(out_entry, buffer);
335 entry->value->Accept(&visitor);
336 visitor.Finish();
337 }
338 return true;
339 }
340
FlattenConfig(const ResourceTableTypeView & type,const ConfigDescription & config,const size_t num_total_entries,std::vector<FlatEntry> * entries,BigBuffer * buffer)341 bool FlattenConfig(const ResourceTableTypeView& type, const ConfigDescription& config,
342 const size_t num_total_entries, std::vector<FlatEntry>* entries,
343 BigBuffer* buffer) {
344 CHECK(num_total_entries != 0);
345 CHECK(num_total_entries <= std::numeric_limits<uint16_t>::max());
346
347 ChunkWriter type_writer(buffer);
348 ResTable_type* type_header = type_writer.StartChunk<ResTable_type>(RES_TABLE_TYPE_TYPE);
349 type_header->id = type.id.value();
350 type_header->config = config;
351 type_header->config.swapHtoD();
352
353 std::vector<uint32_t> offsets;
354 offsets.resize(num_total_entries, 0xffffffffu);
355
356 BigBuffer values_buffer(512);
357 for (FlatEntry& flat_entry : *entries) {
358 CHECK(static_cast<size_t>(flat_entry.entry->id.value()) < num_total_entries);
359 offsets[flat_entry.entry->id.value()] = values_buffer.size();
360 if (!FlattenValue(&flat_entry, &values_buffer)) {
361 diag_->Error(DiagMessage()
362 << "failed to flatten resource '"
363 << ResourceNameRef(package_.name, type.type, flat_entry.entry->name)
364 << "' for configuration '" << config << "'");
365 return false;
366 }
367 }
368
369 bool sparse_encode = use_sparse_entries_;
370
371 // Only sparse encode if the entries will be read on platforms O+.
372 sparse_encode =
373 sparse_encode && (context_->GetMinSdkVersion() >= SDK_O || config.sdkVersion >= SDK_O);
374
375 // Only sparse encode if the offsets are representable in 2 bytes.
376 sparse_encode =
377 sparse_encode && (values_buffer.size() / 4u) <= std::numeric_limits<uint16_t>::max();
378
379 // Only sparse encode if the ratio of populated entries to total entries is below some
380 // threshold.
381 sparse_encode =
382 sparse_encode && ((100 * entries->size()) / num_total_entries) < kSparseEncodingThreshold;
383
384 if (sparse_encode) {
385 type_header->entryCount = util::HostToDevice32(entries->size());
386 type_header->flags |= ResTable_type::FLAG_SPARSE;
387 ResTable_sparseTypeEntry* indices =
388 type_writer.NextBlock<ResTable_sparseTypeEntry>(entries->size());
389 for (size_t i = 0; i < num_total_entries; i++) {
390 if (offsets[i] != ResTable_type::NO_ENTRY) {
391 CHECK((offsets[i] & 0x03) == 0);
392 indices->idx = util::HostToDevice16(i);
393 indices->offset = util::HostToDevice16(offsets[i] / 4u);
394 indices++;
395 }
396 }
397 } else {
398 type_header->entryCount = util::HostToDevice32(num_total_entries);
399 uint32_t* indices = type_writer.NextBlock<uint32_t>(num_total_entries);
400 for (size_t i = 0; i < num_total_entries; i++) {
401 indices[i] = util::HostToDevice32(offsets[i]);
402 }
403 }
404
405 type_header->entriesStart = util::HostToDevice32(type_writer.size());
406 type_writer.buffer()->AppendBuffer(std::move(values_buffer));
407 type_writer.Finish();
408 return true;
409 }
410
FlattenAliases(BigBuffer * buffer)411 bool FlattenAliases(BigBuffer* buffer) {
412 if (aliases_.empty()) {
413 return true;
414 }
415
416 ChunkWriter alias_writer(buffer);
417 auto header =
418 alias_writer.StartChunk<ResTable_staged_alias_header>(RES_TABLE_STAGED_ALIAS_TYPE);
419 header->count = util::HostToDevice32(aliases_.size());
420
421 auto mapping = alias_writer.NextBlock<ResTable_staged_alias_entry>(aliases_.size());
422 for (auto& p : aliases_) {
423 mapping->stagedResId = util::HostToDevice32(p.first);
424 mapping->finalizedResId = util::HostToDevice32(p.second);
425 ++mapping;
426 }
427 alias_writer.Finish();
428 return true;
429 }
430
FlattenOverlayable(BigBuffer * buffer)431 bool FlattenOverlayable(BigBuffer* buffer) {
432 std::set<ResourceId> seen_ids;
433 std::map<std::string, OverlayableChunk> overlayable_chunks;
434
435 CHECK(bool(package_.id)) << "package must have an ID set when flattening <overlayable>";
436 for (auto& type : package_.types) {
437 CHECK(bool(type.id)) << "type must have an ID set when flattening <overlayable>";
438 for (auto& entry : type.entries) {
439 CHECK(bool(type.id)) << "entry must have an ID set when flattening <overlayable>";
440 if (!entry.overlayable_item) {
441 continue;
442 }
443
444 const OverlayableItem& item = entry.overlayable_item.value();
445
446 // Resource ids should only appear once in the resource table
447 ResourceId id = android::make_resid(package_.id.value(), type.id.value(), entry.id.value());
448 CHECK(seen_ids.find(id) == seen_ids.end())
449 << "multiple overlayable definitions found for resource "
450 << ResourceName(package_.name, type.type, entry.name).to_string();
451 seen_ids.insert(id);
452
453 // Find the overlayable chunk with the specified name
454 OverlayableChunk* overlayable_chunk = nullptr;
455 auto iter = overlayable_chunks.find(item.overlayable->name);
456 if (iter == overlayable_chunks.end()) {
457 OverlayableChunk chunk{item.overlayable->actor, item.overlayable->source};
458 overlayable_chunk =
459 &overlayable_chunks.insert({item.overlayable->name, chunk}).first->second;
460 } else {
461 OverlayableChunk& chunk = iter->second;
462 if (!(chunk.source == item.overlayable->source)) {
463 // The name of an overlayable set of resources must be unique
464 context_->GetDiagnostics()->Error(DiagMessage(item.overlayable->source)
465 << "duplicate overlayable name"
466 << item.overlayable->name << "'");
467 context_->GetDiagnostics()->Error(DiagMessage(chunk.source)
468 << "previous declaration here");
469 return false;
470 }
471
472 CHECK(chunk.actor == item.overlayable->actor);
473 overlayable_chunk = &chunk;
474 }
475
476 if (item.policies == 0) {
477 context_->GetDiagnostics()->Error(DiagMessage(item.overlayable->source)
478 << "overlayable " << entry.name
479 << " does not specify policy");
480 return false;
481 }
482
483 auto policy = overlayable_chunk->policy_ids.find(item.policies);
484 if (policy != overlayable_chunk->policy_ids.end()) {
485 policy->second.insert(id);
486 } else {
487 overlayable_chunk->policy_ids.insert(
488 std::make_pair(item.policies, std::set<ResourceId>{id}));
489 }
490 }
491 }
492
493 for (auto& overlayable_pair : overlayable_chunks) {
494 std::string name = overlayable_pair.first;
495 OverlayableChunk& overlayable = overlayable_pair.second;
496
497 // Write the header of the overlayable chunk
498 ChunkWriter overlayable_writer(buffer);
499 auto* overlayable_type =
500 overlayable_writer.StartChunk<ResTable_overlayable_header>(RES_TABLE_OVERLAYABLE_TYPE);
501 if (name.size() >= arraysize(overlayable_type->name)) {
502 diag_->Error(DiagMessage() << "overlayable name '" << name
503 << "' exceeds maximum length ("
504 << arraysize(overlayable_type->name)
505 << " utf16 characters)");
506 return false;
507 }
508 strcpy16_htod(overlayable_type->name, arraysize(overlayable_type->name),
509 util::Utf8ToUtf16(name));
510
511 if (overlayable.actor.size() >= arraysize(overlayable_type->actor)) {
512 diag_->Error(DiagMessage() << "overlayable name '" << overlayable.actor
513 << "' exceeds maximum length ("
514 << arraysize(overlayable_type->actor)
515 << " utf16 characters)");
516 return false;
517 }
518 strcpy16_htod(overlayable_type->actor, arraysize(overlayable_type->actor),
519 util::Utf8ToUtf16(overlayable.actor));
520
521 // Write each policy block for the overlayable
522 for (auto& policy_ids : overlayable.policy_ids) {
523 ChunkWriter policy_writer(buffer);
524 auto* policy_type = policy_writer.StartChunk<ResTable_overlayable_policy_header>(
525 RES_TABLE_OVERLAYABLE_POLICY_TYPE);
526 policy_type->policy_flags =
527 static_cast<PolicyFlags>(util::HostToDevice32(static_cast<uint32_t>(policy_ids.first)));
528 policy_type->entry_count = util::HostToDevice32(static_cast<uint32_t>(
529 policy_ids.second.size()));
530 // Write the ids after the policy header
531 auto* id_block = policy_writer.NextBlock<ResTable_ref>(policy_ids.second.size());
532 for (const ResourceId& id : policy_ids.second) {
533 id_block->ident = util::HostToDevice32(id.id);
534 id_block++;
535 }
536 policy_writer.Finish();
537 }
538 overlayable_writer.Finish();
539 }
540
541 return true;
542 }
543
FlattenTypeSpec(const ResourceTableTypeView & type,const std::vector<ResourceTableEntryView> & sorted_entries,BigBuffer * buffer)544 bool FlattenTypeSpec(const ResourceTableTypeView& type,
545 const std::vector<ResourceTableEntryView>& sorted_entries,
546 BigBuffer* buffer) {
547 ChunkWriter type_spec_writer(buffer);
548 ResTable_typeSpec* spec_header =
549 type_spec_writer.StartChunk<ResTable_typeSpec>(RES_TABLE_TYPE_SPEC_TYPE);
550 spec_header->id = type.id.value();
551
552 if (sorted_entries.empty()) {
553 type_spec_writer.Finish();
554 return true;
555 }
556
557 // We can't just take the size of the vector. There may be holes in the
558 // entry ID space.
559 // Since the entries are sorted by ID, the last one will be the biggest.
560 const size_t num_entries = sorted_entries.back().id.value() + 1;
561
562 spec_header->entryCount = util::HostToDevice32(num_entries);
563
564 // Reserve space for the masks of each resource in this type. These
565 // show for which configuration axis the resource changes.
566 uint32_t* config_masks = type_spec_writer.NextBlock<uint32_t>(num_entries);
567
568 for (const ResourceTableEntryView& entry : sorted_entries) {
569 const uint16_t entry_id = entry.id.value();
570
571 // Populate the config masks for this entry.
572 uint32_t& entry_config_masks = config_masks[entry_id];
573 if (entry.visibility.level == Visibility::Level::kPublic) {
574 entry_config_masks |= util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
575 }
576 if (entry.visibility.staged_api) {
577 entry_config_masks |= util::HostToDevice32(ResTable_typeSpec::SPEC_STAGED_API);
578 }
579
580 const size_t config_count = entry.values.size();
581 for (size_t i = 0; i < config_count; i++) {
582 const ConfigDescription& config = entry.values[i]->config;
583 for (size_t j = i + 1; j < config_count; j++) {
584 config_masks[entry_id] |= util::HostToDevice32(config.diff(entry.values[j]->config));
585 }
586 }
587 }
588 type_spec_writer.Finish();
589 return true;
590 }
591
FlattenTypes(BigBuffer * buffer)592 bool FlattenTypes(BigBuffer* buffer) {
593 size_t expected_type_id = 1;
594 for (const ResourceTableTypeView& type : package_.types) {
595 if (type.type == ResourceType::kStyleable || type.type == ResourceType::kMacro) {
596 // Styleables and macros are not real resource types.
597 continue;
598 }
599
600 // If there is a gap in the type IDs, fill in the StringPool
601 // with empty values until we reach the ID we expect.
602 while (type.id.value() > expected_type_id) {
603 std::stringstream type_name;
604 type_name << "?" << expected_type_id;
605 type_pool_.MakeRef(type_name.str());
606 expected_type_id++;
607 }
608 expected_type_id++;
609 type_pool_.MakeRef(to_string(type.type));
610
611 if (!FlattenTypeSpec(type, type.entries, buffer)) {
612 return false;
613 }
614
615 // Since the entries are sorted by ID, the last ID will be the largest.
616 const size_t num_entries = type.entries.back().id.value() + 1;
617
618 // The binary resource table lists resource entries for each
619 // configuration.
620 // We store them inverted, where a resource entry lists the values for
621 // each
622 // configuration available. Here we reverse this to match the binary
623 // table.
624 std::map<ConfigDescription, std::vector<FlatEntry>> config_to_entry_list_map;
625
626 // hardcoded string uses characters which make it an invalid resource name
627 const std::string obfuscated_resource_name = "0_resource_name_obfuscated";
628
629 for (const ResourceTableEntryView& entry : type.entries) {
630 if (entry.staged_id) {
631 aliases_.insert(std::make_pair(
632 entry.staged_id.value().id.id,
633 ResourceId(package_.id.value(), type.id.value(), entry.id.value()).id));
634 }
635
636 uint32_t local_key_index;
637 ResourceName resource_name({}, type.type, entry.name);
638 if (!collapse_key_stringpool_ ||
639 name_collapse_exemptions_.find(resource_name) != name_collapse_exemptions_.end()) {
640 local_key_index = (uint32_t)key_pool_.MakeRef(entry.name).index();
641 } else {
642 // resource isn't exempt from collapse, add it as obfuscated value
643 local_key_index = (uint32_t)key_pool_.MakeRef(obfuscated_resource_name).index();
644 }
645 // Group values by configuration.
646 for (auto& config_value : entry.values) {
647 config_to_entry_list_map[config_value->config].push_back(
648 FlatEntry{&entry, config_value->value.get(), local_key_index});
649 }
650 }
651
652 // Flatten a configuration value.
653 for (auto& entry : config_to_entry_list_map) {
654 if (!FlattenConfig(type, entry.first, num_entries, &entry.second, buffer)) {
655 return false;
656 }
657 }
658 }
659 return true;
660 }
661
FlattenLibrarySpec(BigBuffer * buffer)662 void FlattenLibrarySpec(BigBuffer* buffer) {
663 ChunkWriter lib_writer(buffer);
664 ResTable_lib_header* lib_header =
665 lib_writer.StartChunk<ResTable_lib_header>(RES_TABLE_LIBRARY_TYPE);
666
667 const size_t num_entries = (package_.id.value() == 0x00 ? 1 : 0) + shared_libs_->size();
668 CHECK(num_entries > 0);
669
670 lib_header->count = util::HostToDevice32(num_entries);
671
672 ResTable_lib_entry* lib_entry = buffer->NextBlock<ResTable_lib_entry>(num_entries);
673 if (package_.id.value() == 0x00) {
674 // Add this package
675 lib_entry->packageId = util::HostToDevice32(0x00);
676 strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
677 util::Utf8ToUtf16(package_.name));
678 ++lib_entry;
679 }
680
681 for (auto& map_entry : *shared_libs_) {
682 lib_entry->packageId = util::HostToDevice32(map_entry.first);
683 strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
684 util::Utf8ToUtf16(map_entry.second));
685 ++lib_entry;
686 }
687 lib_writer.Finish();
688 }
689
690 IAaptContext* context_;
691 IDiagnostics* diag_;
692 const ResourceTablePackageView package_;
693 const std::map<size_t, std::string>* shared_libs_;
694 bool use_sparse_entries_;
695 StringPool type_pool_;
696 StringPool key_pool_;
697 bool collapse_key_stringpool_;
698 const std::set<ResourceName>& name_collapse_exemptions_;
699 std::map<uint32_t, uint32_t> aliases_;
700 };
701
702 } // namespace
703
Consume(IAaptContext * context,ResourceTable * table)704 bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) {
705 TRACE_CALL();
706 // We must do this before writing the resources, since the string pool IDs may change.
707 table->string_pool.Prune();
708 table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
709 int diff = util::compare(a.priority, b.priority);
710 if (diff == 0) {
711 diff = a.config.compare(b.config);
712 }
713 return diff;
714 });
715
716 // Write the ResTable header.
717 const auto& table_view =
718 table->GetPartitionedView(ResourceTableViewOptions{.create_alias_entries = true});
719 ChunkWriter table_writer(buffer_);
720 ResTable_header* table_header = table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE);
721 table_header->packageCount = util::HostToDevice32(table_view.packages.size());
722
723 // Flatten the values string pool.
724 StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool,
725 context->GetDiagnostics());
726
727 BigBuffer package_buffer(1024);
728
729 // Flatten each package.
730 for (auto& package : table_view.packages) {
731 if (context->GetPackageType() == PackageType::kApp) {
732 // Write a self mapping entry for this package if the ID is non-standard (0x7f).
733 CHECK((bool)package.id) << "Resource ids have not been assigned before flattening the table";
734 const uint8_t package_id = package.id.value();
735 if (package_id != kFrameworkPackageId && package_id != kAppPackageId) {
736 auto result = table->included_packages_.insert({package_id, package.name});
737 if (!result.second && result.first->second != package.name) {
738 // A mapping for this package ID already exists, and is a different package. Error!
739 context->GetDiagnostics()->Error(
740 DiagMessage() << android::base::StringPrintf(
741 "can't map package ID %02x to '%s'. Already mapped to '%s'", package_id,
742 package.name.c_str(), result.first->second.c_str()));
743 return false;
744 }
745 }
746 }
747
748 PackageFlattener flattener(context, package, &table->included_packages_,
749 options_.use_sparse_entries, options_.collapse_key_stringpool,
750 options_.name_collapse_exemptions);
751 if (!flattener.FlattenPackage(&package_buffer)) {
752 return false;
753 }
754 }
755
756 // Finally merge all the packages into the main buffer.
757 table_writer.buffer()->AppendBuffer(std::move(package_buffer));
758 table_writer.Finish();
759 return true;
760 }
761
762 } // namespace aapt
763