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 "ResourceUtils.h" |
| 19 | #include "ResourceValues.h" |
| 20 | #include "Source.h" |
| 21 | #include "ValueVisitor.h" |
| 22 | |
| 23 | #include "flatten/ResourceTypeExtensions.h" |
| 24 | #include "unflatten/BinaryResourceParser.h" |
| 25 | #include "unflatten/ResChunkPullParser.h" |
| 26 | #include "util/Util.h" |
| 27 | |
| 28 | #include <androidfw/ResourceTypes.h> |
| 29 | #include <androidfw/TypeWrappers.h> |
Elliott Hughes | e7d2cc3 | 2015-12-08 08:57:14 -0800 | [diff] [blame] | 30 | #include <android-base/macros.h> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | |
| 32 | #include <map> |
| 33 | #include <string> |
| 34 | |
| 35 | namespace aapt { |
| 36 | |
| 37 | using namespace android; |
| 38 | |
| 39 | /* |
| 40 | * Visitor that converts a reference's resource ID to a resource name, |
| 41 | * given a mapping from resource ID to resource name. |
| 42 | */ |
| 43 | class ReferenceIdToNameVisitor : public ValueVisitor { |
| 44 | private: |
| 45 | const std::map<ResourceId, ResourceName>* mMapping; |
| 46 | |
| 47 | public: |
| 48 | using ValueVisitor::visit; |
| 49 | |
| 50 | ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceName>* mapping) : |
| 51 | mMapping(mapping) { |
| 52 | assert(mMapping); |
| 53 | } |
| 54 | |
| 55 | void visit(Reference* reference) override { |
| 56 | if (!reference->id || !reference->id.value().isValid()) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | ResourceId id = reference->id.value(); |
| 61 | auto cacheIter = mMapping->find(id); |
| 62 | if (cacheIter != mMapping->end()) { |
| 63 | reference->name = cacheIter->second; |
| 64 | reference->id = {}; |
| 65 | } |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | BinaryResourceParser::BinaryResourceParser(IAaptContext* context, ResourceTable* table, |
| 70 | const Source& source, const void* data, size_t len) : |
| 71 | mContext(context), mTable(table), mSource(source), mData(data), mDataLen(len) { |
| 72 | } |
| 73 | |
| 74 | bool BinaryResourceParser::parse() { |
| 75 | ResChunkPullParser parser(mData, mDataLen); |
| 76 | |
| 77 | bool error = false; |
| 78 | while(ResChunkPullParser::isGoodEvent(parser.next())) { |
| 79 | if (parser.getChunk()->type != android::RES_TABLE_TYPE) { |
| 80 | mContext->getDiagnostics()->warn(DiagMessage(mSource) |
| 81 | << "unknown chunk of type '" |
| 82 | << (int) parser.getChunk()->type << "'"); |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | if (!parseTable(parser.getChunk())) { |
| 87 | error = true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { |
| 92 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 93 | << "corrupt resource table: " |
| 94 | << parser.getLastError()); |
| 95 | return false; |
| 96 | } |
| 97 | return !error; |
| 98 | } |
| 99 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 100 | Maybe<Reference> BinaryResourceParser::getSymbol(const void* data) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 101 | if (!mSymbolEntries || mSymbolEntryCount == 0) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 102 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if ((uintptr_t) data < (uintptr_t) mData) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 106 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // We only support 32 bit offsets right now. |
| 110 | const uintptr_t offset = (uintptr_t) data - (uintptr_t) mData; |
| 111 | if (offset > std::numeric_limits<uint32_t>::max()) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 112 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | for (size_t i = 0; i < mSymbolEntryCount; i++) { |
| 116 | if (util::deviceToHost32(mSymbolEntries[i].offset) == offset) { |
| 117 | // This offset is a symbol! |
| 118 | const StringPiece16 str = util::getString( |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 119 | mSymbolPool, util::deviceToHost32(mSymbolEntries[i].name.index)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 120 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 121 | ResourceNameRef nameRef; |
| 122 | bool privateRef = false; |
| 123 | if (!ResourceUtils::parseResourceName(str, &nameRef, &privateRef)) { |
| 124 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 127 | // Since we scan the symbol table in order, we can start looking for the |
| 128 | // next symbol from this point. |
| 129 | mSymbolEntryCount -= i + 1; |
| 130 | mSymbolEntries += i + 1; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 131 | |
| 132 | Reference ref(nameRef); |
| 133 | ref.privateReference = privateRef; |
| 134 | return Maybe<Reference>(std::move(ref)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 137 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Parses the SymbolTable_header, which is present on non-final resource tables |
| 142 | * after the compile phase. |
| 143 | * |
| 144 | * | SymbolTable_header | |
| 145 | * |--------------------| |
| 146 | * |SymbolTable_entry 0 | |
| 147 | * |SymbolTable_entry 1 | |
| 148 | * | ... | |
| 149 | * |SymbolTable_entry n | |
| 150 | * |--------------------| |
| 151 | * |
| 152 | */ |
| 153 | bool BinaryResourceParser::parseSymbolTable(const ResChunk_header* chunk) { |
| 154 | const SymbolTable_header* header = convertTo<SymbolTable_header>(chunk); |
| 155 | if (!header) { |
| 156 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 157 | << "corrupt SymbolTable_header"); |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | const uint32_t entrySizeBytes = |
| 162 | util::deviceToHost32(header->count) * sizeof(SymbolTable_entry); |
| 163 | if (entrySizeBytes > getChunkDataLen(&header->header)) { |
| 164 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 165 | << "SymbolTable_header data section too long"); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | mSymbolEntries = (const SymbolTable_entry*) getChunkData(&header->header); |
| 170 | mSymbolEntryCount = util::deviceToHost32(header->count); |
| 171 | |
| 172 | // Skip over the symbol entries and parse the StringPool chunk that should be next. |
| 173 | ResChunkPullParser parser(getChunkData(&header->header) + entrySizeBytes, |
| 174 | getChunkDataLen(&header->header) - entrySizeBytes); |
| 175 | if (!ResChunkPullParser::isGoodEvent(parser.next())) { |
| 176 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 177 | << "failed to parse chunk in SymbolTable: " |
| 178 | << parser.getLastError()); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | const ResChunk_header* nextChunk = parser.getChunk(); |
| 183 | if (util::deviceToHost16(nextChunk->type) != android::RES_STRING_POOL_TYPE) { |
| 184 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 185 | << "expected string pool in SymbolTable but got " |
| 186 | << "chunk of type " |
| 187 | << (int) util::deviceToHost16(nextChunk->type)); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | if (mSymbolPool.setTo(nextChunk, util::deviceToHost32(nextChunk->size)) != NO_ERROR) { |
| 192 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 193 | << "corrupt string pool in SymbolTable: " |
| 194 | << mSymbolPool.getError()); |
| 195 | return false; |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Parses the resource table, which contains all the packages, types, and entries. |
| 202 | */ |
| 203 | bool BinaryResourceParser::parseTable(const ResChunk_header* chunk) { |
| 204 | const ResTable_header* tableHeader = convertTo<ResTable_header>(chunk); |
| 205 | if (!tableHeader) { |
| 206 | mContext->getDiagnostics()->error(DiagMessage(mSource) << "corrupt ResTable_header chunk"); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | ResChunkPullParser parser(getChunkData(&tableHeader->header), |
| 211 | getChunkDataLen(&tableHeader->header)); |
| 212 | while (ResChunkPullParser::isGoodEvent(parser.next())) { |
| 213 | switch (util::deviceToHost16(parser.getChunk()->type)) { |
| 214 | case android::RES_STRING_POOL_TYPE: |
| 215 | if (mValuePool.getError() == NO_INIT) { |
| 216 | status_t err = mValuePool.setTo(parser.getChunk(), |
| 217 | util::deviceToHost32(parser.getChunk()->size)); |
| 218 | if (err != NO_ERROR) { |
| 219 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 220 | << "corrupt string pool in ResTable: " |
| 221 | << mValuePool.getError()); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | // Reserve some space for the strings we are going to add. |
| 226 | mTable->stringPool.hintWillAdd(mValuePool.size(), mValuePool.styleCount()); |
| 227 | } else { |
| 228 | mContext->getDiagnostics()->warn(DiagMessage(mSource) |
| 229 | << "unexpected string pool in ResTable"); |
| 230 | } |
| 231 | break; |
| 232 | |
| 233 | case RES_TABLE_SYMBOL_TABLE_TYPE: |
| 234 | if (!parseSymbolTable(parser.getChunk())) { |
| 235 | return false; |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case RES_TABLE_SOURCE_POOL_TYPE: { |
| 240 | status_t err = mSourcePool.setTo(getChunkData(parser.getChunk()), |
| 241 | getChunkDataLen(parser.getChunk())); |
| 242 | if (err != NO_ERROR) { |
| 243 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 244 | << "corrupt source string pool in ResTable: " |
| 245 | << mSourcePool.getError()); |
| 246 | return false; |
| 247 | } |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | case android::RES_TABLE_PACKAGE_TYPE: |
| 252 | if (!parsePackage(parser.getChunk())) { |
| 253 | return false; |
| 254 | } |
| 255 | break; |
| 256 | |
| 257 | default: |
| 258 | mContext->getDiagnostics() |
| 259 | ->warn(DiagMessage(mSource) |
| 260 | << "unexpected chunk type " |
| 261 | << (int) util::deviceToHost16(parser.getChunk()->type)); |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { |
| 267 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 268 | << "corrupt resource table: " << parser.getLastError()); |
| 269 | return false; |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | bool BinaryResourceParser::parsePackage(const ResChunk_header* chunk) { |
| 276 | const ResTable_package* packageHeader = convertTo<ResTable_package>(chunk); |
| 277 | if (!packageHeader) { |
| 278 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 279 | << "corrupt ResTable_package chunk"); |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | uint32_t packageId = util::deviceToHost32(packageHeader->id); |
| 284 | if (packageId > std::numeric_limits<uint8_t>::max()) { |
| 285 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 286 | << "package ID is too big (" << packageId << ")"); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | // Extract the package name. |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 291 | size_t len = strnlen16((const char16_t*) packageHeader->name, arraysize(packageHeader->name)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 292 | std::u16string packageName; |
| 293 | packageName.resize(len); |
| 294 | for (size_t i = 0; i < len; i++) { |
| 295 | packageName[i] = util::deviceToHost16(packageHeader->name[i]); |
| 296 | } |
| 297 | |
| 298 | ResourceTablePackage* package = mTable->createPackage(packageName, (uint8_t) packageId); |
| 299 | if (!package) { |
| 300 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 301 | << "incompatible package '" << packageName |
| 302 | << "' with ID " << packageId); |
| 303 | return false; |
| 304 | } |
| 305 | |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 306 | // There can be multiple packages in a table, so |
| 307 | // clear the type and key pool in case they were set from a previous package. |
| 308 | mTypePool.uninit(); |
| 309 | mKeyPool.uninit(); |
| 310 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 311 | ResChunkPullParser parser(getChunkData(&packageHeader->header), |
| 312 | getChunkDataLen(&packageHeader->header)); |
| 313 | while (ResChunkPullParser::isGoodEvent(parser.next())) { |
| 314 | switch (util::deviceToHost16(parser.getChunk()->type)) { |
| 315 | case android::RES_STRING_POOL_TYPE: |
| 316 | if (mTypePool.getError() == NO_INIT) { |
| 317 | status_t err = mTypePool.setTo(parser.getChunk(), |
| 318 | util::deviceToHost32(parser.getChunk()->size)); |
| 319 | if (err != NO_ERROR) { |
| 320 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 321 | << "corrupt type string pool in " |
| 322 | << "ResTable_package: " |
| 323 | << mTypePool.getError()); |
| 324 | return false; |
| 325 | } |
| 326 | } else if (mKeyPool.getError() == NO_INIT) { |
| 327 | status_t err = mKeyPool.setTo(parser.getChunk(), |
| 328 | util::deviceToHost32(parser.getChunk()->size)); |
| 329 | if (err != NO_ERROR) { |
| 330 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 331 | << "corrupt key string pool in " |
| 332 | << "ResTable_package: " |
| 333 | << mKeyPool.getError()); |
| 334 | return false; |
| 335 | } |
| 336 | } else { |
| 337 | mContext->getDiagnostics()->warn(DiagMessage(mSource) << "unexpected string pool"); |
| 338 | } |
| 339 | break; |
| 340 | |
| 341 | case android::RES_TABLE_TYPE_SPEC_TYPE: |
| 342 | if (!parseTypeSpec(parser.getChunk())) { |
| 343 | return false; |
| 344 | } |
| 345 | break; |
| 346 | |
| 347 | case android::RES_TABLE_TYPE_TYPE: |
| 348 | if (!parseType(package, parser.getChunk())) { |
| 349 | return false; |
| 350 | } |
| 351 | break; |
| 352 | |
| 353 | case RES_TABLE_PUBLIC_TYPE: |
| 354 | if (!parsePublic(package, parser.getChunk())) { |
| 355 | return false; |
| 356 | } |
| 357 | break; |
| 358 | |
| 359 | default: |
| 360 | mContext->getDiagnostics() |
| 361 | ->warn(DiagMessage(mSource) |
| 362 | << "unexpected chunk type " |
| 363 | << (int) util::deviceToHost16(parser.getChunk()->type)); |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { |
| 369 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 370 | << "corrupt ResTable_package: " |
| 371 | << parser.getLastError()); |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | // Now go through the table and change local resource ID references to |
| 376 | // symbolic references. |
| 377 | ReferenceIdToNameVisitor visitor(&mIdIndex); |
| 378 | for (auto& package : mTable->packages) { |
| 379 | for (auto& type : package->types) { |
| 380 | for (auto& entry : type->entries) { |
| 381 | for (auto& configValue : entry->values) { |
| 382 | configValue.value->accept(&visitor); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | bool BinaryResourceParser::parsePublic(const ResourceTablePackage* package, |
| 391 | const ResChunk_header* chunk) { |
| 392 | const Public_header* header = convertTo<Public_header>(chunk); |
| 393 | if (!header) { |
| 394 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 395 | << "corrupt Public_header chunk"); |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | if (header->typeId == 0) { |
| 400 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 401 | << "invalid type ID " |
| 402 | << (int) header->typeId); |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | StringPiece16 typeStr16 = util::getString(mTypePool, header->typeId - 1); |
| 407 | const ResourceType* parsedType = parseResourceType(typeStr16); |
| 408 | if (!parsedType) { |
| 409 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 410 | << "invalid type '" << typeStr16 << "'"); |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | const uintptr_t chunkEnd = (uintptr_t) chunk + util::deviceToHost32(chunk->size); |
| 415 | const Public_entry* entry = (const Public_entry*) getChunkData(&header->header); |
| 416 | for (uint32_t i = 0; i < util::deviceToHost32(header->count); i++) { |
| 417 | if ((uintptr_t) entry + sizeof(*entry) > chunkEnd) { |
| 418 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 419 | << "Public_entry data section is too long"); |
| 420 | return false; |
| 421 | } |
| 422 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 423 | const ResourceId resId(package->id.value(), header->typeId, |
| 424 | util::deviceToHost16(entry->entryId)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 425 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 426 | const ResourceName name(package->name, *parsedType, |
| 427 | util::getString(mKeyPool, entry->key.index).toString()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 428 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 429 | Symbol symbol; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 430 | if (mSourcePool.getError() == NO_ERROR) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 431 | symbol.source.path = util::utf16ToUtf8(util::getString( |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 432 | mSourcePool, util::deviceToHost32(entry->source.path.index))); |
| 433 | symbol.source.line = util::deviceToHost32(entry->source.line); |
| 434 | } |
| 435 | |
| 436 | StringPiece16 comment = util::getString(mSourcePool, |
| 437 | util::deviceToHost32(entry->source.comment.index)); |
| 438 | if (!comment.empty()) { |
| 439 | symbol.comment = comment.toString(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 442 | switch (util::deviceToHost16(entry->state)) { |
| 443 | case Public_entry::kPrivate: |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 444 | symbol.state = SymbolState::kPrivate; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 445 | break; |
| 446 | |
| 447 | case Public_entry::kPublic: |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 448 | symbol.state = SymbolState::kPublic; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 449 | break; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 450 | |
| 451 | case Public_entry::kUndefined: |
| 452 | symbol.state = SymbolState::kUndefined; |
| 453 | break; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 456 | if (!mTable->setSymbolStateAllowMangled(name, resId, symbol, mContext->getDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
| 460 | // Add this resource name->id mapping to the index so |
| 461 | // that we can resolve all ID references to name references. |
| 462 | auto cacheIter = mIdIndex.find(resId); |
| 463 | if (cacheIter == mIdIndex.end()) { |
| 464 | mIdIndex.insert({ resId, name }); |
| 465 | } |
| 466 | |
| 467 | entry++; |
| 468 | } |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | bool BinaryResourceParser::parseTypeSpec(const ResChunk_header* chunk) { |
| 473 | if (mTypePool.getError() != NO_ERROR) { |
| 474 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 475 | << "missing type string pool"); |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | const ResTable_typeSpec* typeSpec = convertTo<ResTable_typeSpec>(chunk); |
| 480 | if (!typeSpec) { |
| 481 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 482 | << "corrupt ResTable_typeSpec chunk"); |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | if (typeSpec->id == 0) { |
| 487 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 488 | << "ResTable_typeSpec has invalid id: " << typeSpec->id); |
| 489 | return false; |
| 490 | } |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | bool BinaryResourceParser::parseType(const ResourceTablePackage* package, |
| 495 | const ResChunk_header* chunk) { |
| 496 | if (mTypePool.getError() != NO_ERROR) { |
| 497 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 498 | << "missing type string pool"); |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | if (mKeyPool.getError() != NO_ERROR) { |
| 503 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 504 | << "missing key string pool"); |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | const ResTable_type* type = convertTo<ResTable_type>(chunk); |
| 509 | if (!type) { |
| 510 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 511 | << "corrupt ResTable_type chunk"); |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | if (type->id == 0) { |
| 516 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 517 | << "ResTable_type has invalid id: " << (int) type->id); |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | ConfigDescription config; |
| 522 | config.copyFromDtoH(type->config); |
| 523 | |
| 524 | StringPiece16 typeStr16 = util::getString(mTypePool, type->id - 1); |
| 525 | |
| 526 | const ResourceType* parsedType = parseResourceType(typeStr16); |
| 527 | if (!parsedType) { |
| 528 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 529 | << "invalid type name '" << typeStr16 |
| 530 | << "' for type with ID " << (int) type->id); |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | TypeVariant tv(type); |
| 535 | for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) { |
| 536 | const ResTable_entry* entry = *it; |
| 537 | if (!entry) { |
| 538 | continue; |
| 539 | } |
| 540 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 541 | const ResourceName name(package->name, *parsedType, |
| 542 | util::getString(mKeyPool, |
| 543 | util::deviceToHost32(entry->key.index)).toString()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 544 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 545 | const ResourceId resId(package->id.value(), type->id, static_cast<uint16_t>(it.index())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 546 | |
| 547 | std::unique_ptr<Value> resourceValue; |
| 548 | const ResTable_entry_source* sourceBlock = nullptr; |
| 549 | |
| 550 | if (entry->flags & ResTable_entry::FLAG_COMPLEX) { |
| 551 | const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry); |
| 552 | if (util::deviceToHost32(mapEntry->size) - sizeof(*mapEntry) == sizeof(*sourceBlock)) { |
| 553 | const uint8_t* data = (const uint8_t*) mapEntry; |
| 554 | data += util::deviceToHost32(mapEntry->size) - sizeof(*sourceBlock); |
| 555 | sourceBlock = (const ResTable_entry_source*) data; |
| 556 | } |
| 557 | |
| 558 | // TODO(adamlesinski): Check that the entry count is valid. |
| 559 | resourceValue = parseMapEntry(name, config, mapEntry); |
| 560 | } else { |
| 561 | if (util::deviceToHost32(entry->size) - sizeof(*entry) == sizeof(*sourceBlock)) { |
| 562 | const uint8_t* data = (const uint8_t*) entry; |
| 563 | data += util::deviceToHost32(entry->size) - sizeof(*sourceBlock); |
| 564 | sourceBlock = (const ResTable_entry_source*) data; |
| 565 | } |
| 566 | |
| 567 | const Res_value* value = (const Res_value*)( |
| 568 | (const uint8_t*) entry + util::deviceToHost32(entry->size)); |
| 569 | resourceValue = parseValue(name, config, value, entry->flags); |
| 570 | } |
| 571 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 572 | if (!resourceValue) { |
| 573 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 574 | << "failed to parse value for resource " << name |
| 575 | << " (" << resId << ") with configuration '" |
| 576 | << config << "'"); |
| 577 | return false; |
| 578 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 579 | |
| 580 | Source source = mSource; |
| 581 | if (sourceBlock) { |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 582 | StringPiece path = util::getString8(mSourcePool, |
| 583 | util::deviceToHost32(sourceBlock->path.index)); |
| 584 | if (!path.empty()) { |
| 585 | source.path = path.toString(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 586 | } |
| 587 | source.line = util::deviceToHost32(sourceBlock->line); |
Adam Lesinski | 24b8ff0 | 2015-12-16 14:01:57 -0800 | [diff] [blame] | 588 | |
| 589 | if (Style* style = valueCast<Style>(resourceValue.get())) { |
| 590 | // The parent's source is the same as the resource itself, set it here. |
| 591 | if (style->parent) { |
| 592 | style->parent.value().setSource(source); |
| 593 | } |
| 594 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 597 | StringPiece16 comment = util::getString(mSourcePool, |
| 598 | util::deviceToHost32(sourceBlock->comment.index)); |
| 599 | if (!comment.empty()) { |
| 600 | resourceValue->setComment(comment); |
| 601 | } |
| 602 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 603 | resourceValue->setSource(source); |
| 604 | if (!mTable->addResourceAllowMangled(name, config, std::move(resourceValue), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 605 | mContext->getDiagnostics())) { |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 610 | Symbol symbol; |
| 611 | symbol.state = SymbolState::kPublic; |
| 612 | symbol.source = mSource.withLine(0); |
| 613 | if (!mTable->setSymbolStateAllowMangled(name, resId, symbol, |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 614 | mContext->getDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // Add this resource name->id mapping to the index so |
| 620 | // that we can resolve all ID references to name references. |
| 621 | auto cacheIter = mIdIndex.find(resId); |
| 622 | if (cacheIter == mIdIndex.end()) { |
| 623 | mIdIndex.insert({ resId, name }); |
| 624 | } |
| 625 | } |
| 626 | return true; |
| 627 | } |
| 628 | |
| 629 | std::unique_ptr<Item> BinaryResourceParser::parseValue(const ResourceNameRef& name, |
| 630 | const ConfigDescription& config, |
| 631 | const Res_value* value, |
| 632 | uint16_t flags) { |
| 633 | if (name.type == ResourceType::kId) { |
| 634 | return util::make_unique<Id>(); |
| 635 | } |
| 636 | |
| 637 | const uint32_t data = util::deviceToHost32(value->data); |
| 638 | |
| 639 | if (value->dataType == Res_value::TYPE_STRING) { |
| 640 | StringPiece16 str = util::getString(mValuePool, data); |
| 641 | |
| 642 | const ResStringPool_span* spans = mValuePool.styleAt(data); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 643 | |
| 644 | // Check if the string has a valid style associated with it. |
| 645 | if (spans != nullptr && spans->name.index != ResStringPool_span::END) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 646 | StyleString styleStr = { str.toString() }; |
| 647 | while (spans->name.index != ResStringPool_span::END) { |
| 648 | styleStr.spans.push_back(Span{ |
| 649 | util::getString(mValuePool, spans->name.index).toString(), |
| 650 | spans->firstChar, |
| 651 | spans->lastChar |
| 652 | }); |
| 653 | spans++; |
| 654 | } |
| 655 | return util::make_unique<StyledString>(mTable->stringPool.makeRef( |
| 656 | styleStr, StringPool::Context{1, config})); |
| 657 | } else { |
| 658 | if (name.type != ResourceType::kString && |
| 659 | util::stringStartsWith<char16_t>(str, u"res/")) { |
| 660 | // This must be a FileReference. |
| 661 | return util::make_unique<FileReference>(mTable->stringPool.makeRef( |
| 662 | str, StringPool::Context{ 0, config })); |
| 663 | } |
| 664 | |
| 665 | // There are no styles associated with this string, so treat it as |
| 666 | // a simple string. |
| 667 | return util::make_unique<String>(mTable->stringPool.makeRef( |
| 668 | str, StringPool::Context{1, config})); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if (value->dataType == Res_value::TYPE_REFERENCE || |
| 673 | value->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 674 | const Reference::Type type = (value->dataType == Res_value::TYPE_REFERENCE) ? |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 675 | Reference::Type::kResource : Reference::Type::kAttribute; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 676 | |
| 677 | if (data != 0) { |
| 678 | // This is a normal reference. |
| 679 | return util::make_unique<Reference>(data, type); |
| 680 | } |
| 681 | |
| 682 | // This reference has an invalid ID. Check if it is an unresolved symbol. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 683 | if (Maybe<Reference> ref = getSymbol(&value->data)) { |
| 684 | ref.value().referenceType = type; |
| 685 | return util::make_unique<Reference>(std::move(ref.value())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // This is not an unresolved symbol, so it must be the magic @null reference. |
| 689 | Res_value nullType = {}; |
| 690 | nullType.dataType = Res_value::TYPE_REFERENCE; |
| 691 | return util::make_unique<BinaryPrimitive>(nullType); |
| 692 | } |
| 693 | |
| 694 | if (value->dataType == ExtendedTypes::TYPE_RAW_STRING) { |
| 695 | return util::make_unique<RawString>(mTable->stringPool.makeRef( |
| 696 | util::getString(mValuePool, data), StringPool::Context{ 1, config })); |
| 697 | } |
| 698 | |
| 699 | // Treat this as a raw binary primitive. |
| 700 | return util::make_unique<BinaryPrimitive>(*value); |
| 701 | } |
| 702 | |
| 703 | std::unique_ptr<Value> BinaryResourceParser::parseMapEntry(const ResourceNameRef& name, |
| 704 | const ConfigDescription& config, |
| 705 | const ResTable_map_entry* map) { |
| 706 | switch (name.type) { |
| 707 | case ResourceType::kStyle: |
| 708 | return parseStyle(name, config, map); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 709 | case ResourceType::kAttrPrivate: |
| 710 | // fallthrough |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 711 | case ResourceType::kAttr: |
| 712 | return parseAttr(name, config, map); |
| 713 | case ResourceType::kArray: |
| 714 | return parseArray(name, config, map); |
| 715 | case ResourceType::kStyleable: |
| 716 | return parseStyleable(name, config, map); |
| 717 | case ResourceType::kPlurals: |
| 718 | return parsePlural(name, config, map); |
| 719 | default: |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 720 | assert(false && "unknown map type"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | return {}; |
| 724 | } |
| 725 | |
| 726 | std::unique_ptr<Style> BinaryResourceParser::parseStyle(const ResourceNameRef& name, |
| 727 | const ConfigDescription& config, |
| 728 | const ResTable_map_entry* map) { |
| 729 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
| 730 | if (util::deviceToHost32(map->parent.ident) == 0) { |
| 731 | // The parent is either not set or it is an unresolved symbol. |
| 732 | // Check to see if it is a symbol. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 733 | style->parent = getSymbol(&map->parent.ident); |
| 734 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 735 | } else { |
| 736 | // The parent is a regular reference to a resource. |
| 737 | style->parent = Reference(util::deviceToHost32(map->parent.ident)); |
| 738 | } |
| 739 | |
| 740 | for (const ResTable_map& mapEntry : map) { |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 741 | if (Res_INTERNALID(util::deviceToHost32(mapEntry.name.ident))) { |
| 742 | if (style->entries.empty()) { |
| 743 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 744 | << "out-of-sequence meta data in style"); |
| 745 | return {}; |
| 746 | } |
| 747 | collectMetaData(mapEntry, &style->entries.back().key); |
| 748 | continue; |
| 749 | } |
| 750 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 751 | style->entries.emplace_back(); |
| 752 | Style::Entry& styleEntry = style->entries.back(); |
| 753 | |
| 754 | if (util::deviceToHost32(mapEntry.name.ident) == 0) { |
| 755 | // The map entry's key (attribute) is not set. This must be |
| 756 | // a symbol reference, so resolve it. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 757 | Maybe<Reference> symbol = getSymbol(&mapEntry.name.ident); |
| 758 | if (!symbol) { |
| 759 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 760 | << "unresolved style attribute"); |
| 761 | return {}; |
| 762 | } |
| 763 | styleEntry.key = std::move(symbol.value()); |
| 764 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 765 | } else { |
| 766 | // The map entry's key (attribute) is a regular reference. |
| 767 | styleEntry.key.id = ResourceId(util::deviceToHost32(mapEntry.name.ident)); |
| 768 | } |
| 769 | |
| 770 | // Parse the attribute's value. |
| 771 | styleEntry.value = parseValue(name, config, &mapEntry.value, 0); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 772 | if (!styleEntry.value) { |
| 773 | return {}; |
| 774 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 775 | } |
| 776 | return style; |
| 777 | } |
| 778 | |
| 779 | std::unique_ptr<Attribute> BinaryResourceParser::parseAttr(const ResourceNameRef& name, |
| 780 | const ConfigDescription& config, |
| 781 | const ResTable_map_entry* map) { |
| 782 | const bool isWeak = (util::deviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0; |
| 783 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(isWeak); |
| 784 | |
| 785 | // First we must discover what type of attribute this is. Find the type mask. |
| 786 | auto typeMaskIter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool { |
| 787 | return util::deviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE; |
| 788 | }); |
| 789 | |
| 790 | if (typeMaskIter != end(map)) { |
| 791 | attr->typeMask = util::deviceToHost32(typeMaskIter->value.data); |
| 792 | } |
| 793 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 794 | for (const ResTable_map& mapEntry : map) { |
| 795 | if (Res_INTERNALID(util::deviceToHost32(mapEntry.name.ident))) { |
| 796 | switch (util::deviceToHost32(mapEntry.name.ident)) { |
| 797 | case ResTable_map::ATTR_MIN: |
| 798 | attr->minInt = static_cast<int32_t>(mapEntry.value.data); |
| 799 | break; |
| 800 | case ResTable_map::ATTR_MAX: |
| 801 | attr->maxInt = static_cast<int32_t>(mapEntry.value.data); |
| 802 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 803 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 804 | continue; |
| 805 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 806 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 807 | if (attr->typeMask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 808 | Attribute::Symbol symbol; |
| 809 | symbol.value = util::deviceToHost32(mapEntry.value.data); |
| 810 | if (util::deviceToHost32(mapEntry.name.ident) == 0) { |
| 811 | // The map entry's key (id) is not set. This must be |
| 812 | // a symbol reference, so resolve it. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 813 | Maybe<Reference> ref = getSymbol(&mapEntry.name.ident); |
| 814 | if (!ref) { |
| 815 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 816 | << "unresolved attribute symbol"); |
| 817 | return {}; |
| 818 | } |
| 819 | symbol.symbol = std::move(ref.value()); |
| 820 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 821 | } else { |
| 822 | // The map entry's key (id) is a regular reference. |
| 823 | symbol.symbol.id = ResourceId(util::deviceToHost32(mapEntry.name.ident)); |
| 824 | } |
| 825 | |
| 826 | attr->symbols.push_back(std::move(symbol)); |
| 827 | } |
| 828 | } |
| 829 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 830 | // TODO(adamlesinski): Find i80n, attributes. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 831 | return attr; |
| 832 | } |
| 833 | |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 834 | static bool isMetaDataEntry(const ResTable_map& mapEntry) { |
| 835 | switch (util::deviceToHost32(mapEntry.name.ident)) { |
| 836 | case ExtendedResTableMapTypes::ATTR_SOURCE_PATH: |
| 837 | case ExtendedResTableMapTypes::ATTR_SOURCE_LINE: |
| 838 | case ExtendedResTableMapTypes::ATTR_COMMENT: |
| 839 | return true; |
| 840 | } |
| 841 | return false; |
| 842 | } |
| 843 | |
| 844 | bool BinaryResourceParser::collectMetaData(const ResTable_map& mapEntry, Value* value) { |
| 845 | switch (util::deviceToHost32(mapEntry.name.ident)) { |
| 846 | case ExtendedResTableMapTypes::ATTR_SOURCE_PATH: |
| 847 | value->setSource(Source(util::getString8(mSourcePool, |
| 848 | util::deviceToHost32(mapEntry.value.data)))); |
| 849 | return true; |
| 850 | break; |
| 851 | |
| 852 | case ExtendedResTableMapTypes::ATTR_SOURCE_LINE: |
| 853 | value->setSource(value->getSource().withLine(util::deviceToHost32(mapEntry.value.data))); |
| 854 | return true; |
| 855 | break; |
| 856 | |
| 857 | case ExtendedResTableMapTypes::ATTR_COMMENT: |
| 858 | value->setComment(util::getString(mSourcePool, util::deviceToHost32(mapEntry.value.data))); |
| 859 | return true; |
| 860 | break; |
| 861 | } |
| 862 | return false; |
| 863 | } |
| 864 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 865 | std::unique_ptr<Array> BinaryResourceParser::parseArray(const ResourceNameRef& name, |
| 866 | const ConfigDescription& config, |
| 867 | const ResTable_map_entry* map) { |
| 868 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 869 | Source source; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 870 | for (const ResTable_map& mapEntry : map) { |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 871 | if (isMetaDataEntry(mapEntry)) { |
| 872 | if (array->items.empty()) { |
| 873 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 874 | << "out-of-sequence meta data in array"); |
| 875 | return {}; |
| 876 | } |
| 877 | collectMetaData(mapEntry, array->items.back().get()); |
| 878 | continue; |
| 879 | } |
| 880 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 881 | array->items.push_back(parseValue(name, config, &mapEntry.value, 0)); |
| 882 | } |
| 883 | return array; |
| 884 | } |
| 885 | |
| 886 | std::unique_ptr<Styleable> BinaryResourceParser::parseStyleable(const ResourceNameRef& name, |
| 887 | const ConfigDescription& config, |
| 888 | const ResTable_map_entry* map) { |
| 889 | std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>(); |
| 890 | for (const ResTable_map& mapEntry : map) { |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 891 | if (isMetaDataEntry(mapEntry)) { |
| 892 | if (styleable->entries.empty()) { |
| 893 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 894 | << "out-of-sequence meta data in styleable"); |
| 895 | return {}; |
| 896 | } |
| 897 | collectMetaData(mapEntry, &styleable->entries.back()); |
| 898 | continue; |
| 899 | } |
| 900 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 901 | if (util::deviceToHost32(mapEntry.name.ident) == 0) { |
| 902 | // The map entry's key (attribute) is not set. This must be |
| 903 | // a symbol reference, so resolve it. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 904 | Maybe<Reference> ref = getSymbol(&mapEntry.name.ident); |
| 905 | if (!ref) { |
| 906 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 907 | << "unresolved styleable symbol"); |
| 908 | return {}; |
| 909 | } |
| 910 | styleable->entries.emplace_back(std::move(ref.value())); |
| 911 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 912 | } else { |
| 913 | // The map entry's key (attribute) is a regular reference. |
| 914 | styleable->entries.emplace_back(util::deviceToHost32(mapEntry.name.ident)); |
| 915 | } |
| 916 | } |
| 917 | return styleable; |
| 918 | } |
| 919 | |
| 920 | std::unique_ptr<Plural> BinaryResourceParser::parsePlural(const ResourceNameRef& name, |
| 921 | const ConfigDescription& config, |
| 922 | const ResTable_map_entry* map) { |
| 923 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 924 | Item* lastEntry = nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 925 | for (const ResTable_map& mapEntry : map) { |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 926 | if (isMetaDataEntry(mapEntry)) { |
| 927 | if (!lastEntry) { |
| 928 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 929 | << "out-of-sequence meta data in plural"); |
| 930 | return {}; |
| 931 | } |
| 932 | collectMetaData(mapEntry, lastEntry); |
| 933 | continue; |
| 934 | } |
| 935 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 936 | std::unique_ptr<Item> item = parseValue(name, config, &mapEntry.value, 0); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 937 | if (!item) { |
| 938 | return {}; |
| 939 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 940 | |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 941 | lastEntry = item.get(); |
| 942 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 943 | switch (util::deviceToHost32(mapEntry.name.ident)) { |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 944 | case ResTable_map::ATTR_ZERO: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 945 | plural->values[Plural::Zero] = std::move(item); |
| 946 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 947 | case ResTable_map::ATTR_ONE: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 948 | plural->values[Plural::One] = std::move(item); |
| 949 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 950 | case ResTable_map::ATTR_TWO: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 951 | plural->values[Plural::Two] = std::move(item); |
| 952 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 953 | case ResTable_map::ATTR_FEW: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 954 | plural->values[Plural::Few] = std::move(item); |
| 955 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 956 | case ResTable_map::ATTR_MANY: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 957 | plural->values[Plural::Many] = std::move(item); |
| 958 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 959 | case ResTable_map::ATTR_OTHER: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 960 | plural->values[Plural::Other] = std::move(item); |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | return plural; |
| 965 | } |
| 966 | |
| 967 | } // namespace aapt |