md5sum.c 6.1 KB

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