1 // Copyright (C) 2002 Andrew Tridgell
2 // Copyright (C) 2010-2018 Joel Rosdahl
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by the Free
6 // Software Foundation; either version 3 of the License, or (at your option)
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // You should have received a copy of the GNU General Public License along with
15 // this program; if not, write to the Free Software Foundation, Inc., 51
16 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #define HASH_DELIMITER "\000cCaChE"
31 do_hash_buffer(struct hash *hash, const void *s, size_t len)
33 mdfour_update(&hash->md, (const unsigned char *)s, len);
34 if (len > 0 && hash->debug_binary) {
35 (void) fwrite(s, 1, len, hash->debug_binary);
40 do_debug_text(struct hash *hash, const void *s, size_t len)
42 if (len > 0 && hash->debug_text) {
43 (void) fwrite(s, 1, len, hash->debug_text);
50 struct hash *hash = malloc(sizeof(struct hash));
51 mdfour_begin(&hash->md);
52 hash->debug_binary = NULL;
53 hash->debug_text = NULL;
58 hash_copy(struct hash *hash)
60 struct hash *result = malloc(sizeof(struct hash));
61 result->md = hash->md;
62 result->debug_binary = NULL;
63 result->debug_text = NULL;
67 void hash_free(struct hash *hash)
72 void hash_enable_debug(
73 struct hash *hash, const char *section_name,
74 FILE *debug_binary, FILE *debug_text)
76 hash->debug_binary = debug_binary;
77 hash->debug_text = debug_text;
79 do_debug_text(hash, "=== ", 4);
80 do_debug_text(hash, section_name, strlen(section_name));
81 do_debug_text(hash, " ===\n", 5);
85 hash_input_size(struct hash *hash)
87 return hash->md.totalN;
91 hash_buffer(struct hash *hash, const void *s, size_t len)
93 do_hash_buffer(hash, s, len);
94 do_debug_text(hash, s, len);
98 hash_result(struct hash *hash)
100 unsigned char sum[16];
102 hash_result_as_bytes(hash, sum);
103 return format_hash_as_string(sum, (unsigned) hash->md.totalN);
107 hash_result_as_bytes(struct hash *hash, unsigned char *out)
109 mdfour_update(&hash->md, NULL, 0);
110 mdfour_result(&hash->md, out);
114 hash_equal(struct hash *hash1, struct hash *hash2)
116 unsigned char sum1[16];
117 hash_result_as_bytes(hash1, sum1);
118 unsigned char sum2[16];
119 hash_result_as_bytes(hash2, sum2);
120 return memcmp(sum1, sum2, sizeof(sum1)) == 0;
124 hash_delimiter(struct hash *hash, const char *type)
126 do_hash_buffer(hash, HASH_DELIMITER, sizeof(HASH_DELIMITER));
127 do_hash_buffer(hash, type, strlen(type) + 1); // Include NUL.
128 do_debug_text(hash, "### ", 4);
129 do_debug_text(hash, type, strlen(type));
130 do_debug_text(hash, "\n", 1);
134 hash_string(struct hash *hash, const char *s)
136 hash_string_buffer(hash, s, strlen(s));
140 hash_string_buffer(struct hash *hash, const char *s, int length)
142 hash_buffer(hash, s, length);
143 do_debug_text(hash, "\n", 1);
147 hash_int(struct hash *hash, int x)
149 do_hash_buffer(hash, (char *)&x, sizeof(x));
152 snprintf(buf, sizeof(buf), "%d", x);
153 do_debug_text(hash, buf, strlen(buf));
154 do_debug_text(hash, "\n", 1);
158 hash_fd(struct hash *hash, int fd)
160 char buf[READ_BUFFER_SIZE];
163 while ((n = read(fd, buf, sizeof(buf))) != 0) {
164 if (n == -1 && errno != EINTR) {
168 do_hash_buffer(hash, buf, n);
169 do_debug_text(hash, buf, n);
176 hash_file(struct hash *hash, const char *fname)
178 int fd = open(fname, O_RDONLY|O_BINARY);
180 cc_log("Failed to open %s: %s", fname, strerror(errno));
184 bool ret = hash_fd(hash, fd);