AAPT2: Fix windows unicode path issues

Mingw64 was being difficult, so instead of defining a wmain entrypoint,
the command line parameters are parsed manually using built-in Windows
methods that support Unicode. The results are converted to UTF8 and
handled just like the rest of the linux/mac version of the code.

This also removes dependencies on std::istream in favour of a
FileInputStream which calls the appropriate unicode version of
open to read a file.

No speed regressions found on Linux or MacOS.

Bug: 62336414
Bug: 63830502
Test: manual
Change-Id: I597da51e33729ed1b98bf246e7e773337fd3fee8
diff --git a/tools/aapt2/xml/XmlDom.cpp b/tools/aapt2/xml/XmlDom.cpp
index 885ab3e..d6df715 100644
--- a/tools/aapt2/xml/XmlDom.cpp
+++ b/tools/aapt2/xml/XmlDom.cpp
@@ -29,8 +29,9 @@
 #include "XmlPullParser.h"
 #include "util/Util.h"
 
-using android::StringPiece;
-using android::StringPiece16;
+using ::aapt::io::InputStream;
+using ::android::StringPiece;
+using ::android::StringPiece16;
 
 namespace aapt {
 namespace xml {
@@ -189,40 +190,41 @@
   stack->pending_comment += comment;
 }
 
-std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag, const Source& source) {
+std::unique_ptr<XmlResource> Inflate(InputStream* in, IDiagnostics* diag, const Source& source) {
   Stack stack;
 
-  XML_Parser parser = XML_ParserCreateNS(nullptr, kXmlNamespaceSep);
-  XML_SetUserData(parser, &stack);
-  XML_UseParserAsHandlerArg(parser);
-  XML_SetElementHandler(parser, StartElementHandler, EndElementHandler);
-  XML_SetNamespaceDeclHandler(parser, StartNamespaceHandler, EndNamespaceHandler);
-  XML_SetCharacterDataHandler(parser, CharacterDataHandler);
-  XML_SetCommentHandler(parser, CommentDataHandler);
+  std::unique_ptr<std::remove_pointer<XML_Parser>::type, decltype(XML_ParserFree)*> parser = {
+      XML_ParserCreateNS(nullptr, kXmlNamespaceSep), XML_ParserFree};
+  XML_SetUserData(parser.get(), &stack);
+  XML_UseParserAsHandlerArg(parser.get());
+  XML_SetElementHandler(parser.get(), StartElementHandler, EndElementHandler);
+  XML_SetNamespaceDeclHandler(parser.get(), StartNamespaceHandler, EndNamespaceHandler);
+  XML_SetCharacterDataHandler(parser.get(), CharacterDataHandler);
+  XML_SetCommentHandler(parser.get(), CommentDataHandler);
 
-  char buffer[1024];
-  while (!in->eof()) {
-    in->read(buffer, sizeof(buffer) / sizeof(buffer[0]));
-    if (in->bad() && !in->eof()) {
-      stack.root = {};
-      diag->Error(DiagMessage(source) << strerror(errno));
-      break;
-    }
-
-    if (XML_Parse(parser, buffer, in->gcount(), in->eof()) == XML_STATUS_ERROR) {
-      stack.root = {};
-      diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser)))
-                  << XML_ErrorString(XML_GetErrorCode(parser)));
-      break;
+  const char* buffer = nullptr;
+  size_t buffer_size = 0;
+  while (in->Next(reinterpret_cast<const void**>(&buffer), &buffer_size)) {
+    if (XML_Parse(parser.get(), buffer, buffer_size, false) == XML_STATUS_ERROR) {
+      diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get())))
+                  << XML_ErrorString(XML_GetErrorCode(parser.get())));
+      return {};
     }
   }
 
