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