This commit is contained in:
David Rose 2003-10-15 18:32:18 +00:00
parent 6552771a5f
commit db55a27dfa

View File

@ -27,12 +27,16 @@
#include <getopt.h> #include <getopt.h>
#endif #endif
bool output_decimal = false;
bool suppress_filename = false;
ofstream binary_output;
void void
usage() { usage() {
cerr << cerr <<
"\n" "\n"
"Usage:\n\n" "Usage:\n\n"
"check_md5 [-q] [-d] [-i \"input string\"] [file1 file2 ...]\n" "check_md5 [-q] [-d] [-b filename] [-i \"input string\"] [file1 file2 ...]\n"
"check_md5 -h\n\n"; "check_md5 -h\n\n";
} }
@ -46,12 +50,14 @@ help() {
"An MD5 hash is a 128-bit value. The output is presented as a 32-digit\n" "An MD5 hash is a 128-bit value. The output is presented as a 32-digit\n"
"hexadecimal string by default, but with -d, it is presented as four\n" "hexadecimal string by default, but with -d, it is presented as four\n"
"big-endian unsigned 32-bit decimal integers. Normally the filename\n" "big-endian unsigned 32-bit decimal integers. Normally the filename\n"
"of each file is printed along with the hash; -q suppresses this.\n\n"; "of each file is printed along with the hash; -q suppresses this.\n\n"
"To write the 16 bytes (per input file) of the output directly to a\n"
"binary file, use -b with the name of the file to receive the output.\n";
} }
void void
output_hash(const string &filename, const HashVal &hash, output_hash(const string &filename, const HashVal &hash) {
bool output_decimal, bool suppress_filename) {
if (!suppress_filename && !filename.empty()) { if (!suppress_filename && !filename.empty()) {
cout << filename << " "; cout << filename << " ";
} }
@ -61,6 +67,10 @@ output_hash(const string &filename, const HashVal &hash,
hash.output_hex(cout); hash.output_hex(cout);
} }
cout << "\n"; cout << "\n";
// Also output to the binary_output file if it is open. No sweat if
// it's not.
hash.output_binary(binary_output);
} }
@ -68,12 +78,11 @@ int
main(int argc, char *argv[]) { main(int argc, char *argv[]) {
extern char *optarg; extern char *optarg;
extern int optind; extern int optind;
const char *optstr = "i:dqh"; const char *optstr = "i:db:qh";
bool got_input_string = false; bool got_input_string = false;
string input_string; string input_string;
bool output_decimal = false; Filename binary_output_filename;
bool suppress_filename = false;
int flag = getopt(argc, argv, optstr); int flag = getopt(argc, argv, optstr);
@ -88,6 +97,10 @@ main(int argc, char *argv[]) {
output_decimal = true; output_decimal = true;
break; break;
case 'b':
binary_output_filename = Filename::binary_filename(optarg);
break;
case 'q': case 'q':
suppress_filename = true; suppress_filename = true;
break; break;
@ -110,10 +123,17 @@ main(int argc, char *argv[]) {
exit(1); exit(1);
} }
if (!binary_output_filename.empty()) {
if (!binary_output_filename.open_write(binary_output)) {
cerr << "Unable to open " << binary_output_filename << ".\n";
exit(1);
}
}
if (got_input_string) { if (got_input_string) {
HashVal hash; HashVal hash;
hash.hash_string(input_string); hash.hash_string(input_string);
output_hash("", hash, output_decimal, true); output_hash("", hash);
} }
bool okflag = true; bool okflag = true;
@ -130,8 +150,7 @@ main(int argc, char *argv[]) {
cerr << "Unable to read " << source_file << "\n"; cerr << "Unable to read " << source_file << "\n";
okflag = false; okflag = false;
} else { } else {
output_hash(source_file.get_basename(), hash, output_hash(source_file.get_basename(), hash);
output_decimal, suppress_filename);
} }
} }
} }