xml: convert ABX xml files to plain text xml files using integrated code

Change-Id: I1c24360af7bba8f05064759753e0b3b5acc6f277
diff --git a/abx-functions.hpp b/abx-functions.hpp
new file mode 100755
index 0000000..003586d
--- /dev/null
+++ b/abx-functions.hpp
@@ -0,0 +1,289 @@
+#ifndef _ABXFUNCTIONS_HPP
+#define _ABXFUNCTIONS_HPP
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <cstring>
+#include <vector>
+#include <algorithm>
+#include <openssl/evp.h>
+
+// ABX (Android Binary XML) to XML converter
+// Copyright (c) 2022, _that
+// SPDX-License-Identifier: GPL-3.0-only
+
+// test: g++ -g abxtoxml.cpp `pkg-config --libs openssl` && ./a.out packages.xml
+
+// based on https://www.cclsolutionsgroup.com/post/android-abx-binary-xml
+
+using namespace std;
+
+class AbxToXml
+{
+	std::istream& m_is;
+	std::ostream& m_os;
+	bool mTagOpen = false;
+	std::vector<std::string> mInternedStrings;
+	bool mError = true;
+
+public:
+	AbxToXml(std::istream& is, std::ostream& os) : m_is(is), m_os(os) {}
+
+	bool run()
+	{
+		mError = true;
+		char buffer[4];
+		m_is.read(buffer, 4);
+		if (memcmp(buffer, "ABX", 3) != 0)
+			// TODO: handle error: not an ABX file
+			return false;
+
+		if (buffer[3] != 0)
+			// TODO: handle error: we only understand ABX version 0
+			return false;
+
+		mError = false;
+		return convert_abx_content();
+	}
+
+private:
+	enum Event
+	{
+		// based on XmlPullParser.java in libcore
+		START_DOCUMENT = 0,
+		END_DOCUMENT = 1,
+		START_TAG = 2,
+		END_TAG = 3,
+		TEXT = 4,
+		CDSECT = 5,
+		ENTITY_REF = 6,
+		IGNORABLE_WHITESPACE = 7,
+		PROCESSING_INSTRUCTION = 8,
+		COMMENT = 9,
+		DOCDECL = 10,
+
+		// based on BinaryXmlSerializer.java
+		ATTRIBUTE = 15,
+	};
+
+	// based on BinaryXmlSerializer.java
+	enum Type
+	{
+		TYPE_NULL = 1 << 4,
+		TYPE_STRING = 2 << 4,
+		TYPE_STRING_INTERNED = 3 << 4,
+		TYPE_BYTES_HEX = 4 << 4,
+		TYPE_BYTES_BASE64 = 5 << 4,
+		TYPE_INT = 6 << 4,
+		TYPE_INT_HEX = 7 << 4,
+		TYPE_LONG = 8 << 4,
+		TYPE_LONG_HEX = 9 << 4,
+		TYPE_FLOAT = 10 << 4,
+		TYPE_DOUBLE = 11 << 4,
+		TYPE_BOOLEAN_TRUE = 12 << 4,
+		TYPE_BOOLEAN_FALSE = 13 << 4,
+	};
+
+
+	bool convert_abx_content()
+	{
+		char b;
+		while (m_is.get(b))
+		{
+			Event ev = Event(b & 0xf);
+			Type ty = Type(b & 0xf0);
+
+			if (ev == ATTRIBUTE)
+			{
+				// TODO: verify that tag is still open
+				m_os << " " << read_data(TYPE_STRING_INTERNED);
+				m_os << "=\"";
+				m_os << read_data(ty);		// TODO: escaping?
+				m_os << '"';
+				continue;
+			}
+			if (mTagOpen)
+			{
+				m_os << ">";
+				mTagOpen = false;
+			}
+			std::string data = read_data(ty);
+			dispatch_event(ev, data);
+		}
+		return !mError;
+	}
+
+	void dispatch_event(Event ev, const std::string& data)
+	{
+		switch (ev)
+		{
+			case START_DOCUMENT:
+			case END_DOCUMENT:
+				// TODO: track that we actually started the document
+				break;
+
+			case START_TAG:
+				m_os << "<" << data;
+				mTagOpen = true;
+				break;
+
+			case END_TAG:
+				m_os << "</" << data << ">";
+				break;
+
+			case TEXT:
+				m_os << data;
+				break;
+
+			case CDSECT:
+				m_os << "<![CDATA[" <<  data << "]]>";
+				break;
+
+			case ENTITY_REF:
+			case IGNORABLE_WHITESPACE:
+			case PROCESSING_INSTRUCTION:
+			case COMMENT:
+			case DOCDECL:
+				m_os << data;
+				// TODO
+				break;
+
+			default:
+				m_os << "#error: Invalid event " << int(ev);
+				mError = true;
+				break;
+		}
+	}
+
+	template <typename T> static T read_bswap(std::istream& is)
+	{
+		char buffer[sizeof(T)];
+		is.read(buffer, sizeof(T));
+		std::reverse(std::begin(buffer), std::end(buffer));
+		return *reinterpret_cast<T*>(buffer);
+	}
+
+	static uint16_t read_uint16(std::istream& is)
+	{
+		return read_bswap<uint16_t>(is);
+	}
+
+	static std::string read_string(std::istream& is)
+	{
+		uint16_t length = read_uint16(is);
+		std::string s;
+		s.resize(length);
+		is.read(&s[0], length);
+		return s;
+	}
+
+
+	std::string read_data(Type ty)
+	{
+		switch (ty)
+		{
+			case TYPE_NULL:
+				return {};
+
+			case TYPE_STRING:
+				return read_string(m_is);
+
+			case TYPE_STRING_INTERNED:
+				{
+					uint16_t id = read_uint16(m_is);
+					if (id == 0xffff)
+					{
+						std::string s = read_string(m_is);
+						mInternedStrings.push_back(s);
+						return s;
+					}
+					if (id >= mInternedStrings.size())
+					{
+						// TODO: handle error
+						mError = true;
+						return "#error: invalid string ID";
+					}
+					return mInternedStrings.at(id);
+				}
+
+			case TYPE_BYTES_HEX:
+				{
+					std::string s = read_string(m_is);
+					std::string hex;
+					static const char* hexdigits = "0123456789abcdef";
+					for (unsigned char c : s)
+					{
+						hex += hexdigits[c >> 4];
+						hex += hexdigits[c & 0xf];
+					}
+					return hex;
+				}
+
+			case TYPE_BYTES_BASE64:
+				{
+					std::string s = read_string(m_is);
+					std::string b64;
+					auto outlen = ((s.length() + 2) / 3) * 4;
+					b64.resize(outlen+1);		// +1 for null terminator
+					auto got = EVP_EncodeBlock(
+						reinterpret_cast<unsigned char *>(&b64[0]),
+						reinterpret_cast<const unsigned char*>(s.c_str()), s.length());
+					if (got != outlen)
+					{
+						mError = true;
+						return "#error: base64 encoding failed";  // TODO
+					}
+					b64.resize(outlen);
+					return b64;
+				}
+
+			case TYPE_INT_HEX:
+				// TODO: output hex instead of dec (what is the exact format?)
+			case TYPE_INT:
+				{
+					int32_t val = read_bswap<int>(m_is);
+					std::stringstream ss;
+					ss << val;
+					return ss.str();
+				}
+
+			case TYPE_LONG_HEX:
+				// TODO: output hex instead of dec (what is the exact format?)
+			case TYPE_LONG:
+				{
+					int64_t val = read_bswap<long>(m_is);
+					std::stringstream ss;
+					ss << val;
+					return ss.str();
+				}
+
+			case TYPE_FLOAT:
+				{
+					float val = read_bswap<float>(m_is);
+					std::stringstream ss;
+					ss << val;
+					return ss.str();
+				}
+
+			case TYPE_DOUBLE:
+				{
+					double val = read_bswap<double>(m_is);
+					std::stringstream ss;
+					ss << val;
+					return ss.str();
+				}
+
+			case TYPE_BOOLEAN_TRUE:
+				return "1";
+
+			case TYPE_BOOLEAN_FALSE:
+				return "0";
+		}
+		// TODO
+		mError = true;
+		return "#error: invalid type";
+	}
+};
+#endif // _ABXFUNCTIONS_HPP
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index dd8470b..01a108c 100755
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -1856,7 +1856,14 @@
 			user.userId = to_string(userId);
 
 			// Attempt to get name of user. Fallback to user ID if this fails.
