md5/sha1sum: code shrink

function                                             old     new   delta
hash_file                                            357     279     -78

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-10-24 14:54:53 +02:00
parent ba04337e1f
commit 4b63e16d20

View File

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