Plist/BinaryWriter.pm

filter: Statementstmt 99.5% 199 / 200 Branchbran 97.5% 39 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 29 / 29 Podpod 100% 22 / 22 totaltotal 99.3% 289 / 291 CC 28 SCAR 11.2
CC
28
Cov
99%
write_fh
6.0
count
5.0
power
5.0
CRAP 3.1
« Plist/Foundation/ToDoAlarm.pm ↑ Summary Plist/Foundation/NSArray.pm »
1 =head1 NAME
2  
3 Data::Plist::BinaryWriter - write binary property lists
4 from Perl data structures
5  
6 =head1 SYNOPSIS
7  
8 # Create new
9 my $write = Data::Plist::BinaryWriter->new();
10  
11 # Writing to a string ($ret is binary output)
12 my $ret = $write->write($data);
13  
14 # Writing to a file C<$filename>
15 $write->write($filename, $data);
16  
17 =head1 DESCRIPTION
18  
19 C<Data::Plist::BinaryWriter> takes perl data structures,
20 serializes them (see L<Data::Plist/SERIALIZED DATA>) and
21 recursively writes to a given filehandle in Apple's binary
22 property list format.
23  
24 =cut
25  
26 package Data::Plist::BinaryWriter;
27  
28 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 ▶use base qw/Data::Plist::Writer/;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 =head1 METHODS
37  
38 =head2 write_fh $fh, $data
39  
40 Takes a perl data structure C<$data>, serializes it (see
41 L<Data::Plist/SERIALIZED DATA>) and writes it to the given
42 filehandle C<$fh> in Apple's binary property list format.
43  
44 The format starts with "bplist00" and contains a 32-byte
45 trailer. The 32-byte trailer consists of the size of the
46 offset objects in the offset table, the size of the indices
47 of the offset table, the number of objects in the binary
48 file, the index of top object in the binary file and the
49 offset table offset.
50  
51 =cut
52  
53 31 ▶sub write_fh {
Subroutine write_fh: called CC=6 SCAR=17.9
Pod documented
54 31 my $self = shift;
55 31 ▶ $self = $self->new() unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 29 31
56  
57 31 my ( $fh, $object ) = @_;
58 31 ▶ $object = $self->serialize($object) if ( $self->{serialize} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"serialize"} 20 11 31
59 31 binmode $fh;
60 31 $self->{fh} = $fh;
61 31 $self->{index} = [];
62 31 $self->{size} = $self->count($object);
63 31 $self->{objcache} = {};
64 31 ▶ if ( $self->{size} >= 2**8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'size'} >= 256.0) { } 1 30 31
65 1 $self->{refsize} = 2;
66 } else {
67 30 $self->{refsize} = 1;
68 }
69 31 print $fh "bplist00";
70 31 my $top_index = $self->dispatch($object);
71 29 my $offset_size = $self->bytes( $self->{index}->[-1] );
72 29 my $table_offset = tell $fh;
73 29 for ( @{ $self->{index} } ) {
74 373 my $value = pack $self->pack_in( $offset_size - 1 ), $_;
75 373 ▶ if ( $offset_size == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_size == 3) 6 367 373
76 6 $value = substr $value, -3;
77 }
78 373 print $fh $value;
79 }
80 29 print $fh ( pack "x6CC", ($offset_size), $self->{refsize} );
81 29 print $fh ( pack "x4N", scalar keys %{ $self->{objcache} } );
82 29 print $fh ( pack "x4N", $top_index );
83 29 print $fh ( pack "x4N", $table_offset );
84 29 close $fh;
85 29 return 1;
86 }
87  
88 =head2 dispatch $data
89  
90 Takes serialized data structure C<$data> (see
91 L<Data::Plist/SERIALIZED DATA>) and checks its type. Checks
92 the object against previously written objects. If no match
93 is found, calls the appropriate write_ method. Returns the
94 index into the offset table of the offset object that
95 points to the data's position in the binary file.
96  
97 =cut
98  
99 383 ▶sub dispatch {
Subroutine dispatch: called CC=3 SCAR=11.0
Pod documented
100 383 my $self = shift;
101 383 my ($arrayref) = @_;
102 383 my $type = $arrayref->[0];
103 383 my $method = "write_" . $type;
104 383 local $Storable::canonical = 1;
105 383 my $digest = eval { Digest::MD5::md5_hex( Storable::freeze($arrayref) ) };
106 383 ▶ die "Can't $method" unless $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($method) 1 382 383
107 $self->{objcache}{$digest} = $self->$method( $arrayref->[1] )
108 382 ▶ unless ( exists $self->{objcache}{$digest} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"objcache"}{$digest} 380 2 382
109 379 return $self->{objcache}{$digest};
110 }
111  
112 =head2 make_type $type, $length
113  
114 Takes a string representing the object's type C<$type> and an
115 integer indicating its size C<$length>. Returns their binary
116 representation.
117  
118 Each object in the binary file is preceded by a byte - the
119 higher nybble denoting its type and the lower its size. For
120 objects whose size is equal to or great than 15, the lower
121 byte contains an f and an integer object is added right
122 after the first byte containing the object's actual size.
123  
124 =cut
125  
126 69 ▶sub make_type {
Subroutine make_type: called CC=2 SCAR=6.9
Pod documented
127 69 my $self = shift;
128 69 my ( $type, $len ) = @_;
129 69 my $ans = "";
130  
131 69 my $optint = "";
132 69 ▶ if ( $len < 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 15) { } 58 11 69
133 58 $type .= sprintf( "%x", $len );
134 } else {
135 11 $type .= "f";
136 11 my $optlen = $self->power($len);
137 11 $optint = pack( "C" . $self->pack_in($optlen), hex( "1" . $optlen ),
138 $len );
139 }
140 69 $ans = pack( "H*", $type ) . $optint;
141  
142 69 return $ans;
143 }
144  
145 =head2 write_integer $int, $type
146  
147 Takes an integer C<$int> and an optional type C<$type>
148 (used for writing UIDs, since they're essentially the
149 same). Returns the index into the offset table of the
150 offset object that points to the integer's location in the
151 binary file.
152  
153 =cut
154  
155 312 ▶sub write_integer {
Subroutine write_integer: called CC=4 SCAR=13.9
Pod documented
156 312 my $self = shift;
157 312 my ( $int, $type ) = @_;
158 312 my $fmt;
159 my $obj;
160  
161 312 ▶ unless ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 307 5 312
162 307 $type = "1";
163 }
164 312 my $len = $self->power($int);
165  
166 312 ▶ if ( $len == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 3) { } 3 309 312
167 3 ▶ if ( $int < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int < 0) 1 2 3
168 1 $int += Math::BigInt->new(2)->bpow(64);
169 }
170 3 my $hw = Math::BigInt->new($int);
171 3 $hw->brsft(32);
172 3 my $lw = Math::BigInt->new($int);
173 3 $lw->band( Math::BigInt->new("4294967295") );
174  
175 3 $obj
176 = $self->make_type( $type, $len )
177 . pack( "N", $hw )
178 . pack( "N", $lw );
179 } else {
180 309 $fmt = $self->pack_in($len);
181 309 $obj = pack( "C" . $fmt, hex( $type . $len ), $int );
182 }
183 311 return $self->binary_write($obj);
184 }
185  
186 =head2 write_string $string
187  
188 Takes a string C<$string> and returns the index into the offset table
189 of the offset object that points to its location in the binary file.
190 It is encoded in the file using UTF-8.
191  
192 =cut
193  
194 35 ▶sub write_string {
Subroutine write_string: called CC=1 SCAR=0.0
Pod documented
195 35 my $self = shift;
196 35 my ($string) = @_;
197 35 my $type = $self->make_type( "5", length($string) );
198 35 my $obj = $type . $string;
199 35 return $self->binary_write($obj);
200 }
201  
202 =head2 write_ustring $ustring
203  
204 Takes a string C<$ustring> and returns the index into the offset table
205 of the offset object that points to its location in the binary file.
206  
207 While C<ustrings> are technically supposed to be stored in UTF-16,
208 there is no known reason for them to not be written as UTF-8 encoded
209 C<string>s instead; thus, for simplicity, all C<ustring>s are written
210 as C<string>s.
211  
212 =cut
213  
214 1 ▶sub write_ustring {
Subroutine write_ustring: called CC=1 SCAR=0.0
Pod documented
215 1 my $self = shift;
216 1 return $self->write_string(@_);
217 }
218  
219 =head2 write_dict $dict
220  
221 Takes a hash reference C<$dict> and recursively processes
222 each of its keys and values. Stores indices into the offset
223 table of the offset objects pointing to its keys and values
224 in the binary file. Returns the index into the offset table
225 of the offset object that points to its location in the
226 binary file.
227  
228 =cut
229  
230 12 ▶sub write_dict {
Subroutine write_dict: called CC=3 SCAR=11.0
Pod documented
231 12 my $self = shift;
232 12 my $fh = $self->{fh};
233 12 my ($hash) = @_;
234 12 my @keys;
235 my @values;
236 12 for my $key ( keys %$hash ) {
237 19 push @keys, $self->dispatch( [ "string", $key ] );
238 19 push @values, $self->dispatch( $hash->{$key} );
239 }
240 10 my $current = tell $self->{fh};
241 10 print $fh $self->make_type( "d", scalar keys(%$hash) );
242 10 my $packvar = $self->pack_in( $self->{refsize} - 1 );
243 10 print $fh pack $packvar, $_ for @keys, @values;
244 10 push @{ $self->{index} }, $current;
245 10 return ( @{ $self->{index} } - 1 );
246 }
247  
248 =head2 write_array $array
249  
250 Take an array reference C<$array> and recursively processes
251 its contents. Stores the indices into the offset table of
252 the offset objects pointing to its value. Returns the index
253 into the offset table of the offset object that points to
254 its location in the binary file.
255  
256 =cut
257  
258 9 ▶sub write_array {
Subroutine write_array: called CC=3 SCAR=11.0
Pod documented
259 9 my $self = shift;
260 9 my $fh = $self->{fh};
261 9 my ($array) = @_;
262 9 my $size = @$array;
263 9 my @values;
264 9 for (@$array) {
265 314 push @values, $self->dispatch($_);
266 }
267 9 my $current = tell $self->{fh};
268 9 print $fh $self->make_type( "a", $size );
269 9 my $packvar = $self->pack_in( $self->{refsize} - 1 );
270 9 print $fh pack $packvar, $_ for @values;
271 9 push @{ $self->{index} }, $current;
272 9 return ( @{ $self->{index} } - 1 );
273 }
274  
275 =head2 write_UID $id
276  
277 Takes a UID C<$id> and returns the index into the offset
278 table of the offset object that points to its location in
279 the binary file. Passes the UID off to L</write_integer> for
280 actual writing, since they're processed in the same manner,
281 simply with different types.
282  
283 =cut
284  
285 5 ▶sub write_UID {
Subroutine write_UID: called CC=1 SCAR=0.0
Pod documented
286 5 my $self = shift;
287 5 my ($id) = @_;
288 5 return $self->write_integer( $id, "8" );
289 }
290  
291 =head2 write_real $real, $type
292  
293 Takes a float C<$real> and an optional type C<$type>
294 (used for writing dates, since they're essentially the
295 same), and returns the index into the
296 offset table of the offset object that points to its
297 location in the binary file. The bytes of the float are
298 packed in reverse.
299  
300 =cut
301  
302 3 ▶sub write_real {
Subroutine write_real: called CC=2 SCAR=6.9
Pod documented
303 3 my $self = shift;
304 3 my ($float, $type) = @_;
305 3 ▶ unless ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 2 1 3
306 2 $type = "2";
307 }
308 3 my $obj = $self->make_type( $type, 3 ) . reverse( pack( "d", $float ) );
309 3 return $self->binary_write($obj);
310 }
311  
312 =head2 write_date $date
313  
314 Takes a date C<$date> and returns the index into the offset
315 table of the offset object that points to its location in
316 the binary file. Passes the date off to L</write_real> for
317 actual writing, since they're processed in the same manner,
318 simply with different types.
319  
320 =cut
321  
322 1 ▶sub write_date {
Subroutine write_date: called CC=1 SCAR=0.0
Pod documented
323 1 my $self = shift;
324 1 my ($date) = @_;
325 1 return $self->write_real($date, "3");
326 }
327  
328 =head2 write_null $null
329  
330 Takes a null C<$null> and passes it to L</write_misc>, along
331 with an integer indicating what type of misc it is. The
332 null belongs to the misc category (see L</write_misc>).
333  
334 =cut
335  
336 1 ▶sub write_null {
Subroutine write_null: called CC=1 SCAR=0.0
Pod documented
337 1 my $self = shift;
338 1 return $self->write_misc( 0 );
339 }
340  
341 =head2 write_false $false
342  
343 Takes a false C<$false> and passes it to L</write_misc>, along with an
344 integer indicating what type of misc it is. The false
345 belongs to the misc category (see L</write_misc>).
346  
347 =cut
348  
349 1 ▶sub write_false {
Subroutine write_false: called CC=1 SCAR=0.0
Pod documented
350 1 my $self = shift;
351 1 return $self->write_misc( 8 );
352 }
353  
354 =head2 write_true $true
355  
356 Takes a true C<$true> and passes it to L</write_misc>, along with an
357 integer indicating what type of misc it is. The true
358 belongs to the misc category (see L</write_misc>).
359  
360 =cut
361  
362 1 ▶sub write_true {
Subroutine write_true: called CC=1 SCAR=0.0
Pod documented
363 1 my $self = shift;
364 1 return $self->write_misc( 9 );
365 }
366  
367 =head2 write_fill $fill
368  
369 Takes a fill C<$fill> and passes it to L</write_misc>, along with an
370 integer indicating what type of misc it is. The fill
371 belongs to the misc category (see L</write_misc>).
372  
373 =cut
374  
375 1 ▶sub write_fill {
Subroutine write_fill: called CC=1 SCAR=0.0
Pod documented
376 1 my $self = shift;
377 1 return $self->write_misc( 15 );
378 }
379  
380 =head2 write_misc $type
381  
382 Takes an integer indicating an object belonging to the misc
383 category C<$type> (false, null, true or fill) and returns
384 the index into the offset table of the offset object that
385 points to its location in the file.
386  
387 Miscs are a group of data types not easily represented in Perl, and
388 they are written with the only header byte containing a 0 to indicate
389 that they are a misc and their misc type.
390  
391 =cut
392  
393 4 ▶sub write_misc {
Subroutine write_misc: called CC=1 SCAR=0.0
Pod documented
394 4 my $self = shift;
395 4 my ( $type ) = @_;
396 4 my $obj = $self->make_type( "0", $type );
397 4 return $self->binary_write($obj);
398 }
399  
400 =head2 write_data $data
401  
402 Takes some binary data C<$data> and returns the index into the
403 offset table of the offset object that points to its
404 location in the file. Doesn't attempt to process the data
405 at all.
406  
407 =cut
408  
409 5 ▶sub write_data {
Subroutine write_data: called CC=1 SCAR=0.0
Pod documented
410 5 my $self = shift;
411 5 my ($data) = @_;
412 4 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
413 5 my $len = length $data;
414 5 my $obj = $self->make_type( 4, $len ) . $data;
415 5 return $self->binary_write($obj);
416 }
417  
418 =head2 count $data
419  
420 Recursively counts the number of objects in a serialized
421 data structure C<$data>. Does not take into account
422 duplicates, so this number might be slightly higher than
423 the number of objects that is indicated in the 32-byte
424 trailer.
425  
426 =cut
427  
428 380 ▶sub count {
Subroutine count: called CC=5 SCAR=16.1
Pod documented
429  
430 # this might be slightly over, since it doesn't take into account duplicates
431 380 my $self = shift;
432 380 my ($arrayref) = @_;
433 380 my $type = $arrayref->[0];
434 380 my $value;
435 380 ▶ if ( $type eq "dict" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'dict') { } 14 366 380
elsif ($type eq 'array') { } 9 357 366
436 14 my @keys = ( keys %{ $arrayref->[1] } );
437 14 $value = 1 + @keys;
438 14 $value += $_ for map { $self->count( $arrayref->[1]->{$_} ) } @keys;
439 14 return $value;
440 } elsif ( $type eq "array" ) {
441 9 $value = 1;
442 9 $value += $_ for map { $self->count($_) } @{ $arrayref->[1] };
443 9 return $value;
444 } else {
445 357 return 1;
446 }
447 }
448  
449 =head2 binary_write $obj
450  
451 Does the actual writing to the binary file. Takes some
452 binary data C<$obj> and writes it to the filehandle. Also
453 adds the location of the binary data to the offset table
454 and returns the index into the offset table of the current
455 object.
456  
457 =cut
458  
459 358 ▶sub binary_write {
Subroutine binary_write: called CC=1 SCAR=0.0
Pod documented
460 358 my $self = shift;
461 358 my $fh = $self->{fh};
462 358 my ($obj) = @_;
463 358 my $current = tell $self->{fh};
464 358 print $fh $obj;
465 358 push @{ $self->{index} }, $current;
466 358 return ( @{ $self->{index} } - 1 );
467 }
468  
469 =head2 power $int
470  
471 Calculates the number of bytes necessary to encode an
472 integer C<$int>. Returns a power of 2 indicating the number
473 of bytes.
474  
475 =cut
476  
477 323 ▶sub power {
Subroutine power: called CC=5 SCAR=16.1
Pod documented
478 323 my $self = shift;
479 323 my ($int) = @_;
480 323 ▶ if ( $int > 4294967295 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($int > 4294967295) { } 2 321 323
elsif ($int > 65535) { } 4 317 321
elsif ($int > 255) { } 48 269 317
elsif ($int < 0) { } 1 268 269
481 2 return 3;
482  
483 # actually refers to 2^3 bytes
484 } elsif ( $int > 65535 ) {
485 4 return 2;
486  
487 # actually refers to 2^2 bytes
488 } elsif ( $int > 255 ) {
489 48 return 1;
490  
491 # I'm sure you see the trend
492 } elsif ( $int < 0 ) {
493 1 return 3;
494 } else {
495 268 return 0;
496 }
497 }
498  
499 =head2 bytes $int
500  
501 Calculates the number of bytes necessary to encode an
502 integer C<$int>. Returns the actual number of bytes.
503  
504 =cut
505  
506 29 ▶sub bytes {
Subroutine bytes: called CC=4 SCAR=14.0
Pod documented
507 29 my $self = shift;
508 29 my ($int) = @_;
509 29 ▶ if ( $int >= 2**24 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($int >= 16777216.0) { } 0 29 29
elsif ($int >= 65536.0) { } 2 27 29
elsif ($int >= 256) { } 2 25 27
510 0 return 4;
511  
512 # actually refers to 4 bytes
513 } elsif ( $int >= 2**16 ) {
514 2 return 3;
515  
516 # actually refers to 3 bytes
517 } elsif ( $int >= 256 ) {
518 2 return 2;
519  
520 # I'm sure you see the trend
521 } else {
522 25 return 1;
523 }
524 }
525  
526 =head2 pack_in $int
527  
528 Takes either a power of 2 or a number of bytes C<$int> and
529 returns the format pack() needs for encoding.
530  
531 =cut
532  
533 712 ▶sub pack_in {
Subroutine pack_in: called CC=1 SCAR=0.0
Pod documented
534  
535 # can be used with powers or bytes
536 712 my $self = shift;
537 712 my ($int) = @_;
538 712 my $fmt = [ "C", "n", "N", "N" ]->[$int];
539 712 return $fmt;
540 }
541  
542 4 1;
« Plist/Foundation/ToDoAlarm.pm ↑ Summary Plist/Foundation/NSArray.pm »