=================================================================== RCS file: /cvs/cvsweb/cvsweb.cgi,v retrieving revision 3.16 retrieving revision 3.30 diff -u -p -r3.16 -r3.30 --- cvsweb/cvsweb.cgi 2000/09/03 17:33:22 3.16 +++ cvsweb/cvsweb.cgi 2000/09/30 20:10:01 3.30 @@ -9,6 +9,7 @@ # Ken Coar # Dick Balaska # Akinori MUSHA +# Jens-Uwe Mager # # Based on: # * Bill Fenners cvsweb.cgi revision 1.28 available from: @@ -41,8 +42,8 @@ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # -# $zId: cvsweb.cgi,v 1.94 2000/08/24 06:41:22 hnordstrom Exp $ -# $Id: cvsweb.cgi,v 3.16 2000/09/03 17:33:22 knu Exp $ +# $zId: cvsweb.cgi,v 1.103 2000/09/20 17:02:29 jumager Exp $ +# $Id: cvsweb.cgi,v 3.30 2000/09/30 20:10:01 knu Exp $ # ### @@ -53,8 +54,8 @@ use vars qw ( %CVSROOT %CVSROOTdescr %MIRRORS %DEFAULTVALUE %ICONS %MTYPES %alltags @tabcolors %fileinfo %tags @branchnames %nameprinted %symrev %revsym @allrevisions %date %author @revdisplayorder - @revisions %state %difflines %log %branchpoint @revorder $prcgi - @prcategories $prcategories + @revisions %state %difflines %log %branchpoint @revorder + $prcgi @prcategories $prcategories $mancgi $checkoutMagic $doCheckout $scriptname $scriptwhere $where $pathinfo $Browser $nofilelinks $maycompress @stickyvars %funcline_regexp $is_mod_perl @@ -63,8 +64,9 @@ use vars qw ( $bylog $byfile $hr_default $logsort $cvstree $cvsroot $mimetype $defaultTextPlain $defaultViewable $allow_compress $GZIPBIN $backicon $diricon $fileicon $fullname $newname - $cvstreedefault $body_tag $logo $defaulttitle $address - $backcolor $long_intro $short_instruction $shortLogLen + $cvstreedefault $body_tag $body_tag_for_src + $logo $defaulttitle $address + $long_intro $short_instruction $shortLogLen $show_author $dirtable $tablepadding $columnHeaderColorDefault $columnHeaderColorSorted $hr_breakable $showfunc $hr_ignwhite $hr_ignkeysubst $diffcolorHeading $diffcolorEmpty $diffcolorRemove @@ -76,19 +78,21 @@ use vars qw ( $navigationHeaderColor $tableBorderColor $markupLogColor $tabstop $state $annTable $sel $curbranch @HideModules $module $use_descriptions %descriptions @mytz $dwhere $moddate - $use_moddate + $use_moddate $has_zlib $gzip_open ); sub printDiffSelect($); sub findLastModifiedSubdirs(@); sub htmlify($;$); -sub spacedHtmlText($); +sub spacedHtmlText($;$); sub link($$); sub revcmp($$); sub fatal($$); sub redirect($); sub safeglob($); sub getMimeTypeFromSuffix($); +sub head($;$); +sub scan_directives(@); sub doAnnotate($$); sub doCheckout($$); sub cvswebMarkup($$$); @@ -121,14 +125,13 @@ sub forbidden_module($); use Cwd; # == EDIT this == -# User configuration is stored in -$config = undef; - -for ($ENV{CVSWEB_CONFIG}, -# '/home/knu/etc/cvsweb.conf', +# Locations to search for user configuration, in order: +for ( + $ENV{CVSWEB_CONFIG}, '/usr/local/etc/cvsweb.conf', - getcwd . '/cvsweb.conf') { - $config = $_ if defined($_) && -r $_; + getcwd() . '/cvsweb.conf' + ) { + $config = $_ if defined($_) && -r $_; } # == Configuration defaults == @@ -142,8 +145,9 @@ $allow_version_select = 1; # These are defined to allow checking with perl -cw %CVSROOT = %MIRRORS = %DEFAULTVALUE = %ICONS = %MTYPES = %tags = %alltags = @tabcolors = (); -$cvstreedefault = $body_tag = $logo = $defaulttitle = $address = -$backcolor = $long_intro = $short_instruction = $shortLogLen = +$cvstreedefault = $body_tag = $body_tag_for_src = +$logo = $defaulttitle = $address = +$long_intro = $short_instruction = $shortLogLen = $show_author = $dirtable = $tablepadding = $columnHeaderColorDefault = $columnHeaderColorSorted = $hr_breakable = $showfunc = $hr_ignwhite = $hr_ignkeysubst = $diffcolorHeading = $diffcolorEmpty = $diffcolorRemove = @@ -153,13 +157,20 @@ $allow_markup = $use_java_script = $open_extern_window $extern_window_width = $extern_window_height = $edit_option_form = $checkout_magic = $show_subdir_lastmod = $show_log_in_markup = $v = $navigationHeaderColor = $tableBorderColor = $markupLogColor = -$tabstop = $use_moddate = $moddate = undef; +$tabstop = $use_moddate = $moddate = $gzip_open = undef; ##### End of configuration variables ##### use Time::Local; use IPC::Open2; +# Check if the zlib C library interface is installed, and if yes +# we can avoid using the extra gzip process. +eval { + require Compress::Zlib; +}; +$has_zlib = !$@; + $verbose = $v; $checkoutMagic = "~checkout~"; $pathinfo = defined($ENV{PATH_INFO}) ? $ENV{PATH_INFO} : ''; @@ -197,13 +208,13 @@ $nofilelinks = $is_textbased; # braindamaged MS-Internet Exploders claim that they # accept gzip .. but don't in fact and # display garbage then :-/ -# Turn off gzip if running under mod_perl. piping does -# not work as expected inside the server. One can probably -# achieve the same result using Apache::GZIPFilter. -$maycompress = (($ENV{HTTP_ACCEPT_ENCODING} =~ m`gzip` +# Turn off gzip if running under mod_perl and no zlib is available, +# piping does not work as expected inside the server. +$maycompress = (((defined($ENV{HTTP_ACCEPT_ENCODING}) + && $ENV{HTTP_ACCEPT_ENCODING} =~ m`gzip`) || $is_mozilla3) && !$is_msie - && !$is_mod_perl); + && !($is_mod_perl && !$has_zlib)); # put here the variables we need in order # to hold our state - they will be added (with @@ -212,9 +223,11 @@ $maycompress = (($ENV{HTTP_ACCEPT_ENCODING} =~ m`gzip` @stickyvars = qw(cvsroot hideattic sortby logsort f only_with_tag); if (-f $config) { - do $config; -} -else { + do $config + || &fatal("500 Internal Error", + sprintf('Error in loading configuration file: %s

