From 5030d53ff08665d27e388244aa22b272716240bc Mon Sep 17 00:00:00 2001
From: dscho <dscho>
Date: Sun, 9 Feb 2003 15:04:00 +0000
Subject: converted CARD{8,16,32} to uint{8,16,32}_t and included support for
 stdint.h

---
 Makefile.am            |   2 +-
 TODO                   |   2 +-
 acinclude.m4           | 653 +++++++++++++++++++++++++++++++++++++++++++++++++
 auth.c                 |  10 +-
 configure.ac           |   2 +
 contrib/x11vnc.c       |   2 +-
 corre.c                |  42 ++--
 cursor.c               |  12 +-
 cutpaste.c             |   1 -
 hextile.c              |  31 ++-
 httpd.c                |  25 +-
 include/.cvsignore     |   3 +-
 include/rfb.h          |  51 ++--
 include/rfbproto.h     | 162 ++++++------
 main.c                 |   8 +-
 rfbserver.c            |  33 ++-
 rre.c                  |  41 ++--
 sockets.c              |  23 +-
 sraRegion.c            |   3 -
 stats.c                |   2 -
 tableinit24.c          |  36 +--
 tableinitcmtemplate.c  |   6 +-
 tableinittctemplate.c  |   2 +-
 tabletrans24template.c |  60 ++---
 tabletranstemplate.c   |   4 +-
 tight.c                | 208 ++++++++--------
 translate.c            |   6 +-
 vncauth.c              |  14 +-
 zlib.c                 |   1 -
 zrle.cxx               |   3 +-
 30 files changed, 1063 insertions(+), 385 deletions(-)
 create mode 100644 acinclude.m4

diff --git a/Makefile.am b/Makefile.am
index 8bf6bda..5390433 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,7 +3,7 @@ DIST_SUBDIRS=examples contrib
 
 bin_SCRIPTS = libvncserver-config
 
-include_HEADERS=include/rfb.h include/rfbconfig.h include/rfbproto.h \
+include_HEADERS=include/rfb.h include/rfbconfig.h include/rfbint.h include/rfbproto.h \
 	include/keysym.h
 
 noinst_HEADERS=sraRegion.h d3des.h zrleDecode.h zrleEncode.h \
diff --git a/TODO b/TODO
index b857348..8811e0c 100644
--- a/TODO
+++ b/TODO
@@ -1,7 +1,6 @@
 immediate:
 ----------
 
-autoconf also CARD8,CARD16,...
 x11vnc: clipboard, cursor
 extra_bytes in rfbDrawCharWithClip.
 tested mouse buttons make copy rect, but text is not marked as mod.
@@ -25,6 +24,7 @@ CORBA
 done:
 -----
 
+.autoconf also CARD8,CARD16,...
 .autoconf
 .internal HTTP tunnelling feature (needs a special GET target and a few
 .	changes to java applet).
