blob: add498c6c3d5828159dcb9509d55d5422bb25202 [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
19extern "C" {
20 #include "digest/md5.h"
21 #include "libcrecovery/common.h"
22}
23#include <vector>
24#include <string>
25#include <sstream>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/wait.h>
29#include <string.h>
30#include <libgen.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <fstream>
34#include <iostream>
35#include <string>
36#include <sstream>
37#include <dirent.h>
38#include <sys/mman.h>
39#include "common.h"
40#include "data.hpp"
41#include "variables.h"
42#include "twrp-functions.hpp"
43#include "twrpDigest.hpp"
44
45using namespace std;
46
47void twrpDigest::setfn(string fn) {
48 md5fn = fn;
49}
50
51int twrpDigest::computeMD5(void) {
52 string line;
53 struct MD5Context md5c;
54 FILE *file;
55 int len;
56 unsigned char buf[1024];
57 MD5Init(&md5c);
58 file = fopen(md5fn.c_str(), "rb");
59 if (file == NULL)
60 return -1;
61 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
62 MD5Update(&md5c, buf, len);
63 }
64 MD5Final(md5sum ,&md5c);
65 return 0;
66}
67
68int twrpDigest::write_md5digest(void) {
69 int i;
70 string md5string, md5file;
71 char hex[3];
72 md5file = md5fn + ".md5";
73
74 for (i = 0; i < 16; ++i) {
75 snprintf(hex, 3 ,"%02x", md5sum[i]);
76 md5string += hex;
77 }
78 md5string += " ";
79 md5string += basename((char*) md5fn.c_str());
80 md5string += + "\n";
81 TWFunc::write_file(md5file, md5string);
82 return 0;
83}
84
85int twrpDigest::read_md5digest(void) {
86 string md5file = md5fn + ".md5";
bigbiff bigbiff65a4c732013-03-15 15:17:50 -040087 if (TWFunc::read_file(md5file, line) != 0)
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050088 return -1;
89 return 0;
90}
91
92int twrpDigest::verify_md5digest(void) {
93 string buf;
94 char hex[3];
95 int i;
96 string md5string;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050097 if (read_md5digest() != 0)
98 return -1;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -040099 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500100 vector<string> tokens;
101 while (ss >> buf)
102 tokens.push_back(buf);
103 computeMD5();
104 for (i = 0; i < 16; ++i) {
105 snprintf(hex, 3, "%02x", md5sum[i]);
106 md5string += hex;
107 }
108 if (tokens.at(0) != md5string)
109 return -2;
110 return 0;
111}