File Coverage

lib/Archive/Lha/Decode/Base.pm
Criterion Covered Total %
statement 54 55 98.1
branch 2 2 100.0
condition 9 19 47.3
subroutine 10 11 90.9
pod 1 1 100.0
total 76 88 86.3


line stmt bran cond sub pod time code
1             package Archive::Lha::Decode::Base;
2              
3 18     18   1319 use strict;
  18         37  
  18         761  
4 18     18   101 use warnings;
  18         35  
  18         1063  
5 18     18   105 use Carp;
  18         165  
  18         1653  
6 18     18   768 use Archive::Lha::Constants;
  18         34  
  18         142  
7 18     18   9473 use Archive::Lha; # to load XS
  18         58  
  18         1439  
8              
9             my %_params; # keyed by subclass package name
10              
11             my @_PARAM_NAMES = qw(
12             NPT
13             NP
14             NT
15             NC
16             PBIT
17             TBIT
18             CBIT
19             PT_TABLE_BIT
20             PT_TABLE_SIZE
21             C_TABLE_BIT
22             C_TABLE_SIZE
23             DICSIZE
24             MAXMATCH
25             THRESHOLD
26             );
27              
28             # Install class methods for each param, looking up via %_params
29             for my $name (@_PARAM_NAMES) {
30 18     18   139 no strict 'refs';
  18         33  
  18         6228  
31 266   33 266   1214 *{"Archive::Lha::Decode::Base::$name"} = sub { $_params{ ref($_[0]) || $_[0] }{$name} };
32             }
33              
34             sub import {
35 9     9   83 my ($class, %options) = @_;
36 9         22 my $caller = caller;
37              
38 9   100     42 my $dicbit = $options{dicbit} || 13;
39 9   33     51 my $max_match = $options{max_match} || ( 1 << UCHAR_BIT );
40 9   50     31 my $threshold = $options{threshold} || 3;
41 9   33     33 my $np = $options{np} || $dicbit + 1;
42 9         25 my $pbit = _bit_length( $np );
43 9   50     44 my $pt_table_bit = $options{pt_table_bit} || 8;
44 9   50     32 my $c_table_bit = $options{c_table_bit} || 12;
45 9         15 my $pt_table_size = 1 << $pt_table_bit;
46 9         17 my $c_table_size = 1 << $c_table_bit;
47 9         14 my $npt = $pt_table_size >> 1;
48 9         23 my $nt = USHORT_BIT + 3;
49 9         22 my $nc = UCHAR_MAX + $max_match + 2 - $threshold;
50 9         35 my $tbit = _bit_length( $nt );
51 9         22 my $cbit = _bit_length( $nc );
52              
53 9         128 $_params{$caller} = {
54             NPT => $npt,
55             NP => $np,
56             NT => $nt,
57             NC => $nc,
58             PBIT => $pbit,
59             TBIT => $tbit,
60             CBIT => $cbit,
61             PT_TABLE_BIT => $pt_table_bit,
62             PT_TABLE_SIZE => $pt_table_size,
63             C_TABLE_BIT => $c_table_bit,
64             C_TABLE_SIZE => $c_table_size,
65             DICSIZE => 1 << $dicbit,
66             MAXMATCH => $max_match,
67             THRESHOLD => $threshold,
68             };
69              
70             {
71 18     18   159 no strict 'refs';
  18         32  
  18         6968  
  9         21  
72 9 100       1949 unless ( ${"$class\::_accessors_installed"} ) {
  9         68  
73 5         7 ${"$class\::_accessors_installed"} = 1;
  5         15  
74 5         15 for my $name ( qw( pt c tree bit ) ) {
75 20     0   58 *{"$class\::$name"} = sub { shift->{$name} };
  20         82  
  0         0  
76             }
77             }
78 9         14 push @{"$caller\::ISA"}, $class;
  9         1103  
79             }
80             }
81              
82             sub new {
83 19     19 1 73 my ($class, %options) = @_;
84              
85 19         40 my $header = $options{header};
86              
87             my $self = bless {
88             blocksize => 0,
89             read => $options{read},
90             write => $options{write},
91             encoded_size => $header->{encoded_size},
92             original_size => $header->{original_size},
93             crc16 => $header->{crc16} || 0,
94 19   50     113 map { $_ => $class->$_() } @_PARAM_NAMES,
  266         710  
95             }, $class;
96              
97 19         136 $self;
98             }
99              
100             1;
101              
102             __END__