Merge "trying to fix md5 Change-Id: I4ec037f76aa965bc818afe924942adbe9a080b36" into twrp2.4
diff --git a/partition.cpp b/partition.cpp
index 346c298..681604f 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -941,30 +941,46 @@
 }
 
 bool TWPartition::Check_MD5(string restore_folder) {
-	string Full_Filename;
+	string Full_Filename, md5file;
 	char split_filename[512];
 	int index = 0;
 	twrpDigest md5sum;
 
+	memset(split_filename, 0, sizeof(split_filename));
 	Full_Filename = restore_folder + "/" + Backup_FileName;
 	if (!TWFunc::Path_Exists(Full_Filename)) {
 		// This is a split archive, we presume
 		sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
+		LOGI("split_filename: %s\n", split_filename);
+		md5file = split_filename;
+		md5file += ".md5";
+		if (!TWFunc::Path_Exists(md5file)) {
+			LOGE("No md5 file found for '%s'.\n", split_filename);
+			LOGE("Please unselect Enable MD5 verification to restore.\n");
+			return false;
+		}
+		md5sum.setfn(split_filename);
 		while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
-			md5sum.setfn(split_filename);
 			if (md5sum.verify_md5digest() != 0) {
 				LOGE("MD5 failed to match on '%s'.\n", split_filename);
 				return false;
 			}
 			index++;
 			sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
+			md5sum.setfn(split_filename);
 		}
 		return true;
 	} else {
 		// Single file archive
+		md5file = Full_Filename + ".md5";
+		if (!TWFunc::Path_Exists(md5file)) {
+			LOGE("No md5 file found for '%s'.\n", Full_Filename.c_str());
+			LOGE("Please unselect Enable MD5 verification to restore.\n");
+			return false;
+		}
 		md5sum.setfn(Full_Filename);
 		if (md5sum.verify_md5digest() != 0) {
-			LOGE("MD5 failed to match on '%s'.\n", split_filename);
+			LOGE("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
 			return false;
 		} else
 			return true;
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 9beb257..4364a22 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -501,12 +501,8 @@
 		string strfn;
 		sprintf(filename, "%s%03i", Full_File.c_str(), index);
 		strfn = filename;
-		ostringstream intToStr;
-		ostringstream fn;
 		while (TWFunc::Path_Exists(filename) == true) {
-			intToStr << index;
-			fn << setw(3) << setfill('0') << intToStr.str();
-			md5sum.setfn(strfn);
+			md5sum.setfn(filename);
 			if (md5sum.computeMD5() == 0) {
 				if (md5sum.write_md5digest() != 0)
 				{
diff --git a/twrpDigest.cpp b/twrpDigest.cpp
index 16cda3d..add498c 100644
--- a/twrpDigest.cpp
+++ b/twrpDigest.cpp
@@ -84,7 +84,7 @@
 
 int twrpDigest::read_md5digest(void) {
 	string md5file = md5fn + ".md5";
-	if (TWFunc::read_file(md5file, lines) != 0)
+	if (TWFunc::read_file(md5file, line) != 0)
 		return -1;
 	return 0;
 }
@@ -94,10 +94,9 @@
 	char hex[3];
 	int i;
 	string md5string;
-
 	if (read_md5digest() != 0)
 		return -1;
-	stringstream ss(lines.at(0));
+	stringstream ss(line);
 	vector<string> tokens;
 	while (ss >> buf)
 		tokens.push_back(buf);
diff --git a/twrpDigest.hpp b/twrpDigest.hpp
index 80828fc..395c0ae 100644
--- a/twrpDigest.hpp
+++ b/twrpDigest.hpp
@@ -30,6 +30,6 @@
 	private:
 		int read_md5digest(void);
 		string md5fn;
-		vector<string> lines;
+		string line;
 		unsigned char md5sum[MD5LENGTH];
 };