]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: simplify smbpasswd_gethexpwd() with strhex_to_str()
authorVolker Lendecke <vl@samba.org>
Fri, 20 Sep 2024 13:14:48 +0000 (15:14 +0200)
committerRalph Boehme <slow@samba.org>
Tue, 12 Nov 2024 12:09:34 +0000 (12:09 +0000)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source4/lib/samba3/smbpasswd.c

index ae361b72f5e1370c40c5f38ccf7452b21e0b4c2a..581c113e7c462aee9134cc6ea9ab076d364cd485 100644 (file)
 
 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;
 }