Panda3D
|
00001 // Filename: check_md5.cxx 00002 // Created by: 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "pandabase.h" 00016 00017 #include "hashVal.h" 00018 #include "filename.h" 00019 00020 #ifndef HAVE_GETOPT 00021 #include "gnu_getopt.h" 00022 #else 00023 #include <getopt.h> 00024 #endif 00025 00026 bool output_decimal = false; 00027 bool suppress_filename = false; 00028 pofstream binary_output; 00029 00030 void 00031 usage() { 00032 cerr << 00033 "\n" 00034 "Usage:\n\n" 00035 "check_md5 [-q] [-d] [-b filename] [-i \"input string\"] [file1 file2 ...]\n" 00036 "check_md5 -h\n\n"; 00037 } 00038 00039 void 00040 help() { 00041 usage(); 00042 cerr << 00043 "This program outputs the MD5 hash of one or more files (or of a string\n" 00044 "passed on the command line with -i).\n\n" 00045 00046 "An MD5 hash is a 128-bit value. The output is presented as a 32-digit\n" 00047 "hexadecimal string by default, but with -d, it is presented as four\n" 00048 "big-endian unsigned 32-bit decimal integers. Normally the filename\n" 00049 "of each file is printed along with the hash; -q suppresses this.\n\n" 00050 00051 "To write the 16 bytes (per input file) of the output directly to a\n" 00052 "binary file, use -b with the name of the file to receive the output.\n"; 00053 } 00054 00055 void 00056 output_hash(const string &filename, const HashVal &hash) { 00057 if (!suppress_filename && !filename.empty()) { 00058 cout << filename << " "; 00059 } 00060 if (output_decimal) { 00061 hash.output_dec(cout); 00062 } else { 00063 hash.output_hex(cout); 00064 } 00065 cout << "\n"; 00066 00067 // Also output to the binary_output file if it is open. No sweat if 00068 // it's not. 00069 hash.output_binary(binary_output); 00070 } 00071 00072 00073 int 00074 main(int argc, char *argv[]) { 00075 extern char *optarg; 00076 extern int optind; 00077 const char *optstr = "i:db:qh"; 00078 00079 bool got_input_string = false; 00080 string input_string; 00081 Filename binary_output_filename; 00082 00083 int flag = getopt(argc, argv, optstr); 00084 00085 while (flag != EOF) { 00086 switch (flag) { 00087 case 'i': 00088 got_input_string = true; 00089 input_string = optarg; 00090 break; 00091 00092 case 'd': 00093 output_decimal = true; 00094 break; 00095 00096 case 'b': 00097 binary_output_filename = Filename::binary_filename(optarg); 00098 break; 00099 00100 case 'q': 00101 suppress_filename = true; 00102 break; 00103 00104 case 'h': 00105 help(); 00106 exit(1); 00107 00108 default: 00109 exit(1); 00110 } 00111 flag = getopt(argc, argv, optstr); 00112 } 00113 00114 argc -= (optind-1); 00115 argv += (optind-1); 00116 00117 if (argc < 2 && !got_input_string) { 00118 usage(); 00119 exit(1); 00120 } 00121 00122 if (!binary_output_filename.empty()) { 00123 if (!binary_output_filename.open_write(binary_output)) { 00124 cerr << "Unable to open " << binary_output_filename << ".\n"; 00125 exit(1); 00126 } 00127 } 00128 00129 if (got_input_string) { 00130 HashVal hash; 00131 hash.hash_string(input_string); 00132 output_hash("", hash); 00133 } 00134 00135 bool okflag = true; 00136 00137 for (int i = 1; i < argc; i++) { 00138 Filename source_file = Filename::from_os_specific(argv[i]); 00139 00140 if (!source_file.exists()) { 00141 cerr << source_file << " not found!\n"; 00142 okflag = false; 00143 } else { 00144 HashVal hash; 00145 if (!hash.hash_file(source_file)) { 00146 cerr << "Unable to read " << source_file << "\n"; 00147 okflag = false; 00148 } else { 00149 output_hash(source_file.get_basename(), hash); 00150 } 00151 } 00152 } 00153 00154 if (!okflag) { 00155 exit(1); 00156 } 00157 00158 return 0; 00159 }