-			char* userFile = PageManager::LoadFileToBuffer("/data/system/users/" + to_string(userId) + ".xml", NULL);
+			std::string path = "/data/system/users/" + to_string(userId) + ".xml";
+			if (!TWFunc::Check_Xml_Format(path)) {
+				string oldpath = path;
+				if (TWFunc::abx_to_xml(oldpath, path)) {
+					LOGINFO("Android 12+: '%s' has been converted into plain text xml (for user %s).\n", oldpath.c_str(), user.userId.c_str());
+				}
+			}
+			char* userFile = PageManager::LoadFileToBuffer(path, NULL);
 			if (userFile == NULL) {
 				user.userName = to_string(userId);
 			}
@@ -3006,8 +3013,17 @@
 		LOGERR("Cannot decrypt adopted storage because /data will not mount\n");
 		return false;
 	}
+
+	std::string path = "/data/system/storage.xml";
+	if (!TWFunc::Check_Xml_Format(path)) {
+		std::string oldpath = path;
+		if (TWFunc::abx_to_xml(oldpath, path)) {
+			LOGINFO("Android 12+: '%s' has been converted into plain text xml (%s).\n", oldpath.c_str(), path.c_str());
+		}
+	}
+
 	LOGINFO("Decrypt adopted storage starting\n");
