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