blib/lib/File/Binary.pm

filter: Statementstmt 89.2% 124 / 139 Branchbran 59.0% 26 / 44 Conditioncond 44.4% 8 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 93.7% 30 / 32 Podpod 95.0% 19 / 20 totaltotal 79.1% 209 / 264 CC 30 SCAR 14.3
CC
30
Cov
79%
guess_endian
12.4
open
8.2
seek
6.7
CRAP 4.2
↑ Summary
1 package File::Binary;
2  
3 # importage
4 73 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 73 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 73 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 73 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 73 ▶use vars qw(@EXPORT_OK $VERSION $BIG_ENDIAN $LITTLE_ENDIAN $NATIVE_ENDIAN $AUTOLOAD $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 73 ▶use Fcntl qw(:DEFAULT);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 73 $VERSION='1.7';
12  
13 # for seekable stuff
14 73 $DEBUG = 0;
15  
16 # set up some constants
17 73 $BIG_ENDIAN = 2;
18 73 $LITTLE_ENDIAN = 1;
19 73 $NATIVE_ENDIAN = 0;
20  
21 # and export them
22 73 @EXPORT_OK = qw($BIG_ENDIAN $LITTLE_ENDIAN $NATIVE_ENDIAN guess_endian);
23  
24  
25 =head1 NAME
26  
27 File::Binary - Binary file reading module
28  
29 =head1 SYNOPSIS
30  
31 use File::Binary qw($BIG_ENDIAN $LITTLE_ENDIAN $NATIVE_ENDIAN);
32  
33 my $fb = File::Binary->new("myfile");
34
35 $fb->get_ui8();
36 $fb->get_ui16();
37 $fb->get_ui32();
38 $fb->get_si8();
39 $fb->get_si16();
40 $fb->get_si32();
41  
42 $fb->close();
43  
44 $fb->open(">newfile");
45  
46 $fb->put_ui8(255);
47 $fb->put_ui16(65535);
48 $fb->put_ui32(4294967295);
49 $fb->put_si8(-127);
50 $fb->put_si16(-32767);
51 $fb->put_si32(-2147483645);
52
53 $fb->close();
54  
55  
56 $fb->open(IO::Scalar->new($somedata));
57 $fb->set_endian($BIG_ENDIAN); # force endianness
58  
59 # do what they say on the tin
60 $fb->seek($pos);
61 $fb->tell();
62  
63 # etc etc
64  
65  
66 =head1 DESCRIPTION
67  
68 B<File::Binary> is a Binary file reading module, hence the name,
69 and was originally used to write a suite of modules for manipulating
70 Macromedia SWF files.
71  
72 However it's grown beyond that and now actually, err, works.
73 And is generalised. And EVERYTHING! Yay!
74  
75 It has methods for reading and writing signed and unsigned 8, 16 and
76 32 bit integers, at some point in the future I'll figure out a way of
77 putting in methods for >32bit integers nicely but until then, patches
78 welcome.
79  
80 It hasn't retained backwards compatability with the old version of this
81 module for cleanliness sakes and also because the old interface was
82 pretty braindead.
83  
84 =head1 METHODS
85  
86 =head2 new
87  
88 Pass in either a file name or something which isa an IO::Handle.
89  
90 =cut
91  
92 289 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
93 289 my ($class, $file) = @_;
94  
95 289 my $self = {};
96
97 289 bless $self, $class;
98  
99 289 $self->open($file);
100 289 $self->set_endian($NATIVE_ENDIAN);
101  
102  
103 289 return $self;
104 }
105  
106 =head2 open
107  
108 Pass in either a file name or something which isa an IO::Handle.
109  
110 Will try and set binmode for the handle on if possible (i.e
111 if the object has a C<binmode> method) otherwise you should do
112 it yourself.
113  
114 =cut
115  
116 289 ▶sub open {
Subroutine open: called CC=8 SCAR=21.0
Pod documented
117 289 my ($self, $file) = @_;
118
119 289 my $fh;
120 289 my $writeable = -1;
121  
122 289 ▶ if (ref($file) =~ /^IO::/ && $file->isa('IO::Handle')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($file) =~ /^IO::/ and $file->isa('IO::Handle')) { } 72 217 289
Condition66%2/3
ref($file) =~ /^IO::/ and $file->isa("IO::Handle")
!ll&&!rl&&r
217072
MC/DC50%1/2
ref($file) =~ /^IO::/ and $file->isa("IO::Handle")
ref($file) =~ /^IO::/ $file->isa("IO::Handle")
Truth table66%2/3
A and B
A= ref($file) =~ /^IO::/B= $file->isa("IO::Handle")
ABresult
0X0
100
111
123 72 $fh = $file;
124 72 $writeable = 2; # read and write mode
125 } else {
126 217 ▶ $fh = IO::File->new($file) || die "No such file $file\n";
Condition50%1/2
'IO::File'->new($file) || die("No such file $file\n")
l!l
2170
MC/DC0%0/1
'IO::File'->new($file) || die("No such file $file\n")
'IO::File'->new($file)
Truth table50%1/2
A
A= 'IO::File'->new($file)
Aresult
11
00
127 217 ▶ if ($file =~ /^>/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /^>/) { } 72 145 217
elsif ($file =~ /^\+>/) { } 0 145 145
128 72 $writeable = 1;
129 } elsif ($file =~ /^\+>/) {
130 0 $writeable=2;
131 }
132 }
133 289 ▶ $fh->binmode if $fh->can('binmode');
Branch: 1 branch
BranchTrueFalseTotal
if $fh->can("binmode") 289 0 289
134  
135 289 $self->{_bitbuf} = '';
136 289 $self->{_bitpos} = 0;
137 289 $self->{_fh} = $fh;
138 289 $self->{_fhpos} = 0;
139 289 $self->{_flush} = 1;
140 289 $self->{_writeable} = $writeable;
141 289 ▶ $self->{_is_seekable} = UNIVERSAL::isa($fh,'IO::Seekable')?1:0;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($fh, 'IO::Seekable') ? : 217 72 289
142
143  
144 289 return $self;
145 }
146  
147 =head2 seek
148  
149 Seek to a position.
150  
151 Return our current position. If our file handle is not
152 B<ISA IO::Seekable> it will return 0 and, if
153 B<$File::Binary::DEBUG> is set to 1, there will be a warning.
154  
155 You can optionally pass a whence option in the same way as
156 the builtin Perl seek() method. It defaults to C<SEEK_SET>.
157  
158 Returns the current file position.
159  
160  
161 =cut
162  
163 10 ▶sub seek {
Subroutine seek: called CC=5 SCAR=19.1
Pod documented
164 10 my $self = shift;
165 10 my $seek = shift;
166 10 ▶ my $whence = shift || SEEK_SET;
Condition50%1/2
shift() || 0
l!l
010
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
167 10 ▶ unless ($self->{_is_seekable}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_is_seekable"}) 0 10 10
168 0 ▶ carp "FH is not seekable" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Binary::DEBUG 0 0 0
169 0 return 0;
170 }
171  
172 10 ▶ $self->{_fh}->seek($seek, $whence) if defined $seek;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seek 10 0 10
173 10 $self->_init_bits();
174 10 return $self->{_fh}->tell();
175  
176  
177
178 }
179  
180 =head2 tell
181  
182 Return our current position. If our file handle is not
183 B<ISA IO::Seekable> then it will return 0 and, if
184 B<$File::Binary::DEBUG> is set to 1, there will be a
185 warning.
186  
187 =cut
188  
189 10 ▶sub tell {
Subroutine tell: called CC=3 SCAR=15.1
Pod documented
190 10 my $self = shift;
191 10 ▶ unless ($self->{_is_seekable}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_is_seekable"}) 0 10 10
192 0 ▶ carp "FH is not seekable" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Binary::DEBUG 0 0 0
193 0 return 0;
194 }
195  
196 10 return $self->{_fh}->tell();
197 }
198  
199  
200  
201 =head2 set_flush
202  
203 To flush or not to flush. That is the question
204  
205 =cut
206  
207 0 ▶sub set_flush {
Subroutine set_flush: not called CC=1 SCAR=6.9
Pod documented
208 0 my ($self, $flush) = @_;
209  
210 0 $self->{_flush} = $flush;
211 }
212  
213  
214 =head2 set_endian
215  
216 Set the how the module reads files. The options are
217  
218 $BIG_ENDIAN
219 $LITTLE_ENDIAN
220 $NATIVE_ENDIAN
221  
222  
223 I<NATIVE> will deduce the endianess of the current system.
224  
225 =cut
226  
227 578 ▶sub set_endian {
Subroutine set_endian: called CC=4 SCAR=13.9
Pod documented
228 578 my ($self, $endian) = @_;
229  
230 578 ▶ $endian ||= $NATIVE_ENDIAN;
Condition66%2/3
$endian ||= $File::Binary::NATIVE_ENDIAN
l!l&&r!l&&!r
2890289
MC/DC50%1/2
$endian ||= $File::Binary::NATIVE_ENDIAN
$endian $File::Binary::NATIVE_ENDIAN
Truth table66%2/3
A ||= B
A= $endianB= $File::Binary::NATIVE_ENDIAN
ABresult
1X1
011
000
231  
232 578 ▶ $endian = guess_endian() if ($endian == $NATIVE_ENDIAN);
Branch: 1 branch
BranchTrueFalseTotal
if $endian == $File::Binary::NATIVE_ENDIAN 289 289 578
233  
234 578 ▶ if ($endian == $BIG_ENDIAN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endian == $File::Binary::BIG_ENDIAN) { } 144 434 578
235 144 $self->{_ui16} = 'v';
236 144 $self->{_ui32} = 'V';
237 } else {
238 434 $self->{_ui16} = 'n';
239 434 $self->{_ui32} = 'N';
240 }
241  
242 578 $self->{_endian} = $endian;
243  
244 }
245  
246  
247 2904 ▶sub _init_bits {
Subroutine _init_bits: called CC=2 SCAR=7.6
248 2904 my $self = shift;
249  
250 2904 ▶ if ($self->{_writeable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_writeable'}) { } 2904 0 2904
251 2904 $self->_init_bits_write();
252 } else {
253 0 $self->_init_bits_read();
254 }
255 }
256  
257  
258 2904 ▶sub _init_bits_write {
Subroutine _init_bits_write: called CC=2 SCAR=7.9
259 2904 my $self = shift;
260  
261 2904 my $bits = $self->{'_bitbuf'};
262  
263 2904 my $len = length($bits);
264  
265 2904 ▶ return if $len<=0;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 0 2904 0 2904
266  
267 0 $self->{'_bitbuf'} = '';
268 0 $self->{_fh}->write(pack('B8', $bits.('0'x(8-$len))));
269  
270 }
271  
272 0 ▶sub _init_bits_read {
Subroutine _init_bits_read: not called CC=1 SCAR=6.9
273 0 my $self = shift;
274
275 0 $self->{_pos} = 0;
276 0 $self->{_bits} = 0;
277  
278 }
279  
280  
281 =head2 get_bytes
282  
283 Get an arbitary number of bytes from the file.
284  
285 =cut
286  
287 2894 ▶sub get_bytes {
Subroutine get_bytes: called CC=4 SCAR=14.2
Pod documented
288 2894 my ($self, $bytes) = @_;
289
290 2894 $bytes = int $bytes;
291  
292 2894 ▶ carp("Must be positive number") if ($bytes <1);
Branch: 1 branch
BranchTrueFalseTotal
if $bytes < 1 0 2894 2894
293 2894 ▶ carp("This file has been opened in write mode.") if $self->{_writeable} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_writeable"} == 1 0 2894 2894
294  
295 2894 ▶ $self->_init_bits() if $self->{_flush};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_flush"} 2894 0 2894
296
297 2894 $self->{_fh}->read(my $data, $bytes);
298  
299 2894 $self->{_fhpos} += $bytes;
300  
301 2894 return $data;
302 }
303
304  
305 =head2 put_bytes
306  
307 Write some bytes
308  
309 =cut
310  
311 958 ▶sub put_bytes {
Subroutine put_bytes: called CC=2 SCAR=7.1
Pod documented
312 958 my ($self, $bytes) = @_;
313  
314
315 958 ▶ carp("This file has been opened in read mode.") unless $self->{_writeable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_writeable"} 0 958 958
316  
317 ## TODO?
318 #$self->_init_bits;
319 958 $self->{_fh}->write($bytes);
320 }
321  
322  
323  
324  
325 # we could use POSIX::ceil here but I ph34r the POSIX lib
326 958 ▶sub _round {
Subroutine _round: called CC=2 SCAR=7.2
327 958 ▶ my $num = shift || 0;
Condition50%1/2
shift() || 0
l!l
9580
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
328  
329 958 return int ($num + 0.5 * ($num <=> 0 ) );
330 }
331  
332  
333  
334  
335  
336 2894 ▶sub _get_num {
Subroutine _get_num: called CC=1 SCAR=0.0
337 2894 my ($self, $bytes, $template)=@_;
338  
339 2894 unpack $template, $self->get_bytes($bytes);
340 }
341  
342  
343 958 ▶sub _put_num {
Subroutine _put_num: called CC=1 SCAR=0.0
344 958 my ($self, $num, $template) = @_;
345  
346  
347 958 $self->put_bytes(pack($template, _round($num)));
348 }
349  
350  
351  
352 ## 8 bit
353  
354 =head2 get_ui8 get_si8 put_ui8 put_si8
355  
356 read or write signed or unsigned 8 bit integers
357  
358 =cut
359  
360 162 ▶sub get_ui8 {
Subroutine get_ui8: called CC=1 SCAR=0.0
Pod documented
361 162 my $self = shift;
362 162 $self->_get_num(1, 'C');
363 }
364  
365  
366  
367  
368 288 ▶sub get_si8 {
Subroutine get_si8: called CC=1 SCAR=0.0
Pod documented
369 288 my $self = shift;
370 288 $self->_get_num(1, 'c');
371 }
372  
373  
374  
375 54 ▶sub put_ui8 {
Subroutine put_ui8: called CC=1 SCAR=0.0
Pod documented
376 54 my ($self,$num) = @_;
377 54 $self->_put_num($num, 'C');
378 }
379  
380  
381 96 ▶sub put_si8 {
Subroutine put_si8: called CC=1 SCAR=0.0
Pod documented
382 96 my ($self,$num) = @_;
383 96 $self->_put_num($num, 'c');
384  
385 }
386  
387  
388 ## 16 bit
389  
390 =head2 get_ui16 get_si16 put_ui16 put_si16
391  
392 read or write signed or unsigned 16 bit integers
393  
394 =cut
395  
396 828 ▶sub get_ui16 {
Subroutine get_ui16: called CC=1 SCAR=0.0
Pod documented
397 828 my $self = shift;
398 828 $self->_get_num(2, $self->{_ui16});
399 }
400  
401  
402 540 ▶sub get_si16 {
Subroutine get_si16: called CC=2 SCAR=6.9
Pod documented
403 540 my $self = shift;
404
405 540 my $num = $self->get_ui16();
406 540 ▶ $num -= (1<<16) if $num>=(1<<15);
Branch: 1 branch
BranchTrueFalseTotal
if $num >= 32768 270 270 540
407  
408 540 return $num;
409 }
410  
411  
412  
413 276 ▶sub put_ui16 {
Subroutine put_ui16: called CC=1 SCAR=0.0
Pod documented
414 276 my ($self,$num) = @_;
415
416 276 $self->_put_num($num, $self->{_ui16});
417 }
418  
419 73 *put_si16 = \&put_ui16;
420  
421  
422  
423 ## 32 bit
424  
425 =head2 get_ui32 get_s32 put_ui32 put_si32
426  
427 read or write signed or unsigned 32 bit integers
428  
429 =cut
430  
431  
432  
433 1616 ▶sub get_ui32 {
Subroutine get_ui32: called CC=1 SCAR=0.0
Pod documented
434 1616 my $self = shift;
435 1616 return $self->_get_num(4, $self->{_ui32});
436 }
437  
438  
439 1076 ▶sub get_si32 {
Subroutine get_si32: called CC=2 SCAR=6.9
Pod undocumented
440 1076 my $self = shift;
441  
442 1076 my $num = $self->get_ui32();
443 1076 ▶ $num -= (2**32) if ($num>=(2**31));
Branch: 1 branch
BranchTrueFalseTotal
if $num >= 2147483648 548 528 1076
444 1076 return $num;
445 }
446  
447  
448 532 ▶sub put_ui32 {
Subroutine put_ui32: called CC=1 SCAR=0.0
Pod documented
449 532 my ($self, $num) = @_;
450  
451 532 $self->_put_num($num, $self->{_ui32});
452 }
453  
454 73 *put_si32 = \&put_ui32;
455  
456  
457  
458  
459 =head2 guess_endian
460  
461 Guess the endianness of this system. Returns either I<$LITTLE_ENDIAN>
462 or I<$BIG_ENDIAN>
463  
464 =cut
465  
466 289 ▶sub guess_endian {
Subroutine guess_endian: called CC=5 SCAR=25.2
Pod documented
467  
468  
469 #my $svalue = int rand (2**16)-1;
470 #my $lvalue = int rand (2**32)-1;
471  
472 #my $sp = pack("S", $svalue);
473 #my $lp = pack("L", $lvalue);
474  
475  
476 #if (unpack("V", $lp) == $lvalue && unpack("v", $sp) == $svalue) {
477 # return $LITTLE_ENDIAN;
478 #} elsif (unpack("N", $lp) == $lvalue && unpack("n", $sp) == $svalue) {
479 # return $BIG_ENDIAN;
480 #} else {
481 # carp "Couldn't determine whether this machine is big-endian or little-endian\n";
482 #}
483  
484 289 my $bo = $Config{'byteorder'};
485  
486 289 ▶ if (1234 == $bo or 12345678 == $bo) {
Branch: 2 branches
BranchTrueFalseTotal
if (1234 == $bo or 12345678 == $bo) { } 289 0 289
elsif (4321 == $bo or 87654321 == $bo) { } 0 0 0
Condition33%1/3
1234 == $bo or 12345678 == $bo
l!l&&r!l&&!r
02890
MC/DC0%0/2
1234 == $bo or 12345678 == $bo
1234 == $bo 12345678 == $bo
Truth table33%1/3
A or B
A= 1234 == $boB= 12345678 == $bo
ABresult
1X1
011
000
487 289 return $LITTLE_ENDIAN;
488 ▶ } elsif (4321 == $bo or 87654321 == $bo) {
Condition0%0/3
4321 == $bo or 87654321 == $bo
l!l&&r!l&&!r
000
MC/DC0%0/2
4321 == $bo or 87654321 == $bo
4321 == $bo 87654321 == $bo
Truth table0%0/3
A or B
A= 4321 == $boB= 87654321 == $bo
ABresult
1X1
011
000
489 0 return $BIG_ENDIAN;
490 } else {
491 0 carp "Unsupported architecture (probably a Cray or weird order)\n";
492 }
493  
494  
495 }
496  
497  
498 =head2 close
499
500 Close the file up. The I<File::Binary> object will then be useless
501 until you open up another file;
502  
503 =cut
504  
505 289 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
506 289 my $self = shift;
507 289 $self->{_fh}->close();
508 289 $self = {};
509 }
510  
511  
512  
513 =pod
514  
515 =head1 BUGS
516  
517 Can't do numbers greater than 32 bits.
518  
519 Can't extract Floating Point or Fixed Point numbers.
520  
521 Can't extract null terminated strings.
522  
523 Needs tests for seeking and telling.
524  
525 =head1 COPYING
526  
527 (c)opyright 2002, Simon Wistow
528  
529 Distributed under the same terms as Perl itself.
530  
531 This software is under no warranty and will probably ruin your life, kill your friends, burn your house and bring about the apocalypse
532  
533  
534 =head1 AUTHOR
535  
536 Copyright 2003, Simon Wistow <simon@thegestalt.org>
537  
538  
539 =cut
540  
541  
542 73 1;
↑ Summary