diff --git a/acconfig.h b/acconfig.h
index 07127f04cb91f92ab8442036306a0249e0917a5e..a5c4e73038bd8f1518198a59cc97525e6bbe36fc 100644
--- a/acconfig.h
+++ b/acconfig.h
@@ -242,6 +242,13 @@
 	LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 	LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 */
+/* These are defined in lber_types.h */
+/*
+	LBER_INT_T
+	LBER_LEN_T
+	LBER_SOCKET_T
+	LBER_TAG_T
+*/
 
 
 /* Leave that blank line there!!  Autoheader needs it. */
diff --git a/clients/finger/main.c b/clients/finger/main.c
index 352c2ccd79d48209fb3af05446722828bb46ef2c..d172380fad0405397468c8a91f5997b2d934d9b9 100644
--- a/clients/finger/main.c
+++ b/clients/finger/main.c
@@ -411,7 +411,7 @@ do_search( LDAP *ld, char *buf )
 
 
 static int
-entry2textwrite( void *fp, char *buf, int len )
+entry2textwrite( void *fp, char *buf, ber_len_t len )
 {
 	return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
 }
diff --git a/clients/gopher/go500.c b/clients/gopher/go500.c
index b8d148818f1f1f81a94af9e0ab92d3ea7d47903f..323b9fbe42a038f832ff3663df90f8ccd38a7793 100644
--- a/clients/gopher/go500.c
+++ b/clients/gopher/go500.c
@@ -569,7 +569,7 @@ do_search( LDAP *ld, FILE *fp, char *buf )
 }
 
 static int
-entry2textwrite( void *fp, char *buf, int len )
+entry2textwrite( void *fp, char *buf, ber_len_t len )
 {
 	return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
 }
diff --git a/clients/gopher/go500gw.c b/clients/gopher/go500gw.c
index 72825929a921a2f91b782e3fef1113913b2a4b2d..9f8935fcd57f30a0a47888a24023b02559ea4fcc 100644
--- a/clients/gopher/go500gw.c
+++ b/clients/gopher/go500gw.c
@@ -75,7 +75,7 @@ static void do_list	(LDAP *ld, FILE *fp, char *dn);
 static int  isoc	( char **ocl, char *oc );
 static int  make_scope	( LDAP *ld, char *dn );
 static void do_search	(LDAP *ld, FILE *fp, char *query);
-static int  entry2textwrite( void *fp, char *buf, int len );
+static int  entry2textwrite( void *fp, char *buf, ber_len_t len );
 static void do_read	(LDAP *ld, FILE *fp, char *dn);
 static void do_help	(FILE *op);
 static void do_sizelimit(FILE *fp, char type);
@@ -851,7 +851,7 @@ do_search( LDAP *ld, FILE *fp, char *query )
 
 
 static int
-entry2textwrite( void *fp, char *buf, int len )
+entry2textwrite( void *fp, char *buf, ber_len_t len )
 {
         return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
 }
diff --git a/clients/rcpt500/query.c b/clients/rcpt500/query.c
index 6094e155c01e32b4e13ae1b980e8ef78a95dbd4d..53b55729d629939fd288e3c4ae43e96abf56b972 100644
--- a/clients/rcpt500/query.c
+++ b/clients/rcpt500/query.c
@@ -30,7 +30,7 @@ static char *errpreface = "Your query failed: ";
 
 static void close_ldap(LDAP *ld);
 static void append_entry_list(char *rep, char *qu, LDAP *ld, LDAPMessage *msg);
-static int  append_text(void *reply, char *text, int len);
+static int  append_text(void *reply, char *text, ber_len_t len);
 static int  do_read (LDAP *ld, char *dn, char *rep, struct ldap_disptmpl *tmp);
 static void report_ldap_err (LDAP *ldp, char *reply);
 static void remove_trailing_space (char *s);
@@ -290,7 +290,7 @@ append_entry_list( char *reply, char *query, LDAP *ldp, LDAPMessage *ldmsgp )
 
 
 static int
