blob: e888d85613f127e3b424f1c6611497ad4213c0d0 [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>
36#include <iostream>
37#include <string>
38#include <sstream>
39#include <dirent.h>
40#include <sys/mman.h>
Dees_Troy2673cec2013-04-02 20:22:16 +000041#include "twcommon.h"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050042#include "data.hpp"
43#include "variables.h"
44#include "twrp-functions.hpp"
45#include "twrpDigest.hpp"
bigbiff bigbiff6b059542013-09-10 10:28:26 -040046#include "twcommon.h"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050047
48using namespace std;
49
50void twrpDigest::setfn(string fn) {
51 md5fn = fn;
52}
53
54int twrpDigest::computeMD5(void) {
55 string line;
56 struct MD5Context md5c;
57 FILE *file;
58 int len;
59 unsigned char buf[1024];
60 MD5Init(&md5c);
61 file = fopen(md5fn.c_str(), "rb");
62 if (file == NULL)
63 return -1;
64 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
65 MD5Update(&md5c, buf, len);
66 }
Dees_Troy2673cec2013-04-02 20:22:16 +000067 fclose(file);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050068 MD5Final(md5sum ,&md5c);
69 return 0;
70}
71
72int twrpDigest::write_md5digest(void) {
73 int i;
74 string md5string, md5file;
75 char hex[3];
76 md5file = md5fn + ".md5";
77
78 for (i = 0; i < 16; ++i) {
79 snprintf(hex, 3 ,"%02x", md5sum[i]);
80 md5string += hex;
81 }
82 md5string += " ";
83 md5string += basename((char*) md5fn.c_str());
84 md5string += + "\n";
85 TWFunc::write_file(md5file, md5string);
bigbiff bigbiff6b059542013-09-10 10:28:26 -040086 LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str());
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050087 return 0;
88}
89
90int twrpDigest::read_md5digest(void) {
91 string md5file = md5fn + ".md5";
bigbiff bigbiff65a4c732013-03-15 15:17:50 -040092 if (TWFunc::read_file(md5file, line) != 0)
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050093 return -1;
94 return 0;
95}
96
97int twrpDigest::verify_md5digest(void) {
98 string buf;
99 char hex[3];
100 int i;
101 string md5string;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500102 if (read_md5digest() != 0)
103 return -1;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400104 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500105 vector<string> tokens;
106 while (ss >> buf)
107 tokens.push_back(buf);
108 computeMD5();
109 for (i = 0; i < 16; ++i) {
110 snprintf(hex, 3, "%02x", md5sum[i]);
111 md5string += hex;
112 }
113 if (tokens.at(0) != md5string)
114 return -2;
115 return 0;
116}