12537a
From 0943c006c7d900dfc0281639e992791d6c567438 Mon Sep 17 00:00:00 2001
12537a
From: Pierre Ossman <ossman@cendio.se>
12537a
Date: Mon, 23 Sep 2019 11:00:17 +0200
12537a
Subject: [PATCH] Use size_t for lengths in stream objects
12537a
12537a
Provides safety against them accidentally becoming negative because
12537a
of bugs in the calculations.
12537a
12537a
Also does the same to CharArray and friends as they were strongly
12537a
connection to the stream objects.
12537a
---
12537a
 common/rdr/FdInStream.cxx    | 20 ++++++++++----------
12537a
 common/rdr/FdInStream.h      | 17 +++++++++--------
12537a
 common/rdr/FdOutStream.cxx   | 20 ++++++++++----------
12537a
 common/rdr/FdOutStream.h     | 12 ++++++------
12537a
 common/rdr/FileInStream.cxx  |  8 ++++----
12537a
 common/rdr/FileInStream.h    |  4 ++--
12537a
 common/rdr/HexInStream.cxx   | 20 ++++++++++----------
12537a
 common/rdr/HexInStream.h     | 12 ++++++------
12537a
 common/rdr/HexOutStream.cxx  | 20 ++++++++++----------
12537a
 common/rdr/HexOutStream.h    | 12 ++++++------
12537a
 common/rdr/InStream.h        | 16 ++++++++--------
12537a
 common/rdr/MemInStream.h     |  8 ++++----
12537a
 common/rdr/MemOutStream.h    | 12 ++++++------
12537a
 common/rdr/OutStream.h       | 20 ++++++++++----------
12537a
 common/rdr/RandomStream.cxx  | 14 +++++++-------
12537a
 common/rdr/RandomStream.h    |  6 +++---
12537a
 common/rdr/TLSInStream.cxx   | 10 +++++-----
12537a
 common/rdr/TLSInStream.h     | 10 +++++-----
12537a
 common/rdr/TLSOutStream.cxx  | 10 +++++-----
12537a
 common/rdr/TLSOutStream.h    | 10 +++++-----
12537a
 common/rdr/ZlibInStream.cxx  | 16 ++++++++--------
12537a
 common/rdr/ZlibInStream.h    | 14 +++++++-------
12537a
 common/rdr/ZlibOutStream.cxx | 10 +++++-----
12537a
 common/rdr/ZlibOutStream.h   | 10 +++++-----
12537a
 common/rfb/Configuration.cxx |  6 +++---
12537a
 common/rfb/Configuration.h   | 13 +++++++------
12537a
 common/rfb/Password.cxx      |  6 +++---
12537a
 common/rfb/Password.h        |  6 +++---
12537a
 common/rfb/util.h            |  2 +-
12537a
 tests/perf/encperf.cxx       | 10 +++++-----
12537a
 win/rfb_win32/Registry.cxx   |  6 +++---
12537a
 win/rfb_win32/Registry.h     |  6 +++---
12537a
 32 files changed, 184 insertions(+), 182 deletions(-)
12537a
12537a
diff --git a/common/rdr/FdInStream.cxx b/common/rdr/FdInStream.cxx
12537a
index a8b3085..e99c108 100644
12537a
--- a/common/rdr/FdInStream.cxx
12537a
+++ b/common/rdr/FdInStream.cxx
12537a
@@ -56,7 +56,7 @@ using namespace rdr;
12537a
 enum { DEFAULT_BUF_SIZE = 8192,
12537a
        MIN_BULK_SIZE = 1024 };
12537a
 
12537a
-FdInStream::FdInStream(int fd_, int timeoutms_, int bufSize_,
12537a
+FdInStream::FdInStream(int fd_, int timeoutms_, size_t bufSize_,
12537a
                        bool closeWhenDone_)
12537a
   : fd(fd_), closeWhenDone(closeWhenDone_),
12537a
     timeoutms(timeoutms_), blockCallback(0),
12537a
@@ -67,7 +67,7 @@ FdInStream::FdInStream(int fd_, int timeoutms_, int bufSize_,
12537a
 }
12537a
 
12537a
 FdInStream::FdInStream(int fd_, FdInStreamBlockCallback* blockCallback_,
12537a
-                       int bufSize_)
12537a
+                       size_t bufSize_)
12537a
   : fd(fd_), timeoutms(0), blockCallback(blockCallback_),
12537a
     timing(false), timeWaitedIn100us(5), timedKbits(0),
12537a
     bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
12537a
@@ -92,12 +92,12 @@ void FdInStream::setBlockCallback(FdInStreamBlockCallback* blockCallback_)
12537a
   timeoutms = 0;
12537a
 }
12537a
 
12537a
-int FdInStream::pos()
12537a
+size_t FdInStream::pos()
12537a
 {
12537a
   return offset + ptr - start;
12537a
 }
12537a
 
