21 bool output_decimal =
false;
22 bool suppress_filename =
false;
23 pofstream binary_output;
30 "check_md5 [-q] [-d] [-b filename] [-i \"input string\"] [file1 file2 ...]\n" 38 "This program outputs the MD5 hash of one or more files (or of a string\n" 39 "passed on the command line with -i).\n\n" 41 "An MD5 hash is a 128-bit value. The output is presented as a 32-digit\n" 42 "hexadecimal string by default, but with -d, it is presented as four\n" 43 "big-endian unsigned 32-bit decimal integers. Normally the filename\n" 44 "of each file is printed along with the hash; -q suppresses this.\n\n" 46 "To write the 16 bytes (per input file) of the output directly to a\n" 47 "binary file, use -b with the name of the file to receive the output.\n";
51 output_hash(
const std::string &filename,
const HashVal &hash) {
52 if (!suppress_filename && !filename.empty()) {
53 cout << filename <<
" ";
69 main(
int argc,
char **argv) {
72 const char *optstr =
"i:db:qh";
74 bool got_input_string =
false;
75 std::string input_string;
79 int flag = getopt(argc, argv, optstr);
84 got_input_string =
true;
85 input_string = optarg;
89 output_decimal =
true;
93 binary_output_filename = Filename::binary_filename(std::string(optarg));
97 suppress_filename =
true;
107 flag = getopt(argc, argv, optstr);
113 if (argc < 2 && !got_input_string) {
118 if (!binary_output_filename.empty()) {
119 if (!binary_output_filename.
open_write(binary_output)) {
120 cerr <<
"Unable to open " << binary_output_filename <<
".\n";
125 if (got_input_string) {
127 hash.hash_string(input_string);
128 output_hash(
"", hash);
133 for (
int i = 1; i < argc; i++) {
136 if (!source_file.
exists()) {
137 cerr << source_file <<
" not found!\n";
141 if (!hash.hash_file(source_file)) {
142 cerr <<
"Unable to read " << source_file <<
"\n";
bool open_write(std::ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
void output_dec(std::ostream &out) const
Outputs the HashVal as four unsigned decimal integers.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Stores a 128-bit value that represents the hashed contents (typically MD5) of a file or buffer.
void output_hex(std::ostream &out) const
Outputs the HashVal as a 32-digit hexadecimal number.
The name of a file, such as a texture file or an Egg file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void preprocess_argv(int &argc, char **&argv)
Processes the argc, argv pair as needed before passing it to getopt().
std::string get_basename() const
Returns the basename part of the filename.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void output_binary(std::ostream &out) const
Outputs the HashVal as a binary stream of bytes in order.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool exists() const
Returns true if the filename exists on the disk, false otherwise.
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,...