pdecrypt.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include "filename.h"
  14. #include "encrypt_string.h"
  15. #include "pnotify.h"
  16. #include "panda_getopt.h"
  17. #include "preprocess_argv.h"
  18. string password;
  19. bool got_password = false;
  20. void
  21. usage() {
  22. cerr
  23. << "\nUsage:\n"
  24. << " pdecrypt file.pe [file2.pe file3.pe ...]\n"
  25. << " pdecrypt -o dest_file file.pe\n\n"
  26. << "\n"
  27. << "This program reverses the operation of a previous pencrypt command. It\n"
  28. << "decrypts the contents of the named source file(s) and removes the .pe\n"
  29. << "extension. The encryption algorithm need not be specified; it can be\n"
  30. << "determined by examining the header of each encrypted file. The password\n"
  31. << "must match the encryption password exactly. If it does not, an error may\n"
  32. << "or may not be reported; but the file will not be decrypted correctly even\n"
  33. << "if no error is reported.\n\n"
  34. << "Options:\n\n"
  35. << " -p \"password\"\n"
  36. << " Specifies the password to use for decryption. If this is not specified,\n"
  37. << " the user is prompted from standard input.\n\n";
  38. }
  39. int
  40. main(int argc, char **argv) {
  41. extern char *optarg;
  42. extern int optind;
  43. const char *optstr = "o:p:h";
  44. Filename dest_filename;
  45. bool got_dest_filename = false;
  46. preprocess_argv(argc, argv);
  47. int flag = getopt(argc, argv, optstr);
  48. while (flag != EOF) {
  49. switch (flag) {
  50. case 'o':
  51. dest_filename = Filename::from_os_specific(optarg);
  52. got_dest_filename = true;
  53. break;
  54. case 'p':
  55. password = optarg;
  56. got_password = true;
  57. break;
  58. case 'h':
  59. case '?':
  60. default:
  61. usage();
  62. return 1;
  63. }
  64. flag = getopt(argc, argv, optstr);
  65. }
  66. argc -= (optind-1);
  67. argv += (optind-1);
  68. if (argc < 2) {
  69. usage();
  70. return 1;
  71. }
  72. if (got_dest_filename && argc > 2) {
  73. cerr << "Only one input file allowed in conjunction with -o.\n";
  74. return 1;
  75. }
  76. bool all_ok = true;
  77. for (int i = 1; i < argc; i++) {
  78. Filename source_file = Filename::from_os_specific(argv[i]);
  79. if (!got_dest_filename && source_file.get_extension() != "pe") {
  80. cerr << source_file
  81. << " doesn't end in .pe; can't derive filename of output file.\n";
  82. all_ok = false;
  83. } else {
  84. Filename dest_file = dest_filename;
  85. if (!got_dest_filename) {
  86. dest_file = source_file.get_fullpath_wo_extension();
  87. }
  88. // Open source file
  89. pifstream read_stream;
  90. source_file.set_binary();
  91. if (!source_file.open_read(read_stream)) {
  92. cerr << "Couldn't read: " << source_file << endl;
  93. all_ok = false;
  94. } else {
  95. // Open destination file
  96. pofstream write_stream;
  97. dest_file.set_binary();
  98. if (!dest_file.open_write(write_stream, true)) {
  99. cerr << "Failed to open: " << dest_file << endl;
  100. all_ok = false;
  101. } else {
  102. // Prompt for password.
  103. if (!got_password) {
  104. cerr << "Enter password: ";
  105. getline(cin, password);
  106. got_password = true;
  107. }
  108. cerr << dest_file << "\n";
  109. bool success = decrypt_stream(read_stream, write_stream, password);
  110. read_stream.close();
  111. write_stream.close();
  112. if (!success) {
  113. cerr << "Failure decrypting " << source_file << "\n";
  114. all_ok = false;
  115. dest_file.unlink();
  116. } else {
  117. if (!got_dest_filename) {
  118. source_file.unlink();
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. if (all_ok) {
  126. return 0;
  127. } else {
  128. return 1;
  129. }
  130. }