1 // Copyright (C) 2018 Joel Rosdahl
3 // This program is free software; you can redistribute it and/or modify it
4 // under the terms of the GNU General Public License as published by the Free
5 // Software Foundation; either version 3 of the License, or (at your option)
8 // This program is distributed in the hope that it will be useful, but WITHOUT
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 // You should have received a copy of the GNU General Public License along with
14 // this program; if not, write to the Free Software Foundation, Inc., 51
15 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 struct hash *hash_init(void);
27 // Create a new hash from an existing hash state.
28 struct hash *hash_copy(struct hash *hash);
30 // Free a hash created by hash_init or hash_copy.
31 void hash_free(struct hash *hash);
33 // Enable debug logging of hashed input to a binary and a text file.
34 void hash_enable_debug(
35 struct hash *hash, const char *section_name, FILE *debug_binary,
38 // Return how many bytes have been hashed.
39 size_t hash_input_size(struct hash *hash);
41 // Return the hash result as a hex string. Caller frees.
42 char *hash_result(struct hash *hash);
44 // Return the hash result as 16 binary bytes.
45 void hash_result_as_bytes(struct hash *hash, unsigned char *out);
47 // Return whether hash1 and hash2 are equal.
48 bool hash_equal(struct hash *hash1, struct hash *hash2);
50 // Hash some data that is unlikely to occur in the input. The idea is twofold:
52 // - Delimit things like arguments from each other (e.g., so that -I -O2 and
53 // -I-O2 hash differently).
54 // - Tag different types of hashed information so that it's possible to do
55 // conditional hashing of information in a safe way (e.g., if we want to hash
56 // information X if CCACHE_A is set and information Y if CCACHE_B is set,
57 // there should never be a hash collision risk).
58 void hash_delimiter(struct hash *hash, const char *type);
60 // Hash bytes in a buffer.
62 // If hash debugging is enabled, the bytes are written verbatim to the text
64 void hash_buffer(struct hash *hash, const void *s, size_t len);
68 // If hash debugging is enabled, the string is written to the text input file
69 // followed by a newline.
70 void hash_string(struct hash *hash, const char *s);
72 // Hash a string with a known size.
74 // If hash debugging is enabled, the string is written to the text input file
75 // followed by a newline.
76 void hash_string_buffer(struct hash *hash, const char *s, int length);
80 // If hash debugging is enabled, the integer is written in text form to the
81 // text input file followed by a newline.
82 void hash_int(struct hash *hash, int x);
84 // Add contents of an open file to the hash.
86 // If hash debugging is enabled, the data is written verbatim to the text input
89 // Returns true on success, otherwise false.
90 bool hash_fd(struct hash *hash, int fd);
92 // Add contents of a file to the hash.
94 // If hash debugging is enabled, the data is written verbatim to the text input
97 // Returns true on success, otherwise false.
98 bool hash_file(struct hash *hash, const char *fname);