[git commit master 1/1] md5/sha1sum: code shrink

Denys Vlasenko vda.linux at googlemail.com
Sun Oct 24 12:54:53 UTC 2010


commit: http://git.busybox.net/busybox/commit/?id=4b63e16d20f2c89b39e26a10cb1cdbcbf079df89
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
hash_file                                            357     279     -78

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/md5_sha1_sum.c |   41 +++++++++++++++++++++--------------------
 1 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index e79210c..d3d294d 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -10,13 +10,13 @@
 
 /* This is a NOEXEC applet. Be very careful! */
 
-typedef enum {
+enum {
 	/* 4th letter of applet_name is... */
 	HASH_MD5 = 's', /* "md5>s<um" */
 	HASH_SHA1 = '1',
 	HASH_SHA256 = '2',
 	HASH_SHA512 = '5',
-} hash_algo_t;
+};
 
 #define FLAG_SILENT	1
 #define FLAG_CHECK	2
@@ -32,7 +32,7 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
 	return (unsigned char *)hex_value;
 }
 
-static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
+static uint8_t *hash_file(const char *filename)
 {
 	int src_fd, hash_len, count;
 	union _ctx_ {
@@ -45,13 +45,15 @@ static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
 	RESERVE_CONFIG_UBUFFER(in_buf, 4096);
 	void FAST_FUNC (*update)(void*, const void*, size_t);
 	void FAST_FUNC (*final)(void*, void*);
-	hash_algo_t hash_algo = applet_name[3];
+	char hash_algo;
 
 	src_fd = open_or_warn_stdin(filename);
 	if (src_fd < 0) {
 		return NULL;
 	}
 
+	hash_algo = applet_name[3];
+
 	/* figure specific hash algorithims */
 	if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
 		md5_begin(&context.md5);
@@ -74,10 +76,10 @@ static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
 		final = (void*)sha512_end;
 		hash_len = 64;
 	} else {
-		bb_error_msg_and_die("algorithm not supported");
+		xfunc_die(); /* can't reach this */
 	}
 
-	while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
+	while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
 		update(&context, in_buf, count);
 	}
 
@@ -99,27 +101,26 @@ int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
 {
 	int return_value = EXIT_SUCCESS;
-	uint8_t *hash_value;
 	unsigned flags;
-	/*hash_algo_t hash_algo = applet_name[3];*/
 
 	if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
 		/* -b "binary", -t "text" are ignored (shaNNNsum compat) */
 		flags = getopt32(argv, "scwbt");
+		argv += optind;
+		//argc -= optind;
+	} else {
+		argv += 1;
+		//argc -= 1;
 	}
-	else optind = 1;
-	argv += optind;
-	//argc -= optind;
 	if (!*argv)
 		*--argv = (char*)"-";
 
 	if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
 		if (flags & FLAG_SILENT) {
-			bb_error_msg_and_die
-				("-%c is meaningful only when verifying checksums", 's');
-		} else if (flags & FLAG_WARN) {
-			bb_error_msg_and_die
-				("-%c is meaningful only when verifying checksums", 'w');
+			bb_error_msg_and_die("-%c is meaningful only with -c", 's');
+		}
+		if (flags & FLAG_WARN) {
+			bb_error_msg_and_die("-%c is meaningful only with -c", 'w');
 		}
 	}
 
@@ -130,13 +131,13 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
 		char *line;
 
 		if (argv[1]) {
-			bb_error_msg_and_die
-				("only one argument may be specified when using -c");
+			bb_error_msg_and_die("only one argument may be specified with -c");
 		}
 
 		pre_computed_stream = xfopen_stdin(argv[0]);
 
 		while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
+			uint8_t *hash_value;
 			char *filename_ptr;
 
 			count_total++;
@@ -157,7 +158,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
 			*filename_ptr = '\0';
 			filename_ptr += 2;
 
-			hash_value = hash_file(filename_ptr /*, hash_algo*/);
+			hash_value = hash_file(filename_ptr);
 
 			if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
 				if (!(flags & FLAG_SILENT))
@@ -183,7 +184,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
 		*/
 	} else {
 		do {
-			hash_value = hash_file(*argv/*, hash_algo*/);
+			uint8_t *hash_value = hash_file(*argv);
 			if (hash_value == NULL) {
 				return_value = EXIT_FAILURE;
 			} else {
-- 
1.7.1



More information about the busybox-cvs mailing list