PropertyList/ReadBinary.pm

filter: Statementstmt 94.8% 165 / 174 Branchbran 60.8% 28 / 46 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 34 / 34 Podpod 100% 2 / 2 totaltotal 89.0% 235 / 264 CC 24 SCAR 10.9
CC
24
Cov
88%
__ANON__
7.1
_get_filehandle
6.1
_read_object
5.0
CRAP 3.0
« PropertyList.pm ↑ Summary
1 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Mac::PropertyList::ReadBinary;
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Mac::PropertyList;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use POSIX qw(SEEK_END SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our $VERSION = '1.608';
15  
16 6 ▶my $Debug = $ENV{PLIST_DEBUG} || 0;
Condition50%1/2
$ENV{'PLIST_DEBUG'} || 0
l!l
06
MC/DC0%0/1
$ENV{'PLIST_DEBUG'} || 0
$ENV{'PLIST_DEBUG'}
Truth table50%1/2
A
A= $ENV{'PLIST_DEBUG'}
Aresult
11
00
17  
18 6 ▶__PACKAGE__->_run( @ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 6 6
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 Mac::PropertyList::ReadBinary - read binary property list files
25  
26 =head1 SYNOPSIS
27  
28 # use directly
29 use Mac::PropertyList::ReadBinary;
30  
31 my $parser = Mac::PropertyList::ReadBinary->new( $file );
32  
33 my $plist = $parser->plist;
34  
35 # use indirectly, automatically selects right reader
36 use Mac::PropertyList;
37  
38 my $plist = parse_plist_file( $file );
39  
40 =head1 DESCRIPTION
41  
42 This module is a low-level interface to the Mac OS X Property List
43 (plist) format. You probably shouldn't use this in
44 applications—build interfaces on top of this so you don't have to
45 put all the heinous multi-level object stuff where people have to look
46 at it.
47  
48 You can parse a plist file and get back a data structure. You can take
49 that data structure and get back the plist as XML (but not binary
50 yet). If you want to change the structure inbetween that's your
51 business. :)
52  
53 See L<Mac::PropertyList> for more details.
54  
55 =head2 Methods
56  
57 =over 4
58  
59 =item new( FILENAME | SCALAR_REF | FILEHANDLE )
60  
61 Opens the data source, doing the right thing for filenames,
62 scalar references, or a filehandle.
63  
64 =cut
65  
66 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
67 9 my( $class, $source ) = @_;
68  
69 9 my $self = bless { source => $source }, $class;
70  
71 9 $self->_read;
72  
73 9 $self;
74 }
75  
76 36 ▶sub _source { $_[0]->{source} }
Subroutine _source: called CC=1 SCAR=0.0
77 1739 ▶sub _fh { $_[0]->{fh} }
Subroutine _fh: called CC=1 SCAR=0.0
78 257 ▶sub _trailer { $_[0]->{trailer} }
Subroutine _trailer: called CC=1 SCAR=0.0
79 516 ▶sub _offsets { $_[0]->{offsets} }
Subroutine _offsets: called CC=1 SCAR=0.0
80 203 ▶sub _object_ref_size { $_[0]->_trailer->{ref_size} }
Subroutine _object_ref_size: called CC=1 SCAR=0.0
81  
82 =item plist
83  
84 Returns the C<Mac::PropertyList> data structure.
85  
86 =cut
87  
88 9 ▶sub plist { $_[0]->{parsed} }
Subroutine plist: called CC=1 SCAR=0.0
Pod documented
89  
90 9 ▶sub _object_size {
Subroutine _object_size: called CC=1 SCAR=0.0
91 $_[0]->_trailer->{object_count} * $_[0]->_trailer->{offset_size}
92 9 }
93  
94 9 ▶sub _read {
Subroutine _read: called CC=1 SCAR=0.0
95 9 my( $self, $thingy ) = @_;
96  
97 9 $self->{fh} = $self->_get_filehandle;
98 9 $self->_read_plist_trailer;
99  
100 9 $self->_get_offset_table;
101  
102 9 my $top = $self->_read_object_at_offset( $self->_trailer->{top_object} );
103  
104 9 $self->{parsed} = $top;
105 }
106  
107 13 ▶sub _get_filehandle {
Subroutine _get_filehandle: called CC=6 SCAR=18.2
108 13 my( $self, $thingy ) = @_;
109  
110 13 my $fh;
111  
112 13 ▶ if( ! ref $self->_source ) { # filename
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $self->_source) { } 5 8 13
elsif (ref $self->_source eq ref \'') { } 7 1 8
elsif (ref $self->_source) { } 1 0 1
113 5 ▶ open $fh, "<", $self->_source
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $self->_source or die "Could not open [@{[$self->_source];}]! $!" 4 1 5
114 1 or die "Could not open [@{[$self->_source]}]! $!";
115 }
116 elsif( ref $self->_source eq ref \ '' ) { # scalar ref
117 7 ▶ open $fh, "<", $self->_source or croak "Could not open file! $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $self->_source or croak("Could not open file! $!") 7 0 7
118 }
119 elsif( ref $self->_source ) { # filehandle
120 1 $fh = $self->_source;
121 }
122 else {
123 0 croak( 'No source to read from!' );
124 }
125  
126 12 $fh;
127 }
128  
129 9 ▶sub _read_plist_trailer {
Subroutine _read_plist_trailer: called CC=1 SCAR=0.0
130 9 my $self = shift;
131  
132 9 seek $self->_fh, -32, SEEK_END;
133  
134 9 my $buffer;
135 9 read $self->_fh, $buffer, 32;
136 9 my %hash;
137  
138 9 @hash{ qw( offset_size ref_size object_count top_object table_offset ) }
139 = unpack "x6 C C (x4 N)3", $buffer;
140  
141 9 $self->{trailer} = \%hash;
142 }
143  
144 9 ▶sub _get_offset_table {
Subroutine _get_offset_table: called CC=3 SCAR=11.5
145 9 my $self = shift;
146  
147 9 seek $self->_fh, $self->_trailer->{table_offset}, SEEK_SET;
148  
149 9 my $try_to_read = $self->_object_size;
150  
151 9 my $raw_offset_table;
152 9 my $read = read $self->_fh, $raw_offset_table, $try_to_read;
153  
154 9 ▶ croak "reading offset table failed!" unless $read == $try_to_read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read == $try_to_read 0 9 9
155  
156 9 my @offsets = unpack ["","C*","n*","(H6)*","N*"]->[$self->_trailer->{offset_size}], $raw_offset_table;
157  
158 9 $self->{offsets} = \@offsets;
159  
160 9 ▶ if( $self->_trailer->{offset_size} == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_trailer->{"offset_size"} == 3) 0 9 9
161 0 @offsets = map { hex } @offsets;
162 }
163 }
164  
165 516 ▶sub _read_object_at_offset {
Subroutine _read_object_at_offset: called CC=1 SCAR=0.0
166 516 my( $self, $offset ) = @_;
167  
168 516 seek $self->_fh, ${ $self->_offsets }[$offset], SEEK_SET;
169  
170 516 $self->_read_object;
171 }
172  
173 # # # # # # # # # # # # # #
174  
175 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 6 my %singletons = (
178 0 => undef,
179 8 => Mac::PropertyList::false->new(),
180 9 => Mac::PropertyList::true->new(),
181  
182 # 15 is also defined (as "fill") in the comments to Apple's
183 # implementation in CFBinaryPList.c but Apple's actual code has no
184 # support for it at all, either reading or writing, so it's
185 # probably not important to implement.
186 );
187  
188 my $type_readers = {
189 2 ▶ 0 => sub { # the odd balls
Subroutine __ANON__: called CC=2 SCAR=8.1
190 2 my( $self, $length ) = @_;
191  
192 2 ▶ return $singletons{ $length } if exists $singletons{ $length };
Branch: 1 branch
BranchTrueFalseTotal
if exists $singletons{$length} 2 0 2
193  
194 0 croak ( sprintf "Unknown type byte %02X\n", $length );
195 },
196  
197 207 ▶ 1 => sub { # integers
Subroutine __ANON__: called CC=6 SCAR=19.6
198 207 my( $self, $power_of_2 ) = @_;
199  
200 207 ▶ croak "Integer with <$power_of_2> bytes is not supported" if $power_of_2 > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $power_of_2 > 4 0 207 207
201  
202 207 my $byte_length = 1 << $power_of_2;
203  
204 207 my( $buffer, $value );
205 207 read $self->_fh, $buffer, $byte_length;
206  
207 207 my @formats = qw( C n N NN NNNN );
208 207 my @values = unpack $formats[$power_of_2], $buffer;
209  
210 207 ▶ if( $power_of_2 == 3 ) { # 64 bits
Branch: 2 branches
BranchTrueFalseTotal
if ($power_of_2 == 3) { } 28 179 207
elsif ($power_of_2 == 4) { } 0 179 179
211 28 my( $high, $low ) = @values;
212  
213 28 my $b = Math::BigInt->new($high)->blsft(32)->bior($low);
214 28 ▶ if( $b->bcmp(Math::BigInt->new(2)->bpow(63)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->bcmp("Math::BigInt"->new(2)->bpow(63)) >= 0) 16 12 28
215 16 $b -= Math::BigInt->new(2)->bpow(64);
216 }
217  
218 28 @values = ( $b->bstr );
219 }
220 elsif( $power_of_2 == 4 ) { # 128 bits
221 # 128 bits aren't part of the public API, but apparently
222 # they are out there.
223 0 my( $highest, $higher, $high, $low ) = @values;
224 0 my $b = Math::BigInt
225 ->new($highest)
226 ->blsft(32)->bior($higher)
227 ->blsft(32)->bior($high)
228 ->blsft(32)->bior($low);
229  
230 0 ▶ if( $b->bcmp(Math::BigInt->new(2)->bpow(127)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->bcmp("Math::BigInt"->new(2)->bpow(127)) >= 0) 0 0 0
231 0 $b -= Math::BigInt->new(2)->bpow(128);
232 }
233  
234 0 @values = ( $b->bstr );
235 }
236  
237 207 return Mac::PropertyList::integer->new( $values[0] );
238 },
239  
240 3 ▶ 2 => sub { # reals
Subroutine __ANON__: called CC=3 SCAR=11.1
241 3 my( $self, $length ) = @_;
242 3 ▶ croak "Real > 8 bytes" if $length > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $length > 3 0 3 3
243 3 ▶ croak "Bad length [$length]" if $length < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 2 0 3 3
244  
245 3 my $byte_length = 1 << $length;
246  
247 3 my( $buffer, $value );
248 3 read $self->_fh, $buffer, $byte_length;
249  
250 3 my @formats = qw( a a f> d> );
251 3 my @values = unpack $formats[$length], $buffer;
252  
253 3 return Mac::PropertyList::real->new( $values[0] );
254 },
255  
256 6 ▶ 3 => sub { # date
Subroutine __ANON__: called CC=2 SCAR=6.9
257 6 my( $self, $length ) = @_;
258 6 ▶ croak "Date != 8 bytes" if $length != 3;
Branch: 1 branch
BranchTrueFalseTotal
if $length != 3 0 6 6
259 6 my $byte_length = 1 << $length;
260  
261 6 my( $buffer, $value );
262 6 read $self->_fh, $buffer, $byte_length;
263  
264 6 my @values = unpack 'd>', $buffer;
265  
266 6 $self->{MLen} += 9;
267  
268 6 my $adjusted_time = POSIX::strftime(
269 "%Y-%m-%dT%H:%M:%SZ",
270 gmtime( 978307200 + $values[0])
271 );
272  
273 6 return Mac::PropertyList::date->new( $adjusted_time );
274 },
275  
276 1 ▶ 4 => sub { # binary data
Subroutine __ANON__: called CC=1 SCAR=0.0
277 1 my( $self, $length ) = @_;
278  
279 1 my( $buffer, $value );
280 1 read $self->_fh, $buffer, $length;
281  
282 1 return Mac::PropertyList::data->new( $buffer );
283 },
284  
285 293 ▶ 5 => sub { # utf8 string
Subroutine __ANON__: called CC=1 SCAR=0.0
286 293 my( $self, $length ) = @_;
287  
288 293 my( $buffer, $value );
289 293 read $self->_fh, $buffer, $length;
290  
291 293 $buffer = Encode::decode( 'ascii', $buffer );
292  
293 293 return Mac::PropertyList::string->new( $buffer );
294 },
295  
296 2 ▶ 6 => sub { # unicode string
Subroutine __ANON__: called CC=1 SCAR=0.0
297 2 my( $self, $length ) = @_;
298  
299 2 my( $buffer, $value );
300 2 read $self->_fh, $buffer, 2 * $length;
301  
302 2 $buffer = Encode::decode( "UTF-16BE", $buffer );
303  
304 2 return Mac::PropertyList::ustring->new( $buffer );
305 },
306  
307 4 ▶ 8 => sub { # UIDs
Subroutine __ANON__: called CC=1 SCAR=0.0
308 4 my( $self, $length ) = @_;
309  
310 4 my $byte_length = $length + 1;
311  
312 4 read $self->_fh, ( my $buffer ), $byte_length;
313  
314 4 my $value = unpack 'H*', $buffer;
315  
316 4 return Mac::PropertyList::uid->new( $value );
317 },
318  
319 39 ▶ a => sub { # array
Subroutine __ANON__: called CC=2 SCAR=7.0
320 39 my( $self, $elements ) = @_;
321  
322 39 my @objects = do {
323 39 my $buffer;
324 39 read $self->_fh, $buffer, $elements * $self->_object_ref_size;
325 39 ▶ unpack(
Branch: 1 branch
BranchTrueFalseTotal
$self->_object_ref_size == 1 ? : 39 0 39
326 ($self->_object_ref_size == 1 ? "C*" : "n*"), $buffer
327 );
328 };
329  
330 my @array =
331 39 map { $self->_read_object_at_offset( $objects[$_] ) }
332 0 .. $elements - 1;
333  
334 39 return Mac::PropertyList::array->new( \@array );
335 },
336  
337 25 ▶ d => sub { # dictionary
Subroutine __ANON__: called CC=3 SCAR=11.0
338 25 my( $self, $length ) = @_;
339  
340 25 my @key_indices = do {
341 25 my $buffer;
342 25 my $s = $self->_object_ref_size;
343 25 read $self->_fh, $buffer, $length * $self->_object_ref_size;
344 25 ▶ unpack(
Branch: 1 branch
BranchTrueFalseTotal
$self->_object_ref_size == 1 ? : 25 0 25
345 ($self->_object_ref_size == 1 ? "C*" : "n*"), $buffer
346 );
347 };
348  
349 25 my @objects = do {
350 25 my $buffer;
351 25 read $self->_fh, $buffer, $length * $self->_object_ref_size;
352 25 ▶ unpack(
Branch: 1 branch
BranchTrueFalseTotal
$self->_object_ref_size == 1 ? : 25 0 25
353 ($self->_object_ref_size == 1 ? "C*" : "n*"), $buffer
354 );
355 };
356  
357 my %dict = map {
358 25 my $key = $self->_read_object_at_offset($key_indices[$_])->value;
359 213 my $value = $self->_read_object_at_offset($objects[$_]);
360 213 ( $key, $value );
361 } 0 .. $length - 1;
362  
363 25 return Mac::PropertyList::dict->new( \%dict );
364 },
365 6 };
366  
367 582 ▶sub _read_object {
Subroutine _read_object: called CC=5 SCAR=16.1
368 582 my $self = shift;
369 582 my $buffer;
370  
371 582 ▶ croak "read() failed while trying to get type byte! $!"
Branch: 1 branch
BranchTrueFalseTotal
unless read($self->_fh, $buffer, 1) == 1 0 582 582
372 unless read( $self->_fh, $buffer, 1) == 1;
373  
374 582 my $length = unpack( "C*", $buffer ) & 0x0F;
375 582 $buffer = unpack "H*", $buffer;
376 582 my $type = substr $buffer, 0, 1;
377  
378 582 ▶ $length = $self->_read_object->value if $type ne "0" && $length == 15;
Branch: 1 branch
BranchTrueFalseTotal
if $type ne 0 and $length == 15 66 516 582
Condition100%3/3
$type ne 0 and $length == 15
!ll&&!rl&&r
251466
MC/DC100%2/2
$type ne 0 and $length == 15
$type ne 0 $length == 15
Truth table100%3/3
A and B
A= $type ne 0B= $length == 15
ABresult
0X0
100
111
379  
380 582 my $sub = $type_readers->{ $type };
381 582 my $result = eval { $sub->( $self, $length ) };
382 582 ▶ croak "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 582 582
383  
384 582 return $result;
385 }
386  
387 }
388  
389 =back
390  
391 =head1 SEE ALSO
392  
393 Some of the ideas are cribbed from CFBinaryPList.c
394  
395 http://opensource.apple.com/source/CF/CF-550/CFBinaryPList.c
396  
397 =head1 SOURCE AVAILABILITY
398  
399 This project is in Github:
400  
401 https://github.com/briandfoy/mac-propertylist.git
402  
403 =head1 CREDITS
404  
405 =head1 AUTHOR
406  
407 brian d foy, C<< <briandfoy@pobox.com> >>
408  
409 Tom Wyant added support for UID types.
410  
411 =head1 COPYRIGHT AND LICENSE
412  
413 Copyright © 2004-2026, brian d foy <briandfoy@pobox.com>. All rights reserved.
414  
415 This program is free software; you can redistribute it and/or modify
416 it under the terms of the Artistic License 2.0.
417  
418 =cut
419  
420 6 "See why 1984 won't be like 1984";
« PropertyList.pm ↑ Summary