- add libbb function str_tolower to convert a string to lowercase.
[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 "busybox.h"
10
11 typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
12
13 #define FLAG_SILENT     1
14 #define FLAG_CHECK      2
15 #define FLAG_WARN       4
16
17 /* This might be useful elsewhere */
18 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
19                                 unsigned hash_length)
20 {
21         /* xzalloc zero-terminates */
22         char *hex_value = xzalloc((hash_length * 2) + 1);
23         bin2hex(hex_value, (char*)hash_value, hash_length);
24         return hex_value;
25 }
26
27 static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
28 {
29         int src_fd, hash_len, count;
30         union _ctx_ {
31                 sha1_ctx_t sha1;
32                 md5_ctx_t md5;
33         } context;
34         uint8_t *hash_value = NULL;
35         RESERVE_CONFIG_UBUFFER(in_buf, 4096);
36         void (*update)(const void*, size_t, void*);
37         void (*final)(void*, void*);
38
39         src_fd = STDIN_FILENO;
40         if (NOT_LONE_DASH(filename)) {
41                 src_fd = open(filename, O_RDONLY);
42                 if (src_fd < 0) {
43                         bb_perror_msg("%s", filename);
44                         return NULL;
45                 }
46         }
47
48         /* figure specific hash algorithims */
49         if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
50                 md5_begin(&context.md5);
51                 update = (void (*)(const void*, size_t, void*))md5_hash;
52                 final = (void (*)(void*, void*))md5_end;
53                 hash_len = 16;
54         } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
55                 sha1_begin(&context.sha1);
56                 update = (void (*)(const void*, size_t, void*))sha1_hash;
57                 final = (void (*)(void*, void*))sha1_end;
58                 hash_len = 20;
59         } else {
60                 bb_error_msg_and_die("algorithm not supported");
61         }
62
63         while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
64                 update(in_buf, count, &context);
65         }
66
67         if (count == 0) {
68                 final(in_buf, &context);
69                 hash_value = hash_bin_to_hex(in_buf, hash_len);
70         }
71
72         RELEASE_CONFIG_BUFFER(in_buf);
73
74         if (src_fd != STDIN_FILENO) {
75                 close(src_fd);
76         }
77
78         return hash_value;
79 }
80
81 int md5_sha1_sum_main(int argc, char **argv);
82 int md5_sha1_sum_main(int argc, char **argv)
83 {
84         int return_value = EXIT_SUCCESS;
85         uint8_t *hash_value;
86         unsigned flags;
87         hash_algo_t hash_algo = ENABLE_MD5SUM
88                 ? (ENABLE_SHA1SUM ? (**argv=='m' ? HASH_MD5 : HASH_SHA1) : HASH_MD5)
89                 : HASH_SHA1;
90
91         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
92                 flags = getopt32(argc, argv, "scw");
93         else optind = 1;
94
95         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
96                 if (flags & FLAG_SILENT) {
97                         bb_error_msg_and_die
98                                 ("-%c is meaningful only when verifying checksums", 's');
99                 } else if (flags & FLAG_WARN) {
100                         bb_error_msg_and_die
101                                 ("-%c is meaningful only when verifying checksums", 'w');
102                 }
103         }
104
105         if (argc == optind) {
106                 argv[argc++] = (char*)"-";
107         }
108
109         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
110                 FILE *pre_computed_stream;
111                 int count_total = 0;
112                 int count_failed = 0;
113                 char *file_ptr = argv[optind];
114                 char *line;
115
116                 if (optind + 1 != argc) {
117                         bb_error_msg_and_die
118                                 ("only one argument may be specified when using -c");
119                 }
120
121                 pre_computed_stream = stdin;
122                 if (NOT_LONE_DASH(file_ptr)) {
123                         pre_computed_stream = xfopen(file_ptr, "r");
124                 }
125
126                 while ((line = xmalloc_getline(pre_computed_stream)) != NULL) {
127                         char *filename_ptr;
128
129                         count_total++;
130                         filename_ptr = strstr(line, "  ");
131                         /* handle format for binary checksums */
132                         if (filename_ptr == NULL) {
133                                 filename_ptr = strstr(line, " *");
134                         }
135                         if (filename_ptr == NULL) {
136                                 if (flags & FLAG_WARN) {
137                                         bb_error_msg("invalid format");
138                                 }
139                                 count_failed++;
140                                 return_value = EXIT_FAILURE;
141                                 free(line);
142                                 continue;
143                         }
144                         *filename_ptr = '\0';
145                         filename_ptr += 2;
146
147                         hash_value = hash_file(filename_ptr, hash_algo);
148
149                         if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
150                                 if (!(flags & FLAG_SILENT))
151                                         printf("%s: OK\n", filename_ptr);
152                         } else {
153                                 if (!(flags & FLAG_SILENT))
154                                         printf("%s: FAILED\n", filename_ptr);
155                                 count_failed++;
156                                 return_value = EXIT_FAILURE;
157                         }
158                         /* possible free(NULL) */
159                         free(hash_value);
160                         free(line);
161                 }
162                 if (count_failed && !(flags & FLAG_SILENT)) {
163                         bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
164                                                  count_failed, count_total);
165                 }
166                 /*
167                 if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
168                         bb_perror_msg_and_die("cannot close file %s", file_ptr);
169                 }
170                 */
171         } else {
172                 while (optind < argc) {
173                         char *file_ptr = argv[optind++];
174
175                         hash_value = hash_file(file_ptr, hash_algo);
176                         if (hash_value == NULL) {
177                                 return_value = EXIT_FAILURE;
178                         } else {
179                                 printf("%s  %s\n", hash_value, file_ptr);
180                                 free(hash_value);
181                         }
182                 }
183         }
184         return return_value;
185 }