blib/lib/Net/DAAP/DMAP.pm

filter: Statementstmt 46.2% 79 / 171 Branchbran 30.0% 21 / 70 Conditioncond 26.6% 4 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 50.0% 9 / 18 Podpod 0.0% 0 / 12 totaltotal 38.8% 115 / 296 CC 47 SCAR 34.2
CC
47
Cov
41%
update_content_codes
110.0
dmap_to_hash_ref
30.0
dmap_to_array_ref
30.0
CRAP 30.5
↑ Summary
1 package Net::DAAP::DMAP;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 our $NOISY = 0;
4 1 our $VERSION = '1.27';
5  
6 =pod
7  
8 =head1 NAME
9  
10 Net::DAAP::DMAP - Perl module for reading and writing DAAP structures
11  
12 =head2 SYNOPSIS
13  
14 use Net::DAAP::DMAP qw(:all);
15  
16 $hash_ref = dmap_to_hash_ref($dmap); # crude
17 $array_ref = dmap_to_array_ref($dmap); # crude
18  
19 $array_ref = dmap_unpack($dmap); # knows about data types
20 $node = dmap_seek($array_ref, $path);
21  
22 $flattened = dmap_flatten($array_ref); # convert to path = data formta
23 $flat_list = dmap_flat_list($array_ref); # convert to [ path, data ] format
24 $xml = dmap_to_xml($dmap); # convert to XML fragment
25 $dmap = dmap_pack($dmap); # convert to DMAP packet
26 update_content_codes($unpacked_content_codes_response);
27  
28 =head1 DESCRIPTION
29  
30 =head2 WARNING!
31  
32 Until 2.0, I reserve the right to change the interface. In
33 particular, I think C<dmap_flatten>, C<dmap_to_hash_ref>, and
34 C<dmap_to_array_ref> are likely to disappear. And I suspect the hive
35 brain of Perl can come up with a better data structure than I have.
36  
37 =head2 Back to the Description
38  
39 A DMAP structure is a binary record used in Apple's DAAP protocol. A
40 DMAP structure may contain other DMAP structures. Fields in a DMAP
41 structure are identified by a short name ("msdc"). The short name is
42 what's in the binary record, but a content codes list gives a long
43 name ("dmap.databasescount") and a data type for the record (32-bit
44 integer).
45  
46 A parsed DMAP structure is built out of arrays. For example:
47  
48 [
49 [
50 'dmap.loginresponse',
51 [
52 [
53 'dmap.status',
54 200
55 ],
56 [
57 'dmap.sessionid',
58 2393
59 ]
60 ]
61 ]
62 ]
63  
64 (C<dmap_unpack> returns this kind of structure)
65  
66 There are two rules here: a field is wrapped in an array, and
67 a container's values are wrapped in an array. So the structure
68 is programmatically built as:
69  
70 $status_field = [ 'dmap.status', 200 ];
71 $session_id_field = [ 'dmap.sessionid', 2393 ];
72 $response_value = [ $status_field, $session_id_field ];
73 $login_response_field = [ 'dmap.loginresponse', $response_value ];
74 $entire_response = [ $login_response_field ];
75  
76 The outer array is necessary because not every response has only one
77 top-level container as this does.
78  
79 In XML you'd write the response as:
80  
81 <dmap.loginresponse>
82 <dmap.status>200</dmap.status>
83 <dmap.sessionid>2393</dmap.sessionid>
84 </dmap.loginresponse>
85  
86 This is what C<dmap_to_xml> returns.
87  
88 A much more convenient structure for representing this data would
89 be:
90  
91 {
92 'dmap.loginresponse' => {
93 { 'dmap.status' => 200,
94 'dmap.sessionid' => 2393,
95 },
96 }
97  
98 This is the output of C<dmap_to_hash_ref>, but beware! This isn't
99 suitable for every response. The hash is indexed by field name and a
100 structure may contain many elements of the same name. For example,
101 requesting the content codes list gives you a list of records that
102 have the field name C<dmap.dictionary>.
103  
104 The array structure returned by C<dmap_to_array_ref> is complex, but
105 the C<dmap_seek> function makes it easier. This takes a structure and
106 a path expressed as a slash-separated list of field names:
107  
108 dmap.loginresponse/dmap.sessionid
109  
110 The return value is the the value of the first C<dmap.sessionid> found
111 in the first C<dmap.loginresponse> structure. In the case of the
112 sample record above, it would be 2393.
113  
114 Another way to handle these complex arrays is to C<dmap_flatten> them.
115 This returns an array of "I<path> = value" lines, where I<path> is
116 a slash-separated path. For example:
117  
118 [
119 '/dmap.loginresponse/dmap.status = 200',
120 '/dmap.loginresponse/dmap.sessionid = 2393'
121 ]
122  
123 You can use C<grep> and regexps to find data if that's the way your
124 mind works.
125  
126 C<dmap_flatten> has a similar looking cousin called C<dmap_flat_list>,
127 which returns an array of "I<path> => I<value>" pairs. For example:
128  
129  
130 [
131 '/dmap.loginresponse/dmap.status' => 200,
132 '/dmap.loginresponse/dmap.sessionid' => 2393,
133 ]
134  
135 You can then turn this into a hash (which may of course lose you the
136 first elements), or iterate over it in pairs, if that's easier.
137  
138 You can, but don't have to, update the tables of field names ("content
139 codes") and data types. DAAP offers a request that returns a packet
140 of content codes. Feed that packet to C<update_content_codes>.
141  
142 =head2 Implementation Details
143  
144 It's all implementation details. Here are the various data types.
145  
146 1, 3, 5, 7 = ints, size 8,16,32,64 bit
147 9 = string, 10 = time_t-style time
148 11 = version (two 16-bit ints, I think)
149 12 = container
150  
151 This uses Math::BigInt for 64-bit quantities, as not every platform
152 has 64-bit int support available.
153  
154 There's no support for types 2, 4, 6, 8 yet because nobody'd found
155 examples of them in the field: are they endian changes, or signedness
156 changes. The assumption is that all numbers are unsigned (why allow
157 the possibility of a negative number of songs?).
158  
159 =head1 AUTHOR
160  
161 Nathan Torkington, <nathan AT torkington.com>. For support, join the
162 DAAP developers mailing list by sending mail to <daap-dev-subscribe AT
163 develooper.com>.
164  
165 Richard Clamp <richardc@unixbeard.net> is the current maintainer, and
166 took over in July 2004.
167  
168 =cut
169  
170 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 1 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
173  
174 1 our @ISA = qw(Exporter);
175 1 our @EXPORT_OK = qw(dmap_to_hash_ref dmap_to_array_ref update_content_codes
176 dmap_unpack dmap_to_xml dmap_seek dmap_flatten dmap_flat_list dmap_pack );
177 1 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
178  
179 1 our $Types;
180 1 my %Type_To_Unpack;
181 my $Container_Type;
182  
183 # initialize the types and their unpack() equivalents
184 1 init();
185  
186 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
187 1 local $/;
188 1 $Types = eval <DATA>;
189  
190 1 $Container_Type = 12;
191  
192 1 %Type_To_Unpack = (
193 1 => 'c',
194 3 => 'n',
195 5 => 'N',
196 7 => 'Q',
197 9 => 'a*', # utf-8 encoded
198 10 => 'N',
199 11 => 'nn',
200 42 => 'a*', # this is a local invention - 9 is
201 # getting handled as utf-8, but for
202 # dpap.picturedata that would be
203 # bad m'kay
204 );
205 }
206  
207 0 ▶sub dmap_to_hash_ref {
Subroutine dmap_to_hash_ref: not called CC=5 SCAR=34.0
Pod undocumented
208 0 my $buf = shift;
209 0 my %tags;
210  
211 0 ▶ while (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) 0 0 0
212 0 my ($tag, $len) = unpack("a4N", $buf);
213 0 ▶ if (!defined($len) or length $buf < 8+$len) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $len or length $buf < 8 + $len) 0 0 0
Condition0%0/3
not defined $len or length $buf < 8 + $len
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $len or length $buf < 8 + $len
not defined $len length $buf < 8 + $len
Truth table0%0/3
not(A) or B
A= not defined $lenB= length $buf < 8 + $len
ABresult
1X1
011
000
214 0 return;
215 }
216 0 my $data = substr($buf, 8, $len);
217 # try to unpack--if we can, assume it was a container
218 0 my $data2 = dmap_to_hash_ref($data);
219 0 ▶ $tags{$tag} = $data2 ? $data2 : $data;
Branch: 1 branch
BranchTrueFalseTotal
$data2 ? : 0 0 0
220 0 substr($buf, 0, 8+$len) = '';
221 }
222 0 return \%tags;
223 }
224  
225 0 ▶sub dmap_flatten {
Subroutine dmap_flatten: not called CC=1 SCAR=6.9
Pod undocumented
226 0 my $struct = shift;
227 0 my $arrayref = [];
228  
229 0 flatten_traverse($arrayref, "", $struct);
230 0 return $arrayref;
231 }
232  
233 0 ▶sub flatten_traverse {
Subroutine flatten_traverse: not called CC=4 SCAR=30.0
Pod undocumented
234 0 my ($array_ref, $prefix, $struct) = @_;
235  
236 0 foreach my $ref (@$struct) {
237 0 ▶ for (my $i=0; $i < @$ref; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$ref) 0 0 0
238 0 my ($tag, $data) = ($ref->[$i], $ref->[$i+1]);
239  
240 0 ▶ if (ref $data eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
241 0 flatten_traverse($array_ref, "$prefix/$tag", $data);
242 } else {
243 0 push @$array_ref, "$prefix/$tag = $data";
244 }
245 }
246 }
247 }
248  
249 0 ▶sub dmap_flat_list {
Subroutine dmap_flat_list: not called CC=1 SCAR=6.9
Pod undocumented
250 0 return @{ flat_list_traverse([], "", shift) };
251 }
252  
253 0 ▶sub flat_list_traverse {
Subroutine flat_list_traverse: not called CC=4 SCAR=30.0
Pod undocumented
254 0 my ($list, $prefix, $struct) = @_;
255 0 foreach my $ref (@$struct) {
256 0 ▶ for (my $i=0; $i < @$ref; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$ref) 0 0 0
257 0 my ($tag, $data) = ($ref->[$i], $ref->[$i+1]);
258  
259 0 ▶ if (ref $data eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
260 0 flat_list_traverse($list, "$prefix/$tag", $data);
261 } else {
262 0 push @$list, "$prefix/$tag", $data;
263 }
264 }
265 }
266 0 return $list;
267 }
268  
269  
270 120 ▶sub dmap_unpack {
Subroutine dmap_unpack: called CC=8 SCAR=20.8
Pod undocumented
271 120 my $buf = shift;
272 120 my @tags;
273  
274 120 ▶ while (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) 487 120 607
275 487 my ($tag, $len) = unpack("a4N", $buf);
276 487 my $data = substr($buf, 8, $len);
277 487 substr($buf, 0, 8+$len) = '';
278 487 my $type = $Types->{$tag}{TYPE};
279 487 ▶ unless ($type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 1 486 487
280 1 ▶ carp "'$tag' unknown, can't unpack" if $NOISY;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::DAAP::DMAP::NOISY 0 1 1
281 1 next;
282 }
283  
284 486 ▶ if ($type == 12) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 12) { } 113 373 486
elsif ($type == 7) { } 9 364 373
285 113 $data = dmap_unpack($data);
286 } elsif ($type == 7) {
287 9 my ($n1, $n2) = unpack("N2", $data);
288 9 $data = new Math::BigInt(new Math::BigInt($n1)->blsft(32));
289 9 $data += $n2;
290 9 $data = "$data";
291 } else {
292 364 $data = unpack($Type_To_Unpack{$type}, $data);
293 }
294 # type 9 is really utf-8 encoded, so if we can, show that it is
295 486 ▶ if ($type == 9 && eval { require Encode; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 9 and eval { do { require Encode; 1 } }) 207 279 486
Condition66%2/3
$type == 9 and eval { do { require Encode; 1 } }
!ll&&!rl&&r
2790207
MC/DC50%1/2
$type == 9 and eval { do { require Encode; 1 } }
$type == 9 eval { do { require Encode; 1 } }
Truth table66%2/3
A and B
A= $type == 9B= eval { do { require Encode; 1 } }
ABresult
0X0
100
111
296 207 $data = Encode::decode('utf-8', $data);
297 }
298 486 push @tags, [ $Types->{$tag}{NAME}, $data ];
299 }
300  
301 120 return \@tags;
302 }
303  
304 0 ▶sub dmap_to_xml {
Subroutine dmap_to_xml: not called CC=3 SCAR=24.8
Pod undocumented
305 0 my $buf = shift;
306 0 my $xml = '';
307  
308 0 ▶ while (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) 0 0 0
309 0 my ($tag, $len) = unpack("a4N", $buf);
310 0 my $data = substr($buf, 8, $len);
311 0 my $type = $Types->{$tag}{TYPE};
312  
313 0 ▶ if ($type == 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 12) { } 0 0 0
314 0 $data = dmap_to_xml($data);
315 } else {
316 0 $data = unpack($Type_To_Unpack{$type}, $data);
317 }
318 0 $xml .= sprintf("<%s>\n %s\n</%s>\n", $tag, $data, $tag);
319 0 substr($buf, 0, 8+$len) = '';
320 }
321 0 return $xml;
322 }
323  
324 0 ▶sub dmap_to_array_ref {
Subroutine dmap_to_array_ref: not called CC=5 SCAR=34.0
Pod undocumented
325 0 my $buf = shift;
326 0 my @tags;
327  
328 0 ▶ while (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) 0 0 0
329 0 my ($tag, $len) = unpack("a4N", $buf);
330 0 ▶ if (!defined($len) or length $buf < 8+$len) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $len or length $buf < 8 + $len) 0 0 0
Condition0%0/3
not defined $len or length $buf < 8 + $len
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $len or length $buf < 8 + $len
not defined $len length $buf < 8 + $len
Truth table0%0/3
not(A) or B
A= not defined $lenB= length $buf < 8 + $len
ABresult
1X1
011
000
331 0 return;
332 }
333 0 my $data = substr($buf, 8, $len);
334 # try to unpack, assume it was a container if it succeeded
335 0 my $data2 = dmap_to_array_ref($data);
336 0 ▶ push @tags, [ $tag, $data2 ? $data2 : $data ];
Branch: 1 branch
BranchTrueFalseTotal
$data2 ? : 0 0 0
337 0 substr($buf, 0, 8+$len) = '';
338 }
339 0 return \@tags;
340 }
341  
342 0 ▶sub dmap_seek {
Subroutine dmap_seek: not called CC=5 SCAR=34.0
Pod undocumented
343 0 my($struct, $to_find) = @_;
344  
345 0 ▶ CHUNK: while (defined($to_find) && length($to_find)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $to_find and length $to_find) 0 0 0
Condition0%0/3
defined $to_find and length $to_find
!ll&&!rl&&r
000
MC/DC0%0/2
defined $to_find and length $to_find
defined $to_find length $to_find
Truth table0%0/3
A and B
A= defined $to_findB= length $to_find
ABresult
0X0
100
111
346 0 my $top;
347 0 ($top, $to_find) = split m{/}, $to_find, 2;
348  
349 0 ELEMENT: foreach my $elt (@$struct) {
350  
351 0 ▶ if ($elt->[0] eq $top) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->[0] eq $top) 0 0 0
352 0 $struct = $elt->[1];
353 0 next CHUNK;
354 }
355 }
356 0 return; # NOT FOUND
357 }
358 0 return $struct;
359 }
360  
361 0 ▶sub update_content_codes {
Subroutine update_content_codes: not called CC=10 SCAR=47.0
Pod undocumented
362 0 my $array = shift;
363 0 my $short;
364  
365 0 my $mccr = dmap_seek($array, "dmap.contentcodesresponse");
366 0 ▶ die "Couldn't find mccr" unless defined $mccr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mccr 0 0 0
367  
368 0 foreach my $mdcl_rec (@$mccr) {
369 0 ▶ next unless $mdcl_rec->[0] eq 'dmap.dictionary';
Branch: 1 branch
BranchTrueFalseTotal
unless $mdcl_rec->[0] eq "dmap.dictionary" 0 0 0
370 0 my @fields = @{$mdcl_rec->[1]};
371 0 my ($name, $id, $type);
372 0 foreach my $f (@fields) {
373 0 ▶ if ($f->[0] eq 'dmap.contentcodesnumber') { $id = $f->[1] }
Branch: 1 branch
BranchTrueFalseTotal
if ($f->[0] eq "dmap.contentcodesnumber") 0 0 0
374 0 ▶ if ($f->[0] eq 'dmap.contentcodesname') { $name = $f->[1] }
Branch: 1 branch
BranchTrueFalseTotal
if ($f->[0] eq "dmap.contentcodesname") 0 0 0
375 0 ▶ if ($f->[0] eq 'dmap.contentcodestype') { $type = $f->[1] }
Branch: 1 branch
BranchTrueFalseTotal
if ($f->[0] eq "dmap.contentcodestype") 0 0 0
376 }
377 0 ▶ if ($id eq 'mcnm') { $type = 9 } # string names please
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq "mcnm") 0 0 0
378 0 ▶ if ($id eq 'pfdt') { $type = 42 } # and straight binary pictures
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq "pfdt") 0 0 0
379 0 my $record = { NAME => $name, ID => $id, TYPE => $type };
380 0 $short->{$id} = $record;
381 }
382  
383 0 $Types = $short;
384 }
385  
386 120 ▶sub dmap_pack {
Subroutine dmap_pack: called CC=11 SCAR=24.9
Pod undocumented
387 120 my $struct = shift;
388 120 my $out = '';
389  
390 120 ▶ my %by_name = map { %{$_} ? ( $_->{NAME} => $_ ) : () } values %$Types;
Branch: 1 branch
BranchTrueFalseTotal
%{$_;} ? : 10680 4 10684
391 120 for my $pair (@$struct) {
392 486 my ($name, $value) = @$pair;
393 # dmap_unpack doesn't populate the name when its decoded
394 # something it doesn't know the content-code of, like aeSV
395 # which is new to 4.5
396 486 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 486 486
397 0 ▶ carp "element without a name - skipping" if $NOISY;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::DAAP::DMAP::NOISY 0 0 0
398 0 next;
399 }
400 # or, it may be we don't know what kind of thing this is
401 486 ▶ unless ($by_name{ $name }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($by_name{$name}) 0 486 486
402 0 ▶ carp "$name has unknown type - skipping" if $NOISY;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::DAAP::DMAP::NOISY 0 0 0
403 0 next;
404 }
405  
406 486 my $tag = $by_name{ $name }{ID};
407 486 my $type = $by_name{ $name }{TYPE};
408 #print "$name => $tag $type $Type_To_Unpack{$type}\n";
409 #$SIG{__WARN__} = sub { die @_ };
410 486 ▶ if ($type == 9 && eval { require Encode; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 9 and eval { do { require Encode; 1 } }) 207 279 486
Condition66%2/3
$type == 9 and eval { do { require Encode; 1 } }
!ll&&!rl&&r
2790207
MC/DC50%1/2
$type == 9 and eval { do { require Encode; 1 } }
$type == 9 eval { do { require Encode; 1 } }
Truth table66%2/3
A and B
A= $type == 9B= eval { do { require Encode; 1 } }
ABresult
0X0
100
111
411 207 $value = Encode::encode('utf-8', $value);
412 }
413 486 ▶ if ($type == 12) { # container
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 12) { } 113 373 486
elsif ($type == 7) { } 9 364 373
414 113 $value = dmap_pack( $value );
415 }
416 elsif ($type == 7) { # 64-bit
417 9 my $high = Math::BigInt->new( $value )->brsft(32)."";
418 9 my $low = Math::BigInt->new( $value )->band(0xFFFFFFFF)."";
419 9 $value = pack( "N2", $high, $low );
420 }
421 else {
422 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
423 364 $value = pack( $Type_To_Unpack{$type}, $value );
424 }
425 1 ▶ my $length = do { use bytes; length $value };
Subroutine BEGIN: called CC=1 SCAR=0.0
426 486 $out .= $tag . pack("N", $length) . $value;
427 }
428 120 return $out;
429 }
430  
431 1 1;
432  
433 __DATA__
↑ Summary