Panda3D

pdecrypt.cxx
Go to the documentation of this file.
1 /**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file pdecrypt.cxx
10 * @author drose
11 * @date 2004-09-01
12 */
13
14 #include "filename.h"
15 #include "encrypt_string.h"
16 #include "pnotify.h"
17 #include "panda_getopt.h"
18 #include "preprocess_argv.h"
19
20 using std::cerr;
21 using std::endl;
22
23 std::string password;
24 bool got_password = false;
25
26 void
27 usage() {
28 cerr
29 << "\nUsage:\n"
30 << " pdecrypt file.pe [file2.pe file3.pe ...]\n"
31 << " pdecrypt -o dest_file file.pe\n\n"
32 << "\n"
33
34 << "This program reverses the operation of a previous pencrypt command. It\n"
35 << "decrypts the contents of the named source file(s) and removes the .pe\n"
36 << "extension. The encryption algorithm need not be specified; it can be\n"
37 << "determined by examining the header of each encrypted file. The password\n"
38 << "must match the encryption password exactly. If it does not, an error may\n"
39 << "or may not be reported; but the file will not be decrypted correctly even\n"
40 << "if no error is reported.\n\n"
41
42 << "Options:\n\n"
43
44 << " -p \"password\"\n"
45 << " Specifies the password to use for decryption. If this is not specified,\n"
46 << " the user is prompted from standard input.\n\n";
47 }
48
49 int
50 main(int argc, char **argv) {
51 extern char *optarg;
52 extern int optind;
53 const char *optstr = "o:p:h";
54
55 Filename dest_filename;
56 bool got_dest_filename = false;
57
58 preprocess_argv(argc, argv);
59 int flag = getopt(argc, argv, optstr);
60
61 while (flag != EOF) {
62 switch (flag) {
63 case 'o':
64 dest_filename = Filename::from_os_specific(optarg);
65 got_dest_filename = true;
66 break;
67
68 case 'p':
69 password = optarg;
70 got_password = true;
71 break;
72
73 case 'h':
74 case '?':
75 default:
76 usage();
77 return 1;
78 }
79 flag = getopt(argc, argv, optstr);
80 }
81
82 argc -= (optind-1);
83 argv += (optind-1);
84
85 if (argc < 2) {
86 usage();
87 return 1;
88 }
89
90 if (got_dest_filename && argc > 2) {
91 cerr << "Only one input file allowed in conjunction with -o.\n";
92 return 1;
93 }
94
95 bool all_ok = true;
96 for (int i = 1; i < argc; i++) {
97 Filename source_file = Filename::from_os_specific(argv[i]);
98 if (!got_dest_filename && source_file.get_extension() != "pe") {
99 cerr << source_file
100 << " doesn't end in .pe; can't derive filename of output file.\n";
101 all_ok = false;
102
103 } else {
104 Filename dest_file = dest_filename;
105 if (!got_dest_filename) {
106 dest_file = source_file.get_fullpath_wo_extension();
107 }
108
109 // Open source file
110 pifstream read_stream;
111 source_file.set_binary();
112 if (!source_file.open_read(read_stream)) {
113 cerr << "Couldn't read: " << source_file << endl;
114 all_ok = false;
115
116 } else {
117 // Open destination file
118 pofstream write_stream;
119 dest_file.set_binary();
120 if (!dest_file.open_write(write_stream, true)) {
121 cerr << "Failed to open: " << dest_file << endl;
122 all_ok = false;
123
124 } else {
125 // Prompt for password.
126 if (!got_password) {
127 cerr << "Enter password: ";
128 std::getline(std::cin, password);
129 got_password = true;
130 }
131
132 cerr << dest_file << "\n";
133 bool success = decrypt_stream(read_stream, write_stream, password);
134
135 read_stream.close();
136 write_stream.close();
137
138 if (!success) {
139 cerr << "Failure decrypting " << source_file << "\n";
140 all_ok = false;
141 dest_file.unlink();
142
143 } else {
144 if (!got_dest_filename) {
145 source_file.unlink();
146 }
147 }
148 }
149 }
150 }
151 }
152
153 if (all_ok) {
154 return 0;
155 } else {
156 return 1;
157 }
158 }
Filename
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
Filename::get_fullpath_wo_extension
std::string get_fullpath_wo_extension() const
Returns the full filename–directory and basename parts–except for the extension.
Definition: filename.I:377
Filename::open_read
bool open_read(std::ifstream &stream) const
Opens the indicated ifstream for reading the file, if possible.
Definition: filename.cxx:1863
Filename::unlink
bool unlink() const
Permanently deletes the file associated with the filename, if possible.
Definition: filename.cxx:2319
Filename::set_binary
void set_binary()
Indicates that the filename represents a binary file.
Definition: filename.I:414
Filename::from_os_specific
static Filename from_os_specific(const std::string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes,...
Definition: filename.cxx:328
Filename::open_write
bool open_write(std::ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
Definition: filename.cxx:1899
Filename::get_extension
std::string get_extension() const
Returns the file extension.
Definition: filename.I:400
encrypt_string.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
filename.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
panda_getopt.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pnotify.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
preprocess_argv
void preprocess_argv(int &argc, char **&argv)
Processes the argc, argv pair as needed before passing it to getopt().
Definition: preprocess_argv.cxx:30
preprocess_argv.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.