-	char* xmlFile = PageManager::LoadFileToBuffer("/data/system/storage.xml", NULL);
+	char* xmlFile = PageManager::LoadFileToBuffer(path, NULL);
 	xml_document<> *doc = NULL;
 	xml_node<>* volumes = NULL;
 	string Primary_Storage_UUID = "";
@@ -3615,4 +3631,4 @@
 		return false;
 	}
 	return true;
-}
\ No newline at end of file
+}
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index b557684..7ee5139 100755
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -44,6 +44,7 @@
 #include <android-base/chrono_utils.h>
 
 #include "twrp-functions.hpp"
+#include "abx-functions.hpp"
 #include "twcommon.h"
 #include "gui/gui.hpp"
 #ifndef BUILD_TWRPTAR_MAIN
@@ -1539,4 +1540,58 @@
 exit:
 	return TW_DEFAULT_RECOVERY_FOLDER;
 }
+
+bool TWFunc::Check_Xml_Format(const std::string filename) {
+	std::string buffer(' ', 4);
+	std::string abx_hdr("ABX\x00", 4);
+	std::ifstream File;
+	File.open(filename);
+	if (File.is_open()) {
+		File.get(&buffer[0], buffer.size());
+		File.close();
+		// Android Binary Xml start from these bytes
+		if(!buffer.compare(0, abx_hdr.size(), abx_hdr))
+			return false; // ABX format - requires conversion
+	}
+	return true; // good format, possible to parse
+}
+
+// return true=successful conversion (return the name of the converted file in "result");
+// return false=an error happened (leave "result" alone)
+bool TWFunc::abx_to_xml(const std::string path, std::string &result) {
+	bool res = false;
+	if (!TWFunc::Path_Exists(path))
+		return res;
+
+	std::ifstream infile(path);
+	if (!infile.is_open())
+		return res;
+
+	std::string fname = TWFunc::Get_Filename(path);
+	std::string tmp = "/tmp/converted_xml";
+	if (!TWFunc::Path_Exists(tmp)) {
+		if (mkdir(tmp.c_str(), 0777) != 0)
+			tmp = "/tmp";
+	}
+
+	std::string tmp_path = tmp + "/" + fname;
+	std::ofstream outfile(tmp_path);
+	if (!outfile.is_open()) {
+		LOGINFO("Error. The abx conversion of %s has failed.\n", path.c_str());
+		infile.close();
+		return res;
+	}
+
+	AbxToXml r(infile, outfile);
+	if (r.run() && TWFunc::Path_Exists(tmp_path)) {
+		res = true;
+		result = tmp_path;
+	}
+
+	infile.close();
+	outfile.close();
+
+	return res;
+}
+
 #endif // ndef BUILD_TWRPTAR_MAIN
diff --git a/twrp-functions.hpp b/twrp-functions.hpp
index e5656f5..6221741 100755
--- a/twrp-functions.hpp
+++ b/twrp-functions.hpp
@@ -131,7 +131,9 @@
 	static void List_Mounts(); // List current mounts by the kernel
 	static void Clear_Bootloader_Message(); // Removes the bootloader message from misc for next boot
 	static string Check_For_TwrpFolder(); // Gets user defined path on storage where backups should be stored
+	static bool Check_Xml_Format(const std::string filename); // Return whether a xml is in plain xml or ABX format
 
+	static bool abx_to_xml(const std::string path, std::string &result); // could we convert abx to xml (if so, return the full path to the converted file)
 private:
 	static void Copy_Log(string Source, string Destination);