Panda3D
check_md5.cxx
Go to the documentation of this file.
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 check_md5.cxx
10  */
11 
12 #include "pandabase.h"
13 #include "hashVal.h"
14 #include "filename.h"
15 #include "panda_getopt.h"
16 #include "preprocess_argv.h"
17 
18 using std::cerr;
19 using std::cout;
20 
21 bool output_decimal = false;
22 bool suppress_filename = false;
23 pofstream binary_output;
24 
25 void
26 usage() {
27  cerr <<
28  "\n"
29  "Usage:\n\n"
30  "check_md5 [-q] [-d] [-b filename] [-i \"input string\"] [file1 file2 ...]\n"
31  "check_md5 -h\n\n";
32 }
33 
34 void
35 help() {
36  usage();
37  cerr <<
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"
40 
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"
45 
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";
48 }
49 
50 void
51 output_hash(const std::string &filename, const HashVal &hash) {
52  if (!suppress_filename && !filename.empty()) {
53  cout << filename << " ";
54  }
55  if (output_decimal) {
56  hash.output_dec(cout);
57  } else {
58  hash.output_hex(cout);
59  }
60  cout << "\n";
61 
62  // Also output to the binary_output file if it is open. No sweat if it's
63  // not.
64  hash.output_binary(binary_output);
65 }
66 
67 
68 int
69 main(int argc, char **argv) {
70  extern char *optarg;
71  extern int optind;
72  const char *optstr = "i:db:qh";
73 
74  bool got_input_string = false;
75  std::string input_string;
76  Filename binary_output_filename;
77 
78  preprocess_argv(argc, argv);
79  int flag = getopt(argc, argv, optstr);
80 
81  while (flag != EOF) {
82  switch (flag) {
83  case 'i':
84  got_input_string = true;
85  input_string = optarg;
86  break;
87 
88  case 'd':
89  output_decimal = true;
90  break;
91 
92  case 'b':
93  binary_output_filename = Filename::binary_filename(std::string(optarg));
94  break;
95 
96  case 'q':
97  suppress_filename = true;
98  break;
99 
100  case 'h':
101  help();
102  exit(1);
103 
104  default:
105  exit(1);
106  }
107  flag = getopt(argc, argv, optstr);
108  }
109 
110  argc -= (optind-1);
111  argv += (optind-1);
112 
113  if (argc < 2 && !got_input_string) {
114  usage();
115  exit(1);
116  }
117 
118  if (!binary_output_filename.empty()) {
119  if (!binary_output_filename.open_write(binary_output)) {
120  cerr << "Unable to open " << binary_output_filename << ".\n";
121  exit(1);
122  }
123  }
124 
125  if (got_input_string) {
126  HashVal hash;
127  hash.hash_string(input_string);
128  output_hash("", hash);
129  }
130 
131  bool okflag = true;
132 
133  for (int i = 1; i < argc; i++) {
134  Filename source_file = Filename::from_os_specific(argv[i]);
135 
136  if (!source_file.exists()) {
137  cerr << source_file << " not found!\n";
138  okflag = false;
139  } else {
140  HashVal hash;
141  if (!hash.hash_file(source_file)) {
142  cerr << "Unable to read " << source_file << "\n";
143  okflag = false;
144  } else {
145  output_hash(source_file.get_basename(), hash);
146  }
147  }
148  }
149 
150  if (!okflag) {
151  exit(1);
152  }
153 
154  return 0;
155 }
bool open_write(std::ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
Definition: filename.cxx:1899
void output_dec(std::ostream &out) const
Outputs the HashVal as four unsigned decimal integers.
Definition: hashVal.I:103
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.
Definition: hashVal.h:31
void output_hex(std::ostream &out) const
Outputs the HashVal as a 32-digit hexadecimal number.
Definition: hashVal.cxx:34
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
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.
Definition: filename.I:367
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.
Definition: hashVal.cxx:83
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool exists() const
Returns true if the filename exists on the disk, false otherwise.
Definition: filename.cxx:1267
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,...
Definition: filename.cxx:328