]> git.pld-linux.org Git - packages/apache-mod_gzip.git/blob - mod_gzip.c
- sed -e "s/%{_sbindir}\/apxs -e/\%{apxs} -e/"
[packages/apache-mod_gzip.git] / mod_gzip.c
1
2 /* ====================================================================
3  *
4  * MOD_GZIP.C - Version 1.3.19.1a
5  *
6  * This program was developed and is currently maintained by
7  * Remote Communications, Inc.
8  * Home page: http://www.RemoteCommunications.com
9  *
10  * Original author: Kevin Kiley, CTO, Remote Communications, Inc.
11  * Email: Kiley@RemoteCommunications.com
12  *
13  * As of this writing there is an online support forum which
14  * anyone may join by following the instructions found at...
15  * http://lists.over.net/mailman/listinfo/mod_gzip
16  *
17  * ====================================================================
18  */
19
20 /* APACHE LICENSE: START
21  *
22  * Portions of this software are covered under the following license
23  * which, as it states, must remain included in this source code
24  * module and may not be altered in any way.
25  */
26
27 /* ====================================================================
28  * The Apache Software License, Version 1.1
29  *
30  * Copyright (c) 2000 The Apache Software Foundation.  All rights
31  * reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  *
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  *
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in
42  *    the documentation and/or other materials provided with the
43  *    distribution.
44  *
45  * 3. The end-user documentation included with the redistribution,
46  *    if any, must include the following acknowledgment:
47  *       "This product includes software developed by the
48  *        Apache Software Foundation (http://www.apache.org/)."
49  *    Alternately, this acknowledgment may appear in the software itself,
50  *    if and wherever such third-party acknowledgments normally appear.
51  *
52  * 4. The names "Apache" and "Apache Software Foundation" must
53  *    not be used to endorse or promote products derived from this
54  *    software without prior written permission. For written
55  *    permission, please contact apache@apache.org.
56  *
57  * 5. Products derived from this software may not be called "Apache",
58  *    nor may "Apache" appear in their name, without prior written
59  *    permission of the Apache Software Foundation.
60  *
61  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
62  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
63  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
65  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
66  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
67  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
68  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
69  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
70  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
71  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  * ====================================================================
74  *
75  * This software consists of voluntary contributions made by many
76  * individuals on behalf of the Apache Software Foundation.  For more
77  * information on the Apache Software Foundation, please see
78  * <http://www.apache.org/>.
79  *
80  * Portions of this software are based upon public domain software
81  * originally written at the National Center for Supercomputing Applications,
82  * University of Illinois, Urbana-Champaign.
83  */
84
85 /* APACHE LICENSE: END */
86
87 #define CORE_PRIVATE
88
89 #include "httpd.h"
90 #include "http_config.h"
91 #include "http_core.h"
92 #include "http_log.h"
93 #include "http_main.h"
94 #include "http_protocol.h"
95 #include "http_request.h"
96 #include "util_script.h"
97
98 extern API_VAR_EXPORT char ap_server_root[ MAX_STRING_LEN ];
99
100 char mod_gzip_version[] = "1.3.19.1a";
101
102 #define MOD_GZIP_VERSION_INFO_STRING "mod_gzip/1.3.19.1a"
103
104 /*
105  * Declare the NAME by which this module will be known.
106  * This is the NAME that will be used in LoadModule command(s).
107  */
108
109 extern module MODULE_VAR_EXPORT gzip_module;
110
111 /*
112  * Turn MOD_GZIP_USES_APACHE_LOGS switch ON to include the
113  * code that can update Apache logs with compression information.
114  */
115
116 #define MOD_GZIP_USES_APACHE_LOGS
117
118 /*
119  * Turn MOD_GZIP_DEBUG1 switch ON to include debug code.
120  * This is normally OFF by default and should only be
121  * used for diagnosing problems. The log output is
122  * VERY detailed and the log files will be HUGE.
123  */
124
125 /*
126 #define MOD_GZIP_DEBUG1
127 */
128
129 /*
130  * Turn MOD_GZIP_DEBUG1_VERBOSE1 switch ON to include
131  * some VERY 'verbose' debug code such as request record
132  * dumps during transactions and hex dumps of data.
133  * This is normally OFF by default. MOD_GZIP_DEBUG1
134  * switch must also be 'on' for this to have any effect.
135  */
136
137 #ifdef  MOD_GZIP_DEBUG1
138 #define MOD_GZIP_DEBUG1_VERBOSE1
139 #endif
140
141 /*
142  * Turn this 'define' on to send all log output to
143  * Apache error_log instead of a flat file. "LogLevel debug"
144  * must be set in httpd.conf for log output to appear in error_log.
145  */
146
147 /*
148 #define MOD_GZIP_LOG_IS_APACHE_LOG
149 */
150
151 /*
152  * Other globals...
153  */
154
155 #ifndef MOD_GZIP_MAX_PATH_LEN
156 #define MOD_GZIP_MAX_PATH_LEN 512
157 #endif
158
159 #ifdef WIN32
160 char mod_gzip_dirsep[]="\\"; 
161 #else 
162 char mod_gzip_dirsep[]="/";  
163 #endif 
164
165 #define MOD_GZIP_IMAP_MAXNAMES   256
166 #define MOD_GZIP_IMAP_MAXNAMELEN 90
167
168 #define MOD_GZIP_IMAP_ISNONE      0
169 #define MOD_GZIP_IMAP_ISMIME      1
170 #define MOD_GZIP_IMAP_ISHANDLER   2
171 #define MOD_GZIP_IMAP_ISFILE      3
172 #define MOD_GZIP_IMAP_ISURI       4
173 #define MOD_GZIP_IMAP_ISREQHEADER 5
174 #define MOD_GZIP_IMAP_ISRSPHEADER 6
175 #define MOD_GZIP_IMAP_ISPORT      7
176 #define MOD_GZIP_IMAP_ISADDRESS   8
177
178 #define MOD_GZIP_IMAP_STATIC1    9001
179 #define MOD_GZIP_IMAP_DYNAMIC1   9002
180 #define MOD_GZIP_IMAP_DYNAMIC2   9003
181 #define MOD_GZIP_IMAP_DECLINED1  9004
182
183 #define MOD_GZIP_REQUEST         9005
184 #define MOD_GZIP_RESPONSE        9006
185
186 typedef struct {
187     int      include;   
188     int      type;      
189     int      action;    
190     int      direction; 
191     unsigned port;      
192     int      len1;      
193     regex_t *pregex;    
194
195     char name[ MOD_GZIP_IMAP_MAXNAMELEN + 2 ];
196     int  namelen; 
197 } mod_gzip_imap;
198
199 int mod_gzip_imap_size = (int) sizeof( mod_gzip_imap );
200
201 #define MOD_GZIP_CONFIG_MODE_SERVER      1
202 #define MOD_GZIP_CONFIG_MODE_DIRECTORY   2
203 #define MOD_GZIP_CONFIG_MODE_COMBO       3
204
205 typedef struct {
206
207     int  cmode;
208
209     char *loc;                   
210
211     int  is_on;                  
212     int  is_on_set;
213
214     int  keep_workfiles;         
215     int  keep_workfiles_set;
216
217     int  dechunk;                
218     int  dechunk_set;
219
220     int  add_header_count;       
221     int  add_header_count_set;
222
223     int  min_http;               
224     int  min_http_set;
225
226     long minimum_file_size;      
227     int  minimum_file_size_set;
228
229     long maximum_file_size;      
230     int  maximum_file_size_set;
231
232     long maximum_inmem_size;     
233     int  maximum_inmem_size_set;
234
235     char temp_dir[256]; /* Length is safety-checked during startup */
236     int  temp_dir_set;
237
238     int imap_total_entries;
239     int imap_total_ismime;
240     int imap_total_isfile;
241     int imap_total_isuri;
242     int imap_total_ishandler;
243     int imap_total_isreqheader;
244     int imap_total_isrspheader;
245
246     mod_gzip_imap imap[ MOD_GZIP_IMAP_MAXNAMES + 1 ];
247
248     #define MOD_GZIP_COMMAND_VERSION_USED
249     #ifdef  MOD_GZIP_COMMAND_VERSION_USED
250     #define MOD_GZIP_COMMAND_VERSION 8001
251     #define MOD_GZIP_COMMAND_VERSION_MAXLEN 128
252     char command_version[ MOD_GZIP_COMMAND_VERSION_MAXLEN + 1 ];
253     int  command_version_set;
254     #endif
255
256     #define MOD_GZIP_CAN_NEGOTIATE
257     #ifdef  MOD_GZIP_CAN_NEGOTIATE
258     int can_negotiate;
259     int can_negotiate_set;
260     #endif
261
262 } mod_gzip_conf;
263
264 long mod_gzip_iusn = 0; 
265
266 int mod_gzip_strncmp( char *s1, char *s2, int len1 );
267 int mod_gzip_strnicmp( char *s1, char *s2, int len1 );
268 int mod_gzip_strcpy( char *s1, char *s2 );
269 int mod_gzip_strcat( char *s1, char *s2 );
270 int mod_gzip_strlen( char *s1 );
271 int mod_gzip_stringcontains( char *source, char *substring );
272 int mod_gzip_strendswith( char *s1, char *s2, int ignorcase );
273
274 #ifdef MOD_GZIP_CAN_NEGOTIATE
275 int mod_gzip_check_for_precompressed_file( request_rec *r );
276 #endif
277
278 int mod_gzip_create_unique_filename(
279 char *prefix,
280 char *target,
281 int   targetmaxlen
282 );
283
284 int mod_gzip_delete_file(
285 request_rec   *r,
286 char          *filename
287 );
288
289 int mod_gzip_flush_and_update_counts(
290 request_rec   *r,
291 mod_gzip_conf *dconf,
292 long           total_header_bytes_sent,
293 long           total_body_bytes_sent
294 );
295
296 long mod_gzip_sendfile1(
297 request_rec *r,
298 char        *input_filename,
299 FILE        *ifh_passed,
300 long         starting_offset
301 );
302
303 int mod_gzip_sendfile2(
304 request_rec   *r,
305 mod_gzip_conf *dconf,
306 char          *input_filename
307 );
308
309 int mod_gzip_dyn1_getfdo1(
310 request_rec *r,
311 char        *filename
312 );
313
314 int mod_gzip_redir1_handler(
315 request_rec   *r,
316 mod_gzip_conf *dconf
317 );
318
319 long mod_gzip_send(
320 char        *buf,
321 long         buflen,
322 request_rec *r
323 );
324
325 int mod_gzip_encode_and_transmit(
326 request_rec   *r,
327 mod_gzip_conf *dconf,
328 char          *source,
329 int            source_is_a_file,
330 long           input_size,
331 int            nodecline,
332 long           header_length,
333 char          *result_prefix_string
334 );
335
336 typedef struct _GZP_CONTROL {
337
338     int   decompress;
339
340     int   input_ismem;          
341     char *input_ismem_ibuf;     
342     long  input_ismem_ibuflen;  
343
344     char  input_filename[ MOD_GZIP_MAX_PATH_LEN + 2 ]; 
345
346     long  input_offset;         
347
348     int   output_ismem;         
349     char *output_ismem_obuf;    
350     long  output_ismem_obuflen; 
351
352     char  output_filename[ MOD_GZIP_MAX_PATH_LEN + 2 ]; 
353
354     int   result_code; 
355     long  bytes_out;   
356
357 } GZP_CONTROL;
358
359 int gzp_main( request_rec *, GZP_CONTROL *gzp ); 
360
361 char mod_gzip_check_permissions[] =
362 "Make sure all named directories exist and have the correct permissions.";
363
364 #ifdef MOD_GZIP_DEBUG1
365
366 server_rec *mod_gzip_server_now = 0;
367
368 const char *npp( const char *s )
369 {
370  /* NOTE: This 'Null Pointer Protection' call is really only */
371  /* needed for the Solaris Operating System which seems to have */
372  /* some kind of problem handling NULL pointers passed to certain */
373  /* STDLIB calls. Sloaris will GP fault where all other OS's are OK. */
374
375  if ( s ) return s;
376  else     return "NULL";
377 }
378
379 #ifdef MOD_GZIP_LOG_IS_APACHE_LOG
380
381 void mod_gzip_printf( const char *fmt, ... )
382 {
383  int   l;
384
385  va_list ap;
386
387  char log_line[2048]; 
388
389  va_start( ap, fmt );
390
391  l = vsprintf( log_line, fmt, ap );
392
393  va_end(ap);
394
395  ap_log_error( "",0,APLOG_NOERRNO|APLOG_DEBUG, mod_gzip_server_now, log_line);
396
397  return;
398 }
399
400 #else 
401
402 void mod_gzip_printf( const char *fmt, ... )
403 {
404  int   l;
405  char *p1;
406  FILE *log;
407
408  va_list ap;
409
410  char logname[256];
411  char log_line[4096];
412
413  #ifdef WIN32
414  long pid = GetCurrentProcessId();
415  #else
416  long pid = (long) getpid();
417  #endif
418
419  #ifdef WIN32
420  sprintf( logname, "c:\\temp\\t%ld.log",(long)pid);
421  #else
422  sprintf( logname, "/tmp/t%ld.log",(long)pid);
423  #endif
424
425  log = fopen( logname,"a" );
426
427  if ( !log ) 
428    {
429     return; 
430    }
431
432  va_start( ap, fmt );
433
434  l = vsprintf(log_line, fmt, ap);
435
436  p1=log_line;
437  while((*p1!=0)&&(*p1!=13)&&(*p1!=10)) p1++;
438  *p1=0;
439
440  fprintf( log, "%s\n", log_line );
441
442  fclose( log );
443
444  va_end(ap); 
445
446  return; 
447 }
448
449 #endif 
450
451 void mod_gzip_hexdump( char *buffer, int buflen )
452 {
453  int i,o1,o2,o3;
454
455  int len1;
456  int len2;
457
458  char ch1;
459  char ch2;
460  char s[40];
461  char l1[129];
462  char l2[129];
463  char l3[300];
464
465  long offset1=0L;
466
467  o1=0;
468  o2=0;
469  o3=0;
470
471  l1[0] = 0;
472  l2[0] = 0;
473  l3[0] = 0;
474
475  offset1 = 0;
476
477  for ( i=0; i<buflen; i++ )
478     {
479      ch1 = (char) *buffer++;
480
481      #define DUMPIT_ASTERISK    42
482      #define DUMPIT_LAPOSTROPHE 96
483      #define DUMPIT_RAPOSTROPHE 39
484      #define DUMPIT_PERIOD      46
485      #define DUMPIT_CR          67
486      #define DUMPIT_LF          76
487
488      #ifdef MASK_ONLY_CERTAIN_CHARS
489           if ( ch1 ==  0 ) ch2 = DUMPIT_PERIOD;
490      else if ( ch1 == 13 ) ch2 = DUMPIT_CR;
491      else if ( ch1 == 10 ) ch2 = DUMPIT_LF;
492      else if ( ch1 ==  9 ) ch2 = DUMPIT_LAPOSTROPHE;
493      else                  ch2 = ch1;
494      #endif
495
496      #define MASK_ALL_NON_PRINTABLE_CHARS
497      #ifdef  MASK_ALL_NON_PRINTABLE_CHARS
498
499           if ( ch1 == 13 ) ch2 = DUMPIT_CR;
500      else if ( ch1 == 10 ) ch2 = DUMPIT_LF;
501      else if ( ch1 <  32 ) ch2 = DUMPIT_PERIOD;
502      else if ( ch1 >  126) ch2 = DUMPIT_LAPOSTROPHE;
503      else if ( ch1 == 37 ) ch2 = DUMPIT_ASTERISK; 
504      else if ( ch1 == 92 ) ch2 = DUMPIT_ASTERISK; 
505      else                  ch2 = ch1;
506
507      #endif
508
509      l2[o2++] = ch2;
510
511      sprintf( s, "%02X", ch1 );
512
513      if ( mod_gzip_strlen(s) > 2 ) s[2]=0; 
514
515      len1 = mod_gzip_strlen(s);
516      len2 = mod_gzip_strlen(l1);
517
518      if ( mod_gzip_strlen(l1) < (int)(sizeof(l1) - (len1+1)) )
519        {
520         strcat( l1, s   );
521         strcat( l1, " " );
522        }
523
524      if ( o2 >= 16 )
525        {
526         l2[o2]=0;
527
528         mod_gzip_printf( "%6lu| %-49.49s| %-16.16s |", offset1, l1, l2 );
529
530         offset1 += o2;
531
532         o1=0;
533         o2=0;
534         o3=0;
535
536         l1[0] = 0;
537         l2[0] = 0;
538         l3[0] = 0;
539        }
540     }
541
542  if ( o2 > 0  )
543    {
544     l2[o2]=0;
545
546     mod_gzip_printf( "%6lu| %-49.49s| %-16.16s |", offset1, l1, l2 );
547
548     offset1 += o2;
549
550     o1 = o2 = o3 = 0;
551
552     l1[0] = 0;
553     l2[0] = 0;
554     l3[0] = 0;
555    }
556 }
557
558 int mod_gzip_log_comerror( request_rec *r, char *p, int error )
559 {
560  int  i=0;      
561  char b[3][90]; 
562
563  b[0][0]=0; 
564  b[1][0]=0; 
565  b[2][0]=0; 
566
567  #if defined(WIN32) || defined(NETWARE)
568
569  if ( error == WSANOTINITIALISED )
570    {
571     sprintf(b[0],"%s * WSANOTINITIALISED",p);
572     sprintf(b[1],"%s * A successful WSAStartup() must occur",p);
573     sprintf(b[2],"%s * before using this WINSOCK API call.",p);
574    }
575  else if ( error == WSAENETDOWN )
576    {
577     sprintf(b[0],"%s * WSAENETDOWN",p);
578     sprintf(b[1],"%s * The Windows Sockets implementation has detected",p);
579     sprintf(b[2],"%s * that the network subsystem has failed.",p);
580    }
581  else if ( error == WSAENOTCONN )
582    {
583     sprintf(b[0],"%s * WSAENOTCONN",p);
584     sprintf(b[1],"%s * The socket is not connected.",p);
585    }
586  else if ( error == WSAEINTR )
587    {
588     sprintf(b[0],"%s * WSAEINTR",p);
589     sprintf(b[1],"%s * The (blocking) call was cancelled",p);
590     sprintf(b[2],"%s * via WSACancelBlockingCall()",p);
591    }
592  else if ( error == WSAEINPROGRESS )
593    {
594     sprintf(b[0],"%s * WSAEINPROGRESS",p);
595     sprintf(b[1],"%s * A blocking Windows Sockets operation",p);
596     sprintf(b[2],"%s * is in progress.",p);
597    }
598  else if ( error == WSAENOTSOCK )
599    {
600     sprintf(b[0],"%s * WSAENOTSOCK",p);
601     sprintf(b[1],"%s * The descriptor is not a socket.",p);
602    }
603  else if ( error == WSAEOPNOTSUPP )
604    {
605     sprintf(b[0],"%s * WSAEOPNOTSUPP",p);
606     sprintf(b[1],"%s * MSG_OOB was specified, but the socket is",p);
607     sprintf(b[2],"%s * not of type SOCK_STREAM.",p);
608    }
609  else if ( error == WSAESHUTDOWN )
610    {
611     sprintf(b[0],"%s * WSAESHUTDOWN",p);
612     sprintf(b[1],"%s * The socket has been shutdown.",p);
613    }
614  else if ( error == WSAEWOULDBLOCK )
615    {
616     sprintf(b[0],"%s * WSAEWOULDBLOCK",p);
617     sprintf(b[1],"%s * The socket is marked as non-blocking",p);
618     sprintf(b[2],"%s * and receive operation would block.",p);
619    }
620  else if ( error == WSAEMSGSIZE )
621    {
622     sprintf(b[0],"%s * WSAEMSGSIZE",p);
623     sprintf(b[1],"%s * The datagram was too large to",p);
624     sprintf(b[2],"%s * fit into the specified buffer.",p);
625    }
626  else if ( error == WSAEINVAL )
627    {
628     sprintf(b[0],"%s * WSAEINVAL",p);
629     sprintf(b[1],"%s * The socket has not been bound with bind().",p);
630    }
631  else if ( error == WSAECONNABORTED )
632    {
633     sprintf(b[0],"%s * WSAECONNABORTED",p);
634     sprintf(b[1],"%s * The virtual circuit was aborted",p);
635     sprintf(b[2],"%s * due to timeout or other failure.",p);
636    }
637  else if ( error == WSAECONNRESET )
638    {
639     sprintf(b[0],"%s * WSAECONNRESET",p);
640     sprintf(b[1],"%s * The virtual circuit was reset by the remote side.",p);
641    }
642  else
643    {
644     sprintf(b[0],"%s * WSA????",p);
645     sprintf(b[1],"%s * Unexpected WINSOCK error code %d",p,error);
646    }
647
648  #else 
649
650       if ( error == EBADF       ) sprintf(b[0],"%s * EBADF", p );
651  else if ( error == EAGAIN      ) sprintf(b[0],"%s * EAGAIN",p );
652  else if ( error == EDQUOT      ) sprintf(b[0],"%s * EDQUOT",p );
653  else if ( error == EFAULT      ) sprintf(b[0],"%s * EFAULT",p );
654  else if ( error == EFBIG       ) sprintf(b[0],"%s * EFBIG", p );
655  else if ( error == EINTR       ) sprintf(b[0],"%s * EINTR", p );
656  else if ( error == EINVAL      ) sprintf(b[0],"%s * EINVAL",p );
657  else if ( error == EIO         ) sprintf(b[0],"%s * EIO",   p );
658  else if ( error == ENOSPC      ) sprintf(b[0],"%s * ENOSPC",p );
659  else if ( error == ENXIO       ) sprintf(b[0],"%s * ENXIO", p );
660  else if ( error == EPIPE       ) sprintf(b[0],"%s * EPIPE", p );
661  else if ( error == ERANGE      ) sprintf(b[0],"%s * ERANGE",p );
662  else if ( error == EINVAL      ) sprintf(b[0],"%s * EINVAL",p );
663  else if ( error == EWOULDBLOCK ) sprintf(b[0],"%s * EWOULDBLOCK",p );
664
665  else 
666    {
667     sprintf(b[0],"%s * E???? Unexpected error code %d",p,error);
668    }
669
670  #endif 
671
672  for ( i=0; i<3; i++ )
673     {
674      if ( b[i][0] != 0 )
675        {
676         #ifdef MOD_GZIP_DEBUG1
677         mod_gzip_printf("%s",npp(b[i]));
678         #endif
679        }
680     }
681
682  return( 1 );
683 }
684
685 int mod_gzip_translate_comerror( int error, char *buf )
686 {
687  char bb[40]; 
688
689  if ( buf == 0 ) 
690    {
691     return 0; 
692    }
693
694  #if defined(WIN32) || defined(NETWARE)
695
696       if ( error == WSANOTINITIALISED ) sprintf(bb,"WSANOTINITIALISED");
697  else if ( error == WSAENETDOWN       ) sprintf(bb,"WSAENETDOWN");
698  else if ( error == WSAENOTCONN       ) sprintf(bb,"WSAENOTCONN");
699  else if ( error == WSAEINTR          ) sprintf(bb,"WSAEINTR");
700  else if ( error == WSAEINPROGRESS    ) sprintf(bb,"WSAEINPROGRESS");
701  else if ( error == WSAENOTSOCK       ) sprintf(bb,"WSAENOTSOCK");
702  else if ( error == WSAEOPNOTSUPP     ) sprintf(bb,"WSAEOPNOTSUPP");
703  else if ( error == WSAESHUTDOWN      ) sprintf(bb,"WSAESHUTDOWN");
704  else if ( error == WSAEWOULDBLOCK    ) sprintf(bb,"WSAEWOULDBLOCK");
705  else if ( error == WSAEMSGSIZE       ) sprintf(bb,"WSAEMSGSIZE");
706  else if ( error == WSAEINVAL         ) sprintf(bb,"WSAEINVAL");
707  else if ( error == WSAECONNABORTED   ) sprintf(bb,"WSAECONNABORTED");
708  else if ( error == WSAECONNRESET     ) sprintf(bb,"WSAECONNRESET");
709  else                                   sprintf(bb,"%d=WSA????",error);
710
711  #else 
712
713       if ( error == EBADF       ) sprintf(bb,"EBADF"  );
714  else if ( error == EAGAIN      ) sprintf(bb,"EAGAIN" );
715  else if ( error == EDQUOT      ) sprintf(bb,"EDQUOT" );
716  else if ( error == EFAULT      ) sprintf(bb,"EFAULT" );
717  else if ( error == EFBIG       ) sprintf(bb,"EFBIG"  );
718  else if ( error == EINTR       ) sprintf(bb,"EINTR"  );
719  else if ( error == EINVAL      ) sprintf(bb,"EINVAL" );
720  else if ( error == EIO         ) sprintf(bb,"EIO"    );
721  else if ( error == ENOSPC      ) sprintf(bb,"ENOSPC" );
722  else if ( error == ENXIO       ) sprintf(bb,"ENXIO"  );
723  else if ( error == EPIPE       ) sprintf(bb,"EPIPE"  );
724  else if ( error == ERANGE      ) sprintf(bb,"ERANGE" );
725  else if ( error == EINVAL      ) sprintf(bb,"EINVAL" );
726  else if ( error == EWOULDBLOCK ) sprintf(bb,"EWOULDBLOCK" );
727  else                             sprintf(bb,"%d=E????",error);
728
729  #endif 
730
731  mod_gzip_strcpy( buf, bb ); 
732
733  return( 1 );
734 }
735
736 #endif 
737
738 int mod_gzip_validate1(
739 request_rec   *r,
740 mod_gzip_conf *mgc,
741 char          *r__filename,
742 char          *r__uri,
743 char          *r__content_type,
744 char          *r__handler,
745 char          *fieldkey,
746 char          *fieldstring,
747 int            direction
748 )
749 {
750  int   x                = 0;
751  int   clen             = 0;
752  int   hlen             = 0;
753  int   flen             = 0;
754  int   ulen             = 0;
755  int   pass             = 0;
756  int   passes           = 2;
757  char *this_name        = 0;
758  int   this_type        = 0;
759  int   this_len1        = 0;
760  int   this_action      = 0;
761  int   this_include     = 0;
762  char *checktarget      = 0;
763  int   pass_result      = 0;
764  int   action_value     = 0;
765  int   filter_value     = 0;
766  int   type_to_match    = 0;
767  int   ok_to_check_it   = 0;
768  int   http_field_check = 0;
769  int   item_is_included = 0;
770  int   item_is_excluded = 0;
771  int   type_is_included = 0;
772
773  regex_t *this_pregex   = NULL;
774  int      regex_error   = 0;
775
776  #ifdef MOD_GZIP_DEBUG1
777  char cn[]="mod_gzip_validate1()";
778  #endif
779
780  if ( r__filename     ) flen = mod_gzip_strlen( (char *) r__filename );
781  if ( r__uri          ) ulen = mod_gzip_strlen( (char *) r__uri );
782  if ( r__content_type ) clen = mod_gzip_strlen( (char *) r__content_type );
783  if ( r__handler      ) hlen = mod_gzip_strlen( (char *) r__handler );
784
785  #ifdef MOD_GZIP_DEBUG1
786
787  mod_gzip_printf( "%s: Entry...",cn);
788  mod_gzip_printf( "%s: r__filename     = [%s]",cn,npp(r__filename));
789  mod_gzip_printf( "%s: flen            = %d",  cn,flen);
790  mod_gzip_printf( "%s: r__uri          = [%s]",cn,npp(r__uri));
791  mod_gzip_printf( "%s: ulen            = %d",  cn,ulen);
792  mod_gzip_printf( "%s: r__content_type = [%s]",cn,npp(r__content_type));
793  mod_gzip_printf( "%s: clen            = %d",  cn,clen);
794  mod_gzip_printf( "%s: r__handler      = [%s]",cn,npp(r__handler));
795  mod_gzip_printf( "%s: hlen            = %d",  cn,hlen);
796  mod_gzip_printf( "%s: fieldkey        = [%s]",cn,npp(fieldkey));
797  mod_gzip_printf( "%s: fieldstring     = [%s]",cn,npp(fieldstring));
798  mod_gzip_printf( "%s: direction       = %d",  cn,direction);
799
800  #endif
801
802  if ( ( fieldkey ) && ( fieldstring ) )
803    {
804     http_field_check = 1;
805
806     passes = 1;
807
808     if ( direction == MOD_GZIP_REQUEST )
809       {
810        type_to_match = MOD_GZIP_IMAP_ISREQHEADER;
811       }
812     else if ( direction == MOD_GZIP_RESPONSE )
813       {
814        type_to_match = MOD_GZIP_IMAP_ISRSPHEADER;
815       }
816     else
817       {
818        #ifdef MOD_GZIP_DEBUG1
819        mod_gzip_printf( "%s: Invalid 'direction' value.",cn);
820        mod_gzip_printf( "%s: Must be MOD_GZIP_REQUEST or MOD_GZIP_RESPONSE",cn);
821        mod_gzip_printf( "%s: Exit > return( MOD_GZIP_IMAP_DECLINED1 ) >",cn);
822        #endif
823
824        return( MOD_GZIP_IMAP_DECLINED1 );
825       }
826    }
827
828  else if ( ( hlen == 0 ) && ( clen == 0 ) && ( flen == 0 ) )
829    {
830     #ifdef MOD_GZIP_DEBUG1
831     mod_gzip_printf( "%s: hlen = 0 = No handler name passed...",cn);
832     mod_gzip_printf( "%s: clen = 0 = No valid content-type passed",cn);
833     mod_gzip_printf( "%s: flen = 0 = No valid filename passed",cn);
834     mod_gzip_printf( "%s: There is nothing we can use to search",cn);
835     mod_gzip_printf( "%s: for a match in the inclusion/exclusion list.",cn);
836     mod_gzip_printf( "%s: Exit > return( MOD_GZIP_IMAP_DECLINED1 ) >",cn);
837     #endif
838
839     return( MOD_GZIP_IMAP_DECLINED1 );
840    }
841
842  #ifdef MOD_GZIP_DEBUG1
843  mod_gzip_printf( "%s: passes = %d", cn,
844                  (int) passes );
845  mod_gzip_printf( "%s: http_field_check = %d", cn,
846                  (int) http_field_check );
847  mod_gzip_printf( "%s: mgc->imap_total_entries = %d", cn,
848                  (int) mgc->imap_total_entries );
849  #endif
850
851  for ( pass=0; pass<passes; pass++ )
852     {
853      pass_result = 0;
854
855      #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
856      #ifdef MOD_GZIP_DEBUG1
857      mod_gzip_printf( "%s: ",cn);
858      #endif
859      #endif
860
861      filter_value = pass;
862
863      #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
864      #ifdef MOD_GZIP_DEBUG1
865      mod_gzip_printf( "%s: pass         = %d", cn, pass );
866      mod_gzip_printf( "%s: filter_value = %d", cn, filter_value );
867      mod_gzip_printf( "%s: mgc->imap_total_entries = %d", cn,
868                      (int) mgc->imap_total_entries );
869      #endif
870      #endif
871
872      for ( x=0; x < mgc->imap_total_entries; x++ )
873         {
874          this_include = mgc->imap[x].include;
875          this_type    = mgc->imap[x].type;
876          this_action  = mgc->imap[x].action;
877
878          #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
879          #ifdef MOD_GZIP_DEBUG1
880
881          mod_gzip_printf( "%s: --------------------------------------------",cn);
882          mod_gzip_printf( "%s: http_field_check = %d",  cn,http_field_check );
883
884          if ( http_field_check )
885            {
886             mod_gzip_printf( "%s: fieldkey         = [%s]",cn,npp(fieldkey));
887             mod_gzip_printf( "%s: fieldstring      = [%s]",cn,npp(fieldstring));
888            }
889          else
890            {
891             mod_gzip_printf( "%s: r__filename      = [%s]",cn,npp(r__filename));
892             mod_gzip_printf( "%s: r__uri           = [%s]",cn,npp(r__uri));
893             mod_gzip_printf( "%s: r__content_type  = [%s]",cn,npp(r__content_type));
894             mod_gzip_printf( "%s: r__handler       = [%s]",cn,npp(r__handler));
895            }
896
897          if ( this_include == 0 )
898            {
899             mod_gzip_printf( "%s: mgc->imap[%3.3d].include = %d EXCLUDE",cn,x,this_include);
900            }
901          else if ( this_include == 1 )
902            {
903             mod_gzip_printf( "%s: mgc->imap[%3.3d].include = %d INCLUDE",cn,x,this_include);
904            }
905          else
906            {
907             mod_gzip_printf( "%s: mgc->imap[%3.3d].include = %d ??? UNKNOWN VALUE",cn,x,this_include);
908            }
909
910          if ( mgc->imap[x].type == MOD_GZIP_IMAP_ISMIME )
911            {
912             mod_gzip_printf( "%s: mgc->imap[%3.3d].type    = %d MOD_GZIP_IMAP_ISMIME",
913                               cn,x,this_type);
914            }
915          else if ( mgc->imap[x].type == MOD_GZIP_IMAP_ISHANDLER )
916            {
917             mod_gzip_printf( "%s: mgc->imap[%3.3d].type    = %d MOD_GZIP_IMAP_ISHANDLER",
918                               cn,x,this_type);
919            }
920          else if ( mgc->imap[x].type == MOD_GZIP_IMAP_ISFILE )
921            {
922             mod_gzip_printf( "%s: mgc->imap[%3.3d].type    = %d MOD_GZIP_IMAP_ISFILE",
923                               cn,x,this_type);
924            }
925          else if ( mgc->imap[x].type == MOD_GZIP_IMAP_ISURI )
926            {
927             mod_gzip_printf( "%s: mgc->imap[%3.3d].type    = %d MOD_GZIP_IMAP_ISURI",
928                               cn,x,this_type);
929            }
930          else if ( mgc->imap[x].type == MOD_GZIP_IMAP_ISREQHEADER )
931            {
932             mod_gzip_printf( "%s: mgc->imap[%3.3d].type    = %d MOD_GZIP_IMAP_ISREQHEADER",
933                               cn,x,this_type);
934            }
935          else if ( mgc->imap[x].type == MOD_GZIP_IMAP_ISRSPHEADER )
936            {
937             mod_gzip_printf( "%s: mgc->imap[%3.3d].type    = %d MOD_GZIP_IMAP_ISRSPHEADER",
938                               cn,x,this_type);
939            }
940          else
941            {
942             mod_gzip_printf( "%s: mgc->imap[%3.3d].type    = %d MOD_GZIP_IMAP_IS??? Unknown type",
943                               cn,x,this_type);
944            }
945
946          if ( mgc->imap[x].action == MOD_GZIP_IMAP_STATIC1 )
947            {
948             mod_gzip_printf( "%s: mgc->imap[%3.3d].action  = %d MOD_GZIP_IMAP_STATIC1",
949                               cn,x,this_action);
950            }
951          else if ( mgc->imap[x].action == MOD_GZIP_IMAP_DYNAMIC1 )
952            {
953             mod_gzip_printf( "%s: mgc->imap[%3.3d].action  = %d MOD_GZIP_IMAP_DYNAMIC1",
954                               cn,x,this_action);
955            }
956          else if ( mgc->imap[x].action == MOD_GZIP_IMAP_DYNAMIC2 )
957            {
958             mod_gzip_printf( "%s: mgc->imap[%3.3d].action  = %d MOD_GZIP_IMAP_DYNAMIC2",
959                               cn,x,this_action);
960            }
961          else
962            {
963             mod_gzip_printf( "%s: mgc->imap[%3.3d].action  = %d MOD_GZIP_IMAP_??? Unknown action",
964                               cn,x,this_action);
965            }
966
967          mod_gzip_printf( "%s: mgc->imap[%3.3d].name    = [%s]",cn,x,npp(mgc->imap[x].name));
968          mod_gzip_printf( "%s: mgc->imap[%3.3d].namelen = %d",  cn,x,mgc->imap[x].namelen);
969
970          #endif
971          #endif
972
973          if ( this_include == filter_value )
974            {
975             #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
976             #ifdef MOD_GZIP_DEBUG1
977             mod_gzip_printf( "%s: This record matches filter_value %d",
978                               cn, filter_value );
979             mod_gzip_printf( "%s: The record will be checked...",cn);
980             #endif
981             #endif
982
983             type_is_included = 0;
984             checktarget      = 0;
985
986             if ( http_field_check )
987               {
988                if ( this_type == type_to_match )
989                  {
990                   type_is_included = 1;
991
992                   checktarget = (char *) fieldstring;
993                  }
994               }
995             else
996               {
997                if ( ( this_type == MOD_GZIP_IMAP_ISMIME ) &&
998                     ( clen > 0 ) )
999                  {
1000                   type_is_included = 1;
1001
1002                   checktarget = r__content_type;
1003                  }
1004                else if ( ( this_type == MOD_GZIP_IMAP_ISFILE ) &&
1005                          ( flen > 0 ) )
1006                  {
1007                   type_is_included = 1;
1008
1009                   checktarget = r__filename;
1010                  }
1011                else if ( ( this_type == MOD_GZIP_IMAP_ISURI ) &&
1012                          ( ulen > 0 ) )
1013                  {
1014                   type_is_included = 1;
1015
1016                   checktarget = r__uri;
1017                  }
1018                else if ( ( this_type == MOD_GZIP_IMAP_ISHANDLER ) &&
1019                          ( hlen > 0 ) )
1020                  {
1021                   type_is_included = 1;
1022
1023                   checktarget = r__handler;
1024                  }
1025               }
1026
1027             if ( type_is_included )
1028               {
1029                #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1030                #ifdef MOD_GZIP_DEBUG1
1031                mod_gzip_printf( "%s: type_is_included = %d = YES",cn,type_is_included);
1032                #endif
1033                #endif
1034
1035                this_name   = mgc->imap[x].name;
1036                this_len1   = mgc->imap[x].len1;
1037                this_pregex = mgc->imap[x].pregex;
1038
1039                ok_to_check_it = 1;
1040
1041                if ( http_field_check )
1042                  {
1043                   #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1044                   #ifdef MOD_GZIP_DEBUG1
1045                   mod_gzip_printf( "%s: fieldkey  = [%s]",cn,npp(fieldkey));
1046                   mod_gzip_printf( "%s: this_name = [%s]",cn,npp(this_name));
1047                   mod_gzip_printf( "%s: this_len1 = %d",  cn,this_len1);
1048                   mod_gzip_printf( "%s: Call mod_gzip_strnicmp(fieldkey,this_name,this_len1)...",cn);
1049                   #endif
1050                   #endif
1051
1052                   if ( mod_gzip_strnicmp( fieldkey, this_name, this_len1 )==0)
1053                     {
1054                      #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1055                      #ifdef MOD_GZIP_DEBUG1
1056                      mod_gzip_printf( "%s: .... mod_gzip_strnicmp() = TRUE",cn);
1057                      mod_gzip_printf( "%s: .... Field key name MATCHES",cn);
1058                      #endif
1059                      #endif
1060                     }
1061                   else
1062                     {
1063                      #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1064                      #ifdef MOD_GZIP_DEBUG1
1065                      mod_gzip_printf( "%s: .... mod_gzip_strnicmp() = FALSE",cn);
1066                      mod_gzip_printf( "%s: .... Field key name does NOT MATCH",cn);
1067                      #endif
1068                      #endif
1069
1070                      ok_to_check_it = 0;
1071                     }
1072                  }
1073
1074                if ( ok_to_check_it )
1075                  {
1076                   #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1077                   #ifdef MOD_GZIP_DEBUG1
1078                   mod_gzip_printf( "%s: ok_to_check_it = %d = YES",cn,ok_to_check_it);
1079                   #endif
1080                   #endif
1081
1082                   if ( ( this_pregex ) && ( checktarget ) )
1083                     {
1084                      #ifdef MOD_GZIP_DEBUG1
1085                      mod_gzip_printf( "%s: 'this_pregex' is NON-NULL",cn);
1086                      mod_gzip_printf( "%s: Performing regular expression check...",cn);
1087                      mod_gzip_printf( "%s: Call ap_regexec( this_name=[%s], checktarget=[%s] )",
1088                                        cn, npp(this_name), npp(checktarget) );
1089                      #endif
1090
1091                      regex_error =
1092                      ap_regexec(
1093                      this_pregex, checktarget, 0, (regmatch_t *) NULL, 0 );
1094
1095                      if ( regex_error == 0 )
1096                        {
1097                         #ifdef MOD_GZIP_DEBUG1
1098                         mod_gzip_printf( "%s: YYYY regex_error = %d = MATCH!",cn,regex_error);
1099                         #endif
1100
1101                         pass_result  = 1;
1102                         action_value = this_action;
1103                         break;
1104                        }
1105                      else
1106                        {
1107                         #ifdef MOD_GZIP_DEBUG1
1108                         mod_gzip_printf( "%s: NNNN regex_error = %d = NO MATCH!",cn,regex_error);
1109                         #endif
1110                        }
1111                     }
1112                   else
1113                     {
1114                      #ifdef MOD_GZIP_DEBUG1
1115
1116                      if ( !this_pregex )
1117                        {
1118                         mod_gzip_printf( "%s: 'this_pregex' is NULL",cn);
1119                        }
1120                      if ( !checktarget )
1121                        {
1122                         mod_gzip_printf( "%s: 'checktarget' is NULL",cn);
1123                        }
1124
1125                      mod_gzip_printf( "%s: No regular expression check performed",cn);
1126
1127                      #endif
1128                     }
1129                  }
1130                else
1131                  {
1132                   #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1133                   #ifdef MOD_GZIP_DEBUG1
1134                   mod_gzip_printf( "%s: ok_to_check_it = %d = NO",cn,ok_to_check_it);
1135                   mod_gzip_printf( "%s: The record has been SKIPPED...",cn);
1136                   #endif
1137                   #endif
1138                  }
1139               }
1140             else
1141               {
1142                #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1143                #ifdef MOD_GZIP_DEBUG1
1144                mod_gzip_printf( "%s: type_is_included = %d = NO",cn,type_is_included);
1145                mod_gzip_printf( "%s: The record has been SKIPPED...",cn);
1146                #endif
1147                #endif
1148               }
1149            }
1150          else
1151            {
1152             #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1153             #ifdef MOD_GZIP_DEBUG1
1154             mod_gzip_printf( "%s: This record does NOT match filter_value %d",
1155                               cn, filter_value );
1156             mod_gzip_printf( "%s: The record has been SKIPPED...",cn);
1157             #endif
1158             #endif
1159            }
1160         }
1161
1162      #ifdef MOD_GZIP_DEBUG1_VALIDATE1_VERBOSE1
1163      #ifdef MOD_GZIP_DEBUG1
1164      mod_gzip_printf( "%s: --------------------------------------------",cn);
1165      mod_gzip_printf( "%s: pass_result = %d",cn,pass_result);
1166      #endif
1167      #endif
1168
1169      if ( pass_result )
1170        {
1171         if ( pass == 0 ) item_is_excluded = 1;
1172         else             item_is_included = 1;
1173
1174         break;
1175        }
1176
1177     }/* End 'for ( pass=0; pass<passes; pass++ )' */
1178        
1179  #ifdef MOD_GZIP_DEBUG1
1180  mod_gzip_printf( "%s: item_is_excluded = %d",cn,item_is_excluded);
1181  mod_gzip_printf( "%s: item_is_included = %d",cn,item_is_included);
1182  #endif
1183
1184  if ( item_is_excluded )
1185    {
1186     #ifdef MOD_GZIP_DEBUG1
1187     mod_gzip_printf( "%s: The item is EXCLUDED...",cn);
1188     mod_gzip_printf( "%s: Exit > return( MOD_GZIP_IMAP_DECLINED1 ) >",cn);
1189     #endif
1190
1191     return( MOD_GZIP_IMAP_DECLINED1 );
1192    }
1193
1194  else if ( item_is_included )
1195    {
1196     #ifdef MOD_GZIP_DEBUG1
1197     mod_gzip_printf( "%s: The item is INCLUDED...",cn);
1198     mod_gzip_printf( "%s: Exit > return( 1 ) >",cn);
1199     #endif
1200
1201     return action_value;
1202    }
1203
1204  if ( http_field_check )
1205    {
1206     #ifdef MOD_GZIP_DEBUG1
1207     mod_gzip_printf( "%s: ?? Status unknown ?? Default is to 'accept'...",cn);
1208     mod_gzip_printf( "%s: Exit > return( MOD_GZIP_IMAP_STATIC1 ) >",cn);
1209     #endif
1210
1211     return MOD_GZIP_IMAP_STATIC1;
1212    }
1213
1214  #ifdef MOD_GZIP_DEBUG1
1215  mod_gzip_printf( "%s: Exit > return( MOD_GZIP_IMAP_DECLINED1 ) >",cn);
1216  #endif
1217
1218  return( MOD_GZIP_IMAP_DECLINED1 );
1219 }
1220
1221
1222 /* NOTE: If API_VAR_EXPORT prefix is not used for 'top_module' */
1223 /* declaration then MSVC 6.0 will give 'incosistent DLL linkage' */
1224 /* warning during WIN32 compile... */
1225
1226 extern API_VAR_EXPORT module *top_module;
1227
1228 struct _table {
1229     array_header a;
1230 #ifdef MAKE_TABLE_PROFILE
1231     void *creator;
1232 #endif
1233 };
1234 typedef struct _table _table;
1235
1236 #ifdef MOD_GZIP_DEBUG1
1237
1238 int mod_gzip_dump_a_table( request_rec *r, _table *t )
1239 {
1240  table_entry *elts = (table_entry *) t->a.elts;
1241  int i;
1242
1243  char cn[]="mod_gzip_dump_a_table()";
1244
1245  for ( i = 0; i < t->a.nelts; i++ )
1246     {
1247      mod_gzip_printf( "%s: %3.3d key=[%s] val=[%s]",
1248      cn,i,npp(elts[i].key),npp(elts[i].val));
1249     }
1250
1251  return 0;
1252 }
1253
1254 #endif 
1255
1256 #define MOD_GZIP_RUN_TYPE_CHECKERS      1
1257 #define MOD_GZIP_RUN_TRANSLATE_HANDLERS 2
1258
1259 int mod_gzip_run_handlers( request_rec *r, int flag1 )
1260 {
1261  int rc;
1262  int count=0;
1263  int runit=0;
1264  int handler_is_present=0;
1265
1266  module *modp;
1267
1268  #ifdef MOD_GZIP_DEBUG1
1269  #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1270  const handler_rec *handp;
1271  char cn[]="mod_gzip_run_handlers()";
1272  #endif
1273  #endif
1274
1275  #ifdef MOD_GZIP_DEBUG1
1276  #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1277  mod_gzip_server_now = r->server;
1278  mod_gzip_printf( "%s: Entry...",cn);
1279  mod_gzip_printf( "%s: *IN: r->uri          =[%s]", cn, npp(r->uri));
1280  mod_gzip_printf( "%s: *IN: r->unparsed_uri =[%s]", cn, npp(r->unparsed_uri));
1281  mod_gzip_printf( "%s: *IN: r->filename     =[%s]", cn, npp(r->filename));
1282  mod_gzip_printf( "%s: *IN: r->handler      =[%s]", cn, npp(r->handler));
1283  mod_gzip_printf( "%s: *IN: r->content_type =[%s]", cn, npp(r->content_type));
1284  #endif
1285  #endif
1286
1287  if ( flag1 == MOD_GZIP_RUN_TYPE_CHECKERS )
1288    {
1289     #ifdef MOD_GZIP_DEBUG1
1290     #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1291     mod_gzip_printf( "%s: flag1 = %d = MOD_GZIP_RUN_TYPE_CHECKERS",cn,flag1);
1292     #endif
1293     #endif
1294    }
1295  else if ( flag1 == MOD_GZIP_RUN_TRANSLATE_HANDLERS )
1296    {
1297     #ifdef MOD_GZIP_DEBUG1
1298     #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1299     mod_gzip_printf( "%s: flag1 = %d = MOD_GZIP_RUN_TRANSLATE_HANDLERS",cn,flag1);
1300     #endif
1301     #endif
1302    }
1303  else
1304    {
1305     #ifdef MOD_GZIP_DEBUG1
1306     #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1307     mod_gzip_printf( "%s: flag1 = %d = MOD_GZIP_RUN_??? Unknown value",cn,flag1);
1308     mod_gzip_printf( "%s: ERROR: Exit > return( DECLINED ) >",cn);
1309     #endif
1310     #endif
1311
1312     return( DECLINED );
1313    }
1314
1315  for ( modp = top_module; modp; modp = modp->next )
1316     {
1317      #ifdef MOD_GZIP_DEBUG1
1318      #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1319      mod_gzip_printf( "%s: count=%4.4d modp = %10.10ld modp->name=[%s]",
1320      cn,count,(long)modp,npp(modp->name));
1321      #endif
1322      #endif
1323
1324      runit = 0;
1325
1326      if ( modp )
1327        {
1328         runit = 1;
1329
1330         if ( modp == &gzip_module )
1331           {
1332            runit = 0;
1333           }
1334
1335         #ifdef FUTURE_USE
1336         if ( mod_gzip_strnicmp( (char *) modp->name, "mod_gzip.c",10)==0)
1337           {
1338            runit = 0;
1339           }
1340         #endif
1341        }
1342
1343      if ( runit )
1344        {
1345         #ifdef MOD_GZIP_DEBUG1
1346         #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1347
1348         mod_gzip_printf( "%s: ++++++++++ MODULE FOUND!...",cn);
1349         mod_gzip_printf( "%s: ++++++++++ modp->module_index = %d",cn,(int)modp->module_index);
1350
1351         #ifdef REFERENCE
1352
1353         typedef struct {
1354         const char *content_type;
1355         int (*handler) (request_rec *);
1356         } handler_rec;
1357
1358         typedef struct module_struct {
1359         ...
1360         const handler_rec *handlers;
1361         ...
1362         }module;
1363
1364         #endif
1365
1366         mod_gzip_printf( "%s: ++++++++++ METHODS",cn);
1367         mod_gzip_printf( "%s: ++++++++++ modp->translate_handler = %ld",cn,(long)modp->translate_handler);
1368         mod_gzip_printf( "%s: ++++++++++ modp->ap_check_user_id  = %ld",cn,(long)modp->ap_check_user_id);
1369         mod_gzip_printf( "%s: ++++++++++ modp->auth_checker      = %ld",cn,(long)modp->auth_checker);
1370         mod_gzip_printf( "%s: ++++++++++ modp->access_checker    = %ld",cn,(long)modp->access_checker);
1371         mod_gzip_printf( "%s: ++++++++++ modp->type_checker      = %ld",cn,(long)modp->type_checker);
1372         mod_gzip_printf( "%s: ++++++++++ modp->fixer_upper       = %ld",cn,(long)modp->fixer_upper);
1373         mod_gzip_printf( "%s: ++++++++++ modp->logger            = %ld",cn,(long)modp->logger);
1374         mod_gzip_printf( "%s: ++++++++++ modp->header_parser     = %ld",cn,(long)modp->header_parser);
1375         mod_gzip_printf( "%s: .......... CONTENT HANDLERS",cn);
1376
1377         if ( !modp->handlers )
1378           {
1379            mod_gzip_printf( "%s: .......... NO CONTENT HANDLERS!",cn);
1380           }
1381         else
1382           {
1383            for ( handp = modp->handlers; handp->content_type; ++handp )
1384               {
1385                mod_gzip_printf( "%s: .......... handp->content_type = [%s]",
1386                cn,npp(handp->content_type));
1387                mod_gzip_printf( "%s: .......... handp->handler      = %ld",
1388                cn,(long)handp->handler);
1389               }
1390           }
1391
1392         #endif
1393         #endif
1394
1395         handler_is_present = 0;
1396
1397         if ( flag1 == MOD_GZIP_RUN_TYPE_CHECKERS )
1398           {
1399            if ( modp->type_checker ) handler_is_present = 1;
1400           }
1401         else if ( flag1 == MOD_GZIP_RUN_TRANSLATE_HANDLERS )
1402           {
1403            if ( modp->translate_handler ) handler_is_present = 1;
1404           }
1405
1406         #ifdef MOD_GZIP_DEBUG1
1407         #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1408         mod_gzip_printf( "%s: handler_is_present = %d ",
1409                           cn, handler_is_present);
1410         #endif
1411         #endif
1412
1413         if ( handler_is_present )
1414           {
1415            #ifdef MOD_GZIP_DEBUG1
1416            #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1417
1418            mod_gzip_printf( "%s: 'handler_is_present' is TRUE...",cn);
1419
1420            mod_gzip_printf( "%s: r->filename     = [%s]",cn,npp(r->filename));
1421            mod_gzip_printf( "%s: r->uri          = [%s]",cn,npp(r->uri));
1422            mod_gzip_printf( "%s: r->handler      = [%s]",cn,npp(r->handler));
1423            mod_gzip_printf( "%s: r->content_type = [%s]",cn,npp(r->content_type));
1424
1425            if ( flag1 == MOD_GZIP_RUN_TYPE_CHECKERS )
1426              {
1427               mod_gzip_printf( "%s: Call (modp->type_checker)(r)...",cn);
1428              }
1429            else if ( flag1 == MOD_GZIP_RUN_TRANSLATE_HANDLERS )
1430              {
1431               mod_gzip_printf( "%s: Call (modp->translate_handler)(r)...",cn);
1432              }
1433
1434            #endif
1435            #endif
1436
1437            if ( flag1 == MOD_GZIP_RUN_TYPE_CHECKERS )
1438              {
1439               rc = (modp->type_checker)( (request_rec *) r );
1440              }
1441            else if ( flag1 == MOD_GZIP_RUN_TRANSLATE_HANDLERS )
1442              {
1443               rc = (modp->translate_handler)( (request_rec *) r );
1444              }
1445
1446            #ifdef MOD_GZIP_DEBUG1
1447            #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1448
1449            if ( flag1 == MOD_GZIP_RUN_TYPE_CHECKERS )
1450              {
1451               mod_gzip_printf( "%s: Back (modp->type_checker)(r)...",cn);
1452              }
1453            else if ( flag1 == MOD_GZIP_RUN_TRANSLATE_HANDLERS )
1454              {
1455               mod_gzip_printf( "%s: Back (modp->translate_handler)(r)...",cn);
1456              }
1457
1458            mod_gzip_printf( "%s: r->filename     = [%s]",cn,npp(r->filename));
1459            mod_gzip_printf( "%s: r->uri          = [%s]",cn,npp(r->uri));
1460            mod_gzip_printf( "%s: r->handler      = [%s]",cn,npp(r->handler));
1461            mod_gzip_printf( "%s: r->content_type = [%s]",cn,npp(r->content_type));
1462
1463            if ( rc == OK )
1464              {
1465               mod_gzip_printf( "%s: rc = %d = OK",cn, rc );
1466              }
1467            else if ( rc == DECLINED )
1468              {
1469               mod_gzip_printf( "%s: rc = %d = DECLINED",cn, rc );
1470              }
1471            else if ( rc == DONE )
1472              {
1473               mod_gzip_printf( "%s: rc = %d = DONE",cn, rc );
1474              }
1475            else
1476              {
1477               mod_gzip_printf( "%s: rc = %d = HTTP_ERROR?",cn, rc );
1478              }
1479
1480            #endif
1481            #endif
1482
1483            if ( rc == OK )
1484              {
1485               #ifdef MOD_GZIP_DEBUG1
1486               #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1487               mod_gzip_printf( "%s: Call SUCCEEDED",cn );
1488               mod_gzip_printf( "%s: Exit > return( rc=%d ) >",cn,rc);
1489               #endif
1490               #endif
1491
1492               return rc;
1493              }
1494            else
1495              {
1496               #ifdef MOD_GZIP_DEBUG1
1497               #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1498               mod_gzip_printf( "%s: Call FAILED",cn );
1499               #endif
1500               #endif
1501
1502               if ( rc != DECLINED )
1503                 {
1504                  #ifdef MOD_GZIP_DEBUG1
1505                  #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1506                  mod_gzip_printf( "%s: Something other than 'DECLINED' was returned.",cn);
1507                  mod_gzip_printf( "%s: Exit > return( rc=%d ) >",cn,rc);
1508                  #endif
1509                  #endif
1510
1511                  return rc;
1512                 }
1513               else
1514                 {
1515                  #ifdef MOD_GZIP_DEBUG1
1516                  #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1517                  mod_gzip_printf( "%s: 'DECLINED' was returned... Continuing chain...",cn);
1518                  #endif
1519                  #endif
1520                 }
1521              }
1522           }
1523         else
1524           {
1525            #ifdef MOD_GZIP_DEBUG1
1526            #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1527            mod_gzip_printf( "%s: 'handler_is_present' is FALSE...",cn);
1528            #endif
1529            #endif
1530           }
1531        }
1532      else
1533        {
1534         #ifdef MOD_GZIP_DEBUG1
1535         #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1536         mod_gzip_printf( "%s: 'runit' is FALSE...",cn);
1537         mod_gzip_printf( "%s: SKIPPING THIS MODULE",cn);
1538         #endif
1539         #endif
1540        }
1541
1542      count++;
1543     }
1544
1545  #ifdef MOD_GZIP_DEBUG1
1546  #ifdef MOD_GZIP_DEBUG1_RUN_HANDLERS1
1547  mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
1548  #endif
1549  #endif
1550
1551  return DECLINED;
1552 }
1553
1554
1555 int mod_gzip_delete_file(
1556 request_rec   *r,
1557 char          *filename
1558 )
1559 {
1560  #ifdef MOD_GZIP_DEBUG1
1561  char cn[]="mod_gzip_delete_file()";
1562  #endif
1563
1564  int final_rc = 0; 
1565
1566  #ifdef WIN32
1567  BOOL rc;
1568  #else
1569  int rc;
1570  #endif
1571
1572  #ifdef MOD_GZIP_DEBUG1
1573  mod_gzip_printf( "%s: Entry...",cn);
1574  mod_gzip_printf( "%s: filename = [%s]",cn,npp(filename));
1575  #endif
1576
1577  #ifdef WIN32
1578
1579  #ifdef MOD_GZIP_DEBUG1
1580  mod_gzip_printf( "%s: Call DeleteFile(%s)...",cn,npp(filename));
1581  #endif
1582
1583  rc = (BOOL) DeleteFile( filename );
1584
1585  #ifdef MOD_GZIP_DEBUG1
1586  mod_gzip_printf( "%s: Back DeleteFile(%s)...",cn,npp(filename));
1587  #endif
1588
1589  if ( rc == FALSE ) 
1590    {
1591     #ifdef MOD_GZIP_DEBUG1
1592     mod_gzip_printf( "%s: .... DeleteFile() FAILED",cn);
1593     #endif
1594    }
1595  else 
1596    {
1597     #ifdef MOD_GZIP_DEBUG1
1598     mod_gzip_printf( "%s: .... DeleteFile() SUCCEEDED",cn);
1599     #endif
1600
1601     final_rc = 1; 
1602    }
1603
1604  #else 
1605
1606  #ifdef MOD_GZIP_DEBUG1
1607  mod_gzip_printf( "%s: Call unlink(%s)...",cn,npp(filename));
1608  #endif
1609
1610  rc = (int) unlink( filename );
1611
1612  #ifdef MOD_GZIP_DEBUG1
1613  mod_gzip_printf( "%s: Back unlink(%s)...",cn,npp(filename));
1614  #endif
1615
1616  if ( rc < 0 ) 
1617    {
1618     #ifdef MOD_GZIP_DEBUG1
1619     mod_gzip_printf( "%s: .... unlink() FAILED",cn);
1620     #endif
1621    }
1622  else 
1623    {
1624     #ifdef MOD_GZIP_DEBUG1
1625     mod_gzip_printf( "%s: .... unlink() SUCCEEDED",cn);
1626     #endif
1627
1628     final_rc = 1; 
1629    }
1630
1631  #endif 
1632
1633  #ifdef MOD_GZIP_DEBUG1
1634
1635  if ( final_rc == 1 ) 
1636    {
1637     mod_gzip_printf( "%s: Exit > return( final_rc = %d ) SUCCESS >",cn,final_rc);
1638    }
1639  else 
1640    {
1641     mod_gzip_printf( "%s: Exit > return( final_rc = %d ) FAILURE >",cn,final_rc);
1642    }
1643
1644  #endif 
1645
1646  return( final_rc );
1647 }
1648
1649 static void mod_gzip_init( server_rec *server, pool *p )
1650 {
1651  int add_version_info=1;
1652
1653  #ifdef MOD_GZIP_DEBUG1
1654  char cn[]="mod_gzip_init()";
1655  #endif
1656
1657  mod_gzip_conf *mgc;
1658
1659  #ifdef MOD_GZIP_DEBUG1
1660
1661  mod_gzip_server_now = server;
1662
1663  mod_gzip_printf( " " );
1664  mod_gzip_printf( "%s: Entry...", cn );
1665
1666  #endif
1667
1668  mgc = ( mod_gzip_conf * )
1669  ap_get_module_config( server->module_config, &gzip_module );
1670
1671  #ifdef MOD_GZIP_DEBUG1
1672  mod_gzip_printf( "%s: MODULE_MAGIC_NUMBER = %ld", cn, (long) MODULE_MAGIC_NUMBER );
1673  #endif
1674
1675  if ( add_version_info )
1676    {
1677     #ifdef MOD_GZIP_DEBUG1
1678     mod_gzip_printf( "%s: add_version_info = TRUE", cn );
1679     #endif
1680
1681     #if MODULE_MAGIC_NUMBER >= 19980507
1682
1683     #ifdef MOD_GZIP_DEBUG1
1684     mod_gzip_printf( "%s: MODULE_MAGIC_NUMBER is >= 19980507",cn);
1685     mod_gzip_printf( "%s: Call ap_add_version_component(%s)...",
1686     cn, npp(MOD_GZIP_VERSION_INFO_STRING));
1687     #endif
1688
1689     ap_add_version_component( MOD_GZIP_VERSION_INFO_STRING );
1690
1691     #ifdef MOD_GZIP_DEBUG1
1692     mod_gzip_printf( "%s: Back ap_add_version_component(%s)...",
1693     cn, npp(MOD_GZIP_VERSION_INFO_STRING));
1694     #endif
1695
1696     #else
1697
1698     #ifdef MOD_GZIP_DEBUG1
1699     mod_gzip_printf( "%s: MODULE_MAGIC_NUMBER is NOT >= 19980507", cn );
1700     mod_gzip_printf( "%s: ap_add_version_component() NOT called.", cn );
1701     #endif
1702
1703     #endif
1704    }
1705  else
1706    {
1707     #ifdef MOD_GZIP_DEBUG1
1708     mod_gzip_printf( "%s: add_version_info = FALSE", cn );
1709     mod_gzip_printf( "%s: ap_add_version_component() NOT called.", cn );
1710     #endif
1711    }
1712
1713  #ifdef MOD_GZIP_DEBUG1
1714  mod_gzip_printf( "%s: Initialization completed...", cn );
1715  mod_gzip_printf( "%s: Exit > return( void ) >", cn );
1716  mod_gzip_printf( " " );
1717  #endif
1718 }
1719
1720 int mod_gzip_strncmp( char *s1, char *s2, int len1 )
1721 {
1722  int  i;
1723  char ch1;
1724  char ch2;
1725
1726  if ( ( s1 == 0 ) || ( s2 == 0 ) )
1727    {
1728     return 1;
1729    }
1730
1731  for ( i=0; i<len1; i++ )
1732     {
1733      if ( ( *s1 == 0 ) || ( *s2 == 0 ) ) return( 1 ); 
1734
1735      ch1 = *s1;
1736      ch2 = *s2;
1737
1738      if ( ch1 == '/' ) ch1 = '\\';
1739      if ( ch2 == '/' ) ch2 = '\\';
1740
1741      if ( ch1 != ch2 ) return 1;
1742
1743      s1++;
1744      s2++;
1745     }
1746
1747  return 0;
1748 }
1749
1750 int mod_gzip_strnicmp( char *s1, char *s2, int len1 )
1751 {
1752  int  i;
1753  char ch1;
1754  char ch2;
1755
1756  if ( ( s1 == 0 ) || ( s2 == 0 ) )
1757    {
1758     return 1;
1759    }
1760
1761  for ( i=0; i<len1; i++ )
1762     {
1763      if ( ( *s1 == 0 ) || ( *s2 == 0 ) ) return( 1 ); 
1764
1765      ch1 = *s1;
1766      ch2 = *s2;
1767
1768      if ( ch1 > 96 ) ch1 -= 32;
1769      if ( ch2 > 96 ) ch2 -= 32;
1770
1771      if ( ch1 == '/' ) ch1 = '\\';
1772      if ( ch2 == '/' ) ch2 = '\\';
1773
1774      if ( ch1 != ch2 ) return 1;
1775
1776      s1++;
1777      s2++;
1778     }
1779
1780  return 0;
1781 }
1782
1783 int mod_gzip_strendswith( char *s1, char *s2, int ignorcase )
1784 {
1785  int len1;
1786  int len2;
1787
1788  if ( ( s1 == 0 ) || ( s2 == 0 ) )
1789    {
1790     return 0;
1791    }
1792
1793  len1 = mod_gzip_strlen( s1 );
1794  len2 = mod_gzip_strlen( s2 );
1795
1796  if ( len1 < len2 )
1797    {
1798     /* Source string is shorter than search string */
1799     /* so no match is possible */
1800
1801     return 0;
1802    }
1803
1804  s1 += ( len1 - len2 );
1805
1806  if ( ignorcase )
1807    {
1808     if ( mod_gzip_strnicmp( s1, s2, len2 ) == 0 ) return 1; /* TRUE */
1809    }
1810  else
1811    {
1812     if ( mod_gzip_strncmp(  s1, s2, len2 ) == 0 ) return 1; /* TRUE */
1813    }
1814
1815  return 0; /* FALSE */
1816 }
1817
1818 int mod_gzip_strlen( char *s1 )
1819 {
1820  int len = 0;
1821
1822  if ( s1 != 0 )
1823    {
1824     while( *s1 != 0 ) { s1++; len++; }
1825    }
1826
1827  return len;
1828 }
1829
1830 int mod_gzip_strcpy( char *s1, char *s2 )
1831 {
1832  int len = 0;
1833
1834  if ( ( s1 != 0 )&&( s2 != 0 ) )
1835    {
1836     while( *s2 != 0 ) { *s1++ = *s2++; len++; }
1837     *s1=0; 
1838    }
1839
1840  return len;
1841 }
1842
1843 int mod_gzip_strcat( char *s1, char *s2 )
1844 {
1845  int len = 0;
1846
1847  if ( s1 != 0 )
1848    {
1849     while( *s1 != 0 ) { s1++; len++; }
1850     if ( s2 != 0 )
1851       {
1852        while( *s2 != 0 ) { *s1++ = *s2++; len++; }
1853        *s1 = 0; 
1854       }
1855    }
1856
1857  return len;
1858 }
1859
1860 int mod_gzip_stringcontains( char *source, char *substring )
1861 {
1862  int i;
1863  int len1;
1864  int len2;
1865  int len3;
1866  int offset=1; 
1867
1868  char *source1;    
1869  char *substring1; 
1870
1871  if ( source == NULL )
1872    {
1873     return 0;
1874    }
1875
1876  if ( substring == NULL )
1877    {
1878     return 0;
1879    }
1880
1881  source1    = source;    
1882  substring1 = substring; 
1883
1884  len1 = mod_gzip_strlen( source    );
1885  len2 = mod_gzip_strlen( substring );
1886
1887  if ( len1 < len2 )
1888    {
1889     return 0;
1890    }
1891
1892  len3 = len1 - len2;
1893
1894  for ( i=0; i<=len3; i++ )
1895     {
1896      if ( mod_gzip_strnicmp( source, substring, len2 ) == 0 )
1897        {
1898         source    = source1;    
1899         substring = substring1; 
1900
1901         return offset;
1902        }
1903
1904      source++;
1905      offset++;
1906     }
1907
1908  source    = source1;    
1909  substring = substring1; 
1910
1911  return 0;
1912 }
1913
1914 int mod_gzip_show_request_record(
1915 request_rec *r,
1916 char        *cn
1917 )
1918 {
1919  #ifdef MOD_GZIP_DEBUG1
1920
1921  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1922  mod_gzip_printf( "%s: REQUEST RECORD SNAPSHOT",cn);
1923  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1924  mod_gzip_printf( "%s: r = %ld", cn, (long) r );
1925  mod_gzip_printf( "%s: r->connection->keepalive=%d", cn, r->connection->keepalive );
1926  mod_gzip_printf( "%s: r->the_request=[%s]", cn, npp(r->the_request) );
1927  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1928  mod_gzip_printf( "%s: r->next = %ld",cn,(long)r->next);
1929  mod_gzip_printf( "%s: r->prev = %ld",cn,(long)r->prev);
1930  mod_gzip_printf( "%s: r->main = %ld",cn,(long)r->main);
1931  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1932  mod_gzip_printf( "%s: r->sent_bodyct  = %ld",cn,(long)r->sent_bodyct);
1933  mod_gzip_printf( "%s: r->bytes_sent   = %ld",cn,(long)r->bytes_sent);
1934  mod_gzip_printf( "%s: r->chunked      = %ld",cn,(long)r->chunked);
1935  mod_gzip_printf( "%s: r->byterange    = %ld",cn,(long)r->byterange);
1936  mod_gzip_printf( "%s: r->clength      = %ld (The 'real' content length )",cn,(long)r->clength);
1937  mod_gzip_printf( "%s: r->byterange    = %ld",cn,(long)r->remaining);
1938  mod_gzip_printf( "%s: r->read_length  = %ld",cn,(long)r->read_length);
1939  mod_gzip_printf( "%s: r->read_body    = %ld",cn,(long)r->read_body);
1940  mod_gzip_printf( "%s: r->read_chunked = %ld",cn,(long)r->read_chunked);
1941  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1942  mod_gzip_printf( "%s: *IN: r->uri                 =[%s]", cn, npp(r->uri));
1943  mod_gzip_printf( "%s: *IN: r->unparsed_uri        =[%s]", cn, npp(r->unparsed_uri));
1944  mod_gzip_printf( "%s: *IN: r->filename            =[%s]", cn, npp(r->filename));
1945  mod_gzip_printf( "%s: *IN: r->path_info           =[%s]", cn, npp(r->path_info));
1946  mod_gzip_printf( "%s: *IN: r->args                =[%s]", cn, npp(r->args));
1947  mod_gzip_printf( "%s: *IN: r->header_only         =%d",   cn, r->header_only );
1948  mod_gzip_printf( "%s: *IN: r->protocol            =[%s]", cn, npp(r->protocol));
1949  mod_gzip_printf( "%s: *IN: r->proto_num           =%d",   cn, r->proto_num );
1950  mod_gzip_printf( "%s: *IN: r->hostname            =[%s]", cn, npp(r->hostname));
1951  mod_gzip_printf( "%s: *IN: r->the_request         =[%s]", cn, npp(r->the_request));
1952  mod_gzip_printf( "%s: *IN: r->assbackwards        =%d",   cn, r->assbackwards );
1953  mod_gzip_printf( "%s: *IN: r->status_line         =[%s]", cn, npp(r->status_line));
1954  mod_gzip_printf( "%s: *IN: r->status              =%d",   cn, r->status );
1955  mod_gzip_printf( "%s: *IN: r->method              =[%s]", cn, npp(r->method));
1956  mod_gzip_printf( "%s: *IN: r->method_number       =%d",   cn, r->method_number );
1957  mod_gzip_printf( "%s: *IN: r->content_type        =[%s]", cn, npp(r->content_type));
1958  mod_gzip_printf( "%s: *IN: r->handler             =[%s]", cn, npp(r->handler));
1959  mod_gzip_printf( "%s: *IN: r->content_encoding    =[%s]", cn, npp(r->content_encoding));
1960  mod_gzip_printf( "%s: *IN: r->content_language    =[%s]", cn, npp(r->content_language));
1961  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1962  mod_gzip_printf( "%s: *IN: r->parsed_uri.scheme   =[%s]", cn, npp(r->parsed_uri.scheme));
1963  mod_gzip_printf( "%s: *IN: r->parsed_uri.hostinfo =[%s]", cn, npp(r->parsed_uri.hostinfo));
1964  mod_gzip_printf( "%s: *IN: r->parsed_uri.user     =[%s]", cn, npp(r->parsed_uri.user));
1965  mod_gzip_printf( "%s: *IN: r->parsed_uri.password =[%s]", cn, npp(r->parsed_uri.password));
1966  mod_gzip_printf( "%s: *IN: r->parsed_uri.hostname =[%s]", cn, npp(r->parsed_uri.hostname));
1967  mod_gzip_printf( "%s: *IN: r->parsed_uri.port_str =[%s]", cn, npp(r->parsed_uri.port_str));
1968  mod_gzip_printf( "%s: *IN: r->parsed_uri.port     =%u",   cn, r->parsed_uri.port );
1969  mod_gzip_printf( "%s: *IN: r->parsed_uri.path     =[%s]", cn, npp(r->parsed_uri.path));
1970  mod_gzip_printf( "%s: *IN: r->parsed_uri.query    =[%s]", cn, npp(r->parsed_uri.query));
1971  mod_gzip_printf( "%s: *IN: r->parsed_uri.fragment =[%s]", cn, npp(r->parsed_uri.fragment));
1972  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1973  mod_gzip_printf( "%s: 'r->headers_in'...",cn);
1974  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1975  mod_gzip_dump_a_table( r, (_table *) r->headers_in );
1976  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1977  mod_gzip_printf( "%s: 'r->headers_out'...",cn);
1978  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1979  mod_gzip_dump_a_table( r, (_table *) r->headers_out );
1980  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1981  mod_gzip_printf( "%s: 'r->err_headers_out'...",cn);
1982  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1983  mod_gzip_dump_a_table( r, (_table *) r->err_headers_out );
1984  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1985  mod_gzip_printf( "%s: 'r->subprocess_env'...",cn);
1986  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1987  mod_gzip_dump_a_table( r, (_table *) r->subprocess_env );
1988  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1989  mod_gzip_printf( "%s: 'r->notes'...",cn);
1990  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1991  mod_gzip_dump_a_table( r, (_table *) r->notes );
1992  mod_gzip_printf( "%s: -------------------------------------------------------------",cn);
1993
1994  if ( r->next )
1995    {
1996     mod_gzip_printf( "%s: r->next is valid... showing 'r->next' record...",cn);
1997
1998     mod_gzip_show_request_record( r->next, cn );
1999    }
2000
2001  #endif
2002
2003  return 1;
2004 }
2005
2006 #ifndef WIN32
2007 long fake_tid = 99;
2008 #endif
2009
2010 int mod_gzip_create_unique_filename(
2011 char *prefix,
2012 char *target,
2013 int   targetmaxlen
2014 )
2015 {
2016  long  process_id = 0;  
2017  long  thread_id  = 0;  
2018
2019  #ifdef MOD_GZIP_DEBUG1
2020  char cn[]="mod_gzip_create_unique_filename()";
2021  #endif
2022
2023  int prefixlen = 0;
2024  char slash[4];
2025
2026  #ifdef WIN32
2027  process_id = (long) GetCurrentProcessId();
2028  thread_id  = (long) GetCurrentThreadId();
2029  #else 
2030
2031  process_id = (long) getpid();
2032  thread_id  = (long) fake_tid;
2033  fake_tid++;
2034  if ( fake_tid > 9999999 ) fake_tid = 99; 
2035  #endif
2036
2037  #ifdef MOD_GZIP_DEBUG1
2038  mod_gzip_printf( "%s: Entry...",cn );
2039  mod_gzip_printf( "%s: prefix            = [%s]",cn,npp(prefix));
2040  mod_gzip_printf( "%s: target            = %ld", cn,(long)target);
2041  mod_gzip_printf( "%s: targetmaxlen      = %ld", cn,(long)targetmaxlen);
2042  mod_gzip_printf( "%s: process_id        = %ld", cn,(long)process_id );
2043  mod_gzip_printf( "%s: thread_id         = %ld", cn,(long)thread_id  );
2044  mod_gzip_printf( "%s: mod_gzip_iusn     = %ld", cn,(long)mod_gzip_iusn );
2045  #endif
2046
2047  if ( ( !target ) || ( targetmaxlen == 0 ) )
2048    {
2049     #ifdef MOD_GZIP_DEBUG1
2050     mod_gzip_printf( "%s: Invalid target or targetmaxlen value.",cn);
2051     mod_gzip_printf( "%s: Exit > return( 1 ) > ERROR >",cn );
2052     #endif
2053
2054     return 1;
2055    }
2056
2057  if ( prefix ) 
2058    {
2059     prefixlen = mod_gzip_strlen( prefix );
2060    }
2061
2062  if ( prefixlen > 0 ) 
2063    {
2064     slash[0]=0; 
2065
2066     if ( ( *(prefix+(prefixlen-1)) != '\\' ) &&
2067          ( *(prefix+(prefixlen-1)) != '/'  ) )
2068       {
2069        #ifdef WIN32
2070        slash[0]='\\';
2071        #else
2072        slash[0]='/';
2073        #endif
2074        slash[1]=0;
2075       }
2076
2077     sprintf(
2078     target,
2079     "%s%s_%ld_%ld_%ld.wrk",
2080     prefix,              
2081     slash,               
2082     (long) process_id,   
2083     (long) thread_id,    
2084     (long) mod_gzip_iusn 
2085     );
2086    }
2087  else 
2088    {
2089     sprintf(
2090     target,
2091     "_%ld_%ld_%ld.wrk",
2092     (long) process_id,   
2093     (long) thread_id,    
2094     (long) mod_gzip_iusn 
2095     );
2096    }
2097
2098  mod_gzip_iusn++; 
2099
2100  if ( mod_gzip_iusn > 999999999L ) mod_gzip_iusn = 1; 
2101
2102  #ifdef MOD_GZIP_DEBUG1
2103  mod_gzip_printf( "%s: target = [%s]",cn,npp(target));
2104  mod_gzip_printf( "%s: Exit > return( 0 ) >",cn );
2105  #endif
2106
2107  return 0;
2108 }
2109
2110 int mod_gzip_type_checker( request_rec *r )
2111 {
2112  int         i           = 0;
2113  int         rc          = 0;
2114  int         field_ok    = 0;
2115  int         action_flag = 0;
2116  const char *tablekey    = 0;
2117  const char *tablestring = 0;
2118  int         accept_encoding_gzip_seen = 0;
2119
2120  _table *t = 0;
2121  table_entry *elts = 0;
2122
2123  mod_gzip_conf *dconf = 0;
2124
2125  #ifdef MOD_GZIP_DEBUG1
2126  char cn[]="mod_gzip_type_checker()";
2127  mod_gzip_conf *sconf = 0;
2128  #endif
2129
2130  #ifdef MOD_GZIP_DEBUG1
2131  mod_gzip_printf( " " );
2132  mod_gzip_printf( "%s: ''''Entry...",cn);
2133  mod_gzip_printf( "%s: r                = %ld", cn,(long)r);
2134  mod_gzip_printf( "%s: r->main          = %ld", cn,(long)r->main);
2135  mod_gzip_printf( "%s: r->next          = %ld", cn,(long)r->next);
2136  mod_gzip_printf( "%s: r->prev          = %ld", cn,(long)r->prev);
2137  mod_gzip_printf( "%s: r->header_only   = %ld", cn,(long)r->header_only );
2138  mod_gzip_printf( "%s: r->method_number = %ld", cn,(long)r->method_number );
2139  mod_gzip_printf( "%s: r->method        = [%s]",cn,npp(r->method));
2140  mod_gzip_printf( "%s: r->unparsed_uri  = [%s]",cn,npp(r->unparsed_uri));
2141  mod_gzip_printf( "%s: r->uri           = [%s]",cn,npp(r->uri));
2142  mod_gzip_printf( "%s: r->filename      = [%s]",cn,npp(r->filename));
2143  mod_gzip_printf( "%s: r->handler       = [%s]",cn,npp(r->handler));
2144  mod_gzip_printf( "%s: r->content_type  = [%s]",cn,npp(r->content_type));
2145  #endif
2146
2147  #ifdef MOD_GZIP_USES_APACHE_LOGS
2148
2149  if ( r->main )
2150    {
2151     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:UNHANDLED_SUBREQ"));
2152    }
2153  else if ( r->prev )
2154    {
2155     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:UNHANDLED_REDIR"));
2156    }
2157  else
2158    {
2159     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:INIT1"));
2160    }
2161
2162  ap_table_setn( r->notes,"mod_gzip_input_size", ap_pstrdup(r->pool,"0"));
2163  ap_table_setn( r->notes,"mod_gzip_output_size",ap_pstrdup(r->pool,"0"));
2164  ap_table_setn( r->notes,"mod_gzip_compression_ratio",ap_pstrdup(r->pool,"0"));
2165
2166  #endif
2167
2168  if ( r->filename )
2169    {
2170     if ( mod_gzip_strendswith( r->filename, ".gz", 1 ) )
2171       {
2172        #ifdef MOD_GZIP_USES_APACHE_LOGS
2173        if ( r->prev )
2174          {
2175           /* This is a mod_gzip negotiated .gz static file transmit... */
2176           ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:STATIC_GZ_FOUND"));
2177          }
2178        else
2179          {
2180           /* This is a direct request from client for a .gz file... */
2181           ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:FEXT_GZ"));
2182          }
2183        #endif
2184
2185        #ifdef MOD_GZIP_DEBUG1
2186        mod_gzip_printf( "%s: r->filename ends with '.gz'...",cn);
2187        mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2188        #endif
2189
2190        return DECLINED;
2191       }
2192     #ifdef MOD_GZIP_DEBUG1
2193     else
2194       {
2195        #ifdef MOD_GZIP_DEBUG1
2196        mod_gzip_printf( "%s: r->filename does NOT end with '.gz'...",cn);
2197        mod_gzip_printf( "%s: OK to continue...",cn);
2198        #endif
2199       }
2200     #endif
2201    }
2202
2203  dconf = ( mod_gzip_conf * )
2204  ap_get_module_config( r->per_dir_config, &gzip_module );
2205
2206  #ifdef MOD_GZIP_DEBUG1
2207
2208  sconf = ( mod_gzip_conf * )
2209  ap_get_module_config( r->server->module_config, &gzip_module );
2210
2211  mod_gzip_printf( "%s: r->server->server_hostname = [%s]", cn,npp(r->server->server_hostname));
2212  mod_gzip_printf( "%s: sconf        = %ld", cn,(long)sconf);
2213  mod_gzip_printf( "%s: sconf->loc   = [%s]",cn,npp(sconf->loc));
2214  mod_gzip_printf( "%s: sconf->is_on = %ld", cn,(long)sconf->is_on);
2215  mod_gzip_printf( "%s: dconf        = %ld", cn,(long)dconf);
2216  mod_gzip_printf( "%s: dconf->loc   = [%s]",cn,npp(dconf->loc));
2217  mod_gzip_printf( "%s: dconf->is_on = %ld", cn,(long)dconf->is_on);
2218
2219  #endif
2220
2221  if ( !dconf )
2222    {
2223     #ifdef MOD_GZIP_USES_APACHE_LOGS
2224     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:NO_DCONF"));
2225     #endif
2226
2227     #ifdef MOD_GZIP_DEBUG1
2228     mod_gzip_printf( "%s: 'dconf' is NULL. Unable to continue.",cn);
2229     mod_gzip_printf( "%s: Exit > return( DECLINED ) >", cn);
2230     #endif
2231
2232     return DECLINED;
2233    }
2234
2235  if ( !dconf->is_on )
2236    {
2237     #ifdef MOD_GZIP_USES_APACHE_LOGS
2238     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:OFF"));
2239     #endif
2240
2241     #ifdef MOD_GZIP_DEBUG1
2242     mod_gzip_printf( "%s: 'dconf->is_on' is FALSE",cn);
2243     mod_gzip_printf( "%s: mod_gzip is not turned ON for this location...",cn);
2244     mod_gzip_printf( "%s: This transaction will be ignored...",cn);
2245     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2246     #endif
2247
2248     return DECLINED;
2249    }
2250
2251  if ( ( r->method_number != M_GET  ) &&
2252       ( r->method_number != M_POST ) )
2253    {
2254     #ifdef MOD_GZIP_USES_APACHE_LOGS
2255     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:NOT_GET_OR_POST"));
2256     #endif
2257
2258     #ifdef MOD_GZIP_DEBUG1
2259     mod_gzip_printf( "%s: r->method_number is NOT M_GET or M_POST",cn);
2260     mod_gzip_printf( "%s: Ignoring this request...",cn);
2261     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2262     #endif
2263
2264     return DECLINED;
2265    }
2266
2267  if ( r->header_only  )
2268    {
2269     #ifdef MOD_GZIP_USES_APACHE_LOGS
2270     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:HEAD_REQUEST"));
2271     #endif
2272
2273     #ifdef MOD_GZIP_DEBUG1
2274     mod_gzip_printf( "%s: r->header_only is TRUE...",cn);
2275     mod_gzip_printf( "%s: Ignoring this HEAD request...",cn);
2276     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2277     #endif
2278
2279     return DECLINED;
2280    }
2281
2282  if ( r->main ) /* SUBREQUEST */
2283    {
2284     #ifdef MOD_GZIP_DEBUG1
2285     mod_gzip_printf( "%s: r->main is TRUE - This is a subrequest...",cn);
2286     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2287     #endif
2288
2289     return DECLINED;
2290    }
2291  else
2292    {
2293     #ifdef MOD_GZIP_DEBUG1
2294     mod_gzip_printf( "%s: r->main is FALSE",cn);
2295     mod_gzip_printf( "%s: This is NOT a subrequest in progress...",cn);
2296     mod_gzip_printf( "%s: OK to continue...",cn);
2297     #endif
2298    }
2299
2300  if ( r->prev ) /* REDIRECT */
2301    {
2302     #ifdef MOD_GZIP_DEBUG1
2303     mod_gzip_printf( "%s: r->prev is NON-NULL... This is a redirection",cn);
2304     #ifdef MOD_GZIP_DEBUG1_SHOW_REQUEST_RECORD1
2305     mod_gzip_printf( "%s: Showing contents of r->prev now...",cn);
2306     mod_gzip_show_request_record( r->prev, cn );
2307     #endif
2308     #endif
2309
2310     tablestring = ap_table_get(r->prev->notes, "mod_gzip_running");
2311
2312     #ifdef MOD_GZIP_DEBUG1
2313     mod_gzip_printf( "%s: r->prev->notes->mod_gzip_running = [%s]",
2314                       cn,npp(tablestring));
2315     #endif
2316
2317     if ( tablestring )
2318       {
2319        #ifdef MOD_GZIP_DEBUG1
2320        mod_gzip_printf( "%s: 'mod_gzip_running' note FOUND",cn);
2321        #endif
2322
2323        if ( *tablestring == '1' )
2324          {
2325           #ifdef MOD_GZIP_DEBUG1
2326           mod_gzip_printf( "%s: 'mod_gzip_running' note value is '1'...",cn);
2327           #endif
2328
2329           #ifdef MOD_GZIP_DEBUG1
2330           mod_gzip_printf( "%s: ************************************************",cn);
2331           mod_gzip_printf( "%s: mod_gzip is currently 'running' so we ",cn);
2332           mod_gzip_printf( "%s: must return DECLINED so that other type_checker",cn);
2333           mod_gzip_printf( "%s: hooks can fire...",cn);
2334           mod_gzip_printf( "%s: ************************************************",cn);
2335           mod_gzip_printf( "%s: IMPORTANT: We must now 'replicate' the note flag",cn);
2336           mod_gzip_printf( "%s: on this record or next time around r->prev",cn);
2337           mod_gzip_printf( "%s: note flag check will be FALSE.",cn);
2338           mod_gzip_printf( "%s: ************************************************",cn);
2339           #endif
2340
2341           ap_table_setn(r->notes,"mod_gzip_running",ap_pstrdup(r->pool,"1"));
2342
2343           #ifdef MOD_GZIP_DEBUG1
2344           mod_gzip_printf( "%s: r->notes 'mod_gzip_running' set with value = '1'",cn);
2345           mod_gzip_printf( "%s: ************************************************",cn);
2346           mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2347           #endif
2348
2349           return DECLINED;
2350          }
2351        else
2352          {
2353           #ifdef MOD_GZIP_DEBUG1
2354           mod_gzip_printf( "%s: 'mod_gzip_running' note value is NOT '1'...",cn);
2355           #endif
2356          }
2357       }
2358     else
2359       {
2360        #ifdef MOD_GZIP_DEBUG1
2361        mod_gzip_printf( "%s: 'mod_gzip_running' note NOT FOUND",cn);
2362        #endif
2363       }
2364    }
2365  #ifdef MOD_GZIP_DEBUG1
2366  else
2367    {
2368     mod_gzip_printf( "%s: r->prev is NULL... This is NOT a redirection",cn);
2369    }
2370  #endif
2371
2372  #ifdef MOD_GZIP_DEBUG1
2373  mod_gzip_printf( "%s: dconf->min_http = %ld", cn, (long) dconf->min_http );
2374  mod_gzip_printf( "%s: r->proto_num    = %ld", cn, (long) r->proto_num );
2375  #endif
2376
2377  if ( ( dconf->min_http > 0 ) && ( r->proto_num > 0 ) )
2378    {
2379     if ( r->proto_num < dconf->min_http )
2380       {
2381        #ifdef MOD_GZIP_USES_APACHE_LOGS
2382        ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:HTTP_LEVEL_TOO_LOW"));
2383        #endif
2384
2385        #ifdef MOD_GZIP_DEBUG1
2386        mod_gzip_printf( "%s: HTTP protocol version level is TOO LOW", cn);
2387        mod_gzip_printf( "%s: Exit > return( DECLINED ) >", cn);
2388        #endif
2389
2390        return DECLINED;
2391       }
2392     else
2393       {
2394        #ifdef MOD_GZIP_DEBUG1
2395        mod_gzip_printf( "%s: HTTP protocol version level is OK", cn);
2396        #endif
2397       }
2398    }
2399
2400  #ifdef MOD_GZIP_DEBUG1
2401  mod_gzip_printf( "%s: Checking for [Accept-Encoding: gzip]", cn );
2402  #endif
2403
2404  tablestring = ap_table_get(r->headers_in, "Accept-Encoding");
2405
2406  #ifdef MOD_GZIP_DEBUG1
2407  mod_gzip_printf( "%s: r->headers_in->Accept-Encoding = [%s]",
2408                    cn,npp(tablestring));
2409  #endif
2410
2411  if ( tablestring )
2412    {
2413     #ifdef MOD_GZIP_DEBUG1
2414     mod_gzip_printf( "%s: 'Accept-Encoding' field seen...", cn);
2415     mod_gzip_printf( "%s: Checking for 'gzip' value...", cn);
2416     #endif
2417
2418     if ( mod_gzip_stringcontains( (char *)tablestring, "gzip" ) )
2419       {
2420        #ifdef MOD_GZIP_DEBUG1
2421        mod_gzip_printf( "%s: 'gzip' value seen...", cn);
2422        #endif
2423
2424        accept_encoding_gzip_seen = 1;
2425       }
2426     else
2427       {
2428        #ifdef MOD_GZIP_USES_APACHE_LOGS
2429        ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:NO_GZIP"));
2430        #endif
2431
2432        #ifdef MOD_GZIP_DEBUG1
2433        mod_gzip_printf( "%s: 'gzip' value NOT seen...", cn);
2434        mod_gzip_printf( "%s: Exit > return( DECLINED ) >", cn);
2435        #endif
2436
2437        return DECLINED;
2438       }
2439    }
2440  else
2441    {
2442     #ifdef MOD_GZIP_USES_APACHE_LOGS
2443     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:NO_ACCEPT_ENCODING"));
2444     #endif
2445
2446     #ifdef MOD_GZIP_DEBUG1
2447     mod_gzip_printf( "%s: 'Accept-Encoding' field NOT seen...", cn);
2448     mod_gzip_printf( "%s: Exit > return( DECLINED ) >", cn);
2449     #endif
2450
2451     return DECLINED;
2452    }
2453
2454  #ifdef MOD_GZIP_DEBUG1
2455  mod_gzip_printf( "%s: accept_encoding_gzip_seen = %ld",
2456                    cn, accept_encoding_gzip_seen );
2457  #endif
2458
2459  #ifdef MOD_GZIP_DEBUG1
2460  mod_gzip_printf( "%s: dconf->imap_total_entries = %d", cn,
2461                  (int) dconf->imap_total_entries );
2462  #endif
2463
2464  if ( dconf->imap_total_entries < 1 )
2465    {
2466     #ifdef MOD_GZIP_USES_APACHE_LOGS
2467     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:NO_ITEMS_DEFINED"));
2468     #endif
2469
2470     #ifdef MOD_GZIP_DEBUG1
2471     mod_gzip_printf( "%s: There are no IMAP entries. Unable to include/exclude",cn);
2472     mod_gzip_printf( "%s: Exit > return( DECLINED ) >", cn);
2473     #endif
2474
2475     return DECLINED;
2476    }
2477
2478  #ifdef MOD_GZIP_DEBUG1
2479  mod_gzip_printf( "%s: dconf->imap_total_isreqheader = %d", cn,
2480                  (int) dconf->imap_total_isreqheader );
2481  #endif
2482
2483  if ( dconf->imap_total_isreqheader > 0 )
2484    {
2485     #ifdef MOD_GZIP_DEBUG1
2486     mod_gzip_printf( "%s: Checking inbound REQUEST header fields...", cn );
2487     #endif
2488
2489     t    = (_table      *) r->headers_in;
2490     elts = (table_entry *) t->a.elts;
2491
2492     for ( i = 0; i < t->a.nelts; i++ )
2493        {
2494         #ifdef MOD_GZIP_DEBUG1
2495         mod_gzip_printf( "%s: %3.3d key=[%s] val=[%s]",
2496         cn,i,npp(elts[i].key),npp(elts[i].val));
2497         #endif
2498
2499         tablekey    = elts[i].key;
2500         tablestring = elts[i].val;
2501
2502         if (( tablekey && tablestring ))
2503           {
2504            #ifdef MOD_GZIP_DEBUG1
2505            mod_gzip_printf( "%s: Checking key[%s] string[%s]",
2506                                  cn,npp(tablekey),npp(tablestring));
2507            mod_gzip_printf( "%s: Call mod_gzip_validate1()...",cn);
2508            #endif
2509
2510            field_ok =
2511            mod_gzip_validate1(
2512            (request_rec   *) r,
2513            (mod_gzip_conf *) dconf,
2514            NULL, /* r->filename     (Not used here) */
2515            NULL, /* r->uri          (Not used here) */
2516            NULL, /* r->content_type (Not used here) */
2517            NULL, /* r->handler      (Not used here) */
2518            (char *) tablekey,    /* (Field key    ) */
2519            (char *) tablestring, /* (Field string ) */
2520            MOD_GZIP_REQUEST      /* (Direction    ) */
2521            );
2522
2523            #ifdef MOD_GZIP_DEBUG1
2524            mod_gzip_printf( "%s: Back mod_gzip_validate1()...",cn);
2525            mod_gzip_printf( "%s: field_ok = %d",cn,field_ok);
2526            #endif
2527
2528            if ( field_ok == MOD_GZIP_IMAP_DECLINED1 )
2529              {
2530               #ifdef MOD_GZIP_USES_APACHE_LOGS
2531               ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:REQ_HEADER_FIELD_EXCLUDED"));
2532               #endif
2533
2534               #ifdef MOD_GZIP_DEBUG1
2535               mod_gzip_printf( "%s: This request is EXCLUDED...",cn);
2536               mod_gzip_printf( "%s: Exit > return( DECLINED ) >", cn);
2537               #endif
2538
2539               return DECLINED;
2540              }
2541           }
2542        }
2543    }
2544  #ifdef MOD_GZIP_DEBUG1
2545  else
2546    {
2547     #ifdef MOD_GZIP_DEBUG1
2548     mod_gzip_printf( "%s: NO CHECK required on inbound REQUEST header fields...", cn );
2549     #endif
2550    }
2551  #endif
2552
2553  #ifdef MOD_GZIP_DEBUG1
2554  mod_gzip_printf( "%s: 1 ***: r->uri         =[%s]", cn, npp(r->uri ));
2555  mod_gzip_printf( "%s: 1 ***: r->unparsed_uri=[%s]", cn, npp(r->unparsed_uri ));
2556  mod_gzip_printf( "%s: 1 ***: r->filename    =[%s]", cn, npp(r->filename ));
2557  mod_gzip_printf( "%s: 1 ***: r->content_type=[%s]", cn, npp(r->content_type ));
2558  mod_gzip_printf( "%s: 1 ***: r->handler     =[%s]", cn, npp(r->handler ));
2559  #endif
2560
2561  if ( !r->content_type )
2562    {
2563     #ifdef MOD_GZIP_DEBUG1
2564     mod_gzip_printf( "%s: 'r->content_type' is NULL...",cn);
2565     mod_gzip_printf( "%s: Performing 'quick lookup'...",cn);
2566     mod_gzip_printf( "%s: Call mod_gzip_run_handlers(r,RUN_TYPE_CHECKERS)...",cn);
2567     #endif
2568
2569     rc = mod_gzip_run_handlers( r, MOD_GZIP_RUN_TYPE_CHECKERS );
2570
2571     #ifdef MOD_GZIP_DEBUG1
2572
2573     mod_gzip_printf( "%s: Back mod_gzip_run_handlers(r,RUN_TYPE_CHECKERS)...",cn);
2574
2575     if ( rc == OK )
2576       {
2577        mod_gzip_printf( "%s: rc = %d = OK",cn, rc );
2578       }
2579     else if ( rc == DECLINED )
2580       {
2581        mod_gzip_printf( "%s: rc = %d = DECLINED",cn, rc );
2582       }
2583     else if ( rc == DONE )
2584       {
2585        mod_gzip_printf( "%s: rc = %d = DONE",cn, rc );
2586       }
2587     else
2588       {
2589        mod_gzip_printf( "%s: rc = %d = HTTP_ERROR?",cn, rc );
2590       }
2591
2592     #endif
2593    }
2594  #ifdef MOD_GZIP_DEBUG1
2595  else
2596    {
2597     mod_gzip_printf( "%s: 'r->content_type' is VALID already...",cn);
2598     mod_gzip_printf( "%s: No 'quick lookup' was performed...",cn);
2599    }
2600  #endif
2601
2602  #ifdef MOD_GZIP_DEBUG1
2603  mod_gzip_printf( "%s: 2 ***: r->uri         =[%s]", cn, npp(r->uri ));
2604  mod_gzip_printf( "%s: 2 ***: r->unparsed_uri=[%s]", cn, npp(r->unparsed_uri ));
2605  mod_gzip_printf( "%s: 2 ***: r->filename    =[%s]", cn, npp(r->filename ));
2606  mod_gzip_printf( "%s: 2 ***: r->content_type=[%s]", cn, npp(r->content_type ));
2607  mod_gzip_printf( "%s: 2 ***: r->handler     =[%s]", cn, npp(r->handler ));
2608  mod_gzip_printf( "%s: Call mod_gzip_validate1()...",cn);
2609  #endif
2610
2611  action_flag =
2612  mod_gzip_validate1(
2613  (request_rec   *) r,
2614  (mod_gzip_conf *) dconf,
2615  (char *) r->filename,
2616  (char *) r->uri,
2617  (char *) r->content_type,
2618  (char *) r->handler,
2619  NULL, /* Field key    (Not used here) */
2620  NULL, /* Field string (Not used here) */
2621  0     /* Direction    (Not used here) */
2622  );
2623
2624  #ifdef MOD_GZIP_DEBUG1
2625
2626  mod_gzip_printf( "%s: Back mod_gzip_validate1()...",cn);
2627  mod_gzip_printf( "%s: action_flag  = %d",cn,action_flag);
2628
2629  if ( action_flag == MOD_GZIP_IMAP_DYNAMIC1 )
2630    {
2631     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_DYNAMIC1",cn);
2632    }
2633  else if ( action_flag == MOD_GZIP_IMAP_DYNAMIC2 )
2634    {
2635     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_DYNAMIC2",cn);
2636    }
2637  else if ( action_flag == MOD_GZIP_IMAP_STATIC1 )
2638    {
2639     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_STATIC1",cn);
2640    }
2641  else if ( action_flag == MOD_GZIP_IMAP_DECLINED1 )
2642    {
2643     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_DECLINED1",cn);
2644    }
2645  else
2646    {
2647     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_??? Unknown action",cn);
2648    }
2649
2650  #endif
2651
2652  if ( action_flag != MOD_GZIP_IMAP_DECLINED1 )
2653    {
2654     #ifdef MOD_GZIP_DEBUG1
2655     mod_gzip_printf( "%s: This transaction is a valid candidate...",cn);
2656     mod_gzip_printf( "%s: Saving current r->handler value [%s] to mod_gzip_r_handler note...",
2657                       cn, npp(r->handler) );
2658     #endif
2659
2660     if ( r->handler )
2661       {
2662        ap_table_setn( r->notes,"mod_gzip_r_handler",ap_pstrdup(r->pool,r->handler));
2663       }
2664     else
2665       {
2666        ap_table_setn( r->notes,"mod_gzip_r_handler",ap_pstrdup(r->pool,"0"));
2667       }
2668
2669     #ifdef MOD_GZIP_DEBUG1
2670     mod_gzip_printf( "%s: Forcing r->handler to be 'mod_gzip_handler'...", cn );
2671     #endif
2672
2673     r->handler = "mod_gzip_handler";
2674
2675     #ifdef MOD_GZIP_DEBUG1
2676     mod_gzip_printf( "%s: r->handler is now = [%s]", cn, npp(r->handler) );
2677     mod_gzip_printf( "%s: Exit > return( OK ) >", cn );
2678     mod_gzip_printf( " " );
2679     #endif
2680
2681     return OK;
2682    }
2683  else
2684    {
2685     #ifdef MOD_GZIP_DEBUG1
2686     mod_gzip_printf( "%s: This transaction is NOT a valid candidate...",cn);
2687     #endif
2688
2689     #ifdef MOD_GZIP_USES_APACHE_LOGS
2690     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:EXCLUDED"));
2691     #endif
2692
2693     #ifdef MOD_GZIP_DEBUG1
2694     mod_gzip_printf( "%s: Exit > return( DECLINED ) >", cn );
2695     mod_gzip_printf( " " );
2696     #endif
2697
2698     return DECLINED;
2699    }
2700 }
2701
2702 #ifdef MOD_GZIP_COMMAND_VERSION_USED
2703
2704 int mod_gzip_do_command(
2705 int            this_command, /* MOD_GZIP_COMMAND_XXXX */
2706 request_rec   *r,            /* Request record */
2707 mod_gzip_conf *dconf         /* Directory config pointer */
2708 )
2709 {
2710  /* Generic command response transmitter... */
2711
2712  char tmpbuf[2048]; /* Fill/flush as needed. Don't overflow */
2713  char *tmp=tmpbuf;
2714  int  tmplen=0;
2715  char s1[90];
2716
2717  #ifdef USE_MOD_GZIP_DEBUG1
2718  char cn[]="mod_gzip.c: mod_gzip_send_html_command_response()";
2719  #endif
2720
2721  /* Start... */
2722
2723  if ( this_command == MOD_GZIP_COMMAND_VERSION )
2724    {
2725     mod_gzip_strcpy(s1,"No");
2726
2727     if ( dconf )
2728       {
2729        if ( dconf->is_on == 1 ) mod_gzip_strcpy(s1,"Yes");
2730       }
2731
2732     sprintf( tmp,
2733     "<html><body>"
2734     "mod_gzip is available...<br>\r\n"
2735     "mod_gzip_version = %s<br>\r\n"
2736     "mod_gzip_on = %s<br>\r\n"
2737     "</body></html>",
2738     mod_gzip_version,
2739     s1
2740     );
2741
2742     #ifdef MOD_GZIP_USES_APACHE_LOGS
2743     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"COMMAND:VERSION"));
2744     #endif
2745    }
2746  else
2747    {
2748     #ifdef MOD_GZIP_USES_APACHE_LOGS
2749     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:INVALID_COMMAND"));
2750     #endif
2751
2752     return( DECLINED );
2753    }
2754
2755  /* Add the length of the response to the output header... */
2756  /* The third parameter to ap_table_set() MUST be a string. */
2757
2758  tmplen = strlen( tmp );
2759
2760  sprintf( s1, "%d", tmplen );
2761
2762  ap_table_set( r->headers_out, "Content-Length", s1 );
2763
2764  /* Make sure the content type matches this response... */
2765
2766  r->content_type = "text/html";
2767
2768  /* Start a timer for this return trip... */
2769
2770  ap_soft_timeout( "mod_gzip: mod_gzip_do_command", r );
2771
2772  #ifdef MOD_GZIP_COMMANDS_USE_LAST_MODIFIED
2773
2774  /* Set the 'Last modified' stamp to current time/date... */
2775
2776  ap_set_last_modified(r);
2777
2778  /* TODO? Add 'no-cache' option(s) to mod_gzip command responses */
2779  /* so user doesn't have hit reload to get fresh data? This might */
2780  /* be necessary for static files that are subject to an Apache */
2781  /* lookup but mod_gzip command results as sent 'fresh' each */
2782  /* time no matter what so there doesn't seem to be a need for */
2783  /* any 'Last modified' information. Just pump a 200 + data and */
2784  /* then turn and burn... */
2785
2786  #endif /* MOD_GZIP_COMMANDS_USE_LAST_MODIFIED */
2787
2788  /* Send the HTTP response header... */
2789
2790  ap_send_http_header(r);
2791
2792  /* Send the response BODY... */
2793
2794  ap_send_mmap( tmp, r, 0, tmplen );
2795
2796  /* Clean up and exit... */
2797
2798  ap_kill_timeout(r);
2799
2800  return OK;
2801
2802 }/* End of mod_gzip_send_html_command_response() */
2803
2804 #endif /* MOD_GZIP_COMMAND_VERSION_USED */
2805
2806
2807 static int mod_gzip_handler( request_rec *r )
2808 {
2809  #ifdef MOD_GZIP_DEBUG1
2810  char cn[]="mod_gzip_handler()";
2811  #endif
2812
2813  int  rc = DECLINED;
2814  int  action_flag = 0;
2815
2816  request_rec *r__next=0;
2817
2818  mod_gzip_conf *sconf = 0;
2819  mod_gzip_conf *dconf = 0;
2820
2821  const char *tablestring;
2822
2823  const char *s1;
2824  const char *s2;
2825  const char *s3;
2826  const char *s4;
2827
2828  #ifdef MOD_GZIP_CAN_NEGOTIATE
2829  struct stat statbuf;
2830  char  *new_uri;
2831  char  *new_name;
2832  #endif
2833
2834  #ifdef MOD_GZIP_DEBUG1
2835
2836  mod_gzip_server_now = r->server;
2837
2838  mod_gzip_printf( " " );
2839
2840  mod_gzip_printf( "%s: ''''Entry...",cn);
2841  mod_gzip_printf( "%s: r               = %ld", cn,(long)r);
2842  mod_gzip_printf( "%s: r->main         = %ld", cn,(long)r->main);
2843  mod_gzip_printf( "%s: r->next         = %ld", cn,(long)r->next);
2844  mod_gzip_printf( "%s: r->prev         = %ld", cn,(long)r->prev);
2845  mod_gzip_printf( "%s: r->unparsed_uri = [%s]",cn,npp(r->unparsed_uri));
2846  mod_gzip_printf( "%s: r->uri          = [%s]",cn,npp(r->uri));
2847  mod_gzip_printf( "%s: r->filename     = [%s]",cn,npp(r->filename));
2848  mod_gzip_printf( "%s: r->handler      = [%s]",cn,npp(r->handler));
2849
2850  #endif
2851
2852  if ( r->main ) /* SUBREQUEST */
2853    {
2854     #ifdef MOD_GZIP_DEBUG1
2855     mod_gzip_printf( "%s: r->main is TRUE - This is a subrequest...",cn);
2856     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2857     #endif
2858
2859     return DECLINED;
2860    }
2861  #ifdef MOD_GZIP_DEBUG1
2862  else
2863    {
2864     mod_gzip_printf( "%s: r->main is FALSE",cn);
2865     mod_gzip_printf( "%s: This is NOT a subrequest in progress...",cn);
2866     mod_gzip_printf( "%s: OK to continue...",cn);
2867    }
2868  #endif
2869
2870  if ( r->filename )
2871    {
2872     if ( mod_gzip_strendswith( r->filename, ".gz", 1 ) )
2873       {
2874        #ifdef MOD_GZIP_USES_APACHE_LOGS
2875        if ( r->prev )
2876          {
2877           /* This is a mod_gzip negotiated .gz static file transmit... */
2878           ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:STATIC_GZ_FOUND"));
2879          }
2880        else
2881          {
2882           /* This is a direct request from client for a .gz file... */
2883           ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:FEXT_GZ"));
2884          }
2885        #endif
2886
2887        #ifdef MOD_GZIP_DEBUG1
2888        mod_gzip_printf( "%s: r->filename ends with '.gz'...",cn);
2889        mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2890        #endif
2891
2892        return DECLINED;
2893       }
2894     #ifdef MOD_GZIP_DEBUG1
2895     else
2896       {
2897        #ifdef MOD_GZIP_DEBUG1
2898        mod_gzip_printf( "%s: r->filename does NOT end with '.gz'...",cn);
2899        mod_gzip_printf( "%s: OK to continue...",cn);
2900        #endif
2901       }
2902     #endif
2903    }
2904
2905  if ( r->prev ) /* REDIRECT */
2906    {
2907     #ifdef MOD_GZIP_DEBUG1
2908     mod_gzip_printf( "%s: r->prev is TRUE - This is a redirection...",cn);
2909     #endif
2910
2911     /* This might be a 'directory' index lookup... */
2912
2913     tablestring = ap_table_get(r->prev->notes, "mod_gzip_running");
2914
2915     #ifdef MOD_GZIP_DEBUG1
2916     mod_gzip_printf( "%s: r->prev->notes->mod_gzip_running = [%s]",
2917                       cn,npp(tablestring));
2918     #endif
2919
2920     if ( tablestring )
2921       {
2922        #ifdef MOD_GZIP_DEBUG1
2923        mod_gzip_printf( "%s: 'mod_gzip_running' note FOUND",cn);
2924        #endif
2925
2926        if ( *tablestring == '1' )
2927          {
2928           #ifdef MOD_GZIP_DEBUG1
2929           mod_gzip_printf( "%s: 'mod_gzip_running' note value is '1'...",cn);
2930           #endif
2931
2932           #ifdef MOD_GZIP_DEBUG1
2933           mod_gzip_printf( "%s: ************************************************",cn);
2934           mod_gzip_printf( "%s: mod_gzip is currently 'running' so we ",cn);
2935           mod_gzip_printf( "%s: must return DECLINED.",cn);
2936           mod_gzip_printf( "%s: ************************************************",cn);
2937           mod_gzip_printf( "%s: IMPORTANT: We must now 'replicate' the note flag",cn);
2938           mod_gzip_printf( "%s: on this record or next time around r->prev",cn);
2939           mod_gzip_printf( "%s: note flag check will be FALSE.",cn);
2940           mod_gzip_printf( "%s: ************************************************",cn);
2941           #endif
2942
2943           ap_table_setn(r->notes,"mod_gzip_running",ap_pstrdup(r->pool,"1"));
2944
2945           #ifdef MOD_GZIP_DEBUG1
2946           mod_gzip_printf( "%s: r->notes 'mod_gzip_running' set with value = '1'",cn);
2947           mod_gzip_printf( "%s: ************************************************",cn);
2948           #endif
2949
2950           #ifdef MOD_GZIP_DEBUG1
2951           mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
2952           #endif
2953
2954           return DECLINED;
2955          }
2956        #ifdef MOD_GZIP_DEBUG1
2957        else
2958          {
2959           mod_gzip_printf( "%s: 'mod_gzip_running' note value is NOT '1'...",cn);
2960          }
2961        #endif
2962       }
2963     #ifdef MOD_GZIP_DEBUG1
2964     else
2965       {
2966        mod_gzip_printf( "%s: 'mod_gzip_running' note NOT FOUND",cn);
2967       }
2968     #endif
2969    }
2970  #ifdef MOD_GZIP_DEBUG1
2971  else
2972    {
2973     mod_gzip_printf( "%s: r->prev is FALSE",cn);
2974     mod_gzip_printf( "%s: This is NOT a redirection in progress...",cn);
2975     mod_gzip_printf( "%s: OK to continue...",cn);
2976    }
2977  #endif
2978
2979  sconf = ( mod_gzip_conf * )
2980  ap_get_module_config( r->server->module_config, &gzip_module );
2981
2982  dconf = ( mod_gzip_conf * )
2983  ap_get_module_config( r->per_dir_config, &gzip_module );
2984
2985  #ifdef MOD_GZIP_DEBUG1
2986  mod_gzip_printf( "%s: r->server->server_hostname = [%s]", cn,npp(r->server->server_hostname));
2987  mod_gzip_printf( "%s: sconf        = %ld", cn,(long)sconf);
2988  mod_gzip_printf( "%s: sconf->loc   = [%s]",cn,npp(sconf->loc));
2989  mod_gzip_printf( "%s: sconf->is_on = %ld", cn,(long)sconf->is_on);
2990  mod_gzip_printf( "%s: dconf        = %ld", cn,(long)dconf);
2991  mod_gzip_printf( "%s: dconf->loc   = [%s]",cn,npp(dconf->loc));
2992  mod_gzip_printf( "%s: dconf->is_on = %ld", cn,(long)dconf->is_on);
2993  #endif
2994
2995  #ifdef MOD_GZIP_COMMAND_VERSION_USED
2996
2997  /* NOTE: Certain mod_gzip 'commands' should return a response */
2998  /* even if mod_gzip is OFF in the current location. Make sure */
2999  /* the checks for these commands take place BEFORE checking */
3000  /* the actual mod_gzip on/off status... */
3001
3002  #ifdef MOD_GZIP_DEBUG1
3003  mod_gzip_printf( "%s: dconf->command_version = [%s]",cn,npp(dconf->command_version));
3004  #endif
3005
3006  /* Check for mod_gzip commands in the 'r->unparsed_uri' request */
3007  /* line so the commands can actually be part of query parms that */
3008  /* follow the '?'. 'r->uri' is simply he URI itself with any/all */
3009  /* additional query arguments removed already... */
3010
3011  if ( dconf->command_version[0] != 0 )
3012    {
3013     if ( mod_gzip_stringcontains(r->unparsed_uri,dconf->command_version))
3014       {
3015        #ifdef MOD_GZIP_DEBUG1
3016        mod_gzip_printf( "%s: Call mod_gzip_do_command( MOD_GZIP_COMMAND_VERSION, r )...",cn);
3017        #endif
3018
3019        /* mod_gzip_do_command() returns the correct command */
3020        /* response page and (normally) just returns 'OK'... */
3021
3022        return(
3023        mod_gzip_do_command(
3024        MOD_GZIP_COMMAND_VERSION,
3025        r,
3026        dconf
3027        ));
3028       }
3029    }
3030
3031  #endif /* MOD_GZIP_COMMAND_VERSION_USED */
3032
3033  tablestring = ap_table_get(r->notes, "mod_gzip_r_handler");
3034
3035  if ( !tablestring )
3036    {
3037     #ifdef MOD_GZIP_DEBUG1
3038     mod_gzip_printf( "%s: r->notes->mod_gzip_r_handler = NOT FOUND",cn);
3039     mod_gzip_printf( "%s: This transaction will be ignored...",cn);
3040     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
3041     #endif
3042
3043     return DECLINED;
3044    }
3045
3046  #ifdef MOD_GZIP_DEBUG1
3047  mod_gzip_printf( "%s: r->notes->mod_gzip_r_handler = FOUND",cn);
3048  mod_gzip_printf( "%s: r->notes->mod_gzip_r_handler = [%s]",cn,npp(tablestring));
3049  #endif
3050
3051  #ifdef MOD_GZIP_USES_APACHE_LOGS
3052  ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"INIT2"));
3053  #endif
3054
3055  if ( !dconf->is_on )
3056    {
3057     #ifdef MOD_GZIP_USES_APACHE_LOGS
3058     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:OFF2"));
3059     #endif
3060
3061     #ifdef MOD_GZIP_DEBUG1
3062     mod_gzip_printf( "%s: 'dconf->is_on' is FALSE",cn);
3063     mod_gzip_printf( "%s: mod_gzip is not turned ON for this location...",cn);
3064     mod_gzip_printf( "%s: This transaction will be ignored...",cn);
3065     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
3066     #endif
3067
3068     return DECLINED;
3069    }
3070
3071  if ( *tablestring == '0' )
3072    {
3073     r->handler = 0;
3074    }
3075  else
3076    {
3077     r->handler = tablestring;
3078    }
3079
3080  #ifdef MOD_GZIP_DEBUG1
3081  mod_gzip_printf( "%s: r->handler set back to = [%s]",cn,npp(r->handler));
3082  #endif
3083
3084  /* Verify it (again) in case names have changed... */
3085
3086  #ifdef MOD_GZIP_DEBUG1
3087  mod_gzip_printf( "%s: Call mod_gzip_validate1()...",cn);
3088  #endif
3089
3090  action_flag =
3091  mod_gzip_validate1(
3092  (request_rec   *) r,
3093  (mod_gzip_conf *) dconf,
3094  (char *) r->filename,
3095  (char *) r->uri,
3096  (char *) r->content_type,
3097  (char *) r->handler,
3098  NULL, /* Field key    (Not used here  */
3099  NULL, /* Field string (Not used here) */
3100  0     /* Direction    (Not used here) */
3101  );
3102
3103  #ifdef MOD_GZIP_DEBUG1
3104
3105  mod_gzip_printf( "%s: Back mod_gzip_validate1()...",cn);
3106  mod_gzip_printf( "%s: action_flag  = %d",cn,action_flag);
3107
3108  if ( action_flag == MOD_GZIP_IMAP_DYNAMIC1 )
3109    {
3110     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_DYNAMIC1",cn);
3111    }
3112  else if ( action_flag == MOD_GZIP_IMAP_DYNAMIC2 )
3113    {
3114     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_DYNAMIC2",cn);
3115    }
3116  else if ( action_flag == MOD_GZIP_IMAP_STATIC1 )
3117    {
3118     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_STATIC1",cn);
3119    }
3120  else if ( action_flag == MOD_GZIP_IMAP_DECLINED1 )
3121    {
3122     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_DECLINED1",cn);
3123    }
3124  else
3125    {
3126     mod_gzip_printf( "%s: action_flag  = MOD_GZIP_IMAP_??? Unknown action",cn);
3127    }
3128
3129  #endif
3130
3131  if ( action_flag == MOD_GZIP_IMAP_DECLINED1 )
3132    {
3133     #ifdef MOD_GZIP_DEBUG1
3134     mod_gzip_printf( "%s: This transaction is NOT a valid candidate...",cn);
3135     mod_gzip_printf( "%s: This transaction will be ignored...",cn);
3136     #endif
3137
3138     #ifdef MOD_GZIP_USES_APACHE_LOGS
3139     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:EXCLUDED_BY_HANDLER"));
3140     #endif
3141
3142     #ifdef MOD_GZIP_DEBUG1
3143     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
3144     #endif
3145
3146     return DECLINED;
3147    }
3148
3149  #ifdef MOD_GZIP_DEBUG1
3150  mod_gzip_printf( "%s: This transaction is a valid candidate...",cn);
3151  #endif
3152
3153  #ifdef MOD_GZIP_CAN_NEGOTIATE
3154
3155  #ifdef MOD_GZIP_DEBUG1
3156  mod_gzip_printf( "%s: dconf->can_negotiate = %d",cn,(int)dconf->can_negotiate);
3157  #endif
3158
3159  if ( dconf->can_negotiate == 1 )
3160    {
3161     #ifdef MOD_GZIP_DEBUG1
3162     mod_gzip_printf( "%s: dconf->can_negotiate is TRUE...",cn);
3163     #endif
3164
3165     /* Check for a static compressed version of the file requested... */
3166
3167     new_name = ap_pstrcat(r->pool, r->filename, ".gz", NULL);
3168
3169     #ifdef MOD_GZIP_DEBUG1
3170     mod_gzip_printf( "%s: Call stat(new_name=[%s])...",cn,npp(new_name));
3171     #endif
3172
3173     if ( stat( new_name, &statbuf) != 0 )
3174       {
3175        #ifdef MOD_GZIP_DEBUG1
3176        mod_gzip_printf( "%s: .... stat() call FAILED",cn);
3177        mod_gzip_printf( "%s: OK to continue...",cn);
3178        #endif
3179       }
3180     else
3181       {
3182        #ifdef MOD_GZIP_DEBUG1
3183        mod_gzip_printf( "%s: .... stat() call SUCCEEDED",cn);
3184        mod_gzip_printf( "%s: Sending precompressed version of file...",cn);
3185        mod_gzip_printf( "%s: GZ_REDIRECT: START...",cn);
3186        #endif
3187
3188        new_name = ap_pstrcat(r->pool, r->uri, ".gz", NULL);
3189
3190        if ( r->args != NULL )
3191          {
3192           new_uri = ap_pstrcat(r->pool, new_name, "?", r->args, NULL);
3193          }
3194        else
3195          {
3196           new_uri = ap_pstrcat(r->pool, new_name, NULL );
3197          }
3198
3199        #ifdef MOD_GZIP_DEBUG1
3200        mod_gzip_printf( "%s: Call ap_internal_redirect(new_uri=[%s])...",
3201                          cn,npp(new_uri));
3202        mod_gzip_printf( " " );
3203        #endif
3204
3205        ap_internal_redirect( new_uri, r );
3206
3207        #ifdef MOD_GZIP_DEBUG1
3208        mod_gzip_printf( " " );
3209        mod_gzip_printf( "%s: Back ap_internal_redirect(new_uri=[%s])...",
3210                          cn,npp(new_uri));
3211        mod_gzip_printf( "%s: GZ_REDIRECT: FINISHED...",cn);
3212        #endif
3213
3214        #ifdef MOD_GZIP_DEBUG1
3215        mod_gzip_printf( "%s: Exit > return( OK ) >",cn);
3216        #endif
3217
3218        /* We are about to return OK to end the transaction but go */
3219        /* ahead and make the mod_gzip final result a DECLINED */
3220        /* condition since that's what actually happened... */
3221
3222        #ifdef MOD_GZIP_USES_APACHE_LOGS
3223        ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:STATIC_GZ_FOUND"));
3224        #endif
3225
3226        return OK;
3227       }
3228    }
3229  #ifdef MOD_GZIP_DEBUG1
3230  else
3231    {
3232     mod_gzip_printf( "%s: dconf->can_negotiate is FALSE",cn);
3233    }
3234  #endif
3235
3236  #endif /* MOD_GZIP_CAN_NEGOTIATE */
3237
3238  #ifdef MOD_GZIP_DEBUG1
3239  mod_gzip_printf( "%s: Call mod_gzip_redir1_handler( r, dconf )...",cn);
3240  #endif
3241
3242  rc = (int) mod_gzip_redir1_handler( r, dconf );
3243
3244  #ifdef MOD_GZIP_DEBUG1
3245
3246  mod_gzip_printf( "%s: Back mod_gzip_redir1_handler( r, dconf )...",cn);
3247
3248  if ( rc == OK )
3249    {
3250     mod_gzip_printf( "%s: rc = %d OK", cn, (int) rc);
3251    }
3252  else if ( rc == DECLINED )
3253    {
3254     mod_gzip_printf( "%s: rc = %d DECLINED", cn, (int) rc );
3255    }
3256  else
3257    {
3258     mod_gzip_printf( "%s: rc = %d ( HTTP ERROR CODE? )", cn, (int) rc );
3259    }
3260
3261  #endif
3262
3263  if ( rc != OK )
3264    {
3265     #ifdef MOD_GZIP_DEBUG1
3266     mod_gzip_printf( "%s: mod_gzip_redir1_handler() call FAILED...",cn);
3267     #endif
3268
3269     #ifdef MOD_GZIP_USES_APACHE_LOGS
3270
3271     ap_table_setn(
3272     r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"RECOVERY"));
3273     #endif
3274
3275     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_WARNING, r->server,
3276     "mod_gzip: RECOVERY [%s]", r->the_request );
3277
3278     #ifdef MOD_GZIP_DEBUG1
3279     mod_gzip_printf( "%s: RECOVERY_REDIRECT: START...",cn);
3280     mod_gzip_printf( "%s: Call ap_internal_redirect(r->unparsed_uri=[%s])...",
3281                       cn,npp(r->unparsed_uri));
3282     mod_gzip_printf( " " );
3283     #endif
3284
3285     ap_internal_redirect( r->unparsed_uri, r );
3286
3287     #ifdef MOD_GZIP_DEBUG1
3288     mod_gzip_printf( " " );
3289     mod_gzip_printf( "%s: Back ap_internal_redirect(r->unparsed_uri=[%s])...",
3290                       cn,npp(r->unparsed_uri));
3291     mod_gzip_printf( "%s: RECOVERY_REDIRECT: FINISHED...",cn);
3292     #endif
3293
3294     rc = OK;
3295    }
3296
3297  #ifdef MOD_GZIP_USES_APACHE_LOGS
3298
3299  if ( r->next )
3300    {
3301     r__next = r->next;
3302
3303     s1 = ap_table_get( r->notes, "mod_gzip_result" );
3304     s2 = ap_table_get( r->notes, "mod_gzip_input_size" );
3305     s3 = ap_table_get( r->notes, "mod_gzip_output_size" );
3306     s4 = ap_table_get( r->notes, "mod_gzip_compression_ratio" );
3307
3308     while( r__next )
3309       {
3310        if ( s1 ) ap_table_setn( r__next->notes,"mod_gzip_result",ap_pstrdup(r__next->pool,s1));
3311        if ( s2 ) ap_table_setn( r__next->notes,"mod_gzip_input_size",ap_pstrdup(r__next->pool,s2));
3312        if ( s3 ) ap_table_setn( r__next->notes,"mod_gzip_output_size",ap_pstrdup(r__next->pool,s3));
3313        if ( s4 ) ap_table_setn( r__next->notes,"mod_gzip_compression_ratio",ap_pstrdup(r__next->pool,s4));
3314
3315        r__next = r__next->next;
3316       }
3317    }
3318
3319  #endif
3320
3321  #ifdef MOD_GZIP_DEBUG1
3322  #ifdef MOD_GZIP_DEBUG1_SHOW_REQUEST_RECORD2
3323  mod_gzip_show_request_record( r, cn );
3324  #endif
3325  #endif
3326
3327  #ifdef MOD_GZIP_DEBUG1
3328
3329  mod_gzip_printf( "%s: 1 r->connection->client->outcnt     = %ld",
3330                    cn,   r->connection->client->outcnt );
3331  mod_gzip_printf( "%s: 1 r->connection->client->bytes_sent = %ld",
3332                    cn,   r->connection->client->bytes_sent );
3333  mod_gzip_printf( "%s: 1 Sum of the 2......................= %ld",
3334                    cn,   r->connection->client->outcnt +
3335                          r->connection->client->bytes_sent );
3336  if ( rc == OK )
3337    {
3338     mod_gzip_printf( "%s: Exit > return ( rc = %d OK ) >",cn,(int)rc);
3339    }
3340  else if ( rc == DECLINED )
3341    {
3342     mod_gzip_printf( "%s: Exit > return ( rc = %d DECLINED ) >",cn,(int)rc);
3343    }
3344  else
3345    {
3346     mod_gzip_printf( "%s: Exit > return ( rc = %d HTTP_ERROR ) >",cn,(int)rc);
3347    }
3348
3349  #endif
3350
3351  return rc;
3352 }
3353
3354 int mod_gzip_set_defaults1( mod_gzip_conf *cfg )
3355 {
3356  int i;
3357
3358  cfg->is_on                  = 0;    
3359  cfg->is_on_set              = 0;
3360
3361  cfg->keep_workfiles         = 0;    
3362  cfg->keep_workfiles_set     = 0;
3363
3364  cfg->add_header_count       = 0;    
3365  cfg->add_header_count_set   = 0;
3366
3367  cfg->dechunk                = 0;    
3368  cfg->dechunk_set            = 0;
3369
3370  cfg->min_http               = 0;    
3371  cfg->min_http_set           = 0;
3372
3373  cfg->minimum_file_size      = 300;  
3374  cfg->minimum_file_size_set  = 0;
3375
3376  cfg->maximum_file_size      = 0;    
3377  cfg->maximum_file_size_set  = 0;
3378
3379  cfg->maximum_inmem_size     = 0;    
3380  cfg->maximum_inmem_size_set = 0;
3381
3382  #ifdef WIN32
3383  mod_gzip_strcpy( cfg->temp_dir, "c:\\temp\\" );
3384  #else
3385  mod_gzip_strcpy( cfg->temp_dir, "/tmp/" );
3386  #endif
3387  cfg->temp_dir_set           = 0;
3388
3389  cfg->imap_total_entries     = 0;
3390  cfg->imap_total_ismime      = 0;
3391  cfg->imap_total_isfile      = 0;
3392  cfg->imap_total_isuri       = 0;
3393  cfg->imap_total_ishandler   = 0;
3394  cfg->imap_total_isreqheader = 0;
3395  cfg->imap_total_isrspheader = 0;
3396
3397  for ( i=0; i<MOD_GZIP_IMAP_MAXNAMES; i++ )
3398     {
3399      memset( &(cfg->imap[i]), 0, mod_gzip_imap_size );
3400     }
3401
3402  #ifdef MOD_GZIP_COMMAND_VERSION_USED
3403  memset(
3404  cfg->command_version, 0, MOD_GZIP_COMMAND_VERSION_MAXLEN );
3405  cfg->command_version_set = 0;
3406  #endif
3407
3408  #ifdef MOD_GZIP_CAN_NEGOTIATE
3409  cfg->can_negotiate     = 0;
3410  cfg->can_negotiate_set = 0;
3411  #endif
3412
3413  return 0;
3414 }
3415
3416 #ifdef REFERENCE
3417 static void *mod_gzip_merge_dconfig(
3418 pool *p,
3419 void *parent_conf,
3420 void *newloc_conf
3421 )
3422 {
3423  mod_gzip_conf *merged_config = (mod_gzip_conf *) ap_pcalloc(p, sizeof(mod_gzip_conf));
3424  mod_gzip_conf *pconf         = (mod_gzip_conf *) parent_conf;
3425  mod_gzip_conf *nconf         = (mod_gzip_conf *) newloc_conf;
3426
3427  mod_gzip_merge1(
3428  ( pool          * ) p,
3429  ( mod_gzip_conf * ) merged_config,
3430  ( mod_gzip_conf * ) pconf,
3431  ( mod_gzip_conf * ) nconf
3432  );
3433
3434  return (void *) merged_config;
3435 }
3436 #endif
3437
3438 int mod_gzip_merge1(
3439 pool          *p,
3440 mod_gzip_conf *merged_config,
3441 mod_gzip_conf *pconf,
3442 mod_gzip_conf *nconf )
3443 {
3444  #ifdef MOD_GZIP_DEBUG1
3445  char cn[]="mod_gzip_merge1():::::::";
3446  char ch1 = 0; 
3447  #endif
3448
3449  char *p1    = 0; 
3450  char *p2    = 0; 
3451  int   i     = 0; 
3452  int   ii    = 0; 
3453  int   l1    = 0; 
3454  int   l2    = 0; 
3455  int   match = 0; 
3456
3457  int   total             = 0; 
3458  int   total_ismime      = 0;
3459  int   total_isfile      = 0;
3460  int   total_isuri       = 0;
3461  int   total_ishandler   = 0;
3462  int   total_isreqheader = 0;
3463  int   total_isrspheader = 0;
3464
3465  #ifdef MOD_GZIP_DEBUG1
3466  mod_gzip_printf( "%s: ",cn); 
3467  #endif
3468
3469  #ifdef MOD_GZIP_DEBUG1
3470
3471  if ( nconf->is_on_set ) 
3472    {
3473     merged_config->is_on = nconf->is_on;
3474
3475     ch1='!'; 
3476    }
3477  else 
3478    {
3479     merged_config->is_on = pconf->is_on;
3480
3481     ch1='='; 
3482    }
3483
3484  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->is_on              = %ld", cn, ch1, (long) merged_config->is_on );
3485
3486  #else 
3487
3488  merged_config->is_on =
3489  ( nconf->is_on_set) ? nconf->is_on : pconf->is_on;
3490
3491  #endif 
3492
3493  #ifdef MOD_GZIP_DEBUG1
3494
3495  if ( pconf->cmode == nconf->cmode )
3496    {
3497     merged_config->cmode = pconf->cmode;
3498
3499     ch1='='; 
3500    }
3501  else 
3502    {
3503     merged_config->cmode = MOD_GZIP_CONFIG_MODE_COMBO;
3504
3505     ch1='!'; 
3506    }
3507
3508  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->cmode              = %ld", cn, ch1, (long) merged_config->cmode );
3509
3510  #else 
3511
3512  merged_config->cmode =
3513  (pconf->cmode == nconf->cmode) ? pconf->cmode : MOD_GZIP_CONFIG_MODE_COMBO;
3514
3515  #endif 
3516
3517  merged_config->loc = ap_pstrdup( p, nconf->loc );
3518
3519  #ifdef MOD_GZIP_DEBUG1
3520  mod_gzip_printf( "%s: .............. : merged_config->loc                = [%s]", cn, npp(merged_config->loc));
3521  #endif
3522
3523  #ifdef MOD_GZIP_DEBUG1
3524  if ( !nconf->add_header_count_set ) 
3525       { merged_config->add_header_count = pconf->add_header_count; ch1='='; }
3526  else { merged_config->add_header_count = nconf->add_header_count; ch1='!'; }
3527  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->add_header_count   = %ld", cn, ch1, (long) merged_config->add_header_count );
3528  #else
3529  merged_config->add_header_count = ( !nconf->add_header_count_set ) ? pconf->add_header_count : nconf->add_header_count;
3530  #endif
3531
3532  #ifdef MOD_GZIP_DEBUG1
3533  if ( !nconf->keep_workfiles_set ) 
3534       { merged_config->keep_workfiles = pconf->keep_workfiles; ch1='='; }
3535  else { merged_config->keep_workfiles = nconf->keep_workfiles; ch1='!'; }
3536  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->keep_workfiles     = %ld", cn, ch1, (long) merged_config->keep_workfiles );
3537  #else
3538  merged_config->keep_workfiles = ( !nconf->keep_workfiles_set ) ? pconf->keep_workfiles : nconf->keep_workfiles;
3539  #endif
3540
3541  #ifdef MOD_GZIP_CAN_NEGOTIATE
3542  #ifdef MOD_GZIP_DEBUG1
3543  if ( !nconf->can_negotiate_set )
3544       { merged_config->can_negotiate = pconf->can_negotiate; ch1='='; }
3545  else { merged_config->can_negotiate = nconf->can_negotiate; ch1='!'; }
3546  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->can_negotiate      = %ld", cn, ch1, (long) merged_config->can_negotiate );
3547  #else
3548  merged_config->can_negotiate = ( !nconf->can_negotiate_set ) ? pconf->can_negotiate : nconf->can_negotiate;
3549  #endif
3550  #endif
3551
3552  #ifdef MOD_GZIP_DEBUG1
3553  if ( !nconf->dechunk_set ) 
3554       { merged_config->dechunk = pconf->dechunk; ch1='='; }
3555  else { merged_config->dechunk = nconf->dechunk; ch1='!'; }
3556  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->dechunk            = %ld", cn, ch1, (long) merged_config->dechunk );
3557  #else
3558  merged_config->dechunk = ( !nconf->dechunk_set ) ? pconf->dechunk : nconf->dechunk;
3559  #endif
3560
3561  #ifdef MOD_GZIP_DEBUG1
3562  if ( !nconf->min_http_set ) 
3563       { merged_config->min_http = pconf->min_http; ch1='='; }
3564  else { merged_config->min_http = nconf->min_http; ch1='!'; }
3565  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->min_http           = %ld", cn, ch1, (long) merged_config->min_http );
3566  #else
3567  merged_config->min_http = ( !nconf->min_http_set ) ? pconf->min_http : nconf->min_http;
3568  #endif
3569
3570  #ifdef MOD_GZIP_DEBUG1
3571  if ( !nconf->minimum_file_size_set ) 
3572       { merged_config->minimum_file_size = pconf->minimum_file_size; ch1='='; }
3573  else { merged_config->minimum_file_size = nconf->minimum_file_size; ch1='!'; }
3574  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->minimum_file_size  = %ld", cn, ch1, (long) merged_config->minimum_file_size );
3575  #else
3576  merged_config->minimum_file_size = ( !nconf->minimum_file_size_set ) ? pconf->minimum_file_size : nconf->minimum_file_size;
3577  #endif
3578
3579  #ifdef MOD_GZIP_DEBUG1
3580  if ( !nconf->maximum_file_size_set ) 
3581       { merged_config->maximum_file_size = pconf->maximum_file_size; ch1='='; }
3582  else { merged_config->maximum_file_size = nconf->maximum_file_size; ch1='!'; }
3583  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->maximum_file_size  = %ld", cn, ch1, (long) merged_config->maximum_file_size );
3584  #else
3585  merged_config->maximum_file_size = ( !nconf->maximum_file_size_set ) ? pconf->maximum_file_size : nconf->maximum_file_size;
3586  #endif
3587
3588  #ifdef MOD_GZIP_DEBUG1
3589  if ( !nconf->maximum_inmem_size_set ) 
3590       { merged_config->maximum_inmem_size = pconf->maximum_inmem_size; ch1='='; }
3591  else { merged_config->maximum_inmem_size = nconf->maximum_inmem_size; ch1='!'; }
3592  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->maximum_inmem_size = %ld", cn, ch1, (long) merged_config->maximum_inmem_size );
3593  #else
3594  merged_config->maximum_inmem_size = ( !nconf->maximum_inmem_size_set ) ? pconf->maximum_inmem_size : nconf->maximum_inmem_size;
3595  #endif
3596
3597  #ifdef MOD_GZIP_DEBUG1
3598  if ( !nconf->temp_dir_set ) 
3599       { mod_gzip_strcpy(merged_config->temp_dir,pconf->temp_dir); ch1='='; }
3600  else { mod_gzip_strcpy(merged_config->temp_dir,nconf->temp_dir); ch1='!'; }
3601  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->temp_dir           = [%s]", cn, ch1,npp(merged_config->temp_dir));
3602  #else
3603  if ( !nconf->temp_dir_set ) 
3604       { mod_gzip_strcpy(merged_config->temp_dir,pconf->temp_dir); }
3605  else { mod_gzip_strcpy(merged_config->temp_dir,nconf->temp_dir); }
3606  #endif
3607
3608  #ifdef MOD_GZIP_COMMAND_VERSION_USED
3609  #ifdef MOD_GZIP_DEBUG1
3610  if ( !nconf->command_version_set )
3611       { mod_gzip_strcpy(merged_config->command_version,pconf->command_version); ch1='='; }
3612  else { mod_gzip_strcpy(merged_config->command_version,nconf->command_version); ch1='!'; }
3613  mod_gzip_printf( "%s: pconf %c= nconf : merged_config->command_version    = [%s]", cn, ch1,npp(merged_config->command_version));
3614  #else
3615  if ( !nconf->command_version_set )
3616       { mod_gzip_strcpy(merged_config->command_version,pconf->command_version); }
3617  else { mod_gzip_strcpy(merged_config->command_version,nconf->command_version); }
3618  #endif
3619  #endif
3620
3621  total = 0; 
3622
3623  for ( i=0; i<nconf->imap_total_entries; i++ )
3624     {
3625      memcpy(
3626      &(merged_config->imap[i]),
3627      &(nconf->imap[i]),
3628      mod_gzip_imap_size
3629      );
3630
3631      total++; 
3632
3633      if ( nconf->imap[i].type == MOD_GZIP_IMAP_ISMIME )
3634        {
3635         total_ismime++;
3636        }
3637      else if ( nconf->imap[i].type == MOD_GZIP_IMAP_ISFILE )
3638        {
3639         total_isfile++;
3640        }
3641      else if ( nconf->imap[i].type == MOD_GZIP_IMAP_ISURI )
3642        {
3643         total_isuri++;
3644        }
3645      else if ( nconf->imap[i].type == MOD_GZIP_IMAP_ISHANDLER )
3646        {
3647         total_ishandler++;
3648        }
3649      else if ( nconf->imap[i].type == MOD_GZIP_IMAP_ISREQHEADER )
3650        {
3651         total_isreqheader++;
3652        }
3653      else if ( nconf->imap[i].type == MOD_GZIP_IMAP_ISRSPHEADER )
3654        {
3655         total_isrspheader++;
3656        }
3657     }
3658
3659  for ( i=0; i<pconf->imap_total_entries; i++ )
3660     {
3661      p1 = pconf->imap[i].name;
3662      l1 = mod_gzip_strlen( p1 );
3663
3664      match = -1; 
3665
3666      for ( ii=0; ii<nconf->imap_total_entries; ii++ )
3667         {
3668          p2 = nconf->imap[ii].name;
3669          l2 = nconf->imap[ii].namelen;
3670
3671          if ( l1 == l2 ) 
3672            {
3673             if ( mod_gzip_strncmp( p1, p2, l1 ) == 0 )
3674               {
3675                match = ii; 
3676
3677                break; 
3678               }
3679            }
3680         }
3681
3682      if ( match != -1 )
3683        {
3684        }
3685      else 
3686        {
3687         if ( total < MOD_GZIP_IMAP_MAXNAMES ) 
3688           {
3689            memcpy(
3690            &(merged_config->imap[ total ]),
3691            &(pconf->imap[i]),
3692            mod_gzip_imap_size
3693            );
3694
3695            total++; 
3696
3697            if ( pconf->imap[i].type == MOD_GZIP_IMAP_ISMIME )
3698              {
3699               total_ismime++;
3700              }
3701            else if ( pconf->imap[i].type == MOD_GZIP_IMAP_ISFILE )
3702              {
3703               total_isfile++;
3704              }
3705            else if ( pconf->imap[i].type == MOD_GZIP_IMAP_ISURI )
3706              {
3707               total_isuri++;
3708              }
3709            else if ( pconf->imap[i].type == MOD_GZIP_IMAP_ISHANDLER )
3710              {
3711               total_ishandler++;
3712              }
3713            else if ( pconf->imap[i].type == MOD_GZIP_IMAP_ISREQHEADER )
3714              {
3715               total_isreqheader++;
3716              }
3717            else if ( pconf->imap[i].type == MOD_GZIP_IMAP_ISRSPHEADER )
3718              {
3719               total_isrspheader++;
3720              }
3721           }
3722        }
3723     }
3724
3725  merged_config->imap_total_entries     = total;
3726  merged_config->imap_total_ismime      = total_ismime;
3727  merged_config->imap_total_isfile      = total_isfile;
3728  merged_config->imap_total_isuri       = total_isuri;
3729  merged_config->imap_total_ishandler   = total_ishandler;
3730  merged_config->imap_total_isreqheader = total_isreqheader;
3731  merged_config->imap_total_isrspheader = total_isrspheader;
3732
3733  #ifdef MOD_GZIP_DEBUG1
3734  mod_gzip_printf( "%s: pconf += nconf : merged_config->imap_total_entries     = %ld", cn, (long) merged_config->imap_total_entries );
3735  mod_gzip_printf( "%s: pconf += nconf : merged_config->imap_total_ismime      = %ld", cn, (long) merged_config->imap_total_ismime );
3736  mod_gzip_printf( "%s: pconf += nconf : merged_config->imap_total_isfile      = %ld", cn, (long) merged_config->imap_total_isfile );
3737  mod_gzip_printf( "%s: pconf += nconf : merged_config->imap_total_isuri       = %ld", cn, (long) merged_config->imap_total_isuri );
3738  mod_gzip_printf( "%s: pconf += nconf : merged_config->imap_total_ishandler   = %ld", cn, (long) merged_config->imap_total_ishandler );
3739  mod_gzip_printf( "%s: pconf += nconf : merged_config->imap_total_isreqheader = %ld", cn, (long) merged_config->imap_total_isreqheader );
3740  mod_gzip_printf( "%s: pconf += nconf : merged_config->imap_total_isrspheader = %ld", cn, (long) merged_config->imap_total_isrspheader );
3741  #endif
3742
3743  #ifdef MOD_GZIP_DEBUG1
3744  mod_gzip_printf( "%s: ",cn); 
3745  #endif
3746
3747  return 0;
3748 }
3749
3750 static const char *
3751 mod_gzip_set_is_on( cmd_parms *parms, void *cfg, char *arg )
3752 {
3753  mod_gzip_conf *mgc;
3754
3755  #ifdef MOD_GZIP_DEBUG1
3756  server_rec *srv = parms->server;
3757  char cn[]="mod_gzip_set_is_on()";
3758  #endif
3759
3760  #ifdef MOD_GZIP_DEBUG1
3761
3762  mod_gzip_server_now = srv;
3763
3764  mod_gzip_printf( " ");
3765  mod_gzip_printf( "%s: Entry", cn );
3766  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
3767
3768  #endif
3769
3770  mgc = ( mod_gzip_conf * ) cfg;
3771
3772  #ifdef MOD_GZIP_ALTERNATIVE1
3773
3774  mgc = ( mod_gzip_conf * )
3775  ap_get_module_config(parms->server->module_config, &gzip_module);
3776  #endif
3777
3778  if ( ( arg[0] == 'Y' )||( arg[0] == 'y' ) )
3779    {
3780     mgc->is_on = 1;
3781    }
3782  else
3783    {
3784     mgc->is_on = 0;
3785    }
3786
3787  mgc->is_on_set = 1;
3788
3789  #ifdef MOD_GZIP_DEBUG1
3790  mod_gzip_printf( "%s: mgc->loc             = [%s]", cn, npp(mgc->loc));
3791  mod_gzip_printf( "%s: srv->is_virtual      = %ld",  cn, (long)srv->is_virtual );
3792  mod_gzip_printf( "%s: srv->server_hostname = [%s]", cn, npp(srv->server_hostname));
3793  mod_gzip_printf( "%s: mgc->cmode           = %ld",  cn, (long) mgc->cmode );
3794  mod_gzip_printf( "%s: mgc->is_on           = %ld",  cn, (long) mgc->is_on );
3795  mod_gzip_printf( "%s: mgc->is_on_set       = %ld",  cn, (long) mgc->is_on_set );
3796  #endif
3797
3798  return NULL;
3799 }
3800
3801 static const char *
3802 mod_gzip_set_add_header_count( cmd_parms *parms, void *cfg, char *arg )
3803 {
3804  mod_gzip_conf *mgc;
3805
3806  #ifdef MOD_GZIP_DEBUG1
3807  server_rec *srv = parms->server;
3808  char cn[]="mod_gzip_set_add_header_count()";
3809  #endif
3810
3811  #ifdef MOD_GZIP_DEBUG1
3812  mod_gzip_server_now = srv;
3813  mod_gzip_printf( " ");
3814  mod_gzip_printf( "%s: Entry", cn );
3815  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
3816  #endif
3817
3818  mgc = ( mod_gzip_conf * ) cfg;
3819
3820  if ( ( arg[0] == 'Y' )||( arg[0] == 'y' ) )
3821    {
3822     mgc->add_header_count = 1;
3823    }
3824  else
3825    {
3826     mgc->add_header_count = 0;
3827    }
3828
3829  mgc->add_header_count_set = 1;
3830
3831  #ifdef MOD_GZIP_DEBUG1
3832  mod_gzip_printf( "%s: mgc->loc                  = [%s]",cn, npp(mgc->loc));
3833  mod_gzip_printf( "%s: mgc->cmode                = %ld", cn, (long) mgc->cmode );
3834  mod_gzip_printf( "%s: srv->is_virtual           = %ld", cn, (long)srv->is_virtual );
3835  mod_gzip_printf( "%s: srv->server_hostname      = [%s]",cn, npp(srv->server_hostname));
3836  mod_gzip_printf( "%s: mgc->add_header_count     = %ld", cn,
3837                 (long) mgc->add_header_count );
3838  mod_gzip_printf( "%s: mgc->add_header_count_set = %ld", cn,
3839                 (long) mgc->add_header_count_set );
3840  #endif
3841
3842  return NULL;
3843 }
3844
3845 static const char *
3846 mod_gzip_set_keep_workfiles( cmd_parms *parms, void *cfg, char *arg )
3847 {
3848  mod_gzip_conf *mgc;
3849
3850  #ifdef MOD_GZIP_DEBUG1
3851  server_rec *srv = parms->server;
3852  char cn[]="mod_gzip_set_keep_workfiles()";
3853  #endif
3854
3855  #ifdef MOD_GZIP_DEBUG1
3856  mod_gzip_server_now = srv;
3857  mod_gzip_printf( " ");
3858  mod_gzip_printf( "%s: Entry", cn );
3859  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
3860  #endif
3861
3862  mgc = ( mod_gzip_conf * ) cfg;
3863
3864  if ( ( arg[0] == 'Y' )||( arg[0] == 'y' ) )
3865    {
3866     mgc->keep_workfiles = 1;
3867    }
3868  else
3869    {
3870     mgc->keep_workfiles = 0;
3871    }
3872
3873  mgc->keep_workfiles_set = 1;
3874
3875  #ifdef MOD_GZIP_DEBUG1
3876  mod_gzip_printf( "%s: mgc->loc                = [%s]",cn, npp(mgc->loc) );
3877  mod_gzip_printf( "%s: mgc->cmode              = %ld", cn, (long) mgc->cmode );
3878  mod_gzip_printf( "%s: srv->is_virtual         = %ld", cn, (long)srv->is_virtual );
3879  mod_gzip_printf( "%s: srv->server_hostname    = [%s]",cn, npp(srv->server_hostname) );
3880  mod_gzip_printf( "%s: mgc->keep_workfiles     = %ld", cn,
3881                 (long) mgc->keep_workfiles );
3882  mod_gzip_printf( "%s: mgc->keep_workfiles_set = %ld", cn,
3883                 (long) mgc->keep_workfiles_set );
3884  #endif
3885
3886  return NULL;
3887 }
3888
3889 #ifdef MOD_GZIP_CAN_NEGOTIATE
3890 static const char *
3891 mod_gzip_set_can_negotiate( cmd_parms *parms, void *cfg, char *arg )
3892 {
3893  mod_gzip_conf *mgc;
3894
3895  #ifdef MOD_GZIP_DEBUG1
3896  server_rec *srv = parms->server;
3897  char cn[]="mod_gzip_set_can_negotiate()";
3898  #endif
3899
3900  #ifdef MOD_GZIP_DEBUG1
3901  mod_gzip_server_now = srv;
3902  mod_gzip_printf( " ");
3903  mod_gzip_printf( "%s: Entry", cn );
3904  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
3905  #endif
3906
3907  mgc = ( mod_gzip_conf * ) cfg;
3908
3909  if ( ( arg[0] == 'Y' )||( arg[0] == 'y' ) )
3910    {
3911     mgc->can_negotiate = 1;
3912    }
3913  else
3914    {
3915     mgc->can_negotiate = 0;
3916    }
3917
3918  mgc->can_negotiate_set = 1;
3919
3920  #ifdef MOD_GZIP_DEBUG1
3921  mod_gzip_printf( "%s: mgc->loc                = [%s]",cn, npp(mgc->loc) );
3922  mod_gzip_printf( "%s: mgc->cmode              = %ld", cn, (long) mgc->cmode );
3923  mod_gzip_printf( "%s: srv->is_virtual         = %ld", cn, (long)srv->is_virtual );
3924  mod_gzip_printf( "%s: srv->server_hostname    = [%s]",cn, npp(srv->server_hostname) );
3925  mod_gzip_printf( "%s: mgc->can_negotiate      = %ld", cn,
3926                 (long) mgc->can_negotiate );
3927  mod_gzip_printf( "%s: mgc->can_negotiate_set  = %ld", cn,
3928                 (long) mgc->can_negotiate_set );
3929  #endif
3930
3931  return NULL;
3932 }
3933 #endif
3934
3935 static const char *
3936 mod_gzip_set_dechunk( cmd_parms *parms, void *cfg, char *arg )
3937 {
3938  mod_gzip_conf *mgc;
3939
3940  #ifdef MOD_GZIP_DEBUG1
3941  server_rec *srv = parms->server;
3942  char cn[]="mod_gzip_set_dechunk()";
3943  #endif
3944
3945  #ifdef MOD_GZIP_DEBUG1
3946  mod_gzip_server_now = srv;
3947  mod_gzip_printf( " ");
3948  mod_gzip_printf( "%s: Entry", cn );
3949  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
3950  #endif
3951
3952  mgc = ( mod_gzip_conf * ) cfg;
3953
3954  if ( ( arg[0] == 'Y' )||( arg[0] == 'y' ) )
3955    {
3956     mgc->dechunk = 1;
3957    }
3958  else
3959    {
3960     mgc->dechunk = 0;
3961    }
3962
3963  mgc->dechunk_set = 1;
3964
3965  #ifdef MOD_GZIP_DEBUG1
3966  mod_gzip_printf( "%s: mgc->loc                = [%s]",cn, npp(mgc->loc) );
3967  mod_gzip_printf( "%s: mgc->cmode              = %ld", cn, (long) mgc->cmode );
3968  mod_gzip_printf( "%s: srv->is_virtual         = %ld", cn, (long)srv->is_virtual );
3969  mod_gzip_printf( "%s: srv->server_hostname    = [%s]",cn, npp(srv->server_hostname) );
3970  mod_gzip_printf( "%s: mgc->dechunk            = %ld", cn,
3971                 (long) mgc->dechunk );
3972  mod_gzip_printf( "%s: mgc->dechunk_set        = %ld", cn,
3973                 (long) mgc->dechunk_set );
3974  #endif
3975
3976  return NULL;
3977 }
3978
3979 static const char *
3980 mod_gzip_set_min_http( cmd_parms *parms, void *cfg, char *arg )
3981 {
3982  mod_gzip_conf *mgc;
3983
3984  #ifdef MOD_GZIP_DEBUG1
3985  server_rec *srv = parms->server;
3986  char cn[]="mod_gzip_set_min_http()";
3987  #endif
3988
3989  #ifdef MOD_GZIP_DEBUG1
3990  mod_gzip_server_now = srv;
3991  mod_gzip_printf( " ");
3992  mod_gzip_printf( "%s: Entry", cn );
3993  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
3994  #endif
3995
3996  mgc = ( mod_gzip_conf * ) cfg;
3997
3998  mgc->min_http     = (int) atoi( arg );
3999  mgc->min_http_set = 1;
4000
4001  #ifdef MOD_GZIP_DEBUG1
4002  mod_gzip_printf( "%s: mgc->loc             = [%s]",cn, npp(mgc->loc));
4003  mod_gzip_printf( "%s: mgc->cmode           = %ld", cn, (long) mgc->cmode );
4004  mod_gzip_printf( "%s: srv->is_virtual      = %ld", cn, (long)srv->is_virtual );
4005  mod_gzip_printf( "%s: srv->server_hostname = [%s]",cn, npp(srv->server_hostname));
4006  mod_gzip_printf( "%s: mgc->min_http        = %ld", cn,
4007                 (long) mgc->min_http );
4008  mod_gzip_printf( "%s: mgc->min_http        = %ld", cn,
4009                 (long) mgc->min_http_set );
4010  #endif
4011
4012  return NULL;
4013 }
4014
4015 static const char *
4016 mod_gzip_set_minimum_file_size( cmd_parms *parms, void *cfg, char *arg )
4017 {
4018  mod_gzip_conf *mgc;
4019
4020  #ifdef MOD_GZIP_DEBUG1
4021  server_rec *srv = parms->server;
4022  char cn[]="mod_gzip_set_minimum_file_size()";
4023  #endif
4024
4025  #ifdef MOD_GZIP_DEBUG1
4026  mod_gzip_server_now = srv;
4027  mod_gzip_printf( " ");
4028  mod_gzip_printf( "%s: Entry", cn );
4029  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg));
4030  #endif
4031
4032  mgc = ( mod_gzip_conf * ) cfg;
4033
4034  mgc->minimum_file_size     = (long) atol( arg );
4035  mgc->minimum_file_size_set = 1;
4036
4037  if ( mgc->minimum_file_size < 300 )
4038    {
4039     mgc->minimum_file_size = 300;
4040    }
4041
4042  #ifdef MOD_GZIP_DEBUG1
4043  mod_gzip_printf( "%s: mgc->loc                = [%s]",cn, npp(mgc->loc));
4044  mod_gzip_printf( "%s: mgc->cmode              = %ld", cn, (long) mgc->cmode );
4045  mod_gzip_printf( "%s: srv->is_virtual         = %ld", cn, (long)srv->is_virtual );
4046  mod_gzip_printf( "%s: srv->server_hostname    = [%s]",cn, npp(srv->server_hostname));
4047  mod_gzip_printf( "%s: mgc->minimum_file_size  = %ld", cn,
4048                 (long) mgc->minimum_file_size );
4049  mod_gzip_printf( "%s: mgc->minimum_file_size  = %ld", cn,
4050                 (long) mgc->minimum_file_size_set );
4051  #endif
4052
4053  return NULL;
4054 }
4055
4056 static const char *
4057 mod_gzip_set_maximum_file_size( cmd_parms *parms, void *cfg, char *arg )
4058 {
4059  mod_gzip_conf *mgc;
4060
4061  #ifdef MOD_GZIP_DEBUG1
4062  server_rec *srv = parms->server;
4063  char cn[]="mod_gzip_set_maximum_file_size()";
4064  #endif
4065
4066  #ifdef MOD_GZIP_DEBUG1
4067  mod_gzip_server_now = srv;
4068  mod_gzip_printf( " ");
4069  mod_gzip_printf( "%s: Entry", cn );
4070  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg));
4071  #endif
4072
4073  mgc = ( mod_gzip_conf * ) cfg;
4074
4075  mgc->maximum_file_size     = (long) atol( arg );
4076  mgc->maximum_file_size_set = 1;
4077
4078  #ifdef MOD_GZIP_DEBUG1
4079  mod_gzip_printf( "%s: mgc->loc                = [%s]",cn, npp(mgc->loc));
4080  mod_gzip_printf( "%s: mgc->cmode              = %ld", cn, (long) mgc->cmode );
4081  mod_gzip_printf( "%s: srv->is_virtual         = %ld", cn, (long)srv->is_virtual );
4082  mod_gzip_printf( "%s: srv->server_hostname    = [%s]",cn, npp(srv->server_hostname));
4083  mod_gzip_printf( "%s: mgc->maximum_file_size  = %ld", cn,
4084                 (long) mgc->maximum_file_size );
4085  mod_gzip_printf( "%s: mgc->maximum_file_size  = %ld", cn,
4086                 (long) mgc->maximum_file_size_set );
4087  #endif
4088
4089  return NULL;
4090 }
4091
4092 static const char *
4093 mod_gzip_set_maximum_inmem_size( cmd_parms *parms, void *cfg, char *arg )
4094 {
4095  mod_gzip_conf *mgc;
4096
4097  #ifdef MOD_GZIP_DEBUG1
4098  server_rec *srv = parms->server;
4099  char cn[]="mod_gzip_set_maximum_inmem_size()";
4100  #endif
4101
4102  #ifdef MOD_GZIP_DEBUG1
4103  mod_gzip_server_now = srv;
4104  mod_gzip_printf( " ");
4105  mod_gzip_printf( "%s: Entry", cn );
4106  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
4107  #endif
4108
4109  mgc = ( mod_gzip_conf * ) cfg;
4110
4111  mgc->maximum_inmem_size     = (long) atol(arg);
4112  mgc->maximum_inmem_size_set = 1;
4113
4114  #ifdef MOD_GZIP_DEBUG1
4115  mod_gzip_printf( "%s: mgc->loc                = [%s]",cn, npp(mgc->loc));
4116  mod_gzip_printf( "%s: mgc->cmode              = %ld", cn, (long) mgc->cmode );
4117  mod_gzip_printf( "%s: srv->is_virtual         = %ld", cn, (long)srv->is_virtual );
4118  mod_gzip_printf( "%s: srv->server_hostname    = [%s]",cn, npp(srv->server_hostname));
4119  mod_gzip_printf( "%s: mgc->maximum_inmem_size = %ld", cn,
4120                 (long) mgc->maximum_inmem_size );
4121  mod_gzip_printf( "%s: mgc->maximum_inmem_size = %ld", cn,
4122                 (long) mgc->maximum_inmem_size_set );
4123  #endif
4124
4125  return NULL;
4126 }
4127
4128 static const char *
4129 mod_gzip_set_temp_dir( cmd_parms *parms, void *cfg, char *arg )
4130 {
4131  mod_gzip_conf *mgc;
4132
4133  #ifdef MOD_GZIP_DEBUG1
4134  server_rec *srv = parms->server;
4135  char cn[]="mod_gzip_set_temp_dir()";
4136  #endif
4137
4138  int arglen = 0;
4139  int rc     = 0;
4140
4141  struct stat sbuf;
4142
4143  #ifdef WIN32
4144  char dirsep[]="\\";
4145  #else
4146  char dirsep[]="/";
4147  #endif
4148
4149  #ifdef MOD_GZIP_DEBUG1
4150  mod_gzip_server_now = srv;
4151  mod_gzip_printf( " ");
4152  mod_gzip_printf( "%s: Entry", cn );
4153  #endif
4154
4155  if ( !arg )
4156    {
4157     #ifdef MOD_GZIP_DEBUG1
4158     mod_gzip_printf( "%s: ERROR: 'arg' is NULL...", cn );
4159     mod_gzip_printf( "%s: ERROR: No valid directory supplied.", cn );
4160     #endif
4161
4162     return "mod_gzip_temp_dir: ERROR: No valid directory supplied.";
4163    }
4164
4165  #ifdef MOD_GZIP_DEBUG1
4166  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
4167  #endif
4168
4169  mgc = ( mod_gzip_conf * ) cfg;
4170
4171  arglen = mod_gzip_strlen( arg );
4172
4173  if ( arglen < 256 )
4174    {
4175     mod_gzip_strcpy( mgc->temp_dir, arg );
4176     mgc->temp_dir_set = 1;
4177
4178     if ( arglen > 0 )
4179       {
4180        if (( arglen == 1 ) && ( *arg == 32 ))
4181          {
4182           #ifdef MOD_GZIP_DEBUG1
4183           mod_gzip_printf( "%s: Special ONE SPACE pickup seen.", cn );
4184           mod_gzip_printf( "%s: temp_dir set to NOTHING.", cn );
4185           #endif
4186
4187           mod_gzip_strcpy( mgc->temp_dir, "" );
4188          }
4189        else
4190          {
4191           if ( ( *(mgc->temp_dir+(arglen-1)) != '\\' ) &&
4192                ( *(mgc->temp_dir+(arglen-1)) != '/'  ) )
4193             {
4194              mod_gzip_strcat( mgc->temp_dir, dirsep );
4195             }
4196
4197           rc = stat( mgc->temp_dir, &sbuf );
4198
4199           if ( rc )
4200             {
4201              #ifdef MOD_GZIP_DEBUG1
4202              mod_gzip_printf( "%s: .... stat() call FAILED",cn);
4203              mod_gzip_printf( "%s: Directory name is not valid.",cn);
4204              #endif
4205
4206              return "mod_gzip_temp_dir: ERROR: Directory does not exist.";
4207             }
4208           else
4209             {
4210              #ifdef MOD_GZIP_DEBUG1
4211              mod_gzip_printf( "%s: .... stat() call SUCCEEDED",cn);
4212              mod_gzip_printf( "%s: Directory name appears to be valid.",cn);
4213              #endif
4214             }
4215          }
4216       }
4217
4218     #ifdef MOD_GZIP_DEBUG1
4219     mod_gzip_printf( "%s: mgc->loc             = [%s]", cn, npp(mgc->loc));
4220     mod_gzip_printf( "%s: srv->is_virtual      = %ld",  cn, (long)srv->is_virtual );
4221     mod_gzip_printf( "%s: srv->server_hostname = [%s]", cn, npp(srv->server_hostname));
4222     mod_gzip_printf( "%s: mgc->cmode           = %ld",  cn, (long) mgc->cmode);
4223     mod_gzip_printf( "%s: mgc->temp_dir        = [%s]", cn, npp(mgc->temp_dir));
4224     mod_gzip_printf( "%s: mgc->temp_dir_set    = %d",   cn, mgc->temp_dir_set);
4225     #endif
4226
4227     return NULL;
4228    }
4229  else
4230    {
4231     return "mod_gzip_temp_dir pathname must be less than 256 characters.";
4232    }
4233 }
4234
4235 #ifdef MOD_GZIP_COMMAND_VERSION_USED
4236 static const char *
4237 mod_gzip_set_command_version( cmd_parms *parms, void *cfg, char *arg )
4238 {
4239  mod_gzip_conf *mgc;
4240
4241  #ifdef MOD_GZIP_DEBUG1
4242  server_rec *srv = parms->server;
4243  char cn[]="mod_gzip_set_command_version()";
4244  #endif
4245
4246  int arglen = 0;
4247
4248  #ifdef MOD_GZIP_DEBUG1
4249  mod_gzip_server_now = srv;
4250  mod_gzip_printf( " ");
4251  mod_gzip_printf( "%s: Entry", cn );
4252  #endif
4253
4254  if ( !arg )
4255    {
4256     #ifdef MOD_GZIP_DEBUG1
4257     mod_gzip_printf( "%s: ERROR: 'arg' is NULL...", cn );
4258     mod_gzip_printf( "%s: ERROR: No valid string supplied.", cn );
4259     #endif
4260
4261     return "mod_gzip_command_version: ERROR: No valid string supplied.";
4262    }
4263
4264  #ifdef MOD_GZIP_DEBUG1
4265  mod_gzip_printf( "%s: arg=[%s]", cn, npp(arg) );
4266  #endif
4267
4268  mgc = ( mod_gzip_conf * ) cfg;
4269
4270  arglen = mod_gzip_strlen( arg );
4271
4272  if ( arglen < MOD_GZIP_COMMAND_VERSION_MAXLEN )
4273    {
4274     mod_gzip_strcpy( mgc->command_version, arg );
4275     mgc->command_version_set = 1;
4276
4277     #ifdef MOD_GZIP_DEBUG1
4278     mod_gzip_printf( "%s: mgc->loc                 = [%s]", cn, npp(mgc->loc));
4279     mod_gzip_printf( "%s: srv->is_virtual          = %ld",  cn, (long)srv->is_virtual );
4280     mod_gzip_printf( "%s: srv->server_hostname     = [%s]", cn, npp(srv->server_hostname));
4281     mod_gzip_printf( "%s: mgc->cmode               = %ld",  cn, (long) mgc->cmode);
4282     mod_gzip_printf( "%s: mgc->command_version     = [%s]", cn, npp(mgc->command_version));
4283     mod_gzip_printf( "%s: mgc->command_version_set = %d",   cn, mgc->command_version_set);
4284     #endif
4285
4286     return NULL;
4287    }
4288  else
4289    {
4290     return "mod_gzip_command_version string must be less than 128 characters.";
4291    }
4292 }
4293 #endif
4294
4295 static const char *
4296 mod_gzip_imap_add_item(
4297 cmd_parms     *parms,
4298 mod_gzip_conf *mgc,
4299 char          *a1,
4300 char          *a2,
4301 int            flag1
4302 )
4303 {
4304  int      x;
4305  char    *p1;
4306  int      ignorecase=0;
4307
4308  int      this_include=flag1;
4309  int      this_type=0;
4310  int      this_action=0;
4311  int      this_direction=0;
4312  unsigned this_port=0;
4313  int      this_len1=0;
4314  regex_t *this_pregex=NULL;
4315
4316  char    *regex;
4317
4318  /* Diagnostic only
4319  #define MOD_GZIP_TEST_REGEX1
4320  */
4321  #ifdef  MOD_GZIP_TEST_REGEX1
4322  char string[129];
4323  int  regex_error;
4324  #endif
4325
4326  #ifdef MOD_GZIP_DEBUG1
4327  char cn[]="mod_gzip_imap_add_item()";
4328  #endif
4329
4330  #ifdef MOD_GZIP_DEBUG1
4331
4332  mod_gzip_printf( "%s: Entry", cn );
4333  mod_gzip_printf( "%s: 1 a1=[%s]", cn, npp(a1));
4334  mod_gzip_printf( "%s: 1 a2=[%s]", cn, npp(a2));
4335
4336  mod_gzip_printf( "%s: mgc->imap_total_entries     = %d",
4337                    cn, mgc->imap_total_entries );
4338  mod_gzip_printf( "%s: mgc->imap_total_ismime      = %d",
4339                    cn, mgc->imap_total_ismime );
4340  mod_gzip_printf( "%s: mgc->imap_total_isfile      = %d",
4341                    cn, mgc->imap_total_isfile );
4342  mod_gzip_printf( "%s: mgc->imap_total_isuri       = %d",
4343                    cn, mgc->imap_total_isuri );
4344  mod_gzip_printf( "%s: mgc->imap_total_ishandler   = %d",
4345                    cn, mgc->imap_total_ishandler );
4346  mod_gzip_printf( "%s: mgc->imap_total_isreqheader = %d",
4347                    cn, mgc->imap_total_isreqheader );
4348  mod_gzip_printf( "%s: mgc->imap_total_isrspheader = %d",
4349                    cn, mgc->imap_total_isrspheader );
4350
4351  if ( flag1 == 1 )
4352    {
4353     mod_gzip_printf( "%s: flag1 = %d = INCLUDE", cn, flag1 );
4354    }
4355  else if ( flag1 == 0 )
4356    {
4357     mod_gzip_printf( "%s: flag1 = %d = EXCLUDE", cn, flag1 );
4358    }
4359  else
4360    {
4361     mod_gzip_printf( "%s: flag1 = %d = ??? Unknown value", cn, flag1 );
4362    }
4363
4364  #endif
4365
4366  this_type = MOD_GZIP_IMAP_ISNONE;
4367
4368  if ( mod_gzip_strnicmp( a1, "mime", 4 ) == 0 )
4369    {
4370     this_type = MOD_GZIP_IMAP_ISMIME;
4371
4372     #ifdef MOD_GZIP_DEBUG1
4373     mod_gzip_printf( "%s: this_type = MOD_GZIP_IMAP_ISMIME", cn);
4374     #endif
4375    }
4376  else if ( mod_gzip_strnicmp( a1, "file", 4 ) == 0 )
4377    {
4378     this_type = MOD_GZIP_IMAP_ISFILE;
4379
4380     #ifdef MOD_GZIP_DEBUG1
4381     mod_gzip_printf( "%s: this_type = MOD_GZIP_IMAP_ISFILE",cn);
4382     #endif
4383    }
4384  else if ( mod_gzip_strnicmp( a1, "ur", 2 ) == 0 )
4385    {
4386     /* Allow user to specify EITHER 'uri' or 'url' for this 'type' */
4387
4388     this_type = MOD_GZIP_IMAP_ISURI;
4389
4390     #ifdef MOD_GZIP_DEBUG1
4391     mod_gzip_printf( "%s: this_type = MOD_GZIP_IMAP_ISURI",cn);
4392     #endif
4393    }
4394  else if ( mod_gzip_strnicmp( a1, "hand", 4 ) == 0 )
4395    {
4396     this_type = MOD_GZIP_IMAP_ISHANDLER;
4397
4398     #ifdef MOD_GZIP_DEBUG1
4399     mod_gzip_printf( "%s: this_type = MOD_GZIP_IMAP_ISHANDLER",cn);
4400     #endif
4401    }
4402  else if ( mod_gzip_strnicmp( a1, "reqh", 4 ) == 0 )
4403    {
4404     this_type      = MOD_GZIP_IMAP_ISREQHEADER;
4405     this_direction = MOD_GZIP_REQUEST;
4406
4407     #ifdef MOD_GZIP_DEBUG1
4408     mod_gzip_printf( "%s: this_type      = MOD_GZIP_IMAP_ISREQHEADER",cn);
4409     #endif
4410    }
4411  else if ( mod_gzip_strnicmp( a1, "rsph", 4 ) == 0 )
4412    {
4413     this_type      = MOD_GZIP_IMAP_ISRSPHEADER;
4414     this_direction = MOD_GZIP_RESPONSE;
4415
4416     #ifdef MOD_GZIP_DEBUG1
4417     mod_gzip_printf( "%s: this_type      = MOD_GZIP_IMAP_ISRSPHEADER",cn);
4418     #endif
4419    }
4420
4421  if ( this_type == MOD_GZIP_IMAP_ISNONE )
4422    {
4423     #ifdef MOD_GZIP_DEBUG1
4424     mod_gzip_printf( "%s: this_type = ?? UNKNOWN ??",cn);
4425     mod_gzip_printf( "%s: Exit > return( ERRORSTRING ) >",cn);
4426     #endif
4427
4428     return "mod_gzip: ERROR: Valid item types are mime,file,uri,handler,reqheader or rspheader";
4429    }
4430
4431  p1 = a2;
4432
4433  if ( ( this_type == MOD_GZIP_IMAP_ISREQHEADER ) ||
4434       ( this_type == MOD_GZIP_IMAP_ISRSPHEADER ) )
4435    {
4436     while((*p1!=0)&&(*p1!=':')) { p1++; this_len1++; }
4437
4438     if (*p1==':')
4439       {
4440        if ( this_len1 < 1 )
4441          {
4442           return "mod_gzip: ERROR: Missing HTTP field name.";
4443          }
4444
4445        p1++;
4446       }
4447     else
4448       {
4449        return "mod_gzip: ERROR: Missing HTTP field name. No colon found.";
4450       }
4451
4452     while((*p1!=0)&&(*p1<33)) p1++;
4453    }
4454
4455  regex = p1;
4456
4457  #ifdef MOD_GZIP_DEBUG1
4458  mod_gzip_printf( "%s: regex = [%s]", cn, npp(regex) );
4459  #endif
4460
4461  if ( !*regex )
4462    {
4463     return "mod_gzip: ERROR: Missing regular expression string.";
4464    }
4465
4466  ignorecase = 1;
4467
4468  #ifdef MOD_GZIP_DEBUG1
4469  mod_gzip_printf( "%s: ignorecase = %d",cn,ignorecase);
4470  mod_gzip_printf( "%s: Call ap_pregcomp(%s)...",cn,npp(regex));
4471  #endif
4472
4473  this_pregex =
4474  ap_pregcomp(parms->pool, regex,
4475  (REG_EXTENDED | REG_NOSUB
4476  | (ignorecase ? REG_ICASE : 0)));
4477
4478  if ( this_pregex == NULL )
4479    {
4480     #ifdef MOD_GZIP_DEBUG1
4481     mod_gzip_printf( "%s: .... ap_pregcomp(%s) FAILED...",cn,npp(regex));
4482     mod_gzip_printf( "%s: regex 'pre-compile' FAILED...", cn );
4483     mod_gzip_printf( "%s: Exit > return( ERRORSTRING ) >",cn);
4484     #endif
4485
4486     return "mod_gzip: ERROR: Regular expression compile failed.";
4487    }
4488
4489  #ifdef MOD_GZIP_DEBUG1
4490  mod_gzip_printf( "%s: .... ap_pregcomp(%s) SUCCEEDED...",cn,npp(regex));
4491  mod_gzip_printf( "%s: regex 'pre-compiled' OK", cn );
4492  #endif
4493
4494  #ifdef MOD_GZIP_TEST_REGEX1
4495
4496  if ( ( this_type == MOD_GZIP_IMAP_ISREQHEADER ) ||
4497       ( this_type == MOD_GZIP_IMAP_ISRSPHEADER ) )
4498    {
4499     mod_gzip_strcpy(
4500     string,
4501     "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt; TUCOWS)"
4502     );
4503
4504     #ifdef MOD_GZIP_DEBUG1
4505     mod_gzip_printf( "%s: Call ap_regexec( regex=[%s], string=[%s] )",
4506                    cn, npp(regex), npp(string) );
4507     #endif
4508
4509     regex_error = ap_regexec(this_pregex, string, 0, (regmatch_t *)NULL,0);
4510
4511     #ifdef MOD_GZIP_DEBUG1
4512     if ( regex_error != 0 )
4513     mod_gzip_printf( "%s: regex_error = %d = NO MATCH!", cn, regex_error );
4514     else
4515     mod_gzip_printf( "%s: regex_error = %d = MATCH!", cn, regex_error );
4516     #endif
4517    }
4518
4519  #endif /* MOD_GZIP_TEST_REGEX1 */
4520
4521  this_action = MOD_GZIP_IMAP_STATIC1;
4522
4523  #ifdef FUTURE_USE
4524
4525  if ( ( this_action != MOD_GZIP_IMAP_DYNAMIC1 ) &&
4526       ( this_action != MOD_GZIP_IMAP_DYNAMIC2 ) &&
4527       ( this_action != MOD_GZIP_IMAP_STATIC1  ) )
4528    {
4529     #ifdef MOD_GZIP_DEBUG1
4530     mod_gzip_printf( "%s: this_action = %d = MOD_GZIP_IMAP_??? Unknown action",cn,this_action);
4531     mod_gzip_printf( "%s: return( mod_gzip: ERROR: Unrecognized item 'action'",cn);
4532     #endif
4533
4534     return( "mod_gzip: ERROR: Unrecognized item 'action'" );
4535    }
4536
4537  #endif
4538
4539  if ( mgc->imap_total_entries < MOD_GZIP_IMAP_MAXNAMES )
4540    {
4541     if ( mod_gzip_strlen( a2 ) < MOD_GZIP_IMAP_MAXNAMELEN )
4542       {
4543        x = mgc->imap_total_entries;
4544
4545        p1 = a2;
4546
4547        mod_gzip_strcpy( mgc->imap[x].name, p1 );
4548
4549        mgc->imap[x].namelen = mod_gzip_strlen( mgc->imap[x].name );
4550
4551        mgc->imap[x].include   = this_include;
4552        mgc->imap[x].type      = this_type;
4553        mgc->imap[x].action    = this_action;
4554        mgc->imap[x].direction = this_direction;
4555        mgc->imap[x].port      = this_port;
4556        mgc->imap[x].len1      = this_len1;
4557        mgc->imap[x].pregex    = this_pregex;
4558
4559        mgc->imap_total_entries++;
4560
4561        if ( this_type == MOD_GZIP_IMAP_ISMIME )
4562          {
4563           mgc->imap_total_ismime++;
4564          }
4565        else if ( this_type == MOD_GZIP_IMAP_ISFILE )
4566          {
4567           mgc->imap_total_isfile++;
4568          }
4569        else if ( this_type == MOD_GZIP_IMAP_ISURI )
4570          {
4571           mgc->imap_total_isuri++;
4572          }
4573        else if ( this_type == MOD_GZIP_IMAP_ISHANDLER )
4574          {
4575           mgc->imap_total_ishandler++;
4576          }
4577        else if ( this_type == MOD_GZIP_IMAP_ISREQHEADER )
4578          {
4579           mgc->imap_total_isreqheader++;
4580          }
4581        else if ( this_type == MOD_GZIP_IMAP_ISRSPHEADER )
4582          {
4583           mgc->imap_total_isrspheader++;
4584          }
4585       }
4586     else
4587       {
4588        #ifdef MOD_GZIP_DEBUG1
4589        mod_gzip_printf( "%s: return( mod_gzip: ERROR: Item name is too long",cn);
4590        #endif
4591
4592        return( "mod_gzip: ERROR: Item name is too long" );
4593       }
4594    }
4595  else
4596    {
4597     #ifdef MOD_GZIP_DEBUG1
4598     mod_gzip_printf( "%s: return( mod_gzip: ERROR: Item index is full",cn);
4599     #endif
4600
4601     return( "mod_gzip: ERROR: Item index is full" );
4602    }
4603
4604  #ifdef MOD_GZIP_DEBUG1
4605
4606  mod_gzip_printf( "%s: mgc->imap_total_entries     = %d",
4607                    cn, mgc->imap_total_entries );
4608  mod_gzip_printf( "%s: mgc->imap_total_ismime      = %d",
4609                    cn, mgc->imap_total_ismime );
4610  mod_gzip_printf( "%s: mgc->imap_total_isfile      = %d",
4611                    cn, mgc->imap_total_isfile );
4612  mod_gzip_printf( "%s: mgc->imap_total_isuri       = %d",
4613                    cn, mgc->imap_total_isuri );
4614  mod_gzip_printf( "%s: mgc->imap_total_ishandler   = %d",
4615                    cn, mgc->imap_total_ishandler );
4616  mod_gzip_printf( "%s: mgc->imap_total_isreqheader = %d",
4617                    cn, mgc->imap_total_isreqheader );
4618  mod_gzip_printf( "%s: mgc->imap_total_isrspheader = %d",
4619                    cn, mgc->imap_total_isrspheader );
4620
4621  mod_gzip_printf( "%s: Exit > return( NULL ) >",cn);
4622
4623  #endif
4624
4625  return NULL;
4626 }
4627
4628 static const char *
4629 mod_gzip_set_item_include( cmd_parms *parms, void *cfg, char *a1, char *a2 )
4630 {
4631  mod_gzip_conf *mgc;
4632  char *arg;
4633
4634  #ifdef MOD_GZIP_DEBUG1
4635  server_rec *srv = parms->server;
4636  char cn[]="mod_gzip_set_item_include()";
4637  #endif
4638
4639  #ifdef MOD_GZIP_DEBUG1
4640  mod_gzip_server_now = srv;
4641  mod_gzip_printf( " ");
4642  mod_gzip_printf( "%s: Entry", cn );
4643  mod_gzip_printf( "%s: a1=[%s]", cn, npp(a1) );
4644  mod_gzip_printf( "%s: a2=[%s]", cn, npp(a2) );
4645  #endif
4646
4647  arg = a1;
4648
4649  mgc = ( mod_gzip_conf * ) cfg;
4650
4651  #ifdef MOD_GZIP_DEBUG1
4652  mod_gzip_printf( "%s: mgc->loc             = [%s]", cn, npp(mgc->loc));
4653  mod_gzip_printf( "%s: srv->is_virtual      = %ld",  cn, (long)srv->is_virtual );
4654  mod_gzip_printf( "%s: srv->server_hostname = [%s]", cn, npp(srv->server_hostname));
4655  mod_gzip_printf( "%s: mgc->cmode           = %ld",  cn, (long) mgc->cmode );
4656  #endif
4657
4658  return( mod_gzip_imap_add_item( parms, mgc, a1, a2, 1 ) );
4659 }
4660
4661 static const char *
4662 mod_gzip_set_item_exclude( cmd_parms *parms, void *cfg, char *a1, char *a2 )
4663 {
4664  mod_gzip_conf *mgc;
4665  char *arg;
4666
4667  #ifdef MOD_GZIP_DEBUG1
4668  server_rec *srv = parms->server;
4669  char cn[]="mod_gzip_set_item_exclude()";
4670  #endif
4671
4672  #ifdef MOD_GZIP_DEBUG1
4673  mod_gzip_server_now = srv;
4674  mod_gzip_printf( " ");
4675  mod_gzip_printf( "%s: Entry", cn );
4676  mod_gzip_printf( "%s: a1=[%s]", cn, npp(a1));
4677  mod_gzip_printf( "%s: a2=[%s]", cn, npp(a2));
4678  #endif
4679
4680  arg = a1;
4681
4682  mgc = ( mod_gzip_conf * ) cfg;
4683
4684  #ifdef MOD_GZIP_DEBUG1
4685  mod_gzip_printf( "%s: mgc->loc             = [%s]", cn, npp(mgc->loc));
4686  mod_gzip_printf( "%s: srv->is_virtual      = %ld",  cn, (long)srv->is_virtual );
4687  mod_gzip_printf( "%s: srv->server_hostname = [%s]", cn, npp(srv->server_hostname));
4688  mod_gzip_printf( "%s: mgc->cmode           = %ld",  cn, (long) mgc->cmode );
4689  #endif
4690
4691  return( mod_gzip_imap_add_item( parms, mgc, a1, a2, 0 ) );
4692 }
4693
4694 static void *mod_gzip_create_dconfig(
4695 pool *p,
4696 char *dirspec
4697 )
4698 {
4699  mod_gzip_conf *cfg;
4700  char *dname = dirspec;
4701
4702  cfg = (mod_gzip_conf *) ap_pcalloc(p, sizeof(mod_gzip_conf));
4703
4704  cfg->cmode = MOD_GZIP_CONFIG_MODE_DIRECTORY;
4705
4706  dname = (dname != NULL) ? dname : "";
4707
4708  cfg->loc = ap_pstrcat(p, "DIR(", dname, ")", NULL);
4709
4710  mod_gzip_set_defaults1( (mod_gzip_conf *) cfg );
4711
4712  return (void *) cfg;
4713 }
4714
4715 static void *mod_gzip_merge_dconfig(
4716 pool *p,
4717 void *parent_conf,
4718 void *newloc_conf
4719 )
4720 {
4721  mod_gzip_conf *merged_config = (mod_gzip_conf *) ap_pcalloc(p, sizeof(mod_gzip_conf));
4722  mod_gzip_conf *pconf         = (mod_gzip_conf *) parent_conf;
4723  mod_gzip_conf *nconf         = (mod_gzip_conf *) newloc_conf;
4724
4725  mod_gzip_merge1(
4726  ( pool          * ) p,
4727  ( mod_gzip_conf * ) merged_config,
4728  ( mod_gzip_conf * ) pconf,
4729  ( mod_gzip_conf * ) nconf
4730  );
4731
4732  return (void *) merged_config;
4733 }
4734
4735 static void *mod_gzip_create_sconfig(
4736 pool       *p,
4737 server_rec *s
4738 )
4739 {
4740  mod_gzip_conf *cfg;
4741  char *sname = s->server_hostname;
4742
4743  #ifdef MOD_GZIP_DEBUG1
4744  mod_gzip_server_now = s;
4745  #endif
4746
4747  cfg = (mod_gzip_conf *) ap_pcalloc(p, sizeof(mod_gzip_conf));
4748
4749  cfg->cmode = MOD_GZIP_CONFIG_MODE_SERVER;
4750
4751  sname = (sname != NULL) ? sname : "";
4752
4753  cfg->loc = ap_pstrcat(p, "SVR(", sname, ")", NULL);
4754
4755  mod_gzip_set_defaults1( (mod_gzip_conf *) cfg );
4756
4757  return (void *) cfg;
4758 }
4759
4760 static void *mod_gzip_merge_sconfig(
4761 pool *p,
4762 void *parent_conf,
4763 void *newloc_conf
4764 )
4765 {
4766  mod_gzip_conf *merged_config = (mod_gzip_conf *) ap_pcalloc(p, sizeof(mod_gzip_conf));
4767  mod_gzip_conf *pconf         = (mod_gzip_conf *) parent_conf;
4768  mod_gzip_conf *nconf         = (mod_gzip_conf *) newloc_conf;
4769
4770  mod_gzip_merge1(
4771  ( pool          * ) p,
4772  ( mod_gzip_conf * ) merged_config,
4773  ( mod_gzip_conf * ) pconf,
4774  ( mod_gzip_conf * ) nconf
4775  );
4776
4777  return (void *) merged_config;
4778 }
4779
4780 char mod_gzip_command_no_longer_supported[] =
4781 "Configuration directive no longer supported.";
4782
4783 static const char *
4784 mod_gzip_obsolete_command( cmd_parms *parms, void *cfg, char *arg )
4785 {
4786  return mod_gzip_command_no_longer_supported;
4787 }
4788
4789 static const command_rec mod_gzip_cmds[] =
4790 {
4791  {"mod_gzip_on", mod_gzip_set_is_on, NULL, OR_OPTIONS, TAKE1,
4792   "Yes=mod_gzip will handle requests. No=mod_gzip is disabled."},
4793  {"mod_gzip_add_header_count", mod_gzip_set_add_header_count, NULL, OR_OPTIONS, TAKE1,
4794   "Yes=Add header byte counts to Common Log Format output total(s)."},
4795  {"mod_gzip_keep_workfiles", mod_gzip_set_keep_workfiles, NULL, OR_OPTIONS, TAKE1,
4796   "Yes=Keep any work files used. No=Automatically delete any work files used."},
4797  {"mod_gzip_dechunk", mod_gzip_set_dechunk, NULL, OR_OPTIONS, TAKE1,
4798   "Yes=Allow removal of 'Transfer-encoding: chunked' when necessary."},
4799  {"mod_gzip_min_http", mod_gzip_set_min_http, NULL, OR_OPTIONS, TAKE1,
4800   "Minimum HTTP protocol value to support. 1000 = HTTP/1.0  1001 = HTTP/1.1"},
4801  {"mod_gzip_minimum_file_size", mod_gzip_set_minimum_file_size, NULL, OR_OPTIONS, TAKE1,
4802   "Minimum size ( bytes ) of a file eligible for compression"},
4803  {"mod_gzip_maximum_file_size", mod_gzip_set_maximum_file_size, NULL, OR_OPTIONS, TAKE1,
4804   "Maximum size ( bytes ) of a file eligible for compression"},
4805  {"mod_gzip_maximum_inmem_size", mod_gzip_set_maximum_inmem_size, NULL, OR_OPTIONS, TAKE1,
4806   "Maximum size ( bytes ) to use for in-memory compression."},
4807  {"mod_gzip_temp_dir", mod_gzip_set_temp_dir, NULL, OR_OPTIONS, TAKE1,
4808   "The directory to use for work files and compression cache"},
4809  {"mod_gzip_item_include", mod_gzip_set_item_include, NULL, OR_OPTIONS, TAKE2,
4810   "\r\nARG1=[mime,handler,file,uri,reqheader,rspheader] \r\nARG2=[Name of item to INCLUDE in list of things that should be compressed]"},
4811  {"mod_gzip_item_exclude", mod_gzip_set_item_exclude, NULL, OR_OPTIONS, TAKE2,
4812   "\r\nARG1=[mime,handler,file,uri,reqheader,rspheader] \r\nARG2=[Name of item to EXCLUDE from list of things that should be compressed]"},
4813  {"mod_gzip_add_vinfo", mod_gzip_obsolete_command, NULL, OR_OPTIONS, TAKE1,
4814   mod_gzip_command_no_longer_supported },
4815  {"mod_gzip_do_static_files", mod_gzip_obsolete_command, NULL, OR_OPTIONS, TAKE1,
4816   mod_gzip_command_no_longer_supported },
4817  {"mod_gzip_do_cgi", mod_gzip_obsolete_command, NULL, OR_OPTIONS, TAKE1,
4818   mod_gzip_command_no_longer_supported },
4819  {"mod_gzip_verbose_debug", mod_gzip_obsolete_command, NULL, OR_OPTIONS, TAKE1,
4820   mod_gzip_command_no_longer_supported },
4821  {"mod_gzip_post_on", mod_gzip_obsolete_command, NULL, OR_OPTIONS, TAKE1,
4822   mod_gzip_command_no_longer_supported },
4823  #ifdef MOD_GZIP_COMMAND_VERSION_USED
4824  {"mod_gzip_command_version", mod_gzip_set_command_version, NULL, OR_OPTIONS, TAKE1,
4825   "User defined pickup string to use for mod_gzip version command."},
4826  #endif
4827  #ifdef MOD_GZIP_CAN_NEGOTIATE
4828  {"mod_gzip_can_negotiate", mod_gzip_set_can_negotiate, NULL, OR_OPTIONS, TAKE1,
4829   "Yes=Negotiate/send static compressed versions of files  No=Do not negotiate."},
4830  #endif
4831  {NULL}
4832 };
4833
4834 static const handler_rec mod_gzip_handlers[] =
4835 {
4836  {"mod_gzip_handler", mod_gzip_handler},
4837  {CGI_MAGIC_TYPE,     mod_gzip_handler},
4838  {"cgi-script",       mod_gzip_handler},
4839  {"*",                mod_gzip_handler},
4840  {NULL}
4841 };
4842
4843 module MODULE_VAR_EXPORT gzip_module =
4844 {
4845  STANDARD_MODULE_STUFF,
4846  mod_gzip_init,
4847  mod_gzip_create_dconfig,
4848  mod_gzip_merge_dconfig,
4849  mod_gzip_create_sconfig,
4850  mod_gzip_merge_sconfig,
4851  mod_gzip_cmds,
4852  mod_gzip_handlers,
4853  NULL,
4854  NULL,
4855  NULL,
4856  NULL,
4857  mod_gzip_type_checker,
4858  NULL,
4859  NULL,
4860  NULL,
4861  NULL,
4862  NULL,
4863  NULL
4864 };
4865
4866 #ifdef NETWARE
4867 int main(int argc, char *argv[]) 
4868 {
4869     ExitThread(TSR_THREAD, 0);
4870 }
4871 #endif
4872
4873 long mod_gzip_send(
4874 char        *buf,
4875 long         buflen,
4876 request_rec *r
4877 )
4878 {
4879  #ifdef MOD_GZIP_DEBUG1
4880  char cn[]="mod_gzip_send()";
4881  #endif
4882
4883  int  bytes_to_send    = 0;
4884  int  bytes_sent       = 0;
4885  long bytes_left       = buflen;
4886  long total_bytes_sent = 0;
4887
4888  char *p1=buf;
4889  int   p1maxlen=4096; 
4890
4891  #ifdef MOD_GZIP_DEBUG1
4892  #ifdef MOD_GZIP_DEBUG1_SEND1
4893  mod_gzip_printf("%s: Entry...",cn);
4894  mod_gzip_printf("%s: buf    = %ld",cn,(long)buf);
4895  mod_gzip_printf("%s: buflen = %ld",cn,(long)buflen);
4896  mod_gzip_printf("%s: r      = %ld",cn,(long)r);
4897  #endif
4898  #endif
4899
4900  if ( !buf    ) return 0;
4901  if ( !buflen ) return 0;
4902  if ( !r      ) return 0;
4903
4904  for (;;) 
4905     {
4906      if ( bytes_left <= 0 )
4907        {
4908         break;
4909        }
4910
4911      bytes_to_send = p1maxlen; 
4912
4913      if ( bytes_to_send > bytes_left )
4914        {
4915         bytes_to_send = bytes_left; 
4916        }
4917
4918      #ifdef MOD_GZIP_DEBUG1
4919      #ifdef MOD_GZIP_DEBUG1_SEND1
4920      mod_gzip_printf("%s: Call ap_rwrite(bytes_to_send=%d)...",cn,bytes_to_send);
4921      #endif
4922      #endif
4923
4924      bytes_sent =
4925      ap_rwrite(
4926      p1,
4927      bytes_to_send,
4928      (request_rec *) r
4929      );
4930
4931      #ifdef MOD_GZIP_DEBUG1
4932      #ifdef MOD_GZIP_DEBUG1_SEND1
4933      mod_gzip_printf("%s: Back ap_rwrite(bytes_to_send=%d)...",cn,bytes_to_send);
4934      mod_gzip_printf("%s: bytes_sent = %d",cn,bytes_sent);
4935      #endif
4936      #endif
4937
4938      if ( bytes_sent > 0 )
4939        {
4940         total_bytes_sent += bytes_sent;
4941         bytes_left       -= bytes_sent;
4942         p1               += bytes_sent;
4943        }
4944      else
4945        {
4946         break;
4947        }
4948     }
4949
4950  #ifdef MOD_GZIP_DEBUG1
4951  mod_gzip_printf("%s: Done > return( total_bytes_sent = %ld ) >",
4952                           cn, (long) total_bytes_sent);
4953  #endif
4954
4955  return( total_bytes_sent );
4956 }
4957
4958 int mod_gzip_redir1_handler(
4959 request_rec   *r,
4960 mod_gzip_conf *dconf
4961 )
4962 {
4963  #ifdef MOD_GZIP_DEBUG1
4964  char cn[]="mod_gzip_redir1_handler()";
4965  #endif
4966
4967  int rc          = 0;
4968  int status      = 0;
4969  int pid         = 0;
4970  int save_socket = 0;
4971
4972  int   dconf__keep_workfiles = 0;
4973  char *dconf__temp_dir       = 0;
4974
4975  char tempfile_redir1[ MOD_GZIP_MAX_PATH_LEN + 2 ];
4976
4977  #ifdef WIN32
4978  int save_flags = 0;
4979  #endif
4980
4981  #ifdef MOD_GZIP_DEBUG1
4982  mod_gzip_printf( " " );
4983  mod_gzip_printf( "%s: Entry...",cn);
4984  #endif
4985
4986  tempfile_redir1[0] = 0;
4987
4988  dconf__keep_workfiles = dconf->keep_workfiles;
4989  dconf__temp_dir       = dconf->temp_dir;
4990
4991  #ifdef MOD_GZIP_DEBUG1
4992  mod_gzip_printf( "%s: r->proxyreq           = %d",   cn, (int) r->proxyreq );
4993  mod_gzip_printf( "%s: dconf__keep_workfiles = %d",   cn, (int) dconf__keep_workfiles );
4994  mod_gzip_printf( "%s: dconf__temp_dir       = [%s]", cn, npp(dconf__temp_dir));
4995  #endif
4996
4997  ap_table_setn( r->notes,
4998  "mod_gzip_running",ap_pstrdup(r->pool,"1"));
4999
5000  #ifdef MOD_GZIP_DEBUG1
5001  mod_gzip_printf( "%s: r->notes->mod_gzip_running set to '1'",cn);
5002  #endif
5003
5004  pid = getpid();
5005
5006  #ifdef MOD_GZIP_DEBUG1
5007  mod_gzip_printf( "%s: Saving 'r->connection->client->fd' value to 'save_socket' stack variable...",cn);
5008  #endif
5009
5010  save_socket = (int) r->connection->client->fd;
5011
5012  #ifdef MOD_GZIP_DEBUG1
5013  mod_gzip_printf( "%s: 'r->connection->client->fd' saved to 'save_socket' stack variable",cn);
5014  #endif
5015
5016  #ifdef MOD_GZIP_DEBUG1
5017  mod_gzip_printf( "%s: Creating 'tempfile_redir1' string now...",cn);
5018  #endif
5019
5020  mod_gzip_create_unique_filename(
5021  dconf__temp_dir,
5022  (char *) tempfile_redir1,
5023  MOD_GZIP_MAX_PATH_LEN
5024  );
5025
5026  #ifdef MOD_GZIP_DEBUG1
5027  mod_gzip_printf( "%s: tempfile_redir1 = [%s]",cn,npp(tempfile_redir1));
5028  mod_gzip_printf( "%s: Call mod_gzip_dyn1_getfdo1(%s)...",cn,npp(tempfile_redir1));
5029  #endif
5030
5031  status = mod_gzip_dyn1_getfdo1( r, tempfile_redir1 );
5032
5033  #ifdef MOD_GZIP_DEBUG1
5034  mod_gzip_printf( "%s: Back mod_gzip_dyn1_getfdo1(%s)...",cn,npp(tempfile_redir1));
5035  #endif
5036
5037  if ( status != OK )
5038    {
5039     #ifdef MOD_GZIP_DEBUG1
5040     mod_gzip_printf( "%s: Back mod_gzip_dyn1_getfdo1(%s)...",cn,npp(tempfile_redir1));
5041     mod_gzip_printf( "%s: .... mod_gzip_dyn1_getfdo1() call FAILED",cn);
5042     mod_gzip_printf( "%s: status = %d",cn,status);
5043     #endif
5044
5045     #ifdef WIN32
5046     ap_log_error( "",0,APLOG_NOERRNO|APLOG_WARNING, r->server,
5047     "mod_gzip: ERROR: CreateFile(%s) in dyn1_getfdo1",tempfile_redir1);
5048     #else
5049     ap_log_error( "",0,APLOG_NOERRNO|APLOG_WARNING, r->server,
5050     "mod_gzip: ERROR: fopen(%s) in dyn1_getfdo1",tempfile_redir1);
5051     #endif
5052
5053     ap_log_error( "",0,APLOG_NOERRNO|APLOG_WARNING, r->server,
5054     "mod_gzip: ERROR: %s",mod_gzip_check_permissions);
5055
5056     #ifdef MOD_GZIP_USES_APACHE_LOGS
5057     ap_table_setn( r->notes,
5058     "mod_gzip_result",ap_pstrdup(r->pool,"DECLINED:DYN1_OPENFAIL_BODY"));
5059     #endif
5060
5061     #ifdef MOD_GZIP_DEBUG1
5062     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
5063     #endif
5064
5065     return DECLINED;
5066    }
5067  else
5068    {
5069     #ifdef MOD_GZIP_DEBUG1
5070     mod_gzip_printf( "%s: Back mod_gzip_dyn1_getfdo1(%s)...",cn,npp(tempfile_redir1));
5071     mod_gzip_printf( "%s: .... mod_gzip_dyn1_getfdo1() call SUCCEEDED",cn);
5072     #endif
5073    }
5074
5075  #ifdef WIN32
5076
5077  #ifdef MOD_GZIP_DEBUG1
5078  mod_gzip_printf( "%s: WIN32: Saves/restores B_SOCKET flag as well...",cn);
5079  mod_gzip_printf( "%s: WIN32: Saving 'r->connection->client->flags' value to 'save_flags' stack variable...",cn);
5080  #endif
5081
5082  save_flags = (int) r->connection->client->flags;
5083
5084  #ifdef MOD_GZIP_DEBUG1
5085
5086  mod_gzip_printf( "%s: WIN32: r->connection->client->flags = %ld",  cn, (long) r->connection->client->flags );
5087
5088  if ( r->connection->client->flags & B_SOCKET )
5089    {
5090     mod_gzip_printf( "%s: WIN32: r->connection->client->flags & B_SOCKET is TRUE",cn);
5091    }
5092  else
5093    {
5094     mod_gzip_printf( "%s: WIN32: r->connection->client->flags & B_SOCKET is FALSE",cn);
5095    }
5096
5097  #endif
5098
5099  if ( r->connection->client->flags & B_SOCKET )
5100    {
5101     #ifdef MOD_GZIP_DEBUG1
5102     mod_gzip_printf( "%s: WIN32: Clearing B_SOCKET flag now....", cn);
5103     mod_gzip_printf( "%s: WIN32: r->connection->client->flags &= ~B_SOCKET", cn);
5104     #endif
5105
5106     r->connection->client->flags &= ~B_SOCKET;
5107    }
5108  else
5109    {
5110     #ifdef MOD_GZIP_DEBUG1
5111     mod_gzip_printf( "%s: WIN32: B_SOCKET flag not present...", cn);
5112     mod_gzip_printf( "%s: WIN32: No action was taken...", cn);
5113     #endif
5114    }
5115
5116  #ifdef MOD_GZIP_DEBUG1
5117
5118  mod_gzip_printf( "%s: WIN32: r->connection->client->flags = %ld",  cn, (long) r->connection->client->flags );
5119
5120  if ( r->connection->client->flags & B_SOCKET )
5121    {
5122     mod_gzip_printf( "%s: WIN32: r->connection->client->flags & B_SOCKET is TRUE",cn);
5123    }
5124  else
5125    {
5126     mod_gzip_printf( "%s: WIN32: r->connection->client->flags & B_SOCKET is FALSE",cn);
5127    }
5128
5129  #endif
5130
5131  #endif
5132
5133  #ifdef MOD_GZIP_DEBUG1
5134  mod_gzip_printf( "%s: Call ap_internal_redirect(r->unparsed_uri=[%s])...",
5135                    cn,npp(r->unparsed_uri));
5136  mod_gzip_printf( " " );
5137  #endif
5138
5139  ap_internal_redirect( r->unparsed_uri, r );
5140
5141  #ifdef MOD_GZIP_DEBUG1
5142  mod_gzip_printf( " " );
5143  mod_gzip_printf( "%s: Back ap_internal_redirect(r->unparsed_uri=[%s])...",
5144                    cn,npp(r->unparsed_uri));
5145  #endif
5146
5147  #ifdef MOD_GZIP_DEBUG1
5148  mod_gzip_printf( "%s: Before... ap_rflush()...",cn);
5149  mod_gzip_printf( "%s: r->connection->client->outcnt     = %ld",
5150              cn,(long) r->connection->client->outcnt );
5151  mod_gzip_printf( "%s: r->connection->client->bytes_sent = %ld",
5152              cn,(long) r->connection->client->bytes_sent );
5153  mod_gzip_printf( "%s: Call..... ap_rflush()...",cn);
5154  #endif
5155
5156  ap_rflush(r);
5157
5158  #ifdef MOD_GZIP_DEBUG1
5159  mod_gzip_printf( "%s: Back..... ap_rflush()...",cn);
5160  mod_gzip_printf( "%s: After.... ap_rflush()...",cn);
5161  mod_gzip_printf( "%s: r->connection->client->outcnt     = %ld",
5162              cn,(long) r->connection->client->outcnt );
5163  mod_gzip_printf( "%s: r->connection->client->bytes_sent = %ld",
5164              cn,(long) r->connection->client->bytes_sent );
5165  #endif
5166
5167  #ifdef WIN32
5168
5169  if ( r->connection->client->hFH )
5170    {
5171     #ifdef MOD_GZIP_DEBUG1
5172     mod_gzip_printf( "%s: WIN32: Call CloseHandle( r->connection->client->hFH )...",cn);
5173     #endif
5174
5175     CloseHandle( r->connection->client->hFH );
5176
5177     #ifdef MOD_GZIP_DEBUG1
5178     mod_gzip_printf( "%s: WIN32: Back CloseHandle( r->connection->client->hFH )...",cn);
5179     #endif
5180
5181     r->connection->client->hFH = 0;
5182
5183     #ifdef MOD_GZIP_DEBUG1
5184     mod_gzip_printf( "%s: WIN32: r->connection->client->hFH reset to ZERO",cn);
5185     #endif
5186    }
5187  else
5188    {
5189     #ifdef MOD_GZIP_DEBUG1
5190     mod_gzip_printf( "%s: WIN32: r->connection->client->hFH already closed.",cn);
5191     mod_gzip_printf( "%s: WIN32: No CloseFile() call was necessary at this point.",cn);
5192     #endif
5193    }
5194
5195  #else
5196
5197  #ifdef MOD_GZIP_DEBUG1
5198  mod_gzip_printf( "%s: UNIX: Call close( r->connection->client->fd )...",cn);
5199  #endif
5200
5201  close( r->connection->client->fd );
5202
5203  #ifdef MOD_GZIP_DEBUG1
5204  mod_gzip_printf( "%s: UNIX: Back close( r->connection->client->fd )...",cn);
5205  #endif
5206
5207  #endif
5208
5209  r->connection->client->fd = (int) save_socket;
5210
5211  #ifdef MOD_GZIP_DEBUG1
5212  mod_gzip_printf( "%s: 'r->connection->client->fd' RESTORED from 'save_socket' stack variable",cn);
5213  #endif
5214
5215  #ifdef WIN32
5216
5217  r->connection->client->flags = (int) save_flags;
5218
5219  #ifdef MOD_GZIP_DEBUG1
5220  mod_gzip_printf( "%s: WIN32: r->connection->client->flags RESTORED from 'save_flags' stack variable...",cn);
5221  #endif
5222
5223  #ifdef MOD_GZIP_DEBUG1
5224
5225  mod_gzip_printf( "%s: WIN32: r->connection->client->flags = %ld",  cn, (long) r->connection->client->flags );
5226
5227  if ( r->connection->client->flags & B_SOCKET )
5228    {
5229     mod_gzip_printf( "%s: WIN32: r->connection->client->flags & B_SOCKET is TRUE",cn);
5230    }
5231  else
5232    {
5233     mod_gzip_printf( "%s: WIN32: r->connection->client->flags & B_SOCKET is FALSE",cn);
5234    }
5235
5236  #endif
5237
5238  #endif
5239
5240  r->connection->client->bytes_sent = 0;
5241  r->connection->client->outcnt     = 0;
5242
5243  #ifdef MOD_GZIP_DEBUG1
5244  mod_gzip_printf( "%s: Connection byte counts have been RESET...",cn);
5245  mod_gzip_printf( "%s: r->connection->client->outcnt     = %ld",
5246                    cn, r->connection->client->outcnt );
5247  mod_gzip_printf( "%s: r->connection->client->bytes_sent = %ld",
5248                    cn, r->connection->client->bytes_sent );
5249
5250  mod_gzip_printf( "%s: r->bytes_sent = %ld",cn,(long)r->bytes_sent);
5251
5252  if ( r->next )
5253    {
5254     mod_gzip_printf( "%s: r->next->bytes_sent = %ld",cn,(long)r->next->bytes_sent);
5255    }
5256  if ( r->prev )
5257    {
5258     mod_gzip_printf( "%s: r->prev->bytes_sent = %ld",cn,(long)r->prev->bytes_sent);
5259    }
5260
5261  #endif
5262
5263  #ifdef MOD_GZIP_DEBUG1
5264  mod_gzip_printf( "%s: Call mod_gzip_sendfile2(tempfile_redir1=[%s])...",
5265                    cn, npp(tempfile_redir1));
5266  #endif
5267
5268  rc = (int)
5269  mod_gzip_sendfile2(
5270  r,
5271  dconf,
5272  tempfile_redir1
5273  );
5274
5275  #ifdef MOD_GZIP_DEBUG1
5276
5277  mod_gzip_printf( "%s: Back mod_gzip_sendfile2(tempfile_redir1=[%s])...",
5278                    cn, npp(tempfile_redir1));
5279
5280  if ( rc == OK )
5281    {
5282     mod_gzip_printf( "%s: rc = %d OK", cn, (int) rc);
5283    }
5284  else if ( rc == DECLINED )
5285    {
5286     mod_gzip_printf( "%s: rc = %d DECLINED", cn, (int) rc );
5287    }
5288  else
5289    {
5290     mod_gzip_printf( "%s: rc = %d ( HTTP ERROR CODE? )", cn, (int) rc );
5291    }
5292
5293  #endif
5294
5295  if ( rc != OK )
5296    {
5297     #ifdef MOD_GZIP_DEBUG1
5298     mod_gzip_printf( "%s: mod_gzip_sendfile2() call FAILED",cn);
5299     #endif
5300    }
5301  else
5302    {
5303     #ifdef MOD_GZIP_DEBUG1
5304     mod_gzip_printf( "%s: mod_gzip_sendfile2() call SUCCEEDED",cn);
5305     #endif
5306    }
5307
5308  #ifdef MOD_GZIP_DEBUG1
5309  mod_gzip_printf( "%s: r->connection->client->outcnt     = %ld",
5310                    cn, r->connection->client->outcnt );
5311  mod_gzip_printf( "%s: r->connection->client->bytes_sent = %ld",
5312                    cn, r->connection->client->bytes_sent );
5313  mod_gzip_printf( "%s: Sum of the 2..................... = %ld",
5314                    cn, r->connection->client->outcnt +
5315                        r->connection->client->bytes_sent );
5316  #endif
5317
5318  #ifdef MOD_GZIP_DEBUG1
5319  mod_gzip_printf( "%s: FINAL CLEANUP...",cn);
5320  mod_gzip_printf( "%s: dconf__keep_workfiles = %d",
5321                    cn, dconf__keep_workfiles );
5322  #endif
5323
5324  if ( !dconf__keep_workfiles )
5325    {
5326     #ifdef MOD_GZIP_DEBUG1
5327     mod_gzip_printf( "%s: Call mod_gzip_delete_file(tempfile_redir1=[%s])...",
5328                       cn, npp(tempfile_redir1));
5329     #endif
5330
5331     mod_gzip_delete_file( r, tempfile_redir1 );
5332
5333     #ifdef MOD_GZIP_DEBUG1
5334     mod_gzip_printf( "%s: Back mod_gzip_delete_file(tempfile_redir1=[%s])...",
5335                       cn, npp(tempfile_redir1));
5336     #endif
5337    }
5338
5339  #ifdef MOD_GZIP_DEBUG1
5340  mod_gzip_printf( "%s: Exit > return( OK ) >",cn,(int)rc);
5341  #endif
5342
5343  return OK;
5344 }
5345
5346 int mod_gzip_dyn1_getfdo1(
5347 request_rec *r,
5348 char        *filename
5349 )
5350 {
5351  int status      = OK;
5352  int temp_fd     = 0;
5353  int open_failed = 0;
5354
5355  #ifdef WIN32
5356  HANDLE temp_fdhan;
5357  #endif
5358
5359  #ifdef MOD_GZIP_DEBUG1
5360  char cn[]="mod_gzip_dyn1_getfdo1()";
5361  #endif
5362
5363  #ifdef MOD_GZIP_DEBUG1
5364  mod_gzip_printf( "%s: Entry...",cn);
5365  mod_gzip_printf( "%s: filename=[%s]",cn,npp(filename));
5366  #endif
5367
5368  #ifdef MOD_GZIP_DYN1_GETFDO1_ERROR_TEST1
5369
5370  status = DECLINED;
5371  #ifdef MOD_GZIP_DEBUG1
5372  mod_gzip_printf( "%s: ** TEST ** 'status' forced to 'DECLINED'...",cn);
5373  #endif
5374  return DECLINED;
5375  #endif
5376
5377  #ifdef WIN32
5378
5379  #ifdef MOD_GZIP_DEBUG1
5380  mod_gzip_printf( "%s: WIN32: Call CreateFile(filename=[%s],GENERIC_WRITE, FILE_SHARE_WRITE )...",
5381                    cn, npp(filename));
5382  #endif
5383
5384  temp_fdhan =
5385  CreateFile(
5386  filename,
5387  GENERIC_WRITE,
5388  FILE_SHARE_WRITE,
5389  NULL,
5390  CREATE_ALWAYS,
5391  FILE_ATTRIBUTE_NORMAL,
5392  NULL
5393  );
5394
5395  if ( temp_fdhan == INVALID_HANDLE_VALUE )
5396    {
5397     open_failed = 1;
5398    }
5399
5400  #else
5401
5402  #ifdef MOD_GZIP_DEBUG1
5403  mod_gzip_printf( "%s: UNIX: Call open(filename=[%s],O_RDWR|O_CREAT|O_TRUNC,S_IRWXU)...",
5404                    cn, npp(filename));
5405  #endif
5406
5407  if ((temp_fd = open(filename,O_RDWR|O_CREAT|O_TRUNC,S_IRWXU)) == -1)
5408    {
5409     open_failed = 1;
5410    }
5411
5412  #endif
5413
5414  if ( open_failed )
5415    {
5416     #ifdef MOD_GZIP_DEBUG1
5417
5418     #ifdef WIN32
5419     mod_gzip_printf( "%s: WIN32: .... CreateFile() call FAILED", cn );
5420     mod_gzip_printf( "%s: WIN32: temp_fd = %d", cn, temp_fd );
5421     #else
5422     mod_gzip_printf( "%s: UNIX: .... open() call FAILED", cn );
5423     mod_gzip_printf( "%s: UNIX: temp_fd = %d", cn, temp_fd );
5424     #endif
5425
5426     #endif
5427
5428     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
5429     "mod_gzip: ERROR: Couldn't create a file descriptor at HTTP : %s", filename);
5430
5431     #ifdef MOD_GZIP_DEBUG1
5432     mod_gzip_printf( "%s: Exit > return( HTTP_INTERNAL_SERVER_ERROR ) >",cn);
5433     #endif
5434
5435     return HTTP_INTERNAL_SERVER_ERROR;
5436    }
5437
5438  #ifdef MOD_GZIP_DEBUG1
5439
5440  #ifdef WIN32
5441
5442  mod_gzip_printf( "%s: WIN32: .... CreateFile() call SUCCEEDED", cn );
5443  mod_gzip_printf( "%s: WIN32: temp_fdhan = (WIN32 HANDLE) %d", cn, (int) temp_fdhan );
5444
5445  #else
5446
5447  mod_gzip_printf( "%s: UNIX: .... open() call SUCCEEDED", cn );
5448  mod_gzip_printf( "%s: UNIX: temp_fd = (UNIX int) %d", cn, temp_fd );
5449
5450  #endif
5451
5452  #endif
5453
5454  #ifdef WIN32
5455
5456  r->connection->client->fd  = (int) temp_fdhan;
5457  r->connection->client->hFH = (HANDLE) temp_fdhan;
5458
5459  #ifdef MOD_GZIP_DEBUG1
5460
5461  mod_gzip_printf( "%s: WIN32: r->connection->client->fd    = (int   ) %d",  cn, (int ) r->connection->client->fd );
5462  mod_gzip_printf( "%s: WIN32: r->connection->client->hFH   = (HANDLE) %ld", cn, (long) r->connection->client->hFH );
5463  mod_gzip_printf( "%s: WIN32: r->connection->client->flags = %ld",  cn, (long) r->connection->client->flags );
5464
5465  if ( r->connection->client->flags & B_SOCKET )
5466    {
5467     mod_gzip_printf( "%s: WIN32: r->connection->client->flags & B_SOCKET is TRUE",cn);
5468    }
5469  else
5470    {
5471     mod_gzip_printf( "%s: WIN32: r->connection->client->flags & B_SOCKET is FALSE",cn);
5472    }
5473
5474  #endif
5475
5476  #else
5477
5478  r->connection->client->fd = temp_fd;
5479
5480  #ifdef MOD_GZIP_DEBUG1
5481  mod_gzip_printf( "%s: UNIX: r->connection->client->fd = %d", cn, r->connection->client->fd );
5482  #endif
5483
5484  #endif
5485
5486  #ifdef MOD_GZIP_DEBUG1
5487  mod_gzip_printf( "%s: Exit > return( status=%d ) >", cn, status );
5488  #endif
5489
5490  return status;
5491 }
5492
5493 long mod_gzip_sendfile1(
5494 request_rec *r,
5495 char        *input_filename,
5496 FILE        *ifh_passed,
5497 long         starting_offset
5498 )
5499 {
5500  FILE *ifh;
5501  int   rc;
5502  int   err;
5503  int   bytesread;
5504  int   byteswritten;
5505  long  total_byteswritten=0;
5506
5507  #define   MOD_GZIP_SENDFILE1_BUFFER_SIZE 4096
5508  char tmp[ MOD_GZIP_SENDFILE1_BUFFER_SIZE + 16 ];
5509
5510  #ifdef MOD_GZIP_DEBUG1
5511  char cn[]="mod_gzip_sendfile1()";
5512  #endif
5513
5514  #ifdef MOD_GZIP_DEBUG1
5515  mod_gzip_printf( "%s: Entry...",cn);
5516  mod_gzip_printf( "%s: r               = %ld", cn,(long)r);
5517  mod_gzip_printf( "%s: input_filename  = [%s]",cn,npp(input_filename));
5518  mod_gzip_printf( "%s: ifh_passed      = %ld", cn,(long) ifh_passed);
5519  mod_gzip_printf( "%s: starting_offset = %ld", cn,(long) starting_offset);
5520  #endif
5521
5522  if ( !r ) return 0;
5523  if ( ( !ifh_passed ) && ( !input_filename ) ) return 0;
5524
5525  if ( ifh_passed ) 
5526    {
5527     #ifdef MOD_GZIP_DEBUG1
5528     mod_gzip_printf( "%s: 'ifh_passed' is valid",cn);
5529     mod_gzip_printf( "%s: 'ifh' set to 'ifh_passed'...",cn);
5530     mod_gzip_printf( "%s: fopen() was NOT called...",cn);
5531     #endif
5532
5533     ifh = ifh_passed;
5534    }
5535  else 
5536    {
5537     #ifdef MOD_GZIP_DEBUG1
5538     mod_gzip_printf( "%s: 'ifh_passed' was NULL...",cn);
5539     mod_gzip_printf( "%s: Call fopen(%s)...",cn,npp(input_filename));
5540     #endif
5541
5542     ifh = fopen( input_filename, "rb" ); 
5543
5544     if ( !ifh ) 
5545       {
5546        #ifdef MOD_GZIP_DEBUG1
5547        mod_gzip_printf( "%s: .... fopen() FAILED",cn);
5548        mod_gzip_printf( "%s: Exit > return( 0 ) >",cn);
5549        #endif
5550
5551        return 0;
5552       }
5553
5554     #ifdef MOD_GZIP_DEBUG1
5555     mod_gzip_printf( "%s: .... fopen() SUCCEEDED",cn);
5556     #endif
5557    }
5558
5559  if ( starting_offset > -1 )
5560    {
5561     #ifdef MOD_GZIP_DEBUG1
5562     mod_gzip_printf( "%s: Call fseek(ifh,starting_offset=%ld,0)...",
5563                       cn, (long) starting_offset );
5564     #endif
5565
5566     rc = fseek( ifh, starting_offset, 0 );
5567
5568     if ( rc != 0 ) 
5569       {
5570        #ifdef MOD_GZIP_DEBUG1
5571        mod_gzip_printf( "%s: .... fseek() FAILED",cn);
5572        mod_gzip_printf( "%s: Exit > return( 0 ) >",cn);
5573        #endif
5574
5575        return 0;
5576       }
5577     #ifdef MOD_GZIP_DEBUG1
5578     else
5579       {
5580        mod_gzip_printf( "%s: .... fseek() SUCCEEDED",cn);
5581       }
5582     #endif
5583    }
5584
5585  #ifdef MOD_GZIP_DEBUG1
5586  mod_gzip_printf( "%s: sizeof( tmp ) = %d",cn,sizeof(tmp));
5587  mod_gzip_printf( "%s: MOD_GZIP_SENDFILE1_BUFFER_SIZE = %d",
5588                cn,(int)MOD_GZIP_SENDFILE1_BUFFER_SIZE);
5589  mod_gzip_printf( "%s: Sending file contents now...",cn);
5590  #endif
5591
5592  for (;;)
5593     {
5594      bytesread = fread( tmp, 1, MOD_GZIP_SENDFILE1_BUFFER_SIZE, ifh );
5595
5596      #ifdef MOD_GZIP_DEBUG1
5597      #ifdef MOD_GZIP_DEBUG1_SENDFILE1
5598      mod_gzip_printf( "%s: Back fread(): bytesread=%d",cn,bytesread);
5599      #endif
5600      #endif
5601
5602      if ( bytesread < 1 ) break; 
5603
5604      byteswritten = (int)
5605      mod_gzip_send( tmp, bytesread, r );
5606
5607      if ( byteswritten > 0 )
5608        {
5609         total_byteswritten += byteswritten;
5610        }
5611
5612      #ifdef MOD_GZIP_DEBUG1
5613      #ifdef MOD_GZIP_DEBUG1_SENDFILE1
5614      mod_gzip_printf( "%s: byteswritten = %d",cn,(int)byteswritten);
5615      #endif
5616      #endif
5617
5618      if ( byteswritten != bytesread )
5619        {
5620         err = errno;
5621
5622         #ifdef FUTURE_USE
5623         #if defined(WIN32) || defined(NETWARE)
5624         err = WSAGetLastError();
5625         #else  
5626         err = errno;
5627         #endif 
5628         #endif 
5629
5630         #ifdef MOD_GZIP_DEBUG1
5631         mod_gzip_printf( "%s: TRANSMIT ERROR: bytesread=%d byteswritten=%d err=%d",
5632         cn,(int)bytesread,(int)byteswritten,(int)err );
5633         #endif
5634
5635         #ifdef MOD_GZIP_DEBUG1
5636         mod_gzip_printf( "%s: Breaking out of transmit loop early...",cn);
5637         #endif
5638
5639         break; 
5640        }
5641     }
5642
5643  #ifdef MOD_GZIP_DEBUG1
5644  mod_gzip_printf( "%s: Done sending file contents...",cn);
5645  mod_gzip_printf( "%s: total_byteswritten = %ld",cn,total_byteswritten);
5646  #endif
5647
5648  if ( !ifh_passed )
5649    {
5650     #ifdef MOD_GZIP_DEBUG1
5651     mod_gzip_printf( "%s: 'ifh_passed' was NULL so close the",cn);
5652     mod_gzip_printf( "%s: input file that was opened at the",cn);
5653     mod_gzip_printf( "%s: start of this routine...",cn);
5654     mod_gzip_printf( "%s: Call fclose(%s)...",cn,npp(input_filename));
5655     #endif
5656
5657     fclose( ifh );
5658
5659     #ifdef MOD_GZIP_DEBUG1
5660     mod_gzip_printf( "%s: Back fclose(%s)...",cn,npp(input_filename));
5661     #endif
5662    }
5663  else 
5664    {
5665     #ifdef MOD_GZIP_DEBUG1
5666     mod_gzip_printf( "%s: 'ifh_passed' was VALID on entry.",cn);
5667     mod_gzip_printf( "%s: No call to fclose() was made.",cn);
5668     #endif
5669    }
5670
5671  #ifdef MOD_GZIP_DEBUG1
5672  mod_gzip_printf( "%s: Exit > return( total_byteswritten = %ld ) >",
5673                    cn, (long) total_byteswritten );
5674  #endif
5675
5676  return total_byteswritten;
5677 }
5678
5679 int mod_gzip_sendfile2(
5680 request_rec   *r,
5681 mod_gzip_conf *dconf,
5682 char          *input_filename
5683 )
5684 {
5685  FILE *ifh=0;
5686  FILE *ofh=0;
5687  int   ofh_used=0;
5688
5689  int   i=0;
5690  int   rc=1;
5691  int   err=0;
5692  int   final_rc=1;
5693  int   linecount=0;
5694  int   bytesleft=0;
5695  int   bytesread=0;
5696  int   bytestoread=0;
5697  int   skip_advance=0;
5698  int   byteswritten=0;
5699  int   bytestowrite=0;
5700  long  total_bytes_sent=0;
5701  int   valid_char_count=0;
5702  int   total_bytes_left_to_copy=0;
5703
5704  /*
5705   * TODO: This is a brain-teaser to solve.
5706   *
5707   * gcc UNIX compiler will report the following 'ok_to_send'
5708   * flag as no longer being 'used' inside this function but
5709   * if it is removed or even commented out then Apache GP faults
5710   * at runtime when logic enters this function ( but only if
5711   * MOD_GZIP_DEBUG1 flag is OFF at compile time ?? ).
5712   *
5713   * There is no 'macro' anywhere that bears this name and
5714   * there doesn't seem to be any reason for this GP fault.
5715   *
5716   * If this 'ok_to_send' variable is removed everything is fine
5717   * for Win32... it is only UNIX gcc compiler that reports no
5718   * warning but will still GP fault at runtime.
5719   *
5720   * It's a mystery. Can anyone solve it?
5721   */
5722
5723  int ok_to_send = 1; /* This MUST remain or UNIX 'GP faults' ??? */
5724
5725  #ifdef MOD_GZIP_DEBUG1
5726  int   total_bytes_copied=0;
5727  #endif
5728
5729  struct stat sbuf;
5730
5731  #define   MOD_GZIP_SENDFILE2_BUFFER_SIZE 4099
5732  char tmp[ MOD_GZIP_SENDFILE2_BUFFER_SIZE + 16 ];
5733
5734  #define MOD_GZIP_LINE_BUFFER_SIZE 2048
5735  char lbuf[ MOD_GZIP_LINE_BUFFER_SIZE + 16 ];
5736
5737  #define MOD_GZIP_DEBUG1_SENDFILE2
5738
5739  #ifdef MOD_GZIP_DEBUG1
5740  char cn[]="mod_gzip_sendfile2()";
5741  #endif
5742
5743  char *sp    = 0; 
5744  char *p1    = 0;
5745  int   p1len = 0;
5746  char *p2    = lbuf;
5747  int   p2len = 0;
5748  char *p3    = 0;
5749
5750  long hbytes_in    = 0; 
5751  long bbytes_in    = 0; 
5752  long bbytes_out   = 0; 
5753  long bbytes_total = 0; 
5754
5755  int  action_flag  = 0; 
5756  int  header_done  = 0; 
5757  int  body_done    = 0; 
5758  int  te_seen      = 0; 
5759  int  te_chunked   = 0; 
5760  int  send_as_is   = 0; 
5761  int  resp_code    = 0; 
5762  int  ce_seen      = 0; 
5763  int  ct_seen      = 0; 
5764  char ct_value[ 256 ];  
5765
5766  int   field_ok        = 0; 
5767  int   fields_excluded = 0; 
5768  char *fieldkey        = 0; 
5769  char *fieldstring     = 0; 
5770
5771  char output_filename1[256]; 
5772
5773  long raw_input_length = 0; 
5774
5775  #define CHUNKE_GET_HEXSTRING  1
5776  #define CHUNKE_WAIT_REAL_EOL1 2
5777  #define CHUNKE_GET_FOOTER     3
5778  #define CHUNKE_GET_REAL_DATA  4
5779
5780  #define CHUNKE_HEXSTRING_MAXLEN 10
5781
5782  int   chunke_count           = 1;
5783  char *chunke_errstr          = NULL;
5784  int   chunke_state           = CHUNKE_GET_HEXSTRING;
5785  int   chunke_nextstate       = 0;
5786  long  chunke_realdata_length = 0;
5787  long  chunke_realdata_read   = 0;
5788  int   chunke_hexstring_oset  = 0;
5789  char  chunke_hexstring[ CHUNKE_HEXSTRING_MAXLEN + 1 ];
5790
5791  #ifdef CHUNKE_USES_OUTPUT_BUFFER
5792  char *chunke_obufstart  = lbuf; 
5793  char *chunke_obuf       = chunke_obufstart;
5794  int   chunke_obuflen    = 0;
5795  int   chunke_obuflenmax = MOD_GZIP_LINE_BUFFER_SIZE; 
5796  #endif
5797
5798  int   dconf__dechunk        = 0; 
5799  int   dconf__keep_workfiles = 0; 
5800  char *dconf__temp_dir       = 0; 
5801
5802  #ifdef MOD_GZIP_DEBUG1
5803  mod_gzip_printf( "%s: Entry...",cn);
5804  #endif
5805
5806  if ( !r              ) return DECLINED;
5807  if ( !dconf          ) return DECLINED;
5808  if ( !input_filename ) return DECLINED;
5809
5810  #ifdef MOD_GZIP_DEBUG1
5811  mod_gzip_printf( "%s: r = %ld",cn,(long)r);
5812  mod_gzip_printf( "%s: r->proxyreq    = %d",  cn,(int) r->proxyreq );
5813  mod_gzip_printf( "%s: input_filename = [%s]",cn,npp(input_filename));
5814  #endif
5815
5816  ct_value[0] = 0; 
5817
5818  dconf__dechunk        = dconf->dechunk;
5819  dconf__keep_workfiles = dconf->keep_workfiles;
5820  dconf__temp_dir       = dconf->temp_dir;
5821
5822  #ifdef MOD_GZIP_DEBUG1
5823  mod_gzip_printf( "%s: dconf__dechunk        = %d",   cn, (int) dconf__dechunk );
5824  mod_gzip_printf( "%s: dconf__keep_workfiles = %d",   cn, (int) dconf__keep_workfiles );
5825  mod_gzip_printf( "%s: dconf__temp_dir       = [%s]", cn, npp(dconf__temp_dir));
5826  #endif
5827
5828  #ifdef MOD_GZIP_DEBUG1
5829  mod_gzip_printf( "%s: Call stat(%s)...",cn,npp(input_filename));
5830  #endif
5831
5832  rc = stat( input_filename, &sbuf );
5833
5834  if ( rc ) 
5835    {
5836     #ifdef MOD_GZIP_DEBUG1
5837     mod_gzip_printf( "%s: .... stat() call FAILED",cn);
5838     #endif
5839
5840     #ifdef MOD_GZIP_USES_APACHE_LOGS
5841     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"STAT_FAILED"));
5842     #endif
5843
5844     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
5845     "mod_gzip: stat(%s) in sendfile2", input_filename );
5846
5847     ap_log_error( "",0,APLOG_NOERRNO|APLOG_ERR, r->server,
5848     "mod_gzip: %s",mod_gzip_check_permissions);
5849
5850     #ifdef MOD_GZIP_DEBUG1
5851     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
5852     #endif
5853
5854     return DECLINED;
5855    }
5856  else 
5857    {
5858     raw_input_length = (long) sbuf.st_size;
5859
5860     #ifdef MOD_GZIP_DEBUG1
5861     mod_gzip_printf( "%s: .... stat() call SUCCEEDED",cn);
5862     mod_gzip_printf( "%s: sbuf.st_size     = %ld",cn,(long)sbuf.st_size);
5863     mod_gzip_printf( "%s: raw_input_length = %ld",cn,(long)raw_input_length);
5864     #endif
5865
5866     if ( sbuf.st_size < 1 )
5867       {
5868        #ifdef MOD_GZIP_DEBUG1
5869        mod_gzip_printf( "%s: ERROR: The input file is EMPTY",cn);
5870        mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
5871        #endif
5872
5873        #ifdef MOD_GZIP_USES_APACHE_LOGS
5874        ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"CAP_FILE_EMPTY"));
5875        #endif
5876
5877        ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
5878        "mod_gzip: EMPTY FILE [%s] in sendfile2", input_filename );
5879
5880        ap_log_error( "",0,APLOG_NOERRNO|APLOG_ERR, r->server,
5881        "mod_gzip: %s",mod_gzip_check_permissions);
5882
5883        return DECLINED;
5884       }
5885    }
5886
5887  #ifdef MOD_GZIP_DEBUG1
5888  mod_gzip_printf( "%s: Call fopen(%s)...",cn,npp(input_filename));
5889  #endif
5890
5891  ifh = fopen( input_filename, "rb" ); 
5892
5893  if ( !ifh ) 
5894    {
5895     #ifdef MOD_GZIP_DEBUG1
5896     mod_gzip_printf( "%s: .... fopen() FAILED",cn);
5897     #endif
5898
5899     #ifdef MOD_GZIP_USES_APACHE_LOGS
5900     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"CAP_FOPEN_FAILED"));
5901     #endif
5902
5903     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
5904     "mod_gzip: fopen(%s) in sendfile2", input_filename);
5905
5906     ap_log_error( "",0,APLOG_NOERRNO|APLOG_ERR, r->server,
5907     "mod_gzip: %s",mod_gzip_check_permissions);
5908
5909     #ifdef MOD_GZIP_DEBUG1
5910     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
5911     #endif
5912
5913     return DECLINED;
5914    }
5915
5916  #ifdef MOD_GZIP_DEBUG1
5917  mod_gzip_printf( "%s: .... fopen() SUCCEEDED",cn);
5918  mod_gzip_printf( "%s: sizeof( tmp ) = %d",cn,sizeof(tmp));
5919  mod_gzip_printf( "%s: MOD_GZIP_SENDFILE2_BUFFER_SIZE = %d",
5920                cn,(int)MOD_GZIP_SENDFILE2_BUFFER_SIZE);
5921  mod_gzip_printf( "%s: Processing header now...",cn);
5922  #endif
5923
5924  for (;;)
5925     {
5926      bytesread = fread( tmp, 1, MOD_GZIP_SENDFILE2_BUFFER_SIZE, ifh );
5927
5928      #ifdef MOD_GZIP_DEBUG1
5929      mod_gzip_printf( "%s: HEADER: %5.5d: Back fread(): bytesread=%d",
5930                        cn,linecount,bytesread);
5931      #endif
5932
5933      if ( bytesread < 1 ) break; 
5934
5935      p1    = tmp;
5936      p1len = 0;
5937
5938      for ( i=0; i<bytesread; i++ )
5939         {
5940          if ( *p1 == 10 )
5941            {
5942             *p2 = 0; 
5943
5944             linecount++; 
5945
5946             if ( valid_char_count < 1 )
5947               {
5948                #ifdef MOD_GZIP_DEBUG1
5949                mod_gzip_printf( "%s: HEADER: %5.5d: lbuf=[CR/LF]",
5950                                  cn,linecount);
5951                #endif
5952
5953                p1++;         
5954                p1len++;      
5955                hbytes_in++;  
5956
5957                header_done = 1; 
5958
5959                break; 
5960               }
5961             else 
5962               {
5963                #ifdef MOD_GZIP_DEBUG1
5964                mod_gzip_printf( "%s: HEADER: %5.5d: lbuf=[%s]",
5965                                  cn,linecount,lbuf);
5966                #endif
5967
5968                if ( linecount == 1 )
5969                  {
5970                   p3 = lbuf; 
5971
5972                   while((*p3!=0)&&(*p3<33)) p3++; 
5973                   while((*p3!=0)&&(*p3>32)) p3++; 
5974                   while((*p3!=0)&&(*p3<33)) p3++; 
5975
5976                   #ifdef MOD_GZIP_DEBUG1
5977                   mod_gzip_printf( "%s: HEADER: %5.5d: * p3=[%s]",
5978                                     cn,linecount,npp(p3));
5979                   #endif
5980
5981                   if ( *p3 != 0 )
5982                     {
5983                      resp_code = (int) atoi( p3 );
5984                     }
5985
5986                   #ifdef MOD_GZIP_DEBUG1
5987                   mod_gzip_printf( "%s: HEADER: %5.5d: * resp_code = %d",
5988                                     cn,linecount,resp_code);
5989                   #endif
5990                  }
5991
5992                else if ( lbuf[0] == 'T' )
5993                  {
5994                   if ( mod_gzip_strnicmp(lbuf,"Transfer-Encoding:",18)==0)
5995                     {
5996                      #ifdef MOD_GZIP_DEBUG1
5997                      mod_gzip_printf( "%s: HEADER: %5.5d: * 'Transfer-Encoding:' seen",
5998                                        cn,linecount);
5999                      #endif
6000
6001                      te_seen = 1; 
6002
6003                      if ( mod_gzip_stringcontains( lbuf, "chunked" ) )
6004                        {
6005                         #ifdef MOD_GZIP_DEBUG1
6006                         mod_gzip_printf( "%s: HEADER: %5.5d: * 'Transfer-Encoding: chunked' seen",
6007                                           cn,linecount);
6008                         #endif
6009
6010                         te_chunked = 1; 
6011                        }
6012                     }
6013                  }
6014
6015                else if ( lbuf[0] == 'C' )
6016                  {
6017                   if ( mod_gzip_strnicmp(lbuf,"Content-Encoding:",17)==0)
6018                     {
6019                      #ifdef MOD_GZIP_DEBUG1
6020                      mod_gzip_printf( "%s: HEADER: %5.5d: * 'Content-Encoding:' seen",
6021                                        cn,linecount);
6022                      #endif
6023
6024                      ce_seen = 1; 
6025                     }
6026
6027                   else if ( mod_gzip_strnicmp(lbuf,"Content-Type:",13)==0)
6028                     {
6029                      #ifdef MOD_GZIP_DEBUG1
6030                      mod_gzip_printf( "%s: HEADER: %5.5d: * 'Content-Type:' seen",
6031                                        cn,linecount);
6032                      #endif
6033
6034                      ct_seen = 1; 
6035
6036                      p3 = lbuf; 
6037
6038                      while((*p3!=0)&&(*p3<33)) p3++; 
6039                      while((*p3!=0)&&(*p3>32)) p3++; 
6040                      while((*p3!=0)&&(*p3<33)) p3++; 
6041
6042                      if ( p2len < 230 ) 
6043                        {
6044                         mod_gzip_strcpy( ct_value, p3 );
6045                        }
6046                      else 
6047                        {
6048                         sprintf( ct_value, "%-230.230s", p3 );
6049                        }
6050
6051                      #ifdef MOD_GZIP_DEBUG1
6052                      mod_gzip_printf( "%s: HEADER: %5.5d: * ct_value=[%s]",
6053                                        cn,linecount,npp(ct_value));
6054                      #endif
6055                     }
6056                  }
6057
6058                 if ( ( fields_excluded < 1 ) &&
6059                      ( dconf->imap_total_isrspheader > 0 ) )
6060                   {
6061                    #ifdef MOD_GZIP_DEBUG1
6062                    mod_gzip_printf( "%s: dconf->imap_total_isrspheader = %d", cn,
6063                                    (int) dconf->imap_total_isrspheader );
6064                    mod_gzip_printf( "%s: Checking RESPONSE header field...", cn );
6065                    #endif
6066
6067                    p3 = lbuf; 
6068
6069                    while((*p3!=0)&&(*p3<33)) p3++; 
6070                    while((*p3!=0)&&(*p3>32)) p3++; 
6071                    while((*p3!=0)&&(*p3<33)) p3++; 
6072
6073                    fieldkey    = lbuf; 
6074                    fieldstring = p3;   
6075
6076                    #ifdef MOD_GZIP_DEBUG1
6077                    mod_gzip_printf( "%s: Call mod_gzip_validate1()...",cn);
6078                    #endif
6079
6080                    field_ok =
6081                    mod_gzip_validate1(
6082                    (request_rec   *) r,
6083                    (mod_gzip_conf *) dconf,
6084                    NULL, /* r->filename     (Not used here) */
6085                    NULL, /* r->uri          (Not used here) */
6086                    NULL, /* r->content_type (Not used here) */
6087                    NULL, /* r->handler      (Not used here) */
6088                    (char *) fieldkey,     /* Field key */
6089                    (char *) fieldstring,  /* Field string */
6090                    MOD_GZIP_RESPONSE      /* Direction */
6091                    );
6092
6093                    #ifdef MOD_GZIP_DEBUG1
6094                    mod_gzip_printf( "%s: Back mod_gzip_validate1()...",cn);
6095                    mod_gzip_printf( "%s: field_ok = %d",cn,field_ok);
6096                    #endif
6097
6098                    if ( field_ok == MOD_GZIP_IMAP_DECLINED1 )
6099                      {
6100                       fields_excluded++; 
6101                      }
6102                   }
6103               }
6104
6105             p2    = lbuf;
6106             p2len = 0;
6107
6108             valid_char_count = 0;
6109            }
6110          else 
6111            {
6112             if ( *p1 > 32 ) valid_char_count++;
6113
6114             if (( p2len < MOD_GZIP_LINE_BUFFER_SIZE )&&( *p1 != 13 ))
6115               {
6116                *p2++ = *p1;
6117                p2len++;
6118               }
6119            }
6120
6121          p1++;        
6122          p1len++;     
6123          hbytes_in++; 
6124         }
6125
6126      if ( header_done ) break;
6127     }
6128
6129  bbytes_total = raw_input_length - hbytes_in; 
6130
6131  #ifdef MOD_GZIP_DEBUG1
6132  mod_gzip_printf( "%s: Done processing header...",cn);
6133  mod_gzip_printf( "%s: header_done     = %d",cn,header_done);
6134  mod_gzip_printf( "%s: fields_excluded = %d",cn,fields_excluded);
6135  mod_gzip_printf( "%s: bytesread       = %d",cn,bytesread);
6136  mod_gzip_printf( "%s: p1len           = %d",cn,p1len);
6137  mod_gzip_printf( "%s: resp_code       = %d (HTTP response code)",cn,resp_code);
6138  mod_gzip_printf( "%s: hbytes_in       = %ld (Total HEADER bytes read)",cn,(long)hbytes_in);
6139  mod_gzip_printf( "%s: bbytes_total    = %ld (Total BODY bytes left)",cn,(long)bbytes_total);
6140  mod_gzip_printf( "%s: te_seen         = %d (Transfer-Encoding:)",cn,te_seen);
6141  mod_gzip_printf( "%s: te_chunked      = %d (Transfer-Encoding: chunked)",cn,te_chunked);
6142  mod_gzip_printf( "%s: ce_seen         = %d (Content-Encoding:)",cn,ce_seen);
6143  mod_gzip_printf( "%s: ct_seen         = %d (Content-Type:)",cn,ct_seen);
6144  mod_gzip_printf( "%s: ct_value        = [%s]",cn,npp(ct_value));
6145  mod_gzip_printf( "%s: dconf__dechunk  = %ld",cn,(long)dconf__dechunk);
6146  #endif
6147
6148  if ( !header_done )
6149    {
6150     #ifdef MOD_GZIP_DEBUG1
6151     mod_gzip_printf( "%s: ERROR: 'header_done' flag is NOT SET",cn);
6152     mod_gzip_printf( "%s: No valid HTTP 'End of header' was found...",cn);
6153     #endif
6154
6155     #ifdef MOD_GZIP_USES_APACHE_LOGS
6156     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"NO_HTTP_EOH"));
6157     #endif
6158
6159     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6160     "mod_gzip: Invalid HTTP response header for r->the_request[%s] in sendfile2", r->the_request);
6161     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6162     "mod_gzip: Invalid HTTP response header in FILE [%s] in sendfile2",input_filename);
6163
6164     #ifdef MOD_GZIP_DEBUG1
6165     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
6166     #endif
6167
6168     return DECLINED;
6169    }
6170
6171  if ( !resp_code )
6172    {
6173     #ifdef MOD_GZIP_DEBUG1
6174     mod_gzip_printf( "%s: ERROR: 'resp_code' value is NOT SET",cn);
6175     mod_gzip_printf( "%s: No valid HTTP response code was found...",cn);
6176     #endif
6177
6178     #ifdef MOD_GZIP_USES_APACHE_LOGS
6179     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"NO_HTTP_RESP_CODE"));
6180     #endif
6181
6182     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6183     "mod_gzip: No valid HTTP response code for r->the_request[%s] in sendfile2", r->the_request);
6184     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6185     "mod_gzip: No valid HTTP response code in FILE [%s] in sendfile2",input_filename);
6186
6187     #ifdef MOD_GZIP_DEBUG1
6188     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
6189     #endif
6190
6191     return DECLINED;
6192    }
6193
6194  #ifdef MOD_GZIP_DEBUG1
6195  mod_gzip_printf( "%s: --------------------------------------------------",cn);
6196  mod_gzip_printf( "%s: Checking for various 'send as is' conditions...",cn);
6197  mod_gzip_printf( "%s: --------------------------------------------------",cn);
6198  #endif
6199
6200  #ifdef MOD_GZIP_USES_APACHE_LOGS
6201  mod_gzip_strcpy( lbuf, "SEND_AS_IS");
6202  #endif
6203
6204  if ( resp_code != 200 )
6205    {
6206     #ifdef MOD_GZIP_DEBUG1
6207     mod_gzip_printf( "%s: resp_code is NOT '200'...",cn);
6208     mod_gzip_printf( "%s: Issuing send_as_is++",cn);
6209     #endif
6210
6211     send_as_is++;
6212
6213     #ifdef MOD_GZIP_USES_APACHE_LOGS
6214     mod_gzip_strcat( lbuf, ":NO_200");
6215     #endif
6216    }
6217
6218  if ( ( !send_as_is ) && ( fields_excluded > 0 ) )
6219    {
6220     #ifdef MOD_GZIP_DEBUG1
6221     mod_gzip_printf( "%s: 'fields_excluded' = %d ( More than ZERO )",
6222                        cn, fields_excluded );
6223     mod_gzip_printf( "%s: Issuing send_as_is++",cn);
6224     #endif
6225
6226     send_as_is++;
6227
6228     #ifdef MOD_GZIP_USES_APACHE_LOGS
6229     mod_gzip_strcat( lbuf, ":RESPONSE_FIELD_EXCLUDED");
6230     #endif
6231    }
6232
6233  if ( ( !send_as_is ) && ( bbytes_total < 1 ) )
6234    {
6235     #ifdef MOD_GZIP_DEBUG1
6236     mod_gzip_printf( "%s: bbytes_total is less than 1",cn);
6237     mod_gzip_printf( "%s: There is no valid BODY data.",cn);
6238     mod_gzip_printf( "%s: Issuing send_as_is++",cn);
6239     #endif
6240
6241     send_as_is++;
6242
6243     #ifdef MOD_GZIP_USES_APACHE_LOGS
6244     mod_gzip_strcat( lbuf, ":NO_BODY");
6245     #endif
6246    }
6247
6248  if ( ( !send_as_is ) && ( ( te_seen == 1 ) && ( te_chunked == 0 ) ) )
6249    {
6250     #ifdef MOD_GZIP_DEBUG1
6251     mod_gzip_printf( "%s: 'Transfer-Encoding:' is present but 'type' is UNKNOWN",cn);
6252     mod_gzip_printf( "%s: Issuing send_as_is++",cn);
6253     #endif
6254
6255     send_as_is++;
6256
6257     #ifdef MOD_GZIP_USES_APACHE_LOGS
6258     mod_gzip_strcat( lbuf, ":UNKNOWN_TE_VALUE");
6259     #endif
6260    }
6261
6262  if ( ( !send_as_is ) && ( ce_seen == 1 ) )
6263    {
6264     #ifdef MOD_GZIP_DEBUG1
6265     mod_gzip_printf( "%s: 'Content-Encoding:' is already present...",cn);
6266     mod_gzip_printf( "%s: Issuing send_as_is++",cn);
6267     #endif
6268
6269     send_as_is++;
6270
6271     #ifdef MOD_GZIP_USES_APACHE_LOGS
6272     mod_gzip_strcat( lbuf, ":HAS_CE");
6273     #endif
6274    }
6275
6276  if ( ( !send_as_is ) && ( ct_seen != 1 ) )
6277    {
6278     #ifdef MOD_GZIP_DEBUG1
6279     mod_gzip_printf( "%s: 'Content-Type:' response field NOT PRESENT...",cn);
6280     mod_gzip_printf( "%s: Issuing send_as_is++",cn);
6281     #endif
6282
6283     send_as_is++;
6284
6285     #ifdef MOD_GZIP_USES_APACHE_LOGS
6286     mod_gzip_strcat( lbuf, ":NO_CONTENT_TYPE_IN_RESPONSE_HEADER");
6287     #endif
6288    }
6289  else if ( !send_as_is ) 
6290    {
6291     #ifdef MOD_GZIP_DEBUG1
6292     mod_gzip_printf( "%s: 'Content-Type:' response field is PRESENT",cn);
6293     #endif
6294
6295     #ifdef MOD_GZIP_DEBUG1
6296     mod_gzip_printf( "%s: dconf->imap_total_ismime = %d", cn,
6297                     (int) dconf->imap_total_ismime );
6298     #endif
6299
6300     if ( dconf->imap_total_ismime > 0 )
6301       {
6302        #ifdef MOD_GZIP_DEBUG1
6303        mod_gzip_printf( "%s: Call mod_gzip_validate1(ct_value=[%s])...",
6304                          cn,npp(ct_value));
6305        #endif
6306
6307        action_flag =
6308        mod_gzip_validate1(
6309        (request_rec   *) r,
6310        (mod_gzip_conf *) dconf,
6311        NULL,     /* r->filename  (Not used here) */
6312        NULL,     /* r->uri       (Not used here) */
6313        ct_value, /* Content type (Used         ) */
6314        NULL,     /* r->handler   (Not used here) */
6315        NULL,     /* Field key    (Not used here) */
6316        NULL,     /* Field string (Not used here) */
6317        0         /* Direction    (Not used here) */
6318        );
6319
6320        #ifdef MOD_GZIP_DEBUG1
6321        mod_gzip_printf( "%s: Back mod_gzip_validate1(ct_value=[%s])...",
6322                          cn,npp(ct_value));
6323        mod_gzip_printf( "%s: action_flag  = %d",cn,action_flag);
6324        #endif
6325
6326        if ( action_flag != MOD_GZIP_IMAP_DECLINED1 )
6327          {
6328           #ifdef MOD_GZIP_DEBUG1
6329           mod_gzip_printf( "%s: This 'Content-Type:' is NOT EXCLUDED",cn);
6330           #endif
6331          }
6332        else
6333          {
6334           #ifdef MOD_GZIP_DEBUG1
6335           mod_gzip_printf( "%s: This 'Content-Type:' is EXCLUDED",cn);
6336           mod_gzip_printf( "%s: Issuing send_as_is++",cn);
6337           #endif
6338
6339           send_as_is++;
6340
6341           #ifdef MOD_GZIP_USES_APACHE_LOGS
6342           mod_gzip_strcat( lbuf, ":RESPONSE_CONTENT_TYPE_EXCLUDED");
6343           #endif
6344          }
6345       }
6346    }
6347
6348  #ifdef MOD_GZIP_DEBUG1
6349  mod_gzip_printf( "%s: --------------------------------------------------",cn);
6350  mod_gzip_printf( "%s: send_as_is   = %d",cn,send_as_is);
6351  mod_gzip_printf( "%s: --------------------------------------------------",cn);
6352  #endif
6353
6354  if ( send_as_is > 0 ) 
6355    {
6356     #ifdef MOD_GZIP_USES_APACHE_LOGS
6357     ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,lbuf));
6358     #endif
6359
6360     #ifdef MOD_GZIP_DEBUG1
6361     mod_gzip_printf( "%s: Sending response 'as is'...",cn);
6362     mod_gzip_printf( "%s: Issuing 'goto mod_gzip_sendfile2_send_as_is'...",cn);
6363     #endif
6364
6365     goto mod_gzip_sendfile2_send_as_is;
6366    }
6367
6368  if ( !te_seen )
6369    {
6370     #ifdef MOD_GZIP_DEBUG1
6371     mod_gzip_printf( "%s: Call fclose(%s)...",cn, npp(input_filename));
6372     #endif
6373
6374     fclose( ifh );
6375
6376     ifh = 0; 
6377
6378     #ifdef MOD_GZIP_DEBUG1
6379     mod_gzip_printf( "%s: Back fclose(%s)...",cn,npp(input_filename));
6380     mod_gzip_printf( "%s: Call mod_gzip_encode_and_transmit()...",cn);
6381     #endif
6382
6383     rc =
6384     mod_gzip_encode_and_transmit(
6385     r,              
6386     dconf,          
6387     input_filename, 
6388     1,              
6389     bbytes_total,   
6390     0,              
6391     hbytes_in,      
6392     ""              
6393     );
6394
6395     #ifdef MOD_GZIP_DEBUG1
6396     mod_gzip_printf( "%s: Back mod_gzip_encode_and_transmit()...",cn);
6397     mod_gzip_printf( "%s: rc = %d",cn,rc);
6398     #endif
6399
6400     if ( rc == DECLINED )
6401       {
6402        #ifdef MOD_GZIP_DEBUG1
6403        mod_gzip_printf( "%s: rc = DECLINED (FAILURE)",cn);
6404        mod_gzip_printf( "%s: Sending response 'as is'...",cn);
6405        mod_gzip_printf( "%s: Issuing 'goto mod_gzip_sendfile2_send_as_is'...",cn);
6406        #endif
6407
6408        goto mod_gzip_sendfile2_send_as_is;
6409       }
6410
6411     else 
6412       {
6413        #ifdef MOD_GZIP_DEBUG1
6414        mod_gzip_printf( "%s: rc = OK (SUCCESS)",cn);
6415        mod_gzip_printf( "%s: Call goto mod_gzip_sendfile2_cleanup...",cn,rc);
6416        #endif
6417
6418        final_rc = rc; 
6419
6420        goto mod_gzip_sendfile2_cleanup;
6421       }
6422    }
6423
6424  #ifdef MOD_GZIP_DEBUG1
6425  mod_gzip_printf( "%s: * TRANSFER ENCODING DE-CHUNKING OPTION",cn);
6426  mod_gzip_printf( "%s: dconf__dechunk = %ld",cn,(long)dconf__dechunk);
6427  #endif
6428
6429  if ( dconf__dechunk ) 
6430    {
6431     #ifdef MOD_GZIP_DEBUG1
6432     mod_gzip_printf( "%s: dconf__dechunk = YES",cn);
6433     mod_gzip_printf( "%s: BODY data 'de-chunking' option is ON...",cn);
6434     #endif
6435    }
6436  else 
6437    {
6438     #ifdef MOD_GZIP_DEBUG1
6439     mod_gzip_printf( "%s: dconf__dechunk = NO",cn);
6440     mod_gzip_printf( "%s: BODY data 'de-chunking' option is OFF...",cn);
6441     #endif
6442
6443     #ifdef MOD_GZIP_USES_APACHE_LOGS
6444
6445     ap_table_setn( r->notes,"mod_gzip_result",
6446     ap_pstrdup(r->pool,"SEND_AS_IS:DECHUNK_OPTION_IS_OFF"));
6447     #endif
6448
6449     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6450     "mod_gzip: DECHUNK option is OFF in sendfile2");
6451
6452     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6453     "mod_gzip: Cannot compress chunked response for [%s]",
6454     r->the_request);
6455
6456     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6457     "mod_gzip: ** Uncompressed responses that use 'Transfer-encoding: chunked'");
6458
6459     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6460     "mod_gzip: ** must be 'de-chunked' before they can be compressed." );
6461
6462     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6463     "mod_gzip: ** Turn DECHUNK option ON for this response category." );
6464
6465     #ifdef MOD_GZIP_DEBUG1
6466     mod_gzip_printf( "%s: Sending response 'as is'...",cn);
6467     mod_gzip_printf( "%s: Issuing 'goto mod_gzip_sendfile2_send_as_is'...",cn);
6468     #endif
6469
6470     goto mod_gzip_sendfile2_send_as_is;
6471    }
6472
6473  #ifdef MOD_GZIP_DEBUG1
6474  mod_gzip_printf( "%s: De-chunking the body data now...",cn);
6475  #endif
6476
6477  #ifdef MOD_GZIP_DEBUG1
6478  mod_gzip_printf( "%s: Creating 'output_filename1' string now...",cn);
6479  #endif
6480
6481  mod_gzip_create_unique_filename(
6482  dconf__temp_dir,
6483  (char *) output_filename1,
6484  MOD_GZIP_MAX_PATH_LEN
6485  );
6486
6487  #ifdef MOD_GZIP_DEBUG1
6488  mod_gzip_printf( "%s: output_filename1 = [%s]",cn,npp(output_filename1));
6489  mod_gzip_printf( "%s: Call OUTPUT fopen(%s)...",cn,npp(output_filename1));
6490  #endif
6491
6492  ofh = fopen( output_filename1, "wb" );
6493
6494  if ( !ofh ) 
6495    {
6496     #ifdef MOD_GZIP_DEBUG1
6497     mod_gzip_printf( "%s: .... OUTPUT fopen() FAILED",cn);
6498     #endif
6499
6500     #ifdef MOD_GZIP_USES_APACHE_LOGS
6501
6502     ap_table_setn( r->notes,"mod_gzip_result",
6503     ap_pstrdup(r->pool,"SEND_AS_IS:FOPEN_FAILED"));
6504     #endif
6505
6506     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6507     "mod_gzip: fopen(%s) for OUTPUT in sendfile2", output_filename1 );
6508
6509     ap_log_error( "",0,APLOG_NOERRNO|APLOG_ERR, r->server,
6510     "mod_gzip: %s",mod_gzip_check_permissions);
6511
6512     #ifdef MOD_GZIP_DEBUG1
6513     mod_gzip_printf( "%s: Sending response 'as is'...",cn);
6514     mod_gzip_printf( "%s: Issuing 'goto mod_gzip_sendfile2_send_as_is'...",cn);
6515     #endif
6516
6517     goto mod_gzip_sendfile2_send_as_is;
6518    }
6519
6520  #ifdef MOD_GZIP_DEBUG1
6521  mod_gzip_printf( "%s: .... OUTPUT fopen() SUCCEEDED",cn);
6522  #endif
6523
6524  ofh_used = 1; 
6525
6526  #ifdef MOD_GZIP_DEBUG1
6527  mod_gzip_printf( "%s: Call fseek(ifh,0,0)...",cn );
6528  #endif
6529
6530  rc = fseek( ifh, 0, 0 );
6531
6532  if ( rc != 0 ) 
6533    {
6534     #ifdef MOD_GZIP_DEBUG1
6535     mod_gzip_printf( "%s: .... fseek(ifh,0,0) FAILED",cn);
6536     #endif
6537
6538     #ifdef MOD_GZIP_USES_APACHE_LOGS
6539
6540     ap_table_setn( r->notes,"mod_gzip_result",
6541     ap_pstrdup(r->pool,"SEND_AS_IS:FSEEK_FAILED"));
6542     #endif
6543
6544     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
6545     "mod_gzip: fseek(ifh,0,0) in sendfile2" );
6546
6547     #ifdef MOD_GZIP_DEBUG1
6548     mod_gzip_printf( "%s: Sending response 'as is'...",cn);
6549     mod_gzip_printf( "%s: Issuing 'goto mod_gzip_sendfile2_send_as_is'...",cn);
6550     #endif
6551
6552     goto mod_gzip_sendfile2_send_as_is;
6553    }
6554  #ifdef MOD_GZIP_DEBUG1
6555  else
6556    {
6557     mod_gzip_printf( "%s: .... fseek(ifh,0,0) SUCCEEDED",cn);
6558    }
6559  #endif
6560
6561  total_bytes_left_to_copy = hbytes_in;
6562
6563  #ifdef MOD_GZIP_DEBUG1
6564  total_bytes_copied = 0;
6565  mod_gzip_printf( "%s: Copying %ld header bytes now...",
6566                    cn,(long)total_bytes_left_to_copy);
6567  #endif
6568
6569  while( total_bytes_left_to_copy > 0 )
6570     {
6571      if ( total_bytes_left_to_copy < MOD_GZIP_SENDFILE2_BUFFER_SIZE )
6572        {
6573         bytestoread = total_bytes_left_to_copy;
6574        }
6575      else
6576        {
6577         bytestoread = MOD_GZIP_SENDFILE2_BUFFER_SIZE;
6578        }
6579
6580      bytesread    = fread ( tmp, 1, bytestoread, ifh );
6581      byteswritten = fwrite( tmp, 1, bytesread,   ofh );
6582
6583      total_bytes_left_to_copy -= byteswritten;
6584
6585      #ifdef MOD_GZIP_DEBUG1
6586      total_bytes_copied += byteswritten;
6587      #endif
6588     }
6589
6590  #ifdef MOD_GZIP_DEBUG1
6591  mod_gzip_printf( "%s: %ld header bytes have been copied...",
6592                    cn,(long)total_bytes_copied);
6593  #endif
6594
6595  for (;;) 
6596     {
6597      p1 = tmp; 
6598
6599      bytesread = fread( p1, 1, MOD_GZIP_SENDFILE2_BUFFER_SIZE, ifh );
6600
6601      #ifdef MOD_GZIP_DEBUG1
6602      mod_gzip_printf( "%s: Back fread(): bytesread=%d",cn,bytesread);
6603      #endif
6604
6605      if ( bytesread < 1 ) break; 
6606
6607      bbytes_in += bytesread; 
6608      bytesleft  = bytesread; 
6609
6610      if ( te_chunked ) 
6611        {
6612         #ifdef MOD_GZIP_DEBUG1
6613         mod_gzip_printf( "%s: 'te_chunked' flag is TRUE...",cn);
6614         mod_gzip_printf( "%s: Filtering chunked data...",cn);
6615         #endif
6616
6617         sp = p1; 
6618
6619         while ( bytesleft > 0 )
6620           {
6621            switch( chunke_state )
6622              {
6623               case CHUNKE_GET_HEXSTRING:
6624
6625                    if ((*sp==13)||(*sp==10)||(*sp==';'))
6626                      {
6627                       chunke_hexstring[chunke_hexstring_oset]=0;
6628
6629                       #ifdef MOD_GZIP_CHUNKE_DIAG1
6630                       mod_gzip_printf("chunke: %5.5ld chunke_hexstring=[%s]\n",
6631                                        chunke_count, npp(chunke_hexstring) );
6632                       #endif
6633
6634                       chunke_realdata_length = (long)
6635                       strtol(chunke_hexstring,&chunke_errstr,16);
6636
6637                       #ifdef MOD_GZIP_CHUNKE_DIAG1
6638                       mod_gzip_printf("chunke: %5.5ld realdata_length= 0x%X %ld\n",
6639                                  chunke_count, chunke_realdata_length,
6640                                                chunke_realdata_length );
6641                       #endif
6642
6643                       if ( chunke_realdata_length < 1 )
6644                         {
6645                          #ifdef MOD_GZIP_CHUNKE_DIAG1
6646                          mod_gzip_printf("chunke: %5.5ld END OF CHUNKED DATA\n",
6647                                           chunke_count);
6648                          #endif
6649                         }
6650
6651                       chunke_realdata_read = 0;
6652
6653                       if ( *sp == 10 ) 
6654                         {
6655                          if ( chunke_realdata_length == 0 )
6656                            {
6657                             chunke_state = CHUNKE_GET_FOOTER;
6658                            }
6659                          else 
6660                            {
6661                             chunke_state = CHUNKE_GET_REAL_DATA;
6662                            }
6663                         }
6664                       else 
6665                         {
6666                          chunke_state = CHUNKE_WAIT_REAL_EOL1;
6667
6668                          if ( chunke_realdata_length == 0 )
6669                            {
6670                             chunke_nextstate = CHUNKE_GET_FOOTER;
6671                            }
6672                          else 
6673                            {
6674                             chunke_nextstate = CHUNKE_GET_REAL_DATA;
6675                            }
6676                         }
6677                      }
6678
6679                    else 
6680                      {
6681                       if ( ( chunke_hexstring_oset <
6682                              CHUNKE_HEXSTRING_MAXLEN ) &&
6683                            ( *sp > 32 ) )
6684                         {
6685                          chunke_hexstring[
6686                          chunke_hexstring_oset++] = *sp;
6687
6688                          #ifdef MOD_GZIP_CHUNKE_DIAG1
6689                          chunke_hexstring[
6690                          chunke_hexstring_oset ] = 0;
6691                          #endif
6692                         }
6693
6694                       #ifdef MOD_GZIP_CHUNKE_DIAG1
6695                       mod_gzip_printf("chunke: %5.5ld chunke_hexstring is now [%s]\n",
6696                                        chunke_count, npp(chunke_hexstring) );
6697                       #endif
6698                      }
6699
6700                    break;
6701
6702               case CHUNKE_WAIT_REAL_EOL1:
6703
6704                    if ( *sp == 10 )
6705                      {
6706                       #ifdef MOD_GZIP_CHUNKE_DIAG1
6707                       mod_gzip_printf("chunke: %5.5ld Real EOL seen...\n",
6708                                        chunke_count );
6709                       #endif
6710
6711                       chunke_state = chunke_nextstate;
6712                      }
6713
6714                    break;
6715
6716               case CHUNKE_GET_FOOTER:
6717
6718                    if ( *sp == 10 )
6719                      {
6720                       #ifdef MOD_GZIP_CHUNKE_DIAG1
6721                       mod_gzip_printf("chunke: %5.5ld FOOTER EOL seen...\n",
6722                                        chunke_count );
6723                       #endif
6724                      }
6725
6726                    break;
6727
6728               case CHUNKE_GET_REAL_DATA:
6729
6730                    #ifdef CHUNKE_USES_OUTPUT_BUFFER
6731
6732                    *chunke_obuf++ = *sp;
6733                     chunke_obuflen++;
6734
6735                    if ( chunke_obuflen >= chunke_obuflenmax )
6736                      {
6737                       byteswritten =
6738                       fwrite(
6739                       chunke_obufstart,
6740                       1,
6741                       chunke_obuflen,
6742                       ofh
6743                       );
6744
6745                       if ( byteswritten > 0 )
6746                         {
6747                          bbytes_out += byteswritten;
6748                         }
6749
6750                       chunke_obuf    = chunke_obufstart;
6751                       chunke_obuflen = 0;
6752                      }
6753
6754                    chunke_realdata_read++;
6755
6756                    #else 
6757
6758                    bytestowrite = bytesleft; 
6759
6760                    if ( ( chunke_realdata_read + bytesleft ) >
6761                           chunke_realdata_length )
6762                      {
6763                       bytestowrite =
6764                       chunke_realdata_length - chunke_realdata_read;
6765                      }
6766
6767                    byteswritten =
6768                    fwrite(
6769                    sp,
6770                    1,
6771                    bytestowrite,
6772                    ofh
6773                    );
6774
6775                    if ( byteswritten > 0 )
6776                      {
6777                       sp                   += byteswritten;
6778                       bytesleft            -= byteswritten;
6779                       bbytes_out           += byteswritten;
6780                       chunke_realdata_read += byteswritten;
6781
6782                       skip_advance = 1;
6783                      }
6784
6785                    #endif 
6786
6787                    if ( chunke_realdata_read >=
6788                         chunke_realdata_length )
6789                      {
6790                       #ifdef CHUNKE_USES_OUTPUT_BUFFER
6791
6792                       if ( chunke_obuflen > 0  )
6793                         {
6794                          byteswritten =
6795                          fwrite(
6796                          chunke_obufstart,
6797                          1,
6798                          chunke_obuflen,
6799                          ofh
6800                          );
6801
6802                          if ( byteswritten > 0 )
6803                            {
6804                             bbytes_out += byteswritten;
6805                            }
6806
6807                          chunke_obuf    = chunke_obufstart;
6808                          chunke_obuflen = 0;
6809                         }
6810
6811                       #endif 
6812
6813                       #ifdef MOD_GZIP_CHUNKE_DIAG1
6814                       mod_gzip_printf("chunke: %5.5ld DONE reading CHUNK data...\n",
6815                                  chunke_count );
6816                       mod_gzip_printf("chunke: %5.5ld realdata_length = %ld\n",
6817                                  chunke_count, chunke_realdata_length );
6818                       mod_gzip_printf("chunke: %5.5ld realdata_read   = %ld\n",
6819                                  chunke_count, chunke_realdata_read );
6820                       mod_gzip_printf("chunke: %5.5ld Increasing 'chunke_count' to %d\n",
6821                                  chunke_count, chunke_count + 1 );
6822                       mod_gzip_printf("chunke: %5.5ld Advancing to CHUNKE_WAIT_REAL_EOL1...\n",
6823                                  chunke_count );
6824                       #endif
6825
6826                       chunke_count++;
6827
6828                       chunke_hexstring[0]    = 0;
6829                       chunke_hexstring_oset  = 0;
6830                       chunke_realdata_length = 0;
6831                       chunke_realdata_read   = 0;
6832
6833                       chunke_state     = CHUNKE_WAIT_REAL_EOL1;
6834                       chunke_nextstate = CHUNKE_GET_HEXSTRING;
6835                      }
6836
6837                    break;
6838
6839               default: break;
6840              }
6841
6842            if ( skip_advance ) 
6843              {
6844               skip_advance = 0; 
6845              }
6846            else 
6847              {
6848               sp++;        
6849               bytesleft--; 
6850              }
6851           }
6852
6853         #ifdef FUTURE_USE
6854         chunke_buffer_scan_done: ; 
6855         #endif
6856
6857         #ifdef MOD_GZIP_CHUNKE_DIAG1
6858         mod_gzip_printf("chunke: %5.5ld Out of next buffer scan loop\n",
6859                    chunke_count );
6860         mod_gzip_printf("chunke: %5.5ld realdata_length = %ld\n",
6861                    chunke_count, chunke_realdata_length );
6862         mod_gzip_printf("chunke: %5.5ld realdata_read   = %ld\n",
6863                    chunke_count, chunke_realdata_read );
6864         mod_gzip_printf("chunke: %5.5ld realdata_left   = %ld\n",
6865                    chunke_count, chunke_realdata_length - chunke_realdata_read);
6866         mod_gzip_printf("chunke: %5.5ld Looping back for more input data...\n",
6867                    chunke_count );
6868         #endif
6869        }
6870
6871      else 
6872        {
6873         byteswritten =
6874         fwrite(
6875         p1,
6876         1,
6877         bytesread,
6878         ofh
6879         );
6880
6881         if ( byteswritten > 0 )
6882           {
6883            bbytes_out += byteswritten; 
6884           }
6885
6886         #ifdef MOD_GZIP_DEBUG1
6887         mod_gzip_printf( "%s: byteswritten = %d",cn,(int)byteswritten);
6888         #endif
6889
6890         if ( byteswritten != bytesread )
6891           {
6892            err = errno;
6893
6894            #ifdef FUTURE_USE
6895            #if defined(WIN32) || defined(NETWARE)
6896            err = WSAGetLastError();
6897            #else  
6898            err = errno;
6899            #endif 
6900            #endif 
6901
6902            #ifdef MOD_GZIP_DEBUG1
6903            mod_gzip_printf( "%s: TRANSMIT ERROR: bytesread=%d byteswritten=%d err=%d",
6904            cn,(int)bytesread,(int)byteswritten,(int)err );
6905            #endif
6906
6907            #ifdef MOD_GZIP_DEBUG1
6908            mod_gzip_printf( "%s: Breaking out of transmit loop early...",cn);
6909            #endif
6910
6911            body_done = 1; 
6912            break;         
6913           }
6914        }
6915
6916      if ( body_done ) break;
6917     }
6918
6919  bbytes_total = bbytes_out;
6920
6921  #ifdef MOD_GZIP_DEBUG1
6922  mod_gzip_printf( "%s: Done processing BODY...",cn);
6923  mod_gzip_printf( "%s: Call fclose(ofh)...",cn);
6924  #endif
6925
6926  fclose( ofh ); ofh = 0;
6927
6928  #ifdef MOD_GZIP_DEBUG1
6929  mod_gzip_printf( "%s: Back fclose(ofh)...",cn);
6930  mod_gzip_printf( "%s: bbytes_in    = %ld (Total BODY bytes read)",cn,(long)bbytes_in);
6931  mod_gzip_printf( "%s: bbytes_out   = %ld (Total BODY bytes written)",cn,(long)bbytes_out);
6932  mod_gzip_printf( "%s: bbytes_total = %ld (Same as bbytes_out)",cn,(long)bbytes_total);
6933  #endif
6934
6935  if ( bbytes_total > 0 )
6936    {
6937     #ifdef MOD_GZIP_DEBUG1
6938     mod_gzip_printf( "%s: Call fclose(%s)...",cn, npp(input_filename));
6939     #endif
6940
6941     fclose( ifh ); ifh = 0;
6942
6943     #ifdef MOD_GZIP_DEBUG1
6944     mod_gzip_printf( "%s: Back fclose(%s)...",cn, npp(input_filename));
6945     #endif
6946
6947     #ifdef MOD_GZIP_DEBUG1
6948     mod_gzip_printf( "%s: Call mod_gzip_encode_and_transmit()...",cn);
6949     #endif
6950
6951     rc =
6952     mod_gzip_encode_and_transmit(
6953     r,                
6954     dconf,            
6955     output_filename1, 
6956     1,                
6957     bbytes_total,     
6958     0,                
6959     hbytes_in,        
6960     "DECHUNK:"        
6961     );
6962
6963     #ifdef MOD_GZIP_DEBUG1
6964     mod_gzip_printf( "%s: Back mod_gzip_encode_and_transmit()...",cn);
6965     mod_gzip_printf( "%s: rc = %d",cn,rc);
6966     #endif
6967
6968     #ifdef MOD_GZIP_DEBUG1
6969     mod_gzip_printf( "%s: dconf__keep_workfiles = %d",
6970                       cn, dconf__keep_workfiles );
6971     #endif
6972
6973     if ( !dconf__keep_workfiles ) 
6974       {
6975        #ifdef MOD_GZIP_DEBUG1
6976        mod_gzip_printf( "%s: Call mod_gzip_delete_file(output_filename1=[%s])...",
6977                          cn, npp(output_filename1));
6978        #endif
6979
6980        mod_gzip_delete_file( r, output_filename1 );
6981
6982        #ifdef MOD_GZIP_DEBUG1
6983        mod_gzip_printf( "%s: Back mod_gzip_delete_file(output_filename1=[%s])...",
6984                          cn, npp(output_filename1));
6985        #endif
6986       }
6987
6988     if ( rc == DECLINED )
6989       {
6990        #ifdef MOD_GZIP_DEBUG1
6991        mod_gzip_printf( "%s: rc = DECLINED (FAILURE)",cn);
6992        mod_gzip_printf( "%s: Sending response 'as is'...",cn);
6993        mod_gzip_printf( "%s: Issuing 'goto mod_gzip_sendfile2_send_as_is'...",cn);
6994        #endif
6995
6996        goto mod_gzip_sendfile2_send_as_is;
6997       }
6998
6999     else 
7000       {
7001        #ifdef MOD_GZIP_DEBUG1
7002        mod_gzip_printf( "%s: rc = OK (SUCCESS)",cn);
7003        mod_gzip_printf( "%s: Call goto mod_gzip_sendfile2_cleanup...",cn,rc);
7004        #endif
7005
7006        final_rc = rc; 
7007
7008        goto mod_gzip_sendfile2_cleanup;
7009       }
7010    }
7011
7012  else 
7013    {
7014     #ifdef MOD_GZIP_DEBUG1
7015     mod_gzip_printf( "%s: ERROR: bbytes_total is ZERO",cn);
7016     mod_gzip_printf( "%s: ERROR: Original response must be sent 'as is'",cn);
7017     mod_gzip_printf( "%s: Issuing 'goto mod_gzip_sendfile2_send_as_is'...",cn);
7018     #endif
7019
7020     #ifdef MOD_GZIP_USES_APACHE_LOGS
7021
7022     ap_table_setn( r->notes,"mod_gzip_result",
7023     ap_pstrdup(r->pool,"SEND_AS_IS:BODY_MISSING"));
7024     #endif
7025
7026     goto mod_gzip_sendfile2_send_as_is;
7027    }
7028
7029  mod_gzip_sendfile2_send_as_is: ;
7030
7031  #ifdef MOD_GZIP_DEBUG1
7032  mod_gzip_printf( "%s: * SEND 'AS IS'...",cn);
7033  mod_gzip_printf( "%s: * label entry point: mod_gzip_sendfile2_send_as_is",cn);
7034  mod_gzip_printf( "%s: Sending response 'as is'...",cn);
7035  #endif
7036
7037  if ( !ifh )
7038    {
7039     #ifdef MOD_GZIP_DEBUG1
7040     mod_gzip_printf( "%s: Input file was closed.",cn);
7041     mod_gzip_printf( "%s: Re-opening input_filename[%s]...",
7042                       cn, npp(input_filename));
7043     #endif
7044
7045     ifh = fopen( input_filename, "rb" ); 
7046
7047     if ( !ifh ) 
7048       {
7049        #ifdef MOD_GZIP_DEBUG1
7050        mod_gzip_printf( "%s: .... fopen() FAILED",cn);
7051        #endif
7052
7053        ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
7054        "mod_gzip: fopen(%s) on REOPEN in sendfile2", input_filename );
7055
7056        final_rc = DECLINED;
7057
7058        goto mod_gzip_sendfile2_cleanup;
7059       }
7060
7061     #ifdef MOD_GZIP_DEBUG1
7062     mod_gzip_printf( "%s: .... fopen() SUCCEEDED",cn);
7063     #endif
7064    }
7065
7066  #ifdef MOD_GZIP_DEBUG1
7067  mod_gzip_printf( "%s: Call mod_gzip_sendfile1()...",cn);
7068  #endif
7069
7070  total_bytes_sent = (long)
7071  mod_gzip_sendfile1(
7072  r,    
7073  NULL, 
7074  ifh,  
7075  0     
7076  );
7077
7078  #ifdef MOD_GZIP_DEBUG1
7079  mod_gzip_printf( "%s: Back mod_gzip_sendfile1()...",cn);
7080  mod_gzip_printf( "%s: .... total_bytes_sent = %ld",cn,(long)total_bytes_sent);
7081  #endif
7082
7083  #ifdef MOD_GZIP_DEBUG1
7084  mod_gzip_printf( "%s: Call mod_gzip_flush_and_update_counts()...",cn);
7085  #endif
7086
7087  mod_gzip_flush_and_update_counts(
7088  r,
7089  dconf,
7090  hbytes_in,                   
7091  total_bytes_sent - hbytes_in 
7092  );
7093
7094  #ifdef MOD_GZIP_DEBUG1
7095  mod_gzip_printf( "%s: Back mod_gzip_flush_and_update_counts()...",cn);
7096  #endif
7097
7098  final_rc = rc; 
7099
7100  mod_gzip_sendfile2_cleanup: ;
7101
7102  #ifdef MOD_GZIP_DEBUG1
7103  mod_gzip_printf( "%s: * FINAL CLEANUP",cn);
7104  mod_gzip_printf( "%s: * label entry point: mod_gzip_sendfile2_cleanup",cn);
7105  mod_gzip_printf( "%s: dconf__keep_workfiles = %ld",cn,(long)dconf__keep_workfiles);
7106  mod_gzip_printf( "%s: ifh = %ld (Input  file handle)",cn,(long)ifh);
7107  #endif
7108
7109  if ( ifh )
7110    {
7111     #ifdef MOD_GZIP_DEBUG1
7112     mod_gzip_printf( "%s: Input file is still OPEN...",cn );
7113     mod_gzip_printf( "%s: Call fclose(%s)...",cn, npp(input_filename));
7114     #endif
7115
7116     fclose( ifh ); ifh = 0;
7117
7118     #ifdef MOD_GZIP_DEBUG1
7119     mod_gzip_printf( "%s: Back fclose(%s)...",cn,npp(input_filename));
7120     #endif
7121    }
7122  #ifdef MOD_GZIP_DEBUG1
7123  else
7124    {
7125     mod_gzip_printf( "%s: Input file is already CLOSED...",cn );
7126     mod_gzip_printf( "%s: NO CALL MADE TO fclose(%s)",cn,npp(input_filename));
7127    }
7128  #endif
7129
7130  #ifdef MOD_GZIP_DEBUG1
7131  mod_gzip_printf( "%s: Caller must delete input_filename=[%s]",
7132                    cn, npp(input_filename));
7133  #endif
7134
7135  #ifdef MOD_GZIP_DEBUG1
7136  mod_gzip_printf( "%s: ofh_used = %ld (Output file usage flag)",cn,(long)ofh_used);
7137  mod_gzip_printf( "%s: ofh      = %ld (Output file handle)",cn,(long)ofh);
7138  #endif
7139
7140  if ( ofh_used ) 
7141    {
7142     #ifdef MOD_GZIP_DEBUG1
7143     mod_gzip_printf( "%s: 'ofh_used' is TRUE",cn );
7144     #endif
7145
7146     if ( ofh ) 
7147       {
7148        #ifdef MOD_GZIP_DEBUG1
7149        mod_gzip_printf( "%s: Output file is still OPEN...",cn );
7150        mod_gzip_printf( "%s: Call fclose(output_filename1=[%s])...",
7151                          cn, npp(output_filename1));
7152        #endif
7153
7154        fclose( ofh ); ofh = 0;
7155
7156        #ifdef MOD_GZIP_DEBUG1
7157        mod_gzip_printf( "%s: Back fclose(output_filename1=[%s])...",
7158                          cn, npp(output_filename1));
7159        #endif
7160       }
7161     #ifdef MOD_GZIP_DEBUG1
7162     else
7163       {
7164        mod_gzip_printf( "%s: 'ofh' is NULL. File is already CLOSED.",cn );
7165       }
7166     #endif
7167
7168     if ( !dconf__keep_workfiles )
7169       {
7170        #ifdef MOD_GZIP_DEBUG1
7171        mod_gzip_printf( "%s: 'dconf__keep_workfiles' is FALSE",cn );
7172        mod_gzip_printf( "%s: Call mod_gzip_delete_file(output_filename1=[%s])...",
7173                          cn, npp(output_filename1));
7174        #endif
7175
7176        mod_gzip_delete_file( r, output_filename1 );
7177
7178        #ifdef MOD_GZIP_DEBUG1
7179        mod_gzip_printf( "%s: Back mod_gzip_delete_file(output_filename1=[%s])...",
7180                          cn, npp(output_filename1));
7181        #endif
7182       }
7183
7184     #ifdef MOD_GZIP_DEBUG1
7185     else
7186       {
7187        mod_gzip_printf( "%s: 'dconf__keep_workfiles' is TRUE.",cn );
7188        mod_gzip_printf( "%s: Keeping output_filename1=[%s]",
7189                          cn, npp(output_filename1));
7190       }
7191     #endif
7192
7193     ofh_used = 0; 
7194    }
7195  #ifdef MOD_GZIP_DEBUG1
7196  else
7197    {
7198     mod_gzip_printf( "%s: 'ofh_used' is FALSE",cn );
7199     mod_gzip_printf( "%s: Output file was NOT USED",cn );
7200    }
7201  #endif
7202
7203  #ifdef MOD_GZIP_DEBUG1
7204  mod_gzip_printf( "%s: final_rc was......: %d",cn,final_rc);
7205  #endif
7206
7207  final_rc = OK;
7208
7209  #ifdef MOD_GZIP_DEBUG1
7210  mod_gzip_printf( "%s: final_rc is now...: %d",cn,final_rc);
7211  #endif
7212
7213  #ifdef MOD_GZIP_DEBUG1
7214  mod_gzip_printf( "%s: Exit > return( final_rc = %d ) >",cn,final_rc);
7215  #endif
7216
7217  return final_rc; 
7218 }
7219
7220 long mod_gzip_send_header(
7221 request_rec *r,
7222 char        *input_filename,
7223 long         content_length
7224 )
7225 {
7226  #ifdef MOD_GZIP_DEBUG1
7227  char cn[]="mod_gzip_send_header()";
7228  #endif
7229
7230  FILE *ifh=0;
7231
7232  int i;
7233  int bytesread=0;
7234  int ok_to_send=0;
7235  int valid_char_count=0;
7236
7237  #define   MOD_GZIP_SEND_HEADER_BUFFER_SIZE 4096
7238  char tmp[ MOD_GZIP_SEND_HEADER_BUFFER_SIZE + 16 ];
7239
7240  #define MOD_GZIP_LINE_BUFFER_SIZE 2048
7241  char lbuf[ MOD_GZIP_LINE_BUFFER_SIZE + 16 ];
7242
7243  char *p1;
7244  int   p1len = 0;
7245
7246  char *p2    = lbuf;
7247  int   p2len = 0;
7248
7249  int  send_header = 0; 
7250  int  header_done = 0; 
7251  int  te_seen     = 0; 
7252  int  te_chunked  = 0; 
7253  int  ce_seen     = 0; 
7254
7255  long hbytes_in   = 0; 
7256  long hbytes_out  = 0; 
7257
7258  #ifdef MOD_GZIP_DEBUG1
7259  mod_gzip_printf( "%s: Entry...",cn);
7260  mod_gzip_printf( "%s: input_filename = [%s]",cn,npp(input_filename));
7261  mod_gzip_printf( "%s: content_length = %ld", cn,(long)content_length);
7262  #endif
7263
7264  if ( !r              ) return 0;
7265  if ( !input_filename ) return 0;
7266
7267  #ifdef MOD_GZIP_DEBUG1
7268  mod_gzip_printf( "%s: Call fopen(%s)...",cn,npp(input_filename));
7269  #endif
7270
7271  ifh = fopen( input_filename, "rb" ); 
7272
7273  if ( !ifh ) 
7274    {
7275     #ifdef MOD_GZIP_DEBUG1
7276     mod_gzip_printf( "%s: .... fopen() FAILED",cn);
7277     mod_gzip_printf( "%s: Exit > return( 0 ) >",cn);
7278     #endif
7279
7280     return( 0 ); 
7281    }
7282
7283  #ifdef MOD_GZIP_DEBUG1
7284  mod_gzip_printf( "%s: .... fopen() SUCCEEDED",cn);
7285  mod_gzip_printf( "%s: sizeof( tmp ) = %d",cn,sizeof(tmp));
7286  mod_gzip_printf( "%s: MOD_GZIP_SEND_HEADER_BUFFER_SIZE = %d",
7287                cn,(int)MOD_GZIP_SEND_HEADER_BUFFER_SIZE);
7288  mod_gzip_printf( "%s: Processing header now...",cn);
7289  #endif
7290
7291  send_header = 1;
7292
7293  for (;;)
7294     {
7295      bytesread = fread( tmp, 1, MOD_GZIP_SEND_HEADER_BUFFER_SIZE, ifh );
7296
7297      #ifdef MOD_GZIP_DEBUG1
7298      mod_gzip_printf( "%s: HEADER: Back fread(): bytesread=%d",cn,bytesread);
7299      #endif
7300
7301      if ( bytesread < 1 ) break; 
7302
7303      p1    = tmp;
7304      p1len = 0;
7305
7306      for ( i=0; i<bytesread; i++ )
7307         {
7308          if ( *p1 == 10 )
7309            {
7310             *p2 = 0; 
7311
7312             if ( valid_char_count < 1 )
7313               {
7314                p1++;         
7315                p1len++;      
7316                hbytes_in++;  
7317
7318                sprintf( lbuf, "Content-Encoding: gzip" );
7319
7320                #ifdef MOD_GZIP_DEBUG1
7321                mod_gzip_printf( "%s: HEADER: ADDING: lbuf=[%s]",cn,npp(lbuf));
7322                #endif
7323
7324                mod_gzip_strcat( lbuf, "\r\n" );
7325
7326                if ( send_header )
7327                  {
7328                   hbytes_out += mod_gzip_send(lbuf,mod_gzip_strlen(lbuf),r);
7329                  }
7330
7331                sprintf( lbuf, "Content-Length: %ld",(long)content_length);
7332
7333                #ifdef MOD_GZIP_DEBUG1
7334                mod_gzip_printf( "%s: HEADER: ADDING: lbuf=[%s]",cn,npp(lbuf));
7335                #endif
7336
7337                mod_gzip_strcat( lbuf, "\r\n" );
7338
7339                if ( send_header )
7340                  {
7341                   hbytes_out += mod_gzip_send(lbuf,mod_gzip_strlen(lbuf),r);
7342                  }
7343
7344                #ifdef MOD_GZIP_DEBUG1
7345                mod_gzip_printf( "%s: HEADER: ADDING: lbuf=[CR/LF]",cn);
7346                #endif
7347
7348                if ( send_header )
7349                  {
7350                   hbytes_out += mod_gzip_send("\r\n",2,r);
7351                  }
7352
7353                header_done = 1; 
7354
7355                break; 
7356               }
7357             else 
7358               {
7359                ok_to_send = 1; 
7360
7361                if ( lbuf[0] == 'T' )
7362                  {
7363                   if ( mod_gzip_strnicmp(lbuf,"Transfer-Encoding:",18)==0)
7364                     {
7365                      #ifdef MOD_GZIP_DEBUG1
7366                      mod_gzip_printf( "%s: HEADER: * 'Transfer-Encoding:' seen",cn);
7367                      #endif
7368
7369                      te_seen = 1; 
7370
7371                      if ( mod_gzip_stringcontains( lbuf, "chunked" ) )
7372                        {
7373                         #ifdef MOD_GZIP_DEBUG1
7374                         mod_gzip_printf( "%s: HEADER: * 'Transfer-Encoding: chunked' seen",cn);
7375                         #endif
7376
7377                         te_chunked = 1; 
7378                         ok_to_send = 0; 
7379                        }
7380                     }
7381                  }
7382
7383                #ifdef FUTURE_USE
7384                else if ( lbuf[0] == 'E' )
7385                  {
7386                   if ( mod_gzip_strnicmp(lbuf,"ETag:",5)==0)
7387                     {
7388                      ok_to_send = 0; 
7389                     }
7390                  }
7391                #endif
7392
7393                else if ( lbuf[0] == 'C' )
7394                  {
7395                   if ( mod_gzip_strnicmp(lbuf,"Content-Encoding:",17)==0)
7396                     {
7397                      ce_seen = 1; 
7398                     }
7399                   else if ( mod_gzip_strnicmp(lbuf,"Content-Length:",15)==0)
7400                     {
7401                      ok_to_send = 0; 
7402                     }
7403                  }
7404
7405                if ( ok_to_send )
7406                  {
7407                   #ifdef MOD_GZIP_DEBUG1
7408                   mod_gzip_printf( "%s: HEADER: Sending lbuf=[%s]",cn,npp(lbuf));
7409                   #endif
7410
7411                   *p2++ = 13; p2len++;
7412                   *p2++ = 10; p2len++;
7413                   *p2++ = 0;
7414
7415                   if ( send_header )
7416                     {
7417                      hbytes_out += mod_gzip_send( lbuf, p2len, r );
7418                     }
7419                  }
7420                #ifdef MOD_GZIP_DEBUG1
7421                else
7422                  {
7423                   #ifdef MOD_GZIP_DEBUG1
7424                   mod_gzip_printf( "%s: HEADER: NOSEND: lbuf=[%s]",cn,npp(lbuf));
7425                   #endif
7426                  }
7427                #endif
7428               }
7429
7430             p2    = lbuf;
7431             p2len = 0;
7432
7433             valid_char_count = 0;
7434            }
7435          else 
7436            {
7437             if ( *p1 > 32 ) valid_char_count++;
7438
7439             if (( p2len < MOD_GZIP_LINE_BUFFER_SIZE )&&( *p1 != 13 ))
7440               {
7441                *p2++ = *p1;
7442                p2len++;
7443               }
7444            }
7445
7446          p1++;        
7447          p1len++;     
7448          hbytes_in++; 
7449         }
7450
7451      if ( header_done ) break;
7452     }
7453
7454  #ifdef MOD_GZIP_DEBUG1
7455  mod_gzip_printf( "%s: Done processing header...",cn);
7456  #endif
7457
7458  #ifdef MOD_GZIP_DEBUG1
7459  mod_gzip_printf( "%s: Call fclose(%s)...",cn,npp(input_filename));
7460  #endif
7461
7462  fclose( ifh ); ifh = 0;
7463
7464  #ifdef MOD_GZIP_DEBUG1
7465  mod_gzip_printf( "%s: Back fclose(%s)...",cn,npp(input_filename));
7466  #endif
7467
7468  #ifdef MOD_GZIP_DEBUG1
7469  mod_gzip_printf( "%s: p1len        = %d",cn,p1len);
7470  mod_gzip_printf( "%s: hbytes_in    = %ld (Total HEADER bytes read)",cn,(long)hbytes_in);
7471  mod_gzip_printf( "%s: hbytes_out   = %ld (Total HEADER bytes sent)",cn,(long)hbytes_out);
7472  #endif
7473
7474  #ifdef MOD_GZIP_DEBUG1
7475  mod_gzip_printf( "%s: Exit > return( hbytes_out = %d ) >",cn,hbytes_out);
7476  #endif
7477
7478  return (long) hbytes_out; 
7479 }
7480
7481 FILE *mod_gzip_open_output_file(
7482 request_rec *r,
7483 char *output_filename,
7484 int  *rc
7485 )
7486 {
7487  FILE *ifh;
7488
7489  #ifdef MOD_GZIP_DEBUG1
7490  char cn[]="mod_gzip_open_output_file():::";
7491  #endif
7492
7493  #ifdef MOD_GZIP_DEBUG1
7494  mod_gzip_printf( "%s: Entry...",cn);
7495  mod_gzip_printf( "%s: output_filename=[%s]",cn,npp(output_filename));
7496  #endif
7497
7498  ifh = fopen( output_filename, "rb" ); 
7499
7500  if ( !ifh ) 
7501    {
7502     #ifdef MOD_GZIP_DEBUG1
7503     mod_gzip_printf( "%s: ERROR: Cannot open file [%s]",
7504                       cn,npp(output_filename));
7505     #endif
7506
7507     ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
7508     "mod_gzip: Cannot re-open output_filename=[%s]",
7509     output_filename );
7510
7511     #ifdef MOD_GZIP_USES_APACHE_LOGS
7512
7513     ap_table_setn(
7514     r->notes,"mod_gzip_result",ap_pstrdup(r->pool,"SEND_AS_IS:WORK_OPENFAIL"));
7515
7516     #endif 
7517
7518     #ifdef MOD_GZIP_DEBUG1
7519     mod_gzip_printf( "%s: Exit > return( NULL ) >",cn);
7520     #endif
7521
7522     *rc = DECLINED; 
7523
7524     return NULL;
7525    }
7526
7527  #ifdef MOD_GZIP_DEBUG1
7528  mod_gzip_printf( "%s: File is now open...",cn);
7529  mod_gzip_printf( "%s: Exit > return( FILE *ifh ) >",cn);
7530  #endif
7531
7532  *rc = OK; 
7533
7534  return ifh; 
7535 }
7536
7537 int mod_gzip_flush_and_update_counts(
7538 request_rec   *r,
7539 mod_gzip_conf *dconf,
7540 long           total_header_bytes_sent,
7541 long           total_body_bytes_sent
7542 )
7543 {
7544  #ifdef MOD_GZIP_DEBUG1
7545  char cn[]="mod_gzip_flush_and_update_counts()";
7546  #endif
7547
7548  #ifdef MOD_GZIP_DEBUG1
7549  mod_gzip_printf( "%s: Entry",cn);
7550  mod_gzip_printf( "%s: total_header_bytes_sent = %ld",
7551               cn,(long)total_header_bytes_sent);
7552  mod_gzip_printf( "%s: total_body_bytes_sent   = %ld",
7553               cn,(long)total_body_bytes_sent);
7554  mod_gzip_printf( "%s: total_bytes_sent (Sum)  = %ld",
7555               cn,(long)total_header_bytes_sent +
7556                        total_body_bytes_sent );
7557  mod_gzip_printf( "%s: dconf->add_header_count = %d",
7558               cn,(int )dconf->add_header_count );
7559  #endif
7560
7561  #ifdef MOD_GZIP_DEBUG1
7562  mod_gzip_printf( "%s: Before... ap_rflush()...",cn);
7563  mod_gzip_printf( "%s: r->connection->client->outcnt     = %ld",
7564              cn,(long) r->connection->client->outcnt );
7565  mod_gzip_printf( "%s: r->connection->client->bytes_sent = %ld",
7566              cn,(long) r->connection->client->bytes_sent );
7567  mod_gzip_printf( "%s: Call..... ap_rflush()...",cn);
7568  #endif
7569
7570  ap_rflush(r);
7571
7572  #ifdef MOD_GZIP_DEBUG1
7573  mod_gzip_printf( "%s: Back..... ap_rflush()...",cn);
7574  mod_gzip_printf( "%s: After.... ap_rflush()...",cn);
7575  mod_gzip_printf( "%s: r->connection->client->outcnt     = %ld",
7576              cn,(long) r->connection->client->outcnt );
7577  mod_gzip_printf( "%s: r->connection->client->bytes_sent = %ld",
7578              cn,(long) r->connection->client->bytes_sent );
7579  #endif
7580
7581  #ifdef MOD_GZIP_DEBUG1
7582  mod_gzip_printf( "%s: dconf->add_header_count = %ld",
7583               cn,(long)dconf->add_header_count);
7584  #endif
7585
7586  if ( dconf->add_header_count )
7587    {
7588     r->connection->client->bytes_sent =
7589     total_header_bytes_sent + total_body_bytes_sent;
7590    }
7591  else 
7592    {
7593     r->connection->client->bytes_sent = total_body_bytes_sent;
7594    }
7595
7596  #ifdef MOD_GZIP_DEBUG1
7597  mod_gzip_printf( "%s: After count update...",cn);
7598  mod_gzip_printf( "%s: r->connection->client->outcnt     = %ld",
7599              cn,(long) r->connection->client->outcnt );
7600  mod_gzip_printf( "%s: r->connection->client->bytes_sent = %ld",
7601              cn,(long) r->connection->client->bytes_sent );
7602  mod_gzip_printf( "%s: Exit > return( 1 ) >",cn);
7603  #endif
7604
7605  return 1;
7606 }
7607
7608 int mod_gzip_encode_and_transmit(
7609 request_rec   *r,
7610 mod_gzip_conf *dconf,
7611 char          *source,
7612 int            source_is_a_file,
7613 long           input_size,
7614 int            nodecline,
7615 long           header_length,
7616 char          *result_prefix_string
7617 )
7618 {
7619  char *prefix_string = result_prefix_string;
7620
7621  GZP_CONTROL   gzc;
7622  GZP_CONTROL*  gzp = &gzc;
7623
7624  int   rc                = 0;
7625  int   err               = 0;
7626  FILE *ifh               = 0;
7627  int   bytesread         = 0;
7628  long  byteswritten      = 0;
7629  long  output_size       = 0;
7630  long  compression_ratio = 0;
7631  char *gz1_ismem_obuf    = 0;
7632  int   finalize_stats    = 1;
7633  long  output_offset     = 0;
7634
7635  long  total_bytes_sent             = 0;
7636  long  total_header_bytes_sent      = 0;
7637  long  total_compressed_bytes_sent  = 0;
7638  int   gz1_ismem_obuf_was_allocated = 0;
7639
7640  #define MOD_GZIP_LARGE_BUFFER_SIZE 4000
7641
7642  char tmp[ MOD_GZIP_LARGE_BUFFER_SIZE + 2 ];
7643
7644  char actual_content_encoding_name[] = "gzip";
7645
7646  char dummy_result_prefix_string[] = "";
7647
7648  #ifdef MOD_GZIP_DEBUG1
7649  char scratch2[90];
7650  #endif
7651
7652  #ifdef MOD_GZIP_DEBUG1
7653  char cn[]="mod_gzip_encode_and_transmit()";
7654  #endif
7655
7656  #ifdef MOD_GZIP_USES_APACHE_LOGS
7657  char log_info[90];
7658  #endif
7659
7660  char *dconf__temp_dir           = 0;
7661  int   dconf__keep_workfiles     = 0;
7662  long  dconf__minimum_file_size  = 300;
7663  long  dconf__maximum_file_size  = 0;
7664  long  dconf__maximum_inmem_size = 0;
7665
7666  #ifdef MOD_GZIP_DEBUG1
7667  mod_gzip_printf( "%s: Entry...", cn);
7668  #endif
7669
7670  /*
7671   * Initialize the GZP control deck on the stack with some
7672   * safe default values. As of this writing the filename and
7673   * scratch buffers do not need to be fully reset on entry
7674   * so there is no need to suffer the overhead of a full
7675   * 'memset( gzp, 0, sizeof( GZP_CONTROL )' call.
7676   */
7677
7678  gzp->decompress           = 0;
7679  gzp->input_ismem          = 0;
7680  gzp->input_ismem_ibuf     = 0;
7681  gzp->input_ismem_ibuflen  = 0;
7682  gzp->input_filename[0]    = 0;
7683  gzp->input_offset         = header_length;
7684  gzp->output_ismem         = 0;
7685  gzp->output_ismem_obuf    = 0;
7686  gzp->output_ismem_obuflen = 0;
7687  gzp->output_filename[0]   = 0;
7688  gzp->result_code          = 0;
7689  gzp->bytes_out            = 0;
7690
7691  if ( dconf )
7692    {
7693     dconf__keep_workfiles     = dconf->keep_workfiles;
7694     dconf__minimum_file_size  = dconf->minimum_file_size;
7695     dconf__maximum_file_size  = dconf->maximum_file_size;
7696     dconf__maximum_inmem_size = dconf->maximum_inmem_size;
7697     dconf__temp_dir           = dconf->temp_dir;
7698    }
7699
7700  #ifdef MOD_GZIP_DEBUG1
7701
7702  mod_gzip_printf( "%s: dconf__keep_workfiles     = %d",  cn, (int) dconf__keep_workfiles );
7703  mod_gzip_printf( "%s: dconf__temp_dir           = [%s]",cn, npp(dconf__temp_dir));
7704  mod_gzip_printf( "%s: dconf__minimum_file_size  = %ld", cn, (long) dconf__minimum_file_size );
7705  mod_gzip_printf( "%s: dconf__maximum_file_size  = %ld", cn, (long) dconf__maximum_file_size );
7706  mod_gzip_printf( "%s: dconf__maximum_inmem_size = %ld", cn, (long) dconf__maximum_inmem_size );
7707
7708  mod_gzip_printf( "%s: source_is_a_file = %d",  cn, source_is_a_file );
7709  mod_gzip_printf( "%s: nodecline        = %d",  cn, nodecline );
7710  mod_gzip_printf( "%s: header_length    = %ld", cn, header_length );
7711
7712  if ( source_is_a_file )
7713    {
7714     mod_gzip_printf( "%s: source = [%s]", cn, npp(source));
7715    }
7716  else
7717    {
7718     mod_gzip_printf( "%s: source = MEMORY BUFFER", cn );
7719    }
7720
7721  mod_gzip_printf( "%s: input_size = %ld", cn,(long)input_size);
7722
7723  #endif
7724
7725  #ifdef MOD_GZIP_USES_APACHE_LOGS
7726
7727  if ( !prefix_string )
7728    {
7729     prefix_string = dummy_result_prefix_string;
7730    }
7731
7732  sprintf( log_info,"%sOK", prefix_string );
7733
7734  ap_table_setn(
7735  r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
7736
7737  sprintf( log_info,"%d", (int) input_size );
7738  ap_table_setn( r->notes,"mod_gzip_input_size",ap_pstrdup(r->pool,log_info));
7739
7740  #endif
7741
7742  if ( input_size < 1 )
7743    {
7744     #ifdef MOD_GZIP_DEBUG1
7745     mod_gzip_printf( "%s: ERROR: Input source has no valid length.",cn);
7746     mod_gzip_printf( "%s: This request will not be processed...",cn);
7747     #endif
7748
7749     #ifdef MOD_GZIP_USES_APACHE_LOGS
7750
7751     sprintf( log_info,"%sDECLINED:NO_ILEN", prefix_string );
7752
7753     ap_table_setn(
7754     r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
7755
7756     #endif
7757
7758     #ifdef MOD_GZIP_DEBUG1
7759     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
7760     #endif
7761
7762     return DECLINED;
7763    }
7764
7765  #ifdef MOD_GZIP_DEBUG1
7766  mod_gzip_printf( "%s: dconf__minimum_file_size  = %ld",
7767             cn, (long) dconf__minimum_file_size );
7768  #endif
7769
7770  if ( input_size < (long) dconf__minimum_file_size )
7771    {
7772     #ifdef MOD_GZIP_DEBUG1
7773     mod_gzip_printf( "%s: Source does not meet the minimum size requirement...",cn);
7774     #endif
7775
7776     #ifdef MOD_GZIP_USES_APACHE_LOGS
7777
7778     sprintf( log_info,"%sDECLINED:TOO_SMALL", prefix_string );
7779
7780     ap_table_setn(
7781     r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
7782
7783     #endif
7784
7785     #ifdef MOD_GZIP_DEBUG1
7786     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
7787     #endif
7788
7789     return DECLINED;
7790    }
7791  else
7792    {
7793     #ifdef MOD_GZIP_DEBUG1
7794     mod_gzip_printf( "%s: Source meets the minimum size requirement.",cn);
7795     mod_gzip_printf( "%s: Assuming OK to proceed...",cn);
7796     #endif
7797    }
7798
7799  #ifdef MOD_GZIP_DEBUG1
7800  mod_gzip_printf( "%s: dconf__maximum_file_size = %ld",
7801             cn, (long) dconf__maximum_file_size );
7802  #endif
7803
7804  if ( ( dconf__maximum_file_size > 0 ) &&
7805       ( input_size > (long) dconf__maximum_file_size ) )
7806    {
7807     #ifdef MOD_GZIP_DEBUG1
7808     mod_gzip_printf( "%s: Source exceeds the maximum size limit...",cn);
7809     #endif
7810
7811     #ifdef MOD_GZIP_USES_APACHE_LOGS
7812
7813     sprintf( log_info,"%sDECLINED:TOO_BIG", prefix_string );
7814
7815     ap_table_setn(
7816     r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
7817
7818     #endif
7819
7820     #ifdef MOD_GZIP_DEBUG1
7821     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
7822     #endif
7823
7824     return DECLINED;
7825    }
7826  else
7827    {
7828     #ifdef MOD_GZIP_DEBUG1
7829     mod_gzip_printf( "%s: Source does not exceed the maximum size limit.",cn);
7830     mod_gzip_printf( "%s: Assuming OK to proceed...",cn);
7831     #endif
7832    }
7833
7834  #ifdef MOD_GZIP_DEBUG1
7835  mod_gzip_printf( "%s: dconf__maximum_inmem_size = %ld",
7836             cn, (long) dconf__maximum_inmem_size );
7837  #endif
7838
7839  if ( source_is_a_file )
7840    {
7841     #ifdef MOD_GZIP_DEBUG1
7842     mod_gzip_printf( "%s: Input source is file[%s]",cn,npp(source));
7843     #endif
7844
7845     mod_gzip_strcpy( gzp->input_filename, source );
7846
7847     gzp->input_ismem         = 0;
7848     gzp->input_ismem_ibuf    = 0;
7849     gzp->input_ismem_ibuflen = 0;
7850    }
7851  else
7852    {
7853     #ifdef MOD_GZIP_DEBUG1
7854     mod_gzip_printf( "%s: Input source is a MEMORY BUFFER",cn);
7855     #endif
7856
7857     gzp->input_ismem         = 1;
7858     gzp->input_ismem_ibuf    = source;
7859     gzp->input_ismem_ibuflen = input_size;
7860    }
7861
7862  gzp->decompress = 0;
7863
7864  if ( dconf__maximum_inmem_size > (long) 60000L )
7865    {
7866     /* TODO: Some OS'es will have a 'malloc()' problem if the */
7867     /* in-memory size is greater than 64k so for now just set */
7868     /* 60k as the fixed upper limit. Expand this later. */
7869
7870     /* NOTE: Testing has shown that for responses greater than */
7871     /* 60k or so the 'swap to disk' option SHOULD be used, anyway. */
7872     /* MOST responses will be far less than 60k. */
7873
7874     dconf__maximum_inmem_size = (long) 60000L;
7875    }
7876
7877  if ( input_size < (long) dconf__maximum_inmem_size )
7878    {
7879     #ifdef MOD_GZIP_DEBUG1
7880     mod_gzip_printf( "%s: Input source is small enough for in-memory compression.",cn);
7881     #endif
7882
7883     *gzp->output_filename = 0;
7884      gzp->output_ismem    = 1;
7885
7886     #ifdef MOD_GZIP_DEBUG1
7887     mod_gzip_printf( "%s: Call malloc( input_size + 1000 = %ld )...",
7888                      cn,(long)(input_size+1000));
7889     #endif
7890
7891     gz1_ismem_obuf = (char *) malloc( input_size + 1000 );
7892
7893     #ifdef MOD_GZIP_DEBUG1
7894     mod_gzip_printf( "%s: Back malloc( input_size + 1000 = %ld )...",
7895                      cn,(long)(input_size+1000));
7896     #endif
7897
7898     if ( !gz1_ismem_obuf )
7899       {
7900        #ifdef MOD_GZIP_DEBUG1
7901        mod_gzip_printf( "%s: ERROR: Cannot allocate GZP memory...",cn);
7902        mod_gzip_printf( "%s: Defaulting to output file method... ",cn);
7903        #endif
7904
7905        gzp->output_ismem = 0;
7906       }
7907     else
7908       {
7909        #ifdef MOD_GZIP_DEBUG1
7910        mod_gzip_printf( "%s: ( %ld + 1000 ) bytes allocated OK",cn,(long)input_size);
7911        #endif
7912
7913        gz1_ismem_obuf_was_allocated = 1;
7914
7915        /* TODO: This probably isn't necessary and just wastes time... */
7916        memset( gz1_ismem_obuf, 0, ( input_size + 1000 ) );
7917
7918        gzp->output_ismem_obuf    = gz1_ismem_obuf;
7919        gzp->output_ismem_obuflen = input_size + 1000;
7920       }
7921    }
7922  #ifdef MOD_GZIP_DEBUG1
7923  else
7924    {
7925     mod_gzip_printf( "%s: Input source is too large for in-memory compression.",cn);
7926    }
7927  #endif
7928
7929  if ( gzp->output_ismem != 1 )
7930    {
7931     #ifdef MOD_GZIP_DEBUG1
7932     mod_gzip_printf( "%s: Ouput target is a FILE...",cn);
7933     #endif
7934
7935     mod_gzip_create_unique_filename(
7936     dconf__temp_dir,
7937     (char *) gzp->output_filename,
7938     MOD_GZIP_MAX_PATH_LEN
7939     );
7940
7941     gzp->output_ismem         = 0;
7942     gz1_ismem_obuf            = 0;
7943     gzp->output_ismem_obuf    = 0;
7944     gzp->output_ismem_obuflen = 0;
7945    }
7946
7947  #ifdef MOD_GZIP_DEBUG1
7948  mod_gzip_printf( "%s: gzp->decompress      = %d"  ,cn,gzp->decompress);
7949  mod_gzip_printf( "%s: gzp->input_ismem     = %d",  cn,gzp->input_ismem);
7950  mod_gzip_printf( "%s: gzp->output_ismem    = %d",  cn,gzp->output_ismem);
7951  mod_gzip_printf( "%s: gzp->input_filename  = [%s]",cn,npp(gzp->input_filename));
7952  mod_gzip_printf( "%s: gzp->input_offset    = %ld", cn,gzp->input_offset);
7953  mod_gzip_printf( "%s: gzp->output_filename = [%s]",cn,npp(gzp->output_filename));
7954  mod_gzip_printf( "%s: Call gzp_main(r,gzp)...",cn);
7955  #endif
7956
7957  rc = gzp_main( r, gzp );
7958
7959  output_size = (long) gzp->bytes_out;
7960
7961  #ifdef MOD_GZIP_DEBUG1
7962  mod_gzip_printf( "%s: Back gzp_main(r,gzp)...",cn);
7963  mod_gzip_printf( "%s: input_size     = %ld",cn,(long)input_size);
7964  mod_gzip_printf( "%s: output_size    = %ld",cn,(long)output_size);
7965  mod_gzip_printf( "%s: gzp->bytes_out = %ld",cn,(long)gzp->bytes_out);
7966  mod_gzip_printf( "%s: Bytes saved    = %ld",cn,
7967                  (long)input_size-gzp->bytes_out );
7968  #endif
7969
7970  compression_ratio = 0;
7971
7972  if ( ( input_size  > 0 ) &&
7973       ( output_size > 0 ) )
7974    {
7975     compression_ratio = 100 - (int)
7976     ( output_size * 100L / input_size );
7977    }
7978
7979  #ifdef MOD_GZIP_DEBUG1
7980  mod_gzip_printf( "%s: Compression    = %ld percent",cn,
7981           (long) compression_ratio );
7982  #endif
7983
7984  #ifdef MOD_GZIP_USES_APACHE_LOGS
7985
7986  sprintf( log_info,"%d", (int) output_size );
7987  ap_table_setn( r->notes,"mod_gzip_output_size",ap_pstrdup(r->pool,log_info));
7988
7989  sprintf( log_info,"%d", (int) compression_ratio );
7990  ap_table_setn( r->notes,"mod_gzip_compression_ratio",ap_pstrdup(r->pool,log_info));
7991
7992  #endif
7993
7994  if ( output_size < 1 )
7995    {
7996     #ifdef MOD_GZIP_DEBUG1
7997     mod_gzip_printf( "%s: Compressed version has no length.",cn);
7998     mod_gzip_printf( "%s: Sending the original version uncompressed...",cn);
7999     #endif
8000
8001     finalize_stats = 0;
8002
8003     #ifdef MOD_GZIP_USES_APACHE_LOGS
8004
8005     sprintf( log_info,"%sDECLINED:NO_OLEN", prefix_string );
8006
8007     ap_table_setn(
8008     r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
8009
8010     #endif
8011
8012     if ( gz1_ismem_obuf )
8013       {
8014        #ifdef MOD_GZIP_DEBUG1
8015        mod_gzip_printf( "%s: gz1_ismem_obuf is still a VALID pointer",cn);
8016        mod_gzip_printf( "%s: gz1_ismem_obuf_was_allocated = %d",
8017                     cn,(int) gz1_ismem_obuf_was_allocated );
8018        #endif
8019
8020        if ( gz1_ismem_obuf_was_allocated )
8021          {
8022           #ifdef MOD_GZIP_DEBUG1
8023           mod_gzip_printf( "%s: Call free( gz1_ismem_obuf )...",cn);
8024           #endif
8025
8026           free( gz1_ismem_obuf );
8027
8028           #ifdef MOD_GZIP_DEBUG1
8029           mod_gzip_printf( "%s: Back free( gz1_ismem_obuf )...",cn);
8030           #endif
8031
8032           gz1_ismem_obuf = 0;
8033           gz1_ismem_obuf_was_allocated = 0;
8034          }
8035       }
8036
8037     #ifdef MOD_GZIP_DEBUG1
8038     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
8039     #endif
8040
8041     return DECLINED;
8042    }
8043
8044  if ( output_size > input_size )
8045    {
8046     #ifdef MOD_GZIP_DEBUG1
8047     mod_gzip_printf( "%s: Compressed version is larger than original.",cn);
8048     mod_gzip_printf( "%s: Sending the original version uncompressed...",cn);
8049     #endif
8050
8051     finalize_stats = 0;
8052
8053     #ifdef MOD_GZIP_USES_APACHE_LOGS
8054
8055     sprintf( log_info,"%sDECLINED:ORIGINAL_SMALLER", prefix_string );
8056
8057     ap_table_setn(
8058     r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
8059
8060     #endif
8061
8062     if ( gz1_ismem_obuf )
8063       {
8064        #ifdef MOD_GZIP_DEBUG1
8065        mod_gzip_printf( "%s: gz1_ismem_obuf is still a VALID pointer",cn);
8066        mod_gzip_printf( "%s: gz1_ismem_obuf_was_allocated = %d",
8067                     cn,(int) gz1_ismem_obuf_was_allocated );
8068        #endif
8069
8070        if ( gz1_ismem_obuf_was_allocated )
8071          {
8072           #ifdef MOD_GZIP_DEBUG1
8073           mod_gzip_printf( "%s: Call free( gz1_ismem_obuf )...",cn);
8074           #endif
8075
8076           free( gz1_ismem_obuf );
8077
8078           #ifdef MOD_GZIP_DEBUG1
8079           mod_gzip_printf( "%s: Back free( gz1_ismem_obuf )...",cn);
8080           #endif
8081
8082           gz1_ismem_obuf = 0;
8083           gz1_ismem_obuf_was_allocated = 0;
8084          }
8085       }
8086
8087     #ifdef MOD_GZIP_DEBUG1
8088     mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
8089     #endif
8090
8091     return DECLINED;
8092    }
8093  #ifdef MOD_GZIP_DEBUG1
8094  else
8095    {
8096     mod_gzip_printf( "%s: Compressed version is smaller than original.",cn);
8097     mod_gzip_printf( "%s: Sending the compressed version...",cn);
8098    }
8099  #endif
8100
8101  if ( !gzp->output_ismem )
8102    {
8103     #ifdef MOD_GZIP_DEBUG1
8104     mod_gzip_printf( "%s: Re-opening compressed output file [%s]...",
8105              cn, npp(gzp->output_filename));
8106     #endif
8107
8108     ifh = mod_gzip_open_output_file( r, gzp->output_filename, &rc );
8109
8110     if ( !ifh )
8111       {
8112        #ifdef MOD_GZIP_DEBUG1
8113        mod_gzip_printf( "%s: ERROR: Cannot re-open file [%s]",
8114                 cn,npp(gzp->output_filename));
8115        #endif
8116
8117        #ifdef MOD_GZIP_USES_APACHE_LOGS
8118
8119        sprintf( log_info,"%sDECLINED:REOPEN_FAILED", prefix_string );
8120
8121        ap_table_setn(
8122        r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
8123
8124        #endif
8125
8126        #ifdef MOD_GZIP_DEBUG1
8127        mod_gzip_printf( "%s: Exit > return( DECLINED ) >",cn);
8128        #endif
8129
8130        return DECLINED;
8131       }
8132
8133     #ifdef MOD_GZIP_DEBUG1
8134     mod_gzip_printf( "%s: Workfile re-opened OK...",cn);
8135     #endif
8136    }
8137
8138  /* TODO: This isn't really necessary? */
8139  r->content_encoding = actual_content_encoding_name;
8140
8141  #ifdef MOD_GZIP_DEBUG1
8142  mod_gzip_printf( "%s: r->content_encoding is now [%s]",
8143                cn, npp(r->content_encoding));
8144  #endif
8145
8146  #ifdef MOD_GZIP_DEBUG1
8147  mod_gzip_printf( "%s: Starting transmit phase...",cn);
8148  mod_gzip_printf( "%s: output_offset   = %ld",cn, (long) output_offset );
8149  #endif
8150
8151  /* Send the response header... */
8152
8153  #ifdef MOD_GZIP_DEBUG1
8154  mod_gzip_printf( "%s: Call mod_gzip_send_header(source=%s)",cn,npp(source));
8155  #endif
8156
8157  total_header_bytes_sent = (int)
8158  mod_gzip_send_header(
8159  r,
8160  source,
8161  output_size
8162  );
8163
8164  #ifdef MOD_GZIP_DEBUG1
8165  mod_gzip_printf( "%s: Back mod_gzip_send_header(source=%s)",cn,npp(source));
8166  mod_gzip_printf( "%s: total_header_bytes_sent = %d",cn,rc);
8167  #endif
8168
8169  total_compressed_bytes_sent = 0;
8170
8171  /* Send the compressed data.. */
8172
8173  if ( gzp->output_ismem )
8174    {
8175     #ifdef MOD_GZIP_DEBUG1
8176
8177     mod_gzip_printf( "%s: Sending in-memory output buffer...",cn);
8178     mod_gzip_printf( "%s: output_size   = %ld",cn,(long)output_size);
8179     mod_gzip_printf( "%s: output_offset = %ld",cn,(long)output_offset);
8180
8181     #ifdef  MOD_GZIP_DUMP_JUST_BEFORE_SENDING
8182     mod_gzip_hexdump( gz1_ismem_obuf, output_size );
8183     #endif
8184
8185     mod_gzip_printf( "%s: Call mod_gzip_send( gz1_ismem_obuf+output_offset=%ld, output_size=%ld )...",
8186                      cn, (long) output_offset, (long) output_size );
8187     #endif
8188
8189     byteswritten = (long)
8190     mod_gzip_send( gz1_ismem_obuf+output_offset, output_size, r );
8191
8192     #ifdef MOD_GZIP_DEBUG1
8193     mod_gzip_printf( "%s: Back mod_gzip_send( gz1_ismem_obuf+output_offset=%ld, output_size=%ld )...",
8194                      cn, (long) output_offset, (long) output_size );
8195     mod_gzip_printf( "%s: byteswritten = %ld",cn,(long)byteswritten);
8196     #endif
8197
8198     if ( byteswritten > 0 )
8199       {
8200        total_compressed_bytes_sent = byteswritten;
8201       }
8202
8203     if ( byteswritten != output_size )
8204       {
8205        err = errno;
8206
8207        #ifdef FUTURE_USE
8208        #if defined(WIN32) || defined(NETWARE)
8209        err = WSAGetLastError();
8210        #else
8211        err = errno;
8212        #endif
8213        #endif
8214
8215        #ifdef MOD_GZIP_DEBUG1
8216        mod_gzip_printf( "%s: TRANSMIT ERROR: output_size=%ld byteswritten=%ld err=%d",
8217        cn,(long)output_size,(long)byteswritten,(int)err );
8218        #endif
8219
8220        #ifdef MOG_GZIP_DEBUG1
8221
8222        mod_gzip_log_comerror( r, "mod_gzip: TRANSMIT_ERROR:", err );
8223
8224        #else
8225
8226        ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
8227        "mod_gzip: TRANSMIT_ERROR:ISMEM:%d",(int)err);
8228
8229        #endif
8230
8231        #ifdef MOD_GZIP_USES_APACHE_LOGS
8232
8233        #ifdef MOD_GZIP_DEBUG1
8234        mod_gzip_translate_comerror( err, scratch2 );
8235        sprintf( log_info,"%sTRANSMIT_ERROR:ISMEM:%d:%s", prefix_string, (int) err, scratch2 );
8236        #else
8237        sprintf( log_info,"%sTRANSMIT_ERROR:ISMEM:%d", prefix_string, (int) err );
8238        #endif
8239
8240        ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
8241
8242        #endif
8243       }
8244    }
8245  else
8246    {
8247     #ifdef MOD_GZIP_DEBUG1
8248     mod_gzip_printf( "%s: sizeof( tmp )              = %d",cn,sizeof(tmp));
8249     mod_gzip_printf( "%s: MOD_GZIP_LARGE_BUFFER_SIZE = %d",cn,(int)MOD_GZIP_LARGE_BUFFER_SIZE);
8250     mod_gzip_printf( "%s: Transmit buffer size       = %d",cn,sizeof(tmp));
8251     mod_gzip_printf( "%s: Sending output file...",cn);
8252     #endif
8253
8254     if ( output_offset > 0 )
8255       {
8256        #ifdef MOD_GZIP_DEBUG1
8257        mod_gzip_printf( "%s: output_offset = %ld",cn,(long)output_offset);
8258        mod_gzip_printf( "%s: Call fseek(ifh,%ld,1)...",cn,(long)output_offset);
8259        #endif
8260
8261        fseek( ifh, (long) output_offset, 1 );
8262
8263        #ifdef MOD_GZIP_DEBUG1
8264        mod_gzip_printf( "%s: Back fseek(ifh,%ld,1)...",cn,(long)output_offset);
8265        #endif
8266       }
8267     else
8268       {
8269        #ifdef MOD_GZIP_DEBUG1
8270        mod_gzip_printf( "%s: output_offset = %ld = ZERO",cn,(long)output_offset);
8271        mod_gzip_printf( "%s: No fseek() call required before transmit.",cn);
8272        #endif
8273       }
8274
8275     for (;;)
8276        {
8277         bytesread = fread( tmp, 1, MOD_GZIP_LARGE_BUFFER_SIZE, ifh );
8278
8279         #ifdef MOD_GZIP_DEBUG1
8280         mod_gzip_printf( "%s: Back fread(): bytesread=%d",cn,bytesread);
8281         #endif
8282
8283         if ( bytesread < 1 ) break;
8284
8285         byteswritten = (long)
8286         mod_gzip_send( tmp, bytesread, r );
8287
8288         #ifdef MOD_GZIP_DEBUG1
8289         mod_gzip_printf( "%s: byteswritten = %ld",cn,(long)byteswritten);
8290         #endif
8291
8292         if ( byteswritten > 0 )
8293           {
8294            total_compressed_bytes_sent += byteswritten;
8295           }
8296
8297         if ( byteswritten != bytesread )
8298           {
8299            err = errno;
8300
8301            #ifdef FUTURE_USE
8302            #if defined(WIN32) || defined(NETWARE)
8303            err = WSAGetLastError();
8304            #else
8305            err = errno;
8306            #endif
8307            #endif
8308
8309            #ifdef MOD_GZIP_DEBUG1
8310            mod_gzip_printf( "%s: TRANSMIT ERROR: bytesread=%ld byteswritten=%ld err=%d",
8311            cn,(long)bytesread,(long)byteswritten,(int)err );
8312            #endif
8313
8314            #ifdef MOG_GZIP_DEBUG1
8315
8316            mod_gzip_log_comerror( r, "mod_gzip: TRANSMIT_ERROR:", err );
8317
8318            #else
8319
8320            ap_log_error( APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, r->server,
8321            "mod_gzip: TRANSMIT_ERROR:%d",(int)err);
8322
8323            #endif
8324
8325            #ifdef MOD_GZIP_USES_APACHE_LOGS
8326
8327            #ifdef MOD_GZIP_DEBUG1
8328            mod_gzip_translate_comerror( err, scratch2 );
8329            sprintf( log_info,"%sTRANSMIT_ERROR:%d:%s", prefix_string, (int) err, scratch2 );
8330            #else
8331            sprintf( log_info,"%sTRANSMIT_ERROR:%d", prefix_string, (int) err );
8332            #endif
8333
8334            ap_table_setn( r->notes,"mod_gzip_result",ap_pstrdup(r->pool,log_info));
8335
8336            #endif
8337
8338            #ifdef MOD_GZIP_DEBUG1
8339            mod_gzip_printf( "%s: Breaking out of transmit loop early...",cn);
8340            #endif
8341
8342            break;
8343           }
8344
8345        }/* End for(;;) loop that transmits workfile... */
8346
8347    }/* End 'else' */
8348
8349  /* Flush the output and update counts... */
8350
8351  total_bytes_sent =
8352  total_header_bytes_sent +
8353  total_compressed_bytes_sent;
8354
8355  #ifdef MOD_GZIP_DEBUG1
8356  mod_gzip_printf( "%s: Done Sending compressed data...",cn);
8357  mod_gzip_printf( "%s: total_header_bytes_sent     = %ld",
8358               cn,(long)total_header_bytes_sent);
8359  mod_gzip_printf( "%s: total_compressed_bytes_sent = %ld",
8360               cn,(long)total_compressed_bytes_sent);
8361  mod_gzip_printf( "%s: total_bytes_sent (Sum)      = %ld",
8362               cn,(long)total_bytes_sent);
8363  mod_gzip_printf( "%s: Call mod_gzip_flush_and_update_counts()...",cn);
8364  #endif
8365
8366  mod_gzip_flush_and_update_counts(
8367  r,
8368  dconf,
8369  total_header_bytes_sent,
8370  total_compressed_bytes_sent
8371  );
8372
8373  #ifdef MOD_GZIP_DEBUG1
8374  mod_gzip_printf( "%s: Back mod_gzip_flush_and_update_counts()...",cn);
8375  #endif
8376
8377  /* Cleanup... */
8378
8379  #ifdef MOD_GZIP_DEBUG1
8380  mod_gzip_printf( "%s: Cleanup phase START...",cn);
8381  #endif
8382
8383  if ( gzp->output_ismem )
8384    {
8385     #ifdef MOD_GZIP_DEBUG1
8386     mod_gzip_printf( "%s: Output source was MEMORY...",cn);
8387     #endif
8388
8389     if ( gz1_ismem_obuf )
8390       {
8391        #ifdef MOD_GZIP_DEBUG1
8392        mod_gzip_printf( "%s: gz1_ismem_obuf is still VALID...",cn);
8393        #endif
8394
8395        if ( gz1_ismem_obuf_was_allocated )
8396          {
8397           #ifdef MOD_GZIP_DEBUG1
8398           mod_gzip_printf( "%s: gz1_ismem_obuf_was_allocated = TRUE",cn);
8399           mod_gzip_printf( "%s: Call free( gz1_ismem_obuf )...",cn);
8400           #endif
8401
8402           free( gz1_ismem_obuf );
8403
8404           #ifdef MOD_GZIP_DEBUG1
8405           mod_gzip_printf( "%s: Back free( gz1_ismem_obuf )...",cn);
8406           #endif
8407
8408           gz1_ismem_obuf = 0;
8409           gz1_ismem_obuf_was_allocated = 0;
8410          }
8411       }
8412    }
8413  else
8414    {
8415     #ifdef MOD_GZIP_DEBUG1
8416     mod_gzip_printf( "%s: Output source was a WORKFILE...",cn);
8417     mod_gzip_printf( "%s: Closing workfile [%s]...",cn,npp(gzp->output_filename));
8418     #endif
8419
8420     fclose( ifh );
8421     ifh = 0;
8422
8423     #ifdef MOD_GZIP_DEBUG1
8424     mod_gzip_printf( "%s: dconf__keep_workfiles = %d",
8425                       cn, dconf__keep_workfiles );
8426     #endif
8427
8428     if ( !dconf__keep_workfiles )
8429       {
8430        #ifdef MOD_GZIP_DEBUG1
8431        mod_gzip_printf( "%s: Deleting workfile [%s]...",
8432                 cn, npp(gzp->output_filename));
8433        #endif
8434
8435        #ifdef WIN32
8436        DeleteFile( gzp->output_filename );
8437        #else
8438        unlink( gzp->output_filename );
8439        #endif
8440       }
8441     else
8442       {
8443        #ifdef MOD_GZIP_DEBUG1
8444        mod_gzip_printf( "%s: Keeping workfile [%s]...",
8445                          cn, npp(gzp->output_filename));
8446        #endif
8447       }
8448    }
8449
8450  #ifdef MOD_GZIP_USES_APACHE_LOGS
8451
8452  if ( finalize_stats )
8453    {
8454     sprintf( log_info,"%d", (int) output_size );
8455     ap_table_setn( r->notes,"mod_gzip_output_size",ap_pstrdup(r->pool,log_info));
8456
8457     sprintf( log_info,"%d", (int) compression_ratio );
8458     ap_table_setn( r->notes,"mod_gzip_compression_ratio",ap_pstrdup(r->pool,log_info));
8459    }
8460
8461  #endif
8462
8463  if ( r->server->loglevel == APLOG_DEBUG )
8464    {
8465     ap_log_error( "",0,APLOG_NOERRNO|APLOG_DEBUG, r->server,
8466     "mod_gzip: r->uri=[%s] OK: Bytes In:%ld Out:%ld Compression: %ld pct.",
8467     r->uri,
8468     (long) input_size,
8469     (long) output_size,
8470     (long) compression_ratio
8471     );
8472    }
8473
8474  #ifdef MOD_GZIP_DEBUG1
8475  mod_gzip_printf( "%s: Exit > return( OK ) >",cn);
8476  #endif
8477
8478  return OK;
8479 }
8480
8481 /*--------------------------------------------------------------------------*/
8482 /* COMPRESSION_SUPPORT: START                                               */
8483 /*--------------------------------------------------------------------------*/
8484
8485 #define BIG_MEM
8486
8487 typedef unsigned       uns;
8488 typedef unsigned int   uni;
8489 typedef unsigned char  uch;
8490 typedef unsigned short ush;
8491 typedef unsigned long  ulg;
8492 typedef int            gz1_file_t;
8493
8494 #ifdef __STDC__
8495    typedef void *voidp;
8496 #else
8497    typedef char *voidp;
8498 #endif
8499
8500 #if defined(__MSDOS__) && !defined(MSDOS)
8501 #  define MSDOS
8502 #endif
8503
8504 #if defined(__OS2__) && !defined(OS2)
8505 #  define OS2
8506 #endif
8507
8508 #if defined(OS2) && defined(MSDOS)
8509 #  undef MSDOS
8510 #endif
8511
8512 #ifdef MSDOS
8513 #  ifdef __GNUC__
8514 #    define near
8515 #  else
8516 #    define MAXSEG_64K
8517 #    ifdef __TURBOC__
8518 #      define NO_OFF_T
8519 #      ifdef __BORLANDC__
8520 #        define DIRENT
8521 #      else
8522 #        define NO_UTIME
8523 #      endif
8524 #    else
8525 #      define HAVE_SYS_UTIME_H
8526 #      define NO_UTIME_H
8527 #    endif
8528 #  endif
8529 #  define PATH_SEP2 '\\'
8530 #  define PATH_SEP3 ':'
8531 #  define MAX_PATH_LEN  128
8532 #  define NO_MULTIPLE_DOTS
8533 #  define MAX_EXT_CHARS 3
8534 #  define Z_SUFFIX "z"
8535 #  define NO_CHOWN
8536 #  define PROTO
8537 #  define STDC_HEADERS
8538 #  define NO_SIZE_CHECK
8539 #  define casemap(c) tolow(c)
8540 #  include <io.h>
8541 #  undef  OS_CODE
8542 #  define OS_CODE  0x00
8543 #  define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
8544 #  if !defined(NO_ASM) && !defined(ASMV)
8545 #    define ASMV
8546 #  endif
8547 #else
8548 #  define near
8549 #endif
8550
8551 #ifdef OS2
8552 #  define PATH_SEP2 '\\'
8553 #  define PATH_SEP3 ':'
8554 #  define MAX_PATH_LEN  260
8555 #  ifdef OS2FAT
8556 #    define NO_MULTIPLE_DOTS
8557 #    define MAX_EXT_CHARS 3
8558 #    define Z_SUFFIX "z"
8559 #    define casemap(c) tolow(c)
8560 #  endif
8561 #  define NO_CHOWN
8562 #  define PROTO
8563 #  define STDC_HEADERS
8564 #  include <io.h>
8565 #  undef  OS_CODE
8566 #  define OS_CODE  0x06
8567 #  define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
8568 #  ifdef _MSC_VER
8569 #    define HAVE_SYS_UTIME_H
8570 #    define NO_UTIME_H
8571 #    define MAXSEG_64K
8572 #    undef near
8573 #    define near _near
8574 #  endif
8575 #  ifdef __EMX__
8576 #    define HAVE_SYS_UTIME_H
8577 #    define NO_UTIME_H
8578 #    define DIRENT
8579 #    define EXPAND(argc,argv) \
8580        {_response(&argc, &argv); _wildcard(&argc, &argv);}
8581 #  endif
8582 #  ifdef __BORLANDC__
8583 #    define DIRENT
8584 #  endif
8585 #  ifdef __ZTC__
8586 #    define NO_DIR
8587 #    define NO_UTIME_H
8588 #    include <dos.h>
8589 #    define EXPAND(argc,argv) \
8590        {response_expand(&argc, &argv);}
8591 #  endif
8592 #endif
8593
8594 #ifdef WIN32
8595 #  define HAVE_SYS_UTIME_H
8596 #  define NO_UTIME_H
8597 #  define PATH_SEP2 '\\'
8598 #  define PATH_SEP3 ':'
8599 #  undef  MAX_PATH_LEN
8600 #  define MAX_PATH_LEN  260
8601 #  define NO_CHOWN
8602 #  define PROTO
8603 #  define STDC_HEADERS
8604 #  define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
8605 #  include <io.h>
8606 #  include <malloc.h>
8607 #  ifdef NTFAT
8608 #    define NO_MULTIPLE_DOTS
8609 #    define MAX_EXT_CHARS 3
8610 #    define Z_SUFFIX "z"
8611 #    define casemap(c) tolow(c)
8612 #  endif
8613 #  undef  OS_CODE
8614
8615 #  define OS_CODE  0x00
8616
8617 #endif
8618
8619 #ifdef MSDOS
8620 #  ifdef __TURBOC__
8621 #    include <alloc.h>
8622 #    define DYN_ALLOC
8623      void * fcalloc (unsigned items, unsigned size);
8624      void fcfree (void *ptr);
8625 #  else
8626 #    include <malloc.h>
8627 #    define fcalloc(nitems,itemsize) halloc((long)(nitems),(itemsize))
8628 #    define fcfree(ptr) hfree(ptr)
8629 #  endif
8630 #else
8631 #  ifdef MAXSEG_64K
8632 #    define fcalloc(items,size) calloc((items),(size))
8633 #  else
8634 #    define fcalloc(items,size) malloc((size_t)(items)*(size_t)(size))
8635 #  endif
8636 #  define fcfree(ptr) free(ptr)
8637 #endif
8638
8639 #if defined(VAXC) || defined(VMS)
8640 #  define PATH_SEP ']'
8641 #  define PATH_SEP2 ':'
8642 #  define SUFFIX_SEP ';'
8643 #  define NO_MULTIPLE_DOTS
8644 #  define Z_SUFFIX "-gz"
8645 #  define RECORD_IO 1
8646 #  define casemap(c) tolow(c)
8647 #  undef  OS_CODE
8648 #  define OS_CODE  0x02
8649 #  define OPTIONS_VAR "GZIP_OPT"
8650 #  define STDC_HEADERS
8651 #  define NO_UTIME
8652 #  define EXPAND(argc,argv) vms_expand_args(&argc,&argv);
8653 #  include <file.h>
8654 #  define unlink delete
8655 #  ifdef VAXC
8656 #    define NO_FCNTL_H
8657 #    include <unixio.h>
8658 #  endif
8659 #endif
8660
8661 #ifdef AMIGA
8662 #  define PATH_SEP2 ':'
8663 #  define STDC_HEADERS
8664 #  undef  OS_CODE
8665 #  define OS_CODE  0x01
8666 #  define ASMV
8667 #  ifdef __GNUC__
8668 #    define DIRENT
8669 #    define HAVE_UNISTD_H
8670 #  else
8671 #    define NO_STDIN_FSTAT
8672 #    define SYSDIR
8673 #    define NO_SYMLINK
8674 #    define NO_CHOWN
8675 #    define NO_FCNTL_H
8676 #    include <fcntl.h>
8677 #    define direct dirent
8678      extern void _expand_args(int *argc, char ***argv);
8679 #    define EXPAND(argc,argv) _expand_args(&argc,&argv);
8680 #    undef  O_BINARY
8681 #  endif
8682 #endif
8683
8684 #if defined(ATARI) || defined(atarist)
8685 #  ifndef STDC_HEADERS
8686 #    define STDC_HEADERS
8687 #    define HAVE_UNISTD_H
8688 #    define DIRENT
8689 #  endif
8690 #  define ASMV
8691 #  undef  OS_CODE
8692 #  define OS_CODE  0x05
8693 #  ifdef TOSFS
8694 #    define PATH_SEP2 '\\'
8695 #    define PATH_SEP3 ':'
8696 #    define MAX_PATH_LEN  128
8697 #    define NO_MULTIPLE_DOTS
8698 #    define MAX_EXT_CHARS 3
8699 #    define Z_SUFFIX "z"
8700 #    define NO_CHOWN
8701 #    define casemap(c) tolow(c)
8702 #    define NO_SYMLINK
8703 #  endif
8704 #endif
8705
8706 #ifdef MACOS
8707 #  define PATH_SEP ':'
8708 #  define DYN_ALLOC
8709 #  define PROTO
8710 #  define NO_STDIN_FSTAT
8711 #  define NO_CHOWN
8712 #  define NO_UTIME
8713 #  define chmod(file, mode) (0)
8714 #  define OPEN(name, flags, mode) open(name, flags)
8715 #  define SEEKFORWARD(handle, offset) lseek( handle, offset, 1 )
8716 #  undef  OS_CODE
8717 #  define OS_CODE  0x07
8718 #  ifdef MPW
8719 #    define isatty(fd) ((fd) <= 2)
8720 #  endif
8721 #endif
8722
8723 #ifdef __50SERIES
8724 #  define PATH_SEP '>'
8725 #  define STDC_HEADERS
8726 #  define NO_MEMORY_H
8727 #  define NO_UTIME_H
8728 #  define NO_UTIME
8729 #  define NO_CHOWN 
8730 #  define NO_STDIN_FSTAT 
8731 #  define NO_SIZE_CHECK 
8732 #  define NO_SYMLINK
8733 #  define RECORD_IO  1
8734 #  define casemap(c)  tolow(c)
8735 #  define put_char(c) put_byte((c) & 0x7F)
8736 #  define get_char(c) ascii2pascii(get_byte())
8737 #  undef  OS_CODE
8738 #  define OS_CODE  0x0F
8739 #  ifdef SIGTERM
8740 #    undef SIGTERM
8741 #  endif
8742 #endif
8743
8744 #if defined(pyr) && !defined(NOMEMCPY)
8745 #  define NOMEMCPY
8746 #endif
8747
8748 #ifdef TOPS20
8749 #  undef  OS_CODE
8750 #  define OS_CODE  0x0a
8751 #endif
8752
8753 #ifndef unix
8754 #  define NO_ST_INO
8755 #endif
8756
8757 #ifndef OS_CODE
8758 #  undef  OS_CODE
8759 #  define OS_CODE  0x03
8760 #endif
8761
8762 #ifndef PATH_SEP
8763 #  define PATH_SEP '/'
8764 #endif
8765
8766 #ifndef casemap
8767 #  define casemap(c) (c)
8768 #endif
8769
8770 #ifndef OPTIONS_VAR
8771 #  define OPTIONS_VAR "GZIP"
8772 #endif
8773
8774 #ifndef Z_SUFFIX
8775 #  define Z_SUFFIX ".gz"
8776 #endif
8777
8778 #ifdef MAX_EXT_CHARS
8779 #  define MAX_SUFFIX  MAX_EXT_CHARS
8780 #else
8781 #  define MAX_SUFFIX  30
8782 #endif
8783
8784 #ifndef MIN_PART
8785 #  define MIN_PART 3
8786 #endif
8787
8788 #ifndef EXPAND
8789 #  define EXPAND(argc,argv)
8790 #endif
8791
8792 #ifndef RECORD_IO
8793 #  define RECORD_IO 0
8794 #endif
8795
8796 #ifndef SET_BINARY_MODE
8797 #  define SET_BINARY_MODE(fd)
8798 #endif
8799
8800 #ifndef OPEN
8801 #  define OPEN(name, flags, mode) open(name, flags, mode)
8802 #endif
8803
8804 #ifndef SEEKFORWARD
8805 #  define SEEKFORWARD(handle, offset) lseek( handle, offset, 1 )
8806 #endif
8807
8808 #ifndef get_char
8809 #  define get_char() get_byte()
8810 #endif
8811
8812 #ifndef put_char
8813 #  define put_char(c) put_byte(c)
8814 #endif
8815
8816 #ifndef O_BINARY
8817 #define O_BINARY 0
8818 #endif
8819
8820 #define OK          0
8821 #define LZ1_ERROR   1
8822 #define WARNING     2
8823 #define STORED      0
8824 #define COMPRESSED  1
8825 #define PACKED      2
8826 #define LZHED       3
8827 #define DEFLATED    8
8828 #define MAX_METHODS 9
8829
8830 #ifndef O_CREAT
8831 #include <sys/file.h>
8832 #ifndef O_CREAT
8833 #define O_CREAT FCREAT
8834 #endif
8835 #ifndef O_EXCL
8836 #define O_EXCL FEXCL
8837 #endif
8838 #endif
8839
8840 #ifndef S_IRUSR
8841 #define S_IRUSR 0400
8842 #endif
8843 #ifndef S_IWUSR
8844 #define S_IWUSR 0200
8845 #endif
8846 #define RW_USER (S_IRUSR | S_IWUSR)
8847
8848 #ifndef MAX_PATH_LEN
8849 #define MAX_PATH_LEN 256
8850 #endif
8851
8852 #ifndef SEEK_END
8853 #define SEEK_END 2
8854 #endif
8855
8856 #define PACK_MAGIC     "\037\036"
8857 #define GZIP_MAGIC     "\037\213"
8858 #define OLD_GZIP_MAGIC "\037\236"
8859 #define LZH_MAGIC      "\037\240"
8860 #define PKZIP_MAGIC    "\120\113\003\004"
8861 #define ASCII_FLAG   0x01 
8862 #define CONTINUATION 0x02 
8863 #define EXTRA_FIELD  0x04 
8864 #define ORIG_NAME    0x08 
8865 #define COMMENT      0x10 
8866 #define ENCRYPTED    0x20 
8867 #define RESERVED     0xC0 
8868 #define UNKNOWN 0xffff
8869 #define BINARY  0
8870 #define ASCII   1
8871
8872 #ifndef WSIZE
8873 #define WSIZE 0x8000
8874 #endif
8875
8876 #ifndef INBUFSIZ
8877 #ifdef  SMALL_MEM
8878 #define INBUFSIZ  0x2000
8879 #else
8880 #define INBUFSIZ  0x8000
8881 #endif
8882 #endif
8883 #define INBUF_EXTRA 64
8884
8885 #ifndef OUTBUFSIZ
8886 #ifdef SMALL_MEM
8887 #define OUTBUFSIZ   8192
8888 #else
8889 #define OUTBUFSIZ  0x4000
8890 #endif
8891 #endif
8892 #define OUTBUF_EXTRA 2048
8893
8894 #ifndef DIST_BUFSIZE
8895 #ifdef  SMALL_MEM
8896 #define DIST_BUFSIZE 0x2000
8897 #else
8898 #define DIST_BUFSIZE 0x8000
8899 #endif
8900 #endif
8901
8902 #ifndef BITS
8903 #define BITS 16
8904 #endif
8905
8906 #define LZW_MAGIC  "\037\235"
8907
8908 #define MIN_MATCH  3
8909 #define MAX_MATCH  258
8910
8911 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
8912 #define MAX_DIST  (WSIZE-MIN_LOOKAHEAD)
8913
8914 #ifdef  SMALL_MEM
8915 #define HASH_BITS  13
8916 #endif
8917 #ifdef  MEDIUM_MEM
8918 #define HASH_BITS  14
8919 #endif
8920 #ifndef HASH_BITS
8921 #define HASH_BITS  15
8922 #endif
8923
8924 #define HASH_SIZE (unsigned)(1<<HASH_BITS)
8925 #define HASH_MASK (HASH_SIZE-1)
8926 #define WMASK     (WSIZE-1)
8927 #define H_SHIFT   ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
8928
8929 #ifndef TOO_FAR
8930 #define TOO_FAR 4096
8931 #endif
8932
8933 #define NIL          0
8934 #define FAST         4
8935 #define SLOW         2
8936 #define REP_3_6      16
8937 #define REPZ_3_10    17
8938 #define REPZ_11_138  18
8939 #define MAX_BITS     15
8940 #define MAX_BL_BITS  7
8941 #define D_CODES      30
8942 #define BL_CODES     19
8943 #define SMALLEST     1
8944 #define LENGTH_CODES 29
8945 #define LITERALS     256
8946 #define END_BLOCK    256
8947 #define L_CODES (LITERALS+1+LENGTH_CODES)
8948
8949 #ifndef LIT_BUFSIZE
8950 #ifdef  SMALL_MEM
8951 #define LIT_BUFSIZE  0x2000
8952 #else
8953 #ifdef  MEDIUM_MEM
8954 #define LIT_BUFSIZE  0x4000
8955 #else
8956 #define LIT_BUFSIZE  0x8000
8957 #endif
8958 #endif
8959 #endif
8960
8961 #define HEAP_SIZE (2*L_CODES+1)
8962 #define STORED_BLOCK 0
8963 #define STATIC_TREES 1
8964 #define DYN_TREES    2
8965 #define NO_FILE  (-1) 
8966
8967 #define BMAX 16         
8968 #define N_MAX 288       
8969
8970 #define LOCSIG 0x04034b50L      
8971 #define LOCFLG 6                
8972 #define CRPFLG 1                
8973 #define EXTFLG 8                
8974 #define LOCHOW 8                
8975 #define LOCTIM 10               
8976 #define LOCCRC 14               
8977 #define LOCSIZ 18               
8978 #define LOCLEN 22               
8979 #define LOCFIL 26               
8980 #define LOCEXT 28               
8981 #define LOCHDR 30               
8982 #define EXTHDR 16               
8983 #define RAND_HEAD_LEN  12
8984 #define BUFSIZE (8 * 2*sizeof(char))
8985
8986 #define translate_eol 0  
8987
8988 #define FLUSH_BLOCK(eof) \
8989    flush_block(gz1,gz1->block_start >= 0L ? (char*)&gz1->window[(unsigned)gz1->block_start] : \
8990          (char*)NULL, (long)gz1->strstart - gz1->block_start, (eof))
8991
8992 #ifdef DYN_ALLOC
8993 #  define ALLOC(type, array, size) { \
8994       array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
8995       if (array == NULL) error("insufficient memory"); \
8996    }
8997 #  define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
8998 #else
8999 #  define ALLOC(type, array, size)
9000 #  define FREE(array)
9001 #endif
9002
9003 #define GZ1_MAX(a,b) (a >= b ? a : b)
9004
9005 #define tolow(c)  (isupper(c) ? (c)-'A'+'a' : (c))    
9006
9007 #define smaller(tree, n, m) \
9008    (tree[n].fc.freq < tree[m].fc.freq || \
9009    (tree[n].fc.freq == tree[m].fc.freq && gz1->depth[n] <= gz1->depth[m]))
9010
9011 #define send_code(c, tree) send_bits(gz1,tree[c].fc.code, tree[c].dl.len)
9012
9013 #define put_byte(c) {gz1->outbuf[gz1->outcnt++]=(uch)(c); if (gz1->outcnt==OUTBUFSIZ)\
9014                      flush_outbuf(gz1);}
9015
9016 #define put_short(w) \
9017 { if (gz1->outcnt < OUTBUFSIZ-2) { \
9018     gz1->outbuf[gz1->outcnt++] = (uch) ((w) & 0xff); \
9019     gz1->outbuf[gz1->outcnt++] = (uch) ((ush)(w) >> 8); \
9020   } else { \
9021     put_byte((uch)((w) & 0xff)); \
9022     put_byte((uch)((ush)(w) >> 8)); \
9023   } \
9024 }
9025
9026 #define put_long(n) { \
9027     put_short((n) & 0xffff); \
9028     put_short(((ulg)(n)) >> 16); \
9029 }
9030
9031 #ifdef CRYPT
9032
9033 #  define NEXTBYTE() \
9034      (decrypt ? (cc = get_byte(), zdecode(cc), cc) : get_byte())
9035 #else
9036 #  define NEXTBYTE() (uch)get_byte()
9037 #endif
9038
9039 #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
9040 #define DUMPBITS(n) {b>>=(n);k-=(n);}
9041
9042 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
9043 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
9044
9045 #define put_ubyte(c) {gz1->window[gz1->outcnt++]=(uch)(c); if (gz1->outcnt==WSIZE)\
9046    flush_window(gz1);}
9047
9048 #define WARN(msg) { if (gz1->exit_code == OK) gz1->exit_code = WARNING; }
9049
9050 #define get_byte()  (gz1->inptr < gz1->insize ? gz1->inbuf[gz1->inptr++] : fill_inbuf(gz1,0))
9051 #define try_byte()  (gz1->inptr < gz1->insize ? gz1->inbuf[gz1->inptr++] : fill_inbuf(gz1,1))
9052
9053 #define d_code(dist) \
9054    ((dist) < 256 ? gz1->dist_code[dist] : gz1->dist_code[256+((dist)>>7)])
9055
9056 typedef struct config {
9057    ush good_length; 
9058    ush max_lazy;    
9059    ush nice_length; 
9060    ush max_chain;
9061 } config;
9062
9063 config configuration_table[10] = {
9064
9065  {0,    0,  0,    0},  
9066  {4,    4,  8,    4},  
9067  {4,    5, 16,    8},
9068  {4,    6, 32,   32},
9069  {4,    4, 16,   16},  
9070  {8,   16, 32,   32},
9071  {8,   16, 128, 128},
9072  {8,   32, 128, 256},
9073  {32, 128, 258, 1024},
9074  {32, 258, 258, 4096}}; 
9075
9076 typedef struct ct_data {
9077
9078     union {
9079         ush  freq; 
9080         ush  code; 
9081     } fc;
9082     union {
9083         ush  dad;  
9084         ush  len;  
9085     } dl;
9086
9087 } ct_data;
9088
9089 typedef struct tree_desc {
9090     ct_data *dyn_tree;    
9091     ct_data *static_tree; 
9092     int     *extra_bits;  
9093     int     extra_base;   
9094     int     elems;        
9095     int     max_length;   
9096     int     max_code;     
9097 } tree_desc;
9098
9099 struct huft {
9100   uch e;                
9101   uch b;                
9102   union {
9103     ush n;              
9104     struct huft *t;     
9105   } v;
9106 };
9107
9108 uch bl_order[BL_CODES]
9109    = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
9110
9111 int extra_lbits[LENGTH_CODES]
9112    = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
9113
9114 int extra_dbits[D_CODES]
9115    = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
9116
9117 int extra_blbits[BL_CODES]
9118    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
9119
9120 ulg crc_32_tab[] = {
9121   0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
9122   0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
9123   0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
9124   0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
9125   0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
9126   0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
9127   0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
9128   0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
9129   0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
9130   0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
9131   0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
9132   0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
9133   0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
9134   0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
9135   0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
9136   0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
9137   0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
9138   0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
9139   0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
9140   0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
9141   0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
9142   0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
9143   0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
9144   0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
9145   0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
9146   0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
9147   0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
9148   0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
9149   0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
9150   0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
9151   0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
9152   0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
9153   0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
9154   0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
9155   0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
9156   0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
9157   0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
9158   0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
9159   0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
9160   0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
9161   0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
9162   0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
9163   0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
9164   0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
9165   0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
9166   0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
9167   0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
9168   0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
9169   0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
9170   0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
9171   0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
9172   0x2d02ef8dL
9173 };
9174
9175 typedef struct _GZ1 {
9176  long     versionid1;
9177  int      state;
9178  int      done;
9179  int      deflate1_initialized;     
9180  unsigned deflate1_hash_head;       
9181  unsigned deflate1_prev_match;      
9182  int      deflate1_flush;           
9183  int      deflate1_match_available; 
9184  unsigned deflate1_match_length;    
9185
9186  char ifname[MAX_PATH_LEN]; 
9187  char ofname[MAX_PATH_LEN]; 
9188
9189  struct stat istat;     
9190  gz1_file_t  zfile;
9191  
9192  int   input_ismem;     
9193  char *input_ptr;       
9194  long  input_bytesleft; 
9195  
9196  int   output_ismem;    
9197  char *output_ptr;      
9198  uns   output_maxlen;   
9199
9200  int  compr_level;      
9201  long time_stamp;       
9202  long ifile_size;       
9203  int  ifd;              
9204  int  ofd;              
9205  int  part_nb;          
9206  int  last_member;      
9207  int  save_orig_name;   
9208  long header_bytes;     
9209  long bytes_in;         
9210  long bytes_out;        
9211  uns  insize;           
9212  uns  inptr;            
9213  uns  outcnt;           
9214  uns  ins_h;            
9215  long block_start;      
9216  uns  good_match;       
9217  uni  max_lazy_match;   
9218  uni  prev_length;      
9219  uns  max_chain_length; 
9220  uns  strstart;         
9221  uns  match_start;      
9222  int  eofile;           
9223  uns  lookahead;        
9224  ush *file_type;        
9225  int *file_method;      
9226  ulg  opt_len;          
9227  ulg  static_len;       
9228  ulg  compressed_len;   
9229  ulg  input_len;        
9230  uns  last_flags;       
9231  uch  flags;            
9232  uns  last_lit;         
9233  uns  last_dist;        
9234  uch  flag_bit;         
9235  int  heap_len;         
9236  int  heap_max;         
9237  ulg  bb;               
9238  uns  bk;               
9239  ush  bi_buf;           
9240  int  bi_valid;         
9241  uns  hufts;            
9242  int  decrypt;          
9243  int  ascii;            
9244  int  msg_done;         
9245  int  abortflag;        
9246  int  decompress;       
9247  int  do_lzw;           
9248  int  to_stdout;        
9249  int  force;            
9250  int  verbose;
9251  int  quiet;
9252  int  list;             
9253  int  test;             
9254  int  ext_header;       
9255  int  pkzip;            
9256  int  method;           
9257  int  level;            
9258  int  no_time;          
9259  int  no_name;          
9260  int  exit_code;        
9261  int  lbits;            
9262  int  dbits;            
9263  ulg  window_size;      
9264  ulg  crc;              
9265
9266  uch  dist_code[512];
9267  uch  length_code[MAX_MATCH-MIN_MATCH+1];
9268  int  heap[2*L_CODES+1];
9269  uch  depth[2*L_CODES+1];
9270  int  base_length[LENGTH_CODES];
9271  int  base_dist[D_CODES];
9272  ush  bl_count[MAX_BITS+1];
9273  uch  flag_buf[(LIT_BUFSIZE/8)];
9274
9275  #ifdef DYN_ALLOC
9276  uch *inbuf;
9277  uch *outbuf;
9278  ush *d_buf;
9279  uch *window;
9280  #else
9281  uch inbuf [INBUFSIZ +INBUF_EXTRA];
9282  uch outbuf[OUTBUFSIZ+OUTBUF_EXTRA];
9283  ush d_buf [DIST_BUFSIZE];
9284  uch window[2L*WSIZE];
9285  #endif
9286
9287  #ifdef FULL_SEARCH
9288  #define nice_match MAX_MATCH
9289  #else
9290  int nice_match;
9291  #endif
9292
9293  #ifdef CRYPT
9294  uch cc;
9295  #endif
9296
9297  ct_data static_ltree[L_CODES+2];
9298  ct_data static_dtree[D_CODES];
9299  ct_data dyn_dtree[(2*D_CODES+1)];
9300  ct_data dyn_ltree[HEAP_SIZE];
9301  ct_data bl_tree[2*BL_CODES+1];
9302
9303  tree_desc l_desc;
9304  tree_desc d_desc;
9305  tree_desc bl_desc;
9306
9307  #ifndef MAXSEG_64K
9308
9309  ush prev2[1L<<BITS];
9310
9311  #define prev gz1->prev2
9312  #define head (gz1->prev2+WSIZE)
9313
9314  #else
9315
9316  ush * prev2;
9317  ush * tab_prefix1;
9318
9319  #define prev gz1->prev2
9320  #define head gz1->tab_prefix1
9321
9322  #endif
9323
9324 } GZ1;
9325 typedef GZ1 *PGZ1;
9326 int gz1_size = sizeof( GZ1 );
9327
9328 /* Declare some local function protypes... */
9329
9330 /* Any routine name that can/might conflict with */
9331 /* other modules or object code should simply have */
9332 /* the standard prefix 'gz1_' added to the front. */
9333 /* This will only usually be any kind of problem at all */
9334 /* if the code is being compiled directly into the parent */
9335 /* instead of being built as a standalone DLL or DSO library. */
9336
9337 PGZ1  gz1_init        ( void     );
9338 int   gz1_cleanup     ( PGZ1 gz1 );
9339 ulg   gz1_deflate     ( PGZ1 gz1 );
9340 ulg   gz1_deflate_fast( PGZ1 gz1 );
9341
9342 /* The rest of the routines should not need the 'gz1_' prefix. */
9343 /* No conflicts reported at this time. */
9344
9345 int   inflate        ( PGZ1 gz1 );
9346 int   inflate_dynamic( PGZ1 gz1 );
9347 int   inflate_stored ( PGZ1 gz1 );
9348 int   inflate_fixed  ( PGZ1 gz1 );
9349 void  fill_window    ( PGZ1 gz1 );
9350 void  flush_outbuf   ( PGZ1 gz1 );
9351 void  flush_window   ( PGZ1 gz1 );
9352 void  bi_windup      ( PGZ1 gz1 );
9353 void  set_file_type  ( PGZ1 gz1 );
9354 void  init_block     ( PGZ1 gz1 );
9355 int   build_bl_tree  ( PGZ1 gz1 );
9356 void  read_error     ( PGZ1 gz1 );
9357 void  write_error    ( PGZ1 gz1 );
9358 int   get_header     ( PGZ1 gz1, int in );
9359 int   inflate_block  ( PGZ1 gz1, int *e );
9360 int   fill_inbuf     ( PGZ1 gz1, int eof_ok );
9361 char *gz1_basename   ( PGZ1 gz1, char *fname );
9362 int   longest_match  ( PGZ1 gz1, unsigned cur_match );
9363 void  bi_init        ( PGZ1 gz1, gz1_file_t zipfile );
9364 int   file_read      ( PGZ1 gz1, char *buf, unsigned size );
9365 void  write_buf      ( PGZ1 gz1, int fd, voidp buf, unsigned cnt );
9366
9367 void  error( char *msg   );
9368
9369 int zip(
9370 PGZ1 gz1, 
9371 int  in,  
9372 int  out  
9373 );
9374
9375 ulg flush_block(
9376 PGZ1  gz1,        
9377 char *buf,        
9378 ulg   stored_len, 
9379 int   eof         
9380 );
9381
9382 void copy_block(
9383 PGZ1      gz1,    
9384 char     *buf,    
9385 unsigned  len,    
9386 int       header  
9387 );
9388
9389 int ct_tally(
9390 PGZ1 gz1,  
9391 int  dist, 
9392 int  lc    
9393 );
9394
9395 void send_bits(
9396 PGZ1 gz1,   
9397 int  value, 
9398 int  length 
9399 );
9400
9401 void send_tree(
9402 PGZ1      gz1,     
9403 ct_data  *tree,    
9404 int       max_code 
9405 );
9406
9407 void send_all_trees(
9408 PGZ1 gz1,    
9409 int  lcodes, 
9410 int  dcodes, 
9411 int  blcodes 
9412 );
9413
9414 void ct_init(
9415 PGZ1  gz1,    
9416 ush  *attr,   
9417 int  *methodp 
9418 );
9419
9420 void lm_init(
9421 PGZ1 gz1,        
9422 int  pack_level, 
9423 ush *flags       
9424 );
9425
9426 void build_tree(
9427 PGZ1        gz1, 
9428 tree_desc  *desc 
9429 );
9430
9431 void compress_block(
9432 PGZ1      gz1,   
9433 ct_data  *ltree, 
9434 ct_data  *dtree  
9435 );
9436
9437 void gen_bitlen(
9438 PGZ1        gz1, 
9439 tree_desc  *desc 
9440 );
9441
9442 void pqdownheap(
9443 PGZ1      gz1,  
9444 ct_data  *tree, 
9445 int       k     
9446 );
9447
9448 int huft_build(
9449 PGZ1          gz1, 
9450 unsigned     *b,   
9451 unsigned      n,   
9452 unsigned      s,   
9453 ush          *d,   
9454 ush          *e,   
9455 struct huft **t,   
9456 int          *m    
9457 );
9458
9459 ulg updcrc(
9460 PGZ1      gz1, 
9461 uch      *s,   
9462 unsigned  n    
9463 );
9464
9465 int inflate_codes(
9466 PGZ1         gz1,  
9467 struct huft *tl,   
9468 struct huft *td,   
9469 int          bl,   
9470 int          bd    
9471 );
9472
9473 void gen_codes(
9474 PGZ1      gz1,     
9475 ct_data  *tree,    
9476 int       max_code 
9477 );
9478
9479 void scan_tree(
9480 PGZ1     gz1,     
9481 ct_data *tree,    
9482 int      max_code 
9483 );
9484
9485 unsigned bi_reverse(
9486 PGZ1     gz1,  
9487 unsigned code, 
9488 int      len   
9489 );
9490
9491 int huft_free(
9492 PGZ1         gz1, 
9493 struct huft *t    
9494 );
9495
9496 PGZ1 gz1_init()
9497 {
9498  PGZ1 gz1=0; 
9499
9500  gz1 = (PGZ1) malloc( gz1_size );
9501
9502  if ( !gz1 ) 
9503    {
9504     return 0; 
9505    }
9506
9507  memset( gz1, 0, gz1_size );
9508
9509  ALLOC(uch, gz1->inbuf,  INBUFSIZ +INBUF_EXTRA);
9510
9511  if ( !gz1->inbuf ) 
9512    {
9513     free( gz1 ); 
9514     return 0;    
9515    }
9516
9517  ALLOC(uch, gz1->outbuf, OUTBUFSIZ+OUTBUF_EXTRA);
9518  
9519  if ( !gz1->outbuf ) 
9520    {
9521     FREE( gz1->inbuf  ); 
9522     free( gz1         ); 
9523     return 0;            
9524    }
9525
9526  ALLOC(ush, gz1->d_buf,  DIST_BUFSIZE);
9527
9528  if ( !gz1->d_buf ) 
9529    {
9530     FREE( gz1->outbuf ); 
9531     FREE( gz1->inbuf  ); 
9532     free( gz1         ); 
9533     return 0;            
9534    }
9535
9536  ALLOC(uch, gz1->window, 2L*WSIZE);
9537
9538  if ( !gz1->window ) 
9539    {
9540     FREE( gz1->d_buf  ); 
9541     FREE( gz1->outbuf ); 
9542     FREE( gz1->inbuf  ); 
9543     free( gz1         ); 
9544     return 0;            
9545    }
9546
9547  #ifndef MAXSEG_64K
9548  
9549  #else 
9550  
9551  ALLOC(ush, gz1->prev2, 1L<<(BITS-1) );
9552
9553  if ( !gz1->prev2 ) 
9554    {
9555     FREE( gz1->window ); 
9556     FREE( gz1->d_buf  ); 
9557     FREE( gz1->outbuf ); 
9558     FREE( gz1->inbuf  ); 
9559     free( gz1         ); 
9560     return 0;            
9561    }
9562
9563  ALLOC(ush, gz1->tab_prefix1, 1L<<(BITS-1) );
9564
9565  if ( !gz1->tab_prefix1 ) 
9566    {
9567     FREE( gz1->prev2  ); 
9568     FREE( gz1->window ); 
9569     FREE( gz1->d_buf  ); 
9570     FREE( gz1->outbuf ); 
9571     FREE( gz1->inbuf  ); 
9572     free( gz1         ); 
9573     return 0;            
9574    }
9575
9576  #endif 
9577
9578  gz1->method      = DEFLATED;     
9579  gz1->level       = 6;            
9580  gz1->no_time     = -1;           
9581  gz1->no_name     = -1;           
9582  gz1->exit_code   = OK;           
9583  gz1->lbits       = 9;            
9584  gz1->dbits       = 6;            
9585
9586  gz1->window_size = (ulg)2*WSIZE;     
9587  gz1->crc         = (ulg)0xffffffffL; 
9588
9589  gz1->d_desc.dyn_tree     = (ct_data *) gz1->dyn_dtree;
9590  gz1->d_desc.static_tree  = (ct_data *) gz1->static_dtree;
9591  gz1->d_desc.extra_bits   = (int     *) extra_dbits; 
9592  gz1->d_desc.extra_base   = (int      ) 0;
9593  gz1->d_desc.elems        = (int      ) D_CODES;
9594  gz1->d_desc.max_length   = (int      ) MAX_BITS;
9595  gz1->d_desc.max_code     = (int      ) 0;
9596
9597  gz1->l_desc.dyn_tree     = (ct_data *) gz1->dyn_ltree;
9598  gz1->l_desc.static_tree  = (ct_data *) gz1->static_ltree;
9599  gz1->l_desc.extra_bits   = (int     *) extra_lbits; 
9600  gz1->l_desc.extra_base   = (int      ) LITERALS+1;
9601  gz1->l_desc.elems        = (int      ) L_CODES;
9602  gz1->l_desc.max_length   = (int      ) MAX_BITS;
9603  gz1->l_desc.max_code     = (int      ) 0;
9604
9605  gz1->bl_desc.dyn_tree    = (ct_data *) gz1->bl_tree;
9606  gz1->bl_desc.static_tree = (ct_data *) 0;
9607  gz1->bl_desc.extra_bits  = (int     *) extra_blbits; 
9608  gz1->bl_desc.extra_base  = (int      ) 0;
9609  gz1->bl_desc.elems       = (int      ) BL_CODES;
9610  gz1->bl_desc.max_length  = (int      ) MAX_BL_BITS;
9611  gz1->bl_desc.max_code    = (int      ) 0;
9612
9613  return (PGZ1) gz1;
9614
9615 }
9616
9617 int gz1_cleanup( PGZ1 gz1 )
9618 {
9619  
9620  #ifndef MAXSEG_64K
9621  
9622  #else
9623  
9624  FREE( gz1->tab_prefix1 );
9625  FREE( gz1->prev2       );
9626  #endif 
9627
9628  FREE( gz1->window ); 
9629  FREE( gz1->d_buf  ); 
9630  FREE( gz1->outbuf ); 
9631  FREE( gz1->inbuf  ); 
9632
9633  free( gz1 ); 
9634  gz1 = 0;     
9635
9636  return 0;
9637 }
9638
9639 int (*read_buf)(PGZ1 gz1, char *buf, unsigned size);
9640
9641 void error( char *msg )
9642 {
9643  msg = msg;
9644 }
9645
9646 int (*work)( PGZ1 gz1, int infile, int outfile ) = 0; 
9647
9648 #ifdef __BORLANDC__
9649 #pragma argsused
9650 #endif
9651
9652 int get_header( PGZ1 gz1, int in )
9653 {
9654  uch       flags;    
9655  char      magic[2]; 
9656  ulg       stamp;    
9657  unsigned  len;      
9658  unsigned  part;     
9659
9660  if ( gz1->force && gz1->to_stdout )
9661    {
9662     magic[0] = (char)try_byte();
9663     magic[1] = (char)try_byte();
9664    }
9665  else
9666    {
9667     magic[0] = (char)get_byte();
9668     magic[1] = (char)get_byte();
9669    }
9670
9671  gz1->method       = -1;        
9672  gz1->header_bytes = 0;         
9673  gz1->last_member  = RECORD_IO; 
9674  gz1->part_nb++;                
9675
9676  if ( memcmp(magic, GZIP_MAGIC,     2 ) == 0 ||
9677       memcmp(magic, OLD_GZIP_MAGIC, 2 ) == 0 )
9678    {
9679     gz1->method = (int)get_byte();
9680
9681     if ( gz1->method != DEFLATED )
9682       {
9683        gz1->exit_code = LZ1_ERROR;
9684
9685        return -1;
9686       }
9687
9688     return -1;
9689
9690     if ((flags & ENCRYPTED) != 0)
9691       {
9692        gz1->exit_code = LZ1_ERROR;
9693        return -1;
9694       }
9695
9696     if ((flags & CONTINUATION) != 0)
9697       {
9698        gz1->exit_code = LZ1_ERROR;
9699        if ( gz1->force <= 1) return -1;
9700       }
9701
9702     if ((flags & RESERVED) != 0)
9703       {
9704        gz1->exit_code = LZ1_ERROR;
9705        if ( gz1->force <= 1)
9706           return -1;
9707       }
9708
9709     stamp  = (ulg)get_byte();
9710         stamp |= ((ulg)get_byte()) << 8;
9711         stamp |= ((ulg)get_byte()) << 16;
9712         stamp |= ((ulg)get_byte()) << 24;
9713
9714     if ( stamp != 0 && !gz1->no_time )
9715       {
9716        gz1->time_stamp = stamp;
9717       }
9718
9719     (void)get_byte(); 
9720     (void)get_byte(); 
9721
9722     if ((flags & CONTINUATION) != 0)
9723       {
9724        part  = (unsigned)  get_byte();
9725        part |= ((unsigned) get_byte())<<8;
9726       }
9727
9728     if ((flags & EXTRA_FIELD) != 0)
9729       {
9730         len  = (unsigned)  get_byte();
9731         len |= ((unsigned) get_byte())<<8;
9732
9733         while (len--) (void)get_byte();
9734       }
9735
9736     if ((flags & COMMENT) != 0)
9737       {
9738        while (get_char() != 0)  ;
9739       }
9740
9741     if ( gz1->part_nb == 1 )
9742       {
9743        gz1->header_bytes = gz1->inptr + 2*sizeof(long);
9744       }
9745    }
9746
9747  return gz1->method;
9748 }
9749
9750 int fill_inbuf( PGZ1 gz1, int eof_ok )
9751 {
9752  int len;
9753  int bytes_to_copy;
9754
9755  gz1->insize = 0;
9756  errno       = 0;
9757
9758  do {
9759      if ( gz1->input_ismem )
9760        {
9761         if ( gz1->input_bytesleft > 0 )
9762           {
9763            bytes_to_copy = INBUFSIZ - gz1->insize;
9764
9765            if ( bytes_to_copy > gz1->input_bytesleft )
9766              {
9767               bytes_to_copy = gz1->input_bytesleft;
9768              }
9769
9770            memcpy(
9771            (char*)gz1->inbuf+gz1->insize,
9772            gz1->input_ptr,
9773            bytes_to_copy
9774            );
9775
9776            gz1->input_ptr       += bytes_to_copy;
9777            gz1->input_bytesleft -= bytes_to_copy;
9778
9779            len = bytes_to_copy;
9780           }
9781         else 
9782           {
9783            len = 0; 
9784           }
9785        }
9786      else 
9787        {
9788         len =
9789         read(
9790         gz1->ifd,
9791         (char*)gz1->inbuf+gz1->insize,
9792         INBUFSIZ-gz1->insize
9793         );
9794        }
9795
9796      if (len == 0 || len == EOF) break;
9797
9798      gz1->insize += len;
9799
9800     } while( gz1->insize < INBUFSIZ );
9801
9802  if ( gz1->insize == 0 )
9803    {
9804     if( eof_ok ) return EOF;
9805     read_error( gz1 );
9806    }
9807
9808  gz1->bytes_in += (ulg) gz1->insize;
9809  gz1->inptr     = 1;
9810
9811  return gz1->inbuf[0];
9812 }
9813
9814 ulg updcrc(
9815 PGZ1      gz1, 
9816 uch      *s,   
9817 unsigned  n    
9818 )
9819 {
9820  register ulg c; 
9821
9822  if ( s == NULL )
9823    {
9824     c = 0xffffffffL;
9825    }
9826  else
9827    {
9828     c = gz1->crc;
9829
9830     if ( n )
9831       {
9832        do{
9833           c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
9834
9835          } while( --n );
9836       }
9837    }
9838
9839  gz1->crc = c;
9840
9841  return( c ^ 0xffffffffL ); 
9842 }
9843
9844 void read_error( PGZ1 gz1 )
9845 {
9846  gz1->abortflag = 1;
9847 }
9848
9849 void mod_gzip_strlwr( char *s )
9850 {
9851  char *p1=s;
9852
9853  if ( s == 0 ) return;
9854
9855  while ( *p1 != 0 )
9856    {
9857     if ( *p1 > 96 ) *p1 = *p1 - 32;
9858     p1++;
9859    }
9860 }
9861
9862 #ifdef __BORLANDC__
9863 #pragma argsused
9864 #endif
9865
9866 char *gz1_basename( PGZ1 gz1, char *fname )
9867 {
9868  char *p;
9869  if ((p = strrchr(fname, PATH_SEP))  != NULL) fname = p+1;
9870  #ifdef PATH_SEP2
9871  if ((p = strrchr(fname, PATH_SEP2)) != NULL) fname = p+1;
9872  #endif
9873  #ifdef PATH_SEP3
9874  if ((p = strrchr(fname, PATH_SEP3)) != NULL) fname = p+1;
9875  #endif
9876  #ifdef SUFFIX_SEP
9877  if ((p = strrchr(fname, SUFFIX_SEP)) != NULL) *p = '\0';
9878  #endif
9879  if (casemap('A') == 'a') mod_gzip_strlwr(fname);
9880  return fname;
9881 }
9882
9883 void write_buf( PGZ1 gz1, int fd, voidp buf, unsigned cnt )
9884 {
9885  unsigned n;
9886
9887  if ( gz1->output_ismem )
9888    {
9889     if ( ( gz1->bytes_out + cnt ) < gz1->output_maxlen )
9890       {
9891        memcpy( gz1->output_ptr, buf, cnt );
9892        gz1->output_ptr += cnt;
9893       }
9894     else
9895       {
9896        write_error( gz1 );
9897       }
9898    }
9899  else
9900    {
9901     while ((n = write(fd, buf, cnt)) != cnt)
9902       {
9903        if (n == (unsigned)(-1))
9904          {
9905           write_error( gz1 );
9906          }
9907        cnt -= n;
9908        buf = (voidp)((char*)buf+n);
9909       }
9910    }
9911 }
9912
9913 void write_error( PGZ1 gz1 )
9914 {
9915  gz1->abortflag = 1;
9916 }
9917
9918 #ifdef __TURBOC__
9919 #ifndef BC55
9920
9921 static ush ptr_offset = 0;
9922
9923 void * fcalloc(
9924 unsigned items, 
9925 unsigned size   
9926 )
9927 {
9928  void * buf = farmalloc((ulg)items*size + 16L);
9929
9930  if (buf == NULL) return NULL;
9931
9932  if (ptr_offset == 0)
9933    {
9934     ptr_offset = (ush)((uch*)buf-0);
9935    }
9936  else if (ptr_offset != (ush)((uch*)buf-0))
9937    {
9938     error("inconsistent ptr_offset");
9939    }
9940
9941  *((ush*)&buf+1) += (ptr_offset + 15) >> 4;
9942  *(ush*)&buf = 0;
9943
9944  return buf;
9945 }
9946
9947 void fcfree( void *ptr )
9948 {
9949  *((ush*)&ptr+1) -= (ptr_offset + 15) >> 4;
9950  *(ush*)&ptr = ptr_offset;
9951
9952  farfree(ptr);
9953 }
9954
9955 #endif 
9956 #endif 
9957
9958 int zip(
9959 PGZ1 gz1, 
9960 int in,   
9961 int out   
9962 )
9963 {
9964  uch  flags = 0;         
9965  ush  attr  = 0;         
9966  ush  deflate_flags = 0; 
9967
9968  gz1->ifd    = in;
9969  gz1->ofd    = out;
9970  gz1->outcnt = 0;
9971
9972  gz1->method = DEFLATED;
9973
9974  put_byte(GZIP_MAGIC[0]); 
9975  put_byte(GZIP_MAGIC[1]);
9976  put_byte(DEFLATED);      
9977
9978  if ( gz1->save_orig_name )
9979    {
9980         flags |= ORIG_NAME;
9981    }
9982
9983  put_byte(flags);           
9984  put_long(gz1->time_stamp); 
9985
9986  gz1->crc = -1; 
9987
9988  updcrc( gz1, NULL, 0 ); 
9989
9990  bi_init( gz1, out );
9991  ct_init( gz1, &attr, &gz1->method );
9992  lm_init( gz1, gz1->level, &deflate_flags );
9993
9994  put_byte((uch)deflate_flags); 
9995
9996  put_byte(OS_CODE); 
9997
9998  if ( gz1->save_orig_name )
9999    {
10000     char *p = gz1_basename( gz1, gz1->ifname );
10001
10002     do {
10003             put_char(*p);
10004
10005        } while (*p++);
10006    }
10007
10008  gz1->header_bytes = (long)gz1->outcnt;
10009
10010  (void) gz1_deflate( gz1 );
10011
10012  put_long( gz1->crc      );
10013  put_long( gz1->bytes_in );
10014
10015  gz1->header_bytes += 2*sizeof(long);
10016
10017  flush_outbuf( gz1 );
10018
10019  return OK;
10020 }
10021
10022 ulg gz1_deflate( PGZ1 gz1 )
10023 {
10024     unsigned hash_head;      
10025     unsigned prev_match;     
10026     int flush;               
10027     int match_available = 0; 
10028     register unsigned match_length = MIN_MATCH-1; 
10029 #ifdef DEBUG
10030     long isize;        
10031 #endif
10032
10033     if (gz1->compr_level <= 3)
10034       {
10035        return gz1_deflate_fast(gz1);
10036       }
10037
10038     while (gz1->lookahead != 0)
10039       {
10040        gz1->ins_h =
10041        (((gz1->ins_h)<<H_SHIFT) ^ (gz1->window[gz1->strstart+MIN_MATCH-1])) & HASH_MASK;
10042
10043        prev[ gz1->strstart & WMASK ] = hash_head = head[ gz1->ins_h ];
10044
10045        head[ gz1->ins_h ] = gz1->strstart;
10046
10047         gz1->prev_length = match_length, prev_match = gz1->match_start;
10048         match_length = MIN_MATCH-1;
10049
10050         if (hash_head != NIL && gz1->prev_length < gz1->max_lazy_match &&
10051             gz1->strstart - hash_head <= MAX_DIST) {
10052             
10053             match_length = longest_match( gz1, hash_head );
10054             
10055             if (match_length > gz1->lookahead) match_length = gz1->lookahead;
10056
10057             if (match_length == MIN_MATCH && gz1->strstart-gz1->match_start > TOO_FAR){
10058                 
10059                 match_length--;
10060             }
10061         }
10062         
10063         if (gz1->prev_length >= MIN_MATCH && match_length <= gz1->prev_length) {
10064
10065             flush = ct_tally(gz1,gz1->strstart-1-prev_match, gz1->prev_length - MIN_MATCH);
10066
10067             gz1->lookahead        -= ( gz1->prev_length - 1 );
10068             gz1->prev_length -= 2;
10069
10070             do {
10071                 gz1->strstart++;
10072
10073                 gz1->ins_h =
10074                 (((gz1->ins_h)<<H_SHIFT) ^ (gz1->window[ gz1->strstart + MIN_MATCH-1])) & HASH_MASK;
10075
10076                 prev[ gz1->strstart & WMASK ] = hash_head = head[gz1->ins_h];
10077
10078                 head[ gz1->ins_h ] = gz1->strstart;
10079
10080             } while (--gz1->prev_length != 0);
10081             match_available = 0;
10082             match_length = MIN_MATCH-1;
10083             gz1->strstart++;
10084             if (flush) FLUSH_BLOCK(0), gz1->block_start = gz1->strstart;
10085
10086         } else if (match_available) {
10087             
10088             if (ct_tally( gz1, 0, gz1->window[gz1->strstart-1] )) {
10089                 FLUSH_BLOCK(0), gz1->block_start = gz1->strstart;
10090             }
10091             gz1->strstart++;
10092             gz1->lookahead--;
10093         } else {
10094             
10095             match_available = 1;
10096             gz1->strstart++;
10097             gz1->lookahead--;
10098         }
10099         
10100         while (gz1->lookahead < MIN_LOOKAHEAD && !gz1->eofile) fill_window(gz1);
10101     }
10102     if (match_available) ct_tally( gz1, 0, gz1->window[gz1->strstart-1] );
10103
10104     return FLUSH_BLOCK(1); 
10105
10106  return 0;
10107 }
10108
10109 void flush_outbuf( PGZ1 gz1 )
10110 {
10111  if ( gz1->outcnt == 0 )
10112    {
10113     return;
10114    }
10115
10116  write_buf( gz1, gz1->ofd, (char *)gz1->outbuf, gz1->outcnt );
10117
10118  gz1->bytes_out += (ulg)gz1->outcnt;
10119  gz1->outcnt = 0;
10120 }
10121
10122 void lm_init(
10123 PGZ1 gz1,        
10124 int  pack_level, 
10125 ush *flags       
10126 )
10127 {
10128  register unsigned j;
10129
10130  if ( pack_level < 1 || pack_level > 9 )
10131    {
10132     error("bad pack level");
10133    }
10134
10135  gz1->compr_level = pack_level;
10136
10137  #if defined(MAXSEG_64K) && HASH_BITS == 15
10138  for (j = 0;  j < HASH_SIZE; j++) head[j] = NIL;
10139  #else
10140  memset( (char*)head, 0, (HASH_SIZE*sizeof(*head)) );
10141  #endif
10142
10143  gz1->max_lazy_match   = configuration_table[pack_level].max_lazy;
10144  gz1->good_match       = configuration_table[pack_level].good_length;
10145  #ifndef FULL_SEARCH
10146  gz1->nice_match       = configuration_table[pack_level].nice_length;
10147  #endif
10148  gz1->max_chain_length = configuration_table[pack_level].max_chain;
10149
10150  if ( pack_level == 1 )
10151    {
10152     *flags |= FAST;
10153    }
10154  else if ( pack_level == 9 )
10155    {
10156     *flags |= SLOW;
10157    }
10158
10159  gz1->strstart    = 0;
10160  gz1->block_start = 0L;
10161  #ifdef ASMV
10162  match_init(); 
10163  #endif
10164
10165  gz1->lookahead = read_buf(gz1,(char*)gz1->window,
10166                   sizeof(int) <= 2 ? (unsigned)WSIZE : 2*WSIZE);
10167
10168  if (gz1->lookahead == 0 || gz1->lookahead == (unsigned)EOF)
10169    {
10170     gz1->eofile = 1, gz1->lookahead = 0;
10171     return;
10172    }
10173
10174  gz1->eofile = 0;
10175
10176  while (gz1->lookahead < MIN_LOOKAHEAD && !gz1->eofile)
10177    {
10178     fill_window(gz1);
10179    }
10180
10181  gz1->ins_h = 0;
10182
10183  for ( j=0; j<MIN_MATCH-1; j++ )
10184     {
10185      gz1->ins_h =
10186      (((gz1->ins_h)<<H_SHIFT) ^ (gz1->window[j])) & HASH_MASK;
10187     }
10188 }
10189
10190 void fill_window( PGZ1 gz1 )
10191 {
10192  register unsigned n, m;
10193
10194  unsigned more =
10195  (unsigned)( gz1->window_size - (ulg)gz1->lookahead - (ulg)gz1->strstart );
10196
10197  if ( more == (unsigned)EOF)
10198    {
10199     more--;
10200    }
10201  else if ( gz1->strstart >= WSIZE+MAX_DIST )
10202    {
10203     memcpy((char*)gz1->window, (char*)gz1->window+WSIZE, (unsigned)WSIZE);
10204
10205     gz1->match_start -= WSIZE;
10206     gz1->strstart    -= WSIZE; 
10207
10208     gz1->block_start -= (long) WSIZE;
10209
10210     for ( n = 0; n < HASH_SIZE; n++ )
10211        {
10212         m = head[n];
10213         head[n] = (ush)(m >= WSIZE ? m-WSIZE : NIL);
10214        }
10215
10216     for ( n = 0; n < WSIZE; n++ )
10217        {
10218         m = prev[n];
10219
10220         prev[n] = (ush)(m >= WSIZE ? m-WSIZE : NIL);
10221        }
10222
10223     more += WSIZE;
10224    }
10225
10226  if ( !gz1->eofile )
10227    {
10228     n = read_buf(gz1,(char*)gz1->window+gz1->strstart+gz1->lookahead, more);
10229
10230     if ( n == 0 || n == (unsigned)EOF )
10231       {
10232        gz1->eofile = 1;
10233       }
10234     else
10235       {
10236        gz1->lookahead += n;
10237       }
10238    }
10239 }
10240
10241 ulg gz1_deflate_fast( PGZ1 gz1 )
10242 {
10243     unsigned hash_head; 
10244     int flush;      
10245     unsigned match_length = 0;  
10246
10247     gz1->prev_length = MIN_MATCH-1;
10248
10249     while (gz1->lookahead != 0)
10250       {
10251        gz1->ins_h =
10252        (((gz1->ins_h)<<H_SHIFT) ^ (gz1->window[ gz1->strstart + MIN_MATCH-1])) & HASH_MASK;
10253        
10254        prev[ gz1->strstart & WMASK ] = hash_head = head[ gz1->ins_h ];
10255
10256        head[ gz1->ins_h ] = gz1->strstart;
10257
10258         if (hash_head != NIL && gz1->strstart - hash_head <= MAX_DIST) {
10259             
10260             match_length = longest_match( gz1, hash_head );
10261             
10262             if (match_length > gz1->lookahead) match_length = gz1->lookahead;
10263         }
10264         if (match_length >= MIN_MATCH) {
10265
10266             flush = ct_tally(gz1,gz1->strstart-gz1->match_start, match_length - MIN_MATCH);
10267
10268             gz1->lookahead -= match_length;
10269
10270             if (match_length <= gz1->max_lazy_match )
10271               {
10272                 match_length--; 
10273
10274                 do {
10275                     gz1->strstart++;
10276
10277                     gz1->ins_h =
10278                     (((gz1->ins_h)<<H_SHIFT) ^ (gz1->window[ gz1->strstart + MIN_MATCH-1])) & HASH_MASK;
10279                     
10280                     prev[ gz1->strstart & WMASK ] = hash_head = head[ gz1->ins_h ];
10281
10282                     head[ gz1->ins_h ] = gz1->strstart;
10283
10284                 } while (--match_length != 0);
10285             gz1->strstart++;
10286             } else {
10287             gz1->strstart += match_length;
10288                 match_length = 0;
10289             gz1->ins_h = gz1->window[gz1->strstart];
10290
10291             gz1->ins_h =
10292             (((gz1->ins_h)<<H_SHIFT) ^ (gz1->window[gz1->strstart+1])) & HASH_MASK;
10293             
10294 #if MIN_MATCH != 3
10295                 Call UPDATE_HASH() MIN_MATCH-3 more times
10296 #endif
10297             }
10298         } else {
10299             
10300             flush = ct_tally(gz1, 0, gz1->window[gz1->strstart]);
10301             gz1->lookahead--;
10302         gz1->strstart++;
10303         }
10304         if (flush) FLUSH_BLOCK(0), gz1->block_start = gz1->strstart;
10305
10306         while (gz1->lookahead < MIN_LOOKAHEAD && !gz1->eofile) fill_window(gz1);
10307     }
10308
10309  return FLUSH_BLOCK(1);
10310 }
10311
10312 void ct_init(
10313 PGZ1  gz1,    
10314 ush  *attr,   
10315 int  *methodp 
10316 )
10317 {
10318  #ifdef DD1
10319  int i,ii;
10320  #endif
10321
10322  int n;      
10323  int bits;   
10324  int length; 
10325  int code;   
10326  int dist;   
10327
10328  gz1->file_type      = attr;
10329  gz1->file_method    = methodp;
10330  gz1->compressed_len = gz1->input_len = 0L;
10331         
10332  if ( gz1->static_dtree[0].dl.len != 0 )
10333    {
10334     return;
10335    }
10336
10337  length = 0;
10338
10339  for ( code = 0; code < LENGTH_CODES-1; code++ )
10340     {
10341      gz1->base_length[code] = length;
10342
10343      for ( n = 0; n < (1<<extra_lbits[code]); n++ )
10344         {
10345          gz1->length_code[length++] = (uch)code;
10346         }
10347     }
10348
10349  gz1->length_code[length-1] = (uch)code;
10350
10351  dist = 0;
10352
10353  for ( code = 0 ; code < 16; code++ )
10354     {
10355      gz1->base_dist[code] = dist;
10356
10357      for ( n = 0; n < (1<<extra_dbits[code]); n++ )
10358         {
10359          gz1->dist_code[dist++] = (uch)code;
10360         }
10361     }
10362
10363  dist >>= 7; 
10364
10365  for ( ; code < D_CODES; code++ )
10366     {
10367      gz1->base_dist[code] = dist << 7;
10368
10369      for ( n = 0; n < (1<<(extra_dbits[code]-7)); n++ )
10370         {
10371          gz1->dist_code[256 + dist++] = (uch)code;
10372         }
10373     }
10374
10375  for ( bits = 0; bits <= MAX_BITS; bits++ )
10376     {
10377      gz1->bl_count[bits] = 0;
10378     }
10379
10380  n = 0;
10381
10382  while (n <= 143) gz1->static_ltree[n++].dl.len = 8, gz1->bl_count[8]++;
10383  while (n <= 255) gz1->static_ltree[n++].dl.len = 9, gz1->bl_count[9]++;
10384  while (n <= 279) gz1->static_ltree[n++].dl.len = 7, gz1->bl_count[7]++;
10385  while (n <= 287) gz1->static_ltree[n++].dl.len = 8, gz1->bl_count[8]++;
10386
10387  gen_codes(gz1,(ct_data *)gz1->static_ltree, L_CODES+1);
10388
10389  for ( n = 0; n < D_CODES; n++ )
10390     {
10391      gz1->static_dtree[n].dl.len  = 5;
10392      gz1->static_dtree[n].fc.code = bi_reverse( gz1, n, 5 );
10393     }
10394
10395  init_block( gz1 );
10396 }
10397
10398 ulg flush_block(
10399 PGZ1  gz1,        
10400 char *buf,        
10401 ulg   stored_len, 
10402 int   eof         
10403 )
10404 {
10405  ulg opt_lenb;     
10406  ulg static_lenb;  
10407  int max_blindex;  
10408
10409  gz1->flag_buf[gz1->last_flags] = gz1->flags;
10410
10411  if (*gz1->file_type == (ush)UNKNOWN) set_file_type(gz1);
10412
10413  build_tree( gz1, (tree_desc *)(&gz1->l_desc) );
10414  build_tree( gz1, (tree_desc *)(&gz1->d_desc) );
10415
10416  max_blindex = build_bl_tree( gz1 );
10417
10418  opt_lenb         = (gz1->opt_len+3+7)>>3;
10419  static_lenb      = (gz1->static_len+3+7)>>3;
10420  gz1->input_len  += stored_len; 
10421
10422  if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
10423
10424 #ifdef FORCE_METHOD
10425  
10426  if ( level == 1 && eof && gz1->compressed_len == 0L )
10427 #else
10428  if (stored_len <= opt_lenb && eof && gz1->compressed_len == 0L && 0 )
10429 #endif
10430    {
10431     if (buf == (char*)0) error ("block vanished");
10432
10433     copy_block( gz1, buf, (unsigned)stored_len, 0 ); 
10434
10435     gz1->compressed_len = stored_len << 3;
10436     *gz1->file_method   = STORED;
10437
10438 #ifdef FORCE_METHOD
10439  } else if (level == 2 && buf != (char*)0) { 
10440 #else
10441  } else if (stored_len+4 <= opt_lenb && buf != (char*)0) {
10442                     
10443 #endif
10444      
10445      send_bits(gz1,(STORED_BLOCK<<1)+eof, 3);  
10446      gz1->compressed_len = (gz1->compressed_len + 3 + 7) & ~7L;
10447      gz1->compressed_len += (stored_len + 4) << 3;
10448
10449      copy_block(gz1, buf, (unsigned)stored_len, 1); 
10450
10451 #ifdef FORCE_METHOD
10452  } else if (level == 3) { 
10453 #else
10454  } else if (static_lenb == opt_lenb) {
10455 #endif
10456      send_bits(gz1,(STATIC_TREES<<1)+eof, 3);
10457
10458      compress_block(
10459      gz1,
10460      (ct_data *)gz1->static_ltree,
10461      (ct_data *)gz1->static_dtree
10462      );
10463
10464      gz1->compressed_len += 3 + gz1->static_len;
10465     }
10466   else
10467     {
10468      send_bits(gz1,(DYN_TREES<<1)+eof, 3);
10469
10470      send_all_trees(
10471      gz1,
10472      gz1->l_desc.max_code+1,
10473      gz1->d_desc.max_code+1,
10474      max_blindex+1
10475      );
10476
10477      compress_block(
10478      gz1,
10479      (ct_data *)gz1->dyn_ltree,
10480      (ct_data *)gz1->dyn_dtree
10481      );
10482
10483      gz1->compressed_len += 3 + gz1->opt_len;
10484     }
10485
10486  init_block( gz1 );
10487
10488  if ( eof )
10489    {
10490     bi_windup( gz1 );
10491
10492     gz1->compressed_len += 7;  
10493    }
10494
10495  return gz1->compressed_len >> 3;
10496 }
10497
10498 #ifdef __BORLANDC__
10499 #pragma argsused
10500 #endif
10501
10502 unsigned bi_reverse(
10503 PGZ1     gz1,  
10504 unsigned code, 
10505 int      len   
10506 )
10507 {
10508  register unsigned res = 0;
10509
10510  do {
10511      res |= code & 1;
10512      code >>= 1, res <<= 1;
10513
10514     } while (--len > 0);
10515
10516  return res >> 1;
10517 }
10518
10519 void set_file_type( PGZ1 gz1 )
10520 {
10521  int n = 0;
10522  unsigned ascii_freq = 0;
10523  unsigned bin_freq = 0;
10524
10525  while (n < 7)        bin_freq += gz1->dyn_ltree[n++].fc.freq;
10526  while (n < 128)    ascii_freq += gz1->dyn_ltree[n++].fc.freq;
10527  while (n < LITERALS) bin_freq += gz1->dyn_ltree[n++].fc.freq;
10528
10529  *gz1->file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII;
10530 }
10531
10532 void init_block( PGZ1 gz1 )
10533 {
10534  int n; 
10535
10536  for (n = 0; n < L_CODES;  n++) gz1->dyn_ltree[n].fc.freq = 0;
10537  for (n = 0; n < D_CODES;  n++) gz1->dyn_dtree[n].fc.freq = 0;
10538  for (n = 0; n < BL_CODES; n++) gz1->bl_tree[n].fc.freq   = 0;
10539
10540  gz1->dyn_ltree[END_BLOCK].fc.freq = 1;
10541
10542  gz1->opt_len    = 0L;
10543  gz1->static_len = 0L;
10544  gz1->last_lit   = 0;
10545  gz1->last_dist  = 0;
10546  gz1->last_flags = 0;
10547  gz1->flags      = 0;
10548  gz1->flag_bit   = 1;
10549 }
10550
10551 void bi_init( PGZ1 gz1, gz1_file_t zipfile )
10552 {
10553  gz1->zfile    = zipfile;
10554  gz1->bi_buf   = 0;
10555  gz1->bi_valid = 0;
10556
10557  if ( gz1->zfile != NO_FILE )
10558    {
10559     read_buf = file_read;
10560    }
10561 }
10562
10563 int ct_tally(
10564 PGZ1 gz1,  
10565 int  dist, 
10566 int  lc    
10567 )
10568 {
10569  int dcode;
10570
10571  gz1->inbuf[gz1->last_lit++] = (uch)lc;
10572
10573  if ( dist == 0 )
10574    {
10575     gz1->dyn_ltree[lc].fc.freq++; 
10576    }
10577  else
10578    {
10579     dist--; 
10580
10581     gz1->dyn_ltree[gz1->length_code[lc]+LITERALS+1].fc.freq++;
10582     gz1->dyn_dtree[d_code(dist)].fc.freq++;
10583     gz1->d_buf[gz1->last_dist++] = (ush)dist;
10584     gz1->flags |= gz1->flag_bit;
10585    }
10586
10587  gz1->flag_bit <<= 1;
10588
10589  if ( (gz1->last_lit & 7) == 0 )
10590    {
10591     gz1->flag_buf[gz1->last_flags++] = gz1->flags;
10592     gz1->flags = 0, gz1->flag_bit = 1;
10593    }
10594
10595  if ( gz1->level > 2 && (gz1->last_lit & 0xfff) == 0)
10596    {
10597     ulg out_length = (ulg) ( gz1->last_lit * 8L );
10598     ulg in_length  = (ulg) ( gz1->strstart - gz1->block_start );
10599
10600     for ( dcode = 0; dcode < D_CODES; dcode++ )
10601        {
10602         out_length += (ulg) ((gz1->dyn_dtree[dcode].fc.freq)*(5L+extra_dbits[dcode]));
10603        }
10604
10605     out_length >>= 3;
10606
10607     if ( gz1->last_dist < gz1->last_lit/2 && out_length < in_length/2 )
10608       {
10609        return 1;
10610       }
10611    }
10612
10613  return( gz1->last_lit == LIT_BUFSIZE-1 || gz1->last_dist == DIST_BUFSIZE );
10614 }
10615
10616 void compress_block(
10617 PGZ1     gz1,   
10618 ct_data *ltree, 
10619 ct_data *dtree  
10620 )
10621 {
10622  unsigned dist;   
10623  int lc;          
10624  unsigned lx = 0; 
10625  unsigned dx = 0; 
10626  unsigned fx = 0; 
10627  uch flag = 0;    
10628  unsigned code;   
10629  int extra;       
10630
10631  if (gz1->last_lit != 0) do {
10632      if ((lx & 7) == 0) flag = gz1->flag_buf[fx++];
10633      lc = gz1->inbuf[lx++];
10634      if ((flag & 1) == 0) {
10635          send_code(lc, ltree); 
10636      } else {
10637          
10638          code = gz1->length_code[lc];
10639          send_code(code+LITERALS+1, ltree); 
10640          extra = extra_lbits[code];
10641          if (extra != 0) {
10642              lc -= gz1->base_length[code];
10643              send_bits(gz1,lc, extra); 
10644          }
10645          dist = gz1->d_buf[dx++];
10646          
10647          code = d_code(dist);
10648
10649          send_code(code, dtree);       
10650          extra = extra_dbits[code];
10651          if (extra != 0) {
10652              dist -= gz1->base_dist[code];
10653              send_bits(gz1,dist, extra); 
10654          }
10655      } 
10656      flag >>= 1;
10657  } while (lx < gz1->last_lit);
10658
10659  send_code(END_BLOCK, ltree);
10660 }
10661
10662 #ifndef ASMV
10663
10664 int longest_match( PGZ1 gz1, unsigned cur_match )
10665 {
10666  unsigned chain_length = gz1->max_chain_length;   
10667  register uch *scan = gz1->window + gz1->strstart;     
10668  register uch *match;                        
10669  register int len;                           
10670  int best_len = gz1->prev_length;                 
10671  unsigned limit = gz1->strstart > (unsigned)MAX_DIST ? gz1->strstart - (unsigned)MAX_DIST : NIL;
10672  
10673 #if HASH_BITS < 8 || MAX_MATCH != 258
10674    error: Code too clever
10675 #endif
10676
10677 #ifdef UNALIGNED_OK
10678     
10679     register uch *strend    = gz1->window + gz1->strstart + MAX_MATCH - 1;
10680     register ush scan_start = *(ush*)scan;
10681     register ush scan_end   = *(ush*)(scan+best_len-1);
10682 #else
10683     register uch *strend    = gz1->window + gz1->strstart + MAX_MATCH;
10684     register uch scan_end1  = scan[best_len-1];
10685     register uch scan_end   = scan[best_len];
10686 #endif
10687
10688     if (gz1->prev_length >= gz1->good_match) {
10689         chain_length >>= 2;
10690     }
10691
10692     do {
10693         match = gz1->window + cur_match;
10694
10695 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
10696         
10697         if (*(ush*)(match+best_len-1) != scan_end ||
10698             *(ush*)match != scan_start) continue;
10699
10700         scan++, match++;
10701         do {
10702         } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
10703                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
10704                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
10705                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
10706                  scan < strend);
10707         
10708         if (*scan == *match) scan++;
10709
10710         len = (MAX_MATCH - 1) - (int)(strend-scan);
10711         scan = strend - (MAX_MATCH-1);
10712 #else 
10713         if (match[best_len]   != scan_end  ||
10714             match[best_len-1] != scan_end1 ||
10715             *match            != *scan     ||
10716             *++match          != scan[1])      continue;
10717
10718         scan += 2, match++;
10719
10720         do {
10721         } while (*++scan == *++match && *++scan == *++match &&
10722                  *++scan == *++match && *++scan == *++match &&
10723                  *++scan == *++match && *++scan == *++match &&
10724                  *++scan == *++match && *++scan == *++match &&
10725                  scan < strend);
10726
10727         len = MAX_MATCH - (int)(strend - scan);
10728         scan = strend - MAX_MATCH;
10729 #endif 
10730         if (len > best_len) {
10731             gz1->match_start = cur_match;
10732             best_len = len;
10733             if (len >= gz1->nice_match) break;
10734 #ifdef UNALIGNED_OK
10735             scan_end = *(ush*)(scan+best_len-1);
10736 #else
10737             scan_end1  = scan[best_len-1];
10738             scan_end   = scan[best_len];
10739 #endif
10740         }
10741     } while ((cur_match = prev[cur_match & WMASK]) > limit
10742              && --chain_length != 0);
10743
10744     return best_len;
10745 }
10746 #endif 
10747
10748 void send_bits(
10749 PGZ1 gz1,   
10750 int  value, 
10751 int  length 
10752 )
10753 {
10754  if ( gz1->bi_valid > (int) BUFSIZE - length )
10755    {
10756     gz1->bi_buf |= (value << gz1->bi_valid);
10757
10758     put_short(gz1->bi_buf);
10759
10760     gz1->bi_buf = (ush)value >> (BUFSIZE - gz1->bi_valid);
10761     gz1->bi_valid += length - BUFSIZE;
10762    }
10763  else
10764    {
10765     gz1->bi_buf |= value << gz1->bi_valid;
10766     gz1->bi_valid += length;
10767    }
10768 }
10769
10770 void build_tree(
10771 PGZ1       gz1, 
10772 tree_desc *desc 
10773 )
10774 {
10775  int elems      = desc->elems;
10776  ct_data *tree  = desc->dyn_tree;
10777  ct_data *stree = desc->static_tree;
10778
10779  int n;             
10780  int m;             
10781  int max_code = -1; 
10782  int node = elems;  
10783  int new1;          
10784
10785     gz1->heap_len = 0, gz1->heap_max = HEAP_SIZE;
10786
10787     for (n = 0; n < elems; n++) {
10788         if (tree[n].fc.freq != 0) {
10789             gz1->heap[++gz1->heap_len] = max_code = n;
10790             gz1->depth[n] = 0;
10791         } else {
10792             tree[n].dl.len = 0;
10793         }
10794     }
10795
10796     while (gz1->heap_len < 2) {
10797         new1 = gz1->heap[++gz1->heap_len] = (max_code < 2 ? ++max_code : 0);
10798         tree[new1].fc.freq = 1;
10799         gz1->depth[new1] = 0;
10800         gz1->opt_len--; if (stree) gz1->static_len -= stree[new1].dl.len;
10801     }
10802     desc->max_code = max_code;
10803
10804     for (n = gz1->heap_len/2; n >= 1; n--) pqdownheap(gz1, tree, n);
10805
10806     do {
10807         n = gz1->heap[SMALLEST];
10808         gz1->heap[SMALLEST] = gz1->heap[gz1->heap_len--];
10809         pqdownheap(gz1, tree, SMALLEST);
10810         m = gz1->heap[SMALLEST];
10811         gz1->heap[--gz1->heap_max] = n;
10812         gz1->heap[--gz1->heap_max] = m;
10813         tree[node].fc.freq = tree[n].fc.freq + tree[m].fc.freq;
10814         gz1->depth[node] = (uch) (GZ1_MAX(gz1->depth[n], gz1->depth[m]) + 1);
10815         tree[n].dl.dad = tree[m].dl.dad = (ush)node;
10816         gz1->heap[SMALLEST] = node++;
10817         pqdownheap(gz1, tree, SMALLEST);
10818
10819     } while (gz1->heap_len >= 2);
10820
10821     gz1->heap[--gz1->heap_max] = gz1->heap[SMALLEST];
10822
10823     gen_bitlen(gz1,(tree_desc *)desc);
10824
10825     gen_codes(gz1,(ct_data *)tree, max_code);
10826 }
10827
10828 int build_bl_tree( PGZ1 gz1 )
10829 {
10830  int max_blindex; 
10831
10832  scan_tree( gz1, (ct_data *)gz1->dyn_ltree, gz1->l_desc.max_code );
10833  scan_tree( gz1, (ct_data *)gz1->dyn_dtree, gz1->d_desc.max_code );
10834
10835  build_tree( gz1, (tree_desc *)(&gz1->bl_desc) );
10836
10837  for ( max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex-- )
10838     {
10839      if (gz1->bl_tree[bl_order[max_blindex]].dl.len != 0) break;
10840     }
10841
10842  gz1->opt_len += 3*(max_blindex+1) + 5+5+4;
10843
10844  return max_blindex;
10845 }
10846
10847 void gen_codes(
10848 PGZ1     gz1,     
10849 ct_data *tree,    
10850 int      max_code 
10851 )
10852 {
10853  ush next_code[MAX_BITS+1]; 
10854  ush code = 0;              
10855  int bits;                  
10856  int n;                     
10857
10858  for ( bits = 1; bits <= MAX_BITS; bits++ )
10859     {
10860      next_code[bits] = code = (code + gz1->bl_count[bits-1]) << 1;
10861     }
10862
10863  for ( n = 0;  n <= max_code; n++ )
10864     {
10865      int len = tree[n].dl.len;
10866      if (len == 0) continue;
10867
10868      tree[n].fc.code = bi_reverse( gz1, next_code[len]++, len );
10869     }
10870
10871  return;
10872 }
10873
10874 void gen_bitlen(
10875 PGZ1       gz1, 
10876 tree_desc *desc 
10877 )
10878 {
10879  ct_data *tree   = desc->dyn_tree;
10880  int *extra      = desc->extra_bits;
10881  int base             = desc->extra_base;
10882  int max_code         = desc->max_code;
10883  int max_length       = desc->max_length;
10884  ct_data *stree  = desc->static_tree;
10885  int h;              
10886  int n, m;           
10887  int bits;           
10888  int xbits;          
10889  ush f;              
10890  int overflow = 0;   
10891
10892  for (bits = 0; bits <= MAX_BITS; bits++) gz1->bl_count[bits] = 0;
10893
10894  tree[gz1->heap[gz1->heap_max]].dl.len = 0;
10895
10896  for (h = gz1->heap_max+1; h < HEAP_SIZE; h++) {
10897      n = gz1->heap[h];
10898      bits = tree[tree[n].dl.dad].dl.len + 1;
10899      if (bits > max_length) bits = max_length, overflow++;
10900      tree[n].dl.len = (ush)bits;
10901      
10902      if (n > max_code) continue; 
10903
10904      gz1->bl_count[bits]++;
10905      xbits = 0;
10906      if (n >= base) xbits = extra[n-base];
10907      f = tree[n].fc.freq;
10908      gz1->opt_len += (ulg)f * (bits + xbits);
10909      if (stree) gz1->static_len += (ulg)f * (stree[n].dl.len + xbits);
10910  }
10911  if (overflow == 0) return;
10912
10913  do {
10914      bits = max_length-1;
10915      while (gz1->bl_count[bits] == 0) bits--;
10916      gz1->bl_count[bits]--;      
10917      gz1->bl_count[bits+1] += 2; 
10918      gz1->bl_count[max_length]--;
10919      
10920      overflow -= 2;
10921  } while (overflow > 0);
10922
10923  for (bits = max_length; bits != 0; bits--) {
10924      n = gz1->bl_count[bits];
10925      while (n != 0) {
10926          m = gz1->heap[--h];
10927          if (m > max_code) continue;
10928          if (tree[m].dl.len != (unsigned) bits) {
10929              gz1->opt_len += ((long)bits-(long)tree[m].dl.len)*(long)tree[m].fc.freq;
10930              tree[m].dl.len = (ush)bits;
10931          }
10932          n--;
10933      }
10934   }
10935 }
10936
10937 void copy_block(
10938 PGZ1      gz1,    
10939 char     *buf,    
10940 unsigned  len,    
10941 int       header  
10942 )
10943 {
10944  #ifdef CRYPT
10945  int t;
10946  #endif
10947
10948  bi_windup( gz1 ); 
10949
10950  if ( header )
10951    {
10952     put_short((ush)len);
10953     put_short((ush)~len);
10954    }
10955
10956  while( len-- )
10957    {
10958     #ifdef CRYPT
10959         if (key) zencode(*buf, t);
10960     #endif
10961
10962     put_byte(*buf++);
10963    }
10964 }
10965
10966 int file_read( PGZ1 gz1, char *buf, unsigned size )
10967 {
10968  unsigned len = 0;
10969  unsigned bytes_to_copy = 0;
10970
10971  if ( gz1->input_ismem )
10972    {
10973     if ( gz1->input_bytesleft > 0 )
10974       {
10975        bytes_to_copy = size;
10976
10977        if ( bytes_to_copy > (unsigned) gz1->input_bytesleft )
10978          {
10979           bytes_to_copy = (unsigned) gz1->input_bytesleft;
10980          }
10981
10982        memcpy( buf, gz1->input_ptr, bytes_to_copy );
10983
10984        gz1->input_ptr       += bytes_to_copy;
10985        gz1->input_bytesleft -= bytes_to_copy;
10986
10987        len = bytes_to_copy;
10988       }
10989     else
10990       {
10991        len = 0;
10992       }
10993    }
10994  else
10995    {
10996     len = read( gz1->ifd, buf, size );
10997    }
10998
10999  if ( len == (unsigned)(-1) || len == 0 )
11000    {
11001         gz1->crc = gz1->crc ^ 0xffffffffL;
11002     return (int)len;
11003    }
11004
11005  updcrc( gz1, (uch*)buf, len );
11006  gz1->bytes_in += (ulg)len;
11007
11008  return (int)len;
11009 }
11010
11011 void bi_windup( PGZ1 gz1 )
11012 {
11013  if ( gz1->bi_valid > 8 )
11014    {
11015     put_short(gz1->bi_buf);
11016    }
11017  else if ( gz1->bi_valid > 0 )
11018    {
11019     put_byte(gz1->bi_buf);
11020    }
11021
11022  gz1->bi_buf   = 0;
11023  gz1->bi_valid = 0;
11024 }
11025
11026 void send_all_trees(
11027 PGZ1 gz1,    
11028 int  lcodes, 
11029 int  dcodes, 
11030 int  blcodes 
11031 )
11032 {
11033  int rank; 
11034
11035  send_bits(gz1,lcodes-257, 5); 
11036  send_bits(gz1,dcodes-1,   5);
11037  send_bits(gz1,blcodes-4,  4); 
11038
11039  for ( rank = 0; rank < blcodes; rank++ )
11040     {
11041      send_bits(gz1,gz1->bl_tree[bl_order[rank]].dl.len, 3 );
11042     }
11043
11044  send_tree(gz1,(ct_data *)gz1->dyn_ltree, lcodes-1); 
11045  send_tree(gz1,(ct_data *)gz1->dyn_dtree, dcodes-1); 
11046 }
11047
11048 void send_tree(
11049 PGZ1     gz1,     
11050 ct_data *tree,    
11051 int      max_code 
11052 )
11053 {
11054  int n;                        
11055  int prevlen = -1;             
11056  int curlen;                   
11057  int nextlen = tree[0].dl.len; 
11058  int count = 0;                
11059  int max_count = 7;            
11060  int min_count = 4;            
11061
11062  if (nextlen == 0) max_count = 138, min_count = 3;
11063
11064  for ( n = 0; n <= max_code; n++ )
11065     {
11066      curlen  = nextlen;
11067      nextlen = tree[n+1].dl.len;
11068
11069      if (++count < max_count && curlen == nextlen)
11070        {
11071         continue;
11072        }
11073      else if (count < min_count)
11074        {
11075         do { send_code(curlen, gz1->bl_tree); } while (--count != 0);
11076        }
11077      else if (curlen != 0)
11078        {
11079         if ( curlen != prevlen )
11080           {
11081            send_code(curlen, gz1->bl_tree); count--;
11082           }
11083
11084         send_code( REP_3_6, gz1->bl_tree ); send_bits(gz1,count-3, 2);
11085        }
11086      else if (count <= 10)
11087        {
11088         send_code(REPZ_3_10, gz1->bl_tree); send_bits(gz1,count-3, 3);
11089        }
11090      else
11091        {
11092         send_code(REPZ_11_138, gz1->bl_tree); send_bits(gz1,count-11, 7);
11093        }
11094
11095      count   = 0;
11096      prevlen = curlen;
11097
11098      if (nextlen == 0)
11099        {
11100         max_count = 138, min_count = 3;
11101        }
11102      else if (curlen == nextlen)
11103        {
11104         max_count = 6, min_count = 3;
11105        }
11106      else
11107        {
11108         max_count = 7, min_count = 4;
11109        }
11110     }
11111 }
11112
11113 void scan_tree(
11114 PGZ1     gz1,     
11115 ct_data *tree,    
11116 int      max_code 
11117 )
11118 {
11119  int n;                        
11120  int prevlen = -1;             
11121  int curlen;                   
11122  int nextlen = tree[0].dl.len; 
11123  int count = 0;                
11124  int max_count = 7;            
11125  int min_count = 4;            
11126
11127  if (nextlen == 0) max_count = 138, min_count = 3;
11128
11129  tree[max_code+1].dl.len = (ush)0xffff; 
11130
11131  for ( n = 0; n <= max_code; n++ )
11132     {
11133      curlen  = nextlen;
11134      nextlen = tree[n+1].dl.len;
11135
11136      if ( ++count < max_count && curlen == nextlen )
11137        {
11138         continue;
11139        }
11140      else if ( count < min_count )
11141        {
11142         gz1->bl_tree[curlen].fc.freq += count;
11143        }
11144      else if ( curlen != 0 )
11145        {
11146         if ( curlen != prevlen ) gz1->bl_tree[curlen].fc.freq++;
11147         gz1->bl_tree[REP_3_6].fc.freq++;
11148        }
11149      else if ( count <= 10 )
11150        {
11151         gz1->bl_tree[REPZ_3_10].fc.freq++;
11152        }
11153      else
11154        {
11155         gz1->bl_tree[REPZ_11_138].fc.freq++;
11156        }
11157
11158      count   = 0;
11159      prevlen = curlen;
11160
11161      if ( nextlen == 0 )
11162        {
11163         max_count = 138;
11164         min_count = 3;
11165        }
11166      else if (curlen == nextlen)
11167        {
11168         max_count = 6;
11169         min_count = 3;
11170        }
11171      else
11172        {
11173         max_count = 7;
11174         min_count = 4;
11175        }
11176     }
11177 }
11178
11179 void pqdownheap(
11180 PGZ1     gz1,  
11181 ct_data *tree, 
11182 int      k     
11183 )
11184 {
11185  int v = gz1->heap[k];
11186  int j = k << 1;  
11187
11188  while( j <= gz1->heap_len )
11189    {
11190     if (j < gz1->heap_len && smaller(tree, gz1->heap[j+1], gz1->heap[j])) j++;
11191
11192     if (smaller(tree, v, gz1->heap[j])) break;
11193
11194     gz1->heap[k] = gz1->heap[j];  k = j;
11195
11196     j <<= 1;
11197    }
11198
11199  gz1->heap[k] = v;
11200 }
11201
11202 #define GZS_ZIP1      1
11203 #define GZS_ZIP2      2
11204 #define GZS_DEFLATE1  3
11205 #define GZS_DEFLATE2  4
11206
11207 int gzs_fsp     ( PGZ1 gz1 ); 
11208 int gzs_zip1    ( PGZ1 gz1 ); 
11209 int gzs_zip2    ( PGZ1 gz1 ); 
11210 int gzs_deflate1( PGZ1 gz1 ); 
11211 int gzs_deflate2( PGZ1 gz1 ); 
11212
11213 int gzp_main( request_rec *r, GZP_CONTROL *gzp )
11214 {
11215  char cn[]="gzp_main()";
11216
11217  PGZ1 gz1 = 0;
11218  int  rc  = 0;
11219  int  final_exit_code = 0;
11220  int  ofile_flags = O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
11221
11222  gzp->result_code = 0; 
11223  gzp->bytes_out   = 0; 
11224
11225  gz1 = (PGZ1) gz1_init();
11226
11227  if ( gz1 == 0 )
11228    {
11229     return 0;
11230    }
11231
11232  gz1->decompress      = gzp->decompress;
11233
11234  mod_gzip_strcpy( gz1->ifname, gzp->input_filename  );
11235  mod_gzip_strcpy( gz1->ofname, gzp->output_filename );
11236
11237  gz1->input_ismem     = gzp->input_ismem;
11238  gz1->input_ptr       = gzp->input_ismem_ibuf + gzp->input_offset;
11239  gz1->input_bytesleft = gzp->input_ismem_ibuflen;
11240
11241  gz1->output_ismem    = gzp->output_ismem;
11242  gz1->output_ptr      = gzp->output_ismem_obuf;
11243  gz1->output_maxlen   = gzp->output_ismem_obuflen;
11244
11245  if ( gz1->no_time < 0 ) gz1->no_time = gz1->decompress;
11246  if ( gz1->no_name < 0 ) gz1->no_name = gz1->decompress;
11247
11248  work = zip; 
11249
11250  if ( !gz1->input_ismem )
11251    {
11252     errno = 0;
11253
11254     rc = stat( gz1->ifname, &gz1->istat );
11255
11256     if ( rc != 0 ) 
11257       {
11258        ap_log_error( "",0,APLOG_NOERRNO|APLOG_DEBUG, r->server,
11259        "%s: stat(gz1->ifname=%s) FAILED", cn, gz1->ifname );
11260
11261        gz1_cleanup( gz1 );
11262
11263        return 0; 
11264       }
11265
11266     gz1->ifile_size = ( gz1->istat.st_size - gzp->input_offset );
11267
11268     if ( gz1->ifile_size < 0 ) gz1->ifile_size = 0;
11269
11270     gz1->ifd =
11271     OPEN(
11272     gz1->ifname,
11273     gz1->ascii && !gz1->decompress ? O_RDONLY : O_RDONLY | O_BINARY,
11274     RW_USER
11275     );
11276
11277     if ( gz1->ifd == -1 )
11278       {
11279        ap_log_error( "",0,APLOG_NOERRNO|APLOG_DEBUG, r->server,
11280        "%s: OPEN(gz1->ifname=%s) FAILED", cn, gz1->ifname );
11281
11282        gz1_cleanup( gz1 );
11283
11284        return 0; 
11285       }
11286
11287     if ( gzp->input_offset > 0 )
11288       {
11289        SEEKFORWARD( gz1->ifd, gzp->input_offset );
11290       }
11291    }
11292
11293  if ( !gz1->output_ismem ) 
11294    {
11295     if ( gz1->ascii && gz1->decompress )
11296       {
11297        ofile_flags &= ~O_BINARY; 
11298       }
11299
11300     gz1->ofd = OPEN( gz1->ofname, ofile_flags, RW_USER );
11301
11302     if ( gz1->ofd == -1 )
11303       {
11304        ap_log_error( "",0,APLOG_NOERRNO|APLOG_DEBUG, r->server,
11305        "%s: OPEN(gz1->ofname=%s) FAILED", cn, gz1->ofname );
11306
11307        if ( gz1->ifd )
11308          {
11309           close( gz1->ifd ); 
11310           gz1->ifd = 0;      
11311          }
11312
11313        gz1_cleanup( gz1 ); 
11314
11315        return 0; 
11316       }
11317    }
11318
11319  gz1->outcnt    = 0;
11320  gz1->insize    = 0;
11321  gz1->inptr     = 0;
11322  gz1->bytes_in  = 0L;
11323  gz1->bytes_out = 0L; 
11324  gz1->part_nb   = 0;
11325
11326  if ( gz1->decompress )
11327    {
11328     gz1->method = get_header( gz1, gz1->ifd );
11329
11330     if ( gz1->method < 0 )
11331       {
11332        if ( gz1->ifd ) 
11333          {
11334           close( gz1->ifd ); 
11335           gz1->ifd = 0;      
11336          }
11337
11338        if ( gz1->ofd ) 
11339          {
11340           close( gz1->ofd ); 
11341           gz1->ofd = 0;      
11342          }
11343
11344        return 0; 
11345       }
11346    }
11347
11348  gz1->save_orig_name = 0;
11349
11350  gz1->state = GZS_ZIP1;
11351
11352  for (;;) 
11353     {
11354      gzs_fsp( gz1 ); 
11355
11356      if ( gz1->done == 1 ) break; 
11357     }
11358
11359  if ( gz1->ifd ) 
11360    {
11361     close( gz1->ifd ); 
11362     gz1->ifd = 0;      
11363    }
11364
11365  if ( gz1->ofd ) 
11366    {
11367     close( gz1->ofd ); 
11368     gz1->ofd = 0;      
11369    }
11370
11371  gzp->result_code = gz1->exit_code;
11372  gzp->bytes_out   = gz1->bytes_out;
11373
11374  final_exit_code = (int) gz1->exit_code;
11375
11376  gz1_cleanup( gz1 );  
11377
11378  return final_exit_code; 
11379 }
11380
11381 int gzs_fsp( PGZ1 gz1 )
11382 {
11383  int rc=0; 
11384
11385  switch( gz1->state )
11386    {
11387     case GZS_ZIP1:
11388
11389          rc = gzs_zip1( gz1 );
11390
11391          break;
11392
11393     case GZS_ZIP2:
11394
11395          rc = gzs_zip2( gz1 );
11396
11397          break;
11398
11399     case GZS_DEFLATE1:
11400
11401          rc = gzs_deflate1( gz1 );
11402
11403          break;
11404
11405     case GZS_DEFLATE2:
11406
11407          rc = gzs_deflate2( gz1 );
11408
11409          break;
11410
11411     default: 
11412
11413          gz1->done = 1;
11414
11415          break;
11416    }
11417
11418  return( rc );
11419 }
11420
11421 int gzs_zip1( PGZ1 gz1 )
11422 {
11423  uch  flags = 0;         
11424
11425  #ifdef FUTURE_USE
11426  ush  attr          = 0;
11427  ush  deflate_flags = 0;
11428  #endif
11429
11430  gz1->outcnt = 0;
11431
11432  gz1->method = DEFLATED;
11433
11434  put_byte(GZIP_MAGIC[0]); 
11435  put_byte(GZIP_MAGIC[1]);
11436  put_byte(DEFLATED);      
11437
11438  if ( gz1->save_orig_name )
11439    {
11440         flags |= ORIG_NAME;
11441    }
11442
11443  put_byte(flags);           
11444  put_long(gz1->time_stamp); 
11445
11446  gz1->crc = -1; 
11447
11448  updcrc( gz1, NULL, 0 ); 
11449
11450  gz1->state = GZS_ZIP2;
11451
11452  return 0;
11453 }
11454
11455 int gzs_zip2( PGZ1 gz1 )
11456 {
11457  #ifdef FUTURE_USE
11458  uch  flags = 0;
11459  #endif
11460
11461  ush  attr          = 0;
11462  ush  deflate_flags = 0; 
11463
11464  bi_init( gz1, gz1->ofd );
11465  ct_init( gz1, &attr, &gz1->method );
11466  lm_init( gz1, gz1->level, &deflate_flags );
11467  put_byte((uch)deflate_flags); 
11468
11469  put_byte(OS_CODE); 
11470
11471  if ( gz1->save_orig_name )
11472    {
11473     char *p = gz1_basename( gz1, gz1->ifname );
11474
11475     do {
11476             put_char(*p);
11477
11478        } while (*p++);
11479    }
11480
11481  gz1->header_bytes = (long)gz1->outcnt;
11482
11483  gz1->state = GZS_DEFLATE1;
11484
11485  return 0;
11486 }
11487
11488 int gzs_deflate1( PGZ1 gz1 )
11489 {
11490  if ( !gz1->deflate1_initialized )
11491    {
11492     gz1->deflate1_match_available = 0;           
11493     gz1->deflate1_match_length    = MIN_MATCH-1; 
11494     gz1->deflate1_initialized     = 1;
11495    }
11496
11497  if ( gz1->compr_level <= 3 )
11498    {
11499     gz1->done = 1; 
11500
11501     return 0;
11502    }
11503
11504  if ( gz1->lookahead == 0 )
11505    {
11506     if ( gz1->deflate1_match_available )
11507       {
11508        ct_tally( gz1, 0, gz1->window[gz1->strstart-1] );
11509       }
11510
11511     gz1->state = GZS_DEFLATE2;
11512
11513     return (int) FLUSH_BLOCK(1); 
11514    }
11515
11516  #ifdef STAY_HERE_FOR_A_CERTAIN_AMOUNT_OF_ITERATIONS
11517  
11518  while( iterations < max_iterations_per_yield )
11519    {
11520  #endif
11521
11522     gz1->ins_h =
11523     (((gz1->ins_h)<<H_SHIFT) ^ (gz1->window[gz1->strstart+MIN_MATCH-1])) & HASH_MASK;
11524
11525     prev[ gz1->strstart & WMASK ] = gz1->deflate1_hash_head = head[ gz1->ins_h ];
11526
11527     head[ gz1->ins_h ] = gz1->strstart;
11528
11529     gz1->prev_length = gz1->deflate1_match_length, gz1->deflate1_prev_match = gz1->match_start;
11530     gz1->deflate1_match_length = MIN_MATCH-1;
11531
11532     if ( gz1->deflate1_hash_head != NIL && gz1->prev_length < gz1->max_lazy_match &&
11533          gz1->strstart - gz1->deflate1_hash_head <= MAX_DIST)
11534       {
11535        gz1->deflate1_match_length = longest_match( gz1, gz1->deflate1_hash_head );
11536
11537        if ( gz1->deflate1_match_length > gz1->lookahead )
11538          {
11539           gz1->deflate1_match_length = gz1->lookahead;
11540          }
11541
11542        if (gz1->deflate1_match_length == MIN_MATCH && gz1->strstart-gz1->match_start > TOO_FAR)
11543          {
11544           gz1->deflate1_match_length--;
11545          }
11546       }
11547
11548     if ( gz1->prev_length >= MIN_MATCH && gz1->deflate1_match_length <= gz1->prev_length )
11549       {
11550        gz1->deflate1_flush =
11551        ct_tally(gz1,gz1->strstart-1-gz1->deflate1_prev_match, gz1->prev_length - MIN_MATCH);
11552
11553        gz1->lookahead   -= ( gz1->prev_length - 1 );
11554        gz1->prev_length -= 2;
11555
11556        do {
11557            gz1->strstart++;
11558
11559            gz1->ins_h =
11560            (((gz1->ins_h)<<H_SHIFT) ^ (gz1->window[ gz1->strstart + MIN_MATCH-1])) & HASH_MASK;
11561
11562            prev[ gz1->strstart & WMASK ] = gz1->deflate1_hash_head = head[gz1->ins_h];
11563
11564            head[ gz1->ins_h ] = gz1->strstart;
11565
11566           } while (--gz1->prev_length != 0);
11567
11568        gz1->deflate1_match_available = 0;
11569        gz1->deflate1_match_length    = MIN_MATCH-1;
11570
11571        gz1->strstart++;
11572
11573        if (gz1->deflate1_flush) FLUSH_BLOCK(0), gz1->block_start = gz1->strstart;
11574       }
11575
11576     else
11577       {
11578        if ( gz1->deflate1_match_available )
11579          {
11580           if ( ct_tally( gz1, 0, gz1->window[gz1->strstart-1] ) )
11581             {
11582              FLUSH_BLOCK(0), gz1->block_start = gz1->strstart;
11583             }
11584
11585           gz1->strstart++;
11586           gz1->lookahead--;
11587          }
11588        else 
11589          {
11590           gz1->deflate1_match_available = 1;
11591           gz1->strstart++;
11592           gz1->lookahead--;
11593          }
11594
11595        while (gz1->lookahead < MIN_LOOKAHEAD && !gz1->eofile )
11596          {
11597           fill_window(gz1);
11598          }
11599       }
11600
11601  return 0;
11602 }
11603
11604 int gzs_deflate2( PGZ1 gz1 )
11605 {
11606  #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
11607  if (gz1->ifile_size != -1L && gz1->isize != (ulg)gz1->ifile_size)
11608    {
11609    }
11610  #endif
11611
11612  put_long( gz1->crc      );
11613  put_long( gz1->bytes_in );
11614
11615  gz1->header_bytes += 2*sizeof(long);
11616
11617  flush_outbuf( gz1 );
11618
11619  gz1->done = 1; 
11620
11621  return OK;
11622 }
11623
11624 /*--------------------------------------------------------------------------*/
11625 /* COMPRESSION_SUPPORT: END                                                 */
11626 /*--------------------------------------------------------------------------*/
11627
This page took 1.187098 seconds and 3 git commands to generate.