From: Kurt Zeilenga Date: Mon, 25 Oct 1999 01:44:47 +0000 (+0000) Subject: Add macros to compute base64 encode/decode lengths. X-Git-Tag: UCDATA_2_4~324 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=f4a0699311a7436ccf19a704484724d00c964529;p=openldap Add macros to compute base64 encode/decode lengths. --- diff --git a/clients/tools/ldappasswd.c b/clients/tools/ldappasswd.c index ff4cf6420b..c6cc0bce18 100644 --- a/clients/tools/ldappasswd.c +++ b/clients/tools/ldappasswd.c @@ -41,8 +41,6 @@ #include "ldap_defaults.h" /* local macros */ -#define CEILING(x) ((double)(x) > (int)(x) ? (int)(x) + 1 : (int)(x)) - #define LDAP_PASSWD_ATTRIB "userPassword" #define LDAP_PASSWD_CONF LDAP_SYSCONFDIR LDAP_DIRSEP "passwd.conf" @@ -107,8 +105,10 @@ pw_encode (unsigned char *passwd, Salt * salt, unsigned int len) len += salt->len; } - b64_len = CEILING (len / 3) * 4 + 1; + b64_len = LUTIL_BASE64_ENCODE_LEN(len) + 1; + base64digest = (char *)malloc (b64_len); + if (lutil_b64_ntop (npasswd, len, base64digest, b64_len) < 0) { free (base64digest); diff --git a/include/lutil.h b/include/lutil.h index e79dfb2e4c..c58911a62c 100644 --- a/include/lutil.h +++ b/include/lutil.h @@ -19,6 +19,12 @@ * Include file for LDAP utility routine */ +/* n octets encode into ceiling(n/3) * 4 bytes */ +/* Avoid floating point math by through extra padding */ + +#define LUTIL_BASE64_ENCODE_LEN(n) ((n)/3 * 4 + 4) +#define LUTIL_BASE64_DECODE_LEN(n) ((n)/4 * 3) + LDAP_BEGIN_DECL /* ISC Base64 Routines */ diff --git a/libraries/liblutil/passwd.c b/libraries/liblutil/passwd.c index 6843b84848..4960add60d 100644 --- a/libraries/liblutil/passwd.c +++ b/libraries/liblutil/passwd.c @@ -109,7 +109,7 @@ lutil_passwd( if ((p = passwd_scheme( passwd, "{MD5}", schemes )) != NULL ) { lutil_MD5_CTX MD5context; unsigned char MD5digest[16]; - char base64digest[25]; /* ceiling(sizeof(input)/3) * 4 + 1 */ + char base64digest[LUTIL_BASE64_ENCODE_LEN(16)]; lutil_MD5Init(&MD5context); lutil_MD5Update(&MD5context, @@ -127,7 +127,7 @@ lutil_passwd( } else if ((p = passwd_scheme( passwd, "{SHA}", schemes )) != NULL ) { lutil_SHA1_CTX SHA1context; unsigned char SHA1digest[20]; - char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */ + char base64digest[LUTIL_BASE64_ENCODE_LEN(20)]; lutil_SHA1Init(&SHA1context); lutil_SHA1Update(&SHA1context, @@ -150,7 +150,8 @@ lutil_passwd( unsigned char *orig_pass = NULL; /* base64 un-encode password */ - orig_pass = (unsigned char *)malloc((size_t)(pw_len * 0.75 + 1)); + orig_pass = (unsigned char *) malloc( (size_t) ( + LUTIL_BASE64_DECODE_LEN(pw_len) + 1) ); if ((rc = lutil_b64_pton(p, orig_pass, pw_len)) < 0) { free(orig_pass); @@ -179,7 +180,8 @@ lutil_passwd( unsigned char *orig_pass = NULL; /* base64 un-encode password */ - orig_pass = (unsigned char *)malloc((size_t)(pw_len * 0.75 + 1)); + orig_pass = (unsigned char *) malloc( (size_t) ( + LUTIL_BASE64_DECODE_LEN(pw_len) + 1) ); if ((rc = lutil_b64_pton(p, orig_pass, pw_len)) < 0) { free(orig_pass);