%s
', + $config, &htmlify($@))); +} else { &fatal("500 Internal Error", 'Configuration not found. Set the variable $config ' . 'in cvsweb.cgi, or the environment variable ' @@ -343,10 +356,18 @@ foreach my $k (keys %ICONS) { } } +my $config_cvstree = "$config-$cvstree"; + # Do some special configuration for cvstrees -do "$config-$cvstree" if (-f "$config-$cvstree"); +if (-f $config_cvstree) { + do $config_cvstree + || &fatal("500 Internal Error", + sprintf('Error in loading configuration file: %s

%s
', + $config_cvstree, &htmlify($@))); +} $prcategories = '(?:' . join('|', @prcategories) . ')'; +$prcgi .= '%s' if defined($prcgi) && $prcgi !~ /%s/; $fullname = $cvsroot . '/' . $where; $mimetype = &getMimeTypeFromSuffix ($fullname); @@ -358,7 +379,7 @@ $defaultViewable = $allow_markup && viewable($mimetype # ge get an Internal Server Error if we try to pipe the # output through the nonexistent gzip .. # any more elegant ways to prevent this are welcome! -if ($allow_compress && $maycompress) { +if ($allow_compress && $maycompress && !$has_zlib) { foreach (split(/:/, $ENV{PATH})) { if (-x "$_/gzip") { $GZIPBIN = "$_/gzip"; @@ -709,7 +730,8 @@ elsif (-d $fullname) { foreach my $var (@stickyvars) { print "\n" if (defined($input{$var}) - && $input{$var} ne $DEFAULTVALUE{$var} + && (!defined($DEFAULTVALUE{$var}) + || $input{$var} ne $DEFAULTVALUE{$var}) && $input{$var} ne "" && $var ne "only_with_tag"); } @@ -771,15 +793,18 @@ elsif (-d $fullname) { elsif (-f $fullname . ',v') { if (defined($input{'rev'}) || $doCheckout) { &doCheckout($fullname, $input{'rev'}); + gzipclose(); exit; } if (defined($input{'annotate'}) && $allow_annotate) { &doAnnotate($input{'annotate'}); + gzipclose(); exit; } if (defined($input{'r1'}) && defined($input{'r2'})) { &doDiff($fullname, $input{'r1'}, $input{'tr1'}, $input{'r2'}, $input{'tr2'}, $input{'f'}); + gzipclose(); exit; } print("going to dolog($fullname)\n") if ($verbose); @@ -802,6 +827,7 @@ elsif (-d $fullname) { # e.g. foo.c &doDiff($fullname, $input{'r1'}, $input{'tr1'}, $input{'r2'}, $input{'tr2'}, $input{'f'}); + gzipclose(); exit; } elsif (($newname = $fullname) =~ s|/([^/]+)$|/Attic/$1| && @@ -835,6 +861,8 @@ elsif (-d $fullname) { } &fatal("404 Not Found","$where: no such file or directory"); } + +gzipclose(); ## End MAIN sub printDiffSelect($) { @@ -886,7 +914,7 @@ sub findLastModifiedSubdirs(@) { } sub htmlify($;$) { - my($string, $pr) = @_; + my($string, $extra) = @_; # Special Characters; RFC 1866 $string =~ s/&/&/g; @@ -895,48 +923,52 @@ sub htmlify($;$) { $string =~ s/>/>/g; # get URL's as link .. - $string =~ s`(http|ftp|https)(://[-a-zA-Z0-9%.~:_/]+)([?&]([-a-zA-Z0-9%.~:_]+)=([-a-zA-Z0-9%.~:_])+)*`$1$2$3`g; + $string =~ s`(http|ftp|https)(://[-a-zA-Z0-9%.~:_/]+)([?&]([-a-zA-Z0-9%.~:_]+)=([-a-zA-Z0-9%.~:_])+)*`$1$2$3`g; # ` # get e-mails as link - $string =~ s`([-a-zA-Z0-9_.]+@([-a-zA-Z0-9]+\.)+[A-Za-z]{2,4})`$1`g; + $string =~ s`([-a-zA-Z0-9_.]+@([-a-zA-Z0-9]+\.)+[A-Za-z]{2,4})`$1`g; # ` - # get #PR as link .. - if ($pr && defined($prcgi)) { - 1 while $string =~ s`\b(pr[:#]?\s*(?:#?\d+[,\s]\s*)*#?)(\d+)\b`$1$2`i; - $string =~ s`\b${prcategories}/(\d+)\b`$&`igo; + if ($extra) { + # get PR #'s as link .. + if (defined($prcgi)) { + 1 while $string =~ s`\b(pr[:#]?\s*(?:#?\d+[,\s]\s*)*#?)(\d+)\b`$1 . &link($2, sprintf($prcgi, $2))`ie; # ` + $string =~ s`\b${prcategories}/(\d+)\b`&link($&, sprintf($prcgi, $1))`igeo; # ` + } + + # get manpage specs as link .. + if (defined($mancgi)) { + $string =~ s`\b([a-zA-Z]\w+)\(([0-9n])\)\B`&link($&, sprintf($mancgi, $2, $1))`ge; # ` + } } return $string; } -sub spacedHtmlText($) { +sub spacedHtmlText($;$) { local $_ = $_[0]; + my $ts = $_[1] || $tabstop; - # Cut trailing spaces - s/\s+\n$//; + # Cut trailing spaces and tabs + s/[ \t]+$//; - # Expand tabs - s/\t+/' ' x (length($&) * $tabstop - length($`) % $tabstop)/e - if (defined($tabstop)); + if (defined($ts)) { + # Expand tabs + 1 while s/\t+/' ' x (length($&) * $ts - length($`) % $ts)/e + } # replace and (\001 is to protect us from htmlify) # gzip can make excellent use of this repeating pattern :-) - s/\001/\001%/g; #protect our & substitute if ($hr_breakable) { # make every other space 'breakable' - s/ / \001nbsp; \001nbsp; \001nbsp; \001nbsp;/g; # s/ / \001nbsp;/g; # 2 * # leave single space as it is - } - else { - s/ /\001nbsp;\001nbsp;\001nbsp;\001nbsp;\001nbsp;\001nbsp;\001nbsp;\001nbsp;/g; + } else { s/ /\001nbsp;/g; } $_ = htmlify($_); # unescape - s/\001([^%])/&$1/g; - s/\001%/\001/g; + y/\001/&/; return $_; } @@ -1056,6 +1088,40 @@ sub getMimeTypeFromSuffix($) { } ############################### +# read first lines like head(1) +############################### +sub head($;$) { + my $fh = $_[0]; + my $linecount = $_[1] || 10; + + my @buf; + + if ($linecount > 0) { + my $i; + for ($i = 0; !eof($fh) && $i < $linecount; $i++) { + push @buf, scalar <$fh>; + } + } else { + @buf = <$fh>; + } + + @buf; +} + +############################### +# scan vim and Emacs directives +############################### +sub scan_directives(@) { + my $ts = undef; + + for (@_) { + $ts = $1 if /\b(?:ts|tabstop|tab-width)[:=]\s*([1-9]\d*)\b/; + } + + ('tabstop' => $ts); +} + +############################### # show Annotation ############################### sub doAnnotate($$) { @@ -1075,11 +1141,6 @@ sub doAnnotate($$) { ($pathname = $where) =~ s/(Attic\/)?[^\/]*$//; ($filename = $where) =~ s/^.*\///; - http_header(); - - navigateHeader($scriptwhere,$pathname,$filename,$rev, "annotate"); - print "

Annotation of $pathname$filename, Revision $rev

\n"; - # this seems to be necessary $| = 1; $| = 0; # Flush @@ -1089,7 +1150,7 @@ sub doAnnotate($$) { # the public domain. # we could abandon the use of rlog, rcsdiff and co using # the cvsserver in a similiar way one day (..after rewrite) - $pid = open2($reader, $writer, "cvs server") || fatal ("500 Internal Error", + $pid = open2($reader, $writer, "cvs -Rl server") || fatal ("500 Internal Error", "Fatal Error - unable to open cvs for annotation"); # OK, first send the request to the server. A simplified example is: @@ -1141,6 +1202,11 @@ sub doAnnotate($$) { # were nicer about buffering, then we could just leave it open, I think. close ($writer) || die "cannot close: $!"; + http_header(); + + navigateHeader($scriptwhere,$pathname,$filename,$rev, "annotate"); + print "

Annotation of $pathname$filename, Revision $rev

\n"; + # Ready to get the responses from the server. # For example: # E Annotations for foo/xx @@ -1156,7 +1222,15 @@ sub doAnnotate($$) { else { print "
";
     }
