summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-11-10 04:33:55 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-11-10 04:33:55 +0000
commitd2a897aab084b433231aab5f6908a79cba67dc5d (patch)
tree8a0b113bd44bc2be639e070b50668e4ad69091f2 /coreutils
parent0bdf41ad7e81451bc829365e0d9a676dda8145f9 (diff)
downloadbusybox-w32-d2a897aab084b433231aab5f6908a79cba67dc5d.tar.gz
busybox-w32-d2a897aab084b433231aab5f6908a79cba67dc5d.tar.bz2
busybox-w32-d2a897aab084b433231aab5f6908a79cba67dc5d.zip
Merge common parts of sha1sum and md5sum, which is everything except the
algorithms. Move algorithms to hash_fd and make them available via a common function.
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/Config.in19
-rw-r--r--coreutils/Makefile.in4
-rw-r--r--coreutils/md5_sha1_sum.c195
3 files changed, 208 insertions, 10 deletions
diff --git a/coreutils/Config.in b/coreutils/Config.in
index 90f681c04..a388cd9e8 100644
--- a/coreutils/Config.in
+++ b/coreutils/Config.in
@@ -373,14 +373,6 @@ config CONFIG_SHA1SUM
373 help 373 help
374 Compute and check SHA1 message digest 374 Compute and check SHA1 message digest
375 375
376config CONFIG_FEATURE_SHA1SUM_CHECK
377 bool " Enable -c and -w options"
378 default n
379 depends on CONFIG_SHA1SUM
380 help
381 Enabling the -c and -w options allows files to be checked
382 against pre-calculated hash values.
383
384config CONFIG_SLEEP 376config CONFIG_SLEEP
385 bool "sleep (single integer arg with no suffix)" 377 bool "sleep (single integer arg with no suffix)"
386 default n 378 default n
@@ -591,4 +583,15 @@ config CONFIG_FEATURE_HUMAN_READABLE
591 help 583 help
592 Allow df, du, and ls to have human readable output. 584 Allow df, du, and ls to have human readable output.
593 585
586comment "Common options for md5sum, sha1sum"
587 depends on CONFIG_MD5SUM || CONFIG_SHA1SUM
588
589config CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
590 bool " Enable -c and -w options"
591 default n
592 depends on CONFIG_MD5SUM || CONFIG_SHA1SUM
593 help
594 Enabling the -c and -w options allows files to be checked
595 against pre-calculated hash values.
596
594endmenu 597endmenu
diff --git a/coreutils/Makefile.in b/coreutils/Makefile.in
index b672f08f7..c7ba57e04 100644
--- a/coreutils/Makefile.in
+++ b/coreutils/Makefile.in
@@ -52,7 +52,7 @@ COREUTILS-$(CONFIG_LENGTH) += length.o
52COREUTILS-$(CONFIG_LN) += ln.o 52COREUTILS-$(CONFIG_LN) += ln.o
53COREUTILS-$(CONFIG_LOGNAME) += logname.o 53COREUTILS-$(CONFIG_LOGNAME) += logname.o
54COREUTILS-$(CONFIG_LS) += ls.o 54COREUTILS-$(CONFIG_LS) += ls.o
55COREUTILS-$(CONFIG_MD5SUM) += md5sum.o 55COREUTILS-$(CONFIG_MD5SUM) += md5_sha1_sum.o
56COREUTILS-$(CONFIG_MKDIR) += mkdir.o 56COREUTILS-$(CONFIG_MKDIR) += mkdir.o
57COREUTILS-$(CONFIG_MKFIFO) += mkfifo.o 57COREUTILS-$(CONFIG_MKFIFO) += mkfifo.o
58COREUTILS-$(CONFIG_MKNOD) += mknod.o 58COREUTILS-$(CONFIG_MKNOD) += mknod.o
@@ -63,7 +63,7 @@ COREUTILS-$(CONFIG_PWD) += pwd.o
63COREUTILS-$(CONFIG_REALPATH) += realpath.o 63COREUTILS-$(CONFIG_REALPATH) += realpath.o
64COREUTILS-$(CONFIG_RM) += rm.o 64COREUTILS-$(CONFIG_RM) += rm.o
65COREUTILS-$(CONFIG_RMDIR) += rmdir.o 65COREUTILS-$(CONFIG_RMDIR) += rmdir.o
66COREUTILS-$(CONFIG_SHA1SUM) += sha1sum.o 66COREUTILS-$(CONFIG_SHA1SUM) += md5_sha1_sum.o
67COREUTILS-$(CONFIG_SLEEP) += sleep.o 67COREUTILS-$(CONFIG_SLEEP) += sleep.o
68COREUTILS-$(CONFIG_SORT) += sort.o 68COREUTILS-$(CONFIG_SORT) += sort.o
69COREUTILS-$(CONFIG_STTY) += stty.o 69COREUTILS-$(CONFIG_STTY) += stty.o
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
new file mode 100644
index 000000000..f7c44c850
--- /dev/null
+++ b/coreutils/md5_sha1_sum.c
@@ -0,0 +1,195 @@
1/*
2 * Copyright (C) 2003 Glenn L. McGrath
3 * Copyright (C) 2003 Erik Andersen
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include <fcntl.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "busybox.h"
29
30
31#define FLAG_SILENT 1
32#define FLAG_CHECK 2
33#define FLAG_WARN 4
34
35/* This might be usefull elsewhere */
36static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
37 unsigned char hash_length)
38{
39 int x, len, max;
40 unsigned char *hex_value;
41
42 max = (hash_length * 2) + 2;
43 hex_value = xmalloc(max);
44 for (x = len = 0; x < hash_length; x++) {
45 len += snprintf(hex_value + len, max - len, "%02x", hash_value[x]);
46 }
47 return (hex_value);
48}
49
50static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
51{
52 uint8_t *hash_value_bin;
53 uint8_t *hash_value = NULL;
54 uint8_t hash_length;
55 int src_fd;
56
57 if (strcmp(filename, "-") == 0) {
58 src_fd = fileno(stdin);
59 } else {
60 src_fd = open(filename, O_RDONLY);
61 }
62
63 if (hash_algo == HASH_MD5) {
64 hash_length = 16;
65 } else {
66 hash_length = 20;
67 }
68
69 hash_value_bin = xmalloc(hash_length);
70
71 if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
72 hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
73 } else {
74 bb_perror_msg("%s", filename);
75 }
76
77 close(src_fd);
78
79 return(hash_value);
80}
81
82/* This could become a common function for md5 as well, by using md5_stream */
83extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
84{
85 uint8_t *hash_value;
86 unsigned int flags;
87 int return_value = EXIT_SUCCESS;
88
89#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
90 flags = bb_getopt_ulflags(argc, argv, "scw");
91#else
92 flags = bb_getopt_ulflags(argc, argv, "s");
93#endif
94
95#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
96 if (!(flags & FLAG_CHECK)) {
97 if (flags & FLAG_SILENT) {
98 bb_error_msg_and_die
99 ("the -s option is meaningful only when verifying checksums");
100 } else if (flags & FLAG_WARN) {
101 bb_error_msg_and_die
102 ("the -w option is meaningful only when verifying checksums");
103 }
104 }
105#endif
106
107 if (argc == optind) {
108 argv[argc++] = "-";
109 }
110#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
111 if (flags & FLAG_CHECK) {
112 FILE *pre_computed_stream;
113 int count_total = 0;
114 int count_failed = 0;
115 unsigned 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 = bb_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 free(line);
139 continue;
140 }
141 *filename_ptr = '\0';
142 filename_ptr += 2;
143
144 hash_value = hash_file(filename_ptr, hash_algo);
145
146 if (hash_value && (strcmp(hash_value, line) == 0)) {
147 printf("%s: OK\n", filename_ptr);
148 } else {
149 printf("%s: FAILED\n", filename_ptr);
150 count_failed++;
151 }
152 /* possible free(NULL) */
153 free(hash_value);
154 free(line);
155 }
156 if (count_failed) {
157 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
158 count_failed, count_total);
159 }
160 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
161 bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
162 }
163 } else
164#endif
165 while (optind < argc) {
166 unsigned char *file_ptr = argv[optind];
167
168 optind++;
169
170 hash_value = hash_file(file_ptr, hash_algo);
171 if (hash_value == NULL) {
172 return_value++;
173 }
174 else if (!flags & FLAG_SILENT) {
175 printf("%s %s\n", hash_value, file_ptr);
176 free(hash_value);
177 }
178 }
179
180 return (return_value);
181}
182
183#ifdef CONFIG_MD5SUM
184extern int md5sum_main(int argc, char **argv)
185{
186 return(hash_files(argc, argv, HASH_MD5));
187}
188#endif
189
190#ifdef CONFIG_SHA1SUM
191extern int sha1sum_main(int argc, char **argv)
192{
193 return(hash_files(argc, argv, HASH_SHA1));
194}
195#endif