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" |
| 46 | |
| 47 | using namespace std; |
| 48 | |
| 49 | void twrpDigest::setfn(string fn) { |
| 50 | md5fn = fn; |
| 51 | } |
| 52 | |
| 53 | int 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 66 | fclose(file); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 67 | MD5Final(md5sum ,&md5c); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int 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 | |
| 88 | int twrpDigest::read_md5digest(void) { |
| 89 | string md5file = md5fn + ".md5"; |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 90 | if (TWFunc::read_file(md5file, line) != 0) |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 91 | return -1; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | int twrpDigest::verify_md5digest(void) { |
| 96 | string buf; |
| 97 | char hex[3]; |
| 98 | int i; |
| 99 | string md5string; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 100 | if (read_md5digest() != 0) |
| 101 | return -1; |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 102 | stringstream ss(line); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 103 | 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 | } |