-  XML_ParserFree(parser);
-  if (stack.root) {
-    return util::make_unique<XmlResource>(ResourceFile{{}, {}, source}, StringPool{},
-                                          std::move(stack.root));
+  if (in->HadError()) {
+    diag->Error(DiagMessage(source) << in->GetError());
+    return {};
+  } else {
+    // Finish off the parsing.
+    if (XML_Parse(parser.get(), nullptr, 0u, true) == XML_STATUS_ERROR) {
+      diag->Error(DiagMessage(source.WithLine(XML_GetCurrentLineNumber(parser.get())))
+                  << XML_ErrorString(XML_GetErrorCode(parser.get())));
+      return {};
+    }
   }
-  return {};
+  return util::make_unique<XmlResource>(ResourceFile{{}, {}, source}, StringPool{},
+                                        std::move(stack.root));
 }
 
 static void CopyAttributes(Element* el, android::ResXMLParser* parser, StringPool* out_pool) {
diff --git a/tools/aapt2/xml/XmlDom.h b/tools/aapt2/xml/XmlDom.h
index 2dc99d6..54a7033 100644
--- a/tools/aapt2/xml/XmlDom.h
+++ b/tools/aapt2/xml/XmlDom.h
@@ -17,7 +17,6 @@
 #ifndef AAPT_XML_DOM_H
 #define AAPT_XML_DOM_H
 
-#include <istream>
 #include <memory>
 #include <string>
 #include <vector>
@@ -27,6 +26,7 @@
 #include "Diagnostics.h"
 #include "Resource.h"
 #include "ResourceValues.h"
+#include "io/Io.h"
 #include "util/Util.h"
 #include "xml/XmlUtil.h"
 
@@ -37,9 +37,7 @@
 
 class Element;
 
-/**
- * Base class for all XML nodes.
- */
+// Base class for all XML nodes.
 class Node {
  public:
   Node* parent = nullptr;
@@ -60,19 +58,14 @@
   virtual std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) = 0;
 };
 
-/**
- * Base class that implements the visitor methods for a
- * subclass of Node.
- */
+// Base class that implements the visitor methods for a subclass of Node.
 template <typename Derived>
 class BaseNode : public Node {
  public:
   virtual void Accept(RawVisitor* visitor) override;
 };
 
-/**
- * A Namespace XML node. Can only have one child.
- */
+// A Namespace XML node. Can only have one child.
 class Namespace : public BaseNode<Namespace> {
  public:
   std::string namespace_prefix;
@@ -90,9 +83,7 @@
   Maybe<ResourceId> id;
 };
 
-/**
- * An XML attribute.
- */
+// An XML attribute.
 struct Attribute {
   std::string namespace_uri;
   std::string name;
@@ -102,9 +93,7 @@
   std::unique_ptr<Item> compiled_value;
 };
 
-/**
- * An Element XML node.
- */
+// An Element XML node.
 class Element : public BaseNode<Element> {
  public:
   std::string namespace_uri;
@@ -124,9 +113,7 @@
   std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
 };
 
-/**
- * A Text (CDATA) XML node. Can not have any children.
- */
+// A Text (CDATA) XML node. Can not have any children.
 class Text : public BaseNode<Text> {
  public:
   std::string text;
@@ -134,9 +121,7 @@
   std::unique_ptr<Node> Clone(const ElementCloneFunc& el_cloner) override;
 };
 
-/**
- * An XML resource with a source, name, and XML tree.
- */
+// An XML resource with a source, name, and XML tree.
 class XmlResource {
  public:
   ResourceFile file;
@@ -149,27 +134,20 @@
   std::unique_ptr<xml::Node> root;
 };
 
-/**
- * Inflates an XML DOM from a text stream, logging errors to the logger.
- * Returns the root node on success, or nullptr on failure.
- */
-std::unique_ptr<XmlResource> Inflate(std::istream* in, IDiagnostics* diag, const Source& source);
+// Inflates an XML DOM from an InputStream, logging errors to the logger.
+// Returns the root node on success, or nullptr on failure.
+std::unique_ptr<XmlResource> Inflate(io::InputStream* in, IDiagnostics* diag, const Source& source);
 
-/**
- * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
- * Returns the root node on success, or nullptr on failure.
- */
+// Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
+// Returns the root node on success, or nullptr on failure.
 std::unique_ptr<XmlResource> Inflate(const void* data, size_t data_len, IDiagnostics* diag,
                                      const Source& source);
 
 Element* FindRootElement(XmlResource* doc);
 Element* FindRootElement(Node* node);
 
-/**
- * A visitor interface for the different XML Node subtypes. This will not
- * traverse into
- * children. Use Visitor for that.
- */
+// A visitor interface for the different XML Node subtypes. This will not traverse into children.
+// Use Visitor for that.
 class RawVisitor {
  public:
   virtual ~RawVisitor() = default;
@@ -179,18 +157,22 @@
   virtual void Visit(Text* text) {}
 };
 
