brian m. carlson | f18f816 | 2017-03-11 22:28:18 +0000 | [diff] [blame] | 1 | #ifndef HASH_H |
| 2 | #define HASH_H |
| 3 | |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 4 | #include "hash-ll.h" |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 5 | #include "repository.h" |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 6 | |
Jeff King | c0566d7 | 2019-06-20 03:41:45 -0400 | [diff] [blame] | 7 | #define the_hash_algo the_repository->hash_algo |
| 8 | |
brian m. carlson | 5a6dce7 | 2021-04-26 01:02:55 +0000 | [diff] [blame] | 9 | static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2) |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 10 | { |
brian m. carlson | 5a6dce7 | 2021-04-26 01:02:55 +0000 | [diff] [blame] | 11 | return hashcmp_algop(sha1, sha2, the_hash_algo); |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 12 | } |
| 13 | |
brian m. carlson | 5a6dce7 | 2021-04-26 01:02:55 +0000 | [diff] [blame] | 14 | static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2) |
| 15 | { |
| 16 | const struct git_hash_algo *algop; |
| 17 | if (!oid1->algo) |
| 18 | algop = the_hash_algo; |
| 19 | else |
| 20 | algop = &hash_algos[oid1->algo]; |
| 21 | return hashcmp_algop(oid1->hash, oid2->hash, algop); |
| 22 | } |
| 23 | |
brian m. carlson | 5a6dce7 | 2021-04-26 01:02:55 +0000 | [diff] [blame] | 24 | static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2) |
| 25 | { |
| 26 | return hasheq_algop(sha1, sha2, the_hash_algo); |
| 27 | } |
| 28 | |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 29 | static inline int oideq(const struct object_id *oid1, const struct object_id *oid2) |
| 30 | { |
brian m. carlson | 5a6dce7 | 2021-04-26 01:02:55 +0000 | [diff] [blame] | 31 | const struct git_hash_algo *algop; |
| 32 | if (!oid1->algo) |
| 33 | algop = the_hash_algo; |
| 34 | else |
| 35 | algop = &hash_algos[oid1->algo]; |
| 36 | return hasheq_algop(oid1->hash, oid2->hash, algop); |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static inline int is_null_oid(const struct object_id *oid) |
| 40 | { |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 41 | return oideq(oid, null_oid()); |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src) |
| 45 | { |
| 46 | memcpy(sha_dst, sha_src, the_hash_algo->rawsz); |
| 47 | } |
| 48 | |
Matheus Tavares | 3d20ed2 | 2021-05-17 16:49:03 -0300 | [diff] [blame] | 49 | /* Like oidcpy() but zero-pads the unused bytes in dst's hash array. */ |
| 50 | static inline void oidcpy_with_padding(struct object_id *dst, |
Eric Wong | 90e07f0 | 2021-07-07 23:10:18 +0000 | [diff] [blame] | 51 | const struct object_id *src) |
Matheus Tavares | 3d20ed2 | 2021-05-17 16:49:03 -0300 | [diff] [blame] | 52 | { |
| 53 | size_t hashsz; |
| 54 | |
| 55 | if (!src->algo) |
| 56 | hashsz = the_hash_algo->rawsz; |
| 57 | else |
| 58 | hashsz = hash_algos[src->algo].rawsz; |
| 59 | |
| 60 | memcpy(dst->hash, src->hash, hashsz); |
| 61 | memset(dst->hash + hashsz, 0, GIT_MAX_RAWSZ - hashsz); |
| 62 | dst->algo = src->algo; |
| 63 | } |
| 64 | |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 65 | static inline void hashclr(unsigned char *hash) |
| 66 | { |
| 67 | memset(hash, 0, the_hash_algo->rawsz); |
| 68 | } |
| 69 | |
| 70 | static inline void oidclr(struct object_id *oid) |
| 71 | { |
| 72 | memset(oid->hash, 0, GIT_MAX_RAWSZ); |
brian m. carlson | 5a6dce7 | 2021-04-26 01:02:55 +0000 | [diff] [blame] | 73 | oid->algo = hash_algo_by_ptr(the_hash_algo); |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static inline void oidread(struct object_id *oid, const unsigned char *hash) |
| 77 | { |
| 78 | memcpy(oid->hash, hash, the_hash_algo->rawsz); |
brian m. carlson | 5a6dce7 | 2021-04-26 01:02:55 +0000 | [diff] [blame] | 79 | oid->algo = hash_algo_by_ptr(the_hash_algo); |
Jeff King | 3fa6f2a | 2020-12-04 13:51:39 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static inline int is_empty_blob_sha1(const unsigned char *sha1) |
| 83 | { |
| 84 | return hasheq(sha1, the_hash_algo->empty_blob->hash); |
| 85 | } |
| 86 | |
| 87 | static inline int is_empty_blob_oid(const struct object_id *oid) |
| 88 | { |
| 89 | return oideq(oid, the_hash_algo->empty_blob); |
| 90 | } |
| 91 | |
| 92 | static inline int is_empty_tree_sha1(const unsigned char *sha1) |
| 93 | { |
| 94 | return hasheq(sha1, the_hash_algo->empty_tree->hash); |
| 95 | } |
| 96 | |
| 97 | static inline int is_empty_tree_oid(const struct object_id *oid) |
| 98 | { |
| 99 | return oideq(oid, the_hash_algo->empty_tree); |
| 100 | } |
| 101 | |
brian m. carlson | f18f816 | 2017-03-11 22:28:18 +0000 | [diff] [blame] | 102 | #endif |