blob: 19d030ec4a25fba0340f773eb75b785e38d57129 [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 Lesinski1ab598f2015-08-14 14:26:04 -070017#include "flatten/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"
26
27#include "ResourceTable.h"
28#include "ResourceValues.h"
29#include "ValueVisitor.h"
30#include "flatten/ChunkWriter.h"
31#include "flatten/ResourceTypeExtensions.h"
32#include "util/BigBuffer.h"
33
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034using namespace android;
35
36namespace aapt {
37
38namespace {
39
40template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041static bool cmp_ids(const T* a, const T* b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 return a->id.value() < b->id.value();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043}
44
45static void strcpy16_htod(uint16_t* dst, size_t len, const StringPiece16& src) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 if (len == 0) {
47 return;
48 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 size_t i;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 const char16_t* src_data = src.data();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 for (i = 0; i < len - 1 && i < src.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 dst[i] = util::HostToDevice16((uint16_t)src_data[i]);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 }
55 dst[i] = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056}
57
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058static bool cmp_style_entries(const Style::Entry& a, const Style::Entry& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 if (a.key.id) {
60 if (b.key.id) {
61 return a.key.id.value() < b.key.id.value();
62 }
63 return true;
64 } else if (!b.key.id) {
65 return a.key.name.value() < b.key.name.value();
66 }
67 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -080068}
69
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070struct FlatEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 ResourceEntry* entry;
72 Value* value;
Adam Lesinski3b4cd942015-10-30 16:31:42 -070073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 // The entry string pool index to the entry's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 uint32_t entry_key;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076};
77
Adam Lesinski59e04c62016-02-04 15:59:23 -080078class MapFlattenVisitor : public RawValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 using RawValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 MapFlattenVisitor(ResTable_entry_ext* out_entry, BigBuffer* buffer)
83 : out_entry_(out_entry), buffer_(buffer) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 void Visit(Attribute* attr) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 {
87 Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->type_mask);
89 FlattenEntry(&key, &val);
Adam Lesinski28cacf02015-11-23 14:22:47 -080090 }
91
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 if (attr->min_int != std::numeric_limits<int32_t>::min()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN));
94 BinaryPrimitive val(Res_value::TYPE_INT_DEC,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 static_cast<uint32_t>(attr->min_int));
96 FlattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097 }
98
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 if (attr->max_int != std::numeric_limits<int32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX));
101 BinaryPrimitive val(Res_value::TYPE_INT_DEC,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 static_cast<uint32_t>(attr->max_int));
103 FlattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 }
105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 for (Attribute::Symbol& s : attr->symbols) {
107 BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 FlattenEntry(&s.symbol, &val);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 }
110 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 void Visit(Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 if (style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 const Reference& parent_ref = style->parent.value();
115 CHECK(bool(parent_ref.id)) << "parent has no ID";
116 out_entry_->parent.ident = util::HostToDevice32(parent_ref.id.value().id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117 }
118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 // Sort the style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 std::sort(style->entries.begin(), style->entries.end(), cmp_style_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121
122 for (Style::Entry& entry : style->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 FlattenEntry(&entry.key, entry.value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 void Visit(Styleable* styleable) override {
128 for (auto& attr_ref : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 BinaryPrimitive val(Res_value{});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 FlattenEntry(&attr_ref, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 void Visit(Array* array) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 for (auto& item : array->items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
137 FlattenValue(item.get(), out_entry);
138 out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
139 entry_count_++;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800140 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 void Visit(Plural* plural) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 const size_t count = plural->values.size();
145 for (size_t i = 0; i < count; i++) {
146 if (!plural->values[i]) {
147 continue;
148 }
149
150 ResourceId q;
151 switch (i) {
152 case Plural::Zero:
153 q.id = android::ResTable_map::ATTR_ZERO;
154 break;
155
156 case Plural::One:
157 q.id = android::ResTable_map::ATTR_ONE;
158 break;
159
160 case Plural::Two:
161 q.id = android::ResTable_map::ATTR_TWO;
162 break;
163
164 case Plural::Few:
165 q.id = android::ResTable_map::ATTR_FEW;
166 break;
167
168 case Plural::Many:
169 q.id = android::ResTable_map::ATTR_MANY;
170 break;
171
172 case Plural::Other:
173 q.id = android::ResTable_map::ATTR_OTHER;
174 break;
175
176 default:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 LOG(FATAL) << "unhandled plural type";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 break;
179 }
180
181 Reference key(q);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 FlattenEntry(&key, plural->values[i].get());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800183 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 /**
187 * Call this after visiting a Value. This will finish any work that
188 * needs to be done to prepare the entry.
189 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 void Finish() { out_entry_->count = util::HostToDevice32(entry_count_); }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800191
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor);
194
195 void FlattenKey(Reference* key, ResTable_map* out_entry) {
196 CHECK(bool(key->id)) << "key has no ID";
197 out_entry->name.ident = util::HostToDevice32(key->id.value().id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 void FlattenValue(Item* value, ResTable_map* out_entry) {
201 CHECK(value->Flatten(&out_entry->value)) << "flatten failed";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 void FlattenEntry(Reference* key, Item* value) {
205 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
206 FlattenKey(key, out_entry);
207 FlattenValue(value, out_entry);
208 out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
209 entry_count_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 }
211
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 ResTable_entry_ext* out_entry_;
213 BigBuffer* buffer_;
214 size_t entry_count_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215};
216
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700217class PackageFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 public:
219 PackageFlattener(IDiagnostics* diag, ResourceTablePackage* package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 : diag_(diag), package_(package) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 bool FlattenPackage(BigBuffer* buffer) {
223 ChunkWriter pkg_writer(buffer);
224 ResTable_package* pkg_header =
225 pkg_writer.StartChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE);
226 pkg_header->id = util::HostToDevice32(package_->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 if (package_->name.size() >= arraysize(pkg_header->name)) {
229 diag_->Error(DiagMessage() << "package name '" << package_->name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 << "' is too long");
231 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700232 }
233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 // Copy the package name in device endianness.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 strcpy16_htod(pkg_header->name, arraysize(pkg_header->name),
236 util::Utf8ToUtf16(package_->name));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700237
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 // Serialize the types. We do this now so that our type and key strings
239 // are populated. We write those first.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 BigBuffer type_buffer(1024);
241 FlattenTypes(&type_buffer);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size());
244 StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700245
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size());
247 StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700248
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 // Append the types.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 buffer->AppendBuffer(std::move(type_buffer));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700251
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 pkg_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 return true;
254 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700255
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 DISALLOW_COPY_AND_ASSIGN(PackageFlattener);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 template <typename T, bool IsItem>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 T* WriteEntry(FlatEntry* entry, BigBuffer* buffer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 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 Lesinskice5e56e2016-10-21 17:56:45 -0700265 T* result = buffer->NextBlock<T>();
266 ResTable_entry* out_entry = (ResTable_entry*)result;
267 if (entry->entry->symbol_status.state == SymbolState::kPublic) {
268 out_entry->flags |= ResTable_entry::FLAG_PUBLIC;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269 }
270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 if (entry->value->IsWeak()) {
272 out_entry->flags |= ResTable_entry::FLAG_WEAK;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700273 }
274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 if (!IsItem) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 out_entry->flags |= ResTable_entry::FLAG_COMPLEX;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700277 }
278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 out_entry->flags = util::HostToDevice16(out_entry->flags);
280 out_entry->key.index = util::HostToDevice32(entry->entry_key);
281 out_entry->size = util::HostToDevice16(sizeof(T));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 return result;
283 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284
Adam Lesinskice5e56e2016-10-21 17:56:45 -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 CHECK(item->Flatten(outValue)) << "flatten failed";
290 outValue->size = util::HostToDevice16(sizeof(*outValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 ResTable_entry_ext* out_entry =
293 WriteEntry<ResTable_entry_ext, false>(entry, buffer);
294 MapFlattenVisitor visitor(out_entry, buffer);
295 entry->value->Accept(&visitor);
296 visitor.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 }
298 return true;
299 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 bool FlattenConfig(const ResourceTableType* type,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 const ConfigDescription& config,
303 std::vector<FlatEntry>* entries, BigBuffer* buffer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 ChunkWriter type_writer(buffer);
305 ResTable_type* type_header =
306 type_writer.StartChunk<ResTable_type>(RES_TABLE_TYPE_TYPE);
307 type_header->id = type->id.value();
308 type_header->config = config;
309 type_header->config.swapHtoD();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 auto max_accum = [](uint32_t max,
312 const std::unique_ptr<ResourceEntry>& a) -> uint32_t {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 return std::max(max, (uint32_t)a->id.value());
314 };
315
316 // Find the largest entry ID. That is how many entries we will have.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 const uint32_t entry_count =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 std::accumulate(type->entries.begin(), type->entries.end(), 0,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 max_accum) +
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 1;
321
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 type_header->entryCount = util::HostToDevice32(entry_count);
323 uint32_t* indices = type_writer.NextBlock<uint32_t>(entry_count);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 CHECK((size_t)entry_count <= std::numeric_limits<uint16_t>::max());
326 memset(indices, 0xff, entry_count * sizeof(uint32_t));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 type_header->entriesStart = util::HostToDevice32(type_writer.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 const size_t entry_start = type_writer.buffer()->size();
331 for (FlatEntry& flat_entry : *entries) {
332 CHECK(flat_entry.entry->id.value() < entry_count);
333 indices[flat_entry.entry->id.value()] =
334 util::HostToDevice32(type_writer.buffer()->size() - entry_start);
335 if (!FlattenValue(&flat_entry, type_writer.buffer())) {
336 diag_->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 << "failed to flatten resource '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 << ResourceNameRef(package_->name, type->type,
339 flat_entry.entry->name)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 << "' for configuration '" << config << "'");
341 return false;
342 }
343 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 type_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 return true;
346 }
347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 std::vector<ResourceTableType*> CollectAndSortTypes() {
349 std::vector<ResourceTableType*> sorted_types;
350 for (auto& type : package_->types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 if (type->type == ResourceType::kStyleable) {
352 // Styleables aren't real Resource Types, they are represented in the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 // R.java file.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 continue;
355 }
356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 CHECK(bool(type->id)) << "type must have an ID set";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 sorted_types.push_back(type.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 std::sort(sorted_types.begin(), sorted_types.end(),
362 cmp_ids<ResourceTableType>);
363 return sorted_types;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 }
365
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 std::vector<ResourceEntry*> CollectAndSortEntries(ResourceTableType* type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 // Sort the entries by entry ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 std::vector<ResourceEntry*> sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 CHECK(bool(entry->id)) << "entry must have an ID set";
371 sorted_entries.push_back(entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 std::sort(sorted_entries.begin(), sorted_entries.end(),
374 cmp_ids<ResourceEntry>);
375 return sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 }
377
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 bool FlattenTypeSpec(ResourceTableType* type,
379 std::vector<ResourceEntry*>* sorted_entries,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 BigBuffer* buffer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 ChunkWriter type_spec_writer(buffer);
382 ResTable_typeSpec* spec_header =
383 type_spec_writer.StartChunk<ResTable_typeSpec>(
384 RES_TABLE_TYPE_SPEC_TYPE);
385 spec_header->id = type->id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 if (sorted_entries->empty()) {
388 type_spec_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700390 }
391
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 // We can't just take the size of the vector. There may be holes in the
393 // entry ID space.
394 // Since the entries are sorted by ID, the last one will be the biggest.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 const size_t num_entries = sorted_entries->back()->id.value() + 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 spec_header->entryCount = util::HostToDevice32(num_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398
399 // Reserve space for the masks of each resource in this type. These
400 // show for which configuration axis the resource changes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 uint32_t* config_masks = type_spec_writer.NextBlock<uint32_t>(num_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 const size_t actual_num_entries = sorted_entries->size();
404 for (size_t entryIndex = 0; entryIndex < actual_num_entries; entryIndex++) {
405 ResourceEntry* entry = sorted_entries->at(entryIndex);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406
407 // Populate the config masks for this entry.
408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 if (entry->symbol_status.state == SymbolState::kPublic) {
410 config_masks[entry->id.value()] |=
411 util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 }
413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 const size_t config_count = entry->values.size();
415 for (size_t i = 0; i < config_count; i++) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 const ConfigDescription& config = entry->values[i]->config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 for (size_t j = i + 1; j < config_count; j++) {
418 config_masks[entry->id.value()] |=
419 util::HostToDevice32(config.diff(entry->values[j]->config));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700420 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700422 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 type_spec_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 return true;
425 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 bool FlattenTypes(BigBuffer* buffer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 // Sort the types by their IDs. They will be inserted into the StringPool in
429 // this order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 std::vector<ResourceTableType*> sorted_types = CollectAndSortTypes();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 size_t expected_type_id = 1;
433 for (ResourceTableType* type : sorted_types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 // If there is a gap in the type IDs, fill in the StringPool
435 // with empty values until we reach the ID we expect.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 while (type->id.value() > expected_type_id) {
437 std::stringstream type_name;
438 type_name << "?" << expected_type_id;
439 type_pool_.MakeRef(type_name.str());
440 expected_type_id++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 expected_type_id++;
443 type_pool_.MakeRef(ToString(type->type));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 std::vector<ResourceEntry*> sorted_entries = CollectAndSortEntries(type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 if (!FlattenTypeSpec(type, &sorted_entries, buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 return false;
449 }
450
451 // The binary resource table lists resource entries for each
452 // configuration.
453 // We store them inverted, where a resource entry lists the values for
454 // each
455 // configuration available. Here we reverse this to match the binary
456 // table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 std::map<ConfigDescription, std::vector<FlatEntry>>
458 config_to_entry_list_map;
459 for (ResourceEntry* entry : sorted_entries) {
460 const uint32_t key_index =
461 (uint32_t)key_pool_.MakeRef(entry->name).index();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462
463 // Group values by configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 for (auto& config_value : entry->values) {
465 config_to_entry_list_map[config_value->config].push_back(
466 FlatEntry{entry, config_value->value.get(), key_index});
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.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 for (auto& entry : config_to_entry_list_map) {
472 if (!FlattenConfig(type, entry.first, &entry.second, buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 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 Lesinskice5e56e2016-10-21 17:56:45 -0700479
480 IDiagnostics* diag_;
481 ResourceTablePackage* package_;
482 StringPool type_pool_;
483 StringPool key_pool_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700484};
485
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 // We must do this before writing the resources, since the string pool IDs may
490 // change.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 table->string_pool.Sort(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700493 int diff = a.context.priority - b.context.priority;
494 if (diff < 0) return true;
495 if (diff > 0) return false;
496 diff = a.context.config.compare(b.context.config);
497 if (diff < 0) return true;
498 if (diff > 0) return false;
499 return a.value < b.value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 });
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 table->string_pool.Prune();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700502
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 // Write the ResTable header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 ChunkWriter table_writer(buffer_);
505 ResTable_header* table_header =
506 table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE);
507 table_header->packageCount = util::HostToDevice32(table->packages.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700508
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 // Flatten the values string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510 StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 BigBuffer package_buffer(1024);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700513
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 // Flatten each package.
515 for (auto& package : table->packages) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 PackageFlattener flattener(context->GetDiagnostics(), package.get());
517 if (!flattener.FlattenPackage(&package_buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700519 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 // Finally merge all the packages into the main buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 table_writer.buffer()->AppendBuffer(std::move(package_buffer));
524 table_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700526}
527
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528} // namespace aapt