Fix windows build of AAPT2

Change-Id: Ib8e1a4322510b582e9600a08d3118842c9abc73c
diff --git a/tools/aapt2/BigBuffer.h b/tools/aapt2/BigBuffer.h
index 025142b..8b6569c 100644
--- a/tools/aapt2/BigBuffer.h
+++ b/tools/aapt2/BigBuffer.h
@@ -17,6 +17,7 @@
 #ifndef AAPT_BIG_BUFFER_H
 #define AAPT_BIG_BUFFER_H
 
+#include <cassert>
 #include <cstring>
 #include <memory>
 #include <vector>
diff --git a/tools/aapt2/BinaryResourceParser.cpp b/tools/aapt2/BinaryResourceParser.cpp
index d58f05a..3eb96bc 100644
--- a/tools/aapt2/BinaryResourceParser.cpp
+++ b/tools/aapt2/BinaryResourceParser.cpp
@@ -157,8 +157,12 @@
         return false;
     }
 
+    if (reinterpret_cast<uintptr_t>(data) < reinterpret_cast<uintptr_t>(mData)) {
+        return false;
+    }
+
     // We only support 32 bit offsets right now.
-    const ptrdiff_t offset = reinterpret_cast<uintptr_t>(data) -
+    const uintptr_t offset = reinterpret_cast<uintptr_t>(data) -
             reinterpret_cast<uintptr_t>(mData);
     if (offset > std::numeric_limits<uint32_t>::max()) {
         return false;
@@ -227,7 +231,7 @@
         return false;
     }
 
