aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-10-24 14:54:53 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-10-24 14:54:53 +0200
commit4b63e16d20f2c89b39e26a10cb1cdbcbf079df89 (patch)
tree378a5949c128df740fc03eb2c24e5f6d50e14545
parentba04337e1f2d7ff2731959a59b64bc63953e77f4 (diff)
downloadbusybox-w32-4b63e16d20f2c89b39e26a10cb1cdbcbf079df89.tar.gz
busybox-w32-4b63e16d20f2c89b39e26a10cb1cdbcbf079df89.tar.bz2
busybox-w32-4b63e16d20f2c89b39e26a10cb1cdbcbf079df89.zip
md5/sha1sum: code shrink
function old new delta hash_file 357 279 -78 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/md5_sha1_sum.c41
1 files changed, 21 insertions, 20 deletions
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index e79210c0d..d3d294de9 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -10,13 +10,13 @@
10 10
11/* This is a NOEXEC applet. Be very careful! */ 11/* This is a NOEXEC applet. Be very careful! */
12 12
13typedef enum { 13enum {
14 /* 4th letter of applet_name is... */ 14 /* 4th letter of applet_name is... */
15 HASH_MD5 = 's', /* "md5>s<um" */ 15 HASH_MD5 = 's', /* "md5>s<um" */
16 HASH_SHA1 = '1', 16 HASH_SHA1 = '1',
17 HASH_SHA256 = '2', 17 HASH_SHA256 = '2',
18 HASH_SHA512 = '5', 18 HASH_SHA512 = '5',
19} hash_algo_t; 19};
20 20
21#define FLAG_SILENT 1 21#define FLAG_SILENT 1
22#define FLAG_CHECK 2 22#define FLAG_CHECK 2
@@ -32,7 +32,7 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
32 return (unsigned char *)hex_value; 32 return (unsigned char *)hex_value;
33} 33}
34 34
35static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/) 35static uint8_t *hash_file(const char *filename)
36{ 36{
37 int src_fd, hash_len, count; 37 int src_fd, hash_len, count;
38 union _ctx_ { 38 union _ctx_ {
@@ -45,13 +45,15 @@ static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
45 RESERVE_CONFIG_UBUFFER(in_buf, 4096); 45 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
46 void FAST_FUNC (*update)(void*, const void*, size_t); 46 void FAST_FUNC (*update)(void*, const void*, size_t);
47 void FAST_FUNC (*final)(void*, void*); 47 void FAST_FUNC (*final)(void*, void*);
48 hash_algo_t hash_algo = applet_name[3]; 48 char hash_algo;
49 49
50 src_fd = open_or_warn_stdin(filename); 50 src_fd = open_or_warn_stdin(filename);
51 if (src_fd < 0) { 51 if (src_fd < 0) {
52 return NULL; 52 return NULL;
53 } 53 }
54 54
55 hash_algo = applet_name[3];
56
55 /* figure specific hash algorithims */ 57 /* figure specific hash algorithims */
56 if (ENABLE_MD5SUM && hash_algo == HASH_MD5) { 58 if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
57 md5_begin(&context.md5); 59 md5_begin(&context.md5);
@@ -74,10 +76,10 @@ static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
74 final = (void*)sha512_end; 76 final = (void*)sha512_end;
75 hash_len = 64; 77 hash_len = 64;
76 } else { 78 } else {
77 bb_error_msg_and_die("algorithm not supported"); 79 xfunc_die(); /* can't reach this */
78 } 80 }
79 81
80 while (0 < (count = safe_read(src_fd, in_buf, 4096))) { 82 while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
81 update(&context, in_buf, count); 83 update(&context, in_buf, count);
82 } 84 }
83 85
@@ -99,27 +101,26 @@ int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
99int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) 101int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
100{ 102{
101 int return_value = EXIT_SUCCESS; 103 int return_value = EXIT_SUCCESS;
102 uint8_t *hash_value;
103 unsigned flags; 104 unsigned flags;
104 /*hash_algo_t hash_algo = applet_name[3];*/
105 105
106 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) { 106 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
107 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */ 107 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
108 flags = getopt32(argv, "scwbt"); 108 flags = getopt32(argv, "scwbt");
109 argv += optind;
110 //argc -= optind;
111 } else {
112 argv += 1;
113 //argc -= 1;
109 } 114 }
110 else optind = 1;
111 argv += optind;
112 //argc -= optind;
113 if (!*argv) 115 if (!*argv)
114 *--argv = (char*)"-"; 116 *--argv = (char*)"-";
115 117
116 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) { 118 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
117 if (flags & FLAG_SILENT) { 119 if (flags & FLAG_SILENT) {
118 bb_error_msg_and_die 120 bb_error_msg_and_die("-%c is meaningful only with -c", 's');
119 ("-%c is meaningful only when verifying checksums", 's'); 121 }
120 } else if (flags & FLAG_WARN) { 122 if (flags & FLAG_WARN) {
121 bb_error_msg_and_die 123 bb_error_msg_and_die("-%c is meaningful only with -c", 'w');
122 ("-%c is meaningful only when verifying checksums", 'w');
123 } 124 }
124 } 125 }
125 126
@@ -130,13 +131,13 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
130 char *line; 131 char *line;
131 132
132 if (argv[1]) { 133 if (argv[1]) {
133 bb_error_msg_and_die 134 bb_error_msg_and_die("only one argument may be specified with -c");
134 ("only one argument may be specified when using -c");
135 } 135 }
136 136
137 pre_computed_stream = xfopen_stdin(argv[0]); 137 pre_computed_stream = xfopen_stdin(argv[0]);
138 138
139 while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) { 139 while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
140 uint8_t *hash_value;
140 char *filename_ptr; 141 char *filename_ptr;
141 142
142 count_total++; 143 count_total++;
@@ -157,7 +158,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
157 *filename_ptr = '\0'; 158 *filename_ptr = '\0';
158 filename_ptr += 2; 159 filename_ptr += 2;
159 160
160 hash_value = hash_file(filename_ptr /*, hash_algo*/); 161 hash_value = hash_file(filename_ptr);
161 162
162 if (hash_value && (strcmp((char*)hash_value, line) == 0)) { 163 if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
163 if (!(flags & FLAG_SILENT)) 164 if (!(flags & FLAG_SILENT))
@@ -183,7 +184,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
183 */ 184 */
184 } else { 185 } else {
185 do { 186 do {
186 hash_value = hash_file(*argv/*, hash_algo*/); 187 uint8_t *hash_value = hash_file(*argv);
187 if (hash_value == NULL) { 188 if (hash_value == NULL) {
188 return_value = EXIT_FAILURE; 189 return_value = EXIT_FAILURE;
189 } else { 190 } else {