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