blob: ce3d87cca5b295615a411a4b32057fa63958c185 [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"
46
47using namespace std;
48
49void twrpDigest::setfn(string fn) {
50 md5fn = fn;
51}
52
53int twrpDigest::computeMD5(void) {
54 string line;
55 struct MD5Context md5c;
56 FILE *file;
57 int len;
58 unsigned char buf[1024];
59 MD5Init(&md5c);
60 file = fopen(md5fn.c_str(), "rb");
61 if (file == NULL)
62 return -1;
63 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
64 MD5Update(&md5c, buf, len);
65 }
Dees_Troy2673cec2013-04-02 20:22:16 +000066 fclose(file);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050067 MD5Final(md5sum ,&md5c);
68 return 0;
69}
70
71int twrpDigest::write_md5digest(void) {
72 int i;
73 string md5string, md5file;
74 char hex[3];
75 md5file = md5fn + ".md5";
76
77 for (i = 0; i < 16; ++i) {
78 snprintf(hex, 3 ,"%02x", md5sum[i]);
79 md5string += hex;
80 }
81 md5string += " ";
82 md5string += basename((char*) md5fn.c_str());
83 md5string += + "\n";
84 TWFunc::write_file(md5file, md5string);
85 return 0;
86}
87
88int twrpDigest::read_md5digest(void) {
89 string md5file = md5fn + ".md5";
bigbiff bigbiff65a4c732013-03-15 15:17:50 -040090 if (TWFunc::read_file(md5file, line) != 0)
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050091 return -1;
92 return 0;
93}
94
95int twrpDigest::verify_md5digest(void) {
96 string buf;
97 char hex[3];
98 int i;
99 string md5string;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500100 if (read_md5digest() != 0)
101 return -1;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400102 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500103 vector<string> tokens;
104 while (ss >> buf)
105 tokens.push_back(buf);
106 computeMD5();
107 for (i = 0; i < 16; ++i) {
108 snprintf(hex, 3, "%02x", md5sum[i]);
109 md5string += hex;
110 }
111 if (tokens.at(0) != md5string)
112 return -2;
113 return 0;
114}