reftable/block: reuse `zstream` state on inflation
[alt-git.git] / hash-ll.h
blob2cfde63ae1cf029e9700185247fc833c82d54407
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
148 #define GET_OID_HASH_ANY 020000
150 #define GET_OID_DISAMBIGUATORS \
151 (GET_OID_COMMIT | GET_OID_COMMITTISH | \
152 GET_OID_TREE | GET_OID_TREEISH | \
153 GET_OID_BLOB)
155 enum get_oid_result {
156 FOUND = 0,
157 MISSING_OBJECT = -1, /* The requested object is missing */
158 SHORT_NAME_AMBIGUOUS = -2,
159 /* The following only apply when symlinks are followed */
160 DANGLING_SYMLINK = -4, /*
161 * The initial symlink is there, but
162 * (transitively) points to a missing
163 * in-tree file
165 SYMLINK_LOOP = -5,
166 NOT_DIR = -6, /*
167 * Somewhere along the symlink chain, a path is
168 * requested which contains a file as a
169 * non-final element.
173 /* A suitably aligned type for stack allocations of hash contexts. */
174 union git_hash_ctx {
175 git_SHA_CTX sha1;
176 git_SHA256_CTX sha256;
178 typedef union git_hash_ctx git_hash_ctx;
180 typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
181 typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
182 typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
183 typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
184 typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx);
186 struct git_hash_algo {
188 * The name of the algorithm, as appears in the config file and in
189 * messages.
191 const char *name;
193 /* A four-byte version identifier, used in pack indices. */
194 uint32_t format_id;
196 /* The length of the hash in binary. */
197 size_t rawsz;
199 /* The length of the hash in hex characters. */
200 size_t hexsz;
202 /* The block size of the hash. */
203 size_t blksz;
205 /* The hash initialization function. */
206 git_hash_init_fn init_fn;
208 /* The hash context cloning function. */
209 git_hash_clone_fn clone_fn;
211 /* The hash update function. */
212 git_hash_update_fn update_fn;
214 /* The hash finalization function. */
215 git_hash_final_fn final_fn;
217 /* The hash finalization function for object IDs. */
218 git_hash_final_oid_fn final_oid_fn;
220 /* The OID of the empty tree. */
221 const struct object_id *empty_tree;
223 /* The OID of the empty blob. */
224 const struct object_id *empty_blob;
226 /* The all-zeros OID. */
227 const struct object_id *null_oid;
229 extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
232 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
233 * the name doesn't match a known algorithm.
235 int hash_algo_by_name(const char *name);
236 /* Identical, except based on the format ID. */
237 int hash_algo_by_id(uint32_t format_id);
238 /* Identical, except based on the length. */
239 int hash_algo_by_length(int len);
240 /* Identical, except for a pointer to struct git_hash_algo. */
241 static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
243 return p - hash_algos;
246 const struct object_id *null_oid(void);
248 static inline int hashcmp_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
251 * Teach the compiler that there are only two possibilities of hash size
252 * here, so that it can optimize for this case as much as possible.
254 if (algop->rawsz == GIT_MAX_RAWSZ)
255 return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
256 return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
259 static inline int hasheq_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
262 * We write this here instead of deferring to hashcmp so that the
263 * compiler can properly inline it and avoid calling memcmp.
265 if (algop->rawsz == GIT_MAX_RAWSZ)
266 return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
267 return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
270 static inline void oidcpy(struct object_id *dst, const struct object_id *src)
272 memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
273 dst->algo = src->algo;
276 static inline struct object_id *oiddup(const struct object_id *src)
278 struct object_id *dst = xmalloc(sizeof(struct object_id));
279 oidcpy(dst, src);
280 return dst;
283 static inline void oid_set_algo(struct object_id *oid, const struct git_hash_algo *algop)
285 oid->algo = hash_algo_by_ptr(algop);
289 * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
290 * for use in hash tables. Cryptographic hashes are supposed to have
291 * uniform distribution, so in contrast to `memhash()`, this just copies
292 * the first `sizeof(int)` bytes without shuffling any bits. Note that
293 * the results will be different on big-endian and little-endian
294 * platforms, so they should not be stored or transferred over the net.
296 static inline unsigned int oidhash(const struct object_id *oid)
299 * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
300 * platforms that don't support unaligned reads.
302 unsigned int hash;
303 memcpy(&hash, oid->hash, sizeof(hash));
304 return hash;
307 const char *empty_tree_oid_hex(void);
308 const char *empty_blob_oid_hex(void);
310 #endif