debian: new upstream release
[git/debian.git] / hash-ll.h
blob10d84cc208886187d044b0b69fbfc397f4d28864
1 #ifndef HASH_LL_H
2 #define HASH_LL_H
4 #if defined(SHA1_APPLE)
5 #include <CommonCrypto/CommonDigest.h>
6 #elif defined(SHA1_OPENSSL)
7 # include <openssl/sha.h>
8 # if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
9 # define SHA1_NEEDS_CLONE_HELPER
10 # include "sha1/openssl.h"
11 # endif
12 #elif defined(SHA1_DC)
13 #include "sha1dc_git.h"
14 #else /* SHA1_BLK */
15 #include "block-sha1/sha1.h"
16 #endif
18 #if defined(SHA256_NETTLE)
19 #include "sha256/nettle.h"
20 #elif defined(SHA256_GCRYPT)
21 #define SHA256_NEEDS_CLONE_HELPER
22 #include "sha256/gcrypt.h"
23 #elif defined(SHA256_OPENSSL)
24 # include <openssl/sha.h>
25 # if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
26 # define SHA256_NEEDS_CLONE_HELPER
27 # include "sha256/openssl.h"
28 # endif
29 #else
30 #include "sha256/block/sha256.h"
31 #endif
33 #ifndef platform_SHA_CTX
35 * platform's underlying implementation of SHA-1; could be OpenSSL,
36 * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
37 * SHA-1 header may have already defined platform_SHA_CTX for our
38 * own implementations like block-sha1, so we list
39 * the default for OpenSSL compatible SHA-1 implementations here.
41 #define platform_SHA_CTX SHA_CTX
42 #define platform_SHA1_Init SHA1_Init
43 #define platform_SHA1_Update SHA1_Update
44 #define platform_SHA1_Final SHA1_Final
45 #endif
47 #define git_SHA_CTX platform_SHA_CTX
48 #define git_SHA1_Init platform_SHA1_Init
49 #define git_SHA1_Update platform_SHA1_Update
50 #define git_SHA1_Final platform_SHA1_Final
52 #ifdef platform_SHA1_Clone
53 #define git_SHA1_Clone platform_SHA1_Clone
54 #endif
56 #ifndef platform_SHA256_CTX
57 #define platform_SHA256_CTX SHA256_CTX
58 #define platform_SHA256_Init SHA256_Init
59 #define platform_SHA256_Update SHA256_Update
60 #define platform_SHA256_Final SHA256_Final
61 #endif
63 #define git_SHA256_CTX platform_SHA256_CTX
64 #define git_SHA256_Init platform_SHA256_Init
65 #define git_SHA256_Update platform_SHA256_Update
66 #define git_SHA256_Final platform_SHA256_Final
68 #ifdef platform_SHA256_Clone
69 #define git_SHA256_Clone platform_SHA256_Clone
70 #endif
72 #ifdef SHA1_MAX_BLOCK_SIZE
73 #include "compat/sha1-chunked.h"
74 #undef git_SHA1_Update
75 #define git_SHA1_Update git_SHA1_Update_Chunked
76 #endif
78 #ifndef SHA1_NEEDS_CLONE_HELPER
79 static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src)
81 memcpy(dst, src, sizeof(*dst));
83 #endif
85 #ifndef SHA256_NEEDS_CLONE_HELPER
86 static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src)
88 memcpy(dst, src, sizeof(*dst));
90 #endif
93 * Note that these constants are suitable for indexing the hash_algos array and
94 * comparing against each other, but are otherwise arbitrary, so they should not
95 * be exposed to the user or serialized to disk. To know whether a
96 * git_hash_algo struct points to some usable hash function, test the format_id
97 * field for being non-zero. Use the name field for user-visible situations and
98 * the format_id field for fixed-length fields on disk.
100 /* An unknown hash function. */
101 #define GIT_HASH_UNKNOWN 0
102 /* SHA-1 */
103 #define GIT_HASH_SHA1 1
104 /* SHA-256 */
105 #define GIT_HASH_SHA256 2
106 /* Number of algorithms supported (including unknown). */
107 #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
109 /* "sha1", big-endian */
110 #define GIT_SHA1_FORMAT_ID 0x73686131
112 /* The length in bytes and in hex digits of an object name (SHA-1 value). */
113 #define GIT_SHA1_RAWSZ 20
114 #define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
115 /* The block size of SHA-1. */
116 #define GIT_SHA1_BLKSZ 64
118 /* "s256", big-endian */
119 #define GIT_SHA256_FORMAT_ID 0x73323536
121 /* The length in bytes and in hex digits of an object name (SHA-256 value). */
122 #define GIT_SHA256_RAWSZ 32
123 #define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
124 /* The block size of SHA-256. */
125 #define GIT_SHA256_BLKSZ 64
127 /* The length in byte and in hex digits of the largest possible hash value. */
128 #define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
129 #define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
130 /* The largest possible block size for any supported hash. */
131 #define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
133 struct object_id {
134 unsigned char hash[GIT_MAX_RAWSZ];
135 int algo; /* XXX requires 4-byte alignment */
138 #define GET_OID_QUIETLY 01
139 #define GET_OID_COMMIT 02
140 #define GET_OID_COMMITTISH 04
141 #define GET_OID_TREE 010
142 #define GET_OID_TREEISH 020
143 #define GET_OID_BLOB 040
144 #define GET_OID_FOLLOW_SYMLINKS 0100
145 #define GET_OID_RECORD_PATH 0200
146 #define GET_OID_ONLY_TO_DIE 04000
147 #define GET_OID_REQUIRE_PATH 010000
149 #define GET_OID_DISAMBIGUATORS \
150 (GET_OID_COMMIT | GET_OID_COMMITTISH | \
151 GET_OID_TREE | GET_OID_TREEISH | \
152 GET_OID_BLOB)
154 enum get_oid_result {
155 FOUND = 0,
156 MISSING_OBJECT = -1, /* The requested object is missing */
157 SHORT_NAME_AMBIGUOUS = -2,
158 /* The following only apply when symlinks are followed */
159 DANGLING_SYMLINK = -4, /*
160 * The initial symlink is there, but
161 * (transitively) points to a missing
162 * in-tree file
164 SYMLINK_LOOP = -5,
165 NOT_DIR = -6, /*
166 * Somewhere along the symlink chain, a path is
167 * requested which contains a file as a
168 * non-final element.
172 /* A suitably aligned type for stack allocations of hash contexts. */
173 union git_hash_ctx {
174 git_SHA_CTX sha1;
175 git_SHA256_CTX sha256;
177 typedef union git_hash_ctx git_hash_ctx;
179 typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
180 typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
181 typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
182 typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
183 typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx);
185 struct git_hash_algo {
187 * The name of the algorithm, as appears in the config file and in
188 * messages.
190 const char *name;
192 /* A four-byte version identifier, used in pack indices. */
193 uint32_t format_id;
195 /* The length of the hash in binary. */
196 size_t rawsz;
198 /* The length of the hash in hex characters. */
199 size_t hexsz;
201 /* The block size of the hash. */
202 size_t blksz;
204 /* The hash initialization function. */
205 git_hash_init_fn init_fn;
207 /* The hash context cloning function. */
208 git_hash_clone_fn clone_fn;
210 /* The hash update function. */
211 git_hash_update_fn update_fn;
213 /* The hash finalization function. */
214 git_hash_final_fn final_fn;
216 /* The hash finalization function for object IDs. */
217 git_hash_final_oid_fn final_oid_fn;
219 /* The OID of the empty tree. */
220 const struct object_id *empty_tree;
222 /* The OID of the empty blob. */
223 const struct object_id *empty_blob;
225 /* The all-zeros OID. */
226 const struct object_id *null_oid;
228 extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
231 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
232 * the name doesn't match a known algorithm.
234 int hash_algo_by_name(const char *name);
235 /* Identical, except based on the format ID. */
236 int hash_algo_by_id(uint32_t format_id);
237 /* Identical, except based on the length. */
238 int hash_algo_by_length(int len);
239 /* Identical, except for a pointer to struct git_hash_algo. */
240 static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
242 return p - hash_algos;
245 const struct object_id *null_oid(void);
247 static inline int hashcmp_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
250 * Teach the compiler that there are only two possibilities of hash size
251 * here, so that it can optimize for this case as much as possible.
253 if (algop->rawsz == GIT_MAX_RAWSZ)
254 return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
255 return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
258 static inline int hasheq_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
261 * We write this here instead of deferring to hashcmp so that the
262 * compiler can properly inline it and avoid calling memcmp.
264 if (algop->rawsz == GIT_MAX_RAWSZ)
265 return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
266 return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
269 static inline void oidcpy(struct object_id *dst, const struct object_id *src)
271 memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
272 dst->algo = src->algo;
275 static inline struct object_id *oiddup(const struct object_id *src)
277 struct object_id *dst = xmalloc(sizeof(struct object_id));
278 oidcpy(dst, src);
279 return dst;
282 static inline void oid_set_algo(struct object_id *oid, const struct git_hash_algo *algop)
284 oid->algo = hash_algo_by_ptr(algop);
288 * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
289 * for use in hash tables. Cryptographic hashes are supposed to have
290 * uniform distribution, so in contrast to `memhash()`, this just copies
291 * the first `sizeof(int)` bytes without shuffling any bits. Note that
292 * the results will be different on big-endian and little-endian
293 * platforms, so they should not be stored or transferred over the net.
295 static inline unsigned int oidhash(const struct object_id *oid)
298 * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
299 * platforms that don't support unaligned reads.
301 unsigned int hash;
302 memcpy(&hash, oid->hash, sizeof(hash));
303 return hash;
306 const char *empty_tree_oid_hex(void);
307 const char *empty_blob_oid_hex(void);
309 #endif