md5sum.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "config.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <getopt.h>
  21. #include <unistd.h>
  22. /* Take care of NLS matters. */
  23. #if HAVE_LOCALE_H
  24. # include <locale.h>
  25. #endif
  26. #if !HAVE_SETLOCALE
  27. # define setlocale(Category, Locale) /* empty */
  28. #endif
  29. #if ENABLE_NLS
  30. # include <libintl.h>
  31. # define _(Text) gettext (Text)
  32. #else
  33. # undef bindtextdomain
  34. # define bindtextdomain(Domain, Directory) /* empty */
  35. # undef textdomain
  36. # define textdomain(Domain) /* empty */
  37. # define _(Text) Text
  38. #endif
  39. #include <dpkg.h>
  40. #ifdef UNIX
  41. #define FOPRTXT "r"
  42. #define FOPRBIN "r"
  43. #else
  44. #ifdef VMS
  45. #define FOPRTXT "r","ctx=stm"
  46. #define FOPRBIN "rb","ctx=stm"
  47. #else
  48. #define FOPRTXT "r"
  49. #define FOPRBIN "rb"
  50. #endif
  51. #endif
  52. extern char *optarg;
  53. extern int optind;
  54. const char printforhelp[]= N_("Type md5sum --help for help.");
  55. const char thisname[]= MD5SUM;
  56. void usage(void);
  57. void print_digest(unsigned char *p);
  58. int mdfile(int fd, unsigned char **digest);
  59. int do_check(FILE *chkf);
  60. int hex_digit(int c);
  61. int get_md5_line(FILE *fp, unsigned char *digest, char *file);
  62. int process_arg(const char* arg, int bin_mode, unsigned char **digest);
  63. char *progname;
  64. int verbose = 0;
  65. int bin_mode = 0;
  66. static
  67. void
  68. print_md5sum_error(const char* emsg, const char* arg) {
  69. fprintf(stderr, _("error processing %s: %s\n"), arg, emsg);
  70. }
  71. static void cu_closefile(int argc, void** argv) {
  72. FILE *f = (FILE*)(argv[0]);
  73. fclose(f);
  74. }
  75. int
  76. main(int argc, char **argv)
  77. {
  78. jmp_buf ejbuf;
  79. int opt, rc = 0;
  80. int check = 0;
  81. FILE *fp = NULL;
  82. unsigned char *digest = NULL;
  83. setlocale(LC_ALL, "");
  84. bindtextdomain(PACKAGE, LOCALEDIR);
  85. textdomain(PACKAGE);
  86. progname = *argv;
  87. while ((opt = getopt(argc, argv, "cbvp:h")) != EOF) {
  88. switch (opt) {
  89. case 'c': check = 1; break;
  90. case 'v': verbose = 1; break;
  91. case 'b': bin_mode = 1; break;
  92. default: usage();
  93. }
  94. }
  95. argc -= optind;
  96. argv += optind;
  97. if (check) {
  98. switch (argc) {
  99. case 0: fp = stdin; break;
  100. case 1: if ((fp = fopen(*argv, FOPRTXT)) == NULL) {
  101. perror(*argv);
  102. exit(2);
  103. }
  104. break;
  105. default: usage();
  106. }
  107. exit(do_check(fp));
  108. }
  109. if (argc == 0) {
  110. if (setjmp(ejbuf)) {
  111. error_unwind(ehflag_bombout);
  112. exit(1);
  113. }
  114. push_error_handler(&ejbuf, print_md5sum_error, "stdin");
  115. mdfile(fileno(stdin), &digest);
  116. printf("%s %c-\n", digest, bin_mode ? '*' : ' ');
  117. set_error_display(0, 0);
  118. error_unwind(ehflag_normaltidy);
  119. exit(0);
  120. }
  121. for ( ; argc > 0; --argc, ++argv) {
  122. switch (process_arg(*argv, bin_mode, &digest)) {
  123. case 1:
  124. rc++;
  125. break;
  126. case 2:
  127. perror(*argv);
  128. rc++;
  129. break;
  130. case 3:
  131. perror(*argv);
  132. rc++;
  133. break;
  134. }
  135. printf("%s %c%s\n", digest, bin_mode ? '*' : ' ', *argv);
  136. }
  137. return rc;
  138. }
  139. int process_arg(const char* arg, int bin_mode, unsigned char **digest)
  140. {
  141. jmp_buf ejbuf;
  142. FILE *fp = NULL;
  143. if (setjmp(ejbuf)) {
  144. error_unwind(ehflag_bombout);
  145. return 1;
  146. }
  147. push_error_handler(&ejbuf, print_md5sum_error, arg);
  148. if (bin_mode)
  149. fp = fopen(arg, FOPRBIN);
  150. else
  151. fp = fopen(arg, FOPRTXT);
  152. if (fp == NULL)
  153. return 2;
  154. push_cleanup(cu_closefile,ehflag_bombout, 0,0, 1,(void*)fp);
  155. if (mdfile(fileno(fp), digest)) {
  156. fclose(fp);
  157. return 3;
  158. }
  159. pop_cleanup(ehflag_normaltidy); /* fd= fopen() */
  160. fclose(fp);
  161. set_error_display(0, 0);
  162. error_unwind(ehflag_normaltidy);
  163. return 0;
  164. }
  165. void usage(void) NONRETURNING;
  166. void
  167. usage(void)
  168. {
  169. fputs(_("usage: md5sum [-bv] [-c [file]] | [file...]\n\
  170. Generates or checks MD5 Message Digests\n\
  171. -c check message digests (default is generate)\n\
  172. -v verbose, print file names when checking\n\
  173. -b read files in binary mode\n\
  174. The input for -c should be the list of message digests and file names\n\
  175. that is printed on stdout by this program when it generates digests.\n"), stderr);
  176. exit(2);
  177. }
  178. int
  179. mdfile(int fd, unsigned char **digest)
  180. {
  181. ssize_t ret = fd_md5(fd, digest, -1, _("mdfile"));
  182. if ( ret >= 0 )
  183. return 0;
  184. else
  185. return ret;
  186. }
  187. int
  188. hex_digit(int c)
  189. {
  190. if (c >= '0' && c <= '9')
  191. return c - '0';
  192. if (c >= 'a' && c <= 'f')
  193. return c - 'a' + 10;
  194. return -1;
  195. }
  196. int
  197. get_md5_line(FILE *fp, unsigned char *digest, char *file)
  198. {
  199. char buf[1024];
  200. int i, rc;
  201. char *p = buf;
  202. if (fgets(buf, sizeof(buf), fp) == NULL)
  203. return -1;
  204. memcpy(digest, p, 32);
  205. p += 32;
  206. if (*p++ != ' ')
  207. return 0;
  208. /*
  209. * next char is an attribute char, space means text file
  210. * if it's a '*' the file should be checked in binary mode.
  211. */
  212. if (*p == ' ')
  213. rc = 1;
  214. else if (*p == '*')
  215. rc = 2;
  216. else {
  217. fprintf(stderr, _("%s: unrecognized line: %s"), progname, buf);
  218. return 0;
  219. }
  220. ++p;
  221. i = strlen(p);
  222. if (i < 2 || i > 255)
  223. return 0;
  224. p[i-1] = '\0';
  225. strcpy(file, p);
  226. return rc;
  227. }
  228. int
  229. do_check(FILE *chkf)
  230. {
  231. int rc, ex = 0, failed = 0, checked = 0;
  232. unsigned char chk_digest[32], *file_digest = NULL;
  233. char filename[256];
  234. size_t flen = 14;
  235. while ((rc = get_md5_line(chkf, chk_digest, filename)) >= 0) {
  236. if (rc == 0) /* not an md5 line */
  237. continue;
  238. if (verbose) {
  239. if (strlen(filename) > flen)
  240. flen = strlen(filename);
  241. fprintf(stderr, "%-*s ", (int)flen, filename);
  242. }
  243. switch (process_arg(filename, bin_mode || rc == 2, &file_digest)) {
  244. case 2:
  245. fprintf(stderr, _("%s: can't open %s\n"), progname, filename);
  246. ex = 2;
  247. break;
  248. case 3:
  249. fprintf(stderr, _("%s: error reading %s\n"), progname, filename);
  250. ex = 2;
  251. break;
  252. }
  253. if (memcmp(chk_digest, file_digest, 32) != 0) {
  254. if (verbose)
  255. fprintf(stderr, _("FAILED\n"));
  256. else
  257. fprintf(stderr, _("%s: MD5 check failed for '%s'\n"), progname, filename);
  258. ++failed;
  259. } else if (verbose)
  260. fprintf(stderr, _("OK\n"));
  261. ++checked;
  262. }
  263. if (verbose && failed)
  264. fprintf(stderr, _("%s: %d of %d file(s) failed MD5 check\n"), progname, failed, checked);
  265. if (!checked) {
  266. fprintf(stderr, _("%s: no files checked\n"), progname);
  267. return 3;
  268. }
  269. if (!ex && failed)
  270. ex = 1;
  271. return ex;
  272. }