initial load of upstream version 1.06.32
[xmlrpc-c] / tools / turbocharger / mod_gzip.c.diff
1 --- mod_gzip.c-old      Fri Jan 26 10:50:05 2001
2 +++ mod_gzip.c  Fri Jan 26 15:08:26 2001
3 @@ -575,10 +575,15 @@
4   * The GZP request control structure...
5   */
6  
7 +#define GZIP_FORMAT    (0)
8 +#define DEFLATE_FORMAT (1)
9 +
10  typedef struct _GZP_CONTROL {
11  
12      int   decompress;  /* 0=Compress 1=Decompress */
13  
14 +    int   compression_format;  /* GZIP_FORMAT or DEFLATE_FORMAT */
15 +
16      /* Input control... */
17  
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);
21         #endif
22  
23 -       if ( strstr( has_encoding, "gzip" ) )
24 +       if ( strstr( has_encoding, "gzip" ) ||
25 +                       strstr( has_encoding, "deflate" ) )
26           {
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);
32            #endif
33 @@ -2237,7 +2243,7 @@
34         else /* 'gzip' designator not found... */
35           {
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);
40            #endif
41           }
42 @@ -2347,10 +2353,21 @@
43      if ( accept_encoding )
44        {
45         /* ...and if it has the right 'gzip' indicator... */
46 -
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.
49 +        */
50         if ( strstr( accept_encoding, "gzip" ) )
51           {
52            process = 1; /* ...set the 'process' flag TRUE */
53 +          ap_table_setn( r->notes,"mod_gzip_compression_format",
54 +                                                ap_pstrdup(r->pool,"gzip"));
55 +
56 +         }
57 +       else if ( strstr( accept_encoding, "deflate" ) )
58 +         {
59 +          process = 1; /* ...set the 'process' flag TRUE */
60 +          ap_table_setn( r->notes,"mod_gzip_compression_format",
61 +                                                ap_pstrdup(r->pool,"deflate"));
62           }
63  
64        }/* End 'if( accept_encoding )' */
65 @@ -2388,7 +2405,7 @@
66      else /* 'gzip' designator was seen in 'Accept-Encoding:' field */
67        {
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);
72         #endif
73        }
74 @@ -4093,7 +4110,8 @@
75  
76      char tmp[ MOD_GZIP_LARGE_BUFFER_SIZE + 2 ]; /* Scratch buffer */
77  
78 -    char actual_content_encoding_name[] = "gzip"; /* Adjustable */
79 +    char *actual_content_encoding_name = "gzip"; /* Adjustable */
80 +       const char *compression_format;
81  
82      #ifdef MOD_GZIP_DEBUG1
83      char cn[]="mod_gzip_encode_and_transmit()";
84 @@ -4470,6 +4488,18 @@
85  
86      gzp->decompress = 0; /* Perform encoding */
87  
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)
91 +         {
92 +          actual_content_encoding_name = "deflate";
93 +          gzp->compression_format = DEFLATE_FORMAT;
94 +      }
95 +    else
96 +         {
97 +          gzp->compression_format = GZIP_FORMAT;
98 +      }
99 +
100      if ( input_size <= (long) conf->maximum_inmem_size )
101        {
102         /* The input source is small enough to compress directly */
103 @@ -4591,6 +4621,7 @@
104  
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 @@
112  };
113  
114  typedef struct _GZ1 {
115 + long     compression_format;
116 +
117   long     versionid1;
118   int      state;
119   int      done;
120 @@ -7345,6 +7378,7 @@
121   int  dbits;            
122   ulg  window_size;      
123   ulg  crc;              
124 + ulg  adler;
125  
126   uch  dist_code[512];
127   uch  length_code[MAX_MATCH-MIN_MATCH+1];
128 @@ -7449,6 +7483,15 @@
129  
130  void  error( char *msg   );
131  
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.
136 + */
137 +#define ZLIB_HEADER "\170\9c"
138 +
139 +ulg adler32(ulg adler, uch *buf, unsigned len);
140 +
141  int zip(
142  PGZ1 gz1, 
143  int  in,  
144 @@ -9088,10 +9131,20 @@
145   if ( len == (unsigned)(-1) || len == 0 )
146     {
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. */
150      return (int)len;
151     }
152  
153 - updcrc( gz1, (uch*)buf, len );
154 + if (gz1->compression_format != DEFLATE_FORMAT)
155 +   {
156 +    updcrc( gz1, (uch*)buf, len );
157 +   }
158 + else
159 +   {
160 +       gz1->adler = adler32(gz1->adler, (uch*)buf, len);
161 +   }
162 +
163   gz1->bytes_in += (ulg)len;
164  
165   return (int)len;
166 @@ -9288,6 +9341,7 @@
167   gz1->heap[k] = v;
168  }
169  
170 +
171  #define GZS_ZIP1      1
172  #define GZS_ZIP2      2
173  #define GZS_DEFLATE1  3
174 @@ -9317,6 +9371,7 @@
175     }
176  
177   gz1->decompress      = gzp->decompress;
178 + gz1->compression_format = gzp->compression_format;
179  
180   strcpy( gz1->ifname, gzp->input_filename  );
181   strcpy( gz1->ofname, gzp->output_filename );
182 @@ -9489,6 +9544,7 @@
183   return( rc );
184  }
185  
186 +
187  int gzs_zip1( PGZ1 gz1 )
188  {
189   uch  flags = 0;         
190 @@ -9499,21 +9555,40 @@
191  
192   gz1->method = DEFLATED;
193  
194 - put_byte(GZIP_MAGIC[0]); 
195 - put_byte(GZIP_MAGIC[1]);
196 - put_byte(DEFLATED);      
197 + if (gz1->compression_format != DEFLATE_FORMAT)
198 +   {
199 +    put_byte(GZIP_MAGIC[0]); 
200 +    put_byte(GZIP_MAGIC[1]);
201 +    put_byte(DEFLATED);      
202 +   }
203 + else
204 +   {
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
208 +     * brain damage. */
209 +    put_byte(ZLIB_HEADER[0]); 
210 +    put_byte(ZLIB_HEADER[1]);
211 +   }
212  
213   if ( gz1->save_orig_name )
214     {
215         flags |= ORIG_NAME;
216     }
217  
218 - put_byte(flags);           
219 - put_long(gz1->time_stamp); 
220 -
221 - gz1->crc = -1; 
222 + if (gz1->compression_format != DEFLATE_FORMAT)
223 +   {
224 +    put_byte(flags);           
225 +    put_long(gz1->time_stamp); 
226  
227 - updcrc( gz1, NULL, 0 ); 
228 +       gz1->crc = -1; 
229 +    updcrc( gz1, NULL, 0 ); 
230 +   }
231 + else
232 +   {
233 +       /* Deflate compression uses an Adler32 CRC, not a CRC32. */
234 +       gz1->adler = 1L;
235 +   }
236  
237   gz1->state = GZS_ZIP2;
238  
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); 
244 -
245 - put_byte(OS_CODE); 
246  
247 - if ( gz1->save_orig_name )
248 + if (gz1->compression_format != DEFLATE_FORMAT)
249     {
250 -    char *p = gz1_basename( gz1, gz1->ifname );
251 +    put_byte((uch)deflate_flags); 
252 +    put_byte(OS_CODE); 
253 +    if ( gz1->save_orig_name )
254 +      {
255 +       char *p = gz1_basename( gz1, gz1->ifname );
256  
257 -    do {
258 -           put_char(*p);
259 +       do {
260 +              put_char(*p);
261  
262 -       } while (*p++);
263 +          } while (*p++);
264 +      }
265     }
266  
267   gz1->header_bytes = (long)gz1->outcnt;
268 @@ -9674,10 +9751,25 @@
269     }
270   #endif
271  
272 - put_long( gz1->crc      );
273 - put_long( gz1->bytes_in );
274 -
275 - gz1->header_bytes += 2*sizeof(long);
276 + if (gz1->compression_format != DEFLATE_FORMAT)
277 +   {
278 +    put_long( gz1->crc );
279 +    put_long( gz1->bytes_in );
280 +    gz1->header_bytes += 2*sizeof(long);
281 +   }
282 + else
283 +   {
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
288 +        * brain damage. */
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);
294 +   }
295  
296   flush_outbuf( gz1 );
297  
298 @@ -9685,6 +9777,67 @@
299  
300   return OK;
301  }
302 +
303 +
304 +/* =========================================================================
305 +   adler32 -- compute the Adler-32 checksum of a data stream
306 +   Copyright (C) 1995-1998 Mark Adler
307 +
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.
311 +
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:
315 +
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.
323 +
324 +   Modified by Eric Kidd <eric.kidd@pobox.com> to play nicely with mod_gzip.
325 + */
326 +
327 +#define BASE 65521L /* largest prime smaller than 65536 */
328 +#define NMAX 5552
329 +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
330 +
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);
336 +
337 +ulg adler32(ulg adler, uch *buf, unsigned len)
338 +{
339 +    unsigned long s1 = adler & 0xffff;
340 +    unsigned long s2 = (adler >> 16) & 0xffff;
341 +    int k;
342 +
343 +    if (buf == NULL) return 1L;
344 +
345 +    while (len > 0) {
346 +        k = len < NMAX ? len : NMAX;
347 +        len -= k;
348 +        while (k >= 16) {
349 +            DO16(buf);
350 +           buf += 16;
351 +            k -= 16;
352 +        }
353 +        if (k != 0) do {
354 +            s1 += *buf++;
355 +           s2 += s1;
356 +        } while (--k);
357 +        s1 %= BASE;
358 +        s2 %= BASE;
359 +    }
360 +    return (s2 << 16) | s1;
361 +}
362 +
363  
364  /* END OF FILE */
365