Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/md5_sha1_sum.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/coreutils/md5_sha1_sum.c
r1765 r2725 4 4 * Copyright (C) 2003-2004 Erik Andersen 5 5 * 6 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.6 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 7 7 */ 8 8 9 9 #include "libbb.h" 10 10 11 typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t; 11 /* This is a NOEXEC applet. Be very careful! */ 12 12 13 #define FLAG_SILENT 1 14 #define FLAG_CHECK 2 15 #define FLAG_WARN 4 13 enum { 14 /* 4th letter of applet_name is... */ 15 HASH_MD5 = 's', /* "md5>s<um" */ 16 HASH_SHA1 = '1', 17 HASH_SHA256 = '2', 18 HASH_SHA512 = '5', 19 }; 20 21 #define FLAG_SILENT 1 22 #define FLAG_CHECK 2 23 #define FLAG_WARN 4 16 24 17 25 /* This might be useful elsewhere */ … … 22 30 char *hex_value = xzalloc((hash_length * 2) + 1); 23 31 bin2hex(hex_value, (char*)hash_value, hash_length); 24 return hex_value;32 return (unsigned char *)hex_value; 25 33 } 26 34 27 static uint8_t *hash_file(const char *filename , hash_algo_t hash_algo)35 static uint8_t *hash_file(const char *filename) 28 36 { 29 37 int src_fd, hash_len, count; 30 38 union _ctx_ { 39 sha512_ctx_t sha512; 40 sha256_ctx_t sha256; 31 41 sha1_ctx_t sha1; 32 42 md5_ctx_t md5; … … 34 44 uint8_t *hash_value = NULL; 35 45 RESERVE_CONFIG_UBUFFER(in_buf, 4096); 36 void (*update)(const void*, size_t, void*); 37 void (*final)(void*, void*); 46 void FAST_FUNC (*update)(void*, const void*, size_t); 47 void FAST_FUNC (*final)(void*, void*); 48 char hash_algo; 38 49 39 src_fd = STDIN_FILENO; 40 if (NOT_LONE_DASH(filename)) { 41 src_fd = open_or_warn(filename, O_RDONLY); 42 if (src_fd < 0) { 43 return NULL; 44 } 50 src_fd = open_or_warn_stdin(filename); 51 if (src_fd < 0) { 52 return NULL; 45 53 } 46 54 55 hash_algo = applet_name[3]; 56 47 57 /* figure specific hash algorithims */ 48 if (ENABLE_MD5SUM && hash_algo ==HASH_MD5) {58 if (ENABLE_MD5SUM && hash_algo == HASH_MD5) { 49 59 md5_begin(&context.md5); 50 update = (void (*)(const void*, size_t, void*))md5_hash;51 final = (void (*)(void*, void*))md5_end;60 update = (void*)md5_hash; 61 final = (void*)md5_end; 52 62 hash_len = 16; 53 } else if (ENABLE_SHA1SUM && hash_algo ==HASH_SHA1) {63 } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) { 54 64 sha1_begin(&context.sha1); 55 update = (void (*)(const void*, size_t, void*))sha1_hash;56 final = (void (*)(void*, void*))sha1_end;65 update = (void*)sha1_hash; 66 final = (void*)sha1_end; 57 67 hash_len = 20; 68 } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) { 69 sha256_begin(&context.sha256); 70 update = (void*)sha256_hash; 71 final = (void*)sha256_end; 72 hash_len = 32; 73 } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) { 74 sha512_begin(&context.sha512); 75 update = (void*)sha512_hash; 76 final = (void*)sha512_end; 77 hash_len = 64; 58 78 } else { 59 bb_error_msg_and_die("algorithm not supported");79 xfunc_die(); /* can't reach this */ 60 80 } 61 81 62 while ( 0 < (count = safe_read(src_fd, in_buf, 4096))) {63 update( in_buf, count, &context);82 while ((count = safe_read(src_fd, in_buf, 4096)) > 0) { 83 update(&context, in_buf, count); 64 84 } 65 85 66 86 if (count == 0) { 67 final( in_buf, &context);87 final(&context, in_buf); 68 88 hash_value = hash_bin_to_hex(in_buf, hash_len); 69 89 } … … 78 98 } 79 99 80 int md5_sha1_sum_main(int argc, char **argv) ;81 int md5_sha1_sum_main(int argc , char **argv)100 int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 101 int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) 82 102 { 83 103 int return_value = EXIT_SUCCESS; 84 uint8_t *hash_value;85 104 unsigned flags; 86 hash_algo_t hash_algo = ENABLE_MD5SUM87 ? (ENABLE_SHA1SUM ? (applet_name[0] == 'm' ? HASH_MD5 : HASH_SHA1) : HASH_MD5)88 : HASH_SHA1;89 105 90 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) 91 flags = getopt32(argv, "scw"); 92 else optind = 1; 106 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) { 107 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */ 108 flags = getopt32(argv, "scwbt"); 109 argv += optind; 110 //argc -= optind; 111 } else { 112 argv += 1; 113 //argc -= 1; 114 } 115 if (!*argv) 116 *--argv = (char*)"-"; 93 117 94 118 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) { 95 119 if (flags & FLAG_SILENT) { 96 bb_error_msg_and_die 97 ("-%c is meaningful only when verifying checksums", 's'); 98 } else if (flags & FLAG_WARN) { 99 bb_error_msg_and_die 100 ("-%c is meaningful only when verifying checksums", 'w'); 120 bb_error_msg_and_die("-%c is meaningful only with -c", 's'); 101 121 } 102 } 103 104 if (argc == optind) { 105 argv[argc++] = (char*)"-"; 122 if (flags & FLAG_WARN) { 123 bb_error_msg_and_die("-%c is meaningful only with -c", 'w'); 124 } 106 125 } 107 126 … … 110 129 int count_total = 0; 111 130 int count_failed = 0; 112 char *file_ptr = argv[optind];113 131 char *line; 114 132 115 if (optind + 1 != argc) { 116 bb_error_msg_and_die 117 ("only one argument may be specified when using -c"); 133 if (argv[1]) { 134 bb_error_msg_and_die("only one argument may be specified with -c"); 118 135 } 119 136 120 pre_computed_stream = stdin; 121 if (NOT_LONE_DASH(file_ptr)) { 122 pre_computed_stream = xfopen(file_ptr, "r"); 123 } 137 pre_computed_stream = xfopen_stdin(argv[0]); 124 138 125 while ((line = xmalloc_getline(pre_computed_stream)) != NULL) { 139 while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) { 140 uint8_t *hash_value; 126 141 char *filename_ptr; 127 142 … … 144 159 filename_ptr += 2; 145 160 146 hash_value = hash_file(filename_ptr , hash_algo);161 hash_value = hash_file(filename_ptr); 147 162 148 163 if (hash_value && (strcmp((char*)hash_value, line) == 0)) { … … 165 180 /* 166 181 if (fclose_if_not_stdin(pre_computed_stream) == EOF) { 167 bb_perror_msg_and_die("can not close file %s", file_ptr);182 bb_perror_msg_and_die("can't close file %s", file_ptr); 168 183 } 169 184 */ 170 185 } else { 171 while (optind < argc) { 172 char *file_ptr = argv[optind++]; 173 174 hash_value = hash_file(file_ptr, hash_algo); 186 do { 187 uint8_t *hash_value = hash_file(*argv); 175 188 if (hash_value == NULL) { 176 189 return_value = EXIT_FAILURE; 177 190 } else { 178 printf("%s %s\n", hash_value, file_ptr);191 printf("%s %s\n", hash_value, *argv); 179 192 free(hash_value); 180 193 } 181 } 194 } while (*++argv); 182 195 } 183 196 return return_value;
Note:
See TracChangeset
for help on using the changeset viewer.