IO::Compress::* 2.000_13
[p5sagit/p5-mst-13.2.git] / ext / Compress / IO / Base / lib / IO / Compress / Base / Common.pm
CommitLineData
25f0751f 1package IO::Compress::Base::Common;
2
3use strict ;
4use warnings;
5use bytes;
6
7use Carp;
8use Scalar::Util qw(blessed readonly);
9use File::GlobMapper;
10
11require Exporter;
12our ($VERSION, @ISA, @EXPORT, %EXPORT_TAGS);
13@ISA = qw(Exporter);
e7d45986 14$VERSION = '2.000_13';
25f0751f 15
16@EXPORT = qw( isaFilehandle isaFilename whatIsInput whatIsOutput
17 isaFileGlobString cleanFileGlobString oneTarget
18 setBinModeInput setBinModeOutput
19 ckInOutParams
20 createSelfTiedObject
21
22 WANT_CODE
23 WANT_EXT
24 WANT_UNDEF
25 WANT_HASH
26
27 STATUS_OK
28 STATUS_ENDSTREAM
29 STATUS_EOF
30 STATUS_ERROR
31 );
32
33%EXPORT_TAGS = ( Status => [qw( STATUS_OK
34 STATUS_ENDSTREAM
35 STATUS_EOF
36 STATUS_ERROR
37 )]);
38
39
40use constant STATUS_OK => 0;
41use constant STATUS_ENDSTREAM => 1;
42use constant STATUS_EOF => 2;
43use constant STATUS_ERROR => -1;
44#use constant STATUS_OK => 0;
45#use constant STATUS_ENDSTREAM => 1;
46#use constant STATUS_ERROR => 2;
47#use constant STATUS_EOF => 3;
48
49our ($needBinmode);
50$needBinmode = ($^O eq 'MSWin32' ||
51 ($] >= 5.006 && eval ' ${^UNICODE} || ${^UTF8LOCALE} '))
52 ? 1 : 1 ;
53
54sub setBinModeInput($)
55{
56 my $handle = shift ;
57
58 binmode $handle
59 if $needBinmode;
60}
61
62sub setBinModeOutput($)
63{
64 my $handle = shift ;
65
66 binmode $handle
67 if $needBinmode;
68}
69
70sub isaFilehandle($)
71{
72 use utf8; # Pragma needed to keep Perl 5.6.0 happy
73 return (defined $_[0] and
74 (UNIVERSAL::isa($_[0],'GLOB') or UNIVERSAL::isa(\$_[0],'GLOB'))
75 and defined fileno($_[0]) )
76}
77
78sub isaFilename($)
79{
80 return (defined $_[0] and
81 ! ref $_[0] and
82 UNIVERSAL::isa(\$_[0], 'SCALAR'));
83}
84
85sub isaFileGlobString
86{
87 return defined $_[0] && $_[0] =~ /^<.*>$/;
88}
89
90sub cleanFileGlobString
91{
92 my $string = shift ;
93
94 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
95
96 return $string;
97}
98
99use constant WANT_CODE => 1 ;
100use constant WANT_EXT => 2 ;
101use constant WANT_UNDEF => 4 ;
102#use constant WANT_HASH => 8 ;
103use constant WANT_HASH => 0 ;
104
105sub whatIsInput($;$)
106{
107 my $got = whatIs(@_);
108
109 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
110 {
4ebc85a4 111 #use IO::File;
25f0751f 112 $got = 'handle';
4ebc85a4 113 $_[0] = *STDIN;
114 #$_[0] = new IO::File("<-");
25f0751f 115 }
116
117 return $got;
118}
119
120sub whatIsOutput($;$)
121{
122 my $got = whatIs(@_);
123
124 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
125 {
126 $got = 'handle';
4ebc85a4 127 $_[0] = *STDOUT;
128 #$_[0] = new IO::File(">-");
25f0751f 129 }
130
131 return $got;
132}
133
134sub whatIs ($;$)
135{
136 return 'handle' if isaFilehandle($_[0]);
137
138 my $wantCode = defined $_[1] && $_[1] & WANT_CODE ;
139 my $extended = defined $_[1] && $_[1] & WANT_EXT ;
140 my $undef = defined $_[1] && $_[1] & WANT_UNDEF ;
141 my $hash = defined $_[1] && $_[1] & WANT_HASH ;
142
143 return 'undef' if ! defined $_[0] && $undef ;
144
145 if (ref $_[0]) {
146 return '' if blessed($_[0]); # is an object
147 #return '' if UNIVERSAL::isa($_[0], 'UNIVERSAL'); # is an object
148 return 'buffer' if UNIVERSAL::isa($_[0], 'SCALAR');
149 return 'array' if UNIVERSAL::isa($_[0], 'ARRAY') && $extended ;
150 return 'hash' if UNIVERSAL::isa($_[0], 'HASH') && $hash ;
151 return 'code' if UNIVERSAL::isa($_[0], 'CODE') && $wantCode ;
152 return '';
153 }
154
155 return 'fileglob' if $extended && isaFileGlobString($_[0]);
156 return 'filename';
157}
158
159sub oneTarget
160{
161 return $_[0] =~ /^(code|handle|buffer|filename)$/;
162}
163
164sub Validator::new
165{
166 my $class = shift ;
167
168 my $Class = shift ;
169 my $error_ref = shift ;
170 my $reportClass = shift ;
171
172 my %data = (Class => $Class,
173 Error => $error_ref,
174 reportClass => $reportClass,
175 ) ;
176
177 my $obj = bless \%data, $class ;
178
179 local $Carp::CarpLevel = 1;
180
181 my $inType = $data{inType} = whatIsInput($_[0], WANT_EXT|WANT_HASH);
182 my $outType = $data{outType} = whatIsOutput($_[1], WANT_EXT|WANT_HASH);
183
184 my $oneInput = $data{oneInput} = oneTarget($inType);
185 my $oneOutput = $data{oneOutput} = oneTarget($outType);
186
187 if (! $inType)
188 {
189 $obj->croakError("$reportClass: illegal input parameter") ;
190 #return undef ;
191 }
192
193# if ($inType eq 'hash')
194# {
195# $obj->{Hash} = 1 ;
196# $obj->{oneInput} = 1 ;
197# return $obj->validateHash($_[0]);
198# }
199
200 if (! $outType)
201 {
202 $obj->croakError("$reportClass: illegal output parameter") ;
203 #return undef ;
204 }
205
206
207 if ($inType ne 'fileglob' && $outType eq 'fileglob')
208 {
209 $obj->croakError("Need input fileglob for outout fileglob");
210 }
211
212# if ($inType ne 'fileglob' && $outType eq 'hash' && $inType ne 'filename' )
213# {
214# $obj->croakError("input must ne filename or fileglob when output is a hash");
215# }
216
217 if ($inType eq 'fileglob' && $outType eq 'fileglob')
218 {
219 $data{GlobMap} = 1 ;
220 $data{inType} = $data{outType} = 'filename';
221 my $mapper = new File::GlobMapper($_[0], $_[1]);
222 if ( ! $mapper )
223 {
224 return $obj->saveErrorString($File::GlobMapper::Error) ;
225 }
226 $data{Pairs} = $mapper->getFileMap();
227
228 return $obj;
229 }
230
231 $obj->croakError("$reportClass: input and output $inType are identical")
232 if $inType eq $outType && $_[0] eq $_[1] && $_[0] ne '-' ;
233
234 if ($inType eq 'fileglob') # && $outType ne 'fileglob'
235 {
236 my $glob = cleanFileGlobString($_[0]);
237 my @inputs = glob($glob);
238
239 if (@inputs == 0)
240 {
241 # TODO -- legal or die?
242 die "globmap matched zero file -- legal or die???" ;
243 }
244 elsif (@inputs == 1)
245 {
246 $obj->validateInputFilenames($inputs[0])
247 or return undef;
248 $_[0] = $inputs[0] ;
249 $data{inType} = 'filename' ;
250 $data{oneInput} = 1;
251 }
252 else
253 {
254 $obj->validateInputFilenames(@inputs)
255 or return undef;
256 $_[0] = [ @inputs ] ;
257 $data{inType} = 'filenames' ;
258 }
259 }
260 elsif ($inType eq 'filename')
261 {
262 $obj->validateInputFilenames($_[0])
263 or return undef;
264 }
265 elsif ($inType eq 'array')
266 {
267 $data{inType} = 'filenames' ;
268 $obj->validateInputArray($_[0])
269 or return undef ;
270 }
271
272 return $obj->saveErrorString("$reportClass: output buffer is read-only")
273 if $outType eq 'buffer' && readonly(${ $_[1] });
274
275 if ($outType eq 'filename' )
276 {
277 $obj->croakError("$reportClass: output filename is undef or null string")
278 if ! defined $_[1] || $_[1] eq '' ;
e7d45986 279
280 if (-e $_[1])
281 {
282 if (-d _ )
283 {
284 return $obj->saveErrorString("output file '$_[1]' is a directory");
285 }
286 }
25f0751f 287 }
288
289 return $obj ;
290}
291
292sub Validator::saveErrorString
293{
294 my $self = shift ;
295 ${ $self->{Error} } = shift ;
296 return undef;
297
298}
299
300sub Validator::croakError
301{
302 my $self = shift ;
303 $self->saveErrorString($_[0]);
304 croak $_[0];
305}
306
307
308
309sub Validator::validateInputFilenames
310{
311 my $self = shift ;
312
313 foreach my $filename (@_)
314 {
315 $self->croakError("$self->{reportClass}: input filename is undef or null string")
316 if ! defined $filename || $filename eq '' ;
317
318 next if $filename eq '-';
319
320 if (! -e $filename )
321 {
322 return $self->saveErrorString("input file '$filename' does not exist");
323 }
324
e7d45986 325 if (-d _ )
326 {
327 return $self->saveErrorString("input file '$filename' is a directory");
328 }
329
330 if (! -r _ )
25f0751f 331 {
332 return $self->saveErrorString("cannot open file '$filename': $!");
333 }
334 }
335
336 return 1 ;
337}
338
339sub Validator::validateInputArray
340{
341 my $self = shift ;
342
343 if ( @{ $_[0] } == 0 )
344 {
345 return $self->saveErrorString("empty array reference") ;
346 }
347
348 foreach my $element ( @{ $_[0] } )
349 {
350 my $inType = whatIsInput($element);
351
352 if (! $inType)
353 {
354 $self->croakError("unknown input parameter") ;
355 }
356 elsif($inType eq 'filename')
357 {
358 $self->validateInputFilenames($element)
359 or return undef ;
360 }
361 else
362 {
363 $self->croakError("not a filename") ;
364 }
365 }
366
367 return 1 ;
368}
369
370#sub Validator::validateHash
371#{
372# my $self = shift ;
373# my $href = shift ;
374#
375# while (my($k, $v) = each %$href)
376# {
377# my $ktype = whatIsInput($k);
378# my $vtype = whatIsOutput($v, WANT_EXT|WANT_UNDEF) ;
379#
380# if ($ktype ne 'filename')
381# {
382# return $self->saveErrorString("hash key not filename") ;
383# }
384#
385# my %valid = map { $_ => 1 } qw(filename buffer array undef handle) ;
386# if (! $valid{$vtype})
387# {
388# return $self->saveErrorString("hash value not ok") ;
389# }
390# }
391#
392# return $self ;
393#}
394
395sub createSelfTiedObject
396{
397 my $class = shift || (caller)[0] ;
398 my $error_ref = shift ;
399
400 my $obj = bless Symbol::gensym(), ref($class) || $class;
401 tie *$obj, $obj if $] >= 5.005;
402 *$obj->{Closed} = 1 ;
403 $$error_ref = '';
404 *$obj->{Error} = $error_ref ;
405 my $errno = 0 ;
406 *$obj->{ErrorNo} = \$errno ;
407
408 return $obj;
409}
410
411
412
413#package Parse::Parameters ;
414#
415#
416#require Exporter;
417#our ($VERSION, @ISA, @EXPORT);
418#$VERSION = '2.000_08';
419#@ISA = qw(Exporter);
420
421$EXPORT_TAGS{Parse} = [qw( ParseParameters
422 Parse_any Parse_unsigned Parse_signed
423 Parse_boolean Parse_custom Parse_string
424 Parse_store_ref
425 )
426 ];
427
428push @EXPORT, @{ $EXPORT_TAGS{Parse} } ;
429
430use constant Parse_any => 0x01;
431use constant Parse_unsigned => 0x02;
432use constant Parse_signed => 0x04;
433use constant Parse_boolean => 0x08;
434use constant Parse_string => 0x10;
435use constant Parse_custom => 0x12;
436
437use constant Parse_store_ref => 0x100 ;
438
439use constant OFF_PARSED => 0 ;
440use constant OFF_TYPE => 1 ;
441use constant OFF_DEFAULT => 2 ;
442use constant OFF_FIXED => 3 ;
443use constant OFF_FIRST_ONLY => 4 ;
444use constant OFF_STICKY => 5 ;
445
446
447
448sub ParseParameters
449{
450 my $level = shift || 0 ;
451
452 my $sub = (caller($level + 1))[3] ;
453 local $Carp::CarpLevel = 1 ;
454 my $p = new IO::Compress::Base::Parameters() ;
455 $p->parse(@_)
456 or croak "$sub: $p->{Error}" ;
457
458 return $p;
459}
460
461#package IO::Compress::Base::Parameters;
462
463use strict;
464use warnings;
465use Carp;
466
467sub IO::Compress::Base::Parameters::new
468{
469 my $class = shift ;
470
471 my $obj = { Error => '',
472 Got => {},
473 } ;
474
475 #return bless $obj, ref($class) || $class || __PACKAGE__ ;
476 return bless $obj, 'IO::Compress::Base::Parameters' ;
477}
478
479sub IO::Compress::Base::Parameters::setError
480{
481 my $self = shift ;
482 my $error = shift ;
483 my $retval = @_ ? shift : undef ;
484
485 $self->{Error} = $error ;
486 return $retval;
487}
488
489#sub getError
490#{
491# my $self = shift ;
492# return $self->{Error} ;
493#}
494
495sub IO::Compress::Base::Parameters::parse
496{
497 my $self = shift ;
498
499 my $default = shift ;
500
501 my $got = $self->{Got} ;
502 my $firstTime = keys %{ $got } == 0 ;
503
504 my (@Bad) ;
505 my @entered = () ;
506
507 # Allow the options to be passed as a hash reference or
508 # as the complete hash.
509 if (@_ == 0) {
510 @entered = () ;
511 }
512 elsif (@_ == 1) {
513 my $href = $_[0] ;
514 return $self->setError("Expected even number of parameters, got 1")
515 if ! defined $href or ! ref $href or ref $href ne "HASH" ;
516
517 foreach my $key (keys %$href) {
518 push @entered, $key ;
519 push @entered, \$href->{$key} ;
520 }
521 }
522 else {
523 my $count = @_;
524 return $self->setError("Expected even number of parameters, got $count")
525 if $count % 2 != 0 ;
526
527 for my $i (0.. $count / 2 - 1) {
528 push @entered, $_[2* $i] ;
529 push @entered, \$_[2* $i+1] ;
530 }
531 }
532
533
534 while (my ($key, $v) = each %$default)
535 {
536 croak "need 4 params [@$v]"
537 if @$v != 4 ;
538
539 my ($first_only, $sticky, $type, $value) = @$v ;
540 my $x ;
541 $self->_checkType($key, \$value, $type, 0, \$x)
542 or return undef ;
543
544 $key = lc $key;
545
546 if ($firstTime || ! $sticky) {
547 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
548 }
549
550 $got->{$key}[OFF_PARSED] = 0 ;
551 }
552
553 for my $i (0.. @entered / 2 - 1) {
554 my $key = $entered[2* $i] ;
555 my $value = $entered[2* $i+1] ;
556
557 #print "Key [$key] Value [$value]" ;
558 #print defined $$value ? "[$$value]\n" : "[undef]\n";
559
560 $key =~ s/^-// ;
561 my $canonkey = lc $key;
562
563 if ($got->{$canonkey} && ($firstTime ||
564 ! $got->{$canonkey}[OFF_FIRST_ONLY] ))
565 {
566 my $type = $got->{$canonkey}[OFF_TYPE] ;
567 my $s ;
568 $self->_checkType($key, $value, $type, 1, \$s)
569 or return undef ;
570 #$value = $$value unless $type & Parse_store_ref ;
571 $value = $$value ;
572 $got->{$canonkey} = [1, $type, $value, $s] ;
573 }
574 else
575 { push (@Bad, $key) }
576 }
577
578 if (@Bad) {
579 my ($bad) = join(", ", @Bad) ;
580 return $self->setError("unknown key value(s) @Bad") ;
581 }
582
583 return 1;
584}
585
586sub IO::Compress::Base::Parameters::_checkType
587{
588 my $self = shift ;
589
590 my $key = shift ;
591 my $value = shift ;
592 my $type = shift ;
593 my $validate = shift ;
594 my $output = shift;
595
596 #local $Carp::CarpLevel = $level ;
597 #print "PARSE $type $key $value $validate $sub\n" ;
598 if ( $type & Parse_store_ref)
599 {
600 #$value = $$value
601 # if ref ${ $value } ;
602
603 $$output = $value ;
604 return 1;
605 }
606
607 $value = $$value ;
608
609 if ($type & Parse_any)
610 {
611 $$output = $value ;
612 return 1;
613 }
614 elsif ($type & Parse_unsigned)
615 {
616 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
617 if $validate && ! defined $value ;
618 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
619 if $validate && $value !~ /^\d+$/;
620
621 $$output = defined $value ? $value : 0 ;
622 return 1;
623 }
624 elsif ($type & Parse_signed)
625 {
626 return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
627 if $validate && ! defined $value ;
628 return $self->setError("Parameter '$key' must be a signed int, got '$value'")
629 if $validate && $value !~ /^-?\d+$/;
630
631 $$output = defined $value ? $value : 0 ;
632 return 1 ;
633 }
634 elsif ($type & Parse_boolean)
635 {
636 return $self->setError("Parameter '$key' must be an int, got '$value'")
637 if $validate && defined $value && $value !~ /^\d*$/;
638 $$output = defined $value ? $value != 0 : 0 ;
639 return 1;
640 }
641 elsif ($type & Parse_string)
642 {
643 $$output = defined $value ? $value : "" ;
644 return 1;
645 }
646
647 $$output = $value ;
648 return 1;
649}
650
651
652
653sub IO::Compress::Base::Parameters::parsed
654{
655 my $self = shift ;
656 my $name = shift ;
657
658 return $self->{Got}{lc $name}[OFF_PARSED] ;
659}
660
661sub IO::Compress::Base::Parameters::value
662{
663 my $self = shift ;
664 my $name = shift ;
665
666 if (@_)
667 {
668 $self->{Got}{lc $name}[OFF_PARSED] = 1;
669 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
670 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ;
671 }
672
673 return $self->{Got}{lc $name}[OFF_FIXED] ;
674}
675
676sub IO::Compress::Base::Parameters::valueOrDefault
677{
678 my $self = shift ;
679 my $name = shift ;
680 my $default = shift ;
681
682 my $value = $self->{Got}{lc $name}[OFF_DEFAULT] ;
683
684 return $value if defined $value ;
685 return $default ;
686}
687
688sub IO::Compress::Base::Parameters::wantValue
689{
690 my $self = shift ;
691 my $name = shift ;
692
693 return defined $self->{Got}{lc $name}[OFF_DEFAULT] ;
694
695}
696
697sub IO::Compress::Base::Parameters::clone
698{
699 my $self = shift ;
700 my $obj = { };
701 my %got ;
702
703 while (my ($k, $v) = each %{ $self->{Got} }) {
704 $got{$k} = [ @$v ];
705 }
706
707 $obj->{Error} = $self->{Error};
708 $obj->{Got} = \%got ;
709
710 return bless $obj, 'IO::Compress::Base::Parameters' ;
711}
712
e7d45986 713package U64;
714
715use constant MAX32 => 0xFFFFFFFF ;
716use constant LOW => 0 ;
717use constant HIGH => 1;
718
719sub new
720{
721 my $class = shift ;
722
723 my $high = 0 ;
724 my $low = 0 ;
725
726 if (@_ == 2) {
727 $high = shift ;
728 $low = shift ;
729 }
730 elsif (@_ == 1) {
731 $low = shift ;
732 }
733
734 bless [$low, $high], $class;
735}
736
737sub newUnpack_V64
738{
739 my $string = shift;
740
741 my ($low, $hi) = unpack "V V", $string ;
742 bless [ $low, $hi ], "U64";
743}
744
745sub newUnpack_V32
746{
747 my $string = shift;
748
749 my $low = unpack "V", $string ;
750 bless [ $low, 0 ], "U64";
751}
752
753sub reset
754{
755 my $self = shift;
756 $self->[HIGH] = $self->[LOW] = 0;
757}
758
759sub clone
760{
761 my $self = shift;
762 bless [ @$self ], ref $self ;
763}
764
765sub getHigh
766{
767 my $self = shift;
768 return $self->[HIGH];
769}
770
771sub getLow
772{
773 my $self = shift;
774 return $self->[LOW];
775}
776
777sub get32bit
778{
779 my $self = shift;
780 return $self->[LOW];
781}
782
783sub add
784{
785 my $self = shift;
786 my $value = shift;
787
788 if (ref $value eq 'U64') {
789 $self->[HIGH] += $value->[HIGH] ;
790 $value = $value->[LOW];
791 }
792
793 my $available = MAX32 - $self->[LOW] ;
794
795 if ($value > $available) {
796 ++ $self->[HIGH] ;
797 $self->[LOW] = $value - $available - 1;
798 }
799 else {
800 $self->[LOW] += $value ;
801 }
802}
803
804sub equal
805{
806 my $self = shift;
807 my $other = shift;
808
809 return $self->[LOW] == $other->[LOW] &&
810 $self->[HIGH] == $other->[HIGH] ;
811}
812
813sub getPacked_V64
814{
815 my $self = shift;
816
817 return pack "V V", @$self ;
818}
819
820sub getPacked_V32
821{
822 my $self = shift;
823
824 return pack "V", $self->[LOW] ;
825}
826
827sub pack_V64
828{
829 my $low = shift;
830
831 return pack "V V", $low, 0;
832}
833
834
25f0751f 835package IO::Compress::Base::Common;
836
8371;