914f81fa24961f672f366d2e062281f089794c62
[oweals/busybox.git] / md5_sha1_sum.c
1 /*
2  *  Copyright (C) 2003 Glenn L. McGrath
3  *  Copyright (C) 2003-2004 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 useful 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 static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
51 {
52         int src_fd = strcmp(filename, "-") == 0 ? STDIN_FILENO :
53                 open(filename, O_RDONLY);
54         if (src_fd == -1) {
55                 bb_perror_msg("%s", filename);
56                 return NULL;
57         } else {
58                 uint8_t *hash_value;
59                 RESERVE_CONFIG_UBUFFER(hash_value_bin, 20);
60                 hash_value = hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2 ?
61                         hash_bin_to_hex(hash_value_bin, hash_algo == HASH_MD5 ? 16 : 20) :
62                         NULL;
63                 RELEASE_CONFIG_BUFFER(hash_value_bin);
64                 close(src_fd);
65                 return hash_value;
66         }
67 }
68
69 /* This could become a common function for md5 as well, by using md5_stream */
70 static int hash_files(int argc, char **argv, const uint8_t hash_algo)
71 {
72         int return_value = EXIT_SUCCESS;
73         uint8_t *hash_value;
74
75 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
76         unsigned int flags;
77
78         flags = bb_getopt_ulflags(argc, argv, "scw");
79 #endif
80
81 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
82         if (!(flags & FLAG_CHECK)) {
83                 if (flags & FLAG_SILENT) {
84                         bb_error_msg_and_die
85                                 ("the -s option is meaningful only when verifying checksums");
86                 } else if (flags & FLAG_WARN) {
87                         bb_error_msg_and_die
88                                 ("the -w option is meaningful only when verifying checksums");
89                 }
90         }
91 #endif
92
93         if (argc == optind) {
94                 argv[argc++] = "-";
95         }
96 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
97         if (flags & FLAG_CHECK) {
98                 FILE *pre_computed_stream;
99                 int count_total = 0;
100                 int count_failed = 0;
101                 unsigned char *file_ptr = argv[optind];
102                 char *line;
103
104                 if (optind + 1 != argc) {
105                         bb_error_msg_and_die
106                                 ("only one argument may be specified when using -c");
107                 }
108
109                 if (strcmp(file_ptr, "-") == 0) {
110                         pre_computed_stream = stdin;
111                 } else {
112                         pre_computed_stream = bb_xfopen(file_ptr, "r");
113                 }
114
115                 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
116                         char *filename_ptr;
117
118                         count_total++;
119                         filename_ptr = strstr(line, "  ");
120                         if (filename_ptr == NULL) {
121                                 if (flags & FLAG_WARN) {
122                                         bb_error_msg("Invalid format");
123                                 }
124                                 free(line);
125                                 continue;
126                         }
127                         *filename_ptr = '\0';
128                         filename_ptr += 2;
129
130                         hash_value = hash_file(filename_ptr, hash_algo);
131
132                         if (hash_value && (strcmp(hash_value, line) == 0)) {
133                                 if (!(flags & FLAG_SILENT))
134                                         printf("%s: OK\n", filename_ptr);
135                         } else {
136                                 if (!(flags & FLAG_SILENT))
137                                         printf("%s: FAILED\n", filename_ptr);
138                                 count_failed++;
139                                 return_value = EXIT_FAILURE;
140                         }
141                         /* possible free(NULL) */
142                         free(hash_value);
143                         free(line);
144                 }
145                 if (count_failed && !(flags & FLAG_SILENT)) {
146                         bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
147                                                  count_failed, count_total);
148                 }
149                 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
150                         bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
151                 }
152         } else
153 #endif
154         {
155                 uint8_t hash_length;
156
157                 if (hash_algo == HASH_MD5) {
158                         hash_length = 16;
159                 } else {
160                         hash_length = 20;
161                 }
162                 hash_value = xmalloc(hash_length);
163
164                 while (optind < argc) {
165                         unsigned char *file_ptr = argv[optind++];
166
167                         hash_value = hash_file(file_ptr, hash_algo);
168                         if (hash_value == NULL) {
169                                 return_value = EXIT_FAILURE;
170                         } else {
171                                 printf("%s  %s\n", hash_value, file_ptr);
172                                 free(hash_value);
173                         }
174                 }
175         }
176         return (return_value);
177 }
178
179 #ifdef CONFIG_MD5SUM
180 extern int md5sum_main(int argc, char **argv)
181 {
182         return(hash_files(argc, argv, HASH_MD5));
183 }
184 #endif
185
186 #ifdef CONFIG_SHA1SUM
187 extern int sha1sum_main(int argc, char **argv)
188 {
189         return(hash_files(argc, argv, HASH_SHA1));
190 }
191 #endif