File Coverage

blib/lib/IO/Compress/Adapter/Lzf.pm
Criterion Covered Total %
statement 68 78 87.1
branch 15 26 57.6
condition 2 6 33.3
subroutine 14 16 87.5
pod 0 10 0.0
total 99 136 72.7


line stmt bran cond sub pod time code
1             package IO::Compress::Adapter::Lzf ;
2              
3 10     10   77 use strict;
  10         23  
  10         253  
4 10     10   46 use warnings;
  10         16  
  10         201  
5 10     10   40 use bytes;
  10         15  
  10         45  
6              
7 10     10   298 use IO::Compress::Base::Common 2.204 qw(:Status);
  10         132  
  10         1133  
8 10     10   3814 use Compress::LZF ;
  10         3343  
  10         686  
9              
10             our ($VERSION);
11             $VERSION = '2.204';
12              
13 10     10   61 use constant SIGNATURE => 'ZV';
  10         19  
  10         6454  
14              
15             sub mkCompObject
16             {
17 221     221 0 864 my $blocksize = shift ;
18              
19 221         1113 return bless {
20             'Buffer' => '',
21             'BlockSize' => $blocksize,
22             #'CRC' => ! $minimal,
23             'Error' => '',
24             'ErrorNo' => 0,
25             'CompBytes' => 0,
26             'UnCompBytes'=> 0,
27             } ;
28             }
29              
30             sub compr
31             {
32 227     227 0 47408 my $self = shift ;
33              
34 227         374 $self->{Buffer} .= ${ $_[0] } ;
  227         614  
35             return $self->writeBlock(\$_[1], 0)
36 227 50       607 if length $self->{Buffer} >= $self->{BlockSize} ;
37              
38              
39 227         445 return STATUS_OK;
40             }
41              
42             sub flush
43             {
44 4     4 0 872 my $self = shift ;
45              
46             return STATUS_OK
47 4 50       14 unless length $self->{Buffer};
48              
49 4         13 return $self->writeBlock(\$_[0], 1);
50             }
51              
52             sub close
53             {
54 220     220 0 27036 my $self = shift ;
55              
56             return STATUS_OK
57 220 100       655 unless length $self->{Buffer};
58              
59 148         359 return $self->writeBlock(\$_[0], 1);
60             }
61              
62             sub writeBlock
63             {
64 152     152 0 263 my $self = shift;
65 152         214 my $flush = $_[1] ;
66 152         277 my $blockSize = $self->{BlockSize} ;
67              
68 152         419 while (length $self->{Buffer} >= $blockSize) {
69 0         0 my $buff = substr($self->{Buffer}, 0, $blockSize);
70 0         0 substr($self->{Buffer}, 0, $blockSize) = '';
71 0         0 $self->writeOneBlock(\$buff, $_[0]);
72             }
73              
74 152 50 33     632 if ($flush && length $self->{Buffer} ) {
75 152         507 $self->writeOneBlock(\$self->{Buffer}, $_[0]);
76 152         277 $self->{Buffer} = '';
77             }
78              
79 152         362 return STATUS_OK;
80             }
81              
82             sub writeOneBlock
83             {
84 152     152 0 230 my $self = shift;
85 152         201 my $buff = shift;
86              
87 152         206 my $cmp ;
88              
89 152         226 eval { $cmp = Compress::LZF::compress($$buff) };
  152         6750  
90              
91 152 50 33     770 return STATUS_ERROR
92             if $@ || ! defined $cmp;
93              
94 152         250 ${ $_[0] } .= SIGNATURE ;
  152         346  
95              
96             #$self->{UnCompBytes} += length $self->{Buffer} ;
97 152         307 $self->{UnCompBytes} += length $$buff ;
98              
99             # Remove the Compress::LZF header
100 152         359 substr($cmp, 0, c_lzf_header_length($cmp)) = '';
101              
102             #if (length($cmp) >= length($self->{Buffer}))
103 152 100       374 if (length($cmp) >= length $$buff)
104             {
105 67         84 ${ $_[0] } .= pack("Cn", 0, length($$buff));
  67         227  
106 67         122 ${ $_[0] } .= $$buff;
  67         127  
107 67         169 $self->{CompBytes} += length $$buff;
108             }
109             else {
110              
111 85         148 ${ $_[0] } .= pack("Cnn", 1, length($cmp), length($$buff));
  85         327  
112 85         136 ${ $_[0] } .= $cmp;
  85         153  
113 85         136 $self->{CompBytes} += length $cmp;
114             }
115             #$self->{Buffer} = '';
116              
117 152         271 return STATUS_OK;
118             }
119              
120             sub c_lzf_header_length
121             {
122 152     152 0 536 my $firstByte = unpack ("C", substr($_[0], 0, 1));
123              
124 152 100       397 return 1 if $firstByte == 0 ;
125 85 100       239 return 1 unless $firstByte & 0x80 ;
126 73 100       191 return 2 unless $firstByte & 0x20 ;
127 28 50       87 return 3 unless $firstByte & 0x10 ;
128 0 0       0 return 4 unless $firstByte & 0x08 ;
129 0 0       0 return 5 unless $firstByte & 0x04 ;
130 0 0       0 return 6 unless $firstByte & 0x02 ;
131              
132 0         0 return undef;
133             }
134              
135             sub reset
136             {
137 0     0 0 0 return STATUS_OK;
138             }
139              
140             sub compressedBytes
141             {
142 0     0 0 0 my $self = shift ;
143 0         0 $self->{CompBytes};
144             }
145              
146             sub uncompressedBytes
147             {
148 220     220 0 307 my $self = shift ;
149 220         637 $self->{UnCompBytes};
150             }
151              
152             1;
153              
154             __END__