1 --- mod_gzip.c-old Fri Jan 26 10:50:05 2001
2 +++ mod_gzip.c Fri Jan 26 15:08:26 2001
4 * The GZP request control structure...
7 +#define GZIP_FORMAT (0)
8 +#define DEFLATE_FORMAT (1)
10 typedef struct _GZP_CONTROL {
12 int decompress; /* 0=Compress 1=Decompress */
14 + int compression_format; /* GZIP_FORMAT or DEFLATE_FORMAT */
16 /* Input control... */
18 int input_ismem; /* Input source is memory buffer, not file */
19 @@ -2209,10 +2214,11 @@
20 mod_gzip_printf( "%s: Checking for 'gzip' designator...\n",cn);
23 - if ( strstr( has_encoding, "gzip" ) )
24 + if ( strstr( has_encoding, "gzip" ) ||
25 + strstr( has_encoding, "deflate" ) )
27 #ifdef MOD_GZIP_DEBUG1
28 - mod_gzip_printf( "%s: 'Content-encoding:' field contains 'gzip' designator...\n",cn);
29 + mod_gzip_printf( "%s: 'Content-encoding:' field contains 'gzip' or 'deflate' designator...\n",cn);
30 mod_gzip_printf( "%s: Pre-compression is assumed.\n",cn);
31 mod_gzip_printf( "%s: Exit > return( DECLINED ) >\n",cn);
34 else /* 'gzip' designator not found... */
36 #ifdef MOD_GZIP_DEBUG1
37 - mod_gzip_printf( "%s: 'Content-encoding:' field does NOT contain 'gzip' designator...\n",cn);
38 + mod_gzip_printf( "%s: 'Content-encoding:' field does NOT contain 'gzip' or 'deflate' designator...\n",cn);
39 mod_gzip_printf( "%s: Assuming OK to proceed...\n",cn);
42 @@ -2347,10 +2353,21 @@
43 if ( accept_encoding )
45 /* ...and if it has the right 'gzip' indicator... */
47 + /* We record the compression format in a request note, so we
48 + * can get it again later, and so it can potentially be logged.
50 if ( strstr( accept_encoding, "gzip" ) )
52 process = 1; /* ...set the 'process' flag TRUE */
53 + ap_table_setn( r->notes,"mod_gzip_compression_format",
54 + ap_pstrdup(r->pool,"gzip"));
57 + else if ( strstr( accept_encoding, "deflate" ) )
59 + process = 1; /* ...set the 'process' flag TRUE */
60 + ap_table_setn( r->notes,"mod_gzip_compression_format",
61 + ap_pstrdup(r->pool,"deflate"));
64 }/* End 'if( accept_encoding )' */
66 else /* 'gzip' designator was seen in 'Accept-Encoding:' field */
68 #ifdef MOD_GZIP_DEBUG1
69 - mod_gzip_printf( "%s: 'gzip' capability specified by user-agent.\n",cn);
70 + mod_gzip_printf( "%s: 'gzip' or 'deflate' capability specified by user-agent.\n",cn);
71 mod_gzip_printf( "%s: Assuming OK to proceed...\n",cn);
76 char tmp[ MOD_GZIP_LARGE_BUFFER_SIZE + 2 ]; /* Scratch buffer */
78 - char actual_content_encoding_name[] = "gzip"; /* Adjustable */
79 + char *actual_content_encoding_name = "gzip"; /* Adjustable */
80 + const char *compression_format;
82 #ifdef MOD_GZIP_DEBUG1
83 char cn[]="mod_gzip_encode_and_transmit()";
84 @@ -4470,6 +4488,18 @@
86 gzp->decompress = 0; /* Perform encoding */
88 + /* Recover the compression format we're supposed to use. */
89 + compression_format = ap_table_get(r->notes, "mod_gzip_compression_format");
90 + if (compression_format && strcmp(compression_format, "deflate") == 0)
92 + actual_content_encoding_name = "deflate";
93 + gzp->compression_format = DEFLATE_FORMAT;
97 + gzp->compression_format = GZIP_FORMAT;
100 if ( input_size <= (long) conf->maximum_inmem_size )
102 /* The input source is small enough to compress directly */
103 @@ -4591,6 +4621,7 @@
105 #ifdef MOD_GZIP_DEBUG1
106 mod_gzip_printf( "%s: gzp->decompress = %d\n" ,cn,gzp->decompress);
107 + mod_gzip_printf( "%s: gzp->compression_format = %d\n",cn,gzp->compression_format);
108 mod_gzip_printf( "%s: gzp->input_ismem = %d\n", cn,gzp->input_ismem);
109 mod_gzip_printf( "%s: gzp->output_ismem = %d\n", cn,gzp->output_ismem);
110 mod_gzip_printf( "%s: gzp->input_filename = [%s]\n",cn,gzp->input_filename);
111 @@ -7256,6 +7287,8 @@
114 typedef struct _GZ1 {
115 + long compression_format;
120 @@ -7345,6 +7378,7 @@
127 uch length_code[MAX_MATCH-MIN_MATCH+1];
128 @@ -7449,6 +7483,15 @@
130 void error( char *msg );
132 +/* XXX - Precomputed zlib header. If you change the window size or
133 + * compression level from the defaults, this will break badly. The
134 + * algorithm to build this is fairly complex; you can find it in
135 + * the file deflate.c from the zlib distribution.
137 +#define ZLIB_HEADER "\170
\9c"
139 +ulg adler32(ulg adler, uch *buf, unsigned len);
144 @@ -9088,10 +9131,20 @@
145 if ( len == (unsigned)(-1) || len == 0 )
147 gz1->crc = gz1->crc ^ 0xffffffffL;
148 + /* XXX - Do we need to do something with Adler CRC's here?
149 + * I don't think so--they don't seem to need postprocessing. */
153 - updcrc( gz1, (uch*)buf, len );
154 + if (gz1->compression_format != DEFLATE_FORMAT)
156 + updcrc( gz1, (uch*)buf, len );
160 + gz1->adler = adler32(gz1->adler, (uch*)buf, len);
163 gz1->bytes_in += (ulg)len;
166 @@ -9288,6 +9341,7 @@
173 #define GZS_DEFLATE1 3
174 @@ -9317,6 +9371,7 @@
177 gz1->decompress = gzp->decompress;
178 + gz1->compression_format = gzp->compression_format;
180 strcpy( gz1->ifname, gzp->input_filename );
181 strcpy( gz1->ofname, gzp->output_filename );
182 @@ -9489,6 +9544,7 @@
187 int gzs_zip1( PGZ1 gz1 )
190 @@ -9499,21 +9555,40 @@
192 gz1->method = DEFLATED;
194 - put_byte(GZIP_MAGIC[0]);
195 - put_byte(GZIP_MAGIC[1]);
196 - put_byte(DEFLATED);
197 + if (gz1->compression_format != DEFLATE_FORMAT)
199 + put_byte(GZIP_MAGIC[0]);
200 + put_byte(GZIP_MAGIC[1]);
201 + put_byte(DEFLATED);
205 + /* Yes, I know RFC 1951 doesn't mention any header at the start of
206 + * a deflated document, but zlib absolutely requires one. And since nearly
207 + * all "deflate" implementations use zlib, we need to play along with this
209 + put_byte(ZLIB_HEADER[0]);
210 + put_byte(ZLIB_HEADER[1]);
213 if ( gz1->save_orig_name )
219 - put_long(gz1->time_stamp);
222 + if (gz1->compression_format != DEFLATE_FORMAT)
225 + put_long(gz1->time_stamp);
227 - updcrc( gz1, NULL, 0 );
229 + updcrc( gz1, NULL, 0 );
233 + /* Deflate compression uses an Adler32 CRC, not a CRC32. */
237 gz1->state = GZS_ZIP2;
239 @@ -9529,18 +9604,20 @@
240 bi_init( gz1, gz1->ofd );
241 ct_init( gz1, &attr, &gz1->method );
242 lm_init( gz1, gz1->level, &deflate_flags );
243 - put_byte((uch)deflate_flags);
247 - if ( gz1->save_orig_name )
248 + if (gz1->compression_format != DEFLATE_FORMAT)
250 - char *p = gz1_basename( gz1, gz1->ifname );
251 + put_byte((uch)deflate_flags);
253 + if ( gz1->save_orig_name )
255 + char *p = gz1_basename( gz1, gz1->ifname );
267 gz1->header_bytes = (long)gz1->outcnt;
268 @@ -9674,10 +9751,25 @@
272 - put_long( gz1->crc );
273 - put_long( gz1->bytes_in );
275 - gz1->header_bytes += 2*sizeof(long);
276 + if (gz1->compression_format != DEFLATE_FORMAT)
278 + put_long( gz1->crc );
279 + put_long( gz1->bytes_in );
280 + gz1->header_bytes += 2*sizeof(long);
284 + /* Append an Adler32 CRC to our deflated data.
285 + * Yes, I know RFC 1951 doesn't mention any CRC at the end of a
286 + * deflated document, but zlib absolutely requires one. And since nearly
287 + * all "deflate" implementations use zlib, we need to play along with this
289 + put_byte( (gz1->adler >> 24) );
290 + put_byte( (gz1->adler >> 16) & 0xFF );
291 + put_byte( (gz1->adler >> 8) & 0xFF );
292 + put_byte( (gz1->adler ) & 0xFF );
293 + gz1->header_bytes += 4*sizeof(uch);
298 @@ -9685,6 +9777,67 @@
304 +/* =========================================================================
305 + adler32 -- compute the Adler-32 checksum of a data stream
306 + Copyright (C) 1995-1998 Mark Adler
308 + This software is provided 'as-is', without any express or implied
309 + warranty. In no event will the authors be held liable for any damages
310 + arising from the use of this software.
312 + Permission is granted to anyone to use this software for any purpose,
313 + including commercial applications, and to alter it and redistribute it
314 + freely, subject to the following restrictions:
316 + 1. The origin of this software must not be misrepresented; you must not
317 + claim that you wrote the original software. If you use this software
318 + in a product, an acknowledgment in the product documentation would be
319 + appreciated but is not required.
320 + 2. Altered source versions must be plainly marked as such, and must not be
321 + misrepresented as being the original software.
322 + 3. This notice may not be removed or altered from any source distribution.
324 + Modified by Eric Kidd <eric.kidd@pobox.com> to play nicely with mod_gzip.
327 +#define BASE 65521L /* largest prime smaller than 65536 */
329 +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
331 +#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
332 +#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
333 +#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
334 +#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
335 +#define DO16(buf) DO8(buf,0); DO8(buf,8);
337 +ulg adler32(ulg adler, uch *buf, unsigned len)
339 + unsigned long s1 = adler & 0xffff;
340 + unsigned long s2 = (adler >> 16) & 0xffff;
343 + if (buf == NULL) return 1L;
346 + k = len < NMAX ? len : NMAX;
360 + return (s2 << 16) | s1;