blob: b3b308a29fc508a2c95ee5626ea2a011d086b1cf [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 Lesinski6b372992017-08-09 10:54:23 -070041static bool cmp_xml_attribute_by_id(const xml::Attribute* a, const xml::Attribute* b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 if (a->compiled_attribute && a->compiled_attribute.value().id) {
43 if (b->compiled_attribute && b->compiled_attribute.value().id) {
Adam Lesinski6b372992017-08-09 10:54:23 -070044 return a->compiled_attribute.value().id.value() < b->compiled_attribute.value().id.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 }
46 return true;
47 } else if (!b->compiled_attribute) {
48 int diff = a->namespace_uri.compare(b->namespace_uri);
49 if (diff < 0) {
50 return true;
51 } else if (diff > 0) {
52 return false;
53 }
54 return a->name < b->name;
55 }
56 return false;
57}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059class XmlFlattenerVisitor : public xml::Visitor {
60 public:
61 using xml::Visitor::Visit;
62
63 StringPool pool;
64 std::map<uint8_t, StringPool> package_pools;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 struct StringFlattenDest {
67 StringPool::Ref ref;
68 ResStringPool_ref* dest;
69 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 std::vector<StringFlattenDest> string_refs;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 XmlFlattenerVisitor(BigBuffer* buffer, XmlFlattenerOptions options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 : buffer_(buffer), options_(options) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 void Visit(xml::Text* node) override {
77 if (util::TrimWhitespace(node->text).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 // Skip whitespace only text nodes.
79 return;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080 }
81
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 ChunkWriter writer(buffer_);
Adam Lesinski6b372992017-08-09 10:54:23 -070083 ResXMLTree_node* flat_node = writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 flat_node->lineNumber = util::HostToDevice32(node->line_number);
85 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
Adam Lesinski48448e82017-04-26 15:13:52 -070088
89 // Process plain strings to make sure they get properly escaped.
90 util::StringBuilder builder;
91 builder.Append(node->text);
92 AddString(builder.ToString(), kLowPriority, &flat_text->data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 }
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 void Visit(xml::Element* node) override {
Adam Lesinski6b372992017-08-09 10:54:23 -070098 for (const xml::NamespaceDecl& decl : node->namespace_decls) {
99 // Skip dedicated tools namespace.
100 if (decl.uri != xml::kSchemaTools) {
101 WriteNamespace(decl, android::RES_XML_START_NAMESPACE_TYPE);
102 }
103 }
104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 ChunkWriter start_writer(buffer_);
107 ResXMLTree_node* flat_node =
108 start_writer.StartChunk<ResXMLTree_node>(RES_XML_START_ELEMENT_TYPE);
109 flat_node->lineNumber = util::HostToDevice32(node->line_number);
110 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111
Adam Lesinski6b372992017-08-09 10:54:23 -0700112 ResXMLTree_attrExt* flat_elem = start_writer.NextBlock<ResXMLTree_attrExt>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113
Adam Lesinski6b372992017-08-09 10:54:23 -0700114 // A missing namespace must be null, not an empty string. Otherwise the runtime complains.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 AddString(node->namespace_uri, kLowPriority, &flat_elem->ns,
116 true /* treat_empty_string_as_null */);
Adam Lesinski6b372992017-08-09 10:54:23 -0700117 AddString(node->name, kLowPriority, &flat_elem->name, true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem));
Adam Lesinski6b372992017-08-09 10:54:23 -0700120 flat_elem->attributeSize = util::HostToDevice16(sizeof(ResXMLTree_attribute));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 WriteAttributes(node, flat_elem, &start_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 start_writer.Finish();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125 }
126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 xml::Visitor::Visit(node);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128
129 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 ChunkWriter end_writer(buffer_);
131 ResXMLTree_node* flat_end_node =
132 end_writer.StartChunk<ResXMLTree_node>(RES_XML_END_ELEMENT_TYPE);
133 flat_end_node->lineNumber = util::HostToDevice32(node->line_number);
134 flat_end_node->comment.index = util::HostToDevice32(-1);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
Adam Lesinski6b372992017-08-09 10:54:23 -0700136 ResXMLTree_endElementExt* flat_end_elem = end_writer.NextBlock<ResXMLTree_endElementExt>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 AddString(node->namespace_uri, kLowPriority, &flat_end_elem->ns,
138 true /* treat_empty_string_as_null */);
139 AddString(node->name, kLowPriority, &flat_end_elem->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 end_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700143
144 for (auto iter = node->namespace_decls.rbegin(); iter != node->namespace_decls.rend(); ++iter) {
145 // Skip dedicated tools namespace.
146 if (iter->uri != xml::kSchemaTools) {
147 WriteNamespace(*iter, android::RES_XML_END_NAMESPACE_TYPE);
148 }
149 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 private:
153 DISALLOW_COPY_AND_ASSIGN(XmlFlattenerVisitor);
154
155 void AddString(const StringPiece& str, uint32_t priority,
156 android::ResStringPool_ref* dest,
157 bool treat_empty_string_as_null = false) {
158 if (str.empty() && treat_empty_string_as_null) {
159 // Some parts of the runtime treat null differently than empty string.
160 dest->index = util::DeviceToHost32(-1);
161 } else {
162 string_refs.push_back(StringFlattenDest{
163 pool.MakeRef(str, StringPool::Context(priority)), dest});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 }
166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) {
168 string_refs.push_back(StringFlattenDest{ref, dest});
169 }
170
Adam Lesinski6b372992017-08-09 10:54:23 -0700171 void WriteNamespace(const xml::NamespaceDecl& decl, uint16_t type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 ChunkWriter writer(buffer_);
173
174 ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type);
Adam Lesinski6b372992017-08-09 10:54:23 -0700175 flatNode->lineNumber = util::HostToDevice32(decl.line_number);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 flatNode->comment.index = util::HostToDevice32(-1);
177
Adam Lesinski060b53d2017-07-28 17:10:35 -0700178 ResXMLTree_namespaceExt* flat_ns = writer.NextBlock<ResXMLTree_namespaceExt>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700179 AddString(decl.prefix, kLowPriority, &flat_ns->prefix);
180 AddString(decl.uri, kLowPriority, &flat_ns->uri);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181
182 writer.Finish();
183 }
184
Adam Lesinski48448e82017-04-26 15:13:52 -0700185 void WriteAttributes(xml::Element* node, ResXMLTree_attrExt* flat_elem, ChunkWriter* writer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 filtered_attrs_.clear();
187 filtered_attrs_.reserve(node->attributes.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188
189 // Filter the attributes.
190 for (xml::Attribute& attr : node->attributes) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700191 if (attr.namespace_uri != xml::kSchemaTools) {
192 filtered_attrs_.push_back(&attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194 }
195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 if (filtered_attrs_.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 return;
198 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700199
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 const ResourceId kIdAttr(0x010100d0);
201
Adam Lesinski48448e82017-04-26 15:13:52 -0700202 std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 ResXMLTree_attribute* flat_attr =
207 writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size());
208 uint16_t attribute_index = 1;
209 for (const xml::Attribute* xml_attr : filtered_attrs_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 // Assign the indices for specific attributes.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 if (xml_attr->compiled_attribute &&
212 xml_attr->compiled_attribute.value().id &&
213 xml_attr->compiled_attribute.value().id.value() == kIdAttr) {
214 flat_elem->idIndex = util::HostToDevice16(attribute_index);
215 } else if (xml_attr->namespace_uri.empty()) {
216 if (xml_attr->name == "class") {
217 flat_elem->classIndex = util::HostToDevice16(attribute_index);
218 } else if (xml_attr->name == "style") {
219 flat_elem->styleIndex = util::HostToDevice16(attribute_index);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 }
221 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 attribute_index++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223
Adam Lesinski48448e82017-04-26 15:13:52 -0700224 // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 // is empty (doesn't exist).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
227 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 flat_attr->rawValue.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230
Adam Lesinski48448e82017-04-26 15:13:52 -0700231 if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) {
232 // The attribute has no associated ResourceID, so the string order doesn't matter.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 AddString(xml_attr->name, kLowPriority, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 } else {
235 // Attribute names are stored without packages, but we use
236 // their StringPool index to lookup their resource IDs.
237 // This will cause collisions, so we can't dedupe
238 // attribute names from different packages. We use separate
239 // pools that we later combine.
240 //
241 // Lookup the StringPool for this package and make the reference there.
Adam Lesinski48448e82017-04-26 15:13:52 -0700242 const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 StringPool::Ref name_ref =
245 package_pools[aapt_attr.id.value().package_id()].MakeRef(
246 xml_attr->name, StringPool::Context(aapt_attr.id.value().id));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247
248 // Add it to the list of strings to flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 AddString(name_ref, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 }
251
Adam Lesinski48448e82017-04-26 15:13:52 -0700252 // Process plain strings to make sure they get properly escaped.
253 StringPiece raw_value = xml_attr->value;
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700254
255 util::StringBuilder str_builder(true /*preserve_spaces*/);
256 str_builder.Append(xml_attr->value);
257
Adam Lesinski48448e82017-04-26 15:13:52 -0700258 if (!options_.keep_raw_values) {
Adam Lesinski48448e82017-04-26 15:13:52 -0700259 raw_value = str_builder.ToString();
260 }
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 if (options_.keep_raw_values || !xml_attr->compiled_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 // Keep raw values if the value is not compiled or
264 // if we're building a static library (need symbols).
Adam Lesinski48448e82017-04-26 15:13:52 -0700265 AddString(raw_value, kLowPriority, &flat_attr->rawValue);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 }
267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 if (xml_attr->compiled_value) {
269 CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 } else {
271 // Flatten as a regular string type.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING;
Adam Lesinski48448e82017-04-26 15:13:52 -0700273
274 AddString(str_builder.ToString(), kLowPriority,
275 (ResStringPool_ref*) &flat_attr->typedValue.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 }
277
Adam Lesinski48448e82017-04-26 15:13:52 -0700278 flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 flat_attr++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 }
281 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282
283 BigBuffer* buffer_;
284 XmlFlattenerOptions options_;
285
Adam Lesinski060b53d2017-07-28 17:10:35 -0700286 // Scratch vector to filter attributes. We avoid allocations making this a member.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 std::vector<xml::Attribute*> filtered_attrs_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288};
289
290} // namespace
291
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292bool XmlFlattener::Flatten(IAaptContext* context, xml::Node* node) {
293 BigBuffer node_buffer(1024);
294 XmlFlattenerVisitor visitor(&node_buffer, options_);
295 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296
297 // Merge the package pools into the main pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 for (auto& package_pool_entry : visitor.package_pools) {
299 visitor.pool.Merge(std::move(package_pool_entry.second));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 }
301
302 // Sort the string pool so that attribute resource IDs show up first.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700303 visitor.pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
304 return util::compare(a.priority, b.priority);
305 });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306
307 // Now we flatten the string pool references into the correct places.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 for (const auto& ref_entry : visitor.string_refs) {
309 ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 }
311
312 // Write the XML header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 ChunkWriter xml_header_writer(buffer_);
314 xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315
316 // Flatten the StringPool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 StringPool::FlattenUtf8(buffer_, visitor.pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318
319 {
320 // Write the array of resource IDs, indexed by StringPool order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 ChunkWriter res_id_map_writer(buffer_);
322 res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE);
Adam Lesinski060b53d2017-07-28 17:10:35 -0700323 for (const auto& str : visitor.pool.strings()) {
324 ResourceId id(str->context.priority);
325 if (str->context.priority == kLowPriority || !id.is_valid()) {
326 // When we see the first non-resource ID, we're done.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 break;
328 }
Adam Lesinski060b53d2017-07-28 17:10:35 -0700329 *res_id_map_writer.NextBlock<uint32_t>() = util::HostToDevice32(id.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 res_id_map_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 }
333
334 // Move the nodeBuffer and append it to the out buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 buffer_->AppendBuffer(std::move(node_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336
337 // Finish the xml header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 xml_header_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700340}
341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342bool XmlFlattener::Consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 if (!resource->root) {
344 return false;
345 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 return Flatten(context, resource->root.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700347}
348
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349} // namespace aapt