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