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
17 #include "format/binary/XmlFlattener.h"
18
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
28 #include "ResourceUtils.h"
29 #include "SdkConstants.h"
30 #include "ValueVisitor.h"
31 #include "format/binary/ChunkWriter.h"
32 #include "format/binary/ResourceTypeExtensions.h"
33 #include "xml/XmlDom.h"
34
35 using namespace android;
36
37 using ::aapt::ResourceUtils::StringBuilder;
38
39 namespace aapt {
40
41 namespace {
42
43 constexpr uint32_t kLowPriority = 0xffffffffu;
44
cmp_xml_attribute_by_id(const xml::Attribute * a,const xml::Attribute * b)45 static bool cmp_xml_attribute_by_id(const xml::Attribute* a, const xml::Attribute* b) {
46 if (a->compiled_attribute && a->compiled_attribute.value().id) {
47 if (b->compiled_attribute && b->compiled_attribute.value().id) {
48 return a->compiled_attribute.value().id.value() < b->compiled_attribute.value().id.value();
49 }
50 return true;
51 } else if (!b->compiled_attribute) {
52 int diff = a->namespace_uri.compare(b->namespace_uri);
53 if (diff < 0) {
54 return true;
55 } else if (diff > 0) {
56 return false;
57 }
58 return a->name < b->name;
59 }
60 return false;
61 }
62
63 class XmlFlattenerVisitor : public xml::ConstVisitor {
64 public:
65 using xml::ConstVisitor::Visit;
66
67 StringPool pool;
68 std::map<uint8_t, StringPool> package_pools;
69
70 struct StringFlattenDest {
71 StringPool::Ref ref;
72 ResStringPool_ref* dest;
73 };
74
75 std::vector<StringFlattenDest> string_refs;
76
XmlFlattenerVisitor(BigBuffer * buffer,XmlFlattenerOptions options)77 XmlFlattenerVisitor(BigBuffer* buffer, XmlFlattenerOptions options)
78 : buffer_(buffer), options_(options) {
79 }
80
Visit(const xml::Text * node)81 void Visit(const xml::Text* node) override {
82 std::string text = util::TrimWhitespace(node->text).to_string();
83
84 // Skip whitespace only text nodes.
85 if (text.empty()) {
86 return;
87 }
88
89 // Compact leading and trailing whitespace into a single space
90 if (isspace(node->text[0])) {
91 text = ' ' + text;
92 }
93 if (isspace(node->text[node->text.length() - 1])) {
94 text = text + ' ';
95 }
96
97 ChunkWriter writer(buffer_);
98 ResXMLTree_node* flat_node = writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE);
99 flat_node->lineNumber = util::HostToDevice32(node->line_number);
100 flat_node->comment.index = util::HostToDevice32(-1);
101
102 // Process plain strings to make sure they get properly escaped.
103 text = StringBuilder(true /*preserve_spaces*/).AppendText(text).to_string();
104
105 ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
106 AddString(text, kLowPriority, &flat_text->data);
107 writer.Finish();
108 }
109
Visit(const xml::Element * node)110 void Visit(const xml::Element* node) override {
111 for (const xml::NamespaceDecl& decl : node->namespace_decls) {
112 // Skip dedicated tools namespace.
113 if (decl.uri != xml::kSchemaTools) {
114 WriteNamespace(decl, android::RES_XML_START_NAMESPACE_TYPE);
115 }
116 }
117
118 {
119 ChunkWriter start_writer(buffer_);
120 ResXMLTree_node* flat_node =
121 start_writer.StartChunk<ResXMLTree_node>(RES_XML_START_ELEMENT_TYPE);
122 flat_node->lineNumber = util::HostToDevice32(node->line_number);
123 flat_node->comment.index = util::HostToDevice32(-1);
124
125 ResXMLTree_attrExt* flat_elem = start_writer.NextBlock<ResXMLTree_attrExt>();
126
127 // A missing namespace must be null, not an empty string. Otherwise the runtime complains.
128 AddString(node->namespace_uri, kLowPriority, &flat_elem->ns,
129 true /* treat_empty_string_as_null */);
130 AddString(node->name, kLowPriority, &flat_elem->name, true /* treat_empty_string_as_null */);
131
132 flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem));
133 flat_elem->attributeSize = util::HostToDevice16(sizeof(ResXMLTree_attribute));
134
135 WriteAttributes(node, flat_elem, &start_writer);
136
137 start_writer.Finish();
138 }
139
140 xml::ConstVisitor::Visit(node);
141
142 {
143 ChunkWriter end_writer(buffer_);
144 ResXMLTree_node* flat_end_node =
145 end_writer.StartChunk<ResXMLTree_node>(RES_XML_END_ELEMENT_TYPE);
146 flat_end_node->lineNumber = util::HostToDevice32(node->line_number);
147 flat_end_node->comment.index = util::HostToDevice32(-1);
148
149 ResXMLTree_endElementExt* flat_end_elem = end_writer.NextBlock<ResXMLTree_endElementExt>();
150 AddString(node->namespace_uri, kLowPriority, &flat_end_elem->ns,
151 true /* treat_empty_string_as_null */);
152 AddString(node->name, kLowPriority, &flat_end_elem->name);
153
154 end_writer.Finish();
155 }
156
157 for (auto iter = node->namespace_decls.rbegin(); iter != node->namespace_decls.rend(); ++iter) {
158 // Skip dedicated tools namespace.
159 if (iter->uri != xml::kSchemaTools) {
160 WriteNamespace(*iter, android::RES_XML_END_NAMESPACE_TYPE);
161 }
162 }
163 }
164
165 private:
166 DISALLOW_COPY_AND_ASSIGN(XmlFlattenerVisitor);
167
168 // We are adding strings to a StringPool whose strings will be sorted and merged with other
169 // string pools. That means we can't encode the ID of a string directly. Instead, we defer the
170 // writing of the ID here, until after the StringPool is merged and sorted.
AddString(const StringPiece & str,uint32_t priority,android::ResStringPool_ref * dest,bool treat_empty_string_as_null=false)171 void AddString(const StringPiece& str, uint32_t priority, android::ResStringPool_ref* dest,
172 bool treat_empty_string_as_null = false) {
173 if (str.empty() && treat_empty_string_as_null) {
174 // Some parts of the runtime treat null differently than empty string.
175 dest->index = util::DeviceToHost32(-1);
176 } else {
177 string_refs.push_back(
178 StringFlattenDest{pool.MakeRef(str, StringPool::Context(priority)), dest});
179 }
180 }
181
182 // We are adding strings to a StringPool whose strings will be sorted and merged with other
183 // string pools. That means we can't encode the ID of a string directly. Instead, we defer the
184 // writing of the ID here, until after the StringPool is merged and sorted.
AddString(const StringPool::Ref & ref,android::ResStringPool_ref * dest)185 void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) {
186 string_refs.push_back(StringFlattenDest{ref, dest});
187 }
188
WriteNamespace(const xml::NamespaceDecl & decl,uint16_t type)189 void WriteNamespace(const xml::NamespaceDecl& decl, uint16_t type) {
190 ChunkWriter writer(buffer_);
191
192 ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type);
193 flatNode->lineNumber = util::HostToDevice32(decl.line_number);
194 flatNode->comment.index = util::HostToDevice32(-1);
195
196 ResXMLTree_namespaceExt* flat_ns = writer.NextBlock<ResXMLTree_namespaceExt>();
197 AddString(decl.prefix, kLowPriority, &flat_ns->prefix);
198 AddString(decl.uri, kLowPriority, &flat_ns->uri);
199
200 writer.Finish();
201 }
202
WriteAttributes(const xml::Element * node,ResXMLTree_attrExt * flat_elem,ChunkWriter * writer)203 void WriteAttributes(const xml::Element* node, ResXMLTree_attrExt* flat_elem,
204 ChunkWriter* writer) {
205 filtered_attrs_.clear();
206 filtered_attrs_.reserve(node->attributes.size());
207
208 // Filter the attributes.
209 for (const xml::Attribute& attr : node->attributes) {
210 if (attr.namespace_uri != xml::kSchemaTools) {
211 filtered_attrs_.push_back(&attr);
212 }
213 }
214
215 if (filtered_attrs_.empty()) {
216 return;
217 }
218
219 const ResourceId kIdAttr(0x010100d0);
220
221 std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id);
222
223 flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
224
225 ResXMLTree_attribute* flat_attr =
226 writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size());
227 uint16_t attribute_index = 1;
228 for (const xml::Attribute* xml_attr : filtered_attrs_) {
229 // Assign the indices for specific attributes.
230 if (xml_attr->compiled_attribute && xml_attr->compiled_attribute.value().id &&
231 xml_attr->compiled_attribute.value().id.value() == kIdAttr) {
232 flat_elem->idIndex = util::HostToDevice16(attribute_index);
233 } else if (xml_attr->namespace_uri.empty()) {
234 if (xml_attr->name == "class") {
235 flat_elem->classIndex = util::HostToDevice16(attribute_index);
236 } else if (xml_attr->name == "style") {
237 flat_elem->styleIndex = util::HostToDevice16(attribute_index);
238 }
239 }
240 attribute_index++;
241
242 // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace
243 // is empty (doesn't exist).
244 AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
245 true /* treat_empty_string_as_null */);
246
247 flat_attr->rawValue.index = util::HostToDevice32(-1);
248
249 if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) {
250 // The attribute has no associated ResourceID, so the string order doesn't matter.
251 AddString(xml_attr->name, kLowPriority, &flat_attr->name);
252 } else {
253 // Attribute names are stored without packages, but we use
254 // their StringPool index to lookup their resource IDs.
255 // This will cause collisions, so we can't dedupe
256 // attribute names from different packages. We use separate
257 // pools that we later combine.
258 //
259 // Lookup the StringPool for this package and make the reference there.
260 const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value();
261
262 StringPool::Ref name_ref = package_pools[aapt_attr.id.value().package_id()].MakeRef(
263 xml_attr->name, StringPool::Context(aapt_attr.id.value().id));
264
265 // Add it to the list of strings to flatten.
266 AddString(name_ref, &flat_attr->name);
267 }
268
269 std::string processed_str;
270 Maybe<StringPiece> compiled_text;
271 if (xml_attr->compiled_value != nullptr) {
272 // Make sure we're not flattening a String. A String can be referencing a string from
273 // a different StringPool than we're using here to build the binary XML.
274 String* string_value = ValueCast<String>(xml_attr->compiled_value.get());
275 if (string_value != nullptr) {
276 // Mark the String's text as needing to be serialized.
277 compiled_text = StringPiece(*string_value->value);
278 } else {
279 // Serialize this compiled value safely.
280 CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue));
281 }
282 } else {
283 // There is no compiled value, so treat the raw string as compiled, once it is processed to
284 // make sure escape sequences are properly interpreted.
285 processed_str =
286 StringBuilder(true /*preserve_spaces*/).AppendText(xml_attr->value).to_string();
287 compiled_text = StringPiece(processed_str);
288 }
289
290 if (compiled_text) {
291 // Write out the compiled text and raw_text.
292 flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING;
293 AddString(compiled_text.value(), kLowPriority,
294 reinterpret_cast<ResStringPool_ref*>(&flat_attr->typedValue.data));
295 if (options_.keep_raw_values) {
296 AddString(xml_attr->value, kLowPriority, &flat_attr->rawValue);
297 } else {
298 AddString(compiled_text.value(), kLowPriority, &flat_attr->rawValue);
299 }
300 } else if (options_.keep_raw_values && !xml_attr->value.empty()) {
301 AddString(xml_attr->value, kLowPriority, &flat_attr->rawValue);
302 }
303
304 flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue));
305 flat_attr++;
306 }
307 }
308
309 BigBuffer* buffer_;
310 XmlFlattenerOptions options_;
311
312 // Scratch vector to filter attributes. We avoid allocations making this a member.
313 std::vector<const xml::Attribute*> filtered_attrs_;
314 };
315
316 } // namespace
317
Flatten(IAaptContext * context,const xml::Node * node)318 bool XmlFlattener::Flatten(IAaptContext* context, const xml::Node* node) {
319 BigBuffer node_buffer(1024);
320 XmlFlattenerVisitor visitor(&node_buffer, options_);
321 node->Accept(&visitor);
322
323 // Merge the package pools into the main pool.
324 for (auto& package_pool_entry : visitor.package_pools) {
325 visitor.pool.Merge(std::move(package_pool_entry.second));
326 }
327
328 // Sort the string pool so that attribute resource IDs show up first.
329 visitor.pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
330 return util::compare(a.priority, b.priority);
331 });
332
333 // Now we flatten the string pool references into the correct places.
334 for (const auto& ref_entry : visitor.string_refs) {
335 ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
336 }
337
338 // Write the XML header.
339 ChunkWriter xml_header_writer(buffer_);
340 xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE);
341
342 // Flatten the StringPool.
343 if (options_.use_utf16) {
344 StringPool::FlattenUtf16(buffer_, visitor.pool, context->GetDiagnostics());
345 } else {
346 StringPool::FlattenUtf8(buffer_, visitor.pool, context->GetDiagnostics());
347 }
348
349 {
350 // Write the array of resource IDs, indexed by StringPool order.
351 ChunkWriter res_id_map_writer(buffer_);
352 res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE);
353 for (const auto& str : visitor.pool.strings()) {
354 ResourceId id(str->context.priority);
355 if (str->context.priority == kLowPriority || !id.is_valid()) {
356 // When we see the first non-resource ID, we're done.
357 break;
358 }
359 *res_id_map_writer.NextBlock<uint32_t>() = util::HostToDevice32(id.id);
360 }
361 res_id_map_writer.Finish();
362 }
363
364 // Move the nodeBuffer and append it to the out buffer.
365 buffer_->AppendBuffer(std::move(node_buffer));
366
367 // Finish the xml header.
368 xml_header_writer.Finish();
369 return true;
370 }
371
Consume(IAaptContext * context,const xml::XmlResource * resource)372 bool XmlFlattener::Consume(IAaptContext* context, const xml::XmlResource* resource) {
373 if (!resource->root) {
374 return false;
375 }
376 return Flatten(context, resource->root.get());
377 }
378
379 } // namespace aapt
380