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   95 use strict;
  10         29  
  10         306  
4 10     10   52 use warnings;
  10         20  
  10         232  
5 10     10   51 use bytes;
  10         16  
  10         66  
6              
7 10     10   339 use IO::Compress::Base::Common 2.205 qw(:Status);
  10         165  
  10         1357  
8 10     10   4457 use Compress::LZF ;
  10         3855  
  10         824  
9              
10             our ($VERSION);
11             $VERSION = '2.205';
12              
13 10     10   73 use constant SIGNATURE => 'ZV';
  10         21  
  10         7630  
14              
15             sub mkCompObject
16             {
17 221     221 0 990 my $blocksize = shift ;
18              
19 221         1212 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 50880 my $self = shift ;
33              
34 227         426 $self->{Buffer} .= ${ $_[0] } ;
  227         662  
35             return $self->writeBlock(\$_[1], 0)
36 227 50       720 if length $self->{Buffer} >= $self->{BlockSize} ;
37              
38              
39 227         508 return STATUS_OK;
40             }
41              
42             sub flush
43             {
44 4     4 0 922 my $self = shift ;
45              
46             return STATUS_OK
47 4 50       14 unless length $self->{Buffer};
48              
49 4         12 return $self->writeBlock(\$_[0], 1);
50             }
51              
52             sub close
53             {
54 220     220 0 29032 my $self = shift ;
55              
56             return STATUS_OK
57 220 100       733 unless length $self->{Buffer};
58              
59 148         404 return $self->writeBlock(\$_[0], 1);
60             }
61              
62             sub writeBlock
63             {
64 152     152 0 245 my $self = shift;
65 152         241 my $flush = $_[1] ;
66 152         261 my $blockSize = $self->{BlockSize} ;
67              
68 152         449 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     712 if ($flush && length $self->{Buffer} ) {
75 152         533 $self->writeOneBlock(\$self->{Buffer}, $_[0]);
76 152         292 $self->{Buffer} = '';
77             }
78              
79 152         413 return STATUS_OK;
80             }
81              
82             sub writeOneBlock
83             {
84 152     152 0 253 my $self = shift;
85 152         221 my $buff = shift;
86              
87 152         221 my $cmp ;
88              
89 152         277 eval { $cmp = Compress::LZF::compress($$buff) };
  152         6866  
90              
91 152 50 33     882 return STATUS_ERROR
92             if $@ || ! defined $cmp;
93              
94 152         236 ${ $_[0] } .= SIGNATURE ;
  152         389  
95              
96             #$self->{UnCompBytes} += length $self->{Buffer} ;
97 152         339 $self->{UnCompBytes} += length $$buff ;
98              
99             # Remove the Compress::LZF header
100 152         382 substr($cmp, 0, c_lzf_header_length($cmp)) = '';
101              
102             #if (length($cmp) >= length($self->{Buffer}))
103 152 100       431 if (length($cmp) >= length $$buff)
104             {
105 67         97 ${ $_[0] } .= pack("Cn", 0, length($$buff));
  67         283  
106 67         137 ${ $_[0] } .= $$buff;
  67         138  
107 67         141 $self->{CompBytes} += length $$buff;
108             }
109             else {
110              
111 85         124 ${ $_[0] } .= pack("Cnn", 1, length($cmp), length($$buff));
  85         385  
112 85         143 ${ $_[0] } .= $cmp;
  85         188  
113 85         164 $self->{CompBytes} += length $cmp;
114             }
115             #$self->{Buffer} = '';
116              
117 152         314 return STATUS_OK;
118             }
119              
120             sub c_lzf_header_length
121             {
122 152     152 0 655 my $firstByte = unpack ("C", substr($_[0], 0, 1));
123              
124 152 100       456 return 1 if $firstByte == 0 ;
125 85 100       265 return 1 unless $firstByte & 0x80 ;
126 73 100       206 return 2 unless $firstByte & 0x20 ;
127 28 50       95 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 392 my $self = shift ;
149 220         766 $self->{UnCompBytes};
150             }
151              
152             1;
153              
154             __END__