Clear up ZlibInStream::reset() behaviour

It previously only did a reset of the ZlibInStream object, not the
underlying zlib stream. It also had the side effect of flushing
the underlying stream and disassociating from it.

Clear things up by changing the naming, and introducing a proper
reset function (which is needed by the Tight decoder).
diff --git a/common/rdr/ZlibInStream.cxx b/common/rdr/ZlibInStream.cxx
index 6f3a7d0..4053bd1 100644
--- a/common/rdr/ZlibInStream.cxx
+++ b/common/rdr/ZlibInStream.cxx
@@ -16,6 +16,8 @@
  * USA.
  */
 
+#include <assert.h>
+
 #include <rdr/ZlibInStream.h>
 #include <rdr/Exception.h>
 #include <zlib.h>
@@ -26,26 +28,16 @@
 
 ZlibInStream::ZlibInStream(int bufSize_)
   : underlying(0), bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0),
-    bytesIn(0)
+    zs(NULL), bytesIn(0)
 {
-  zs = new z_stream;
-  zs->zalloc    = Z_NULL;
-  zs->zfree     = Z_NULL;
-  zs->opaque    = Z_NULL;
-  zs->next_in   = Z_NULL;
-  zs->avail_in  = 0;
-  if (inflateInit(zs) != Z_OK) {
-    delete zs;
-    throw Exception("ZlibInStream: inflateInit failed");
-  }
   ptr = end = start = new U8[bufSize];
+  init();
 }
 
 ZlibInStream::~ZlibInStream()
 {
+  deinit();
   delete [] start;
-  inflateEnd(zs);
-  delete zs;
 }
 
 void ZlibInStream::setUnderlying(InStream* is, int bytesIn_)
@@ -60,7 +52,7 @@
   return offset + ptr - start;
 }
 
-void ZlibInStream::reset()
+void ZlibInStream::removeUnderlying()
 {
   ptr = end = start;
   if (!underlying) return;
@@ -72,6 +64,38 @@
   underlying = 0;
 }
 
+void ZlibInStream::reset()
+{
+  deinit();
+  init();
+}
+
+void ZlibInStream::init()
+{
+  assert(zs == NULL);
+
+  zs = new z_stream;
+  zs->zalloc    = Z_NULL;
+  zs->zfree     = Z_NULL;
+  zs->opaque    = Z_NULL;
+  zs->next_in   = Z_NULL;
+  zs->avail_in  = 0;
+  if (inflateInit(zs) != Z_OK) {
+    delete zs;
+    zs = NULL;
+    throw Exception("ZlibInStream: inflateInit failed");
+  }
+}
+
+void ZlibInStream::deinit()
+{
+  assert(zs != NULL);
+  removeUnderlying();
+  inflateEnd(zs);
+  delete zs;
+  zs = NULL;
+}
+
 int ZlibInStream::overrun(int itemSize, int nItems, bool wait)
 {
   if (itemSize > bufSize)
diff --git a/common/rdr/ZlibInStream.h b/common/rdr/ZlibInStream.h
index c26b6d6..6bd4da4 100644
--- a/common/rdr/ZlibInStream.h
+++ b/common/rdr/ZlibInStream.h
@@ -38,11 +38,15 @@
     virtual ~ZlibInStream();
 
     void setUnderlying(InStream* is, int bytesIn);
-    void reset();
+    void removeUnderlying();
     int pos();
+    void reset();
 
   private:
 
+    void init();
+    void deinit();
+
     int overrun(int itemSize, int nItems, bool wait);
     bool decompress(bool wait);
 
diff --git a/common/rfb/TightDecoder.cxx b/common/rfb/TightDecoder.cxx
index cab49fd..1bfa7a9 100644
--- a/common/rfb/TightDecoder.cxx
+++ b/common/rfb/TightDecoder.cxx
@@ -165,7 +165,7 @@
   bufptr += 1;
   buflen -= 1;
 
-  // Flush zlib streams if we are told by the server to do so.
+  // Reset zlib streams if we are told by the server to do so.
   for (int i = 0; i < 4; i++) {
     if (comp_ctl & 1) {
       zis[i].reset();
@@ -309,8 +309,8 @@
     netbuf = new rdr::U8[dataSize];
 
     zis[streamId].readBytes(netbuf, dataSize);
-    zis[streamId].reset();
 
+    zis[streamId].removeUnderlying();
     delete ms;
 
     bufptr = netbuf;
diff --git a/common/rfb/zrleDecode.h b/common/rfb/zrleDecode.h
index 2566171..07d6795 100644
--- a/common/rfb/zrleDecode.h
+++ b/common/rfb/zrleDecode.h
@@ -177,7 +177,7 @@
     }
   }
 
-  zis->reset();
+  zis->removeUnderlying();
 }
 
 #undef ZRLE_DECODE