+#ifdef FEATURE_ZLIB
+/*********************************************************************
+ *
+ * Function : decompress_iob
+ *
+ * Description : Decompress buffered page, expanding the
+ * buffer as necessary. csp->iob->cur
+ * should point to the the beginning of the
+ * compressed data block.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : JB_ERR_OK on success,
+ * JB_ERR_MEMORY if out-of-memory limit reached, and
+ * JB_ERR_COMPRESS if error decompressing buffer.
+ *
+ *********************************************************************/
+jb_err decompress_iob(struct client_state *csp)
+{
+ char *buf; /* new, uncompressed buffer */
+ char *cur; /* Current iob position (to keep the original
+ * iob->cur unmodified if we return early) */
+ size_t bufsize; /* allocated size of the new buffer */
+ size_t old_size; /* Content size before decompression */
+ size_t skip_size; /* Number of bytes at the beginning of the iob
+ that we should NOT decompress. */
+ int status; /* return status of the inflate() call */
+ z_stream zstr; /* used by calls to zlib */
+
+ assert(csp->iob->cur - csp->iob->buf > 0);
+ assert(csp->iob->eod - csp->iob->cur > 0);
+
+ bufsize = csp->iob->size;
+ skip_size = (size_t)(csp->iob->cur - csp->iob->buf);
+ old_size = (size_t)(csp->iob->eod - csp->iob->cur);
+
+ cur = csp->iob->cur;
+
+ if (bufsize < 10)
+ {
+ /*
+ * This is to protect the parsing of gzipped data,
+ * but it should(?) be valid for deflated data also.
+ */
+ log_error(LOG_LEVEL_ERROR, "Buffer too small decompressing iob");
+ return JB_ERR_COMPRESS;
+ }
+
+ if (csp->content_type & CT_GZIP)
+ {
+ /*
+ * Our task is slightly complicated by the facts that data
+ * compressed by gzip does not include a zlib header, and
+ * that there is no easily accessible interface in zlib to
+ * handle a gzip header. We strip off the gzip header by
+ * hand, and later inform zlib not to expect a header.
+ */
+
+ /*
+ * Strip off the gzip header. Please see RFC 1952 for more
+ * explanation of the appropriate fields.
+ */
+ if ((*cur++ != (char)0x1f)
+ || (*cur++ != (char)0x8b)
+ || (*cur++ != Z_DEFLATED))
+ {
+ log_error(LOG_LEVEL_ERROR, "Invalid gzip header when decompressing");
+ return JB_ERR_COMPRESS;
+ }
+ else
+ {
+ int flags = *cur++;
+ /*
+ * XXX: These magic numbers should be replaced
+ * with macros to give a better idea what they do.
+ */
+ if (flags & 0xe0)
+ {
+ /* The gzip header has reserved bits set; bail out. */
+ log_error(LOG_LEVEL_ERROR, "Invalid gzip header flags when decompressing");
+ return JB_ERR_COMPRESS;
+ }
+ cur += 6;
+
+ /* Skip extra fields if necessary. */
+ if (flags & 0x04)
+ {
+ /*
+ * Skip a given number of bytes, specified
+ * as a 16-bit little-endian value.
+ */
+ /*
+ * XXX: This code used to be:
+ *
+ * csp->iob->cur += *csp->iob->cur++ + (*csp->iob->cur++ << 8);
+ *
+ * which I had to change into:
+ *
+ * cur += *cur++ + (*cur++ << 8);
+ *
+ * at which point gcc43 finally noticed that the value
+ * of cur is undefined (it depends on which of the
+ * summands is evaluated first).
+ *
+ * I haven't come across a site where this
+ * code is actually executed yet, but I hope
+ * it works anyway.
+ */
+ int skip_bytes;
+ skip_bytes = *cur++;
+ skip_bytes = *cur++ << 8;
+
+ assert(skip_bytes == *csp->iob->cur - 2 + ((*csp->iob->cur - 1) << 8));
+
+ /*
+ * The number of bytes to skip should be positive
+ * and we'd like to stay in the buffer.
+ */
+ if ((skip_bytes < 0) || (skip_bytes >= (csp->iob->eod - cur)))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Unreasonable amount of bytes to skip (%d). Stopping decompression",
+ skip_bytes);
+ return JB_ERR_COMPRESS;
+ }
+ log_error(LOG_LEVEL_INFO,
+ "Skipping %d bytes for gzip compression. Does this sound right?",
+ skip_bytes);
+ cur += skip_bytes;
+ }
+
+ /* Skip the filename if necessary. */
+ if (flags & 0x08)
+ {
+ /* A null-terminated string is supposed to follow. */
+ while (*cur++ && (cur < csp->iob->eod));
+
+ }
+
+ /* Skip the comment if necessary. */
+ if (flags & 0x10)
+ {
+ /* A null-terminated string is supposed to follow. */
+ while (*cur++ && (cur < csp->iob->eod));
+ }
+
+ /* Skip the CRC if necessary. */
+ if (flags & 0x02)
+ {
+ cur += 2;
+ }
+
+ if (cur >= csp->iob->eod)
+ {
+ /*
+ * If the current position pointer reached or passed
+ * the buffer end, we were obviously tricked to skip
+ * too much.
+ */
+ log_error(LOG_LEVEL_ERROR,
+ "Malformed gzip header detected. Aborting decompression.");
+ return JB_ERR_COMPRESS;
+ }
+ }
+ }
+ else if (csp->content_type & CT_DEFLATE)
+ {
+ /*
+ * XXX: The debug level should be lowered
+ * before the next stable release.
+ */
+ log_error(LOG_LEVEL_INFO, "Decompressing deflated iob: %d", *cur);
+ /*
+ * In theory (that is, according to RFC 1950), deflate-compressed
+ * data should begin with a two-byte zlib header and have an
+ * adler32 checksum at the end. It seems that in practice only
+ * the raw compressed data is sent. Note that this means that
+ * we are not RFC 1950-compliant here, but the advantage is that
+ * this actually works. :)
+ *
+ * We add a dummy null byte to tell zlib where the data ends,
+ * and later inform it not to expect a header.
+ *
+ * Fortunately, add_to_iob() has thoughtfully null-terminated
+ * the buffer; we can just increment the end pointer to include
+ * the dummy byte.
+ */
+ csp->iob->eod++;
+ }
+ else
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Unable to determine compression format for decompression");
+ return JB_ERR_COMPRESS;
+ }
+
+ /* Set up the fields required by zlib. */
+ zstr.next_in = (Bytef *)cur;
+ zstr.avail_in = (unsigned int)(csp->iob->eod - cur);
+ zstr.zalloc = Z_NULL;
+ zstr.zfree = Z_NULL;
+ zstr.opaque = Z_NULL;
+
+ /*
+ * Passing -MAX_WBITS to inflateInit2 tells the library
+ * that there is no zlib header.
+ */
+ if (inflateInit2 (&zstr, -MAX_WBITS) != Z_OK)
+ {
+ log_error(LOG_LEVEL_ERROR, "Error initializing decompression");
+ return JB_ERR_COMPRESS;
+ }
+
+ /*
+ * Next, we allocate new storage for the inflated data.
+ * We don't modify the existing iob yet, so in case there
+ * is error in decompression we can recover gracefully.
+ */
+ buf = zalloc(bufsize);
+ if (NULL == buf)
+ {
+ log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
+ return JB_ERR_MEMORY;
+ }
+
+ assert(bufsize >= skip_size);
+ memcpy(buf, csp->iob->buf, skip_size);
+ zstr.avail_out = bufsize - skip_size;
+ zstr.next_out = (Bytef *)buf + skip_size;
+
+ /* Try to decompress the whole stream in one shot. */
+ while (Z_BUF_ERROR == (status = inflate(&zstr, Z_FINISH)))
+ {
+ /* We need to allocate more memory for the output buffer. */
+
+ char *tmpbuf; /* used for realloc'ing the buffer */
+ size_t oldbufsize = bufsize; /* keep track of the old bufsize */
+
+ /*
+ * If zlib wants more data then there's a problem, because
+ * the complete compressed file should have been buffered.
+ */
+ if (0 == zstr.avail_in)
+ {
+ log_error(LOG_LEVEL_ERROR, "Unexpected end of compressed iob");
+ return JB_ERR_COMPRESS;
+ }
+
+ /*
+ * If we tried the limit and still didn't have enough
+ * memory, just give up.
+ */
+ if (bufsize == csp->config->buffer_limit)
+ {
+ log_error(LOG_LEVEL_ERROR, "Buffer limit reached while decompressing iob");
+ return JB_ERR_MEMORY;
+ }
+
+ /* Try doubling the buffer size each time. */
+ bufsize *= 2;
+
+ /* Don't exceed the buffer limit. */
+ if (bufsize > csp->config->buffer_limit)
+ {
+ bufsize = csp->config->buffer_limit;
+ }
+
+ /* Try to allocate the new buffer. */
+ tmpbuf = realloc(buf, bufsize);
+ if (NULL == tmpbuf)
+ {
+ log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
+ freez(buf);
+ return JB_ERR_MEMORY;
+ }
+ else
+ {
+ char *oldnext_out = (char *)zstr.next_out;
+
+ /*
+ * Update the fields for inflate() to use the new
+ * buffer, which may be in a location different from
+ * the old one.
+ */
+ zstr.avail_out += bufsize - oldbufsize;
+ zstr.next_out = (Bytef *)tmpbuf + bufsize - zstr.avail_out;
+
+ /*
+ * Compare with an uglier method of calculating these values
+ * that doesn't require the extra oldbufsize variable.
+ */
+ assert(zstr.avail_out == tmpbuf + bufsize - (char *)zstr.next_out);
+ assert((char *)zstr.next_out == tmpbuf + ((char *)oldnext_out - buf));
+ assert(zstr.avail_out > 0);
+
+ buf = tmpbuf;
+ }
+ }
+
+ if (Z_STREAM_ERROR == inflateEnd(&zstr))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Inconsistent stream state after decompression: %s", zstr.msg);
+ /*
+ * XXX: Intentionally no return.
+ *
+ * According to zlib.h, Z_STREAM_ERROR is returned
+ * "if the stream state was inconsistent".
+ *
+ * I assume in this case inflate()'s status
+ * would also be something different than Z_STREAM_END
+ * so this check should be redundant, but lets see.
+ */
+ }
+
+ if (status != Z_STREAM_END)
+ {
+ /* We failed to decompress the stream. */
+ log_error(LOG_LEVEL_ERROR,
+ "Error in decompressing to the buffer (iob): %s", zstr.msg);
+ return JB_ERR_COMPRESS;
+ }
+
+ /*
+ * Finally, we can actually update the iob, since the
+ * decompression was successful. First, free the old
+ * buffer.
+ */
+ freez(csp->iob->buf);
+
+ /* Now, update the iob to use the new buffer. */
+ csp->iob->buf = buf;
+ csp->iob->cur = csp->iob->buf + skip_size;
+ csp->iob->eod = (char *)zstr.next_out;
+ csp->iob->size = bufsize;
+
+ /*
+ * Make sure the new uncompressed iob obeys some minimal
+ * consistency conditions.
+ */
+ if ((csp->iob->buf < csp->iob->cur)
+ && (csp->iob->cur <= csp->iob->eod)
+ && (csp->iob->eod <= csp->iob->buf + csp->iob->size))
+ {
+ const size_t new_size = (size_t)(csp->iob->eod - csp->iob->cur);
+ if (new_size > 0)
+ {
+ log_error(LOG_LEVEL_RE_FILTER,
+ "Decompression successful. Old size: %d, new size: %d.",
+ old_size, new_size);
+ }
+ else
+ {
+ /* zlib thinks this is OK, so lets do the same. */
+ log_error(LOG_LEVEL_INFO, "Decompression didn't result in any content.");
+ }
+ }
+ else
+ {
+ /* It seems that zlib did something weird. */
+ log_error(LOG_LEVEL_ERROR,
+ "Unexpected error decompressing the buffer (iob): %d==%d, %d>%d, %d<%d",
+ csp->iob->cur, csp->iob->buf + skip_size, csp->iob->eod, csp->iob->buf,
+ csp->iob->eod, csp->iob->buf + csp->iob->size);
+ return JB_ERR_COMPRESS;
+ }
+
+ return JB_ERR_OK;
+
+}
+#endif /* defined(FEATURE_ZLIB) */
+
+