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 | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 17 | #include "format/binary/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 | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 26 | #include "android-base/stringprintf.h" |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 27 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | |
| 29 | #include "ResourceTable.h" |
| 30 | #include "ResourceValues.h" |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 31 | #include "SdkConstants.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | #include "ValueVisitor.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 33 | #include "format/binary/ChunkWriter.h" |
| 34 | #include "format/binary/ResourceTypeExtensions.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | #include "util/BigBuffer.h" |
| 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | using namespace android; |
| 38 | |
| 39 | namespace aapt { |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | template <typename T> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | static bool cmp_ids(const T* a, const T* b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | return a->id.value() < b->id.value(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | 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] | 49 | if (len == 0) { |
| 50 | return; |
| 51 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | size_t i; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | const char16_t* src_data = src.data(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | for (i = 0; i < len - 1 && i < src.size(); i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | dst[i] = util::HostToDevice16((uint16_t)src_data[i]); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | } |
| 58 | dst[i] = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | 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] | 62 | if (a.key.id) { |
| 63 | if (b.key.id) { |
| 64 | return a.key.id.value() < b.key.id.value(); |
| 65 | } |
| 66 | return true; |
| 67 | } else if (!b.key.id) { |
| 68 | return a.key.name.value() < b.key.name.value(); |
| 69 | } |
| 70 | return false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | struct FlatEntry { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | ResourceEntry* entry; |
| 75 | Value* value; |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | // The entry string pool index to the entry's name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | uint32_t entry_key; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 81 | class MapFlattenVisitor : public ValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | public: |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 83 | using ValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 84 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | MapFlattenVisitor(ResTable_entry_ext* out_entry, BigBuffer* buffer) |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 86 | : out_entry_(out_entry), buffer_(buffer) { |
| 87 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | void Visit(Attribute* attr) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | { |
| 91 | Reference key = Reference(ResourceId(ResTable_map::ATTR_TYPE)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, attr->type_mask); |
| 93 | FlattenEntry(&key, &val); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | if (attr->min_int != std::numeric_limits<int32_t>::min()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | Reference key = Reference(ResourceId(ResTable_map::ATTR_MIN)); |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 98 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->min_int)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | FlattenEntry(&key, &val); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | if (attr->max_int != std::numeric_limits<int32_t>::max()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | Reference key = Reference(ResourceId(ResTable_map::ATTR_MAX)); |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 104 | BinaryPrimitive val(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(attr->max_int)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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 { |
Ryan Mitchell | 0f7da5e | 2018-08-16 16:10:00 -0700 | [diff] [blame] | 137 | const size_t count = array->elements.size(); |
| 138 | for (size_t i = 0; i < count; i++) { |
| 139 | Reference key(android::ResTable_map::ATTR_MIN + i); |
| 140 | FlattenEntry(&key, array->elements[i].get()); |
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 | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 191 | void Finish() { |
| 192 | out_entry_->count = util::HostToDevice32(entry_count_); |
| 193 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 194 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | DISALLOW_COPY_AND_ASSIGN(MapFlattenVisitor); |
| 197 | |
| 198 | void FlattenKey(Reference* key, ResTable_map* out_entry) { |
| 199 | CHECK(bool(key->id)) << "key has no ID"; |
| 200 | out_entry->name.ident = util::HostToDevice32(key->id.value().id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 202 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 203 | void FlattenValue(Item* value, ResTable_map* out_entry) { |
| 204 | CHECK(value->Flatten(&out_entry->value)) << "flatten failed"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | void FlattenEntry(Reference* key, Item* value) { |
| 208 | ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>(); |
| 209 | FlattenKey(key, out_entry); |
| 210 | FlattenValue(value, out_entry); |
| 211 | out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value)); |
| 212 | entry_count_++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | ResTable_entry_ext* out_entry_; |
| 216 | BigBuffer* buffer_; |
| 217 | size_t entry_count_ = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 220 | struct OverlayableChunk { |
| 221 | std::string actor; |
| 222 | Source source; |
| 223 | std::map<OverlayableItem::PolicyFlags, std::set<ResourceId>> policy_ids; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 224 | }; |
| 225 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 226 | class PackageFlattener { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | public: |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 228 | PackageFlattener(IAaptContext* context, ResourceTablePackage* package, |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 229 | const std::map<size_t, std::string>* shared_libs, bool use_sparse_entries, |
| 230 | bool collapse_key_stringpool, const std::set<std::string>& whitelisted_resources) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 231 | : context_(context), |
| 232 | diag_(context->GetDiagnostics()), |
| 233 | package_(package), |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 234 | shared_libs_(shared_libs), |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 235 | use_sparse_entries_(use_sparse_entries), |
| 236 | collapse_key_stringpool_(collapse_key_stringpool), |
| 237 | whitelisted_resources_(whitelisted_resources) { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 238 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 239 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | bool FlattenPackage(BigBuffer* buffer) { |
| 241 | ChunkWriter pkg_writer(buffer); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 242 | 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] | 243 | pkg_header->id = util::HostToDevice32(package_->id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 245 | // AAPT truncated the package name, so do the same. |
| 246 | // Shared libraries require full package names, so don't truncate theirs. |
| 247 | if (context_->GetPackageType() != PackageType::kApp && |
| 248 | package_->name.size() >= arraysize(pkg_header->name)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | diag_->Error(DiagMessage() << "package name '" << package_->name |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 250 | << "' is too long. " |
| 251 | "Shared libraries cannot have truncated package names"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | return false; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 255 | // Copy the package name in device endianness. |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 256 | 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] | 257 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | // Serialize the types. We do this now so that our type and key strings |
| 259 | // are populated. We write those first. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | BigBuffer type_buffer(1024); |
| 261 | FlattenTypes(&type_buffer); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 262 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 263 | pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size()); |
Ryan Mitchell | a15c2a8 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 264 | StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_, diag_); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 265 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size()); |
Ryan Mitchell | a15c2a8 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 267 | StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_, diag_); |
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 | // Append the types. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | buffer->AppendBuffer(std::move(type_buffer)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 271 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 272 | // If there are libraries (or if the package ID is 0x00), encode a library chunk. |
| 273 | if (package_->id.value() == 0x00 || !shared_libs_->empty()) { |
| 274 | FlattenLibrarySpec(buffer); |
| 275 | } |
| 276 | |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 277 | if (!FlattenOverlayable(buffer)) { |
| 278 | return false; |
| 279 | } |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 280 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | pkg_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | return true; |
| 283 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 284 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 285 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 286 | DISALLOW_COPY_AND_ASSIGN(PackageFlattener); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 287 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 288 | template <typename T, bool IsItem> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 289 | T* WriteEntry(FlatEntry* entry, BigBuffer* buffer) { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 290 | static_assert( |
| 291 | std::is_same<ResTable_entry, T>::value || std::is_same<ResTable_entry_ext, T>::value, |
| 292 | "T must be ResTable_entry or ResTable_entry_ext"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 293 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | T* result = buffer->NextBlock<T>(); |
| 295 | ResTable_entry* out_entry = (ResTable_entry*)result; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 296 | if (entry->entry->visibility.level == Visibility::Level::kPublic) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 297 | out_entry->flags |= ResTable_entry::FLAG_PUBLIC; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | if (entry->value->IsWeak()) { |
| 301 | out_entry->flags |= ResTable_entry::FLAG_WEAK; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 304 | if (!IsItem) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | out_entry->flags |= ResTable_entry::FLAG_COMPLEX; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | out_entry->flags = util::HostToDevice16(out_entry->flags); |
| 309 | out_entry->key.index = util::HostToDevice32(entry->entry_key); |
| 310 | out_entry->size = util::HostToDevice16(sizeof(T)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | return result; |
| 312 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 313 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 314 | bool FlattenValue(FlatEntry* entry, BigBuffer* buffer) { |
| 315 | if (Item* item = ValueCast<Item>(entry->value)) { |
| 316 | WriteEntry<ResTable_entry, true>(entry, buffer); |
| 317 | Res_value* outValue = buffer->NextBlock<Res_value>(); |
| 318 | CHECK(item->Flatten(outValue)) << "flatten failed"; |
| 319 | outValue->size = util::HostToDevice16(sizeof(*outValue)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 320 | } else { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 321 | ResTable_entry_ext* out_entry = WriteEntry<ResTable_entry_ext, false>(entry, buffer); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | MapFlattenVisitor visitor(out_entry, buffer); |
| 323 | entry->value->Accept(&visitor); |
| 324 | visitor.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 325 | } |
| 326 | return true; |
| 327 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 328 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 329 | bool FlattenConfig(const ResourceTableType* type, const ConfigDescription& config, |
| 330 | const size_t num_total_entries, std::vector<FlatEntry>* entries, |
| 331 | BigBuffer* buffer) { |
| 332 | CHECK(num_total_entries != 0); |
| 333 | CHECK(num_total_entries <= std::numeric_limits<uint16_t>::max()); |
| 334 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 335 | ChunkWriter type_writer(buffer); |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 336 | ResTable_type* type_header = type_writer.StartChunk<ResTable_type>(RES_TABLE_TYPE_TYPE); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | type_header->id = type->id.value(); |
| 338 | type_header->config = config; |
| 339 | type_header->config.swapHtoD(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 340 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 341 | std::vector<uint32_t> offsets; |
| 342 | offsets.resize(num_total_entries, 0xffffffffu); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 343 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 344 | BigBuffer values_buffer(512); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | for (FlatEntry& flat_entry : *entries) { |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 346 | CHECK(static_cast<size_t>(flat_entry.entry->id.value()) < num_total_entries); |
| 347 | offsets[flat_entry.entry->id.value()] = values_buffer.size(); |
| 348 | if (!FlattenValue(&flat_entry, &values_buffer)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | diag_->Error(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 350 | << "failed to flatten resource '" |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 351 | << ResourceNameRef(package_->name, type->type, flat_entry.entry->name) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 352 | << "' for configuration '" << config << "'"); |
| 353 | return false; |
| 354 | } |
| 355 | } |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 356 | |
| 357 | bool sparse_encode = use_sparse_entries_; |
| 358 | |
| 359 | // Only sparse encode if the entries will be read on platforms O+. |
| 360 | sparse_encode = |
| 361 | sparse_encode && (context_->GetMinSdkVersion() >= SDK_O || config.sdkVersion >= SDK_O); |
| 362 | |
| 363 | // Only sparse encode if the offsets are representable in 2 bytes. |
| 364 | sparse_encode = |
| 365 | sparse_encode && (values_buffer.size() / 4u) <= std::numeric_limits<uint16_t>::max(); |
| 366 | |
| 367 | // Only sparse encode if the ratio of populated entries to total entries is below some |
| 368 | // threshold. |
| 369 | sparse_encode = |
| 370 | sparse_encode && ((100 * entries->size()) / num_total_entries) < kSparseEncodingThreshold; |
| 371 | |
| 372 | if (sparse_encode) { |
| 373 | type_header->entryCount = util::HostToDevice32(entries->size()); |
| 374 | type_header->flags |= ResTable_type::FLAG_SPARSE; |
| 375 | ResTable_sparseTypeEntry* indices = |
| 376 | type_writer.NextBlock<ResTable_sparseTypeEntry>(entries->size()); |
| 377 | for (size_t i = 0; i < num_total_entries; i++) { |
| 378 | if (offsets[i] != ResTable_type::NO_ENTRY) { |
| 379 | CHECK((offsets[i] & 0x03) == 0); |
| 380 | indices->idx = util::HostToDevice16(i); |
| 381 | indices->offset = util::HostToDevice16(offsets[i] / 4u); |
| 382 | indices++; |
| 383 | } |
| 384 | } |
| 385 | } else { |
| 386 | type_header->entryCount = util::HostToDevice32(num_total_entries); |
| 387 | uint32_t* indices = type_writer.NextBlock<uint32_t>(num_total_entries); |
| 388 | for (size_t i = 0; i < num_total_entries; i++) { |
| 389 | indices[i] = util::HostToDevice32(offsets[i]); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | type_header->entriesStart = util::HostToDevice32(type_writer.size()); |
| 394 | type_writer.buffer()->AppendBuffer(std::move(values_buffer)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 395 | type_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | return true; |
| 397 | } |
| 398 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 399 | std::vector<ResourceTableType*> CollectAndSortTypes() { |
| 400 | std::vector<ResourceTableType*> sorted_types; |
| 401 | for (auto& type : package_->types) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 402 | if (type->type == ResourceType::kStyleable) { |
| 403 | // Styleables aren't real Resource Types, they are represented in the |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 404 | // R.java file. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 405 | continue; |
| 406 | } |
| 407 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 408 | CHECK(bool(type->id)) << "type must have an ID set"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 409 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 410 | sorted_types.push_back(type.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 411 | } |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 412 | std::sort(sorted_types.begin(), sorted_types.end(), cmp_ids<ResourceTableType>); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 413 | return sorted_types; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 416 | std::vector<ResourceEntry*> CollectAndSortEntries(ResourceTableType* type) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 417 | // Sort the entries by entry ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 418 | std::vector<ResourceEntry*> sorted_entries; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 419 | for (auto& entry : type->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 420 | CHECK(bool(entry->id)) << "entry must have an ID set"; |
| 421 | sorted_entries.push_back(entry.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 422 | } |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 423 | std::sort(sorted_entries.begin(), sorted_entries.end(), cmp_ids<ResourceEntry>); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 424 | return sorted_entries; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 427 | bool FlattenOverlayable(BigBuffer* buffer) { |
| 428 | std::set<ResourceId> seen_ids; |
| 429 | std::map<std::string, OverlayableChunk> overlayable_chunks; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 430 | |
| 431 | CHECK(bool(package_->id)) << "package must have an ID set when flattening <overlayable>"; |
| 432 | for (auto& type : package_->types) { |
| 433 | CHECK(bool(type->id)) << "type must have an ID set when flattening <overlayable>"; |
| 434 | for (auto& entry : type->entries) { |
| 435 | CHECK(bool(type->id)) << "entry must have an ID set when flattening <overlayable>"; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 436 | if (!entry->overlayable_item) { |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 437 | continue; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 438 | } |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 439 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 440 | OverlayableItem& item = entry->overlayable_item.value(); |
| 441 | |
| 442 | // Resource ids should only appear once in the resource table |
| 443 | ResourceId id = android::make_resid(package_->id.value(), type->id.value(), |
| 444 | entry->id.value()); |
| 445 | CHECK(seen_ids.find(id) == seen_ids.end()) |
| 446 | << "multiple overlayable definitions found for resource " |
| 447 | << ResourceName(package_->name, type->type, entry->name).to_string(); |
| 448 | seen_ids.insert(id); |
| 449 | |
| 450 | // Find the overlayable chunk with the specified name |
| 451 | OverlayableChunk* overlayable_chunk = nullptr; |
| 452 | auto iter = overlayable_chunks.find(item.overlayable->name); |
| 453 | if (iter == overlayable_chunks.end()) { |
| 454 | OverlayableChunk chunk{item.overlayable->actor, item.overlayable->source}; |
| 455 | overlayable_chunk = |
| 456 | &overlayable_chunks.insert({item.overlayable->name, chunk}).first->second; |
| 457 | } else { |
| 458 | OverlayableChunk& chunk = iter->second; |
| 459 | if (!(chunk.source == item.overlayable->source)) { |
| 460 | // The name of an overlayable set of resources must be unique |
| 461 | context_->GetDiagnostics()->Error(DiagMessage(item.overlayable->source) |
| 462 | << "duplicate overlayable name" |
| 463 | << item.overlayable->name << "'"); |
| 464 | context_->GetDiagnostics()->Error(DiagMessage(chunk.source) |
| 465 | << "previous declaration here"); |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | CHECK(chunk.actor == item.overlayable->actor); |
| 470 | overlayable_chunk = &chunk; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 473 | if (item.policies == 0) { |
| 474 | context_->GetDiagnostics()->Error(DiagMessage(item.overlayable->source) |
| 475 | << "overlayable " |
| 476 | << entry->name |
| 477 | << " does not specify policy"); |
| 478 | return false; |
| 479 | } |
| 480 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 481 | uint32_t policy_flags = 0; |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 482 | if (item.policies & OverlayableItem::Policy::kPublic) { |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 483 | policy_flags |= ResTable_overlayable_policy_header::POLICY_PUBLIC; |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 484 | } |
| 485 | if (item.policies & OverlayableItem::Policy::kSystem) { |
| 486 | policy_flags |= ResTable_overlayable_policy_header::POLICY_SYSTEM_PARTITION; |
| 487 | } |
| 488 | if (item.policies & OverlayableItem::Policy::kVendor) { |
| 489 | policy_flags |= ResTable_overlayable_policy_header::POLICY_VENDOR_PARTITION; |
| 490 | } |
| 491 | if (item.policies & OverlayableItem::Policy::kProduct) { |
| 492 | policy_flags |= ResTable_overlayable_policy_header::POLICY_PRODUCT_PARTITION; |
| 493 | } |
| 494 | if (item.policies & OverlayableItem::Policy::kSignature) { |
| 495 | policy_flags |= ResTable_overlayable_policy_header::POLICY_SIGNATURE; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 498 | auto policy = overlayable_chunk->policy_ids.find(policy_flags); |
| 499 | if (policy != overlayable_chunk->policy_ids.end()) { |
| 500 | policy->second.insert(id); |
| 501 | } else { |
| 502 | overlayable_chunk->policy_ids.insert( |
| 503 | std::make_pair(policy_flags, std::set<ResourceId>{id})); |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 504 | } |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 508 | for (auto& overlayable_pair : overlayable_chunks) { |
| 509 | std::string name = overlayable_pair.first; |
| 510 | OverlayableChunk& overlayable = overlayable_pair.second; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 511 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 512 | // Write the header of the overlayable chunk |
| 513 | ChunkWriter overlayable_writer(buffer); |
| 514 | auto* overlayable_type = |
| 515 | overlayable_writer.StartChunk<ResTable_overlayable_header>(RES_TABLE_OVERLAYABLE_TYPE); |
| 516 | if (name.size() >= arraysize(overlayable_type->name)) { |
| 517 | diag_->Error(DiagMessage() << "overlayable name '" << name |
| 518 | << "' exceeds maximum length (" |
| 519 | << arraysize(overlayable_type->name) |
| 520 | << " utf16 characters)"); |
| 521 | return false; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 522 | } |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 523 | strcpy16_htod(overlayable_type->name, arraysize(overlayable_type->name), |
| 524 | util::Utf8ToUtf16(name)); |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 525 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 526 | if (overlayable.actor.size() >= arraysize(overlayable_type->actor)) { |
| 527 | diag_->Error(DiagMessage() << "overlayable name '" << overlayable.actor |
| 528 | << "' exceeds maximum length (" |
| 529 | << arraysize(overlayable_type->actor) |
| 530 | << " utf16 characters)"); |
| 531 | return false; |
| 532 | } |
| 533 | strcpy16_htod(overlayable_type->actor, arraysize(overlayable_type->actor), |
| 534 | util::Utf8ToUtf16(overlayable.actor)); |
| 535 | |
| 536 | // Write each policy block for the overlayable |
| 537 | for (auto& policy_ids : overlayable.policy_ids) { |
| 538 | ChunkWriter policy_writer(buffer); |
| 539 | auto* policy_type = policy_writer.StartChunk<ResTable_overlayable_policy_header>( |
| 540 | RES_TABLE_OVERLAYABLE_POLICY_TYPE); |
| 541 | policy_type->policy_flags = util::HostToDevice32(static_cast<uint32_t>(policy_ids.first)); |
| 542 | policy_type->entry_count = util::HostToDevice32(static_cast<uint32_t>( |
| 543 | policy_ids.second.size())); |
| 544 | // Write the ids after the policy header |
| 545 | auto* id_block = policy_writer.NextBlock<ResTable_ref>(policy_ids.second.size()); |
| 546 | for (const ResourceId& id : policy_ids.second) { |
| 547 | id_block->ident = util::HostToDevice32(id.id); |
| 548 | id_block++; |
| 549 | } |
| 550 | policy_writer.Finish(); |
| 551 | } |
| 552 | overlayable_writer.Finish(); |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 555 | return true; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 558 | bool FlattenTypeSpec(ResourceTableType* type, std::vector<ResourceEntry*>* sorted_entries, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 559 | BigBuffer* buffer) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 560 | ChunkWriter type_spec_writer(buffer); |
| 561 | ResTable_typeSpec* spec_header = |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 562 | type_spec_writer.StartChunk<ResTable_typeSpec>(RES_TABLE_TYPE_SPEC_TYPE); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 563 | spec_header->id = type->id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 564 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 565 | if (sorted_entries->empty()) { |
| 566 | type_spec_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 567 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 570 | // We can't just take the size of the vector. There may be holes in the |
| 571 | // entry ID space. |
| 572 | // 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] | 573 | const size_t num_entries = sorted_entries->back()->id.value() + 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 574 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 575 | spec_header->entryCount = util::HostToDevice32(num_entries); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 576 | |
| 577 | // Reserve space for the masks of each resource in this type. These |
| 578 | // show for which configuration axis the resource changes. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 579 | uint32_t* config_masks = type_spec_writer.NextBlock<uint32_t>(num_entries); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 580 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 581 | const size_t actual_num_entries = sorted_entries->size(); |
| 582 | for (size_t entryIndex = 0; entryIndex < actual_num_entries; entryIndex++) { |
| 583 | ResourceEntry* entry = sorted_entries->at(entryIndex); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 584 | |
| 585 | // Populate the config masks for this entry. |
| 586 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 587 | if (entry->visibility.level == Visibility::Level::kPublic) { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 588 | config_masks[entry->id.value()] |= util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 591 | const size_t config_count = entry->values.size(); |
| 592 | for (size_t i = 0; i < config_count; i++) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 593 | const ConfigDescription& config = entry->values[i]->config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 594 | for (size_t j = i + 1; j < config_count; j++) { |
| 595 | config_masks[entry->id.value()] |= |
| 596 | util::HostToDevice32(config.diff(entry->values[j]->config)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 597 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 598 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 599 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 600 | type_spec_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 601 | return true; |
| 602 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 603 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 604 | bool FlattenTypes(BigBuffer* buffer) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 605 | // Sort the types by their IDs. They will be inserted into the StringPool in |
| 606 | // this order. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 607 | std::vector<ResourceTableType*> sorted_types = CollectAndSortTypes(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 608 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 609 | size_t expected_type_id = 1; |
| 610 | for (ResourceTableType* type : sorted_types) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 611 | // If there is a gap in the type IDs, fill in the StringPool |
| 612 | // with empty values until we reach the ID we expect. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 613 | while (type->id.value() > expected_type_id) { |
| 614 | std::stringstream type_name; |
| 615 | type_name << "?" << expected_type_id; |
| 616 | type_pool_.MakeRef(type_name.str()); |
| 617 | expected_type_id++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 618 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 619 | expected_type_id++; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 620 | type_pool_.MakeRef(to_string(type->type)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 621 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 622 | std::vector<ResourceEntry*> sorted_entries = CollectAndSortEntries(type); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 623 | if (sorted_entries.empty()) { |
| 624 | continue; |
| 625 | } |
| 626 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 627 | if (!FlattenTypeSpec(type, &sorted_entries, buffer)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 628 | return false; |
| 629 | } |
| 630 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 631 | // Since the entries are sorted by ID, the last ID will be the largest. |
| 632 | const size_t num_entries = sorted_entries.back()->id.value() + 1; |
| 633 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 634 | // The binary resource table lists resource entries for each |
| 635 | // configuration. |
| 636 | // We store them inverted, where a resource entry lists the values for |
| 637 | // each |
| 638 | // configuration available. Here we reverse this to match the binary |
| 639 | // table. |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 640 | std::map<ConfigDescription, std::vector<FlatEntry>> config_to_entry_list_map; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 641 | |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 642 | // hardcoded string uses characters which make it an invalid resource name |
| 643 | const std::string obfuscated_resource_name = "0_resource_name_obfuscated"; |
| 644 | |
| 645 | for (ResourceEntry* entry : sorted_entries) { |
| 646 | uint32_t local_key_index; |
| 647 | if (!collapse_key_stringpool_ || |
| 648 | whitelisted_resources_.find(entry->name) != whitelisted_resources_.end()) { |
| 649 | local_key_index = (uint32_t)key_pool_.MakeRef(entry->name).index(); |
| 650 | } else { |
| 651 | // resource isn't whitelisted, add it as obfuscated value |
| 652 | local_key_index = (uint32_t)key_pool_.MakeRef(obfuscated_resource_name).index(); |
| 653 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 654 | // Group values by configuration. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 655 | for (auto& config_value : entry->values) { |
| 656 | config_to_entry_list_map[config_value->config].push_back( |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 657 | FlatEntry{entry, config_value->value.get(), local_key_index}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 658 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 659 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 660 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 661 | // Flatten a configuration value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 662 | for (auto& entry : config_to_entry_list_map) { |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 663 | if (!FlattenConfig(type, entry.first, num_entries, &entry.second, buffer)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 664 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 665 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 666 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 667 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 668 | return true; |
| 669 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 670 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 671 | void FlattenLibrarySpec(BigBuffer* buffer) { |
| 672 | ChunkWriter lib_writer(buffer); |
| 673 | ResTable_lib_header* lib_header = |
| 674 | lib_writer.StartChunk<ResTable_lib_header>(RES_TABLE_LIBRARY_TYPE); |
| 675 | |
| 676 | const size_t num_entries = (package_->id.value() == 0x00 ? 1 : 0) + shared_libs_->size(); |
| 677 | CHECK(num_entries > 0); |
| 678 | |
| 679 | lib_header->count = util::HostToDevice32(num_entries); |
| 680 | |
| 681 | ResTable_lib_entry* lib_entry = buffer->NextBlock<ResTable_lib_entry>(num_entries); |
| 682 | if (package_->id.value() == 0x00) { |
| 683 | // Add this package |
| 684 | lib_entry->packageId = util::HostToDevice32(0x00); |
| 685 | strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName), |
| 686 | util::Utf8ToUtf16(package_->name)); |
| 687 | ++lib_entry; |
| 688 | } |
| 689 | |
| 690 | for (auto& map_entry : *shared_libs_) { |
| 691 | lib_entry->packageId = util::HostToDevice32(map_entry.first); |
| 692 | strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName), |
| 693 | util::Utf8ToUtf16(map_entry.second)); |
| 694 | ++lib_entry; |
| 695 | } |
| 696 | lib_writer.Finish(); |
| 697 | } |
| 698 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 699 | IAaptContext* context_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 700 | IDiagnostics* diag_; |
| 701 | ResourceTablePackage* package_; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 702 | const std::map<size_t, std::string>* shared_libs_; |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 703 | bool use_sparse_entries_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 704 | StringPool type_pool_; |
| 705 | StringPool key_pool_; |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 706 | bool collapse_key_stringpool_; |
| 707 | const std::set<std::string>& whitelisted_resources_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 708 | }; |
| 709 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 710 | } // namespace |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 711 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 712 | bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) { |
Ryan Mitchell | 90b7a08 | 2019-02-15 17:39:58 +0000 | [diff] [blame] | 713 | // We must do this before writing the resources, since the string pool IDs may change. |
| 714 | table->string_pool.Prune(); |
| 715 | table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int { |
| 716 | int diff = util::compare(a.priority, b.priority); |
| 717 | if (diff == 0) { |
| 718 | diff = a.config.compare(b.config); |
| 719 | } |
| 720 | return diff; |
| 721 | }); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 722 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 723 | // Write the ResTable header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 724 | ChunkWriter table_writer(buffer_); |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 725 | ResTable_header* table_header = table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 726 | table_header->packageCount = util::HostToDevice32(table->packages.size()); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 727 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 728 | // Flatten the values string pool. |
Ryan Mitchell | a15c2a8 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 729 | StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool, |
| 730 | context->GetDiagnostics()); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 731 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 732 | BigBuffer package_buffer(1024); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 733 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 734 | // Flatten each package. |
| 735 | for (auto& package : table->packages) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 736 | if (context->GetPackageType() == PackageType::kApp) { |
| 737 | // Write a self mapping entry for this package if the ID is non-standard (0x7f). |
| 738 | const uint8_t package_id = package->id.value(); |
| 739 | if (package_id != kFrameworkPackageId && package_id != kAppPackageId) { |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 740 | auto result = table->included_packages_.insert({package_id, package->name}); |
| 741 | if (!result.second && result.first->second != package->name) { |
| 742 | // A mapping for this package ID already exists, and is a different package. Error! |
| 743 | context->GetDiagnostics()->Error( |
| 744 | DiagMessage() << android::base::StringPrintf( |
| 745 | "can't map package ID %02x to '%s'. Already mapped to '%s'", package_id, |
| 746 | package->name.c_str(), result.first->second.c_str())); |
| 747 | return false; |
| 748 | } |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 752 | PackageFlattener flattener(context, package.get(), &table->included_packages_, |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 753 | options_.use_sparse_entries, options_.collapse_key_stringpool, |
| 754 | options_.whitelisted_resources); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 755 | if (!flattener.FlattenPackage(&package_buffer)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 756 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 757 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 758 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 759 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 760 | // Finally merge all the packages into the main buffer. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 761 | table_writer.buffer()->AppendBuffer(std::move(package_buffer)); |
| 762 | table_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 763 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 766 | } // namespace aapt |