#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"
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);
* 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 */
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,
} 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,
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);
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);