15 #include "pandabase.h" 19 #include "panda_getopt.h" 20 #include "preprocess_argv.h" 22 bool output_decimal =
false;
23 bool suppress_filename =
false;
24 pofstream binary_output;
31 "check_md5 [-q] [-d] [-b filename] [-i \"input string\"] [file1 file2 ...]\n" 39 "This program outputs the MD5 hash of one or more files (or of a string\n" 40 "passed on the command line with -i).\n\n" 42 "An MD5 hash is a 128-bit value. The output is presented as a 32-digit\n" 43 "hexadecimal string by default, but with -d, it is presented as four\n" 44 "big-endian unsigned 32-bit decimal integers. Normally the filename\n" 45 "of each file is printed along with the hash; -q suppresses this.\n\n" 47 "To write the 16 bytes (per input file) of the output directly to a\n" 48 "binary file, use -b with the name of the file to receive the output.\n";
52 output_hash(
const string &filename,
const HashVal &hash) {
53 if (!suppress_filename && !filename.empty()) {
54 cout << filename <<
" ";
70 main(
int argc,
char **argv) {
76 const char *optstr =
"i:db:qh";
78 bool got_input_string =
false;
82 preprocess_argv(argc, argv);
83 int flag = getopt(argc, argv, optstr);
88 got_input_string =
true;
89 input_string = optarg;
93 output_decimal =
true;
97 binary_output_filename = Filename::binary_filename(
string(optarg));
101 suppress_filename =
true;
111 flag = getopt(argc, argv, optstr);
117 if (argc < 2 && !got_input_string) {
122 if (!binary_output_filename.empty()) {
123 if (!binary_output_filename.
open_write(binary_output)) {
124 cerr <<
"Unable to open " << binary_output_filename <<
".\n";
129 if (got_input_string) {
131 hash.hash_string(input_string);
132 output_hash(
"", hash);
137 for (
int i = 1; i < argc; i++) {
140 if (!source_file.
exists()) {
141 cerr << source_file <<
" not found!\n";
145 if (!hash.hash_file(source_file)) {
146 cerr <<
"Unable to read " << source_file <<
"\n";
void output_binary(ostream &out) const
Outputs the HashVal as a binary stream of bytes in order.
string get_basename() const
Returns the basename part of the filename.
bool open_write(ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
Stores a 128-bit value that represents the hashed contents (typically MD5) of a file or buffer...
The name of a file, such as a texture file or an Egg file.
void output_hex(ostream &out) const
Outputs the HashVal as a 32-digit hexadecimal number.
void output_dec(ostream &out) const
Outputs the HashVal as four unsigned decimal integers.
bool exists() const
Returns true if the filename exists on the disk, false otherwise.
static Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).