diff options
-rw-r--r-- | coreutils/sha1sum.c | 80 |
1 files changed, 41 insertions, 39 deletions
diff --git a/coreutils/sha1sum.c b/coreutils/sha1sum.c index d94cb4b53..8056b1d58 100644 --- a/coreutils/sha1sum.c +++ b/coreutils/sha1sum.c | |||
@@ -4,7 +4,7 @@ | |||
4 | * shasum fixed with reference to coreutils and the nist fip180-1 document | 4 | * shasum fixed with reference to coreutils and the nist fip180-1 document |
5 | * which is incorrect, in section 5 | 5 | * which is incorrect, in section 5 |
6 | * - ft(B,C,D) = (B AND C) OR ((NOT B) AND D) ( 0 <= t <= 19) | 6 | * - ft(B,C,D) = (B AND C) OR ((NOT B) AND D) ( 0 <= t <= 19) |
7 | * + ft(B,C,D) = (D XOR (B AND (C XOR S))) ( 0 <= t <= 19) | 7 | * + ft(B,C,D) = (D XOR (B AND (C XOR D))) ( 0 <= t <= 19) |
8 | * | 8 | * |
9 | * Copyright (C) 1999 Scott G. Miller | 9 | * Copyright (C) 1999 Scott G. Miller |
10 | * Copyright (C) 2003 Glenn L. McGrath | 10 | * Copyright (C) 2003 Glenn L. McGrath |
@@ -112,7 +112,7 @@ static void sha_hash(unsigned int *data, int *hash) | |||
112 | hash[4] += e; | 112 | hash[4] += e; |
113 | } | 113 | } |
114 | 114 | ||
115 | static void sha1sum_stream(FILE * fd, unsigned int *hashval) | 115 | static void sha1sum_stream(FILE *fd, unsigned int *hashval) |
116 | { | 116 | { |
117 | RESERVE_CONFIG_BUFFER(buffer, 64); | 117 | RESERVE_CONFIG_BUFFER(buffer, 64); |
118 | int length = 0; | 118 | int length = 0; |
@@ -128,11 +128,6 @@ static void sha1sum_stream(FILE * fd, unsigned int *hashval) | |||
128 | length += c; | 128 | length += c; |
129 | if (feof(fd) || ferror(fd)) { | 129 | if (feof(fd) || ferror(fd)) { |
130 | int i; | 130 | int i; |
131 | /* If reading from stdin we need to get rid of a tailing character */ | ||
132 | if (fd == stdin) { | ||
133 | c--; | ||
134 | length--; | ||
135 | } | ||
136 | for (i = c; i < 61; i++) { | 131 | for (i = c; i < 61; i++) { |
137 | if (i == c) { | 132 | if (i == c) { |
138 | buffer[i] = 0x80; | 133 | buffer[i] = 0x80; |
@@ -153,34 +148,37 @@ static void sha1sum_stream(FILE * fd, unsigned int *hashval) | |||
153 | return; | 148 | return; |
154 | } | 149 | } |
155 | 150 | ||
156 | static void print_hash(unsigned short hash_length, unsigned int *hash_val, char *filename) | 151 | static void print_hash(unsigned int *hash_value, unsigned char hash_length, unsigned char *filename) |
157 | { | 152 | { |
158 | int x; | 153 | unsigned char x; |
159 | 154 | ||
160 | for (x = 0; x < hash_length; x++) { | 155 | for (x = 0; x < hash_length; x++) { |
161 | printf("%08x", hash_val[x]); | 156 | printf("%08x", hash_value[x]); |
162 | } | ||
163 | if (filename != NULL) { | ||
164 | putchar(' '); | ||
165 | putchar(' '); | ||
166 | puts(filename); | ||
167 | } | 157 | } |
168 | putchar('\n'); | 158 | putchar(' '); |
159 | putchar(' '); | ||
160 | puts(filename); | ||
169 | } | 161 | } |
170 | 162 | ||
163 | #define FLAG_SILENT 1 | ||
164 | #define FLAG_CHECK 2 | ||
165 | #define FLAG_WARN 3 | ||
166 | |||
171 | /* This should become a common function used by sha1sum and md5sum, | 167 | /* This should become a common function used by sha1sum and md5sum, |
172 | * it needs extra functionality first | 168 | * it needs extra functionality first |
173 | */ | 169 | */ |
174 | extern int authenticate(const int argc, char **argv, void (*hash_ptr)(FILE *stream, unsigned int *hashval), const unsigned short hash_length) | 170 | extern int authenticate(int argc, char **argv, void (*hash_ptr)(FILE *stream, unsigned int *hashval), const unsigned char hash_length) |
175 | { | 171 | { |
172 | unsigned int hash_value[hash_length]; | ||
173 | unsigned char flags = 0; | ||
176 | int opt; | 174 | int opt; |
177 | unsigned int *hashval; | ||
178 | 175 | ||
179 | while ((opt = getopt(argc, argv, "sc:w")) != -1) { | 176 | while ((opt = getopt(argc, argv, "sc:w")) != -1) { |
180 | switch (opt) { | 177 | switch (opt) { |
181 | #if 0 | ||
182 | case 's': /* Dont output anything, status code shows success */ | 178 | case 's': /* Dont output anything, status code shows success */ |
179 | flags |= FLAG_SILENT; | ||
183 | break; | 180 | break; |
181 | #if 0 | ||
184 | case 'c': /* Check a list of checksums against stored values */ | 182 | case 'c': /* Check a list of checksums against stored values */ |
185 | break; | 183 | break; |
186 | case 'w': /* Warn of bad formatting when checking files */ | 184 | case 'w': /* Warn of bad formatting when checking files */ |
@@ -191,34 +189,38 @@ extern int authenticate(const int argc, char **argv, void (*hash_ptr)(FILE *stre | |||
191 | } | 189 | } |
192 | } | 190 | } |
193 | 191 | ||
194 | hashval = xmalloc(hash_length * sizeof(unsigned int)); | ||
195 | |||
196 | if (argc == optind) { | 192 | if (argc == optind) { |
197 | hash_ptr(stdin, hashval); | 193 | argv[argc] = "-"; |
198 | print_hash(hash_length, hashval, NULL); | 194 | } |
199 | } else { | 195 | |
200 | int i; | 196 | while (optind < argc) { |
201 | 197 | FILE *stream; | |
202 | for (i = optind; i < argc; i++) { | 198 | unsigned char *file_ptr = argv[optind]; |
203 | if (!strcmp(argv[i], "-")) { | 199 | |
204 | hash_ptr(stdin, hashval); | 200 | if ((file_ptr[0] == '-') && (file_ptr[1] == '\0')) { |
205 | print_hash(hash_length, hashval, NULL); | 201 | stream = stdin; |
206 | } else { | 202 | } else { |
207 | FILE *stream = bb_xfopen(argv[i], "r"); | 203 | stream = bb_wfopen(file_ptr, "r"); |
208 | hash_ptr(stream, hashval); | 204 | if (stream == NULL) { |
209 | fclose(stream); | 205 | return(EXIT_FAILURE); |
210 | print_hash(hash_length, hashval, argv[i]); | ||
211 | } | 206 | } |
212 | } | 207 | } |
213 | } | 208 | hash_ptr(stream, hash_value); |
209 | if (!flags & FLAG_SILENT) { | ||
210 | print_hash(hash_value, hash_length, file_ptr); | ||
211 | } | ||
214 | 212 | ||
215 | free(hashval); | 213 | if (fclose(stream) == EOF) { |
214 | bb_perror_msg_and_die("Couldnt close file %s", file_ptr); | ||
215 | } | ||
216 | |||
217 | optind++; | ||
218 | } | ||
216 | 219 | ||
217 | return 0; | 220 | return(EXIT_SUCCESS); |
218 | } | 221 | } |
219 | 222 | ||
220 | extern int sha1sum_main(int argc, char **argv) | 223 | extern int sha1sum_main(int argc, char **argv) |
221 | { | 224 | { |
222 | /* sha1 length is 5 nibbles */ | ||
223 | return (authenticate(argc, argv, sha1sum_stream, 5)); | 225 | return (authenticate(argc, argv, sha1sum_stream, 5)); |
224 | } | 226 | } |