- fix build
[oweals/busybox.git] / coreutils / md5_sha1_sum.c
1 /*
2  *  Copyright (C) 2003 Glenn L. McGrath
3  *  Copyright (C) 2003-2004 Erik Andersen
4  *
5  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6  */
7
8 #include <fcntl.h>
9 #include <limits.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "busybox.h"
17
18 typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
19
20 #define FLAG_SILENT     1
21 #define FLAG_CHECK      2
22 #define FLAG_WARN       4
23
24 /* This might be useful elsewhere */
25 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
26                                                                           unsigned char hash_length)
27 {
28         int x, len, max;
29         unsigned char *hex_value;
30
31         max = (hash_length * 2) + 2;
32         hex_value = xmalloc(max);
33         for (x = len = 0; x < hash_length; x++) {
34                 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
35         }
36         return (hex_value);
37 }
38
39 static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
40 {
41         int src_fd, hash_len, count;
42         union _ctx_ {
43                 sha1_ctx_t sha1;
44                 md5_ctx_t md5;
45         } context;
46         uint8_t *hash_value = NULL;
47         RESERVE_CONFIG_UBUFFER(in_buf, 4096);
48         void (*update)(const void*, size_t, void*);
49         void (*final)(void*, void*);
50         
51         if(strcmp(filename, "-") == 0) {
52                 src_fd = STDIN_FILENO;
53         } else if(0 > (src_fd = open(filename, O_RDONLY))) {
54                 bb_perror_msg("%s", filename);
55                 return NULL;
56         }
57
58         // figure specific hash algorithims
59         if(ENABLE_MD5SUM && hash_algo==HASH_MD5) {
60                 md5_begin(&context.md5);
61                 update = (void (*)(const void*, size_t, void*))md5_hash;
62                 final = (void (*)(void*, void*))md5_end;
63                 hash_len = 16;
64         } else if(ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
65                 sha1_begin(&context.sha1);
66                 update = (void (*)(const void*, size_t, void*))sha1_hash;
67                 final = (void (*)(void*, void*))sha1_end;
68                 hash_len = 20;
69         } else {
70                 bb_error_msg_and_die("algotithm not supported");
71         }
72         
73
74         while(0 < (count = read(src_fd, in_buf, sizeof in_buf))) {
75                 update(in_buf, count, &context);
76         }
77
78         if(count == 0) {
79                 final(in_buf, &context);
80                 hash_value = hash_bin_to_hex(in_buf, hash_len);
81         }
82         
83         RELEASE_CONFIG_BUFFER(in_buf);
84         
85         if(src_fd != STDIN_FILENO) {
86                 close(src_fd);
87         }
88         
89         return hash_value;
90 }
91
92 /* This could become a common function for md5 as well, by using md5_stream */
93 static int hash_files(int argc, char **argv, hash_algo_t hash_algo)
94 {
95         int return_value = EXIT_SUCCESS;
96         uint8_t *hash_value;
97         unsigned int flags;
98
99         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
100                 flags = bb_getopt_ulflags(argc, argv, "scw");
101
102         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
103                 if (flags & FLAG_SILENT) {
104                         bb_error_msg_and_die
105                                 ("the -s option is meaningful only when verifying checksums");
106                 } else if (flags & FLAG_WARN) {
107                         bb_error_msg_and_die
108                                 ("the -w option is meaningful only when verifying checksums");
109                 }
110         }
111
112         if (argc == optind) {
113                 argv[argc++] = "-";
114         }
115         
116         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) {
117                 FILE *pre_computed_stream;
118                 int count_total = 0;
119                 int count_failed = 0;
120                 char *file_ptr = argv[optind];
121                 char *line;
122
123                 if (optind + 1 != argc) {
124                         bb_error_msg_and_die
125                                 ("only one argument may be specified when using -c");
126                 }
127
128                 if (strcmp(file_ptr, "-") == 0) {
129                         pre_computed_stream = stdin;
130                 } else {
131                         pre_computed_stream = bb_xfopen(file_ptr, "r");
132                 }
133
134                 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
135                         char *filename_ptr;
136
137                         count_total++;
138                         filename_ptr = strstr(line, "  ");
139                         if (filename_ptr == NULL) {
140                                 if (flags & FLAG_WARN) {
141                                         bb_error_msg("Invalid format");
142                                 }
143                                 count_failed++;
144                                 return_value = EXIT_FAILURE;
145                                 free(line);
146                                 continue;
147                         }
148                         *filename_ptr = '\0';
149                         filename_ptr += 2;
150
151                         hash_value = hash_file(filename_ptr, hash_algo);
152
153                         if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
154                                 if (!(flags & FLAG_SILENT))
155                                         printf("%s: OK\n", filename_ptr);
156                         } else {
157                                 if (!(flags & FLAG_SILENT))
158                                         printf("%s: FAILED\n", filename_ptr);
159                                 count_failed++;
160                                 return_value = EXIT_FAILURE;
161                         }
162                         /* possible free(NULL) */
163                         free(hash_value);
164                         free(line);
165                 }
166                 if (count_failed && !(flags & FLAG_SILENT)) {
167                         bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
168                                                  count_failed, count_total);
169                 }
170                 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
171                         bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
172                 }
173         } else {
174                 while (optind < argc) {
175                         char *file_ptr = argv[optind++];
176
177                         hash_value = hash_file(file_ptr, hash_algo);
178                         if (hash_value == NULL) {
179                                 return_value = EXIT_FAILURE;
180                         } else {
181                                 printf("%s  %s\n", hash_value, file_ptr);
182                                 free(hash_value);
183                         }
184                 }
185         }
186         return (return_value);
187 }
188
189 #ifdef CONFIG_MD5SUM
190 int md5sum_main(int argc, char **argv)
191 {
192         return(hash_files(argc, argv, HASH_MD5));
193 }
194 #endif
195
196 #ifdef CONFIG_SHA1SUM
197 int sha1sum_main(int argc, char **argv)
198 {
199         return(hash_files(argc, argv, HASH_SHA1));
200 }
201 #endif