| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "ResourceTable.h" |
| 18 | #include "ResourceValues.h" |
| 19 | #include "ValueVisitor.h" |
| 20 | |
| 21 | #include "flatten/ChunkWriter.h" |
| 22 | #include "flatten/ResourceTypeExtensions.h" |
| 23 | #include "flatten/TableFlattener.h" |
| 24 | #include "util/BigBuffer.h" |
| 25 | |
| Elliott Hughes | e7d2cc3 | 2015-12-08 08:57:14 -0800 | [diff] [blame] | 26 | #include <android-base/macros.h> |
| Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 28 | #include <numeric> |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 29 | #include <sstream> |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include <type_traits> |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | |
| 32 | using namespace android; |
| 33 | |
| 34 | namespace aapt { |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | template <typename T> |
| 39 | static bool cmpIds(const T* a, const T* b) { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 40 | return a->id.value() < b->id.value(); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void strcpy16_htod(uint16_t* dst, size_t len, const StringPiece16& src) { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 44 | if (len == 0) { |
| 45 | return; |
| 46 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 48 | size_t i; |
| 49 | const char16_t* srcData = src.data(); |
| 50 | for (i = 0; i < len - 1 && i < src.size(); i++) { |
| 51 | dst[i] = util::hostToDevice16((uint16_t)srcData[i]); |
| 52 | } |
| 53 | dst[i] = 0; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 56 | static bool cmpStyleEntries(const Style::Entry& a, const Style::Entry& b) { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 57 | if (a.key.id) { |
| 58 | if (b.key.id) { |
| 59 | return a.key.id.value() < b.key.id.value(); |
| 60 | } |
| 61 | return true; |
| 62 | } else if (!b.key.id) { |
| 63 | return a.key.name.value() < b.key.name.value(); |
| 64 | } |
| 65 | return false; |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | struct FlatEntry { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 69 | ResourceEntry* entry; |
| 70 | Value* value; |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 71 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 72 | // The entry string pool index to the entry's name. |
| 73 | uint32_t entryKey; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 76 | class MapFlattenVisitor : public RawValueVisitor { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 77 | public: |
| 78 | using RawValueVisitor::visit; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 79 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 80 | MapFlattenVisitor(ResTable_entry_ext* outEntry, BigBuffer* buffer) |
| 81 | : mOutEntry(outEntry), mBuffer(buffer) {} |
| 82 | |
| 83 | void visit(Attribute* attr) override { |
| 84 | { |
| 85 | Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE)); |
| 86 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->typeMask); |
| 87 | flattenEntry(&key, &val); |
| Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 90 | if (attr->minInt != std::numeric_limits<int32_t>::min()) { |
| 91 | Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN)); |
| 92 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, |
| 93 | static_cast<uint32_t>(attr->minInt)); |
| 94 | flattenEntry(&key, &val); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 97 | if (attr->maxInt != std::numeric_limits<int32_t>::max()) { |
| 98 | Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX)); |
| 99 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, |
| 100 | static_cast<uint32_t>(attr->maxInt)); |
| 101 | flattenEntry(&key, &val); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 104 | for (Attribute::Symbol& s : attr->symbols) { |
| 105 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value); |
| 106 | flattenEntry(&s.symbol, &val); |
| 107 | } |
| 108 | } |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 109 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 110 | void visit(Style* style) override { |
| 111 | if (style->parent) { |
| 112 | const Reference& parentRef = style->parent.value(); |
| 113 | assert(parentRef.id && "parent has no ID"); |
| 114 | mOutEntry->parent.ident = util::hostToDevice32(parentRef.id.value().id); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 117 | // Sort the style. |
| 118 | std::sort(style->entries.begin(), style->entries.end(), cmpStyleEntries); |
| 119 | |
| 120 | for (Style::Entry& entry : style->entries) { |
| 121 | flattenEntry(&entry.key, entry.value.get()); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 123 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 125 | void visit(Styleable* styleable) override { |
| 126 | for (auto& attrRef : styleable->entries) { |
| 127 | BinaryPrimitive val(Res_value{}); |
| 128 | flattenEntry(&attrRef, &val); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 129 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 130 | } |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 131 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 132 | void visit(Array* array) override { |
| 133 | for (auto& item : array->items) { |
| 134 | ResTable_map* outEntry = mBuffer->nextBlock<ResTable_map>(); |
| 135 | flattenValue(item.get(), outEntry); |
| 136 | outEntry->value.size = util::hostToDevice16(sizeof(outEntry->value)); |
| 137 | mEntryCount++; |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 138 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 139 | } |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 140 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 141 | void visit(Plural* plural) override { |
| 142 | const size_t count = plural->values.size(); |
| 143 | for (size_t i = 0; i < count; i++) { |
| 144 | if (!plural->values[i]) { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | ResourceId q; |
| 149 | switch (i) { |
| 150 | case Plural::Zero: |
| 151 | q.id = android::ResTable_map::ATTR_ZERO; |
| 152 | break; |
| 153 | |
| 154 | case Plural::One: |
| 155 | q.id = android::ResTable_map::ATTR_ONE; |
| 156 | break; |
| 157 | |
| 158 | case Plural::Two: |
| 159 | q.id = android::ResTable_map::ATTR_TWO; |
| 160 | break; |
| 161 | |
| 162 | case Plural::Few: |
| 163 | q.id = android::ResTable_map::ATTR_FEW; |
| 164 | break; |
| 165 | |
| 166 | case Plural::Many: |
| 167 | q.id = android::ResTable_map::ATTR_MANY; |
| 168 | break; |
| 169 | |
| 170 | case Plural::Other: |
| 171 | q.id = android::ResTable_map::ATTR_OTHER; |
| 172 | break; |
| 173 | |
| 174 | default: |
| 175 | assert(false); |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | Reference key(q); |
| 180 | flattenEntry(&key, plural->values[i].get()); |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 181 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 182 | } |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 183 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 184 | /** |
| 185 | * Call this after visiting a Value. This will finish any work that |
| 186 | * needs to be done to prepare the entry. |
| 187 | */ |
| 188 | void finish() { mOutEntry->count = util::hostToDevice32(mEntryCount); } |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 189 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 190 | private: |
| 191 | void flattenKey(Reference* key, ResTable_map* outEntry) { |
| 192 | assert(key->id && "key has no ID"); |
| 193 | outEntry->name.ident = util::hostToDevice32(key->id.value().id); |
| 194 | } |
| Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 195 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 196 | void flattenValue(Item* value, ResTable_map* outEntry) { |
| 197 | bool result = value->flatten(&outEntry->value); |
| 198 | assert(result && "flatten failed"); |
| 199 | } |
| 200 | |
| 201 | void flattenEntry(Reference* key, Item* value) { |
| 202 | ResTable_map* outEntry = mBuffer->nextBlock<ResTable_map>(); |
| 203 | flattenKey(key, outEntry); |
| 204 | flattenValue(value, outEntry); |
| 205 | outEntry->value.size = util::hostToDevice16(sizeof(outEntry->value)); |
| 206 | mEntryCount++; |
| 207 | } |
| 208 | |
| 209 | ResTable_entry_ext* mOutEntry; |
| 210 | BigBuffer* mBuffer; |
| 211 | size_t mEntryCount = 0; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 214 | class PackageFlattener { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 215 | public: |
| 216 | PackageFlattener(IDiagnostics* diag, ResourceTablePackage* package) |
| 217 | : mDiag(diag), mPackage(package) {} |
| 218 | |
| 219 | bool flattenPackage(BigBuffer* buffer) { |
| 220 | ChunkWriter pkgWriter(buffer); |
| 221 | ResTable_package* pkgHeader = |
| 222 | pkgWriter.startChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE); |
| 223 | pkgHeader->id = util::hostToDevice32(mPackage->id.value()); |
| 224 | |
| 225 | if (mPackage->name.size() >= arraysize(pkgHeader->name)) { |
| 226 | mDiag->error(DiagMessage() << "package name '" << mPackage->name |
| 227 | << "' is too long"); |
| 228 | return false; |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 231 | // Copy the package name in device endianness. |
| 232 | strcpy16_htod(pkgHeader->name, arraysize(pkgHeader->name), |
| 233 | util::utf8ToUtf16(mPackage->name)); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 234 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 235 | // Serialize the types. We do this now so that our type and key strings |
| 236 | // are populated. We write those first. |
| 237 | BigBuffer typeBuffer(1024); |
| 238 | flattenTypes(&typeBuffer); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 239 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 240 | pkgHeader->typeStrings = util::hostToDevice32(pkgWriter.size()); |
| 241 | StringPool::flattenUtf16(pkgWriter.getBuffer(), mTypePool); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 242 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 243 | pkgHeader->keyStrings = util::hostToDevice32(pkgWriter.size()); |
| 244 | StringPool::flattenUtf8(pkgWriter.getBuffer(), mKeyPool); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 245 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 246 | // Append the types. |
| 247 | buffer->appendBuffer(std::move(typeBuffer)); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 248 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 249 | pkgWriter.finish(); |
| 250 | return true; |
| 251 | } |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 252 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 253 | private: |
| 254 | IDiagnostics* mDiag; |
| 255 | ResourceTablePackage* mPackage; |
| 256 | StringPool mTypePool; |
| 257 | StringPool mKeyPool; |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 258 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 259 | template <typename T, bool IsItem> |
| 260 | T* writeEntry(FlatEntry* entry, BigBuffer* buffer) { |
| 261 | static_assert(std::is_same<ResTable_entry, T>::value || |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 262 | std::is_same<ResTable_entry_ext, T>::value, |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 263 | "T must be ResTable_entry or ResTable_entry_ext"); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 264 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 265 | T* result = buffer->nextBlock<T>(); |
| 266 | ResTable_entry* outEntry = (ResTable_entry*)(result); |
| 267 | if (entry->entry->symbolStatus.state == SymbolState::kPublic) { |
| 268 | outEntry->flags |= ResTable_entry::FLAG_PUBLIC; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 271 | if (entry->value->isWeak()) { |
| 272 | outEntry->flags |= ResTable_entry::FLAG_WEAK; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 275 | if (!IsItem) { |
| 276 | outEntry->flags |= ResTable_entry::FLAG_COMPLEX; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 279 | outEntry->flags = util::hostToDevice16(outEntry->flags); |
| 280 | outEntry->key.index = util::hostToDevice32(entry->entryKey); |
| 281 | outEntry->size = util::hostToDevice16(sizeof(T)); |
| 282 | return result; |
| 283 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 284 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 285 | bool flattenValue(FlatEntry* entry, BigBuffer* buffer) { |
| 286 | if (Item* item = valueCast<Item>(entry->value)) { |
| 287 | writeEntry<ResTable_entry, true>(entry, buffer); |
| 288 | Res_value* outValue = buffer->nextBlock<Res_value>(); |
| 289 | bool result = item->flatten(outValue); |
| 290 | assert(result && "flatten failed"); |
| 291 | outValue->size = util::hostToDevice16(sizeof(*outValue)); |
| 292 | } else { |
| 293 | ResTable_entry_ext* outEntry = |
| 294 | writeEntry<ResTable_entry_ext, false>(entry, buffer); |
| 295 | MapFlattenVisitor visitor(outEntry, buffer); |
| 296 | entry->value->accept(&visitor); |
| 297 | visitor.finish(); |
| 298 | } |
| 299 | return true; |
| 300 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 301 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 302 | bool flattenConfig(const ResourceTableType* type, |
| 303 | const ConfigDescription& config, |
| 304 | std::vector<FlatEntry>* entries, BigBuffer* buffer) { |
| 305 | ChunkWriter typeWriter(buffer); |
| 306 | ResTable_type* typeHeader = |
| 307 | typeWriter.startChunk<ResTable_type>(RES_TABLE_TYPE_TYPE); |
| 308 | typeHeader->id = type->id.value(); |
| 309 | typeHeader->config = config; |
| 310 | typeHeader->config.swapHtoD(); |
| 311 | |
| 312 | auto maxAccum = [](uint32_t max, |
| 313 | const std::unique_ptr<ResourceEntry>& a) -> uint32_t { |
| 314 | return std::max(max, (uint32_t)a->id.value()); |
| 315 | }; |
| 316 | |
| 317 | // Find the largest entry ID. That is how many entries we will have. |
| 318 | const uint32_t entryCount = |
| 319 | std::accumulate(type->entries.begin(), type->entries.end(), 0, |
| 320 | maxAccum) + |
| 321 | 1; |
| 322 | |
| 323 | typeHeader->entryCount = util::hostToDevice32(entryCount); |
| 324 | uint32_t* indices = typeWriter.nextBlock<uint32_t>(entryCount); |
| 325 | |
| 326 | assert((size_t)entryCount <= std::numeric_limits<uint16_t>::max() + 1); |
| 327 | memset(indices, 0xff, entryCount * sizeof(uint32_t)); |
| 328 | |
| 329 | typeHeader->entriesStart = util::hostToDevice32(typeWriter.size()); |
| 330 | |
| 331 | const size_t entryStart = typeWriter.getBuffer()->size(); |
| 332 | for (FlatEntry& flatEntry : *entries) { |
| 333 | assert(flatEntry.entry->id.value() < entryCount); |
| 334 | indices[flatEntry.entry->id.value()] = |
| 335 | util::hostToDevice32(typeWriter.getBuffer()->size() - entryStart); |
| 336 | if (!flattenValue(&flatEntry, typeWriter.getBuffer())) { |
| 337 | mDiag->error(DiagMessage() |
| 338 | << "failed to flatten resource '" |
| 339 | << ResourceNameRef(mPackage->name, type->type, |
| 340 | flatEntry.entry->name) |
| 341 | << "' for configuration '" << config << "'"); |
| 342 | return false; |
| 343 | } |
| 344 | } |
| 345 | typeWriter.finish(); |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | std::vector<ResourceTableType*> collectAndSortTypes() { |
| 350 | std::vector<ResourceTableType*> sortedTypes; |
| 351 | for (auto& type : mPackage->types) { |
| 352 | if (type->type == ResourceType::kStyleable) { |
| 353 | // Styleables aren't real Resource Types, they are represented in the |
| 354 | // R.java |
| 355 | // file. |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | assert(type->id && "type must have an ID set"); |
| 360 | |
| 361 | sortedTypes.push_back(type.get()); |
| 362 | } |
| 363 | std::sort(sortedTypes.begin(), sortedTypes.end(), |
| 364 | cmpIds<ResourceTableType>); |
| 365 | return sortedTypes; |
| 366 | } |
| 367 | |
| 368 | std::vector<ResourceEntry*> collectAndSortEntries(ResourceTableType* type) { |
| 369 | // Sort the entries by entry ID. |
| 370 | std::vector<ResourceEntry*> sortedEntries; |
| 371 | for (auto& entry : type->entries) { |
| 372 | assert(entry->id && "entry must have an ID set"); |
| 373 | sortedEntries.push_back(entry.get()); |
| 374 | } |
| 375 | std::sort(sortedEntries.begin(), sortedEntries.end(), |
| 376 | cmpIds<ResourceEntry>); |
| 377 | return sortedEntries; |
| 378 | } |
| 379 | |
| 380 | bool flattenTypeSpec(ResourceTableType* type, |
| 381 | std::vector<ResourceEntry*>* sortedEntries, |
| 382 | BigBuffer* buffer) { |
| 383 | ChunkWriter typeSpecWriter(buffer); |
| 384 | ResTable_typeSpec* specHeader = |
| 385 | typeSpecWriter.startChunk<ResTable_typeSpec>(RES_TABLE_TYPE_SPEC_TYPE); |
| 386 | specHeader->id = type->id.value(); |
| 387 | |
| 388 | if (sortedEntries->empty()) { |
| 389 | typeSpecWriter.finish(); |
| 390 | return true; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 393 | // We can't just take the size of the vector. There may be holes in the |
| 394 | // entry ID space. |
| 395 | // Since the entries are sorted by ID, the last one will be the biggest. |
| 396 | const size_t numEntries = sortedEntries->back()->id.value() + 1; |
| 397 | |
| 398 | specHeader->entryCount = util::hostToDevice32(numEntries); |
| 399 | |
| 400 | // Reserve space for the masks of each resource in this type. These |
| 401 | // show for which configuration axis the resource changes. |
| 402 | uint32_t* configMasks = typeSpecWriter.nextBlock<uint32_t>(numEntries); |
| 403 | |
| 404 | const size_t actualNumEntries = sortedEntries->size(); |
| 405 | for (size_t entryIndex = 0; entryIndex < actualNumEntries; entryIndex++) { |
| 406 | ResourceEntry* entry = sortedEntries->at(entryIndex); |
| 407 | |
| 408 | // Populate the config masks for this entry. |
| 409 | |
| 410 | if (entry->symbolStatus.state == SymbolState::kPublic) { |
| 411 | configMasks[entry->id.value()] |= |
| 412 | util::hostToDevice32(ResTable_typeSpec::SPEC_PUBLIC); |
| 413 | } |
| 414 | |
| 415 | const size_t configCount = entry->values.size(); |
| 416 | for (size_t i = 0; i < configCount; i++) { |
| 417 | const ConfigDescription& config = entry->values[i]->config; |
| 418 | for (size_t j = i + 1; j < configCount; j++) { |
| 419 | configMasks[entry->id.value()] |= |
| 420 | util::hostToDevice32(config.diff(entry->values[j]->config)); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 421 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 422 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 423 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 424 | typeSpecWriter.finish(); |
| 425 | return true; |
| 426 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 427 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 428 | bool flattenTypes(BigBuffer* buffer) { |
| 429 | // Sort the types by their IDs. They will be inserted into the StringPool in |
| 430 | // this order. |
| 431 | std::vector<ResourceTableType*> sortedTypes = collectAndSortTypes(); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 432 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 433 | size_t expectedTypeId = 1; |
| 434 | for (ResourceTableType* type : sortedTypes) { |
| 435 | // If there is a gap in the type IDs, fill in the StringPool |
| 436 | // with empty values until we reach the ID we expect. |
| 437 | while (type->id.value() > expectedTypeId) { |
| 438 | std::stringstream typeName; |
| 439 | typeName << "?" << expectedTypeId; |
| 440 | mTypePool.makeRef(typeName.str()); |
| 441 | expectedTypeId++; |
| 442 | } |
| 443 | expectedTypeId++; |
| 444 | mTypePool.makeRef(toString(type->type)); |
| 445 | |
| 446 | std::vector<ResourceEntry*> sortedEntries = collectAndSortEntries(type); |
| 447 | |
| 448 | if (!flattenTypeSpec(type, &sortedEntries, buffer)) { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | // The binary resource table lists resource entries for each |
| 453 | // configuration. |
| 454 | // We store them inverted, where a resource entry lists the values for |
| 455 | // each |
| 456 | // configuration available. Here we reverse this to match the binary |
| 457 | // table. |
| 458 | std::map<ConfigDescription, std::vector<FlatEntry>> configToEntryListMap; |
| 459 | for (ResourceEntry* entry : sortedEntries) { |
| 460 | const uint32_t keyIndex = |
| 461 | (uint32_t)mKeyPool.makeRef(entry->name).getIndex(); |
| 462 | |
| 463 | // Group values by configuration. |
| 464 | for (auto& configValue : entry->values) { |
| 465 | configToEntryListMap[configValue->config].push_back( |
| 466 | FlatEntry{entry, configValue->value.get(), keyIndex}); |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 467 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 468 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 469 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 470 | // Flatten a configuration value. |
| 471 | for (auto& entry : configToEntryListMap) { |
| 472 | if (!flattenConfig(type, entry.first, &entry.second, buffer)) { |
| 473 | return false; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 474 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 475 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 476 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 477 | return true; |
| 478 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 479 | }; |
| 480 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 481 | } // namespace |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 482 | |
| 483 | bool TableFlattener::consume(IAaptContext* context, ResourceTable* table) { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 484 | // We must do this before writing the resources, since the string pool IDs may |
| 485 | // change. |
| 486 | table->stringPool.sort( |
| 487 | [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool { |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 488 | int diff = a.context.priority - b.context.priority; |
| 489 | if (diff < 0) return true; |
| 490 | if (diff > 0) return false; |
| 491 | diff = a.context.config.compare(b.context.config); |
| 492 | if (diff < 0) return true; |
| 493 | if (diff > 0) return false; |
| 494 | return a.value < b.value; |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 495 | }); |
| 496 | table->stringPool.prune(); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 497 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 498 | // Write the ResTable header. |
| 499 | ChunkWriter tableWriter(mBuffer); |
| 500 | ResTable_header* tableHeader = |
| 501 | tableWriter.startChunk<ResTable_header>(RES_TABLE_TYPE); |
| 502 | tableHeader->packageCount = util::hostToDevice32(table->packages.size()); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 503 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 504 | // Flatten the values string pool. |
| 505 | StringPool::flattenUtf8(tableWriter.getBuffer(), table->stringPool); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 506 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 507 | BigBuffer packageBuffer(1024); |
| Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 508 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 509 | // Flatten each package. |
| 510 | for (auto& package : table->packages) { |
| 511 | PackageFlattener flattener(context->getDiagnostics(), package.get()); |
| 512 | if (!flattener.flattenPackage(&packageBuffer)) { |
| 513 | return false; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 514 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 515 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 516 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 517 | // Finally merge all the packages into the main buffer. |
| 518 | tableWriter.getBuffer()->appendBuffer(std::move(packageBuffer)); |
| 519 | tableWriter.finish(); |
| 520 | return true; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 523 | } // namespace aapt |