-/**
- * Visitor whose default implementation visits the children nodes of any node.
- */
+// Visitor whose default implementation visits the children nodes of any node.
 class Visitor : public RawVisitor {
  public:
   using RawVisitor::Visit;
 
-  void Visit(Namespace* node) override { VisitChildren(node); }
+  void Visit(Namespace* node) override {
+    VisitChildren(node);
+  }
 
-  void Visit(Element* node) override { VisitChildren(node); }
+  void Visit(Element* node) override {
+    VisitChildren(node);
+  }
 
-  void Visit(Text* text) override { VisitChildren(text); }
+  void Visit(Text* text) override {
+    VisitChildren(text);
+  }
 
   void VisitChildren(Node* node) {
     for (auto& child : node->children) {
@@ -199,9 +181,7 @@
   }
 };
 
-/**
- * An XML DOM visitor that will record the package name for a namespace prefix.
- */
+// An XML DOM visitor that will record the package name for a namespace prefix.
 class PackageAwareVisitor : public Visitor, public IPackageDeclStack {
  public:
   using Visitor::Visit;
@@ -233,7 +213,9 @@
 
   T* value = nullptr;
 
-  void Visit(T* v) override { value = v; }
+  void Visit(T* v) override {
+    value = v;
+  }
 };
 
 template <typename T>
diff --git a/tools/aapt2/xml/XmlDom_test.cpp b/tools/aapt2/xml/XmlDom_test.cpp
index f0122e8..1340ada 100644
--- a/tools/aapt2/xml/XmlDom_test.cpp
+++ b/tools/aapt2/xml/XmlDom_test.cpp
@@ -16,23 +16,20 @@
 
 #include "xml/XmlDom.h"
 
-#include <sstream>
 #include <string>
 
+#include "io/StringInputStream.h"
 #include "test/Test.h"
 
+using ::aapt::io::StringInputStream;
 using ::testing::Eq;
 using ::testing::NotNull;
 using ::testing::SizeIs;
 
 namespace aapt {
 
-constexpr const char* kXmlPreamble =
-    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
-
 TEST(XmlDomTest, Inflate) {
-  std::stringstream in(kXmlPreamble);
-  in << R"(
+  std::string input = R"(<?xml version="1.0" encoding="utf-8"?>
       <Layout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="wrap_content">
@@ -41,9 +38,9 @@
             android:layout_height="wrap_content" />
       </Layout>)";
 
-  const Source source("test.xml");
   StdErrDiagnostics diag;
-  std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, source);
+  StringInputStream in(input);
+  std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, Source("test.xml"));
   ASSERT_THAT(doc, NotNull());
 
   xml::Namespace* ns = xml::NodeCast<xml::Namespace>(doc->root.get());
diff --git a/tools/aapt2/xml/XmlPullParser.cpp b/tools/aapt2/xml/XmlPullParser.cpp
index c2a9c82..30bdc50 100644
--- a/tools/aapt2/xml/XmlPullParser.cpp
+++ b/tools/aapt2/xml/XmlPullParser.cpp
@@ -22,14 +22,15 @@
 #include "xml/XmlPullParser.h"
 #include "xml/XmlUtil.h"
 
