]> git.ipfire.org Git - thirdparty/git.git/blame - hash.h
tree-diff.c: move S_DIFFTREE_IFXMIN_NEQ define from cache.h
[thirdparty/git.git] / hash.h
CommitLineData
f18f816c 1#ifndef HASH_H
2#define HASH_H
3
3fa6f2aa 4#include "repository.h"
f50e766b 5
9dc523aa 6#if defined(SHA1_APPLE)
f18f816c 7#include <CommonCrypto/CommonDigest.h>
8#elif defined(SHA1_OPENSSL)
9#include <openssl/sha.h>
8325e43b 10#elif defined(SHA1_DC)
36f048c5 11#include "sha1dc_git.h"
f18f816c 12#else /* SHA1_BLK */
13#include "block-sha1/sha1.h"
14#endif
15
e5557358 16#if defined(SHA256_NETTLE)
17#include "sha256/nettle.h"
18#elif defined(SHA256_GCRYPT)
768e30ea 19#define SHA256_NEEDS_CLONE_HELPER
27dc04c5 20#include "sha256/gcrypt.h"
4b4e2918 21#elif defined(SHA256_OPENSSL)
22#include <openssl/sha.h>
27dc04c5 23#else
13eeedb5 24#include "sha256/block/sha256.h"
27dc04c5 25#endif
13eeedb5 26
164e7163 27#ifndef platform_SHA_CTX
28/*
29 * platform's underlying implementation of SHA-1; could be OpenSSL,
b212c0ca 30 * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
31 * SHA-1 header may have already defined platform_SHA_CTX for our
9dc523aa 32 * own implementations like block-sha1, so we list
164e7163 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
13eeedb5 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
768e30ea 58#ifdef platform_SHA256_Clone
59#define git_SHA256_Clone platform_SHA256_Clone
60#endif
61
164e7163 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
768e30ea 68static 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
74static 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
f50e766b 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
13eeedb5 92/* SHA-256 */
93#define GIT_HASH_SHA256 2
f50e766b 94/* Number of algorithms supported (including unknown). */
13eeedb5 95#define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
f50e766b 96
27f3796a
HWN
97/* "sha1", big-endian */
98#define GIT_SHA1_FORMAT_ID 0x73686131
99
ab795f0d 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
27f3796a
HWN
106/* "s256", big-endian */
107#define GIT_SHA256_FORMAT_ID 0x73323536
108
ab795f0d 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
121struct object_id {
122 unsigned char hash[GIT_MAX_RAWSZ];
8bcda98d 123 int algo; /* XXX requires 4-byte alignment */
ab795f0d 124};
125
41227cb1
EN
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
142enum 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
ac73cedf 160/* A suitably aligned type for stack allocations of hash contexts. */
161union git_hash_ctx {
162 git_SHA_CTX sha1;
13eeedb5 163 git_SHA256_CTX sha256;
ac73cedf 164};
165typedef union git_hash_ctx git_hash_ctx;
166
167typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
768e30ea 168typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
ac73cedf 169typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
170typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
ab795f0d 171typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx);
f50e766b 172
173struct 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
f50e766b 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
a2ce0a75 189 /* The block size of the hash. */
190 size_t blksz;
191
f50e766b 192 /* The hash initialization function. */
193 git_hash_init_fn init_fn;
194
768e30ea 195 /* The hash context cloning function. */
196 git_hash_clone_fn clone_fn;
197
f50e766b 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
ab795f0d 204 /* The hash finalization function for object IDs. */
205 git_hash_final_oid_fn final_oid_fn;
206
f50e766b 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;
14228447 212
213 /* The all-zeros OID. */
214 const struct object_id *null_oid;
f50e766b 215};
216extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
217
2f90b9d9 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 */
222int hash_algo_by_name(const char *name);
223/* Identical, except based on the format ID. */
224int hash_algo_by_id(uint32_t format_id);
95399788 225/* Identical, except based on the length. */
226int hash_algo_by_length(int len);
2f90b9d9 227/* Identical, except for a pointer to struct git_hash_algo. */
228static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
229{
230 return p - hash_algos;
231}
232
c0566d78
JK
233#define the_hash_algo the_repository->hash_algo
234
14228447 235const struct object_id *null_oid(void);
3fa6f2aa 236
5a6dce70 237static inline int hashcmp_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
3fa6f2aa
JK
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 */
5a6dce70 243 if (algop->rawsz == GIT_MAX_RAWSZ)
3fa6f2aa
JK
244 return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
245 return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
246}
247
5a6dce70 248static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
249{
250 return hashcmp_algop(sha1, sha2, the_hash_algo);
251}
252
3fa6f2aa
JK
253static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
254{
5a6dce70 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);
3fa6f2aa
JK
261}
262
5a6dce70 263static inline int hasheq_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
3fa6f2aa
JK
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 */
5a6dce70 269 if (algop->rawsz == GIT_MAX_RAWSZ)
3fa6f2aa
JK
270 return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
271 return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
272}
273
5a6dce70 274static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
275{
276 return hasheq_algop(sha1, sha2, the_hash_algo);
277}
278
3fa6f2aa
JK
279static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
280{
5a6dce70 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);
3fa6f2aa
JK
287}
288
289static inline int is_null_oid(const struct object_id *oid)
290{
14228447 291 return oideq(oid, null_oid());
3fa6f2aa
JK
292}
293
294static 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
299static inline void oidcpy(struct object_id *dst, const struct object_id *src)
300{
301 memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
5a6dce70 302 dst->algo = src->algo;
3fa6f2aa
JK
303}
304
3d20ed27
MT
305/* Like oidcpy() but zero-pads the unused bytes in dst's hash array. */
306static inline void oidcpy_with_padding(struct object_id *dst,
90e07f0a 307 const struct object_id *src)
3d20ed27
MT
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
3fa6f2aa
JK
321static 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
328static inline void hashclr(unsigned char *hash)
329{
330 memset(hash, 0, the_hash_algo->rawsz);
331}
332
333static inline void oidclr(struct object_id *oid)
334{
335 memset(oid->hash, 0, GIT_MAX_RAWSZ);
5a6dce70 336 oid->algo = hash_algo_by_ptr(the_hash_algo);
3fa6f2aa
JK
337}
338
339static inline void oidread(struct object_id *oid, const unsigned char *hash)
340{
341 memcpy(oid->hash, hash, the_hash_algo->rawsz);
5a6dce70 342 oid->algo = hash_algo_by_ptr(the_hash_algo);
3fa6f2aa
JK
343}
344
345static inline int is_empty_blob_sha1(const unsigned char *sha1)
346{
347 return hasheq(sha1, the_hash_algo->empty_blob->hash);
348}
349
350static inline int is_empty_blob_oid(const struct object_id *oid)
351{
352 return oideq(oid, the_hash_algo->empty_blob);
353}
354
355static inline int is_empty_tree_sha1(const unsigned char *sha1)
356{
357 return hasheq(sha1, the_hash_algo->empty_tree->hash);
358}
359
360static inline int is_empty_tree_oid(const struct object_id *oid)
361{
362 return oideq(oid, the_hash_algo->empty_tree);
363}
364
5a6dce70 365static 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
3fa6f2aa
JK
370const char *empty_tree_oid_hex(void);
371const char *empty_blob_oid_hex(void);
372
f18f816c 373#endif