]> git.ipfire.org Git - thirdparty/git.git/blob - hash.h
Merge branch 'fc/vimdiff-layout-vimdiff3-fix'
[thirdparty/git.git] / hash.h
1 #ifndef HASH_H
2 #define HASH_H
3
4 #include "git-compat-util.h"
5 #include "repository.h"
6
7 #if defined(SHA1_PPC)
8 #include "ppc/sha1.h"
9 #elif defined(SHA1_APPLE)
10 #include <CommonCrypto/CommonDigest.h>
11 #elif defined(SHA1_OPENSSL)
12 #include <openssl/sha.h>
13 #elif defined(SHA1_DC)
14 #include "sha1dc_git.h"
15 #else /* SHA1_BLK */
16 #include "block-sha1/sha1.h"
17 #endif
18
19 #if defined(SHA256_NETTLE)
20 #include "sha256/nettle.h"
21 #elif defined(SHA256_GCRYPT)
22 #define SHA256_NEEDS_CLONE_HELPER
23 #include "sha256/gcrypt.h"
24 #elif defined(SHA256_OPENSSL)
25 #include <openssl/sha.h>
26 #else
27 #include "sha256/block/sha256.h"
28 #endif
29
30 #ifndef platform_SHA_CTX
31 /*
32 * platform's underlying implementation of SHA-1; could be OpenSSL,
33 * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
34 * SHA-1 header may have already defined platform_SHA_CTX for our
35 * own implementations like block-sha1 and ppc-sha1, so we list
36 * the default for OpenSSL compatible SHA-1 implementations here.
37 */
38 #define platform_SHA_CTX SHA_CTX
39 #define platform_SHA1_Init SHA1_Init
40 #define platform_SHA1_Update SHA1_Update
41 #define platform_SHA1_Final SHA1_Final
42 #endif
43
44 #define git_SHA_CTX platform_SHA_CTX
45 #define git_SHA1_Init platform_SHA1_Init
46 #define git_SHA1_Update platform_SHA1_Update
47 #define git_SHA1_Final platform_SHA1_Final
48
49 #ifndef platform_SHA256_CTX
50 #define platform_SHA256_CTX SHA256_CTX
51 #define platform_SHA256_Init SHA256_Init
52 #define platform_SHA256_Update SHA256_Update
53 #define platform_SHA256_Final SHA256_Final
54 #endif
55
56 #define git_SHA256_CTX platform_SHA256_CTX
57 #define git_SHA256_Init platform_SHA256_Init
58 #define git_SHA256_Update platform_SHA256_Update
59 #define git_SHA256_Final platform_SHA256_Final
60
61 #ifdef platform_SHA256_Clone
62 #define git_SHA256_Clone platform_SHA256_Clone
63 #endif
64
65 #ifdef SHA1_MAX_BLOCK_SIZE
66 #include "compat/sha1-chunked.h"
67 #undef git_SHA1_Update
68 #define git_SHA1_Update git_SHA1_Update_Chunked
69 #endif
70
71 static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src)
72 {
73 memcpy(dst, src, sizeof(*dst));
74 }
75
76 #ifndef SHA256_NEEDS_CLONE_HELPER
77 static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src)
78 {
79 memcpy(dst, src, sizeof(*dst));
80 }
81 #endif
82
83 /*
84 * Note that these constants are suitable for indexing the hash_algos array and
85 * comparing against each other, but are otherwise arbitrary, so they should not
86 * be exposed to the user or serialized to disk. To know whether a
87 * git_hash_algo struct points to some usable hash function, test the format_id
88 * field for being non-zero. Use the name field for user-visible situations and
89 * the format_id field for fixed-length fields on disk.
90 */
91 /* An unknown hash function. */
92 #define GIT_HASH_UNKNOWN 0
93 /* SHA-1 */
94 #define GIT_HASH_SHA1 1
95 /* SHA-256 */
96 #define GIT_HASH_SHA256 2
97 /* Number of algorithms supported (including unknown). */
98 #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
99
100 /* "sha1", big-endian */
101 #define GIT_SHA1_FORMAT_ID 0x73686131
102
103 /* The length in bytes and in hex digits of an object name (SHA-1 value). */
104 #define GIT_SHA1_RAWSZ 20
105 #define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
106 /* The block size of SHA-1. */
107 #define GIT_SHA1_BLKSZ 64
108
109 /* "s256", big-endian */
110 #define GIT_SHA256_FORMAT_ID 0x73323536
111
112 /* The length in bytes and in hex digits of an object name (SHA-256 value). */
113 #define GIT_SHA256_RAWSZ 32
114 #define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
115 /* The block size of SHA-256. */
116 #define GIT_SHA256_BLKSZ 64
117
118 /* The length in byte and in hex digits of the largest possible hash value. */
119 #define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
120 #define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
121 /* The largest possible block size for any supported hash. */
122 #define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
123
124 struct object_id {
125 unsigned char hash[GIT_MAX_RAWSZ];
126 int algo; /* XXX requires 4-byte alignment */
127 };
128
129 /* A suitably aligned type for stack allocations of hash contexts. */
130 union git_hash_ctx {
131 git_SHA_CTX sha1;
132 git_SHA256_CTX sha256;
133 };
134 typedef union git_hash_ctx git_hash_ctx;
135
136 typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
137 typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
138 typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
139 typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
140 typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx);
141
142 struct git_hash_algo {
143 /*
144 * The name of the algorithm, as appears in the config file and in
145 * messages.
146 */
147 const char *name;
148
149 /* A four-byte version identifier, used in pack indices. */
150 uint32_t format_id;
151
152 /* The length of the hash in binary. */
153 size_t rawsz;
154
155 /* The length of the hash in hex characters. */
156 size_t hexsz;
157
158 /* The block size of the hash. */
159 size_t blksz;
160
161 /* The hash initialization function. */
162 git_hash_init_fn init_fn;
163
164 /* The hash context cloning function. */
165 git_hash_clone_fn clone_fn;
166
167 /* The hash update function. */
168 git_hash_update_fn update_fn;
169
170 /* The hash finalization function. */
171 git_hash_final_fn final_fn;
172
173 /* The hash finalization function for object IDs. */
174 git_hash_final_oid_fn final_oid_fn;
175
176 /* The OID of the empty tree. */
177 const struct object_id *empty_tree;
178
179 /* The OID of the empty blob. */
180 const struct object_id *empty_blob;
181
182 /* The all-zeros OID. */
183 const struct object_id *null_oid;
184 };
185 extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
186
187 /*
188 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
189 * the name doesn't match a known algorithm.
190 */
191 int hash_algo_by_name(const char *name);
192 /* Identical, except based on the format ID. */
193 int hash_algo_by_id(uint32_t format_id);
194 /* Identical, except based on the length. */
195 int hash_algo_by_length(int len);
196 /* Identical, except for a pointer to struct git_hash_algo. */
197 static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
198 {
199 return p - hash_algos;
200 }
201
202 #define the_hash_algo the_repository->hash_algo
203
204 const struct object_id *null_oid(void);
205
206 static inline int hashcmp_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
207 {
208 /*
209 * Teach the compiler that there are only two possibilities of hash size
210 * here, so that it can optimize for this case as much as possible.
211 */
212 if (algop->rawsz == GIT_MAX_RAWSZ)
213 return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
214 return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
215 }
216
217 static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
218 {
219 return hashcmp_algop(sha1, sha2, the_hash_algo);
220 }
221
222 static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
223 {
224 const struct git_hash_algo *algop;
225 if (!oid1->algo)
226 algop = the_hash_algo;
227 else
228 algop = &hash_algos[oid1->algo];
229 return hashcmp_algop(oid1->hash, oid2->hash, algop);
230 }
231
232 static inline int hasheq_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
233 {
234 /*
235 * We write this here instead of deferring to hashcmp so that the
236 * compiler can properly inline it and avoid calling memcmp.
237 */
238 if (algop->rawsz == GIT_MAX_RAWSZ)
239 return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
240 return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
241 }
242
243 static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
244 {
245 return hasheq_algop(sha1, sha2, the_hash_algo);
246 }
247
248 static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
249 {
250 const struct git_hash_algo *algop;
251 if (!oid1->algo)
252 algop = the_hash_algo;
253 else
254 algop = &hash_algos[oid1->algo];
255 return hasheq_algop(oid1->hash, oid2->hash, algop);
256 }
257
258 static inline int is_null_oid(const struct object_id *oid)
259 {
260 return oideq(oid, null_oid());
261 }
262
263 static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
264 {
265 memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
266 }
267
268 static inline void oidcpy(struct object_id *dst, const struct object_id *src)
269 {
270 memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
271 dst->algo = src->algo;
272 }
273
274 /* Like oidcpy() but zero-pads the unused bytes in dst's hash array. */
275 static inline void oidcpy_with_padding(struct object_id *dst,
276 const struct object_id *src)
277 {
278 size_t hashsz;
279
280 if (!src->algo)
281 hashsz = the_hash_algo->rawsz;
282 else
283 hashsz = hash_algos[src->algo].rawsz;
284
285 memcpy(dst->hash, src->hash, hashsz);
286 memset(dst->hash + hashsz, 0, GIT_MAX_RAWSZ - hashsz);
287 dst->algo = src->algo;
288 }
289
290 static inline struct object_id *oiddup(const struct object_id *src)
291 {
292 struct object_id *dst = xmalloc(sizeof(struct object_id));
293 oidcpy(dst, src);
294 return dst;
295 }
296
297 static inline void hashclr(unsigned char *hash)
298 {
299 memset(hash, 0, the_hash_algo->rawsz);
300 }
301
302 static inline void oidclr(struct object_id *oid)
303 {
304 memset(oid->hash, 0, GIT_MAX_RAWSZ);
305 oid->algo = hash_algo_by_ptr(the_hash_algo);
306 }
307
308 static inline void oidread(struct object_id *oid, const unsigned char *hash)
309 {
310 memcpy(oid->hash, hash, the_hash_algo->rawsz);
311 oid->algo = hash_algo_by_ptr(the_hash_algo);
312 }
313
314 static inline int is_empty_blob_sha1(const unsigned char *sha1)
315 {
316 return hasheq(sha1, the_hash_algo->empty_blob->hash);
317 }
318
319 static inline int is_empty_blob_oid(const struct object_id *oid)
320 {
321 return oideq(oid, the_hash_algo->empty_blob);
322 }
323
324 static inline int is_empty_tree_sha1(const unsigned char *sha1)
325 {
326 return hasheq(sha1, the_hash_algo->empty_tree->hash);
327 }
328
329 static inline int is_empty_tree_oid(const struct object_id *oid)
330 {
331 return oideq(oid, the_hash_algo->empty_tree);
332 }
333
334 static inline void oid_set_algo(struct object_id *oid, const struct git_hash_algo *algop)
335 {
336 oid->algo = hash_algo_by_ptr(algop);
337 }
338
339 const char *empty_tree_oid_hex(void);
340 const char *empty_blob_oid_hex(void);
341
342 #endif