md5sum.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * md5sum.c - Generate/check MD5 Message Digests
  3. *
  4. * Compile and link with md5.c. If you don't have getopt() in your library
  5. * also include getopt.c. For MSDOS you can also link with the wildcard
  6. * initialization function (wildargs.obj for Turbo C and setargv.obj for MSC)
  7. * so that you can use wildcards on the commandline.
  8. *
  9. * Written March 1993 by Branko Lankester
  10. * Modified June 1993 by Colin Plumb for altered md5.c.
  11. * Modified Feburary 1995 by Ian Jackson for use with Colin Plumb's md5.c.
  12. * Hacked (modified is too nice a word) January 1997 by Galen Hazelwood
  13. * to support GNU gettext.
  14. * This file is in the public domain.
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <getopt.h>
  19. #include "config.h"
  20. #include "md5.h"
  21. /* Take care of NLS matters. */
  22. #if HAVE_LOCALE_H
  23. # include <locale.h>
  24. #endif
  25. #if !HAVE_SETLOCALE
  26. # define setlocale(Category, Locale) /* empty */
  27. #endif
  28. #if ENABLE_NLS
  29. # include <libintl.h>
  30. # define _(Text) gettext (Text)
  31. #else
  32. # undef bindtextdomain
  33. # define bindtextdomain(Domain, Directory) /* empty */
  34. # undef textdomain
  35. # define textdomain(Domain) /* empty */
  36. # define _(Text) Text
  37. #endif
  38. #ifdef UNIX
  39. #define FOPRTXT "r"
  40. #define FOPRBIN "r"
  41. #else
  42. #ifdef VMS
  43. #define FOPRTXT "r","ctx=stm"
  44. #define FOPRBIN "rb","ctx=stm"
  45. #else
  46. #define FOPRTXT "r"
  47. #define FOPRBIN "rb"
  48. #endif
  49. #endif
  50. extern char *optarg;
  51. extern int optind;
  52. void usage(void);
  53. void print_digest(unsigned char *p);
  54. int mdfile(FILE *fp, unsigned char *digest);
  55. int do_check(FILE *chkf);
  56. int hex_digit(int c);
  57. int get_md5_line(FILE *fp, unsigned char *digest, char *file);
  58. char *progname;
  59. int verbose = 0;
  60. int bin_mode = 0;
  61. int
  62. main(int argc, char **argv)
  63. {
  64. int opt, rc = 0;
  65. int check = 0;
  66. FILE *fp = NULL;
  67. unsigned char digest[16];
  68. setlocale(LC_ALL, "");
  69. bindtextdomain(PACKAGE, LOCALEDIR);
  70. textdomain(PACKAGE);
  71. progname = *argv;
  72. while ((opt = getopt(argc, argv, "cbvp:h")) != EOF) {
  73. switch (opt) {
  74. case 'c': check = 1; break;
  75. case 'v': verbose = 1; break;
  76. case 'b': bin_mode = 1; break;
  77. default: usage();
  78. }
  79. }
  80. argc -= optind;
  81. argv += optind;
  82. if (check) {
  83. switch (argc) {
  84. case 0: fp = stdin; break;
  85. case 1: if ((fp = fopen(*argv, FOPRTXT)) == NULL) {
  86. perror(*argv);
  87. exit(2);
  88. }
  89. break;
  90. default: usage();
  91. }
  92. exit(do_check(fp));
  93. }
  94. if (argc == 0) {
  95. if (mdfile(stdin, digest)) {
  96. fprintf(stderr, _("%s: read error on stdin\n"), progname);
  97. exit(2);
  98. }
  99. print_digest(digest);
  100. printf("\n");
  101. exit(0);
  102. }
  103. for ( ; argc > 0; --argc, ++argv) {
  104. if (bin_mode)
  105. fp = fopen(*argv, FOPRBIN);
  106. else
  107. fp = fopen(*argv, FOPRTXT);
  108. if (fp == NULL) {
  109. perror(*argv);
  110. rc = 2;
  111. continue;
  112. }
  113. if (mdfile(fp, digest)) {
  114. fprintf(stderr, _("%s: error reading %s\n"), progname, *argv);
  115. rc = 2;
  116. } else {
  117. print_digest(digest);
  118. printf(" %c%s\n", bin_mode ? '*' : ' ', *argv);
  119. }
  120. fclose(fp);
  121. }
  122. exit(rc);
  123. }
  124. void
  125. usage()
  126. {
  127. fputs(_("usage: md5sum [-bv] [-c [file]] | [file...]\n\
  128. Generates or checks MD5 Message Digests\n\
  129. -c check message digests (default is generate)\n\
  130. -v verbose, print file names when checking\n\
  131. -b read files in binary mode\n\
  132. The input for -c should be the list of message digests and file names\n\
  133. that is printed on stdout by this program when it generates digests.\n"), stderr);
  134. exit(2);
  135. }
  136. int
  137. mdfile(FILE *fp, unsigned char *digest)
  138. {
  139. unsigned char buf[1024];
  140. struct MD5Context ctx;
  141. int n;
  142. MD5Init(&ctx);
  143. while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
  144. MD5Update(&ctx, buf, n);
  145. MD5Final(digest, &ctx);
  146. if (ferror(fp))
  147. return -1;
  148. return 0;
  149. }
  150. void
  151. print_digest(unsigned char *p)
  152. {
  153. int i;
  154. for (i = 0; i < 16; ++i)
  155. printf("%02x", *p++);
  156. }
  157. int
  158. hex_digit(int c)
  159. {
  160. if (c >= '0' && c <= '9')
  161. return c - '0';
  162. if (c >= 'a' && c <= 'f')
  163. return c - 'a' + 10;
  164. return -1;
  165. }
  166. int
  167. get_md5_line(FILE *fp, unsigned char *digest, char *file)
  168. {
  169. char buf[1024];
  170. int i, d1, d2, rc;
  171. char *p = buf;
  172. if (fgets(buf, sizeof(buf), fp) == NULL)
  173. return -1;
  174. for (i = 0; i < 16; ++i) {
  175. if ((d1 = hex_digit(*p++)) == -1)
  176. return 0;
  177. if ((d2 = hex_digit(*p++)) == -1)
  178. return 0;
  179. *digest++ = d1*16 + d2;
  180. }
  181. if (*p++ != ' ')
  182. return 0;
  183. /*
  184. * next char is an attribute char, space means text file
  185. * if it's a '*' the file should be checked in binary mode.
  186. */
  187. if (*p == ' ')
  188. rc = 1;
  189. else if (*p == '*')
  190. rc = 2;
  191. else {
  192. fprintf(stderr, _("%s: unrecognized line: %s"), progname, buf);
  193. return 0;
  194. }
  195. ++p;
  196. i = strlen(p);
  197. if (i < 2 || i > 255)
  198. return 0;
  199. p[i-1] = '\0';
  200. strcpy(file, p);
  201. return rc;
  202. }
  203. int
  204. do_check(FILE *chkf)
  205. {
  206. int rc, ex = 0, failed = 0, checked = 0;
  207. unsigned char chk_digest[16], file_digest[16];
  208. char filename[256];
  209. FILE *fp;
  210. int flen = 14;
  211. while ((rc = get_md5_line(chkf, chk_digest, filename)) >= 0) {
  212. if (rc == 0) /* not an md5 line */
  213. continue;
  214. if (verbose) {
  215. if (strlen(filename) > flen)
  216. flen = strlen(filename);
  217. fprintf(stderr, "%-*s ", flen, filename);
  218. }
  219. if (bin_mode || rc == 2)
  220. fp = fopen(filename, FOPRBIN);
  221. else
  222. fp = fopen(filename, FOPRTXT);
  223. if (fp == NULL) {
  224. fprintf(stderr, _("%s: can't open %s\n"), progname, filename);
  225. ex = 2;
  226. continue;
  227. }
  228. if (mdfile(fp, file_digest)) {
  229. fprintf(stderr, _("%s: error reading %s\n"), progname, filename);
  230. ex = 2;
  231. fclose(fp);
  232. continue;
  233. }
  234. fclose(fp);
  235. if (memcmp(chk_digest, file_digest, 16) != 0) {
  236. if (verbose)
  237. fprintf(stderr, _("FAILED\n"));
  238. else
  239. fprintf(stderr, _("%s: MD5 check failed for '%s'\n"), progname, filename);
  240. ++failed;
  241. } else if (verbose)
  242. fprintf(stderr, _("OK\n"));
  243. ++checked;
  244. }
  245. if (verbose && failed)
  246. fprintf(stderr, _("%s: %d of %d file(s) failed MD5 check\n"), progname, failed, checked);
  247. if (!checked) {
  248. fprintf(stderr, _("%s: no files checked\n"), progname);
  249. return 3;
  250. }
  251. if (!ex && failed)
  252. ex = 1;
  253. return ex;
  254. }