blob: 0711749d03789ff959a38670d1222e06e2ada306 [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 Lesinskic744ae82017-05-17 19:28:38 -0700197 if (attr.namespace_uri != xml::kSchemaTools) {
198 filtered_attrs_.push_back(&attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200 }
201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 if (filtered_attrs_.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 return;
204 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 const ResourceId kIdAttr(0x010100d0);
207
Adam Lesinski48448e82017-04-26 15:13:52 -0700208 std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 ResXMLTree_attribute* flat_attr =
213 writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size());
214 uint16_t attribute_index = 1;
215 for (const xml::Attribute* xml_attr : filtered_attrs_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 // Assign the indices for specific attributes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 if (xml_attr->compiled_attribute &&
218 xml_attr->compiled_attribute.value().id &&
219 xml_attr->compiled_attribute.value().id.value() == kIdAttr) {
220 flat_elem->idIndex = util::HostToDevice16(attribute_index);
221 } else if (xml_attr->namespace_uri.empty()) {
222 if (xml_attr->name == "class") {
223 flat_elem->classIndex = util::HostToDevice16(attribute_index);
224 } else if (xml_attr->name == "style") {
225 flat_elem->styleIndex = util::HostToDevice16(attribute_index);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 }
227 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 attribute_index++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229
Adam Lesinski48448e82017-04-26 15:13:52 -0700230 // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 // is empty (doesn't exist).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
233 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 flat_attr->rawValue.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236
Adam Lesinski48448e82017-04-26 15:13:52 -0700237 if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) {
238 // The attribute has no associated ResourceID, so the string order doesn't matter.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 AddString(xml_attr->name, kLowPriority, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 } else {
241 // Attribute names are stored without packages, but we use
242 // their StringPool index to lookup their resource IDs.
243 // This will cause collisions, so we can't dedupe
244 // attribute names from different packages. We use separate
245 // pools that we later combine.
246 //
247 // Lookup the StringPool for this package and make the reference there.
Adam Lesinski48448e82017-04-26 15:13:52 -0700248 const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 StringPool::Ref name_ref =
251 package_pools[aapt_attr.id.value().package_id()].MakeRef(
252 xml_attr->name, StringPool::Context(aapt_attr.id.value().id));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253
254 // Add it to the list of strings to flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 AddString(name_ref, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 }
257
Adam Lesinski48448e82017-04-26 15:13:52 -0700258 // Process plain strings to make sure they get properly escaped.
259 StringPiece raw_value = xml_attr->value;
260 util::StringBuilder str_builder;
261 if (!options_.keep_raw_values) {
262 str_builder.Append(xml_attr->value);
263 raw_value = str_builder.ToString();
264 }
265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 if (options_.keep_raw_values || !xml_attr->compiled_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 // Keep raw values if the value is not compiled or
268 // if we're building a static library (need symbols).
Adam Lesinski48448e82017-04-26 15:13:52 -0700269 AddString(raw_value, kLowPriority, &flat_attr->rawValue);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 }
271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 if (xml_attr->compiled_value) {
273 CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 } else {
275 // Flatten as a regular string type.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING;
Adam Lesinski48448e82017-04-26 15:13:52 -0700277
278 AddString(str_builder.ToString(), kLowPriority,
279 (ResStringPool_ref*) &flat_attr->typedValue.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 }
281
Adam Lesinski48448e82017-04-26 15:13:52 -0700282 flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 flat_attr++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 }
285 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286
287 BigBuffer* buffer_;
288 XmlFlattenerOptions options_;
289
290 // Scratch vector to filter attributes. We avoid allocations
291 // making this a member.
292 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 Lesinskice5e56e2016-10-21 17:56:45 -0700308 visitor.pool.Sort(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
310 return a.context.priority < b.context.priority;
311 });
312
313 // Now we flatten the string pool references into the correct places.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 for (const auto& ref_entry : visitor.string_refs) {
315 ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 }
317
318 // Write the XML header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 ChunkWriter xml_header_writer(buffer_);
320 xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321
322 // Flatten the StringPool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 StringPool::FlattenUtf8(buffer_, visitor.pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324
325 {
326 // Write the array of resource IDs, indexed by StringPool order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 ChunkWriter res_id_map_writer(buffer_);
328 res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE);
329 for (const auto& str : visitor.pool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 ResourceId id = {str->context.priority};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 if (id.id == kLowPriority || !id.is_valid()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 // When we see the first non-resource ID,
333 // we're done.
334 break;
335 }
336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 *res_id_map_writer.NextBlock<uint32_t>() = id.id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 res_id_map_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 }
341
342 // Move the nodeBuffer and append it to the out buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 buffer_->AppendBuffer(std::move(node_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344
345 // Finish the xml header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 xml_header_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348}
349
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350bool XmlFlattener::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 if (!resource->root) {
352 return false;
353 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 return Flatten(context, resource->root.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700355}
356
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357} // namespace aapt