Turn defconfig into what make allyesconfig is today. Turn allyesconfig
[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
19 #define FLAG_SILENT     1
20 #define FLAG_CHECK      2
21 #define FLAG_WARN       4
22
23 /* This might be useful elsewhere */
24 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
25                                                                           unsigned char hash_length)
26 {
27         int x, len, max;
28         unsigned char *hex_value;
29
30         max = (hash_length * 2) + 2;
31         hex_value = xmalloc(max);
32         for (x = len = 0; x < hash_length; x++) {
33                 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
34         }
35         return (hex_value);
36 }
37
38 static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
39 {
40         int src_fd = strcmp(filename, "-") == 0 ? STDIN_FILENO :
41                 open(filename, O_RDONLY);
42         if (src_fd == -1) {
43                 bb_perror_msg("%s", filename);
44                 return NULL;
45         } else {
46                 uint8_t *hash_value;
47                 RESERVE_CONFIG_UBUFFER(hash_value_bin, 20);
48                 hash_value = hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2 ?
49                         hash_bin_to_hex(hash_value_bin, hash_algo == HASH_MD5 ? 16 : 20) :
50                         NULL;
51                 RELEASE_CONFIG_BUFFER(hash_value_bin);
52                 close(src_fd);
53                 return hash_value;
54         }
55 }
56
57 /* This could become a common function for md5 as well, by using md5_stream */
58 static int hash_files(int argc, char **argv, const uint8_t hash_algo)
59 {
60         int return_value = EXIT_SUCCESS;
61         uint8_t *hash_value;
62
63 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
64         unsigned int flags;
65
66         flags = bb_getopt_ulflags(argc, argv, "scw");
67 #endif
68
69 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
70         if (!(flags & FLAG_CHECK)) {
71                 if (flags & FLAG_SILENT) {
72                         bb_error_msg_and_die
73                                 ("the -s option is meaningful only when verifying checksums");
74                 } else if (flags & FLAG_WARN) {
75                         bb_error_msg_and_die
76                                 ("the -w option is meaningful only when verifying checksums");
77                 }
78         }
79 #endif
80
81         if (argc == optind) {
82                 argv[argc++] = "-";
83         }
84 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
85         if (flags & FLAG_CHECK) {
86                 FILE *pre_computed_stream;
87                 int count_total = 0;
88                 int count_failed = 0;
89                 char *file_ptr = argv[optind];
90                 char *line;
91
92                 if (optind + 1 != argc) {
93                         bb_error_msg_and_die
94                                 ("only one argument may be specified when using -c");
95                 }
96
97                 if (strcmp(file_ptr, "-") == 0) {
98                         pre_computed_stream = stdin;
99                 } else {
100                         pre_computed_stream = bb_xfopen(file_ptr, "r");
101                 }
102
103                 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
104                         char *filename_ptr;
105
106                         count_total++;
107                         filename_ptr = strstr(line, "  ");
108                         if (filename_ptr == NULL) {
109                                 if (flags & FLAG_WARN) {
110                                         bb_error_msg("Invalid format");
111                                 }
112                                 count_failed++;
113                                 return_value = EXIT_FAILURE;
114                                 free(line);
115                                 continue;
116                         }
117                         *filename_ptr = '\0';
118                         filename_ptr += 2;
119
120                         hash_value = hash_file(filename_ptr, hash_algo);
121
122                         if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
123                                 if (!(flags & FLAG_SILENT))
124                                         printf("%s: OK\n", filename_ptr);
125                         } else {
126                                 if (!(flags & FLAG_SILENT))
127                                         printf("%s: FAILED\n", filename_ptr);
128                                 count_failed++;
129                                 return_value = EXIT_FAILURE;
130                         }
131                         /* possible free(NULL) */
132                         free(hash_value);
133                         free(line);
134                 }
135                 if (count_failed && !(flags & FLAG_SILENT)) {
136                         bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
137                                                  count_failed, count_total);
138                 }
139                 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
140                         bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
141                 }
142         } else
143 #endif
144         {
145                 while (optind < argc) {
146                         char *file_ptr = argv[optind++];
147
148                         hash_value = hash_file(file_ptr, hash_algo);
149                         if (hash_value == NULL) {
150                                 return_value = EXIT_FAILURE;
151                         } else {
152                                 printf("%s  %s\n", hash_value, file_ptr);
153                                 free(hash_value);
154                         }
155                 }
156         }
157         return (return_value);
158 }
159
160 #ifdef CONFIG_MD5SUM
161 int md5sum_main(int argc, char **argv)
162 {
163         return(hash_files(argc, argv, HASH_MD5));
164 }
165 #endif
166
167 #ifdef CONFIG_SHA1SUM
168 int sha1sum_main(int argc, char **argv)
169 {
170         return(hash_files(argc, argv, HASH_SHA1));
171 }
172 #endif