hex: allow specifying hex type with hex2chr

We have several places where we use hex2chr.  One of those is parsing
object IDs, but others decode quoted-printable or percent encoding.  All
of them accept both uppercase and lowercase hex.

In a future commit, we'll change some of these cases, so make hex2chr
accept the kind of encoding to use: lowercase only hex or any kind of
hex.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
brian m. carlson
2026-07-29 23:32:11 +00:00
committed by Junio C Hamano
parent fa632be563
commit f661df1054
7 changed files with 9 additions and 9 deletions

View File

@@ -17,10 +17,10 @@ static inline unsigned int hexval(unsigned char c, enum hexkind kind)
* Convert two consecutive hexadecimal digits into a char. Return a
* negative value on error. Don't run over the end of short strings.
*/
static inline int hex2chr(const char *s)
static inline int hex2chr(const char *s, enum hexkind kind)
{
unsigned int val = hexval(s[0], HEX_KIND_MIXED);
return (val & ~0xf) ? val : (val << 4) | hexval(s[1], HEX_KIND_MIXED);
unsigned int val = hexval(s[0], kind);
return (val & ~0xf) ? val : (val << 4) | hexval(s[1], kind);
}
/*

2
hex.c
View File

@@ -9,7 +9,7 @@ static int get_hash_hex_algop(const char *hex, unsigned char *hash,
const struct git_hash_algo *algop)
{
for (size_t i = 0; i < algop->rawsz; i++) {
int val = hex2chr(hex);
int val = hex2chr(hex, HEX_KIND_MIXED);
if (val < 0)
return -1;
*hash++ = val;

View File

@@ -396,7 +396,7 @@ static int decode_q_segment(struct strbuf *out, const struct strbuf *q_seg,
int ch, d = *in;
if (d == '\n' || !d)
break; /* drop trailing newline */
ch = hex2chr(in);
ch = hex2chr(in, HEX_KIND_MIXED);
if (ch >= 0) {
strbuf_addch(out, ch);
in += 2;

View File

@@ -3567,7 +3567,7 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting
if (cp[1] == '%')
cp++;
else {
int ch = hex2chr(cp + 1);
int ch = hex2chr(cp + 1, HEX_KIND_MIXED);
if (0 <= ch) {
strbuf_addch(s, ch);
cp += 3;

View File

@@ -457,7 +457,7 @@ size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder)
return 1;
case 'x':
/* %x00 == NUL, %x0a == LF, etc. */
ch = hex2chr(placeholder + 1);
ch = hex2chr(placeholder + 1, HEX_KIND_MIXED);
if (ch < 0)
return 0;
strbuf_addch(sb, ch);

2
url.c
View File

@@ -62,7 +62,7 @@ static char *url_decode_internal(const char **query, int len,
}
if (c == '%' && (len < 0 || len >= 3)) {
int val = hex2chr(q + 1);
int val = hex2chr(q + 1, HEX_KIND_MIXED);
if (0 < val) {
strbuf_addch(out, val);
q += 3;

View File

@@ -50,7 +50,7 @@ static int append_normalized_escapes(struct strbuf *buf,
if (ch == '%') {
if (from_len < 2)
return 0;
ch = hex2chr(from);
ch = hex2chr(from, HEX_KIND_MIXED);
if (ch < 0)
return 0;
from += 2;