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" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include "unflatten/BinaryResourceParser.h" |
| 23 | #include "unflatten/ResChunkPullParser.h" |
| 24 | #include "util/Util.h" |
| 25 | |
| 26 | #include <androidfw/ResourceTypes.h> |
| 27 | #include <androidfw/TypeWrappers.h> |
Elliott Hughes | e7d2cc3 | 2015-12-08 08:57:14 -0800 | [diff] [blame] | 28 | #include <android-base/macros.h> |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 29 | #include <algorithm> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include <map> |
| 31 | #include <string> |
| 32 | |
| 33 | namespace aapt { |
| 34 | |
| 35 | using namespace android; |
| 36 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 37 | namespace { |
| 38 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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 | |
Chih-Hung Hsieh | d53e3be | 2016-05-03 10:02:51 -0700 | [diff] [blame] | 50 | explicit ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceName>* mapping) : |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | }; |
| 67 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 68 | } // namespace |
| 69 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | BinaryResourceParser::BinaryResourceParser(IAaptContext* context, ResourceTable* table, |
| 71 | const Source& source, const void* data, size_t len) : |
| 72 | mContext(context), mTable(table), mSource(source), mData(data), mDataLen(len) { |
| 73 | } |
| 74 | |
| 75 | bool BinaryResourceParser::parse() { |
| 76 | ResChunkPullParser parser(mData, mDataLen); |
| 77 | |
| 78 | bool error = false; |
| 79 | while(ResChunkPullParser::isGoodEvent(parser.next())) { |
| 80 | if (parser.getChunk()->type != android::RES_TABLE_TYPE) { |
| 81 | mContext->getDiagnostics()->warn(DiagMessage(mSource) |
| 82 | << "unknown chunk of type '" |
| 83 | << (int) parser.getChunk()->type << "'"); |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (!parseTable(parser.getChunk())) { |
| 88 | error = true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { |
| 93 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 94 | << "corrupt resource table: " |
| 95 | << parser.getLastError()); |
| 96 | return false; |
| 97 | } |
| 98 | return !error; |
| 99 | } |
| 100 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Parses the resource table, which contains all the packages, types, and entries. |
| 103 | */ |
| 104 | bool BinaryResourceParser::parseTable(const ResChunk_header* chunk) { |
| 105 | const ResTable_header* tableHeader = convertTo<ResTable_header>(chunk); |
| 106 | if (!tableHeader) { |
| 107 | mContext->getDiagnostics()->error(DiagMessage(mSource) << "corrupt ResTable_header chunk"); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | ResChunkPullParser parser(getChunkData(&tableHeader->header), |
| 112 | getChunkDataLen(&tableHeader->header)); |
| 113 | while (ResChunkPullParser::isGoodEvent(parser.next())) { |
| 114 | switch (util::deviceToHost16(parser.getChunk()->type)) { |
| 115 | case android::RES_STRING_POOL_TYPE: |
| 116 | if (mValuePool.getError() == NO_INIT) { |
| 117 | status_t err = mValuePool.setTo(parser.getChunk(), |
| 118 | util::deviceToHost32(parser.getChunk()->size)); |
| 119 | if (err != NO_ERROR) { |
| 120 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 121 | << "corrupt string pool in ResTable: " |
| 122 | << mValuePool.getError()); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // Reserve some space for the strings we are going to add. |
| 127 | mTable->stringPool.hintWillAdd(mValuePool.size(), mValuePool.styleCount()); |
| 128 | } else { |
| 129 | mContext->getDiagnostics()->warn(DiagMessage(mSource) |
| 130 | << "unexpected string pool in ResTable"); |
| 131 | } |
| 132 | break; |
| 133 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 134 | case android::RES_TABLE_PACKAGE_TYPE: |
| 135 | if (!parsePackage(parser.getChunk())) { |
| 136 | return false; |
| 137 | } |
| 138 | break; |
| 139 | |
| 140 | default: |
| 141 | mContext->getDiagnostics() |
| 142 | ->warn(DiagMessage(mSource) |
| 143 | << "unexpected chunk type " |
| 144 | << (int) util::deviceToHost16(parser.getChunk()->type)); |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { |
| 150 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 151 | << "corrupt resource table: " << parser.getLastError()); |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | bool BinaryResourceParser::parsePackage(const ResChunk_header* chunk) { |
| 159 | const ResTable_package* packageHeader = convertTo<ResTable_package>(chunk); |
| 160 | if (!packageHeader) { |
| 161 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 162 | << "corrupt ResTable_package chunk"); |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | uint32_t packageId = util::deviceToHost32(packageHeader->id); |
| 167 | if (packageId > std::numeric_limits<uint8_t>::max()) { |
| 168 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 169 | << "package ID is too big (" << packageId << ")"); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // Extract the package name. |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 174 | size_t len = strnlen16((const char16_t*) packageHeader->name, arraysize(packageHeader->name)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 175 | std::u16string packageName; |
| 176 | packageName.resize(len); |
| 177 | for (size_t i = 0; i < len; i++) { |
| 178 | packageName[i] = util::deviceToHost16(packageHeader->name[i]); |
| 179 | } |
| 180 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 181 | ResourceTablePackage* package = mTable->createPackage(util::utf16ToUtf8(packageName), |
| 182 | static_cast<uint8_t>(packageId)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 183 | if (!package) { |
| 184 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 185 | << "incompatible package '" << packageName |
| 186 | << "' with ID " << packageId); |
| 187 | return false; |
| 188 | } |
| 189 | |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 190 | // There can be multiple packages in a table, so |
| 191 | // clear the type and key pool in case they were set from a previous package. |
| 192 | mTypePool.uninit(); |
| 193 | mKeyPool.uninit(); |
| 194 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 195 | ResChunkPullParser parser(getChunkData(&packageHeader->header), |
| 196 | getChunkDataLen(&packageHeader->header)); |
| 197 | while (ResChunkPullParser::isGoodEvent(parser.next())) { |
| 198 | switch (util::deviceToHost16(parser.getChunk()->type)) { |
| 199 | case android::RES_STRING_POOL_TYPE: |
| 200 | if (mTypePool.getError() == NO_INIT) { |
| 201 | status_t err = mTypePool.setTo(parser.getChunk(), |
| 202 | util::deviceToHost32(parser.getChunk()->size)); |
| 203 | if (err != NO_ERROR) { |
| 204 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 205 | << "corrupt type string pool in " |
| 206 | << "ResTable_package: " |
| 207 | << mTypePool.getError()); |
| 208 | return false; |
| 209 | } |
| 210 | } else if (mKeyPool.getError() == NO_INIT) { |
| 211 | status_t err = mKeyPool.setTo(parser.getChunk(), |
| 212 | util::deviceToHost32(parser.getChunk()->size)); |
| 213 | if (err != NO_ERROR) { |
| 214 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 215 | << "corrupt key string pool in " |
| 216 | << "ResTable_package: " |
| 217 | << mKeyPool.getError()); |
| 218 | return false; |
| 219 | } |
| 220 | } else { |
| 221 | mContext->getDiagnostics()->warn(DiagMessage(mSource) << "unexpected string pool"); |
| 222 | } |
| 223 | break; |
| 224 | |
| 225 | case android::RES_TABLE_TYPE_SPEC_TYPE: |
| 226 | if (!parseTypeSpec(parser.getChunk())) { |
| 227 | return false; |
| 228 | } |
| 229 | break; |
| 230 | |
| 231 | case android::RES_TABLE_TYPE_TYPE: |
| 232 | if (!parseType(package, parser.getChunk())) { |
| 233 | return false; |
| 234 | } |
| 235 | break; |
| 236 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 237 | default: |
| 238 | mContext->getDiagnostics() |
| 239 | ->warn(DiagMessage(mSource) |
| 240 | << "unexpected chunk type " |
| 241 | << (int) util::deviceToHost16(parser.getChunk()->type)); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { |
| 247 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 248 | << "corrupt ResTable_package: " |
| 249 | << parser.getLastError()); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | // Now go through the table and change local resource ID references to |
| 254 | // symbolic references. |
| 255 | ReferenceIdToNameVisitor visitor(&mIdIndex); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 256 | visitAllValuesInTable(mTable, &visitor); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 257 | return true; |
| 258 | } |
| 259 | |
| 260 | bool BinaryResourceParser::parseTypeSpec(const ResChunk_header* chunk) { |
| 261 | if (mTypePool.getError() != NO_ERROR) { |
| 262 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 263 | << "missing type string pool"); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | const ResTable_typeSpec* typeSpec = convertTo<ResTable_typeSpec>(chunk); |
| 268 | if (!typeSpec) { |
| 269 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 270 | << "corrupt ResTable_typeSpec chunk"); |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | if (typeSpec->id == 0) { |
| 275 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 276 | << "ResTable_typeSpec has invalid id: " << typeSpec->id); |
| 277 | return false; |
| 278 | } |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | bool BinaryResourceParser::parseType(const ResourceTablePackage* package, |
| 283 | const ResChunk_header* chunk) { |
| 284 | if (mTypePool.getError() != NO_ERROR) { |
| 285 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 286 | << "missing type string pool"); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | if (mKeyPool.getError() != NO_ERROR) { |
| 291 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 292 | << "missing key string pool"); |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | const ResTable_type* type = convertTo<ResTable_type>(chunk); |
| 297 | if (!type) { |
| 298 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 299 | << "corrupt ResTable_type chunk"); |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | if (type->id == 0) { |
| 304 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 305 | << "ResTable_type has invalid id: " << (int) type->id); |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | ConfigDescription config; |
| 310 | config.copyFromDtoH(type->config); |
| 311 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 312 | const std::string typeStr = util::getString(mTypePool, type->id - 1); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 313 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 314 | const ResourceType* parsedType = parseResourceType(typeStr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 315 | if (!parsedType) { |
| 316 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 317 | << "invalid type name '" << typeStr |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 318 | << "' for type with ID " << (int) type->id); |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | TypeVariant tv(type); |
| 323 | for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) { |
| 324 | const ResTable_entry* entry = *it; |
| 325 | if (!entry) { |
| 326 | continue; |
| 327 | } |
| 328 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 329 | const ResourceName name(package->name, *parsedType, |
| 330 | util::getString(mKeyPool, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 331 | util::deviceToHost32(entry->key.index))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 332 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 333 | 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] | 334 | |
| 335 | std::unique_ptr<Value> resourceValue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 336 | if (entry->flags & ResTable_entry::FLAG_COMPLEX) { |
| 337 | const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 338 | |
| 339 | // TODO(adamlesinski): Check that the entry count is valid. |
| 340 | resourceValue = parseMapEntry(name, config, mapEntry); |
| 341 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 342 | const Res_value* value = (const Res_value*)( |
| 343 | (const uint8_t*) entry + util::deviceToHost32(entry->size)); |
| 344 | resourceValue = parseValue(name, config, value, entry->flags); |
| 345 | } |
| 346 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 347 | if (!resourceValue) { |
| 348 | mContext->getDiagnostics()->error(DiagMessage(mSource) |
| 349 | << "failed to parse value for resource " << name |
| 350 | << " (" << resId << ") with configuration '" |
| 351 | << config << "'"); |
| 352 | return false; |
| 353 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 354 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 355 | if (!mTable->addResourceAllowMangled(name, config, {}, std::move(resourceValue), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 356 | mContext->getDiagnostics())) { |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 361 | Symbol symbol; |
| 362 | symbol.state = SymbolState::kPublic; |
| 363 | symbol.source = mSource.withLine(0); |
| 364 | if (!mTable->setSymbolStateAllowMangled(name, resId, symbol, |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 365 | mContext->getDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Add this resource name->id mapping to the index so |
| 371 | // that we can resolve all ID references to name references. |
| 372 | auto cacheIter = mIdIndex.find(resId); |
| 373 | if (cacheIter == mIdIndex.end()) { |
| 374 | mIdIndex.insert({ resId, name }); |
| 375 | } |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | std::unique_ptr<Item> BinaryResourceParser::parseValue(const ResourceNameRef& name, |
| 381 | const ConfigDescription& config, |
| 382 | const Res_value* value, |
| 383 | uint16_t flags) { |
| 384 | if (name.type == ResourceType::kId) { |
| 385 | return util::make_unique<Id>(); |
| 386 | } |
| 387 | |
| 388 | const uint32_t data = util::deviceToHost32(value->data); |
| 389 | |
| 390 | if (value->dataType == Res_value::TYPE_STRING) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 391 | const std::string str = util::getString(mValuePool, data); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 392 | |
| 393 | const ResStringPool_span* spans = mValuePool.styleAt(data); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 394 | |
| 395 | // Check if the string has a valid style associated with it. |
| 396 | if (spans != nullptr && spans->name.index != ResStringPool_span::END) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 397 | StyleString styleStr = { str }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 398 | while (spans->name.index != ResStringPool_span::END) { |
| 399 | styleStr.spans.push_back(Span{ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 400 | util::getString(mValuePool, spans->name.index), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 401 | spans->firstChar, |
| 402 | spans->lastChar |
| 403 | }); |
| 404 | spans++; |
| 405 | } |
| 406 | return util::make_unique<StyledString>(mTable->stringPool.makeRef( |
| 407 | styleStr, StringPool::Context{1, config})); |
| 408 | } else { |
| 409 | if (name.type != ResourceType::kString && |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 410 | util::stringStartsWith(str, "res/")) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 411 | // This must be a FileReference. |
| 412 | return util::make_unique<FileReference>(mTable->stringPool.makeRef( |
| 413 | str, StringPool::Context{ 0, config })); |
| 414 | } |
| 415 | |
| 416 | // There are no styles associated with this string, so treat it as |
| 417 | // a simple string. |
| 418 | return util::make_unique<String>(mTable->stringPool.makeRef( |
| 419 | str, StringPool::Context{1, config})); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if (value->dataType == Res_value::TYPE_REFERENCE || |
| 424 | value->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 425 | const Reference::Type type = (value->dataType == Res_value::TYPE_REFERENCE) ? |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 426 | Reference::Type::kResource : Reference::Type::kAttribute; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 427 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 428 | if (data == 0) { |
| 429 | // A reference of 0, must be the magic @null reference. |
| 430 | Res_value nullType = {}; |
| 431 | nullType.dataType = Res_value::TYPE_REFERENCE; |
| 432 | return util::make_unique<BinaryPrimitive>(nullType); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 435 | // This is a normal reference. |
| 436 | return util::make_unique<Reference>(data, type); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | // Treat this as a raw binary primitive. |
| 440 | return util::make_unique<BinaryPrimitive>(*value); |
| 441 | } |
| 442 | |
| 443 | std::unique_ptr<Value> BinaryResourceParser::parseMapEntry(const ResourceNameRef& name, |
| 444 | const ConfigDescription& config, |
| 445 | const ResTable_map_entry* map) { |
| 446 | switch (name.type) { |
| 447 | case ResourceType::kStyle: |
| 448 | return parseStyle(name, config, map); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 449 | case ResourceType::kAttrPrivate: |
| 450 | // fallthrough |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 451 | case ResourceType::kAttr: |
| 452 | return parseAttr(name, config, map); |
| 453 | case ResourceType::kArray: |
| 454 | return parseArray(name, config, map); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 455 | case ResourceType::kPlurals: |
| 456 | return parsePlural(name, config, map); |
| 457 | default: |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 458 | assert(false && "unknown map type"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | return {}; |
| 462 | } |
| 463 | |
| 464 | std::unique_ptr<Style> BinaryResourceParser::parseStyle(const ResourceNameRef& name, |
| 465 | const ConfigDescription& config, |
| 466 | const ResTable_map_entry* map) { |
| 467 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 468 | if (util::deviceToHost32(map->parent.ident) != 0) { |
| 469 | // The parent is a regular reference to a resource. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 470 | style->parent = Reference(util::deviceToHost32(map->parent.ident)); |
| 471 | } |
| 472 | |
| 473 | for (const ResTable_map& mapEntry : map) { |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 474 | if (Res_INTERNALID(util::deviceToHost32(mapEntry.name.ident))) { |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 475 | continue; |
| 476 | } |
| 477 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 478 | Style::Entry styleEntry; |
| 479 | styleEntry.key = Reference(util::deviceToHost32(mapEntry.name.ident)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 480 | styleEntry.value = parseValue(name, config, &mapEntry.value, 0); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 481 | if (!styleEntry.value) { |
| 482 | return {}; |
| 483 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 484 | style->entries.push_back(std::move(styleEntry)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 485 | } |
| 486 | return style; |
| 487 | } |
| 488 | |
| 489 | std::unique_ptr<Attribute> BinaryResourceParser::parseAttr(const ResourceNameRef& name, |
| 490 | const ConfigDescription& config, |
| 491 | const ResTable_map_entry* map) { |
| 492 | const bool isWeak = (util::deviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0; |
| 493 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(isWeak); |
| 494 | |
| 495 | // First we must discover what type of attribute this is. Find the type mask. |
| 496 | auto typeMaskIter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool { |
| 497 | return util::deviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE; |
| 498 | }); |
| 499 | |
| 500 | if (typeMaskIter != end(map)) { |
| 501 | attr->typeMask = util::deviceToHost32(typeMaskIter->value.data); |
| 502 | } |
| 503 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 504 | for (const ResTable_map& mapEntry : map) { |
| 505 | if (Res_INTERNALID(util::deviceToHost32(mapEntry.name.ident))) { |
| 506 | switch (util::deviceToHost32(mapEntry.name.ident)) { |
| 507 | case ResTable_map::ATTR_MIN: |
| 508 | attr->minInt = static_cast<int32_t>(mapEntry.value.data); |
| 509 | break; |
| 510 | case ResTable_map::ATTR_MAX: |
| 511 | attr->maxInt = static_cast<int32_t>(mapEntry.value.data); |
| 512 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 513 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 514 | continue; |
| 515 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 516 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 517 | if (attr->typeMask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 518 | Attribute::Symbol symbol; |
| 519 | symbol.value = util::deviceToHost32(mapEntry.value.data); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 520 | symbol.symbol = Reference(util::deviceToHost32(mapEntry.name.ident)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 521 | attr->symbols.push_back(std::move(symbol)); |
| 522 | } |
| 523 | } |
| 524 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 525 | // TODO(adamlesinski): Find i80n, attributes. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 526 | return attr; |
| 527 | } |
| 528 | |
| 529 | std::unique_ptr<Array> BinaryResourceParser::parseArray(const ResourceNameRef& name, |
| 530 | const ConfigDescription& config, |
| 531 | const ResTable_map_entry* map) { |
| 532 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
| 533 | for (const ResTable_map& mapEntry : map) { |
| 534 | array->items.push_back(parseValue(name, config, &mapEntry.value, 0)); |
| 535 | } |
| 536 | return array; |
| 537 | } |
| 538 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 539 | std::unique_ptr<Plural> BinaryResourceParser::parsePlural(const ResourceNameRef& name, |
| 540 | const ConfigDescription& config, |
| 541 | const ResTable_map_entry* map) { |
| 542 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
| 543 | for (const ResTable_map& mapEntry : map) { |
| 544 | std::unique_ptr<Item> item = parseValue(name, config, &mapEntry.value, 0); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 545 | if (!item) { |
| 546 | return {}; |
| 547 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 548 | |
| 549 | switch (util::deviceToHost32(mapEntry.name.ident)) { |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 550 | case ResTable_map::ATTR_ZERO: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 551 | plural->values[Plural::Zero] = std::move(item); |
| 552 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 553 | case ResTable_map::ATTR_ONE: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 554 | plural->values[Plural::One] = std::move(item); |
| 555 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 556 | case ResTable_map::ATTR_TWO: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 557 | plural->values[Plural::Two] = std::move(item); |
| 558 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 559 | case ResTable_map::ATTR_FEW: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 560 | plural->values[Plural::Few] = std::move(item); |
| 561 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 562 | case ResTable_map::ATTR_MANY: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 563 | plural->values[Plural::Many] = std::move(item); |
| 564 | break; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 565 | case ResTable_map::ATTR_OTHER: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 566 | plural->values[Plural::Other] = std::move(item); |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | return plural; |
| 571 | } |
| 572 | |
| 573 | } // namespace aapt |