From: Volker Lendecke Date: Tue, 10 Sep 2024 06:45:31 +0000 (+0200) Subject: libreplace: Introduce hexchars_{upper|lower} X-Git-Tag: tdb-1.4.13~554 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd5562bee7d1bf503f0adc9347a9f7c16cf3cc22;p=thirdparty%2Fsamba.git libreplace: Introduce hexchars_{upper|lower} We use that in quite a few places in our code. Signed-off-by: Volker Lendecke Reviewed-by: Ralph Boehme --- diff --git a/lib/replace/inet_pton.c b/lib/replace/inet_pton.c index 80e4865ef49..9e461d770ab 100644 --- a/lib/replace/inet_pton.c +++ b/lib/replace/inet_pton.c @@ -131,8 +131,6 @@ inet_pton6(src, dst) const char *src; unsigned char *dst; { - static const char xdigits_l[] = "0123456789abcdef", - xdigits_u[] = "0123456789ABCDEF"; unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; const char *xdigits, *curtok; int ch, saw_xdigit; @@ -151,8 +149,8 @@ inet_pton6(src, dst) while ((ch = *src++) != '\0') { const char *pch; - if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) - pch = strchr((xdigits = xdigits_u), ch); + if ((pch = strchr((xdigits = hexchars_lower), ch)) == NULL) + pch = strchr((xdigits = hexchars_upper), ch); if (pch != NULL) { val <<= 4; val |= (pch - xdigits); diff --git a/lib/replace/replace.c b/lib/replace/replace.c index 68e0a38bca6..f7f26712614 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -1231,3 +1231,6 @@ int rep_renameat2(int __oldfd, const char *__old, int __newfd, return renameat(__oldfd, __old, __newfd, __new); } #endif /* ! HAVE_RENAMEAT2 */ + +const char hexchars_lower[] = "0123456789abcdef"; +const char hexchars_upper[] = "0123456789ABCDEF"; diff --git a/lib/replace/replace.h b/lib/replace/replace.h index 586f4e0a575..b4bc1327824 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -1101,6 +1101,9 @@ static inline bool hex_byte(const char *in, uint8_t *out) return ok; } +extern const char hexchars_lower[]; +extern const char hexchars_upper[]; + /* Needed for Solaris atomic_add_XX functions. */ #if defined(HAVE_SYS_ATOMIC_H) #include diff --git a/lib/replace/snprintf.c b/lib/replace/snprintf.c index dd878fca113..63b0882e33f 100644 --- a/lib/replace/snprintf.c +++ b/lib/replace/snprintf.c @@ -832,9 +832,8 @@ static void fmtint(char *buffer, size_t *currlen, size_t maxlen, if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ do { - convert[place++] = - (caps? "0123456789ABCDEF":"0123456789abcdef") - [uvalue % (unsigned)base ]; + convert[place++] = (caps ? hex_upper + : hex_lower)[uvalue % (unsigned)base]; uvalue = (uvalue / (unsigned)base ); } while(uvalue && (place < sizeof(convert))); if (place == sizeof(convert)) place--; @@ -1028,8 +1027,8 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, idx = (int) ((temp -intpart +0.05)* 10.0); /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ /* printf ("%llf, %f, %x\n", temp, intpart, idx); */ - iconvert[iplace++] = - (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; + iconvert[iplace++] = (caps ? hexchars_upper + : hexchars_lower)[idx]; } while (intpart && (iplace < 311)); if (iplace == 311) iplace--; iconvert[iplace] = 0; @@ -1043,8 +1042,8 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, idx = (int) ((temp -fracpart +0.05)* 10.0); /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */ /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */ - fconvert[fplace++] = - (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; + fconvert[fplace++] = (caps ? hexchars_upper + : hexchars_lower)[idx]; } while(fracpart && (fplace < 311)); if (fplace == 311) fplace--; }