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" |
| 25 | #include "androidfw/ResourceTypes.h" |
| 26 | #include "androidfw/TypeWrappers.h" |
| 27 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "ResourceTable.h" |
| 29 | #include "ResourceUtils.h" |
| 30 | #include "ResourceValues.h" |
| 31 | #include "Source.h" |
| 32 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | #include "unflatten/ResChunkPullParser.h" |
| 34 | #include "util/Util.h" |
| 35 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | namespace aapt { |
| 37 | |
| 38 | using namespace android; |
| 39 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 40 | namespace { |
| 41 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | /* |
| 43 | * Visitor that converts a reference's resource ID to a resource name, |
| 44 | * given a mapping from resource ID to resource name. |
| 45 | */ |
| 46 | class ReferenceIdToNameVisitor : public ValueVisitor { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 47 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 48 | using ValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 50 | explicit ReferenceIdToNameVisitor( |
| 51 | 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 | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 76 | BinaryResourceParser::BinaryResourceParser(IAaptContext* context, |
| 77 | ResourceTable* table, |
| 78 | const Source& source, |
| 79 | const void* data, size_t len) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 80 | : context_(context), |
| 81 | table_(table), |
| 82 | source_(source), |
| 83 | data_(data), |
| 84 | data_len_(len) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 85 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 86 | bool BinaryResourceParser::Parse() { |
| 87 | ResChunkPullParser parser(data_, data_len_); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 89 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 90 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 91 | if (parser.chunk()->type != android::RES_TABLE_TYPE) { |
| 92 | context_->GetDiagnostics()->Warn(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 93 | << "unknown chunk of type '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 94 | << (int)parser.chunk()->type << "'"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 95 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 98 | if (!ParseTable(parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 99 | error = true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 103 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 104 | context_->GetDiagnostics()->Error( |
| 105 | DiagMessage(source_) << "corrupt resource table: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | return !error; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 111 | /** |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 112 | * Parses the resource table, which contains all the packages, types, and |
| 113 | * entries. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 114 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 115 | bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) { |
| 116 | const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk); |
| 117 | if (!table_header) { |
| 118 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 119 | << "corrupt ResTable_header chunk"); |
| 120 | return false; |
| 121 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 123 | ResChunkPullParser parser(GetChunkData(&table_header->header), |
| 124 | GetChunkDataLen(&table_header->header)); |
| 125 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 126 | switch (util::DeviceToHost16(parser.chunk()->type)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 127 | case android::RES_STRING_POOL_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 128 | if (value_pool_.getError() == NO_INIT) { |
| 129 | status_t err = value_pool_.setTo( |
| 130 | parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 131 | if (err != NO_ERROR) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 132 | context_->GetDiagnostics()->Error( |
| 133 | DiagMessage(source_) << "corrupt string pool in ResTable: " |
| 134 | << value_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 138 | // Reserve some space for the strings we are going to add. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 139 | table_->string_pool.HintWillAdd(value_pool_.size(), |
| 140 | value_pool_.styleCount()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 141 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 142 | context_->GetDiagnostics()->Warn( |
| 143 | DiagMessage(source_) << "unexpected string pool in ResTable"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 145 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 147 | case android::RES_TABLE_PACKAGE_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 148 | if (!ParsePackage(parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | break; |
| 152 | |
| 153 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 154 | context_->GetDiagnostics()->Warn( |
| 155 | DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 156 | << "unexpected chunk type " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 157 | << (int)util::DeviceToHost16(parser.chunk()->type)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 158 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 159 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 162 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 163 | context_->GetDiagnostics()->Error( |
| 164 | DiagMessage(source_) << "corrupt resource table: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 170 | bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) { |
| 171 | const ResTable_package* package_header = ConvertTo<ResTable_package>(chunk); |
| 172 | if (!package_header) { |
| 173 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 174 | << "corrupt ResTable_package chunk"); |
| 175 | return false; |
| 176 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 178 | uint32_t package_id = util::DeviceToHost32(package_header->id); |
| 179 | if (package_id > std::numeric_limits<uint8_t>::max()) { |
| 180 | context_->GetDiagnostics()->Error( |
| 181 | DiagMessage(source_) << "package ID is too big (" << package_id << ")"); |
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 | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 185 | // Extract the package name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 186 | size_t len = strnlen16((const char16_t*)package_header->name, |
| 187 | arraysize(package_header->name)); |
| 188 | std::u16string package_name; |
| 189 | package_name.resize(len); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 190 | for (size_t i = 0; i < len; i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 191 | package_name[i] = util::DeviceToHost16(package_header->name[i]); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 192 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 193 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 194 | ResourceTablePackage* package = table_->CreatePackage( |
| 195 | util::Utf16ToUtf8(package_name), static_cast<uint8_t>(package_id)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 196 | if (!package) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 197 | context_->GetDiagnostics()->Error( |
| 198 | DiagMessage(source_) << "incompatible package '" << package_name |
| 199 | << "' 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) { |
| 214 | status_t err = type_pool_.setTo( |
| 215 | 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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 217 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 218 | << "corrupt type string pool in " |
| 219 | << "ResTable_package: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 220 | << type_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 221 | return false; |
| 222 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 223 | } else if (key_pool_.getError() == NO_INIT) { |
| 224 | status_t err = key_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 key string pool in " |
| 229 | << "ResTable_package: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 230 | << key_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 234 | context_->GetDiagnostics()->Warn(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 235 | << "unexpected string pool"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 236 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 237 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 238 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 239 | case android::RES_TABLE_TYPE_SPEC_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 240 | if (!ParseTypeSpec(parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 245 | case android::RES_TABLE_TYPE_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 246 | if (!ParseType(package, parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 247 | return false; |
| 248 | } |
| 249 | break; |
| 250 | |
| 251 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 252 | context_->GetDiagnostics()->Warn( |
| 253 | DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 254 | << "unexpected chunk type " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 255 | << (int)util::DeviceToHost16(parser.chunk()->type)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 260 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 261 | context_->GetDiagnostics()->Error( |
| 262 | DiagMessage(source_) << "corrupt ResTable_package: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | |
| 266 | // Now go through the table and change local resource ID references to |
| 267 | // symbolic references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 268 | ReferenceIdToNameVisitor visitor(&id_index_); |
| 269 | VisitAllValuesInTable(table_, &visitor); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 270 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 273 | bool BinaryResourceParser::ParseTypeSpec(const ResChunk_header* chunk) { |
| 274 | if (type_pool_.getError() != NO_ERROR) { |
| 275 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 276 | << "missing type string pool"); |
| 277 | return false; |
| 278 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 279 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 280 | const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk); |
| 281 | if (!type_spec) { |
| 282 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 283 | << "corrupt ResTable_typeSpec chunk"); |
| 284 | return false; |
| 285 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 286 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 287 | if (type_spec->id == 0) { |
| 288 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 289 | << "ResTable_typeSpec has invalid id: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 290 | << type_spec->id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 296 | bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 297 | const ResChunk_header* chunk) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 298 | if (type_pool_.getError() != NO_ERROR) { |
| 299 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 300 | << "missing type string pool"); |
| 301 | return false; |
| 302 | } |
| 303 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 304 | if (key_pool_.getError() != NO_ERROR) { |
| 305 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 306 | << "missing key string pool"); |
| 307 | return false; |
| 308 | } |
| 309 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 310 | const ResTable_type* type = ConvertTo<ResTable_type>(chunk); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 311 | if (!type) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 312 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 313 | << "corrupt ResTable_type chunk"); |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | if (type->id == 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 318 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 319 | << "ResTable_type has invalid id: " |
| 320 | << (int)type->id); |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | ConfigDescription config; |
| 325 | config.copyFromDtoH(type->config); |
| 326 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 327 | const std::string type_str = util::GetString(type_pool_, type->id - 1); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 328 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 329 | const ResourceType* parsed_type = ParseResourceType(type_str); |
| 330 | if (!parsed_type) { |
| 331 | context_->GetDiagnostics()->Error( |
| 332 | DiagMessage(source_) << "invalid type name '" << type_str |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 333 | << "' for type with ID " << (int)type->id); |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | TypeVariant tv(type); |
| 338 | for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) { |
| 339 | const ResTable_entry* entry = *it; |
| 340 | if (!entry) { |
| 341 | continue; |
| 342 | } |
| 343 | |
| 344 | const ResourceName name( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 345 | package->name, *parsed_type, |
| 346 | util::GetString(key_pool_, util::DeviceToHost32(entry->key.index))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 347 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 348 | const ResourceId res_id(package->id.value(), type->id, |
| 349 | static_cast<uint16_t>(it.index())); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 350 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 351 | std::unique_ptr<Value> resource_value; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 352 | if (entry->flags & ResTable_entry::FLAG_COMPLEX) { |
| 353 | const ResTable_map_entry* mapEntry = |
| 354 | static_cast<const ResTable_map_entry*>(entry); |
| 355 | |
| 356 | // TODO(adamlesinski): Check that the entry count is valid. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 357 | resource_value = ParseMapEntry(name, config, mapEntry); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 358 | } else { |
| 359 | const Res_value* value = |
| 360 | (const Res_value*)((const uint8_t*)entry + |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 361 | util::DeviceToHost32(entry->size)); |
| 362 | resource_value = ParseValue(name, config, value, entry->flags); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 365 | if (!resource_value) { |
| 366 | context_->GetDiagnostics()->Error( |
| 367 | DiagMessage(source_) << "failed to parse value for resource " << name |
| 368 | << " (" << res_id << ") with configuration '" |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 369 | << config << "'"); |
| 370 | return false; |
| 371 | } |
| 372 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 373 | if (!table_->AddResourceAllowMangled(name, config, {}, |
| 374 | std::move(resource_value), |
| 375 | context_->GetDiagnostics())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 376 | return false; |
| 377 | } |
| 378 | |
| 379 | if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) { |
| 380 | Symbol symbol; |
| 381 | symbol.state = SymbolState::kPublic; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 382 | symbol.source = source_.WithLine(0); |
| 383 | if (!table_->SetSymbolStateAllowMangled(name, res_id, symbol, |
| 384 | context_->GetDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 385 | return false; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 386 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 389 | // Add this resource name->id mapping to the index so |
| 390 | // that we can resolve all ID references to name references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 391 | auto cache_iter = id_index_.find(res_id); |
| 392 | if (cache_iter == id_index_.end()) { |
| 393 | id_index_.insert({res_id, name}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 394 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 395 | } |
| 396 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 399 | std::unique_ptr<Item> BinaryResourceParser::ParseValue( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 400 | const ResourceNameRef& name, const ConfigDescription& config, |
| 401 | const Res_value* value, uint16_t flags) { |
| 402 | if (name.type == ResourceType::kId) { |
| 403 | return util::make_unique<Id>(); |
| 404 | } |
| 405 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 406 | const uint32_t data = util::DeviceToHost32(value->data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 407 | |
| 408 | if (value->dataType == Res_value::TYPE_STRING) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 409 | const std::string str = util::GetString(value_pool_, data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 410 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 411 | const ResStringPool_span* spans = value_pool_.styleAt(data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 412 | |
| 413 | // Check if the string has a valid style associated with it. |
| 414 | if (spans != nullptr && spans->name.index != ResStringPool_span::END) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 415 | StyleString style_str = {str}; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 416 | while (spans->name.index != ResStringPool_span::END) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 417 | style_str.spans.push_back( |
| 418 | Span{util::GetString(value_pool_, spans->name.index), |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 419 | spans->firstChar, spans->lastChar}); |
| 420 | spans++; |
| 421 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 422 | return util::make_unique<StyledString>(table_->string_pool.MakeRef( |
| 423 | style_str, |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 424 | StringPool::Context(StringPool::Context::kStylePriority, config))); |
| 425 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 426 | if (name.type != ResourceType::kString && util::StartsWith(str, "res/")) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 427 | // This must be a FileReference. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 428 | return util::make_unique<FileReference>(table_->string_pool.MakeRef( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 429 | str, |
| 430 | StringPool::Context(StringPool::Context::kHighPriority, config))); |
| 431 | } |
| 432 | |
| 433 | // There are no styles associated with this string, so treat it as |
| 434 | // a simple string. |
| 435 | return util::make_unique<String>( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 436 | table_->string_pool.MakeRef(str, StringPool::Context(config))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | if (value->dataType == Res_value::TYPE_REFERENCE || |
| 441 | value->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 442 | const Reference::Type type = (value->dataType == Res_value::TYPE_REFERENCE) |
| 443 | ? Reference::Type::kResource |
| 444 | : Reference::Type::kAttribute; |
| 445 | |
| 446 | if (data == 0) { |
| 447 | // A reference of 0, must be the magic @null reference. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 448 | Res_value null_type = {}; |
| 449 | null_type.dataType = Res_value::TYPE_REFERENCE; |
| 450 | return util::make_unique<BinaryPrimitive>(null_type); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 453 | // This is a normal reference. |
| 454 | return util::make_unique<Reference>(data, type); |
| 455 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 456 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 457 | // Treat this as a raw binary primitive. |
| 458 | return util::make_unique<BinaryPrimitive>(*value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 461 | std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 462 | const ResourceNameRef& name, const ConfigDescription& config, |
| 463 | const ResTable_map_entry* map) { |
| 464 | switch (name.type) { |
| 465 | case ResourceType::kStyle: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 466 | return ParseStyle(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 467 | case ResourceType::kAttrPrivate: |
| 468 | // fallthrough |
| 469 | case ResourceType::kAttr: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 470 | return ParseAttr(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 471 | case ResourceType::kArray: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 472 | return ParseArray(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 473 | case ResourceType::kPlurals: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 474 | return ParsePlural(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 475 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 476 | LOG(FATAL) << "unknown map type"; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 482 | std::unique_ptr<Style> BinaryResourceParser::ParseStyle( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 483 | const ResourceNameRef& name, const ConfigDescription& config, |
| 484 | const ResTable_map_entry* map) { |
| 485 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 486 | if (util::DeviceToHost32(map->parent.ident) != 0) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 487 | // The parent is a regular reference to a resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 488 | style->parent = Reference(util::DeviceToHost32(map->parent.ident)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 491 | for (const ResTable_map& map_entry : map) { |
| 492 | if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 493 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 496 | Style::Entry style_entry; |
| 497 | style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident)); |
| 498 | style_entry.value = ParseValue(name, config, &map_entry.value, 0); |
| 499 | if (!style_entry.value) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 500 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 501 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 502 | style->entries.push_back(std::move(style_entry)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 503 | } |
| 504 | return style; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 507 | std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 508 | const ResourceNameRef& name, const ConfigDescription& config, |
| 509 | const ResTable_map_entry* map) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 510 | const bool is_weak = |
| 511 | (util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0; |
| 512 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(is_weak); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 513 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 514 | // 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^] | 515 | auto type_mask_iter = |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 516 | std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 517 | return util::DeviceToHost32(entry.name.ident) == |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 518 | ResTable_map::ATTR_TYPE; |
| 519 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 520 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 521 | if (type_mask_iter != end(map)) { |
| 522 | attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 525 | for (const ResTable_map& map_entry : map) { |
| 526 | if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) { |
| 527 | switch (util::DeviceToHost32(map_entry.name.ident)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 528 | case ResTable_map::ATTR_MIN: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 529 | attr->min_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 530 | break; |
| 531 | case ResTable_map::ATTR_MAX: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 532 | attr->max_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 538 | if (attr->type_mask & |
| 539 | (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 540 | Attribute::Symbol symbol; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 541 | symbol.value = util::DeviceToHost32(map_entry.value.data); |
| 542 | symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 543 | attr->symbols.push_back(std::move(symbol)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 544 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 545 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 546 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 547 | // TODO(adamlesinski): Find i80n, attributes. |
| 548 | return attr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 551 | std::unique_ptr<Array> BinaryResourceParser::ParseArray( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 552 | const ResourceNameRef& name, const ConfigDescription& config, |
| 553 | const ResTable_map_entry* map) { |
| 554 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 555 | for (const ResTable_map& map_entry : map) { |
| 556 | array->items.push_back(ParseValue(name, config, &map_entry.value, 0)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 557 | } |
| 558 | return array; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 561 | std::unique_ptr<Plural> BinaryResourceParser::ParsePlural( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 562 | const ResourceNameRef& name, const ConfigDescription& config, |
| 563 | const ResTable_map_entry* map) { |
| 564 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 565 | for (const ResTable_map& map_entry : map) { |
| 566 | std::unique_ptr<Item> item = ParseValue(name, config, &map_entry.value, 0); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 567 | if (!item) { |
| 568 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 569 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 570 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 571 | switch (util::DeviceToHost32(map_entry.name.ident)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 572 | case ResTable_map::ATTR_ZERO: |
| 573 | plural->values[Plural::Zero] = std::move(item); |
| 574 | break; |
| 575 | case ResTable_map::ATTR_ONE: |
| 576 | plural->values[Plural::One] = std::move(item); |
| 577 | break; |
| 578 | case ResTable_map::ATTR_TWO: |
| 579 | plural->values[Plural::Two] = std::move(item); |
| 580 | break; |
| 581 | case ResTable_map::ATTR_FEW: |
| 582 | plural->values[Plural::Few] = std::move(item); |
| 583 | break; |
| 584 | case ResTable_map::ATTR_MANY: |
| 585 | plural->values[Plural::Many] = std::move(item); |
| 586 | break; |
| 587 | case ResTable_map::ATTR_OTHER: |
| 588 | plural->values[Plural::Other] = std::move(item); |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | return plural; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 595 | } // namespace aapt |