]> git.ipfire.org Git - thirdparty/git.git/blame - hash.h
The twentieth batch
[thirdparty/git.git] / hash.h
CommitLineData
f18f816c 1#ifndef HASH_H
2#define HASH_H
3
d1cbe1e6 4#include "hash-ll.h"
3fa6f2aa 5#include "repository.h"
f50e766b 6
c0566d78
JK
7#define the_hash_algo the_repository->hash_algo
8
5a6dce70 9static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
10{
11 return hashcmp_algop(sha1, sha2, the_hash_algo);
12}
13
3fa6f2aa
JK
14static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
15{
5a6dce70 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);
3fa6f2aa
JK
22}
23
5a6dce70 24static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
25{
26 return hasheq_algop(sha1, sha2, the_hash_algo);
27}
28
3fa6f2aa
JK
29static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
30{
5a6dce70 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);
3fa6f2aa
JK
37}
38
39static inline int is_null_oid(const struct object_id *oid)
40{
14228447 41 return oideq(oid, null_oid());
3fa6f2aa
JK
42}
43
44static 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
3d20ed27
MT
49/* Like oidcpy() but zero-pads the unused bytes in dst's hash array. */
50static inline void oidcpy_with_padding(struct object_id *dst,
90e07f0a 51 const struct object_id *src)
3d20ed27
MT
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
3fa6f2aa
JK
65static inline void hashclr(unsigned char *hash)
66{
67 memset(hash, 0, the_hash_algo->rawsz);
68}
69
70static inline void oidclr(struct object_id *oid)
71{
72 memset(oid->hash, 0, GIT_MAX_RAWSZ);
5a6dce70 73 oid->algo = hash_algo_by_ptr(the_hash_algo);
3fa6f2aa
JK
74}
75
095261a1 76static inline void oidread_algop(struct object_id *oid, const unsigned char *hash, const struct git_hash_algo *algop)
77{
78 memcpy(oid->hash, hash, algop->rawsz);
79 oid->algo = hash_algo_by_ptr(algop);
80}
81
3fa6f2aa
JK
82static inline void oidread(struct object_id *oid, const unsigned char *hash)
83{
095261a1 84 oidread_algop(oid, hash, the_hash_algo);
3fa6f2aa
JK
85}
86
87static inline int is_empty_blob_sha1(const unsigned char *sha1)
88{
89 return hasheq(sha1, the_hash_algo->empty_blob->hash);
90}
91
92static inline int is_empty_blob_oid(const struct object_id *oid)
93{
94 return oideq(oid, the_hash_algo->empty_blob);
95}
96
97static inline int is_empty_tree_sha1(const unsigned char *sha1)
98{
99 return hasheq(sha1, the_hash_algo->empty_tree->hash);
100}
101
102static inline int is_empty_tree_oid(const struct object_id *oid)
103{
104 return oideq(oid, the_hash_algo->empty_tree);
105}
106
f18f816c 107#endif