Perl module for reading and writing daap structures
Until 2.0, I reserve the right to change the interface. In particular, I think \*(C`dmap_flatten\*(C', \*(C`dmap_to_hash_ref\*(C', and \*(C`dmap_to_array_ref\*(C' are likely to disappear. And I suspect the hive brain of Perl can come up with a better data structure than I have.
A \s-1DMAP\s0 structure is a binary record used in Apple's \s-1DAAP\s0 protocol. A \s-1DMAP\s0 structure may contain other \s-1DMAP\s0 structures. Fields in a \s-1DMAP\s0 structure are identified by a short name (\*(L"msdc\*(R"). The short name is what's in the binary record, but a content codes list gives a long name (\*(L"dmap.databasescount\*(R") and a data type for the record (32-bit integer).
A parsed \s-1DMAP\s0 structure is built out of arrays. For example:
[ [ 'dmap.loginresponse', [ [ 'dmap.status', 200 ], [ 'dmap.sessionid', 2393 ] ] ] ]
(\*(C`dmap_unpack\*(C' returns this kind of structure)
There are two rules here: a field is wrapped in an array, and a container's values are wrapped in an array. So the structure is programmatically built as:
$status_field = [ 'dmap.status', 200 ]; $session_id_field = [ 'dmap.sessionid', 2393 ]; $response_value = [ $status_field, $session_id_field ]; $login_response_field = [ 'dmap.loginresponse', $response_value ]; $entire_response = [ $login_response_field ];
The outer array is necessary because not every response has only one top-level container as this does.
In \s-1XML\s0 you'd write the response as:
<dmap.loginresponse> <dmap.status>200</dmap.status> <dmap.sessionid>2393</dmap.sessionid> </dmap.loginresponse>
This is what \*(C`dmap_to_xml\*(C' returns.
A much more convenient structure for representing this data would be:
{ 'dmap.loginresponse' => { { 'dmap.status' => 200, 'dmap.sessionid' => 2393, }, }
This is the output of \*(C`dmap_to_hash_ref\*(C', but beware! This isn't suitable for every response. The hash is indexed by field name and a structure may contain many elements of the same name. For example, requesting the content codes list gives you a list of records that have the field name \*(C`dmap.dictionary\*(C'.
The array structure returned by \*(C`dmap_to_array_ref\*(C' is complex, but the \*(C`dmap_seek\*(C' function makes it easier. This takes a structure and a path expressed as a slash-separated list of field names:
dmap.loginresponse/dmap.sessionid
The return value is the the value of the first \*(C`dmap.sessionid\*(C' found in the first \*(C`dmap.loginresponse\*(C' structure. In the case of the sample record above, it would be 2393.
Another way to handle these complex arrays is to \*(C`dmap_flatten\*(C' them. This returns an array of "path = value" lines, where path is a slash-separated path. For example:
[ '/dmap.loginresponse/dmap.status = 200', '/dmap.loginresponse/dmap.sessionid = 2393' ]
You can use \*(C`grep\*(C' and regexps to find data if that's the way your mind works.
\*(C`dmap_flatten\*(C' has a similar looking cousin called \*(C`dmap_flat_list\*(C', which returns an array of "path => value" pairs. For example:
[ '/dmap.loginresponse/dmap.status' => 200, '/dmap.loginresponse/dmap.sessionid' => 2393, ]
You can then turn this into a hash (which may of course lose you the first elements), or iterate over it in pairs, if that's easier.
You can, but don't have to, update the tables of field names (\*(L"content codes\*(R") and data types. \s-1DAAP\s0 offers a request that returns a packet of content codes. Feed that packet to \*(C`update_content_codes\*(C'.
It's all implementation details. Here are the various data types.
1, 3, 5, 7 = ints, size 8,16,32,64 bit 9 = string, 10 = time_t-style time 11 = version (two 16-bit ints, I think) 12 = container
This uses Math::BigInt for 64-bit quantities, as not every platform has 64-bit int support available.
There's no support for types 2, 4, 6, 8 yet because nobody'd found examples of them in the field: are they endian changes, or signedness changes. The assumption is that all numbers are unsigned (why allow the possibility of a negative number of songs?).
Nathan Torkington, <nathan \s-1AT\s0 torkington.com>. For support, join the \s-1DAAP\s0 developers mailing list by sending mail to <daap-dev-subscribe \s-1AT\s0 develooper.com>.
Richard Clamp <[email protected]> is the current maintainer, and took over in July 2004.