From d23309733a5b2a9e1adc304ee50c5a5ed7a087c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 3 Sep 2016 17:59:20 +0200 Subject: introduce hex2chr() for converting two hexadecimal digits to a character Add and use a helper function that decodes the char value of two hexadecimal digits. It returns a negative number on error, avoids running over the end of the given string and doesn't shift negative values. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- cache.h | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'cache.h') diff --git a/cache.h b/cache.h index c141b3ca0..81af2c2a6 100644 --- a/cache.h +++ b/cache.h @@ -1133,6 +1133,16 @@ static inline unsigned int hexval(unsigned char c) return hexval_table[c]; } +/* + * 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) +{ + int val = hexval(s[0]); + return (val < 0) ? val : (val << 4) | hexval(s[1]); +} + /* Convert to/from hex/sha1 representation */ #define MINIMUM_ABBREV minimum_abbrev #define DEFAULT_ABBREV default_abbrev -- cgit v1.2.1