bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1 | /* |
| 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 Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 19 | extern "C" |
| 20 | { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 21 | #include "digest/md5.h" |
| 22 | #include "libcrecovery/common.h" |
| 23 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 24 | |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 25 | #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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 41 | #include "twcommon.h" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 42 | #include "data.hpp" |
| 43 | #include "variables.h" |
| 44 | #include "twrp-functions.hpp" |
| 45 | #include "twrpDigest.hpp" |
bigbiff bigbiff | 6b05954 | 2013-09-10 10:28:26 -0400 | [diff] [blame] | 46 | #include "twcommon.h" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 47 | |
| 48 | using namespace std; |
| 49 | |
| 50 | void twrpDigest::setfn(string fn) { |
| 51 | md5fn = fn; |
| 52 | } |
| 53 | |
| 54 | int 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 67 | fclose(file); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 68 | MD5Final(md5sum ,&md5c); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int 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 bigbiff | 6b05954 | 2013-09-10 10:28:26 -0400 | [diff] [blame] | 86 | LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str()); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int twrpDigest::read_md5digest(void) { |
| 91 | string md5file = md5fn + ".md5"; |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 92 | if (TWFunc::read_file(md5file, line) != 0) |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 93 | return -1; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int twrpDigest::verify_md5digest(void) { |
| 98 | string buf; |
| 99 | char hex[3]; |
| 100 | int i; |
| 101 | string md5string; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 102 | if (read_md5digest() != 0) |
| 103 | return -1; |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 104 | stringstream ss(line); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 105 | 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 | } |