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 "XmlDom.h"
18 
19 #include <expat.h>
20 
21 #include <memory>
22 #include <stack>
23 #include <string>
24 #include <tuple>
25 
26 #include "android-base/logging.h"
27 
28 #include "ResourceUtils.h"
29 #include "trace/TraceBuffer.h"
30 #include "XmlPullParser.h"
31 #include "util/Util.h"
32 
33 using ::aapt::io::InputStream;
34 using ::android::StringPiece;
35 using ::android::StringPiece16;
36 
37 namespace aapt {
38 namespace xml {
39 
40 constexpr char kXmlNamespaceSep = 1;
41 
42 struct Stack {
43   std::unique_ptr<xml::Element> root;
44   std::stack<xml::Element*> node_stack;
45   std::unique_ptr<xml::Element> pending_element;
46   std::string pending_comment;
47   std::unique_ptr<xml::Text> last_text_node;
48 };
49 
50 // Extracts the namespace and name of an expanded element or attribute name.
SplitName(const char * name,std::string * out_ns,std::string * out_name)51 static void SplitName(const char* name, std::string* out_ns, std::string* out_name) {
52   const char* p = name;
53   while (*p != 0 && *p != kXmlNamespaceSep) {
54     p++;
55   }
56 
57   if (*p == 0) {
58     out_ns->clear();
59     out_name->assign(name);
60   } else {
61     out_ns->assign(name, (p - name));
62     out_name->assign(p + 1);
63   }
64 }
65 
FinishPendingText(Stack * stack)66 static void FinishPendingText(Stack* stack) {
67   if (stack->last_text_node != nullptr) {
68     if (!stack->last_text_node->text.empty()) {
69       CHECK(!stack->node_stack.empty());
70       stack->node_stack.top()->AppendChild(std::move(stack->last_text_node));
71     } else {
72       // Drop an empty text node.
73     }
74     stack->last_text_node = nullptr;
75   }
76 }
77 
StartNamespaceHandler(void * user_data,const char * prefix,const char * uri)78 static void XMLCALL StartNamespaceHandler(void* user_data, const char* prefix, const char* uri) {
79   XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
80   Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
81   FinishPendingText(stack);
82 
83   NamespaceDecl decl;
84   decl.line_number = XML_GetCurrentLineNumber(parser);
85   decl.column_number = XML_GetCurrentColumnNumber(parser);
86   decl.prefix = prefix ? prefix : "";
87   decl.uri = uri ? uri : "";
88 
89   if (stack->pending_element == nullptr) {
90     stack->pending_element = util::make_unique<Element>();
91   }
92   stack->pending_element->namespace_decls.push_back(std::move(decl));
93 }
94 
EndNamespaceHandler(void * user_data,const char *)95 static void XMLCALL EndNamespaceHandler(void* user_data, const char* /*prefix*/) {
96   XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
97   Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
98   FinishPendingText(stack);
99 }
100 
less_attribute(const Attribute & lhs,const Attribute & rhs)101 static bool less_attribute(const Attribute& lhs, const Attribute& rhs) {
102   return std::tie(lhs.namespace_uri, lhs.name, lhs.value) <
103          std::tie(rhs.namespace_uri, rhs.name, rhs.value);
104 }
105 
StartElementHandler(void * user_data,const char * name,const char ** attrs)106 static void XMLCALL StartElementHandler(void* user_data, const char* name, const char** attrs) {
107   XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
108   Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
109   FinishPendingText(stack);
110 
111   std::unique_ptr<Element> el;
112   if (stack->pending_element != nullptr) {
113     el = std::move(stack->pending_element);
114   } else {
115     el = util::make_unique<Element>();
116   }
117 
118   el->line_number = XML_GetCurrentLineNumber(parser);
119   el->column_number = XML_GetCurrentColumnNumber(parser);
120   el->comment = std::move(stack->pending_comment);
121 
122   SplitName(name, &el->namespace_uri, &el->name);
123 
124   while (*attrs) {
125     Attribute attribute;
126     SplitName(*attrs++, &attribute.namespace_uri, &attribute.name);
127     attribute.value = *attrs++;
128     el->attributes.push_back(std::move(attribute));
129   }
130 
131   // Sort the attributes.
132   std::sort(el->attributes.begin(), el->attributes.end(), less_attribute);
133 
134   // Add to the stack.
135   Element* this_el = el.get();
136   if (!stack->node_stack.empty()) {
137     stack->node_stack.top()->AppendChild(std::move(el));
138   } else {
139     stack->root = std::move(el);
140   }
141   stack->node_stack.push(this_el);
142 }
143 
EndElementHandler(void * user_data,const char * name)144 static void XMLCALL EndElementHandler(void* user_data, const char* name) {
145   XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
146   Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
147   FinishPendingText(stack);
148 
149   CHECK(!stack->node_stack.empty());
150   // stack->nodeStack.top()->comment = std::move(stack->pendingComment);
151   stack->node_stack.pop();
152 }
153 
CharacterDataHandler(void * user_data,const char * s,int len)154 static void XMLCALL CharacterDataHandler(void* user_data, const char* s, int len) {
155   XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
156   Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
157 
158   const StringPiece str(s, len);
159   if (str.empty()) {
160     return;
161   }
162 
163   // See if we can just append the text to a previous text node.
164   if (stack->last_text_node != nullptr) {
165     stack->last_text_node->text.append(str.data(), str.size());
166     return;
167   }
168 
169   stack->last_text_node = util::make_unique<Text>();
170   stack->last_text_node->line_number = XML_GetCurrentLineNumber(parser);
171   stack->last_text_node->column_number = XML_GetCurrentColumnNumber(parser);
172   stack->last_text_node->text = str.to_string();
173 }
174 
CommentDataHandler(void * user_data,const char * comment)175 static void XMLCALL CommentDataHandler(void* user_data, const char* comment) {
176   XML_Parser parser = reinterpret_cast<XML_Parser>(user_data);
177   Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
178   FinishPendingText(stack);
179 
180   if (!stack->pending_comment.empty()) {
181     stack->pending_comment += '\n';
182   }
183   stack->pending_comment += comment;
184 }
185 
Inflate(InputStream * in,IDiagnostics * diag,const Source & source)186 std::unique_ptr<XmlResource> Inflate(InputStream* in, IDiagnostics* diag, const Source& source) {
187   Stack stack;
188 
189   std::unique_ptr<std::remove_pointer<XML_Parser>::type, decltype(XML_ParserFree)*> parser = {
190       XML_ParserCreateNS(nullptr, kXmlNamespaceSep), XML_ParserFree};
191   XML_SetUserData(parser.get(), &stack);
192   XML_UseParserAsHandlerArg(parser.get());
193   XML_SetElementHandler(parser.get(), StartElementHandler, EndElementHandler);
194   XML_SetNamespaceDeclHandler(parser.get(), StartNamespaceHandler, EndNamespaceHandler);
195   XML_SetCharacterDataHandler(parser.get(), CharacterDataHandler);
196   XML_SetCommentHandler(parser.get(), CommentDataHandler);
197 
198   const char* buffer = nullptr;
199   size_t buffer_size = 0;
200   while (in->Next(reinterpret_cast<const void**>(&buffer), &buffer_size)) {
201     if (XML_Parse(parser.get(), buffer, buffer_size, false) == XML_STATUS_ERROR) {
202       diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get())))
203                   << XML_ErrorString(XML_GetErrorCode(parser.get())));
204       return {};
205     }
206   }
207 
208   if (in->HadError()) {
209     diag->Error(DiagMessage(source) << in->GetError());
210     return {};
211   } else {
212     // Finish off the parsing.
213     if (XML_Parse(parser.get(), nullptr, 0u, true) == XML_STATUS_ERROR) {
214       diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get())))
215                   << XML_ErrorString(XML_GetErrorCode(parser.get())));
216       return {};
217     }
218   }
219   return util::make_unique<XmlResource>(ResourceFile{{}, {}, ResourceFile::Type::kUnknown, source},
220                                         StringPool{}, std::move(stack.root));
221 }
222 
CopyAttributes(Element * el,android::ResXMLParser * parser,StringPool * out_pool)223 static void CopyAttributes(Element* el, android::ResXMLParser* parser, StringPool* out_pool) {
224   const size_t attr_count = parser->getAttributeCount();
225   if (attr_count > 0) {
226     el->attributes.reserve(attr_count);
227     for (size_t i = 0; i < attr_count; i++) {
228       Attribute attr;
229       size_t len;
230       const char16_t* str16 = parser->getAttributeNamespace(i, &len);
231       if (str16) {
232         attr.namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len));
233       }
234 
235       str16 = parser->getAttributeName(i, &len);
236       if (str16) {
237         attr.name = util::Utf16ToUtf8(StringPiece16(str16, len));
238       }
239 
240       uint32_t res_id = parser->getAttributeNameResID(i);
241       if (res_id > 0) {
242         attr.compiled_attribute = AaptAttribute(::aapt::Attribute(), {res_id});
243       }
244 
245       str16 = parser->getAttributeStringValue(i, &len);
246       if (str16) {
247         attr.value = util::Utf16ToUtf8(StringPiece16(str16, len));
248       }
249 
250       android::Res_value res_value;
251       if (parser->getAttributeValue(i, &res_value) > 0) {
252         // Only compile the value if it is not a string, or it is a string that differs from
253         // the raw attribute value.
254         int32_t raw_value_idx = parser->getAttributeValueStringID(i);
255         if (res_value.dataType != android::Res_value::TYPE_STRING || raw_value_idx < 0 ||
256             static_cast<uint32_t>(raw_value_idx) != res_value.data) {
257           attr.compiled_value = ResourceUtils::ParseBinaryResValue(
258               ResourceType::kAnim, {}, parser->getStrings(), res_value, out_pool);
259         }
260       }
261 
262       el->attributes.push_back(std::move(attr));
263     }
264   }
265 }
266 
Inflate(const void * data,size_t len,std::string * out_error)267 std::unique_ptr<XmlResource> Inflate(const void* data, size_t len, std::string* out_error) {
268   TRACE_CALL();
269   // We import the android namespace because on Windows NO_ERROR is a macro, not
270   // an enum, which causes errors when qualifying it with android::
271   using namespace android;
272 
273   std::unique_ptr<XmlResource> xml_resource = util::make_unique<XmlResource>();
274 
275   std::stack<Element*> node_stack;
276   std::unique_ptr<Element> pending_element;
277 
278   ResXMLTree tree;
279   if (tree.setTo(data, len) != NO_ERROR) {
280     if (out_error != nullptr) {
281       *out_error = "failed to initialize ResXMLTree";
282     }
283     return {};
284   }
285 
286   ResXMLParser::event_code_t code;
287   while ((code = tree.next()) != ResXMLParser::BAD_DOCUMENT && code != ResXMLParser::END_DOCUMENT) {
288     std::unique_ptr<Node> new_node;
289     switch (code) {
290       case ResXMLParser::START_NAMESPACE: {
291         NamespaceDecl decl;
292         decl.line_number = tree.getLineNumber();
293 
294         size_t len;
295         const char16_t* str16 = tree.getNamespacePrefix(&len);
296         if (str16) {
297           decl.prefix = util::Utf16ToUtf8(StringPiece16(str16, len));
298         }
299 
300         str16 = tree.getNamespaceUri(&len);
301         if (str16) {
302           decl.uri = util::Utf16ToUtf8(StringPiece16(str16, len));
303         }
304 
305         if (pending_element == nullptr) {
306           pending_element = util::make_unique<Element>();
307         }
308         // pending_element is not nullptr
309         // NOLINTNEXTLINE(bugprone-use-after-move)
310         pending_element->namespace_decls.push_back(std::move(decl));
311         break;
312       }
313 
314       case ResXMLParser::START_TAG: {
315         std::unique_ptr<Element> el;
316         if (pending_element != nullptr) {
317           el = std::move(pending_element);
318         } else {
319           el = util::make_unique<Element>();
320         }
321         el->line_number = tree.getLineNumber();
322 
323         size_t len;
324         const char16_t* str16 = tree.getElementNamespace(&len);
325         if (str16) {
326           el->namespace_uri = util::Utf16ToUtf8(StringPiece16(str16, len));
327         }
328 
329         str16 = tree.getElementName(&len);
330         if (str16) {
331           el->name = util::Utf16ToUtf8(StringPiece16(str16, len));
332         }
333 
334         Element* this_el = el.get();
335         CopyAttributes(el.get(), &tree, &xml_resource->string_pool);
336 
337         if (!node_stack.empty()) {
338           node_stack.top()->AppendChild(std::move(el));
339         } else {
340           xml_resource->root = std::move(el);
341         }
342         node_stack.push(this_el);
343         break;
344       }
345 
346       case ResXMLParser::TEXT: {
347         std::unique_ptr<Text> text = util::make_unique<Text>();
348         text->line_number = tree.getLineNumber();
349         size_t len;
350         const char16_t* str16 = tree.getText(&len);
351         if (str16) {
352           text->text = util::Utf16ToUtf8(StringPiece16(str16, len));
353         }
354         CHECK(!node_stack.empty());
355         node_stack.top()->AppendChild(std::move(text));
356         break;
357       }
358 
359       case ResXMLParser::END_NAMESPACE:
360         break;
361 
362       case ResXMLParser::END_TAG:
363         CHECK(!node_stack.empty());
364         node_stack.pop();
365         break;
366 
367       default:
368         LOG(FATAL) << "unhandled XML chunk type";
369         break;
370     }
371   }
372   return xml_resource;
373 }
374 
Clone() const375 std::unique_ptr<XmlResource> XmlResource::Clone() const {
376   std::unique_ptr<XmlResource> cloned = util::make_unique<XmlResource>(file);
377   if (root != nullptr) {
378     cloned->root = root->CloneElement([&](const xml::Element& src, xml::Element* dst) {
379       dst->attributes.reserve(src.attributes.size());
380       for (const xml::Attribute& attr : src.attributes) {
381         xml::Attribute cloned_attr;
382         cloned_attr.name = attr.name;
383         cloned_attr.namespace_uri = attr.namespace_uri;
384         cloned_attr.value = attr.value;
385         cloned_attr.compiled_attribute = attr.compiled_attribute;
386         if (attr.compiled_value != nullptr) {
387           cloned_attr.compiled_value.reset(attr.compiled_value->Clone(&cloned->string_pool));
388         }
389         dst->attributes.push_back(std::move(cloned_attr));
390       }
391     });
392   }
393   return cloned;
394 }
395 
FindRootElement(Node * node)396 Element* FindRootElement(Node* node) {
397   if (node == nullptr) {
398     return nullptr;
399   }
400 
401   while (node->parent != nullptr) {
402     node = node->parent;
403   }
404   return NodeCast<Element>(node);
405 }
406 
AppendChild(std::unique_ptr<Node> child)407 void Element::AppendChild(std::unique_ptr<Node> child) {
408   child->parent = this;
409   children.push_back(std::move(child));
410 }
411 
InsertChild(size_t index,std::unique_ptr<Node> child)412 void Element::InsertChild(size_t index, std::unique_ptr<Node> child) {
413   child->parent = this;
414   children.insert(children.begin() + index, std::move(child));
415 }
416 
FindAttribute(const StringPiece & ns,const StringPiece & name)417 Attribute* Element::FindAttribute(const StringPiece& ns, const StringPiece& name) {
418   return const_cast<Attribute*>(static_cast<const Element*>(this)->FindAttribute(ns, name));
419 }
420 
FindAttribute(const StringPiece & ns,const StringPiece & name) const421 const Attribute* Element::FindAttribute(const StringPiece& ns, const StringPiece& name) const {
422   for (const auto& attr : attributes) {
423     if (ns == attr.namespace_uri && name == attr.name) {
424       return &attr;
425     }
426   }
427   return nullptr;
428 }
429 
RemoveAttribute(const StringPiece & ns,const StringPiece & name)430 void Element::RemoveAttribute(const StringPiece& ns, const StringPiece& name) {
431   auto new_attr_end = std::remove_if(attributes.begin(), attributes.end(),
432     [&](const Attribute& attr) -> bool {
433       return ns == attr.namespace_uri && name == attr.name;
434     });
435 
436   attributes.erase(new_attr_end, attributes.end());
437 }
438 
FindOrCreateAttribute(const StringPiece & ns,const StringPiece & name)439 Attribute* Element::FindOrCreateAttribute(const StringPiece& ns, const StringPiece& name) {
440   Attribute* attr = FindAttribute(ns, name);
441   if (attr == nullptr) {
442     attributes.push_back(Attribute{ns.to_string(), name.to_string()});
443     attr = &attributes.back();
444   }
445   return attr;
446 }
447 
FindChild(const StringPiece & ns,const StringPiece & name)448 Element* Element::FindChild(const StringPiece& ns, const StringPiece& name) {
449   return FindChildWithAttribute(ns, name, {}, {}, {});
450 }
451 
FindChild(const StringPiece & ns,const StringPiece & name) const452 const Element* Element::FindChild(const StringPiece& ns, const StringPiece& name) const {
453   return FindChildWithAttribute(ns, name, {}, {}, {});
454 }
455 
FindChildWithAttribute(const StringPiece & ns,const StringPiece & name,const StringPiece & attr_ns,const StringPiece & attr_name,const StringPiece & attr_value)456 Element* Element::FindChildWithAttribute(const StringPiece& ns, const StringPiece& name,
457                                          const StringPiece& attr_ns, const StringPiece& attr_name,
458                                          const StringPiece& attr_value) {
459   return const_cast<Element*>(static_cast<const Element*>(this)->FindChildWithAttribute(
460       ns, name, attr_ns, attr_name, attr_value));
461 }
462 
FindChildWithAttribute(const StringPiece & ns,const StringPiece & name,const StringPiece & attr_ns,const StringPiece & attr_name,const StringPiece & attr_value) const463 const Element* Element::FindChildWithAttribute(const StringPiece& ns, const StringPiece& name,
464                                                const StringPiece& attr_ns,
465                                                const StringPiece& attr_name,
466                                                const StringPiece& attr_value) const {
467   for (const auto& child : children) {
468     if (const Element* el = NodeCast<Element>(child.get())) {
469       if (ns == el->namespace_uri && name == el->name) {
470         if (attr_ns.empty() && attr_name.empty()) {
471           return el;
472         }
473 
474         const Attribute* attr = el->FindAttribute(attr_ns, attr_name);
475         if (attr && attr_value == attr->value) {
476           return el;
477         }
478       }
479     }
480   }
481   return nullptr;
482 }
483 
GetChildElements()484 std::vector<Element*> Element::GetChildElements() {
485   std::vector<Element*> elements;
486   for (auto& child_node : children) {
487     if (Element* child = NodeCast<Element>(child_node.get())) {
488       elements.push_back(child);
489     }
490   }
491   return elements;
492 }
493 
Clone(const ElementCloneFunc & el_cloner) const494 std::unique_ptr<Node> Element::Clone(const ElementCloneFunc& el_cloner) const {
495   auto el = util::make_unique<Element>();
496   el->namespace_decls = namespace_decls;
497   el->comment = comment;
498   el->line_number = line_number;
499   el->column_number = column_number;
500   el->name = name;
501   el->namespace_uri = namespace_uri;
502   el->attributes.reserve(attributes.size());
503   el_cloner(*this, el.get());
504   el->children.reserve(children.size());
505   for (const std::unique_ptr<xml::Node>& child : children) {
506     el->AppendChild(child->Clone(el_cloner));
507   }
508   return std::move(el);
509 }
510 
CloneElement(const ElementCloneFunc & el_cloner) const511 std::unique_ptr<Element> Element::CloneElement(const ElementCloneFunc& el_cloner) const {
512   return std::unique_ptr<Element>(static_cast<Element*>(Clone(el_cloner).release()));
513 }
514 
Accept(Visitor * visitor)515 void Element::Accept(Visitor* visitor) {
516   visitor->BeforeVisitElement(this);
517   visitor->Visit(this);
518   visitor->AfterVisitElement(this);
519 }
520 
Accept(ConstVisitor * visitor) const521 void Element::Accept(ConstVisitor* visitor) const {
522   visitor->BeforeVisitElement(this);
523   visitor->Visit(this);
524   visitor->AfterVisitElement(this);
525 }
526 
Clone(const ElementCloneFunc &) const527 std::unique_ptr<Node> Text::Clone(const ElementCloneFunc&) const {
528   auto t = util::make_unique<Text>();
529   t->comment = comment;
530   t->line_number = line_number;
531   t->column_number = column_number;
532   t->text = text;
533   return std::move(t);
534 }
535 
Accept(Visitor * visitor)536 void Text::Accept(Visitor* visitor) {
537   visitor->Visit(this);
538 }
539 
Accept(ConstVisitor * visitor) const540 void Text::Accept(ConstVisitor* visitor) const {
541   visitor->Visit(this);
542 }
543 
BeforeVisitElement(Element * el)544 void PackageAwareVisitor::BeforeVisitElement(Element* el) {
545   std::vector<PackageDecl> decls;
546   for (const NamespaceDecl& decl : el->namespace_decls) {
547     if (Maybe<ExtractedPackage> maybe_package = ExtractPackageFromNamespace(decl.uri)) {
548       decls.push_back(PackageDecl{decl.prefix, std::move(maybe_package.value())});
549     }
550   }
551   package_decls_.push_back(std::move(decls));
552 }
553 
AfterVisitElement(Element * el)554 void PackageAwareVisitor::AfterVisitElement(Element* el) {
555   package_decls_.pop_back();
556 }
557 
TransformPackageAlias(const StringPiece & alias) const558 Maybe<ExtractedPackage> PackageAwareVisitor::TransformPackageAlias(const StringPiece& alias) const {
559   if (alias.empty()) {
560     return ExtractedPackage{{}, false /*private*/};
561   }
562 
563   const auto rend = package_decls_.rend();
564   for (auto iter = package_decls_.rbegin(); iter != rend; ++iter) {
565     const std::vector<PackageDecl>& decls = *iter;
566     const auto rend2 = decls.rend();
567     for (auto iter2 = decls.rbegin(); iter2 != rend2; ++iter2) {
568       const PackageDecl& decl = *iter2;
569       if (alias == decl.prefix) {
570         if (decl.package.package.empty()) {
571           return ExtractedPackage{{}, decl.package.private_namespace};
572         }
573         return decl.package;
574       }
575     }
576   }
577   return {};
578 }
579 
580 }  // namespace xml
581 }  // namespace aapt
582