From: Volker Lendecke Date: Fri, 20 Sep 2024 13:14:48 +0000 (+0200) Subject: lib: simplify smbpasswd_gethexpwd() with strhex_to_str() X-Git-Tag: tdb-1.4.13~559 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=282a5778fb60f3b0879959a9a6a0176a10a94ab0;p=thirdparty%2Fsamba.git lib: simplify smbpasswd_gethexpwd() with strhex_to_str() Signed-off-by: Volker Lendecke Reviewed-by: Ralph Boehme --- diff --git a/source4/lib/samba3/smbpasswd.c b/source4/lib/samba3/smbpasswd.c index ae361b72f5e..581c113e7c4 100644 --- a/source4/lib/samba3/smbpasswd.c +++ b/source4/lib/samba3/smbpasswd.c @@ -60,31 +60,27 @@ struct samr_Password *smbpasswd_gethexpwd(TALLOC_CTX *mem_ctx, const char *p) { - int i; - unsigned char lonybble, hinybble; - const char *hexchars = "0123456789ABCDEF"; - const char *p1, *p2; - struct samr_Password *pwd = talloc(mem_ctx, struct samr_Password); - - if (!p) return NULL; - - for (i = 0; i < (sizeof(pwd->hash) * 2); i += 2) - { - hinybble = toupper(p[i]); - lonybble = toupper(p[i + 1]); - - p1 = strchr_m(hexchars, hinybble); - p2 = strchr_m(hexchars, lonybble); - - if (!p1 || !p2) { - return NULL; - } - - hinybble = PTR_DIFF(p1, hexchars); - lonybble = PTR_DIFF(p2, hexchars); - - pwd->hash[i / 2] = (hinybble << 4) | lonybble; + struct samr_Password *pwd = NULL; + size_t len; + + if (p == NULL) { + return NULL; + } + + pwd = talloc(mem_ctx, struct samr_Password); + if (pwd == NULL) { + return NULL; } + + len = strhex_to_str((char *)pwd->hash, + sizeof(pwd->hash), + p, + sizeof(pwd->hash) * 2); + if (len != sizeof(pwd->hash)) { + TALLOC_FREE(pwd); + return NULL; + } + return pwd; }