package IO::Compress::Lzop ; use strict ; use warnings; require Exporter ; use bytes; use IO::Compress::Base 2.060 ; use IO::Compress::Base::Common 2.060 qw(isaScalar createSelfTiedObject); use IO::Compress::Adapter::LZO 2.060 ; use Compress::LZO qw(crc32 adler32 LZO_VERSION); use IO::Compress::Lzop::Constants 2.060 ; our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $LzopError); $VERSION = '2.060'; $LzopError = ''; @ISA = qw(Exporter IO::Compress::Base); @EXPORT_OK = qw( $LzopError lzop ) ; %EXPORT_TAGS = %IO::Compress::Base::EXPORT_TAGS ; push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; Exporter::export_ok_tags('all'); sub new { my $class = shift ; my $obj = createSelfTiedObject($class, \$LzopError); return $obj->_create(undef, @_); } sub lzop { my $obj = createSelfTiedObject(undef, \$LzopError); return $obj->_def(@_); } #sub ckSum #{ # my $self = shift ; # # return adler32($_[0]) if *$self->{LZOP}{Adler32} ; # return crc32($_[0]) if *$self->{LZOP}{CRRC32} ; # return ''; #} sub mkHeader { my $self = shift ; my $param = shift ; my $filename = ''; my $time = $param->getValue('time') ; my $flags = F_OS_UNIX ; if (! $param->getValue('minimal')) { $flags |= F_ADLER32_D | F_ADLER32_C ; $filename = $param->getValue('name') || ''; } my $mode = 0 ; if ($param->getValue('mode')) { $mode = $param->getValue('mode'); } my $xtr = ''; if ($param->parsed('extra')) { $flags |= F_H_EXTRA_FIELD ; my $extra = $param->getValue('extra') ; $xtr .= pack 'N', length($extra) ; # Extra Length $xtr .= $extra ; # Extra Data $xtr .= pack 'N', adler32($xtr) ; # Extra CRC } my $hdr = '' ; $hdr .= pack 'n', 0x1010 ; # lzop Version $hdr .= pack 'n', 0x1080 ; # LZO library version $hdr .= pack 'n', 0x1010 ; # lzop extract version $hdr .= pack 'C', 1 ; # Method $hdr .= pack 'C', 5 ; # Level $hdr .= pack 'N', $flags ; # Flags $hdr .= pack 'N', $mode ; # Mode $hdr .= pack 'N', $time ; # Time $hdr .= pack 'N', 0 ; # GMDiff # Filename $hdr .= pack 'C', length $filename ; # filename length $hdr .= $filename ; # Header CRC $hdr .= pack 'N', adler32($hdr) ; # Header CRC # Extra $hdr .= $xtr; return SIGNATURE . $hdr; } sub ckParams { my $self = shift ; my $got = shift; if (! $got->parsed('time') ) { # Modification time defaults to now. $got->setValue('time' => time) ; } #*$self->{LZOP}{Adler32} = ($got->getValue('??') ? 0 : 1) ; return 1 ; } sub mkComp { my $self = shift ; my $got = shift ; my ($obj, $errstr, $errno) = IO::Compress::Adapter::LZO::mkCompObject( $got->getValue('blocksize'), $got->getValue('optimize'), $got->getValue('minimal'), ); return $self->saveErrorString(undef, $errstr, $errno) if ! defined $obj; return $obj; } sub mkTrailer { my $self = shift ; return pack "N", 0 ; } sub mkFinalTrailer { return ''; } #sub newHeader #{ # my $self = shift ; # return ''; #} our %PARAMS = ( 'name' => [IO::Compress::Base::Common::Parse_any, undef], 'time' => [IO::Compress::Base::Common::Parse_any, undef], 'mode' => [IO::Compress::Base::Common::Parse_any, 0], 'extra' => [IO::Compress::Base::Common::Parse_any, undef], 'minimal' => [IO::Compress::Base::Common::Parse_boolean, 0], 'blocksize' => [IO::Compress::Base::Common::Parse_unsigned, BLOCK_SIZE], 'optimize' => [IO::Compress::Base::Common::Parse_boolean, 1], # TODO # none # crc32 # adler32 ); sub getExtraParams { return %PARAMS ; } sub getInverseClass { return ('IO::Uncompress::UnLzop'); } sub getFileInfo { my $self = shift ; my $params = shift; my $filename = shift ; return if isaScalar($filename) ; my ($defaultMode, $defaultTime) = (stat($filename))[2, 9] ; $params->setValue('name' => $filename) if ! $params->parsed('name') ; $params->setValue('time' => $defaultTime) if ! $params->parsed('time') ; $params->setValue('mode' => $defaultMode) if ! $params->parsed('mode') ; } 1; __END__ =head1 NAME IO::Compress::Lzop - Write lzop files/buffers =head1 SYNOPSIS use IO::Compress::Lzop qw(lzop $LzopError) ; my $status = lzop $input => $output [,OPTS] or die "lzop failed: $LzopError\n"; my $z = new IO::Compress::Lzop $output [,OPTS] or die "lzop failed: $LzopError\n"; $z->print($string); $z->printf($format, $string); $z->write($string); $z->syswrite($string [, $length, $offset]); $z->flush(); $z->tell(); $z->eof(); $z->seek($position, $whence); $z->binmode(); $z->fileno(); $z->opened(); $z->autoflush(); $z->input_line_number(); $z->newStream( [OPTS] ); $z->close() ; $LzopError ; # IO::File mode print $z $string; printf $z $format, $string; tell $z eof $z seek $z, $position, $whence binmode $z fileno $z close $z ; =head1 DESCRIPTION This module provides a Perl interface that allows writing lzop compressed data to files or buffer. For reading lzop files/buffers, see the companion module L. =head1 Functional Interface A top-level function, C, is provided to carry out "one-shot" compression between buffers and/or files. For finer control over the compression process, see the L section. use IO::Compress::Lzop qw(lzop $LzopError) ; lzop $input_filename_or_reference => $output_filename_or_reference [,OPTS] or die "lzop failed: $LzopError\n"; The functional interface needs Perl5.005 or better. =head2 lzop $input => $output [, OPTS] C expects at least two parameters, C<$input_filename_or_reference> and C<$output_filename_or_reference>. =head3 The C<$input_filename_or_reference> parameter The parameter, C<$input_filename_or_reference>, is used to define the source of the uncompressed data. It can take one of the following forms: =over 5 =item A filename If the <$input_filename_or_reference> parameter is a simple scalar, it is assumed to be a filename. This file will be opened for reading and the input data will be read from it. =item A filehandle If the C<$input_filename_or_reference> parameter is a filehandle, the input data will be read from it. The string '-' can be used as an alias for standard input. =item A scalar reference If C<$input_filename_or_reference> is a scalar reference, the input data will be read from C<$$input_filename_or_reference>. =item An array reference If C<$input_filename_or_reference> is an array reference, each element in the array must be a filename. The input data will be read from each file in turn. The complete array will be walked to ensure that it only contains valid filenames before any data is compressed. =item An Input FileGlob string If C<$input_filename_or_reference> is a string that is delimited by the characters "<" and ">" C will assume that it is an I. The input is the list of files that match the fileglob. See L for more details. =back If the C<$input_filename_or_reference> parameter is any other type, C will be returned. In addition, if C<$input_filename_or_reference> is a simple filename, the default values for the C and C