-append_text( void *reply, char *text, int len )
+append_text( void *reply, char *text, ber_len_t len )
 {
     strcat( (char *) reply, text );
     return( len );
diff --git a/configure b/configure
index 29cd543254733c7c2a8ddae5706a68c8097c8f4c..bcbd0da26405451c34ea3839ae299a02eb3a5a8a 100755
--- a/configure
+++ b/configure
@@ -10859,16 +10859,30 @@ if test $cross_compiling = yes ; then
 #define CROSS_COMPILING 1
 EOF
 
+
+	cat >> confdefs.h <<\EOF
+#define LBER_INT_T long
+EOF
+
+	cat >> confdefs.h <<\EOF
+#define LBER_TAG_T long
+EOF
+
+	cat >> confdefs.h <<\EOF
+#define LBER_SOCKET_T int
+EOF
+
+
 else
 	echo $ac_n "checking whether byte ordering is bigendian""... $ac_c" 1>&6
-echo "configure:10865: checking whether byte ordering is bigendian" >&5
+echo "configure:10879: checking whether byte ordering is bigendian" >&5
 if eval "test \"`echo '$''{'ac_cv_c_bigendian'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   ac_cv_c_bigendian=unknown
 # See if sys/param.h defines the BYTE_ORDER macro.
 cat > conftest.$ac_ext <<EOF
-#line 10872 "configure"
+#line 10886 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/param.h>
@@ -10879,11 +10893,11 @@ int main() {
 #endif
 ; return 0; }
 EOF
-if { (eval echo configure:10883: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:10897: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   # It does; now see whether it defined to BIG_ENDIAN or not.
 cat > conftest.$ac_ext <<EOF
-#line 10887 "configure"
+#line 10901 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/param.h>
@@ -10894,7 +10908,7 @@ int main() {
 #endif
 ; return 0; }
 EOF
-if { (eval echo configure:10898: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:10912: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_bigendian=yes
 else
@@ -10914,7 +10928,7 @@ if test "$cross_compiling" = yes; then
     { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; }
 else
   cat > conftest.$ac_ext <<EOF
-#line 10918 "configure"
+#line 10932 "configure"
 #include "confdefs.h"
 main () {
   /* Are we little or big endian?  From Harbison&Steele.  */
@@ -10927,7 +10941,7 @@ main () {
   exit (u.c[sizeof (long) - 1] == 1);
 }
 EOF
-if { (eval echo configure:10931: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:10945: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
 then
   ac_cv_c_bigendian=no
 else
@@ -10951,7 +10965,7 @@ EOF
 fi
 
 	echo $ac_n "checking size of short""... $ac_c" 1>&6
-echo "configure:10955: checking size of short" >&5
+echo "configure:10969: checking size of short" >&5
 if eval "test \"`echo '$''{'ac_cv_sizeof_short'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -10959,7 +10973,7 @@ else
     { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; }
 else
   cat > conftest.$ac_ext <<EOF
-#line 10963 "configure"
+#line 10977 "configure"
 #include "confdefs.h"
 #include <stdio.h>
 main()
@@ -10970,7 +10984,7 @@ main()
   exit(0);
 }
 EOF
-if { (eval echo configure:10974: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:10988: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
 then
   ac_cv_sizeof_short=`cat conftestval`
 else
@@ -10990,7 +11004,7 @@ EOF
 
  
 	echo $ac_n "checking size of int""... $ac_c" 1>&6
-echo "configure:10994: checking size of int" >&5
+echo "configure:11008: checking size of int" >&5
 if eval "test \"`echo '$''{'ac_cv_sizeof_int'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -10998,7 +11012,7 @@ else
     { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; }
 else
   cat > conftest.$ac_ext <<EOF
-#line 11002 "configure"
+#line 11016 "configure"
 #include "confdefs.h"
 #include <stdio.h>
 main()
@@ -11009,7 +11023,7 @@ main()
   exit(0);
 }
 EOF
-if { (eval echo configure:11013: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:11027: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
 then
   ac_cv_sizeof_int=`cat conftestval`
 else
@@ -11029,7 +11043,7 @@ EOF
 
  
 	echo $ac_n "checking size of long""... $ac_c" 1>&6
-echo "configure:11033: checking size of long" >&5
+echo "configure:11047: checking size of long" >&5
 if eval "test \"`echo '$''{'ac_cv_sizeof_long'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -11037,7 +11051,7 @@ else
     { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; }
 else
   cat > conftest.$ac_ext <<EOF
-#line 11041 "configure"
+#line 11055 "configure"
 #include "confdefs.h"
 #include <stdio.h>
 main()
@@ -11048,7 +11062,7 @@ main()
   exit(0);
 }
 EOF
-if { (eval echo configure:11052: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:11066: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
 then
   ac_cv_sizeof_long=`cat conftestval`
 else
@@ -11070,11 +11084,42 @@ EOF
 
 	if test "$ac_cv_sizeof_int" -lt 4 ; then
 		echo "configure: warning: OpenLDAP requires 'int' to be 32 bits or greater." 1>&2
+
+		cat >> confdefs.h <<\EOF
+#define LBER_INT_T long
+EOF
+
+		cat >> confdefs.h <<\EOF
+#define LBER_TAG_T long
+EOF
+
+		cat >> confdefs.h <<\EOF
+#define LBER_SOCKET_T int
+EOF
+
+	else
+		cat >> confdefs.h <<\EOF
+#define LBER_INT_T int
+EOF
+
+		cat >> confdefs.h <<\EOF
+#define LBER_TAG_T long
+EOF
+
+		cat >> confdefs.h <<\EOF
+#define LBER_SOCKET_T int
+EOF
+
 	fi
 fi
 
+cat >> confdefs.h <<\EOF
+#define LBER_LEN_T long
+EOF
+
+
 echo $ac_n "checking for 8-bit clean memcmp""... $ac_c" 1>&6
-echo "configure:11078: checking for 8-bit clean memcmp" >&5
+echo "configure:11123: checking for 8-bit clean memcmp" >&5
 if eval "test \"`echo '$''{'ac_cv_func_memcmp_clean'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -11082,7 +11127,7 @@ else
   ac_cv_func_memcmp_clean=no
 else
   cat > conftest.$ac_ext <<EOF
-#line 11086 "configure"
+#line 11131 "configure"
 #include "confdefs.h"
 
 main()
@@ -11092,7 +11137,7 @@ main()
 }
 
 EOF
-if { (eval echo configure:11096: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:11141: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
 then
   ac_cv_func_memcmp_clean=yes
 else
@@ -11110,12 +11155,12 @@ echo "$ac_t""$ac_cv_func_memcmp_clean" 1>&6
 test $ac_cv_func_memcmp_clean = no && LIBOBJS="$LIBOBJS memcmp.${ac_objext}"
 
 echo $ac_n "checking for strftime""... $ac_c" 1>&6
-echo "configure:11114: checking for strftime" >&5
+echo "configure:11159: checking for strftime" >&5
 if eval "test \"`echo '$''{'ac_cv_func_strftime'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 11119 "configure"
+#line 11164 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char strftime(); below.  */
@@ -11138,7 +11183,7 @@ strftime();
 
 ; return 0; }
 EOF
-if { (eval echo configure:11142: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:11187: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_strftime=yes"
 else
@@ -11160,7 +11205,7 @@ else
   echo "$ac_t""no" 1>&6
 # strftime is in -lintl on SCO UNIX.
 echo $ac_n "checking for strftime in -lintl""... $ac_c" 1>&6
-echo "configure:11164: checking for strftime in -lintl" >&5
+echo "configure:11209: checking for strftime in -lintl" >&5
 ac_lib_var=`echo intl'_'strftime | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -11168,7 +11213,7 @@ else
   ac_save_LIBS="$LIBS"
 LIBS="-lintl  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 11172 "configure"
+#line 11217 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -11179,7 +11224,7 @@ int main() {
 strftime()
 ; return 0; }
 EOF
-if { (eval echo configure:11183: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:11228: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -11207,12 +11252,12 @@ fi
 
 
 echo $ac_n "checking for vprintf""... $ac_c" 1>&6
-echo "configure:11211: checking for vprintf" >&5
+echo "configure:11256: checking for vprintf" >&5
 if eval "test \"`echo '$''{'ac_cv_func_vprintf'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 11216 "configure"
+#line 11261 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char vprintf(); below.  */
@@ -11235,7 +11280,7 @@ vprintf();
 
 ; return 0; }
 EOF
-if { (eval echo configure:11239: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:11284: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_vprintf=yes"
 else
@@ -11259,12 +11304,12 @@ fi
 
 if test "$ac_cv_func_vprintf" != yes; then
 echo $ac_n "checking for _doprnt""... $ac_c" 1>&6
-echo "configure:11263: checking for _doprnt" >&5
+echo "configure:11308: checking for _doprnt" >&5
 if eval "test \"`echo '$''{'ac_cv_func__doprnt'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 11268 "configure"
+#line 11313 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char _doprnt(); below.  */
@@ -11287,7 +11332,7 @@ _doprnt();
 
 ; return 0; }
 EOF
-if { (eval echo configure:11291: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:11336: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func__doprnt=yes"
 else
@@ -11316,12 +11361,12 @@ if test $ac_cv_func_vprintf = yes ; then
 		for ac_func in vsnprintf vsprintf
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:11320: checking for $ac_func" >&5
+echo "configure:11365: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 11325 "configure"
+#line 11370 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -11344,7 +11389,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:11348: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:11393: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -11413,12 +11458,12 @@ for ac_func in \
 
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:11417: checking for $ac_func" >&5
+echo "configure:11462: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 11422 "configure"
+#line 11467 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -11441,7 +11486,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:11445: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:11490: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -11469,12 +11514,12 @@ done
 for ac_func in getopt tempnam
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:11473: checking for $ac_func" >&5
+echo "configure:11518: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 11478 "configure"
+#line 11523 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -11497,7 +11542,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:11501: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:11546: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -11527,13 +11572,13 @@ done
 # Check Configuration
 
 echo $ac_n "checking declaration of sys_errlist""... $ac_c" 1>&6
-echo "configure:11531: checking declaration of sys_errlist" >&5
+echo "configure:11576: checking declaration of sys_errlist" >&5
 if eval "test \"`echo '$''{'ol_cv_dcl_sys_errlist'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   
 	cat > conftest.$ac_ext <<EOF
-#line 11537 "configure"
+#line 11582 "configure"
 #include "confdefs.h"
 
 #include <stdio.h>
@@ -11543,7 +11588,7 @@ int main() {
 char *c = (char *) *sys_errlist
 ; return 0; }
 EOF
-if { (eval echo configure:11547: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:11592: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ol_cv_dcl_sys_errlist=yes
 else
@@ -11563,20 +11608,20 @@ if test $ol_cv_dcl_sys_errlist = no ; then
 EOF
 
 	echo $ac_n "checking existence of sys_errlist""... $ac_c" 1>&6
-echo "configure:11567: checking existence of sys_errlist" >&5
+echo "configure:11612: checking existence of sys_errlist" >&5
 	if eval "test \"`echo '$''{'ol_cv_have_sys_errlist'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   
 		cat > conftest.$ac_ext <<EOF
-#line 11573 "configure"
+#line 11618 "configure"
 #include "confdefs.h"
 #include <errno.h>
 int main() {
 char *c = (char *) *sys_errlist
 ; return 0; }
 EOF
-if { (eval echo configure:11580: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:11625: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   ol_cv_have_sys_errlist=yes
 else
@@ -11889,7 +11934,7 @@ tests/progs/Makefile:build/top.mk:tests/progs/Makefile.in:build/rules.mk \
 contrib/Makefile:build/top.mk:contrib/Makefile.in:build/dir.mk \
 contrib/saucer/Makefile:build/top.mk:contrib/saucer/Makefile.in:build/rules.mk \
 contrib/web_ldap/Makefile:build/top.mk:contrib/web_ldap/Makefile.in:build/rules.mk \
- include/portable.h include/ldap_features.h" | sed "s/:[^ ]*//g"` conftest*; exit 1' 1 2 15
+ include/portable.h include/ldap_features.h include/lber_types.h" | sed "s/:[^ ]*//g"` conftest*; exit 1' 1 2 15
 EOF
 cat >> $CONFIG_STATUS <<EOF
 
@@ -12159,7 +12204,7 @@ ac_eD='%g'
 if test "${CONFIG_HEADERS+set}" != set; then
 EOF
 cat >> $CONFIG_STATUS <<EOF
-  CONFIG_HEADERS="include/portable.h include/ldap_features.h"
+  CONFIG_HEADERS="include/portable.h include/ldap_features.h include/lber_types.h"
 EOF
 cat >> $CONFIG_STATUS <<\EOF
 fi
diff --git a/configure.in b/configure.in
index b30e485fa3a39e9c20a583211ff1d412c8c37691..47d5db1c19266982867836fbc35c564d165022e0 100644
--- a/configure.in
+++ b/configure.in
@@ -35,7 +35,7 @@ dnl We use autoconf features new to 2.13.
 dnl 	aclocal.m4 should be built using aclocal from automake 1.4
 AC_PREREQ(2.13)dnl Required Autoconf version
 
-AC_CONFIG_HEADER(include/portable.h include/ldap_features.h)dnl
+AC_CONFIG_HEADER(include/portable.h include/ldap_features.h include/lber_types.h)dnl
 
 dnl
 dnl Start Args
@@ -1621,6 +1621,11 @@ OL_C_VOLATILE
 
 if test $cross_compiling = yes ; then
 	AC_DEFINE(CROSS_COMPILING, 1, [define if cross compiling])
+
+	AC_DEFINE(LBER_INT_T,long)
+	AC_DEFINE(LBER_TAG_T,long)
+	AC_DEFINE(LBER_SOCKET_T,int)
+
 else
 	AC_C_BIGENDIAN
 	AC_CHECK_SIZEOF(short) 
@@ -1629,9 +1634,19 @@ else
 
 	if test "$ac_cv_sizeof_int" -lt 4 ; then
 		AC_MSG_WARN([OpenLDAP requires 'int' to be 32 bits or greater.])
+
+		AC_DEFINE(LBER_INT_T,long)
+		AC_DEFINE(LBER_TAG_T,long)
+		AC_DEFINE(LBER_SOCKET_T,int)
+	else
+		AC_DEFINE(LBER_INT_T,int)
+		AC_DEFINE(LBER_TAG_T,long)
+		AC_DEFINE(LBER_SOCKET_T,int)
 	fi
 fi
 
+AC_DEFINE(LBER_LEN_T,long)
+
 dnl ----------------------------------------------------------------
 dnl Checks for library functions.
 AC_FUNC_MEMCMP
diff --git a/include/ac/socket.h b/include/ac/socket.h
index 5901442749b63edfd60829605a6baffb11180b9d..45554cbd5a0208650a81e36733054febd212610d 100644
--- a/include/ac/socket.h
+++ b/include/ac/socket.h
@@ -67,6 +67,7 @@
 #	define tcp_close( s )		closesocket( s );
 #	define ioctl( s, c, a )		ioctlsocket( (s), (c), (a) )
 #	define ioctl_t				u_long
+#	define AC_SOCKET_INVALID	((unsigned int) ~0)
 
 #define EWOULDBLOCK WSAEWOULDBLOCK
 
@@ -92,12 +93,16 @@
 #	define ioctl_t				int
 #endif
 
+#ifndef AC_SOCKET_INVALID
+#	define AC_SOCKET_INVALID	(-1)
+#endif
+
 #if	defined(__WIN32) && defined(_ALPHA)
 /* NT on Alpha is hosed. */
 #define AC_HTONL( l ) \
         ((((l)&0xff)<<24) + (((l)&0xff00)<<8) + \
          (((l)&0xff0000)>>8) + (((l)&0xff000000)>>24))
-#define AC_NTOHL(l) LBER_HTONL(l)
+#define AC_NTOHL(l) AC_HTONL(l)
 
 #elif defined(__alpha) && !defined(VMS)
 /*
@@ -116,5 +121,9 @@
 #define AC_NTOHL( l ) ntohl( l )
 #endif
 
+/* htons()/ntohs() may be broken much like htonl()/ntohl() */
+#define AC_HTONS( s ) htons( s )
+#define AC_NTOHS( s ) ntohs( s )
+
 
 #endif /* _AC_SOCKET_H_ */
diff --git a/include/avl.h b/include/avl.h
index fe90302fb31cb30e072234979ca66f6b4401051d..ad2c347bf36fa7a5bb6585f12de45f3bc7ee9845 100644
--- a/include/avl.h
+++ b/include/avl.h
@@ -56,7 +56,7 @@ struct avlnode {
 #endif /* AVL_INTERNALS */
 
 typedef int		(*AVL_APPLY) LDAP_P((void *, void*));
-typedef int		(*AVL_CMP) LDAP_P((void*, void*));
+typedef int		(*AVL_CMP) LDAP_P((const void*, const void*));
 typedef int		(*AVL_DUP) LDAP_P((void*, void*));
 typedef void	(*AVL_FREE) LDAP_P((void*));
 
@@ -70,10 +70,10 @@ LDAP_F( void* )
 avl_delete LDAP_P((Avlnode **, void*, AVL_CMP));
 
 LDAP_F( void* )
-avl_find LDAP_P((Avlnode *, void*, AVL_CMP));
+avl_find LDAP_P((Avlnode *, const void*, AVL_CMP));
 
 LDAP_F( void* )
-avl_find_lin LDAP_P((Avlnode *, void*, AVL_CMP));
+avl_find_lin LDAP_P((Avlnode *, const void*, AVL_CMP));
 
 #ifdef AVL_NONREENTRANT
 LDAP_F( void* )
diff --git a/include/disptmpl.h b/include/disptmpl.h
index 015418a510f68c9f5623df0c9be98038bdbedfaf..369edf0771f75564f1b8ba3700f18e202372d2d0 100644
--- a/include/disptmpl.h
+++ b/include/disptmpl.h
@@ -221,14 +221,15 @@ struct ldap_disptmpl {
 #define LDAP_DTMPL_BUFSIZ	8192
 
 
-typedef int (*ldap_writeptype) LDAP_P(( void *writeparm, char *p, int len ));
+typedef int (*ldap_writeptype) LDAP_P((
+	void *writeparm, char *p, ber_len_t len ));
 
 LDAP_F( int )
 ldap_init_templates LDAP_P(( char *file, struct ldap_disptmpl **tmpllistp ));
 
 LDAP_F( int )
 ldap_init_templates_buf LDAP_P(( char *buf,
-	long buflen,
+	ber_len_t buflen,
 	struct ldap_disptmpl **tmpllistp ));
 
 LDAP_F( void )
diff --git a/include/lber.h b/include/lber.h
index a9d3c85d07c6a5c95699265698e6fa6661e1ceb6..eb0ad06d0c91ca7f0653edafdedbd42cd4bc75fd 100644
--- a/include/lber.h
+++ b/include/lber.h
@@ -23,13 +23,30 @@
 #define _LBER_H
 
 #include <ldap_cdefs.h>
-
-#ifndef LBER_SIZE_T_DEFINED
-#include <stddef.h>
-#endif
+#include <lber_types.h>
 
 LDAP_BEGIN_DECL
 
+/* boolean, enumerations, and integers */
+typedef LBER_INT_T ber_int_t;
+
+/* signed and unsigned versions */
+typedef signed LBER_INT_T ber_sint_t;
+typedef unsigned LBER_INT_T ber_uint_t;
+
+/* tags */
+typedef LBER_TAG_T ber_tag_t;
+
+/* "socket" descriptors */
+typedef LBER_SOCKET_T ber_socket_t;
+
+/* lengths */
+typedef unsigned LBER_LEN_T ber_len_t;
+
+/* signed lengths */
+typedef LBER_LEN_T ber_slen_t;
+
+
 /* Overview of LBER tag construction
  *
  *	Bits
@@ -48,41 +65,41 @@ LDAP_BEGIN_DECL
  */
 
 /* BER classes and mask */
-#define LBER_CLASS_UNIVERSAL	0x00UL
-#define LBER_CLASS_APPLICATION	0x40UL
-#define LBER_CLASS_CONTEXT	0x80UL
-#define LBER_CLASS_PRIVATE	0xc0UL
-#define LBER_CLASS_MASK		0xc0UL
+#define LBER_CLASS_UNIVERSAL	(ber_tag_t) 0x00U
+#define LBER_CLASS_APPLICATION	(ber_tag_t) 0x40U
+#define LBER_CLASS_CONTEXT	(ber_tag_t) 0x80U
+#define LBER_CLASS_PRIVATE	(ber_tag_t) 0xc0U
+#define LBER_CLASS_MASK		(ber_tag_t) 0xc0U
 
 /* BER encoding type and mask */
-#define LBER_PRIMITIVE		0x00UL
-#define LBER_CONSTRUCTED	0x20UL
-#define LBER_ENCODING_MASK	0x20UL
+#define LBER_PRIMITIVE		(ber_tag_t) 0x00U
+#define LBER_CONSTRUCTED	(ber_tag_t) 0x20U
+#define LBER_ENCODING_MASK	(ber_tag_t) 0x20U
 
-#define LBER_BIG_TAG_MASK	0x1fUL
-#define LBER_MORE_TAG_MASK	0x80UL
+#define LBER_BIG_TAG_MASK	(ber_tag_t) 0x1fU
+#define LBER_MORE_TAG_MASK	(ber_tag_t) 0x80U
 
 /*
  * Note that LBER_ERROR and LBER_DEFAULT are values that can never appear
  * as valid BER tags, and so it is safe to use them to report errors.  In
  * fact, any tag for which the following is true is invalid:
  */
-#define LBER_INVALID(tag) (((tag) & 0x00000080) && (((tag) & 0xFFFFFF00))
-#define LBER_ERROR		0xffffffffUL
-#define LBER_DEFAULT		0xffffffffUL
+#define LBER_INVALID(t)     (((t) & 0x080) && (((t) & (ber_tag_t) ~ 0x0FF))
+#define LBER_ERROR			((ber_tag_t) ~ 0x0)
+#define LBER_DEFAULT		((ber_tag_t) ~ 0x0)
 
 /* general BER types we know about */
-#define LBER_BOOLEAN		0x01UL
-#define LBER_INTEGER		0x02UL
-#define LBER_BITSTRING		0x03UL
-#define LBER_OCTETSTRING	0x04UL
-#define LBER_NULL		0x05UL
-#define LBER_ENUMERATED		0x0aUL
-#define LBER_SEQUENCE		0x30UL	/* constructed */
-#define LBER_SET		0x31UL	/* constructed */
-
-#define OLD_LBER_SEQUENCE	0x10UL	/* w/o constructed bit - broken */
-#define OLD_LBER_SET		0x11UL	/* w/o constructed bit - broken */
+#define LBER_BOOLEAN		(ber_tag_t) 0x01UL
+#define LBER_INTEGER		(ber_tag_t) 0x02UL
+#define LBER_BITSTRING		(ber_tag_t) 0x03UL
+#define LBER_OCTETSTRING	(ber_tag_t) 0x04UL
+#define LBER_NULL			(ber_tag_t) 0x05UL
+#define LBER_ENUMERATED		(ber_tag_t) 0x0aUL
+#define LBER_SEQUENCE		(ber_tag_t) 0x30UL	/* constructed */
+#define LBER_SET			(ber_tag_t) 0x31UL	/* constructed */
+
+#define OLD_LBER_SEQUENCE	(ber_tag_t) 0x10UL	/* w/o constructed bit - broken */
+#define OLD_LBER_SET		(ber_tag_t) 0x11UL	/* w/o constructed bit - broken */
 
 typedef int (*BERTranslateProc) LDAP_P((
 	char **bufp,
@@ -105,9 +122,9 @@ typedef int (*BERTranslateProc) LDAP_P((
 
 typedef void (*BER_LOG_PRINT_FN) LDAP_P(( char *buf ));
 
-typedef void* (*BER_MEMALLOC_FN)	LDAP_P(( size_t size ));
-typedef void* (*BER_MEMCALLOC_FN)	LDAP_P(( size_t n, size_t size ));
-typedef void* (*BER_MEMREALLOC_FN)	LDAP_P(( void *p, size_t size ));
+typedef void* (*BER_MEMALLOC_FN)	LDAP_P(( ber_len_t size ));
+typedef void* (*BER_MEMCALLOC_FN)	LDAP_P(( ber_len_t n, ber_len_t size ));
+typedef void* (*BER_MEMREALLOC_FN)	LDAP_P(( void *p, ber_len_t size ));
 typedef void  (*BER_MEMFREE_FN)		LDAP_P(( void *p ));
 
 typedef struct lber_memory_fns {
@@ -141,7 +158,7 @@ typedef struct seqorset Seqorset;
 
 /* structure for returning a sequence of octet strings + length */
 typedef struct berval {
-	unsigned long	bv_len;
+	ber_len_t	bv_len;
 	char		*bv_val;
 } BerValue;
 
@@ -154,7 +171,7 @@ ber_print_error LDAP_P((
 
 LDAP_F( void )
 ber_bprint LDAP_P((
-	LDAP_CONST char *data, int len ));
+	LDAP_CONST char *data, ber_len_t len ));
 
 LDAP_F( void )
 ber_dump LDAP_P((
@@ -173,68 +190,69 @@ typedef int (*BERDecodeCallback) LDAP_P((
 	void *data,
 	int mode ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_tag LDAP_P((
 	BerElement *ber ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_skip_tag LDAP_P((
 	BerElement *ber,
-	unsigned long *len ));
+	ber_len_t *len ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_peek_tag LDAP_P((
 	LDAP_CONST BerElement *ber,
-	unsigned long *len ));
+	ber_len_t *len ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_int LDAP_P((
 	BerElement *ber,
-	long *num ));
+	ber_int_t *num ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_stringb LDAP_P((
 	BerElement *ber,
 	char *buf,
-	unsigned long *len ));
+	ber_len_t *len ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_stringa LDAP_P((
-	BerElement *ber, char **buf ));
+	BerElement *ber,
+	char **buf ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_stringal LDAP_P((
 	BerElement *ber,
 	struct berval **bv ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_bitstringa LDAP_P((
 	BerElement *ber,
 	char **buf,
-	unsigned long *len ));
+	ber_len_t *len ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_null LDAP_P((
 	BerElement *ber ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_boolean LDAP_P((
 	BerElement *ber,
-	int *boolval ));
+	ber_int_t *boolval ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_first_element LDAP_P((
 	BerElement *ber,
-	unsigned long *len,
+	ber_len_t *len,
 	char **last ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_next_element LDAP_P((
 	BerElement *ber,
-	unsigned long *len,
+	ber_len_t *len,
 	char *last ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_scanf LDAP_P((								  
 	BerElement *ber,
 	LDAP_CONST char *fmt,
@@ -256,61 +274,61 @@ typedef int (*BEREncodeCallback) LDAP_P((
 LDAP_F( int )
 ber_put_enum LDAP_P((
 	BerElement *ber,
-	long num,
-	unsigned long tag ));
+	ber_int_t num,
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_put_int LDAP_P((
 	BerElement *ber,
-	long num,
-	unsigned long tag ));
+	ber_int_t num,
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_put_ostring LDAP_P((
 	BerElement *ber,
 	LDAP_CONST char *str,
-	unsigned long len,
-	unsigned long tag ));
+	ber_len_t len,
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_put_berval LDAP_P((
 	BerElement *ber,
 	LDAP_CONST struct berval *bv,
-	unsigned long tag ));
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_put_string LDAP_P((
 	BerElement *ber,
 	LDAP_CONST char *str,
-	unsigned long tag ));
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_put_bitstring LDAP_P((
 	BerElement *ber,
 	LDAP_CONST char *str,
-	unsigned long bitlen,
-	unsigned long tag ));
+	ber_len_t bitlen,
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_put_null LDAP_P((
 	BerElement *ber,
-	unsigned long tag ));
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_put_boolean LDAP_P((
 	BerElement *ber,
-	int boolval,
-	unsigned long tag ));
+	ber_int_t boolval,
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_start_seq LDAP_P((
 	BerElement *ber,
-	unsigned long tag ));
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_start_set LDAP_P((
 	BerElement *ber,
-	unsigned long tag ));
+	ber_tag_t tag ));
 
 LDAP_F( int )
 ber_put_seq LDAP_P((
@@ -331,17 +349,17 @@ ber_printf LDAP_P((
  * in io.c:
  */
 
-LDAP_F( long )
+LDAP_F( ber_slen_t )
 ber_read LDAP_P((
 	BerElement *ber,
 	char *buf,
-	unsigned long len ));
+	ber_len_t len ));
 
-LDAP_F( long )
+LDAP_F( ber_slen_t )
 ber_write LDAP_P((
 	BerElement *ber,
 	LDAP_CONST char *buf,
-	unsigned long len,
+	ber_len_t len,
 	int nosos ));
 
 LDAP_F( void )
@@ -351,7 +369,9 @@ ber_free LDAP_P((
 
 LDAP_F( int )
 ber_flush LDAP_P((
-	Sockbuf *sb, BerElement *ber, int freeit ));
+	Sockbuf *sb,
+	BerElement *ber,
+	int freeit ));
 
 LDAP_F( BerElement * )
 ber_alloc LDAP_P(( void )); /* DEPRECATED */
@@ -367,10 +387,10 @@ LDAP_F( BerElement * )
 ber_dup LDAP_P((
 	LDAP_CONST BerElement *ber ));
 
-LDAP_F( unsigned long )
+LDAP_F( ber_tag_t )
 ber_get_next LDAP_P((
 	Sockbuf *sb,
-	unsigned long *len,
+	ber_len_t *len,
 	BerElement *ber ));
 
 LDAP_F( void )
@@ -417,7 +437,7 @@ ber_sockbuf_alloc( void );
 
 LDAP_F( Sockbuf *  )
 ber_sockbuf_alloc_fd(
-	int fd );
+	ber_socket_t fd );
 
 LDAP_F( void )
 ber_sockbuf_free(
@@ -428,17 +448,17 @@ ber_sockbuf_free(
  */
 LDAP_F( void * )
 ber_memalloc LDAP_P((
-	size_t s ));
+	ber_len_t s ));
 
 LDAP_F( void * )
 ber_memrealloc LDAP_P((
 	void* p,
-	size_t s ));
+	ber_len_t s ));
 
 LDAP_F( void * )
 ber_memcalloc LDAP_P((
-	size_t n,
-	size_t s ));
+	ber_len_t n,
+	ber_len_t s ));
 
 LDAP_F( void )
 ber_memfree LDAP_P((
diff --git a/include/lber_types.h.in b/include/lber_types.h.in
new file mode 100644
index 0000000000000000000000000000000000000000..5f1bb70f39dad10ce5c09a6a610d59c67f9f39ce
--- /dev/null
+++ b/include/lber_types.h.in
@@ -0,0 +1,30 @@
+/*
+ * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted only
+ * as authorized by the OpenLDAP Public License.  A copy of this
+ * license is available at http://www.OpenLDAP.org/license.html or
+ * in file LICENSE in the top-level directory of the distribution.
+ */
+
+/*
+ * LBER types
+ */
+
+#ifndef _LBER_TYPES_H
+#define _LBER_TYPES_H
+
+/* LBER boolean, enum, integers - 32 bits or larger*/
+#undef LBER_INT_T
+
+/* LBER tags - 32 bits or larger */
+#undef LBER_TAG_T
+
+/* LBER socket descriptor */
+#undef LBER_SOCKET_T
+
+/* LBER lengths - 32 bits or larger*/
+#undef LBER_LEN_T
+
+#endif /* _LBER_TYPES_H */
diff --git a/include/lber_types.h.nt b/include/lber_types.h.nt
new file mode 100644
index 0000000000000000000000000000000000000000..6e26c6771fc8a4f35a23be1362df509e4f50ce85
--- /dev/null
+++ b/include/lber_types.h.nt
@@ -0,0 +1,43 @@
+/*
+ * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted only
+ * as authorized by the OpenLDAP Public License.  A copy of this
+ * license is available at http://www.OpenLDAP.org/license.html or
+ * in file LICENSE in the top-level directory of the distribution.
+ */
+
+/*
+ * LBER types for Windows NT
+ */
+
+#ifndef _LBER_TYPES_H_NT
+#define _LBER_TYPES_H_NT
+
+/*
+ * NT types:
+ *
+ *	bitsof(short) == 2
+ *	bitsof(int)	== 4
+ *	bitsof(long) == 4
+ *
+ *  typedef unsigned int size_t;
+ *	typedef unsigned int SOCKET;
+ *
+ *	we use native C types to avoid sucking in system headers
+ */
+
+/* LBER boolean, enum, integers - 32 bits or larger*/
+#define LBER_INT_T	int
+
+/* LBER tags - 32 bits or larger */
+#define LBER_TAG_T	long
+
+/* LBER socket descriptor */
+#define LBER_SOCKET_T	unsigned int
+
+/* LBER lengths - 32 bits or larger*/
+#define LBER_LEN_T		int
+
+#endif /* _LBER_TYPES_H_NT */
diff --git a/include/ldap.h b/include/ldap.h
index e435088e9520828ebebcaf01c4ce96d0a7227a3e..7258b8112d94f5f9c8d826e398037ad2015f33ce 100644
--- a/include/ldap.h
+++ b/include/ldap.h
@@ -42,7 +42,7 @@ LDAP_BEGIN_DECL
  * As such, the number will be above the old RFC but below 
  * whatever number does finally get assigned
  */
-#define LDAP_API_VERSION	2002
+#define LDAP_API_VERSION	2003
 #define LDAP_VENDOR_NAME	"OpenLDAP"
 /* We'll eventually release as 200 */
 #define LDAP_VENDOR_VERSION	192
@@ -175,164 +175,164 @@ typedef struct ldapcontrol {
  */
 
 /* general stuff */
-#define LDAP_TAG_MESSAGE	0x30UL	/* constructed + 16 */
-#define OLD_LDAP_TAG_MESSAGE	0x10UL	/* forgot the constructed bit  */
-#define LDAP_TAG_MSGID		0x02UL	/* integer */
-#define LDAP_TAG_LDAPDN		0x04UL	/* octect string */
-#define LDAP_TAG_CONTROLS	0xa0UL	/* context specific + constructed + 0 */
-#define LDAP_TAG_REFERRAL	0xa3UL	/* context specific + constructed + 3 */
+#define LDAP_TAG_MESSAGE	(ber_tag_t) 0x30U	/* constructed + 16 */
+#define OLD_LDAP_TAG_MESSAGE	(ber_tag_t) 0x10U	/* forgot the constructed bit  */
+#define LDAP_TAG_MSGID		(ber_tag_t) 0x02U	/* integer */
+#define LDAP_TAG_LDAPDN		(ber_tag_t) 0x04U	/* octect string */
+#define LDAP_TAG_CONTROLS	(ber_tag_t) 0xa0U	/* context specific + constructed + 0 */
+#define LDAP_TAG_REFERRAL	(ber_tag_t) 0xa3U	/* context specific + constructed + 3 */
 
-#define LDAP_TAG_NEWSUPERIOR	0x80UL	/* context-specific + primitive + 0 */
+#define LDAP_TAG_NEWSUPERIOR	(ber_tag_t) 0x80U	/* context-specific + primitive + 0 */
 
-#define LDAP_TAG_EXOP_REQ_OID   0x80UL	/* context specific + primitive */
-#define LDAP_TAG_EXOP_REQ_VALUE 0x81UL	/* context specific + primitive */
-#define LDAP_TAG_EXOP_RES_OID   0x8aUL	/* context specific + primitive */
-#define LDAP_TAG_EXOP_RES_VALUE 0x8bUL	/* context specific + primitive */
+#define LDAP_TAG_EXOP_REQ_OID   (ber_tag_t) 0x80U	/* context specific + primitive */
+#define LDAP_TAG_EXOP_REQ_VALUE (ber_tag_t) 0x81U	/* context specific + primitive */
+#define LDAP_TAG_EXOP_RES_OID   (ber_tag_t) 0x8aU	/* context specific + primitive */
+#define LDAP_TAG_EXOP_RES_VALUE (ber_tag_t) 0x8bU	/* context specific + primitive */
 
-#define LDAP_TAG_SASL_RES_CREDS	0x87UL	/* context specific + primitive */
+#define LDAP_TAG_SASL_RES_CREDS	(ber_tag_t) 0x87U	/* context specific + primitive */
 
 
 
 
 /* possible operations a client can invoke */
-#define LDAP_REQ_BIND			0x60UL	/* application + constructed */
-#define LDAP_REQ_UNBIND			0x42UL	/* application + primitive   */
-#define LDAP_REQ_SEARCH			0x63UL	/* application + constructed */
-#define LDAP_REQ_MODIFY			0x66UL	/* application + constructed */
-#define LDAP_REQ_ADD			0x68UL	/* application + constructed */
-#define LDAP_REQ_DELETE			0x4aUL	/* application + primitive   */
-#define LDAP_REQ_MODRDN			0x6cUL	/* application + constructed */
+#define LDAP_REQ_BIND			(ber_tag_t) 0x60U	/* application + constructed */
+#define LDAP_REQ_UNBIND			(ber_tag_t) 0x42U	/* application + primitive   */
+#define LDAP_REQ_SEARCH			(ber_tag_t) 0x63U	/* application + constructed */
+#define LDAP_REQ_MODIFY			(ber_tag_t) 0x66U	/* application + constructed */
+#define LDAP_REQ_ADD			(ber_tag_t) 0x68U	/* application + constructed */
+#define LDAP_REQ_DELETE			(ber_tag_t) 0x4aU	/* application + primitive   */
+#define LDAP_REQ_MODRDN			(ber_tag_t) 0x6cU	/* application + constructed */
 #define LDAP_REQ_MODDN			LDAP_REQ_MODRDN	
 #define LDAP_REQ_RENAME			LDAP_REQ_MODRDN	
-#define LDAP_REQ_COMPARE		0x6eUL	/* application + constructed */
-#define LDAP_REQ_ABANDON		0x50UL	/* application + primitive   */
-#define LDAP_REQ_EXTENDED		0x77UL	/* application + constructed */
+#define LDAP_REQ_COMPARE		(ber_tag_t) 0x6eU	/* application + constructed */
+#define LDAP_REQ_ABANDON		(ber_tag_t) 0x50U	/* application + primitive   */
+#define LDAP_REQ_EXTENDED		(ber_tag_t) 0x77U	/* application + constructed */
 
 /* U-Mich version 3.0 compatibility stuff */
-#define LDAP_REQ_UNBIND_30		0x62UL
-#define LDAP_REQ_DELETE_30		0x6aUL
-#define LDAP_REQ_ABANDON_30		0x70UL
+#define LDAP_REQ_UNBIND_30		(ber_tag_t) 0x62U
+#define LDAP_REQ_DELETE_30		(ber_tag_t) 0x6aU
+#define LDAP_REQ_ABANDON_30		(ber_tag_t) 0x70U
 
 /* 
  * old broken stuff for backwards compatibility - forgot application tag
  * and constructed/primitive bit
  */
-#define OLD_LDAP_REQ_BIND		0x00UL
-#define OLD_LDAP_REQ_UNBIND		0x02UL
-#define OLD_LDAP_REQ_SEARCH		0x03UL
-#define OLD_LDAP_REQ_MODIFY		0x06UL
-#define OLD_LDAP_REQ_ADD		0x08UL
-#define OLD_LDAP_REQ_DELETE		0x0aUL
-#define OLD_LDAP_REQ_MODRDN		0x0cUL
-#define OLD_LDAP_REQ_COMPARE		0x0eUL
-#define OLD_LDAP_REQ_ABANDON		0x10UL
+#define OLD_LDAP_REQ_BIND		(ber_tag_t) 0x00U
+#define OLD_LDAP_REQ_UNBIND		(ber_tag_t) 0x02U
+#define OLD_LDAP_REQ_SEARCH		(ber_tag_t) 0x03U
+#define OLD_LDAP_REQ_MODIFY		(ber_tag_t) 0x06U
+#define OLD_LDAP_REQ_ADD		(ber_tag_t) 0x08U
+#define OLD_LDAP_REQ_DELETE		(ber_tag_t) 0x0aU
+#define OLD_LDAP_REQ_MODRDN		(ber_tag_t) 0x0cU
+#define OLD_LDAP_REQ_COMPARE		(ber_tag_t) 0x0eU
+#define OLD_LDAP_REQ_ABANDON		(ber_tag_t) 0x10U
 
 /* possible result types a server can return */
-#define LDAP_RES_BIND			0x61UL	/* application + constructed */
-#define LDAP_RES_SEARCH_ENTRY		0x64UL	/* application + constructed */
-#define LDAP_RES_SEARCH_REFERENCE	0x73UL	/* V3: application + constructed */
-#define LDAP_RES_SEARCH_RESULT		0x65UL	/* application + constructed */
-#define LDAP_RES_MODIFY			0x67UL	/* application + constructed */
-#define LDAP_RES_ADD			0x69UL	/* application + constructed */
-#define LDAP_RES_DELETE			0x6bUL	/* application + constructed */
-#define LDAP_RES_MODRDN			0x6dUL	/* application + constructed */
+#define LDAP_RES_BIND			(ber_tag_t) 0x61U	/* application + constructed */
+#define LDAP_RES_SEARCH_ENTRY		(ber_tag_t) 0x64U	/* application + constructed */
+#define LDAP_RES_SEARCH_REFERENCE	(ber_tag_t) 0x73U	/* V3: application + constructed */
+#define LDAP_RES_SEARCH_RESULT		(ber_tag_t) 0x65U	/* application + constructed */
+#define LDAP_RES_MODIFY			(ber_tag_t) 0x67U	/* application + constructed */
+#define LDAP_RES_ADD			(ber_tag_t) 0x69U	/* application + constructed */
+#define LDAP_RES_DELETE			(ber_tag_t) 0x6bU	/* application + constructed */
+#define LDAP_RES_MODRDN			(ber_tag_t) 0x6dU	/* application + constructed */
 #define LDAP_RES_MODDN			LDAP_RES_MODRDN	/* application + constructed */
 #define LDAP_RES_RENAME			LDAP_RES_MODRDN	/* application + constructed */
-#define LDAP_RES_COMPARE		0x6fUL	/* application + constructed */
-#define LDAP_RES_EXTENDED		0x78UL	/* V3: application + constructed */
-#define LDAP_RES_ANY			((unsigned long)(-1))
+#define LDAP_RES_COMPARE		(ber_tag_t) 0x6fU	/* application + constructed */
+#define LDAP_RES_EXTENDED		(ber_tag_t) 0x78U	/* V3: application + constructed */
+#define LDAP_RES_ANY			((ber_tag_t)(~0))
 
 /* old broken stuff for backwards compatibility */
-#define OLD_LDAP_RES_BIND		0x01UL
-#define OLD_LDAP_RES_SEARCH_ENTRY	0x04UL
-#define OLD_LDAP_RES_SEARCH_RESULT	0x05UL
-#define OLD_LDAP_RES_MODIFY		0x07UL
-#define OLD_LDAP_RES_ADD		0x09UL
-#define OLD_LDAP_RES_DELETE		0x0bUL
-#define OLD_LDAP_RES_MODRDN		0x0dUL
+#define OLD_LDAP_RES_BIND		(ber_tag_t) 0x01UL
+#define OLD_LDAP_RES_SEARCH_ENTRY	(ber_tag_t) 0x04UL
+#define OLD_LDAP_RES_SEARCH_RESULT	(ber_tag_t) 0x05U
+#define OLD_LDAP_RES_MODIFY		(ber_tag_t) 0x07U
+#define OLD_LDAP_RES_ADD		(ber_tag_t) 0x09U
+#define OLD_LDAP_RES_DELETE		(ber_tag_t) 0x0bU
+#define OLD_LDAP_RES_MODRDN		(ber_tag_t) 0x0dU
 #define OLD_LDAP_RES_MODDN		OLD_LDAP_RES_MODRDN
-#define OLD_LDAP_RES_COMPARE		0x0fUL
+#define OLD_LDAP_RES_COMPARE	(ber_tag_t) 0x0fU
 
 /* sasl methods */
 #define LDAP_SASL_SIMPLE			NULL
 
 /* authentication methods available */
-#define LDAP_AUTH_NONE		0x00UL	/* no authentication		  */
-#define LDAP_AUTH_SIMPLE	0x80UL	/* context specific + primitive   */
-#define LDAP_AUTH_SASL		0xa3UL	/* context specific + primitive   */
-#define LDAP_AUTH_KRBV4		0xffUL	/* means do both of the following */
-#define LDAP_AUTH_KRBV41	0x81UL	/* context specific + primitive   */
-#define LDAP_AUTH_KRBV42	0x82UL	/* context specific + primitive   */
+#define LDAP_AUTH_NONE		(ber_tag_t) 0x00U	/* no authentication		  */
+#define LDAP_AUTH_SIMPLE	(ber_tag_t) 0x80U	/* context specific + primitive   */
+#define LDAP_AUTH_SASL		(ber_tag_t) 0xa3U	/* context specific + primitive   */
+#define LDAP_AUTH_KRBV4		(ber_tag_t) 0xffU	/* means do both of the following */
+#define LDAP_AUTH_KRBV41	(ber_tag_t) 0x81U	/* context specific + primitive   */
+#define LDAP_AUTH_KRBV42	(ber_tag_t) 0x82U	/* context specific + primitive   */
 
 /* U-Mich version 3.0 compatibility auth methods */
-#define LDAP_AUTH_SIMPLE_30	0xa0UL	/* context specific + constructed */
-#define LDAP_AUTH_KRBV41_30	0xa1UL	/* context specific + constructed */
-#define LDAP_AUTH_KRBV42_30	0xa2UL	/* context specific + constructed */
+#define LDAP_AUTH_SIMPLE_30	(ber_tag_t) 0xa0U	/* context specific + constructed */
+#define LDAP_AUTH_KRBV41_30	(ber_tag_t) 0xa1U	/* context specific + constructed */
+#define LDAP_AUTH_KRBV42_30	(ber_tag_t) 0xa2U	/* context specific + constructed */
 
 /* old broken stuff */
-#define OLD_LDAP_AUTH_SIMPLE	0x00UL
-#define OLD_LDAP_AUTH_KRBV4	0x01UL
-#define OLD_LDAP_AUTH_KRBV42	0x02UL
+#define OLD_LDAP_AUTH_SIMPLE	(ber_tag_t) 0x00U
+#define OLD_LDAP_AUTH_KRBV4	(ber_tag_t) 0x01U
+#define OLD_LDAP_AUTH_KRBV42	(ber_tag_t) 0x02U
 
 /* filter types */
-#define LDAP_FILTER_AND		0xa0UL	/* context specific + constructed */
-#define LDAP_FILTER_OR		0xa1UL	/* context specific + constructed */
-#define LDAP_FILTER_NOT		0xa2UL	/* context specific + constructed */
-#define LDAP_FILTER_EQUALITY	0xa3UL	/* context specific + constructed */
-#define LDAP_FILTER_SUBSTRINGS	0xa4UL	/* context specific + constructed */
-#define LDAP_FILTER_GE		0xa5UL	/* context specific + constructed */
-#define LDAP_FILTER_LE		0xa6UL	/* context specific + constructed */
-#define LDAP_FILTER_PRESENT	0x87UL	/* context specific + primitive   */
-#define LDAP_FILTER_APPROX	0xa8UL	/* context specific + constructed */
-#define LDAP_FILTER_EXTENDED	0xa9UL	/* context specific + constructed */
+#define LDAP_FILTER_AND		(ber_tag_t) 0xa0U	/* context specific + constructed */
+#define LDAP_FILTER_OR		(ber_tag_t) 0xa1U	/* context specific + constructed */
+#define LDAP_FILTER_NOT		(ber_tag_t) 0xa2U	/* context specific + constructed */
+#define LDAP_FILTER_EQUALITY	(ber_tag_t) 0xa3U	/* context specific + constructed */
+#define LDAP_FILTER_SUBSTRINGS	(ber_tag_t) 0xa4U	/* context specific + constructed */
+#define LDAP_FILTER_GE		(ber_tag_t) 0xa5U	/* context specific + constructed */
+#define LDAP_FILTER_LE		(ber_tag_t) 0xa6U	/* context specific + constructed */
+#define LDAP_FILTER_PRESENT	(ber_tag_t) 0x87U	/* context specific + primitive   */
+#define LDAP_FILTER_APPROX	(ber_tag_t) 0xa8U	/* context specific + constructed */
+#define LDAP_FILTER_EXTENDED	(ber_tag_t) 0xa9U	/* context specific + constructed */
 
 /* U-Mich version 3.0 compatibility filter types */
-#define LDAP_FILTER_PRESENT_30	0xa7UL	/* context specific + constructed */
+#define LDAP_FILTER_PRESENT_30	(ber_tag_t) 0xa7U	/* context specific + constructed */
 
 /* old broken stuff */
-#define OLD_LDAP_FILTER_AND		0x00UL
-#define OLD_LDAP_FILTER_OR		0x01UL
-#define OLD_LDAP_FILTER_NOT		0x02UL
-#define OLD_LDAP_FILTER_EQUALITY	0x03UL
-#define OLD_LDAP_FILTER_SUBSTRINGS	0x04UL
-#define OLD_LDAP_FILTER_GE		0x05UL
-#define OLD_LDAP_FILTER_LE		0x06UL
-#define OLD_LDAP_FILTER_PRESENT		0x07UL
-#define OLD_LDAP_FILTER_APPROX		0x08UL
+#define OLD_LDAP_FILTER_AND		(ber_tag_t) 0x00U
+#define OLD_LDAP_FILTER_OR		(ber_tag_t) 0x01U
+#define OLD_LDAP_FILTER_NOT		(ber_tag_t) 0x02U
+#define OLD_LDAP_FILTER_EQUALITY	(ber_tag_t) 0x03U
+#define OLD_LDAP_FILTER_SUBSTRINGS	(ber_tag_t) 0x04U
+#define OLD_LDAP_FILTER_GE		(ber_tag_t) 0x05U
+#define OLD_LDAP_FILTER_LE		(ber_tag_t) 0x06U
+#define OLD_LDAP_FILTER_PRESENT		(ber_tag_t) 0x07U
+#define OLD_LDAP_FILTER_APPROX		(ber_tag_t) 0x08U
 
 /* extended filter component types */
-#define LDAP_FILTER_EXTENDED_OID	0x81UL	/* context specific */
-#define LDAP_FILTER_EXTENDED_TYPE	0x82UL	/* context specific */
-#define LDAP_FILTER_EXTENDED_VALUE	0x83UL	/* context specific */
-#define LDAP_FILTER_EXTENDED_DNATTRS	0x84UL	/* context specific */
+#define LDAP_FILTER_EXTENDED_OID	(ber_tag_t) 0x81U	/* context specific */
+#define LDAP_FILTER_EXTENDED_TYPE	(ber_tag_t) 0x82U	/* context specific */
+#define LDAP_FILTER_EXTENDED_VALUE	(ber_tag_t) 0x83U	/* context specific */
+#define LDAP_FILTER_EXTENDED_DNATTRS	(ber_tag_t) 0x84U	/* context specific */
 
 /* substring filter component types */
-#define LDAP_SUBSTRING_INITIAL	0x80UL	/* context specific */
-#define LDAP_SUBSTRING_ANY	0x81UL	/* context specific */
-#define LDAP_SUBSTRING_FINAL	0x82UL	/* context specific */
+#define LDAP_SUBSTRING_INITIAL	(ber_tag_t) 0x80U	/* context specific */
+#define LDAP_SUBSTRING_ANY	(ber_tag_t) 0x81U	/* context specific */
+#define LDAP_SUBSTRING_FINAL	(ber_tag_t) 0x82U	/* context specific */
 
 /* U-Mich version 3.0 compatibility substring filter component types */
-#define LDAP_SUBSTRING_INITIAL_30	0xa0UL	/* context specific */
-#define LDAP_SUBSTRING_ANY_30		0xa1UL	/* context specific */
-#define LDAP_SUBSTRING_FINAL_30		0xa2UL	/* context specific */
+#define LDAP_SUBSTRING_INITIAL_30	(ber_tag_t) 0xa0U	/* context specific */
+#define LDAP_SUBSTRING_ANY_30		(ber_tag_t) 0xa1U	/* context specific */
+#define LDAP_SUBSTRING_FINAL_30		(ber_tag_t) 0xa2U	/* context specific */
 
 /* old broken stuff */
-#define OLD_LDAP_SUBSTRING_INITIAL	0x00UL
-#define OLD_LDAP_SUBSTRING_ANY		0x01UL
-#define OLD_LDAP_SUBSTRING_FINAL	0x02UL
+#define OLD_LDAP_SUBSTRING_INITIAL	(ber_tag_t) 0x00U
+#define OLD_LDAP_SUBSTRING_ANY		(ber_tag_t) 0x01U
+#define OLD_LDAP_SUBSTRING_FINAL	(ber_tag_t) 0x02U
 
 /* search scopes */
-#define LDAP_SCOPE_BASE		0x0000
-#define LDAP_SCOPE_ONELEVEL	0x0001
-#define LDAP_SCOPE_SUBTREE	0x0002
+#define LDAP_SCOPE_BASE		(ber_int_t) 0x0000
+#define LDAP_SCOPE_ONELEVEL	(ber_int_t) 0x0001
+#define LDAP_SCOPE_SUBTREE	(ber_int_t) 0x0002
 
 /* for modifications */
 typedef struct ldapmod {
 	int		mod_op;
-#define LDAP_MOD_ADD		0x0000
-#define LDAP_MOD_DELETE		0x0001
-#define LDAP_MOD_REPLACE	0x0002
-#define LDAP_MOD_BVALUES	0x0080
+#define LDAP_MOD_ADD		(ber_int_t) 0x0000
+#define LDAP_MOD_DELETE		(ber_int_t) 0x0001
+#define LDAP_MOD_REPLACE	(ber_int_t) 0x0002
+#define LDAP_MOD_BVALUES	(ber_int_t) 0x0080
 /* IMPORTANT: do not use code 0x1000 (or above),
  * it is used internally by the backends!
  * (see ldap/servers/slapd/slap.h)
@@ -732,7 +732,7 @@ ldap_kerberos_bind2_s LDAP_P((
  * (deprecated)
  */
 LDAP_F( int )
-ldap_enable_cache LDAP_P(( LDAP *ld, long timeout, long maxmem ));
+ldap_enable_cache LDAP_P(( LDAP *ld, long timeout, ber_len_t maxmem ));
 
 LDAP_F( void )
 ldap_disable_cache LDAP_P(( LDAP *ld ));
@@ -1311,7 +1311,7 @@ ldap_init_getfilter LDAP_P((
 LDAP_F( LDAPFiltDesc *)
 ldap_init_getfilter_buf LDAP_P((
 	/* LDAP_CONST */ char *buf,
-	long buflen ));
+	ber_len_t buflen ));
 
 LDAP_F( LDAPFiltInfo *)
 ldap_getfirstfilter LDAP_P((
@@ -1332,7 +1332,7 @@ ldap_setfilteraffixes LDAP_P((
 LDAP_F( void )
 ldap_build_filter LDAP_P((
 	char *buf,
-	unsigned long buflen,
+	ber_len_t buflen,
 	LDAP_CONST char *pattern,
 	LDAP_CONST char *prefix,
 	LDAP_CONST char *suffix,
@@ -1347,17 +1347,17 @@ ldap_build_filter LDAP_P((
 
 LDAP_F( void * )
 ldap_memalloc LDAP_P((
-	size_t s ));
+	ber_len_t s ));
 
 LDAP_F( void * )
 ldap_memrealloc LDAP_P((
 	void* p,
-	size_t s ));
+	ber_len_t s ));
 
 LDAP_F( void * )
 ldap_memcalloc LDAP_P((
-	size_t n,
-	size_t s ));
+	ber_len_t n,
+	ber_len_t s ));
 
 LDAP_F( void )
 ldap_memfree LDAP_P((
@@ -1499,14 +1499,15 @@ LDAP_F( int )
 ldap_translate_from_t61 LDAP_P((
 	LDAP *ld,
 	char **bufp,
-	unsigned long *lenp,
+	ber_len_t *lenp,
 	int free_input ));
 
 LDAP_F( int )
 ldap_translate_to_t61 LDAP_P((
 	LDAP *ld,
 	char **bufp,
-	unsigned long *lenp, int free_input ));
+	ber_len_t *lenp,
+	int free_input ));
 
 LDAP_F( void )
 ldap_enable_translation LDAP_P((
@@ -1517,13 +1518,13 @@ ldap_enable_translation LDAP_P((
 LDAP_F( int )
 ldap_t61_to_8859 LDAP_P((
 	char **bufp,
-	unsigned long *buflenp,
+	ber_len_t *buflenp,
 	int free_input ));
 
 LDAP_F( int )
 ldap_8859_to_t61 LDAP_P((
 	char **bufp,
-	unsigned long *buflenp,
+	ber_len_t *buflenp,
 	int free_input ));
 
 LDAP_END_DECL
diff --git a/include/portable.h.in b/include/portable.h.in
index 7a22738a25174faacce8793e32e051b7975deef0..5dbfc517b7955878ccb3896e58323023765546a9 100644
--- a/include/portable.h.in
+++ b/include/portable.h.in
@@ -216,6 +216,13 @@
  LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
  LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 */
+/* These are defined in lber_types.h */
+/*
+ LBER_INT_T
+ LBER_LEN_T
+ LBER_SOCKET_T
+ LBER_TAG_T
+*/
 
 /* The number of bytes in a int.  */
 #undef SIZEOF_INT
diff --git a/include/portable.h.nt b/include/portable.h.nt
index beee37a789a7762962e6bdd3bedb561f1f6b091e..dae5950a560a650b257104d5e0136d28b5ded94c 100644
--- a/include/portable.h.nt
+++ b/include/portable.h.nt
@@ -21,6 +21,7 @@
 #define OPENLDAP_PACKAGE "OpenLDAP"
 #define OPENLDAP_VERSION "-devel"
 #define EXEEXT ".exe"
+#define LDAP_DIRSEP "\\"
 
 #if defined( _DEBUG ) && !defined( LDAP_DEBUG )
 /* #define LDAP_MEMORY_DEBUG 1 */
@@ -48,6 +49,8 @@
 /* define type for caddr_t */
 typedef char * caddr_t;
 
+#define ssize_t signed int
+
 #define LOG_DEBUG 0
 #define openlog( a, b )
 #define closelog()
diff --git a/include/srchpref.h b/include/srchpref.h
index 9eb10cc8062dac6fa8324264b5ad0d14d65cdf8d..4a40aa545214714f36eb3ed9b04b8714c258e0d9 100644
--- a/include/srchpref.h
+++ b/include/srchpref.h
@@ -83,7 +83,7 @@ ldap_init_searchprefs LDAP_P(( char *file,
 
 LDAP_F( int )
 ldap_init_searchprefs_buf LDAP_P(( char *buf,
-	long buflen,
+	ber_len_t buflen,
 	struct ldap_searchobj **solistp ));
 
 LDAP_F( void )
diff --git a/libraries/libavl/avl.c b/libraries/libavl/avl.c
index e4b2b1be444111e3c7d97cb7412ca1602c1c026c..27678616ee33db1bbed29ff642379264dee0ec3e 100644
--- a/libraries/libavl/avl.c
+++ b/libraries/libavl/avl.c
@@ -622,7 +622,7 @@ avl_free( Avlnode *root, AVL_FREE dfree )
  */
 
 void*
-avl_find( Avlnode *root, void* data, AVL_CMP fcmp )
+avl_find( Avlnode *root, const void* data, AVL_CMP fcmp )
 {
 	int	cmp;
 
@@ -644,7 +644,7 @@ avl_find( Avlnode *root, void* data, AVL_CMP fcmp )
  */
 
 void*
-avl_find_lin( Avlnode *root, void* data, AVL_CMP fcmp )
+avl_find_lin( Avlnode *root, const void* data, AVL_CMP fcmp )
 {
 	void*	res;
 
diff --git a/libraries/liblber/bprint.c b/libraries/liblber/bprint.c
index 0aa69e7ffa95a64c84836f7c9081ad281aceda20..4a5def2586f49f22061701dda7b955b1518450f4 100644
--- a/libraries/liblber/bprint.c
+++ b/libraries/liblber/bprint.c
@@ -105,7 +105,7 @@ int
 ber_log_bprint(int errlvl,
 	int loglvl,
 	const char *data,
-	int len )
+	ber_len_t len )
 {
 	assert( data != NULL );
 
@@ -120,7 +120,7 @@ ber_log_bprint(int errlvl,
 void
 ber_bprint(
 	LDAP_CONST char *data,
-	int len )
+	ber_len_t len )
 {
     static const char	hexdig[] = "0123456789abcdef";
 #define BPLEN	48
@@ -239,9 +239,11 @@ ber_sos_dump(
 
 	(*ber_pvt_log_print)( "*** sos dump ***\n" );
 
-	while ( sos != NULLSEQORSET ) {
+	while ( sos != NULL ) {
 		sprintf( buf, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n",
-		    (long) sos->sos_clen, (long) sos->sos_first, (long) sos->sos_ptr );
+		    (long) sos->sos_clen,
+			(long) sos->sos_first,
+			(long) sos->sos_ptr );
 		(*ber_pvt_log_print)( buf );
 
 		sprintf( buf, "              current len %ld contents:\n",
diff --git a/libraries/liblber/decode.c b/libraries/liblber/decode.c
index 7d89a1dacd7a938dbd585c0212c9b71de02bb167..d0886582a29fbe73e2781c4acd32af29a5511f61 100644
--- a/libraries/liblber/decode.c
+++ b/libraries/liblber/decode.c
@@ -30,14 +30,17 @@
 
 #include "lber-int.h"
 
-static int ber_getnint LDAP_P(( BerElement *ber, long *num, int len ));
+static ber_tag_t ber_getnint LDAP_P((
+	BerElement *ber,
+	ber_int_t *num,
+	ber_len_t len ));
 
 /* return the tag - LBER_DEFAULT returned means trouble */
-unsigned long
+ber_tag_t
 ber_get_tag( BerElement *ber )
 {
 	unsigned char	xbyte;
-	unsigned long	tag;
+	ber_tag_t	tag;
 	char		*tagp;
 	unsigned int	i;
 
@@ -48,11 +51,11 @@ ber_get_tag( BerElement *ber )
 		return( LBER_DEFAULT );
 
 	if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
-		return( (unsigned long) xbyte );
+		return( (ber_tag_t) xbyte );
 
 	tagp = (char *) &tag;
 	tagp[0] = xbyte;
-	for ( i = 1; i < sizeof(long); i++ ) {
+	for ( i = 1; i < sizeof(ber_tag_t); i++ ) {
 		if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
 			return( LBER_DEFAULT );
 
@@ -63,21 +66,21 @@ ber_get_tag( BerElement *ber )
 	}
 
 	/* tag too big! */
-	if ( i == sizeof(long) )
+	if ( i == sizeof(ber_tag_t) )
 		return( LBER_DEFAULT );
 
 	/* want leading, not trailing 0's */
-	return( tag >> (sizeof(long) - i - 1) );
+	return( tag >> (sizeof(ber_tag_t) - i - 1) );
 }
 
-unsigned long
-ber_skip_tag( BerElement *ber, unsigned long *len )
+ber_tag_t
+ber_skip_tag( BerElement *ber, ber_len_t *len )
 {
-	unsigned long	tag;
+	ber_tag_t	tag;
 	unsigned char	lc;
-	unsigned int	noctets;
+	ber_len_t	noctets;
 	int		diff;
-	unsigned long	netlen;
+	ber_len_t	netlen;
 
 	assert( ber != NULL );
 	assert( len != NULL );
@@ -112,13 +115,13 @@ ber_skip_tag( BerElement *ber, unsigned long *len )
 		return( LBER_DEFAULT );
 	if ( lc & 0x80U ) {
 		noctets = (lc & 0x7fU);
-		if ( (unsigned) noctets > sizeof(unsigned long) )
+		if ( noctets > sizeof(ber_len_t) )
 			return( LBER_DEFAULT );
-		diff = sizeof(unsigned long) - noctets;
+		diff = sizeof(ber_len_t) - noctets;
 		if ( (unsigned) ber_read( ber, (char *) &netlen + diff, noctets )
 		    != noctets )
 			return( LBER_DEFAULT );
-		*len = AC_NTOHL( netlen );
+		*len = LBER_LEN_NTOH( netlen );
 	} else {
 		*len = lc;
 	}
@@ -126,12 +129,12 @@ ber_skip_tag( BerElement *ber, unsigned long *len )
 	return( tag );
 }
 
-unsigned long
+ber_tag_t
 ber_peek_tag(
-	LDAP_CONST BerElement *ber_in, /* not const per c-api-02 */
-	unsigned long *len )
+	LDAP_CONST BerElement *ber_in,
+	ber_len_t *len )
 {
-	unsigned long	tag;
+	ber_tag_t	tag;
 	BerElement *ber;
 
 	assert( ber_in != NULL );
@@ -151,12 +154,13 @@ ber_peek_tag(
 	return( tag );
 }
 
-static int
-ber_getnint( BerElement *ber, long *num, int len )
+static ber_tag_t
+ber_getnint(
+	BerElement *ber,
+	ber_int_t *num,
+	ber_len_t len )
 {
-	int	diff, sign, i;
-	long	netnum;
-	char    *p;
+	unsigned char buf[sizeof(ber_int_t)];
 
 	assert( ber != NULL );
 	assert( num != NULL );
@@ -170,32 +174,40 @@ ber_getnint( BerElement *ber, long *num, int len )
 	 * extend after we read it in.
 	 */
 
-	if ( (unsigned) len > sizeof(long) )
+	if ( len > sizeof(ber_int_t) )
 		return( -1 );
 
-	netnum = 0;
-	diff = sizeof(long) - len;
-	/* read into the low-order bytes of netnum */
-	if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
+	/* read into the low-order bytes of our buffer */
+	if ( (ber_len_t) ber_read( ber, buf, len ) != len ) {
 		return( -1 );
+	}
+
+	if( len ) {
+		/* sign extend if necessary */
+		ber_len_t i;
+		ber_int_t netnum = 0x80 & buf[0] ? -1 : 0;
 
-        /* sign extend if necessary */
-        p = (char *) &netnum;
-        sign = (0x80 & *(p+diff) );
-        if ( sign && ((unsigned) len < sizeof(long)) ) {
-                for ( i = 0; i < diff; i++ ) {
-                        *(p+i) = (unsigned char) 0xff;
+		/* shift in the bytes */
+		for( i=0 ; i<len; i++ ) {
+			netnum = (netnum << 8 ) | buf[i];
 		}
+
+		*num = netnum;
+
+	} else {
+		*num = 0;
 	}
-	*num = AC_NTOHL( netnum );
 
 	return( len );
 }
 
-unsigned long
-ber_get_int( BerElement *ber, long *num )
+ber_tag_t
+ber_get_int(
+	BerElement *ber,
+	ber_int_t *num )
 {
-	unsigned long	tag, len;
+	ber_tag_t	tag;
+	ber_len_t	len;
 
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -203,16 +215,21 @@ ber_get_int( BerElement *ber, long *num )
 	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
 		return( LBER_DEFAULT );
 
-	if ( (unsigned long) ber_getnint( ber, num, (int)len ) != len )
+	if ( (unsigned long) ber_getnint( ber, num, len ) != len )
 		return( LBER_DEFAULT );
 	else
 		return( tag );
 }
 
-unsigned long
-ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
+ber_tag_t
+ber_get_stringb(
+	BerElement *ber,
+	char *buf,
+	ber_len_t *len )
 {
-	unsigned long	datalen, tag;
+	ber_len_t	datalen;
+	ber_tag_t	tag;
+
 #ifdef STR_TRANSLATION
 	char		*transbuf;
 #endif /* STR_TRANSLATION */
@@ -253,10 +270,11 @@ ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
 	return( tag );
 }
 
-unsigned long
+ber_tag_t
 ber_get_stringa( BerElement *ber, char **buf )
 {
-	unsigned long	datalen, tag;
+	ber_len_t	datalen;
+	ber_tag_t	tag;
 
 	assert( ber != NULL );
 	assert( buf != NULL );
@@ -268,7 +286,7 @@ ber_get_stringa( BerElement *ber, char **buf )
 		return( LBER_DEFAULT );
 	}
 
-	if ( (*buf = (char *) LBER_MALLOC( (size_t)datalen + 1 )) == NULL )
+	if ( (*buf = (char *) LBER_MALLOC( datalen + 1 )) == NULL )
 		return( LBER_DEFAULT );
 
 	if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
@@ -294,10 +312,11 @@ ber_get_stringa( BerElement *ber, char **buf )
 	return( tag );
 }
 
-unsigned long
+ber_tag_t
 ber_get_stringal( BerElement *ber, struct berval **bv )
 {
-	unsigned long	len, tag;
+	ber_len_t	len;
+	ber_tag_t	tag;
 
 	assert( ber != NULL );
 	assert( bv != NULL );
@@ -312,13 +331,13 @@ ber_get_stringal( BerElement *ber, struct berval **bv )
 	if ( (*bv = (struct berval *) LBER_MALLOC( sizeof(struct berval) )) == NULL )
 		return( LBER_DEFAULT );
 
-	if ( ((*bv)->bv_val = (char *) LBER_MALLOC( (size_t)len + 1 )) == NULL ) {
+	if ( ((*bv)->bv_val = (char *) LBER_MALLOC( len + 1 )) == NULL ) {
 		LBER_FREE( *bv );
 		*bv = NULL;
 		return( LBER_DEFAULT );
 	}
 
-	if ( (unsigned long) ber_read( ber, (*bv)->bv_val, len ) != len ) {
+	if ( (ber_len_t) ber_read( ber, (*bv)->bv_val, len ) != len ) {
 		ber_bvfree( *bv );
 		*bv = NULL;
 		return( LBER_DEFAULT );
@@ -343,10 +362,14 @@ ber_get_stringal( BerElement *ber, struct berval **bv )
 	return( tag );
 }
 
-unsigned long
-ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
+ber_tag_t
+ber_get_bitstringa(
+	BerElement *ber,
+	char **buf,
+	ber_len_t *blen )
 {
-	unsigned long	datalen, tag;
+	ber_len_t	datalen;
+	ber_tag_t	tag;
 	unsigned char	unusedbits;
 
 	assert( ber != NULL );
@@ -361,7 +384,7 @@ ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
 	}
 	--datalen;
 
-	if ( (*buf = (char *) LBER_MALLOC( (size_t)datalen )) == NULL )
+	if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL )
 		return( LBER_DEFAULT );
 
 	if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
@@ -380,10 +403,11 @@ ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
 	return( tag );
 }
 
-unsigned long
+ber_tag_t
 ber_get_null( BerElement *ber )
 {
-	unsigned long	len, tag;
+	ber_len_t	len;
+	ber_tag_t	tag;
 
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -397,11 +421,13 @@ ber_get_null( BerElement *ber )
 	return( tag );
 }
 
-unsigned long
-ber_get_boolean( BerElement *ber, int *boolval )
+ber_tag_t
+ber_get_boolean(
+	BerElement *ber,
+	ber_int_t *boolval )
 {
-	long	longbool;
-	int	rc;
+	ber_int_t	longbool;
+	ber_tag_t	rc;
 
 	assert( ber != NULL );
 	assert( boolval != NULL );
@@ -414,8 +440,11 @@ ber_get_boolean( BerElement *ber, int *boolval )
 	return( rc );
 }
 
-unsigned long
-ber_first_element( BerElement *ber, unsigned long *len, char **last )
+ber_tag_t
+ber_first_element(
+	BerElement *ber,
+	ber_len_t *len,
+	char **last )
 {
 	assert( ber != NULL );
 	assert( len != NULL );
@@ -436,8 +465,11 @@ ber_first_element( BerElement *ber, unsigned long *len, char **last )
 	return( ber_peek_tag( ber, len ) );
 }
 
-unsigned long
-ber_next_element( BerElement *ber, unsigned long *len, char *last )
+ber_tag_t
+ber_next_element(
+	BerElement *ber,
+	ber_len_t *len,
+	char *last )
 {
 	assert( ber != NULL );
 	assert( len != NULL );
@@ -453,7 +485,7 @@ ber_next_element( BerElement *ber, unsigned long *len, char *last )
 }
 
 /* VARARGS */
-unsigned long
+ber_tag_t
 ber_scanf
 #if HAVE_STDARG
 	( BerElement *ber,
@@ -473,9 +505,12 @@ va_dcl
 	char		*last;
 	char		*s, **ss, ***sss;
 	struct berval 	***bv, **bvp, *bval;
-	int		*i, j;
-	long		*l;
-	unsigned long	rc, tag, len;
+	ber_int_t	*i;
+	int j;
+	ber_len_t	*l;
+	ber_tag_t	*t;
+	ber_tag_t	rc, tag;
+	ber_len_t	len;
 
 #ifdef HAVE_STDARG
 	va_start( ap, fmt );
@@ -516,18 +551,18 @@ va_dcl
 			break;
 
 		case 'b':	/* boolean */
-			i = va_arg( ap, int * );
+			i = va_arg( ap, ber_int_t * );
 			rc = ber_get_boolean( ber, i );
 			break;
 
 		case 'e':	/* enumerated */
 		case 'i':	/* int */
-			l = va_arg( ap, long * );
-			rc = ber_get_int( ber, l );
+			i = va_arg( ap, ber_int_t * );
+			rc = ber_get_int( ber, i );
 			break;
 
 		case 'l':	/* length of next item */
-			l = va_arg( ap, long * );
+			l = va_arg( ap, ber_len_t * );
 			rc = ber_peek_tag( ber, l );
 			break;
 
@@ -537,7 +572,7 @@ va_dcl
 
 		case 's':	/* octet string - in a buffer */
 			s = va_arg( ap, char * );
-			l = va_arg( ap, long * );
+			l = va_arg( ap, ber_len_t * );
 			rc = ber_get_stringb( ber, s, l );
 			break;
 
@@ -554,18 +589,18 @@ va_dcl
 
 		case 'B':	/* bit string - allocate storage as needed */
 			ss = va_arg( ap, char ** );
-			l = va_arg( ap, long * ); /* for length, in bits */
+			l = va_arg( ap, ber_len_t * ); /* for length, in bits */
 			rc = ber_get_bitstringa( ber, ss, l );
 			break;
 
 		case 't':	/* tag of next item */
-			l = va_arg( ap, long * );
-			*l = rc = ber_peek_tag( ber, &len );
+			t = va_arg( ap, ber_tag_t * );
+			*t = rc = ber_peek_tag( ber, &len );
 			break;
 
 		case 'T':	/* skip tag of next item */
-			l = va_arg( ap, long * );
-			*l = rc = ber_skip_tag( ber, &len );
+			t = va_arg( ap, ber_tag_t * );
+			*t = rc = ber_skip_tag( ber, &len );
 			break;
 
 		case 'v':	/* sequence of strings */
@@ -666,20 +701,23 @@ va_dcl
 			break;
 
 		case 'b':	/* boolean */
+		case 'e':	/* enumerated */
+		case 'i':	/* int */
 			(void) va_arg( ap, int * );
 			break;
 
 		case 's':	/* octet string - in a buffer */
 			(void) va_arg( ap, char * );
-			(void) va_arg( ap, long * );
+			(void) va_arg( ap, ber_len_t * );
 			break;
 
-		case 'e':	/* enumerated */
-		case 'i':	/* int */
 		case 'l':	/* length of next item */
+			(void) va_arg( ap, ber_len_t * );
+			break;
+
 		case 't':	/* tag of next item */
 		case 'T':	/* skip tag of next item */
-			(void) va_arg( ap, long * );
+			(void) va_arg( ap, ber_tag_t * );
 			break;
 
 		case 'o':	/* octet string in a supplied berval */
@@ -705,7 +743,7 @@ va_dcl
 				LBER_FREE( *ss );
 				*ss = NULL;
 			}
-			*(va_arg( ap, long * )) = 0; /* for length, in bits */
+			*(va_arg( ap, ber_len_t * )) = 0; /* for length, in bits */
 			break;
 
 		case 'v':	/* sequence of strings */
diff --git a/libraries/liblber/encode.c b/libraries/liblber/encode.c
index f3b5c4803b031d2d43d495915769832aaac8c962..4e5081a830b0dbfbbed470c82c4808f7fc006afc 100644
--- a/libraries/liblber/encode.c
+++ b/libraries/liblber/encode.c
@@ -30,27 +30,32 @@
 
 #include "lber-int.h"
 
-static int ber_put_len LDAP_P(( BerElement *ber,
-	unsigned long len, int nosos ));
+static int ber_put_len LDAP_P((
+	BerElement *ber,
+	ber_len_t len,
+	int nosos ));
 
-static int ber_start_seqorset LDAP_P(( BerElement *ber,
-	unsigned long tag ));
+static int ber_start_seqorset LDAP_P((
+	BerElement *ber,
+	ber_tag_t tag ));
 
 static int ber_put_seqorset LDAP_P(( BerElement *ber ));
 
-static int ber_put_int_or_enum LDAP_P(( BerElement *ber,
-	long num, unsigned long tag ));
+static int ber_put_int_or_enum LDAP_P((
+	BerElement *ber,
+	ber_int_t num,
+	ber_tag_t tag ));
 
 
-static int
-ber_calc_taglen( unsigned long tag )
+static ber_len_t
+ber_calc_taglen( ber_tag_t tag )
 {
 	int	i;
-	unsigned long	mask;
+	ber_tag_t	mask;
 
 	/* find the first non-all-zero byte in the tag */
-	for ( i = sizeof(long) - 1; i > 0; i-- ) {
-		mask = (0xffUL << (i * 8));
+	for ( i = sizeof(ber_tag_t) - 1; i > 0; i-- ) {
+		mask = ((ber_tag_t)0xffU << (i * 8));
 		/* not all zero */
 		if ( tag & mask )
 			break;
@@ -60,31 +65,38 @@ ber_calc_taglen( unsigned long tag )
 }
 
 static int
-ber_put_tag( BerElement	*ber, unsigned long tag, int nosos )
+ber_put_tag(
+	BerElement	*ber,
+	ber_tag_t tag,
+	int nosos )
 {
-	int		taglen;
-	unsigned long	ntag;
+	int rc;
+	ber_len_t	taglen;
+	ber_tag_t	ntag;
 
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
 
 	taglen = ber_calc_taglen( tag );
 
-	ntag = AC_HTONL( tag );
+	ntag = LBER_TAG_HTON( tag );
+
+	rc = ber_write( ber,
+		((char *) &ntag) + sizeof(ber_tag_t) - taglen,
+	    taglen, nosos );
 
-	return( ber_write( ber, ((char *) &ntag) + sizeof(long) - taglen,
-	    taglen, nosos ) );
+	return rc;
 }
 
-static int
-ber_calc_lenlen( unsigned long len )
+static ber_len_t
+ber_calc_lenlen( ber_len_t len )
 {
 	/*
 	 * short len if it's less than 128 - one byte giving the len,
 	 * with bit 8 0.
 	 */
 
-	if ( len <= 0x7FUL )
+	if ( len <= (ber_len_t) 0x7FU )
 		return( 1 );
 
 	/*
@@ -92,23 +104,23 @@ ber_calc_lenlen( unsigned long len )
 	 * length of the length, followed by the length itself.
 	 */
 
-	if ( len <= 0xffUL )
+	if ( len <= (ber_len_t) 0xffU )
 		return( 2 );
-	if ( len <= 0xffffUL )
+	if ( len <= (ber_len_t) 0xffffU )
 		return( 3 );
-	if ( len <= 0xffffffUL )
+	if ( len <= (ber_len_t) 0xffffffU )
 		return( 4 );
 
 	return( 5 );
 }
 
 static int
-ber_put_len( BerElement *ber, unsigned long len, int nosos )
+ber_put_len( BerElement *ber, ber_len_t len, int nosos )
 {
 	int		i;
 	char		lenlen;
-	unsigned long	mask;
-	unsigned long	netlen;
+	ber_len_t	mask;
+	ber_len_t	netlen;
 
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -119,7 +131,7 @@ ber_put_len( BerElement *ber, unsigned long len, int nosos )
 	 */
 
 	if ( len <= 127 ) {
-		netlen = AC_HTONL( len );
+		netlen = LBER_LEN_HTON( len );
 		return( ber_write( ber, (char *) &netlen + sizeof(long) - 1,
 		    1, nosos ) );
 	}
@@ -130,8 +142,8 @@ ber_put_len( BerElement *ber, unsigned long len, int nosos )
 	 */
 
 	/* find the first non-all-zero byte */
-	for ( i = sizeof(long) - 1; i > 0; i-- ) {
-		mask = (0xffUL << (i * 8));
+	for ( i = sizeof(ber_len_t) - 1; i > 0; i-- ) {
+		mask = (0xffU << (i * 8));
 		/* not all zero */
 		if ( len & mask )
 			break;
@@ -146,8 +158,8 @@ ber_put_len( BerElement *ber, unsigned long len, int nosos )
 		return( -1 );
 
 	/* write the length itself */
-	netlen = AC_HTONL( len );
-	if ( ber_write( ber, (char *) &netlen + (sizeof(long) - i), i, nosos )
+	netlen = LBER_LEN_HTON( len );
+	if ( ber_write( ber, (char *) &netlen + (sizeof(ber_len_t) - i), i, nosos )
 	    != i )
 		return( -1 );
 
@@ -155,11 +167,14 @@ ber_put_len( BerElement *ber, unsigned long len, int nosos )
 }
 
 static int
-ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
+ber_put_int_or_enum(
+	BerElement *ber,
+	ber_int_t num,
+	ber_tag_t tag )
 {
-	int	i, sign, taglen;
-	int	len, lenlen;
-	long	netnum, mask;
+	int	i, sign;
+	ber_len_t	len, lenlen, taglen;
+	ber_int_t	netnum, mask;
 
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -170,8 +185,8 @@ ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
 	 * high bit is set - look for first non-all-one byte
 	 * high bit is clear - look for first non-all-zero byte
 	 */
-	for ( i = sizeof(long) - 1; i > 0; i-- ) {
-		mask = (0xffUL << (i * 8));
+	for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
+		mask = (0xffU << (i * 8));
 
 		if ( sign ) {
 			/* not all ones */
@@ -188,7 +203,7 @@ ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
 	 * we now have the "leading byte".  if the high bit on this
 	 * byte matches the sign bit, we need to "back up" a byte.
 	 */
-	mask = (num & (0x80UL << (i * 8)));
+	mask = (num & (0x80U << (i * 8)));
 	if ( (mask && !sign) || (sign && !mask) )
 		i++;
 
@@ -200,8 +215,8 @@ ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
 	if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
 		return( -1 );
 	i++;
-	netnum = AC_HTONL( num );
-	if ( ber_write( ber, (char *) &netnum + (sizeof(long) - i), i, 0 )
+	netnum = LBER_INT_HTON( num );
+	if ( ber_write( ber, (char *) &netnum + (sizeof(ber_int_t) - i), i, 0 )
 	   != i )
 		return( -1 );
 
@@ -210,7 +225,10 @@ ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
 }
 
 int
-ber_put_enum( BerElement *ber, long num, unsigned long tag )
+ber_put_enum(
+	BerElement *ber,
+	ber_int_t num,
+	ber_tag_t tag )
 {
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -222,7 +240,10 @@ ber_put_enum( BerElement *ber, long num, unsigned long tag )
 }
 
 int
-ber_put_int( BerElement *ber, long num, unsigned long tag )
+ber_put_int(
+	BerElement *ber,
+	ber_int_t num,
+	ber_tag_t tag )
 {
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -237,10 +258,11 @@ int
 ber_put_ostring(
 	BerElement *ber,
 	LDAP_CONST char *str,
-	unsigned long len,
-	unsigned long tag )
+	ber_len_t len,
+	ber_tag_t tag )
 {
-	int	taglen, lenlen, rc;
+	ber_len_t	taglen, lenlen;
+	int rc;
 #ifdef STR_TRANSLATION
 	int	free_str;
 #endif /* STR_TRANSLATION */
@@ -285,11 +307,12 @@ ber_put_ostring(
 
 	return( rc );
 }
+
 int
 ber_put_berval(
 	BerElement *ber,
 	LDAP_CONST struct berval *bv,
-	unsigned long tag )
+	ber_tag_t tag )
 {
 	assert( ber != NULL );
 	assert( bv != NULL );
@@ -307,7 +330,7 @@ int
 ber_put_string(
 	BerElement *ber,
 	LDAP_CONST char *str,
-	unsigned long tag )
+	ber_tag_t tag )
 {
 	assert( ber != NULL );
 	assert( str != NULL );
@@ -321,10 +344,10 @@ int
 ber_put_bitstring(
 	BerElement *ber,
 	LDAP_CONST char *str,
-	unsigned long blen /* in bits */,
-	unsigned long tag )
+	ber_len_t blen /* in bits */,
+	ber_tag_t tag )
 {
-	int		taglen, lenlen, len;
+	ber_len_t		taglen, lenlen, len;
 	unsigned char	unusedbits;
 
 	assert( ber != NULL );
@@ -346,7 +369,7 @@ ber_put_bitstring(
 	if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
 		return( -1 );
 
-	if ( ber_write( ber, str, len, 0 ) != len )
+	if ( (ber_len_t) ber_write( ber, str, len, 0 ) != len )
 		return( -1 );
 
 	/* return length of tag + length + unused bit count + contents */
@@ -354,9 +377,9 @@ ber_put_bitstring(
 }
 
 int
-ber_put_null( BerElement *ber, unsigned long tag )
+ber_put_null( BerElement *ber, ber_tag_t tag )
 {
-	int	taglen;
+	ber_len_t	taglen;
 
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -374,10 +397,13 @@ ber_put_null( BerElement *ber, unsigned long tag )
 }
 
 int
-ber_put_boolean( BerElement *ber, int boolval, unsigned long tag )
+ber_put_boolean(
+	BerElement *ber,
+	ber_int_t boolval,
+	ber_tag_t tag )
 {
-	int		taglen;
-	unsigned char	trueval = 0xffU;
+	ber_len_t		taglen;
+	unsigned char	trueval = 0xFFU;
 	unsigned char	falseval = 0x00U;
 
 	assert( ber != NULL );
@@ -402,7 +428,9 @@ ber_put_boolean( BerElement *ber, int boolval, unsigned long tag )
 #define FOUR_BYTE_LEN	5
 
 static int
-ber_start_seqorset( BerElement *ber, unsigned long tag )
+ber_start_seqorset(
+	BerElement *ber,
+	ber_tag_t tag )
 {
 	Seqorset	*new;
 
@@ -411,11 +439,11 @@ ber_start_seqorset( BerElement *ber, unsigned long tag )
 
 	new = (Seqorset *) LBER_CALLOC( 1, sizeof(Seqorset) );
 
-	if ( new == NULLSEQORSET )
+	if ( new == NULL )
 		return( -1 );
 
 	new->sos_ber = ber;
-	if ( ber->ber_sos == NULLSEQORSET )
+	if ( ber->ber_sos == NULL )
 		new->sos_first = ber->ber_ptr;
 	else
 		new->sos_first = ber->ber_sos->sos_ptr;
@@ -431,7 +459,7 @@ ber_start_seqorset( BerElement *ber, unsigned long tag )
 }
 
 int
-ber_start_seq( BerElement *ber, unsigned long tag )
+ber_start_seq( BerElement *ber, ber_tag_t tag )
 {
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -443,7 +471,7 @@ ber_start_seq( BerElement *ber, unsigned long tag )
 }
 
 int
-ber_start_set( BerElement *ber, unsigned long tag )
+ber_start_set( BerElement *ber, ber_tag_t tag )
 {
 	assert( ber != NULL );
 	assert( BER_VALID( ber ) );
@@ -457,8 +485,8 @@ ber_start_set( BerElement *ber, unsigned long tag )
 static int
 ber_put_seqorset( BerElement *ber )
 {
-	unsigned long	len, netlen;
-	int		taglen, lenlen;
+	ber_len_t	len, netlen;
+	ber_len_t	taglen, lenlen;
 	unsigned char	ltag = 0x80U + FOUR_BYTE_LEN - 1;
 	Seqorset	*next;
 	Seqorset	**sos = &ber->ber_sos;
@@ -475,8 +503,8 @@ ber_put_seqorset( BerElement *ber )
 	 */
 
 	len = (*sos)->sos_clen;
-	netlen = AC_HTONL( len );
-	if ( sizeof(long) > 4 && len > 0xffffffffUL )
+	netlen = LBER_LEN_HTON( len );
+	if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL )
 		return( -1 );
 
 	if ( ber->ber_options & LBER_USE_DER ) {
@@ -485,7 +513,7 @@ ber_put_seqorset( BerElement *ber )
 		lenlen = FOUR_BYTE_LEN;
 	}
 
-	if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
+	if ( (next = (*sos)->sos_next) == NULL ) {
 		/* write the tag */
 		if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
 			return( -1 );
@@ -520,18 +548,18 @@ ber_put_seqorset( BerElement *ber )
 		/* The ber_ptr is at the set/seq start - move it to the end */
 		(*sos)->sos_ber->ber_ptr += len;
 	} else {
-		unsigned long	ntag;
+		ber_tag_t	ntag;
 
 		/* the tag */
 		taglen = ber_calc_taglen( (*sos)->sos_tag );
-		ntag = AC_HTONL( (*sos)->sos_tag );
+		ntag = LBER_TAG_HTON( (*sos)->sos_tag );
 		SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
-		    sizeof(long) - taglen, taglen );
+		    sizeof(ber_tag_t) - taglen, taglen );
 
 		if ( ber->ber_options & LBER_USE_DER ) {
 			ltag = (lenlen == 1)
 				? (unsigned char) len
-				: (unsigned char) (0x80UL + (lenlen - 1));
+				: (unsigned char) (0x80U + (lenlen - 1));
 		}
 
 		/* one byte of length length */
@@ -541,7 +569,7 @@ ber_put_seqorset( BerElement *ber )
 			if (lenlen > 1) {
 				/* Write the length itself */
 				SAFEMEMCPY( (*sos)->sos_first + 2,
-				    (char *)&netlen + sizeof(unsigned long) -
+				    (char *)&netlen + sizeof(ber_len_t) -
 				    (lenlen - 1),
 				    lenlen - 1 );
 			}
@@ -558,7 +586,7 @@ ber_put_seqorset( BerElement *ber )
 		} else {
 			/* the length itself */
 			SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
-			    (char *) &netlen + sizeof(long) -
+			    (char *) &netlen + sizeof(ber_len_t) -
 			    (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
 		}
 
@@ -610,8 +638,9 @@ va_dcl
 #endif
 	char		*s, **ss;
 	struct berval	*bv, **bvp;
-	int		rc, i;
-	unsigned long	len;
+	int		rc;
+	ber_int_t	i;
+	ber_len_t	len;
 
 #ifdef HAVE_STDARG
 	va_start( ap, fmt );
@@ -639,17 +668,17 @@ va_dcl
 			} break;
 
 		case 'b':	/* boolean */
-			i = va_arg( ap, int );
+			i = va_arg( ap, ber_int_t );
 			rc = ber_put_boolean( ber, i, ber->ber_tag );
 			break;
 
 		case 'i':	/* int */
-			i = va_arg( ap, int );
+			i = va_arg( ap, ber_int_t );
 			rc = ber_put_int( ber, i, ber->ber_tag );
 			break;
 
 		case 'e':	/* enumeration */
-			i = va_arg( ap, int );
+			i = va_arg( ap, ber_int_t );
 			rc = ber_put_enum( ber, i, ber->ber_tag );
 			break;
 
@@ -659,7 +688,7 @@ va_dcl
 
 		case 'o':	/* octet string (non-null terminated) */
 			s = va_arg( ap, char * );
-			len = va_arg( ap, int );
+			len = va_arg( ap, ber_len_t );
 			rc = ber_put_ostring( ber, s, len, ber->ber_tag );
 			break;
 
@@ -681,7 +710,7 @@ va_dcl
 			break;
 
 		case 't':	/* tag for the next element */
-			ber->ber_tag = va_arg( ap, unsigned long );
+			ber->ber_tag = va_arg( ap, ber_tag_t );
 			ber->ber_usertag = 1;
 			break;
 
diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c
index 607bb753b53cb76a319e121b6f4d8039e48785d3..a29cdf54d53df22d19871ef70924741d15a6490c 100644
--- a/libraries/liblber/io.c
+++ b/libraries/liblber/io.c
@@ -36,8 +36,14 @@
 
 #include "lber-int.h"
 
-static long BerRead LDAP_P(( Sockbuf *sb, char *buf, long len ));
-static int ber_realloc LDAP_P(( BerElement *ber, unsigned long len ));
+static ber_slen_t BerRead LDAP_P((
+	Sockbuf *sb,
+	char *buf,
+	ber_len_t len ));
+
+static int ber_realloc LDAP_P((
+	BerElement *ber,
+	ber_len_t len ));
 
 #define EXBUFSIZ	1024
 
@@ -49,11 +55,14 @@ static int ber_realloc LDAP_P(( BerElement *ber, unsigned long len ));
 # define MAX_BERBUFSIZE 65535
 #endif
 
-static long
-BerRead( Sockbuf *sb, char *buf, long len )
+static ber_slen_t
+BerRead(
+	Sockbuf *sb,
+	char *buf,
+	ber_len_t len )
 {
-	int	c;
-	long	nread = 0;
+	ber_slen_t	c;
+	ber_slen_t	nread = 0;
 
 	assert( sb != NULL );
 	assert( buf != NULL );
@@ -74,10 +83,13 @@ BerRead( Sockbuf *sb, char *buf, long len )
 	return( nread );
 }
 
-long
-ber_read( BerElement *ber, char *buf, unsigned long len )
+ber_slen_t
+ber_read(
+	BerElement *ber,
+	char *buf,
+	ber_len_t len )
 {
-	unsigned long	actuallen, nleft;
+	ber_len_t	actuallen, nleft;
 
 	assert( ber != NULL );
 	assert( buf != NULL );
@@ -87,18 +99,18 @@ ber_read( BerElement *ber, char *buf, unsigned long len )
 	nleft = ber->ber_end - ber->ber_ptr;
 	actuallen = nleft < len ? nleft : len;
 
-	SAFEMEMCPY( buf, ber->ber_ptr, (size_t)actuallen );
+	SAFEMEMCPY( buf, ber->ber_ptr, actuallen );
 
 	ber->ber_ptr += actuallen;
 
-	return( (long)actuallen );
+	return( (ber_slen_t) actuallen );
 }
 
-long
+ber_slen_t
 ber_write(
 	BerElement *ber,
 	LDAP_CONST char *buf,
-	unsigned long len,
+	ber_len_t len,
 	int nosos )
 {
 	assert( ber != NULL );
@@ -113,7 +125,8 @@ ber_write(
 		}
 		SAFEMEMCPY( ber->ber_ptr, buf, (size_t)len );
 		ber->ber_ptr += len;
-		return( len );
+		return( (ber_slen_t) len );
+
 	} else {
 		if ( ber->ber_sos->sos_ptr + len > ber->ber_end ) {
 			if ( ber_realloc( ber, len ) != 0 )
@@ -122,14 +135,14 @@ ber_write(
 		SAFEMEMCPY( ber->ber_sos->sos_ptr, buf, (size_t)len );
 		ber->ber_sos->sos_ptr += len;
 		ber->ber_sos->sos_clen += len;
-		return( len );
+		return( (ber_slen_t) len );
 	}
 }
 
 static int
-ber_realloc( BerElement *ber, unsigned long len )
+ber_realloc( BerElement *ber, ber_len_t len )
 {
-	unsigned long	need, have, total;
+	ber_len_t	need, have, total;
 	Seqorset	*s;
 	long		off;
 	char		*oldbuf;
@@ -163,7 +176,7 @@ ber_realloc( BerElement *ber, unsigned long len )
 	if ( ber->ber_buf != oldbuf ) {
 		ber->ber_ptr = ber->ber_buf + (ber->ber_ptr - oldbuf);
 
-		for ( s = ber->ber_sos; s != NULLSEQORSET; s = s->sos_next ) {
+		for ( s = ber->ber_sos; s != NULL; s = s->sos_next ) {
 			off = s->sos_first - oldbuf;
 			s->sos_first = ber->ber_buf + off;
 
@@ -200,7 +213,8 @@ ber_free( BerElement *ber, int freebuf )
 int
 ber_flush( Sockbuf *sb, BerElement *ber, int freeit )
 {
-	long	nwritten, towrite, rc;	
+	ber_len_t	nwritten, towrite;
+	ber_slen_t	rc;	
 
 	assert( sb != NULL );
 	assert( ber != NULL );
@@ -257,8 +271,8 @@ ber_alloc_t( int options )
 
 	ber = (BerElement *) LBER_CALLOC( 1, sizeof(BerElement) );
 
-	if ( ber == NULLBER )
-		return( NULLBER );
+	if ( ber == NULL )
+		return( NULL );
 
 	ber->ber_valid = LBER_VALID_BERELEMENT;
 	ber->ber_tag = LBER_DEFAULT;
@@ -336,13 +350,13 @@ ber_init( struct berval *bv )
 
 	ber = ber_alloc_t( 0 );
 
-	if( ber == NULLBER ) {
+	if( ber == NULL ) {
 		/* allocation failed */
 		return ( NULL );
 	}
 
 	/* copy the data */
-	if ( ( (unsigned int) ber_write ( ber, bv->bv_val, bv->bv_len, 0 )) != bv->bv_len ) {
+	if ( ( (ber_len_t) ber_write ( ber, bv->bv_val, bv->bv_len, 0 )) != bv->bv_len ) {
 		/* write failed, so free and return NULL */
 		ber_free( ber, 1 );
 		return( NULL );
@@ -383,14 +397,14 @@ int ber_flatten(
 
 	} else {
 		/* copy the berval */
-		ptrdiff_t len = ber->ber_ptr - ber->ber_buf;
+		ber_len_t len = ber->ber_ptr - ber->ber_buf;
 
 		if ( (bv->bv_val = (char *) LBER_MALLOC( len + 1 )) == NULL ) {
 			ber_bvfree( bv );
 			return( -1 );
 		}
 
-		SAFEMEMCPY( bv->bv_val, ber->ber_buf, (size_t)len );
+		SAFEMEMCPY( bv->bv_val, ber->ber_buf, len );
 		bv->bv_val[len] = '\0';
 		bv->bv_len = len;
 	}
@@ -417,11 +431,11 @@ ber_reset( BerElement *ber, int was_writing )
 
 #if 0
 /* return the tag - LBER_DEFAULT returned means trouble */
-static unsigned long
+static ber_tag_t
 get_tag( Sockbuf *sb )
 {
 	unsigned char	xbyte;
-	unsigned long	tag;
+	ber_tag_t	tag;
 	char		*tagp;
 	unsigned int	i;
 
@@ -432,11 +446,11 @@ get_tag( Sockbuf *sb )
 		return( LBER_DEFAULT );
 
 	if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
-		return( (unsigned long) xbyte );
+		return( (ber_tag_t) xbyte );
 
 	tagp = (char *) &tag;
 	tagp[0] = xbyte;
-	for ( i = 1; i < sizeof(long); i++ ) {
+	for ( i = 1; i < sizeof(ber_tag_t); i++ ) {
 		if ( ber_pvt_sb_read( sb, (char *) &xbyte, 1 ) != 1 )
 			return( LBER_DEFAULT );
 
@@ -447,11 +461,11 @@ get_tag( Sockbuf *sb )
 	}
 
 	/* tag too big! */
-	if ( i == sizeof(long) )
+	if ( i == sizeof(ber_tag_t) )
 		return( LBER_DEFAULT );
 
 	/* want leading, not trailing 0's */
-	return( tag >> (sizeof(long) - i - 1) );
+	return( tag >> (sizeof(ber_tag_t) - i - 1) );
 }
 #endif
 
@@ -461,8 +475,11 @@ get_tag( Sockbuf *sb )
  * a full packet is read.
  */
 
-unsigned long
-ber_get_next( Sockbuf *sb, unsigned long *len, BerElement *ber )
+ber_tag_t
+ber_get_next(
+	Sockbuf *sb,
+	ber_len_t *len,
+	BerElement *ber )
 {
 	assert( sb != NULL );
 	assert( len != NULL );
@@ -531,7 +548,7 @@ get_lenbyte:
 			return LBER_DEFAULT;
 		if (c & 0x80U) {
 			int len = c & 0x7fU;
-			if ( (len==0) || ((unsigned) len>sizeof( ber->ber_len ) ) ) {
+			if ( (len==0) || ( len>sizeof( ber->ber_len ) ) ) {
 				errno = ERANGE;
 				return LBER_DEFAULT;
 			}
@@ -544,8 +561,8 @@ get_lenbyte:
 		}
 	}
 	if (PTR_IN_VAR(ber->ber_rwptr, ber->ber_len)) {
-		int res;
-		int to_go;
+		ber_slen_t res;
+		ber_slen_t to_go;
 		to_go = (char *) &ber->ber_len + sizeof( ber->ber_len ) -
 			ber->ber_rwptr;
 		assert( to_go > 0 );
@@ -555,7 +572,7 @@ get_lenbyte:
 		ber->ber_rwptr += res;
 		if (res==to_go) {
 			/* convert length. */
-			ber->ber_len = AC_NTOHL( ber->ber_len );
+			ber->ber_len = LBER_LEN_NTOH( ber->ber_len );
 			goto fill_buffer;
 		} else {
 #if defined( EWOULDBLOCK )
@@ -581,8 +598,8 @@ fill_buffer:
 		ber->ber_end = ber->ber_buf + ber->ber_len;
 	}
 	if ((ber->ber_rwptr>=ber->ber_buf) && (ber->ber_rwptr<ber->ber_end)) {
-		int res;
-		int to_go;
+		ber_slen_t res;
+		ber_slen_t to_go;
 		
 		to_go = ber->ber_end - ber->ber_rwptr;
 		assert( to_go > 0 );
diff --git a/libraries/liblber/lber-int.h b/libraries/liblber/lber-int.h
index cbe28423eb40f3aaf128683c8c821a35090c75f3..cb68944ede89e8283ea2ef2031cf5f8286137952 100644
--- a/libraries/liblber/lber-int.h
+++ b/libraries/liblber/lber-int.h
@@ -23,6 +23,15 @@
 
 LDAP_BEGIN_DECL
 
+/* these have to match lber types settings */
+#define LBER_INT_HTON(i)	AC_HTONL(i)
+#define LBER_INT_NTOH(i)	AC_NTOHL(i)
+#define LBER_LEN_HTON(l)	AC_HTONL(l)
+#define LBER_LEN_NTOH(l)	AC_NTOHL(l)
+#define LBER_TAG_HTON(t)	AC_HTONL(t)
+#define LBER_TAG_NTOH(t)	AC_NTOHL(t)
+
+
 struct lber_options {
 	short lbo_valid;
 #define LBER_UNINITIALIZED		0x0
@@ -43,10 +52,10 @@ struct berelement {
 #define ber_options		ber_opts.lbo_options
 #define ber_debug		ber_opts.lbo_debug
 
-	int			ber_usertag;
+	ber_tag_t	ber_usertag;
 
-	unsigned long	ber_tag;
-	unsigned long	ber_len;
+	ber_tag_t	ber_tag;
+	ber_len_t	ber_len;
 
 	char		*ber_buf;
 	char		*ber_ptr;
@@ -58,7 +67,7 @@ struct berelement {
 	BERTranslateProc ber_decode_translate_proc;
 };
 #define BER_VALID(ber)	((ber)->ber_valid==LBER_VALID_BERELEMENT)
-#define NULLBER	((BerElement *) 0)
+
 
 #define ber_pvt_ber_bytes(ber)		((ber)->ber_ptr - (ber)->ber_buf)
 #define ber_pvt_ber_remaining(ber)	((ber)->ber_end - (ber)->ber_ptr)
@@ -69,8 +78,8 @@ struct sockbuf_io {
 	int	(*sbi_setup)( struct sockbuf * sb, void *arg );
 	int	(*sbi_remove)( struct sockbuf *sb );
 	
-	long	(*sbi_read)( struct sockbuf *sb, void *buf, long len );
-	long	(*sbi_write)( struct sockbuf *sb, void *buf, long len );
+	ber_slen_t	(*sbi_read)( struct sockbuf *sb, void *buf, ber_len_t len );
+	ber_slen_t	(*sbi_write)( struct sockbuf *sb, void *buf, ber_len_t len );
 	int	(*sbi_close)( struct sockbuf *sb );
 };
 
@@ -85,9 +94,9 @@ struct sockbuf_sec {
 };
 
 struct sockbuf_buf {
-	long	buf_size;
-	long	buf_ptr;
-	long	buf_end;
+	ber_len_t	buf_size;
+	ber_len_t	buf_ptr;
+	ber_len_t	buf_end;
 	char	*buf_base;
 };
 
@@ -121,12 +130,9 @@ struct sockbuf {
    	void		*sb_sdata;	/* security-layer data pointer */
 	Sockbuf_Sec	*sb_sec;
 #endif	
-	
-#ifndef MACOS
-	int		sb_sd;
-#else /* MACOS */
-	void		*sb_sd;
-#endif /* MACOS */
+
+	ber_socket_t	sb_sd;
+
 #ifdef DEADWOOD
 	long		sb_max_incoming;
 #endif
@@ -134,7 +140,7 @@ struct sockbuf {
 #ifdef LDAP_SASL   
 	Sockbuf_Buf	sb_sec_buf_in;
 	Sockbuf_Buf	sb_sec_buf_out;
-	long		sb_sec_prev_len;
+	ber_len_t	sb_sec_prev_len;
 #endif   
 };
 #define SOCKBUF_VALID(ber)	((sb)->sb_valid==LBER_VALID_SOCKBUF)
@@ -143,7 +149,7 @@ struct sockbuf {
 #define	ber_pvt_sb_get_desc( sb ) ((sb)->sb_sd)
 #define ber_pvt_sb_set_desc( sb, val ) ((sb)->sb_sd =(val))
 
-#define ber_pvt_sb_in_use( sb ) ((sb)->sb_sd!=-1)
+#define ber_pvt_sb_in_use( sb ) ((sb)->sb_sd != AC_SOCKET_INVALID)
 
 #ifdef USE_SASL
 #define ber_pvt_sb_data_ready( sb ) \
@@ -157,13 +163,13 @@ struct sockbuf {
 
 struct seqorset {
 	BerElement	*sos_ber;
-	unsigned long	sos_clen;
-	unsigned long	sos_tag;
+	ber_len_t	sos_clen;
+	ber_tag_t	sos_tag;
 	char		*sos_first;
 	char		*sos_ptr;
 	struct seqorset	*sos_next;
 };
-#define NULLSEQORSET	((Seqorset *) 0)
+
 
 /*
  * bprint.c
@@ -175,7 +181,7 @@ ber_log_bprint LDAP_P((
 	int errlvl,
 	int loglvl,
 	const char *data,
-	int len ));
+	ber_len_t len ));
 
 LDAP_F( int )
 ber_log_dump LDAP_P((
@@ -242,11 +248,11 @@ ber_pvt_sb_set_nonblock LDAP_P(( Sockbuf *sb, int nb ));
 LDAP_F( int )
 ber_pvt_sb_set_readahead LDAP_P(( Sockbuf *sb, int rh ));
 
-LDAP_F( long )
-ber_pvt_sb_read LDAP_P(( Sockbuf *sb, void *buf, long len ));
+LDAP_F( ber_slen_t )
+ber_pvt_sb_read LDAP_P(( Sockbuf *sb, void *buf, ber_len_t len ));
 
-LDAP_F( long )
-ber_pvt_sb_write LDAP_P(( Sockbuf *sb, void *buf, long len ));
+LDAP_F( ber_slen_t )
+ber_pvt_sb_write LDAP_P(( Sockbuf *sb, void *buf, ber_len_t len ));
 
 LDAP_F(	int )
 ber_pvt_sb_udp_set_dst LDAP_P((Sockbuf *sb, void *addr ));
diff --git a/libraries/liblber/liblber.dsp b/libraries/liblber/liblber.dsp
index cd6997d68214ccd5680b9380fe28f816409eda32..02b855d3e3e3e926924d3a6c0bd05930526bb459 100644
--- a/libraries/liblber/liblber.dsp
+++ b/libraries/liblber/liblber.dsp
@@ -151,6 +151,10 @@ SOURCE=..\..\include\lber_pvt.h
 # End Source File
 # Begin Source File
 
+SOURCE=..\..\include\lber_types.h
+# End Source File
+# Begin Source File
+
 SOURCE=.\memory.c
 # End Source File
 # Begin Source File
diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c
index 7fedad324f6204507a8e0cbbb69ac195f3c6da55..cdc5549ae5405a11f4c90f57720256464dd5d44f 100644
--- a/libraries/liblber/memory.c
+++ b/libraries/liblber/memory.c
@@ -15,13 +15,17 @@
 #if LDAP_MEMORY_DEBUG
 struct ber_mem_hdr {
 	union bmu_align_u {
+		ber_len_t	bmu_len_t;
+		ber_tag_t	bmu_tag_t;
+		ber_int_t	bmu_int_t;
+
 		size_t	bmu_size_t;
 		void *	bmu_voidp;
 		double	bmu_double;
 		long	bmu_long;
 		char	bmu_char[4];
 	} ber_align;
-#define bm_junk	ber_align.bmu_size_t
+#define bm_junk	ber_align.bmu_len_t
 #define bm_data	ber_align.bmu_char[1]
 };
 #define BER_MEM_JUNK 0xddeeddeeU
@@ -104,7 +108,7 @@ ber_memvfree( void **vec )
 
 
 void *
-ber_memalloc( size_t s )
+ber_memalloc( ber_len_t s )
 {
     ber_int_options.lbo_valid = LBER_INITIALIZED;
 
@@ -138,7 +142,7 @@ ber_memalloc( size_t s )
 
 
 void *
-ber_memcalloc( size_t n, size_t s )
+ber_memcalloc( ber_len_t n, ber_len_t s )
 {
     ber_int_options.lbo_valid = LBER_INITIALIZED;
 
@@ -171,7 +175,7 @@ ber_memcalloc( size_t n, size_t s )
 
 
 void *
-ber_memrealloc( void* p, size_t s )
+ber_memrealloc( void* p, ber_len_t s )
 {
     ber_int_options.lbo_valid = LBER_INITIALIZED;
 
@@ -280,7 +284,7 @@ ber_bvdup(
 		return NULL;
 	}
 
-	SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
+	SAFEMEMCPY( new->bv_val, bv->bv_val, bv->bv_len );
 	new->bv_val[bv->bv_len] = '\0';
 	new->bv_len = bv->bv_len;
 
diff --git a/libraries/liblber/sockbuf.c b/libraries/liblber/sockbuf.c
index 78251b361a47f9e758089b96c1a3ea979550f7cf..37b796bc7206ff4228e093d6fa1ff6f3b4743682 100644
--- a/libraries/liblber/sockbuf.c
+++ b/libraries/liblber/sockbuf.c
@@ -45,8 +45,8 @@
 #define sockbuf_io_read( sb, buf, len ) \
 ((sb)->sb_io->sbi_read( (sb), (buf), (len) ))
 
-static long have_no_read( Sockbuf *sb, void *buf, long len );
-static long have_no_write( Sockbuf *sb, void *buf, long len );
+static ber_slen_t have_no_read( Sockbuf *sb, void *buf, ber_len_t len );
+static ber_slen_t have_no_write( Sockbuf *sb, void *buf, ber_len_t len );
 static int have_no_close( Sockbuf *sb );
 
 static Sockbuf_IO sb_IO_None=
@@ -103,17 +103,17 @@ status_is_ok( Sockbuf *sb )
 #endif
 
 #ifdef USE_SASL
-static long
+static ber_len_t
 packet_length( char *buf )
 {
-   long size;
+   ber_len_t size;
 
    assert( buf != NULL );
 
-   size = (((unsigned long)buf[0])<<24)|
-     (((unsigned long)buf[1])<<16)|
-     (((unsigned long)buf[2])<<8)|
-     (((unsigned long)buf[3]));
+   size = (((ber_len_t)buf[0])<<24)|
+     (((ber_len_t)buf[1])<<16)|
+     (((ber_len_t)buf[2])<<8)|
+     (((ber_len_t)buf[3]));
    
    if ((size<0) || (size>MAX_BUF_SIZE))	{
       /* somebody is trying to mess me up. */
@@ -128,9 +128,9 @@ packet_length( char *buf )
 #endif
 
 static int
-grow_buffer( Sockbuf_Buf * buf, long minsize )
+grow_buffer( Sockbuf_Buf * buf, ber_len_t minsize )
 {
-   long pw;;
+   ber_len_t pw;;
    
    assert( buf != NULL );
 
@@ -165,8 +165,8 @@ grow_buffer( Sockbuf_Buf * buf, long minsize )
 }
 
 #ifdef USE_SASL
-static long
-sockbuf_sec_release( Sockbuf *sb, char *buf, long len )
+static ber_slen_t
+sockbuf_sec_release( Sockbuf *sb, char *buf, ber_len_t len )
 {
    /* when this is called:
     *  sb->sb_sec_buf_in.buf_base  points to a packet.
@@ -304,10 +304,10 @@ sockbuf_sec_protect( Sockbuf *sb, char *buf, long len )
 }
 #endif
 
-static long 
-sockbuf_copy_out( Sockbuf *sb, char **buf, long len )
+static ber_len_t 
+sockbuf_copy_out( Sockbuf *sb, char **buf, ber_len_t len )
 {
-   long blen = (sb->sb_buf.buf_end - sb->sb_buf.buf_ptr );
+   ber_len_t blen = (sb->sb_buf.buf_end - sb->sb_buf.buf_ptr );
 
    assert( buf != NULL );
 
@@ -316,7 +316,7 @@ sockbuf_copy_out( Sockbuf *sb, char **buf, long len )
    assert( status_is_ok(sb) );
 
    if (blen) {
-      long rlen = (blen<len) ? blen : len;
+      ber_len_t rlen = (blen<len) ? blen : len;
       memcpy( *buf, sb->sb_buf.buf_base + sb->sb_buf.buf_ptr, rlen );
       sb->sb_buf.buf_ptr+=rlen;
       *buf+=rlen;
@@ -346,7 +346,7 @@ Sockbuf *ber_sockbuf_alloc( void )
 	return sb;
 }
 
-Sockbuf *ber_sockbuf_alloc_fd( int fd )
+Sockbuf *ber_sockbuf_alloc_fd( ber_socket_t fd )
 {
 	Sockbuf *sb = ber_sockbuf_alloc();
 
@@ -365,11 +365,11 @@ void ber_sockbuf_free( Sockbuf *sb )
 	LBER_FREE(sb);
 }
 
-long 
-ber_pvt_sb_read( Sockbuf *sb, void *buf_arg, long len )
+ber_slen_t 
+ber_pvt_sb_read( Sockbuf *sb, void *buf_arg, ber_len_t len )
 {
    char *buf;
-   long ret;
+   ber_slen_t ret;
    
    assert( buf_arg != NULL );
    assert( sb != NULL );
@@ -400,7 +400,7 @@ ber_pvt_sb_read( Sockbuf *sb, void *buf_arg, long len )
 
 #ifdef USE_SASL
    if (sb->sb_sec) {
-      int max;
+      ber_slen_t max;
       assert( sb->sb_sec->sbs_release );
       assert( sb->sb_sec_buf_in.buf_base );
       if (sb->sb_read_ahead) {
@@ -477,9 +477,9 @@ decode_packet:
    } else {
 #endif
       if (sb->sb_read_ahead) {
-	 long max;
+	 ber_slen_t max;
 	 max = sb->sb_buf.buf_size - sb->sb_buf.buf_end;
-	 if (max>len) {
+	 if (max> (ber_slen_t) len) {
 	    for(;;) {
 	       ret = sockbuf_io_read( sb, 
 				     sb->sb_buf.buf_base +
@@ -556,10 +556,10 @@ long sockbuf_do_write( Sockbuf *sb )
 }
 #endif
 
-long ber_pvt_sb_write( Sockbuf *sb, void *buf, long len_arg )
+ber_slen_t ber_pvt_sb_write( Sockbuf *sb, void *buf, ber_len_t len_arg )
 {
-   long ret;
-   long len = len_arg;
+   ber_slen_t ret;
+   ber_len_t len = len_arg;
 
 	assert( buf != NULL );
 	assert( sb != NULL );
@@ -849,8 +849,8 @@ int ber_pvt_sb_clear_io( Sockbuf *sb )
  * Support for TCP
  */
 
-static long
-stream_read( Sockbuf *sb, void *buf, long len )
+static ber_slen_t
+stream_read( Sockbuf *sb, void *buf, ber_len_t len )
 {
 	assert( sb != NULL);
 	assert( SOCKBUF_VALID( sb ) );
@@ -892,8 +892,8 @@ stream_read( Sockbuf *sb, void *buf, long len )
 #endif
 }
 
-static long
-stream_write( Sockbuf *sb, void *buf, long len )
+static ber_slen_t
+stream_write( Sockbuf *sb, void *buf, ber_len_t len )
 {
 	assert( sb != NULL);
 	assert( SOCKBUF_VALID( sb ) );
@@ -994,11 +994,11 @@ dgram_release( Sockbuf *sb )
    return 0;
 }
 
-static long
-dgram_read( Sockbuf *sb, void *buf, long len )
+static ber_slen_t
+dgram_read( Sockbuf *sb, void *buf, ber_len_t len )
 {
 #ifdef LDAP_CONNECTIONLESS
-   long rc;
+   ber_slen_t rc;
    int addrlen;
    struct dgram_data *dd;
    
@@ -1025,11 +1025,11 @@ dgram_read( Sockbuf *sb, void *buf, long len )
 # endif /* LDAP_CONNECTIONLESS */
 }
 
-static long 
-dgram_write( Sockbuf *sb, void *buf, long len )
+static ber_slen_t 
+dgram_write( Sockbuf *sb, void *buf, ber_len_t len )
 {
 #ifdef LDAP_CONNECTIONLESS
-   int rc;
+   ber_slen_t rc;
    struct dgram_data *dd;
    
 	assert( sb != NULL );
@@ -1106,8 +1106,8 @@ void *ber_pvt_sb_udp_get_src( Sockbuf *sb )
  * break the servers.
  */
 
-static long
-have_no_read( Sockbuf *sb, void *buf, long len )
+static ber_slen_t
+have_no_read( Sockbuf *sb, void *buf, ber_len_t len )
 {
 	assert( sb != NULL );
 	assert( SOCKBUF_VALID( sb ) );
@@ -1118,8 +1118,8 @@ have_no_read( Sockbuf *sb, void *buf, long len )
    return -1;
 }
 
-static long
-have_no_write( Sockbuf *sb, void *buf, long len )
+static ber_slen_t
+have_no_write( Sockbuf *sb, void *buf, ber_len_t len )
 {
 	assert( sb != NULL );
 	assert( SOCKBUF_VALID( sb ) );
diff --git a/libraries/libldap/abandon.c b/libraries/libldap/abandon.c
index 39ecfe157efdca25ff9ced6a4862d3be2ea47c46..04066e48824abec585268cf044208959c5ef0ba1 100644
--- a/libraries/libldap/abandon.c
+++ b/libraries/libldap/abandon.c
@@ -28,8 +28,8 @@
 
 static int do_abandon LDAP_P((
 	LDAP *ld,
-	int origid,
-	int msgid,
+	ber_int_t origid,
+	ber_int_t msgid,
 	LDAPControl **sctrls,
 	LDAPControl **cctrls));
 
@@ -84,14 +84,14 @@ ldap_abandon( LDAP *ld, int msgid )
 static int
 do_abandon(
 	LDAP *ld,
-	int origid,
-	int msgid,
+	ber_int_t origid,
+	ber_int_t msgid,
 	LDAPControl **sctrls,
 	LDAPControl **cctrls)
 {
 	BerElement	*ber;
 	int		i, err, sendabandon;
-	unsigned int *old_abandon;
+	ber_int_t *old_abandon;
 	Sockbuf		*sb;
 	LDAPRequest	*lr;
 
@@ -131,20 +131,20 @@ do_abandon(
 	err = 0;
 	if ( sendabandon ) {
 		/* create a message to send */
-		if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+		if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 			err = -1;
 			ld->ld_errno = LDAP_NO_MEMORY;
 
 		} else {
 #ifdef LDAP_CONNECTIONLESS
 			if ( ld->ld_cldapnaddr > 0 ) {
-				err = ber_printf( ber, "{isti", /* leave open '}' */
+				err = ber_printf( ber, "{isti", /* '}' */
 				    ++ld->ld_msgid, ld->ld_cldapdn,
 				    LDAP_REQ_ABANDON, msgid );
 			} else
 #endif /* LDAP_CONNECTIONLESS */
 			{
-				err = ber_printf( ber, "{iti",  /* leave open '}' */
+				err = ber_printf( ber, "{iti",  /* '}' */
 					++ld->ld_msgid,
 				    LDAP_REQ_ABANDON, msgid );
 			}
@@ -162,7 +162,7 @@ do_abandon(
 
 				} else {
 					/* close '{' */
-					err = ber_printf( ber, "}" );
+					err = ber_printf( ber, /*{*/ "}" );
 
 					if( err == -1 ) {
 						/* encoding error */
@@ -209,8 +209,8 @@ do_abandon(
 
 	old_abandon = ld->ld_abandoned;
 
-	ld->ld_abandoned = (int *) LDAP_REALLOC( (char *)
-		ld->ld_abandoned, (i + 2) * sizeof(int) );
+	ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
+		ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
 		
 	if ( ld->ld_abandoned == NULL ) {
 		ld->ld_abandoned = old_abandon;
diff --git a/libraries/libldap/add.c b/libraries/libldap/add.c
index d945988a390d07881b641ed7bdbd64a9e31708ff..748803de33a941174cf29c6c4ff67e67c752939d 100644
--- a/libraries/libldap/add.c
+++ b/libraries/libldap/add.c
@@ -105,12 +105,12 @@ ldap_add_ext(
 	assert( msgidp != NULL );
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		ld->ld_errno = LDAP_NO_MEMORY;
 		return ld->ld_errno;
 	}
 
-	rc = ber_printf( ber, "{it{s{", /* leave open '}}}' */
+	rc = ber_printf( ber, "{it{s{", /* '}}}' */
 		++ld->ld_msgid, LDAP_REQ_ADD, dn );
 
 	if ( rc == -1 ) {
@@ -135,8 +135,7 @@ ldap_add_ext(
 		}
 	}
 
-	/* close '{{' */
-	if ( ber_printf( ber, "}}" ) == -1 ) {
+	if ( ber_printf( ber, /*{{*/ "}}" ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
 		return ld->ld_errno;
@@ -148,8 +147,7 @@ ldap_add_ext(
 		return ld->ld_errno;
 	}
 
-	/* close '{' */
-	if ( ber_printf( ber, "}" ) == -1 ) {
+	if ( ber_printf( ber, /*{*/ "}" ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
 		return ld->ld_errno;
diff --git a/libraries/libldap/cache.c b/libraries/libldap/cache.c
index 4a0984b18fcce6a792c17463eebbe55a87201ebd..020a1b81f4886d864ba4a824fb619ce0aab84773 100644
--- a/libraries/libldap/cache.c
+++ b/libraries/libldap/cache.c
@@ -27,19 +27,19 @@ static int		cache_hash LDAP_P(( BerElement *ber ));
 static LDAPMessage	*msg_dup LDAP_P(( LDAPMessage *msg ));
 static int		request_cmp LDAP_P(( BerElement	*req1, BerElement *req2 ));
 static int		chain_contains_dn LDAP_P(( LDAPMessage *msg, LDAP_CONST char *dn ));
-static long		msg_size LDAP_P(( LDAPMessage *msg ));
+static ber_len_t	msg_size LDAP_P(( LDAPMessage *msg ));
 static void		check_cache_memused LDAP_P(( LDAPCache *lc ));
-static void		uncache_entry_or_req LDAP_P(( LDAP *ld, LDAP_CONST char *dn, int msgid ));
+static void		uncache_entry_or_req LDAP_P(( LDAP *ld, LDAP_CONST char *dn, ber_int_t msgid ));
 
 #endif
 
 int
-ldap_enable_cache( LDAP *ld, long timeout, long maxmem )
+ldap_enable_cache( LDAP *ld, long timeout, ber_len_t maxmem )
 {
 #ifndef LDAP_NOCACHE
-	if ( ld->ld_cache == NULLLDCACHE ) {
+	if ( ld->ld_cache == NULL ) {
 		if (( ld->ld_cache = (LDAPCache *)LDAP_MALLOC( sizeof( LDAPCache )))
-		    == NULLLDCACHE ) {
+		    == NULL ) {
 			ld->ld_errno = LDAP_NO_MEMORY;
 			return( -1 );
 		}
@@ -62,7 +62,7 @@ void
 ldap_disable_cache( LDAP *ld )
 {
 #ifndef LDAP_NOCACHE
-	if ( ld->ld_cache != NULLLDCACHE ) {
+	if ( ld->ld_cache != NULL ) {
 		ld->ld_cache->lc_enabled = 0;
 	}
 #endif
@@ -74,7 +74,7 @@ void
 ldap_set_cache_options( LDAP *ld, unsigned long opts )
 {
 #ifndef LDAP_NOCACHE
-	if ( ld->ld_cache != NULLLDCACHE ) {
+	if ( ld->ld_cache != NULL ) {
 		ld->ld_cache->lc_options = opts;
 	}
 #endif
@@ -85,10 +85,10 @@ void
 ldap_destroy_cache( LDAP *ld )
 {
 #ifndef LDAP_NOCACHE
-	if ( ld->ld_cache != NULLLDCACHE ) {
+	if ( ld->ld_cache != NULL ) {
 		ldap_flush_cache( ld );
 		LDAP_FREE( (char *)ld->ld_cache );
-		ld->ld_cache = NULLLDCACHE;
+		ld->ld_cache = NULL;
 	}
 #endif
 }
@@ -103,22 +103,22 @@ ldap_flush_cache( LDAP *ld )
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_flush_cache\n", 0, 0, 0 );
 
-	if ( ld->ld_cache != NULLLDCACHE ) {
+	if ( ld->ld_cache != NULL ) {
 		/* delete all requests in the queue */
-		for ( m = ld->ld_cache->lc_requests; m != NULLMSG; m = next ) {
+		for ( m = ld->ld_cache->lc_requests; m != NULL; m = next ) {
 			next = m->lm_next;
 			ldap_msgfree( m );
 		}
-		ld->ld_cache->lc_requests = NULLMSG;
+		ld->ld_cache->lc_requests = NULL;
 
 		/* delete all messages in the cache */
 		for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
 			for ( m = ld->ld_cache->lc_buckets[ i ];
-			    m != NULLMSG; m = next ) {
+			    m != NULL; m = next ) {
 				next = m->lm_next;
 				ldap_msgfree( m );
 			}
-			ld->ld_cache->lc_buckets[ i ] = NULLMSG;
+			ld->ld_cache->lc_buckets[ i ] = NULL;
 		}
 		ld->ld_cache->lc_memused = sizeof( LDAPCache );
 	}
@@ -155,26 +155,26 @@ ldap_uncache_entry( LDAP *ld, LDAP_CONST char *dn )
 static void
 uncache_entry_or_req( LDAP *ld,
 	const char *dn,		/* if non-NULL, uncache entry */
-	int msgid )		/* request to uncache (if dn == NULL) */
+	ber_int_t msgid )		/* request to uncache (if dn == NULL) */
 {
 	int		i;
 	LDAPMessage	*m, *prev, *next;
 
 	Debug( LDAP_DEBUG_TRACE,
-	    "ldap_uncache_entry_or_req  dn %s  msgid %d  ld_cache %lx\n",
-	    dn, msgid, (long) ld->ld_cache );
+	    "ldap_uncache_entry_or_req  dn %s  msgid %ld  ld_cache %lx\n",
+	    dn, (long) msgid, (long) ld->ld_cache );
 
-	if ( ld->ld_cache == NULLLDCACHE ) {
+	if ( ld->ld_cache == NULL ) {
 	    return;
 	}
 
 	/* first check the request queue */
-	prev = NULLMSG;
-	for ( m = ld->ld_cache->lc_requests; m != NULLMSG; m = next ) {
+	prev = NULL;
+	for ( m = ld->ld_cache->lc_requests; m != NULL; m = next ) {
 		next = m->lm_next;
 		if (( dn != NULL && chain_contains_dn( m, dn )) ||
 			( dn == NULL && m->lm_msgid == msgid )) {
-			if ( prev == NULLMSG ) {
+			if ( prev == NULL ) {
 				ld->ld_cache->lc_requests = next;
 			} else {
 				prev->lm_next = next;
@@ -188,13 +188,13 @@ uncache_entry_or_req( LDAP *ld,
 
 	/* now check the rest of the cache */
 	for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
-		prev = NULLMSG;
-		for ( m = ld->ld_cache->lc_buckets[ i ]; m != NULLMSG;
+		prev = NULL;
+		for ( m = ld->ld_cache->lc_buckets[ i ]; m != NULL;
 		    m = next ) {
 			next = m->lm_next;
 			if (( dn != NULL && chain_contains_dn( m, dn )) ||
 				( dn == NULL && m->lm_msgid == msgid )) {
-				if ( prev == NULLMSG ) {
+				if ( prev == NULL ) {
 					ld->ld_cache->lc_buckets[ i ] = next;
 				} else {
 					prev->lm_next = next;
@@ -211,23 +211,23 @@ uncache_entry_or_req( LDAP *ld,
 #endif
 
 void
-ldap_add_request_to_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
+ldap_add_request_to_cache( LDAP *ld, ber_tag_t msgtype, BerElement *request )
 {
 #ifndef LDAP_NOCACHE
 	LDAPMessage	*new;
-	long		len;
+	ber_len_t	len;
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_add_request_to_cache\n", 0, 0, 0 );
 
 	ld->ld_errno = LDAP_SUCCESS;
-	if ( ld->ld_cache == NULLLDCACHE ||
+	if ( ld->ld_cache == NULL ||
 	    ( ld->ld_cache->lc_enabled == 0 )) {
 		return;
 	}
 
 	if (( new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
 	    != NULL ) {
-		if (( new->lm_ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+		if (( new->lm_ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 			LDAP_FREE( (char *)new );
 			return;
 		}
@@ -264,7 +264,7 @@ ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
 	Debug( LDAP_DEBUG_TRACE, "ldap_add_result_to_cache: id %d, type %d\n", 
 		result->lm_msgid, result->lm_msgtype, 0 );
 
-	if ( ld->ld_cache == NULLLDCACHE ||
+	if ( ld->ld_cache == NULL ||
 	    ( ld->ld_cache->lc_enabled == 0 )) {
 		Debug( LDAP_DEBUG_TRACE, "artc: cache disabled\n", 0, 0, 0 );
 		return;
@@ -287,7 +287,7 @@ ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
 	 * result to it.  if this result completes the results for the
 	 * request, add the request/result chain to the cache proper.
 	 */
-	prev = NULLMSG;
+	prev = NULL;
 	for ( m = ld->ld_cache->lc_requests; m != NULL; m = m->lm_next ) {
 		if ( m->lm_msgid == result->lm_msgid ) {
 			break;
@@ -295,12 +295,12 @@ ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
 		prev = m;
 	}
 
-	if ( m != NULLMSG ) {	/* found request; add to end of chain */
+	if ( m != NULL ) {	/* found request; add to end of chain */
 		req = m;
-		for ( ; m->lm_chain != NULLMSG; m = m->lm_chain )
+		for ( ; m->lm_chain != NULL; m = m->lm_chain )
 			;
-		if (( new = msg_dup( result )) != NULLMSG ) {
-			new->lm_chain = NULLMSG;
+		if (( new = msg_dup( result )) != NULL ) {
+			new->lm_chain = NULL;
 			m->lm_chain = new;
 			Debug( LDAP_DEBUG_TRACE,
 			    "artc: result added to cache request chain\n",
@@ -331,7 +331,7 @@ ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
 				keep = 1;
 			}
 
-			if ( prev == NULLMSG ) {
+			if ( prev == NULL ) {
 				ld->ld_cache->lc_requests = req->lm_next;
 			} else {
 				prev->lm_next = req->lm_next;
@@ -371,7 +371,7 @@ ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
  * will find them.
  */
 int
-ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
+ldap_check_cache( LDAP *ld, ber_tag_t msgtype, BerElement *request )
 {
 #ifndef LDAP_NOCACHE
 	LDAPMessage	*m, *new, *prev, *next;
@@ -381,7 +381,7 @@ ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_check_cache\n", 0, 0, 0 );
 
-	if ( ld->ld_cache == NULLLDCACHE ||
+	if ( ld->ld_cache == NULL ||
 	    ( ld->ld_cache->lc_enabled == 0 )) {
 		return( -1 );
 	}
@@ -391,9 +391,9 @@ ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
 
 	c_time = time( NULL );
 
-	prev = NULLMSG;
+	prev = NULL;
 	hash = cache_hash( &reqber );
-	for ( m = ld->ld_cache->lc_buckets[ hash ]; m != NULLMSG; m = next ) {
+	for ( m = ld->ld_cache->lc_buckets[ hash ]; m != NULL; m = next ) {
 		Debug( LDAP_DEBUG_TRACE,"cc: examining id %d,type %d\n",
 		    m->lm_msgid, m->lm_msgtype, 0 );
 		if ( difftime(c_time, m->lm_time) > ld->ld_cache->lc_timeout ) {
@@ -418,7 +418,7 @@ ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
 		}
 	}
 
-	if ( m == NULLMSG ) {
+	if ( m == NULL ) {
 		return( -1 );
 	}
 
@@ -426,13 +426,13 @@ ldap_check_cache( LDAP *ld, unsigned long msgtype, BerElement *request )
 	 * add duplicates of responses to incoming queue
 	 */
 	first = 1;
-	for ( m = m->lm_chain; m != NULLMSG; m = m->lm_chain ) {
-		if (( new = msg_dup( m )) == NULLMSG ) {
+	for ( m = m->lm_chain; m != NULL; m = m->lm_chain ) {
+		if (( new = msg_dup( m )) == NULL ) {
 			return( -1 );
 		}
 
 		new->lm_msgid = ld->ld_msgid;
-		new->lm_chain = NULLMSG;
+		new->lm_chain = NULL;
 		if ( first ) {
 			new->lm_next = ld->ld_responses;
 			ld->ld_responses = new;
@@ -458,7 +458,7 @@ static int
 cache_hash( BerElement *ber )
 {
 	BerElement	bercpy;
-	unsigned long	len;
+	ber_len_t	len;
 
 	/*
          * just take the length of the packet and mod with # of buckets
@@ -481,20 +481,20 @@ static LDAPMessage *
 msg_dup( LDAPMessage *msg )
 {
 	LDAPMessage	*new;
-	long		len;
+	ber_len_t	len;
 
 	if (( new = (LDAPMessage *)LDAP_MALLOC( sizeof(LDAPMessage))) != NULL ) {
 		*new = *msg;	/* struct copy */
-		if (( new->lm_ber = ber_dup( msg->lm_ber )) == NULLBER ) {
+		if (( new->lm_ber = ber_dup( msg->lm_ber )) == NULL ) {
 			LDAP_FREE( (char *)new );
-			return( NULLMSG );
+			return( NULL );
 		}
 		len = msg->lm_ber->ber_end - msg->lm_ber->ber_buf;
 		if (( new->lm_ber->ber_buf = (char *) ber_memalloc(
 		    (size_t)len )) == NULL ) {
 			ber_free( new->lm_ber, 0 );
 			LDAP_FREE( (char *)new );
-			return( NULLMSG );
+			return( NULL );
 		}
 		SAFEMEMCPY( new->lm_ber->ber_buf, msg->lm_ber->ber_buf,
 		    (size_t)len );
@@ -510,7 +510,7 @@ msg_dup( LDAPMessage *msg )
 static int
 request_cmp( BerElement *req1, BerElement *req2 )
 {
-	unsigned long	len;
+	ber_len_t	len;
 	BerElement	r1, r2;
 
 	r1 = *req1;	/* struct copies */
@@ -532,7 +532,7 @@ request_cmp( BerElement *req1, BerElement *req2 )
 	 * check remaining length and bytes if necessary
 	 */
 	if (( len = r1.ber_end - r1.ber_ptr ) !=
-		(unsigned long) (r2.ber_end - r2.ber_ptr) )
+		(ber_len_t) (r2.ber_end - r2.ber_ptr) )
 	{
 		return( -1 );	/* different lengths */
 	}
@@ -545,7 +545,7 @@ chain_contains_dn( LDAPMessage *msg, const char *dn )
 {
 	LDAPMessage	*m;
 	BerElement	ber;
-	long		msgid;
+	ber_int_t		msgid;
 	char		*s;
 	int		rc;
 
@@ -554,7 +554,7 @@ chain_contains_dn( LDAPMessage *msg, const char *dn )
 	 * first check the base or dn of the request
 	 */
 	ber = *msg->lm_ber;	/* struct copy */
-	if ( ber_scanf( &ber, "{i{a", &msgid, &s ) != LBER_ERROR ) {
+	if ( ber_scanf( &ber, "{i{a" /*}}*/, &msgid, &s ) != LBER_ERROR ) {
 	    rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
 	    LDAP_FREE( s );
 	    if ( rc != 0 ) {
@@ -570,12 +570,12 @@ chain_contains_dn( LDAPMessage *msg, const char *dn )
 	 * now check the dn of each search result
 	 */
 	rc = 0;
-	for ( m = msg->lm_chain; m != NULLMSG && rc == 0 ; m = m->lm_chain ) {
+	for ( m = msg->lm_chain; m != NULL && rc == 0 ; m = m->lm_chain ) {
 		if ( m->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
 			continue;
 		}
 		ber = *m->lm_ber;	/* struct copy */
-		if ( ber_scanf( &ber, "{a", &s ) != LBER_ERROR ) {
+		if ( ber_scanf( &ber, "{a" /*}*/, &s ) != LBER_ERROR ) {
 			rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
 			LDAP_FREE( s );
 		}
@@ -585,14 +585,14 @@ chain_contains_dn( LDAPMessage *msg, const char *dn )
 }
 
 
-static long
+static ber_len_t
 msg_size( LDAPMessage *msg )
 {
 	LDAPMessage	*m;
-	long		size;
+	ber_len_t	size;
 
 	size = 0;
-	for ( m = msg; m != NULLMSG; m = m->lm_chain ) {
+	for ( m = msg; m != NULL; m = m->lm_chain ) {
 		size += ( sizeof( LDAPMessage ) + m->lm_ber->ber_end -
 		    m->lm_ber->ber_buf );
 	}
@@ -634,12 +634,12 @@ check_cache_memused( LDAPCache *lc )
 	while ( lc->lc_memused > lc->lc_maxmem * SIZE_FACTOR ) {
 		c_time = time( NULL );
 		for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
-			prev = NULLMSG;
-			for ( m = lc->lc_buckets[ i ]; m != NULLMSG;
+			prev = NULL;
+			for ( m = lc->lc_buckets[ i ]; m != NULL;
 			    m = next ) {
 				next = m->lm_next;
 				if ( difftime(c_time, m->lm_time) > remove_threshold) {
-					if ( prev == NULLMSG ) {
+					if ( prev == NULL ) {
 						lc->lc_buckets[ i ] = next;
 					} else {
 						prev->lm_next = next;
diff --git a/libraries/libldap/charset.c b/libraries/libldap/charset.c
index 8144e0335b8b13bbd88942a6bf1bd71bfc0bf908..75c30ff9d96f007796ffdc5306e0cc5a323d0eb9 100644
--- a/libraries/libldap/charset.c
+++ b/libraries/libldap/charset.c
@@ -54,7 +54,7 @@ ldap_enable_translation( LDAP *ld, LDAPMessage *entry, int enable )
 
 
 int
-ldap_translate_from_t61( LDAP *ld, char **bufp, unsigned long *lenp,
+ldap_translate_from_t61( LDAP *ld, char **bufp, ber_len_t *lenp,
     int free_input )
 {
 	if ( ld->ld_lber_decode_translate_proc == 0 ) {
@@ -66,7 +66,7 @@ ldap_translate_from_t61( LDAP *ld, char **bufp, unsigned long *lenp,
 
 
 int
-ldap_translate_to_t61( LDAP *ld, char **bufp, unsigned long *lenp,
+ldap_translate_to_t61( LDAP *ld, char **bufp, ber_len_t *lenp,
     int free_input )
 {
 	if ( ld->ld_lber_encode_translate_proc == 0 ) {
@@ -1022,12 +1022,12 @@ c_to_cc( Byte *o, const Couple *cc, Byte c )
 /* --- routine to convert from T.61 to ISO 8859-n --- */
 
 int
-ldap_t61_to_8859( char **bufp, unsigned long *buflenp, int free_input )
+ldap_t61_to_8859( char **bufp, ber_len_t *buflenp, int free_input )
 {
   Byte		*s, *oo, *o;
   unsigned int  n;
   int           c;
-  unsigned long len;
+  ber_len_t len;
 
   Debug( LDAP_DEBUG_TRACE, "ldap_t61_to_8859 input length: %ld\n",
 	*buflenp, 0, 0 );
@@ -1559,11 +1559,11 @@ cc_to_t61( Byte *o, const Byte *s )
 /* --- routine to convert from ISO 8859-n to T.61 --- */
 
 int
-ldap_8859_to_t61( char **bufp, unsigned long *buflenp, int free_input )
+ldap_8859_to_t61( char **bufp, ber_len_t *buflenp, int free_input )
 {
   Byte		*s, *oo, *o, *aux;
   int		c;
-  unsigned long len; 
+  ber_len_t len; 
   const Couple	*cc;
 
   Debug( LDAP_DEBUG_TRACE, "ldap_8859_to_t61 input length: %ld\n",
diff --git a/libraries/libldap/cldap.c b/libraries/libldap/cldap.c
index ec7ea7ee81dac6b4d4b172d88c6d0a2407ec743b..58a6abc9041d6d08b13c672f254e979bc6df80f9 100644
--- a/libraries/libldap/cldap.c
+++ b/libraries/libldap/cldap.c
@@ -279,7 +279,8 @@ cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
     Sockbuf 		*sb = &ld->ld_sb;
     BerElement		ber;
     char		*logdn;
-    int			ret, id, fromaddr, i;
+    int			ret, fromaddr, i;
+	ber_int_t	id;
     struct timeval	tv;
 
     fromaddr = -1;
@@ -406,8 +407,10 @@ static int
 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
 	LDAPMessage **res, char *base )
 {
-    unsigned long	tag, len;
-    int			baselen, slen, rc;
+    ber_tag_t	tag;
+	ber_len_t	len;
+    int			baselen, slen;
+	ber_tag_t	rc;
     char		*dn, *p, *cookie;
     LDAPMessage		*chain, *prev, *ldm;
     struct berval	*bv;
@@ -422,7 +425,7 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
 	    tag = ber_next_element( ber, &len, cookie )) {
 	if (( ldm = (LDAPMessage *)LDAP_CALLOC( 1, sizeof(LDAPMessage)))
 		== NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
-		== NULLBER ) {
+		== NULL ) {
 	    rc = LDAP_NO_MEMORY;
 	    break;	/* return w/error*/
 	}
@@ -437,8 +440,7 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
 		break;	/* return w/error */
 	    }
 
-	    if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
-		    bv->bv_len ) == -1 ) {
+	    if ( ber_printf( ldm->lm_ber, "tO", tag, bv ) == -1 ) {
 		break;	/* return w/error */
 	    }
 	    ber_bvfree( bv );
@@ -446,7 +448,7 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
 	    rc = LDAP_SUCCESS;
 
 	} else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
-	    if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
+	    if ( ber_scanf( ber, "{aO" /*}*/, &dn, &bv ) == LBER_ERROR ) {
 		break;	/* return w/error */
 	    }
 	    Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
@@ -513,7 +515,7 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
 
     /* dispose of any leftovers */
     if ( ldm != NULL ) {
-	if ( ldm->lm_ber != NULLBER ) {
+	if ( ldm->lm_ber != NULL ) {
 	    ber_free( ldm->lm_ber, 1 );
 	}
 	LDAP_FREE( ldm );
diff --git a/libraries/libldap/compare.c b/libraries/libldap/compare.c
index 21a99aa80d3a1ec26dda6af0d06b7e306133ec0c..767581a6efbbb5a083d61d004f03e61bbbe91164 100644
--- a/libraries/libldap/compare.c
+++ b/libraries/libldap/compare.c
@@ -61,11 +61,11 @@ ldap_compare_ext(
 	assert( msgidp != NULL );
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		return( LDAP_NO_MEMORY );
 	}
 
-	if ( ber_printf( ber, "{it{s{sO}}", /* leave open '}' */
+	if ( ber_printf( ber, "{it{s{sO}}", /* '}' */
 		++ld->ld_msgid,
 		LDAP_REQ_COMPARE, dn, attr, &bvalue ) == -1 )
 	{
@@ -80,7 +80,7 @@ ldap_compare_ext(
 		return ld->ld_errno;
 	}
 
-	if( ber_printf( ber, "}" ) == -1 ) {
+	if( ber_printf( ber, /*{*/ "}" ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
 		return( ld->ld_errno );
diff --git a/libraries/libldap/controls.c b/libraries/libldap/controls.c
index 7c6249fb6cc2bde71db9088b63d4110eaf049e7c..b609b7bc5660cd0c180fe2df01663248bda74c69 100644
--- a/libraries/libldap/controls.c
+++ b/libraries/libldap/controls.c
@@ -62,13 +62,13 @@ int ldap_int_put_controls(
 	}
 
 	/* Controls are encoded as a sequence of sequences */
-	if( ber_printf( ber, "t{", LDAP_TAG_CONTROLS ) == -1 ) {
+	if( ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		return ld->ld_errno;
 	}
 
 	for( c = ctrls ; *c != NULL; c++ ) {
-		if ( ber_printf( ber, "{s",
+		if ( ber_printf( ber, "{s" /*}*/,
 			(*c)->ldctl_oid ) == -1 )
 		{
 			ld->ld_errno = LDAP_ENCODING_ERROR;
@@ -77,7 +77,7 @@ int ldap_int_put_controls(
 
 		if( (*c)->ldctl_iscritical /* only if true */
 			&&  ( ber_printf( ber, "b",
-				(*c)->ldctl_iscritical ) == -1 ) )
+				(ber_int_t) (*c)->ldctl_iscritical ) == -1 ) )
 		{
 			ld->ld_errno = LDAP_ENCODING_ERROR;
 			return ld->ld_errno;
@@ -92,14 +92,14 @@ int ldap_int_put_controls(
 		}
 
 
-		if( ber_printf( ber, "}" ) == -1 ) {
+		if( ber_printf( ber, /*{*/"}" ) == -1 ) {
 			ld->ld_errno = LDAP_ENCODING_ERROR;
 			return ld->ld_errno;
 		}
 	}
 
 
-	if( ber_printf( ber, "}" ) == -1 ) {
+	if( ber_printf( ber, /*{*/"}" ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		return ld->ld_errno;
 	}
@@ -112,7 +112,8 @@ int ldap_int_get_controls(
 	LDAPControl ***ctrls )
 {
 	int nctrls;
-	unsigned long tag, len;
+	ber_tag_t tag;
+	ber_len_t len;
 	char *opaque;
 
 	assert( ber != NULL );
@@ -179,14 +180,16 @@ int ldap_int_get_controls(
 		tctrls[nctrls++] = tctrl;
 		tctrls[nctrls] = NULL;
 
-		tag = ber_scanf( ber, "{a", &tctrl->ldctl_oid );
+		tag = ber_scanf( ber, "{a" /*}*/, &tctrl->ldctl_oid );
 
 		if( tag != LBER_ERROR ) {
 			tag = ber_peek_tag( ber, &len );
 		}
 
 		if( tag == LBER_BOOLEAN ) {
-			tag = ber_scanf( ber, "b", &tctrl->ldctl_iscritical );
+			ber_int_t crit;
+			tag = ber_scanf( ber, "b", &crit );
+			tctrl->ldctl_iscritical = crit ? (char) 0 : (char) ~0;
 		}
 
 		if( tag != LBER_ERROR ) {
@@ -255,7 +258,7 @@ ldap_controls_free( LDAPControl **controls )
 /*
  * Duplicate an array of LDAPControl
  */
-LDAPControl **ldap_controls_dup( const LDAPControl **controls )
+LDAPControl **ldap_controls_dup( LDAPControl **controls )
 {
 	LDAPControl **new;
 	int i;
@@ -297,7 +300,7 @@ LDAPControl **ldap_controls_dup( const LDAPControl **controls )
 /*
  * Duplicate a LDAPControl
  */
-LDAPControl *ldap_control_dup( const LDAPControl *c )
+LDAPControl *ldap_control_dup( LDAPControl *c )
 {
 	LDAPControl *new;
 
diff --git a/libraries/libldap/delete.c b/libraries/libldap/delete.c
index b5602db2eb943abefd7dea3336e3a659b2cd572a..0413c235cf977d4c094bacf230acf13a35cddd7a 100644
--- a/libraries/libldap/delete.c
+++ b/libraries/libldap/delete.c
@@ -54,12 +54,12 @@ ldap_delete_ext(
 	assert( msgidp != NULL );
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		ld->ld_errno = LDAP_NO_MEMORY;
 		return( ld->ld_errno );
 	}
 
-	if ( ber_printf( ber, "{its", /* leave open - '}' */
+	if ( ber_printf( ber, "{its", /* '}' */
 		++ld->ld_msgid, LDAP_REQ_DELETE, dn ) == -1 )
 	{
 		ld->ld_errno = LDAP_ENCODING_ERROR;
@@ -73,8 +73,7 @@ ldap_delete_ext(
 		return ld->ld_errno;
 	}
 
-	/* close '{' */
-	if ( ber_printf( ber, "}" ) == -1 ) {
+	if ( ber_printf( ber, /*{*/ "}" ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
 		return( ld->ld_errno );
diff --git a/libraries/libldap/disptmpl.c b/libraries/libldap/disptmpl.c
index 34a18a80bf90c41135aefe74ca4928aaa17130ee..32c253d32dc0699290e10ba5c9e709f37cc84775 100644
--- a/libraries/libldap/disptmpl.c
+++ b/libraries/libldap/disptmpl.c
@@ -36,7 +36,7 @@
 #include "disptmpl.h"
 
 static void free_disptmpl LDAP_P(( struct ldap_disptmpl *tmpl ));
-static int read_next_tmpl LDAP_P(( char **bufp, long *blenp,
+static int read_next_tmpl LDAP_P(( char **bufp, ber_len_t *blenp,
 	struct ldap_disptmpl **tmplp, int dtversion ));
 
 static const char *const	tmploptions[] = {
@@ -139,7 +139,7 @@ ldap_init_templates( char *file, struct ldap_disptmpl **tmpllistp )
 
 
 int
-ldap_init_templates_buf( char *buf, long buflen,
+ldap_init_templates_buf( char *buf, ber_len_t buflen,
 	struct ldap_disptmpl **tmpllistp )
 {
     int				rc=-1, version;
@@ -441,7 +441,7 @@ ldap_tmplattrs( struct ldap_disptmpl *tmpl, char **includeattrs,
 
 
 static int
-read_next_tmpl( char **bufp, long *blenp, struct ldap_disptmpl **tmplp,
+read_next_tmpl( char **bufp, ber_len_t *blenp, struct ldap_disptmpl **tmplp,
 	int dtversion )
 {
     int				i, j, tokcnt, samerow, adsource;
diff --git a/libraries/libldap/dsparse.c b/libraries/libldap/dsparse.c
index 19446e0ee4f0ac059654dc525f3bfd08995b7dba..2d7a7e02191464b0a00f69e9f01c76f88f56cb72 100644
--- a/libraries/libldap/dsparse.c
+++ b/libraries/libldap/dsparse.c
@@ -34,13 +34,13 @@
 
 #include "ldap-int.h"
 
-static int next_line LDAP_P(( char **bufp, long *blenp, char **linep ));
+static int next_line LDAP_P(( char **bufp, ber_len_t *blenp, char **linep ));
 static char *next_token LDAP_P(( char ** sp ));
 
 
 
 int
-next_line_tokens( char **bufp, long *blenp, char ***toksp )
+next_line_tokens( char **bufp, ber_len_t *blenp, char ***toksp )
 {
     char	*p, *line, *token, **toks;
     int		rc, tokcnt;
@@ -90,10 +90,10 @@ next_line_tokens( char **bufp, long *blenp, char ***toksp )
 
 
 static int
-next_line( char **bufp, long *blenp, char **linep )
+next_line( char **bufp, ber_len_t *blenp, char **linep )
 {
     char	*linestart, *line, *p;
-    long	plen;
+    ber_slen_t	plen;
 
     linestart = *bufp;
     p = *bufp;
diff --git a/libraries/libldap/error.c b/libraries/libldap/error.c
index 41147c7609ffd3f8f16d64f81fcb44afc2726700..41570b64777439757a0c98119504111091580d3f 100644
--- a/libraries/libldap/error.c
+++ b/libraries/libldap/error.c
@@ -163,8 +163,8 @@ ldap_result2error( LDAP *ld, LDAPMessage *r, int freeit )
 {
 	LDAPMessage	*lm;
 	BerElement	ber;
-	long		along;
-	unsigned long	rc;
+	ber_int_t	along;
+	ber_tag_t	rc;
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_result2error\n", 0, 0, 0 );
 
@@ -172,7 +172,7 @@ ldap_result2error( LDAP *ld, LDAPMessage *r, int freeit )
 	assert( LDAP_VALID( ld ) );
 	assert( r != NULL );
 
-	if ( ld == NULL || r == NULLMSG )
+	if ( ld == NULL || r == NULL )
 		return( LDAP_PARAM_ERROR );
 
 	for ( lm = r; lm->lm_chain != NULL; lm = lm->lm_chain )
diff --git a/libraries/libldap/extended.c b/libraries/libldap/extended.c
index 31b4dd146c4337bc81d03899f48ee48d0bd7c63e..e92d4d25783b12cd8339298b29c5d0ded2a46265 100644
--- a/libraries/libldap/extended.c
+++ b/libraries/libldap/extended.c
@@ -60,12 +60,12 @@ ldap_extended_operation(
 	}
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		ld->ld_errno = LDAP_NO_MEMORY;
 		return( ld->ld_errno );
 	}
 
-	if ( ber_printf( ber, "{it{tstO}", /* leave '}' for later */
+	if ( ber_printf( ber, "{it{tstO}", /* '}' */
 		++ld->ld_msgid, LDAP_REQ_EXTENDED, LDAP_TAG_EXOP_REQ_OID,
 			reqoid, LDAP_TAG_EXOP_REQ_VALUE, reqdata ) == -1 )
 	{
@@ -152,10 +152,11 @@ ldap_parse_extended_result (
 	int				freeit )
 {
 	BerElement *ber;
-	int rc;
-	unsigned long tag, len;
+	ber_tag_t rc;
+	ber_tag_t tag;
+	ber_len_t len;
 	struct berval *resdata;
-	long errcode;
+	ber_int_t errcode;
 	char *resoid;
 
 	assert( ld != NULL );
@@ -184,7 +185,7 @@ ldap_parse_extended_result (
 		ld->ld_matched = NULL;
 	}
 
-	rc = ber_scanf( ber, "{iaa", &errcode,
+	rc = ber_scanf( ber, "{iaa" /*}*/, &errcode,
 		&ld->ld_matched, &ld->ld_matched );
 
 	if( rc == LBER_ERROR ) {
diff --git a/libraries/libldap/free.c b/libraries/libldap/free.c
index f9569dd04a17d15f0ed6c4c89e412cc0514253e8..ae208894d018a4f3a5e3dfc6cf40c782dc11f352 100644
--- a/libraries/libldap/free.c
+++ b/libraries/libldap/free.c
@@ -37,19 +37,19 @@ ldap_memvfree( void **v )
 }
 
 void *
-ldap_memalloc( size_t s )
+ldap_memalloc( ber_len_t s )
 {
 	return LDAP_MALLOC( s );
 }
 
 void *
-ldap_memcalloc( size_t n, size_t s )
+ldap_memcalloc( ber_len_t n, ber_len_t s )
 {
 	return LDAP_CALLOC( n, s );
 }
 
 void *
-ldap_memrealloc( void* p, size_t s )
+ldap_memrealloc( void* p, ber_len_t s )
 {
 	return LDAP_REALLOC( p, s );
 }
diff --git a/libraries/libldap/getattr.c b/libraries/libldap/getattr.c
index fc4d31f56a4afea6957985a91bacf744747ac918..07e8e27a4465e08b41b10e1cb0790ac21c7f0a45 100644
--- a/libraries/libldap/getattr.c
+++ b/libraries/libldap/getattr.c
@@ -33,7 +33,7 @@ ldap_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **ber )
 	assert( entry != NULL );
 	assert( ber != NULL );
 
-	if ( (*ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (*ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		*ber = NULL;
 		return( NULL );
 	}
diff --git a/libraries/libldap/getdn.c b/libraries/libldap/getdn.c
index dfd1c2e28a7fd217488d5b2d9f70afa44e37f1a4..e26412af07ac47d8de4276941f649ab7d348645d 100644
--- a/libraries/libldap/getdn.c
+++ b/libraries/libldap/getdn.c
@@ -38,7 +38,7 @@ ldap_get_dn( LDAP *ld, LDAPMessage *entry )
 	}
 
 	tmp = *entry->lm_ber;	/* struct copy */
-	if ( ber_scanf( &tmp, "{a", &dn ) == LBER_ERROR ) {
+	if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
 		ld->ld_errno = LDAP_DECODING_ERROR;
 		return( NULL );
 	}
diff --git a/libraries/libldap/getdxbyname.c b/libraries/libldap/getdxbyname.c
index f35f4b772742014fe89d34a9d8d8559b5bb87537..ec6a959cede29988110a1127dee9ae2a2753b87c 100644
--- a/libraries/libldap/getdxbyname.c
+++ b/libraries/libldap/getdxbyname.c
@@ -23,7 +23,7 @@
 
 #include "ldap-int.h"
 
-static char ** decode_answer LDAP_P(( unsigned char *answer, int len ));
+static char ** decode_answer LDAP_P(( unsigned char *answer, ber_len_t len ));
 
 #define MAX_TO_SORT	32
 
diff --git a/libraries/libldap/getentry.c b/libraries/libldap/getentry.c
index 8724758b8439c8d8e8aeb0b3f9eb33f67f6468d4..272ea304add86cbc71b7fd04a4f64debb33836b9 100644
--- a/libraries/libldap/getentry.c
+++ b/libraries/libldap/getentry.c
@@ -28,8 +28,8 @@ ldap_first_entry( LDAP *ld, LDAPMessage *chain )
 	assert( ld != NULL );
 	assert( LDAP_VALID( ld ) );
 
-	if( ld == NULL || chain == NULLMSG ) {
-		return NULLMSG;
+	if( ld == NULL || chain == NULL ) {
+		return NULL;
 	}
 
 	return chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY
@@ -43,13 +43,13 @@ ldap_next_entry( LDAP *ld, LDAPMessage *entry )
 	assert( ld != NULL );
 	assert( LDAP_VALID( ld ) );
 
-	if ( ld == NULL || entry == NULLMSG ) {
-		return NULLMSG;
+	if ( ld == NULL || entry == NULL ) {
+		return NULL;
 	}
 
 	for (
 		entry = entry->lm_chain;
-		entry != NULLMSG;
+		entry != NULL;
 		entry = entry->lm_chain )
 	{
 		if( entry->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
@@ -57,7 +57,7 @@ ldap_next_entry( LDAP *ld, LDAPMessage *entry )
 		}
 	}
 
-	return( NULLMSG );
+	return( NULL );
 }
 
 int
diff --git a/libraries/libldap/getfilter.c b/libraries/libldap/getfilter.c
index bf75521457ba02ae5672d473ddd14dddb2af364a..e3cb60fd0ac3a3eca75a04b957ee7323f35f8109 100644
--- a/libraries/libldap/getfilter.c
+++ b/libraries/libldap/getfilter.c
@@ -83,7 +83,7 @@ ldap_init_getfilter( LDAP_CONST char *fname )
 
 
 LDAPFiltDesc *
-ldap_init_getfilter_buf( char *buf, long buflen )
+ldap_init_getfilter_buf( char *buf, ber_len_t buflen )
 {
     LDAPFiltDesc	*lfdp;
     LDAPFiltList	*flp, *nextflp;
@@ -314,7 +314,7 @@ ldap_getnextfilter( LDAPFiltDesc *lfdp )
 void
 ldap_build_filter(
 	char *filtbuf,
-	unsigned long buflen,
+	ber_len_t buflen,
 	LDAP_CONST char *pattern,
 	LDAP_CONST char *prefix,
 	LDAP_CONST char *suffix,
diff --git a/libraries/libldap/getvalues.c b/libraries/libldap/getvalues.c
index 2833e18f5d362aea35b0a1aa43f8a824a18878df..f384ca9b414d13f671ea328554bc669b194ee94a 100644
--- a/libraries/libldap/getvalues.c
+++ b/libraries/libldap/getvalues.c
@@ -35,7 +35,7 @@ ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
 	ber = *entry->lm_ber;
 
 	/* skip sequence, dn, sequence of, and snag the first attr */
-	if ( ber_scanf( &ber, "{x{{a", &attr ) == LBER_ERROR ) {
+	if ( ber_scanf( &ber, "{x{{a" /*}}}*/, &attr ) == LBER_ERROR ) {
 		ld->ld_errno = LDAP_DECODING_ERROR;
 		return( NULL );
 	}
@@ -48,7 +48,7 @@ ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
 		LDAP_FREE(attr);
 		attr = NULL;
 
-		if ( ber_scanf( &ber, "x}{a", &attr ) == LBER_ERROR ) {
+		if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
 			ld->ld_errno = LDAP_DECODING_ERROR;
 			return( NULL );
 		}
@@ -87,7 +87,7 @@ ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
 	ber = *entry->lm_ber;
 
 	/* skip sequence, dn, sequence of, and snag the first attr */
-	if ( ber_scanf( &ber, "{x{{a", &attr ) == LBER_ERROR ) {
+	if ( ber_scanf( &ber, "{x{{a" /* }}} */, &attr ) == LBER_ERROR ) {
 		ld->ld_errno = LDAP_DECODING_ERROR;
 		return( NULL );
 	}
@@ -100,7 +100,7 @@ ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
 		LDAP_FREE( attr );
 		attr = NULL;
 
-		if ( ber_scanf( &ber, "x}{a", &attr ) == LBER_ERROR ) {
+		if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
 			ld->ld_errno = LDAP_DECODING_ERROR;
 			return( NULL );
 		}
diff --git a/libraries/libldap/kbind.c b/libraries/libldap/kbind.c
index 47ab77f939cc1cb120baaf496ff45e45495b7300..5e63698ddc7e2d4fbf58fe7286a97d05cbc224c7 100644
--- a/libraries/libldap/kbind.c
+++ b/libraries/libldap/kbind.c
@@ -60,7 +60,8 @@ ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
 {
 	BerElement	*ber;
 	char		*cred;
-	int		rc, credlen;
+	int		rc;
+	ber_len_t credlen;
 #ifdef STR_TRANSLATION
 	int		str_translation_on;
 #endif /* STR_TRANSLATION */
@@ -76,7 +77,7 @@ ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
 	}
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		LDAP_FREE( cred );
 		return( -1 );
 	}
@@ -153,7 +154,8 @@ ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
 {
 	BerElement	*ber;
 	char		*cred;
-	int		rc, credlen;
+	int		rc;
+	ber_len_t credlen;
 #ifdef STR_TRANSLATION
 	int		str_translation_on;
 #endif /* STR_TRANSLATION */
@@ -169,7 +171,7 @@ ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
 	}
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		LDAP_FREE( cred );
 		return( -1 );
 	}
@@ -253,7 +255,7 @@ ldap_get_kerberosv4_credentials(
 	LDAP *ld,
 	LDAP_CONST char *who,
 	LDAP_CONST char *service,
-	int *len )
+	ber_len_t *len )
 {
 	KTEXT_ST	ktxt;
 	int		err;
diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h
index f638a581f48d701f495cdbe1a833c1657b47be70..3a26f42cabb04c27f95f5d3d1519787e0006ae36 100644
--- a/libraries/libldap/ldap-int.h
+++ b/libraries/libldap/ldap-int.h
@@ -70,14 +70,14 @@ LDAP_BEGIN_DECL
  */
 
 struct ldapmsg {
-	int		lm_msgid;	/* the message id */
-	int		lm_msgtype;	/* the message type */
+	ber_int_t		lm_msgid;	/* the message id */
+	ber_tag_t		lm_msgtype;	/* the message type */
 	BerElement	*lm_ber;	/* the ber encoded message contents */
 	struct ldapmsg	*lm_chain;	/* for search - next msg in the resp */
 	struct ldapmsg	*lm_next;	/* next response */
 	time_t	lm_time;	/* used to maintain cache */
 };
-#define NULLMSG ((LDAPMessage *)NULL)
+
 /*
  * structure representing get/set'able options
  * which have global defaults.
@@ -90,10 +90,10 @@ struct ldapoptions {
 
 	int		ldo_debug;
 
-	int		ldo_version;	/* version to connect at */
-	int		ldo_deref;
-	int		ldo_timelimit;
-	int		ldo_sizelimit;
+	ber_int_t		ldo_version;	/* version to connect at */
+	ber_int_t		ldo_deref;
+	ber_int_t		ldo_timelimit;
+	ber_int_t		ldo_sizelimit;
 
 	int		ldo_defport;
 	char*	ldo_defbase;
@@ -146,17 +146,17 @@ typedef struct ldap_conn {
  * structure used to track outstanding requests
  */
 typedef struct ldapreq {
-	int		lr_msgid;	/* the message id */
+	ber_int_t		lr_msgid;	/* the message id */
 	int		lr_status;	/* status of request */
 #define LDAP_REQST_INPROGRESS	1
 #define LDAP_REQST_CHASINGREFS	2
 #define LDAP_REQST_NOTCONNECTED	3
 #define LDAP_REQST_WRITING	4
 	int		lr_outrefcnt;	/* count of outstanding referrals */
-	int		lr_origid;	/* original request's message id */
+	ber_int_t		lr_origid;	/* original request's message id */
 	int		lr_parentcnt;	/* count of parent requests */
-	int		lr_res_msgtype;	/* result message type */
-	int		lr_res_errno;	/* result LDAP errno */
+	ber_tag_t		lr_res_msgtype;	/* result message type */
+	ber_int_t		lr_res_errno;	/* result LDAP errno */
 	char		*lr_res_error;	/* result error string */
 	char		*lr_res_matched;/* result matched DN string */
 	BerElement	*lr_ber;	/* ber encoded request contents */
@@ -175,14 +175,13 @@ typedef struct ldapcache {
 	LDAPMessage	*lc_buckets[LDAP_CACHE_BUCKETS];/* hash table */
 	LDAPMessage	*lc_requests;			/* unfulfilled reqs */
 	long		lc_timeout;			/* request timeout */
-	long		lc_maxmem;			/* memory to use */
-	long		lc_memused;			/* memory in use */
+	ber_len_t		lc_maxmem;			/* memory to use */
+	ber_len_t		lc_memused;			/* memory in use */
 	int		lc_enabled;			/* enabled? */
 	unsigned long	lc_options;			/* options */
 #define LDAP_CACHE_OPT_CACHENOERRS	0x00000001
 #define LDAP_CACHE_OPT_CACHEALLERRS	0x00000002
 }  LDAPCache;
-#define NULLLDCACHE ((LDAPCache *)NULL)
 
 /*
  * handy macro for checking if handle is connectionless
@@ -217,7 +216,7 @@ struct ldap {
 #define ld_sctrls		ld_options.ldo_sctrls
 #define ld_cctrls		ld_options.ldo_cctrls
 
-	int		ld_version;		/* version connected at */
+	ber_int_t		ld_version;		/* version connected at */
 	char	*ld_host;
 	int		ld_port;
 
@@ -226,16 +225,16 @@ struct ldap {
 	LDAPFiltDesc	*ld_filtd;	/* from getfilter for ufn searches */
 	char		*ld_ufnprefix;	/* for incomplete ufn's */
 
-	int		ld_errno;
+	ber_int_t	ld_errno;
 	char	*ld_error;
 	char	*ld_matched;
-	int		ld_msgid;
+	ber_len_t		ld_msgid;
 
 	/* do not mess with these */
 	LDAPRequest	*ld_requests;	/* list of outstanding requests */
 	LDAPMessage	*ld_responses;	/* list of outstanding responses */
 
-	int		*ld_abandoned;	/* array of abandoned requests */
+	ber_int_t		*ld_abandoned;	/* array of abandoned requests */
 
 	LDAPCache	*ld_cache;	/* non-null if cache is initialized */
 	/* stuff used by connectionless searches. */
@@ -295,19 +294,19 @@ int ldap_log_printf LDAP_P((LDAP *ld, int level, const char *fmt, ...));
 /*
  * in cache.c
  */
-void ldap_add_request_to_cache LDAP_P(( LDAP *ld, unsigned long msgtype,
+void ldap_add_request_to_cache LDAP_P(( LDAP *ld, ber_tag_t msgtype,
         BerElement *request ));
 void ldap_add_result_to_cache LDAP_P(( LDAP *ld, LDAPMessage *result ));
-int ldap_check_cache LDAP_P(( LDAP *ld, unsigned long msgtype, BerElement *request ));
+int ldap_check_cache LDAP_P(( LDAP *ld, ber_tag_t msgtype, BerElement *request ));
 
 /*
  * in controls.c
  */
 LDAPControl *ldap_control_dup LDAP_P((
-	const LDAPControl *ctrl ));
+	LDAPControl *ctrl ));
 
 LDAPControl **ldap_controls_dup LDAP_P((
-	const LDAPControl **ctrls ));
+	LDAPControl **ctrls ));
 
 int ldap_int_get_controls LDAP_P((
 	BerElement *be,
@@ -321,7 +320,7 @@ int ldap_int_put_controls LDAP_P((
 /*
  * in dsparse.c
  */
-int next_line_tokens LDAP_P(( char **bufp, long *blenp, char ***toksp ));
+int next_line_tokens LDAP_P(( char **bufp, ber_len_t *blenp, char ***toksp ));
 void free_strarray LDAP_P(( char **sap ));
 
 #ifdef HAVE_KERBEROS
@@ -332,7 +331,7 @@ char *ldap_get_kerberosv4_credentials LDAP_P((
 	LDAP *ld,
 	LDAP_CONST char *who,
 	LDAP_CONST char *service,
-	int *len ));
+	ber_len_t *len ));
 
 #endif /* HAVE_KERBEROS */
 
@@ -368,17 +367,17 @@ int ldap_is_write_ready( LDAP *ld, Sockbuf *sb );
 /*
  * in request.c
  */
-int ldap_send_initial_request( LDAP *ld, unsigned long msgtype,
+ber_int_t ldap_send_initial_request( LDAP *ld, ber_tag_t msgtype,
 	const char *dn, BerElement *ber );
 BerElement *ldap_alloc_ber_with_options( LDAP *ld );
 void ldap_set_ber_options( LDAP *ld, BerElement *ber );
 
-int ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid,
+int ldap_send_server_request( LDAP *ld, BerElement *ber, ber_int_t msgid,
 	LDAPRequest *parentreq, LDAPServer *srvlist, LDAPConn *lc,
 	int bind );
 LDAPConn *ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
 	int connect, int bind );
-LDAPRequest *ldap_find_request_by_msgid( LDAP *ld, int msgid );
+LDAPRequest *ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid );
 void ldap_free_request( LDAP *ld, LDAPRequest *lr );
 void ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind );
 void ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all );
@@ -399,12 +398,15 @@ LDAP_F int cldap_getmsg	( LDAP *ld, struct timeval *timeout, BerElement *ber );
  */
 BerElement *ldap_build_search_req LDAP_P((
 	LDAP *ld,
-	const char *base, int scope,
-	const char *filter, char **attrs, int attrsonly,
+	const char *base,
+	ber_int_t scope,
+	const char *filter,
+	char **attrs,
+	ber_int_t attrsonly,
 	LDAPControl **sctrls,
 	LDAPControl **cctrls,
-	int timelimit,
-	int sizelimit ));
+	ber_int_t timelimit,
+	ber_int_t sizelimit ));
 
 /*
  * in string.c
@@ -443,9 +445,9 @@ char **ldap_getdxbyname( const char *domain );
  */
 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
 extern 
-int ldap_t61_to_8859( char **bufp, unsigned long *buflenp, int free_input );
+int ldap_t61_to_8859( char **bufp, ber_len_t *buflenp, int free_input );
 extern 
-int ldap_8859_to_t61( char **bufp, unsigned long *buflenp, int free_input );
+int ldap_8859_to_t61( char **bufp, ber_len_t *buflenp, int free_input );
 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
 
diff --git a/libraries/libldap/messages.c b/libraries/libldap/messages.c
index a67aecc860e4a05421e56fdde3192054a5f82345..33091da163ec568aba92864a95d71a8aba404472 100644
--- a/libraries/libldap/messages.c
+++ b/libraries/libldap/messages.c
@@ -25,8 +25,8 @@ ldap_first_message( LDAP *ld, LDAPMessage *chain )
 	assert( ld != NULL );
 	assert( LDAP_VALID( ld ) );
 
-	if ( ld == NULL || chain == NULLMSG ) {
-		return NULLMSG;
+	if ( ld == NULL || chain == NULL ) {
+		return NULL;
 	}
 	
   	return chain;
@@ -38,8 +38,8 @@ ldap_next_message( LDAP *ld, LDAPMessage *msg )
 	assert( ld != NULL );
 	assert( LDAP_VALID( ld ) );
 
-	if ( ld == NULL || msg == NULLMSG || msg->lm_chain == NULL ) {
-		return NULLMSG;
+	if ( ld == NULL || msg == NULL || msg->lm_chain == NULL ) {
+		return NULL;
 	}
 
 	return( msg->lm_chain );
diff --git a/libraries/libldap/modify.c b/libraries/libldap/modify.c
index 71abe8aba1b40af10119037eb91a1d3fefe3d9e1..583f919ddbdcf7260be09b3d7f06ba7007a78f36 100644
--- a/libraries/libldap/modify.c
+++ b/libraries/libldap/modify.c
@@ -73,11 +73,11 @@ ldap_modify_ext( LDAP *ld,
 	Debug( LDAP_DEBUG_TRACE, "ldap_modify_ext\n", 0, 0, 0 );
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		return( LDAP_NO_MEMORY );
 	}
 
-	if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_MODIFY, dn )
+	if ( ber_printf( ber, "{it{s{" /*}}}*/, ++ld->ld_msgid, LDAP_REQ_MODIFY, dn )
 	    == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
@@ -88,10 +88,11 @@ ldap_modify_ext( LDAP *ld,
 	for ( i = 0; mods[i] != NULL; i++ ) {
 		if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
 			rc = ber_printf( ber, "{e{s[V]}}",
-			    mods[i]->mod_op & ~LDAP_MOD_BVALUES,
+			    (ber_int_t) ( mods[i]->mod_op & ~LDAP_MOD_BVALUES ),
 			    mods[i]->mod_type, mods[i]->mod_bvalues );
 		} else {
-			rc = ber_printf( ber, "{e{s[v]}}", mods[i]->mod_op,
+			rc = ber_printf( ber, "{e{s[v]}}",
+				(ber_int_t) mods[i]->mod_op,
 			    mods[i]->mod_type, mods[i]->mod_values );
 		}
 
@@ -102,7 +103,7 @@ ldap_modify_ext( LDAP *ld,
 		}
 	}
 
-	if ( ber_printf( ber, "}}" ) == -1 ) {
+	if ( ber_printf( ber, /*{{*/ "}}" ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
 		return( ld->ld_errno );
@@ -114,7 +115,7 @@ ldap_modify_ext( LDAP *ld,
 		return ld->ld_errno;
 	}
 
-	if ( ber_printf( ber, "}" ) == -1 ) {
+	if ( ber_printf( ber, /*{*/ "}" ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
 		return( ld->ld_errno );
diff --git a/libraries/libldap/modrdn.c b/libraries/libldap/modrdn.c
index d2885bd63e95e3d7f3d5e79393dcff7e44f8f239..a8998b2bf867e689394f9566aee17086d141c969 100644
--- a/libraries/libldap/modrdn.c
+++ b/libraries/libldap/modrdn.c
@@ -70,7 +70,7 @@ ldap_rename(
 	Debug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		return( LDAP_NO_MEMORY );
 	}
 
@@ -82,15 +82,15 @@ ldap_rename(
 			return( ld->ld_errno );
 		}
 
-		rc = ber_printf( ber, "{it{ssbts}", /* leave '}' for later */ 
+		rc = ber_printf( ber, "{it{ssbts}", /* '}' */ 
 			++ld->ld_msgid, LDAP_REQ_MODDN,
-			dn, newrdn, deleteoldrdn,
+			dn, newrdn, (ber_int_t) deleteoldrdn,
 			LDAP_TAG_NEWSUPERIOR, newSuperior );
 
 	} else {
-		rc = ber_printf( ber, "{it{ssb}", /* leave '}' for later */ 
+		rc = ber_printf( ber, "{it{ssb}", /* '}' */ 
 			++ld->ld_msgid, LDAP_REQ_MODDN,
-			dn, newrdn, deleteoldrdn );
+			dn, newrdn, (ber_int_t) deleteoldrdn );
 	}
 
 	if ( rc < 0 ) {
@@ -105,8 +105,7 @@ ldap_rename(
 		return ld->ld_errno;
 	}
 
-	/* close the '{' */
-	rc = ber_printf( ber, "}" );
+	rc = ber_printf( ber, /*{*/ "}" );
 	if ( rc < 0 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
diff --git a/libraries/libldap/options.c b/libraries/libldap/options.c
index 3e748a7848452c7830b625c047d147990a3ce508..748f9c40781bfdc12827d78cad3efb626e40773a 100644
--- a/libraries/libldap/options.c
+++ b/libraries/libldap/options.c
@@ -152,7 +152,7 @@ ldap_get_option(
 			break;
 		} 
 
-		* (int *) outvalue = ber_pvt_sb_get_desc( &(ld->ld_sb) );
+		* (ber_socket_t *) outvalue = ber_pvt_sb_get_desc( &(ld->ld_sb) );
 		return LDAP_OPT_SUCCESS;
 
 	case LDAP_OPT_DEREF:
diff --git a/libraries/libldap/os-ip.c b/libraries/libldap/os-ip.c
index 9f7e7051aa4ae35f9e3eebeb99ad2764d7cafb83..b17e71a58fd78657f56154c5ff8152dd75ce43c2 100644
--- a/libraries/libldap/os-ip.c
+++ b/libraries/libldap/os-ip.c
@@ -44,7 +44,8 @@ ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address,
  * XXX async is not used yet!
  */
 {
-	int			rc, i, s = 0;
+	int			rc, i;
+	ber_socket_t s = AC_SOCKET_INVALID;
 	int			connected, use_hp;
 	struct sockaddr_in	sin;
 	struct hostent		*hp = NULL;
diff --git a/libraries/libldap/references.c b/libraries/libldap/references.c
index 08e96b05b67e9a26fe708e906a35c668de84ec80..50cf0fc27cd361fc0cefa6c70e2b8113daa77fd9 100644
--- a/libraries/libldap/references.c
+++ b/libraries/libldap/references.c
@@ -26,8 +26,8 @@ ldap_first_reference( LDAP *ld, LDAPMessage *chain )
 	assert( ld != NULL );
 	assert( chain !=  NULL );
 
-	if ( ld == NULL || chain == NULLMSG ) {
-		return NULLMSG;
+	if ( ld == NULL || chain == NULL ) {
+		return NULL;
 	}
 
 	return chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE
@@ -41,13 +41,13 @@ ldap_next_reference( LDAP *ld, LDAPMessage *ref )
 	assert( ld != NULL );
 	assert( ref !=  NULL );
 
-	if ( ld == NULL || ref == NULLMSG ) {
-		return NULLMSG;
+	if ( ld == NULL || ref == NULL ) {
+		return NULL;
 	}
 
 	for (
 		ref = ref->lm_chain;
-		ref != NULLMSG;
+		ref != NULL;
 		ref = ref->lm_chain )
 	{
 		if( ref->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
@@ -55,7 +55,7 @@ ldap_next_reference( LDAP *ld, LDAPMessage *ref )
 		}
 	}
 
-	return( NULLMSG );
+	return( NULL );
 }
 
 int
diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c
index 133e42fd10a449c6142691afa5ce9f1c97e18a5c..76aa484e90fcc8c927d03b69cdf3dd8d938473c3 100644
--- a/libraries/libldap/request.c
+++ b/libraries/libldap/request.c
@@ -32,8 +32,11 @@ static void free_servers LDAP_P(( LDAPServer *srvlist ));
 static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
-static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
-    int msgid, char **dnp ));
+static BerElement *re_encode_request LDAP_P((
+	LDAP *ld,
+	BerElement *origber,
+    ber_int_t msgid,
+	char **dnp ));
 
 
 BerElement *
@@ -41,7 +44,7 @@ ldap_alloc_ber_with_options( LDAP *ld )
 {
 	BerElement	*ber;
 
-    if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
+    if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
 		ld->ld_errno = LDAP_NO_MEMORY;
 #ifdef STR_TRANSLATION
 	} else {
@@ -67,10 +70,10 @@ ldap_set_ber_options( LDAP *ld, BerElement *ber )
 }
 
 
-int
+ber_int_t
 ldap_send_initial_request(
 	LDAP *ld,
-	unsigned long msgtype,
+	ber_tag_t msgtype,
 	const char *dn,
 	BerElement *ber )
 {
@@ -150,8 +153,14 @@ ldap_send_initial_request(
 
 
 int
-ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
-	*parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
+ldap_send_server_request(
+	LDAP *ld,
+	BerElement *ber,
+	ber_int_t msgid,
+	LDAPRequest *parentreq,
+	LDAPServer *srvlist,
+	LDAPConn *lc,
+	int bind )
 {
 	LDAPRequest	*lr;
 	int incparent;
@@ -505,15 +514,15 @@ ldap_dump_requests_and_responses( LDAP *ld )
 	}
 
 	fprintf( stderr, "** Response Queue:\n" );
-	if (( lm = ld->ld_responses ) == NULLMSG ) {
+	if (( lm = ld->ld_responses ) == NULL ) {
 		fprintf( stderr, "   Empty\n" );
 	}
-	for ( ; lm != NULLMSG; lm = lm->lm_next ) {
+	for ( ; lm != NULL; lm = lm->lm_next ) {
 		fprintf( stderr, " * msgid %d,  type %d\n",
 		    lm->lm_msgid, lm->lm_msgtype );
 		if (( l = lm->lm_chain ) != NULL ) {
 			fprintf( stderr, "   chained responses:\n" );
-			for ( ; l != NULLMSG; l = l->lm_chain ) {
+			for ( ; l != NULL; l = l->lm_chain ) {
 				fprintf( stderr,
 				    "  * msgid %d,  type %d\n",
 				    l->lm_msgid, l->lm_msgtype );
@@ -775,20 +784,22 @@ ldap_append_referral( LDAP *ld, char **referralsp, char *s )
 
 
 static BerElement *
-re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
+re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp )
 {
 /*
  * XXX this routine knows way too much about how the lber library works!
  */
-	unsigned long	along, tag;
-	long		ver;
+	ber_int_t	along;
+	ber_len_t	len;
+	ber_tag_t	tag;
+	ber_int_t	ver;
 	int		rc;
 	BerElement	tmpber, *ber;
 	char		*orig_dn;
 
 	Debug( LDAP_DEBUG_TRACE,
-	    "re_encode_request: new msgid %d, new dn <%s>\n",
-	    msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
+	    "re_encode_request: new msgid %ld, new dn <%s>\n",
+	    (long) msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
 
 	tmpber = *origber;
 
@@ -797,17 +808,17 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
 	 * followed by a sequence that is tagged with the operation code
 	 */
 	if ( ber_scanf( &tmpber, "{i", /*}*/ &along ) != LDAP_TAG_MSGID ||
-	    ( tag = ber_skip_tag( &tmpber, &along )) == LBER_DEFAULT ) {
+	    ( tag = ber_skip_tag( &tmpber, &len )) == LBER_DEFAULT ) {
                 ld->ld_errno = LDAP_DECODING_ERROR;
 		return( NULL );
 	}
 
-        if (( ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+        if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
                 return( NULL );
         }
 
 	/* bind requests have a version number before the DN & other stuff */
-	if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, (long *)&ver ) ==
+	if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, &ver ) ==
 	    LBER_DEFAULT ) {
                 ld->ld_errno = LDAP_DECODING_ERROR;
 		ber_free( ber, 1 );
@@ -858,7 +869,7 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
 
 
 LDAPRequest *
-ldap_find_request_by_msgid( LDAP *ld, int msgid )
+ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
 {
     	LDAPRequest	*lr;
 
diff --git a/libraries/libldap/result.c b/libraries/libldap/result.c
index 5f9ff57cecf54c7826d90b7a5d2ab29882daaead..adc34d9db4159ff1c50cce4e7d4de66e7aeceeaf 100644
--- a/libraries/libldap/result.c
+++ b/libraries/libldap/result.c
@@ -36,13 +36,13 @@
 #include "ldap-int.h"
 
 
-static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
-static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
-static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
+static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
+static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
+static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
 	LDAPMessage **result ));
-static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
-	LDAPMessage **result ));
-static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
+static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
+	int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
+static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
 
 
@@ -87,15 +87,15 @@ ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
 	 * wait until it arrives or timeout occurs.
 	 */
 
-	*result = NULLMSG;
-	lastlm = NULLMSG;
-	for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
+	*result = NULL;
+	lastlm = NULL;
+	for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
 		nextlm = lm->lm_next;
 
 		if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
 			ldap_mark_abandoned( ld, lm->lm_msgid );
 
-			if ( lastlm == NULLMSG ) {
+			if ( lastlm == NULL ) {
 				ld->ld_responses = lm->lm_next;
 			} else {
 				lastlm->lm_next = nextlm;
@@ -115,12 +115,12 @@ ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
 			    && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
 				break;
 
-			for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
+			for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
 				if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
 					break;
 			}
 
-			if ( tmp == NULLMSG ) {
+			if ( tmp == NULL ) {
 				return( wait4msg( ld, msgid, all, timeout,
 				    result ) );
 			}
@@ -129,20 +129,20 @@ ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
 		}
 		lastlm = lm;
 	}
-	if ( lm == NULLMSG ) {
+	if ( lm == NULL ) {
 		return( wait4msg( ld, msgid, all, timeout, result ) );
 	}
 
-	if ( lastlm == NULLMSG ) {
-		ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
+	if ( lastlm == NULL ) {
+		ld->ld_responses = (all == 0 && lm->lm_chain != NULL
 		    ? lm->lm_chain : lm->lm_next);
 	} else {
-		lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
+		lastlm->lm_next = (all == 0 && lm->lm_chain != NULL
 		    ? lm->lm_chain : lm->lm_next);
 	}
 	if ( all == 0 )
-		lm->lm_chain = NULLMSG;
-	lm->lm_next = NULLMSG;
+		lm->lm_chain = NULL;
+	lm->lm_next = NULL;
 
 	*result = lm;
 	ld->ld_errno = LDAP_SUCCESS;
@@ -150,7 +150,11 @@ ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
 }
 
 static int
-wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
+wait4msg(
+	LDAP *ld,
+	ber_int_t msgid,
+	int all,
+	struct timeval *timeout,
 	LDAPMessage **result )
 {
 	int		rc;
@@ -253,26 +257,32 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 }
 
 
-static int
-try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
-    LDAPConn *lc, LDAPMessage **result )
+static ber_tag_t
+try_read1msg(
+	LDAP *ld,
+	ber_int_t msgid,
+	int all,
+	Sockbuf *sb,
+    LDAPConn *lc,
+	LDAPMessage **result )
 {
 	BerElement	*ber;
 	LDAPMessage	*new, *l, *prev, *tmp;
-	long		id;
-	unsigned long	tag, len;
+	ber_int_t	id;
+	ber_tag_t	tag;
+	ber_len_t	len;
 	int		foundit = 0;
 	LDAPRequest	*lr;
 	BerElement	tmpber;
 	int		rc, refer_cnt, hadref, simple_request;
-	unsigned long	lderr;
+	ber_int_t	lderr;
 
 	assert( ld != NULL );
 	assert( lc != NULL );
 	
 	Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
 
-    if ( lc->lconn_ber == NULLBER ) {
+    if ( lc->lconn_ber == NULL ) {
 		lc->lconn_ber = ldap_alloc_ber_with_options(ld);
 
 		if( lc->lconn_ber == NULL ) {
@@ -309,7 +319,7 @@ try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
      * We read a complete message.
 	 * The connection should no longer need this ber.
 	 */
-    lc->lconn_ber = NULLBER;
+    lc->lconn_ber = NULL;
 
 	/* message id */
 	if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
@@ -319,7 +329,7 @@ try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
 	}
 
 	/* if it's been abandoned, toss it */
-	if ( ldap_abandoned( ld, (int)id ) ) {
+	if ( ldap_abandoned( ld, id ) ) {
 		ber_free( ber, 1 );
 		return( -2 );	/* continue looking */
 	}
@@ -477,15 +487,15 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 	 * search response.
 	 */
 
-	prev = NULLMSG;
-	for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
+	prev = NULL;
+	for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
 		if ( l->lm_msgid == new->lm_msgid )
 			break;
 		prev = l;
 	}
 
 	/* not part of an existing search response */
-	if ( l == NULLMSG ) {
+	if ( l == NULL ) {
 		if ( foundit ) {
 			*result = new;
 			ld->ld_errno = LDAP_SUCCESS;
@@ -501,7 +511,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 	    new->lm_msgid, new->lm_msgtype, 0 );
 
 	/* part of a search response - add to end of list of entries */
-	for ( tmp = l; tmp->lm_chain != NULLMSG &&
+	for ( tmp = l; tmp->lm_chain != NULL &&
 	    tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
 	    tmp = tmp->lm_chain )
 		;	/* NULL */
@@ -509,7 +519,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 
 	/* return the whole chain if that's what we were looking for */
 	if ( foundit ) {
-		if ( prev == NULLMSG )
+		if ( prev == NULL )
 			ld->ld_responses = l->lm_next;
 		else
 			prev->lm_next = l->lm_next;
@@ -533,11 +543,12 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 }
 
 
-static unsigned long
+static ber_tag_t
 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
 {
-	unsigned long	len, tag;
-	long		along;
+	ber_len_t	len;
+	ber_int_t	tag;
+	ber_int_t	along;
 	BerElement *ber;
 
 	*bp = NULL;
@@ -549,7 +560,7 @@ build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
 	}
 
 	if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
-	    (unsigned long) lr->lr_res_msgtype, lr->lr_res_errno,
+	    lr->lr_res_msgtype, lr->lr_res_errno,
 	    lr->lr_res_matched ? lr->lr_res_matched : "",
 	    lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
 
@@ -651,7 +662,7 @@ ldap_msgfree( LDAPMessage *lm )
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
 
-	for ( ; lm != NULLMSG; lm = next ) {
+	for ( ; lm != NULL; lm = next ) {
 		next = lm->lm_chain;
 		type = lm->lm_msgtype;
 		ber_free( lm->lm_ber, 1 );
@@ -675,17 +686,17 @@ ldap_msgdelete( LDAP *ld, int msgid )
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
 
-	prev = NULLMSG;
-	for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
+	prev = NULL;
+	for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
 		if ( lm->lm_msgid == msgid )
 			break;
 		prev = lm;
 	}
 
-	if ( lm == NULLMSG )
+	if ( lm == NULL )
 		return( -1 );
 
-	if ( prev == NULLMSG )
+	if ( prev == NULL )
 		ld->ld_responses = lm->lm_next;
 	else
 		prev->lm_next = lm->lm_next;
@@ -701,7 +712,7 @@ ldap_msgdelete( LDAP *ld, int msgid )
  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
  */
 static int
-ldap_abandoned( LDAP *ld, int msgid )
+ldap_abandoned( LDAP *ld, ber_int_t msgid )
 {
 	int	i;
 
@@ -717,7 +728,7 @@ ldap_abandoned( LDAP *ld, int msgid )
 
 
 static int
-ldap_mark_abandoned( LDAP *ld, int msgid )
+ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
 {
 	int	i;
 
@@ -743,8 +754,9 @@ ldap_mark_abandoned( LDAP *ld, int msgid )
 int
 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
 {
-	int		rc;
-	unsigned long	tag, len;
+	int	rc;
+	ber_tag_t	tag;
+	ber_len_t	len;
 
 	if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
 		rc = ldap_select1( ld, timeout );
@@ -763,6 +775,6 @@ cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
 		return( -1 );
 	}
 
-	return( tag );
+	return( 0 );
 }
 #endif /* LDAP_CONNECTIONLESS */
diff --git a/libraries/libldap/sbind.c b/libraries/libldap/sbind.c
index fd7892d5e65fcb1fb380af0167b0bc5fff6f3ca6..8546a879bf9c82817baea994da69552906f7a507 100644
--- a/libraries/libldap/sbind.c
+++ b/libraries/libldap/sbind.c
@@ -67,7 +67,7 @@ ldap_simple_bind( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
 		passwd = "";
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		return( -1 );
 	}
 
diff --git a/libraries/libldap/schema.c b/libraries/libldap/schema.c
index 2a9d3ec32fbbf876e6e4c312b7aaeea7ae3bad8c..d5bc4e8bc404e8fb173be25deead14b687109bbc 100644
--- a/libraries/libldap/schema.c
+++ b/libraries/libldap/schema.c
@@ -29,8 +29,8 @@
 
 typedef struct safe_string {
 	char * val;
-	int size;
-	int pos;
+	ber_len_t size;
+	ber_len_t pos;
 	int at_whsp;
 } safe_string;
 
diff --git a/libraries/libldap/search.c b/libraries/libldap/search.c
index d1afce7c0a40aa188e3b028b8b533c53f9b9305f..379aeae4b5dae35afb089eab15821381e7666d0e 100644
--- a/libraries/libldap/search.c
+++ b/libraries/libldap/search.c
@@ -28,7 +28,7 @@ static char *find_right_paren LDAP_P((
 static char *put_complex_filter LDAP_P((
 	BerElement *ber,
 	char *str,
-	unsigned long tag,
+	ber_tag_t tag,
 	int not ));
 
 static int put_filter LDAP_P((
@@ -98,7 +98,7 @@ ldap_search_ext(
 	ber = ldap_build_search_req( ld, base, scope, filter, attrs,
 	    attrsonly, sctrls, cctrls, timelimit, sizelimit ); 
 
-	if ( ber == NULLBER ) {
+	if ( ber == NULL ) {
 		return ld->ld_errno;
 	}
 
@@ -184,7 +184,7 @@ ldap_search(
 	ber = ldap_build_search_req( ld, base, scope, filter, attrs,
 	    attrsonly, NULL, NULL, -1, -1 ); 
 
-	if ( ber == NULLBER ) {
+	if ( ber == NULL ) {
 		return( -1 );
 	}
 
@@ -208,14 +208,14 @@ BerElement *
 ldap_build_search_req(
 	LDAP *ld,
 	LDAP_CONST char *base_in,
-	int scope,
+	ber_int_t scope,
 	LDAP_CONST char *filter_in,
 	char **attrs,
-	int attrsonly,
+	ber_int_t attrsonly,
 	LDAPControl **sctrls,
 	LDAPControl **cctrls,
-	int timelimit,
-	int sizelimit )
+	ber_int_t timelimit,
+	ber_int_t sizelimit )
 {
 	BerElement	*ber;
 	int		err;
@@ -247,8 +247,8 @@ ldap_build_search_req(
 	 */
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
-		return( NULLBER );
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
+		return( NULL );
 	}
 
 	if ( base_in == NULL ) {
@@ -273,7 +273,7 @@ ldap_build_search_req(
 	} else {
 #endif /* LDAP_CONNECTIONLESS */
 		err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
-		    LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
+		    LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
 			(sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
 			(timelimit < 0) ? ld->ld_timelimit : timelimit,
 		    attrsonly );
@@ -284,7 +284,7 @@ ldap_build_search_req(
 	if ( err == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
-		return( NULLBER );
+		return( NULL );
 	}
 
 	filter = LDAP_STRDUP( filter_in );
@@ -294,25 +294,25 @@ ldap_build_search_req(
 	if ( err  == -1 ) {
 		ld->ld_errno = LDAP_FILTER_ERROR;
 		ber_free( ber, 1 );
-		return( NULLBER );
+		return( NULL );
 	}
 
-	if ( ber_printf( ber, "{v}}", attrs ) == -1 ) {
+	if ( ber_printf( ber, /*{*/ "{v}}", attrs ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
-		return( NULLBER );
+		return( NULL );
 	}
 
 	/* Put Server Controls */
 	if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
 		ber_free( ber, 1 );
-		return( NULLBER );
+		return( NULL );
 	}
 
-	if ( ber_printf( ber, "}", attrs ) == -1 ) {
+	if ( ber_printf( ber, /*{*/ "}", attrs ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
-		return( NULLBER );
+		return( NULL );
 	}
 
 	return( ber );
@@ -344,7 +344,7 @@ find_right_paren( char *s )
 }
 
 static char *
-put_complex_filter( BerElement *ber, char *str, unsigned long tag, int not )
+put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
 {
 	char	*next;
 
@@ -356,7 +356,7 @@ put_complex_filter( BerElement *ber, char *str, unsigned long tag, int not )
 	 */
 
 	/* put explicit tag */
-	if ( ber_printf( ber, "t{", tag ) == -1 )
+	if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
 		return( NULL );
 
 	str++;
@@ -369,7 +369,7 @@ put_complex_filter( BerElement *ber, char *str, unsigned long tag, int not )
 	*next++ = ')';
 
 	/* flush explicit tagged thang */
-	if ( ber_printf( ber, "}" ) == -1 )
+	if ( ber_printf( ber, /*{*/ "}" ) == -1 )
 		return( NULL );
 
 	return( next );
@@ -509,7 +509,7 @@ put_filter( BerElement *ber, char *str )
 		case ')':
 			Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
 			    0 );
-			if ( ber_printf( ber, "]" ) == -1 )
+			if ( ber_printf( ber, /*[*/ "]" ) == -1 )
 				return( -1 );
 			str++;
 			parens--;
@@ -590,7 +590,7 @@ put_simple_filter(
 {
 	char		*s;
 	char		*value, savechar;
-	unsigned long	ftype;
+	ber_tag_t	ftype;
 	int		rc;
 
 	Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
@@ -646,12 +646,12 @@ static int
 put_substring_filter( BerElement *ber, char *type, char *val )
 {
 	char		*nextstar, gotstar = 0;
-	unsigned long	ftype;
+	ber_tag_t	ftype = LDAP_FILTER_SUBSTRINGS;
 
 	Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
 	    val, 0 );
 
-	if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 )
+	if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
 		return( -1 );
 
 	while ( val != NULL ) {
@@ -676,7 +676,7 @@ put_substring_filter( BerElement *ber, char *type, char *val )
 		val = nextstar;
 	}
 
-	if ( ber_printf( ber, "}}" ) == -1 )
+	if ( ber_printf( ber, /* {{ */ "}}" ) == -1 )
 		return( -1 );
 
 	return( 0 );
diff --git a/libraries/libldap/srchpref.c b/libraries/libldap/srchpref.c
index 11082dc66891f09040ca6f9b0d2e603ffdf8d3a9..54405e341df20929eb33fb16ceb43b8d775c25ee 100644
--- a/libraries/libldap/srchpref.c
+++ b/libraries/libldap/srchpref.c
@@ -99,7 +99,9 @@ ldap_init_searchprefs( char *file, struct ldap_searchobj **solistp )
 
 
 int
-ldap_init_searchprefs_buf( char *buf, long buflen,
+ldap_init_searchprefs_buf(
+	char *buf,
+	ber_len_t buflen,
 	struct ldap_searchobj **solistp )
 {
     int				rc = -1, version;
diff --git a/libraries/libldap/test.c b/libraries/libldap/test.c
index b6aab5924d0ecb59a3d82a21308426cddf6cef07..dc52cd1f6b68a7ab08c98567379136e089538171 100644
--- a/libraries/libldap/test.c
+++ b/libraries/libldap/test.c
@@ -113,7 +113,7 @@ static int
 file_read( char *path, struct berval *bv )
 {
 	FILE		*fp;
-	long		rlen;
+	ber_slen_t	rlen;
 	int		eof;
 
 	if (( fp = fopen( path, "r" )) == NULL ) {
@@ -145,7 +145,7 @@ file_read( char *path, struct berval *bv )
 	eof = feof( fp );
 	fclose( fp );
 
-	if ( (unsigned long) rlen != bv->bv_len ) {
+	if ( (ber_len_t) rlen != bv->bv_len ) {
 		perror( path );
 		free( bv->bv_val );
 		return( -1 );
@@ -575,7 +575,7 @@ main( int argc, char **argv )
 				printf( "\nresult: msgtype %d msgid %d\n",
 				    msgtype, res->lm_msgid );
 				handle_result( ld, res );
-				res = NULLMSG;
+				res = NULL;
 				break;
 
 			case 'm':	/* remove */
@@ -618,7 +618,7 @@ main( int argc, char **argv )
 				printf( "\nresult: msgid %d\n",
 				    res->lm_msgid );
 				handle_result( ld, res );
-				res = NULLMSG;
+				res = NULL;
 			    }
 #endif /* LDAP_CONNECTIONLESS */
 			} else {
@@ -663,7 +663,7 @@ main( int argc, char **argv )
 			else {
 				printf( "\nresult: err %d\n", id );
 				handle_result( ld, res );
-				res = NULLMSG;
+				res = NULL;
 			}
 			free_list( types );
 			break;
@@ -912,7 +912,7 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
 {
 	LDAPMessage	*e;
 
-	for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
+	for ( e = ldap_first_entry( ld, res ); e != NULL;
 	    e = ldap_next_entry( ld, e ) )
 	{
 		BerElement	*ber = NULL;
@@ -945,7 +945,7 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
 					int	j, nonascii;
 
 					nonascii = 0;
-					for ( j = 0; (unsigned long) j < vals[i]->bv_len; j++ )
+					for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
 						if ( !isascii( vals[i]->bv_val[j] ) ) {
 							nonascii = 1;
 							break;
@@ -972,6 +972,6 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
 	}
 
 	if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
-	    || res->lm_chain != NULLMSG )
+	    || res->lm_chain != NULL )
 		print_ldap_result( ld, res, "search" );
 }
diff --git a/libraries/libldap/tmplout.c b/libraries/libldap/tmplout.c
index b3b16b194b911126b1f40a8e7821d2d57edacd59..cfa29ee2f57ea31250ae19e0978d0465ecc32a8d 100644
--- a/libraries/libldap/tmplout.c
+++ b/libraries/libldap/tmplout.c
@@ -426,7 +426,7 @@ do_entry2text_search(
     struct ldap_disptmpl	*tmpl;
     struct timeval		timeout;
 
-    if ( dn == NULL && entry == NULLMSG ) {
+    if ( dn == NULL && entry == NULL ) {
 	ld->ld_errno = LDAP_PARAM_ERROR;
 	return( ld->ld_errno );
     }
@@ -467,7 +467,7 @@ do_entry2text_search(
 
 
     if ( tmpllist != NULL ) {
-	ldmp = NULLMSG;
+	ldmp = NULL;
 
 	if ( entry == NULL ) {
 	    char	*ocattrs[2];
diff --git a/libraries/libldap/unbind.c b/libraries/libldap/unbind.c
index b5011978d0589a43db62914274e9517b005cc38c..39b3263e4c6dd2b04505c630b7e300fe7924ca84 100644
--- a/libraries/libldap/unbind.c
+++ b/libraries/libldap/unbind.c
@@ -161,12 +161,12 @@ ldap_send_unbind(
 	Debug( LDAP_DEBUG_TRACE, "ldap_send_unbind\n", 0, 0, 0 );
 
 	/* create a message to send */
-	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
 		return( ld->ld_errno );
 	}
 
 	/* fill it in */
-	if ( ber_printf( ber, "{itn", ++ld->ld_msgid,
+	if ( ber_printf( ber, "{itn" /*}*/, ++ld->ld_msgid,
 	    LDAP_REQ_UNBIND ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
@@ -179,8 +179,7 @@ ldap_send_unbind(
 		return ld->ld_errno;
 	}
 
-	if ( ber_printf( ber, "}", ++ld->ld_msgid,
-	    LDAP_REQ_UNBIND ) == -1 ) {
+	if ( ber_printf( ber, /*{*/ "}", LDAP_REQ_UNBIND ) == -1 ) {
 		ld->ld_errno = LDAP_ENCODING_ERROR;
 		ber_free( ber, 1 );
 		return( ld->ld_errno );
diff --git a/libraries/libldap/url.c b/libraries/libldap/url.c
index 7ede89a768fe6f89adc1004be48debaadbcb4255..c5a4e0650c7f12181aaac0a97e7b315a5601533a 100644
--- a/libraries/libldap/url.c
+++ b/libraries/libldap/url.c
@@ -267,7 +267,7 @@ ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
 	    ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
 		-1, -1 );
 
-	if ( ber == NULLBER ) {
+	if ( ber == NULL ) {
 		return( -1 );
 	}
 
diff --git a/servers/slapd/abandon.c b/servers/slapd/abandon.c
index 968c64d4cfe02576a310f88bfa57a10f349568b8..d6b4704d32cac7a66e3581c99f2be758749f4e91 100644
--- a/servers/slapd/abandon.c
+++ b/servers/slapd/abandon.c
@@ -25,7 +25,7 @@ do_abandon(
     Operation	*op
 )
 {
-	int		id;
+	ber_int_t		id;
 	Operation	*o;
 	Operation	**oo;
 
diff --git a/servers/slapd/add.c b/servers/slapd/add.c
index 5e195c726f1a15d68ae444a10f51dde467dced1d..6ca236a2796e9ecd40e183013ab34004a4ed6a71 100644
--- a/servers/slapd/add.c
+++ b/servers/slapd/add.c
@@ -27,7 +27,8 @@ do_add( Connection *conn, Operation *op )
 {
 	BerElement	*ber = op->o_ber;
 	char		*dn, *last;
-	unsigned long	len, tag;
+	ber_len_t	len;
+	ber_tag_t	tag;
 	Entry		*e;
 	Backend		*be;
 
@@ -46,7 +47,7 @@ do_add( Connection *conn, Operation *op )
 	 */
 
 	/* get the name */
-	if ( ber_scanf( ber, "{a", &dn ) == LBER_ERROR ) {
+	if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
 		Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
 		send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
 		    "decoding error" );
diff --git a/servers/slapd/back-ldbm/back-ldbm.h b/servers/slapd/back-ldbm/back-ldbm.h
index 73fc4eb902e93cad32b96c3a4cf12168d0faddae..267bbcffa6361001ecdce5d54eb595dd935bc6c1 100644
--- a/servers/slapd/back-ldbm/back-ldbm.h
+++ b/servers/slapd/back-ldbm/back-ldbm.h
@@ -144,8 +144,8 @@ struct ldbminfo {
 
 extern int ldbm_ignore_nextid_file;
 
-#include "proto-back-ldbm.h"
-
 LDAP_END_DECL
 
+#include "proto-back-ldbm.h"
+
 #endif /* _back_ldbm_h_ */
diff --git a/servers/slapd/back-ldbm/external.h b/servers/slapd/back-ldbm/external.h
index 54741012cfc88232244a3d57777c89b0bae04bbf..144b56c78efeb4fb288770eee2112af2b6151516 100644
--- a/servers/slapd/back-ldbm/external.h
+++ b/servers/slapd/back-ldbm/external.h
@@ -38,7 +38,7 @@ extern int	ldbm_back_modify LDAP_P(( BackendDB *bd,
 
 extern int	ldbm_back_modrdn LDAP_P(( BackendDB *bd,
 	Connection *conn, Operation *op,
-	char *dn, char*newrdn, int deleteoldrdn,
+	char *dn, char* newrdn, int deleteoldrdn,
 	char *newSuperior ));
 
 extern int	ldbm_back_add LDAP_P(( BackendDB *bd,
@@ -48,7 +48,7 @@ extern int	ldbm_back_delete LDAP_P(( BackendDB *bd,
 	Connection *conn, Operation *op, char *dn ));
 
 extern int	ldbm_back_abandon LDAP_P(( BackendDB *bd,
-	Connection *conn, Operation *op, int msgid ));
+	Connection *conn, Operation *op, ber_int_t msgid ));
 
 extern int	ldbm_back_group LDAP_P(( BackendDB *bd,
 	Entry *target, char* gr_ndn, char* op_ndn,
diff --git a/servers/slapd/back-ldbm/startup.c b/servers/slapd/back-ldbm/startup.c
index 910eb67e231775a5b02805b2c1b7cf7a12419a8a..c9e8703d1f2f9162920c38354322791eb7d49264 100644
--- a/servers/slapd/back-ldbm/startup.c
+++ b/servers/slapd/back-ldbm/startup.c
@@ -55,7 +55,7 @@ ldbm_back_startup(
 		char   cwd[MAXPATHLEN];
 
 		(void) getcwd( cwd, MAXPATHLEN );
-		sprintf( cwd, "%s%s%s", cwd, LDAP_DIRSEP, li->li_directory );
+		sprintf( cwd, "%s" LDAP_DIRSEP "%s", cwd, li->li_directory );
 		free( li->li_directory );
 		li->li_directory = strdup( cwd );
 
diff --git a/servers/slapd/bind.c b/servers/slapd/bind.c
index 8207166e7371e8d119627c7408742b0d6bcbcf1b..c72368608e9ba82a6f9543c74e72855a5f314d3c 100644
--- a/servers/slapd/bind.c
+++ b/servers/slapd/bind.c
@@ -28,10 +28,10 @@ do_bind(
 )
 {
 	BerElement	*ber = op->o_ber;
-	int		version;
-	unsigned long method;
+	ber_int_t		version;
+	ber_tag_t method;
 	char		*cdn, *ndn;
-	unsigned long	rc;
+	ber_tag_t	rc;
 	struct berval	cred;
 	Backend		*be;
 
@@ -59,7 +59,8 @@ do_bind(
 
 	{
 	BerElement	*tber;
-	unsigned long	tlen, ttag;
+	ber_len_t	tlen;
+	ber_tag_t	ttag;
 
 	tber = ber_dup( op->o_ber );
 	ttag = ber_skip_tag( tber, &tlen );
diff --git a/servers/slapd/ch_malloc.c b/servers/slapd/ch_malloc.c
index b44579697044bcd91e5c22ed2f1e66649bb34862..c1ec6f0b1c3067a1b5a3b5bafc98943a162ca1c9 100644
--- a/servers/slapd/ch_malloc.c
+++ b/servers/slapd/ch_malloc.c
@@ -13,13 +13,14 @@
 
 void *
 ch_malloc(
-    unsigned long	size
+    ber_len_t	size
 )
 {
 	void	*new;
 
-	if ( (new = (void *) malloc( size )) == NULL ) {
-		Debug( LDAP_DEBUG_ANY, "malloc of %lu bytes failed\n", size, 0, 0 );
+	if ( (new = (void *) ber_memalloc( size )) == NULL ) {
+		Debug( LDAP_DEBUG_ANY, "ch_malloc of %lu bytes failed\n",
+			(long) size, 0, 0 );
 		exit( 1 );
 	}
 
@@ -29,7 +30,7 @@ ch_malloc(
 void *
 ch_realloc(
     void		*block,
-    unsigned long	size
+    ber_len_t	size
 )
 {
 	void	*new;
@@ -38,8 +39,13 @@ ch_realloc(
 		return( ch_malloc( size ) );
 	}
 
-	if ( (new = (void *) realloc( block, size )) == NULL ) {
-		Debug( LDAP_DEBUG_ANY, "realloc of %lu bytes failed\n", size, 0, 0 );
+	if( size == 0 ) {
+		ch_free( block );
+	}
+
+	if ( (new = (void *) ber_memrealloc( block, size )) == NULL ) {
+		Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
+			(long) size, 0, 0 );
 		exit( 1 );
 	}
 
@@ -48,15 +54,15 @@ ch_realloc(
 
 void *
 ch_calloc(
-    unsigned long	nelem,
-    unsigned long	size
+    ber_len_t	nelem,
+    ber_len_t	size
 )
 {
 	void	*new;
 
-	if ( (new = (void *) calloc( nelem, size )) == NULL ) {
-		Debug( LDAP_DEBUG_ANY, "calloc of %lu elems of %lu bytes failed\n",
-		  nelem, size, 0 );
+	if ( (new = (void *) ber_memcalloc( nelem, size )) == NULL ) {
+		Debug( LDAP_DEBUG_ANY, "ch_calloc of %lu elems of %lu bytes failed\n",
+		  (long) nelem, (long) size, 0 );
 		exit( 1 );
 	}
 
@@ -70,11 +76,16 @@ ch_strdup(
 {
 	char	*new;
 
-	if ( (new = strdup( string )) == NULL ) {
-		Debug( LDAP_DEBUG_ANY, "strdup(%s) failed\n", string, 0, 0 );
+	if ( (new = ber_strdup( string )) == NULL ) {
+		Debug( LDAP_DEBUG_ANY, "ch_strdup(%s) failed\n", string, 0, 0 );
 		exit( 1 );
 	}
 
 	return( new );
 }
 
+void
+ch_free( void *ptr )
+{
+	ber_memfree( ptr );
+}
\ No newline at end of file
diff --git a/servers/slapd/connection.c b/servers/slapd/connection.c
index ad6b96c99ebc7b604a774fe1f041311752a33e1d..319b20396e6ed234a4b76cc202f05bd73bd0a7ca 100644
--- a/servers/slapd/connection.c
+++ b/servers/slapd/connection.c
@@ -16,7 +16,7 @@
 /* protected by connections_mutex */
 static ldap_pvt_thread_mutex_t connections_mutex;
 static Connection *connections = NULL;
-static long conn_nextid = 0;
+static unsigned long conn_nextid = 0;
 
 /* structure state (protected by connections_mutex) */
 #define SLAP_C_UNINITIALIZED	0x00	/* MUST BE ZERO (0) */
@@ -30,7 +30,7 @@ static long conn_nextid = 0;
 #define SLAP_C_BINDING			0x03	/* binding */
 #define SLAP_C_CLOSING			0x04	/* closing */
 
-static Connection* connection_get( int s );
+static Connection* connection_get( ber_socket_t s );
 
 static int connection_input( Connection *c );
 static void connection_close( Connection *c );
@@ -84,7 +84,7 @@ int connections_init(void)
  */
 int connections_destroy(void)
 {
-	int i;
+	ber_socket_t i;
 
 	/* should check return of every call */
 
@@ -114,7 +114,7 @@ int connections_destroy(void)
  */
 int connections_shutdown(void)
 {
-	int i;
+	ber_socket_t i;
 
 	ldap_pvt_thread_mutex_lock( &connections_mutex );
 
@@ -137,19 +137,19 @@ int connections_shutdown(void)
 	return 0;
 }
 
-static Connection* connection_get( int s )
+static Connection* connection_get( ber_socket_t s )
 {
 	/* connections_mutex should be locked by caller */
 
 	Connection *c;
 
 	Debug( LDAP_DEBUG_ARGS,
-		"connection_get(%d)\n",
-		s, 0, 0 );
+		"connection_get(%ld)\n",
+		(long) s, 0, 0 );
 
 	assert( connections != NULL );
 
-	if(s < 0) {
+	if(s == AC_SOCKET_INVALID) {
 		return NULL;
 	}
 
@@ -161,7 +161,7 @@ static Connection* connection_get( int s )
 #else
 	c = NULL;
 	{
-		int i;
+		ber_socket_t i;
 
 		for(i=0; i<dtblsize; i++) {
 			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
@@ -225,18 +225,18 @@ static void connection_return( Connection *c )
 }
 
 long connection_init(
-	int s,
+	ber_socket_t s,
 	const char* name,
 	const char* addr)
 {
-	long id;
+	unsigned long id;
 	Connection *c;
 	assert( connections != NULL );
 
-	if( s < 0 ) {
+	if( s == AC_SOCKET_INVALID ) {
         Debug( LDAP_DEBUG_ANY,
-			"connection_init(%d): invalid.\n",
-			s, 0, 0 );
+			"connection_init(%ld): invalid.\n",
+			(long) s, 0, 0 );
 		return -1;
 	}
 
@@ -252,7 +252,7 @@ long connection_init(
 
 #else
 	{
-		int i;
+		unsigned int i;
 
 		c = NULL;
 
@@ -483,9 +483,9 @@ static void connection_close( Connection *c )
 	connection_destroy( c );
 }
 
-long connections_nextid(void)
+unsigned long connections_nextid(void)
 {
-	long id;
+	unsigned long id;
 	assert( connections != NULL );
 
 	ldap_pvt_thread_mutex_lock( &connections_mutex );
@@ -497,7 +497,7 @@ long connections_nextid(void)
 	return id;
 }
 
-Connection* connection_first( int *index )
+Connection* connection_first( ber_socket_t *index )
 {
 	assert( connections != NULL );
 	assert( index != NULL );
@@ -509,7 +509,7 @@ Connection* connection_first( int *index )
 	return connection_next(NULL, index);
 }
 
-Connection* connection_next( Connection *c, int *index )
+Connection* connection_next( Connection *c, ber_socket_t *index )
 {
 	assert( connections != NULL );
 	assert( index != NULL );
@@ -569,7 +569,7 @@ static void *
 connection_operation( void *arg_v )
 {
 	struct co_arg	*arg = arg_v;
-	int tag = arg->co_op->o_tag;
+	ber_tag_t tag = arg->co_op->o_tag;
 	Connection *conn = arg->co_conn;
 
 #ifdef LDAP_COUNTERS
@@ -678,7 +678,7 @@ connection_operation( void *arg_v )
 	return NULL;
 }
 
-int connection_read(int s)
+int connection_read(ber_socket_t s)
 {
 	int rc = 0;
 	Connection *c;
@@ -691,8 +691,8 @@ int connection_read(int s)
 
 	if( c == NULL ) {
 		Debug( LDAP_DEBUG_ANY,
-			"connection_read(%d): no connection!\n",
-			s, 0, 0 );
+			"connection_read(%ld): no connection!\n",
+			(long) s, 0, 0 );
 
 		slapd_remove(s, 0);
 
@@ -746,8 +746,9 @@ connection_input(
 )
 {
 	Operation *op;
-	unsigned long	tag, len;
-	long		msgid;
+	ber_tag_t	tag;
+	ber_len_t	len;
+	ber_int_t	msgid;
 	BerElement	*ber;
 
 	if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
@@ -870,7 +871,7 @@ static int connection_op_activate( Connection *conn, Operation *op )
 	struct co_arg *arg;
 	char *tmpdn;
 	int status;
-	unsigned long tag = op->o_tag;
+	ber_tag_t tag = op->o_tag;
 
 	if ( conn->c_dn != NULL ) {
 		tmpdn = ch_strdup( conn->c_dn );
@@ -912,7 +913,7 @@ static int connection_op_activate( Connection *conn, Operation *op )
 	return status;
 }
 
-int connection_write(int s)
+int connection_write(ber_socket_t s)
 {
 	Connection *c;
 	assert( connections != NULL );
@@ -925,8 +926,8 @@ int connection_write(int s)
 
 	if( c == NULL ) {
 		Debug( LDAP_DEBUG_ANY,
-			"connection_write(%d): no connection!\n",
-			s, 0, 0 );
+			"connection_write(%ld): no connection!\n",
+			(long) s, 0, 0 );
 		slapd_remove(s, 0);
 		ldap_pvt_thread_mutex_unlock( &connections_mutex );
 		return -1;
diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c
index ffbf9ea1c7371abe7a3de507c722aa68d0f981f8..eb26b8feded137a5058ee9e6029907d0e708c733 100644
--- a/servers/slapd/daemon.c
+++ b/servers/slapd/daemon.c
@@ -21,12 +21,13 @@ int deny_severity = LOG_NOTICE;
 #endif /* TCP Wrappers */
 
 /* globals */
-int dtblsize;
-static int tcps;
+ber_socket_t dtblsize;
+static ber_socket_t tcps;
 
 #ifdef HAVE_WINSOCK2
 /* in nt_main.c */
 extern ldap_pvt_thread_cond_t			started_event;
+
 /* forward reference */
 void hit_socket();
 /* In wsa_err.c */
@@ -78,7 +79,7 @@ struct slap_daemon {
 /*
  * Add a descriptor to daemon control
  */
-static void slapd_add(int s) {
+static void slapd_add(ber_socket_t s) {
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
 	assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
@@ -91,10 +92,11 @@ static void slapd_add(int s) {
 	}
 #endif
 
-	FD_SET( (unsigned) s, &slap_daemon.sd_actives );
-	FD_SET( (unsigned) s, &slap_daemon.sd_readers );
+	FD_SET( s, &slap_daemon.sd_actives );
+	FD_SET( s, &slap_daemon.sd_readers );
 
-	Debug( LDAP_DEBUG_CONNS, "daemon: added %d%s%s\n", s,
+	Debug( LDAP_DEBUG_CONNS, "daemon: added %ld%s%s\n",
+		(long) s,
 	    FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
 		FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
 
@@ -104,27 +106,28 @@ static void slapd_add(int s) {
 /*
  * Remove the descriptor from daemon control
  */
-void slapd_remove(int s, int wake) {
+void slapd_remove(ber_socket_t s, int wake) {
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 	WAKE_LISTENER(wake);
 
-	Debug( LDAP_DEBUG_CONNS, "daemon: removing %d%s%s\n", s,
+	Debug( LDAP_DEBUG_CONNS, "daemon: removing %ld%s%s\n",
+		(long) s,
 	    FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
 		FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
 
-	FD_CLR( (unsigned) s, &slap_daemon.sd_actives );
-	FD_CLR( (unsigned) s, &slap_daemon.sd_readers );
-	FD_CLR( (unsigned) s, &slap_daemon.sd_writers );
+	FD_CLR( s, &slap_daemon.sd_actives );
+	FD_CLR( s, &slap_daemon.sd_readers );
+	FD_CLR( s, &slap_daemon.sd_writers );
 
 	ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
 }
 
-void slapd_clr_write(int s, int wake) {
+void slapd_clr_write(ber_socket_t s, int wake) {
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 	WAKE_LISTENER(wake);
 
-	assert( FD_ISSET( (unsigned) s, &slap_daemon.sd_actives) );
-	FD_CLR( (unsigned) s, &slap_daemon.sd_writers );
+	assert( FD_ISSET( s, &slap_daemon.sd_actives) );
+	FD_CLR( s, &slap_daemon.sd_writers );
 
 	ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
 
@@ -133,7 +136,7 @@ void slapd_clr_write(int s, int wake) {
 	}
 }
 
-void slapd_set_write(int s, int wake) {
+void slapd_set_write(ber_socket_t s, int wake) {
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
     WAKE_LISTENER(wake);
 
@@ -147,12 +150,12 @@ void slapd_set_write(int s, int wake) {
 	}
 }
 
-void slapd_clr_read(int s, int wake) {
+void slapd_clr_read(ber_socket_t s, int wake) {
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
     WAKE_LISTENER(wake);
 
 	assert( FD_ISSET( s, &slap_daemon.sd_actives) );
-	FD_CLR( (unsigned) s, &slap_daemon.sd_readers );
+	FD_CLR( s, &slap_daemon.sd_readers );
 
 	ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
 
@@ -161,12 +164,12 @@ void slapd_clr_read(int s, int wake) {
 	}
 }
 
-void slapd_set_read(int s, int wake) {
+void slapd_set_read(ber_socket_t s, int wake) {
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
     WAKE_LISTENER(wake);
 
 	assert( FD_ISSET( s, &slap_daemon.sd_actives) );
-	FD_SET( (unsigned) s, &slap_daemon.sd_readers );
+	FD_SET( s, &slap_daemon.sd_readers );
 
 	ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
 
@@ -175,8 +178,9 @@ void slapd_set_read(int s, int wake) {
 	}
 }
 
-static void slapd_close(int s) {
-	Debug( LDAP_DEBUG_CONNS, "daemon: closing %d\n", s, 0, 0 );
+static void slapd_close(ber_socket_t s) {
+	Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
+		(long) s, 0, 0 );
 	tcp_close(s);
 }
 
@@ -185,7 +189,8 @@ static void slapd_close(int s) {
 int
 set_socket( struct sockaddr_in *addr )
 {
-	int	tcps = -1;
+	ber_socket_t	tcps = AC_SOCKET_INVALID;
+
     if ( !daemon_initialized ) sockinit();
 
 #ifdef HAVE_SYSCONF
@@ -205,7 +210,7 @@ set_socket( struct sockaddr_in *addr )
 	if( addr != NULL ) {
 		int	tmp;
 
-		if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
+		if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID ) {
 #ifndef HAVE_WINSOCK
 			int err = errno;
 			Debug( LDAP_DEBUG_ANY,
@@ -224,9 +229,9 @@ set_socket( struct sockaddr_in *addr )
 #ifndef HAVE_WINSOCK
 		if ( tcps >= dtblsize ) {
 			Debug( LDAP_DEBUG_ANY,
-				"daemon: listener descriptor %d is too great\n",
-				tcps, dtblsize, 0 );
-			return -1;
+				"daemon: listener descriptor %ld is too great %ld\n",
+				(long) tcps, (long) dtblsize, 0 );
+			return( -1);
 		}
 #endif
 
@@ -237,8 +242,8 @@ set_socket( struct sockaddr_in *addr )
 		{
 			int err = errno;
 			Debug( LDAP_DEBUG_ANY,
-			       "slapd(%d): setsockopt() failed errno %d (%s)\n",
-		    	tcps, err,
+			       "slapd(%ld): setsockopt() failed errno %d (%s)\n",
+		    	(long) tcps, err,
 				err > -1 && err < sys_nerr
 					? sys_errlist[err] : "unknown" );
 		}
@@ -250,8 +255,8 @@ set_socket( struct sockaddr_in *addr )
 		{
 			int err = errno;
 			Debug( LDAP_DEBUG_ANY,
-				"slapd(%d): setsockopt(KEEPALIVE) failed errno %d (%s)\n",
-		    	tcps, err,
+				"slapd(%ld): setsockopt(KEEPALIVE) failed errno %d (%s)\n",
+		    	(long) tcps, err,
 				err > -1 && err < sys_nerr
 					? sys_errlist[err] : "unknown" );
 		}
@@ -260,8 +265,8 @@ set_socket( struct sockaddr_in *addr )
 
 		if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) {
 			int err = errno;
-			Debug( LDAP_DEBUG_ANY, "daemon: bind(%d) failed errno %d (%s)\n",
-		    	tcps, err,
+			Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno %d (%s)\n",
+		    	(long) tcps, err,
 				err > -1 && err < sys_nerr
 					? sys_errlist[err] : "unknown" );
 			return -1;
@@ -281,7 +286,6 @@ slapd_daemon_task(
 	struct sockaddr_in *slapd_addr = args->addr;
 
 	tcps  = args->tcps;
-	/*free( ptr );  This seems to be wrong unless I hosed something */
 
 	inetd = ( slapd_addr == NULL);
     if ( !daemon_initialized ) sockinit();
@@ -296,8 +300,8 @@ slapd_daemon_task(
 		if ( listen( tcps, 5 ) == -1 ) {
 			int err = errno;
 			Debug( LDAP_DEBUG_ANY,
-				"daemon: listen(%d, 5) failed errno %d (%s)\n",
-			    tcps, err,
+				"daemon: listen(%ld, 5) failed errno %d (%s)\n",
+			    (long) tcps, err,
 				err > -1 && err < sys_nerr
 					? sys_errlist[err] : "unknown" );
 			return( (void*)-1 );
@@ -306,7 +310,7 @@ slapd_daemon_task(
 		slapd_add( tcps );
 
 	} else {
-		if( connection_init( 0, NULL, NULL ) ) {
+		if( connection_init( (ber_socket_t) 0, NULL, NULL ) ) {
 			Debug( LDAP_DEBUG_ANY,
 				"connection_init(%d) failed.\n",
 				0, 0, 0 );
@@ -321,9 +325,11 @@ slapd_daemon_task(
 		ldap_pvt_thread_cond_signal( &started_event );
 #endif
 	/* initialization complete. Here comes the loop. */
+
 	while ( !slapd_shutdown ) {
-		unsigned int i;
-		int ns, nfds;
+		ber_socket_t i;
+		int ns;
+		ber_socket_t nfds;
 		int ebadf = 0;
 #define SLAPD_EBADF_LIMIT 10
 
@@ -428,17 +434,17 @@ slapd_daemon_task(
 		}
 
 		if ( FD_ISSET( tcps, &readfds ) ) {
-			int s;
+			ber_int_t s;
 			int len = sizeof(from);
 			long id;
 
 			if ( (s = accept( tcps,
-				(struct sockaddr *) &from, &len )) == -1 )
+				(struct sockaddr *) &from, &len )) == AC_SOCKET_INVALID )
 			{
 				int err = errno;
 				Debug( LDAP_DEBUG_ANY,
-				    "daemon: accept(%d) failed errno %d (%s)\n", err,
-				    tcps, err >= 0 && err < sys_nerr ?
+				    "daemon: accept(%ld) failed errno %d (%s)\n", err,
+				    (long) tcps, err >= 0 && err < sys_nerr ?
 				    sys_errlist[err] : "unknown");
 				continue;
 			}
@@ -459,15 +465,15 @@ slapd_daemon_task(
 			/* make sure descriptor number isn't too great */
 			if ( s >= dtblsize ) {
 				Debug( LDAP_DEBUG_ANY,
-					"daemon: %d beyond descriptor table size %d\n",
-					s, dtblsize, 0 );
+					"daemon: %ld beyond descriptor table size %ld\n",
+					(long) s, (long) dtblsize, 0 );
 				slapd_close(s);
 				continue;
 			}
 #endif
 		   
-			Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %d\n",
-				s, 0, 0 );
+			Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %ld\n",
+				(long) s, 0, 0 );
 
 			len = sizeof(from);
 			if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
@@ -507,8 +513,8 @@ slapd_daemon_task(
 			{
 				/* DENY ACCESS */
 				Statslog( LDAP_DEBUG_ANY,
-			   	 "fd=%d connection from %s (%s) denied.\n",
-			   	 	s,
+			   	 "fd=%ld connection from %s (%s) denied.\n",
+			   	 	(long) s,
 					client_name == NULL ? "unknown" : client_name,
 					client_addr == NULL ? "unknown" : client_addr,
 			   	  0, 0 );
@@ -520,8 +526,8 @@ slapd_daemon_task(
 
 			if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
 				Debug( LDAP_DEBUG_ANY,
-					"daemon: connection_init(%d, %s, %s) failed.\n",
-					s,
+					"daemon: connection_init(%ld, %s, %s) failed.\n",
+					(long) s,
 					client_name == NULL ? "unknown" : client_name,
 					client_addr == NULL ? "unknown" : client_addr);
 				slapd_close(s);
@@ -529,8 +535,8 @@ slapd_daemon_task(
 			}
 
 			Statslog( LDAP_DEBUG_STATS,
-				"daemon: conn=%d fd=%d connection from %s (%s) accepted.\n",
-				id, s,
+				"daemon: conn=%d fd=%ld connection from %s (%s) accepted.\n",
+				id, (long) s,
 				client_name == NULL ? "unknown" : client_name,
 				client_addr == NULL ? "unknown" : client_addr,
 				0 );
@@ -570,7 +576,7 @@ slapd_daemon_task(
 		for ( i = 0; i < nfds; i++ )
 #endif
 		{
-			int wd;
+			ber_socket_t wd;
 
 #ifdef HAVE_WINSOCK
 			wd = writefds.fd_array[i];
@@ -607,7 +613,7 @@ slapd_daemon_task(
 		for ( i = 0; i < nfds; i++ )
 #endif
 		{
-			int rd;
+			ber_socket_t rd;
 
 #ifdef HAVE_WINSOCK
 			rd = readfds.fd_array[i];
@@ -747,11 +753,12 @@ void sockinit()
 
 void hit_socket()
 {
-	int s, on = 1;
+	ber_socket_t s;
+	int on = 1;
 	extern struct sockaddr_in	bind_addr;
 
 	/* throw something at the socket to terminate the select() in the daemon thread. */
-	if (( s = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET )
+	if (( s = socket( AF_INET, SOCK_STREAM, 0 )) == AC_SOCKET_INVALID )
 		Debug( LDAP_DEBUG_ANY,
 			"slap_set_shutdown: socket failed\n\tWSAGetLastError=%d (%s)\n",
 			WSAGetLastError(), WSAGetLastErrorString(), 0 );
@@ -784,7 +791,6 @@ void sockinit()
 void sockinit()
 {
     daemon_initialized = 1;
-    return;
 }
 #endif
 
diff --git a/servers/slapd/filter.c b/servers/slapd/filter.c
index 2a977940e18a4b5f4c3510a0bba60e0ef6edc3a9..5176842df14fad902a6147f884cfd0b8f85e7fd2 100644
--- a/servers/slapd/filter.c
+++ b/servers/slapd/filter.c
@@ -187,7 +187,8 @@ get_filter_list( Connection *conn, BerElement *ber, Filter **f, char **fstr )
 {
 	Filter		**new;
 	int		err;
-	unsigned long	tag, len;
+	ber_tag_t	tag;
+	ber_len_t	len;
 	char		*last, *ftmp;
 
 	Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
@@ -227,7 +228,9 @@ get_substring_filter(
     char	**fstr
 )
 {
-	unsigned long	tag, len, rc;
+	ber_tag_t	tag;
+	ber_len_t	len;
+	ber_tag_t	rc;
 	char		*val, *last;
 	int		syntax;
 
@@ -238,7 +241,7 @@ get_substring_filter(
 		(void) ber_skip_tag( ber, &len );
 	}
 #endif
-	if ( ber_scanf( ber, "{a", &f->f_sub_type ) == LBER_ERROR ) {
+	if ( ber_scanf( ber, "{a" /*}*/, &f->f_sub_type ) == LBER_ERROR ) {
 		return( LDAP_PROTOCOL_ERROR );
 	}
 	attr_normalize( f->f_sub_type );
diff --git a/servers/slapd/main.c b/servers/slapd/main.c
index 23e9ce060fdc11b56dbea1394fb5dc830f75e945..8850e54a2f0e0b83beb96ec03825a3f85c0af1f6 100644
--- a/servers/slapd/main.c
+++ b/servers/slapd/main.c
@@ -13,9 +13,7 @@
 
 #include "ldap_defaults.h"
 #include "slap.h"
-#include "lutil.h"			/* Get lutil_detach() */
-
-int set_socket( struct sockaddr_in *addr );
+#include "lutil.h"
 
 #ifdef LDAP_SIGCHLD
 static RETSIGTYPE wait4child( int sig );
@@ -96,7 +94,7 @@ usage( char *name )
 	fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
     fprintf( stderr, "\n        [-a bind-address] [-i] [-u]" );
 #ifdef HAVE_WINSOCK
-	fprintf( stderr, " [-v NTserviceName]" );
+	fprintf( stderr, " [-n NTserviceName]" );
 #endif
 #if LDAP_CONNECTIONLESS
 	fprintf( stderr, " [-c]" );
@@ -115,16 +113,14 @@ usage( char *name )
 
 time_t starttime;
 struct sockaddr_in	bind_addr;
-int tcps;
+ber_int_t tcps;
 
 #ifdef HAVE_WINSOCK
 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
-{
 #else
 int main( int argc, char **argv )
-{
 #endif
-
+{
 	int		i;
 	int		inetd = 0;
 	int		rc;
@@ -181,7 +177,6 @@ int main( int argc, char **argv )
 			Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
 		}
 	}
-		
 #endif
 
 	while ( (i = getopt( argc, argv,
@@ -199,7 +194,7 @@ int main( int argc, char **argv )
 				 "c"
 #endif
 #ifdef HAVE_WINSOCK
-				 "v:"
+				 "n:"
 #endif
 			     )) != EOF ) {
 		switch ( i ) {
@@ -308,7 +303,7 @@ int main( int argc, char **argv )
 			break;
 #endif /* SETUID && GETUID */
 #ifdef HAVE_WINSOCK
-		case 'v':  /* NT service name */
+		case 'n':  /* NT service name */
 			NTservice = ch_strdup( optarg );
 			break;
 #endif
@@ -355,7 +350,6 @@ int main( int argc, char **argv )
 		goto destroy;
 	}
 
-
 	tcps = set_socket( inetd ? NULL : &bind_addr );
 	if ( tcps == -1 )
 		goto destroy;
@@ -373,7 +367,7 @@ int main( int argc, char **argv )
 #ifdef LDAP_SIGCHLD
 	(void) SIGNAL( LDAP_SIGCHLD, wait4child );
 #endif
-#ifdef HAVE_WINSOCK
+#ifdef SIGBREAK
 	/* SIGBREAK is generated when Ctrl-Break is pressed. */
 	(void) SIGNAL( SIGBREAK, slap_set_shutdown );
 #endif
@@ -436,7 +430,6 @@ int main( int argc, char **argv )
 		ldap_pvt_thread_cond_destroy( &started_event );
 #endif
 
-
 shutdown:
 	/* remember an error during shutdown */
 	rc |= slap_shutdown(-1);
diff --git a/servers/slapd/modify.c b/servers/slapd/modify.c
index 53b4ac7680e208f7dc8a4e393df1a4c44ae23f2e..345dc5f36b7146e8669c2c7ac1e8af27e0b8a3ec 100644
--- a/servers/slapd/modify.c
+++ b/servers/slapd/modify.c
@@ -30,7 +30,8 @@ do_modify(
 {
 	char		*ndn;
 	char		*last;
-	unsigned long	tag, len;
+	ber_tag_t	tag;
+	ber_len_t	len;
 	LDAPModList	*modlist;
 	LDAPModList	**modtail;
 #ifdef LDAP_DEBUG
@@ -77,9 +78,11 @@ do_modify(
 	    tag != LBER_DEFAULT;
 	    tag = ber_next_element( op->o_ber, &len, last ) )
 	{
+		ber_int_t mop;
+
 		(*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
 
-		if ( ber_scanf( op->o_ber, "{i{a[V]}}", &(*modtail)->ml_op,
+		if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
 		    &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
 		    == LBER_ERROR )
 		{
@@ -92,6 +95,8 @@ do_modify(
 			return;
 		}
 
+		(*modtail)->ml_op = mop;
+		
 		if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
 		    (*modtail)->ml_op != LDAP_MOD_DELETE &&
 		    (*modtail)->ml_op != LDAP_MOD_REPLACE )
diff --git a/servers/slapd/modrdn.c b/servers/slapd/modrdn.c
index b1c5e1a551d593e86f14542c943a7d44e608197a..66b7f93f782581ecd72acf262d5e3c848151817f 100644
--- a/servers/slapd/modrdn.c
+++ b/servers/slapd/modrdn.c
@@ -39,13 +39,13 @@ do_modrdn(
 )
 {
 	char	*ndn, *newrdn;
-	int	deloldrdn;
+	ber_int_t	deloldrdn;
 	Backend	*be;
 	/* Vars for LDAP v3 newSuperior support */
 	char	*newSuperior = NULL;
 	char    *nnewSuperior = NULL;
 	Backend	*newSuperior_be = NULL;
-	unsigned long	length;
+	ber_len_t	length;
 
 	Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
 
@@ -100,7 +100,7 @@ do_modrdn(
 		}/* else if ( conn->c_protocol ==  0 ) */
 
 
-		if ( ber_scanf( op->o_ber, "a}", &newSuperior ) 
+		if ( ber_scanf( op->o_ber, /*{*/ "a}", &newSuperior ) 
 		     == LBER_ERROR ) {
 
 		    Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\"}) failed\n",
@@ -109,7 +109,7 @@ do_modrdn(
 				      "" );
 		    return;
 
-		}/* if ( ber_scanf( ber, "a}", &newSuperior ) == ... ) */
+		}/* if ( ber_scanf( ber, { "a}", &newSuperior ) == ... ) */
 
 
 		Debug( LDAP_DEBUG_ARGS, "do_modrdn: newSuperior=(%s)\n",
diff --git a/servers/slapd/monitor.c b/servers/slapd/monitor.c
index b2ea44db1d519e6db12be391a7cbc44e3de91434..eccdcbec12c2efe885166f41cf8bb06341a02cad 100644
--- a/servers/slapd/monitor.c
+++ b/servers/slapd/monitor.c
@@ -129,7 +129,7 @@ monitor_info( Connection *conn, Operation *op )
 	val.bv_len = strlen( buf );
 	attr_merge( e, "totalconnections", vals );
 
-	sprintf( buf, "%d", dtblsize );
+	sprintf( buf, "%ld", (long) dtblsize );
 	val.bv_val = buf;
 	val.bv_len = strlen( buf );
 	attr_merge( e, "dtablesize", vals );
diff --git a/servers/slapd/operation.c b/servers/slapd/operation.c
index 44c821845ecbde47745c3dafbba682c736fcdae5..6f6ab510ef6ce7feedb074f974a16562af971f8a 100644
--- a/servers/slapd/operation.c
+++ b/servers/slapd/operation.c
@@ -33,9 +33,9 @@ slap_op_free( Operation *op )
 Operation *
 slap_op_alloc(
     BerElement		*ber,
-    unsigned long	msgid,
-    unsigned long	tag,
-    long			id
+    ber_int_t	msgid,
+    ber_tag_t	tag,
+    ber_int_t	id
 )
 {
 	Operation	*op;
diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h
index 517875c2e831c4223787a2e860c230f5e436d1ee..dba1e5debea7a5c65407842f04cf72c61d4106b0 100644
--- a/servers/slapd/proto-slap.h
+++ b/servers/slapd/proto-slap.h
@@ -3,6 +3,8 @@
 
 #include <ldap_cdefs.h>
 
+LDAP_BEGIN_DECL
+
 /*
  * acl.c
  */
@@ -96,10 +98,12 @@ extern int	backend_group LDAP_P((Backend *be,
  * ch_malloc.c
  */
 
-void * ch_malloc LDAP_P(( unsigned long size ));
-void * ch_realloc LDAP_P(( void *block, unsigned long size ));
-void * ch_calloc LDAP_P(( unsigned long nelem, unsigned long size ));
+void * ch_malloc LDAP_P(( ber_len_t size ));
+void * ch_realloc LDAP_P(( void *block, ber_len_t size ));
+void * ch_calloc LDAP_P(( ber_len_t nelem, ber_len_t size ));
 char * ch_strdup LDAP_P(( const char *string ));
+void   ch_free LDAP_P(( void * ));
+#define free ch_free
 
 /*
  * charray.c
@@ -126,19 +130,19 @@ int connections_shutdown LDAP_P((void));
 int connections_destroy LDAP_P((void));
 
 long connection_init LDAP_P((
-	int s,
+	ber_socket_t s,
 	const char* name, const char* addr));
 
 void connection_closing LDAP_P(( Connection *c ));
 int connection_state_closing LDAP_P(( Connection *c ));
 
-int connection_write LDAP_P((int s));
-int connection_read LDAP_P((int s));
+int connection_write LDAP_P((ber_socket_t s));
+int connection_read LDAP_P((ber_socket_t s));
 
-long connections_nextid(void);
+unsigned long connections_nextid(void);
 
-Connection* connection_first LDAP_P((int *));
-Connection* connection_next LDAP_P((Connection *, int *));
+Connection* connection_first LDAP_P((ber_socket_t *));
+Connection* connection_next LDAP_P((Connection *, ber_socket_t *));
 void connection_done LDAP_P((Connection *));
 
 /*
@@ -202,8 +206,8 @@ void monitor_info LDAP_P(( Connection *conn, Operation *op ));
 
 void slap_op_free LDAP_P(( Operation *op ));
 Operation * slap_op_alloc LDAP_P((
-	BerElement *ber, unsigned long msgid,
-	unsigned long tag, long id ));
+	BerElement *ber, ber_int_t msgid,
+	ber_tag_t tag, ber_int_t id ));
 
 int slap_op_add LDAP_P(( Operation **olist, Operation *op ));
 int slap_op_remove LDAP_P(( Operation **olist, Operation *op ));
@@ -344,14 +348,16 @@ struct slapd_args {
 	struct sockaddr_in *addr;
 	int tcps;
 };
+
 extern int	slapd_daemon LDAP_P((struct slapd_args *args));
+extern int	set_socket LDAP_P((struct sockaddr_in *addr));
 
-extern void slapd_set_write LDAP_P((int s, int wake));
-extern void slapd_clr_write LDAP_P((int s, int wake));
-extern void slapd_set_read LDAP_P((int s, int wake));
-extern void slapd_clr_read LDAP_P((int s, int wake));
+extern void slapd_set_write LDAP_P((ber_socket_t s, int wake));
+extern void slapd_clr_write LDAP_P((ber_socket_t s, int wake));
+extern void slapd_set_read LDAP_P((ber_socket_t s, int wake));
+extern void slapd_clr_read LDAP_P((ber_socket_t s, int wake));
 
-extern void slapd_remove LDAP_P((int s, int wake));
+extern void slapd_remove LDAP_P((ber_socket_t s, int wake));
 
 extern void	slap_set_shutdown LDAP_P((int sig));
 extern void	slap_do_nothing   LDAP_P((int sig));
@@ -371,7 +377,9 @@ extern void	do_unbind LDAP_P((Connection *conn, Operation *op));
 extern int send_search_entry LDAP_P((Backend *be, Connection *conn, Operation *op, Entry *e, char **attrs, int attrsonly));
 extern int str2result LDAP_P(( char *s, int *code, char **matched, char **info ));
 
-extern int dtblsize;
+extern ber_socket_t dtblsize;
+
+LDAP_END_DECL
 
 #endif /* _proto_slap */
 
diff --git a/servers/slapd/result.c b/servers/slapd/result.c
index 1726dd3b841ea60fde96b53928128dffa4ef7eff..901afcc012020cc5a49c7ee9ea8bbf6e1ec8cd83 100644
--- a/servers/slapd/result.c
+++ b/servers/slapd/result.c
@@ -20,7 +20,7 @@ static void
 send_ldap_result2(
     Connection	*conn,
     Operation	*op,
-    int		err,
+    ber_int_t	err,
     char	*matched,
     char	*text,
     int		nentries
@@ -28,7 +28,8 @@ send_ldap_result2(
 {
 	BerElement	*ber;
 	int		rc;
-	unsigned long	tag, bytes;
+	ber_tag_t	tag;
+	ber_len_t	bytes;
 
 	if ( err == LDAP_PARTIAL_RESULTS && (text == NULL || *text == '\0') )
 		err = LDAP_NO_SUCH_OBJECT;
@@ -57,12 +58,11 @@ send_ldap_result2(
 
 
 #ifdef LDAP_COMPAT30
-	if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
-	    == NULLBER )
+	ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER );
 #else
-	if ( (ber = der_alloc()) == NULLBER )
+	ber = der_alloc();
 #endif
-	{
+	if ( ber == NULL ) {
 		Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
 		return;
 	}
@@ -145,8 +145,9 @@ send_ldap_result2(
 	ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
 
 	Statslog( LDAP_DEBUG_STATS,
-	    "conn=%d op=%d RESULT err=%d tag=%lu nentries=%d\n", conn->c_connid,
-	    op->o_opid, err, tag, nentries );
+	    "conn=%ld op=%ld RESULT err=%ld tag=%lu nentries=%d\n",
+		(long) conn->c_connid, (long) op->o_opid,
+		(long) err, (long) tag, nentries );
 
 	return;
 }
@@ -155,7 +156,7 @@ void
 send_ldap_result(
     Connection	*conn,
     Operation	*op,
-    int		err,
+    ber_int_t	err,
     char	*matched,
     char	*text
 )
@@ -177,7 +178,7 @@ void
 send_ldap_search_result(
     Connection	*conn,
     Operation	*op,
-    int		err,
+    ber_int_t	err,
     char	*matched,
     char	*text,
     int		nentries
@@ -215,12 +216,12 @@ send_search_entry(
 	edn = e->e_ndn;
 
 #ifdef LDAP_COMPAT30
-	if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
-		== NULLBER )
+	ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER );
 #else
-	if ( (ber = der_alloc()) == NULLBER )
+	ber = der_alloc();
 #endif
-	{
+
+	if ( ber == NULL ) {
 		Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
 		send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
 			"ber_alloc" );
@@ -273,7 +274,7 @@ send_search_entry(
 			continue;
 		}
 
-		if (( rc = ber_printf( ber, "{s[", a->a_type )) == -1 ) {
+		if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
 			Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
 			ber_free( ber, 1 );
 			send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
@@ -290,10 +291,7 @@ send_search_entry(
 					continue;
 				}
 
-				if (( rc = ber_printf( ber, "o",
-				    a->a_vals[i]->bv_val,
-				    a->a_vals[i]->bv_len )) == -1 )
-				{
+				if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
 					Debug( LDAP_DEBUG_ANY,
 					    "ber_printf failed\n", 0, 0, 0 );
 					ber_free( ber, 1 );
@@ -305,7 +303,7 @@ send_search_entry(
 			}
 		}
 
-		if (( rc = ber_printf( ber, "]}" )) == -1 ) {
+		if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
 			Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
 			ber_free( ber, 1 );
 			send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
@@ -316,10 +314,10 @@ send_search_entry(
 
 #ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
-		rc = ber_printf( ber, "}}}}" );
+		rc = ber_printf( ber, /*{{{{{*/ "}}}}" );
 	} else
 #endif
-		rc = ber_printf( ber, "}}}" );
+		rc = ber_printf( ber, /*{{{{*/ "}}}" );
 
 	if ( rc == -1 ) {
 		Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
@@ -387,8 +385,8 @@ send_search_entry(
 	num_entries_sent++;
 	ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
 
-	Statslog( LDAP_DEBUG_STATS2, "conn=%d op=%d ENTRY dn=\"%s\"\n",
-	    conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
+	Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
+	    (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
 
 	Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
 
diff --git a/servers/slapd/schema.c b/servers/slapd/schema.c
index 829e4f639674fe5e2897e8430fe90fa822fcba7c..97c80be9b1481e0bd09119f03e5ba50c5839abe6 100644
--- a/servers/slapd/schema.c
+++ b/servers/slapd/schema.c
@@ -924,8 +924,6 @@ int
 schema_init( void )
 {
 	int		res;
-	int		code;
-	const char	*err;
 	int		i;
 	static int	schema_init_done = 0;
 
diff --git a/servers/slapd/search.c b/servers/slapd/search.c
index a6772d6757de822ef2fb392c27070ea4244d1d92..584a7b8882834578f1b4346d9166461d3bab1ff3 100644
--- a/servers/slapd/search.c
+++ b/servers/slapd/search.c
@@ -28,8 +28,8 @@ do_search(
 )
 {
 	int		i, err;
-	int		scope, deref, attrsonly;
-	int		sizelimit, timelimit;
+	ber_int_t		scope, deref, attrsonly;
+	ber_int_t		sizelimit, timelimit;
 	char		*base = NULL, *fstr = NULL;
 	Filter		*filter = NULL;
 	char		**attrs = NULL;
@@ -88,7 +88,7 @@ do_search(
 	Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr, 0, 0 );
 
 	/* attributes */
-	if ( ber_scanf( op->o_ber, "{v}}", &attrs ) == LBER_ERROR ) {
+	if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
 		send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
 		goto return_results;
 	}
diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h
index 6c5a3ce7e4093619cadc4ba8056a8c8d61c064f0..cd89c3687fa47396ecdd300347ce15b8dac74c80 100644
--- a/servers/slapd/slap.h
+++ b/servers/slapd/slap.h
@@ -9,7 +9,6 @@
 #include <ac/syslog.h>
 #include <ac/regex.h>
 #include <ac/socket.h>
-#include <ldap_schema.h>
 
 #include "avl.h"
 
@@ -23,8 +22,13 @@
 #include "lber.h"
 #include "ldap.h"
 
+#include "ldap_schema.h"
+
 #include "ldap_pvt_thread.h"
 #include "ldif.h"
+
+LDAP_BEGIN_DECL
+
 #ifdef f_next
 #undef f_next /* name conflict between sys/file.h on SCO and struct filter */
 #endif
@@ -67,8 +71,6 @@
 #define SLAP_SCHERR_SYN_NOT_FOUND	11
 #define SLAP_SCHERR_MR_INCOMPLETE	12
 
-LDAP_BEGIN_DECL
-
 extern int slap_debug;
 
 struct slap_op;
@@ -86,7 +88,7 @@ typedef struct ava {
  * represents a search filter
  */
 typedef struct filter {
-	unsigned long	f_choice;	/* values taken from ldap.h */
+	ber_tag_t	f_choice;	/* values taken from ldap.h */
 
 	union f_un_u {
 		/* present */
@@ -147,7 +149,7 @@ typedef struct attr {
  * the id used in the indexes to refer to an entry
  */
 typedef unsigned long	ID;
-#define NOID	((unsigned long)-1)
+#define NOID	((unsigned long)~0)
 
 /*
  * represents an entry in core
@@ -426,8 +428,10 @@ struct backend_info {
 		struct slap_conn *c, struct slap_op *o ));
 	int	(*bi_op_search) LDAP_P((BackendDB *bd,
 		struct slap_conn *c, struct slap_op *o,
-		char *base, int scope, int deref, int slimit, int tlimit,
-		Filter *f, char *filterstr, char **attrs, int attrsonly));
+		char *base, int scope, int deref,
+		int slimit, int tlimit,
+		Filter *f, char *filterstr, char **attrs,
+		int attrsonly));
 	int	(*bi_op_compare)LDAP_P((BackendDB *bd,
 		struct slap_conn *c, struct slap_op *o,
 		char *dn, Ava *ava));
@@ -447,7 +451,7 @@ struct backend_info {
 	/* Bug: be_op_abandon in unused! */
 	int	(*bi_op_abandon) LDAP_P((BackendDB *bd,
 		struct slap_conn *c, struct slap_op *o,
-		int msgid));
+		ber_int_t msgid));
 
 	/* Auxilary Functions */
 	int	(*bi_entry_release_rw) LDAP_P((BackendDB *bd, Entry *e, int rw));
@@ -466,18 +470,18 @@ struct backend_info {
  */
 
 typedef struct slap_op {
-	long	o_opid;		/* id of this operation		  */
-	long	o_msgid;	/* msgid of the request		  */
+	ber_int_t	o_opid;		/* id of this operation		  */
+	ber_int_t	o_msgid;	/* msgid of the request		  */
 
 	ldap_pvt_thread_t	o_tid;		/* thread handling this op	  */
 
 	BerElement	*o_ber;		/* ber of the request		  */
 
-	unsigned long	o_tag;		/* tag of the request		  */
+	ber_tag_t	o_tag;		/* tag of the request		  */
 	time_t		o_time;		/* time op was initiated	  */
 	char		*o_dn;		/* dn bound when op was initiated */
 	char		*o_ndn;		/* normalized dn bound when op was initiated */
-	int			o_authtype;	/* auth method used to bind dn	  */
+	ber_tag_t	o_authtype;	/* auth method used to bind dn	  */
 					/* values taken from ldap.h	  */
 					/* LDAP_AUTH_*			  */
 
@@ -510,15 +514,16 @@ typedef struct slap_conn {
 	/* only can be changed by connect_init */
 	time_t		c_starttime;	/* when the connection was opened */
 	time_t		c_activitytime;	/* when the connection was last used */
-	long		c_connid;	/* id of this connection for stats*/
+	unsigned long		c_connid;	/* id of this connection for stats*/
 	char		*c_client_addr;	/* address of client */
 	char		*c_client_name;	/* name of client */
 
 	/* only can be changed by binding thread */
 	char	*c_cdn;		/* DN provided by the client */
 	char	*c_dn;		/* DN bound to this conn  */
-	int		c_protocol;	/* version of the LDAP protocol used by client */
-	int		c_authtype;	/* auth method used to bind c_dn  */
+	ber_int_t	c_protocol;	/* version of the LDAP protocol used by client */
+	ber_tag_t	c_authtype;	/* auth method used to bind c_dn  */
+
 #ifdef LDAP_COMPAT
 	int		c_version;	/* for compatibility w/ U-Mich 2.0 & 3.0 */
 #endif
@@ -551,8 +556,8 @@ typedef struct slap_conn {
 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )
 #endif
 
-#include "proto-slap.h"
-
 LDAP_END_DECL
 
+#include "proto-slap.h"
+
 #endif /* _slap_h_ */
diff --git a/servers/slapd/tools/ldapsyntax.c b/servers/slapd/tools/ldapsyntax.c
index 99fca405bef9b00afc90f833613f462a2b01a8fa..542792b231a9bdbac6d08e413aa3e379c1a5431c 100644
--- a/servers/slapd/tools/ldapsyntax.c
+++ b/servers/slapd/tools/ldapsyntax.c
@@ -417,8 +417,10 @@ photo2ldif( PS ps, AttributeValue av )
 	if (( phber = der_alloc()) == NULLBER ) {
 	    return( -1 );
 	}
-	if ( ber_printf( phber, "t{[tB]{B}}", 0xA3, 0x81, faxparamset,
-		31, (char *)pe->pe_prim, len * 8 ) == -1 ) {
+	if ( ber_printf( phber, "t{[tB]{B}}",
+		(ber_tag_t) 0xA3, (ber_tag_t) 0x81, faxparamset, (ber_len_t) 31,
+		(char *)pe->pe_prim, (ber_len_t) (len * 8) ) == -1 )
+	{
 	    ber_free( phber, 1 );
 	    return( -1 );
 	}
diff --git a/servers/slurpd/ch_malloc.c b/servers/slurpd/ch_malloc.c
index 191a7d06c4c382b047cbfeef99ba9feb02a86aa1..0adcb7bddff979a28090779657f3c234eca43b95 100644
--- a/servers/slurpd/ch_malloc.c
+++ b/servers/slurpd/ch_malloc.c
@@ -31,13 +31,14 @@
  */
 void *
 ch_malloc(
-    unsigned long	size
+    ber_len_t	size
 )
 {
 	void	*new;
 
 	if ( (new = (void *) malloc( size )) == NULL ) {
-		fprintf( stderr, "malloc of %lu bytes failed\n", size );
+		fprintf( stderr, "malloc of %lu bytes failed\n",
+			(long) size );
 		exit( 1 );
 	}
 
@@ -54,7 +55,7 @@ ch_malloc(
 void *
 ch_realloc(
     void		*block,
-    unsigned long	size
+    ber_len_t	size
 )
 {
 	void	*new;
@@ -64,7 +65,8 @@ ch_realloc(
 	}
 
 	if ( (new = (void *) realloc( block, size )) == NULL ) {
-		fprintf( stderr, "realloc of %lu bytes failed\n", size );
+		fprintf( stderr, "realloc of %lu bytes failed\n",
+			(long) size );
 		exit( 1 );
 	}
 
@@ -80,15 +82,15 @@ ch_realloc(
  */
 void *
 ch_calloc(
-    unsigned long	nelem,
-    unsigned long	size
+    ber_len_t	nelem,
+    ber_len_t	size
 )
 {
 	void	*new;
 
 	if ( (new = (void *) calloc( nelem, size )) == NULL ) {
 		fprintf( stderr, "calloc of %lu elems of %lu bytes failed\n",
-		    nelem, size );
+		    (long) nelem, (long) size );
 		exit( 1 );
 	}
 
diff --git a/servers/slurpd/proto-slurp.h b/servers/slurpd/proto-slurp.h
index 56e32829021be96de76a665d45dd22359e0dc05e..37bcb3f85d9e50795700456b897a64c1902877c0 100644
--- a/servers/slurpd/proto-slurp.h
+++ b/servers/slurpd/proto-slurp.h
@@ -12,9 +12,9 @@ RETSIGTYPE do_admin	LDAP_P((int sig));
 int doargs	LDAP_P((int argc, char **argv, struct globals *g));
 
 /* ch_malloc.c */
-void *ch_malloc	LDAP_P((unsigned long size));
-void *ch_realloc	LDAP_P((void *block, unsigned long size));
-void *ch_calloc	LDAP_P((unsigned long nelem, unsigned long size));
+void *ch_malloc	LDAP_P((ber_len_t size));
+void *ch_realloc	LDAP_P((void *block, ber_len_t size));
+void *ch_calloc	LDAP_P((ber_len_t nelem, ber_len_t size));
 void ch_free	LDAP_P((void *p));
 
 /* config.c */