md5sum.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. }
  133. printf("%s %c%s\n", digest, bin_mode ? '*' : ' ', *argv);
  134. }
  135. return rc;
  136. }
  137. int process_arg(const char* arg, int bin_mode, unsigned char **digest)
  138. {
  139. jmp_buf ejbuf;
  140. FILE *fp = NULL;
  141. if (setjmp(ejbuf)) {
  142. error_unwind(ehflag_bombout);
  143. return 1;
  144. }
  145. push_error_handler(&ejbuf, print_md5sum_error, arg);
  146. if (bin_mode)
  147. fp = fopen(arg, FOPRBIN);
  148. else
  149. fp = fopen(arg, FOPRTXT);
  150. if (fp == NULL)
  151. return 2;
  152. push_cleanup(cu_closefile,ehflag_bombout, 0,0, 1,(void*)fp);
  153. if (mdfile(fileno(fp), digest)) {
  154. fclose(fp);
  155. return 3;
  156. }
  157. pop_cleanup(ehflag_normaltidy); /* fd= fopen() */
  158. fclose(fp);
  159. set_error_display(0, 0);
  160. error_unwind(ehflag_normaltidy);
  161. return 0;
  162. }
  163. void
  164. usage(void)
  165. {
  166. fputs(_("usage: md5sum [-bv] [-c [file]] | [file...]\n\
  167. Generates or checks MD5 Message Digests\n\
  168. -c check message digests (default is generate)\n\
  169. -v verbose, print file names when checking\n\
  170. -b read files in binary mode\n\
  171. The input for -c should be the list of message digests and file names\n\
  172. that is printed on stdout by this program when it generates digests.\n"), stderr);
  173. exit(2);
  174. }
  175. int
  176. mdfile(int fd, unsigned char **digest)
  177. {
  178. ssize_t ret = fd_md5(fd, digest, -1, _("mdfile"));
  179. if ( ret >= 0 )
  180. return 0;
  181. else
  182. return ret;
  183. }
  184. int
  185. hex_digit(int c)
  186. {
  187. if (c >= '0' && c <= '9')
  188. return c - '0';
  189. if (c >= 'a' && c <= 'f')
  190. return c - 'a' + 10;
  191. return -1;
  192. }
  193. int
  194. get_md5_line(FILE *fp, unsigned char *digest, char *file)
  195. {
  196. char buf[1024];
  197. int i, rc;
  198. char *p = buf;
  199. if (fgets(buf, sizeof(buf), fp) == NULL)
  200. return -1;
  201. memcpy(digest, p, 32);
  202. p += 32;
  203. if (*p++ != ' ')
  204. return 0;
  205. /*
  206. * next char is an attribute char, space means text file
  207. * if it's a '*' the file should be checked in binary mode.
  208. */
  209. if (*p == ' ')
  210. rc = 1;
  211. else if (*p == '*')
  212. rc = 2;
  213. else {
  214. fprintf(stderr, _("%s: unrecognized line: %s"), progname, buf);
  215. return 0;
  216. }
  217. ++p;
  218. i = strlen(p);
  219. if (i < 2 || i > 255)
  220. return 0;
  221. p[i-1] = '\0';
  222. strcpy(file, p);
  223. return rc;
  224. }
  225. int
  226. do_check(FILE *chkf)
  227. {
  228. int rc, ex = 0, failed = 0, checked = 0;
  229. unsigned char chk_digest[32], *file_digest = NULL;
  230. char filename[256];
  231. size_t flen = 14;
  232. while ((rc = get_md5_line(chkf, chk_digest, filename)) >= 0) {
  233. if (rc == 0) /* not an md5 line */
  234. continue;
  235. if (verbose) {
  236. if (strlen(filename) > flen)
  237. flen = strlen(filename);
  238. fprintf(stderr, "%-*s ", (int)flen, filename);
  239. }
  240. switch (process_arg(filename, bin_mode || rc == 2, &file_digest)) {
  241. case 2:
  242. fprintf(stderr, _("%s: can't open %s\n"), progname, filename);
  243. ex = 2;
  244. break;
  245. case 3:
  246. fprintf(stderr, _("%s: error reading %s\n"), progname, filename);
  247. ex = 2;
  248. break;
  249. }
  250. if (memcmp(chk_digest, file_digest, 32) != 0) {
  251. if (verbose)
  252. fprintf(stderr, _("FAILED\n"));
  253. else
  254. fprintf(stderr, _("%s: MD5 check failed for '%s'\n"), progname, filename);
  255. ++failed;
  256. } else if (verbose)
  257. fprintf(stderr, _("OK\n"));
  258. ++checked;
  259. }
  260. if (verbose && failed)
  261. fprintf(stderr, _("%s: %d of %d file(s) failed MD5 check\n"), progname, failed, checked);
  262. if (!checked) {
  263. fprintf(stderr, _("%s: no files checked\n"), progname);
  264. return 3;
  265. }
  266. if (!ex && failed)
  267. ex = 1;
  268. return ex;
  269. }