Coverage Report

Created: 2026-10-17 07:15

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