Only use getopt and associated flags if checking is enabled
[platform/upstream/busybox.git] / coreutils / md5_sha1_sum.c
1 /*
2  *  Copyright (C) 2003 Glenn L. McGrath
3  *  Copyright (C) 2003 Erik Andersen
4  * 
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 of the License, or
8  *  (at your option) any later version.
9  *
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.
14  *
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
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "busybox.h"
29
30
31 #define FLAG_SILENT     1
32 #define FLAG_CHECK      2
33 #define FLAG_WARN       4
34
35 /* This might be usefull elsewhere */
36 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
37                                                                           unsigned char hash_length)
38 {
39         int x, len, max;
40         unsigned char *hex_value;
41
42         max = (hash_length * 2) + 2;
43         hex_value = xmalloc(max);
44         for (x = len = 0; x < hash_length; x++) {
45                 len += snprintf(hex_value + len, max - len, "%02x", hash_value[x]);
46         }
47         return (hex_value);
48 }
49
50 /* This could become a common function for md5 as well, by using md5_stream */
51 extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
52 {
53         int return_value = EXIT_SUCCESS;
54
55 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
56         unsigned int flags;
57
58         flags = bb_getopt_ulflags(argc, argv, "scw");
59 #endif
60
61 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
62         if (!(flags & FLAG_CHECK)) {
63                 if (flags & FLAG_SILENT) {
64                         bb_error_msg_and_die
65                                 ("the -s option is meaningful only when verifying checksums");
66                 } else if (flags & FLAG_WARN) {
67                         bb_error_msg_and_die
68                                 ("the -w option is meaningful only when verifying checksums");
69                 }
70         }
71 #endif
72
73         if (argc == optind) {
74                 argv[argc++] = "-";
75         }
76 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
77         if (flags & FLAG_CHECK) {
78                 FILE *pre_computed_stream;
79                 int count_total = 0;
80                 int count_failed = 0;
81                 unsigned char *file_ptr = argv[optind];
82                 char *line;
83
84                 if (optind + 1 != argc) {
85                         bb_error_msg_and_die
86                                 ("only one argument may be specified when using -c");
87                 }
88
89                 if (strcmp(file_ptr, "-") == 0) {
90                         pre_computed_stream = stdin;
91                 } else {
92                         pre_computed_stream = bb_xfopen(file_ptr, "r");
93                 }
94
95                 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
96                         char *filename_ptr;
97
98                         count_total++;
99                         filename_ptr = strstr(line, "  ");
100                         if (filename_ptr == NULL) {
101                                 if (flags & FLAG_WARN) {
102                                         bb_error_msg("Invalid format");
103                                 }
104                                 free(line);
105                                 continue;
106                         }
107                         *filename_ptr = '\0';
108                         filename_ptr += 2;
109
110                         hash_value = hash_file(filename_ptr, hash_algo);
111
112                         if (hash_value && (strcmp(hash_value, line) == 0)) {
113                                 printf("%s: OK\n", filename_ptr);
114                         } else {
115                                 printf("%s: FAILED\n", filename_ptr);
116                                 count_failed++;
117                         }
118                         /* possible free(NULL) */
119                         free(hash_value);
120                         free(line);
121                 }
122                 if (count_failed) {
123                         bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
124                                                  count_failed, count_total);
125                 }
126                 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
127                         bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
128                 }
129         } else
130 #endif
131         {
132                 uint8_t *hash_value_bin;
133                 uint8_t hash_length;
134
135                 if (hash_algo == HASH_MD5) {
136                         hash_length = 16;
137                 } else {
138                         hash_length = 20;
139                 }
140                 hash_value_bin = xmalloc(hash_length);
141
142                 while (optind < argc) {
143                         unsigned char *file_ptr = argv[optind++];
144                         uint8_t *hash_value;
145                         int src_fd;
146
147                         if ((file_ptr[0] == '-') && (file_ptr[1] == '\0')) {
148                                 src_fd = fileno(stdin);
149                         } else {
150                                 src_fd = open(file_ptr, O_RDONLY);
151                         }
152
153                         if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
154                                 hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
155                         } else {
156                                 bb_perror_msg("%s", file_ptr);
157                                 continue;
158                         }
159                         close(src_fd);
160
161                         if (hash_value == NULL) {
162                                 return_value++;
163                         } else {
164 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
165                                 if (!flags & FLAG_SILENT)
166 #endif
167                                         printf("%s  %s\n", hash_value, file_ptr);
168                                 free(hash_value);
169                         }
170                 }
171         }
172         return (return_value);
173 }
174
175 #ifdef CONFIG_MD5SUM
176 extern int md5sum_main(int argc, char **argv)
177 {
178         return(hash_files(argc, argv, HASH_MD5));
179 }
180 #endif
181
182 #ifdef CONFIG_SHA1SUM
183 extern int sha1sum_main(int argc, char **argv)
184 {
185         return(hash_files(argc, argv, HASH_SHA1));
186 }
187 #endif