blob: 6726df3e59202751520ded27f3aa408ed8c80db3 [file] [log] [blame]
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020019extern "C"
20{
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050021 #include "digest/md5.h"
22 #include "libcrecovery/common.h"
23}
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020024
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050025#include <vector>
26#include <string>
27#include <sstream>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <sys/wait.h>
31#include <string.h>
32#include <libgen.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <fstream>
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050036#include <sys/mman.h>
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050038#include "data.hpp"
39#include "variables.h"
40#include "twrp-functions.hpp"
41#include "twrpDigest.hpp"
42
43using namespace std;
44
45void twrpDigest::setfn(string fn) {
46 md5fn = fn;
47}
48
49int twrpDigest::computeMD5(void) {
50 string line;
51 struct MD5Context md5c;
52 FILE *file;
53 int len;
54 unsigned char buf[1024];
55 MD5Init(&md5c);
56 file = fopen(md5fn.c_str(), "rb");
57 if (file == NULL)
58 return -1;
59 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
60 MD5Update(&md5c, buf, len);
61 }
Dees_Troy2673cec2013-04-02 20:22:16 +000062 fclose(file);
Matt Mower0251abc2014-04-08 21:33:08 -050063 MD5Final(md5sum, &md5c);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050064 return 0;
65}
66
67int twrpDigest::write_md5digest(void) {
68 int i;
69 string md5string, md5file;
70 char hex[3];
71 md5file = md5fn + ".md5";
72
73 for (i = 0; i < 16; ++i) {
Matt Mower0251abc2014-04-08 21:33:08 -050074 snprintf(hex, 3, "%02x", md5sum[i]);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050075 md5string += hex;
76 }
77 md5string += " ";
78 md5string += basename((char*) md5fn.c_str());
79 md5string += + "\n";
80 TWFunc::write_file(md5file, md5string);
bigbiff bigbiff6b059542013-09-10 10:28:26 -040081 LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str());
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050082 return 0;
83}
84
85int twrpDigest::read_md5digest(void) {
Matt Mower0251abc2014-04-08 21:33:08 -050086 int i = 0;
87 bool foundMd5File = false;
88 string md5file = "";
89 vector<string> md5ext;
90 md5ext.push_back(".md5");
91 md5ext.push_back(".md5sum");
92
93 while (i < md5ext.size()) {
94 md5file = md5fn + md5ext[i];
95 if (TWFunc::Path_Exists(md5file)) {
96 foundMd5File = true;
97 break;
98 }
99 i++;
100 }
101
102 if (!foundMd5File) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500103 return -1;
Matt Mower0251abc2014-04-08 21:33:08 -0500104 } else if (TWFunc::read_file(md5file, line) != 0) {
105 LOGERR("Could not read %s\n", md5file.c_str());
106 }
107
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500108 return 0;
109}
110
111int twrpDigest::verify_md5digest(void) {
112 string buf;
113 char hex[3];
114 int i;
115 string md5string;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500116 if (read_md5digest() != 0)
117 return -1;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400118 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500119 vector<string> tokens;
120 while (ss >> buf)
121 tokens.push_back(buf);
122 computeMD5();
123 for (i = 0; i < 16; ++i) {
124 snprintf(hex, 3, "%02x", md5sum[i]);
125 md5string += hex;
126 }
127 if (tokens.at(0) != md5string)
128 return -2;
129 return 0;
130}