-    while (<$reader>) {
+
+    # prefetch several lines
+    my @buf = head($reader);
+
+    my %d = scan_directives(@buf);
+
+    while (@buf || !eof($reader)) {
+	$_ = @buf ? shift @buf : <$reader>;
+
 	my @words = split;
 	# Adding one is for the (single) space which follows $words[0].
 	my $rest = substr ($_, length ($words[0]) + 1);
@@ -1165,32 +1239,41 @@ sub doAnnotate($$) {
 	}
 	elsif ($words[0] eq "M") {
 	    $lineNr++;
-	    my $lrev = substr ($_, 2, 13);
-	    my $lusr = substr ($_, 16,  9);
-	    my $line = substr ($_, 36);
+	    (my $lrev = substr($_, 2, 13)) =~ y/ //d;
+	    (my $lusr = substr($_, 16,  9)) =~ y/ //d;
+	    my $line = substr($_, 36);
+	    my $isCurrentRev = ($rev eq $lrev);
 	    # we should parse the date here ..
 	    if ($lrev eq $oldLrev) {
-		$revprint = "             ";
+		$revprint = sprintf('%-8s', '');
 	    }
 	    else {
-		$revprint = $lrev; $oldLusr = "";
+		$revprint = sprintf('%-8s', $lrev);
+		$revprint =~ s`\S+`$&`;	# `		
+		$oldLusr = '';
 	    }
 	    if ($lusr eq $oldLusr) {
-		$usrprint = "         ";
+		$usrprint = '';
 	    }
 	    else {
 		$usrprint = $lusr;
 	    }
 	    $oldLrev = $lrev;
 	    $oldLusr = $lusr;
-	    # is there a less timeconsuming way to strip spaces ?
-	    ($lrev = $lrev) =~ s/\s+//g;
-	    my $isCurrentRev = ($rev eq $lrev);
 
-	    print "" if ($isCurrentRev);
-	    printf ("%8s%s%8s %4d:", $revprint, ($isCurrentRev ? "|" : " "), $usrprint, $lineNr);
-	    print spacedHtmlText($line);
-	    print "" if ($isCurrentRev);
+	    # Set bold for text-based browsers only - graphical
+	    # browsers show bold fonts a bit wider than regular fonts,
+	    # so it looks irregular.
+	    print "" if ($isCurrentRev && $is_textbased);
+
+	    printf ("%s%s %-8s %4d:",
+		    $revprint,
+		    $isCurrentRev ? '!' : ' ',
+		    $usrprint,
+		    $lineNr);
+	    print spacedHtmlText($line, $d{'tabstop'});
+
+	    print "" if ($isCurrentRev && $is_textbased);
 	}
 	elsif ($words[0] eq "ok") {
 	    # We could complain about any text received after this, like the
@@ -1332,16 +1415,26 @@ sub cvswebMarkup($$$) {
 	    $input{only_with_tag};
     }
     print "";
-    my @content = <$filehandle>;
     my $url = download_url($fileurl, $revision, $mimetype);
     print "
"; if ($mimetype =~ /^image/) { print "
"; } + elsif ($mimetype =~ m%^application/pdf%) { + print "
"; + } else { print "
";
-	foreach (@content) {
-	    print spacedHtmlText($_);
+
+	# prefetch several lines
+	my @buf = head($filehandle);
+
+	my %d = scan_directives(@buf);
+
+	while (@buf || !eof($filehandle)) {
+	    $_ = @buf ? shift @buf : <$filehandle>;
+
+	    print spacedHtmlText($_, $d{'tabstop'});
 	}
 	print "
"; } @@ -1350,7 +1443,7 @@ sub cvswebMarkup($$$) { sub viewable($) { my ($mimetype) = @_; - $mimetype =~ m%^(text|image)/%; + $mimetype =~ m%^((text|image)/|application/pdf)% ; } ############################### @@ -1448,6 +1541,7 @@ sub doDiff($$$$$$) { if ($human_readable) { http_header(); &human_readable_diff($fh, $rev2); + gzipclose(); exit; } else { @@ -2117,40 +2211,42 @@ EOF foreach (@stickyvars) { print "\n" if (defined($input{$_}) - && ($input{$_} ne $DEFAULTVALUE{$_} && $input{$_} ne "")); + && ((!defined($DEFAULTVALUE{$_}) + || $input{$_} ne $DEFAULTVALUE{$_}) + && $input{$_} ne "")); } - print "Diffs between \n"; + print "\n"; + print ""; + print "\n"; + print ""; + print "\n"; print "\n"; + print "
Diffs between \n"; print "\n"; $diffrev = $revdisplayorder[$#revdisplayorder]; $diffrev = $input{"r1"} if (defined($input{"r1"})); - print "\n"; - print " and \n"; + print "
and \n"; print "\n"; $diffrev = $revdisplayorder[0]; $diffrev = $input{"r2"} if (defined($input{"r2"})); - print "\n"; - print "
Type of Diff should be a "; - printDiffSelect(0); - print "\n"; + print "
\n"; print "
\n"; + print ""; + print "\n"; + print ""; + print "\n"; if (@branchnames) { + print ""; + print "\n"; } - print "\n"; - print "\n"; foreach (@stickyvars) { + next if ($_ eq "f"); next if ($_ eq "only_with_tag"); next if ($_ eq "logsort"); print "\n" - if (defined($input{$_}) && $input{$_} ne $DEFAULTVALUE{$_} + if (defined($input{$_}) + && (!defined($DEFAULTVALUE{$_}) + || $input{$_} ne $DEFAULTVALUE{$_}) && $input{$_} ne ""); } - print "Sort log by: \n"; - print ""; + print ""; + print ""; print "\n"; + print "
Preferred Diff type:"; + printDiffSelect($use_java_script); + print "
View only Branch:"; print "\n"; - print "\n"; - foreach (@stickyvars) { - next if ($_ eq "only_with_tag"); - next if ($_ eq "logsort"); - print "\n" - if (defined($input{$_}) && $input{$_} ne $DEFAULTVALUE{$_} - && $input{$_} ne ""); - } - print "View only Branch: \n"; print "\n"; - print "\n"; - print "\n"; + print "
"; + print "\n"; + print "Sort log by:\n"; - print "\n"; + print "
"; print &html_footer; print "\n"; } @@ -2226,7 +2324,7 @@ sub flush_diff_rows($$$$) { # human_readable_diff(String revision_to_return_to); ## sub human_readable_diff($){ - my ($i,$difftxt, $where_nd, $filename, $pathname, $scriptwhere_nd); + my ($difftxt, $where_nd, $filename, $pathname, $scriptwhere_nd); my ($fh, $rev) = @_; my ($date1, $date2, $r1d, $r2d, $r1r, $r2r, $rev1, $rev2, $sym1, $sym2); my (@rightCol, @leftCol); @@ -2280,9 +2378,15 @@ sub human_readable_diff($){ # cascading style sheets because we've to set the # font and color for each row. anyone ...? #### - while (<$fh>) { - $difftxt = $_; + # prefetch several lines + my @buf = head($fh); + + my %d = scan_directives(@buf); + + while (@buf || !eof($fh)) { + $difftxt = @buf ? shift @buf : <$fh>; + if ($difftxt =~ /^@@/) { ($oldline,$newline,$funname) = $difftxt =~ /@@ \-([0-9]+).*\+([0-9]+).*@@(.*)/; print ""; @@ -2298,7 +2402,7 @@ sub human_readable_diff($){ } else { ($diffcode,$rest) = $difftxt =~ /^([-+ ])(.*)/; - $_ = spacedHtmlText ($rest); + $_ = spacedHtmlText($rest, $d{'tabstop'}); # Add fontface, size $_ = "$fs $_$fe"; @@ -2382,9 +2486,9 @@ sub navigateHeader($$$$$) { $swhere = urlencode($filename) if ($swhere eq ""); print "<\!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"; print "\n\n"; - print ''; + print ''; print "\n$path$filename - $title - $rev\n"; - print "\n"; + print "$body_tag_for_src\n"; print ""; print "
"; print "$backicon"; @@ -2683,7 +2787,7 @@ sub http_header(;$) { my $content_type = shift || "text/html"; if (defined($moddate)) { if ($is_mod_perl) { - Apache->request->header_out(Last_modified => scalar gmtime($moddate) . " GMT"); + Apache->request->header_out("Last-Modified" => scalar gmtime($moddate) . " GMT"); } else { print "Last-Modified: " . scalar gmtime($moddate) . " GMT\r\n"; @@ -2696,8 +2800,7 @@ sub http_header(;$) { print "Content-type: $content_type\r\n"; } if ($allow_compress && $maycompress) { - my $fh = do {local(*FH);}; - if (defined($GZIPBIN) && open($fh, "|$GZIPBIN -1 -c")) { + if ($has_zlib || (defined($GZIPBIN) && open(GZIP, "|$GZIPBIN -1 -c"))) { if ($is_mod_perl) { Apache->request->content_encoding("x-gzip"); Apache->request->header_out(Vary => "Accept-Encoding"); @@ -2709,7 +2812,11 @@ sub http_header(;$) { print "\r\n"; # Close headers } $| = 1; $| = 0; # Flush header output - select ($fh); + if ($has_zlib) { + tie *GZIP, __PACKAGE__, \*STDOUT; + } + select(GZIP); + $gzip_open = 1; # print "" if ($content_type eq "text/html"); } else { @@ -2734,7 +2841,7 @@ sub http_header(;$) { sub html_header($) { my ($title) = @_; - my $version = '$zRevision: 1.94 $ $Revision: 3.16 $'; #' + my $version = '$zRevision: 1.103 $ $Revision: 3.30 $'; #' http_header(); print < Compress::Zlib::Z_BEST_COMPRESSION(), + -WindowBits => -Compress::Zlib::MAX_WBITS()) or return undef; + my ($o) = { + handle => $out, + dh => $d, + crc => 0, + len => 0, + }; + my ($header) = pack("c10", MAGIC1, MAGIC2, Compress::Zlib::Z_DEFLATED(), 0,0,0,0,0,0, OSCODE); + print {$o->{handle}} $header; + return bless($o, $class); +} + +sub PRINT { + my ($o) = shift; + my ($buf) = join(defined $, ? $, : "",@_); + my ($len) = length($buf); + my ($compressed, $status) = $o->{dh}->deflate($buf); + print {$o->{handle}} $compressed if defined($compressed); + $o->{crc} = Compress::Zlib::crc32($buf, $o->{crc}); + $o->{len} += $len; + return $len; +} + +sub PRINTF { + my ($o) = shift; + my ($fmt) = shift; + my ($buf) = sprintf($fmt, @_); + my ($len) = length($buf); + my ($compressed, $status) = $o->{dh}->deflate($buf); + print {$o->{handle}} $compressed if defined($compressed); + $o->{crc} = Compress::Zlib::crc32($buf, $o->{crc}); + $o->{len} += $len; + return $len; +} + +sub WRITE { + my ($o, $buf, $len, $off) = @_; + my ($compressed, $status) = $o->{dh}->deflate(substr($buf, 0, $len)); + print {$o->{handle}} $compressed if defined($compressed); + $o->{crc} = Compress::Zlib::crc32(substr($buf, 0, $len), $o->{crc}); + $o->{len} += $len; + return $len; +} + +sub CLOSE { + my ($o) = @_; + return if !defined( $o->{dh}); + my ($buf) = $o->{dh}->flush(); + $buf .= pack("V V", $o->{crc}, $o->{len}); + print {$o->{handle}} $buf; + undef $o->{dh}; +} + +sub DESTROY { + my ($o) = @_; + CLOSE($o); }