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