Line | Count | Source |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "hash.h" |
5 | | #include "hex.h" |
6 | | |
7 | | static int get_hash_hex_algop(const char *hex, unsigned char *hash, |
8 | | const struct git_hash_algo *algop) |
9 | 0 | { |
10 | 0 | for (size_t i = 0; i < algop->rawsz; i++) { |
11 | 0 | int val = hex2chr(hex); |
12 | 0 | if (val < 0) |
13 | 0 | return -1; |
14 | 0 | *hash++ = val; |
15 | 0 | hex += 2; |
16 | 0 | } |
17 | 0 | return 0; |
18 | 0 | } |
19 | | |
20 | | int get_hash_hex(const char *hex, unsigned char *sha1) |
21 | 0 | { |
22 | 0 | return get_hash_hex_algop(hex, sha1, the_hash_algo); |
23 | 0 | } |
24 | | |
25 | | int get_oid_hex_algop(const char *hex, struct object_id *oid, |
26 | | const struct git_hash_algo *algop) |
27 | 0 | { |
28 | 0 | int ret = get_hash_hex_algop(hex, oid->hash, algop); |
29 | 0 | if (!ret) { |
30 | 0 | oid_set_algo(oid, algop); |
31 | 0 | if (algop->rawsz != GIT_MAX_RAWSZ) |
32 | 0 | memset(oid->hash + algop->rawsz, 0, |
33 | 0 | GIT_MAX_RAWSZ - algop->rawsz); |
34 | 0 | } |
35 | 0 | return ret; |
36 | 0 | } |
37 | | |
38 | | /* |
39 | | * NOTE: This function relies on hash algorithms being in order from shortest |
40 | | * length to longest length. |
41 | | */ |
42 | | int get_oid_hex_any(const char *hex, struct object_id *oid) |
43 | 0 | { |
44 | 0 | int i; |
45 | 0 | for (i = GIT_HASH_NALGOS - 1; i > 0; i--) { |
46 | 0 | if (!get_oid_hex_algop(hex, oid, &hash_algos[i])) |
47 | 0 | return i; |
48 | 0 | } |
49 | 0 | return GIT_HASH_UNKNOWN; |
50 | 0 | } |
51 | | |
52 | | int get_oid_hex(const char *hex, struct object_id *oid) |
53 | 0 | { |
54 | 0 | return get_oid_hex_algop(hex, oid, the_hash_algo); |
55 | 0 | } |
56 | | |
57 | | int parse_oid_hex_algop(const char *hex, struct object_id *oid, |
58 | | const char **end, |
59 | | const struct git_hash_algo *algop) |
60 | 0 | { |
61 | 0 | int ret = get_oid_hex_algop(hex, oid, algop); |
62 | 0 | if (!ret) |
63 | 0 | *end = hex + algop->hexsz; |
64 | 0 | return ret; |
65 | 0 | } |
66 | | |
67 | | int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end) |
68 | 0 | { |
69 | 0 | int ret = get_oid_hex_any(hex, oid); |
70 | 0 | if (ret) |
71 | 0 | *end = hex + hash_algos[ret].hexsz; |
72 | 0 | return ret; |
73 | 0 | } |
74 | | |
75 | | int parse_oid_hex(const char *hex, struct object_id *oid, const char **end) |
76 | 0 | { |
77 | 0 | return parse_oid_hex_algop(hex, oid, end, the_hash_algo); |
78 | 0 | } |
79 | | |
80 | | char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, |
81 | | const struct git_hash_algo *algop) |
82 | 0 | { |
83 | 0 | static const char hex[] = "0123456789abcdef"; |
84 | 0 | char *buf = buffer; |
85 | | |
86 | | /* |
87 | | * Our struct object_id has been memset to 0, so default to printing |
88 | | * using the default hash. |
89 | | */ |
90 | 0 | if (algop == &hash_algos[0]) |
91 | 0 | algop = the_hash_algo; |
92 | |
|
93 | 0 | for (size_t i = 0; i < algop->rawsz; i++) { |
94 | 0 | unsigned int val = *hash++; |
95 | 0 | *buf++ = hex[val >> 4]; |
96 | 0 | *buf++ = hex[val & 0xf]; |
97 | 0 | } |
98 | 0 | *buf = '\0'; |
99 | |
|
100 | 0 | return buffer; |
101 | 0 | } |
102 | | |
103 | | char *oid_to_hex_r(char *buffer, const struct object_id *oid) |
104 | 0 | { |
105 | 0 | return hash_to_hex_algop_r(buffer, oid->hash, &hash_algos[oid->algo]); |
106 | 0 | } |
107 | | |
108 | | char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *algop) |
109 | 0 | { |
110 | 0 | static int bufno; |
111 | 0 | static char hexbuffer[4][GIT_MAX_HEXSZ + 1]; |
112 | 0 | bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer); |
113 | 0 | return hash_to_hex_algop_r(hexbuffer[bufno], hash, algop); |
114 | 0 | } |
115 | | |
116 | | char *hash_to_hex(const unsigned char *hash) |
117 | 0 | { |
118 | 0 | return hash_to_hex_algop(hash, the_hash_algo); |
119 | 0 | } |
120 | | |
121 | | char *oid_to_hex(const struct object_id *oid) |
122 | 0 | { |
123 | 0 | return hash_to_hex_algop(oid->hash, &hash_algos[oid->algo]); |
124 | 0 | } |