text options to encrypt_file and pencrypt

This commit is contained in:
David Rose 2009-09-02 06:07:28 +00:00
parent b027447c0f
commit f360650ccc
2 changed files with 42 additions and 9 deletions

View File

@ -62,6 +62,10 @@ usage() {
<< " are more secure. If this is not specified, the user is prompted from\n"
<< " standard input.\n\n"
<< " -t Read the file as a text file. This will automatically convert\n"
<< " universal end-of-line characters into a newline character, ascii\n"
<< " 10.\n\n"
<< " -a \"algorithm\"\n"
<< " Specifies the particular encryption algorithm to use. The complete\n"
<< " set of available algorithms is defined by the current version of\n"
@ -88,10 +92,11 @@ int
main(int argc, char *argv[]) {
extern char *optarg;
extern int optind;
const char *optstr = "o:p:a:k:i:h";
const char *optstr = "o:p:ta:k:i:h";
Filename dest_filename;
bool got_dest_filename = false;
bool text_file = false;
int flag = getopt(argc, argv, optstr);
@ -102,6 +107,10 @@ main(int argc, char *argv[]) {
got_dest_filename = true;
break;
case 't':
text_file = true;
break;
case 'p':
password = optarg;
got_password = true;
@ -157,7 +166,11 @@ main(int argc, char *argv[]) {
// Open source file
pifstream read_stream;
source_file.set_binary();
if (text_file) {
source_file.set_text();
} else {
source_file.set_binary();
}
if (!source_file.open_read(read_stream)) {
cerr << "Couldn't read: " << source_file << endl;
all_ok = false;

View File

@ -89,19 +89,39 @@ decrypt_string(const string &source, const string &password) {
EXPCL_PANDAEXPRESS bool
encrypt_file(const Filename &source, const Filename &dest, const string &password,
const string &algorithm, int key_length, int iteration_count) {
Filename source_filename = Filename::binary_filename(source);
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
istream *source_stream = vfs->open_read_file(source_filename, true);
if (source_stream == NULL) {
express_cat.info() << "Couldn't open file " << source_filename << "\n";
return false;
Filename source_filename = source;
if (!source_filename.is_binary() && !source_filename.is_text()) {
// The default is binary, if not specified otherwise.
source_filename.set_binary();
}
Filename dest_filename = Filename::binary_filename(dest);
pofstream dest_stream;
if (!dest_filename.open_write(dest_stream)) {
express_cat.info() << "Couldn't open file " << dest_filename << "\n";
vfs->close_read_file(source_stream);
return false;
}
// Try to open the file from disk first, instead of using the vfs,
// so we can get the newline conversion with a text file. This is a
// little weird if you have a vfs file shadowing a disk file, but
// whatever.
if (source_filename.is_text()) {
pifstream source_stream;
if (source_filename.open_read(source_stream)) {
bool result = encrypt_stream(source_stream, dest_stream, password,
algorithm, key_length, iteration_count);
return result;
}
}
// OK, couldn't read the disk file, or it wasn't set in text mode.
// Read the file from the vfs, and sorry--no text conversion for
// you.
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
istream *source_stream = vfs->open_read_file(source_filename, true);
if (source_stream == NULL) {
express_cat.info() << "Couldn't open file " << source_filename << "\n";
return false;
}