Panda3D
|
00001 // Filename: pdecrypt.cxx 00002 // Created by: drose (01Sep04) 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 "filename.h" 00016 #include "encrypt_string.h" 00017 #include "pnotify.h" 00018 00019 #ifndef HAVE_GETOPT 00020 #include "gnu_getopt.h" 00021 #else 00022 #ifdef PHAVE_GETOPT_H 00023 #include <getopt.h> 00024 #endif 00025 #endif 00026 00027 string password; 00028 bool got_password = false; 00029 00030 void 00031 usage() { 00032 cerr 00033 << "\nUsage:\n" 00034 << " pdecrypt file.pe [file2.pe file3.pe ...]\n" 00035 << " pdecrypt -o dest_file file.pe\n\n" 00036 << "\n" 00037 00038 << "This program reverses the operation of a previous pencrypt command. It\n" 00039 << "decrypts the contents of the named source file(s) and removes the .pe\n" 00040 << "extension. The encryption algorithm need not be specified; it can be\n" 00041 << "determined by examining the header of each encrypted file. The password\n" 00042 << "must match the encryption password exactly. If it does not, an error may\n" 00043 << "or may not be reported; but the file will not be decrypted correctly even\n" 00044 << "if no error is reported.\n\n" 00045 00046 << "Options:\n\n" 00047 00048 << " -p \"password\"\n" 00049 << " Specifies the password to use for decryption. If this is not specified,\n" 00050 << " the user is prompted from standard input.\n\n"; 00051 } 00052 00053 int 00054 main(int argc, char *argv[]) { 00055 extern char *optarg; 00056 extern int optind; 00057 const char *optstr = "o:p:h"; 00058 00059 Filename dest_filename; 00060 bool got_dest_filename = false; 00061 00062 int flag = getopt(argc, argv, optstr); 00063 00064 while (flag != EOF) { 00065 switch (flag) { 00066 case 'o': 00067 dest_filename = Filename::from_os_specific(optarg); 00068 got_dest_filename = true; 00069 break; 00070 00071 case 'p': 00072 password = optarg; 00073 got_password = true; 00074 break; 00075 00076 case 'h': 00077 case '?': 00078 default: 00079 usage(); 00080 return 1; 00081 } 00082 flag = getopt(argc, argv, optstr); 00083 } 00084 00085 argc -= (optind-1); 00086 argv += (optind-1); 00087 00088 if (argc < 2) { 00089 usage(); 00090 return 1; 00091 } 00092 00093 if (got_dest_filename && argc > 2) { 00094 cerr << "Only one input file allowed in conjunction with -o.\n"; 00095 return 1; 00096 } 00097 00098 bool all_ok = true; 00099 for (int i = 1; i < argc; i++) { 00100 Filename source_file = Filename::from_os_specific(argv[i]); 00101 if (!got_dest_filename && source_file.get_extension() != "pe") { 00102 cerr << source_file 00103 << " doesn't end in .pe; can't derive filename of output file.\n"; 00104 all_ok = false; 00105 00106 } else { 00107 Filename dest_file = dest_filename; 00108 if (!got_dest_filename) { 00109 dest_file = source_file.get_fullpath_wo_extension(); 00110 } 00111 00112 // Open source file 00113 pifstream read_stream; 00114 source_file.set_binary(); 00115 if (!source_file.open_read(read_stream)) { 00116 cerr << "Couldn't read: " << source_file << endl; 00117 all_ok = false; 00118 00119 } else { 00120 // Open destination file 00121 pofstream write_stream; 00122 dest_file.set_binary(); 00123 if (!dest_file.open_write(write_stream, true)) { 00124 cerr << "Failed to open: " << dest_file << endl; 00125 all_ok = false; 00126 00127 } else { 00128 // Prompt for password. 00129 if (!got_password) { 00130 cerr << "Enter password: "; 00131 getline(cin, password); 00132 got_password = true; 00133 } 00134 00135 cerr << dest_file << "\n"; 00136 bool success = decrypt_stream(read_stream, write_stream, password); 00137 00138 read_stream.close(); 00139 write_stream.close(); 00140 00141 if (!success) { 00142 cerr << "Failure decrypting " << source_file << "\n"; 00143 all_ok = false; 00144 dest_file.unlink(); 00145 00146 } else { 00147 if (!got_dest_filename) { 00148 source_file.unlink(); 00149 } 00150 } 00151 } 00152 } 00153 } 00154 } 00155 00156 if (all_ok) { 00157 return 0; 00158 } else { 00159 return 1; 00160 } 00161 }