Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "ResourceTable.h" |
| 18 | #include "ResourceValues.h" |
| 19 | #include "ValueVisitor.h" |
| 20 | |
| 21 | #include "flatten/ChunkWriter.h" |
| 22 | #include "flatten/ResourceTypeExtensions.h" |
| 23 | #include "flatten/TableFlattener.h" |
| 24 | #include "util/BigBuffer.h" |
| 25 | |
Elliott Hughes | e7d2cc3 | 2015-12-08 08:57:14 -0800 | [diff] [blame] | 26 | #include <android-base/macros.h> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | #include <type_traits> |
| 28 | #include <numeric> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | namespace aapt { |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | template <typename T> |
| 37 | static bool cmpIds(const T* a, const T* b) { |
| 38 | return a->id.value() < b->id.value(); |
| 39 | } |
| 40 | |
| 41 | static void strcpy16_htod(uint16_t* dst, size_t len, const StringPiece16& src) { |
| 42 | if (len == 0) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | size_t i; |
| 47 | const char16_t* srcData = src.data(); |
| 48 | for (i = 0; i < len - 1 && i < src.size(); i++) { |
| 49 | dst[i] = util::hostToDevice16((uint16_t) srcData[i]); |
| 50 | } |
| 51 | dst[i] = 0; |
| 52 | } |
| 53 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 54 | static bool cmpStyleEntries(const Style::Entry& a, const Style::Entry& b) { |
| 55 | if (a.key.id) { |
| 56 | if (b.key.id) { |
| 57 | return a.key.id.value() < b.key.id.value(); |
| 58 | } |
| 59 | return true; |
| 60 | } else if (!b.key.id) { |
| 61 | return a.key.name.value() < b.key.name.value(); |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | struct FlatEntry { |
| 67 | ResourceEntry* entry; |
| 68 | Value* value; |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 69 | |
| 70 | // The entry string pool index to the entry's name. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | uint32_t entryKey; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 74 | class MapFlattenVisitor : public RawValueVisitor { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 75 | public: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | using RawValueVisitor::visit; |
| 77 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 78 | MapFlattenVisitor(ResTable_entry_ext* outEntry, BigBuffer* buffer) : |
| 79 | mOutEntry(outEntry), mBuffer(buffer) { |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 82 | void visit(Attribute* attr) override { |
| 83 | { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 84 | Reference key = Reference(ResTable_map::ATTR_TYPE); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 85 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->typeMask); |
| 86 | flattenEntry(&key, &val); |
| 87 | } |
| 88 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 89 | if (attr->minInt != std::numeric_limits<int32_t>::min()) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 90 | Reference key = Reference(ResTable_map::ATTR_MIN); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 91 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->minInt)); |
| 92 | flattenEntry(&key, &val); |
| 93 | } |
| 94 | |
| 95 | if (attr->maxInt != std::numeric_limits<int32_t>::max()) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 96 | Reference key = Reference(ResTable_map::ATTR_MAX); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 97 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->maxInt)); |
| 98 | flattenEntry(&key, &val); |
| 99 | } |
| 100 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 101 | for (Attribute::Symbol& s : attr->symbols) { |
| 102 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value); |
| 103 | flattenEntry(&s.symbol, &val); |
| 104 | } |
| 105 | } |
| 106 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | void visit(Style* style) override { |
| 108 | if (style->parent) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 109 | const Reference& parentRef = style->parent.value(); |
| 110 | assert(parentRef.id && "parent has no ID"); |
| 111 | mOutEntry->parent.ident = util::hostToDevice32(parentRef.id.value().id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Sort the style. |
| 115 | std::sort(style->entries.begin(), style->entries.end(), cmpStyleEntries); |
| 116 | |
| 117 | for (Style::Entry& entry : style->entries) { |
| 118 | flattenEntry(&entry.key, entry.value.get()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void visit(Styleable* styleable) override { |
| 123 | for (auto& attrRef : styleable->entries) { |
| 124 | BinaryPrimitive val(Res_value{}); |
| 125 | flattenEntry(&attrRef, &val); |
| 126 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 127 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void visit(Array* array) override { |
| 131 | for (auto& item : array->items) { |
| 132 | ResTable_map* outEntry = mBuffer->nextBlock<ResTable_map>(); |
| 133 | flattenValue(item.get(), outEntry); |
| 134 | outEntry->value.size = util::hostToDevice16(sizeof(outEntry->value)); |
| 135 | mEntryCount++; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void visit(Plural* plural) override { |
| 140 | const size_t count = plural->values.size(); |
| 141 | for (size_t i = 0; i < count; i++) { |
| 142 | if (!plural->values[i]) { |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | ResourceId q; |
| 147 | switch (i) { |
| 148 | case Plural::Zero: |
| 149 | q.id = android::ResTable_map::ATTR_ZERO; |
| 150 | break; |
| 151 | |
| 152 | case Plural::One: |
| 153 | q.id = android::ResTable_map::ATTR_ONE; |
| 154 | break; |
| 155 | |
| 156 | case Plural::Two: |
| 157 | q.id = android::ResTable_map::ATTR_TWO; |
| 158 | break; |
| 159 | |
| 160 | case Plural::Few: |
| 161 | q.id = android::ResTable_map::ATTR_FEW; |
| 162 | break; |
| 163 | |
| 164 | case Plural::Many: |
| 165 | q.id = android::ResTable_map::ATTR_MANY; |
| 166 | break; |
| 167 | |
| 168 | case Plural::Other: |
| 169 | q.id = android::ResTable_map::ATTR_OTHER; |
| 170 | break; |
| 171 | |
| 172 | default: |
| 173 | assert(false); |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | Reference key(q); |
| 178 | flattenEntry(&key, plural->values[i].get()); |
| 179 | } |
| 180 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 181 | |
| 182 | /** |
| 183 | * Call this after visiting a Value. This will finish any work that |
| 184 | * needs to be done to prepare the entry. |
| 185 | */ |
| 186 | void finish() { |
| 187 | mOutEntry->count = util::hostToDevice32(mEntryCount); |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | void flattenKey(Reference* key, ResTable_map* outEntry) { |
| 192 | assert(key->id && "key has no ID"); |
| 193 | outEntry->name.ident = util::hostToDevice32(key->id.value().id); |
| 194 | } |
| 195 | |
| 196 | void flattenValue(Item* value, ResTable_map* outEntry) { |
| 197 | bool result = value->flatten(&outEntry->value); |
| 198 | assert(result && "flatten failed"); |
| 199 | } |
| 200 | |
| 201 | void flattenEntry(Reference* key, Item* value) { |
| 202 | ResTable_map* outEntry = mBuffer->nextBlock<ResTable_map>(); |
| 203 | flattenKey(key, outEntry); |
| 204 | flattenValue(value, outEntry); |
| 205 | outEntry->value.size = util::hostToDevice16(sizeof(outEntry->value)); |
| 206 | mEntryCount++; |
| 207 | } |
| 208 | |
| 209 | ResTable_entry_ext* mOutEntry; |
| 210 | BigBuffer* mBuffer; |
| 211 | size_t mEntryCount = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 214 | class PackageFlattener { |
| 215 | public: |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 216 | PackageFlattener(IDiagnostics* diag, ResourceTablePackage* package) : |
| 217 | mDiag(diag), mPackage(package) { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | bool flattenPackage(BigBuffer* buffer) { |
| 221 | ChunkWriter pkgWriter(buffer); |
| 222 | ResTable_package* pkgHeader = pkgWriter.startChunk<ResTable_package>( |
| 223 | RES_TABLE_PACKAGE_TYPE); |
| 224 | pkgHeader->id = util::hostToDevice32(mPackage->id.value()); |
| 225 | |
| 226 | if (mPackage->name.size() >= arraysize(pkgHeader->name)) { |
| 227 | mDiag->error(DiagMessage() << |
| 228 | "package name '" << mPackage->name << "' is too long"); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | // Copy the package name in device endianness. |
| 233 | strcpy16_htod(pkgHeader->name, arraysize(pkgHeader->name), mPackage->name); |
| 234 | |
| 235 | // Serialize the types. We do this now so that our type and key strings |
| 236 | // are populated. We write those first. |
| 237 | BigBuffer typeBuffer(1024); |
| 238 | flattenTypes(&typeBuffer); |
| 239 | |
| 240 | pkgHeader->typeStrings = util::hostToDevice32(pkgWriter.size()); |
| 241 | StringPool::flattenUtf16(pkgWriter.getBuffer(), mTypePool); |
| 242 | |
| 243 | pkgHeader->keyStrings = util::hostToDevice32(pkgWriter.size()); |
| 244 | StringPool::flattenUtf16(pkgWriter.getBuffer(), mKeyPool); |
| 245 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 246 | // Append the types. |
| 247 | buffer->appendBuffer(std::move(typeBuffer)); |
| 248 | |
| 249 | pkgWriter.finish(); |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | private: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 254 | IDiagnostics* mDiag; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 255 | ResourceTablePackage* mPackage; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 256 | StringPool mTypePool; |
| 257 | StringPool mKeyPool; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 258 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 259 | template <typename T, bool IsItem> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 260 | T* writeEntry(FlatEntry* entry, BigBuffer* buffer) { |
| 261 | static_assert(std::is_same<ResTable_entry, T>::value || |
| 262 | std::is_same<ResTable_entry_ext, T>::value, |
| 263 | "T must be ResTable_entry or ResTable_entry_ext"); |
| 264 | |
| 265 | T* result = buffer->nextBlock<T>(); |
| 266 | ResTable_entry* outEntry = (ResTable_entry*)(result); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 267 | if (entry->entry->symbolStatus.state == SymbolState::kPublic) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 268 | outEntry->flags |= ResTable_entry::FLAG_PUBLIC; |
| 269 | } |
| 270 | |
| 271 | if (entry->value->isWeak()) { |
| 272 | outEntry->flags |= ResTable_entry::FLAG_WEAK; |
| 273 | } |
| 274 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 275 | if (!IsItem) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 276 | outEntry->flags |= ResTable_entry::FLAG_COMPLEX; |
| 277 | } |
| 278 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 279 | outEntry->flags = util::hostToDevice16(outEntry->flags); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 280 | outEntry->key.index = util::hostToDevice32(entry->entryKey); |
| 281 | outEntry->size = util::hostToDevice16(sizeof(T)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 282 | return result; |
| 283 | } |
| 284 | |
| 285 | bool flattenValue(FlatEntry* entry, BigBuffer* buffer) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 286 | if (Item* item = valueCast<Item>(entry->value)) { |
| 287 | writeEntry<ResTable_entry, true>(entry, buffer); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 288 | Res_value* outValue = buffer->nextBlock<Res_value>(); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 289 | bool result = item->flatten(outValue); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 290 | assert(result && "flatten failed"); |
| 291 | outValue->size = util::hostToDevice16(sizeof(*outValue)); |
| 292 | } else { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 293 | ResTable_entry_ext* outEntry = writeEntry<ResTable_entry_ext, false>(entry, buffer); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 294 | MapFlattenVisitor visitor(outEntry, buffer); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 295 | entry->value->accept(&visitor); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 296 | visitor.finish(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 297 | } |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | bool flattenConfig(const ResourceTableType* type, const ConfigDescription& config, |
| 302 | std::vector<FlatEntry>* entries, BigBuffer* buffer) { |
| 303 | ChunkWriter typeWriter(buffer); |
| 304 | ResTable_type* typeHeader = typeWriter.startChunk<ResTable_type>(RES_TABLE_TYPE_TYPE); |
| 305 | typeHeader->id = type->id.value(); |
| 306 | typeHeader->config = config; |
| 307 | typeHeader->config.swapHtoD(); |
| 308 | |
| 309 | auto maxAccum = [](uint32_t max, const std::unique_ptr<ResourceEntry>& a) -> uint32_t { |
| 310 | return std::max(max, (uint32_t) a->id.value()); |
| 311 | }; |
| 312 | |
| 313 | // Find the largest entry ID. That is how many entries we will have. |
| 314 | const uint32_t entryCount = |
| 315 | std::accumulate(type->entries.begin(), type->entries.end(), 0, maxAccum) + 1; |
| 316 | |
| 317 | typeHeader->entryCount = util::hostToDevice32(entryCount); |
| 318 | uint32_t* indices = typeWriter.nextBlock<uint32_t>(entryCount); |
| 319 | |
| 320 | assert((size_t) entryCount <= std::numeric_limits<uint16_t>::max() + 1); |
| 321 | memset(indices, 0xff, entryCount * sizeof(uint32_t)); |
| 322 | |
| 323 | typeHeader->entriesStart = util::hostToDevice32(typeWriter.size()); |
| 324 | |
| 325 | const size_t entryStart = typeWriter.getBuffer()->size(); |
| 326 | for (FlatEntry& flatEntry : *entries) { |
| 327 | assert(flatEntry.entry->id.value() < entryCount); |
| 328 | indices[flatEntry.entry->id.value()] = util::hostToDevice32( |
| 329 | typeWriter.getBuffer()->size() - entryStart); |
| 330 | if (!flattenValue(&flatEntry, typeWriter.getBuffer())) { |
| 331 | mDiag->error(DiagMessage() |
| 332 | << "failed to flatten resource '" |
| 333 | << ResourceNameRef(mPackage->name, type->type, flatEntry.entry->name) |
| 334 | << "' for configuration '" << config << "'"); |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | typeWriter.finish(); |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | std::vector<ResourceTableType*> collectAndSortTypes() { |
| 343 | std::vector<ResourceTableType*> sortedTypes; |
| 344 | for (auto& type : mPackage->types) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 345 | if (type->type == ResourceType::kStyleable) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 346 | // Styleables aren't real Resource Types, they are represented in the R.java |
| 347 | // file. |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | assert(type->id && "type must have an ID set"); |
| 352 | |
| 353 | sortedTypes.push_back(type.get()); |
| 354 | } |
| 355 | std::sort(sortedTypes.begin(), sortedTypes.end(), cmpIds<ResourceTableType>); |
| 356 | return sortedTypes; |
| 357 | } |
| 358 | |
| 359 | std::vector<ResourceEntry*> collectAndSortEntries(ResourceTableType* type) { |
| 360 | // Sort the entries by entry ID. |
| 361 | std::vector<ResourceEntry*> sortedEntries; |
| 362 | for (auto& entry : type->entries) { |
| 363 | assert(entry->id && "entry must have an ID set"); |
| 364 | sortedEntries.push_back(entry.get()); |
| 365 | } |
| 366 | std::sort(sortedEntries.begin(), sortedEntries.end(), cmpIds<ResourceEntry>); |
| 367 | return sortedEntries; |
| 368 | } |
| 369 | |
| 370 | bool flattenTypeSpec(ResourceTableType* type, std::vector<ResourceEntry*>* sortedEntries, |
| 371 | BigBuffer* buffer) { |
| 372 | ChunkWriter typeSpecWriter(buffer); |
| 373 | ResTable_typeSpec* specHeader = typeSpecWriter.startChunk<ResTable_typeSpec>( |
| 374 | RES_TABLE_TYPE_SPEC_TYPE); |
| 375 | specHeader->id = type->id.value(); |
| 376 | |
| 377 | if (sortedEntries->empty()) { |
| 378 | typeSpecWriter.finish(); |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | // We can't just take the size of the vector. There may be holes in the entry ID space. |
| 383 | // Since the entries are sorted by ID, the last one will be the biggest. |
| 384 | const size_t numEntries = sortedEntries->back()->id.value() + 1; |
| 385 | |
| 386 | specHeader->entryCount = util::hostToDevice32(numEntries); |
| 387 | |
| 388 | // Reserve space for the masks of each resource in this type. These |
| 389 | // show for which configuration axis the resource changes. |
| 390 | uint32_t* configMasks = typeSpecWriter.nextBlock<uint32_t>(numEntries); |
| 391 | |
| 392 | const size_t actualNumEntries = sortedEntries->size(); |
| 393 | for (size_t entryIndex = 0; entryIndex < actualNumEntries; entryIndex++) { |
| 394 | ResourceEntry* entry = sortedEntries->at(entryIndex); |
| 395 | |
| 396 | // Populate the config masks for this entry. |
| 397 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 398 | if (entry->symbolStatus.state == SymbolState::kPublic) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 399 | configMasks[entry->id.value()] |= |
| 400 | util::hostToDevice32(ResTable_typeSpec::SPEC_PUBLIC); |
| 401 | } |
| 402 | |
| 403 | const size_t configCount = entry->values.size(); |
| 404 | for (size_t i = 0; i < configCount; i++) { |
| 405 | const ConfigDescription& config = entry->values[i].config; |
| 406 | for (size_t j = i + 1; j < configCount; j++) { |
| 407 | configMasks[entry->id.value()] |= util::hostToDevice32( |
| 408 | config.diff(entry->values[j].config)); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | typeSpecWriter.finish(); |
| 413 | return true; |
| 414 | } |
| 415 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 416 | bool flattenTypes(BigBuffer* buffer) { |
| 417 | // Sort the types by their IDs. They will be inserted into the StringPool in this order. |
| 418 | std::vector<ResourceTableType*> sortedTypes = collectAndSortTypes(); |
| 419 | |
| 420 | size_t expectedTypeId = 1; |
| 421 | for (ResourceTableType* type : sortedTypes) { |
| 422 | // If there is a gap in the type IDs, fill in the StringPool |
| 423 | // with empty values until we reach the ID we expect. |
| 424 | while (type->id.value() > expectedTypeId) { |
| 425 | std::u16string typeName(u"?"); |
| 426 | typeName += expectedTypeId; |
| 427 | mTypePool.makeRef(typeName); |
| 428 | expectedTypeId++; |
| 429 | } |
| 430 | expectedTypeId++; |
| 431 | mTypePool.makeRef(toString(type->type)); |
| 432 | |
| 433 | std::vector<ResourceEntry*> sortedEntries = collectAndSortEntries(type); |
| 434 | |
| 435 | if (!flattenTypeSpec(type, &sortedEntries, buffer)) { |
| 436 | return false; |
| 437 | } |
| 438 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 439 | // The binary resource table lists resource entries for each configuration. |
| 440 | // We store them inverted, where a resource entry lists the values for each |
| 441 | // configuration available. Here we reverse this to match the binary table. |
| 442 | std::map<ConfigDescription, std::vector<FlatEntry>> configToEntryListMap; |
| 443 | for (ResourceEntry* entry : sortedEntries) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 444 | const uint32_t keyIndex = (uint32_t) mKeyPool.makeRef(entry->name).getIndex(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 445 | |
| 446 | // Group values by configuration. |
| 447 | for (auto& configValue : entry->values) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 448 | configToEntryListMap[configValue.config].push_back(FlatEntry{ |
| 449 | entry, configValue.value.get(), keyIndex }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | // Flatten a configuration value. |
| 454 | for (auto& entry : configToEntryListMap) { |
| 455 | if (!flattenConfig(type, entry.first, &entry.second, buffer)) { |
| 456 | return false; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | return true; |
| 461 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
| 464 | } // namespace |
| 465 | |
| 466 | bool TableFlattener::consume(IAaptContext* context, ResourceTable* table) { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 467 | // We must do this before writing the resources, since the string pool IDs may change. |
| 468 | table->stringPool.sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool { |
| 469 | int diff = a.context.priority - b.context.priority; |
| 470 | if (diff < 0) return true; |
| 471 | if (diff > 0) return false; |
| 472 | diff = a.context.config.compare(b.context.config); |
| 473 | if (diff < 0) return true; |
| 474 | if (diff > 0) return false; |
| 475 | return a.value < b.value; |
| 476 | }); |
| 477 | table->stringPool.prune(); |
| 478 | |
| 479 | // Write the ResTable header. |
| 480 | ChunkWriter tableWriter(mBuffer); |
| 481 | ResTable_header* tableHeader = tableWriter.startChunk<ResTable_header>(RES_TABLE_TYPE); |
| 482 | tableHeader->packageCount = util::hostToDevice32(table->packages.size()); |
| 483 | |
| 484 | // Flatten the values string pool. |
| 485 | StringPool::flattenUtf8(tableWriter.getBuffer(), table->stringPool); |
| 486 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 487 | BigBuffer packageBuffer(1024); |
| 488 | |
| 489 | // Flatten each package. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 490 | for (auto& package : table->packages) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 491 | PackageFlattener flattener(context->getDiagnostics(), package.get()); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 492 | if (!flattener.flattenPackage(&packageBuffer)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 493 | return false; |
| 494 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 497 | // Finally merge all the packages into the main buffer. |
| 498 | tableWriter.getBuffer()->appendBuffer(std::move(packageBuffer)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 499 | tableWriter.finish(); |
| 500 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | } // namespace aapt |