blob: 331ef784a7da08269b7ef19c081ac5d3f4e57c65 [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
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700183 ResXMLTree_namespaceExt* flat_ns = writer.NextBlock<ResXMLTree_namespaceExt>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 AddString(node->namespace_prefix, kLowPriority, &flat_ns->prefix);
185 AddString(node->namespace_uri, kLowPriority, &flat_ns->uri);
186
187 writer.Finish();
188 }
189
Adam Lesinski48448e82017-04-26 15:13:52 -0700190 void WriteAttributes(xml::Element* node, ResXMLTree_attrExt* flat_elem, ChunkWriter* writer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 filtered_attrs_.clear();
192 filtered_attrs_.reserve(node->attributes.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193
194 // Filter the attributes.
195 for (xml::Attribute& attr : node->attributes) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700196 if (attr.namespace_uri != xml::kSchemaTools) {
197 filtered_attrs_.push_back(&attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700199 }
200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 if (filtered_attrs_.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 return;
203 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700204
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 const ResourceId kIdAttr(0x010100d0);
206
Adam Lesinski48448e82017-04-26 15:13:52 -0700207 std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 ResXMLTree_attribute* flat_attr =
212 writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size());
213 uint16_t attribute_index = 1;
214 for (const xml::Attribute* xml_attr : filtered_attrs_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 // Assign the indices for specific attributes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 if (xml_attr->compiled_attribute &&
217 xml_attr->compiled_attribute.value().id &&
218 xml_attr->compiled_attribute.value().id.value() == kIdAttr) {
219 flat_elem->idIndex = util::HostToDevice16(attribute_index);
220 } else if (xml_attr->namespace_uri.empty()) {
221 if (xml_attr->name == "class") {
222 flat_elem->classIndex = util::HostToDevice16(attribute_index);
223 } else if (xml_attr->name == "style") {
224 flat_elem->styleIndex = util::HostToDevice16(attribute_index);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 }
226 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 attribute_index++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228
Adam Lesinski48448e82017-04-26 15:13:52 -0700229 // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 // is empty (doesn't exist).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
232 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 flat_attr->rawValue.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235
Adam Lesinski48448e82017-04-26 15:13:52 -0700236 if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) {
237 // The attribute has no associated ResourceID, so the string order doesn't matter.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 AddString(xml_attr->name, kLowPriority, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 } else {
240 // Attribute names are stored without packages, but we use
241 // their StringPool index to lookup their resource IDs.
242 // This will cause collisions, so we can't dedupe
243 // attribute names from different packages. We use separate
244 // pools that we later combine.
245 //
246 // Lookup the StringPool for this package and make the reference there.
Adam Lesinski48448e82017-04-26 15:13:52 -0700247 const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 StringPool::Ref name_ref =
250 package_pools[aapt_attr.id.value().package_id()].MakeRef(
251 xml_attr->name, StringPool::Context(aapt_attr.id.value().id));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252
253 // Add it to the list of strings to flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 AddString(name_ref, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 }
256
Adam Lesinski48448e82017-04-26 15:13:52 -0700257 // Process plain strings to make sure they get properly escaped.
258 StringPiece raw_value = xml_attr->value;
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700259
260 util::StringBuilder str_builder(true /*preserve_spaces*/);
261 str_builder.Append(xml_attr->value);
262
Adam Lesinski48448e82017-04-26 15:13:52 -0700263 if (!options_.keep_raw_values) {
Adam Lesinski48448e82017-04-26 15:13:52 -0700264 raw_value = str_builder.ToString();
265 }
266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 if (options_.keep_raw_values || !xml_attr->compiled_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 // Keep raw values if the value is not compiled or
269 // if we're building a static library (need symbols).
Adam Lesinski48448e82017-04-26 15:13:52 -0700270 AddString(raw_value, kLowPriority, &flat_attr->rawValue);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 }
272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 if (xml_attr->compiled_value) {
274 CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 } else {
276 // Flatten as a regular string type.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING;
Adam Lesinski48448e82017-04-26 15:13:52 -0700278
279 AddString(str_builder.ToString(), kLowPriority,
280 (ResStringPool_ref*) &flat_attr->typedValue.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 }
282
Adam Lesinski48448e82017-04-26 15:13:52 -0700283 flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 flat_attr++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 }
286 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287
288 BigBuffer* buffer_;
289 XmlFlattenerOptions options_;
290
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700291 // Scratch vector to filter attributes. We avoid allocations making this a member.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 std::vector<xml::Attribute*> filtered_attrs_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293};
294
295} // namespace
296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297bool XmlFlattener::Flatten(IAaptContext* context, xml::Node* node) {
298 BigBuffer node_buffer(1024);
299 XmlFlattenerVisitor visitor(&node_buffer, options_);
300 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301
302 // Merge the package pools into the main pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 for (auto& package_pool_entry : visitor.package_pools) {
304 visitor.pool.Merge(std::move(package_pool_entry.second));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 }
306
307 // Sort the string pool so that attribute resource IDs show up first.
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700308 visitor.pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
309 return util::compare(a.priority, b.priority);
310 });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311
312 // Now we flatten the string pool references into the correct places.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 for (const auto& ref_entry : visitor.string_refs) {
314 ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 }
316
317 // Write the XML header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 ChunkWriter xml_header_writer(buffer_);
319 xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320
321 // Flatten the StringPool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 StringPool::FlattenUtf8(buffer_, visitor.pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323
324 {
325 // Write the array of resource IDs, indexed by StringPool order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 ChunkWriter res_id_map_writer(buffer_);
327 res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE);
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700328 for (const auto& str : visitor.pool.strings()) {
329 ResourceId id(str->context.priority);
330 if (str->context.priority == kLowPriority || !id.is_valid()) {
331 // When we see the first non-resource ID, we're done.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 break;
333 }
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700334 *res_id_map_writer.NextBlock<uint32_t>() = util::HostToDevice32(id.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 res_id_map_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 }
338
339 // Move the nodeBuffer and append it to the out buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 buffer_->AppendBuffer(std::move(node_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341
342 // Finish the xml header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 xml_header_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700345}
346
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347bool XmlFlattener::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 if (!resource->root) {
349 return false;
350 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 return Flatten(context, resource->root.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700352}
353
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354} // namespace aapt