From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: <git@vger.kernel.org>
Subject: [RFC PATCH 2/6] hex: allow specifying hex type with hex2chr
Date: Wed, 29 Jul 2026 23:32:11 +0000 [thread overview]
Message-ID: <20260729233215.398654-3-sandals@crustytoothpaste.net> (raw)
In-Reply-To: <20260729233215.398654-1-sandals@crustytoothpaste.net>
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>
---
hex-ll.h | 6 +++---
hex.c | 2 +-
mailinfo.c | 2 +-
ref-filter.c | 2 +-
strbuf.c | 2 +-
url.c | 2 +-
urlmatch.c | 2 +-
7 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/hex-ll.h b/hex-ll.h
index da1b5239b2..26847c7b2f 100644
--- a/hex-ll.h
+++ b/hex-ll.h
@@ -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);
}
/*
diff --git a/hex.c b/hex.c
index f02832140d..6150bdcbf8 100644
--- a/hex.c
+++ b/hex.c
@@ -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;
diff --git a/mailinfo.c b/mailinfo.c
index 13949ff31e..85c3119048 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -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;
diff --git a/ref-filter.c b/ref-filter.c
index 29aca08ce7..884bcd8fc5 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -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;
diff --git a/strbuf.c b/strbuf.c
index 44955669e8..88d23f8ac5 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -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);
diff --git a/url.c b/url.c
index a59818278f..b4d72f784a 100644
--- a/url.c
+++ b/url.c
@@ -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;
diff --git a/urlmatch.c b/urlmatch.c
index 20bc2d009c..989f1d794b 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -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;
next prev parent reply other threads:[~2026-07-29 23:32 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-29 23:32 [RFC PATCH 0/6] Git 3.0: restrict hex object IDs to lowercase only brian m. carlson
2026-07-29 23:32 ` [RFC PATCH 1/6] hex: add functionality for lowercase-only hex brian m. carlson
2026-07-29 23:32 ` brian m. carlson [this message]
2026-07-29 23:32 ` [RFC PATCH 3/6] hex: make hex_to_bytes accept kind of hex to use brian m. carlson
2026-07-29 23:32 ` [RFC PATCH 4/6] hex: label usages of hex parsing for object IDs brian m. carlson
2026-07-29 23:32 ` [RFC PATCH 5/6] object-name: use hexval brian m. carlson
2026-07-29 23:32 ` [RFC PATCH 6/6] hex: allow only lowercase object IDs in breaking changes mode brian m. carlson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260729233215.398654-3-sandals@crustytoothpaste.net \
--to=sandals@crustytoothpaste.net \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox