blob: 5ca78a7744113b15ecb154f08ac0ecb524c86d66 [file] [log] [blame]
Patrick Steinhardte7da9382024-06-14 08:50:23 +02001#define USE_THE_REPOSITORY_VARIABLE
2
Elijah Newrenb73ecb42023-02-24 00:09:26 +00003#include "git-compat-util.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00004#include "hash.h"
Elijah Newrenb73ecb42023-02-24 00:09:26 +00005#include "hex.h"
Linus Torvaldsa5031212010-01-21 15:25:19 -08006
brian m. carlsondadacf12020-02-22 20:17:28 +00007static int get_hash_hex_algop(const char *hex, unsigned char *hash,
8 const struct git_hash_algo *algop)
Linus Torvaldsa5031212010-01-21 15:25:19 -08009{
10 int i;
brian m. carlsondadacf12020-02-22 20:17:28 +000011 for (i = 0; i < algop->rawsz; i++) {
René Scharfed2330972016-09-03 17:59:20 +020012 int val = hex2chr(hex);
13 if (val < 0)
Linus Torvaldsa5031212010-01-21 15:25:19 -080014 return -1;
brian m. carlsondadacf12020-02-22 20:17:28 +000015 *hash++ = val;
Linus Torvaldsa5031212010-01-21 15:25:19 -080016 hex += 2;
17 }
18 return 0;
19}
20
Junio C Hamano08e5fb12023-07-24 16:11:03 -070021int get_hash_hex(const char *hex, unsigned char *sha1)
brian m. carlsondadacf12020-02-22 20:17:28 +000022{
23 return get_hash_hex_algop(hex, sha1, the_hash_algo);
24}
25
26int get_oid_hex_algop(const char *hex, struct object_id *oid,
27 const struct git_hash_algo *algop)
28{
brian m. carlson5a6dce72021-04-26 01:02:55 +000029 int ret = get_hash_hex_algop(hex, oid->hash, algop);
Patrick Steinhardtc98d7622024-06-14 08:49:59 +020030 if (!ret) {
brian m. carlson5a6dce72021-04-26 01:02:55 +000031 oid_set_algo(oid, algop);
Patrick Steinhardtc98d7622024-06-14 08:49:59 +020032 if (algop->rawsz != GIT_MAX_RAWSZ)
33 memset(oid->hash + algop->rawsz, 0,
34 GIT_MAX_RAWSZ - algop->rawsz);
35 }
brian m. carlson5a6dce72021-04-26 01:02:55 +000036 return ret;
brian m. carlsondadacf12020-02-22 20:17:28 +000037}
38
brian m. carlson61e2a702020-02-22 20:17:29 +000039/*
40 * NOTE: This function relies on hash algorithms being in order from shortest
41 * length to longest length.
42 */
43int get_oid_hex_any(const char *hex, struct object_id *oid)
44{
45 int i;
46 for (i = GIT_HASH_NALGOS - 1; i > 0; i--) {
brian m. carlson5a6dce72021-04-26 01:02:55 +000047 if (!get_oid_hex_algop(hex, oid, &hash_algos[i]))
brian m. carlson61e2a702020-02-22 20:17:29 +000048 return i;
49 }
50 return GIT_HASH_UNKNOWN;
51}
52
brian m. carlsonaa1c6fd2015-03-13 23:39:28 +000053int get_oid_hex(const char *hex, struct object_id *oid)
54{
brian m. carlsondadacf12020-02-22 20:17:28 +000055 return get_oid_hex_algop(hex, oid, the_hash_algo);
56}
57
58int parse_oid_hex_algop(const char *hex, struct object_id *oid,
59 const char **end,
60 const struct git_hash_algo *algop)
61{
brian m. carlson5a6dce72021-04-26 01:02:55 +000062 int ret = get_oid_hex_algop(hex, oid, algop);
brian m. carlsondadacf12020-02-22 20:17:28 +000063 if (!ret)
64 *end = hex + algop->hexsz;
65 return ret;
brian m. carlsonaa1c6fd2015-03-13 23:39:28 +000066}
67
brian m. carlson61e2a702020-02-22 20:17:29 +000068int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end)
69{
70 int ret = get_oid_hex_any(hex, oid);
71 if (ret)
72 *end = hex + hash_algos[ret].hexsz;
73 return ret;
74}
75
brian m. carlson605f4302017-02-20 00:10:13 +000076int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
77{
brian m. carlsondadacf12020-02-22 20:17:28 +000078 return parse_oid_hex_algop(hex, oid, end, the_hash_algo);
brian m. carlson605f4302017-02-20 00:10:13 +000079}
80
brian m. carlson47edb642018-11-14 04:09:29 +000081char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
82 const struct git_hash_algo *algop)
Linus Torvaldsa5031212010-01-21 15:25:19 -080083{
Linus Torvaldsa5031212010-01-21 15:25:19 -080084 static const char hex[] = "0123456789abcdef";
Jeff Kingaf49c6d2015-09-24 17:05:45 -040085 char *buf = buffer;
Linus Torvaldsa5031212010-01-21 15:25:19 -080086 int i;
87
brian m. carlsonb8505ec2021-04-26 01:03:00 +000088 /*
89 * Our struct object_id has been memset to 0, so default to printing
90 * using the default hash.
91 */
92 if (algop == &hash_algos[0])
93 algop = the_hash_algo;
94
brian m. carlson47edb642018-11-14 04:09:29 +000095 for (i = 0; i < algop->rawsz; i++) {
96 unsigned int val = *hash++;
Linus Torvaldsa5031212010-01-21 15:25:19 -080097 *buf++ = hex[val >> 4];
98 *buf++ = hex[val & 0xf];
99 }
100 *buf = '\0';
101
102 return buffer;
103}
brian m. carlsonaa1c6fd2015-03-13 23:39:28 +0000104
brian m. carlson47edb642018-11-14 04:09:29 +0000105char *oid_to_hex_r(char *buffer, const struct object_id *oid)
106{
brian m. carlson3dd71462021-04-26 01:03:01 +0000107 return hash_to_hex_algop_r(buffer, oid->hash, &hash_algos[oid->algo]);
brian m. carlson47edb642018-11-14 04:09:29 +0000108}
109
110char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *algop)
Jeff Kingaf49c6d2015-09-24 17:05:45 -0400111{
112 static int bufno;
brian m. carlsondc015052017-03-26 16:01:24 +0000113 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
René Scharfebb847352016-10-23 19:57:30 +0200114 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
brian m. carlson47edb642018-11-14 04:09:29 +0000115 return hash_to_hex_algop_r(hexbuffer[bufno], hash, algop);
116}
117
brian m. carlson47edb642018-11-14 04:09:29 +0000118char *hash_to_hex(const unsigned char *hash)
119{
120 return hash_to_hex_algop(hash, the_hash_algo);
Jeff Kingaf49c6d2015-09-24 17:05:45 -0400121}
122
brian m. carlsonaa1c6fd2015-03-13 23:39:28 +0000123char *oid_to_hex(const struct object_id *oid)
124{
brian m. carlson3dd71462021-04-26 01:03:01 +0000125 return hash_to_hex_algop(oid->hash, &hash_algos[oid->algo]);
brian m. carlsonaa1c6fd2015-03-13 23:39:28 +0000126}