12537a
-void FdInStream::readBytes(void* data, int length)
12537a
+void FdInStream::readBytes(void* data, size_t length)
12537a
 {
12537a
   if (length < MIN_BULK_SIZE) {
12537a
     InStream::readBytes(data, length);
12537a
@@ -106,7 +106,7 @@ void FdInStream::readBytes(void* data, int length)
12537a
 
12537a
   U8* dataPtr = (U8*)data;
12537a
 
12537a
-  int n = end - ptr;
12537a
+  size_t n = end - ptr;
12537a
   if (n > length) n = length;
12537a
 
12537a
   memcpy(dataPtr, ptr, n);
12537a
@@ -123,7 +123,7 @@ void FdInStream::readBytes(void* data, int length)
12537a
 }
12537a
 
12537a
 
12537a
-int FdInStream::overrun(int itemSize, int nItems, bool wait)
12537a
+size_t FdInStream::overrun(size_t itemSize, size_t nItems, bool wait)
12537a
 {
12537a
   if (itemSize > bufSize)
12537a
     throw Exception("FdInStream overrun: max itemSize exceeded");
12537a
@@ -135,7 +135,7 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
12537a
   end -= ptr - start;
12537a
   ptr = start;
12537a
 
12537a
-  int bytes_to_read;
12537a
+  size_t bytes_to_read;
12537a
   while (end < start + itemSize) {
12537a
     bytes_to_read = start + bufSize - end;
12537a
     if (!timing) {
12537a
@@ -147,12 +147,12 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
12537a
       // bytes is ineffecient.
12537a
       bytes_to_read = vncmin(bytes_to_read, vncmax(itemSize*nItems, 8));
12537a
     }
12537a
-    int n = readWithTimeoutOrCallback((U8*)end, bytes_to_read, wait);
12537a
+    size_t n = readWithTimeoutOrCallback((U8*)end, bytes_to_read, wait);
12537a
     if (n == 0) return 0;
12537a
     end += n;
12537a
   }
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
@@ -171,7 +171,7 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
12537a
 // returning EINTR.
12537a
 //
12537a
 
12537a
-int FdInStream::readWithTimeoutOrCallback(void* buf, int len, bool wait)
12537a
+size_t FdInStream::readWithTimeoutOrCallback(void* buf, size_t len, bool wait)
12537a
 {
12537a
   struct timeval before, after;
12537a
   if (timing)
12537a
diff --git a/common/rdr/FdInStream.h b/common/rdr/FdInStream.h
12537a
index 5d9598c..b93fd84 100644
12537a
--- a/common/rdr/FdInStream.h
12537a
+++ b/common/rdr/FdInStream.h
12537a
@@ -36,16 +36,17 @@ namespace rdr {
12537a
 
12537a
   public:
12537a
 
12537a
-    FdInStream(int fd, int timeoutms=-1, int bufSize=0,
12537a
+    FdInStream(int fd, int timeoutms=-1, size_t bufSize=0,
12537a
                bool closeWhenDone_=false);
12537a
-    FdInStream(int fd, FdInStreamBlockCallback* blockCallback, int bufSize=0);
12537a
+    FdInStream(int fd, FdInStreamBlockCallback* blockCallback,
12537a
+               size_t bufSize=0);
12537a
     virtual ~FdInStream();
12537a
 
12537a
     void setTimeout(int timeoutms);
12537a
     void setBlockCallback(FdInStreamBlockCallback* blockCallback);
12537a
     int getFd() { return fd; }
12537a
-    int pos();
12537a
-    void readBytes(void* data, int length);
12537a
+    size_t pos();
12537a
+    void readBytes(void* data, size_t length);
12537a
 
12537a
     void startTiming();
12537a
     void stopTiming();
12537a
@@ -53,10 +54,10 @@ namespace rdr {
12537a
     unsigned int timeWaited() { return timeWaitedIn100us; }
12537a
 
12537a
   protected:
12537a
-    int overrun(int itemSize, int nItems, bool wait);
12537a
+    size_t overrun(size_t itemSize, size_t nItems, bool wait);
12537a
 
12537a
   private:
12537a
-    int readWithTimeoutOrCallback(void* buf, int len, bool wait=true);
12537a
+    size_t readWithTimeoutOrCallback(void* buf, size_t len, bool wait=true);
12537a
 
12537a
     int fd;
12537a
     bool closeWhenDone;
12537a
@@ -67,8 +68,8 @@ namespace rdr {
12537a
     unsigned int timeWaitedIn100us;
12537a
     unsigned int timedKbits;
12537a
 
12537a
-    int bufSize;
12537a
-    int offset;
12537a
+    size_t bufSize;
12537a
+    size_t offset;
12537a
     U8* start;
12537a
   };
12537a
 
12537a
diff --git a/common/rdr/FdOutStream.cxx b/common/rdr/FdOutStream.cxx
12537a
index 29e864f..0900fd1 100644
12537a
--- a/common/rdr/FdOutStream.cxx
12537a
+++ b/common/rdr/FdOutStream.cxx
12537a
@@ -50,7 +50,7 @@ using namespace rdr;
12537a
 
12537a
 enum { DEFAULT_BUF_SIZE = 16384 };
12537a
 
12537a
-FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, int bufSize_)
12537a
+FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, size_t bufSize_)
12537a
   : fd(fd_), blocking(blocking_), timeoutms(timeoutms_),
12537a
     bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
12537a
 {
12537a
@@ -78,7 +78,7 @@ void FdOutStream::setBlocking(bool blocking_) {
12537a
   blocking = blocking_;
12537a
 }
12537a
 
12537a
-int FdOutStream::length()
12537a
+size_t FdOutStream::length()
12537a
 {
12537a
   return offset + ptr - sentUpTo;
12537a
 }
12537a
@@ -96,9 +96,9 @@ unsigned FdOutStream::getIdleTime()
12537a
 void FdOutStream::flush()
12537a
 {
12537a
   while (sentUpTo < ptr) {
12537a
-    int n = writeWithTimeout((const void*) sentUpTo,
12537a
-                             ptr - sentUpTo,
12537a
-                             blocking? timeoutms : 0);
12537a
+    size_t n = writeWithTimeout((const void*) sentUpTo,
12537a
+                                ptr - sentUpTo,
12537a
+                                blocking? timeoutms : 0);
12537a
 
12537a
     // Timeout?
12537a
     if (n == 0) {
12537a
@@ -119,7 +119,7 @@ void FdOutStream::flush()
12537a
 }
12537a
 
12537a
 
12537a
-int FdOutStream::overrun(int itemSize, int nItems)
12537a
+size_t FdOutStream::overrun(size_t itemSize, size_t nItems)
12537a
 {
12537a
   if (itemSize > bufSize)
12537a
     throw Exception("FdOutStream overrun: max itemSize exceeded");
12537a
@@ -128,10 +128,10 @@ int FdOutStream::overrun(int itemSize, int nItems)
12537a
   flush();
12537a
 
12537a
   // Still not enough space?
12537a
-  if (itemSize > end - ptr) {
12537a
+  if (itemSize > (size_t)(end - ptr)) {
12537a
     // Can we shuffle things around?
12537a
     // (don't do this if it gains us less than 25%)
12537a
-    if ((sentUpTo - start > bufSize / 4) &&
12537a
+    if (((size_t)(sentUpTo - start) > bufSize / 4) &&
12537a
         (itemSize < bufSize - (ptr - sentUpTo))) {
12537a
       memmove(start, sentUpTo, ptr - sentUpTo);
12537a
       ptr = start + (ptr - sentUpTo);
12537a
@@ -149,7 +149,7 @@ int FdOutStream::overrun(int itemSize, int nItems)
12537a
   }
12537a
 
12537a
   // Can we fit all the items asked for?
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
@@ -165,7 +165,7 @@ int FdOutStream::overrun(int itemSize, int nItems)
12537a
 // select() and write() returning EINTR.
12537a
 //
12537a
 
12537a
-int FdOutStream::writeWithTimeout(const void* data, int length, int timeoutms)
12537a
+size_t FdOutStream::writeWithTimeout(const void* data, size_t length, int timeoutms)
12537a
 {
12537a
   int n;
12537a
 
12537a
diff --git a/common/rdr/FdOutStream.h b/common/rdr/FdOutStream.h
12537a
index b7f6cb0..ed84fdb 100644
12537a
--- a/common/rdr/FdOutStream.h
12537a
+++ b/common/rdr/FdOutStream.h
12537a
@@ -34,7 +34,7 @@ namespace rdr {
12537a
 
12537a
   public:
12537a
 
12537a
-    FdOutStream(int fd, bool blocking=true, int timeoutms=-1, int bufSize=0);
12537a
+    FdOutStream(int fd, bool blocking=true, int timeoutms=-1, size_t bufSize=0);
12537a
     virtual ~FdOutStream();
12537a
 
12537a
     void setTimeout(int timeoutms);
12537a
@@ -42,20 +42,20 @@ namespace rdr {
12537a
     int getFd() { return fd; }
12537a
 
12537a
     void flush();
12537a
-    int length();
12537a
+    size_t length();
12537a
 
12537a
     int bufferUsage();
12537a
 
12537a
     unsigned getIdleTime();
12537a
 
12537a
   private:
12537a
-    int overrun(int itemSize, int nItems);
12537a
-    int writeWithTimeout(const void* data, int length, int timeoutms);
12537a
+    size_t overrun(size_t itemSize, size_t nItems);
12537a
+    size_t writeWithTimeout(const void* data, size_t length, int timeoutms);
12537a
     int fd;
12537a
     bool blocking;
12537a
     int timeoutms;
12537a
-    int bufSize;
12537a
-    int offset;
12537a
+    size_t bufSize;
12537a
+    size_t offset;
12537a
     U8* start;
12537a
     U8* sentUpTo;
12537a
     struct timeval lastWrite;
12537a
diff --git a/common/rdr/FileInStream.cxx b/common/rdr/FileInStream.cxx
12537a
index 8d5c22e..56830a0 100644
12537a
--- a/common/rdr/FileInStream.cxx
12537a
+++ b/common/rdr/FileInStream.cxx
12537a
@@ -48,7 +48,7 @@ void FileInStream::reset(void) {
12537a
   ptr = end = b;
12537a
 }
12537a
 
12537a
-int FileInStream::pos()
12537a
+size_t FileInStream::pos()
12537a
 {
12537a
   if (!file)
12537a
     throw Exception("File is not open");
12537a
@@ -56,9 +56,9 @@ int FileInStream::pos()
12537a
   return ftell(file) + ptr - b;
12537a
 }
12537a
 
12537a
-int FileInStream::overrun(int itemSize, int nItems, bool wait)
12537a
+size_t FileInStream::overrun(size_t itemSize, size_t nItems, bool wait)
12537a
 {
12537a
-  if (itemSize > (int)sizeof(b))
12537a
+  if (itemSize > sizeof(b))
12537a
     throw Exception("FileInStream overrun: max itemSize exceeded");
12537a
 
12537a
   if (end - ptr != 0)
12537a
@@ -80,7 +80,7 @@ int FileInStream::overrun(int itemSize, int nItems, bool wait)
12537a
     end += b + sizeof(b) - end;
12537a
   }
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
diff --git a/common/rdr/FileInStream.h b/common/rdr/FileInStream.h
12537a
index ace04f3..a33c765 100644
12537a
--- a/common/rdr/FileInStream.h
12537a
+++ b/common/rdr/FileInStream.h
12537a
@@ -35,10 +35,10 @@ namespace rdr {
12537a
 
12537a
     void reset(void);
12537a
 
12537a
-    int pos();
12537a
+    size_t pos();
12537a
 
12537a
   protected:
12537a
-    int overrun(int itemSize, int nItems, bool wait = true);
12537a
+    size_t overrun(size_t itemSize, size_t nItems, bool wait = true);
12537a
 
12537a
   private:
12537a
     U8 b[131072];
12537a
diff --git a/common/rdr/HexInStream.cxx b/common/rdr/HexInStream.cxx
12537a
index 80f8a79..8f93988 100644
12537a
--- a/common/rdr/HexInStream.cxx
12537a
+++ b/common/rdr/HexInStream.cxx
12537a
@@ -28,7 +28,7 @@ const int DEFAULT_BUF_LEN = 16384;
12537a
 
12537a
 static inline int min(int a, int b) {return a
12537a
 
12537a
-HexInStream::HexInStream(InStream& is, int bufSize_)
12537a
+HexInStream::HexInStream(InStream& is, size_t bufSize_)
12537a
 : bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_LEN), offset(0), in_stream(is)
12537a
 {
12537a
   ptr = end = start = new U8[bufSize];
12537a
@@ -50,8 +50,8 @@ bool HexInStream::readHexAndShift(char c, int* v) {
12537a
   return true;
12537a
 }
12537a
 
12537a
-bool HexInStream::hexStrToBin(const char* s, char** data, int* length) {
12537a
-  int l=strlen(s);
12537a
+bool HexInStream::hexStrToBin(const char* s, char** data, size_t* length) {
12537a
+  size_t l=strlen(s);
12537a
   if ((l % 2) == 0) {
12537a
     delete [] *data;
12537a
     *data = 0; *length = 0;
12537a
@@ -59,7 +59,7 @@ bool HexInStream::hexStrToBin(const char* s, char** data, int* length) {
12537a
       return true;
12537a
     *data = new char[l/2];
12537a
     *length = l/2;
12537a
-    for(int i=0;i
12537a
+    for(size_t i=0;i
12537a
       int byte = 0;
12537a
       if (!readHexAndShift(s[i], &byte) ||
12537a
         !readHexAndShift(s[i+1], &byte))
12537a
@@ -76,11 +76,11 @@ decodeError:
12537a
 }
12537a
 
12537a
 
12537a
-int HexInStream::pos() {
12537a
+size_t HexInStream::pos() {
12537a
   return offset + ptr - start;
12537a
 }
12537a
 
12537a
-int HexInStream::overrun(int itemSize, int nItems, bool wait) {
12537a
+size_t HexInStream::overrun(size_t itemSize, size_t nItems, bool wait) {
12537a
   if (itemSize > bufSize)
12537a
     throw Exception("HexInStream overrun: max itemSize exceeded");
12537a
 
12537a
@@ -92,14 +92,14 @@ int HexInStream::overrun(int itemSize, int nItems, bool wait) {
12537a
   ptr = start;
12537a
 
12537a
   while (end < ptr + itemSize) {
12537a
-    int n = in_stream.check(2, 1, wait);
12537a
+    size_t n = in_stream.check(2, 1, wait);
12537a
     if (n == 0) return 0;
12537a
     const U8* iptr = in_stream.getptr();
12537a
     const U8* eptr = in_stream.getend();
12537a
-    int length = min((eptr - iptr)/2, start + bufSize - end);
12537a
+    size_t length = min((eptr - iptr)/2, start + bufSize - end);
12537a
 
12537a
     U8* optr = (U8*) end;
12537a
-    for (int i=0; i
12537a
+    for (size_t i=0; i
12537a
       int v = 0;
12537a
       readHexAndShift(iptr[i*2], &v);
12537a
       readHexAndShift(iptr[i*2+1], &v);
12537a
@@ -110,7 +110,7 @@ int HexInStream::overrun(int itemSize, int nItems, bool wait) {
12537a
     end += length;
12537a
   }
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
diff --git a/common/rdr/HexInStream.h b/common/rdr/HexInStream.h
12537a
index 6bfb843..8e495fb 100644
12537a
--- a/common/rdr/HexInStream.h
12537a
+++ b/common/rdr/HexInStream.h
12537a
@@ -26,21 +26,21 @@ namespace rdr {
12537a
   class HexInStream : public InStream {
12537a
   public:
12537a
 
12537a
-    HexInStream(InStream& is, int bufSize=0);
12537a
+    HexInStream(InStream& is, size_t bufSize=0);
12537a
     virtual ~HexInStream();
12537a
 
12537a
-    int pos();
12537a
+    size_t pos();
12537a
 
12537a
     static bool readHexAndShift(char c, int* v);
12537a
-    static bool hexStrToBin(const char* s, char** data, int* length);
12537a
+    static bool hexStrToBin(const char* s, char** data, size_t* length);
12537a
 
12537a
   protected:
12537a
-    int overrun(int itemSize, int nItems, bool wait);
12537a
+    size_t overrun(size_t itemSize, size_t nItems, bool wait);
12537a
 
12537a
   private:
12537a
-    int bufSize;
12537a
+    size_t bufSize;
12537a
     U8* start;
12537a
-    int offset;
12537a
+    size_t offset;
12537a
 
12537a
     InStream& in_stream;
12537a
   };
12537a
diff --git a/common/rdr/HexOutStream.cxx b/common/rdr/HexOutStream.cxx
12537a
index 9b0b6c4..7232514 100644
12537a
--- a/common/rdr/HexOutStream.cxx
12537a
+++ b/common/rdr/HexOutStream.cxx
12537a
@@ -23,9 +23,9 @@ using namespace rdr;
12537a
 
12537a
 const int DEFAULT_BUF_LEN = 16384;
12537a
 
12537a
-static inline int min(int a, int b) {return a
12537a
+static inline size_t min(size_t a, size_t b) {return a
12537a
 
12537a
-HexOutStream::HexOutStream(OutStream& os, int buflen)
12537a
+HexOutStream::HexOutStream(OutStream& os, size_t buflen)
12537a
 : out_stream(os), offset(0), bufSize(buflen ? buflen : DEFAULT_BUF_LEN)
12537a
 {
12537a
   if (bufSize % 2)
12537a
@@ -48,9 +48,9 @@ char HexOutStream::intToHex(int i) {
12537a
     throw rdr::Exception("intToHex failed");
12537a
 }
12537a
 
12537a
-char* HexOutStream::binToHexStr(const char* data, int length) {
12537a
+char* HexOutStream::binToHexStr(const char* data, size_t length) {
12537a
   char* buffer = new char[length*2+1];
12537a
-  for (int i=0; i
12537a
+  for (size_t i=0; i
12537a
     buffer[i*2] = intToHex((data[i] >> 4) & 15);
12537a
     buffer[i*2+1] = intToHex((data[i] & 15));
12537a
     if (!buffer[i*2] || !buffer[i*2+1]) {
12537a
@@ -70,9 +70,9 @@ HexOutStream::writeBuffer() {
12537a
     out_stream.check(2);
12537a
     U8* optr = out_stream.getptr();
12537a
     U8* oend = out_stream.getend();
12537a
-    int length = min(ptr-pos, (oend-optr)/2);
12537a
+    size_t length = min(ptr-pos, (oend-optr)/2);
12537a
 
12537a
-    for (int i=0; i
12537a
+    for (size_t i=0; i
12537a
       optr[i*2] = intToHex((pos[i] >> 4) & 0xf);
12537a
       optr[i*2+1] = intToHex(pos[i] & 0xf);
12537a
     }
12537a
@@ -84,7 +84,7 @@ HexOutStream::writeBuffer() {
12537a
   ptr = start;
12537a
 }
12537a
 
12537a
-int HexOutStream::length()
12537a
+size_t HexOutStream::length()
12537a
 {
12537a
   return offset + ptr - start;
12537a
 }
12537a
@@ -95,14 +95,14 @@ HexOutStream::flush() {
12537a
   out_stream.flush();
12537a
 }
12537a
 
12537a
-int
12537a
-HexOutStream::overrun(int itemSize, int nItems) {
12537a
+size_t
12537a
+HexOutStream::overrun(size_t itemSize, size_t nItems) {
12537a
   if (itemSize > bufSize)
12537a
     throw Exception("HexOutStream overrun: max itemSize exceeded");
12537a
 
12537a
   writeBuffer();
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
diff --git a/common/rdr/HexOutStream.h b/common/rdr/HexOutStream.h
12537a
index 10247e6..92442a7 100644
12537a
--- a/common/rdr/HexOutStream.h
12537a
+++ b/common/rdr/HexOutStream.h
12537a
@@ -26,24 +26,24 @@ namespace rdr {
12537a
   class HexOutStream : public OutStream {
12537a
   public:
12537a
 
12537a
-    HexOutStream(OutStream& os, int buflen=0);
12537a
+    HexOutStream(OutStream& os, size_t buflen=0);
12537a
     virtual ~HexOutStream();
12537a
 
12537a
     void flush();
12537a
-    int length();
12537a
+    size_t length();
12537a
 
12537a
     static char intToHex(int i);
12537a
-    static char* binToHexStr(const char* data, int length);
12537a
+    static char* binToHexStr(const char* data, size_t length);
12537a
 
12537a
   private:
12537a
     void writeBuffer();
12537a
-    int overrun(int itemSize, int nItems);
12537a
+    size_t overrun(size_t itemSize, size_t nItems);
12537a
 
12537a
     OutStream& out_stream;
12537a
 
12537a
     U8* start;
12537a
-    int offset;
12537a
-    int bufSize;
12537a
+    size_t offset;
12537a
+    size_t bufSize;
12537a
   };
12537a
 
12537a
 }
12537a
diff --git a/common/rdr/InStream.h b/common/rdr/InStream.h
12537a
index 212a2ec..14ecf09 100644
12537a
--- a/common/rdr/InStream.h
12537a
+++ b/common/rdr/InStream.h
12537a
@@ -41,7 +41,7 @@ namespace rdr {
12537a
     // for the bytes, zero is returned if the bytes are not immediately
12537a
     // available.
12537a
 
12537a
-    inline int check(int itemSize, int nItems=1, bool wait=true)
12537a
+    inline size_t check(size_t itemSize, size_t nItems=1, bool wait=true)
12537a
     {
12537a
       if (ptr + itemSize * nItems > end) {
12537a
         if (ptr + itemSize > end)
12537a
@@ -56,7 +56,7 @@ namespace rdr {
12537a
     // be read without blocking.  It returns true if this is the case, false
12537a
     // otherwise.  The length must be "small" (less than the buffer size).
12537a
 
12537a
-    inline bool checkNoWait(int length) { return check(length, 1, false)!=0; }
12537a
+    inline bool checkNoWait(size_t length) { return check(length, 1, false)!=0; }
12537a
 
12537a
     // readU/SN() methods read unsigned and signed N-bit integers.
12537a
 
12537a
@@ -82,9 +82,9 @@ namespace rdr {
12537a
 
12537a
     static U32 maxStringLength;
12537a
 
12537a
-    inline void skip(int bytes) {
12537a
+    inline void skip(size_t bytes) {
12537a
       while (bytes > 0) {
12537a
-        int n = check(1, bytes);
12537a
+        size_t n = check(1, bytes);
12537a
         ptr += n;
12537a
         bytes -= n;
12537a
       }
12537a
@@ -92,11 +92,11 @@ namespace rdr {
12537a
 
12537a
     // readBytes() reads an exact number of bytes.
12537a
 
12537a
-    void readBytes(void* data, int length) {
12537a
+    void readBytes(void* data, size_t length) {
12537a
       U8* dataPtr = (U8*)data;
12537a
       U8* dataEnd = dataPtr + length;
12537a
       while (dataPtr < dataEnd) {
12537a
-        int n = check(1, dataEnd - dataPtr);
12537a
+        size_t n = check(1, dataEnd - dataPtr);
12537a
         memcpy(dataPtr, ptr, n);
12537a
         ptr += n;
12537a
         dataPtr += n;
12537a
@@ -114,7 +114,7 @@ namespace rdr {
12537a
 
12537a
     // pos() returns the position in the stream.
12537a
 
12537a
-    virtual int pos() = 0;
12537a
+    virtual size_t pos() = 0;
12537a
 
12537a
     // getptr(), getend() and setptr() are "dirty" methods which allow you to
12537a
     // manipulate the buffer directly.  This is useful for a stream which is a
12537a
@@ -133,7 +133,7 @@ namespace rdr {
12537a
     // instead of blocking to wait for the bytes, zero is returned if the bytes
12537a
     // are not immediately available.
12537a
 
12537a
-    virtual int overrun(int itemSize, int nItems, bool wait=true) = 0;
12537a
+    virtual size_t overrun(size_t itemSize, size_t nItems, bool wait=true) = 0;
12537a
 
12537a
   protected:
12537a
 
12537a
diff --git a/common/rdr/MemInStream.h b/common/rdr/MemInStream.h
12537a
index 1a6a798..3e9e77b 100644
12537a
--- a/common/rdr/MemInStream.h
12537a
+++ b/common/rdr/MemInStream.h
12537a
@@ -36,7 +36,7 @@ namespace rdr {
12537a
 
12537a
   public:
12537a
 
12537a
-    MemInStream(const void* data, int len, bool deleteWhenDone_=false)
12537a
+    MemInStream(const void* data, size_t len, bool deleteWhenDone_=false)
12537a
       : start((const U8*)data), deleteWhenDone(deleteWhenDone_)
12537a
     {
12537a
       ptr = start;
12537a
@@ -48,12 +48,12 @@ namespace rdr {
12537a
         delete [] start;
12537a
     }
12537a
 
12537a
-    int pos() { return ptr - start; }
12537a
-    void reposition(int pos) { ptr = start + pos; }
12537a
+    size_t pos() { return ptr - start; }
12537a
+    void reposition(size_t pos) { ptr = start + pos; }
12537a
 
12537a
   private:
12537a
 
12537a
-    int overrun(int itemSize, int nItems, bool wait) { throw EndOfStream(); }
12537a
+    size_t overrun(size_t itemSize, size_t nItems, bool wait) { throw EndOfStream(); }
12537a
     const U8* start;
12537a
     bool deleteWhenDone;
12537a
   };
12537a
diff --git a/common/rdr/MemOutStream.h b/common/rdr/MemOutStream.h
12537a
index 3b17e55..4a815b3 100644
12537a
--- a/common/rdr/MemOutStream.h
12537a
+++ b/common/rdr/MemOutStream.h
12537a
@@ -40,16 +40,16 @@ namespace rdr {
12537a
       delete [] start;
12537a
     }
12537a
 
12537a
-    void writeBytes(const void* data, int length) {
12537a
+    void writeBytes(const void* data, size_t length) {
12537a
       check(length);
12537a
       memcpy(ptr, data, length);
12537a
       ptr += length;
12537a
     }
12537a
 
12537a
-    int length() { return ptr - start; }
12537a
+    size_t length() { return ptr - start; }
12537a
     void clear() { ptr = start; };
12537a
     void clearAndZero() { memset(start, 0, ptr-start); clear(); }
12537a
-    void reposition(int pos) { ptr = start + pos; }
12537a
+    void reposition(size_t pos) { ptr = start + pos; }
12537a
 
12537a
     // data() returns a pointer to the buffer.
12537a
 
12537a
@@ -60,9 +60,9 @@ namespace rdr {
12537a
     // overrun() either doubles the buffer or adds enough space for nItems of
12537a
     // size itemSize bytes.
12537a
 
12537a
-    int overrun(int itemSize, int nItems) {
12537a
-      int len = ptr - start + itemSize * nItems;
12537a
-      if (len < (end - start) * 2)
12537a
+    size_t overrun(size_t itemSize, size_t nItems) {
12537a
+      size_t len = ptr - start + itemSize * nItems;
12537a
+      if (len < (size_t)(end - start) * 2)
12537a
         len = (end - start) * 2;
12537a
 
12537a
       U8* newStart = new U8[len];
12537a
diff --git a/common/rdr/OutStream.h b/common/rdr/OutStream.h
12537a
index a749a20..11aafd2 100644
12537a
--- a/common/rdr/OutStream.h
12537a
+++ b/common/rdr/OutStream.h
12537a
@@ -44,7 +44,7 @@ namespace rdr {
12537a
     // itemSize bytes.  Returns the number of items which fit (up to a maximum
12537a
     // of nItems).
12537a
 
12537a
-    inline int check(int itemSize, int nItems=1)
12537a
+    inline size_t check(size_t itemSize, size_t nItems=1)
12537a
     {
12537a
       if (ptr + itemSize * nItems > end) {
12537a
         if (ptr + itemSize > end)
12537a
@@ -76,13 +76,13 @@ namespace rdr {
12537a
       writeBytes(str, len);
12537a
     }
12537a
 
12537a
-    inline void pad(int bytes) {
12537a
+    inline void pad(size_t bytes) {
12537a
       while (bytes-- > 0) writeU8(0);
12537a
     }
12537a
 
12537a
-    inline void skip(int bytes) {
12537a
+    inline void skip(size_t bytes) {
12537a
       while (bytes > 0) {
12537a
-        int n = check(1, bytes);
12537a
+        size_t n = check(1, bytes);
12537a
         ptr += n;
12537a
         bytes -= n;
12537a
       }
12537a
@@ -90,11 +90,11 @@ namespace rdr {
12537a
 
12537a
     // writeBytes() writes an exact number of bytes.
12537a
 
12537a
-    void writeBytes(const void* data, int length) {
12537a
+    void writeBytes(const void* data, size_t length) {
12537a
       const U8* dataPtr = (const U8*)data;
12537a
       const U8* dataEnd = dataPtr + length;
12537a
       while (dataPtr < dataEnd) {
12537a
-        int n = check(1, dataEnd - dataPtr);
12537a
+        size_t n = check(1, dataEnd - dataPtr);
12537a
         memcpy(ptr, dataPtr, n);
12537a
         ptr += n;
12537a
         dataPtr += n;
12537a
@@ -103,9 +103,9 @@ namespace rdr {
12537a
 
12537a
     // copyBytes() efficiently transfers data between streams
12537a
 
12537a
-    void copyBytes(InStream* is, int length) {
12537a
+    void copyBytes(InStream* is, size_t length) {
12537a
       while (length > 0) {
12537a
-        int n = check(1, length);
12537a
+        size_t n = check(1, length);
12537a
         is->readBytes(ptr, n);
12537a
         ptr += n;
12537a
         length -= n;
12537a
@@ -124,7 +124,7 @@ namespace rdr {
12537a
 
12537a
     // length() returns the length of the stream.
12537a
 
12537a
-    virtual int length() = 0;
12537a
+    virtual size_t length() = 0;
12537a
 
12537a
     // flush() requests that the stream be flushed.
12537a
 
12537a
@@ -145,7 +145,7 @@ namespace rdr {
12537a
     // the number of items which fit (up to a maximum of nItems).  itemSize is
12537a
     // supposed to be "small" (a few bytes).
12537a
 
12537a
-    virtual int overrun(int itemSize, int nItems) = 0;
12537a
+    virtual size_t overrun(size_t itemSize, size_t nItems) = 0;
12537a
 
12537a
   protected:
12537a
 
12537a
diff --git a/common/rdr/RandomStream.cxx b/common/rdr/RandomStream.cxx
12537a
index 3fde18d..7681095 100644
12537a
--- a/common/rdr/RandomStream.cxx
12537a
+++ b/common/rdr/RandomStream.cxx
12537a
@@ -32,7 +32,7 @@
12537a
 
12537a
 using namespace rdr;
12537a
 
12537a
-const int DEFAULT_BUF_LEN = 256;
12537a
+const size_t DEFAULT_BUF_LEN = 256;
12537a
 
12537a
 unsigned int RandomStream::seed;
12537a
 
12537a
@@ -83,11 +83,11 @@ RandomStream::~RandomStream() {
12537a
 #endif
12537a
 }
12537a
 
12537a
-int RandomStream::pos() {
12537a
+size_t RandomStream::pos() {
12537a
   return offset + ptr - start;
12537a
 }
12537a
 
12537a
-int RandomStream::overrun(int itemSize, int nItems, bool wait) {
12537a
+size_t RandomStream::overrun(size_t itemSize, size_t nItems, bool wait) {
12537a
   if (itemSize > DEFAULT_BUF_LEN)
12537a
     throw Exception("RandomStream overrun: max itemSize exceeded");
12537a
 
12537a
@@ -98,7 +98,7 @@ int RandomStream::overrun(int itemSize, int nItems, bool wait) {
12537a
   offset += ptr - start;
12537a
   ptr = start;
12537a
 
12537a
-  int length = start + DEFAULT_BUF_LEN - end;
12537a
+  size_t length = start + DEFAULT_BUF_LEN - end;
12537a
 
12537a
 #ifdef RFB_HAVE_WINCRYPT
12537a
   if (provider) {
12537a
@@ -109,7 +109,7 @@ int RandomStream::overrun(int itemSize, int nItems, bool wait) {
12537a
 #else
12537a
 #ifndef WIN32
12537a
   if (fp) {
12537a
-    int n = fread((U8*)end, length, 1, fp);
12537a
+    size_t n = fread((U8*)end, length, 1, fp);
12537a
     if (n != 1)
12537a
       throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
12537a
                                  errno);
12537a
@@ -119,11 +119,11 @@ int RandomStream::overrun(int itemSize, int nItems, bool wait) {
12537a
   {
12537a
 #endif
12537a
 #endif
12537a
-    for (int i=0; i
12537a
+    for (size_t i=0; i
12537a
       *(U8*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
12537a
   }
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
diff --git a/common/rdr/RandomStream.h b/common/rdr/RandomStream.h
12537a
index c33360d..80b389b 100644
12537a
--- a/common/rdr/RandomStream.h
12537a
+++ b/common/rdr/RandomStream.h
12537a
@@ -39,14 +39,14 @@ namespace rdr {
12537a
     RandomStream();
12537a
     virtual ~RandomStream();
12537a
 
12537a
-    int pos();
12537a
+    size_t pos();
12537a
 
12537a
   protected:
12537a
-    int overrun(int itemSize, int nItems, bool wait);
12537a
+    size_t overrun(size_t itemSize, size_t nItems, bool wait);
12537a
 
12537a
   private:
12537a
     U8* start;
12537a
-    int offset;
12537a
+    size_t offset;
12537a
 
12537a
     static unsigned int seed;
12537a
 #ifdef RFB_HAVE_WINCRYPT
12537a
diff --git a/common/rdr/TLSInStream.cxx b/common/rdr/TLSInStream.cxx
12537a
index 77b1672..d0f9426 100644
12537a
--- a/common/rdr/TLSInStream.cxx
12537a
+++ b/common/rdr/TLSInStream.cxx
12537a
@@ -75,12 +75,12 @@ TLSInStream::~TLSInStream()
12537a
   delete[] start;
12537a
 }
12537a
 
12537a
-int TLSInStream::pos()
12537a
+size_t TLSInStream::pos()
12537a
 {
12537a
   return offset + ptr - start;
12537a
 }
12537a
 
12537a
-int TLSInStream::overrun(int itemSize, int nItems, bool wait)
12537a
+size_t TLSInStream::overrun(size_t itemSize, size_t nItems, bool wait)
12537a
 {
12537a
   if (itemSize > bufSize)
12537a
     throw Exception("TLSInStream overrun: max itemSize exceeded");
12537a
@@ -93,19 +93,19 @@ int TLSInStream::overrun(int itemSize, int nItems, bool wait)
12537a
   ptr = start;
12537a
 
12537a
   while (end < start + itemSize) {
12537a
-    int n = readTLS((U8*) end, start + bufSize - end, wait);
12537a
+    size_t n = readTLS((U8*) end, start + bufSize - end, wait);
12537a
     if (!wait && n == 0)
12537a
       return 0;
12537a
     end += n;
12537a
   }
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
 }
12537a
 
12537a
-int TLSInStream::readTLS(U8* buf, int len, bool wait)
12537a
+size_t TLSInStream::readTLS(U8* buf, size_t len, bool wait)
12537a
 {
12537a
   int n;
12537a
 
12537a
diff --git a/common/rdr/TLSInStream.h b/common/rdr/TLSInStream.h
12537a
index b16d9f5..5f9dee7 100644
12537a
--- a/common/rdr/TLSInStream.h
12537a
+++ b/common/rdr/TLSInStream.h
12537a
@@ -36,17 +36,17 @@ namespace rdr {
12537a
     TLSInStream(InStream* in, gnutls_session_t session);
12537a
     virtual ~TLSInStream();
12537a
 
12537a
-    int pos();
12537a
+    size_t pos();
12537a
 
12537a
   private:
12537a
-    int overrun(int itemSize, int nItems, bool wait);
12537a
-    int readTLS(U8* buf, int len, bool wait);
12537a
+    size_t overrun(size_t itemSize, size_t nItems, bool wait);
12537a
+    size_t readTLS(U8* buf, size_t len, bool wait);
12537a
     static ssize_t pull(gnutls_transport_ptr_t str, void* data, size_t size);
12537a
 
12537a
     gnutls_session_t session;
12537a
     InStream* in;
12537a
-    int bufSize;
12537a
-    int offset;
12537a
+    size_t bufSize;
12537a
+    size_t offset;
12537a
     U8* start;
12537a
   };
12537a
 };
12537a
diff --git a/common/rdr/TLSOutStream.cxx b/common/rdr/TLSOutStream.cxx
12537a
index 44d2d9f..30c456f 100644
12537a
--- a/common/rdr/TLSOutStream.cxx
12537a
+++ b/common/rdr/TLSOutStream.cxx
12537a
@@ -75,7 +75,7 @@ TLSOutStream::~TLSOutStream()
12537a
   delete [] start;
12537a
 }
12537a
 
12537a
-int TLSOutStream::length()
12537a
+size_t TLSOutStream::length()
12537a
 {
12537a
   return offset + ptr - start;
12537a
 }
12537a
@@ -84,7 +84,7 @@ void TLSOutStream::flush()
12537a
 {
12537a
   U8* sentUpTo = start;
12537a
   while (sentUpTo < ptr) {
12537a
-    int n = writeTLS(sentUpTo, ptr - sentUpTo);
12537a
+    size_t n = writeTLS(sentUpTo, ptr - sentUpTo);
12537a
     sentUpTo += n;
12537a
     offset += n;
12537a
   }
12537a
@@ -93,20 +93,20 @@ void TLSOutStream::flush()
12537a
   out->flush();
12537a
 }
12537a
 
12537a
-int TLSOutStream::overrun(int itemSize, int nItems)
12537a
+size_t TLSOutStream::overrun(size_t itemSize, size_t nItems)
12537a
 {
12537a
   if (itemSize > bufSize)
12537a
     throw Exception("TLSOutStream overrun: max itemSize exceeded");
12537a
 
12537a
   flush();
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
 }
12537a
 
12537a
-int TLSOutStream::writeTLS(const U8* data, int length)
12537a
+size_t TLSOutStream::writeTLS(const U8* data, size_t length)
12537a
 {
12537a
   int n;
12537a
 
12537a
diff --git a/common/rdr/TLSOutStream.h b/common/rdr/TLSOutStream.h
12537a
index 81dd237..71a7f3b 100644
12537a
--- a/common/rdr/TLSOutStream.h
12537a
+++ b/common/rdr/TLSOutStream.h
12537a
@@ -36,20 +36,20 @@ namespace rdr {
12537a
     virtual ~TLSOutStream();
12537a
 
12537a
     void flush();
12537a
-    int length();
12537a
+    size_t length();
12537a
 
12537a
   protected:
12537a
-    int overrun(int itemSize, int nItems);
12537a
+    size_t overrun(size_t itemSize, size_t nItems);
12537a
 
12537a
   private:
12537a
-    int writeTLS(const U8* data, int length);
12537a
+    size_t writeTLS(const U8* data, size_t length);
12537a
     static ssize_t push(gnutls_transport_ptr_t str, const void* data, size_t size);
12537a
 
12537a
     gnutls_session_t session;
12537a
     OutStream* out;
12537a
-    int bufSize;
12537a
+    size_t bufSize;
12537a
     U8* start;
12537a
-    int offset;
12537a
+    size_t offset;
12537a
   };
12537a
 };
12537a
 
12537a
diff --git a/common/rdr/ZlibInStream.cxx b/common/rdr/ZlibInStream.cxx
12537a
index a361010..e2f971c 100644
12537a
--- a/common/rdr/ZlibInStream.cxx
12537a
+++ b/common/rdr/ZlibInStream.cxx
12537a
@@ -26,7 +26,7 @@ using namespace rdr;
12537a
 
12537a
 enum { DEFAULT_BUF_SIZE = 16384 };
12537a
 
12537a
-ZlibInStream::ZlibInStream(int bufSize_)
12537a
+ZlibInStream::ZlibInStream(size_t bufSize_)
12537a
   : underlying(0), bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0),
12537a
     zs(NULL), bytesIn(0)
12537a
 {
12537a
@@ -40,14 +40,14 @@ ZlibInStream::~ZlibInStream()
12537a
   delete [] start;
12537a
 }
12537a
 
12537a
-void ZlibInStream::setUnderlying(InStream* is, int bytesIn_)
12537a
+void ZlibInStream::setUnderlying(InStream* is, size_t bytesIn_)
12537a
 {
12537a
   underlying = is;
12537a
   bytesIn = bytesIn_;
12537a
   ptr = end = start;
12537a
 }
12537a
 
12537a
-int ZlibInStream::pos()
12537a
+size_t ZlibInStream::pos()
12537a
 {
12537a
   return offset + ptr - start;
12537a
 }
12537a
@@ -96,7 +96,7 @@ void ZlibInStream::deinit()
12537a
   zs = NULL;
12537a
 }
12537a
 
12537a
-int ZlibInStream::overrun(int itemSize, int nItems, bool wait)
12537a
+size_t ZlibInStream::overrun(size_t itemSize, size_t nItems, bool wait)
12537a
 {
12537a
   if (itemSize > bufSize)
12537a
     throw Exception("ZlibInStream overrun: max itemSize exceeded");
12537a
@@ -108,12 +108,12 @@ int ZlibInStream::overrun(int itemSize, int nItems, bool wait)
12537a
   end -= ptr - start;
12537a
   ptr = start;
12537a
 
12537a
-  while (end - ptr < itemSize) {
12537a
+  while ((size_t)(end - ptr) < itemSize) {
12537a
     if (!decompress(wait))
12537a
       return 0;
12537a
   }
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
@@ -131,11 +131,11 @@ bool ZlibInStream::decompress(bool wait)
12537a
   zs->next_out = (U8*)end;
12537a
   zs->avail_out = start + bufSize - end;
12537a
 
12537a
-  int n = underlying->check(1, 1, wait);
12537a
+  size_t n = underlying->check(1, 1, wait);
12537a
   if (n == 0) return false;
12537a
   zs->next_in = (U8*)underlying->getptr();
12537a
   zs->avail_in = underlying->getend() - underlying->getptr();
12537a
-  if ((int)zs->avail_in > bytesIn)
12537a
+  if (zs->avail_in > bytesIn)
12537a
     zs->avail_in = bytesIn;
12537a
 
12537a
   int rc = inflate(zs, Z_SYNC_FLUSH);
12537a
diff --git a/common/rdr/ZlibInStream.h b/common/rdr/ZlibInStream.h
12537a
index 86ba1ff..08784b0 100644
12537a
--- a/common/rdr/ZlibInStream.h
12537a
+++ b/common/rdr/ZlibInStream.h
12537a
@@ -34,12 +34,12 @@ namespace rdr {
12537a
 
12537a
   public:
12537a
 
12537a
-    ZlibInStream(int bufSize=0);
12537a
+    ZlibInStream(size_t bufSize=0);
12537a
     virtual ~ZlibInStream();
12537a
 
12537a
-    void setUnderlying(InStream* is, int bytesIn);
12537a
+    void setUnderlying(InStream* is, size_t bytesIn);
12537a
     void flushUnderlying();
12537a
-    int pos();
12537a
+    size_t pos();
12537a
     void reset();
12537a
 
12537a
   private:
12537a
@@ -47,14 +47,14 @@ namespace rdr {
12537a
     void init();
12537a
     void deinit();
12537a
 
12537a
-    int overrun(int itemSize, int nItems, bool wait);
12537a
+    size_t overrun(size_t itemSize, size_t nItems, bool wait);
12537a
     bool decompress(bool wait);
12537a
 
12537a
     InStream* underlying;
12537a
-    int bufSize;
12537a
-    int offset;
12537a
+    size_t bufSize;
12537a
+    size_t offset;
12537a
     z_stream_s* zs;
12537a
-    int bytesIn;
12537a
+    size_t bytesIn;
12537a
     U8* start;
12537a
   };
12537a
 
12537a
diff --git a/common/rdr/ZlibOutStream.cxx b/common/rdr/ZlibOutStream.cxx
12537a
index 9d9f8ba..4e7ffd6 100644
12537a
--- a/common/rdr/ZlibOutStream.cxx
12537a
+++ b/common/rdr/ZlibOutStream.cxx
12537a
@@ -30,7 +30,7 @@ using namespace rdr;
12537a
 
12537a
 enum { DEFAULT_BUF_SIZE = 16384 };
12537a
 
12537a
-ZlibOutStream::ZlibOutStream(OutStream* os, int bufSize_, int compressLevel)
12537a
+ZlibOutStream::ZlibOutStream(OutStream* os, size_t bufSize_, int compressLevel)
12537a
   : underlying(os), compressionLevel(compressLevel), newLevel(compressLevel),
12537a
     bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
12537a
 {
12537a
@@ -72,7 +72,7 @@ void ZlibOutStream::setCompressionLevel(int level)
12537a
   newLevel = level;
12537a
 }
12537a
 
12537a
-int ZlibOutStream::length()
12537a
+size_t ZlibOutStream::length()
12537a
 {
12537a
   return offset + ptr - start;
12537a
 }
12537a
@@ -95,7 +95,7 @@ void ZlibOutStream::flush()
12537a
   ptr = start;
12537a
 }
12537a
 
12537a
-int ZlibOutStream::overrun(int itemSize, int nItems)
12537a
+size_t ZlibOutStream::overrun(size_t itemSize, size_t nItems)
12537a
 {
12537a
 #ifdef ZLIBOUT_DEBUG
12537a
   fprintf(stderr,"zos overrun\n");
12537a
@@ -106,7 +106,7 @@ int ZlibOutStream::overrun(int itemSize, int nItems)
12537a
 
12537a
   checkCompressionLevel();
12537a
 
12537a
-  while (end - ptr < itemSize) {
12537a
+  while ((size_t)(end - ptr) < itemSize) {
12537a
     zs->next_in = start;
12537a
     zs->avail_in = ptr - start;
12537a
 
12537a
@@ -127,7 +127,7 @@ int ZlibOutStream::overrun(int itemSize, int nItems)
12537a
     }
12537a
   }
12537a
 
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
 
12537a
   return nItems;
12537a
diff --git a/common/rdr/ZlibOutStream.h b/common/rdr/ZlibOutStream.h
12537a
index 2d82a13..11bb046 100644
12537a
--- a/common/rdr/ZlibOutStream.h
12537a
+++ b/common/rdr/ZlibOutStream.h
12537a
@@ -35,25 +35,25 @@ namespace rdr {
12537a
 
12537a
   public:
12537a
 
12537a
-    ZlibOutStream(OutStream* os=0, int bufSize=0, int compressionLevel=-1);
12537a
+    ZlibOutStream(OutStream* os=0, size_t bufSize=0, int compressionLevel=-1);
12537a
     virtual ~ZlibOutStream();
12537a
 
12537a
     void setUnderlying(OutStream* os);
12537a
     void setCompressionLevel(int level=-1);
12537a
     void flush();
12537a
-    int length();
12537a
+    size_t length();
12537a
 
12537a
   private:
12537a
 
12537a
-    int overrun(int itemSize, int nItems);
12537a
+    size_t overrun(size_t itemSize, size_t nItems);
12537a
     void deflate(int flush);
12537a
     void checkCompressionLevel();
12537a
 
12537a
     OutStream* underlying;
12537a
     int compressionLevel;
12537a
     int newLevel;
12537a
-    int bufSize;
12537a
-    int offset;
12537a
+    size_t bufSize;
12537a
+    size_t offset;
12537a
     z_stream_s* zs;
12537a
     U8* start;
12537a
   };
12537a
diff --git a/common/rfb/Configuration.cxx b/common/rfb/Configuration.cxx
12537a
index a5c2302..50ecfb3 100644
12537a
--- a/common/rfb/Configuration.cxx
12537a
+++ b/common/rfb/Configuration.cxx
12537a
@@ -403,7 +403,7 @@ StringParameter::operator const char *() const {
12537a
 // -=- BinaryParameter
12537a
 
12537a
 BinaryParameter::BinaryParameter(const char* name_, const char* desc_,
12537a
-				 const void* v, int l, ConfigurationObject co)
12537a
+				 const void* v, size_t l, ConfigurationObject co)
12537a
 : VoidParameter(name_, desc_, co), value(0), length(0), def_value((char*)v), def_length(l) {
12537a
   if (l) {
12537a
     value = new char[l];
12537a
@@ -423,7 +423,7 @@ bool BinaryParameter::setParam(const char* v) {
12537a
   return rdr::HexInStream::hexStrToBin(v, &value, &length);
12537a
 }
12537a
 
12537a
-void BinaryParameter::setParam(const void* v, int len) {
12537a
+void BinaryParameter::setParam(const void* v, size_t len) {
12537a
   LOCK_CONFIG;
12537a
   if (immutable) return; 
12537a
   vlog.debug("set %s(Binary)", getName());
12537a
@@ -444,7 +444,7 @@ char* BinaryParameter::getValueStr() const {
12537a
   return rdr::HexOutStream::binToHexStr(value, length);
12537a
 }
12537a
 
12537a
-void BinaryParameter::getData(void** data_, int* length_) const {
12537a
+void BinaryParameter::getData(void** data_, size_t* length_) const {
12537a
   LOCK_CONFIG;
12537a
   if (length_) *length_ = length;
12537a
   if (data_) {
12537a
diff --git a/common/rfb/Configuration.h b/common/rfb/Configuration.h
12537a
index fbf161d..ced726b 100644
12537a
--- a/common/rfb/Configuration.h
12537a
+++ b/common/rfb/Configuration.h
12537a
@@ -249,23 +249,24 @@ namespace rfb {
12537a
 
12537a
   class BinaryParameter : public VoidParameter {
12537a
   public:
12537a
-    BinaryParameter(const char* name_, const char* desc_, const void* v, int l,
12537a
-		    ConfigurationObject co=ConfGlobal);
12537a
+    BinaryParameter(const char* name_, const char* desc_,
12537a
+                    const void* v, size_t l,
12537a
+                    ConfigurationObject co=ConfGlobal);
12537a
     virtual ~BinaryParameter();
12537a
     virtual bool setParam(const char* value);
12537a
-    virtual void setParam(const void* v, int l);
12537a
+    virtual void setParam(const void* v, size_t l);
12537a
     virtual char* getDefaultStr() const;
12537a
     virtual char* getValueStr() const;
12537a
 
12537a
     // getData() will return length zero if there is no data
12537a
     // NB: data may be set to zero, OR set to a zero-length buffer
12537a
-    void getData(void** data, int* length) const;
12537a
+    void getData(void** data, size_t* length) const;
12537a
 
12537a
   protected:
12537a
     char* value;
12537a
-    int length;
12537a
+    size_t length;
12537a
     char* def_value;
12537a
-    int def_length;
12537a
+    size_t def_length;
12537a
   };
12537a
 
12537a
   // -=- ParameterIterator
12537a
diff --git a/common/rfb/Password.cxx b/common/rfb/Password.cxx
12537a
index 240c9d4..e4a508c 100644
12537a
--- a/common/rfb/Password.cxx
12537a
+++ b/common/rfb/Password.cxx
12537a
@@ -38,7 +38,7 @@ PlainPasswd::PlainPasswd() {}
12537a
 PlainPasswd::PlainPasswd(char* pwd) : CharArray(pwd) {
12537a
 }
12537a
 
12537a
-PlainPasswd::PlainPasswd(int len) : CharArray(len) {
12537a
+PlainPasswd::PlainPasswd(size_t len) : CharArray(len) {
12537a
 }
12537a
 
12537a
 PlainPasswd::PlainPasswd(const ObfuscatedPasswd& obfPwd) : CharArray(9) {
12537a
@@ -63,11 +63,11 @@ void PlainPasswd::replaceBuf(char* b) {
12537a
 ObfuscatedPasswd::ObfuscatedPasswd() : length(0) {
12537a
 }
12537a
 
12537a
-ObfuscatedPasswd::ObfuscatedPasswd(int len) : CharArray(len), length(len) {
12537a
+ObfuscatedPasswd::ObfuscatedPasswd(size_t len) : CharArray(len), length(len) {
12537a
 }
12537a
 
12537a
 ObfuscatedPasswd::ObfuscatedPasswd(const PlainPasswd& plainPwd) : CharArray(8), length(8) {
12537a
-  int l = strlen(plainPwd.buf), i;
12537a
+  size_t l = strlen(plainPwd.buf), i;
12537a
   for (i=0; i<8; i++)
12537a
     buf[i] = i
12537a
   deskey(d3desObfuscationKey, EN0);
12537a
diff --git a/common/rfb/Password.h b/common/rfb/Password.h
12537a
index e5196ee..712bc81 100644
12537a
--- a/common/rfb/Password.h
12537a
+++ b/common/rfb/Password.h
12537a
@@ -28,7 +28,7 @@ namespace rfb {
12537a
   public:
12537a
     PlainPasswd();
12537a
     PlainPasswd(char* pwd);
12537a
-    PlainPasswd(int len);
12537a
+    PlainPasswd(size_t len);
12537a
     PlainPasswd(const ObfuscatedPasswd& obfPwd);
12537a
     ~PlainPasswd();
12537a
     void replaceBuf(char* b);
12537a
@@ -37,10 +37,10 @@ namespace rfb {
12537a
   class ObfuscatedPasswd : public CharArray {
12537a
   public:
12537a
     ObfuscatedPasswd();
12537a
-    ObfuscatedPasswd(int l);
12537a
+    ObfuscatedPasswd(size_t l);
12537a
     ObfuscatedPasswd(const PlainPasswd& plainPwd);
12537a
     ~ObfuscatedPasswd();
12537a
-    int length;
12537a
+    size_t length;
12537a
   };
12537a
 
12537a
 }
12537a
diff --git a/common/rfb/util.h b/common/rfb/util.h
12537a
index e9114c3..48c2ba6 100644
12537a
--- a/common/rfb/util.h
12537a
+++ b/common/rfb/util.h
12537a
@@ -45,7 +45,7 @@ namespace rfb {
12537a
   public:
12537a
     CharArray() : buf(0) {}
12537a
     CharArray(char* str) : buf(str) {} // note: assumes ownership
12537a
-    CharArray(int len) {
12537a
+    CharArray(size_t len) {
12537a
       buf = new char[len];
12537a
     }
12537a
     ~CharArray() {
12537a
diff --git a/win/rfb_win32/Registry.cxx b/win/rfb_win32/Registry.cxx
12537a
index afbdd06..a81e822 100644
12537a
--- a/win/rfb_win32/Registry.cxx
12537a
+++ b/win/rfb_win32/Registry.cxx
12537a
@@ -146,7 +146,7 @@ void RegKey::setString(const TCHAR* valname, const TCHAR* value) const {
12537a
   if (result != ERROR_SUCCESS) throw rdr::SystemException("setString", result);
12537a
 }
12537a
 
12537a
-void RegKey::setBinary(const TCHAR* valname, const void* value, int length) const {
12537a
+void RegKey::setBinary(const TCHAR* valname, const void* value, size_t length) const {
12537a
   LONG result = RegSetValueEx(key, valname, 0, REG_BINARY, (const BYTE*)value, length);
12537a
   if (result != ERROR_SUCCESS) throw rdr::SystemException("setBinary", result);
12537a
 }
12537a
@@ -169,12 +169,12 @@ TCHAR* RegKey::getString(const TCHAR* valname, const TCHAR* def) const {
12537a
   }
12537a
 }
12537a
 
12537a
-void RegKey::getBinary(const TCHAR* valname, void** data, int* length) const {
12537a
+void RegKey::getBinary(const TCHAR* valname, void** data, size_t* length) const {
12537a
   TCharArray hex(getRepresentation(valname));
12537a
   if (!rdr::HexInStream::hexStrToBin(CStr(hex.buf), (char**)data, length))
12537a
     throw rdr::Exception("getBinary failed");
12537a
 }
12537a
-void RegKey::getBinary(const TCHAR* valname, void** data, int* length, void* def, int deflen) const {
12537a
+void RegKey::getBinary(const TCHAR* valname, void** data, size_t* length, void* def, size_t deflen) const {
12537a
   try {
12537a
     getBinary(valname, data, length);
12537a
   } catch(rdr::Exception) {
12537a
diff --git a/win/rfb_win32/Registry.h b/win/rfb_win32/Registry.h
12537a
index 68d535c..2bb1691 100644
12537a
--- a/win/rfb_win32/Registry.h
12537a
+++ b/win/rfb_win32/Registry.h
12537a
@@ -71,15 +71,15 @@ namespace rfb {
12537a
 
12537a
       void setExpandString(const TCHAR* valname, const TCHAR* s) const;
12537a
       void setString(const TCHAR* valname, const TCHAR* s) const;
12537a
-      void setBinary(const TCHAR* valname, const void* data, int length) const;
12537a
+      void setBinary(const TCHAR* valname, const void* data, size_t length) const;
12537a
       void setInt(const TCHAR* valname, int i) const;
12537a
       void setBool(const TCHAR* valname, bool b) const;
12537a
 
12537a
       TCHAR* getString(const TCHAR* valname) const;
12537a
       TCHAR* getString(const TCHAR* valname, const TCHAR* def) const;
12537a
 
12537a
-      void getBinary(const TCHAR* valname, void** data, int* length) const;
12537a
-      void getBinary(const TCHAR* valname, void** data, int* length, void* def, int deflength) const;
12537a
+      void getBinary(const TCHAR* valname, void** data, size_t* length) const;
12537a
+      void getBinary(const TCHAR* valname, void** data, size_t* length, void* def, size_t deflength) const;
12537a
 
12537a
       int getInt(const TCHAR* valname) const;
12537a
       int getInt(const TCHAR* valname, int def) const;
12537a
diff --git a/tests/encperf.cxx b/tests/encperf.cxx
12537a
index 7b9ff81..f10dd6e 100644
12537a
--- a/tests/encperf.cxx
12537a
+++ b/tests/encperf.cxx
12537a
@@ -71,11 +71,11 @@ class DummyOutStream : public rdr::OutStream {
12537a
 public:
12537a
   DummyOutStream();
12537a
 
12537a
-  virtual int length();
12537a
+  virtual size_t length();
12537a
   virtual void flush();
12537a
 
12537a
 private:
12537a
-  virtual int overrun(int itemSize, int nItems);
12537a
+  virtual size_t overrun(size_t itemSize, size_t nItems);
12537a
 
12537a
   int offset;
12537a
   rdr::U8 buf[131072];
12537a
@@ -141,7 +141,7 @@ DummyOutStream::DummyOutStream()
12537a
   end = buf + sizeof(buf);
12537a
 }
12537a
 
12537a
-int DummyOutStream::length()
12537a
+size_t DummyOutStream::length()
12537a
 {
12537a
   flush();
12537a
   return offset;
12537a
@@ -153,10 +153,10 @@ void DummyOutStream::flush()
12537a
   ptr = buf;
12537a
 }
12537a
 
12537a
-int DummyOutStream::overrun(int itemSize, int nItems)
12537a
+size_t DummyOutStream::overrun(size_t itemSize, size_t nItems)
12537a
 {
12537a
   flush();
12537a
-  if (itemSize * nItems > end - ptr)
12537a
+  if (itemSize * nItems > (size_t)(end - ptr))
12537a
     nItems = (end - ptr) / itemSize;
12537a
   return nItems;
12537a
 }
12537a
diff --git a/common/rdr/SubstitutingInStream.h b/common/rdr/SubstitutingInStream.h
12537a
index 325b01c..4acfc04 100644
12537a
--- a/common/rdr/SubstitutingInStream.h
12537a
+++ b/common/rdr/SubstitutingInStream.h
12537a
@@ -45,9 +45,9 @@ namespace rdr {
12537a
       delete [] subst;
12537a
     }
12537a
 
12537a
-    int pos() { return underlying->pos(); }
12537a
+    size_t pos() { return underlying->pos(); }
12537a
 
12537a
-    virtual int overrun(int itemSize, int nItems, bool wait=true) {
12537a
+    virtual size_t overrun(size_t itemSize, size_t nItems, bool wait=true) {
12537a
       if (itemSize != 1)
12537a
         throw new rdr::Exception("SubstitutingInStream: itemSize must be 1");
12537a