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 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 17 | #include "format/binary/BinaryResourceParser.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "android-base/logging.h" |
| 24 | #include "android-base/macros.h" |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "androidfw/ResourceTypes.h" |
| 27 | #include "androidfw/TypeWrappers.h" |
| 28 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | #include "ResourceTable.h" |
| 30 | #include "ResourceUtils.h" |
| 31 | #include "ResourceValues.h" |
| 32 | #include "Source.h" |
| 33 | #include "ValueVisitor.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 34 | #include "format/binary/ResChunkPullParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | #include "util/Util.h" |
| 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | using namespace android; |
| 38 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 39 | using ::android::base::StringPrintf; |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 41 | namespace aapt { |
| 42 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 43 | namespace { |
| 44 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 45 | static std::u16string strcpy16_dtoh(const char16_t* src, size_t len) { |
| 46 | size_t utf16_len = strnlen16(src, len); |
| 47 | if (utf16_len == 0) { |
| 48 | return {}; |
| 49 | } |
| 50 | std::u16string dst; |
| 51 | dst.resize(utf16_len); |
| 52 | for (size_t i = 0; i < utf16_len; i++) { |
| 53 | dst[i] = util::DeviceToHost16(src[i]); |
| 54 | } |
| 55 | return dst; |
| 56 | } |
| 57 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 58 | // Visitor that converts a reference's resource ID to a resource name, given a mapping from |
| 59 | // resource ID to resource name. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 60 | class ReferenceIdToNameVisitor : public DescendingValueVisitor { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 61 | public: |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 62 | using DescendingValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 64 | explicit ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceName>* mapping) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | : mapping_(mapping) { |
| 66 | CHECK(mapping_ != nullptr); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | void Visit(Reference* reference) override { |
| 70 | if (!reference->id || !reference->id.value().is_valid()) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 71 | return; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 74 | ResourceId id = reference->id.value(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | auto cache_iter = mapping_->find(id); |
| 76 | if (cache_iter != mapping_->end()) { |
| 77 | reference->name = cache_iter->second; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 79 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | DISALLOW_COPY_AND_ASSIGN(ReferenceIdToNameVisitor); |
| 83 | |
| 84 | const std::map<ResourceId, ResourceName>* mapping_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 87 | } // namespace |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 88 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 89 | BinaryResourceParser::BinaryResourceParser(IDiagnostics* diag, ResourceTable* table, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 90 | const Source& source, const void* data, size_t len, |
| 91 | io::IFileCollection* files) |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 92 | : diag_(diag), table_(table), source_(source), data_(data), data_len_(len), files_(files) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 93 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | bool BinaryResourceParser::Parse() { |
| 96 | ResChunkPullParser parser(data_, data_len_); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 97 | |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 98 | if (!ResChunkPullParser::IsGoodEvent(parser.Next())) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 99 | diag_->Error(DiagMessage(source_) << "corrupt resources.arsc: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 102 | |
| 103 | if (parser.chunk()->type != android::RES_TABLE_TYPE) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 104 | diag_->Error(DiagMessage(source_) << StringPrintf("unknown chunk of type 0x%02x", |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 105 | static_cast<int>(parser.chunk()->type))); |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if (!ParseTable(parser.chunk())) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (parser.Next() != ResChunkPullParser::Event::kEndDocument) { |
| 114 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 115 | diag_->Warn(DiagMessage(source_) |
| 116 | << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error()); |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 117 | } else { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 118 | diag_->Warn(DiagMessage(source_) |
| 119 | << StringPrintf("unexpected chunk of type 0x%02x trailing RES_TABLE_TYPE", |
| 120 | static_cast<int>(parser.chunk()->type))); |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 126 | // Parses the resource table, which contains all the packages, types, and entries. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) { |
| 128 | const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk); |
| 129 | if (!table_header) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 130 | diag_->Error(DiagMessage(source_) << "corrupt ResTable_header chunk"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 133 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 134 | ResChunkPullParser parser(GetChunkData(&table_header->header), |
| 135 | GetChunkDataLen(&table_header->header)); |
| 136 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 137 | switch (util::DeviceToHost16(parser.chunk()->type)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 138 | case android::RES_STRING_POOL_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | if (value_pool_.getError() == NO_INIT) { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 140 | status_t err = |
| 141 | value_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 142 | if (err != NO_ERROR) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 143 | diag_->Error(DiagMessage(source_) |
| 144 | << "corrupt string pool in ResTable: " << value_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 145 | return false; |
| 146 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 147 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 148 | // Reserve some space for the strings we are going to add. |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 149 | table_->string_pool.HintWillAdd(value_pool_.size(), value_pool_.styleCount()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 150 | } else { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 151 | diag_->Warn(DiagMessage(source_) << "unexpected string pool in ResTable"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 153 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 154 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 155 | case android::RES_TABLE_PACKAGE_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | if (!ParsePackage(parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | break; |
| 160 | |
| 161 | default: |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 162 | diag_->Warn(DiagMessage(source_) |
| 163 | << "unexpected chunk type " |
| 164 | << static_cast<int>(util::DeviceToHost16(parser.chunk()->type))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 165 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 166 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 170 | diag_->Error(DiagMessage(source_) << "corrupt resource table: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) { |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 177 | constexpr size_t kMinPackageSize = |
| 178 | sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset); |
| 179 | const ResTable_package* package_header = ConvertTo<ResTable_package, kMinPackageSize>(chunk); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 180 | if (!package_header) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 181 | diag_->Error(DiagMessage(source_) << "corrupt ResTable_package chunk"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 182 | return false; |
| 183 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 184 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | uint32_t package_id = util::DeviceToHost32(package_header->id); |
| 186 | if (package_id > std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 187 | diag_->Error(DiagMessage(source_) << "package ID is too big (" << package_id << ")"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 188 | return false; |
| 189 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 191 | // Extract the package name. |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 192 | std::u16string package_name = strcpy16_dtoh((const char16_t*)package_header->name, |
| 193 | arraysize(package_header->name)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 195 | ResourceTablePackage* package = |
| 196 | table_->CreatePackage(util::Utf16ToUtf8(package_name), static_cast<uint8_t>(package_id)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 197 | if (!package) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 198 | diag_->Error(DiagMessage(source_) |
| 199 | << "incompatible package '" << package_name << "' with ID " << package_id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 202 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 203 | // There can be multiple packages in a table, so |
| 204 | // clear the type and key pool in case they were set from a previous package. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | type_pool_.uninit(); |
| 206 | key_pool_.uninit(); |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 207 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 208 | ResChunkPullParser parser(GetChunkData(&package_header->header), |
| 209 | GetChunkDataLen(&package_header->header)); |
| 210 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 211 | switch (util::DeviceToHost16(parser.chunk()->type)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 212 | case android::RES_STRING_POOL_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | if (type_pool_.getError() == NO_INIT) { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 214 | status_t err = |
| 215 | type_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 216 | if (err != NO_ERROR) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 217 | diag_->Error(DiagMessage(source_) << "corrupt type string pool in " |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 218 | << "ResTable_package: " << type_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 219 | return false; |
| 220 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | } else if (key_pool_.getError() == NO_INIT) { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 222 | status_t err = |
| 223 | key_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 224 | if (err != NO_ERROR) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 225 | diag_->Error(DiagMessage(source_) << "corrupt key string pool in " |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 226 | << "ResTable_package: " << key_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | } else { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 230 | diag_->Warn(DiagMessage(source_) << "unexpected string pool"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 231 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 232 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 233 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 234 | case android::RES_TABLE_TYPE_SPEC_TYPE: |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 235 | if (!ParseTypeSpec(package, parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 239 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 240 | case android::RES_TABLE_TYPE_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 241 | if (!ParseType(package, parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | break; |
| 245 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 246 | case android::RES_TABLE_LIBRARY_TYPE: |
| 247 | if (!ParseLibrary(parser.chunk())) { |
| 248 | return false; |
| 249 | } |
| 250 | break; |
| 251 | |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 252 | case android::RES_TABLE_OVERLAYABLE_TYPE: |
| 253 | if (!ParseOverlayable(parser.chunk())) { |
| 254 | return false; |
| 255 | } |
| 256 | break; |
| 257 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 258 | default: |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 259 | diag_->Warn(DiagMessage(source_) |
| 260 | << "unexpected chunk type " |
| 261 | << static_cast<int>(util::DeviceToHost16(parser.chunk()->type))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 267 | diag_->Error(DiagMessage(source_) << "corrupt ResTable_package: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | |
| 271 | // Now go through the table and change local resource ID references to |
| 272 | // symbolic references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | ReferenceIdToNameVisitor visitor(&id_index_); |
| 274 | VisitAllValuesInTable(table_, &visitor); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 275 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 278 | bool BinaryResourceParser::ParseTypeSpec(const ResourceTablePackage* package, |
| 279 | const ResChunk_header* chunk) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 280 | if (type_pool_.getError() != NO_ERROR) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 281 | diag_->Error(DiagMessage(source_) << "missing type string pool"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 282 | return false; |
| 283 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 284 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk); |
| 286 | if (!type_spec) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 287 | diag_->Error(DiagMessage(source_) << "corrupt ResTable_typeSpec chunk"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 288 | return false; |
| 289 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 290 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | if (type_spec->id == 0) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 292 | diag_->Error(DiagMessage(source_) << "ResTable_typeSpec has invalid id: " << type_spec->id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 293 | return false; |
| 294 | } |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 295 | |
| 296 | // The data portion of this chunk contains entry_count 32bit entries, |
| 297 | // each one representing a set of flags. |
| 298 | const size_t entry_count = dtohl(type_spec->entryCount); |
| 299 | |
| 300 | // There can only be 2^16 entries in a type, because that is the ID |
| 301 | // space for entries (EEEE) in the resource ID 0xPPTTEEEE. |
| 302 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
| 303 | diag_->Error(DiagMessage(source_) |
| 304 | << "ResTable_typeSpec has too many entries (" << entry_count << ")"); |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | const size_t data_size = util::DeviceToHost32(type_spec->header.size) - |
| 309 | util::DeviceToHost16(type_spec->header.headerSize); |
| 310 | if (entry_count * sizeof(uint32_t) > data_size) { |
| 311 | diag_->Error(DiagMessage(source_) << "ResTable_typeSpec too small to hold entries."); |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | // Record the type_spec_flags for later. We don't know resource names yet, and we need those |
| 316 | // to mark resources as overlayable. |
| 317 | const uint32_t* type_spec_flags = reinterpret_cast<const uint32_t*>( |
| 318 | reinterpret_cast<uintptr_t>(type_spec) + util::DeviceToHost16(type_spec->header.headerSize)); |
| 319 | for (size_t i = 0; i < entry_count; i++) { |
| 320 | ResourceId id(package->id.value_or_default(0x0), type_spec->id, static_cast<size_t>(i)); |
| 321 | entry_type_spec_flags_[id] = util::DeviceToHost32(type_spec_flags[i]); |
| 322 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 323 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 326 | bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 327 | const ResChunk_header* chunk) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | if (type_pool_.getError() != NO_ERROR) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 329 | diag_->Error(DiagMessage(source_) << "missing type string pool"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | if (key_pool_.getError() != NO_ERROR) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 334 | diag_->Error(DiagMessage(source_) << "missing key string pool"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 338 | // Specify a manual size, because ResTable_type contains ResTable_config, which changes |
| 339 | // a lot and has its own code to handle variable size. |
| 340 | const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 341 | if (!type) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 342 | diag_->Error(DiagMessage(source_) << "corrupt ResTable_type chunk"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | |
| 346 | if (type->id == 0) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 347 | diag_->Error(DiagMessage(source_) << "ResTable_type has invalid id: " << (int)type->id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | |
| 351 | ConfigDescription config; |
| 352 | config.copyFromDtoH(type->config); |
| 353 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 354 | const std::string type_str = util::GetString(type_pool_, type->id - 1); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 355 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 356 | // Be lenient on the name of the type if the table is lenient on resource validation. |
| 357 | auto parsed_type = ResourceType::kUnknown; |
| 358 | if (const ResourceType* parsed = ParseResourceType(type_str)) { |
| 359 | parsed_type = *parsed; |
| 360 | } else if (table_->GetValidateResources()) { |
| 361 | diag_->Error(DiagMessage(source_) << "invalid type name '" << type_str << "' for type with ID " |
| 362 | << (int) type->id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 363 | return false; |
| 364 | } |
| 365 | |
| 366 | TypeVariant tv(type); |
| 367 | for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) { |
| 368 | const ResTable_entry* entry = *it; |
| 369 | if (!entry) { |
| 370 | continue; |
| 371 | } |
| 372 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 373 | const ResourceName name(package->name, parsed_type, |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 374 | util::GetString(key_pool_, util::DeviceToHost32(entry->key.index))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 375 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 376 | const ResourceId res_id(package->id.value(), type->id, static_cast<uint16_t>(it.index())); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 377 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 378 | std::unique_ptr<Value> resource_value; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 379 | if (entry->flags & ResTable_entry::FLAG_COMPLEX) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 380 | const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 381 | |
| 382 | // TODO(adamlesinski): Check that the entry count is valid. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 383 | resource_value = ParseMapEntry(name, config, mapEntry); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 384 | } else { |
| 385 | const Res_value* value = |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 386 | (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size)); |
| 387 | resource_value = ParseValue(name, config, *value); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 390 | if (!resource_value) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 391 | diag_->Error(DiagMessage(source_) << "failed to parse value for resource " << name << " (" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 392 | << res_id << ") with configuration '" << config << "'"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 396 | if (!table_->AddResourceWithIdMangled(name, res_id, config, {}, std::move(resource_value), |
| 397 | diag_)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 398 | return false; |
| 399 | } |
| 400 | |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 401 | if (entry->flags & ResTable_entry::FLAG_PUBLIC) { |
| 402 | Visibility visibility; |
| 403 | visibility.level = Visibility::Level::kPublic; |
| 404 | visibility.source = source_.WithLine(0); |
| 405 | if (!table_->SetVisibilityWithIdMangled(name, visibility, res_id, diag_)) { |
| 406 | return false; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // Erase the ID from the map once processed, so that we don't mark the same symbol more than |
| 410 | // once. |
| 411 | entry_type_spec_flags_.erase(res_id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 414 | // Add this resource name->id mapping to the index so |
| 415 | // that we can resolve all ID references to name references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 416 | auto cache_iter = id_index_.find(res_id); |
| 417 | if (cache_iter == id_index_.end()) { |
| 418 | id_index_.insert({res_id, name}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 419 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 420 | } |
| 421 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 424 | bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) { |
| 425 | DynamicRefTable dynamic_ref_table; |
| 426 | if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) { |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries(); |
| 431 | const size_t count = entries.size(); |
| 432 | for (size_t i = 0; i < count; i++) { |
| 433 | table_->included_packages_[entries.valueAt(i)] = |
| 434 | util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string())); |
| 435 | } |
| 436 | return true; |
| 437 | } |
| 438 | |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 439 | bool BinaryResourceParser::ParseOverlayable(const ResChunk_header* chunk) { |
| 440 | const ResTable_overlayable_header* header = ConvertTo<ResTable_overlayable_header>(chunk); |
| 441 | if (!header) { |
| 442 | diag_->Error(DiagMessage(source_) << "corrupt ResTable_category_header chunk"); |
| 443 | return false; |
| 444 | } |
| 445 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 446 | auto overlayable = std::make_shared<Overlayable>(); |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 447 | overlayable->name = util::Utf16ToUtf8(strcpy16_dtoh((const char16_t*)header->name, |
| 448 | arraysize(header->name))); |
| 449 | overlayable->actor = util::Utf16ToUtf8(strcpy16_dtoh((const char16_t*)header->actor, |
| 450 | arraysize(header->name))); |
| 451 | overlayable->source = source_.WithLine(0); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 452 | |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 453 | ResChunkPullParser parser(GetChunkData(chunk), |
| 454 | GetChunkDataLen(chunk)); |
| 455 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 456 | if (util::DeviceToHost16(parser.chunk()->type) == android::RES_TABLE_OVERLAYABLE_POLICY_TYPE) { |
| 457 | const ResTable_overlayable_policy_header* policy_header = |
| 458 | ConvertTo<ResTable_overlayable_policy_header>(parser.chunk()); |
| 459 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 460 | OverlayableItem::PolicyFlags policies = OverlayableItem::Policy::kNone; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 461 | if (policy_header->policy_flags & ResTable_overlayable_policy_header::POLICY_PUBLIC) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 462 | policies |= OverlayableItem::Policy::kPublic; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 463 | } |
| 464 | if (policy_header->policy_flags |
| 465 | & ResTable_overlayable_policy_header::POLICY_SYSTEM_PARTITION) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 466 | policies |= OverlayableItem::Policy::kSystem; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 467 | } |
| 468 | if (policy_header->policy_flags |
| 469 | & ResTable_overlayable_policy_header::POLICY_VENDOR_PARTITION) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 470 | policies |= OverlayableItem::Policy::kVendor; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 471 | } |
| 472 | if (policy_header->policy_flags |
| 473 | & ResTable_overlayable_policy_header::POLICY_PRODUCT_PARTITION) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 474 | policies |= OverlayableItem::Policy::kProduct; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 475 | } |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 476 | |
| 477 | const ResTable_ref* const ref_begin = reinterpret_cast<const ResTable_ref*>( |
| 478 | ((uint8_t *)policy_header) + util::DeviceToHost32(policy_header->header.headerSize)); |
| 479 | const ResTable_ref* const ref_end = ref_begin |
| 480 | + util::DeviceToHost32(policy_header->entry_count); |
| 481 | for (auto ref_iter = ref_begin; ref_iter != ref_end; ++ref_iter) { |
| 482 | ResourceId res_id(util::DeviceToHost32(ref_iter->ident)); |
| 483 | const auto iter = id_index_.find(res_id); |
| 484 | |
| 485 | // If the overlayable chunk comes before the type chunks, the resource ids and resource name |
| 486 | // pairing will not exist at this point. |
| 487 | if (iter == id_index_.cend()) { |
| 488 | diag_->Error(DiagMessage(source_) << "failed to find resource name for overlayable" |
| 489 | << " resource " << res_id); |
| 490 | return false; |
| 491 | } |
| 492 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 493 | OverlayableItem overlayable_item(overlayable); |
| 494 | overlayable_item.source = source_.WithLine(0); |
| 495 | overlayable_item.policies = policies; |
| 496 | if (!table_->SetOverlayable(iter->second, overlayable_item, diag_)) { |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 497 | return false; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | return true; |
| 504 | } |
| 505 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 506 | std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name, |
| 507 | const ConfigDescription& config, |
| 508 | const android::Res_value& value) { |
| 509 | std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_, |
| 510 | value, &table_->string_pool); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 511 | if (files_ != nullptr) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 512 | FileReference* file_ref = ValueCast<FileReference>(item.get()); |
| 513 | if (file_ref != nullptr) { |
| 514 | file_ref->file = files_->FindFile(*file_ref->path); |
| 515 | if (file_ref->file == nullptr) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 516 | diag_->Warn(DiagMessage() << "resource " << name << " for config '" << config |
| 517 | << "' is a file reference to '" << *file_ref->path |
| 518 | << "' but no such path exists"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 519 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 520 | } |
| 521 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 522 | return item; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 525 | std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry(const ResourceNameRef& name, |
| 526 | const ConfigDescription& config, |
| 527 | const ResTable_map_entry* map) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 528 | switch (name.type) { |
| 529 | case ResourceType::kStyle: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | return ParseStyle(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 531 | case ResourceType::kAttrPrivate: |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 532 | // fallthrough |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 533 | case ResourceType::kAttr: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 534 | return ParseAttr(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 535 | case ResourceType::kArray: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 536 | return ParseArray(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 537 | case ResourceType::kPlurals: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 538 | return ParsePlural(name, config, map); |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 539 | case ResourceType::kId: |
| 540 | // Special case: An ID is not a bag, but some apps have defined the auto-generated |
| 541 | // IDs that come from declaring an enum value in an attribute as an empty map... |
| 542 | // We can ignore the value here. |
| 543 | return util::make_unique<Id>(); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 544 | default: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 545 | diag_->Error(DiagMessage() << "illegal map type '" << to_string(name.type) << "' (" |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 546 | << (int)name.type << ")"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 552 | std::unique_ptr<Style> BinaryResourceParser::ParseStyle(const ResourceNameRef& name, |
| 553 | const ConfigDescription& config, |
| 554 | const ResTable_map_entry* map) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 555 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 556 | if (util::DeviceToHost32(map->parent.ident) != 0) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 557 | // The parent is a regular reference to a resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 558 | style->parent = Reference(util::DeviceToHost32(map->parent.ident)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 561 | for (const ResTable_map& map_entry : map) { |
| 562 | if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 563 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 566 | Style::Entry style_entry; |
| 567 | style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident)); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 568 | style_entry.value = ParseValue(name, config, map_entry.value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 569 | if (!style_entry.value) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 570 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 571 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 572 | style->entries.push_back(std::move(style_entry)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 573 | } |
| 574 | return style; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 577 | std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr(const ResourceNameRef& name, |
| 578 | const ConfigDescription& config, |
| 579 | const ResTable_map_entry* map) { |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 580 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(); |
| 581 | attr->SetWeak((util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 582 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 583 | // First we must discover what type of attribute this is. Find the type mask. |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 584 | auto type_mask_iter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool { |
| 585 | return util::DeviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE; |
| 586 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 587 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 588 | if (type_mask_iter != end(map)) { |
| 589 | attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 592 | for (const ResTable_map& map_entry : map) { |
| 593 | if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) { |
| 594 | switch (util::DeviceToHost32(map_entry.name.ident)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 595 | case ResTable_map::ATTR_MIN: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 596 | attr->min_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 597 | break; |
| 598 | case ResTable_map::ATTR_MAX: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 599 | attr->max_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 605 | if (attr->type_mask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 606 | Attribute::Symbol symbol; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 607 | symbol.value = util::DeviceToHost32(map_entry.value.data); |
| 608 | symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 609 | attr->symbols.push_back(std::move(symbol)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 610 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 611 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 612 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 613 | // TODO(adamlesinski): Find i80n, attributes. |
| 614 | return attr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 617 | std::unique_ptr<Array> BinaryResourceParser::ParseArray(const ResourceNameRef& name, |
| 618 | const ConfigDescription& config, |
| 619 | const ResTable_map_entry* map) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 620 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 621 | for (const ResTable_map& map_entry : map) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 622 | array->elements.push_back(ParseValue(name, config, map_entry.value)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 623 | } |
| 624 | return array; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 627 | std::unique_ptr<Plural> BinaryResourceParser::ParsePlural(const ResourceNameRef& name, |
| 628 | const ConfigDescription& config, |
| 629 | const ResTable_map_entry* map) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 630 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 631 | for (const ResTable_map& map_entry : map) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 632 | std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 633 | if (!item) { |
| 634 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 635 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 636 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 637 | switch (util::DeviceToHost32(map_entry.name.ident)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 638 | case ResTable_map::ATTR_ZERO: |
| 639 | plural->values[Plural::Zero] = std::move(item); |
| 640 | break; |
| 641 | case ResTable_map::ATTR_ONE: |
| 642 | plural->values[Plural::One] = std::move(item); |
| 643 | break; |
| 644 | case ResTable_map::ATTR_TWO: |
| 645 | plural->values[Plural::Two] = std::move(item); |
| 646 | break; |
| 647 | case ResTable_map::ATTR_FEW: |
| 648 | plural->values[Plural::Few] = std::move(item); |
| 649 | break; |
| 650 | case ResTable_map::ATTR_MANY: |
| 651 | plural->values[Plural::Many] = std::move(item); |
| 652 | break; |
| 653 | case ResTable_map::ATTR_OTHER: |
| 654 | plural->values[Plural::Other] = std::move(item); |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | return plural; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 661 | } // namespace aapt |