4 #include "repository.h"
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"
13 #include "block-sha1/sha1.h"
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>
24 #include "sha256/block/sha256.h"
27 #ifndef platform_SHA_CTX
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.
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
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
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
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
58 #ifdef platform_SHA256_Clone
59 #define git_SHA256_Clone platform_SHA256_Clone
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
68 static inline void git_SHA1_Clone(git_SHA_CTX
*dst
, const git_SHA_CTX
*src
)
70 memcpy(dst
, src
, sizeof(*dst
));
73 #ifndef SHA256_NEEDS_CLONE_HELPER
74 static inline void git_SHA256_Clone(git_SHA256_CTX
*dst
, const git_SHA256_CTX
*src
)
76 memcpy(dst
, src
, sizeof(*dst
));
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.
88 /* An unknown hash function. */
89 #define GIT_HASH_UNKNOWN 0
91 #define GIT_HASH_SHA1 1
93 #define GIT_HASH_SHA256 2
94 /* Number of algorithms supported (including unknown). */
95 #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
97 /* "sha1", big-endian */
98 #define GIT_SHA1_FORMAT_ID 0x73686131
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
106 /* "s256", big-endian */
107 #define GIT_SHA256_FORMAT_ID 0x73323536
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
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
122 unsigned char hash
[GIT_MAX_RAWSZ
];
123 int algo
; /* XXX requires 4-byte alignment */
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
137 #define GET_OID_DISAMBIGUATORS \
138 (GET_OID_COMMIT | GET_OID_COMMITTISH | \
139 GET_OID_TREE | GET_OID_TREEISH | \
142 enum get_oid_result
{
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
154 * Somewhere along the symlink chain, a path is
155 * requested which contains a file as a
160 /* A suitably aligned type for stack allocations of hash contexts. */
163 git_SHA256_CTX sha256
;
165 typedef union git_hash_ctx git_hash_ctx
;
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
);
173 struct git_hash_algo
{
175 * The name of the algorithm, as appears in the config file and in
180 /* A four-byte version identifier, used in pack indices. */
183 /* The length of the hash in binary. */
186 /* The length of the hash in hex characters. */
189 /* The block size of the hash. */
192 /* The hash initialization function. */
193 git_hash_init_fn init_fn
;
195 /* The hash context cloning function. */
196 git_hash_clone_fn clone_fn
;
198 /* The hash update function. */
199 git_hash_update_fn update_fn
;
201 /* The hash finalization function. */
202 git_hash_final_fn final_fn
;
204 /* The hash finalization function for object IDs. */
205 git_hash_final_oid_fn final_oid_fn
;
207 /* The OID of the empty tree. */
208 const struct object_id
*empty_tree
;
210 /* The OID of the empty blob. */
211 const struct object_id
*empty_blob
;
213 /* The all-zeros OID. */
214 const struct object_id
*null_oid
;
216 extern const struct git_hash_algo hash_algos
[GIT_HASH_NALGOS
];
219 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
220 * the name doesn't match a known algorithm.
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
)
230 return p
- hash_algos
;
233 #define the_hash_algo the_repository->hash_algo
235 const struct object_id
*null_oid(void);
237 static inline int hashcmp_algop(const unsigned char *sha1
, const unsigned char *sha2
, const struct git_hash_algo
*algop
)
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.
243 if (algop
->rawsz
== GIT_MAX_RAWSZ
)
244 return memcmp(sha1
, sha2
, GIT_MAX_RAWSZ
);
245 return memcmp(sha1
, sha2
, GIT_SHA1_RAWSZ
);
248 static inline int hashcmp(const unsigned char *sha1
, const unsigned char *sha2
)
250 return hashcmp_algop(sha1
, sha2
, the_hash_algo
);
253 static inline int oidcmp(const struct object_id
*oid1
, const struct object_id
*oid2
)
255 const struct git_hash_algo
*algop
;
257 algop
= the_hash_algo
;
259 algop
= &hash_algos
[oid1
->algo
];
260 return hashcmp_algop(oid1
->hash
, oid2
->hash
, algop
);
263 static inline int hasheq_algop(const unsigned char *sha1
, const unsigned char *sha2
, const struct git_hash_algo
*algop
)
266 * We write this here instead of deferring to hashcmp so that the
267 * compiler can properly inline it and avoid calling memcmp.
269 if (algop
->rawsz
== GIT_MAX_RAWSZ
)
270 return !memcmp(sha1
, sha2
, GIT_MAX_RAWSZ
);
271 return !memcmp(sha1
, sha2
, GIT_SHA1_RAWSZ
);
274 static inline int hasheq(const unsigned char *sha1
, const unsigned char *sha2
)
276 return hasheq_algop(sha1
, sha2
, the_hash_algo
);
279 static inline int oideq(const struct object_id
*oid1
, const struct object_id
*oid2
)
281 const struct git_hash_algo
*algop
;
283 algop
= the_hash_algo
;
285 algop
= &hash_algos
[oid1
->algo
];
286 return hasheq_algop(oid1
->hash
, oid2
->hash
, algop
);
289 static inline int is_null_oid(const struct object_id
*oid
)
291 return oideq(oid
, null_oid());
294 static inline void hashcpy(unsigned char *sha_dst
, const unsigned char *sha_src
)
296 memcpy(sha_dst
, sha_src
, the_hash_algo
->rawsz
);
299 static inline void oidcpy(struct object_id
*dst
, const struct object_id
*src
)
301 memcpy(dst
->hash
, src
->hash
, GIT_MAX_RAWSZ
);
302 dst
->algo
= src
->algo
;
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
)
312 hashsz
= the_hash_algo
->rawsz
;
314 hashsz
= hash_algos
[src
->algo
].rawsz
;
316 memcpy(dst
->hash
, src
->hash
, hashsz
);
317 memset(dst
->hash
+ hashsz
, 0, GIT_MAX_RAWSZ
- hashsz
);
318 dst
->algo
= src
->algo
;
321 static inline struct object_id
*oiddup(const struct object_id
*src
)
323 struct object_id
*dst
= xmalloc(sizeof(struct object_id
));
328 static inline void hashclr(unsigned char *hash
)
330 memset(hash
, 0, the_hash_algo
->rawsz
);
333 static inline void oidclr(struct object_id
*oid
)
335 memset(oid
->hash
, 0, GIT_MAX_RAWSZ
);
336 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
339 static inline void oidread(struct object_id
*oid
, const unsigned char *hash
)
341 memcpy(oid
->hash
, hash
, the_hash_algo
->rawsz
);
342 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
345 static inline int is_empty_blob_sha1(const unsigned char *sha1
)
347 return hasheq(sha1
, the_hash_algo
->empty_blob
->hash
);
350 static inline int is_empty_blob_oid(const struct object_id
*oid
)
352 return oideq(oid
, the_hash_algo
->empty_blob
);
355 static inline int is_empty_tree_sha1(const unsigned char *sha1
)
357 return hasheq(sha1
, the_hash_algo
->empty_tree
->hash
);
360 static inline int is_empty_tree_oid(const struct object_id
*oid
)
362 return oideq(oid
, the_hash_algo
->empty_tree
);
365 static inline void oid_set_algo(struct object_id
*oid
, const struct git_hash_algo
*algop
)
367 oid
->algo
= hash_algo_by_ptr(algop
);
370 const char *empty_tree_oid_hex(void);
371 const char *empty_blob_oid_hex(void);