1 /* md5sum.c - Compute MD5 checksum of files or strings according to the
2 * definition of MD5 in RFC 1321 from April 1992.
3 * Copyright (C) 1995-1999 Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu> */
21 /* Hacked to work with BusyBox by Alfred M. Szmidt <ams@trillian.itslinux.org> */
28 //----------------------------------------------------------------------------
30 //----------------------------------------------------------------------------
32 /* md5.c - Functions to compute MD5 message digest of files or memory blocks
33 * according to the definition of MD5 in RFC 1321 from April 1992.
34 * Copyright (C) 1995, 1996 Free Software Foundation, Inc.
36 * NOTE: The canonical source of this file is maintained with the GNU C
37 * Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
39 * This program is free software; you can redistribute it and/or modify it
40 * under the terms of the GNU General Public License as published by the
41 * Free Software Foundation; either version 2, or (at your option) any
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
49 * You should have received a copy of the GNU General Public License
50 * along with this program; if not, write to the Free Software Foundation,
51 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
54 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
56 #include <sys/types.h>
62 //----------------------------------------------------------------------------
64 //----------------------------------------------------------------------------
66 /* md5.h - Declaration of functions and data types used for MD5 sum
67 computing library functions.
68 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
69 NOTE: The canonical source of this file is maintained with the GNU C
70 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
72 This program is free software; you can redistribute it and/or modify it
73 under the terms of the GNU General Public License as published by the
74 Free Software Foundation; either version 2, or (at your option) any
77 This program is distributed in the hope that it will be useful,
78 but WITHOUT ANY WARRANTY; without even the implied warranty of
79 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80 GNU General Public License for more details.
82 You should have received a copy of the GNU General Public License
83 along with this program; if not, write to the Free Software Foundation,
84 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
91 #if defined HAVE_LIMITS_H || _LIBC
95 /* The following contortions are an attempt to use the C preprocessor
96 to determine an unsigned integral type that is 32 bits wide. An
97 alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
98 doing that would require that the configure script compile and *run*
99 the resulting executable. Locally running cross-compiled executables
100 is usually not possible. */
103 # include <sys/types.h>
104 typedef u_int32_t md5_uint32;
106 # if defined __STDC__ && __STDC__
107 # define UINT_MAX_32_BITS 4294967295U
109 # define UINT_MAX_32_BITS 0xFFFFFFFF
112 /* If UINT_MAX isn't defined, assume it's a 32-bit type.
113 This should be valid for all systems GNU cares about because
114 that doesn't include 16-bit systems, and only modern systems
115 (that certainly have <limits.h>) have 64+-bit integral types. */
118 # define UINT_MAX UINT_MAX_32_BITS
121 # if UINT_MAX == UINT_MAX_32_BITS
122 typedef unsigned int md5_uint32;
124 # if USHRT_MAX == UINT_MAX_32_BITS
125 typedef unsigned short md5_uint32;
127 # if ULONG_MAX == UINT_MAX_32_BITS
128 typedef unsigned long md5_uint32;
130 /* The following line is intended to evoke an error.
131 Using #error is not portable enough. */
132 "Cannot determine unsigned 32-bit data type."
139 #if defined (__STDC__) && __STDC__
145 /* Structure to save state of computation between the single steps. */
159 * The following three functions are build up the low level used in
160 * the functions `md5_stream' and `md5_buffer'.
163 /* Initialize structure containing state of computation.
164 (RFC 1321, 3.3: Step 3) */
165 extern void md5_init_ctx __P ((struct md5_ctx *ctx));
167 /* Starting with the result of former calls of this function (or the
168 initialization function update the context for the next LEN bytes
170 It is necessary that LEN is a multiple of 64!!! */
171 extern void md5_process_block __P ((const void *buffer, size_t len,
172 struct md5_ctx *ctx));
174 /* Starting with the result of former calls of this function (or the
175 initialization function update the context for the next LEN bytes
177 It is NOT required that LEN is a multiple of 64. */
178 extern void md5_process_bytes __P ((const void *buffer, size_t len,
179 struct md5_ctx *ctx));
181 /* Process the remaining bytes in the buffer and put result from CTX
182 in first 16 bytes following RESBUF. The result is always in little
183 endian byte order, so that a byte-wise output yields to the wanted
184 ASCII representation of the message digest.
186 IMPORTANT: On some systems it is required that RESBUF is correctly
187 aligned for a 32 bits value. */
188 extern void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf));
191 /* Put result from CTX in first 16 bytes following RESBUF. The result is
192 always in little endian byte order, so that a byte-wise output yields
193 to the wanted ASCII representation of the message digest.
195 IMPORTANT: On some systems it is required that RESBUF is correctly
196 aligned for a 32 bits value. */
197 extern void *md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf));
200 /* Compute MD5 message digest for bytes read from STREAM. The
201 resulting message digest number will be written into the 16 bytes
202 beginning at RESBLOCK. */
203 extern int md5_stream __P ((FILE *stream, void *resblock));
205 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
206 result is always in little endian byte order, so that a byte-wise
207 output yields to the wanted ASCII representation of the message
209 extern void *md5_buffer __P ((const char *buffer, size_t len, void *resblock));
213 //----------------------------------------------------------------------------
214 //--------end of md5.h
215 //----------------------------------------------------------------------------
219 /* This array contains the bytes used to pad the buffer to the next
220 64-byte boundary. (RFC 1321, 3.1: Step 1) */
221 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
223 /* Initialize structure containing state of computation.
224 (RFC 1321, 3.3: Step 3) */
225 void md5_init_ctx(struct md5_ctx *ctx)
232 ctx->total[0] = ctx->total[1] = 0;
236 /* Put result from CTX in first 16 bytes following RESBUF. The result
237 must be in little endian byte order.
239 IMPORTANT: On some systems it is required that RESBUF is correctly
240 aligned for a 32 bits value. */
241 void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf)
243 ((md5_uint32 *) resbuf)[0] = SWAP(ctx->A);
244 ((md5_uint32 *) resbuf)[1] = SWAP(ctx->B);
245 ((md5_uint32 *) resbuf)[2] = SWAP(ctx->C);
246 ((md5_uint32 *) resbuf)[3] = SWAP(ctx->D);
251 /* Process the remaining bytes in the internal buffer and the usual
252 prolog according to the standard and write the result to RESBUF.
254 IMPORTANT: On some systems it is required that RESBUF is correctly
255 aligned for a 32 bits value. */
256 void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf)
258 /* Take yet unprocessed bytes into account. */
259 md5_uint32 bytes = ctx->buflen;
262 /* Now count remaining bytes. */
263 ctx->total[0] += bytes;
264 if (ctx->total[0] < bytes)
267 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
268 memcpy(&ctx->buffer[bytes], fillbuf, pad);
270 /* Put the 64-bit file length in *bits* at the end of the buffer. */
271 *(md5_uint32 *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3);
272 *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] =
273 SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29));
275 /* Process last bytes. */
276 md5_process_block(ctx->buffer, bytes + pad + 8, ctx);
278 return md5_read_ctx(ctx, resbuf);
281 /* Compute MD5 message digest for bytes read from STREAM. The
282 resulting message digest number will be written into the 16 bytes
283 beginning at RESBLOCK. */
284 int md5_stream(FILE *stream, void *resblock)
286 /* Important: BLOCKSIZE must be a multiple of 64. */
287 #define BLOCKSIZE 4096
289 char buffer[BLOCKSIZE + 72];
292 /* Initialize the computation context. */
295 /* Iterate over full file contents. */
297 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
298 computation function processes the whole buffer so that with the
299 next round of the loop another block can be read. */
303 /* Read block. Take care for partial reads. */
305 n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream);
309 while (sum < BLOCKSIZE && n != 0);
310 if (n == 0 && ferror(stream))
313 /* If end of file is reached, end the loop. */
317 /* Process buffer with BLOCKSIZE bytes. Note that
320 md5_process_block(buffer, BLOCKSIZE, &ctx);
323 /* Add the last bytes if necessary. */
325 md5_process_bytes(buffer, sum, &ctx);
327 /* Construct result in desired memory. */
328 md5_finish_ctx(&ctx, resblock);
332 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
333 result is always in little endian byte order, so that a byte-wise
334 output yields to the wanted ASCII representation of the message
336 void *md5_buffer(const char *buffer, size_t len, void *resblock)
340 /* Initialize the computation context. */
343 /* Process whole buffer but last len % 64 bytes. */
344 md5_process_bytes(buffer, len, &ctx);
346 /* Put result in desired memory area. */
347 return md5_finish_ctx(&ctx, resblock);
350 void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx)
352 /* When we already have some bits in our internal buffer concatenate
353 both inputs first. */
354 if (ctx->buflen != 0) {
355 size_t left_over = ctx->buflen;
356 size_t add = 128 - left_over > len ? len : 128 - left_over;
358 memcpy(&ctx->buffer[left_over], buffer, add);
361 if (left_over + add > 64) {
362 md5_process_block(ctx->buffer, (left_over + add) & ~63, ctx);
363 /* The regions in the following copy operation cannot overlap. */
364 memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
365 (left_over + add) & 63);
366 ctx->buflen = (left_over + add) & 63;
369 buffer = (const char *) buffer + add;
373 /* Process available complete blocks. */
375 md5_process_block(buffer, len & ~63, ctx);
376 buffer = (const char *) buffer + (len & ~63);
380 /* Move remaining bytes in internal buffer. */
382 memcpy(ctx->buffer, buffer, len);
387 /* These are the four functions used in the four steps of the MD5 algorithm
388 and defined in the RFC 1321. The first function is a little bit optimized
389 (as found in Colin Plumbs public domain implementation). */
390 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
391 #define FF(b, c, d) (d ^ (b & (c ^ d)))
392 #define FG(b, c, d) FF (d, b, c)
393 #define FH(b, c, d) (b ^ c ^ d)
394 #define FI(b, c, d) (c ^ (b | ~d))
396 /* Process LEN bytes of BUFFER, accumulating context into CTX.
397 It is assumed that LEN % 64 == 0. */
398 void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
400 md5_uint32 correct_words[16];
401 const md5_uint32 *words = buffer;
402 size_t nwords = len / sizeof(md5_uint32);
403 const md5_uint32 *endp = words + nwords;
404 md5_uint32 A = ctx->A;
405 md5_uint32 B = ctx->B;
406 md5_uint32 C = ctx->C;
407 md5_uint32 D = ctx->D;
409 /* First increment the byte count. RFC 1321 specifies the possible
410 length of the file up to 2^64 bits. Here we only compute the
411 number of bytes. Do a double word increment. */
412 ctx->total[0] += len;
413 if (ctx->total[0] < len)
416 /* Process all bytes in the buffer with 64 bytes in each round of
418 while (words < endp) {
419 md5_uint32 *cwp = correct_words;
420 md5_uint32 A_save = A;
421 md5_uint32 B_save = B;
422 md5_uint32 C_save = C;
423 md5_uint32 D_save = D;
425 /* First round: using the given function, the context and a constant
426 the next context is computed. Because the algorithms processing
427 unit is a 32-bit word and it is determined to work on words in
428 little endian byte order we perhaps have to change the byte order
429 before the computation. To reduce the work for the next steps
430 we store the swapped words in the array CORRECT_WORDS. */
432 #define OP(a, b, c, d, s, T) \
435 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
442 /* It is unfortunate that C does not provide an operator for
443 cyclic rotation. Hope the C compiler is smart enough. */
444 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
446 /* Before we start, one word to the strange constants.
447 They are defined in RFC 1321 as
449 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
453 OP(A, B, C, D, 7, 0xd76aa478);
454 OP(D, A, B, C, 12, 0xe8c7b756);
455 OP(C, D, A, B, 17, 0x242070db);
456 OP(B, C, D, A, 22, 0xc1bdceee);
457 OP(A, B, C, D, 7, 0xf57c0faf);
458 OP(D, A, B, C, 12, 0x4787c62a);
459 OP(C, D, A, B, 17, 0xa8304613);
460 OP(B, C, D, A, 22, 0xfd469501);
461 OP(A, B, C, D, 7, 0x698098d8);
462 OP(D, A, B, C, 12, 0x8b44f7af);
463 OP(C, D, A, B, 17, 0xffff5bb1);
464 OP(B, C, D, A, 22, 0x895cd7be);
465 OP(A, B, C, D, 7, 0x6b901122);
466 OP(D, A, B, C, 12, 0xfd987193);
467 OP(C, D, A, B, 17, 0xa679438e);
468 OP(B, C, D, A, 22, 0x49b40821);
470 /* For the second to fourth round we have the possibly swapped words
471 in CORRECT_WORDS. Redefine the macro to take an additional first
472 argument specifying the function to use. */
474 #define OP(f, a, b, c, d, k, s, T) \
477 a += f (b, c, d) + correct_words[k] + T; \
484 OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
485 OP(FG, D, A, B, C, 6, 9, 0xc040b340);
486 OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
487 OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
488 OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
489 OP(FG, D, A, B, C, 10, 9, 0x02441453);
490 OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
491 OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
492 OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
493 OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
494 OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
495 OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
496 OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
497 OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
498 OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
499 OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
502 OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
503 OP(FH, D, A, B, C, 8, 11, 0x8771f681);
504 OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
505 OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
506 OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
507 OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
508 OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
509 OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
510 OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
511 OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
512 OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
513 OP(FH, B, C, D, A, 6, 23, 0x04881d05);
514 OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
515 OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
516 OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
517 OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
520 OP(FI, A, B, C, D, 0, 6, 0xf4292244);
521 OP(FI, D, A, B, C, 7, 10, 0x432aff97);
522 OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
523 OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
524 OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
525 OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
526 OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
527 OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
528 OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
529 OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
530 OP(FI, C, D, A, B, 6, 15, 0xa3014314);
531 OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
532 OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
533 OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
534 OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
535 OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
537 /* Add the starting values of the context. */
544 /* Put checksum in context given as argument. */
551 //----------------------------------------------------------------------------
552 //--------end of md5.c
553 //----------------------------------------------------------------------------
555 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
556 #define IN_CTYPE_DOMAIN(c) 1
557 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
558 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
559 #define TOLOWER(Ch) tolower (Ch)
560 #define OPENOPTS(BINARY) "r"
562 /* The minimum length of a valid digest line in a file produced
563 by `md5sum FILE' and read by `md5sum -c'. This length does
564 not include any newline character at the end of a line. */
565 #define MIN_DIGEST_LINE_LENGTH 35 /* 32 - message digest length
566 2 - blank and binary indicator
567 1 - minimum filename length */
569 static int have_read_stdin; /* Nonzero if any of the files read were
570 the standard input. */
572 static int status_only = 0; /* With -c, don't generate any output.
573 The exit code indicates success or failure */
574 static int warn = 0; /* With -w, print a message to standard error warning
575 about each improperly formatted MD5 checksum line */
577 static const char md5sum_usage[] =
578 "md5sum [OPTION] [FILE]...\n"
579 "or: md5sum [OPTION] -c [FILE]\n"
580 #ifndef BB_FEATURE_TRIVIAL_HELP
581 "\nPrint or check MD5 checksums.\n\n"
583 "With no FILE, or when FILE is -, read standard input.\n\n"
584 "\t-b\tread files in binary mode\n"
585 "\t-c\tcheck MD5 sums against given list\n"
586 "\t-t\tread files in text mode (default)\n"
587 "\t-g\tread a string\n"
588 "\nThe following two options are useful only when verifying checksums:\n"
589 "\t-s,\tdon't output anything, status code shows success\n"
590 "\t-w,\twarn about improperly formated MD5 checksum lines\n"
594 static int split_3(char *s,
601 int escaped_filename = 0;
603 while (ISWHITE(s[i]))
606 /* The line must have at least 35 (36 if the first is a backslash)
607 more characters to contain correct message digest information.
608 Ignore this line if it is too short. */
609 if (!(s_len - i >= MIN_DIGEST_LINE_LENGTH
610 || (s[i] == '\\' && s_len - i >= 1 + MIN_DIGEST_LINE_LENGTH)))
615 escaped_filename = 1;
617 *u = (unsigned char *) &s[i];
619 /* The first field has to be the 32-character hexadecimal
620 representation of the message digest. If it is not followed
621 immediately by a white space it's an error. */
628 if (s[i] != ' ' && s[i] != '*')
630 *binary = (s[i++] == '*');
632 /* All characters between the type indicator and end of line are
633 significant -- that includes leading and trailing white space. */
636 if (escaped_filename) {
637 /* Translate each `\n' string in the file name to a NEWLINE,
638 and each `\\' string to a backslash. */
645 if (i == s_len - 1) {
646 /* A valid line does not end with a backslash. */
658 /* Only `\' or `n' may follow a backslash. */
664 /* The file name may not contain a NUL. */
678 static int hex_digits(unsigned char const *s)
688 /* An interface to md5_stream. Operate on FILENAME (it may be "-") and
689 put the result in *MD5_RESULT. Return non-zero upon failure, zero
690 to indicate success. */
691 static int md5_file(const char *filename,
693 unsigned char *md5_result)
697 if (STREQ(filename, "-")) {
701 fp = fopen(filename, OPENOPTS(binary));
703 errorMsg("md5sum: %s: %s\n", filename, strerror(errno));
708 if (md5_stream(fp, md5_result)) {
709 errorMsg("md5sum: %s: %s\n", filename, strerror(errno));
716 if (fp != stdin && fclose(fp) == EOF) {
717 errorMsg("md5sum: %s: %s\n", filename, strerror(errno));
724 static int md5_check(const char *checkfile_name)
726 FILE *checkfile_stream;
727 int n_properly_formated_lines = 0;
728 int n_mismatched_checksums = 0;
729 int n_open_or_read_failures = 0;
730 unsigned char md5buffer[16];
733 size_t line_chars_allocated;
735 if (STREQ(checkfile_name, "-")) {
737 checkfile_stream = stdin;
739 checkfile_stream = fopen(checkfile_name, "r");
740 if (checkfile_stream == NULL) {
741 errorMsg("md5sum: %s: %s\n", checkfile_name, strerror(errno));
748 line_chars_allocated = 0;
753 unsigned char *md5num;
758 line_length = getline(&line, &line_chars_allocated, checkfile_stream);
760 if (line_length <= 0)
763 /* Ignore comment lines, which begin with a '#' character. */
767 /* Remove any trailing newline. */
768 if (line[line_length - 1] == '\n')
769 line[--line_length] = '\0';
771 if (split_3(line, line_length, &md5num, &binary, &filename)
772 || !hex_digits(md5num)) {
774 errorMsg("%s: %lu: improperly formatted MD5 checksum line\n",
775 checkfile_name, (unsigned long) line_number);
778 static const char bin2hex[] = {
785 ++n_properly_formated_lines;
787 if (md5_file(filename, binary, md5buffer)) {
788 ++n_open_or_read_failures;
790 printf("%s: FAILED open or read\n", filename);
795 /* Compare generated binary number with text representation
796 in check file. Ignore case of hex digits. */
797 for (cnt = 0; cnt < 16; ++cnt) {
798 if (TOLOWER(md5num[2 * cnt])
799 != bin2hex[md5buffer[cnt] >> 4]
800 || (TOLOWER(md5num[2 * cnt + 1])
801 != (bin2hex[md5buffer[cnt] & 0xf])))
805 ++n_mismatched_checksums;
808 printf("%s: %s\n", filename,
809 (cnt != 16 ? "FAILED" : "OK"));
816 while (!feof(checkfile_stream) && !ferror(checkfile_stream));
821 if (ferror(checkfile_stream)) {
822 errorMsg("%s: read error", checkfile_name); /* */
826 if (checkfile_stream != stdin && fclose(checkfile_stream) == EOF) {
827 errorMsg("md5sum: %s: %s\n", checkfile_name, strerror(errno));
831 if (n_properly_formated_lines == 0) {
832 /* Warn if no tests are found. */
833 errorMsg("%s: no properly formatted MD5 checksum lines found\n",
838 int n_computed_checkums = (n_properly_formated_lines
839 - n_open_or_read_failures);
841 if (n_open_or_read_failures > 0) {
842 errorMsg("WARNING: %d of %d listed files could not be read\n",
843 n_open_or_read_failures, n_properly_formated_lines);
847 if (n_mismatched_checksums > 0) {
848 errorMsg("WARNING: %d of %d computed checksums did NOT match\n",
849 n_mismatched_checksums, n_computed_checkums);
855 return ((n_properly_formated_lines > 0 && n_mismatched_checksums == 0
856 && n_open_or_read_failures == 0) ? 0 : 1);
859 int md5sum_main(int argc,
862 unsigned char md5buffer[16];
865 char **string = NULL;
866 size_t n_strings = 0;
868 int file_type_specified = 0;
871 while ((opt = getopt(argc, argv, "g:bcstw")) != -1) {
873 case 'g': { /* read a string */
875 string = (char **) xmalloc ((argc - 1) * sizeof (char *));
879 string[n_strings++] = optarg;
883 case 'b': /* read files in binary mode */
884 file_type_specified = 1;
888 case 'c': /* check MD5 sums against given list */
892 case 's': /* don't output anything, status code shows success */
897 case 't': /* read files in text mode (default) */
898 file_type_specified = 1;
902 case 'w': /* warn about improperly formated MD5 checksum lines */
912 if (file_type_specified && do_check) {
913 errorMsg("the -b and -t options are meaningless when verifying checksums\n");
917 if (n_strings > 0 && do_check) {
918 errorMsg("the -g and -c options are mutually exclusive\n");
922 if (status_only && !do_check) {
923 errorMsg("the -s option is meaningful only when verifying checksums\n");
927 if (warn && !do_check) {
928 errorMsg("the -w option is meaningful only when verifying checksums\n");
936 errorMsg("no files may be specified when using -g\n");
939 for (i = 0; i < n_strings; ++i) {
941 md5_buffer (string[i], strlen (string[i]), md5buffer);
943 for (cnt = 0; cnt < 16; ++cnt)
944 printf ("%02x", md5buffer[cnt]);
946 printf (" \"%s\"\n", string[i]);
948 } else if (do_check) {
949 if (optind + 1 < argc) {
950 errorMsg("only one argument may be specified when using -c\n");
953 err = md5_check ((optind == argc) ? "-" : argv[optind]);
958 for (; optind < argc; ++optind) {
960 char *file = argv[optind];
962 fail = md5_file (file, binary, md5buffer);
966 /* Output a leading backslash if the file name contains
967 a newline or backslash. */
968 if (strchr (file, '\n') || strchr (file, '\\'))
971 for (i = 0; i < 16; ++i)
972 printf ("%02x", md5buffer[i]);
980 /* Translate each NEWLINE byte to the string, "\\n",
981 and each backslash to "\\\\". */
982 for (i = 0; i < strlen (file); ++i) {
985 fputs ("\\n", stdout);
989 fputs ("\\\\", stdout);
1002 if (fclose (stdout) == EOF) {
1003 errorMsg("write error");
1007 if (have_read_stdin && fclose (stdin) == EOF) {
1008 errorMsg("standard input");
1012 exit (err == 0 ? TRUE : FALSE);