Teach md5sum and sha1sum to work the way other applets do so I don't have to
[platform/upstream/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 ?
91                                                                         (**argv=='m' ? HASH_MD5 : HASH_SHA1)
92                                                                 : HASH_MD5)
93                                                         : HASH_SHA1;
94
95         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
96                 flags = bb_getopt_ulflags(argc, argv, "scw");
97         else optind = 1;
98
99         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
100                 if (flags & FLAG_SILENT) {
101                         bb_error_msg_and_die
102                                 ("the -s option is meaningful only when verifying checksums");
103                 } else if (flags & FLAG_WARN) {
104                         bb_error_msg_and_die
105                                 ("the -w option is meaningful only when verifying checksums");
106                 }
107         }
108
109         if (argc == optind) {
110                 argv[argc++] = "-";
111         }
112
113         if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) {
114                 FILE *pre_computed_stream;
115                 int count_total = 0;
116                 int count_failed = 0;
117                 char *file_ptr = argv[optind];
118                 char *line;
119
120                 if (optind + 1 != argc) {
121                         bb_error_msg_and_die
122                                 ("only one argument may be specified when using -c");
123                 }
124
125                 if (strcmp(file_ptr, "-") == 0) {
126                         pre_computed_stream = stdin;
127                 } else {
128                         pre_computed_stream = xfopen(file_ptr, "r");
129                 }
130
131                 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
132                         char *filename_ptr;
133
134                         count_total++;
135                         filename_ptr = strstr(line, "  ");
136                         if (filename_ptr == NULL) {
137                                 if (flags & FLAG_WARN) {
138                                         bb_error_msg("Invalid format");
139                                 }
140                                 count_failed++;
141                                 return_value = EXIT_FAILURE;
142                                 free(line);
143                                 continue;
144                         }
145                         *filename_ptr = '\0';
146                         filename_ptr += 2;
147
148                         hash_value = hash_file(filename_ptr, hash_algo);
149
150                         if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
151                                 if (!(flags & FLAG_SILENT))
152                                         printf("%s: OK\n", filename_ptr);
153                         } else {
154                                 if (!(flags & FLAG_SILENT))
155                                         printf("%s: FAILED\n", filename_ptr);
156                                 count_failed++;
157                                 return_value = EXIT_FAILURE;
158                         }
159                         /* possible free(NULL) */
160                         free(hash_value);
161                         free(line);
162                 }
163                 if (count_failed && !(flags & FLAG_SILENT)) {
164                         bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
165                                                  count_failed, count_total);
166                 }
167                 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
168                         bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
169                 }
170         } else {
171                 while (optind < argc) {
172                         char *file_ptr = argv[optind++];
173
174                         hash_value = hash_file(file_ptr, hash_algo);
175                         if (hash_value == NULL) {
176                                 return_value = EXIT_FAILURE;
177                         } else {
178                                 printf("%s  %s\n", hash_value, file_ptr);
179                                 free(hash_value);
180                         }
181                 }
182         }
183         return (return_value);
184 }