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