ADB: Add adb backup for TWRP.

Functionality for client side to backup
tar and image streams over adbd to the client under backup.ab.

Using adb backup on the client side you can backup the partitions
TWRP knows about.

On the client side you can do the following:
adb backup -f <filename> --twrp <options> where options are
--compress: compress data
system: backup system
cache: backup cache
data: backup data
boot: backup boot
etc for each partition.

You can string multiple options,
i.e. adb backup -f <filename> --twrp --compress cache system data

adb backup in TWRP will take any option corresponding
to TWRP fstab partitions, e.g. efs boot as well.

If you do not specify the filename with the -f option,
adb will backup your data to a filename backup.ab on the client.
You can then rename the file and encrypt it with desktop tools.

If you don't want to use command line arguments:
adb backup --twrp

will bring up the gui and allow you to choose partitions
from the backup page.

To restore the backup use the following convention:
adb restore <filename>

Structures are used to store metadata in binary inside
of the file itself. If the metadata structure is modified,
update the adb version so that it will invalidate older
backups and not cause issues on restore. When restoring,
we currently do not support picking specific partitions.
It's all or nothing.

Change-Id: Idb92c37fc9801dc8d89ed2a4570e9d12e76facf8
diff --git a/twrpDigest.cpp b/twrpDigest.cpp
index 0dd3ea9..ab8f456 100644
--- a/twrpDigest.cpp
+++ b/twrpDigest.cpp
@@ -48,13 +48,47 @@
 	md5fn = fn;
 }
 
+void twrpDigest::initMD5(void) {
+	MD5Init(&md5c);
+	md5string = "";
+}
+
+int twrpDigest::updateMD5stream(unsigned char* stream, int len) {
+	if (md5fn.empty()) {
+		MD5Update(&md5c, stream, len);
+	}
+	else {
+		return -1;
+	}
+	return 0;
+}
+
+void twrpDigest::finalizeMD5stream() {
+	MD5Final(md5sum, &md5c);
+}
+
+string twrpDigest::createMD5string() {
+	int i;
+	char hex[3];
+
+	for (i = 0; i < 16; ++i) {
+		snprintf(hex, 3, "%02x", md5sum[i]);
+		md5string += hex;
+	}
+	if (!md5fn.empty()) {
+		md5string += "  ";
+		md5string += basename((char*) md5fn.c_str());
+		md5string +=  + "\n";
+	}
+	return md5string;
+}
+
 int twrpDigest::computeMD5(void) {
 	string line;
-	struct MD5Context md5c;
 	FILE *file;
 	int len;
 	unsigned char buf[1024];
-	MD5Init(&md5c);
+	initMD5();
 	file = fopen(md5fn.c_str(), "rb");
 	if (file == NULL)
 		return -1;
@@ -67,21 +101,13 @@
 }
 
 int twrpDigest::write_md5digest(void) {
-	int i;
-	string md5string, md5file;
-	char hex[3];
+	string md5file, md5str;
 	md5file = md5fn + ".md5";
 
-	for (i = 0; i < 16; ++i) {
-		snprintf(hex, 3, "%02x", md5sum[i]);
-		md5string += hex;
-	}
-	md5string += "  ";
-	md5string += basename((char*) md5fn.c_str());
-	md5string +=  + "\n";
-	TWFunc::write_file(md5file, md5string);
+	md5str = createMD5string();
+	TWFunc::write_file(md5file, md5str);
 	tw_set_default_metadata(md5file.c_str());
-	LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str());
+	LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5str.c_str());
 	return 0;
 }
 
@@ -124,7 +150,7 @@
 	string buf;
 	char hex[3];
 	int i, ret;
-	string md5string;
+	string md5str;
 
 	ret = read_md5digest();
 	if (ret != 0)
@@ -136,9 +162,9 @@
 	computeMD5();
 	for (i = 0; i < 16; ++i) {
 		snprintf(hex, 3, "%02x", md5sum[i]);
-		md5string += hex;
+		md5str += hex;
 	}
-	if (tokens.at(0) != md5string) {
+	if (tokens.at(0) != md5str) {
 		gui_err("md5_fail=MD5 does not match");
 		return -2;
 	}