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 | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 17 | #include "unflatten/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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | #include "unflatten/ResChunkPullParser.h" |
| 35 | #include "util/Util.h" |
| 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | namespace aapt { |
| 38 | |
| 39 | using namespace android; |
| 40 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 41 | using ::android::base::StringPrintf; |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 43 | namespace { |
| 44 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 45 | // Visitor that converts a reference's resource ID to a resource name, given a mapping from |
| 46 | // resource ID to resource name. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | class ReferenceIdToNameVisitor : public ValueVisitor { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 48 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | using ValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 51 | explicit ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceName>* mapping) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | : mapping_(mapping) { |
| 53 | CHECK(mapping_ != nullptr); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | void Visit(Reference* reference) override { |
| 57 | if (!reference->id || !reference->id.value().is_valid()) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 58 | return; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 61 | ResourceId id = reference->id.value(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | auto cache_iter = mapping_->find(id); |
| 63 | if (cache_iter != mapping_->end()) { |
| 64 | reference->name = cache_iter->second; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 66 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | DISALLOW_COPY_AND_ASSIGN(ReferenceIdToNameVisitor); |
| 70 | |
| 71 | const std::map<ResourceId, ResourceName>* mapping_; |
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 | } // namespace |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 75 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 76 | BinaryResourceParser::BinaryResourceParser(IAaptContext* context, ResourceTable* table, |
| 77 | const Source& source, const void* data, size_t len, |
| 78 | io::IFileCollection* files) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | : context_(context), |
| 80 | table_(table), |
| 81 | source_(source), |
| 82 | data_(data), |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 83 | data_len_(len), |
| 84 | files_(files) { |
| 85 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | bool BinaryResourceParser::Parse() { |
| 88 | ResChunkPullParser parser(data_, data_len_); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 90 | if (!ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 91 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
| 92 | << "corrupt resources.arsc: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 95 | |
| 96 | if (parser.chunk()->type != android::RES_TABLE_TYPE) { |
| 97 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
| 98 | << StringPrintf("unknown chunk of type 0x%02x", |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 99 | static_cast<int>(parser.chunk()->type))); |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
| 103 | if (!ParseTable(parser.chunk())) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | if (parser.Next() != ResChunkPullParser::Event::kEndDocument) { |
| 108 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 109 | context_->GetDiagnostics()->Warn( |
| 110 | DiagMessage(source_) << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error()); |
| 111 | } else { |
| 112 | context_->GetDiagnostics()->Warn( |
| 113 | DiagMessage(source_) << StringPrintf( |
| 114 | "unexpected chunk of type 0x%02x trailing RES_TABLE_TYPE", |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 115 | static_cast<int>(parser.chunk()->type))); |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 121 | /** |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 122 | * Parses the resource table, which contains all the packages, types, and |
| 123 | * entries. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 125 | bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) { |
| 126 | const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk); |
| 127 | if (!table_header) { |
| 128 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 129 | << "corrupt ResTable_header chunk"); |
| 130 | return false; |
| 131 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 132 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | ResChunkPullParser parser(GetChunkData(&table_header->header), |
| 134 | GetChunkDataLen(&table_header->header)); |
| 135 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 136 | switch (util::DeviceToHost16(parser.chunk()->type)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 137 | case android::RES_STRING_POOL_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | if (value_pool_.getError() == NO_INIT) { |
| 139 | status_t err = value_pool_.setTo( |
| 140 | parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 141 | if (err != NO_ERROR) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | context_->GetDiagnostics()->Error( |
| 143 | DiagMessage(source_) << "corrupt string pool in ResTable: " |
| 144 | << 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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | table_->string_pool.HintWillAdd(value_pool_.size(), |
| 150 | value_pool_.styleCount()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 151 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | context_->GetDiagnostics()->Warn( |
| 153 | DiagMessage(source_) << "unexpected string pool in ResTable"); |
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 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 157 | case android::RES_TABLE_PACKAGE_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | if (!ParsePackage(parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | break; |
| 162 | |
| 163 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 164 | context_->GetDiagnostics()->Warn( |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 165 | DiagMessage(source_) << "unexpected chunk type " |
| 166 | << static_cast<int>(util::DeviceToHost16(parser.chunk()->type))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 167 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 172 | context_->GetDiagnostics()->Error( |
| 173 | DiagMessage(source_) << "corrupt resource table: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) { |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 180 | constexpr size_t kMinPackageSize = |
| 181 | sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset); |
| 182 | const ResTable_package* package_header = ConvertTo<ResTable_package, kMinPackageSize>(chunk); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 183 | if (!package_header) { |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 184 | context_->GetDiagnostics()->Error(DiagMessage(source_) << "corrupt ResTable_package chunk"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 185 | return false; |
| 186 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 187 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 188 | uint32_t package_id = util::DeviceToHost32(package_header->id); |
| 189 | if (package_id > std::numeric_limits<uint8_t>::max()) { |
| 190 | context_->GetDiagnostics()->Error( |
| 191 | DiagMessage(source_) << "package ID is too big (" << package_id << ")"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 192 | return false; |
| 193 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 195 | // Extract the package name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | size_t len = strnlen16((const char16_t*)package_header->name, |
| 197 | arraysize(package_header->name)); |
| 198 | std::u16string package_name; |
| 199 | package_name.resize(len); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 200 | for (size_t i = 0; i < len; i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | package_name[i] = util::DeviceToHost16(package_header->name[i]); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 202 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 203 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | ResourceTablePackage* package = table_->CreatePackage( |
| 205 | util::Utf16ToUtf8(package_name), static_cast<uint8_t>(package_id)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 206 | if (!package) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | context_->GetDiagnostics()->Error( |
| 208 | DiagMessage(source_) << "incompatible package '" << package_name |
| 209 | << "' with ID " << package_id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 210 | return false; |
| 211 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 213 | // There can be multiple packages in a table, so |
| 214 | // 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] | 215 | type_pool_.uninit(); |
| 216 | key_pool_.uninit(); |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 217 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | ResChunkPullParser parser(GetChunkData(&package_header->header), |
| 219 | GetChunkDataLen(&package_header->header)); |
| 220 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 221 | switch (util::DeviceToHost16(parser.chunk()->type)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 222 | case android::RES_STRING_POOL_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | if (type_pool_.getError() == NO_INIT) { |
| 224 | status_t err = type_pool_.setTo( |
| 225 | parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 226 | if (err != NO_ERROR) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 228 | << "corrupt type string pool in " |
| 229 | << "ResTable_package: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | << type_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | } else if (key_pool_.getError() == NO_INIT) { |
| 234 | status_t err = key_pool_.setTo( |
| 235 | parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 236 | if (err != NO_ERROR) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 238 | << "corrupt key string pool in " |
| 239 | << "ResTable_package: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | << key_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | } else { |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 244 | context_->GetDiagnostics()->Warn(DiagMessage(source_) << "unexpected string pool"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 245 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 246 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 247 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 248 | case android::RES_TABLE_TYPE_SPEC_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | if (!ParseTypeSpec(parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 253 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 254 | case android::RES_TABLE_TYPE_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | if (!ParseType(package, parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | break; |
| 259 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 260 | case android::RES_TABLE_LIBRARY_TYPE: |
| 261 | if (!ParseLibrary(parser.chunk())) { |
| 262 | return false; |
| 263 | } |
| 264 | break; |
| 265 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 266 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 267 | context_->GetDiagnostics()->Warn( |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame^] | 268 | DiagMessage(source_) << "unexpected chunk type " |
| 269 | << static_cast<int>(util::DeviceToHost16(parser.chunk()->type))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | } |
| 273 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 274 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 275 | context_->GetDiagnostics()->Error( |
| 276 | DiagMessage(source_) << "corrupt ResTable_package: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | // Now go through the table and change local resource ID references to |
| 281 | // symbolic references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 282 | ReferenceIdToNameVisitor visitor(&id_index_); |
| 283 | VisitAllValuesInTable(table_, &visitor); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 284 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 287 | bool BinaryResourceParser::ParseTypeSpec(const ResChunk_header* chunk) { |
| 288 | if (type_pool_.getError() != NO_ERROR) { |
| 289 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 290 | << "missing type string pool"); |
| 291 | return false; |
| 292 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 293 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk); |
| 295 | if (!type_spec) { |
| 296 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 297 | << "corrupt ResTable_typeSpec chunk"); |
| 298 | return false; |
| 299 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 300 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 301 | if (type_spec->id == 0) { |
| 302 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 303 | << "ResTable_typeSpec has invalid id: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 304 | << type_spec->id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 311 | const ResChunk_header* chunk) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 312 | if (type_pool_.getError() != NO_ERROR) { |
| 313 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 314 | << "missing type string pool"); |
| 315 | return false; |
| 316 | } |
| 317 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | if (key_pool_.getError() != NO_ERROR) { |
| 319 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 320 | << "missing key string pool"); |
| 321 | return false; |
| 322 | } |
| 323 | |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 324 | // Specify a manual size, because ResTable_type contains ResTable_config, which changes |
| 325 | // a lot and has its own code to handle variable size. |
| 326 | const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 327 | if (!type) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 329 | << "corrupt ResTable_type chunk"); |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | if (type->id == 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 334 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 335 | << "ResTable_type has invalid id: " |
| 336 | << (int)type->id); |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | ConfigDescription config; |
| 341 | config.copyFromDtoH(type->config); |
| 342 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 343 | const std::string type_str = util::GetString(type_pool_, type->id - 1); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 344 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | const ResourceType* parsed_type = ParseResourceType(type_str); |
| 346 | if (!parsed_type) { |
| 347 | context_->GetDiagnostics()->Error( |
| 348 | DiagMessage(source_) << "invalid type name '" << type_str |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 349 | << "' for type with ID " << (int)type->id); |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | TypeVariant tv(type); |
| 354 | for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) { |
| 355 | const ResTable_entry* entry = *it; |
| 356 | if (!entry) { |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | const ResourceName name( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 361 | package->name, *parsed_type, |
| 362 | util::GetString(key_pool_, util::DeviceToHost32(entry->key.index))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 363 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 364 | const ResourceId res_id(package->id.value(), type->id, |
| 365 | static_cast<uint16_t>(it.index())); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 366 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 367 | std::unique_ptr<Value> resource_value; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 368 | if (entry->flags & ResTable_entry::FLAG_COMPLEX) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 369 | const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 370 | |
| 371 | // TODO(adamlesinski): Check that the entry count is valid. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 372 | resource_value = ParseMapEntry(name, config, mapEntry); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 373 | } else { |
| 374 | const Res_value* value = |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 375 | (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size)); |
| 376 | resource_value = ParseValue(name, config, *value); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 379 | if (!resource_value) { |
| 380 | context_->GetDiagnostics()->Error( |
| 381 | DiagMessage(source_) << "failed to parse value for resource " << name |
| 382 | << " (" << res_id << ") with configuration '" |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 383 | << config << "'"); |
| 384 | return false; |
| 385 | } |
| 386 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 387 | if (!table_->AddResourceAllowMangled(name, res_id, config, {}, std::move(resource_value), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 388 | context_->GetDiagnostics())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 389 | return false; |
| 390 | } |
| 391 | |
| 392 | if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) { |
| 393 | Symbol symbol; |
| 394 | symbol.state = SymbolState::kPublic; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 395 | symbol.source = source_.WithLine(0); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 396 | if (!table_->SetSymbolStateAllowMangled(name, res_id, symbol, context_->GetDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 397 | return false; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 398 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 401 | // Add this resource name->id mapping to the index so |
| 402 | // that we can resolve all ID references to name references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 403 | auto cache_iter = id_index_.find(res_id); |
| 404 | if (cache_iter == id_index_.end()) { |
| 405 | id_index_.insert({res_id, name}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 406 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 407 | } |
| 408 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 411 | bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) { |
| 412 | DynamicRefTable dynamic_ref_table; |
| 413 | if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) { |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries(); |
| 418 | const size_t count = entries.size(); |
| 419 | for (size_t i = 0; i < count; i++) { |
| 420 | table_->included_packages_[entries.valueAt(i)] = |
| 421 | util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string())); |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 426 | std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name, |
| 427 | const ConfigDescription& config, |
| 428 | const android::Res_value& value) { |
| 429 | std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_, |
| 430 | value, &table_->string_pool); |
| 431 | if (files_ != nullptr && item != nullptr) { |
| 432 | FileReference* file_ref = ValueCast<FileReference>(item.get()); |
| 433 | if (file_ref != nullptr) { |
| 434 | file_ref->file = files_->FindFile(*file_ref->path); |
| 435 | if (file_ref->file == nullptr) { |
Adam Lesinski | 742888f | 2017-04-28 15:34:52 -0700 | [diff] [blame] | 436 | context_->GetDiagnostics()->Warn(DiagMessage() << "resource " << name << " for config '" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 437 | << config << "' is a file reference to '" |
| 438 | << *file_ref->path |
| 439 | << "' but no such path exists"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 440 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 443 | return item; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 446 | std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 447 | const ResourceNameRef& name, const ConfigDescription& config, |
| 448 | const ResTable_map_entry* map) { |
| 449 | switch (name.type) { |
| 450 | case ResourceType::kStyle: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 451 | return ParseStyle(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 452 | case ResourceType::kAttrPrivate: |
| 453 | // fallthrough |
| 454 | case ResourceType::kAttr: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 455 | return ParseAttr(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 456 | case ResourceType::kArray: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 457 | return ParseArray(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 458 | case ResourceType::kPlurals: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 459 | return ParsePlural(name, config, map); |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 460 | case ResourceType::kId: |
| 461 | // Special case: An ID is not a bag, but some apps have defined the auto-generated |
| 462 | // IDs that come from declaring an enum value in an attribute as an empty map... |
| 463 | // We can ignore the value here. |
| 464 | return util::make_unique<Id>(); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 465 | default: |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 466 | context_->GetDiagnostics()->Error(DiagMessage() << "illegal map type '" << ToString(name.type) |
| 467 | << "' (" << (int)name.type << ")"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 473 | std::unique_ptr<Style> BinaryResourceParser::ParseStyle( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 474 | const ResourceNameRef& name, const ConfigDescription& config, |
| 475 | const ResTable_map_entry* map) { |
| 476 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 477 | if (util::DeviceToHost32(map->parent.ident) != 0) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 478 | // The parent is a regular reference to a resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 479 | style->parent = Reference(util::DeviceToHost32(map->parent.ident)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 482 | for (const ResTable_map& map_entry : map) { |
| 483 | if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 484 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 487 | Style::Entry style_entry; |
| 488 | style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident)); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 489 | style_entry.value = ParseValue(name, config, map_entry.value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 490 | if (!style_entry.value) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 491 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 492 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 493 | style->entries.push_back(std::move(style_entry)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 494 | } |
| 495 | return style; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 498 | std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 499 | const ResourceNameRef& name, const ConfigDescription& config, |
| 500 | const ResTable_map_entry* map) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 501 | const bool is_weak = |
| 502 | (util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0; |
| 503 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(is_weak); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 504 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 505 | // First we must discover what type of attribute this is. Find the type mask. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 506 | auto type_mask_iter = |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 507 | std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 508 | return util::DeviceToHost32(entry.name.ident) == |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 509 | ResTable_map::ATTR_TYPE; |
| 510 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 511 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 512 | if (type_mask_iter != end(map)) { |
| 513 | attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 516 | for (const ResTable_map& map_entry : map) { |
| 517 | if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) { |
| 518 | switch (util::DeviceToHost32(map_entry.name.ident)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 519 | case ResTable_map::ATTR_MIN: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 520 | attr->min_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 521 | break; |
| 522 | case ResTable_map::ATTR_MAX: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 523 | attr->max_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 524 | break; |
| 525 | } |
| 526 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 529 | if (attr->type_mask & |
| 530 | (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 531 | Attribute::Symbol symbol; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 532 | symbol.value = util::DeviceToHost32(map_entry.value.data); |
| 533 | symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 534 | attr->symbols.push_back(std::move(symbol)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 535 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 536 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 537 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 538 | // TODO(adamlesinski): Find i80n, attributes. |
| 539 | return attr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 542 | std::unique_ptr<Array> BinaryResourceParser::ParseArray( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 543 | const ResourceNameRef& name, const ConfigDescription& config, |
| 544 | const ResTable_map_entry* map) { |
| 545 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 546 | for (const ResTable_map& map_entry : map) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 547 | array->items.push_back(ParseValue(name, config, map_entry.value)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 548 | } |
| 549 | return array; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 552 | std::unique_ptr<Plural> BinaryResourceParser::ParsePlural( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 553 | const ResourceNameRef& name, const ConfigDescription& config, |
| 554 | const ResTable_map_entry* map) { |
| 555 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 556 | for (const ResTable_map& map_entry : map) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 557 | std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 558 | if (!item) { |
| 559 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 560 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 561 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 562 | switch (util::DeviceToHost32(map_entry.name.ident)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 563 | case ResTable_map::ATTR_ZERO: |
| 564 | plural->values[Plural::Zero] = std::move(item); |
| 565 | break; |
| 566 | case ResTable_map::ATTR_ONE: |
| 567 | plural->values[Plural::One] = std::move(item); |
| 568 | break; |
| 569 | case ResTable_map::ATTR_TWO: |
| 570 | plural->values[Plural::Two] = std::move(item); |
| 571 | break; |
| 572 | case ResTable_map::ATTR_FEW: |
| 573 | plural->values[Plural::Few] = std::move(item); |
| 574 | break; |
| 575 | case ResTable_map::ATTR_MANY: |
| 576 | plural->values[Plural::Many] = std::move(item); |
| 577 | break; |
| 578 | case ResTable_map::ATTR_OTHER: |
| 579 | plural->values[Plural::Other] = std::move(item); |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | return plural; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 586 | } // namespace aapt |