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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "flatten/TableFlattener.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 20 | #include <numeric> |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 21 | #include <sstream> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include <type_traits> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 24 | #include "android-base/logging.h" |
| 25 | #include "android-base/macros.h" |
| 26 | |
| 27 | #include "ResourceTable.h" |
| 28 | #include "ResourceValues.h" |
| 29 | #include "ValueVisitor.h" |
| 30 | #include "flatten/ChunkWriter.h" |
| 31 | #include "flatten/ResourceTypeExtensions.h" |
| 32 | #include "util/BigBuffer.h" |
| 33 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | using namespace android; |
| 35 | |
| 36 | namespace aapt { |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | template <typename T> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 41 | static bool cmp_ids(const T* a, const T* b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | return a->id.value() < b->id.value(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | 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] | 46 | if (len == 0) { |
| 47 | return; |
| 48 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | size_t i; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 51 | const char16_t* src_data = src.data(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | for (i = 0; i < len - 1 && i < src.size(); i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 53 | dst[i] = util::HostToDevice16((uint16_t)src_data[i]); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | } |
| 55 | dst[i] = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 58 | static bool cmp_style_entries(const Style::Entry& a, const Style::Entry& b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | if (a.key.id) { |
| 60 | if (b.key.id) { |
| 61 | return a.key.id.value() < b.key.id.value(); |
| 62 | } |
| 63 | return true; |
| 64 | } else if (!b.key.id) { |
| 65 | return a.key.name.value() < b.key.name.value(); |
| 66 | } |
| 67 | return false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | struct FlatEntry { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | ResourceEntry* entry; |
| 72 | Value* value; |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | // The entry string pool index to the entry's name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 75 | uint32_t entry_key; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 78 | class MapFlattenVisitor : public RawValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 80 | using RawValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 82 | MapFlattenVisitor(ResTable_entry_ext* out_entry, BigBuffer* buffer) |
| 83 | : out_entry_(out_entry), buffer_(buffer) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 85 | void Visit(Attribute* attr) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | { |
| 87 | Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 88 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->type_mask); |
| 89 | FlattenEntry(&key, &val); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 92 | if (attr->min_int != std::numeric_limits<int32_t>::min()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN)); |
| 94 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 95 | static_cast<uint32_t>(attr->min_int)); |
| 96 | FlattenEntry(&key, &val); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 99 | if (attr->max_int != std::numeric_limits<int32_t>::max()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX)); |
| 101 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 102 | static_cast<uint32_t>(attr->max_int)); |
| 103 | FlattenEntry(&key, &val); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | for (Attribute::Symbol& s : attr->symbols) { |
| 107 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, s.value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 108 | FlattenEntry(&s.symbol, &val); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 112 | void Visit(Style* style) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | if (style->parent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 114 | const Reference& parent_ref = style->parent.value(); |
| 115 | CHECK(bool(parent_ref.id)) << "parent has no ID"; |
| 116 | out_entry_->parent.ident = util::HostToDevice32(parent_ref.id.value().id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | // Sort the style. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 120 | std::sort(style->entries.begin(), style->entries.end(), cmp_style_entries); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | |
| 122 | for (Style::Entry& entry : style->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 123 | FlattenEntry(&entry.key, entry.value.get()); |
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 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 127 | void Visit(Styleable* styleable) override { |
| 128 | for (auto& attr_ref : styleable->entries) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | BinaryPrimitive val(Res_value{}); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 130 | FlattenEntry(&attr_ref, &val); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 133 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 134 | void Visit(Array* array) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | for (auto& item : array->items) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 136 | ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>(); |
| 137 | FlattenValue(item.get(), out_entry); |
| 138 | out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value)); |
| 139 | entry_count_++; |
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 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 142 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 143 | void Visit(Plural* plural) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | const size_t count = plural->values.size(); |
| 145 | for (size_t i = 0; i < count; i++) { |
| 146 | if (!plural->values[i]) { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | ResourceId q; |
| 151 | switch (i) { |
| 152 | case Plural::Zero: |
| 153 | q.id = android::ResTable_map::ATTR_ZERO; |
| 154 | break; |
| 155 | |
| 156 | case Plural::One: |
| 157 | q.id = android::ResTable_map::ATTR_ONE; |
| 158 | break; |
| 159 | |
| 160 | case Plural::Two: |
| 161 | q.id = android::ResTable_map::ATTR_TWO; |
| 162 | break; |
| 163 | |
| 164 | case Plural::Few: |
| 165 | q.id = android::ResTable_map::ATTR_FEW; |
| 166 | break; |
| 167 | |
| 168 | case Plural::Many: |
| 169 | q.id = android::ResTable_map::ATTR_MANY; |
| 170 | break; |
| 171 | |
| 172 | case Plural::Other: |
| 173 | q.id = android::ResTable_map::ATTR_OTHER; |
| 174 | break; |
| 175 | |
| 176 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 177 | LOG(FATAL) << "unhandled plural type"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | |
| 181 | Reference key(q); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 182 | FlattenEntry(&key, plural->values[i].get()); |
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 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 185 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Call this after visiting a Value. This will finish any work that |
| 188 | * needs to be done to prepare the entry. |
| 189 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 190 | void Finish() { out_entry_->count = util::HostToDevice32(entry_count_); } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 191 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 193 | DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor); |
| 194 | |
| 195 | void FlattenKey(Reference* key, ResTable_map* out_entry) { |
| 196 | CHECK(bool(key->id)) << "key has no ID"; |
| 197 | out_entry->name.ident = util::HostToDevice32(key->id.value().id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 199 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 200 | void FlattenValue(Item* value, ResTable_map* out_entry) { |
| 201 | CHECK(value->Flatten(&out_entry->value)) << "flatten failed"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 204 | void FlattenEntry(Reference* key, Item* value) { |
| 205 | ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>(); |
| 206 | FlattenKey(key, out_entry); |
| 207 | FlattenValue(value, out_entry); |
| 208 | out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value)); |
| 209 | entry_count_++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 212 | ResTable_entry_ext* out_entry_; |
| 213 | BigBuffer* buffer_; |
| 214 | size_t entry_count_ = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 217 | class PackageFlattener { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | public: |
| 219 | PackageFlattener(IDiagnostics* diag, ResourceTablePackage* package) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 220 | : diag_(diag), package_(package) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 221 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 222 | bool FlattenPackage(BigBuffer* buffer) { |
| 223 | ChunkWriter pkg_writer(buffer); |
| 224 | ResTable_package* pkg_header = |
| 225 | pkg_writer.StartChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE); |
| 226 | pkg_header->id = util::HostToDevice32(package_->id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 228 | if (package_->name.size() >= arraysize(pkg_header->name)) { |
| 229 | diag_->Error(DiagMessage() << "package name '" << package_->name |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | << "' is too long"); |
| 231 | return false; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | // Copy the package name in device endianness. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 235 | strcpy16_htod(pkg_header->name, arraysize(pkg_header->name), |
| 236 | util::Utf8ToUtf16(package_->name)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 237 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | // Serialize the types. We do this now so that our type and key strings |
| 239 | // are populated. We write those first. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 240 | BigBuffer type_buffer(1024); |
| 241 | FlattenTypes(&type_buffer); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 242 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 243 | pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size()); |
| 244 | StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 245 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 246 | pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size()); |
| 247 | StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_); |
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 | // Append the types. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 250 | buffer->AppendBuffer(std::move(type_buffer)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 251 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 252 | pkg_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 253 | return true; |
| 254 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 255 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 257 | DISALLOW_COPY_AND_ASSIGN(PackageFlattener); |
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> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 260 | T* WriteEntry(FlatEntry* entry, BigBuffer* buffer) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 265 | T* result = buffer->NextBlock<T>(); |
| 266 | ResTable_entry* out_entry = (ResTable_entry*)result; |
| 267 | if (entry->entry->symbol_status.state == SymbolState::kPublic) { |
| 268 | out_entry->flags |= ResTable_entry::FLAG_PUBLIC; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 271 | if (entry->value->IsWeak()) { |
| 272 | out_entry->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) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 276 | out_entry->flags |= ResTable_entry::FLAG_COMPLEX; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 279 | out_entry->flags = util::HostToDevice16(out_entry->flags); |
| 280 | out_entry->key.index = util::HostToDevice32(entry->entry_key); |
| 281 | out_entry->size = util::HostToDevice16(sizeof(T)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | return result; |
| 283 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 284 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 285 | 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 | CHECK(item->Flatten(outValue)) << "flatten failed"; |
| 290 | outValue->size = util::HostToDevice16(sizeof(*outValue)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 292 | ResTable_entry_ext* out_entry = |
| 293 | WriteEntry<ResTable_entry_ext, false>(entry, buffer); |
| 294 | MapFlattenVisitor visitor(out_entry, buffer); |
| 295 | entry->value->Accept(&visitor); |
| 296 | visitor.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 297 | } |
| 298 | return true; |
| 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 | bool FlattenConfig(const ResourceTableType* type, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 302 | const ConfigDescription& config, |
| 303 | std::vector<FlatEntry>* entries, BigBuffer* buffer) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 304 | ChunkWriter type_writer(buffer); |
| 305 | ResTable_type* type_header = |
| 306 | type_writer.StartChunk<ResTable_type>(RES_TABLE_TYPE_TYPE); |
| 307 | type_header->id = type->id.value(); |
| 308 | type_header->config = config; |
| 309 | type_header->config.swapHtoD(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 310 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 311 | auto max_accum = [](uint32_t max, |
| 312 | const std::unique_ptr<ResourceEntry>& a) -> uint32_t { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 313 | return std::max(max, (uint32_t)a->id.value()); |
| 314 | }; |
| 315 | |
| 316 | // Find the largest entry ID. That is how many entries we will have. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 317 | const uint32_t entry_count = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 318 | std::accumulate(type->entries.begin(), type->entries.end(), 0, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 319 | max_accum) + |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 320 | 1; |
| 321 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 322 | type_header->entryCount = util::HostToDevice32(entry_count); |
| 323 | uint32_t* indices = type_writer.NextBlock<uint32_t>(entry_count); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 324 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 325 | CHECK((size_t)entry_count <= std::numeric_limits<uint16_t>::max()); |
| 326 | memset(indices, 0xff, entry_count * sizeof(uint32_t)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 327 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 328 | type_header->entriesStart = util::HostToDevice32(type_writer.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 329 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 330 | const size_t entry_start = type_writer.buffer()->size(); |
| 331 | for (FlatEntry& flat_entry : *entries) { |
| 332 | CHECK(flat_entry.entry->id.value() < entry_count); |
| 333 | indices[flat_entry.entry->id.value()] = |
| 334 | util::HostToDevice32(type_writer.buffer()->size() - entry_start); |
| 335 | if (!FlattenValue(&flat_entry, type_writer.buffer())) { |
| 336 | diag_->Error(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 337 | << "failed to flatten resource '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 338 | << ResourceNameRef(package_->name, type->type, |
| 339 | flat_entry.entry->name) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 340 | << "' for configuration '" << config << "'"); |
| 341 | return false; |
| 342 | } |
| 343 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 344 | type_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 348 | std::vector<ResourceTableType*> CollectAndSortTypes() { |
| 349 | std::vector<ResourceTableType*> sorted_types; |
| 350 | for (auto& type : package_->types) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 351 | if (type->type == ResourceType::kStyleable) { |
| 352 | // Styleables aren't real Resource Types, they are represented in the |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 353 | // R.java file. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 354 | continue; |
| 355 | } |
| 356 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 357 | CHECK(bool(type->id)) << "type must have an ID set"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 359 | sorted_types.push_back(type.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 360 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 361 | std::sort(sorted_types.begin(), sorted_types.end(), |
| 362 | cmp_ids<ResourceTableType>); |
| 363 | return sorted_types; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 366 | std::vector<ResourceEntry*> CollectAndSortEntries(ResourceTableType* type) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 367 | // Sort the entries by entry ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 368 | std::vector<ResourceEntry*> sorted_entries; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 | for (auto& entry : type->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 370 | CHECK(bool(entry->id)) << "entry must have an ID set"; |
| 371 | sorted_entries.push_back(entry.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 372 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 373 | std::sort(sorted_entries.begin(), sorted_entries.end(), |
| 374 | cmp_ids<ResourceEntry>); |
| 375 | return sorted_entries; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 378 | bool FlattenTypeSpec(ResourceTableType* type, |
| 379 | std::vector<ResourceEntry*>* sorted_entries, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 380 | BigBuffer* buffer) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 381 | ChunkWriter type_spec_writer(buffer); |
| 382 | ResTable_typeSpec* spec_header = |
| 383 | type_spec_writer.StartChunk<ResTable_typeSpec>( |
| 384 | RES_TABLE_TYPE_SPEC_TYPE); |
| 385 | spec_header->id = type->id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 386 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 387 | if (sorted_entries->empty()) { |
| 388 | type_spec_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 389 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 392 | // We can't just take the size of the vector. There may be holes in the |
| 393 | // entry ID space. |
| 394 | // Since the entries are sorted by ID, the last one will be the biggest. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 395 | const size_t num_entries = sorted_entries->back()->id.value() + 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 397 | spec_header->entryCount = util::HostToDevice32(num_entries); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 398 | |
| 399 | // Reserve space for the masks of each resource in this type. These |
| 400 | // show for which configuration axis the resource changes. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 401 | uint32_t* config_masks = type_spec_writer.NextBlock<uint32_t>(num_entries); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 402 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 403 | const size_t actual_num_entries = sorted_entries->size(); |
| 404 | for (size_t entryIndex = 0; entryIndex < actual_num_entries; entryIndex++) { |
| 405 | ResourceEntry* entry = sorted_entries->at(entryIndex); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 406 | |
| 407 | // Populate the config masks for this entry. |
| 408 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 409 | if (entry->symbol_status.state == SymbolState::kPublic) { |
| 410 | config_masks[entry->id.value()] |= |
| 411 | util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 414 | const size_t config_count = entry->values.size(); |
| 415 | for (size_t i = 0; i < config_count; i++) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 416 | const ConfigDescription& config = entry->values[i]->config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 417 | for (size_t j = i + 1; j < config_count; j++) { |
| 418 | config_masks[entry->id.value()] |= |
| 419 | util::HostToDevice32(config.diff(entry->values[j]->config)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 420 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 421 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 422 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 423 | type_spec_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 424 | return true; |
| 425 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 426 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 427 | bool FlattenTypes(BigBuffer* buffer) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 428 | // Sort the types by their IDs. They will be inserted into the StringPool in |
| 429 | // this order. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 430 | std::vector<ResourceTableType*> sorted_types = CollectAndSortTypes(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 431 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 432 | size_t expected_type_id = 1; |
| 433 | for (ResourceTableType* type : sorted_types) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 434 | // If there is a gap in the type IDs, fill in the StringPool |
| 435 | // with empty values until we reach the ID we expect. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 436 | while (type->id.value() > expected_type_id) { |
| 437 | std::stringstream type_name; |
| 438 | type_name << "?" << expected_type_id; |
| 439 | type_pool_.MakeRef(type_name.str()); |
| 440 | expected_type_id++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 441 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 442 | expected_type_id++; |
| 443 | type_pool_.MakeRef(ToString(type->type)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 444 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 445 | std::vector<ResourceEntry*> sorted_entries = CollectAndSortEntries(type); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 446 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 447 | if (!FlattenTypeSpec(type, &sorted_entries, buffer)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 448 | return false; |
| 449 | } |
| 450 | |
| 451 | // The binary resource table lists resource entries for each |
| 452 | // configuration. |
| 453 | // We store them inverted, where a resource entry lists the values for |
| 454 | // each |
| 455 | // configuration available. Here we reverse this to match the binary |
| 456 | // table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 457 | std::map<ConfigDescription, std::vector<FlatEntry>> |
| 458 | config_to_entry_list_map; |
| 459 | for (ResourceEntry* entry : sorted_entries) { |
| 460 | const uint32_t key_index = |
| 461 | (uint32_t)key_pool_.MakeRef(entry->name).index(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 462 | |
| 463 | // Group values by configuration. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 464 | for (auto& config_value : entry->values) { |
| 465 | config_to_entry_list_map[config_value->config].push_back( |
| 466 | FlatEntry{entry, config_value->value.get(), key_index}); |
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. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 471 | for (auto& entry : config_to_entry_list_map) { |
| 472 | if (!FlattenConfig(type, entry.first, &entry.second, buffer)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 479 | |
| 480 | IDiagnostics* diag_; |
| 481 | ResourceTablePackage* package_; |
| 482 | StringPool type_pool_; |
| 483 | StringPool key_pool_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 484 | }; |
| 485 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 486 | } // namespace |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 487 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 488 | bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 489 | // We must do this before writing the resources, since the string pool IDs may |
| 490 | // change. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 491 | table->string_pool.Sort( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 492 | [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 493 | int diff = a.context.priority - b.context.priority; |
| 494 | if (diff < 0) return true; |
| 495 | if (diff > 0) return false; |
| 496 | diff = a.context.config.compare(b.context.config); |
| 497 | if (diff < 0) return true; |
| 498 | if (diff > 0) return false; |
| 499 | return a.value < b.value; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 500 | }); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 501 | table->string_pool.Prune(); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 502 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 503 | // Write the ResTable header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 504 | ChunkWriter table_writer(buffer_); |
| 505 | ResTable_header* table_header = |
| 506 | table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE); |
| 507 | table_header->packageCount = util::HostToDevice32(table->packages.size()); |
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 the values string pool. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 510 | StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 511 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 512 | BigBuffer package_buffer(1024); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 513 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 514 | // Flatten each package. |
| 515 | for (auto& package : table->packages) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 516 | PackageFlattener flattener(context->GetDiagnostics(), package.get()); |
| 517 | if (!flattener.FlattenPackage(&package_buffer)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 518 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 519 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 520 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 521 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 522 | // Finally merge all the packages into the main buffer. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 523 | table_writer.buffer()->AppendBuffer(std::move(package_buffer)); |
| 524 | table_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 525 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 528 | } // namespace aapt |