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