-    if (mSymbolPool.setTo(parser.getChunk(), parser.getChunk()->size) != android::NO_ERROR) {
+    if (mSymbolPool.setTo(parser.getChunk(), parser.getChunk()->size) != NO_ERROR) {
         Logger::error(mSource)
                 << "failed to parse symbol string pool with code: "
                 << mSymbolPool.getError()
@@ -252,9 +256,9 @@
     while (ResChunkPullParser::isGoodEvent(parser.next())) {
         switch (parser.getChunk()->type) {
         case android::RES_STRING_POOL_TYPE:
-            if (mValuePool.getError() == android::NO_INIT) {
+            if (mValuePool.getError() == NO_INIT) {
                 if (mValuePool.setTo(parser.getChunk(), parser.getChunk()->size) !=
-                        android::NO_ERROR) {
+                        NO_ERROR) {
                     Logger::error(mSource)
                             << "failed to parse value string pool with code: "
                             << mValuePool.getError()
@@ -281,7 +285,7 @@
 
         case RES_TABLE_SOURCE_POOL_TYPE: {
             if (mSourcePool.setTo(getChunkData(*parser.getChunk()),
-                        getChunkDataLen(*parser.getChunk())) != android::NO_ERROR) {
+                        getChunkDataLen(*parser.getChunk())) != NO_ERROR) {
                 Logger::error(mSource)
                         << "failed to parse source pool with code: "
                         << mSourcePool.getError()
@@ -319,7 +323,7 @@
 }
 
 bool BinaryResourceParser::parsePackage(const ResChunk_header* chunk) {
-    if (mValuePool.getError() != android::NO_ERROR) {
+    if (mValuePool.getError() != NO_ERROR) {
         Logger::error(mSource)
                 << "no value string pool for ResTable."
                 << std::endl;
@@ -356,9 +360,9 @@
     while (ResChunkPullParser::isGoodEvent(parser.next())) {
         switch (parser.getChunk()->type) {
         case android::RES_STRING_POOL_TYPE:
-            if (mTypePool.getError() == android::NO_INIT) {
+            if (mTypePool.getError() == NO_INIT) {
                 if (mTypePool.setTo(parser.getChunk(), parser.getChunk()->size) !=
-                        android::NO_ERROR) {
+                        NO_ERROR) {
                     Logger::error(mSource)
                             << "failed to parse type string pool with code "
                             << mTypePool.getError()
@@ -366,9 +370,9 @@
                             << std::endl;
                     return false;
                 }
-            } else if (mKeyPool.getError() == android::NO_INIT) {
+            } else if (mKeyPool.getError() == NO_INIT) {
                 if (mKeyPool.setTo(parser.getChunk(), parser.getChunk()->size) !=
-                        android::NO_ERROR) {
+                        NO_ERROR) {
                     Logger::error(mSource)
                             << "failed to parse key string pool with code "
                             << mKeyPool.getError()
@@ -429,7 +433,7 @@
 }
 
 bool BinaryResourceParser::parseTypeSpec(const ResChunk_header* chunk) {
-    if (mTypePool.getError() != android::NO_ERROR) {
+    if (mTypePool.getError() != NO_ERROR) {
         Logger::error(mSource)
                 << "no type string pool available for ResTable_typeSpec."
                 << std::endl;
@@ -456,14 +460,14 @@
 }
 
 bool BinaryResourceParser::parseType(const ResChunk_header* chunk) {
-    if (mTypePool.getError() != android::NO_ERROR) {
+    if (mTypePool.getError() != NO_ERROR) {
         Logger::error(mSource)
                 << "no type string pool available for ResTable_typeSpec."
                 << std::endl;
         return false;
     }
 
-    if (mKeyPool.getError() != android::NO_ERROR) {
+    if (mKeyPool.getError() != NO_ERROR) {
         Logger::error(mSource)
                 << "no key string pool available for ResTable_type."
                 << std::endl;
diff --git a/tools/aapt2/Files.cpp b/tools/aapt2/Files.cpp
index c910c81..349abbd 100644
--- a/tools/aapt2/Files.cpp
+++ b/tools/aapt2/Files.cpp
@@ -22,6 +22,11 @@
 #include <string>
 #include <sys/stat.h>
 
+#ifdef HAVE_MS_C_RUNTIME
+// Windows includes.
+#include <direct.h>
+#endif
+
 namespace aapt {
 
 FileType getFileType(const StringPiece& path) {
@@ -43,10 +48,14 @@
         return FileType::kBlockDev;
     } else if (S_ISFIFO(sb.st_mode)) {
         return FileType::kFifo;
+#if defined(S_ISLNK)
     } else if (S_ISLNK(sb.st_mode)) {
         return FileType::kSymlink;
+#endif
+#if defined(S_ISSOCK)
     } else if (S_ISSOCK(sb.st_mode)) {
         return FileType::kSocket;
+#endif
     } else {
         return FileType::kUnknown;
     }
diff --git a/tools/aapt2/Files.h b/tools/aapt2/Files.h
index e5e196e..37e6f8c 100644
--- a/tools/aapt2/Files.h
+++ b/tools/aapt2/Files.h
@@ -21,6 +21,7 @@
 #include "Source.h"
 #include "StringPiece.h"
 
+#include <cassert>
 #include <string>
 #include <vector>
 
diff --git a/tools/aapt2/JavaClassGenerator.cpp b/tools/aapt2/JavaClassGenerator.cpp
index 7ec2848..779a346 100644
--- a/tools/aapt2/JavaClassGenerator.cpp
+++ b/tools/aapt2/JavaClassGenerator.cpp
@@ -20,6 +20,7 @@
 #include "ResourceValues.h"
 #include "StringPiece.h"
 
+#include <algorithm>
 #include <ostream>
 #include <set>
 #include <sstream>
@@ -87,10 +88,11 @@
         assert(id.isValid());
 
         if (!isValidSymbol(entry->name)) {
-            mError = (std::stringstream()
-                    << "invalid symbol name '"
-                    << StringPiece16(entry->name)
-                    << "'").str();
+            std::stringstream err;
+            err << "invalid symbol name '"
+                << StringPiece16(entry->name)
+                << "'";
+            mError = err.str();
             return false;
         }
 
@@ -164,10 +166,11 @@
             for (const auto& entry : type->entries) {
                 assert(!entry->values.empty());
                 if (!isValidSymbol(entry->name)) {
-                    mError = (std::stringstream()
-                            << "invalid symbol name '"
-                            << StringPiece16(entry->name)
-                            << "'").str();
+                    std::stringstream err;
+                    err << "invalid symbol name '"
+                        << StringPiece16(entry->name)
+                        << "'";
+                    mError = err.str();
                     return false;
                 }
                 entry->values.front().value->accept(*this, GenArgs{ out, *entry });
diff --git a/tools/aapt2/Linker.cpp b/tools/aapt2/Linker.cpp
index a863197..1cfb297 100644
--- a/tools/aapt2/Linker.cpp
+++ b/tools/aapt2/Linker.cpp
@@ -24,6 +24,7 @@
 
 #include <androidfw/AssetManager.h>
 #include <array>
+#include <bitset>
 #include <iostream>
 #include <map>
 #include <ostream>
diff --git a/tools/aapt2/Main.cpp b/tools/aapt2/Main.cpp
index f4e80c5..cfc5874 100644
--- a/tools/aapt2/Main.cpp
+++ b/tools/aapt2/Main.cpp
@@ -171,6 +171,9 @@
 }
 
 bool loadResTable(android::ResTable* table, const Source& source) {
+    // For NO_ERROR (which on Windows is a MACRO).
+    using namespace android;
+
     std::ifstream ifs(source.path, std::ifstream::in | std::ifstream::binary);
     if (!ifs) {
         Logger::error(source) << strerror(errno) << std::endl;
@@ -187,7 +190,7 @@
     char* buf = new char[dataSize];
     ifs.read(buf, dataSize);
 
-    bool result = table->add(buf, dataSize, -1, true) == android::NO_ERROR;
+    bool result = table->add(buf, dataSize, -1, true) == NO_ERROR;
 
     delete [] buf;
     return result;
@@ -426,6 +429,8 @@
 };
 
 bool compileAndroidManifest(std::shared_ptr<Resolver> resolver, const AaptOptions& options) {
+    using namespace android;
+
     Source outSource = options.output;
     appendPath(&outSource.path, "AndroidManifest.xml");
 
@@ -456,8 +461,8 @@
         p += b.size;
     }
 
-    android::ResXMLTree tree;
-    if (tree.setTo(data.get(), outBuffer.size()) != android::NO_ERROR) {
+    ResXMLTree tree;
+    if (tree.setTo(data.get(), outBuffer.size()) != NO_ERROR) {
         return false;
     }
 
diff --git a/tools/aapt2/ManifestValidator.cpp b/tools/aapt2/ManifestValidator.cpp
index 596c758..7ec0bc7 100644
--- a/tools/aapt2/ManifestValidator.cpp
+++ b/tools/aapt2/ManifestValidator.cpp
@@ -151,7 +151,7 @@
                 << "> attribute '"
                 << attributeName
                 << "' has invalid character '"
-                << *badIter
+                << StringPiece16(badIter, 1)
                 << "'."
                 << std::endl;
         return false;
diff --git a/tools/aapt2/Resource.h b/tools/aapt2/Resource.h
index 3fd678e..4d2c64c 100644
--- a/tools/aapt2/Resource.h
+++ b/tools/aapt2/Resource.h
@@ -20,6 +20,7 @@
 #include "StringPiece.h"
 
 #include <iomanip>
+#include <limits>
 #include <string>
 #include <tuple>
 
diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp
index d3720c4..4c96187 100644
--- a/tools/aapt2/ResourceParser.cpp
+++ b/tools/aapt2/ResourceParser.cpp
@@ -973,7 +973,7 @@
 
     std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
     attr->symbols.swap(items);
-    attr->typeMask = typeMask ? typeMask : android::ResTable_map::TYPE_ANY;
+    attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
     return attr;
 }
 
diff --git a/tools/aapt2/ResourceTable.cpp b/tools/aapt2/ResourceTable.cpp
index 0b3dd78..794090d0 100644
--- a/tools/aapt2/ResourceTable.cpp
+++ b/tools/aapt2/ResourceTable.cpp
@@ -164,7 +164,7 @@
                 << "' has invalid entry name '"
                 << name.entry
                 << "'. Invalid character '"
-                << *badCharIter
+                << StringPiece16(badCharIter, 1)
                 << "'."
                 << std::endl;
         return false;
@@ -258,7 +258,7 @@
                 << "' has invalid entry name '"
                 << name.entry
                 << "'. Invalid character '"
-                << *badCharIter
+                << StringPiece16(badCharIter, 1)
                 << "'."
                 << std::endl;
         return false;
@@ -314,21 +314,21 @@
 std::tuple<const ResourceTableType*, const ResourceEntry*>
 ResourceTable::findResource(const ResourceNameRef& name) const {
     if (name.package != mPackage) {
-        return {nullptr, nullptr};
+        return {};
     }
 
     auto iter = std::lower_bound(mTypes.begin(), mTypes.end(), name.type, lessThanType);
     if (iter == mTypes.end() || (*iter)->type != name.type) {
-        return {nullptr, nullptr};
+        return {};
     }
 
     const std::unique_ptr<ResourceTableType>& type = *iter;
     auto iter2 = std::lower_bound(type->entries.begin(), type->entries.end(), name.entry,
                                   lessThanEntry);
     if (iter2 == type->entries.end() || name.entry != (*iter2)->name) {
-        return {nullptr, nullptr};
+        return {};
     }
-    return {iter->get(), iter2->get()};
+    return std::make_tuple(iter->get(), iter2->get());
 }
 
 } // namespace aapt
diff --git a/tools/aapt2/StringPool_test.cpp b/tools/aapt2/StringPool_test.cpp
index 5ee1a2d..85d101a 100644
--- a/tools/aapt2/StringPool_test.cpp
+++ b/tools/aapt2/StringPool_test.cpp
@@ -20,6 +20,8 @@
 #include <gtest/gtest.h>
 #include <string>
 
+using namespace android;
+
 namespace aapt {
 
 TEST(StringPoolTest, InsertOneString) {
@@ -189,28 +191,28 @@
     }
 
     {
-        android::ResStringPool test;
-        ASSERT_EQ(android::NO_ERROR, test.setTo(data, buffer.size()));
+        ResStringPool test;
+        ASSERT_TRUE(test.setTo(data, buffer.size()) == NO_ERROR);
 
         EXPECT_EQ(util::getString(test, 0), u"hello");
         EXPECT_EQ(util::getString(test, 1), u"goodbye");
         EXPECT_EQ(util::getString(test, 2), sLongString);
         EXPECT_EQ(util::getString(test, 3), u"style");
 
-        const android::ResStringPool_span* span = test.styleAt(3);
+        const ResStringPool_span* span = test.styleAt(3);
         ASSERT_NE(nullptr, span);
         EXPECT_EQ(util::getString(test, span->name.index), u"b");
         EXPECT_EQ(0u, span->firstChar);
         EXPECT_EQ(1u, span->lastChar);
         span++;
 
-        ASSERT_NE(android::ResStringPool_span::END, span->name.index);
+        ASSERT_NE(ResStringPool_span::END, span->name.index);
         EXPECT_EQ(util::getString(test, span->name.index), u"i");
         EXPECT_EQ(2u, span->firstChar);
         EXPECT_EQ(3u, span->lastChar);
         span++;
 
-        EXPECT_EQ(android::ResStringPool_span::END, span->name.index);
+        EXPECT_EQ(ResStringPool_span::END, span->name.index);
     }
     delete[] data;
 }
diff --git a/tools/aapt2/TableFlattener.cpp b/tools/aapt2/TableFlattener.cpp
index c306185..67c56e7 100644
--- a/tools/aapt2/TableFlattener.cpp
+++ b/tools/aapt2/TableFlattener.cpp
@@ -24,6 +24,7 @@
 #include "TableFlattener.h"
 #include "Util.h"
 
+#include <algorithm>
 #include <androidfw/ResourceTypes.h>
 #include <sstream>
 
diff --git a/tools/aapt2/Util.h b/tools/aapt2/Util.h
index 2de9568..510ed76 100644
--- a/tools/aapt2/Util.h
+++ b/tools/aapt2/Util.h
@@ -88,7 +88,7 @@
 
 inline ::std::function<::std::ostream&(::std::ostream&)> formatSize(size_t size) {
     return [size](::std::ostream& out) -> ::std::ostream& {
-        constexpr size_t K = 1024;
+        constexpr size_t K = 1024u;
         constexpr size_t M = K * K;
         constexpr size_t G = M * K;
         if (size < K) {
diff --git a/tools/aapt2/XliffXmlPullParser.h b/tools/aapt2/XliffXmlPullParser.h
index d362521..d4aa222 100644
--- a/tools/aapt2/XliffXmlPullParser.h
+++ b/tools/aapt2/XliffXmlPullParser.h
@@ -19,6 +19,7 @@
 
 #include "XmlPullParser.h"
 
+#include <memory>
 #include <string>
 
 namespace aapt {
diff --git a/tools/aapt2/XmlFlattener_test.cpp b/tools/aapt2/XmlFlattener_test.cpp
index 79030be..6e24847 100644
--- a/tools/aapt2/XmlFlattener_test.cpp
+++ b/tools/aapt2/XmlFlattener_test.cpp
@@ -27,6 +27,8 @@
 #include <sstream>
 #include <string>
 
+using namespace android;
+
 namespace aapt {
 
 constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
@@ -40,16 +42,16 @@
 
         table->addResource(ResourceName{ {}, ResourceType::kAttr, u"id" },
                            ResourceId{ 0x01010000 }, {}, {},
-                           util::make_unique<Attribute>(false, android::ResTable_map::TYPE_ANY));
+                           util::make_unique<Attribute>(false, ResTable_map::TYPE_ANY));
 
         table->addResource(ResourceName{ {}, ResourceType::kId, u"test" },
                            ResourceId{ 0x01020000 }, {}, {}, util::make_unique<Id>());
 
         mFlattener = std::make_shared<XmlFlattener>(
-                std::make_shared<Resolver>(table, std::make_shared<android::AssetManager>()));
+                std::make_shared<Resolver>(table, std::make_shared<AssetManager>()));
     }
 
-    ::testing::AssertionResult testFlatten(std::istream& in, android::ResXMLTree* outTree) {
+    ::testing::AssertionResult testFlatten(std::istream& in, ResXMLTree* outTree) {
         std::stringstream input(kXmlPreamble);
         input << in.rdbuf() << std::endl;
         std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(input);
@@ -59,7 +61,7 @@
         }
 
         std::unique_ptr<uint8_t[]> data = util::copy(outBuffer);
-        if (outTree->setTo(data.get(), outBuffer.size(), true) != android::NO_ERROR) {
+        if (outTree->setTo(data.get(), outBuffer.size(), true) != NO_ERROR) {
             return ::testing::AssertionFailure();
         }
         return ::testing::AssertionSuccess();
@@ -74,11 +76,11 @@
           << "      android:id=\"@id/test\">" << std::endl
           << "</View>" << std::endl;
 
-    android::ResXMLTree tree;
+    ResXMLTree tree;
     ASSERT_TRUE(testFlatten(input, &tree));
 
-    while (tree.next() != android::ResXMLTree::END_DOCUMENT) {
-        ASSERT_NE(tree.getEventType(), android::ResXMLTree::BAD_DOCUMENT);
+    while (tree.next() != ResXMLTree::END_DOCUMENT) {
+        ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
     }
 }
 
diff --git a/tools/aapt2/XmlPullParser.h b/tools/aapt2/XmlPullParser.h
index c667df2..753405c 100644
--- a/tools/aapt2/XmlPullParser.h
+++ b/tools/aapt2/XmlPullParser.h
@@ -116,20 +116,6 @@
     const_iterator findAttribute(StringPiece16 namespaceUri, StringPiece16 name) const;
 };
 
-/*
- * Automatically reads up to the end tag of the element it was initialized with
- * when being destroyed.
- */
-class AutoFinishElement {
-public:
-    AutoFinishElement(const std::shared_ptr<XmlPullParser>& parser);
-    ~AutoFinishElement();
-
-private:
-    std::shared_ptr<XmlPullParser> mParser;
-    int mDepth;
-};
-
 //
 // Implementation
 //
@@ -212,23 +198,6 @@
     return endIter;
 }
 
-inline AutoFinishElement::AutoFinishElement(const std::shared_ptr<XmlPullParser>& parser) :
-        mParser(parser), mDepth(parser->getDepth()) {
-}
-
-inline AutoFinishElement::~AutoFinishElement() {
-    int depth;
-    XmlPullParser::Event event;
-    while ((depth = mParser->getDepth()) >= mDepth &&
-            XmlPullParser::isGoodEvent(event = mParser->getEvent())) {
-        if (depth == mDepth && (event == XmlPullParser::Event::kEndElement ||
-                event == XmlPullParser::Event::kEndNamespace)) {
-            return;
-        }
-        mParser->next();
-    }
-}
-
 } // namespace aapt
 
 #endif // AAPT_XML_PULL_PARSER_H