aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-02-21 06:44:43 +0000
committerRob Landley <rob@landley.net>2006-02-21 06:44:43 +0000
commit5cf7c2df668d25c41a05670edd08558226f0bfdf (patch)
tree3c3a7b2aa31dd4105f15f435a5894c91c941686e /coreutils
parenta7e3d0520856db27744b4a33e786123d44bf5b2a (diff)
downloadbusybox-w32-5cf7c2df668d25c41a05670edd08558226f0bfdf.tar.gz
busybox-w32-5cf7c2df668d25c41a05670edd08558226f0bfdf.tar.bz2
busybox-w32-5cf7c2df668d25c41a05670edd08558226f0bfdf.zip
Patch from Devin Bayer to split up hash_fd.c into md5.c and sha1.c. (I tweaked
md5_sha1_sum.c to convert some #ifdef CONFIG to if(ENABLE).)
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/Config.in15
-rw-r--r--coreutils/md5_sha1_sum.c77
2 files changed, 53 insertions, 39 deletions
diff --git a/coreutils/Config.in b/coreutils/Config.in
index ffdc11ced..cd45cc077 100644
--- a/coreutils/Config.in
+++ b/coreutils/Config.in
@@ -315,21 +315,6 @@ config CONFIG_MD5SUM
315 help 315 help
316 md5sum is used to print or check MD5 checksums. 316 md5sum is used to print or check MD5 checksums.
317 317
318config CONFIG_MD5SUM_SIZE_VS_SPEED
319 int " Trade Bytes for Speed"
320 default 2
321 range 0 3
322 depends on CONFIG_MD5SUM
323 help
324 Trade binary size versus speed for the md5sum algorithm.
325 Approximate values running uClibc and hashing
326 linux-2.4.4.tar.bz2 were:
327 user times (sec) text size (386)
328 0 (fastest) 1.1 6144
329 1 1.4 5392
330 2 3.0 5088
331 3 (smallest) 5.1 4912
332
333config CONFIG_MKDIR 318config CONFIG_MKDIR
334 bool "mkdir" 319 bool "mkdir"
335 default n 320 default n
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index 287c2f524..626dcee56 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -15,6 +15,7 @@
15 15
16#include "busybox.h" 16#include "busybox.h"
17 17
18typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
18 19
19#define FLAG_SILENT 1 20#define FLAG_SILENT 1
20#define FLAG_CHECK 2 21#define FLAG_CHECK 2
@@ -35,39 +36,70 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
35 return (hex_value); 36 return (hex_value);
36} 37}
37 38
38static uint8_t *hash_file(const char *filename, uint8_t hash_algo) 39static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
39{ 40{
40 int src_fd = strcmp(filename, "-") == 0 ? STDIN_FILENO : 41 int src_fd, hash_len, count;
41 open(filename, O_RDONLY); 42 union _ctx_ {
42 if (src_fd == -1) { 43 sha1_ctx_t sha1;
44 md5_ctx_t md5;
45 } context;
46 uint8_t *hash_value = NULL;
47 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
48 void (*update)(const void*, size_t, void*);
49 void (*final)(void*, void*);
50
51 if(strcmp(filename, "-") == 0) {
52 src_fd = STDIN_FILENO;
53 } else if(0 > (src_fd = open(filename, O_RDONLY))) {
43 bb_perror_msg("%s", filename); 54 bb_perror_msg("%s", filename);
44 return NULL; 55 return NULL;
56 }
57
58 // figure specific hash algorithims
59 if(ENABLE_MD5SUM && hash_algo==HASH_MD5) {
60 md5_begin(&context.md5);
61 update = (void (*)(const void*, size_t, void*))md5_hash;
62 final = (void (*)(void*, void*))md5_end;
63 hash_len = 16;
64 } else if(ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
65 sha1_begin(&context.sha1);
66 update = (void (*)(const void*, size_t, void*))sha1_hash;
67 final = (void (*)(void*, void*))sha1_end;
68 hash_len = 20;
45 } else { 69 } else {
46 uint8_t *hash_value; 70 bb_error_msg_and_die("algotithm not supported");
47 RESERVE_CONFIG_UBUFFER(hash_value_bin, 20); 71 }
48 hash_value = hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2 ? 72
49 hash_bin_to_hex(hash_value_bin, hash_algo == HASH_MD5 ? 16 : 20) : 73
50 NULL; 74 while(0 < (count = read(src_fd, in_buf, sizeof in_buf))) {
51 RELEASE_CONFIG_BUFFER(hash_value_bin); 75 update(in_buf, count, &context);
76 }
77
78 if(count == 0) {
79 final(in_buf, &context);
80 hash_value = hash_bin_to_hex(in_buf, hash_len);
81 }
82
83 RELEASE_CONFIG_BUFFER(in_buf);
84
85 if(src_fd != STDIN_FILENO) {
52 close(src_fd); 86 close(src_fd);
53 return hash_value;
54 } 87 }
88
89 return hash_value;
55} 90}
56 91
57/* This could become a common function for md5 as well, by using md5_stream */ 92/* This could become a common function for md5 as well, by using md5_stream */
58static int hash_files(int argc, char **argv, const uint8_t hash_algo) 93static int hash_files(int argc, char **argv, hash_algo_t hash_algo)
59{ 94{
60 int return_value = EXIT_SUCCESS; 95 int return_value = EXIT_SUCCESS;
61 uint8_t *hash_value; 96 uint8_t *hash_value;
62
63#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
64 unsigned int flags; 97 unsigned int flags;
65 98
66 flags = bb_getopt_ulflags(argc, argv, "scw"); 99 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
67#endif 100 flags = bb_getopt_ulflags(argc, argv, "scw");
68 101
69#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK 102 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
70 if (!(flags & FLAG_CHECK)) {
71 if (flags & FLAG_SILENT) { 103 if (flags & FLAG_SILENT) {
72 bb_error_msg_and_die 104 bb_error_msg_and_die
73 ("the -s option is meaningful only when verifying checksums"); 105 ("the -s option is meaningful only when verifying checksums");
@@ -76,13 +108,12 @@ static int hash_files(int argc, char **argv, const uint8_t hash_algo)
76 ("the -w option is meaningful only when verifying checksums"); 108 ("the -w option is meaningful only when verifying checksums");
77 } 109 }
78 } 110 }
79#endif
80 111
81 if (argc == optind) { 112 if (argc == optind) {
82 argv[argc++] = "-"; 113 argv[argc++] = "-";
83 } 114 }
84#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK 115
85 if (flags & FLAG_CHECK) { 116 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) {
86 FILE *pre_computed_stream; 117 FILE *pre_computed_stream;
87 int count_total = 0; 118 int count_total = 0;
88 int count_failed = 0; 119 int count_failed = 0;
@@ -139,9 +170,7 @@ static int hash_files(int argc, char **argv, const uint8_t hash_algo)
139 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) { 170 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
140 bb_perror_msg_and_die("Couldnt close file %s", file_ptr); 171 bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
141 } 172 }
142 } else 173 } else {
143#endif
144 {
145 while (optind < argc) { 174 while (optind < argc) {
146 char *file_ptr = argv[optind++]; 175 char *file_ptr = argv[optind++];
147 176