blob: e98d2d758df472a6bf43ff82d06a1dd6206a38a7 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskicacb28f2016-10-19 12:18:14 -070017#include "flatten/XmlFlattener.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <algorithm>
20#include <map>
21#include <vector>
22
23#include "android-base/logging.h"
24#include "android-base/macros.h"
25#include "androidfw/ResourceTypes.h"
26#include "utils/misc.h"
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "SdkConstants.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "flatten/ChunkWriter.h"
30#include "flatten/ResourceTypeExtensions.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080031#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033using namespace android;
34
35namespace aapt {
36
37namespace {
38
39constexpr uint32_t kLowPriority = 0xffffffffu;
40
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041static bool cmp_xml_attribute_by_id(const xml::Attribute* a,
42 const xml::Attribute* b) {
43 if (a->compiled_attribute && a->compiled_attribute.value().id) {
44 if (b->compiled_attribute && b->compiled_attribute.value().id) {
45 return a->compiled_attribute.value().id.value() <
46 b->compiled_attribute.value().id.value();
47 }
48 return true;
49 } else if (!b->compiled_attribute) {
50 int diff = a->namespace_uri.compare(b->namespace_uri);
51 if (diff < 0) {
52 return true;
53 } else if (diff > 0) {
54 return false;
55 }
56 return a->name < b->name;
57 }
58 return false;
59}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061class XmlFlattenerVisitor : public xml::Visitor {
62 public:
63 using xml::Visitor::Visit;
64
65 StringPool pool;
66 std::map<uint8_t, StringPool> package_pools;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 struct StringFlattenDest {
69 StringPool::Ref ref;
70 ResStringPool_ref* dest;
71 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::vector<StringFlattenDest> string_refs;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 XmlFlattenerVisitor(BigBuffer* buffer, XmlFlattenerOptions options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 : buffer_(buffer), options_(options) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 void Visit(xml::Namespace* node) override {
79 if (node->namespace_uri == xml::kSchemaTools) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 // Skip dedicated tools namespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 xml::Visitor::Visit(node);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 WriteNamespace(node, android::RES_XML_START_NAMESPACE_TYPE);
84 xml::Visitor::Visit(node);
85 WriteNamespace(node, android::RES_XML_END_NAMESPACE_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 }
87 }
88
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 void Visit(xml::Text* node) override {
90 if (util::TrimWhitespace(node->text).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 // Skip whitespace only text nodes.
92 return;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093 }
94
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 ChunkWriter writer(buffer_);
96 ResXMLTree_node* flat_node =
97 writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE);
98 flat_node->lineNumber = util::HostToDevice32(node->line_number);
99 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
Adam Lesinski48448e82017-04-26 15:13:52 -0700102
103 // Process plain strings to make sure they get properly escaped.
104 util::StringBuilder builder;
105 builder.Append(node->text);
106 AddString(builder.ToString(), kLowPriority, &flat_text->data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 }
110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 void Visit(xml::Element* node) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 ChunkWriter start_writer(buffer_);
114 ResXMLTree_node* flat_node =
115 start_writer.StartChunk<ResXMLTree_node>(RES_XML_START_ELEMENT_TYPE);
116 flat_node->lineNumber = util::HostToDevice32(node->line_number);
117 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 ResXMLTree_attrExt* flat_elem =
120 start_writer.NextBlock<ResXMLTree_attrExt>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121
122 // A missing namespace must be null, not an empty string. Otherwise the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 // runtime complains.
124 AddString(node->namespace_uri, kLowPriority, &flat_elem->ns,
125 true /* treat_empty_string_as_null */);
126 AddString(node->name, kLowPriority, &flat_elem->name,
127 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem));
130 flat_elem->attributeSize =
131 util::HostToDevice16(sizeof(ResXMLTree_attribute));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 WriteAttributes(node, flat_elem, &start_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 start_writer.Finish();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136 }
137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 xml::Visitor::Visit(node);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
140 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 ChunkWriter end_writer(buffer_);
142 ResXMLTree_node* flat_end_node =
143 end_writer.StartChunk<ResXMLTree_node>(RES_XML_END_ELEMENT_TYPE);
144 flat_end_node->lineNumber = util::HostToDevice32(node->line_number);
145 flat_end_node->comment.index = util::HostToDevice32(-1);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 ResXMLTree_endElementExt* flat_end_elem =
148 end_writer.NextBlock<ResXMLTree_endElementExt>();
149 AddString(node->namespace_uri, kLowPriority, &flat_end_elem->ns,
150 true /* treat_empty_string_as_null */);
151 AddString(node->name, kLowPriority, &flat_end_elem->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 end_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
155 }
156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 private:
158 DISALLOW_COPY_AND_ASSIGN(XmlFlattenerVisitor);
159
160 void AddString(const StringPiece& str, uint32_t priority,
161 android::ResStringPool_ref* dest,
162 bool treat_empty_string_as_null = false) {
163 if (str.empty() && treat_empty_string_as_null) {
164 // Some parts of the runtime treat null differently than empty string.
165 dest->index = util::DeviceToHost32(-1);
166 } else {
167 string_refs.push_back(StringFlattenDest{
168 pool.MakeRef(str, StringPool::Context(priority)), dest});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 }
171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) {
173 string_refs.push_back(StringFlattenDest{ref, dest});
174 }
175
176 void WriteNamespace(xml::Namespace* node, uint16_t type) {
177 ChunkWriter writer(buffer_);
178
179 ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type);
180 flatNode->lineNumber = util::HostToDevice32(node->line_number);
181 flatNode->comment.index = util::HostToDevice32(-1);
182
183 ResXMLTree_namespaceExt* flat_ns =
184 writer.NextBlock<ResXMLTree_namespaceExt>();
185 AddString(node->namespace_prefix, kLowPriority, &flat_ns->prefix);
186 AddString(node->namespace_uri, kLowPriority, &flat_ns->uri);
187
188 writer.Finish();
189 }
190
Adam Lesinski48448e82017-04-26 15:13:52 -0700191 void WriteAttributes(xml::Element* node, ResXMLTree_attrExt* flat_elem, ChunkWriter* writer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 filtered_attrs_.clear();
193 filtered_attrs_.reserve(node->attributes.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194
195 // Filter the attributes.
196 for (xml::Attribute& attr : node->attributes) {
Adam Lesinski48448e82017-04-26 15:13:52 -0700197 if (options_.max_sdk_level && attr.compiled_attribute && attr.compiled_attribute.value().id) {
198 size_t sdk_level = FindAttributeSdkLevel(attr.compiled_attribute.value().id.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 if (sdk_level > options_.max_sdk_level.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203 if (attr.namespace_uri == xml::kSchemaTools) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 continue;
205 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 filtered_attrs_.push_back(&attr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207 }
208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 if (filtered_attrs_.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 return;
211 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 const ResourceId kIdAttr(0x010100d0);
214
Adam Lesinski48448e82017-04-26 15:13:52 -0700215 std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 ResXMLTree_attribute* flat_attr =
220 writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size());
221 uint16_t attribute_index = 1;
222 for (const xml::Attribute* xml_attr : filtered_attrs_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 // Assign the indices for specific attributes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 if (xml_attr->compiled_attribute &&
225 xml_attr->compiled_attribute.value().id &&
226 xml_attr->compiled_attribute.value().id.value() == kIdAttr) {
227 flat_elem->idIndex = util::HostToDevice16(attribute_index);
228 } else if (xml_attr->namespace_uri.empty()) {
229 if (xml_attr->name == "class") {
230 flat_elem->classIndex = util::HostToDevice16(attribute_index);
231 } else if (xml_attr->name == "style") {
232 flat_elem->styleIndex = util::HostToDevice16(attribute_index);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 }
234 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 attribute_index++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236
Adam Lesinski48448e82017-04-26 15:13:52 -0700237 // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 // is empty (doesn't exist).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
240 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 flat_attr->rawValue.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243
Adam Lesinski48448e82017-04-26 15:13:52 -0700244 if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) {
245 // The attribute has no associated ResourceID, so the string order doesn't matter.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 AddString(xml_attr->name, kLowPriority, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 } else {
248 // Attribute names are stored without packages, but we use
249 // their StringPool index to lookup their resource IDs.
250 // This will cause collisions, so we can't dedupe
251 // attribute names from different packages. We use separate
252 // pools that we later combine.
253 //
254 // Lookup the StringPool for this package and make the reference there.
Adam Lesinski48448e82017-04-26 15:13:52 -0700255 const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 StringPool::Ref name_ref =
258 package_pools[aapt_attr.id.value().package_id()].MakeRef(
259 xml_attr->name, StringPool::Context(aapt_attr.id.value().id));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260
261 // Add it to the list of strings to flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 AddString(name_ref, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 }
264
Adam Lesinski48448e82017-04-26 15:13:52 -0700265 // Process plain strings to make sure they get properly escaped.
266 StringPiece raw_value = xml_attr->value;
267 util::StringBuilder str_builder;
268 if (!options_.keep_raw_values) {
269 str_builder.Append(xml_attr->value);
270 raw_value = str_builder.ToString();
271 }
272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 if (options_.keep_raw_values || !xml_attr->compiled_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 // Keep raw values if the value is not compiled or
275 // if we're building a static library (need symbols).
Adam Lesinski48448e82017-04-26 15:13:52 -0700276 AddString(raw_value, kLowPriority, &flat_attr->rawValue);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 }
278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 if (xml_attr->compiled_value) {
280 CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 } else {
282 // Flatten as a regular string type.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING;
Adam Lesinski48448e82017-04-26 15:13:52 -0700284
285 AddString(str_builder.ToString(), kLowPriority,
286 (ResStringPool_ref*) &flat_attr->typedValue.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 }
288
Adam Lesinski48448e82017-04-26 15:13:52 -0700289 flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 flat_attr++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 }
292 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293
294 BigBuffer* buffer_;
295 XmlFlattenerOptions options_;
296
297 // Scratch vector to filter attributes. We avoid allocations
298 // making this a member.
299 std::vector<xml::Attribute*> filtered_attrs_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300};
301
302} // namespace
303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304bool XmlFlattener::Flatten(IAaptContext* context, xml::Node* node) {
305 BigBuffer node_buffer(1024);
306 XmlFlattenerVisitor visitor(&node_buffer, options_);
307 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308
309 // Merge the package pools into the main pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 for (auto& package_pool_entry : visitor.package_pools) {
311 visitor.pool.Merge(std::move(package_pool_entry.second));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 }
313
314 // Sort the string pool so that attribute resource IDs show up first.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 visitor.pool.Sort(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
317 return a.context.priority < b.context.priority;
318 });
319
320 // Now we flatten the string pool references into the correct places.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 for (const auto& ref_entry : visitor.string_refs) {
322 ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 }
324
325 // Write the XML header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 ChunkWriter xml_header_writer(buffer_);
327 xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328
329 // Flatten the StringPool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 StringPool::FlattenUtf8(buffer_, visitor.pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331
332 {
333 // Write the array of resource IDs, indexed by StringPool order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 ChunkWriter res_id_map_writer(buffer_);
335 res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE);
336 for (const auto& str : visitor.pool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 ResourceId id = {str->context.priority};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 if (id.id == kLowPriority || !id.is_valid()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 // When we see the first non-resource ID,
340 // we're done.
341 break;
342 }
343
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 *res_id_map_writer.NextBlock<uint32_t>() = id.id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 res_id_map_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 }
348
349 // Move the nodeBuffer and append it to the out buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 buffer_->AppendBuffer(std::move(node_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351
352 // Finish the xml header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 xml_header_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700355}
356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357bool XmlFlattener::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 if (!resource->root) {
359 return false;
360 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 return Flatten(context, resource->root.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700362}
363
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364} // namespace aapt