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> |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 36 | #include <sys/mman.h> |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 37 | #include "twcommon.h" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 38 | #include "data.hpp" |
| 39 | #include "variables.h" |
| 40 | #include "twrp-functions.hpp" |
| 41 | #include "twrpDigest.hpp" |
| 42 | |
| 43 | using namespace std; |
| 44 | |
| 45 | void twrpDigest::setfn(string fn) { |
| 46 | md5fn = fn; |
| 47 | } |
| 48 | |
| 49 | int twrpDigest::computeMD5(void) { |
| 50 | string line; |
| 51 | struct MD5Context md5c; |
| 52 | FILE *file; |
| 53 | int len; |
| 54 | unsigned char buf[1024]; |
| 55 | MD5Init(&md5c); |
| 56 | file = fopen(md5fn.c_str(), "rb"); |
| 57 | if (file == NULL) |
| 58 | return -1; |
| 59 | while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
| 60 | MD5Update(&md5c, buf, len); |
| 61 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 62 | fclose(file); |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 63 | MD5Final(md5sum, &md5c); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int twrpDigest::write_md5digest(void) { |
| 68 | int i; |
| 69 | string md5string, md5file; |
| 70 | char hex[3]; |
| 71 | md5file = md5fn + ".md5"; |
| 72 | |
| 73 | for (i = 0; i < 16; ++i) { |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 74 | snprintf(hex, 3, "%02x", md5sum[i]); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 75 | md5string += hex; |
| 76 | } |
| 77 | md5string += " "; |
| 78 | md5string += basename((char*) md5fn.c_str()); |
| 79 | md5string += + "\n"; |
| 80 | TWFunc::write_file(md5file, md5string); |
bigbiff bigbiff | 6b05954 | 2013-09-10 10:28:26 -0400 | [diff] [blame] | 81 | LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str()); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | int twrpDigest::read_md5digest(void) { |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 86 | int i = 0; |
| 87 | bool foundMd5File = false; |
| 88 | string md5file = ""; |
| 89 | vector<string> md5ext; |
| 90 | md5ext.push_back(".md5"); |
| 91 | md5ext.push_back(".md5sum"); |
| 92 | |
| 93 | while (i < md5ext.size()) { |
| 94 | md5file = md5fn + md5ext[i]; |
| 95 | if (TWFunc::Path_Exists(md5file)) { |
| 96 | foundMd5File = true; |
| 97 | break; |
| 98 | } |
| 99 | i++; |
| 100 | } |
| 101 | |
| 102 | if (!foundMd5File) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 103 | return -1; |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 104 | } else if (TWFunc::read_file(md5file, line) != 0) { |
| 105 | LOGERR("Could not read %s\n", md5file.c_str()); |
| 106 | } |
| 107 | |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int twrpDigest::verify_md5digest(void) { |
| 112 | string buf; |
| 113 | char hex[3]; |
| 114 | int i; |
| 115 | string md5string; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 116 | if (read_md5digest() != 0) |
| 117 | return -1; |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 118 | stringstream ss(line); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 119 | vector<string> tokens; |
| 120 | while (ss >> buf) |
| 121 | tokens.push_back(buf); |
| 122 | computeMD5(); |
| 123 | for (i = 0; i < 16; ++i) { |
| 124 | snprintf(hex, 3, "%02x", md5sum[i]); |
| 125 | md5string += hex; |
| 126 | } |
| 127 | if (tokens.at(0) != md5string) |
| 128 | return -2; |
| 129 | return 0; |
| 130 | } |