[elinks-dev] Deflate encoding patch

Radim Kolar hsn at cybermail.net
Tue Oct 7 08:00:37 PDT 2003


diff -Naur elinks/src/protocol/http/http.c elinks-current/src/protocol/http/http.c
--- elinks/src/protocol/http/http.c	2003-10-05 18:59:36.000000000 +0200
+++ elinks-current/src/protocol/http/http.c	2003-10-07 16:23:00.000000000 +0200
@@ -1359,6 +1359,8 @@
 #ifdef HAVE_ZLIB_H
 				if (!strcasecmp(d, "gzip") || !strcasecmp(d, "x-gzip"))
 					conn->content_encoding = ENCODING_GZIP;
+				if (!strcasecmp(d, "deflate"))
+				        conn->content_encoding = ENCODING_DEFLATE;
 #endif
 #ifdef HAVE_BZLIB_H
 				if (!strcasecmp(d, "bzip2") || !strcasecmp(d, "x-bzip2"))
diff -Naur elinks/src/util/encoding.c elinks-current/src/util/encoding.c
--- elinks/src/util/encoding.c	2003-06-22 18:31:45.000000000 +0200
+++ elinks-current/src/util/encoding.c	2003-10-07 16:39:47.000000000 +0200
@@ -136,6 +136,122 @@
 	gzip_extensions,
 };
 
+/*************************************************************************
+  Deflate encoding (ENCODING_DEFLATE)
+*************************************************************************/
+
+struct deflate_data
+{
+    	FILE *file;
+	char readbuf[512];
+	z_stream zlib;
+	int last;
+};
+
+static int
+deflate_open(struct stream_encoded *stream, int fd)
+{
+	struct deflate_data *data = mem_alloc(sizeof(struct deflate_data));
+	int err;
+
+	if (!data) {
+		return -1;
+	}
+	
+	data->file = fdopen(fd, "rb");
+	data->last=0;
+	data->zlib.zalloc = (alloc_func)Z_NULL;
+	data->zlib.zfree  = (free_func)Z_NULL;
+	data->zlib.opaque = Z_NULL;
+	
+	data->zlib.next_in = NULL;
+	data->zlib.avail_in = 0;
+
+	err=inflateInit2(&data->zlib,-MAX_WBITS);
+	if(err != Z_OK)
+	{
+		mem_free(data);
+		return -1;
+	}
+
+	stream->data=data;
+
+	return 0;
+}
+
+static unsigned char *
+deflate_decode(struct stream_encoded *stream, unsigned char *data, int len, int *new_len)
+{
+        *new_len = len;
+	return data;
+}
+
+static int
+deflate_read(struct stream_encoded *stream, unsigned char *data, int len)
+{
+	struct deflate_data *ddata = (struct deflate_data *) stream->data;
+	int err;
+
+	if(ddata->last) 
+	{
+	    return 0;
+	}
+
+	ddata->zlib.avail_out=len;
+	ddata->zlib.next_out=data;
+
+	while(ddata->zlib.avail_out>0)
+	{
+	    if(ddata->zlib.avail_in==0)
+	    {
+		err=fread(ddata->readbuf,1,512,ddata->file);
+		if(err<=0 && ferror(ddata->file)) {
+		    printf("Read from file FAILED!\n\a");
+		    return -1;
+		}
+		printf("\nREAD: %d INPUT bytes\n",err);
+		ddata->zlib.avail_in=err;
+		ddata->zlib.next_in=ddata->readbuf;
+	    }
+	    /* sometimes we do not read any input data, it is 
+	     * correct to retry?
+	     */ 
+	    if(ddata->zlib.avail_in==0) continue; 
+	    err=inflate(&ddata->zlib,Z_SYNC_FLUSH);
+
+	    if(err==Z_STREAM_END) { 
+		ddata->last=1;
+		printf("\nSTREAM-END!\n");
+		break;
+	    }
+	    else if(err!=Z_OK) {
+		printf("\nERROR when decompresing\n\a");
+		return -1;
+	    }
+	}
+	
+	printf("\nDECOMPRESED %d bytes\n",len-ddata->zlib.avail_out);
+	return len-ddata->zlib.avail_out;
+}
+
+static void
+deflate_close(struct stream_encoded *stream)
+{
+	struct deflate_data *data = (struct deflate_data *) stream->data;
+
+        inflateEnd(&data->zlib);
+	fclose(data->file);
+	mem_free(data);
+}
+
+static struct decoding_backend deflate_decoding_backend = {
+        "deflate",
+	deflate_open,
+	deflate_read,
+	deflate_decode,
+	deflate_close,
+	dummy_extensions,
+};
 #endif
 
 
@@ -241,6 +357,11 @@
 #else
 	&dummy_decoding_backend,
 #endif
+#ifdef HAVE_ZLIB_H
+	&deflate_decoding_backend,
+#else
+	&dummy_decoding_backend,
+#endif
 };
 
 
diff -Naur elinks/src/util/encoding.h elinks-current/src/util/encoding.h
--- elinks/src/util/encoding.h	2003-06-22 18:31:58.000000000 +0200
+++ elinks-current/src/util/encoding.h	2003-10-07 16:28:25.000000000 +0200
@@ -7,6 +7,7 @@
 	ENCODING_NONE = 0,
 	ENCODING_GZIP,
 	ENCODING_BZIP2,
+	ENCODING_DEFLATE,
 
 	/* Max. number of known encoding including ENCODING_NONE. */
 	ENCODINGS_KNOWN,





More information about the elinks-dev mailing list