+ size_t in_len = session->inbuf.length();
+
+ /* Do we have anything to do? */
+ if (in_len <= 0)
+ return 0;
+
+ /* Prepare decompression */
+ session->d_stream.next_in = (Bytef *)session->inbuf.c_str();
+ session->d_stream.avail_in = in_len;
+
+ session->d_stream.next_out = (Bytef*)buffer;
+ /* Last byte is reserved for NULL terminating that beast */
+ session->d_stream.avail_out = count - 1;
+
+ /* Z_SYNC_FLUSH: Do as much as possible */
+ int ret = inflate(&session->d_stream, Z_SYNC_FLUSH);
+ /* TODO CloseStream() in here at random places */
+ switch (ret)
+ {
+ case Z_NEED_DICT:
+ case Z_STREAM_ERROR:
+ /* This is one of the 'not supposed to happen' things.
+ * Memory corruption, anyone?
+ */
+ Error(session, "General Error. This is not supposed to happen :/");
+ break;
+ case Z_DATA_ERROR:
+ Error(session, "Decompression failed, malformed data");
+ break;
+ case Z_MEM_ERROR:
+ Error(session, "Out of memory");
+ break;
+ case Z_BUF_ERROR:
+ /* This one is non-fatal, buffer is just full
+ * (can't happen here).
+ */
+ Error(session, "Internal error. This is not supposed to happen.");
+ break;
+ case Z_STREAM_END:
+ /* This module *never* generates these :/ */
+ Error(session, "End-of-stream marker received");
+ break;
+ case Z_OK:
+ break;
+ default:
+ /* NO WAI! This can't happen. All errors are handled above. */
+ Error(session, "Unknown error");
+ break;
+ }
+ if (ret != Z_OK)
+ {
+ readresult = 0;
+ return 0;