blob: 32924215ab4e6694cd8a874fd0f5567e85990b0c [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"
Adam Lesinski1210e8c2017-11-07 17:08:07 -080026#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027
28#include "ResourceTable.h"
29#include "ResourceValues.h"
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080030#include "SdkConstants.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031#include "ValueVisitor.h"
32#include "flatten/ChunkWriter.h"
33#include "flatten/ResourceTypeExtensions.h"
34#include "util/BigBuffer.h"
35
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036using namespace android;
37
38namespace aapt {
39
40namespace {
41
42template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043static bool cmp_ids(const T* a, const T* b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 return a->id.value() < b->id.value();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045}
46
47static void strcpy16_htod(uint16_t* dst, size_t len, const StringPiece16& src) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 if (len == 0) {
49 return;
50 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 size_t i;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 const char16_t* src_data = src.data();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 for (i = 0; i < len - 1 && i < src.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 dst[i] = util::HostToDevice16((uint16_t)src_data[i]);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 }
57 dst[i] = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058}
59
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060static bool cmp_style_entries(const Style::Entry& a, const Style::Entry& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 if (a.key.id) {
62 if (b.key.id) {
63 return a.key.id.value() < b.key.id.value();
64 }
65 return true;
66 } else if (!b.key.id) {
67 return a.key.name.value() < b.key.name.value();
68 }
69 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -080070}
71
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072struct FlatEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ResourceEntry* entry;
74 Value* value;
Adam Lesinski3b4cd942015-10-30 16:31:42 -070075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 // The entry string pool index to the entry's name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 uint32_t entry_key;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078};
79
Adam Lesinski59e04c62016-02-04 15:59:23 -080080class MapFlattenVisitor : public RawValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 using RawValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 MapFlattenVisitor(ResTable_entry_ext* out_entry, BigBuffer* buffer)
85 : out_entry_(out_entry), buffer_(buffer) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 void Visit(Attribute* attr) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 {
89 Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->type_mask);
91 FlattenEntry(&key, &val);
Adam Lesinski28cacf02015-11-23 14:22:47 -080092 }
93
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 if (attr->min_int != std::numeric_limits<int32_t>::min()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN));
96 BinaryPrimitive val(Res_value::TYPE_INT_DEC,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 static_cast<uint32_t>(attr->min_int));
98 FlattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099 }
100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 if (attr->max_int != std::numeric_limits<int32_t>::max()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX));
103 BinaryPrimitive val(Res_value::TYPE_INT_DEC,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 static_cast<uint32_t>(attr->max_int));
105 FlattenEntry(&key, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106 }
107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 for (Attribute::Symbol& s : attr->symbols) {
109 BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 FlattenEntry(&s.symbol, &val);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 }
112 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 void Visit(Style* style) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 if (style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 const Reference& parent_ref = style->parent.value();
117 CHECK(bool(parent_ref.id)) << "parent has no ID";
118 out_entry_->parent.ident = util::HostToDevice32(parent_ref.id.value().id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119 }
120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 // Sort the style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 std::sort(style->entries.begin(), style->entries.end(), cmp_style_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123
124 for (Style::Entry& entry : style->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 FlattenEntry(&entry.key, entry.value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 void Visit(Styleable* styleable) override {
130 for (auto& attr_ref : styleable->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 BinaryPrimitive val(Res_value{});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 FlattenEntry(&attr_ref, &val);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 void Visit(Array* array) override {
Adam Lesinskib7917212017-08-10 15:37:28 -0700137 for (auto& item : array->elements) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
139 FlattenValue(item.get(), out_entry);
140 out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
141 entry_count_++;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800142 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 void Visit(Plural* plural) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 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:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 LOG(FATAL) << "unhandled plural type";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 break;
181 }
182
183 Reference key(q);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 FlattenEntry(&key, plural->values[i].get());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800185 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800187
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 /**
189 * Call this after visiting a Value. This will finish any work that
190 * needs to be done to prepare the entry.
191 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 void Finish() { out_entry_->count = util::HostToDevice32(entry_count_); }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800193
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor);
196
197 void FlattenKey(Reference* key, ResTable_map* out_entry) {
198 CHECK(bool(key->id)) << "key has no ID";
199 out_entry->name.ident = util::HostToDevice32(key->id.value().id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 void FlattenValue(Item* value, ResTable_map* out_entry) {
203 CHECK(value->Flatten(&out_entry->value)) << "flatten failed";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 }
205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 void FlattenEntry(Reference* key, Item* value) {
207 ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
208 FlattenKey(key, out_entry);
209 FlattenValue(value, out_entry);
210 out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
211 entry_count_++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 }
213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 ResTable_entry_ext* out_entry_;
215 BigBuffer* buffer_;
216 size_t entry_count_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217};
218
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700219class PackageFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 public:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800221 PackageFlattener(IAaptContext* context, ResourceTablePackage* package,
222 const std::map<size_t, std::string>* shared_libs, bool use_sparse_entries)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800223 : context_(context),
224 diag_(context->GetDiagnostics()),
225 package_(package),
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800226 shared_libs_(shared_libs),
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800227 use_sparse_entries_(use_sparse_entries) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 bool FlattenPackage(BigBuffer* buffer) {
230 ChunkWriter pkg_writer(buffer);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800231 ResTable_package* pkg_header = pkg_writer.StartChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 pkg_header->id = util::HostToDevice32(package_->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233
Adam Lesinskib522f042017-04-21 16:57:59 -0700234 // AAPT truncated the package name, so do the same.
235 // Shared libraries require full package names, so don't truncate theirs.
236 if (context_->GetPackageType() != PackageType::kApp &&
237 package_->name.size() >= arraysize(pkg_header->name)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 diag_->Error(DiagMessage() << "package name '" << package_->name
Adam Lesinskib522f042017-04-21 16:57:59 -0700239 << "' is too long. "
240 "Shared libraries cannot have truncated package names");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700242 }
243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 // Copy the package name in device endianness.
Adam Lesinskib522f042017-04-21 16:57:59 -0700245 strcpy16_htod(pkg_header->name, arraysize(pkg_header->name), util::Utf8ToUtf16(package_->name));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700246
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 // Serialize the types. We do this now so that our type and key strings
248 // are populated. We write those first.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 BigBuffer type_buffer(1024);
250 FlattenTypes(&type_buffer);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700251
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size());
253 StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size());
256 StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700257
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 // Append the types.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 buffer->AppendBuffer(std::move(type_buffer));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700260
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800261 // If there are libraries (or if the package ID is 0x00), encode a library chunk.
262 if (package_->id.value() == 0x00 || !shared_libs_->empty()) {
263 FlattenLibrarySpec(buffer);
264 }
265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 pkg_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 return true;
268 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700269
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 DISALLOW_COPY_AND_ASSIGN(PackageFlattener);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 template <typename T, bool IsItem>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 T* WriteEntry(FlatEntry* entry, BigBuffer* buffer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 static_assert(std::is_same<ResTable_entry, T>::value ||
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700276 std::is_same<ResTable_entry_ext, T>::value,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 "T must be ResTable_entry or ResTable_entry_ext");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 T* result = buffer->NextBlock<T>();
280 ResTable_entry* out_entry = (ResTable_entry*)result;
281 if (entry->entry->symbol_status.state == SymbolState::kPublic) {
282 out_entry->flags |= ResTable_entry::FLAG_PUBLIC;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 }
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 if (entry->value->IsWeak()) {
286 out_entry->flags |= ResTable_entry::FLAG_WEAK;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700287 }
288
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 if (!IsItem) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 out_entry->flags |= ResTable_entry::FLAG_COMPLEX;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700291 }
292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 out_entry->flags = util::HostToDevice16(out_entry->flags);
294 out_entry->key.index = util::HostToDevice32(entry->entry_key);
295 out_entry->size = util::HostToDevice16(sizeof(T));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return result;
297 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 bool FlattenValue(FlatEntry* entry, BigBuffer* buffer) {
300 if (Item* item = ValueCast<Item>(entry->value)) {
301 WriteEntry<ResTable_entry, true>(entry, buffer);
302 Res_value* outValue = buffer->NextBlock<Res_value>();
303 CHECK(item->Flatten(outValue)) << "flatten failed";
304 outValue->size = util::HostToDevice16(sizeof(*outValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 ResTable_entry_ext* out_entry =
307 WriteEntry<ResTable_entry_ext, false>(entry, buffer);
308 MapFlattenVisitor visitor(out_entry, buffer);
309 entry->value->Accept(&visitor);
310 visitor.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 }
312 return true;
313 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800315 bool FlattenConfig(const ResourceTableType* type, const ConfigDescription& config,
316 const size_t num_total_entries, std::vector<FlatEntry>* entries,
317 BigBuffer* buffer) {
318 CHECK(num_total_entries != 0);
319 CHECK(num_total_entries <= std::numeric_limits<uint16_t>::max());
320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 ChunkWriter type_writer(buffer);
322 ResTable_type* type_header =
323 type_writer.StartChunk<ResTable_type>(RES_TABLE_TYPE_TYPE);
324 type_header->id = type->id.value();
325 type_header->config = config;
326 type_header->config.swapHtoD();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800328 std::vector<uint32_t> offsets;
329 offsets.resize(num_total_entries, 0xffffffffu);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800331 BigBuffer values_buffer(512);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 for (FlatEntry& flat_entry : *entries) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800333 CHECK(static_cast<size_t>(flat_entry.entry->id.value()) < num_total_entries);
334 offsets[flat_entry.entry->id.value()] = values_buffer.size();
335 if (!FlattenValue(&flat_entry, &values_buffer)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 diag_->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 << "failed to flatten resource '"
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800338 << ResourceNameRef(package_->name, type->type, flat_entry.entry->name)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 << "' for configuration '" << config << "'");
340 return false;
341 }
342 }
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800343
344 bool sparse_encode = use_sparse_entries_;
345
346 // Only sparse encode if the entries will be read on platforms O+.
347 sparse_encode =
348 sparse_encode && (context_->GetMinSdkVersion() >= SDK_O || config.sdkVersion >= SDK_O);
349
350 // Only sparse encode if the offsets are representable in 2 bytes.
351 sparse_encode =
352 sparse_encode && (values_buffer.size() / 4u) <= std::numeric_limits<uint16_t>::max();
353
354 // Only sparse encode if the ratio of populated entries to total entries is below some
355 // threshold.
356 sparse_encode =
357 sparse_encode && ((100 * entries->size()) / num_total_entries) < kSparseEncodingThreshold;
358
359 if (sparse_encode) {
360 type_header->entryCount = util::HostToDevice32(entries->size());
361 type_header->flags |= ResTable_type::FLAG_SPARSE;
362 ResTable_sparseTypeEntry* indices =
363 type_writer.NextBlock<ResTable_sparseTypeEntry>(entries->size());
364 for (size_t i = 0; i < num_total_entries; i++) {
365 if (offsets[i] != ResTable_type::NO_ENTRY) {
366 CHECK((offsets[i] & 0x03) == 0);
367 indices->idx = util::HostToDevice16(i);
368 indices->offset = util::HostToDevice16(offsets[i] / 4u);
369 indices++;
370 }
371 }
372 } else {
373 type_header->entryCount = util::HostToDevice32(num_total_entries);
374 uint32_t* indices = type_writer.NextBlock<uint32_t>(num_total_entries);
375 for (size_t i = 0; i < num_total_entries; i++) {
376 indices[i] = util::HostToDevice32(offsets[i]);
377 }
378 }
379
380 type_header->entriesStart = util::HostToDevice32(type_writer.size());
381 type_writer.buffer()->AppendBuffer(std::move(values_buffer));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 type_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 return true;
384 }
385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 std::vector<ResourceTableType*> CollectAndSortTypes() {
387 std::vector<ResourceTableType*> sorted_types;
388 for (auto& type : package_->types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 if (type->type == ResourceType::kStyleable) {
390 // Styleables aren't real Resource Types, they are represented in the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 // R.java file.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 continue;
393 }
394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 CHECK(bool(type->id)) << "type must have an ID set";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 sorted_types.push_back(type.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 std::sort(sorted_types.begin(), sorted_types.end(),
400 cmp_ids<ResourceTableType>);
401 return sorted_types;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 }
403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 std::vector<ResourceEntry*> CollectAndSortEntries(ResourceTableType* type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 // Sort the entries by entry ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 std::vector<ResourceEntry*> sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 CHECK(bool(entry->id)) << "entry must have an ID set";
409 sorted_entries.push_back(entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 }
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800411 std::sort(sorted_entries.begin(), sorted_entries.end(), cmp_ids<ResourceEntry>);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 return sorted_entries;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 }
414
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 bool FlattenTypeSpec(ResourceTableType* type,
416 std::vector<ResourceEntry*>* sorted_entries,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 BigBuffer* buffer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 ChunkWriter type_spec_writer(buffer);
419 ResTable_typeSpec* spec_header =
420 type_spec_writer.StartChunk<ResTable_typeSpec>(
421 RES_TABLE_TYPE_SPEC_TYPE);
422 spec_header->id = type->id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 if (sorted_entries->empty()) {
425 type_spec_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700427 }
428
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 // We can't just take the size of the vector. There may be holes in the
430 // entry ID space.
431 // Since the entries are sorted by ID, the last one will be the biggest.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 const size_t num_entries = sorted_entries->back()->id.value() + 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 spec_header->entryCount = util::HostToDevice32(num_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435
436 // Reserve space for the masks of each resource in this type. These
437 // show for which configuration axis the resource changes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 uint32_t* config_masks = type_spec_writer.NextBlock<uint32_t>(num_entries);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 const size_t actual_num_entries = sorted_entries->size();
441 for (size_t entryIndex = 0; entryIndex < actual_num_entries; entryIndex++) {
442 ResourceEntry* entry = sorted_entries->at(entryIndex);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443
444 // Populate the config masks for this entry.
445
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 if (entry->symbol_status.state == SymbolState::kPublic) {
447 config_masks[entry->id.value()] |=
448 util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 }
450
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 const size_t config_count = entry->values.size();
452 for (size_t i = 0; i < config_count; i++) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 const ConfigDescription& config = entry->values[i]->config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 for (size_t j = i + 1; j < config_count; j++) {
455 config_masks[entry->id.value()] |=
456 util::HostToDevice32(config.diff(entry->values[j]->config));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700457 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700459 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 type_spec_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 return true;
462 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 bool FlattenTypes(BigBuffer* buffer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 // Sort the types by their IDs. They will be inserted into the StringPool in
466 // this order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 std::vector<ResourceTableType*> sorted_types = CollectAndSortTypes();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700468
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 size_t expected_type_id = 1;
470 for (ResourceTableType* type : sorted_types) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 // If there is a gap in the type IDs, fill in the StringPool
472 // with empty values until we reach the ID we expect.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 while (type->id.value() > expected_type_id) {
474 std::stringstream type_name;
475 type_name << "?" << expected_type_id;
476 type_pool_.MakeRef(type_name.str());
477 expected_type_id++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 expected_type_id++;
480 type_pool_.MakeRef(ToString(type->type));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 std::vector<ResourceEntry*> sorted_entries = CollectAndSortEntries(type);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800483 if (sorted_entries.empty()) {
484 continue;
485 }
486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 if (!FlattenTypeSpec(type, &sorted_entries, buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 return false;
489 }
490
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800491 // Since the entries are sorted by ID, the last ID will be the largest.
492 const size_t num_entries = sorted_entries.back()->id.value() + 1;
493
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 // The binary resource table lists resource entries for each
495 // configuration.
496 // We store them inverted, where a resource entry lists the values for
497 // each
498 // configuration available. Here we reverse this to match the binary
499 // table.
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800500 std::map<ConfigDescription, std::vector<FlatEntry>> config_to_entry_list_map;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 for (ResourceEntry* entry : sorted_entries) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800502 const uint32_t key_index = (uint32_t)key_pool_.MakeRef(entry->name).index();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503
504 // Group values by configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 for (auto& config_value : entry->values) {
506 config_to_entry_list_map[config_value->config].push_back(
507 FlatEntry{entry, config_value->value.get(), key_index});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700508 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700510
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 // Flatten a configuration value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 for (auto& entry : config_to_entry_list_map) {
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800513 if (!FlattenConfig(type, entry.first, num_entries, &entry.second, buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700515 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700517 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 return true;
519 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800521 void FlattenLibrarySpec(BigBuffer* buffer) {
522 ChunkWriter lib_writer(buffer);
523 ResTable_lib_header* lib_header =
524 lib_writer.StartChunk<ResTable_lib_header>(RES_TABLE_LIBRARY_TYPE);
525
526 const size_t num_entries = (package_->id.value() == 0x00 ? 1 : 0) + shared_libs_->size();
527 CHECK(num_entries > 0);
528
529 lib_header->count = util::HostToDevice32(num_entries);
530
531 ResTable_lib_entry* lib_entry = buffer->NextBlock<ResTable_lib_entry>(num_entries);
532 if (package_->id.value() == 0x00) {
533 // Add this package
534 lib_entry->packageId = util::HostToDevice32(0x00);
535 strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
536 util::Utf8ToUtf16(package_->name));
537 ++lib_entry;
538 }
539
540 for (auto& map_entry : *shared_libs_) {
541 lib_entry->packageId = util::HostToDevice32(map_entry.first);
542 strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
543 util::Utf8ToUtf16(map_entry.second));
544 ++lib_entry;
545 }
546 lib_writer.Finish();
547 }
548
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800549 IAaptContext* context_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700550 IDiagnostics* diag_;
551 ResourceTablePackage* package_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800552 const std::map<size_t, std::string>* shared_libs_;
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800553 bool use_sparse_entries_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554 StringPool type_pool_;
555 StringPool key_pool_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700556};
557
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700561 // We must do this before writing the resources, since the string pool IDs may change.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 table->string_pool.Prune();
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700563 table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
564 int diff = util::compare(a.priority, b.priority);
565 if (diff == 0) {
566 diff = a.config.compare(b.config);
567 }
568 return diff;
569 });
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700570
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 // Write the ResTable header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 ChunkWriter table_writer(buffer_);
Adam Lesinski4ca56972017-04-26 21:49:53 -0700573 ResTable_header* table_header = table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 table_header->packageCount = util::HostToDevice32(table->packages.size());
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700575
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 // Flatten the values string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700578
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 BigBuffer package_buffer(1024);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700580
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 // Flatten each package.
582 for (auto& package : table->packages) {
Adam Lesinski1210e8c2017-11-07 17:08:07 -0800583 if (context->GetPackageType() == PackageType::kApp) {
584 // Write a self mapping entry for this package if the ID is non-standard (0x7f).
585 const uint8_t package_id = package->id.value();
586 if (package_id != kFrameworkPackageId && package_id != kAppPackageId) {
587 auto result = table->included_packages_.insert({package_id, package->name});
588 if (!result.second && result.first->second != package->name) {
589 // A mapping for this package ID already exists, and is a different package. Error!
590 context->GetDiagnostics()->Error(
591 DiagMessage() << android::base::StringPrintf(
592 "can't map package ID %02x to '%s'. Already mapped to '%s'", package_id,
593 package->name.c_str(), result.first->second.c_str()));
594 return false;
595 }
596 }
597 }
598
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800599 PackageFlattener flattener(context, package.get(), &table->included_packages_,
600 options_.use_sparse_entries);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 if (!flattener.FlattenPackage(&package_buffer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700605
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 // Finally merge all the packages into the main buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 table_writer.buffer()->AppendBuffer(std::move(package_buffer));
608 table_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700610}
611
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612} // namespace aapt