r5679 - trunk/varnish-cache/bin/varnishd

phk at varnish-cache.org phk at varnish-cache.org
Tue Jan 4 11:50:54 CET 2011


Author: phk
Date: 2011-01-04 11:50:53 +0100 (Tue, 04 Jan 2011)
New Revision: 5679

Modified:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_gzip.c
Log:
A first order vgz API



Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2011-01-04 10:49:09 UTC (rev 5678)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2011-01-04 10:50:53 UTC (rev 5679)
@@ -584,6 +584,14 @@
 int FetchReqBody(struct sess *sp);
 void Fetch_Init(void);
 
+/* cache_gzip.c */
+struct vgz;
+
+struct vgz *VGZ_NewUnzip(struct sess *sp, struct ws *tmp, struct ws *buf);
+int VGZ_Feed(struct vgz *, const void *, size_t len);
+int VGZ_Produce(struct vgz *, const void **, size_t *len);
+int VGZ_Destroy(struct vgz **);
+
 /* cache_http.c */
 unsigned HTTP_estimate(unsigned nhttp);
 void HTTP_Copy(struct http *to, const struct http * const fm);

Modified: trunk/varnish-cache/bin/varnishd/cache_gzip.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_gzip.c	2011-01-04 10:49:09 UTC (rev 5678)
+++ trunk/varnish-cache/bin/varnishd/cache_gzip.c	2011-01-04 10:50:53 UTC (rev 5679)
@@ -25,7 +25,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * Interaction with the linvgz (libz) library.
+ * Interaction with the linvgz (zlib) library.
  *
  * The libz library pollutes namespace a LOT when you include the "zlib.h"
  * file so we contain the damage by vectoring all access to libz through
@@ -40,3 +40,132 @@
 #include "svnid.h"
 SVNID("$Id$")
 
+// #include <sys/types.h>
+// #include <sys/time.h>
+
+#include <stdio.h>
+// #include <stdlib.h>
+
+#include "cache.h"
+
+#include "zlib.h"
+
+struct vgz {
+	unsigned		magic;
+#define VGZ_MAGIC		0x162df0cb
+	struct sess 		*sp;
+	struct ws		*tmp;
+	char			*tmp_snapshot;
+
+	struct ws		*buf;
+	unsigned		bufsiz;
+
+	z_stream		vz;
+};
+
+/*--------------------------------------------------------------------*/
+
+static voidpf
+vgz_alloc(voidpf opaque, uInt items, uInt size)
+{
+	struct vgz *vg;
+
+	CAST_OBJ_NOTNULL(vg, opaque, VGZ_MAGIC);
+
+	return(WS_Alloc(vg->tmp, items * size));
+}
+
+static void
+vgz_free(voidpf opaque, voidpf address)
+{
+	struct vgz *vg;
+
+	CAST_OBJ_NOTNULL(vg, opaque, VGZ_MAGIC);
+	(void)address;
+}
+
+/*--------------------------------------------------------------------*/
+
+struct vgz *
+VGZ_NewUnzip(struct sess *sp, struct ws *tmp, struct ws *buf)
+{
+	struct vgz *vg;
+	char *s;
+
+	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
+
+	WS_Assert(tmp);
+	WS_Assert(buf);
+
+	s = WS_Snapshot(tmp);
+	vg = (void*)WS_Alloc(tmp, sizeof *vg);
+	AN(vg);
+	memset(vg, 0, sizeof *vg);
+	vg->magic = VGZ_MAGIC;
+	vg->sp = sp;
+	vg->tmp = tmp;
+	vg->buf = buf;
+	vg->tmp_snapshot = s;
+
+	vg->vz.zalloc = vgz_alloc;
+	vg->vz.zfree = vgz_free;
+	vg->vz.opaque = vg;
+
+	vg->bufsiz = WS_Reserve(buf, 0);
+
+	assert(Z_OK == inflateInit2(&vg->vz, 31));
+	return (vg);
+}
+
+/*--------------------------------------------------------------------*/
+
+int
+VGZ_Feed(struct vgz *vg, const void *ptr, size_t len)
+{
+
+	CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
+
+	vg->vz.next_in = TRUST_ME(ptr);
+	vg->vz.avail_in = len;
+
+	return (0);
+}
+
+/*--------------------------------------------------------------------*/
+
+int
+VGZ_Produce(struct vgz *vg, const void **pptr, size_t *plen)
+{
+	int i;
+
+	CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
+
+	*pptr = NULL;
+	*plen = 0;
+	vg->vz.next_out = (void*)vg->buf->f;
+	vg->vz.avail_out = vg->bufsiz;
+
+	i = inflate(&vg->vz, 0);
+	if (i == Z_OK || i == Z_STREAM_END) {
+		*pptr = vg->buf->f;
+		*plen = vg->bufsiz - vg->vz.avail_out;
+	}
+	if (i == Z_OK)
+		return (0);
+	if (i == Z_STREAM_END)
+		return (1);
+	return (-1);
+}
+
+/*--------------------------------------------------------------------*/
+
+int
+VGZ_Destroy(struct vgz **vg)
+{
+
+	CHECK_OBJ_NOTNULL(*vg, VGZ_MAGIC);
+	WS_Release((*vg)->buf, 0);
+	WS_Reset((*vg)->tmp, (*vg)->tmp_snapshot);
+	*vg = NULL;
+	return (0);
+}




More information about the varnish-commit mailing list