The fifth batch
[alt-git.git] / hex.c
blob865a232167553d6376c51d51604544cc71813fb1
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "hash.h"
5 #include "hex.h"
7 static int get_hash_hex_algop(const char *hex, unsigned char *hash,
8 const struct git_hash_algo *algop)
10 for (size_t i = 0; i < algop->rawsz; i++) {
11 int val = hex2chr(hex);
12 if (val < 0)
13 return -1;
14 *hash++ = val;
15 hex += 2;
17 return 0;
20 int get_hash_hex(const char *hex, unsigned char *sha1)
22 return get_hash_hex_algop(hex, sha1, the_hash_algo);
25 int get_oid_hex_algop(const char *hex, struct object_id *oid,
26 const struct git_hash_algo *algop)
28 int ret = get_hash_hex_algop(hex, oid->hash, algop);
29 if (!ret) {
30 oid_set_algo(oid, algop);
31 if (algop->rawsz != GIT_MAX_RAWSZ)
32 memset(oid->hash + algop->rawsz, 0,
33 GIT_MAX_RAWSZ - algop->rawsz);
35 return ret;
39 * NOTE: This function relies on hash algorithms being in order from shortest
40 * length to longest length.
42 int get_oid_hex_any(const char *hex, struct object_id *oid)
44 int i;
45 for (i = GIT_HASH_NALGOS - 1; i > 0; i--) {
46 if (!get_oid_hex_algop(hex, oid, &hash_algos[i]))
47 return i;
49 return GIT_HASH_UNKNOWN;
52 int get_oid_hex(const char *hex, struct object_id *oid)
54 return get_oid_hex_algop(hex, oid, the_hash_algo);
57 int parse_oid_hex_algop(const char *hex, struct object_id *oid,
58 const char **end,
59 const struct git_hash_algo *algop)
61 int ret = get_oid_hex_algop(hex, oid, algop);
62 if (!ret)
63 *end = hex + algop->hexsz;
64 return ret;
67 int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end)
69 int ret = get_oid_hex_any(hex, oid);
70 if (ret)
71 *end = hex + hash_algos[ret].hexsz;
72 return ret;
75 int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
77 return parse_oid_hex_algop(hex, oid, end, the_hash_algo);
80 char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
81 const struct git_hash_algo *algop)
83 static const char hex[] = "0123456789abcdef";
84 char *buf = buffer;
87 * Our struct object_id has been memset to 0, so default to printing
88 * using the default hash.
90 if (algop == &hash_algos[0])
91 algop = the_hash_algo;
93 for (size_t i = 0; i < algop->rawsz; i++) {
94 unsigned int val = *hash++;
95 *buf++ = hex[val >> 4];
96 *buf++ = hex[val & 0xf];
98 *buf = '\0';
100 return buffer;
103 char *oid_to_hex_r(char *buffer, const struct object_id *oid)
105 return hash_to_hex_algop_r(buffer, oid->hash, &hash_algos[oid->algo]);
108 char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *algop)
110 static int bufno;
111 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
112 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
113 return hash_to_hex_algop_r(hexbuffer[bufno], hash, algop);
116 char *hash_to_hex(const unsigned char *hash)
118 return hash_to_hex_algop(hash, the_hash_algo);
121 char *oid_to_hex(const struct object_id *oid)
123 return hash_to_hex_algop(oid->hash, &hash_algos[oid->algo]);