blob: 697b07fa414db73ab5c5d798b89f989b4ce73f4b [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"
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080029#include "SdkConstants.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030#include "ValueVisitor.h"
31#include "flatten/ChunkWriter.h"
32#include "flatten/ResourceTypeExtensions.h"
33#include "util/BigBuffer.h"
34
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035using namespace android;
36
37namespace aapt {
38
39namespace {
40
41template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042static bool cmp_ids(const T* a, const T* b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 return a->id.value() < b->id.value();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044}
45
46static void strcpy16_htod(uint16_t* dst, size_t len, const StringPiece16& src) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 if (len == 0) {
48 return;
49 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 size_t i;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 const char16_t* src_data = src.data();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 for (i = 0; i < len - 1 && i < src.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 dst[i] = util::HostToDevice16((uint16_t)src_data[i]);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 }
56 dst[i] = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057}
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059static bool cmp_style_entries(const Style::Entry& a, const Style::Entry& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 if (a.key.id) {
61 if (b.key.id) {
62 return a.key.id.value() < b.key.id.value();
63 }
64 return true;
65 } else if (!b.key.id) {
66 return a.key.name.value() < b.key.name.value();
67 }
68 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -080069}
70
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071struct FlatEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 ResourceEntry* entry;
73 Value* value;
Adam Lesinski3b4cd942015-10-30 16:31:42 -070074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 // The entry string pool index to the entry's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 uint32_t entry_key;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077};
78
Adam Lesinski59e04c62016-02-04 15:59:23 -080079class MapFlattenVisitor : public RawValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 using RawValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 MapFlattenVisitor(ResTable_entry_ext* out_entry, BigBuffer* buffer)
84 : out_entry_(out_entry), buffer_(buffer) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 void Visit(Attribute* attr) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 {
88 Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->type_mask);
90 FlattenEntry(&key, &val);
Adam Lesinski28cacf02015-11-23 14:22:47 -080091 }
92
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 if (attr->min_int != std::numeric_limits<int32_t>::min()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN));
95 BinaryPrimitive val(Res_value::TYPE_INT_DEC,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 static_cast<uint32_t>(attr->min_int));
97 FlattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098 }
99
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 if (attr->max_int != std::numeric_limits<int32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX));
102 BinaryPrimitive val(Res_value::TYPE_INT_DEC,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 static_cast<uint32_t>(attr->max_int));
104 FlattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105 }
106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 for (Attribute::Symbol& s : attr->symbols) {
108 BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 FlattenEntry(&s.symbol, &val);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 }
111 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 void Visit(Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 if (style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 const Reference& parent_ref = style->parent.value();
116 CHECK(bool(parent_ref.id)) << "parent has no ID";
117 out_entry_->parent.ident = util::HostToDevice32(parent_ref.id.value().id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118 }
119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 // Sort the style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 std::sort(style->entries.begin(), style->entries.end(), cmp_style_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122
123 for (Style::Entry& entry : style->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 FlattenEntry(&entry.key, entry.value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 void Visit(Styleable* styleable) override {
129 for (auto& attr_ref : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 BinaryPrimitive val(Res_value{});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 FlattenEntry(&attr_ref, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 void Visit(Array* array) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 for (auto& item : array->items) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
138 FlattenValue(item.get(), out_entry);
139 out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
140 entry_count_++;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800141 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 void Visit(Plural* plural) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 const size_t count = plural->values.size();
146 for (size_t i = 0; i < count; i++) {
147 if (!plural->values[i]) {
148 continue;
149 }
150
151 ResourceId q;
152 switch (i) {
153 case Plural::Zero:
154 q.id = android::ResTable_map::ATTR_ZERO;
155 break;
156
157 case Plural::One:
158 q.id = android::ResTable_map::ATTR_ONE;
159 break;
160
161 case Plural::Two:
162 q.id = android::ResTable_map::ATTR_TWO;
163 break;
164
165 case Plural::Few:
166 q.id = android::ResTable_map::ATTR_FEW;
167 break;
168
169 case Plural::Many:
170 q.id = android::ResTable_map::ATTR_MANY;
171 break;
172
173 case Plural::Other:
174 q.id = android::ResTable_map::ATTR_OTHER;
175 break;
176
177 default:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 LOG(FATAL) << "unhandled plural type";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 break;
180 }
181
182 Reference key(q);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 FlattenEntry(&key, plural->values[i].get());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800184 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 /**
188 * Call this after visiting a Value. This will finish any work that
189 * needs to be done to prepare the entry.
190 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 void Finish() { out_entry_->count = util::HostToDevice32(entry_count_); }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800192
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor);
195
196 void FlattenKey(Reference* key, ResTable_map* out_entry) {
197 CHECK(bool(key->id)) << "key has no ID";
198 out_entry->name.ident = util::HostToDevice32(key->id.value().id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 void FlattenValue(Item* value, ResTable_map* out_entry) {
202 CHECK(value->Flatten(&out_entry->value)) << "flatten failed";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 void FlattenEntry(Reference* key, Item* value) {
206 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
207 FlattenKey(key, out_entry);
208 FlattenValue(value, out_entry);
209 out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
210 entry_count_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 }
212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 ResTable_entry_ext* out_entry_;
214 BigBuffer* buffer_;
215 size_t entry_count_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700216};
217
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700218class PackageFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 public:
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800220 PackageFlattener(IAaptContext* context, ResourceTablePackage* package, bool use_sparse_entries)
221 : context_(context),
222 diag_(context->GetDiagnostics()),
223 package_(package),
224 use_sparse_entries_(use_sparse_entries) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 bool FlattenPackage(BigBuffer* buffer) {
227 ChunkWriter pkg_writer(buffer);
228 ResTable_package* pkg_header =
229 pkg_writer.StartChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE);
230 pkg_header->id = util::HostToDevice32(package_->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 if (package_->name.size() >= arraysize(pkg_header->name)) {
233 diag_->Error(DiagMessage() << "package name '" << package_->name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 << "' is too long");
235 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700236 }
237
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 // Copy the package name in device endianness.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 strcpy16_htod(pkg_header->name, arraysize(pkg_header->name),
240 util::Utf8ToUtf16(package_->name));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 // Serialize the types. We do this now so that our type and key strings
243 // are populated. We write those first.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 BigBuffer type_buffer(1024);
245 FlattenTypes(&type_buffer);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size());
248 StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700249
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size());
251 StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700252
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 // Append the types.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 buffer->AppendBuffer(std::move(type_buffer));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 pkg_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 return true;
258 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700259
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 DISALLOW_COPY_AND_ASSIGN(PackageFlattener);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700262
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 template <typename T, bool IsItem>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 T* WriteEntry(FlatEntry* entry, BigBuffer* buffer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 static_assert(std::is_same<ResTable_entry, T>::value ||
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700266 std::is_same<ResTable_entry_ext, T>::value,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 "T must be ResTable_entry or ResTable_entry_ext");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 T* result = buffer->NextBlock<T>();
270 ResTable_entry* out_entry = (ResTable_entry*)result;
271 if (entry->entry->symbol_status.state == SymbolState::kPublic) {
272 out_entry->flags |= ResTable_entry::FLAG_PUBLIC;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700273 }
274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 if (entry->value->IsWeak()) {
276 out_entry->flags |= ResTable_entry::FLAG_WEAK;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700277 }
278
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 if (!IsItem) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 out_entry->flags |= ResTable_entry::FLAG_COMPLEX;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281 }
282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 out_entry->flags = util::HostToDevice16(out_entry->flags);
284 out_entry->key.index = util::HostToDevice32(entry->entry_key);
285 out_entry->size = util::HostToDevice16(sizeof(T));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 return result;
287 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700288
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 bool FlattenValue(FlatEntry* entry, BigBuffer* buffer) {
290 if (Item* item = ValueCast<Item>(entry->value)) {
291 WriteEntry<ResTable_entry, true>(entry, buffer);
292 Res_value* outValue = buffer->NextBlock<Res_value>();
293 CHECK(item->Flatten(outValue)) << "flatten failed";
294 outValue->size = util::HostToDevice16(sizeof(*outValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 ResTable_entry_ext* out_entry =
297 WriteEntry<ResTable_entry_ext, false>(entry, buffer);
298 MapFlattenVisitor visitor(out_entry, buffer);
299 entry->value->Accept(&visitor);
300 visitor.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 }
302 return true;
303 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800305 bool FlattenConfig(const ResourceTableType* type, const ConfigDescription& config,
306 const size_t num_total_entries, std::vector<FlatEntry>* entries,
307 BigBuffer* buffer) {
308 CHECK(num_total_entries != 0);
309 CHECK(num_total_entries <= std::numeric_limits<uint16_t>::max());
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 ChunkWriter type_writer(buffer);
312 ResTable_type* type_header =
313 type_writer.StartChunk<ResTable_type>(RES_TABLE_TYPE_TYPE);
314 type_header->id = type->id.value();
315 type_header->config = config;
316 type_header->config.swapHtoD();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800318 std::vector<uint32_t> offsets;
319 offsets.resize(num_total_entries, 0xffffffffu);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800321 BigBuffer values_buffer(512);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 for (FlatEntry& flat_entry : *entries) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800323 CHECK(static_cast<size_t>(flat_entry.entry->id.value()) < num_total_entries);
324 offsets[flat_entry.entry->id.value()] = values_buffer.size();
325 if (!FlattenValue(&flat_entry, &values_buffer)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 diag_->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 << "failed to flatten resource '"
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800328 << ResourceNameRef(package_->name, type->type, flat_entry.entry->name)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 << "' for configuration '" << config << "'");
330 return false;
331 }
332 }
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800333
334 bool sparse_encode = use_sparse_entries_;
335
336 // Only sparse encode if the entries will be read on platforms O+.
337 sparse_encode =
338 sparse_encode && (context_->GetMinSdkVersion() >= SDK_O || config.sdkVersion >= SDK_O);
339
340 // Only sparse encode if the offsets are representable in 2 bytes.
341 sparse_encode =
342 sparse_encode && (values_buffer.size() / 4u) <= std::numeric_limits<uint16_t>::max();
343
344 // Only sparse encode if the ratio of populated entries to total entries is below some
345 // threshold.
346 sparse_encode =
347 sparse_encode && ((100 * entries->size()) / num_total_entries) < kSparseEncodingThreshold;
348
349 if (sparse_encode) {
350 type_header->entryCount = util::HostToDevice32(entries->size());
351 type_header->flags |= ResTable_type::FLAG_SPARSE;
352 ResTable_sparseTypeEntry* indices =
353 type_writer.NextBlock<ResTable_sparseTypeEntry>(entries->size());
354 for (size_t i = 0; i < num_total_entries; i++) {
355 if (offsets[i] != ResTable_type::NO_ENTRY) {
356 CHECK((offsets[i] & 0x03) == 0);
357 indices->idx = util::HostToDevice16(i);
358 indices->offset = util::HostToDevice16(offsets[i] / 4u);
359 indices++;
360 }
361 }
362 } else {
363 type_header->entryCount = util::HostToDevice32(num_total_entries);
364 uint32_t* indices = type_writer.NextBlock<uint32_t>(num_total_entries);
365 for (size_t i = 0; i < num_total_entries; i++) {
366 indices[i] = util::HostToDevice32(offsets[i]);
367 }
368 }
369
370 type_header->entriesStart = util::HostToDevice32(type_writer.size());
371 type_writer.buffer()->AppendBuffer(std::move(values_buffer));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 type_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 return true;
374 }
375
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 std::vector<ResourceTableType*> CollectAndSortTypes() {
377 std::vector<ResourceTableType*> sorted_types;
378 for (auto& type : package_->types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379 if (type->type == ResourceType::kStyleable) {
380 // Styleables aren't real Resource Types, they are represented in the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 // R.java file.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 continue;
383 }
384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 CHECK(bool(type->id)) << "type must have an ID set";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 sorted_types.push_back(type.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389 std::sort(sorted_types.begin(), sorted_types.end(),
390 cmp_ids<ResourceTableType>);
391 return sorted_types;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 }
393
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 std::vector<ResourceEntry*> CollectAndSortEntries(ResourceTableType* type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 // Sort the entries by entry ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 std::vector<ResourceEntry*> sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 CHECK(bool(entry->id)) << "entry must have an ID set";
399 sorted_entries.push_back(entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 }
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800401 std::sort(sorted_entries.begin(), sorted_entries.end(), cmp_ids<ResourceEntry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 return sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 }
404
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 bool FlattenTypeSpec(ResourceTableType* type,
406 std::vector<ResourceEntry*>* sorted_entries,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 BigBuffer* buffer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 ChunkWriter type_spec_writer(buffer);
409 ResTable_typeSpec* spec_header =
410 type_spec_writer.StartChunk<ResTable_typeSpec>(
411 RES_TABLE_TYPE_SPEC_TYPE);
412 spec_header->id = type->id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 if (sorted_entries->empty()) {
415 type_spec_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700417 }
418
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 // We can't just take the size of the vector. There may be holes in the
420 // entry ID space.
421 // Since the entries are sorted by ID, the last one will be the biggest.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 const size_t num_entries = sorted_entries->back()->id.value() + 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 spec_header->entryCount = util::HostToDevice32(num_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425
426 // Reserve space for the masks of each resource in this type. These
427 // show for which configuration axis the resource changes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 uint32_t* config_masks = type_spec_writer.NextBlock<uint32_t>(num_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 const size_t actual_num_entries = sorted_entries->size();
431 for (size_t entryIndex = 0; entryIndex < actual_num_entries; entryIndex++) {
432 ResourceEntry* entry = sorted_entries->at(entryIndex);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433
434 // Populate the config masks for this entry.
435
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 if (entry->symbol_status.state == SymbolState::kPublic) {
437 config_masks[entry->id.value()] |=
438 util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 }
440
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 const size_t config_count = entry->values.size();
442 for (size_t i = 0; i < config_count; i++) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 const ConfigDescription& config = entry->values[i]->config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 for (size_t j = i + 1; j < config_count; j++) {
445 config_masks[entry->id.value()] |=
446 util::HostToDevice32(config.diff(entry->values[j]->config));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700447 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700449 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 type_spec_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 return true;
452 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 bool FlattenTypes(BigBuffer* buffer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 // Sort the types by their IDs. They will be inserted into the StringPool in
456 // this order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 std::vector<ResourceTableType*> sorted_types = CollectAndSortTypes();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700458
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 size_t expected_type_id = 1;
460 for (ResourceTableType* type : sorted_types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 // If there is a gap in the type IDs, fill in the StringPool
462 // with empty values until we reach the ID we expect.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 while (type->id.value() > expected_type_id) {
464 std::stringstream type_name;
465 type_name << "?" << expected_type_id;
466 type_pool_.MakeRef(type_name.str());
467 expected_type_id++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 expected_type_id++;
470 type_pool_.MakeRef(ToString(type->type));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 std::vector<ResourceEntry*> sorted_entries = CollectAndSortEntries(type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 if (!FlattenTypeSpec(type, &sorted_entries, buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 return false;
475 }
476
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800477 // Since the entries are sorted by ID, the last ID will be the largest.
478 const size_t num_entries = sorted_entries.back()->id.value() + 1;
479
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 // The binary resource table lists resource entries for each
481 // configuration.
482 // We store them inverted, where a resource entry lists the values for
483 // each
484 // configuration available. Here we reverse this to match the binary
485 // table.
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800486 std::map<ConfigDescription, std::vector<FlatEntry>> config_to_entry_list_map;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 for (ResourceEntry* entry : sorted_entries) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800488 const uint32_t key_index = (uint32_t)key_pool_.MakeRef(entry->name).index();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489
490 // Group values by configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 for (auto& config_value : entry->values) {
492 config_to_entry_list_map[config_value->config].push_back(
493 FlatEntry{entry, config_value->value.get(), key_index});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700494 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700496
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 // Flatten a configuration value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700498 for (auto& entry : config_to_entry_list_map) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800499 if (!FlattenConfig(type, entry.first, num_entries, &entry.second, buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700501 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 return true;
505 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800507 IAaptContext* context_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 IDiagnostics* diag_;
509 ResourceTablePackage* package_;
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800510 bool use_sparse_entries_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 StringPool type_pool_;
512 StringPool key_pool_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700513};
514
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700516
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 // We must do this before writing the resources, since the string pool IDs may
519 // change.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 table->string_pool.Sort(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700522 int diff = a.context.priority - b.context.priority;
523 if (diff < 0) return true;
524 if (diff > 0) return false;
525 diff = a.context.config.compare(b.context.config);
526 if (diff < 0) return true;
527 if (diff > 0) return false;
528 return a.value < b.value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 });
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 table->string_pool.Prune();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700531
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 // Write the ResTable header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 ChunkWriter table_writer(buffer_);
534 ResTable_header* table_header =
535 table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE);
536 table_header->packageCount = util::HostToDevice32(table->packages.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700537
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 // Flatten the values string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 BigBuffer package_buffer(1024);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700542
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 // Flatten each package.
544 for (auto& package : table->packages) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800545 PackageFlattener flattener(context, package.get(), options_.use_sparse_entries);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 if (!flattener.FlattenPackage(&package_buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700548 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700550
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 // Finally merge all the packages into the main buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 table_writer.buffer()->AppendBuffer(std::move(package_buffer));
553 table_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700555}
556
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557} // namespace aapt