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