blob: 663d08566a8818d355a786aaef985a6b23233641 [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>
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "twcommon.h"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#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 }
Dees_Troy2673cec2013-04-02 20:22:16 +000064 fclose(file);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050065 MD5Final(md5sum ,&md5c);
66 return 0;
67}
68
69int twrpDigest::write_md5digest(void) {
70 int i;
71 string md5string, md5file;
72 char hex[3];
73 md5file = md5fn + ".md5";
74
75 for (i = 0; i < 16; ++i) {
76 snprintf(hex, 3 ,"%02x", md5sum[i]);
77 md5string += hex;
78 }
79 md5string += " ";
80 md5string += basename((char*) md5fn.c_str());
81 md5string += + "\n";
82 TWFunc::write_file(md5file, md5string);
83 return 0;
84}
85
86int twrpDigest::read_md5digest(void) {
87 string md5file = md5fn + ".md5";
bigbiff bigbiff65a4c732013-03-15 15:17:50 -040088 if (TWFunc::read_file(md5file, line) != 0)
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050089 return -1;
90 return 0;
91}
92
93int twrpDigest::verify_md5digest(void) {
94 string buf;
95 char hex[3];
96 int i;
97 string md5string;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050098 if (read_md5digest() != 0)
99 return -1;
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400100 stringstream ss(line);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500101 vector<string> tokens;
102 while (ss >> buf)
103 tokens.push_back(buf);
104 computeMD5();
105 for (i = 0; i < 16; ++i) {
106 snprintf(hex, 3, "%02x", md5sum[i]);
107 md5string += hex;
108 }
109 if (tokens.at(0) != md5string)
110 return -2;
111 return 0;
112}