NFB.pm

filter: Statementstmt 99.1% 120 / 121 Branchbran 77.2% 17 / 22 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 21 / 21 Podpod 100% 8 / 8 totaltotal 92.8% 168 / 181 CC 15 SCAR 9.6
CC
15
Cov
93%
DESTROY
6.0
read
5.0
elements
3.0
CRAP 2.6
« NFB/Element.pm ↑ Summary
1 package Nokia::File::NFB;
2  
3 ## Create, Parse and Write Nokia NFB files.
4 ## Robert Price - http://www.robertprice.co.uk/
5  
6 3 ▶use 5.00503;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Compress::Zlib qw(crc32);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Encode qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Fcntl qw(:seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use Nokia::File::NFB::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 $VERSION = '0.01';
20  
21  
22 ## new()
23 ## the object constructor.
24 ## OUTPUT: a blessed hash corresponding to the object.
25 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
26 4 my $class = shift;
27 4 my $self = {};
28 4 bless $self, $class;
29 4 return $self;
30 }
31  
32  
33 ## DESTROY()
34 ## destructor for the object. We just make sure we have any
35 ## open file handles closed before we die.
36 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=17.9
37 4 my $self = shift;
38  
39 ## close any filehandles we have in the object that are still open.
40 4 ▶ $self->{'_fh'}->close() if ((exists $self->{'_fh'}) && ($self->{'_fh'}) && (ref($self->{'_fh'}) eq 'FileHandle'));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_fh"} and $self->{"_fh"} and ref $self->{"_fh"} eq "FileHandle" 0 4 4
Condition33%2/6
exists $self->{"_fh"} and $self->{"_fh"} and ref $self->{"_fh"} eq "FileHandle"
!ll&&!rl&&r
400
exists $self->{"_fh"} and $self->{"_fh"}
!ll&&!rl&&r
400
MC/DC0%0/3
exists $self->{"_fh"} and $self->{"_fh"} and ref $self->{"_fh"} eq "FileHandle"
exists $self->{"_fh"} $self->{"_fh"} ref $self->{"_fh"} eq "FileHandle"
Truth table25%1/4
A and B and C
A= exists $self->{"_fh"}B= $self->{"_fh"}C= ref $self->{"_fh"} eq "FileHandle"
ABCresult
0XX0
10X0
1100
1111
41 }
42  
43  
44 ## version()
45 ## get or set the NFB file version.
46 ## INPUT: a string with the NFB file version in (optional).
47 ## OUTPUT: a string with the NFB file version in.
48 8 ▶sub version {
Subroutine version: called CC=2 SCAR=6.9
Pod documented
49 8 my $self = shift;
50 8 ▶ $self->{'_version'} = $_[0] if ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 3 5 8
51 8 return $self->{'_version'};
52 }
53  
54  
55 ## firmware()
56 ## get or set the phone firmware relating to the file.
57 ## INPUT: a string with the phone firmware in (optional).
58 ## OUTPUT: a string with the phone firmware in.
59 8 ▶sub firmware {
Subroutine firmware: called CC=2 SCAR=6.9
Pod documented
60 8 my $self = shift;
61 8 ▶ $self->{'_firmware'} = $_[0] if ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 3 5 8
62 8 return $self->{'_firmware'};
63 }
64  
65  
66 ## phone()
67 ## get or set the phone model relating to the file.
68 ## INPUT: a string with the phone model in (optional).
69 ## OUTPUT: a string with the phone model in.
70 7 ▶sub phone {
Subroutine phone: called CC=2 SCAR=6.9
Pod documented
71 7 my $self = shift;
72 7 ▶ $self->{'_phone'} = $_[0] if ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 2 5 7
73 7 return $self->{'_phone'};
74 }
75  
76  
77 ## elements()
78 ## return a reference to an array with the elements in.
79 ## INPUT: a reference to an array with elements in (optional).
80 ## OUTPUT: a reference to an array with the elements in.
81 4 ▶sub elements {
Subroutine elements: called CC=3 SCAR=11.0
Pod documented
82 4 my $self = shift;
83 4 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 2 2 4
84 2 my $elements = $_[0];
85 2 ▶ croak("elements need to be a reference to an ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elements eq "ARRAY" 0 2 2
86 unless (ref($elements) eq 'ARRAY');
87 2 $self->{'_elements'} = $elements;
88 }
89 4 return $self->{'_elements'};
90 }
91  
92  
93 ## read()
94 ## reads in the NFB file from a specified filename, parse it
95 ## and setup the object with the corresponding data.
96 ## INPUT: filename to read.
97 2 ▶sub read {
Subroutine read: called CC=5 SCAR=16.1
Pod documented
98 2 my $self = shift;
99 2 my $filename = shift;
100
101 ## keep a copy of the filename in the object for backup.
102 2 $self->{'_filename'} = $filename;
103
104 ## open the file as readonly, else warn the user.
105 2 my $fh = new FileHandle($filename, 'r');
106 2 ▶ croak "Unable to open $filename for reading" unless (defined($fh));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 2 2
107 2 $self->{'_fh'} = $fh;
108
109 ## ensure we are in binary mode as some system (win32 for example)
110 ## will assume we are handling text otherwise.
111 2 binmode $fh, ':raw';
112  
113 ## work out the length of the file and save it in the object.
114 2 $fh->seek(-4, SEEK_END);
115 2 $self->{'_length'} = $fh->tell();
116  
117 ## work out the checksum for the data and save it in the object.
118 2 $fh->seek(0, SEEK_SET);
119 2 $self->{'_crc'} = crc32($self->_read($self->{'_length'}));
120
121 ## work out the file version, this is a little endian long
122 2 $fh->seek(0, SEEK_SET);
123 2 $self->{'_version'} = unpack('V',$self->_read(4));
124  
125 ## get the id and phone and store in the object.
126 2 $self->{'_firmware'} = $self->_readstring();
127 2 $self->{'_phone'} = $self->_readstring();
128
129 ## parse the main data.
130  
131 ## work out how many elements of data we have stored in the file.
132 2 $self->{'_number_of_elements'} = unpack('V', $self->_read(4));
133  
134 ## where we are going to store our elements
135 2 $self->{'_elements'} = undef;
136
137 ## iterate over all the elements in the file.
138 2 ▶ for (my $i=0; $i<$self->{'_number_of_elements'}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"_number_of_elements"}) 4 2 6
139
140 ## work out filetype of the element, 1 = file, 2 = directory.
141 4 my $filetype = unpack('V', $self->_read(4));
142
143 ## if we have a file...
144 4 ▶ if ($filetype == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filetype == 1) { } 2 2 4
elsif ($filetype == 2) { } 2 0 2
145  
146 ## get the filename.
147 2 my $path = $self->_readstring();
148
149 ## get the length of data.
150 2 my $length = unpack('V', $self->_read(4));
151  
152 ## get the data itself.
153 2 my $fdata = $self->_read($length);
154  
155 ## get the file's timestamp.
156 2 my $timestamp = unpack('V', $self->_read(4));
157  
158 ## store it all in an Element, and save.
159 2 my $data = Nokia::File::NFB::Element->new({
160 'type' => $filetype,
161 'name' => $path,
162 'size' => $length,
163 'time' => $timestamp,
164 'data' => $fdata,
165 });
166 2 push @{$self->{'_elements'}}, $data;
167
168 ## if we have a directory
169 } elsif ($filetype == 2) {
170  
171 ## get the directory's name.
172 2 my $path = $self->_readstring();
173  
174 ## store it all in an Element and save.
175 2 my $data = Nokia::File::NFB::Element->new({
176 'type' => $filetype,
177 'name' => $path,
178 });
179 2 push @{$self->{'_elements'}}, $data;
180
181 ## else it's an unknown file type
182 } else {
183 0 croak("Unknown element type found in data");
184 }
185 }
186
187 ## get the checksum from the file.
188 2 $self->{'_checksum'} = unpack('V', $self->_read(4));
189
190 ## close the file and remove references to the filehandle from the object.
191 2 $fh->close();
192 2 delete $self->{'_fh'};
193 }
194  
195  
196  
197 ## write()
198 ## write out the NFB file as a binary file to a specified filename.
199 ## IN: filename to use
200 1 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
Pod documented
201 1 my $self = shift;
202 1 my $filename = shift;
203  
204 ## open the file for writing and save the handle to the object
205 1 my $fh = new FileHandle($filename, 'w');
206 1 ▶ croak "Unable to open $filename for writing" unless (defined($fh));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 1 1
207 1 $self->{'_fh'} = $fh;
208
209 ## ensure we are in binary mode as some system (win32 for example)
210 ## will assume we are handling text otherwise.
211 1 binmode $fh, ':raw';
212  
213 ## write the binary data out
214 1 print $fh $self->binary();
215  
216 ## close the file and remove references to the filehandle from the object.
217 1 $fh->close();
218 1 delete $self->{'_fh'};
219 }
220  
221  
222  
223 ## binary()
224 ## return the NFB file as a binary scalar.
225 ## OUTPUT: The NFB file as a binary scalar variable.
226 1 ▶sub binary {
Subroutine binary: called CC=2 SCAR=6.9
Pod documented
227 1 my $self = shift;
228 1 my $binfile;
229
230 ## write out the nfb/c version.
231 1 $binfile .= pack('V',$self->{'_version'}); ## pack as little endian long.
232
233 ## write out the firmware.
234 1 $binfile .= pack('V', length($self->{'_firmware'})); ## size of the string.
235 1 $binfile .= encode('UCS-2LE', $self->{'_firmware'}); ## and the string itself, in UCS2 (little endian) format.
236  
237 ## write out the phone model.
238 1 $binfile .= pack('V', length($self->{'_phone'}));
239 1 $binfile .= encode('UCS-2LE', $self->{'_phone'});
240
241 ## the number of elements.
242 1 $binfile .= pack('V',scalar(@{$self->{'_elements'}}));
243
244 ## add each element.
245 1 foreach my $element (@{$self->{'_elements'}}) {
246 2 $binfile .= $element->binary();
247 }
248
249 ## work out the checksum and add it at the end.
250 1 my $checksum = crc32($binfile);
251 1 $binfile .= pack('V',$checksum);
252  
253 1 return $binfile;
254 }
255  
256  
257 ## _readstring
258 ## utility function to read a UCS2 string and return it in utf8.
259 ## OUTPUT: string - the string in utf8 format.
260 8 ▶sub _readstring {
Subroutine _readstring: called CC=1 SCAR=0.0
261 8 my $self = shift;
262
263 ## get the length of the string, as UCS2 is two bytes long, have to double it.
264 8 my $length = unpack("V", $self->_read(4)) * 2;
265
266 ## read, decode and return the string.
267 8 my $ucs2string = $self->_read($length);
268 8 return decode('UCS-2LE', $ucs2string);
269 }
270  
271  
272  
273 ## _read
274 ## utility function to read data from a FileHandle and return it.
275 ## INPUT: size - the ammount of data to read.
276 ## OUTPUT: data - the data.
277 34 ▶sub _read {
Subroutine _read: called CC=1 SCAR=0.0
278 34 my $self = shift;
279 34 my $size = shift;
280 34 my $data;
281 34 my $fh = $self->{'_fh'};
282 34 $fh->read($data, $size);
283 34 return $data;
284 }
285  
286 3 1;
287 __END__
« NFB/Element.pm ↑ Summary