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 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   ResourceEntry* entry;
76   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 ValueVisitor {
83  public:
84   using ValueVisitor::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(Attribute * attr)90   void Visit(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 (Attribute::Symbol& s : attr->symbols) {
110       BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value);
111       FlattenEntry(&s.symbol, &val);
112     }
113   }
114 
Visit(Style * style)115   void Visit(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::sort(style->entries.begin(), style->entries.end(), cmp_style_entries);
124 
125     for (Style::Entry& entry : style->entries) {
126       FlattenEntry(&entry.key, entry.value.get());
127     }
128   }
129 
Visit(Styleable * styleable)130   void Visit(Styleable* styleable) override {
131     for (auto& attr_ref : styleable->entries) {
132       BinaryPrimitive val(Res_value{});
133       FlattenEntry(&attr_ref, &val);
134     }
135   }
136 
Visit(Array * array)137   void Visit(Array* array) override {
138     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());
142     }
143   }
144 
Visit(Plural * plural)145   void Visit(Plural* plural) override {
146     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:
179           LOG(FATAL) << "unhandled plural type";
180           break;
181       }
182 
183       Reference key(q);
184       FlattenEntry(&key, plural->values[i].get());
185     }
186   }
187 
188   /**
189    * Call this after visiting a Value. This will finish any work that
190    * needs to be done to prepare the entry.
191    */
Finish()192   void Finish() {
193     out_entry_->count = util::HostToDevice32(entry_count_);
194   }
195 
196  private:
197   DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor);
198 
FlattenKey(Reference * key,ResTable_map * out_entry)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);
202   }
203 
FlattenValue(Item * value,ResTable_map * out_entry)204   void FlattenValue(Item* value, ResTable_map* out_entry) {
205     CHECK(value->Flatten(&out_entry->value)) << "flatten failed";
206   }
207 
FlattenEntry(Reference * key,Item * value)208   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_++;
214   }
215 
216   ResTable_entry_ext* out_entry_;
217   BigBuffer* buffer_;
218   size_t entry_count_ = 0;
219 };
220 
221 struct OverlayableChunk {
222   std::string actor;
223   Source source;
224   std::map<OverlayableItem::PolicyFlags, std::set<ResourceId>> policy_ids;
225 };
226 
227 class PackageFlattener {
228  public:
PackageFlattener(IAaptContext * context,ResourceTablePackage * package,const std::map<size_t,std::string> * shared_libs,bool use_sparse_entries,bool collapse_key_stringpool,const std::set<std::string> & whitelisted_resources)229   PackageFlattener(IAaptContext* context, ResourceTablePackage* package,
230                    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)
232       : context_(context),
233         diag_(context->GetDiagnostics()),
234         package_(package),
235         shared_libs_(shared_libs),
236         use_sparse_entries_(use_sparse_entries),
237         collapse_key_stringpool_(collapse_key_stringpool),
238         whitelisted_resources_(whitelisted_resources) {
239   }
240 
FlattenPackage(BigBuffer * buffer)241   bool FlattenPackage(BigBuffer* buffer) {
242     TRACE_CALL();
243     ChunkWriter pkg_writer(buffer);
244     ResTable_package* pkg_header = pkg_writer.StartChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE);
245     pkg_header->id = util::HostToDevice32(package_->id.value());
246 
247     // 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)) {
251       diag_->Error(DiagMessage() << "package name '" << package_->name
252                                  << "' is too long. "
253                                     "Shared libraries cannot have truncated package names");
254       return false;
255     }
256 
257     // Copy the package name in device endianness.
258     strcpy16_htod(pkg_header->name, arraysize(pkg_header->name), util::Utf8ToUtf16(package_->name));
259 
260     // Serialize the types. We do this now so that our type and key strings
261     // are populated. We write those first.
262     BigBuffer type_buffer(1024);
263     FlattenTypes(&type_buffer);
264 
265     pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size());
266     StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_, diag_);
267 
268     pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size());
269     StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_, diag_);
270 
271     // Append the types.
272     buffer->AppendBuffer(std::move(type_buffer));
273 
274     // 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 
279     if (!FlattenOverlayable(buffer)) {
280       return false;
281     }
282 
283     pkg_writer.Finish();
284     return true;
285   }
286 
287  private:
288   DISALLOW_COPY_AND_ASSIGN(PackageFlattener);
289 
290   template <typename T, bool IsItem>
WriteEntry(FlatEntry * entry,BigBuffer * buffer)291   T* WriteEntry(FlatEntry* entry, BigBuffer* buffer) {
292     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");
295 
296     T* result = buffer->NextBlock<T>();
297     ResTable_entry* out_entry = (ResTable_entry*)result;
298     if (entry->entry->visibility.level == Visibility::Level::kPublic) {
299       out_entry->flags |= ResTable_entry::FLAG_PUBLIC;
300     }
301 
302     if (entry->value->IsWeak()) {
303       out_entry->flags |= ResTable_entry::FLAG_WEAK;
304     }
305 
306     if (!IsItem) {
307       out_entry->flags |= ResTable_entry::FLAG_COMPLEX;
308     }
309 
310     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));
313     return result;
314   }
315 
FlattenValue(FlatEntry * entry,BigBuffer * buffer)316   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));
322     } else {
323       ResTable_entry_ext* out_entry = WriteEntry<ResTable_entry_ext, false>(entry, buffer);
324       MapFlattenVisitor visitor(out_entry, buffer);
325       entry->value->Accept(&visitor);
326       visitor.Finish();
327     }
328     return true;
329   }
330 
FlattenConfig(const ResourceTableType * type,const ConfigDescription & config,const size_t num_total_entries,std::vector<FlatEntry> * entries,BigBuffer * buffer)331   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 
337     ChunkWriter type_writer(buffer);
338     ResTable_type* type_header = type_writer.StartChunk<ResTable_type>(RES_TABLE_TYPE_TYPE);
339     type_header->id = type->id.value();
340     type_header->config = config;
341     type_header->config.swapHtoD();
342 
343     std::vector<uint32_t> offsets;
344     offsets.resize(num_total_entries, 0xffffffffu);
345 
346     BigBuffer values_buffer(512);
347     for (FlatEntry& flat_entry : *entries) {
348       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)) {
351         diag_->Error(DiagMessage()
352                      << "failed to flatten resource '"
353                      << ResourceNameRef(package_->name, type->type, flat_entry.entry->name)
354                      << "' for configuration '" << config << "'");
355         return false;
356       }
357     }
358 
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));
397     type_writer.Finish();
398     return true;
399   }
400 
CollectAndSortTypes()401   std::vector<ResourceTableType*> CollectAndSortTypes() {
402     std::vector<ResourceTableType*> sorted_types;
403     for (auto& type : package_->types) {
404       if (type->type == ResourceType::kStyleable) {
405         // Styleables aren't real Resource Types, they are represented in the
406         // R.java file.
407         continue;
408       }
409 
410       CHECK(bool(type->id)) << "type must have an ID set";
411 
412       sorted_types.push_back(type.get());
413     }
414     std::sort(sorted_types.begin(), sorted_types.end(), cmp_ids<ResourceTableType>);
415     return sorted_types;
416   }
417 
CollectAndSortEntries(ResourceTableType * type)418   std::vector<ResourceEntry*> CollectAndSortEntries(ResourceTableType* type) {
419     // Sort the entries by entry ID.
420     std::vector<ResourceEntry*> sorted_entries;
421     for (auto& entry : type->entries) {
422       CHECK(bool(entry->id)) << "entry must have an ID set";
423       sorted_entries.push_back(entry.get());
424     }
425     std::sort(sorted_entries.begin(), sorted_entries.end(), cmp_ids<ResourceEntry>);
426     return sorted_entries;
427   }
428 
FlattenOverlayable(BigBuffer * buffer)429   bool FlattenOverlayable(BigBuffer* buffer) {
430     std::set<ResourceId> seen_ids;
431     std::map<std::string, OverlayableChunk> overlayable_chunks;
432 
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>";
438         if (!entry->overlayable_item) {
439           continue;
440         }
441 
442         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;
473         }
474 
475         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 
483         uint32_t policy_flags = 0;
484         if (item.policies & OverlayableItem::Policy::kPublic) {
485           policy_flags |= ResTable_overlayable_policy_header::POLICY_PUBLIC;
486         }
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;
498         }
499         if (item.policies & OverlayableItem::Policy::kOdm) {
500           policy_flags |= ResTable_overlayable_policy_header::POLICY_ODM_PARTITION;
501         }
502         if (item.policies & OverlayableItem::Policy::kOem) {
503           policy_flags |= ResTable_overlayable_policy_header::POLICY_OEM_PARTITION;
504         }
505 
506         auto policy = overlayable_chunk->policy_ids.find(policy_flags);
507         if (policy != overlayable_chunk->policy_ids.end()) {
508           policy->second.insert(id);
509         } else {
510           overlayable_chunk->policy_ids.insert(
511               std::make_pair(policy_flags, std::set<ResourceId>{id}));
512         }
513       }
514     }
515 
516     for (auto& overlayable_pair : overlayable_chunks) {
517       std::string name = overlayable_pair.first;
518       OverlayableChunk& overlayable = overlayable_pair.second;
519 
520       // Write the header of the overlayable chunk
521       ChunkWriter overlayable_writer(buffer);
522       auto* overlayable_type =
523           overlayable_writer.StartChunk<ResTable_overlayable_header>(RES_TABLE_OVERLAYABLE_TYPE);
524       if (name.size() >= arraysize(overlayable_type->name)) {
525         diag_->Error(DiagMessage() << "overlayable name '" << name
526                                    << "' exceeds maximum length ("
527                                    << arraysize(overlayable_type->name)
528                                    << " utf16 characters)");
529         return false;
530       }
531       strcpy16_htod(overlayable_type->name, arraysize(overlayable_type->name),
532                     util::Utf8ToUtf16(name));
533 
534       if (overlayable.actor.size() >= arraysize(overlayable_type->actor)) {
535         diag_->Error(DiagMessage() << "overlayable name '" << overlayable.actor
536                                    << "' exceeds maximum length ("
537                                    << arraysize(overlayable_type->actor)
538                                    << " utf16 characters)");
539         return false;
540       }
541       strcpy16_htod(overlayable_type->actor, arraysize(overlayable_type->actor),
542                     util::Utf8ToUtf16(overlayable.actor));
543 
544       // Write each policy block for the overlayable
545       for (auto& policy_ids : overlayable.policy_ids) {
546         ChunkWriter policy_writer(buffer);
547         auto* policy_type = policy_writer.StartChunk<ResTable_overlayable_policy_header>(
548             RES_TABLE_OVERLAYABLE_POLICY_TYPE);
549         policy_type->policy_flags = util::HostToDevice32(static_cast<uint32_t>(policy_ids.first));
550         policy_type->entry_count = util::HostToDevice32(static_cast<uint32_t>(
551                                                             policy_ids.second.size()));
552         // Write the ids after the policy header
553         auto* id_block = policy_writer.NextBlock<ResTable_ref>(policy_ids.second.size());
554         for (const ResourceId& id : policy_ids.second) {
555           id_block->ident = util::HostToDevice32(id.id);
556           id_block++;
557         }
558         policy_writer.Finish();
559       }
560       overlayable_writer.Finish();
561     }
562 
563     return true;
564   }
565 
FlattenTypeSpec(ResourceTableType * type,std::vector<ResourceEntry * > * sorted_entries,BigBuffer * buffer)566   bool FlattenTypeSpec(ResourceTableType* type, std::vector<ResourceEntry*>* sorted_entries,
567                        BigBuffer* buffer) {
568     ChunkWriter type_spec_writer(buffer);
569     ResTable_typeSpec* spec_header =
570         type_spec_writer.StartChunk<ResTable_typeSpec>(RES_TABLE_TYPE_SPEC_TYPE);
571     spec_header->id = type->id.value();
572 
573     if (sorted_entries->empty()) {
574       type_spec_writer.Finish();
575       return true;
576     }
577 
578     // We can't just take the size of the vector. There may be holes in the
579     // entry ID space.
580     // Since the entries are sorted by ID, the last one will be the biggest.
581     const size_t num_entries = sorted_entries->back()->id.value() + 1;
582 
583     spec_header->entryCount = util::HostToDevice32(num_entries);
584 
585     // Reserve space for the masks of each resource in this type. These
586     // show for which configuration axis the resource changes.
587     uint32_t* config_masks = type_spec_writer.NextBlock<uint32_t>(num_entries);
588 
589     const size_t actual_num_entries = sorted_entries->size();
590     for (size_t entryIndex = 0; entryIndex < actual_num_entries; entryIndex++) {
591       ResourceEntry* entry = sorted_entries->at(entryIndex);
592 
593       // Populate the config masks for this entry.
594 
595       if (entry->visibility.level == Visibility::Level::kPublic) {
596         config_masks[entry->id.value()] |= util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
597       }
598 
599       const size_t config_count = entry->values.size();
600       for (size_t i = 0; i < config_count; i++) {
601         const ConfigDescription& config = entry->values[i]->config;
602         for (size_t j = i + 1; j < config_count; j++) {
603           config_masks[entry->id.value()] |=
604               util::HostToDevice32(config.diff(entry->values[j]->config));
605         }
606       }
607     }
608     type_spec_writer.Finish();
609     return true;
610   }
611 
FlattenTypes(BigBuffer * buffer)612   bool FlattenTypes(BigBuffer* buffer) {
613     // Sort the types by their IDs. They will be inserted into the StringPool in
614     // this order.
615     std::vector<ResourceTableType*> sorted_types = CollectAndSortTypes();
616 
617     size_t expected_type_id = 1;
618     for (ResourceTableType* type : sorted_types) {
619       // If there is a gap in the type IDs, fill in the StringPool
620       // with empty values until we reach the ID we expect.
621       while (type->id.value() > expected_type_id) {
622         std::stringstream type_name;
623         type_name << "?" << expected_type_id;
624         type_pool_.MakeRef(type_name.str());
625         expected_type_id++;
626       }
627       expected_type_id++;
628       type_pool_.MakeRef(to_string(type->type));
629 
630       std::vector<ResourceEntry*> sorted_entries = CollectAndSortEntries(type);
631       if (sorted_entries.empty()) {
632         continue;
633       }
634 
635       if (!FlattenTypeSpec(type, &sorted_entries, buffer)) {
636         return false;
637       }
638 
639       // Since the entries are sorted by ID, the last ID will be the largest.
640       const size_t num_entries = sorted_entries.back()->id.value() + 1;
641 
642       // The binary resource table lists resource entries for each
643       // configuration.
644       // We store them inverted, where a resource entry lists the values for
645       // each
646       // configuration available. Here we reverse this to match the binary
647       // table.
648       std::map<ConfigDescription, std::vector<FlatEntry>> config_to_entry_list_map;
649 
650       // hardcoded string uses characters which make it an invalid resource name
651       const std::string obfuscated_resource_name = "0_resource_name_obfuscated";
652 
653       for (ResourceEntry* entry : sorted_entries) {
654         uint32_t local_key_index;
655         if (!collapse_key_stringpool_ ||
656             whitelisted_resources_.find(entry->name) != whitelisted_resources_.end()) {
657           local_key_index = (uint32_t)key_pool_.MakeRef(entry->name).index();
658         } else {
659           // resource isn't whitelisted, add it as obfuscated value
660           local_key_index = (uint32_t)key_pool_.MakeRef(obfuscated_resource_name).index();
661         }
662         // Group values by configuration.
663         for (auto& config_value : entry->values) {
664           config_to_entry_list_map[config_value->config].push_back(
665               FlatEntry{entry, config_value->value.get(), local_key_index});
666         }
667       }
668 
669       // Flatten a configuration value.
670       for (auto& entry : config_to_entry_list_map) {
671         if (!FlattenConfig(type, entry.first, num_entries, &entry.second, buffer)) {
672           return false;
673         }
674       }
675     }
676     return true;
677   }
678 
FlattenLibrarySpec(BigBuffer * buffer)679   void FlattenLibrarySpec(BigBuffer* buffer) {
680     ChunkWriter lib_writer(buffer);
681     ResTable_lib_header* lib_header =
682         lib_writer.StartChunk<ResTable_lib_header>(RES_TABLE_LIBRARY_TYPE);
683 
684     const size_t num_entries = (package_->id.value() == 0x00 ? 1 : 0) + shared_libs_->size();
685     CHECK(num_entries > 0);
686 
687     lib_header->count = util::HostToDevice32(num_entries);
688 
689     ResTable_lib_entry* lib_entry = buffer->NextBlock<ResTable_lib_entry>(num_entries);
690     if (package_->id.value() == 0x00) {
691       // Add this package
692       lib_entry->packageId = util::HostToDevice32(0x00);
693       strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
694                     util::Utf8ToUtf16(package_->name));
695       ++lib_entry;
696     }
697 
698     for (auto& map_entry : *shared_libs_) {
699       lib_entry->packageId = util::HostToDevice32(map_entry.first);
700       strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
701                     util::Utf8ToUtf16(map_entry.second));
702       ++lib_entry;
703     }
704     lib_writer.Finish();
705   }
706 
707   IAaptContext* context_;
708   IDiagnostics* diag_;
709   ResourceTablePackage* package_;
710   const std::map<size_t, std::string>* shared_libs_;
711   bool use_sparse_entries_;
712   StringPool type_pool_;
713   StringPool key_pool_;
714   bool collapse_key_stringpool_;
715   const std::set<std::string>& whitelisted_resources_;
716 };
717 
718 }  // namespace
719 
Consume(IAaptContext * context,ResourceTable * table)720 bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) {
721   TRACE_CALL();
722   // We must do this before writing the resources, since the string pool IDs may change.
723   table->string_pool.Prune();
724   table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
725     int diff = util::compare(a.priority, b.priority);
726     if (diff == 0) {
727       diff = a.config.compare(b.config);
728     }
729     return diff;
730   });
731 
732   // Write the ResTable header.
733   ChunkWriter table_writer(buffer_);
734   ResTable_header* table_header = table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE);
735   table_header->packageCount = util::HostToDevice32(table->packages.size());
736 
737   // Flatten the values string pool.
738   StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool,
739       context->GetDiagnostics());
740 
741   BigBuffer package_buffer(1024);
742 
743   // Flatten each package.
744   for (auto& package : table->packages) {
745     if (context->GetPackageType() == PackageType::kApp) {
746       // Write a self mapping entry for this package if the ID is non-standard (0x7f).
747       const uint8_t package_id = package->id.value();
748       if (package_id != kFrameworkPackageId && package_id != kAppPackageId) {
749         auto result = table->included_packages_.insert({package_id, package->name});
750         if (!result.second && result.first->second != package->name) {
751           // A mapping for this package ID already exists, and is a different package. Error!
752           context->GetDiagnostics()->Error(
753               DiagMessage() << android::base::StringPrintf(
754                   "can't map package ID %02x to '%s'. Already mapped to '%s'", package_id,
755                   package->name.c_str(), result.first->second.c_str()));
756           return false;
757         }
758       }
759     }
760 
761     PackageFlattener flattener(context, package.get(), &table->included_packages_,
762                                options_.use_sparse_entries, options_.collapse_key_stringpool,
763                                options_.whitelisted_resources);
764     if (!flattener.FlattenPackage(&package_buffer)) {
765       return false;
766     }
767   }
768 
769   // Finally merge all the packages into the main buffer.
770   table_writer.buffer()->AppendBuffer(std::move(package_buffer));
771   table_writer.Finish();
772   return true;
773 }
774 
775 }  // namespace aapt
776