-using android::StringPiece;
+using ::aapt::io::InputStream;
+using ::android::StringPiece;
 
 namespace aapt {
 namespace xml {
 
 constexpr char kXmlNamespaceSep = 1;
 
-XmlPullParser::XmlPullParser(std::istream& in) : in_(in), empty_(), depth_(0) {
+XmlPullParser::XmlPullParser(InputStream* in) : in_(in), empty_(), depth_(0) {
   parser_ = XML_ParserCreateNS(nullptr, kXmlNamespaceSep);
   XML_SetUserData(parser_, this);
   XML_SetElementHandler(parser_, StartElementHandler, EndElementHandler);
@@ -40,30 +41,35 @@
   event_queue_.push(EventData{Event::kStartDocument, 0, depth_++});
 }
 
-XmlPullParser::~XmlPullParser() { XML_ParserFree(parser_); }
+XmlPullParser::~XmlPullParser() {
+  XML_ParserFree(parser_);
+}
 
 XmlPullParser::Event XmlPullParser::Next() {
   const Event currentEvent = event();
-  if (currentEvent == Event::kBadDocument ||
-      currentEvent == Event::kEndDocument) {
+  if (currentEvent == Event::kBadDocument || currentEvent == Event::kEndDocument) {
     return currentEvent;
   }
 
   event_queue_.pop();
   while (event_queue_.empty()) {
-    in_.read(buffer_, sizeof(buffer_) / sizeof(*buffer_));
+    const char* buffer = nullptr;
+    size_t buffer_size = 0;
+    bool done = false;
+    if (!in_->Next(reinterpret_cast<const void**>(&buffer), &buffer_size)) {
+      if (in_->HadError()) {
+        error_ = in_->GetError();
+        event_queue_.push(EventData{Event::kBadDocument});
+        break;
+      }
 
-    const bool done = in_.eof();
-    if (in_.bad() && !done) {
-      error_ = strerror(errno);
-      event_queue_.push(EventData{Event::kBadDocument});
-      continue;
+      done = true;
     }
 
-    if (XML_Parse(parser_, buffer_, in_.gcount(), done) == XML_STATUS_ERROR) {
+    if (XML_Parse(parser_, buffer, buffer_size, done) == XML_STATUS_ERROR) {
       error_ = XML_ErrorString(XML_GetErrorCode(parser_));
       event_queue_.push(EventData{Event::kBadDocument});
-      continue;
+      break;
     }
 
     if (done) {
diff --git a/tools/aapt2/xml/XmlPullParser.h b/tools/aapt2/xml/XmlPullParser.h
index cdeeefd..a00caa1 100644
--- a/tools/aapt2/xml/XmlPullParser.h
+++ b/tools/aapt2/xml/XmlPullParser.h
@@ -31,6 +31,7 @@
 #include "androidfw/StringPiece.h"
 
 #include "Resource.h"
+#include "io/Io.h"
 #include "process/IResourceTableConsumer.h"
 #include "util/Maybe.h"
 #include "xml/XmlUtil.h"
@@ -64,7 +65,7 @@
   static bool SkipCurrentElement(XmlPullParser* parser);
   static bool IsGoodEvent(Event event);
 
-  explicit XmlPullParser(std::istream& in);
+  explicit XmlPullParser(io::InputStream* in);
   ~XmlPullParser();
 
   /**
@@ -169,9 +170,8 @@
     std::vector<Attribute> attributes;
   };
 
-  std::istream& in_;
+  io::InputStream* in_;
   XML_Parser parser_;
-  char buffer_[16384];
   std::queue<EventData> event_queue_;
   std::string error_;
   const std::string empty_;
@@ -228,18 +228,15 @@
   return out;
 }
 
-inline bool XmlPullParser::NextChildNode(XmlPullParser* parser,
-                                         size_t start_depth) {
+inline bool XmlPullParser::NextChildNode(XmlPullParser* parser, size_t start_depth) {
   Event event;
 
   // First get back to the start depth.
-  while (IsGoodEvent(event = parser->Next()) &&
-         parser->depth() > start_depth + 1) {
+  while (IsGoodEvent(event = parser->Next()) && parser->depth() > start_depth + 1) {
   }
 
   // Now look for the first good node.
-  while ((event != Event::kEndElement || parser->depth() > start_depth) &&
-         IsGoodEvent(event)) {
+  while ((event != Event::kEndElement || parser->depth() > start_depth) && IsGoodEvent(event)) {
     switch (event) {
       case Event::kText:
       case Event::kComment:
diff --git a/tools/aapt2/xml/XmlPullParser_test.cpp b/tools/aapt2/xml/XmlPullParser_test.cpp
index 1cce485..681d9d4 100644
--- a/tools/aapt2/xml/XmlPullParser_test.cpp
+++ b/tools/aapt2/xml/XmlPullParser_test.cpp
@@ -16,21 +16,22 @@
 
 #include "xml/XmlPullParser.h"
 
-#include <sstream>
-
 #include "androidfw/StringPiece.h"
 
+#include "io/StringInputStream.h"
 #include "test/Test.h"
 
-using android::StringPiece;
+using ::aapt::io::StringInputStream;
+using ::android::StringPiece;
 
 namespace aapt {
 
 TEST(XmlPullParserTest, NextChildNodeTraversesCorrectly) {
-  std::stringstream str;
-  str << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
-         "<a><b><c xmlns:a=\"http://schema.org\"><d/></c><e/></b></a>";
-  xml::XmlPullParser parser(str);
+  std::string str =
+      R"(<?xml version="1.0" encoding="utf-8"?>
+         <a><b><c xmlns:a="http://schema.org"><d/></c><e/></b></a>)";
+  StringInputStream input(str);
+  xml::XmlPullParser parser(&input);
 
   const size_t depth_outer = parser.depth();
   ASSERT_TRUE(xml::XmlPullParser::NextChildNode(&parser, depth_outer));