use bb_xbind/bb_xlisten
[oweals/busybox.git] / coreutils / md5_sha1_sum.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Copyright (C) 2003 Glenn L. McGrath
4  *  Copyright (C) 2003-2004 Erik Andersen
5  *
6  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
7  */
8
9 #include <fcntl.h>
10 #include <limits.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 #include "busybox.h"
18
19 typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
20
21 #define FLAG_SILENT     1
22 #define FLAG_CHECK      2
23 #define FLAG_WARN       4
24
25 /* This might be useful elsewhere */
26 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
27                                                                           unsigned char hash_length)
28 {
29         int x, len, max;
30         unsigned char *hex_value;
31
32         max = (hash_length * 2) + 2;
33         hex_value = xmalloc(max);
34         for (x = len = 0; x < hash_length; x++) {
35                 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
36         }
37         return (hex_value);
38 }
39
40 static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
41 {
42         int src_fd, hash_len, count;
43         union _ctx_ {
44                 sha1_ctx_t sha1;
45                 md5_ctx_t md5;
46         } context;
47         uint8_t *hash_value = NULL;
48         RESERVE_CONFIG_UBUFFER(in_buf, 4096);
49         void (*update)(const void*, size_t, void*);
50         void (*final)(void*, void*);
51
52         if (strcmp(filename, "-") == 0) {
53                 src_fd = STDIN_FILENO;
54         } else if(0 > (src_fd = open(filename, O_RDONLY))) {
55                 bb_perror_msg("%s", filename);
56                 return NULL;
57         }
58
59         /* figure specific hash algorithims */
60         if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
61                 md5_begin(&context.md5);
62                 update = (void (*)(const void*, size_t, void*))md5_hash;
63                 final = (void (*)(void*, void*))md5_end;
64                 hash_len = 16;
65         } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
66                 sha1_begin(&context.sha1);
67                 update = (void (*)(const void*, size_t, void*))sha1_hash;
68                 final = (void (*)(void*, void*))sha1_end;
69                 hash_len = 20;
70         } else {
71                 bb_error_msg_and_die("algorithm not supported");
72         }
73
74         while (0 < (count = read(src_fd, in_buf, 4096))) {
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