/usr/share/perl5/XMLTV/Gunzip.pm is in libxmltv-perl 0.5.70-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | # $Id: Gunzip.pm,v 1.7 2010/04/24 12:28:22 crispygoth Exp $
=pod
=head1 NAME
XMLTV::Gunzip - Wrapper to Compress::Zlib or gzip(1)
=head1 SYNOPSIS
use XMLTV::Gunzip;
my $decompressed = gunzip($gzdata);
my $fh = gunzip_open('file.gz') or die;
while (<$fh>) { print }
Compress::Zlib will be used if installed, otherwise an external gzip
will be spawned. gunzip() returns the decompressed data and throws an
exception if things go wrong; gunzip_open() returns a filehandle, or
undef.
=head1 AUTHOR
Ed Avis, ed@membled.com. Distributed as part of the xmltv package.
=head1 SEE ALSO
L<Compress::Zlib>, L<gzip(1)>, L<XMLTV>.
=cut
use warnings;
use strict;
package XMLTV::Gunzip;
use base 'Exporter';
our @EXPORT; @EXPORT = qw(gunzip gunzip_open);
use File::Temp;
# Implementations of gunzip().
#
sub zlib_gunzip( $ ) {
for (Compress::Zlib::memGunzip(shift)) {
die 'memGunzip() failed' if not defined;
return $_;
}
}
sub external_gunzip( $ ) {
my ($fh, $fname) = File::Temp::tempfile();
print $fh shift or die "cannot write to $fname: $!";
close $fh or die "cannot close $fname: $!";
open(GZIP, "gzip -d <$fname |") or die "cannot run gzip: $!";
local $/ = undef;
my $r = <GZIP>;
close GZIP or die "cannot close pipe from gzip: $!";
unlink $fname or die "cannot unlink $fname: $!";
return $r;
}
my $gunzip_f;
sub gunzip( $ ) { return $gunzip_f->(shift) }
# Implementations of gunzip_open().
#
sub perlio_gunzip_open( $ ) {
my $fname = shift;
# Use PerlIO::gzip.
local *FH;
open FH, '<:gzip', $fname
or die "cannot open $fname via PerlIO::gzip: $!";
return *FH;
}
sub zlib_gunzip_open( $ ) {
my $fname = shift;
# Use the XMLTV::Zlib_handle package defined later in this file.
local *FH;
tie *FH, 'XMLTV::Zlib_handle', $fname, 'r'
or die "cannot open $fname using XMLTV::Zlib_handle: $!";
return *FH;
}
sub external_gunzip_open( $ ) {
my $fname = shift;
local *FH;
if (not open(FH, "gzip -d <$fname |")) {
warn "cannot run gzip: $!";
return undef;
}
return *FH;
}
my $gunzip_open_f;
sub gunzip_open( $ ) { return $gunzip_open_f->(shift) }
# Switch between implementations depending on whether Compress::Zlib
# is available.
#
BEGIN {
eval { require Compress::Zlib }; my $have_zlib = not $@;
eval { require PerlIO::gzip }; my $have_perlio = not $@;
if (not $have_zlib and not $have_perlio) {
$gunzip_f = \&external_gunzip;
$gunzip_open_f = \&external_gunzip_open;
}
elsif (not $have_zlib and $have_perlio) {
# Could gunzip by writing to a file and reading that with
# PerlIO, but won't bother yet.
#
$gunzip_f = \&external_gunzip;
$gunzip_open_f = \&perlio_gunzip_open;
}
elsif ($have_zlib and not $have_perlio) {
$gunzip_f = \&zlib_gunzip;
$gunzip_open_f = \&zlib_gunzip_open;
}
elsif ($have_zlib and $have_perlio) {
$gunzip_f = \&zlib_gunzip;
$gunzip_open_f = \&perlio_gunzip_open;
}
else { die }
}
####
# This is a filehandle wrapper around Compress::Zlib, but supporting
# only read at the moment.
#
package XMLTV::Zlib_handle;
require Tie::Handle; use base 'Tie::Handle';
use Carp;
sub TIEHANDLE {
croak 'usage: package->TIEHANDLE(file, mode)' if @_ != 3;
my ($pkg, $file, $mode) = @_;
croak "only mode 'r' is supported" if $mode ne 'r';
# This object is a reference to a Compress::Zlib handle. I did
# try to inherit directly from Compress::Zlib, but got weird
# errors of '(in cleanup) gzclose is not a valid Zlib macro'.
#
my $fh = Compress::Zlib::gzopen($file, $mode);
if (not $fh) {
warn "could not gzopen $file";
return undef;
}
return bless(\$fh, $pkg);
}
# Assuming that WRITE() is like print(), not like syswrite().
sub WRITE {
my ($self, $scalar, $length, $offset) = @_;
return 1 if not $length;
my $r = $$self->gzwrite(substr($scalar, $offset, $length));
if ($r == 0) {
warn "gzwrite() failed";
return 0;
}
elsif (0 < $r and $r < $length) {
warn "gzwrite() wrote only $r of $length bytes";
return 0;
}
elsif ($r == $length) {
return 1;
}
else { die }
}
# PRINT(), PRINTF() inherited from Tie::Handle
sub READ {
my ($self, $scalar, $length, $offset) = @_;
local $_;
my $n = $$self->gzread($_, $length);
if ($n == -1) {
warn 'gzread() failed';
return undef;
}
elsif ($n == 0) {
# EOF.
return 0;
}
elsif (0 < $n and $n <= $length) {
die if $n != length;
substr($scalar, $offset, $n) = $_;
return $n;
}
else { die }
}
sub READLINE {
my $self = shift;
# When gzreadline() uses $/, this can be removed.
die '$/ not supported' if $/ ne "\n";
local $_;
my $r = $$self->gzreadline($_);
if ($r == -1) {
warn 'gzreadline() failed';
return undef;
}
elsif ($r == 0) {
# EOF.
die if length;
return undef;
}
else {
# Number of bytes read.
die if $r != length;
return $_;
}
}
# GETC inherited from Tie::Handle
# This seems to segfault in my perl installation.
sub CLOSE {
my $self = shift;
gzclose $$self; # no meaningful return value?
return 1;
}
sub OPEN {
# Compress::Zlib doesn't support reopening.
my $self = shift;
die 'not yet implemented';
}
sub BINMODE {}
sub EOF {
my $self = shift;
return $$self->gzeof();
}
sub TELL {
# Could track position manually. But Compress::Zlib should do it.
die 'not implemented';
}
sub SEEK {
# Argh, fairly impossible. Could simulate, but probably better to
# throw.
#
die 'not implemented';
}
sub DESTROY { &CLOSE }
1;
|