source: MondoRescue/branches/stable/mindi-busybox/coreutils/md5_sha1_sum.c@ 902

Last change on this file since 902 was 902, checked in by Bruno Cornec, 17 years ago

mindi-busybox now uses busybox 1.2.2 (Thanks Rob for that last update and good lucck for your future projects).

File size: 4.8 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2003 Glenn L. McGrath
4 * Copyright (C) 2003-2004 Erik Andersen
5 *
6 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
7 */
8
9#include <fcntl.h>
10#include <limits.h>
11#include <stdio.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16
17#include "busybox.h"
18
19typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
20
21#define FLAG_SILENT 1
22#define FLAG_CHECK 2
23#define FLAG_WARN 4
24
25/* This might be useful elsewhere */
26static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
27 unsigned char hash_length)
28{
29 int x, len, max;
30 unsigned char *hex_value;
31
32 max = (hash_length * 2) + 2;
33 hex_value = xmalloc(max);
34 for (x = len = 0; x < hash_length; x++) {
35 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
36 }
37 return (hex_value);
38}
39
40static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
41{
42 int src_fd, hash_len, count;
43 union _ctx_ {
44 sha1_ctx_t sha1;
45 md5_ctx_t md5;
46 } context;
47 uint8_t *hash_value = NULL;
48 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
49 void (*update)(const void*, size_t, void*);
50 void (*final)(void*, void*);
51
52 if (strcmp(filename, "-") == 0) {
53 src_fd = STDIN_FILENO;
54 } else if(0 > (src_fd = open(filename, O_RDONLY))) {
55 bb_perror_msg("%s", filename);
56 return NULL;
57 }
58
59 /* figure specific hash algorithims */
60 if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
61 md5_begin(&context.md5);
62 update = (void (*)(const void*, size_t, void*))md5_hash;
63 final = (void (*)(void*, void*))md5_end;
64 hash_len = 16;
65 } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
66 sha1_begin(&context.sha1);
67 update = (void (*)(const void*, size_t, void*))sha1_hash;
68 final = (void (*)(void*, void*))sha1_end;
69 hash_len = 20;
70 } else {
71 bb_error_msg_and_die("algorithm not supported");
72 }
73
74 while (0 < (count = read(src_fd, in_buf, 4096))) {
75 update(in_buf, count, &context);
76 }
77
78 if (count == 0) {
79 final(in_buf, &context);
80 hash_value = hash_bin_to_hex(in_buf, hash_len);
81 }
82
83 RELEASE_CONFIG_BUFFER(in_buf);
84
85 if (src_fd != STDIN_FILENO) {
86 close(src_fd);
87 }
88
89 return hash_value;
90}
91
92/* This could become a common function for md5 as well, by using md5_stream */
93static int hash_files(int argc, char **argv, hash_algo_t hash_algo)
94{
95 int return_value = EXIT_SUCCESS;
96 uint8_t *hash_value;
97 unsigned int flags;
98
99 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
100 flags = bb_getopt_ulflags(argc, argv, "scw");
101 else optind = 1;
102
103 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
104 if (flags & FLAG_SILENT) {
105 bb_error_msg_and_die
106 ("the -s option is meaningful only when verifying checksums");
107 } else if (flags & FLAG_WARN) {
108 bb_error_msg_and_die
109 ("the -w option is meaningful only when verifying checksums");
110 }
111 }
112
113 if (argc == optind) {
114 argv[argc++] = "-";
115 }
116
117 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) {
118 FILE *pre_computed_stream;
119 int count_total = 0;
120 int count_failed = 0;
121 char *file_ptr = argv[optind];
122 char *line;
123
124 if (optind + 1 != argc) {
125 bb_error_msg_and_die
126 ("only one argument may be specified when using -c");
127 }
128
129 if (strcmp(file_ptr, "-") == 0) {
130 pre_computed_stream = stdin;
131 } else {
132 pre_computed_stream = bb_xfopen(file_ptr, "r");
133 }
134
135 while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
136 char *filename_ptr;
137
138 count_total++;
139 filename_ptr = strstr(line, " ");
140 if (filename_ptr == NULL) {
141 if (flags & FLAG_WARN) {
142 bb_error_msg("Invalid format");
143 }
144 count_failed++;
145 return_value = EXIT_FAILURE;
146 free(line);
147 continue;
148 }
149 *filename_ptr = '\0';
150 filename_ptr += 2;
151
152 hash_value = hash_file(filename_ptr, hash_algo);
153
154 if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
155 if (!(flags & FLAG_SILENT))
156 printf("%s: OK\n", filename_ptr);
157 } else {
158 if (!(flags & FLAG_SILENT))
159 printf("%s: FAILED\n", filename_ptr);
160 count_failed++;
161 return_value = EXIT_FAILURE;
162 }
163 /* possible free(NULL) */
164 free(hash_value);
165 free(line);
166 }
167 if (count_failed && !(flags & FLAG_SILENT)) {
168 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
169 count_failed, count_total);
170 }
171 if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
172 bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
173 }
174 } else {
175 while (optind < argc) {
176 char *file_ptr = argv[optind++];
177
178 hash_value = hash_file(file_ptr, hash_algo);
179 if (hash_value == NULL) {
180 return_value = EXIT_FAILURE;
181 } else {
182 printf("%s %s\n", hash_value, file_ptr);
183 free(hash_value);
184 }
185 }
186 }
187 return (return_value);
188}
189
190#ifdef CONFIG_MD5SUM
191int md5sum_main(int argc, char **argv)
192{
193 return (hash_files(argc, argv, HASH_MD5));
194}
195#endif
196
197#ifdef CONFIG_SHA1SUM
198int sha1sum_main(int argc, char **argv)
199{
200 return (hash_files(argc, argv, HASH_SHA1));
201}
202#endif
Note: See TracBrowser for help on using the repository browser.