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