diff --git a/acinclude.m4 b/acinclude.m4
new file mode 100644
index 0000000..163efa5
--- /dev/null
+++ b/acinclude.m4
@@ -0,0 +1,653 @@
+AH_TEMPLATE(socklen_t, [The type for socklen])
+AC_DEFUN([AC_TYPE_SOCKLEN_T],
+[AC_CACHE_CHECK([for socklen_t], ac_cv_type_socklen_t,
+[
+  AC_TRY_COMPILE(
+  [#include <sys/types.h>
+   #include <sys/socket.h>],
+  [socklen_t len = 42; return 0;],
+  ac_cv_type_socklen_t=yes,
+  ac_cv_type_socklen_t=no)
+])
+  if test $ac_cv_type_socklen_t != yes; then
+    AC_DEFINE(socklen_t, int)
+  fi
+])
+
+dnl Available from the GNU Autoconf Macro Archive at:
+dnl http://www.gnu.org/software/ac-archive/htmldoc/ac_compile_check_sizeof.html
+dnl
+AC_DEFUN([AC_COMPILE_CHECK_SIZEOF],
+[changequote(<<, >>)dnl
+dnl The name to #define.
+define(<<AC_TYPE_NAME>>, translit(sizeof_$1, [a-z *], [A-Z_P]))dnl
+dnl The cache variable name.
+define(<<AC_CV_NAME>>, translit(ac_cv_sizeof_$1, [ *], [_p]))dnl
+changequote([, ])dnl
+AC_MSG_CHECKING(size of $1)
+AC_CACHE_VAL(AC_CV_NAME,
+[for ac_size in 4 8 1 2 16 $2 ; do # List sizes in rough order of prevalence.
+  AC_TRY_COMPILE([#include "confdefs.h"
+#include <sys/types.h>
+$2
+], [switch (0) case 0: case (sizeof ($1) == $ac_size):;], AC_CV_NAME=$ac_size)
+  if test x$AC_CV_NAME != x ; then break; fi
+done
+])
+if test x$AC_CV_NAME = x ; then
+  AC_MSG_ERROR([cannot determine a size for $1])
+fi
+AC_MSG_RESULT($AC_CV_NAME)
+AC_DEFINE_UNQUOTED(AC_TYPE_NAME, $AC_CV_NAME, [The number of bytes in type $1])
+undefine([AC_TYPE_NAME])dnl
+undefine([AC_CV_NAME])dnl
+])
+
+dnl Available from the GNU Autoconf Macro Archive at:
+dnl http://www.gnu.org/software/ac-archive/htmldoc/ac_create_stdint_h.html
+dnl
+AC_DEFUN([AC_CREATE_STDINT_H],
+[# ------ AC CREATE STDINT H -------------------------------------
+AC_MSG_CHECKING([for stdint-types....])
+ac_stdint_h=`echo ifelse($1, , _stdint.h, $1)`
+if test "$ac_stdint_h" = "stdint.h" ; then
+ AC_MSG_RESULT("(are you sure you want them in ./stdint.h?)")
+elif test "$ac_stdint_h" = "inttypes.h" ; then
+ AC_MSG_RESULT("(are you sure you want them in ./inttypes.h?)")
+else
+ AC_MSG_RESULT("(putting them into $ac_stdint_h)")
+fi
+
+inttype_headers=`echo inttypes.h sys/inttypes.h sys/inttypes.h $2 \
+| sed -e 's/,/ /g'`
+
+ ac_cv_header_stdint_x="no-file"
+ ac_cv_header_stdint_o="no-file"
+ ac_cv_header_stdint_u="no-file"
+ for i in stdint.h $inttype_headers ; do
+   unset ac_cv_type_uintptr_t
+   unset ac_cv_type_uint64_t
+   _AC_CHECK_TYPE_NEW(uintptr_t,[ac_cv_header_stdint_x=$i],dnl
+     continue,[#include <$i>])
+   AC_CHECK_TYPE(uint64_t,[and64="(uint64_t too)"],[and64=""],[#include<$i>])
+   AC_MSG_RESULT(... seen our uintptr_t in $i $and64)
+   break;
+ done
+ if test "$ac_cv_header_stdint_x" = "no-file" ; then
+ for i in stdint.h $inttype_headers ; do
+   unset ac_cv_type_uint32_t
+   unset ac_cv_type_uint64_t
+   AC_CHECK_TYPE(uint32_t,[ac_cv_header_stdint_o=$i],dnl
+     continue,[#include <$i>])
+   AC_CHECK_TYPE(uint64_t,[and64="(uint64_t too)"],[and64=""],[#include<$i>])
+   AC_MSG_RESULT(... seen our uint32_t in $i $and64)
+   break;
+ done
+ if test "$ac_cv_header_stdint_o" = "no-file" ; then
+ for i in sys/types.h $inttype_headers ; do
+   unset ac_cv_type_u_int32_t
+   unset ac_cv_type_u_int64_t
+   AC_CHECK_TYPE(u_int32_t,[ac_cv_header_stdint_u=$i],dnl
+     continue,[#include <$i>])
+   AC_CHECK_TYPE(uint64_t,[and64="(u_int64_t too)"],[and64=""],[#include<$i>])
+   AC_MSG_RESULT(... seen our u_int32_t in $i $and64)
+   break;
+ done
+ fi
+ fi
+
+# ----------------- DONE inttypes.h checks MAYBE C basic types --------
+
+if test "$ac_cv_header_stdint_x" = "no-file" ; then
+   AC_COMPILE_CHECK_SIZEOF(char)
+   AC_COMPILE_CHECK_SIZEOF(short)
+   AC_COMPILE_CHECK_SIZEOF(int)
+   AC_COMPILE_CHECK_SIZEOF(long)
+   AC_COMPILE_CHECK_SIZEOF(void*)
+   ac_cv_header_stdint_test="yes"
+else
+   ac_cv_header_stdint_test="no"
+fi
+
+# ----------------- DONE inttypes.h checks START header -------------
+_ac_stdint_h=AS_TR_CPP(_$ac_stdint_h)
+AC_MSG_RESULT(creating $ac_stdint_h : $_ac_stdint_h)
+echo "#ifndef" $_ac_stdint_h >$ac_stdint_h
+echo "#define" $_ac_stdint_h "1" >>$ac_stdint_h
+echo "#ifndef" _GENERATED_STDINT_H >>$ac_stdint_h
+echo "#define" _GENERATED_STDINT_H '"'$PACKAGE $VERSION'"' >>$ac_stdint_h
+if test "$GCC" = "yes" ; then
+  echo "/* generated using a gnu compiler version" `$CC --version` "*/" \
+  >>$ac_stdint_h
+else
+  echo "/* generated using $CC */" >>$ac_stdint_h
+fi
+echo "" >>$ac_stdint_h
+
+if test "$ac_cv_header_stdint_x" != "no-file" ; then
+   ac_cv_header_stdint="$ac_cv_header_stdint_x"
+elif  test "$ac_cv_header_stdint_o" != "no-file" ; then
+   ac_cv_header_stdint="$ac_cv_header_stdint_o"
+elif  test "$ac_cv_header_stdint_u" != "no-file" ; then
+   ac_cv_header_stdint="$ac_cv_header_stdint_u"
+else
+   ac_cv_header_stdint="stddef.h"
+fi
+
+# ----------------- See if int_least and int_fast types are present
+unset ac_cv_type_int_least32_t
+unset ac_cv_type_int_fast32_t
+AC_CHECK_TYPE(int_least32_t,,,[#include <$ac_cv_header_stdint>])
+AC_CHECK_TYPE(int_fast32_t,,,[#include<$ac_cv_header_stdint>])
+
+if test "$ac_cv_header_stdint" != "stddef.h" ; then
+if test "$ac_cv_header_stdint" != "stdint.h" ; then
+AC_MSG_RESULT(..adding include stddef.h)
+   echo "#include <stddef.h>" >>$ac_stdint_h
+fi ; fi
+AC_MSG_RESULT(..adding include $ac_cv_header_stdint)
+   echo "#include <$ac_cv_header_stdint>" >>$ac_stdint_h
+echo "" >>$ac_stdint_h
+
+# ----------------- DONE header START basic int types -------------
+if test "$ac_cv_header_stdint_x" = "no-file" ; then
+   AC_MSG_RESULT(... need to look at C basic types)
+dnl ac_cv_header_stdint_test="yes" # moved up before creating the file
+else
+   AC_MSG_RESULT(... seen good stdint.h inttypes)
+dnl ac_cv_header_stdint_test="no"  # moved up before creating the file
+fi
+
+if test "$ac_cv_header_stdint_u" != "no-file" ; then
+   AC_MSG_RESULT(... seen bsd/sysv typedefs)
+   cat >>$ac_stdint_h <<EOF
+
+/* int8_t int16_t int32_t defined by inet code, redeclare the u_intXX types */
+typedef u_int8_t uint8_t;
+typedef u_int16_t uint16_t;
+typedef u_int32_t uint32_t;
+EOF
+    cat >>$ac_stdint_h <<EOF
+
+/* glibc compatibility */
+#ifndef __int8_t_defined
+#define __int8_t_defined
+#endif
+EOF
+fi
+
+ac_cv_sizeof_x="$ac_cv_sizeof_char:$ac_cv_sizeof_short"
+ac_cv_sizeof_X="$ac_cv_sizeof_x:$ac_cv_sizeof_int"
+ac_cv_sizeof_X="$ac_cv_sizeof_X:$ac_cv_sizeof_voidp:$ac_cv_sizeof_long"
+if test "$ac_cv_header_stdint" = "stddef.h" ; then
+#   we must guess all the basic types. Apart from byte-adressable system,
+# there a few 32-bit-only dsp-systems. nibble-addressable systems are way off.
+    cat >>$ac_stdint_h <<EOF
+/* ------------ BITSPECIFIC INTTYPES SECTION --------------- */
+EOF
+    t="typedefs for a"
+    case "$ac_cv_sizeof_X" in
+     1:2:2:2:4) AC_MSG_RESULT(..adding $t normal 16-bit system)
+                cat >>$ac_stdint_h <<EOF
+/*              a normal 16-bit system                       */
+typedef unsigned char   uint8_t;
+typedef unsigned short  uint16_t;
+typedef unsigned long   uint32_t;
+#ifndef __int8_t_defined
+#define __int8_t_defined
+typedef          char    int8_t;
+typedef          short   int16_t;
+typedef          long    int32_t;
+#endif
+EOF
+;;
+     1:2:2:4:4) AC_MSG_RESULT(..adding $t 32-bit system derived from a 16-bit)
+                cat >>$ac_stdint_h <<EOF
+/*              a 32-bit system derived from a 16-bit        */
+typedef unsigned char   uint8_t;
+typedef unsigned short  uint16_t;
+typedef unsigned int    uint32_t;
+#ifndef __int8_t_defined
+#define __int8_t_defined
+typedef          char    int8_t;
+typedef          short   int16_t;
+typedef          int     int32_t;
+#endif
+EOF
+;;
+     1:2:4:4:4) AC_MSG_RESULT(..adding $t normal 32-bit system)
+                cat >>$ac_stdint_h <<EOF
+/*              a normal 32-bit system                       */
+typedef unsigned char   uint8_t;
+typedef unsigned short  uint16_t;
+typedef unsigned int    uint32_t;
+#ifndef __int8_t_defined
+#define __int8_t_defined
+typedef          char    int8_t;
+typedef          short   int16_t;
+typedef          int     int32_t;
+#endif
+EOF
+;;
+     1:2:4:4:8) AC_MSG_RESULT(..adding $t 32-bit system prepared for 64-bit)
+                cat >>$ac_stdint_h <<EOF
+
+/*              a 32-bit system prepared for 64-bit          */
+typedef unsigned char   uint8_t;
+typedef unsigned short  uint16_t;
+typedef unsigned int    uint32_t;
+#ifndef __int8_t_defined
+#define __int8_t_defined
+typedef          char    int8_t;
+typedef          short   int16_t;
+typedef          int     int32_t;
+#endif
+EOF
+;;
+     1:2:4:8:8) AC_MSG_RESULT(..adding $t normal 64-bit system)
+                cat >>$ac_stdint_h <<EOF
+
+/*              a normal 64-bit system                       */
+typedef unsigned char   uint8_t;
+typedef unsigned short  uint16_t;
+typedef unsigned int    uint32_t;
+#ifndef __int8_t_defined
+#define __int8_t_defined
+typedef          char    int8_t;
+typedef          short   int16_t;
+typedef          int     int32_t;
+#endif
+EOF
+;;
+     1:2:4:8:4) AC_MSG_RESULT(..adding $t 64-bit system derived from a 32-bit)
+                cat >>$ac_stdint_h <<EOF
+
+/*              a 64-bit system derived from a 32-bit system */
+typedef unsigned char   uint8_t;
+typedef unsigned short  uint16_t;
+typedef unsigned int    uint32_t;
+#ifndef __int8_t_defined
+#define __int8_t_defined
+typedef          char    int8_t;
+typedef          short   int16_t;
+typedef          int     int32_t;
+#endif
+EOF
+;;
+  *)
+    AC_MSG_ERROR([ $ac_cv_sizeof_X dnl
+ what is that a system? contact the author, quick! http://ac-archive.sf.net])
+    exit 1
+;;
+   esac
+fi
+
+# ------------- DONE basic int types START int64_t types ------------
+if test "$ac_cv_type_uint64_t" = "yes"
+then AC_MSG_RESULT(... seen good uint64_t)
+     cat >>$ac_stdint_h <<EOF
+
+/* system headers have good uint64_t */
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+#endif
+EOF
+
+elif test "$ac_cv_type_u_int64_t" = "yes"
+then AC_MSG_RESULT(..adding typedef u_int64_t uint64_t)
+     cat >>$ac_stdint_h <<EOF
+
+/* system headers have an u_int64_t */
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+typedef u_int64_t uint64_t;
+#endif
+EOF
+else AC_MSG_RESULT(..adding generic uint64_t runtime checks)
+     cat >>$ac_stdint_h <<EOF
+
+/* -------------------- 64 BIT GENERIC SECTION -------------------- */
+/* here are some common heuristics using compiler runtime specifics */
+#if defined __STDC_VERSION__ && defined __STDC_VERSION__ > 199901L
+
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+typedef long long int64_t;
+typedef unsigned long long uint64_t;
+#endif
+
+#elif !defined __STRICT_ANSI__
+#if defined _MSC_VER || defined __WATCOMC__ || defined __BORLANDC__
+
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+typedef __int64 int64_t;
+typedef unsigned __int64 uint64_t;
+#endif
+
+#elif defined __GNUC__ || defined __MWERKS__ || defined __ELF__
+dnl /* note: all ELF-systems seem to have loff-support which needs 64-bit */
+
+#if !defined _NO_LONGLONG
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+typedef long long int64_t;
+typedef unsigned long long uint64_t;
+#endif
+#endif
+
+#elif defined __alpha || (defined __mips && defined _ABIN32)
+
+#if !defined _NO_LONGLONG
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+typedef long int64_t;
+typedef unsigned long uint64_t;
+#endif
+#endif
+  /* compiler/cpu type ... or just ISO C99 */
+#endif
+#endif
+EOF
+
+# plus a default 64-bit for systems that are likely to be 64-bit ready
+  case "$ac_cv_sizeof_x:$ac_cv_sizeof_voidp:$ac_cv_sizeof_long" in
+    1:2:8:8) AC_MSG_RESULT(..adding uint64_t default, normal 64-bit system)
+cat >>$ac_stdint_h <<EOF
+/* DEFAULT: */
+/* seen normal 64-bit system, CC has sizeof(long and void*) == 8 bytes */
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+typedef long int64_t;
+typedef unsigned long uint64_t;
+#endif
+EOF
+;;
+    1:2:4:8) AC_MSG_RESULT(..adding uint64_t default, typedef to long)
+cat >>$ac_stdint_h <<EOF
+/* DEFAULT: */
+/* seen 32-bit system prepared for 64-bit, CC has sizeof(long) == 8 bytes */
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+typedef long int64_t;
+typedef unsigned long uint64_t;
+#endif
+EOF
+;;
+    1:2:8:4) AC_MSG_RESULT(..adding uint64_t default, typedef long long)
+cat >>$ac_stdint_h <<EOF
+/* DEFAULT: */
+/* seen 64-bit derived from a 32-bit, CC has sizeof(long) == 4 bytes */
+#ifndef _HAVE_UINT64_T
+#define _HAVE_UINT64_T
+typedef long long int64_t;
+typedef unsigned long long uint64_t;
+#endif
+EOF
+;;
+   *)
+cat >>$ac_stdint_h <<EOF
+/* NOTE: */
+/* the configure-checks for the basic types did not make us believe */
+/* that we could add a fallback to a 'long long' typedef to int64_t */
+EOF
+  esac
+fi
+
+# ------------- DONE int64_t types START intptr types ------------
+if test "$ac_cv_header_stdint_x" = "no-file" ; then
+  cat >>$ac_stdint_h <<EOF
+
+/* -------------------------- INPTR SECTION --------------------------- */
+EOF
+  case "$ac_cv_sizeof_x:$ac_cv_sizeof_voidp" in
+  1:2:2)
+    a="int16_t" ; cat >>$ac_stdint_h <<EOF
+/* we tested sizeof(void*) to be of 2 chars, hence we declare it 16-bit */
+
+typedef uint16_t uintptr_t;
+typedef  int16_t  intptr_t;
+EOF
+;;
+  1:2:4)
+    a="int32_t" ; cat >>$ac_stdint_h <<EOF
+/* we tested sizeof(void*) to be of 4 chars, hence we declare it 32-bit */
+
+typedef uint32_t uintptr_t;
+typedef  int32_t  intptr_t;
+EOF
+;;
+  1:2:8)
+    a="int64_t" ; cat >>$ac_stdint_h <<EOF
+/* we tested sizeof(void*) to be of 8 chars, hence we declare it 64-bit */
+
+typedef uint64_t uintptr_t;
+typedef  int64_t  intptr_t;
+EOF
+;;
+  *)
+    a="long" ; cat >>$ac_stdint_h <<EOF
+/* we tested sizeof(void*) but got no guess, hence we declare it as if long */
+
+typedef unsigned long uintptr_t;
+typedef          long  intptr_t;
+EOF
+;;
+  esac
+AC_MSG_RESULT(..adding typedef $a intptr_t)
+fi
+
+# ------------- DONE intptr types START int_least types ------------
+if test "$ac_cv_type_int_least32_t" = "no"; then
+AC_MSG_RESULT(..adding generic int_least-types)
+     cat >>$ac_stdint_h <<EOF
+
+/* --------------GENERIC INT_LEAST ------------------ */
+
+typedef  int8_t    int_least8_t;
+typedef  int16_t   int_least16_t;
+typedef  int32_t   int_least32_t;
+#ifdef _HAVE_INT64_T
+typedef  int64_t   int_least64_t;
+#endif
+
+typedef uint8_t   uint_least8_t;
+typedef uint16_t  uint_least16_t;
+typedef uint32_t  uint_least32_t;
+#ifdef _HAVE_INT64_T
+typedef uint64_t  uint_least64_t;
+#endif
+EOF
+fi
+
+# ------------- DONE intptr types START int_least types ------------
+if test "$ac_cv_type_int_fast32_t" = "no"; then
+AC_MSG_RESULT(..adding generic int_fast-types)
+     cat >>$ac_stdint_h <<EOF
+
+/* --------------GENERIC INT_FAST ------------------ */
+
+typedef  int8_t    int_fast8_t;
+typedef  int32_t   int_fast16_t;
+typedef  int32_t   int_fast32_t;
+#ifdef _HAVE_INT64_T
+typedef  int64_t   int_fast64_t;
+#endif
+
+typedef uint8_t   uint_fast8_t;
+typedef uint32_t  uint_fast16_t;
+typedef uint32_t  uint_fast32_t;
+#ifdef _HAVE_INT64_T
+typedef uint64_t  uint_fast64_t;
+#endif
+EOF
+fi
+
+if test "$ac_cv_header_stdint_x" = "no-file" ; then
+     cat >>$ac_stdint_h <<EOF
+
+#ifdef _HAVE_INT64_T
+typedef int64_t        intmax_t;
+typedef uint64_t      uintmax_t;
+#else
+typedef long int       intmax_t;
+typedef unsigned long uintmax_t;
+#endif
+EOF
+fi
+
+AC_MSG_RESULT(... DONE $ac_stdint_h)
+   cat >>$ac_stdint_h <<EOF
+
+  /* once */
+#endif
+#endif
+EOF
+])
+
+dnl quote from SunOS-5.8 sys/inttypes.h:
+dnl Use at your own risk.  As of February 1996, the committee is squarely
+dnl behind the fixed sized types; the "least" and "fast" types are still being
+dnl discussed.  The probability that the "fast" types may be removed before
+dnl the standard is finalized is high enough that they are not currently
+dnl implemented.
+
+# AM_INIT_AUTOMAKE(PACKAGE,VERSION, [NO-DEFINE])
+# ----------------------------------------------
+AC_DEFUN([AM_INIT_AUTOMAKE],
+[AC_REQUIRE([AC_PROG_INSTALL])dnl
+# test to see if srcdir already configured
+if test "`CDPATH=:; cd $srcdir && pwd`" != "`pwd`" &&
+   test -f $srcdir/config.status; then
+  AC_MSG_ERROR([source directory already configured; run \"make distclean\" there first])
+fi
+
+# Define the identity of the package.
+PACKAGE=$1
+AC_SUBST(PACKAGE)dnl
+VERSION=$2
+AC_SUBST(VERSION)dnl
+ifelse([$3],,
+[AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE", [Name of package])
+AC_DEFINE_UNQUOTED(VERSION, "$VERSION", [Version number of package])])
+
+# Autoconf 2.50 wants to disallow AM_ names.  We explicitly allow
+# the ones we care about.
+ifdef([m4_pattern_allow],
+      [m4_pattern_allow([^AM_[A-Z]+FLAGS])])dnl
+
+# Autoconf 2.50 always computes EXEEXT.  However we need to be
+# compatible with 2.13, for now.  So we always define EXEEXT, but we
+# don't compute it.
+AC_SUBST(EXEEXT)
+# Similar for OBJEXT -- only we only use OBJEXT if the user actually
+# requests that it be used.  This is a bit dumb.
+: ${OBJEXT=o}
+AC_SUBST(OBJEXT)
+
+# Some tools Automake needs.
+AC_REQUIRE([AM_SANITY_CHECK])dnl
+AC_REQUIRE([AC_ARG_PROGRAM])dnl
+AM_MISSING_PROG(ACLOCAL, aclocal)
+AM_MISSING_PROG(AUTOCONF, autoconf)
+AM_MISSING_PROG(AUTOMAKE, automake)
+AM_MISSING_PROG(AUTOHEADER, autoheader)
+AM_MISSING_PROG(MAKEINFO, makeinfo)
+AM_MISSING_PROG(AMTAR, tar)
+AM_PROG_INSTALL_SH
+AM_PROG_INSTALL_STRIP
+# We need awk for the "check" target.  The system "awk" is bad on
+# some platforms.
+AC_REQUIRE([AC_PROG_AWK])dnl
+AC_REQUIRE([AC_PROG_MAKE_SET])dnl
+AC_REQUIRE([AM_DEP_TRACK])dnl
+AC_REQUIRE([AM_SET_DEPDIR])dnl
+AC_PROVIDE_IFELSE([AC_PROG_][CC],
+                  [_AM_DEPENDENCIES(CC)],
+                  [define([AC_PROG_][CC],
+                          defn([AC_PROG_][CC])[_AM_DEPENDENCIES(CC)])])dnl
+AC_PROVIDE_IFELSE([AC_PROG_][CXX],
+                  [_AM_DEPENDENCIES(CXX)],
+                  [define([AC_PROG_][CXX],
+                          defn([AC_PROG_][CXX])[_AM_DEPENDENCIES(CXX)])])dnl
+])
+
+# AM_CONDITIONAL(NAME, SHELL-CONDITION)
+# -------------------------------------
+# Define a conditional.
+#
+# FIXME: Once using 2.50, use this:
+# m4_match([$1], [^TRUE\|FALSE$], [AC_FATAL([$0: invalid condition: $1])])dnl
+AC_DEFUN([AM_CONDITIONAL],
+[ifelse([$1], [TRUE],
+        [errprint(__file__:__line__: [$0: invalid condition: $1
+])dnl
+m4exit(1)])dnl
+ifelse([$1], [FALSE],
+       [errprint(__file__:__line__: [$0: invalid condition: $1
+])dnl
+m4exit(1)])dnl
+AC_SUBST([$1_TRUE])
+AC_SUBST([$1_FALSE])
+if $2; then
+  $1_TRUE=
+  $1_FALSE='#'
+else
+  $1_TRUE='#'
+  $1_FALSE=
+fi])
+
+# Like AC_CONFIG_HEADER, but automatically create stamp file.
+
+# serial 3
+
+# When config.status generates a header, we must update the stamp-h file.
+# This file resides in the same directory as the config header
+# that is generated.  We must strip everything past the first ":",
+# and everything past the last "/".
+
+AC_PREREQ([2.12])
+
+AC_DEFUN([AM_CONFIG_HEADER],
+[ifdef([AC_FOREACH],dnl
+	 [dnl init our file count if it isn't already
+	 m4_ifndef([_AM_Config_Header_Index], m4_define([_AM_Config_Header_Index], [0]))
+	 dnl prepare to store our destination file list for use in config.status
+	 AC_FOREACH([_AM_File], [$1],
+		    [m4_pushdef([_AM_Dest], m4_patsubst(_AM_File, [:.*]))
+		    m4_define([_AM_Config_Header_Index], m4_incr(_AM_Config_Header_Index))
+		    dnl and add it to the list of files AC keeps track of, along
+		    dnl with our hook
+		    AC_CONFIG_HEADERS(_AM_File,
+dnl COMMANDS, [, INIT-CMDS]
+[# update the timestamp
+echo timestamp >"AS_ESCAPE(_AM_DIRNAME(]_AM_Dest[))/stamp-h]_AM_Config_Header_Index["
+][$2]m4_ifval([$3], [, [$3]]))dnl AC_CONFIG_HEADERS
+		    m4_popdef([_AM_Dest])])],dnl
+[AC_CONFIG_HEADER([$1])
+  AC_OUTPUT_COMMANDS(
+   ifelse(patsubst([$1], [[^ ]], []),
+	  [],
+	  [test -z "$CONFIG_HEADERS" || echo timestamp >dnl
+	   patsubst([$1], [^\([^:]*/\)?.*], [\1])stamp-h]),dnl
+[am_indx=1
+for am_file in $1; do
+  case " \$CONFIG_HEADERS " in
+  *" \$am_file "*)
+    am_dir=\`echo \$am_file |sed 's%:.*%%;s%[^/]*\$%%'\`
+    if test -n "\$am_dir"; then
+      am_tmpdir=\`echo \$am_dir |sed 's%^\(/*\).*\$%\1%'\`
+      for am_subdir in \`echo \$am_dir |sed 's%/% %'\`; do
+        am_tmpdir=\$am_tmpdir\$am_subdir/
+        if test ! -d \$am_tmpdir; then
+          mkdir \$am_tmpdir
+        fi
+      done
+    fi
+    echo timestamp > "\$am_dir"stamp-h\$am_indx
+    ;;
+  esac
+  am_indx=\`expr \$am_indx + 1\`
+done])
+])]) # AM_CONFIG_HEADER
+
diff --git a/auth.c b/auth.c
index b903425..7833404 100644
--- a/auth.c
+++ b/auth.c
@@ -26,8 +26,6 @@
  *  USA.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include "rfb.h"
 
 /*
@@ -46,12 +44,12 @@ rfbAuthNewClient(cl)
     cl->state = RFB_AUTHENTICATION;
 
     if (cl->screen->rfbAuthPasswdData && !cl->reverseConnection) {
-        *(CARD32 *)buf = Swap32IfLE(rfbVncAuth);
+        *(uint32_t *)buf = Swap32IfLE(rfbVncAuth);
         vncRandomBytes(cl->authChallenge);
         memcpy(&buf[4], (char *)cl->authChallenge, CHALLENGESIZE);
         len = 4 + CHALLENGESIZE;
     } else {
-        *(CARD32 *)buf = Swap32IfLE(rfbNoAuth);
+        *(uint32_t *)buf = Swap32IfLE(rfbNoAuth);
         len = 4;
         cl->state = RFB_INITIALISATION;
     }
@@ -74,8 +72,8 @@ rfbAuthProcessClientMessage(cl)
     rfbClientPtr cl;
 {
     int n;
-    CARD8 response[CHALLENGESIZE];
-    CARD32 authResult;
+    uint8_t response[CHALLENGESIZE];
+    uint32_t authResult;
 
     if ((n = ReadExact(cl, (char *)response, CHALLENGESIZE)) <= 0) {
         if (n != 0)
diff --git a/configure.ac b/configure.ac
index 1467688..47422c1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -71,6 +71,8 @@ AC_C_INLINE
 AC_C_BIGENDIAN
 AC_TYPE_SIZE_T
 AC_HEADER_TIME
+AC_TYPE_SOCKLEN_T
+AC_CREATE_STDINT_H(include/rfbint.h)
 
 # Checks for library functions.
 AC_FUNC_MALLOC
diff --git a/contrib/x11vnc.c b/contrib/x11vnc.c
index ed7689f..e7c6fd2 100644
--- a/contrib/x11vnc.c
+++ b/contrib/x11vnc.c
@@ -461,7 +461,7 @@ void initialize_screen(int *argc, char **argv, XImage *fb) {
 	screen->paddedWidthInBytes = fb->bytes_per_line;
 	screen->rfbServerFormat.bitsPerPixel = fb->bits_per_pixel;
 	screen->rfbServerFormat.depth = fb->depth;
-	screen->rfbServerFormat.trueColour = (CARD8) TRUE;
+	screen->rfbServerFormat.trueColour = (uint8_t) TRUE;
 
 	if ( screen->rfbServerFormat.bitsPerPixel == 8 ) {
 		/* 8 bpp */
diff --git a/corre.c b/corre.c
index d866712..06934b1 100644
--- a/corre.c
+++ b/corre.c
@@ -27,8 +27,6 @@
  *  USA.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include "rfb.h"
 
 /*
@@ -45,10 +43,10 @@ static int rreAfterBufSize = 0;
 static char *rreAfterBuf = NULL;
 static int rreAfterBufLen;
 
-static int subrectEncode8(CARD8 *data, int w, int h);
-static int subrectEncode16(CARD16 *data, int w, int h);
-static int subrectEncode32(CARD32 *data, int w, int h);
-static CARD32 getBgColour(char *data, int size, int bpp);
+static int subrectEncode8(uint8_t *data, int w, int h);
+static int subrectEncode16(uint16_t *data, int w, int h);
+static int subrectEncode32(uint32_t *data, int w, int h);
+static uint32_t getBgColour(char *data, int size, int bpp);
 static Bool rfbSendSmallRectEncodingCoRRE(rfbClientPtr cl, int x, int y,
                                           int w, int h);
 
@@ -123,13 +121,13 @@ rfbSendSmallRectEncodingCoRRE(cl, x, y, w, h)
 
     switch (cl->format.bitsPerPixel) {
     case 8:
-        nSubrects = subrectEncode8((CARD8 *)rreBeforeBuf, w, h);
+        nSubrects = subrectEncode8((uint8_t *)rreBeforeBuf, w, h);
         break;
     case 16:
-        nSubrects = subrectEncode16((CARD16 *)rreBeforeBuf, w, h);
+        nSubrects = subrectEncode16((uint16_t *)rreBeforeBuf, w, h);
         break;
     case 32:
-        nSubrects = subrectEncode32((CARD32 *)rreBeforeBuf, w, h);
+        nSubrects = subrectEncode32((uint32_t *)rreBeforeBuf, w, h);
         break;
     default:
         rfbLog("getBgColour: bpp %d?\n",cl->format.bitsPerPixel);
@@ -209,25 +207,25 @@ rfbSendSmallRectEncodingCoRRE(cl, x, y, w, h)
 #define DEFINE_SUBRECT_ENCODE(bpp)                                            \
 static int                                                                    \
 subrectEncode##bpp(data,w,h)                                                  \
-    CARD##bpp *data;                                                          \
+    uint##bpp##_t *data;                                                      \
     int w;                                                                    \
     int h;                                                                    \
 {                                                                             \
-    CARD##bpp cl;                                                             \
+    uint##bpp##_t cl;                                                         \
     rfbCoRRERectangle subrect;                                                \
     int x,y;                                                                  \
     int i,j;                                                                  \
     int hx=0,hy,vx=0,vy;                                                      \
     int hyflag;                                                               \
-    CARD##bpp *seg;                                                           \
-    CARD##bpp *line;                                                          \
+    uint##bpp##_t *seg;                                                       \
+    uint##bpp##_t *line;                                                      \
     int hw,hh,vw,vh;                                                          \
     int thex,they,thew,theh;                                                  \
     int numsubs = 0;                                                          \
     int newLen;                                                               \
-    CARD##bpp bg = (CARD##bpp)getBgColour((char*)data,w*h,bpp);               \
+    uint##bpp##_t bg = (uint##bpp##_t)getBgColour((char*)data,w*h,bpp);       \
                                                                               \
-    *((CARD##bpp*)rreAfterBuf) = bg;                                          \
+    *((uint##bpp##_t*)rreAfterBuf) = bg;                                      \
                                                                               \
     rreAfterBufLen = (bpp/8);                                                 \
                                                                               \
@@ -279,7 +277,7 @@ subrectEncode##bpp(data,w,h)                                                  \
             return -1;                                                        \
                                                                               \
           numsubs += 1;                                                       \
-          *((CARD##bpp*)(rreAfterBuf + rreAfterBufLen)) = cl;                 \
+          *((uint##bpp##_t*)(rreAfterBuf + rreAfterBufLen)) = cl;                 \
           rreAfterBufLen += (bpp/8);                                          \
           memcpy(&rreAfterBuf[rreAfterBufLen],&subrect,sz_rfbCoRRERectangle); \
           rreAfterBufLen += sz_rfbCoRRERectangle;                             \
@@ -307,7 +305,7 @@ DEFINE_SUBRECT_ENCODE(32)
 /*
  * getBgColour() gets the most prevalent colour in a byte array.
  */
-static CARD32
+static uint32_t
 getBgColour(data,size,bpp)
     char *data;
     int size;
@@ -320,13 +318,13 @@ getBgColour(data,size,bpp)
   int i,j,k;
 
   int maxcount = 0;
-  CARD8 maxclr = 0;
+  uint8_t maxclr = 0;
 
   if (bpp != 8) {
     if (bpp == 16) {
-      return ((CARD16 *)data)[0];
+      return ((uint16_t *)data)[0];
     } else if (bpp == 32) {
-      return ((CARD32 *)data)[0];
+      return ((uint32_t *)data)[0];
     } else {
       rfbLog("getBgColour: bpp %d?\n",bpp);
       exit(1);
@@ -338,7 +336,7 @@ getBgColour(data,size,bpp)
   }
 
   for (j=0; j<size; j++) {
-    k = (int)(((CARD8 *)data)[j]);
+    k = (int)(((uint8_t *)data)[j]);
     if (k >= NUMCLRS) {
       rfbLog("getBgColour: unusual colour = %d\n", k);
       exit(1);
@@ -346,7 +344,7 @@ getBgColour(data,size,bpp)
     counts[k] += 1;
     if (counts[k] > maxcount) {
       maxcount = counts[k];
-      maxclr = ((CARD8 *)data)[j];
+      maxclr = ((uint8_t *)data)[j];
     }
   }
   
diff --git a/cursor.c b/cursor.c
index 7408a8a..032d2c4 100644
--- a/cursor.c
+++ b/cursor.c
@@ -40,8 +40,8 @@ rfbSendCursorShape(cl)
     int saved_ublen;
     int bitmapRowBytes, maskBytes, dataBytes;
     int i, j;
-    CARD8 *bitmapData;
-    CARD8 bitmapByte;
+    uint8_t *bitmapData;
+    uint8_t bitmapByte;
 
     pCursor = cl->screen->getCursorPtr(cl);
     /*if(!pCursor) return TRUE;*/
@@ -131,7 +131,7 @@ rfbSendCursorShape(cl)
 	memcpy(&cl->updateBuf[cl->ublen], (char *)&colors, sz_rfbXCursorColors);
 	cl->ublen += sz_rfbXCursorColors;
 
-	bitmapData = (CARD8 *)pCursor->source;
+	bitmapData = (uint8_t *)pCursor->source;
 
 	for (i = 0; i < pCursor->height; i++) {
 	    for (j = 0; j < bitmapRowBytes; j++) {
@@ -154,7 +154,7 @@ rfbSendCursorShape(cl)
 
     /* Prepare transparency mask. */
 
-    bitmapData = (CARD8 *)pCursor->mask;
+    bitmapData = (uint8_t *)pCursor->mask;
 
     for (i = 0; i < pCursor->height; i++) {
 	for (j = 0; j < bitmapRowBytes; j++) {
@@ -322,7 +322,7 @@ void MakeXCursorFromRichCursor(rfbScreenInfoPtr rfbScreen,rfbCursorPtr cursor)
    rfbPixelFormat* format=&rfbScreen->rfbServerFormat;
    int i,j,w=(cursor->width+7)/8,bpp=format->bitsPerPixel/8,
      width=cursor->width*bpp;
-   CARD32 background;
+   uint32_t background;
    char *back=(char*)&background;
    unsigned char bit;
    
@@ -344,7 +344,7 @@ void MakeRichCursorFromXCursor(rfbScreenInfoPtr rfbScreen,rfbCursorPtr cursor)
 {
    rfbPixelFormat* format=&rfbScreen->rfbServerFormat;
    int i,j,w=(cursor->width+7)/8,bpp=format->bitsPerPixel/8;
-   CARD32 background,foreground;
+   uint32_t background,foreground;
    char *back=(char*)&background,*fore=(char*)&foreground;
    unsigned char *cp;
    unsigned char bit;
diff --git a/cutpaste.c b/cutpaste.c
index 7dcefe7..60cce2b 100644
--- a/cutpaste.c
+++ b/cutpaste.c
@@ -23,7 +23,6 @@
  *  USA.
  */
 
-#include <stdio.h>
 #include "rfb.h"
 
 
diff --git a/hextile.c b/hextile.c
index 6166844..2e314be 100644
--- a/hextile.c
+++ b/hextile.c
@@ -25,7 +25,6 @@
  *  USA.
  */
 
-#include <stdio.h>
 #include "rfb.h"
 
 static Bool sendHextiles8(rfbClientPtr cl, int x, int y, int w, int h);
@@ -90,10 +89,10 @@ rfbSendRectEncodingHextile(cl, x, y, w, h)
 #define DEFINE_SEND_HEXTILES(bpp)                                               \
                                                                                 \
                                                                                 \
-static Bool subrectEncode##bpp(rfbClientPtr cli, CARD##bpp *data, int w, int h, \
-                               CARD##bpp bg, CARD##bpp fg, Bool mono);          \
-static void testColours##bpp(CARD##bpp *data, int size, Bool *mono,             \
-                             Bool *solid, CARD##bpp *bg, CARD##bpp *fg);        \
+static Bool subrectEncode##bpp(rfbClientPtr cli, uint##bpp##_t *data, int w, int h, \
+                               uint##bpp##_t bg, uint##bpp##_t fg, Bool mono);          \
+static void testColours##bpp(uint##bpp##_t *data, int size, Bool *mono,             \
+                             Bool *solid, uint##bpp##_t *bg, uint##bpp##_t *fg);        \
                                                                                 \
                                                                                 \
 /*                                                                              \
@@ -108,11 +107,11 @@ sendHextiles##bpp(cl, rx, ry, rw, rh)
     int x, y, w, h;                                                             \
     int startUblen;                                                             \
     char *fbptr;                                                                \
-    CARD##bpp bg = 0, fg = 0, newBg, newFg;                                     \
+    uint##bpp##_t bg = 0, fg = 0, newBg, newFg;                                     \
     Bool mono, solid;                                                           \
     Bool validBg = FALSE;                                                       \
     Bool validFg = FALSE;                                                       \
-    CARD##bpp clientPixelData[16*16*(bpp/8)];                                   \
+    uint##bpp##_t clientPixelData[16*16*(bpp/8)];                                   \
                                                                                 \
     for (y = ry; y < ry+rh; y += 16) {                                          \
         for (x = rx; x < rx+rw; x += 16) {                                      \
@@ -194,16 +193,16 @@ sendHextiles##bpp(cl, rx, ry, rw, rh)
                                                                                 \
                                                                                 \
 static Bool                                                                     \
-subrectEncode##bpp(rfbClientPtr cl, CARD##bpp *data, int w, int h,              \
-                   CARD##bpp bg, CARD##bpp fg, Bool mono)                       \
+subrectEncode##bpp(rfbClientPtr cl, uint##bpp##_t *data, int w, int h,              \
+                   uint##bpp##_t bg, uint##bpp##_t fg, Bool mono)                       \
 {                                                                               \
-    CARD##bpp cl2;                                                              \
+    uint##bpp##_t cl2;                                                              \
     int x,y;                                                                    \
     int i,j;                                                                    \
     int hx=0,hy,vx=0,vy;                                                        \
     int hyflag;                                                                 \
-    CARD##bpp *seg;                                                             \
-    CARD##bpp *line;                                                            \
+    uint##bpp##_t *seg;                                                             \
+    uint##bpp##_t *line;                                                            \
     int hw,hh,vw,vh;                                                            \
     int thex,they,thew,theh;                                                    \
     int numsubs = 0;                                                            \
@@ -297,14 +296,14 @@ subrectEncode##bpp(rfbClientPtr cl, CARD##bpp *data, int w, int h,
                                                                                 \
 static void                                                                     \
 testColours##bpp(data,size,mono,solid,bg,fg)                                    \
-    CARD##bpp *data;                                                            \
+    uint##bpp##_t *data;                                                            \
     int size;                                                                   \
     Bool *mono;                                                                 \
     Bool *solid;                                                                \
-    CARD##bpp *bg;                                                              \
-    CARD##bpp *fg;                                                              \
+    uint##bpp##_t *bg;                                                              \
+    uint##bpp##_t *fg;                                                              \
 {                                                                               \
-    CARD##bpp colour1 = 0, colour2 = 0;                                         \
+    uint##bpp##_t colour1 = 0, colour2 = 0;                                         \
     int n1 = 0, n2 = 0;                                                         \
     *mono = TRUE;                                                               \
     *solid = TRUE;                                                              \
diff --git a/httpd.c b/httpd.c
index 328f416..15265c4 100644
--- a/httpd.c
+++ b/httpd.c
@@ -22,32 +22,43 @@
  *  USA.
  */
 
-#include <stdio.h>
+#include "rfb.h"
+
 #include <ctype.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+#include <errno.h>
+
 #ifdef WIN32
 #include <winsock.h>
 #define close closesocket
 #else
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <netdb.h>
-#include <pwd.h>
 #include <arpa/inet.h>
-#include <unistd.h>
 #endif
-#include <fcntl.h>
-#include <errno.h>
+#include <pwd.h>
+#endif
 
 #ifdef USE_LIBWRAP
 #include <tcpd.h>
 #endif
 
-#include "rfb.h"
-
 #define NOT_FOUND_STR "HTTP/1.0 404 Not found\r\n\r\n" \
     "<HEAD><TITLE>File Not Found</TITLE></HEAD>\n" \
     "<BODY><H1>File Not Found</H1></BODY>\n"
diff --git a/include/.cvsignore b/include/.cvsignore
index ba2c70c..cec07e0 100644
--- a/include/.cvsignore
+++ b/include/.cvsignore
@@ -1,4 +1,5 @@
 rfbconfig.h
 rfbconfig.h.in
-stamp-h1
+stamp-h*
+rfbint.h
 
diff --git a/include/rfb.h b/include/rfb.h
index 52a0804..e9f0bb9 100644
--- a/include/rfb.h
+++ b/include/rfb.h
@@ -36,52 +36,47 @@ extern "C"
 #include <stdlib.h>
 #include <string.h>
 #include "rfbconfig.h"
+#include "rfbint.h"
 #include "keysym.h"
 
 #ifdef HAVE_LIBZ
 #include <zlib.h>
 #endif
 
-/* TODO: this stuff has to go into autoconf */
-typedef unsigned char CARD8;
-typedef unsigned short CARD16;
-typedef unsigned int CARD32;
-typedef CARD32 Pixel;
-/* typedef CARD32 KeySym; */
-typedef unsigned long KeySym;
-#define SIGNED signed
-
-typedef signed char Bool;
-
-#undef FALSE
-#define FALSE 0
-#undef TRUE
-#define TRUE -1
-
 #include "rfbproto.h"
 
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
+
 #if defined(WIN32)
 #define WORDS_BIGENDIAN
 #undef Bool
 #define Bool int
-#endif
-
-#ifdef __sgi__
-typedef int socklen_t;
-#endif
-
-#ifdef WIN32
 #include <sys/timeb.h>
 #include <winsock.h>
 #undef SOCKET
 #define SOCKET int
 #else
 #define max(a,b) (((a)>(b))?(a):(b))
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
+#endif
 #define SOCKET int
+#ifndef Bool
+typedef int8_t Bool;
+#undef FALSE
+#define FALSE 0
+#undef TRUE
+#define TRUE -1
 #endif
+#endif
+
+typedef uint32_t KeySym;
+typedef uint32_t Pixel;
 
 #ifndef INADDR_NONE
 #define                INADDR_NONE     ((in_addr_t) 0xffffffff)
@@ -171,11 +166,11 @@ typedef enum rfbNewClientAction (*NewClientHookPtr)(struct _rfbClientRec* cl);
 typedef void (*DisplayHookPtr)(struct _rfbClientRec* cl);
 
 typedef struct {
-  CARD32 count;
+  uint32_t count;
   Bool is16; /* is the data format short? */
   union {
-    CARD8* bytes;
-    CARD16* shorts;
+    uint8_t* bytes;
+    uint16_t* shorts;
   } data; /* there have to be count*3 entries */
 } rfbColourMap;
 
@@ -390,7 +385,7 @@ typedef struct _rfbClientRec {
     int correMaxWidth, correMaxHeight;
 
     /* The following member is only used during VNC authentication */
-    CARD8 authChallenge[CHALLENGESIZE];
+    uint8_t authChallenge[CHALLENGESIZE];
 
     /* The following members represent the update needed to get the client's
        framebuffer from its present state to the current state of our
@@ -473,7 +468,7 @@ typedef struct _rfbClientRec {
 
     struct z_stream_s compStream;
     Bool compStreamInited;
-    CARD32 zlibCompressLevel;
+    uint32_t zlibCompressLevel;
 
 #ifdef HAVE_LIBJPEG
     /* tight encoding -- preserve zlib streams' state for each client */
diff --git a/include/rfbproto.h b/include/rfbproto.h
index bdb9670..d3e2916 100644
--- a/include/rfbproto.h
+++ b/include/rfbproto.h
@@ -60,10 +60,10 @@
  */
 
 typedef struct {
-    CARD16 x;
-    CARD16 y;
-    CARD16 w;
-    CARD16 h;
+    uint16_t x;
+    uint16_t y;
+    uint16_t w;
+    uint16_t h;
 } rfbRectangle;
 
 #define sz_rfbRectangle 8
@@ -75,32 +75,32 @@ typedef struct {
 
 typedef struct {
 
-    CARD8 bitsPerPixel;		/* 8,16,32 only */
+    uint8_t bitsPerPixel;		/* 8,16,32 only */
 
-    CARD8 depth;		/* 8 to 32 */
+    uint8_t depth;		/* 8 to 32 */
 
-    CARD8 bigEndian;		/* True if multi-byte pixels are interpreted
+    uint8_t bigEndian;		/* True if multi-byte pixels are interpreted
 				   as big endian, or if single-bit-per-pixel
 				   has most significant bit of the byte
 				   corresponding to first (leftmost) pixel. Of
 				   course this is meaningless for 8 bits/pix */
 
-    CARD8 trueColour;		/* If false then we need a "colour map" to
+    uint8_t trueColour;		/* If false then we need a "colour map" to
 				   convert pixels to RGB.  If true, xxxMax and
 				   xxxShift specify bits used for red, green
 				   and blue */
 
     /* the following fields are only meaningful if trueColour is true */
 
-    CARD16 redMax;		/* maximum red value (= 2^n - 1 where n is the
+    uint16_t redMax;		/* maximum red value (= 2^n - 1 where n is the
 				   number of bits used for red). Note this
 				   value is always in big endian order. */
 
-    CARD16 greenMax;		/* similar for green */
+    uint16_t greenMax;		/* similar for green */
 
-    CARD16 blueMax;		/* and blue */
+    uint16_t blueMax;		/* and blue */
 
-    CARD8 redShift;		/* number of shifts needed to get the red
+    uint8_t redShift;		/* number of shifts needed to get the red
 				   value in a pixel to the least significant
 				   bit. To find the red value from a given
 				   pixel, do the following:
@@ -112,12 +112,12 @@ typedef struct {
 				   4) You now have the red value between 0 and
 				      redMax. */
 
-    CARD8 greenShift;		/* similar for green */
+    uint8_t greenShift;		/* similar for green */
 
-    CARD8 blueShift;		/* and blue */
+    uint8_t blueShift;		/* and blue */
 
-    CARD8 pad1;
-    CARD16 pad2;
+    uint8_t pad1;
+    uint16_t pad2;
 
 } rfbPixelFormat;
 
@@ -216,7 +216,7 @@ typedef char rfbProtocolVersionMsg[13];	/* allow extra byte for null */
  */
 
 typedef struct {
-    CARD8 shared;
+    uint8_t shared;
 } rfbClientInitMsg;
 
 #define sz_rfbClientInitMsg 1
@@ -231,10 +231,10 @@ typedef struct {
  */
 
 typedef struct {
-    CARD16 framebufferWidth;
-    CARD16 framebufferHeight;
+    uint16_t framebufferWidth;
+    uint16_t framebufferHeight;
     rfbPixelFormat format;	/* the server's preferred pixel format */
-    CARD32 nameLength;
+    uint32_t nameLength;
     /* followed by char name[nameLength] */
 } rfbServerInitMsg;
 
@@ -363,9 +363,9 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbFramebufferUpdate */
-    CARD8 pad;
-    CARD16 nRects;
+    uint8_t type;			/* always rfbFramebufferUpdate */
+    uint8_t pad;
+    uint16_t nRects;
     /* followed by nRects rectangles */
 } rfbFramebufferUpdateMsg;
 
@@ -381,7 +381,7 @@ typedef struct {
 
 typedef struct {
     rfbRectangle r;
-    CARD32 encoding;	/* one of the encoding types rfbEncoding... */
+    uint32_t encoding;	/* one of the encoding types rfbEncoding... */
 } rfbFramebufferUpdateRectHeader;
 
 #define sz_rfbFramebufferUpdateRectHeader (sz_rfbRectangle + 4)
@@ -399,8 +399,8 @@ typedef struct {
  */
 
 typedef struct {
-    CARD16 srcX;
-    CARD16 srcY;
+    uint16_t srcX;
+    uint16_t srcY;
 } rfbCopyRect;
 
 #define sz_rfbCopyRect 4
@@ -414,7 +414,7 @@ typedef struct {
  */
 
 typedef struct {
-    CARD32 nSubrects;
+    uint32_t nSubrects;
 } rfbRREHeader;
 
 #define sz_rfbRREHeader 4
@@ -429,10 +429,10 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 x;
-    CARD8 y;
-    CARD8 w;
-    CARD8 h;
+    uint8_t x;
+    uint8_t y;
+    uint8_t w;
+    uint8_t h;
 } rfbCoRRERectangle;
 
 #define sz_rfbCoRRERectangle 4
@@ -498,7 +498,7 @@ typedef struct {
  */
 
 typedef struct {
-    CARD32 nBytes;
+    uint32_t nBytes;
 } rfbZlibHeader;
 
 #define sz_rfbZlibHeader 4
@@ -661,12 +661,12 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 foreRed;
-    CARD8 foreGreen;
-    CARD8 foreBlue;
-    CARD8 backRed;
-    CARD8 backGreen;
-    CARD8 backBlue;
+    uint8_t foreRed;
+    uint8_t foreGreen;
+    uint8_t foreBlue;
+    uint8_t backRed;
+    uint8_t backGreen;
+    uint8_t backBlue;
 } rfbXCursorColors;
 
 #define sz_rfbXCursorColors 6
@@ -692,7 +692,7 @@ typedef struct {
  */
 
 typedef struct {
-    CARD32 length;
+    uint32_t length;
 } rfbZRLEHeader;
 
 #define sz_rfbZRLEHeader 4
@@ -711,12 +711,12 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbSetColourMapEntries */
-    CARD8 pad;
-    CARD16 firstColour;
-    CARD16 nColours;
+    uint8_t type;			/* always rfbSetColourMapEntries */
+    uint8_t pad;
+    uint16_t firstColour;
+    uint16_t nColours;
 
-    /* Followed by nColours * 3 * CARD16
+    /* Followed by nColours * 3 * uint16_t
        r1, g1, b1, r2, g2, b2, r3, g3, b3, ..., rn, bn, gn */
 
 } rfbSetColourMapEntriesMsg;
@@ -730,7 +730,7 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbBell */
+    uint8_t type;			/* always rfbBell */
 } rfbBellMsg;
 
 #define sz_rfbBellMsg 1
@@ -742,10 +742,10 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbServerCutText */
-    CARD8 pad1;
-    CARD16 pad2;
-    CARD32 length;
+    uint8_t type;			/* always rfbServerCutText */
+    uint8_t pad1;
+    uint16_t pad2;
+    uint32_t length;
     /* followed by char text[length] */
 } rfbServerCutTextMsg;
 
@@ -762,7 +762,7 @@ typedef rfbServerCutTextMsg rfbBackChannelMsg;
  */
 
 typedef union {
-    CARD8 type;
+    uint8_t type;
     rfbFramebufferUpdateMsg fu;
     rfbSetColourMapEntriesMsg scme;
     rfbBellMsg b;
@@ -784,9 +784,9 @@ typedef union {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbSetPixelFormat */
-    CARD8 pad1;
-    CARD16 pad2;
+    uint8_t type;			/* always rfbSetPixelFormat */
+    uint8_t pad1;
+    uint16_t pad2;
     rfbPixelFormat format;
 } rfbSetPixelFormatMsg;
 
@@ -801,12 +801,12 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbFixColourMapEntries */
-    CARD8 pad;
-    CARD16 firstColour;
-    CARD16 nColours;
+    uint8_t type;			/* always rfbFixColourMapEntries */
+    uint8_t pad;
+    uint16_t firstColour;
+    uint16_t nColours;
 
-    /* Followed by nColours * 3 * CARD16
+    /* Followed by nColours * 3 * uint16_t
        r1, g1, b1, r2, g2, b2, r3, g3, b3, ..., rn, bn, gn */
 
 } rfbFixColourMapEntriesMsg;
@@ -821,10 +821,10 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbSetEncodings */
-    CARD8 pad;
-    CARD16 nEncodings;
-    /* followed by nEncodings * CARD32 encoding types */
+    uint8_t type;			/* always rfbSetEncodings */
+    uint8_t pad;
+    uint16_t nEncodings;
+    /* followed by nEncodings * uint32_t encoding types */
 } rfbSetEncodingsMsg;
 
 #define sz_rfbSetEncodingsMsg 4
@@ -837,12 +837,12 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbFramebufferUpdateRequest */
-    CARD8 incremental;
-    CARD16 x;
-    CARD16 y;
-    CARD16 w;
-    CARD16 h;
+    uint8_t type;			/* always rfbFramebufferUpdateRequest */
+    uint8_t incremental;
+    uint16_t x;
+    uint16_t y;
+    uint16_t w;
+    uint16_t h;
 } rfbFramebufferUpdateRequestMsg;
 
 #define sz_rfbFramebufferUpdateRequestMsg 10
@@ -880,10 +880,10 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbKeyEvent */
-    CARD8 down;			/* true if down (press), false if up */
-    CARD16 pad;
-    CARD32 key;			/* key is specified as an X keysym */
+    uint8_t type;			/* always rfbKeyEvent */
+    uint8_t down;			/* true if down (press), false if up */
+    uint16_t pad;
+    uint32_t key;			/* key is specified as an X keysym */
 } rfbKeyEventMsg;
 
 #define sz_rfbKeyEventMsg 8
@@ -894,10 +894,10 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbPointerEvent */
-    CARD8 buttonMask;		/* bits 0-7 are buttons 1-8, 0=up, 1=down */
-    CARD16 x;
-    CARD16 y;
+    uint8_t type;			/* always rfbPointerEvent */
+    uint8_t buttonMask;		/* bits 0-7 are buttons 1-8, 0=up, 1=down */
+    uint16_t x;
+    uint16_t y;
 } rfbPointerEventMsg;
 
 #define rfbButton1Mask 1
@@ -913,10 +913,10 @@ typedef struct {
  */
 
 typedef struct {
-    CARD8 type;			/* always rfbClientCutText */
-    CARD8 pad1;
-    CARD16 pad2;
-    CARD32 length;
+    uint8_t type;			/* always rfbClientCutText */
+    uint8_t pad1;
+    uint16_t pad2;
+    uint32_t length;
     /* followed by char text[length] */
 } rfbClientCutTextMsg;
 
@@ -929,7 +929,7 @@ typedef struct {
  */
 
 typedef union {
-    CARD8 type;
+    uint8_t type;
     rfbSetPixelFormatMsg spf;
     rfbFixColourMapEntriesMsg fcme;
     rfbSetEncodingsMsg se;
diff --git a/main.c b/main.c
index 59ddf93..be2e270 100644
--- a/main.c
+++ b/main.c
@@ -10,8 +10,6 @@
  *  see GPL (latest version) for full details
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include <stdarg.h>
 #include <errno.h>
 
@@ -20,12 +18,16 @@
 #define true -1
 #endif
 
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
+
 #ifndef WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <unistd.h>
 #endif
+
 #include <signal.h>
 #include <time.h>
 
@@ -734,7 +736,7 @@ void rfbInitServer(rfbScreenInfoPtr rfbScreen)
   httpInitSockets(rfbScreen);
 }
 
-#ifdef WIN32
+#ifndef HAVE_GETTIMEOFDAY
 #include <fcntl.h>
 #include <conio.h>
 #include <sys/timeb.h>
diff --git a/rfbserver.c b/rfbserver.c
index 09be38e..68a7649 100644
--- a/rfbserver.c
+++ b/rfbserver.c
@@ -24,23 +24,30 @@
  *  USA.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include "rfb.h"
 #include "sraRegion.h"
+
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
 #ifdef WIN32
 #define write(sock,buf,len) send(sock,buf,len,0)
 #else
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <pwd.h>
+#ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
 #endif
-#include <fcntl.h>
-#include <sys/types.h>
+#endif
 
 #ifdef CORBA
 #include <vncserverctrl.h>
@@ -538,8 +545,8 @@ rfbClientConnFailed(cl, reason)
     int len = strlen(reason);
 
     buf = (char *)malloc(8 + len);
-    ((CARD32 *)buf)[0] = Swap32IfLE(rfbConnFailed);
-    ((CARD32 *)buf)[1] = Swap32IfLE(len);
+    ((uint32_t *)buf)[0] = Swap32IfLE(rfbConnFailed);
+    ((uint32_t *)buf)[1] = Swap32IfLE(len);
     memcpy(buf + 8, reason, len);
 
     if (WriteExact(cl, buf, 8 + len) < 0)
@@ -692,7 +699,7 @@ rfbProcessClientNormalMessage(cl)
     case rfbSetEncodings:
     {
         int i;
-        CARD32 enc;
+        uint32_t enc;
 
         if ((n = ReadExact(cl, ((char *)&msg) + 1,
                            sz_rfbSetEncodingsMsg - 1)) <= 0) {
@@ -828,15 +835,15 @@ rfbProcessClientNormalMessage(cl)
 #endif
             default:
 #ifdef HAVE_LIBZ
-		if ( enc >= (CARD32)rfbEncodingCompressLevel0 &&
-		     enc <= (CARD32)rfbEncodingCompressLevel9 ) {
+		if ( enc >= (uint32_t)rfbEncodingCompressLevel0 &&
+		     enc <= (uint32_t)rfbEncodingCompressLevel9 ) {
 		    cl->zlibCompressLevel = enc & 0x0F;
 #ifdef HAVE_LIBJPEG
 		    cl->tightCompressLevel = enc & 0x0F;
 		    rfbLog("Using compression level %d for client %s\n",
 			   cl->tightCompressLevel, cl->host);
-		} else if ( enc >= (CARD32)rfbEncodingQualityLevel0 &&
-			    enc <= (CARD32)rfbEncodingQualityLevel9 ) {
+		} else if ( enc >= (uint32_t)rfbEncodingQualityLevel0 &&
+			    enc <= (uint32_t)rfbEncodingQualityLevel9 ) {
 		    cl->tightQualityLevel = enc & 0x0F;
 		    rfbLog("Using image quality level %d for client %s\n",
 			   cl->tightQualityLevel, cl->host);
@@ -1178,7 +1185,7 @@ rfbSendFramebufferUpdate(cl, givenUpdateRegion)
 
     fu->type = rfbFramebufferUpdate;
     if (nUpdateRegionRects != 0xFFFF) {
-	fu->nRects = Swap16IfLE((CARD16)(sraRgnCountRects(updateCopyRegion) +
+	fu->nRects = Swap16IfLE((uint16_t)(sraRgnCountRects(updateCopyRegion) +
 					 nUpdateRegionRects +
 					 !!sendCursorShape + !!sendCursorPos));
     } else {
@@ -1516,7 +1523,7 @@ rfbSendSetColourMapEntries(cl, firstColour, nColours)
 {
     char buf[sz_rfbSetColourMapEntriesMsg + 256 * 3 * 2];
     rfbSetColourMapEntriesMsg *scme = (rfbSetColourMapEntriesMsg *)buf;
-    CARD16 *rgb = (CARD16 *)(&buf[sz_rfbSetColourMapEntriesMsg]);
+    uint16_t *rgb = (uint16_t *)(&buf[sz_rfbSetColourMapEntriesMsg]);
     rfbColourMap* cm = &cl->screen->colourMap;
     
     int i, len;
diff --git a/rre.c b/rre.c
index 9a552cb..df20668 100644
--- a/rre.c
+++ b/rre.c
@@ -26,7 +26,6 @@
  *  USA.
  */
 
-#include <stdio.h>
 #include "rfb.h"
 
 /*
@@ -43,10 +42,10 @@ static int rreAfterBufSize = 0;
 static char *rreAfterBuf = NULL;
 static int rreAfterBufLen;
 
-static int subrectEncode8(CARD8 *data, int w, int h);
-static int subrectEncode16(CARD16 *data, int w, int h);
-static int subrectEncode32(CARD32 *data, int w, int h);
-static CARD32 getBgColour(char *data, int size, int bpp);
+static int subrectEncode8(uint8_t *data, int w, int h);
+static int subrectEncode16(uint16_t *data, int w, int h);
+static int subrectEncode32(uint32_t *data, int w, int h);
+static uint32_t getBgColour(char *data, int size, int bpp);
 
 
 /*
@@ -91,13 +90,13 @@ rfbSendRectEncodingRRE(cl, x, y, w, h)
 
     switch (cl->format.bitsPerPixel) {
     case 8:
-        nSubrects = subrectEncode8((CARD8 *)rreBeforeBuf, w, h);
+        nSubrects = subrectEncode8((uint8_t *)rreBeforeBuf, w, h);
         break;
     case 16:
-        nSubrects = subrectEncode16((CARD16 *)rreBeforeBuf, w, h);
+        nSubrects = subrectEncode16((uint16_t *)rreBeforeBuf, w, h);
         break;
     case 32:
-        nSubrects = subrectEncode32((CARD32 *)rreBeforeBuf, w, h);
+        nSubrects = subrectEncode32((uint32_t *)rreBeforeBuf, w, h);
         break;
     default:
         rfbLog("getBgColour: bpp %d?\n",cl->format.bitsPerPixel);
@@ -177,25 +176,25 @@ rfbSendRectEncodingRRE(cl, x, y, w, h)
 #define DEFINE_SUBRECT_ENCODE(bpp)                                            \
 static int                                                                    \
 subrectEncode##bpp(data,w,h)                                                  \
-    CARD##bpp *data;                                                          \
+    uint##bpp##_t *data;                                                          \
     int w;                                                                    \
     int h;                                                                    \
 {                                                                             \
-    CARD##bpp cl;                                                             \
+    uint##bpp##_t cl;                                                             \
     rfbRectangle subrect;                                                     \
     int x,y;                                                                  \
     int i,j;                                                                  \
     int hx=0,hy,vx=0,vy;                                                      \
     int hyflag;                                                               \
-    CARD##bpp *seg;                                                           \
-    CARD##bpp *line;                                                          \
+    uint##bpp##_t *seg;                                                           \
+    uint##bpp##_t *line;                                                          \
     int hw,hh,vw,vh;                                                          \
     int thex,they,thew,theh;                                                  \
     int numsubs = 0;                                                          \
     int newLen;                                                               \
-    CARD##bpp bg = (CARD##bpp)getBgColour((char*)data,w*h,bpp);               \
+    uint##bpp##_t bg = (uint##bpp##_t)getBgColour((char*)data,w*h,bpp);               \
                                                                               \
-    *((CARD##bpp*)rreAfterBuf) = bg;                                          \
+    *((uint##bpp##_t*)rreAfterBuf) = bg;                                          \
                                                                               \
     rreAfterBufLen = (bpp/8);                                                 \
                                                                               \
@@ -247,7 +246,7 @@ subrectEncode##bpp(data,w,h)                                                  \
             return -1;                                                        \
                                                                               \
           numsubs += 1;                                                       \
-          *((CARD##bpp*)(rreAfterBuf + rreAfterBufLen)) = cl;                 \
+          *((uint##bpp##_t*)(rreAfterBuf + rreAfterBufLen)) = cl;                 \
           rreAfterBufLen += (bpp/8);                                          \
           memcpy(&rreAfterBuf[rreAfterBufLen],&subrect,sz_rfbRectangle);      \
           rreAfterBufLen += sz_rfbRectangle;                                  \
@@ -275,7 +274,7 @@ DEFINE_SUBRECT_ENCODE(32)
 /*
  * getBgColour() gets the most prevalent colour in a byte array.
  */
-static CARD32
+static uint32_t
 getBgColour(data,size,bpp)
     char *data;
     int size;
@@ -288,13 +287,13 @@ getBgColour(data,size,bpp)
   int i,j,k;
 
   int maxcount = 0;
-  CARD8 maxclr = 0;
+  uint8_t maxclr = 0;
 
   if (bpp != 8) {
     if (bpp == 16) {
-      return ((CARD16 *)data)[0];
+      return ((uint16_t *)data)[0];
     } else if (bpp == 32) {
-      return ((CARD32 *)data)[0];
+      return ((uint32_t *)data)[0];
     } else {
       rfbLog("getBgColour: bpp %d?\n",bpp);
       exit(1);
@@ -306,7 +305,7 @@ getBgColour(data,size,bpp)
   }
 
   for (j=0; j<size; j++) {
-    k = (int)(((CARD8 *)data)[j]);
+    k = (int)(((uint8_t *)data)[j]);
     if (k >= NUMCLRS) {
       rfbLog("getBgColour: unusual colour = %d\n", k);
       exit(1);
@@ -314,7 +313,7 @@ getBgColour(data,size,bpp)
     counts[k] += 1;
     if (counts[k] > maxcount) {
       maxcount = counts[k];
-      maxclr = ((CARD8 *)data)[j];
+      maxclr = ((uint8_t *)data)[j];
     }
   }
   
diff --git a/sockets.c b/sockets.c
index 0c560f2..3f4d69b 100644
--- a/sockets.c
+++ b/sockets.c
@@ -39,8 +39,12 @@
  *  USA.
  */
 
-#include <stdio.h>
+#include "rfb.h"
+
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
+
 #ifdef WIN32
 #pragma warning (disable: 4018 4761)
 #define close closesocket
@@ -49,14 +53,23 @@
 #define ETIMEDOUT WSAETIMEDOUT
 #define write(sock,buf,len) send(sock,buf,len,0)
 #else
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <netdb.h>
-#include <unistd.h>
 #include <arpa/inet.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#endif
+
 #if defined(__linux__) && defined(NEED_TIMEVAL)
 struct timeval 
 {
@@ -64,7 +77,11 @@ struct timeval
 }
 ;
 #endif
+
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
+#endif
+
 #include <errno.h>
 
 #ifdef USE_LIBWRAP
@@ -74,8 +91,6 @@ int allow_severity=LOG_INFO;
 int deny_severity=LOG_WARNING;
 #endif
 
-#include "rfb.h"
-
 /*#ifndef WIN32
 int max(int i,int j) { return(i<j?j:i); }
 #endif
diff --git a/sraRegion.c b/sraRegion.c
index 47f3e78..d62a404 100755
--- a/sraRegion.c
+++ b/sraRegion.c
@@ -8,9 +8,6 @@
 #include "rfb.h"
 #include "sraRegion.h"
 
-#include <stdlib.h>
-#include <stdio.h>
-
 /* -=- Internal Span structure */
 
 struct sraRegion;
diff --git a/stats.c b/stats.c
index 7d8b6b6..223607d 100644
--- a/stats.c
+++ b/stats.c
@@ -24,8 +24,6 @@
  *  USA.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include "rfb.h"
 
 static const char* encNames[] = {
diff --git a/tableinit24.c b/tableinit24.c
index f1e63a5..39e9920 100644
--- a/tableinit24.c
+++ b/tableinit24.c
@@ -24,22 +24,22 @@
  */
 
 static void
-rfbInitOneRGBTable24 (CARD8 *table, int inMax, int outMax, int outShift,int swap);
+rfbInitOneRGBTable24 (uint8_t *table, int inMax, int outMax, int outShift,int swap);
 
 
 static void
 rfbInitColourMapSingleTable24(char **table, rfbPixelFormat *in,
                             rfbPixelFormat *out,rfbColourMap* colourMap)
 {
-    CARD32 i, r, g, b, outValue;
-    CARD8 *t;
-    CARD8 c;
+    uint32_t i, r, g, b, outValue;
+    uint8_t *t;
+    uint8_t c;
     unsigned int nEntries = 1 << in->bitsPerPixel;
     int shift = colourMap->is16?16:8;
 
     if (*table) free(*table);
     *table = (char *)malloc(nEntries * 3 + 1);
-    t = (CARD8 *)*table;
+    t = (uint8_t *)*table;
 
     for (i = 0; i < nEntries; i++) {
         r = g = b = 0;
@@ -57,7 +57,7 @@ rfbInitColourMapSingleTable24(char **table, rfbPixelFormat *in,
         outValue = ((((r * (1 + out->redMax)) >> shift) << out->redShift) |
                 (((g * (1 + out->greenMax)) >> shift) << out->greenShift) |
                 (((b * (1 + out->blueMax)) >> shift) << out->blueShift));
-	*(CARD32*)&t[3*i] = outValue;
+	*(uint32_t*)&t[3*i] = outValue;
 	if(!rfbEndianTest)
 	  memmove(t+3*i,t+3*i+1,3);
         if (out->bigEndian != in->bigEndian) {
@@ -77,13 +77,13 @@ rfbInitTrueColourSingleTable24 (char **table, rfbPixelFormat *in,
 {
     int i,outValue;
     int inRed, inGreen, inBlue, outRed, outGreen, outBlue;
-    CARD8 *t;
-    CARD8 c;
+    uint8_t *t;
+    uint8_t c;
     int nEntries = 1 << in->bitsPerPixel;
 
     if (*table) free(*table);
     *table = (char *)malloc(nEntries * 3 + 1);
-    t = (CARD8 *)*table;
+    t = (uint8_t *)*table;
 
     for (i = 0; i < nEntries; i++) {
         inRed   = (i >> in->redShift)   & in->redMax;
@@ -97,7 +97,7 @@ rfbInitTrueColourSingleTable24 (char **table, rfbPixelFormat *in,
 	outValue = ((outRed   << out->redShift)   |
                 (outGreen << out->greenShift) |
                 (outBlue  << out->blueShift));
-	*(CARD32*)&t[3*i] = outValue;
+	*(uint32_t*)&t[3*i] = outValue;
 	if(!rfbEndianTest)
 	  memmove(t+3*i,t+3*i+1,3);
         if (out->bigEndian != in->bigEndian) {
@@ -116,14 +116,14 @@ static void
 rfbInitTrueColourRGBTables24 (char **table, rfbPixelFormat *in,
                                rfbPixelFormat *out)
 {
-    CARD8 *redTable;
-    CARD8 *greenTable;
-    CARD8 *blueTable;
+    uint8_t *redTable;
+    uint8_t *greenTable;
+    uint8_t *blueTable;
 
     if (*table) free(*table);
     *table = (char *)malloc((in->redMax + in->greenMax + in->blueMax + 3)
                             * 3 + 1);
-    redTable = (CARD8 *)*table;
+    redTable = (uint8_t *)*table;
     greenTable = redTable + 3*(in->redMax + 1);
     blueTable = greenTable + 3*(in->greenMax + 1);
 
@@ -136,17 +136,17 @@ rfbInitTrueColourRGBTables24 (char **table, rfbPixelFormat *in,
 }
 
 static void
-rfbInitOneRGBTable24 (CARD8 *table, int inMax, int outMax, int outShift,
+rfbInitOneRGBTable24 (uint8_t *table, int inMax, int outMax, int outShift,
                        int swap)
 {
     int i;
     int nEntries = inMax + 1;
-    CARD32 outValue;
-    CARD8 c;
+    uint32_t outValue;
+    uint8_t c;
 
     for (i = 0; i < nEntries; i++) {
       outValue = ((i * outMax + inMax / 2) / inMax) << outShift;
-      *(CARD32 *)&table[3*i] = outValue;
+      *(uint32_t *)&table[3*i] = outValue;
       if(!rfbEndianTest)
 	memmove(table+3*i,table+3*i+1,3);
         if (swap) {
diff --git a/tableinitcmtemplate.c b/tableinitcmtemplate.c
index 2d10ea5..df01b23 100644
--- a/tableinitcmtemplate.c
+++ b/tableinitcmtemplate.c
@@ -37,7 +37,7 @@
 #error "It is included as part of translate.c"
 #endif
 
-#define OUT_T CONCAT2E(CARD,OUT)
+#define OUT_T CONCAT3E(uint,OUT,_t)
 #define SwapOUT(x) CONCAT2E(Swap,OUT(x))
 #define rfbInitColourMapSingleTableOUT \
                                 CONCAT2E(rfbInitColourMapSingleTable,OUT)
@@ -46,9 +46,9 @@ static void
 rfbInitColourMapSingleTableOUT(char **table, rfbPixelFormat *in,
                             rfbPixelFormat *out,rfbColourMap* colourMap)
 {
-    CARD32 i, r, g, b;
+    uint32_t i, r, g, b;
     OUT_T *t;
-    CARD32 nEntries = 1 << in->bitsPerPixel;
+    uint32_t nEntries = 1 << in->bitsPerPixel;
     int shift = colourMap->is16?16:8;
 
     if (*table) free(*table);
diff --git a/tableinittctemplate.c b/tableinittctemplate.c
index 93223d9..8d4f742 100644
--- a/tableinittctemplate.c
+++ b/tableinittctemplate.c
@@ -39,7 +39,7 @@
 #error "It is included as part of translate.c"
 #endif
 
-#define OUT_T CONCAT2E(CARD,OUT)
+#define OUT_T CONCAT3E(uint,OUT,_t)
 #define SwapOUT(x) CONCAT2E(Swap,OUT(x))
 #define rfbInitTrueColourSingleTableOUT \
                                 CONCAT2E(rfbInitTrueColourSingleTable,OUT)
diff --git a/tabletrans24template.c b/tabletrans24template.c
index 44a37cb..4b3a0a0 100644
--- a/tabletrans24template.c
+++ b/tabletrans24template.c
@@ -53,19 +53,19 @@ rfbTranslateWithSingleTable24to24 (char *table, rfbPixelFormat *in,
                                     int bytesBetweenInputLines,
                                     int width, int height)
 {
-    CARD8 *ip = (CARD8 *)iptr;
-    CARD8 *op = (CARD8 *)optr;
+    uint8_t *ip = (uint8_t *)iptr;
+    uint8_t *op = (uint8_t *)optr;
     int ipextra = bytesBetweenInputLines - width * 3;
-    CARD8 *opLineEnd;
-    CARD8 *t = (CARD8 *)table;
+    uint8_t *opLineEnd;
+    uint8_t *t = (uint8_t *)table;
     int shift = rfbEndianTest?0:8;
-    CARD8 c;
+    uint8_t c;
 
     while (height > 0) {
         opLineEnd = op + width*3;
 
         while (op < opLineEnd) {
-	    *(CARD32*)op = t[((*(CARD32 *)ip)>>shift)&0x00ffffff];
+	    *(uint32_t*)op = t[((*(uint32_t *)ip)>>shift)&0x00ffffff];
 	    if(!rfbEndianTest)
 	      memmove(op,op+1,3);
 	    if (out->bigEndian != in->bigEndian) {
@@ -92,21 +92,21 @@ rfbTranslateWithRGBTables24to24 (char *table, rfbPixelFormat *in,
                                   int bytesBetweenInputLines,
                                   int width, int height)
 {
-    CARD8 *ip = (CARD8 *)iptr;
-    CARD8 *op = (CARD8 *)optr;
+    uint8_t *ip = (uint8_t *)iptr;
+    uint8_t *op = (uint8_t *)optr;
     int ipextra = bytesBetweenInputLines - width*3;
-    CARD8 *opLineEnd;
-    CARD8 *redTable = (CARD8 *)table;
-    CARD8 *greenTable = redTable + 3*(in->redMax + 1);
-    CARD8 *blueTable = greenTable + 3*(in->greenMax + 1);
-    CARD32 outValue,inValue;
+    uint8_t *opLineEnd;
+    uint8_t *redTable = (uint8_t *)table;
+    uint8_t *greenTable = redTable + 3*(in->redMax + 1);
+    uint8_t *blueTable = greenTable + 3*(in->greenMax + 1);
+    uint32_t outValue,inValue;
     int shift = rfbEndianTest?0:8;
 
     while (height > 0) {
         opLineEnd = op+3*width;
 
         while (op < opLineEnd) {
-	    inValue = ((*(CARD32 *)ip)>>shift)&0x00ffffff;
+	    inValue = ((*(uint32_t *)ip)>>shift)&0x00ffffff;
             outValue = (redTable[(inValue >> in->redShift) & in->redMax] |
                        greenTable[(inValue >> in->greenShift) & in->greenMax] |
                        blueTable[(inValue >> in->blueShift) & in->blueMax]);
@@ -121,8 +121,8 @@ rfbTranslateWithRGBTables24to24 (char *table, rfbPixelFormat *in,
 
 #else
 
-#define IN_T CONCAT2E(CARD,BPP)
-#define OUT_T CONCAT2E(CARD,BPP)
+#define IN_T CONCAT3E(uint,BPP,_t)
+#define OUT_T CONCAT3E(uint,BPP,_t)
 #define rfbTranslateWithSingleTable24toOUT \
                                 CONCAT4E(rfbTranslateWithSingleTable,24,to,BPP)
 #define rfbTranslateWithSingleTableINto24 \
@@ -144,7 +144,7 @@ rfbTranslateWithSingleTable24toOUT (char *table, rfbPixelFormat *in,
                                     int bytesBetweenInputLines,
                                     int width, int height)
 {
-    CARD8 *ip = (CARD8 *)iptr;
+    uint8_t *ip = (uint8_t *)iptr;
     OUT_T *op = (OUT_T *)optr;
     int ipextra = bytesBetweenInputLines - width*3;
     OUT_T *opLineEnd;
@@ -155,7 +155,7 @@ rfbTranslateWithSingleTable24toOUT (char *table, rfbPixelFormat *in,
         opLineEnd = op + width;
 
         while (op < opLineEnd) {
-            *(op++) = t[((*(CARD32 *)ip)>>shift)&0x00ffffff];
+            *(op++) = t[((*(uint32_t *)ip)>>shift)&0x00ffffff];
 	    ip+=3;
         }
 
@@ -177,21 +177,21 @@ rfbTranslateWithRGBTables24toOUT (char *table, rfbPixelFormat *in,
                                   int bytesBetweenInputLines,
                                   int width, int height)
 {
-    CARD8 *ip = (CARD8 *)iptr;
+    uint8_t *ip = (uint8_t *)iptr;
     OUT_T *op = (OUT_T *)optr;
     int ipextra = bytesBetweenInputLines - width*3;
     OUT_T *opLineEnd;
     OUT_T *redTable = (OUT_T *)table;
     OUT_T *greenTable = redTable + in->redMax + 1;
     OUT_T *blueTable = greenTable + in->greenMax + 1;
-    CARD32 inValue;
+    uint32_t inValue;
     int shift = rfbEndianTest?0:8;
 
     while (height > 0) {
         opLineEnd = &op[width];
 
         while (op < opLineEnd) {
-	    inValue = ((*(CARD32 *)ip)>>shift)&0x00ffffff;
+	    inValue = ((*(uint32_t *)ip)>>shift)&0x00ffffff;
             *(op++) = (redTable[(inValue >> in->redShift) & in->redMax] |
                        greenTable[(inValue >> in->greenShift) & in->greenMax] |
                        blueTable[(inValue >> in->blueShift) & in->blueMax]);
@@ -215,10 +215,10 @@ rfbTranslateWithSingleTableINto24 (char *table, rfbPixelFormat *in,
                                     int width, int height)
 {
     IN_T *ip = (IN_T *)iptr;
-    CARD8 *op = (CARD8 *)optr;
+    uint8_t *op = (uint8_t *)optr;
     int ipextra = bytesBetweenInputLines / sizeof(IN_T) - width;
-    CARD8 *opLineEnd;
-    CARD8 *t = (CARD8 *)table;
+    uint8_t *opLineEnd;
+    uint8_t *t = (uint8_t *)table;
 
     while (height > 0) {
         opLineEnd = op + width * 3;
@@ -247,13 +247,13 @@ rfbTranslateWithRGBTablesINto24 (char *table, rfbPixelFormat *in,
                                   int width, int height)
 {
     IN_T *ip = (IN_T *)iptr;
-    CARD8 *op = (CARD8 *)optr;
+    uint8_t *op = (uint8_t *)optr;
     int ipextra = bytesBetweenInputLines / sizeof(IN_T) - width;
-    CARD8 *opLineEnd;
-    CARD8 *redTable = (CARD8 *)table;
-    CARD8 *greenTable = redTable + 3*(in->redMax + 1);
-    CARD8 *blueTable = greenTable + 3*(in->greenMax + 1);
-    CARD32 outValue;
+    uint8_t *opLineEnd;
+    uint8_t *redTable = (uint8_t *)table;
+    uint8_t *greenTable = redTable + 3*(in->redMax + 1);
+    uint8_t *blueTable = greenTable + 3*(in->greenMax + 1);
+    uint32_t outValue;
 
     while (height > 0) {
         opLineEnd = op+3*width;
diff --git a/tabletranstemplate.c b/tabletranstemplate.c
index 0aafff0..e83c623 100644
--- a/tabletranstemplate.c
+++ b/tabletranstemplate.c
@@ -39,8 +39,8 @@
 #error "It is included as part of translate.c"
 #endif
 
-#define IN_T CONCAT2E(CARD,IN)
-#define OUT_T CONCAT2E(CARD,OUT)
+#define IN_T CONCAT3E(uint,IN,_t)
+#define OUT_T CONCAT3E(uint,OUT,_t)
 #define rfbTranslateWithSingleTableINtoOUT \
                                 CONCAT4E(rfbTranslateWithSingleTable,IN,to,OUT)
 #define rfbTranslateWithRGBTablesINtoOUT \
diff --git a/tight.c b/tight.c
index 6f4997f..cdc0495 100644
--- a/tight.c
+++ b/tight.c
@@ -87,7 +87,7 @@ static int qualityLevel;
 typedef struct COLOR_LIST_s {
     struct COLOR_LIST_s *next;
     int idx;
-    CARD32 rgb;
+    uint32_t rgb;
 } COLOR_LIST;
 
 typedef struct PALETTE_ENTRY_s {
@@ -102,7 +102,7 @@ typedef struct PALETTE_s {
 } PALETTE;
 
 static int paletteNumColors, paletteMaxColors;
-static CARD32 monoBackground, monoForeground;
+static uint32_t monoBackground, monoForeground;
 static PALETTE palette;
 
 /* Pointers to dynamically-allocated buffers. */
@@ -119,18 +119,18 @@ static int *prevRowBuf = NULL;
 /* Prototypes for static functions. */
 
 static void FindBestSolidArea (rfbClientPtr cl, int x, int y, int w, int h,
-                               CARD32 colorValue, int *w_ptr, int *h_ptr);
+                               uint32_t colorValue, int *w_ptr, int *h_ptr);
 static void ExtendSolidArea   (rfbClientPtr cl, int x, int y, int w, int h,
-                               CARD32 colorValue,
+                               uint32_t colorValue,
                                int *x_ptr, int *y_ptr, int *w_ptr, int *h_ptr);
 static Bool CheckSolidTile    (rfbClientPtr cl, int x, int y, int w, int h,
-                               CARD32 *colorPtr, Bool needSameColor);
+                               uint32_t *colorPtr, Bool needSameColor);
 static Bool CheckSolidTile8   (rfbClientPtr cl, int x, int y, int w, int h,
-                               CARD32 *colorPtr, Bool needSameColor);
+                               uint32_t *colorPtr, Bool needSameColor);
 static Bool CheckSolidTile16  (rfbClientPtr cl, int x, int y, int w, int h,
-                               CARD32 *colorPtr, Bool needSameColor);
+                               uint32_t *colorPtr, Bool needSameColor);
 static Bool CheckSolidTile32  (rfbClientPtr cl, int x, int y, int w, int h,
-                               CARD32 *colorPtr, Bool needSameColor);
+                               uint32_t *colorPtr, Bool needSameColor);
 
 static Bool SendRectSimple    (rfbClientPtr cl, int x, int y, int w, int h);
 static Bool SendSubrect       (rfbClientPtr cl, int x, int y, int w, int h);
@@ -151,20 +151,20 @@ static void FillPalette16(int count);
 static void FillPalette32(int count);
 
 static void PaletteReset(void);
-static int PaletteInsert(CARD32 rgb, int numPixels, int bpp);
+static int PaletteInsert(uint32_t rgb, int numPixels, int bpp);
 
 static void Pack24(rfbClientPtr cl, char *buf, rfbPixelFormat *fmt, int count);
 
-static void EncodeIndexedRect16(CARD8 *buf, int count);
-static void EncodeIndexedRect32(CARD8 *buf, int count);
+static void EncodeIndexedRect16(uint8_t *buf, int count);
+static void EncodeIndexedRect32(uint8_t *buf, int count);
 
-static void EncodeMonoRect8(CARD8 *buf, int w, int h);
-static void EncodeMonoRect16(CARD8 *buf, int w, int h);
-static void EncodeMonoRect32(CARD8 *buf, int w, int h);
+static void EncodeMonoRect8(uint8_t *buf, int w, int h);
+static void EncodeMonoRect16(uint8_t *buf, int w, int h);
+static void EncodeMonoRect32(uint8_t *buf, int w, int h);
 
 static void FilterGradient24(rfbClientPtr cl, char *buf, rfbPixelFormat *fmt, int w, int h);
-static void FilterGradient16(rfbClientPtr cl, CARD16 *buf, rfbPixelFormat *fmt, int w, int h);
-static void FilterGradient32(rfbClientPtr cl, CARD32 *buf, rfbPixelFormat *fmt, int w, int h);
+static void FilterGradient16(rfbClientPtr cl, uint16_t *buf, rfbPixelFormat *fmt, int w, int h);
+static void FilterGradient32(rfbClientPtr cl, uint32_t *buf, rfbPixelFormat *fmt, int w, int h);
 
 static int DetectSmoothImage(rfbClientPtr cl, rfbPixelFormat *fmt, int w, int h);
 static unsigned long DetectSmoothImage24(rfbClientPtr cl, rfbPixelFormat *fmt, int w, int h);
@@ -173,10 +173,10 @@ static unsigned long DetectSmoothImage32(rfbClientPtr cl, rfbPixelFormat *fmt, i
 
 static Bool SendJpegRect(rfbClientPtr cl, int x, int y, int w, int h,
                          int quality);
-static void PrepareRowForJpeg(rfbClientPtr cl, CARD8 *dst, int x, int y, int count);
-static void PrepareRowForJpeg24(rfbClientPtr cl, CARD8 *dst, int x, int y, int count);
-static void PrepareRowForJpeg16(rfbClientPtr cl, CARD8 *dst, int x, int y, int count);
-static void PrepareRowForJpeg32(rfbClientPtr cl, CARD8 *dst, int x, int y, int count);
+static void PrepareRowForJpeg(rfbClientPtr cl, uint8_t *dst, int x, int y, int count);
+static void PrepareRowForJpeg24(rfbClientPtr cl, uint8_t *dst, int x, int y, int count);
+static void PrepareRowForJpeg16(rfbClientPtr cl, uint8_t *dst, int x, int y, int count);
+static void PrepareRowForJpeg32(rfbClientPtr cl, uint8_t *dst, int x, int y, int count);
 
 static void JpegInitDestination(j_compress_ptr cinfo);
 static boolean JpegEmptyOutputBuffer(j_compress_ptr cinfo);
@@ -220,7 +220,7 @@ rfbSendRectEncodingTight(cl, x, y, w, h)
     int x, y, w, h;
 {
     int nMaxRows;
-    CARD32 colorValue;
+    uint32_t colorValue;
     int dx, dy, dw, dh;
     int x_best, y_best, w_best, h_best;
     char *fbptr;
@@ -356,7 +356,7 @@ static void
 FindBestSolidArea(cl, x, y, w, h, colorValue, w_ptr, h_ptr)
     rfbClientPtr cl;
     int x, y, w, h;
-    CARD32 colorValue;
+    uint32_t colorValue;
     int *w_ptr, *h_ptr;
 {
     int dx, dy, dw, dh;
@@ -398,7 +398,7 @@ static void
 ExtendSolidArea(cl, x, y, w, h, colorValue, x_ptr, y_ptr, w_ptr, h_ptr)
     rfbClientPtr cl;
     int x, y, w, h;
-    CARD32 colorValue;
+    uint32_t colorValue;
     int *x_ptr, *y_ptr, *w_ptr, *h_ptr;
 {
     int cx, cy;
@@ -432,7 +432,7 @@ ExtendSolidArea(cl, x, y, w, h, colorValue, x_ptr, y_ptr, w_ptr, h_ptr)
     *w_ptr += cx - (*x_ptr + *w_ptr);
 }
 
-static Bool CheckSolidTile(rfbClientPtr cl, int x, int y, int w, int h, CARD32* colorPtr, Bool needSameColor)
+static Bool CheckSolidTile(rfbClientPtr cl, int x, int y, int w, int h, uint32_t* colorPtr, Bool needSameColor)
 {
     switch(cl->screen->rfbServerFormat.bitsPerPixel) {
     case 32:
@@ -447,17 +447,17 @@ static Bool CheckSolidTile(rfbClientPtr cl, int x, int y, int w, int h, CARD32*
 #define DEFINE_CHECK_SOLID_FUNCTION(bpp)                                      \
                                                                               \
 static Bool                                                                   \
-CheckSolidTile##bpp(rfbClientPtr cl, int x, int y, int w, int h, CARD32* colorPtr, Bool needSameColor) \
+CheckSolidTile##bpp(rfbClientPtr cl, int x, int y, int w, int h, uint32_t* colorPtr, Bool needSameColor) \
 {                                                                             \
-    CARD##bpp *fbptr;                                                         \
-    CARD##bpp colorValue;                                                     \
+    uint##bpp##_t *fbptr;                                                         \
+    uint##bpp##_t colorValue;                                                     \
     int dx, dy;                                                               \
                                                                               \
-    fbptr = (CARD##bpp *)                                                     \
+    fbptr = (uint##bpp##_t *)                                                     \
         &cl->screen->frameBuffer[y * cl->screen->paddedWidthInBytes + x * (bpp/8)]; \
                                                                               \
     colorValue = *fbptr;                                                      \
-    if (needSameColor && (CARD32)colorValue != *colorPtr)                     \
+    if (needSameColor && (uint32_t)colorValue != *colorPtr)                     \
         return FALSE;                                                         \
                                                                               \
     for (dy = 0; dy < h; dy++) {                                              \
@@ -465,10 +465,10 @@ CheckSolidTile##bpp(rfbClientPtr cl, int x, int y, int w, int h, CARD32* colorPt
             if (colorValue != fbptr[dx])                                      \
                 return FALSE;                                                 \
         }                                                                     \
-        fbptr = (CARD##bpp *)((CARD8 *)fbptr + cl->screen->paddedWidthInBytes); \
+        fbptr = (uint##bpp##_t *)((uint8_t *)fbptr + cl->screen->paddedWidthInBytes); \
     }                                                                         \
                                                                               \
-    *colorPtr = (CARD32)colorValue;                                           \
+    *colorPtr = (uint32_t)colorValue;                                           \
     return TRUE;                                                              \
 }
 
@@ -691,10 +691,10 @@ SendMonoRect(cl, w, h)
     switch (cl->format.bitsPerPixel) {
 
     case 32:
-        EncodeMonoRect32((CARD8 *)tightBeforeBuf, w, h);
+        EncodeMonoRect32((uint8_t *)tightBeforeBuf, w, h);
 
-        ((CARD32 *)tightAfterBuf)[0] = monoBackground;
-        ((CARD32 *)tightAfterBuf)[1] = monoForeground;
+        ((uint32_t *)tightAfterBuf)[0] = monoBackground;
+        ((uint32_t *)tightAfterBuf)[1] = monoForeground;
         if (usePixelFormat24) {
             Pack24(cl, tightAfterBuf, &cl->format, 2);
             paletteLen = 6;
@@ -707,10 +707,10 @@ SendMonoRect(cl, w, h)
         break;
 
     case 16:
-        EncodeMonoRect16((CARD8 *)tightBeforeBuf, w, h);
+        EncodeMonoRect16((uint8_t *)tightBeforeBuf, w, h);
 
-        ((CARD16 *)tightAfterBuf)[0] = (CARD16)monoBackground;
-        ((CARD16 *)tightAfterBuf)[1] = (CARD16)monoForeground;
+        ((uint16_t *)tightAfterBuf)[0] = (uint16_t)monoBackground;
+        ((uint16_t *)tightAfterBuf)[1] = (uint16_t)monoForeground;
 
         memcpy(&cl->updateBuf[cl->ublen], tightAfterBuf, 4);
         cl->ublen += 4;
@@ -718,7 +718,7 @@ SendMonoRect(cl, w, h)
         break;
 
     default:
-        EncodeMonoRect8((CARD8 *)tightBeforeBuf, w, h);
+        EncodeMonoRect8((uint8_t *)tightBeforeBuf, w, h);
 
         cl->updateBuf[cl->ublen++] = (char)monoBackground;
         cl->updateBuf[cl->ublen++] = (char)monoForeground;
@@ -754,10 +754,10 @@ SendIndexedRect(cl, w, h)
     switch (cl->format.bitsPerPixel) {
 
     case 32:
-        EncodeIndexedRect32((CARD8 *)tightBeforeBuf, w * h);
+        EncodeIndexedRect32((uint8_t *)tightBeforeBuf, w * h);
 
         for (i = 0; i < paletteNumColors; i++) {
-            ((CARD32 *)tightAfterBuf)[i] =
+            ((uint32_t *)tightAfterBuf)[i] =
                 palette.entry[i].listNode->rgb;
         }
         if (usePixelFormat24) {
@@ -772,11 +772,11 @@ SendIndexedRect(cl, w, h)
         break;
 
     case 16:
-        EncodeIndexedRect16((CARD8 *)tightBeforeBuf, w * h);
+        EncodeIndexedRect16((uint8_t *)tightBeforeBuf, w * h);
 
         for (i = 0; i < paletteNumColors; i++) {
-            ((CARD16 *)tightAfterBuf)[i] =
-                (CARD16)palette.entry[i].listNode->rgb;
+            ((uint16_t *)tightAfterBuf)[i] =
+                (uint16_t)palette.entry[i].listNode->rgb;
         }
 
         memcpy(&cl->updateBuf[cl->ublen], tightAfterBuf, paletteNumColors * 2);
@@ -847,10 +847,10 @@ SendGradientRect(cl, w, h)
         FilterGradient24(cl, tightBeforeBuf, &cl->format, w, h);
         len = 3;
     } else if (cl->format.bitsPerPixel == 32) {
-        FilterGradient32(cl, (CARD32 *)tightBeforeBuf, &cl->format, w, h);
+        FilterGradient32(cl, (uint32_t *)tightBeforeBuf, &cl->format, w, h);
         len = 4;
     } else {
-        FilterGradient16(cl, (CARD16 *)tightBeforeBuf, &cl->format, w, h);
+        FilterGradient16(cl, (uint16_t *)tightBeforeBuf, &cl->format, w, h);
         len = 2;
     }
 
@@ -958,8 +958,8 @@ static void
 FillPalette8(count)
     int count;
 {
-    CARD8 *data = (CARD8 *)tightBeforeBuf;
-    CARD8 c0, c1;
+    uint8_t *data = (uint8_t *)tightBeforeBuf;
+    uint8_t c0, c1;
     int i, n0, n1;
 
     paletteNumColors = 0;
@@ -987,11 +987,11 @@ FillPalette8(count)
     }
     if (i == count) {
         if (n0 > n1) {
-            monoBackground = (CARD32)c0;
-            monoForeground = (CARD32)c1;
+            monoBackground = (uint32_t)c0;
+            monoForeground = (uint32_t)c1;
         } else {
-            monoBackground = (CARD32)c1;
-            monoForeground = (CARD32)c0;
+            monoBackground = (uint32_t)c1;
+            monoForeground = (uint32_t)c0;
         }
         paletteNumColors = 2;   /* Two colors */
     }
@@ -1003,8 +1003,8 @@ static void                                                             \
 FillPalette##bpp(count)                                                 \
     int count;                                                          \
 {                                                                       \
-    CARD##bpp *data = (CARD##bpp *)tightBeforeBuf;                      \
-    CARD##bpp c0, c1, ci;                                               \
+    uint##bpp##_t *data = (uint##bpp##_t *)tightBeforeBuf;                      \
+    uint##bpp##_t c0, c1, ci;                                               \
     int i, n0, n1, ni;                                                  \
                                                                         \
     c0 = data[0];                                                       \
@@ -1033,32 +1033,32 @@ FillPalette##bpp(count)                                                 \
     }                                                                   \
     if (i >= count) {                                                   \
         if (n0 > n1) {                                                  \
-            monoBackground = (CARD32)c0;                                \
-            monoForeground = (CARD32)c1;                                \
+            monoBackground = (uint32_t)c0;                                \
+            monoForeground = (uint32_t)c1;                                \
         } else {                                                        \
-            monoBackground = (CARD32)c1;                                \
-            monoForeground = (CARD32)c0;                                \
+            monoBackground = (uint32_t)c1;                                \
+            monoForeground = (uint32_t)c0;                                \
         }                                                               \
         paletteNumColors = 2;   /* Two colors */                        \
         return;                                                         \
     }                                                                   \
                                                                         \
     PaletteReset();                                                     \
-    PaletteInsert (c0, (CARD32)n0, bpp);                                \
-    PaletteInsert (c1, (CARD32)n1, bpp);                                \
+    PaletteInsert (c0, (uint32_t)n0, bpp);                                \
+    PaletteInsert (c1, (uint32_t)n1, bpp);                                \
                                                                         \
     ni = 1;                                                             \
     for (i++; i < count; i++) {                                         \
         if (data[i] == ci) {                                            \
             ni++;                                                       \
         } else {                                                        \
-            if (!PaletteInsert (ci, (CARD32)ni, bpp))                   \
+            if (!PaletteInsert (ci, (uint32_t)ni, bpp))                   \
                 return;                                                 \
             ci = data[i];                                               \
             ni = 1;                                                     \
         }                                                               \
     }                                                                   \
-    PaletteInsert (ci, (CARD32)ni, bpp);                                \
+    PaletteInsert (ci, (uint32_t)ni, bpp);                                \
 }
 
 DEFINE_FILL_PALETTE_FUNCTION(16)
@@ -1081,7 +1081,7 @@ PaletteReset(void)
 
 static int
 PaletteInsert(rgb, numPixels, bpp)
-    CARD32 rgb;
+    uint32_t rgb;
     int numPixels;
     int bpp;
 {
@@ -1158,11 +1158,11 @@ static void Pack24(cl, buf, fmt, count)
     rfbPixelFormat *fmt;
     int count;
 {
-    CARD32 *buf32;
-    CARD32 pix;
+    uint32_t *buf32;
+    uint32_t pix;
     int r_shift, g_shift, b_shift;
 
-    buf32 = (CARD32 *)buf;
+    buf32 = (uint32_t *)buf;
 
     if (!cl->screen->rfbServerFormat.bigEndian == !fmt->bigEndian) {
         r_shift = fmt->redShift;
@@ -1191,15 +1191,15 @@ static void Pack24(cl, buf, fmt, count)
                                                                         \
 static void                                                             \
 EncodeIndexedRect##bpp(buf, count)                                      \
-    CARD8 *buf;                                                         \
+    uint8_t *buf;                                                         \
     int count;                                                          \
 {                                                                       \
     COLOR_LIST *pnode;                                                  \
-    CARD##bpp *src;                                                     \
-    CARD##bpp rgb;                                                      \
+    uint##bpp##_t *src;                                                     \
+    uint##bpp##_t rgb;                                                      \
     int rep = 0;                                                        \
                                                                         \
-    src = (CARD##bpp *) buf;                                            \
+    src = (uint##bpp##_t *) buf;                                            \
                                                                         \
     while (count--) {                                                   \
         rgb = *src++;                                                   \
@@ -1208,10 +1208,10 @@ EncodeIndexedRect##bpp(buf, count)                                      \
         }                                                               \
         pnode = palette.hash[HASH_FUNC##bpp(rgb)];                      \
         while (pnode != NULL) {                                         \
-            if ((CARD##bpp)pnode->rgb == rgb) {                         \
-                *buf++ = (CARD8)pnode->idx;                             \
+            if ((uint##bpp##_t)pnode->rgb == rgb) {                         \
+                *buf++ = (uint8_t)pnode->idx;                             \
                 while (rep) {                                           \
-                    *buf++ = (CARD8)pnode->idx;                         \
+                    *buf++ = (uint8_t)pnode->idx;                         \
                     rep--;                                              \
                 }                                                       \
                 break;                                                  \
@@ -1228,17 +1228,17 @@ DEFINE_IDX_ENCODE_FUNCTION(32)
                                                                         \
 static void                                                             \
 EncodeMonoRect##bpp(buf, w, h)                                          \
-    CARD8 *buf;                                                         \
+    uint8_t *buf;                                                         \
     int w, h;                                                           \
 {                                                                       \
-    CARD##bpp *ptr;                                                     \
-    CARD##bpp bg;                                                       \
+    uint##bpp##_t *ptr;                                                     \
+    uint##bpp##_t bg;                                                       \
     unsigned int value, mask;                                           \
     int aligned_width;                                                  \
     int x, y, bg_bits;                                                  \
                                                                         \
-    ptr = (CARD##bpp *) buf;                                            \
-    bg = (CARD##bpp) monoBackground;                                    \
+    ptr = (uint##bpp##_t *) buf;                                            \
+    bg = (uint##bpp##_t) monoBackground;                                    \
     aligned_width = w - w % 8;                                          \
                                                                         \
     for (y = 0; y < h; y++) {                                           \
@@ -1259,7 +1259,7 @@ EncodeMonoRect##bpp(buf, w, h)                                          \
                     value |= mask;                                      \
                 }                                                       \
             }                                                           \
-            *buf++ = (CARD8)value;                                      \
+            *buf++ = (uint8_t)value;                                      \
         }                                                               \
                                                                         \
         mask = 0x80;                                                    \
@@ -1273,7 +1273,7 @@ EncodeMonoRect##bpp(buf, w, h)                                          \
             }                                                           \
             mask >>= 1;                                                 \
         }                                                               \
-        *buf++ = (CARD8)value;                                          \
+        *buf++ = (uint8_t)value;                                          \
     }                                                                   \
 }
 
@@ -1295,15 +1295,15 @@ FilterGradient24(cl, buf, fmt, w, h)
     rfbPixelFormat *fmt;
     int w, h;
 {
-    CARD32 *buf32;
-    CARD32 pix32;
+    uint32_t *buf32;
+    uint32_t pix32;
     int *prevRowPtr;
     int shiftBits[3];
     int pixHere[3], pixUpper[3], pixLeft[3], pixUpperLeft[3];
     int prediction;
     int x, y, c;
 
-    buf32 = (CARD32 *)buf;
+    buf32 = (uint32_t *)buf;
     memset (prevRowBuf, 0, w * 3 * sizeof(int));
 
     if (!cl->screen->rfbServerFormat.bigEndian == !fmt->bigEndian) {
@@ -1353,11 +1353,11 @@ FilterGradient24(cl, buf, fmt, w, h)
 static void                                                              \
 FilterGradient##bpp(cl, buf, fmt, w, h)                                      \
     rfbClientPtr cl; \
-    CARD##bpp *buf;                                                      \
+    uint##bpp##_t *buf;                                                      \
     rfbPixelFormat *fmt;                                                 \
     int w, h;                                                            \
 {                                                                        \
-    CARD##bpp pix, diff;                                                 \
+    uint##bpp##_t pix, diff;                                                 \
     Bool endianMismatch;                                                 \
     int *prevRowPtr;                                                     \
     int maxColor[3], shiftBits[3];                                       \
@@ -1539,7 +1539,7 @@ DetectSmoothImage##bpp (cl, fmt, w, h)
     int w, h;                                                                \
 {                                                                            \
     Bool endianMismatch;                                                     \
-    CARD##bpp pix;                                                           \
+    uint##bpp##_t pix;                                                           \
     int maxColor[3], shiftBits[3];                                           \
     int x, y, d, dx, c;                                                      \
     int diffStat[256];                                                       \
@@ -1561,7 +1561,7 @@ DetectSmoothImage##bpp (cl, fmt, w, h)
     y = 0, x = 0;                                                            \
     while (y < h && x < w) {                                                 \
         for (d = 0; d < h - y && d < w - x - DETECT_SUBROW_WIDTH; d++) {     \
-            pix = ((CARD##bpp *)tightBeforeBuf)[(y+d)*w+x+d];                \
+            pix = ((uint##bpp##_t *)tightBeforeBuf)[(y+d)*w+x+d];                \
             if (endianMismatch) {                                            \
                 pix = Swap##bpp(pix);                                        \
             }                                                                \
@@ -1569,7 +1569,7 @@ DetectSmoothImage##bpp (cl, fmt, w, h)
                 left[c] = (int)(pix >> shiftBits[c] & maxColor[c]);          \
             }                                                                \
             for (dx = 1; dx <= DETECT_SUBROW_WIDTH; dx++) {                  \
-                pix = ((CARD##bpp *)tightBeforeBuf)[(y+d)*w+x+d+dx];         \
+                pix = ((uint##bpp##_t *)tightBeforeBuf)[(y+d)*w+x+d+dx];         \
                 if (endianMismatch) {                                        \
                     pix = Swap##bpp(pix);                                    \
                 }                                                            \
@@ -1631,14 +1631,14 @@ SendJpegRect(cl, x, y, w, h, quality)
 {
     struct jpeg_compress_struct cinfo;
     struct jpeg_error_mgr jerr;
-    CARD8 *srcBuf;
+    uint8_t *srcBuf;
     JSAMPROW rowPointer[1];
     int dy;
 
     if (cl->screen->rfbServerFormat.bitsPerPixel == 8)
         return SendFullColorRect(cl, w, h);
 
-    srcBuf = (CARD8 *)malloc(w * 3);
+    srcBuf = (uint8_t *)malloc(w * 3);
     if (srcBuf == NULL) {
         return SendFullColorRect(cl, w, h);
     }
@@ -1689,7 +1689,7 @@ SendJpegRect(cl, x, y, w, h, quality)
 static void
 PrepareRowForJpeg(cl, dst, x, y, count)
     rfbClientPtr cl;
-    CARD8 *dst;
+    uint8_t *dst;
     int x, y, count;
 {
     if (cl->screen->rfbServerFormat.bitsPerPixel == 32) {
@@ -1709,20 +1709,20 @@ PrepareRowForJpeg(cl, dst, x, y, count)
 static void
 PrepareRowForJpeg24(cl, dst, x, y, count)
     rfbClientPtr cl;
-    CARD8 *dst;
+    uint8_t *dst;
     int x, y, count;
 {
-    CARD32 *fbptr;
-    CARD32 pix;
+    uint32_t *fbptr;
+    uint32_t pix;
 
-    fbptr = (CARD32 *)
+    fbptr = (uint32_t *)
         &cl->screen->frameBuffer[y * cl->screen->paddedWidthInBytes + x * 4];
 
     while (count--) {
         pix = *fbptr++;
-        *dst++ = (CARD8)(pix >> cl->screen->rfbServerFormat.redShift);
-        *dst++ = (CARD8)(pix >> cl->screen->rfbServerFormat.greenShift);
-        *dst++ = (CARD8)(pix >> cl->screen->rfbServerFormat.blueShift);
+        *dst++ = (uint8_t)(pix >> cl->screen->rfbServerFormat.redShift);
+        *dst++ = (uint8_t)(pix >> cl->screen->rfbServerFormat.greenShift);
+        *dst++ = (uint8_t)(pix >> cl->screen->rfbServerFormat.blueShift);
     }
 }
 
@@ -1731,14 +1731,14 @@ PrepareRowForJpeg24(cl, dst, x, y, count)
 static void                                                                 \
 PrepareRowForJpeg##bpp(cl, dst, x, y, count)                                    \
     rfbClientPtr cl; \
-    CARD8 *dst;                                                             \
+    uint8_t *dst;                                                             \
     int x, y, count;                                                        \
 {                                                                           \
-    CARD##bpp *fbptr;                                                       \
-    CARD##bpp pix;                                                          \
+    uint##bpp##_t *fbptr;                                                       \
+    uint##bpp##_t pix;                                                          \
     int inRed, inGreen, inBlue;                                             \
                                                                             \
-    fbptr = (CARD##bpp *)                                                   \
+    fbptr = (uint##bpp##_t *)                                                   \
         &cl->screen->frameBuffer[y * cl->screen->paddedWidthInBytes +             \
                              x * (bpp / 8)];                                \
                                                                             \
@@ -1752,11 +1752,11 @@ PrepareRowForJpeg##bpp(cl, dst, x, y, count)
         inBlue  = (int)                                                     \
             (pix >> cl->screen->rfbServerFormat.blueShift  & cl->screen->rfbServerFormat.blueMax);  \
                                                                             \
-	*dst++ = (CARD8)((inRed   * 255 + cl->screen->rfbServerFormat.redMax / 2) /     \
+	*dst++ = (uint8_t)((inRed   * 255 + cl->screen->rfbServerFormat.redMax / 2) /     \
                          cl->screen->rfbServerFormat.redMax);                           \
-	*dst++ = (CARD8)((inGreen * 255 + cl->screen->rfbServerFormat.greenMax / 2) /   \
+	*dst++ = (uint8_t)((inGreen * 255 + cl->screen->rfbServerFormat.greenMax / 2) /   \
                          cl->screen->rfbServerFormat.greenMax);                         \
-	*dst++ = (CARD8)((inBlue  * 255 + cl->screen->rfbServerFormat.blueMax / 2) /    \
+	*dst++ = (uint8_t)((inBlue  * 255 + cl->screen->rfbServerFormat.blueMax / 2) /    \
                          cl->screen->rfbServerFormat.blueMax);                          \
     }                                                                       \
 }
diff --git a/translate.c b/translate.c
index d6021d5..54a8e1b 100644
--- a/translate.c
+++ b/translate.c
@@ -23,8 +23,6 @@
  *  USA.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include "rfb.h"
 #include "sraRegion.h"
 
@@ -60,6 +58,8 @@ static const rfbPixelFormat BGR233Format = {
 
 #define CONCAT2(a,b) a##b
 #define CONCAT2E(a,b) CONCAT2(a,b)
+#define CONCAT3(a,b,c) a##b##c
+#define CONCAT3E(a,b,c) CONCAT3(a,b,c)
 #define CONCAT4(a,b,c,d) a##b##c##d
 #define CONCAT4E(a,b,c,d) CONCAT4(a,b,c,d)
 
@@ -364,7 +364,7 @@ rfbSetClientColourMapBGR233(cl)
 {
     char buf[sz_rfbSetColourMapEntriesMsg + 256 * 3 * 2];
     rfbSetColourMapEntriesMsg *scme = (rfbSetColourMapEntriesMsg *)buf;
-    CARD16 *rgb = (CARD16 *)(&buf[sz_rfbSetColourMapEntriesMsg]);
+    uint16_t *rgb = (uint16_t *)(&buf[sz_rfbSetColourMapEntriesMsg]);
     int i, len;
     int r, g, b;
 
diff --git a/vncauth.c b/vncauth.c
index fb2c98e..9caf834 100644
--- a/vncauth.c
+++ b/vncauth.c
@@ -21,21 +21,27 @@
  * vncauth.c - Functions for VNC password management and authentication.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include "rfb.h"
+#include "d3des.h"
+
 #include <string.h>
 #include <math.h>
+
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
+#endif
+
 #include <time.h>
+
 #ifdef WIN32
 #define srandom srand
 #define random rand
 #else
 #include <sys/time.h>
 #endif
-#include "rfb.h"
-#include "d3des.h"
 
 
 /*
diff --git a/zlib.c b/zlib.c
index d0aaa8b..e1dab36 100644
--- a/zlib.c
+++ b/zlib.c
@@ -30,7 +30,6 @@
  * or send email to feedback@developvnc.org.
  */
 
-#include <stdio.h>
 #include "rfb.h"
 
 #ifdef HAVE_LIBZ
diff --git a/zrle.cxx b/zrle.cxx
index 7839677..4572e98 100644
--- a/zrle.cxx
+++ b/zrle.cxx
@@ -23,7 +23,6 @@
 // Routines to implement Zlib Run-length Encoding (ZRLE).
 //
 
-#include <stdio.h>
 extern "C" {
 #include "rfb.h"
 }
@@ -158,7 +157,7 @@ Bool rfbSendRectEncodingZRLE(rfbClientPtr cl, int x, int y, int w, int h)
       bytesToCopy = mos.length() - i;
     }
 
-    memcpy(cl->updateBuf+cl->ublen, (CARD8*)mos.data() + i, bytesToCopy);
+    memcpy(cl->updateBuf+cl->ublen, (uint8_t*)mos.data() + i, bytesToCopy);
 
     cl->ublen += bytesToCopy;
     i += bytesToCopy;
-- 
cgit v1.2.3

