md5sum.c 6.4 KB

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