GNUpod/iTunesDBwrite.pm untested

filter: Statementstmt 0.0% 0 / 308 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 429 CC 23 SCAR 28.5
CC
23
Cov
0%
mk_mhit
30.0
mk_mhod
30.0
mk_splprefmhod
30.0
CRAP 17.2
1 #!/usr/bin/perl
2  
3 package Mac::iPod::GNUpod::iTunesDBwrite;
4  
5 # This package split off from iTunesDB.pm in the GNUpod toolset. Original code
6 # (C) 2002-2003 Adrian Ulrich <pab at blinkenlights.ch>. Part of the
7 # gnupod-tools collection, URL: http://www.gnu.org/software/gnupod/
8 #
9 # Code rewrite and adaptation for CPAN by JS Bangs <jaspax at cpan.org>.
10  
11 use strict;
12 use warnings;
13 no warnings 'uninitialized';
14 use Unicode::String;
15 use Mac::iPod::GNUpod::Utils;
16  
17 #mk_mhod() will take care of lc() entries
18 my %mhod_id = (
19 title => 1,
20 path => 2,
21 album => 3,
22 artist => 4,
23 genre => 5,
24 fdesc => 6,
25 eq => 7,
26 comment => 8,
27 composer => 12
28 );# SPLPREF =>50, SPLDATA =>51, PLTHING => 100) ;
29  
30 my @mhod_array;
31 foreach(keys(%mhod_id)) {
32 $mhod_array[$mhod_id{$_}] = $_;
33 }
34  
35 # create an iTunesDB header
36 sub mk_mhbd {
37 my ($hr) = @_;
38  
39 my $ret = "mhbd";
40 $ret .= pack("h8", _itop(104)); #Header Size
41 $ret .= pack("h8", _itop($hr->{size}+104)); #size of the whole mhdb
42 $ret .= pack("H8", "01"); #?
43 $ret .= pack("H8", "01"); #? - changed to 2 from itunes2 to 3 .. version?
44 $ret .= pack("H8", "02"); #?
45 $ret .= pack("H160", "00"); #dummy space
46  
47 return $ret;
48 }
49  
50 # a iTunesDB has 2 mhsd's: (This is a child of mk_mhbd)
51 # mhsd1 holds every song on the ipod
52 # mhsd2 holds playlists
53 sub mk_mhsd {
54 my ($hr) = @_;
55  
56 my $ret = "mhsd";
57 $ret .= pack("h8", _itop(96)); #Headersize, static
58 $ret .= pack("h8", _itop($hr->{size}+96)); #Size
59 $ret .= pack("h8", _itop($hr->{type})); #type .. 1 = song .. 2 = playlist
60 $ret .= pack("H160", "00"); #dummy space
61  
62 return $ret;
63 }
64  
65 # Make a complete mhit/mhod from a file hashref
66 sub render_mhit {
67 my ($file, $newid) = @_;
68 return if not $file;
69 my ($cumul_mhod, $count_mhod);
70  
71 # Copy the hashref and give it the new id
72 #my %copy = %$file;
73 #$copy{id} = $newid;
74  
75 # Make mhods
76 while (my ($key, $val) = each %$file) {
77 next unless $val; # No empty fields
78 my $new_mhod = mk_mhod( { stype => $key, string => $val } );
79 $cumul_mhod .= $new_mhod;
80 $count_mhod++ if defined $new_mhod;
81 }
82  
83 # Now make the mhit and tack the mhods to it
84 my $mhit = mk_mhit({
85 size => length($cumul_mhod),
86 count => $count_mhod,
87 fh => $file
88 });
89  
90 return $mhit . $cumul_mhod;
91 }
92
93 # Create an mhit entry, needs to know about the length of his
94 # mhod(s) (You have to create them yourself..!)
95 sub mk_mhit {
96 my($hr) = @_;
97 my %file_hash = %{$hr->{fh}};
98  
99 #We have to fix 'volume'
100 my $vol = sprintf("%.0f",( int($file_hash{volume})*2.55 ));
101  
102 if($vol >= 0 && $vol <= 255) { } #Nothing to do
103 elsif($vol < 0 && $vol >= -255) { #Convert value
104 $vol = oct("0xFFFFFFFF") + $vol;
105 }
106 else {
107 $@ .= "Song id $file_hash{id} has volume set to $file_hash{volume} percent. Volume set to +-0%\n";
108 $vol = 0; #We won't nuke the iPod with an ultra high volume setting..
109 }
110  
111 foreach( ("rating", "prerating") ) {
112 if($file_hash{$_} < 0 || $file_hash{$_} > 5) {
113 $@ .= "Song $file_hash{id} has an invalid $_: $file_hash{$_}\n";
114 $file_hash{$_} = 0;
115 }
116 }
117  
118 #Check for stupid input
119 my ($c_id) = $file_hash{id} =~ /(\d+)/;
120 if($c_id < 1) {
121 $@ .= "ID can't be $c_id, must be > 0\n";
122 }
123  
124 my $ret = "mhit";
125 $ret .= pack("h8", _itop(156)); #header size
126 $ret .= pack("h8", _itop(int($hr->{size})+156)); #len of this entry
127 $ret .= pack("h8", _itop($hr->{count})); #num of mhods in this mhit
128 $ret .= pack("h8", _itop($c_id)); #Song index number
129 $ret .= pack("h8", _itop(1)); #?
130 $ret .= pack("H8"); #dummyspace
131 $ret .= pack("h8", _itop(256+(oct('0x14000000')
132 *$file_hash{rating}))); #type+rating .. this is very STUPID..
133 $ret .= pack("h8", _mactime()); #timestamp (we create a dummy timestamp, iTunes doesn't seem to make use of this..?!)
134 $ret .= pack("h8", _itop($file_hash{filesize})); #filesize
135 $ret .= pack("h8", _itop($file_hash{time})); #seconds of song
136 $ret .= pack("h8", _itop($file_hash{songnum})); #nr. on CD .. we dunno use it (in this version)
137 $ret .= pack("h8", _itop($file_hash{songs})); #songs on this CD
138 $ret .= pack("h8", _itop($file_hash{year})); #the year
139 $ret .= pack("h8", _itop($file_hash{bitrate})); #bitrate
140 $ret .= pack("H4", "00"); #??
141 $ret .= pack("h4", _itop($file_hash{srate} || 44100)); #Srate (note: h4!)
142 $ret .= pack("h8", _itop($vol)); #Volume
143 $ret .= pack("h8", _itop($file_hash{starttime})); #Start time?
144 $ret .= pack("h8", _itop($file_hash{stoptime})); #Stop time?
145 $ret .= pack("H8");
146 $ret .= pack("h8", _itop($file_hash{playcount}));
147 $ret .= pack("H8"); #Sometimes eq playcount .. ?!
148 $ret .= pack("h8"); #Last playtime.. FIXME
149 $ret .= pack("h8", _itop($file_hash{cdnum})); #cd number
150 $ret .= pack("h8", _itop($file_hash{cds})); #number of cds
151 $ret .= pack("H8"); #hardcoded space
152 $ret .= pack("h8", _mactime()); #dummy timestamp again...
153 $ret .= pack("H16");
154 $ret .= pack("H8"); #??
155 $ret .= pack("h8", _itop($file_hash{prerating}*oct('0x140000'))); #This is also stupid: the iTunesDB has a rating history
156 $ret .= pack("H8"); # ???
157 $ret .= pack("H56"); #
158 return $ret;
159 }
160  
161 # An mhod simply holds information
162 sub mk_mhod {
163 ## - type id
164 #1 - titel
165 #2 - ipod filename
166 #3 - album
167 #4 - interpret
168 #5 - genre
169 #6 - filetype
170 #7 - EQ Setting
171 #8 - comment
172 #12 - composer
173 #100 - Playlist item or/and PlaylistLayout (used for trash? ;))
174  
175 my ($hr) = @_;
176 my $type_string = $hr->{stype};
177 my $string = $hr->{string};
178 my $fqid = $hr->{fqid};
179 my $type = $mhod_id{lc($type_string)};
180  
181 #Appnd size for normal mhod's
182 my $mod = 40;
183  
184 #Called with fqid, this has to be an PLTHING (100)
185 if($fqid) {
186 #fqid set, that's a pl item!
187 $type = 100;
188 #Playlist mhods are longer
189 $mod += 4;
190 }
191 elsif(!$type) { #No type and no fqid, skip it
192 return undef;
193 }
194 else { #has a type, default fqid
195 $fqid = 1;
196 }
197  
198 if($type == 7 && $string !~ /#!#\d+#!#/) {
199 $@ .= "Wrong format: '$type_string=\"$string\"', value should be like '#!#NUMBER#!#'. ignoring value\n";
200 $string = undef;
201 }
202  
203 $string = _ipod_string($string); #cache data
204 my $ret = "mhod"; #header
205 $ret .= pack("h8", _itop(24)); #size of header
206 $ret .= pack("h8", _itop(length($string)+$mod)); # size of header+body
207 $ret .= pack("h8", _itop("$type")); #type of the entry
208 $ret .= pack("H16"); #dummy space
209 $ret .= pack("h8", _itop($fqid)); #Refers to this id if a PL item
210 #else -> 1
211 $ret .= pack("h8", _itop(length($string))); #size of string
212  
213  
214 if($type != 100){ #no PL mhod
215 $ret .= pack("h16"); #trash
216 $ret .= $string; #the string
217 }
218 else { #PL mhod
219 $ret .= pack("h24"); #playlist mhods are a different
220 }
221 return $ret;
222 }
223  
224 # Create a spl-pref (type=50) mhod
225 sub mk_splprefmhod {
226 my($hs) = @_;
227 my($live, $chkrgx, $chklim, $mos) = 0;
228
229 #Bool stuff
230 $live = 1 if $hs->{liveupdate};
231 my $checkrule = int($hs->{checkrule});
232 $mos = 1 if $hs->{mos};
233  
234 if($checkrule < 1 || $checkrule > 3) {
235 $@ .= "'checkrule' ($checkrule) out of range. Value set to 1 (=LimitMatch)\n";
236 $checkrule = 1;
237 }
238  
239 $chkrgx = 1 if $checkrule>1;
240 $chklim = $checkrule-$chkrgx*2;
241 #lim-only = 1 / match only = 2 / both = 3
242  
243 my $ret = "mhod";
244 $ret .= pack("h8", _itop(24)); #Size of header
245 $ret .= pack("h8", _itop(96));
246 $ret .= pack("h8", _itop(50));
247 $ret .= pack("H16");
248 $ret .= pack("h2", _itop($live)); #LiveUpdate ?
249 $ret .= pack("h2", _itop($chkrgx)); #Check regexps?
250 $ret .= pack("h2", _itop($chklim)); #Check limits?
251 $ret .= pack("h2", _itop($hs->{item})); #Wich item?
252 $ret .= pack("h2", _itop($hs->{sort})); #How to sort
253 $ret .= pack("h6");
254 $ret .= pack("h8", _itop($hs->{value})); #lval
255 $ret .= pack("h2", _itop($mos)); #mos
256 $ret .= pack("h118");
257 }
258  
259 # Create a spl-data (type=51) mhod
260 sub mk_spldatamhod {
261 my($hs) = @_;
262  
263 my $anymatch = 1 if $hs->{anymatch};
264  
265 if(ref($hs->{data}) ne "ARRAY") {
266 $@ .= "No spldata found in spl, iTunes4-workaround enabled";
267 push(@{$hs->{data}}, {field=>4,action=>2,string=>""});
268 }
269  
270 my $cr = undef;
271 foreach my $chr (@{$hs->{data}}) {
272 my $string = undef;
273 #Fixme: this is ugly (same as read_spldata)
274 if($chr->{field} =~ /^(2|3|4|8|9|14|18)$/) {
275 $string = Unicode::String::utf8($chr->{string})->utf16;
276 }
277 else {
278 my ($from, $to) = $chr->{string} =~ /(\d+):?(\d*)/;
279 $to ||=$from;
280 $string = pack("H8");
281 $string .= pack("H8", _x86itop($from));
282 $string .= pack("H24");
283 $string .= pack("H8", _x86itop(1));
284 $string .= pack("H8");
285 $string .= pack("H8", _x86itop($to));
286 $string .= pack("H24");
287 $string .= pack("H8", _x86itop(1));
288 $string .= pack("H40");
289 # __hd($string);
290 }
291  
292 if(length($string) > 254) { #length field is limited to 0xfe!
293 $@ .= "Splstring too long for iTunes, cropping\n";
294 $string = substr($string,0,254);
295 }
296  
297 $cr .= pack("H6");
298 $cr .= pack("h2", _itop($chr->{field}));
299 $cr .= pack("H6", reverse("010000"));
300 $cr .= pack("h2", _itop($chr->{action}));
301 $cr .= pack("H94");
302 $cr .= pack("h2", _itop(length($string)));
303 $cr .= $string;
304 }
305  
306 my $ret = "mhod";
307 $ret .= pack("h8", _itop(24)); #Size of header
308 $ret .= pack("h8", _itop(length($cr)+160)); #header+body size
309 $ret .= pack("h8", _itop(51)); #type
310 $ret .= pack("H16");
311 $ret .= "SLst"; #Magic
312 $ret .= pack("H8", reverse("00010001")); #?
313 $ret .= pack("h6");
314 $ret .= pack("h2", _itop(int(@{$hs->{data}}))); #HTM (Childs from cr)
315 $ret .= pack("h6");
316 $ret .= pack("h2", _itop($anymatch)); #anymatch rule on or off
317 $ret .= pack("h240");
318  
319  
320 $ret .= $cr;
321 return $ret;
322 }
323  
324 # Render a playlist
325 sub r_mpl {
326 # Expects a hash w/ the following keys:
327 # name => $ name of the pl
328 # type => $ type of the pl
329 # ids => [] list of songids of in pl
330 # curid => $ current id in db
331 # splprefs => {} holds spl prefs
332 # spldata => {} holds spl data
333 my %dat = @_;
334 my ($pl, $fc, $mhp) = ('', 0, 0);
335  
336 # Spls handled here
337 if(ref($dat{splprefs}) eq "HASH") {
338 my $spl = $dat{splprefs};
339 $pl .= mk_splprefmhod({
340 item => $spl->{limititem},
341 sort => $spl->{limitsort},
342 mos => $spl->{moselected},
343 liveupdate => $spl->{liveupdate},
344 value => $spl->{limitval},
345 checkrule => $spl->{checkrule}
346 });
347  
348 $pl .= mk_spldatamhod({anymatch => $spl->{matchany}, data => $dat{spldata}});
349 $mhp=2;
350 }
351  
352 foreach(@{$dat{ids}}) {
353 $dat{curid}++;
354 my $cmhip = mk_mhip({childs => 1, plid => $dat{curid}, sid => $_});
355 my $cmhod = mk_mhod({fqid => $_});
356 next unless (defined($cmhip) && defined($cmhod)); #mk_mhod needs to be ok
357 $fc++;
358 $pl .= $cmhip . $cmhod;
359 }
360 my $plsize = length($pl);
361  
362 #mhyp appends a listview to itself
363 my $mhyp = mk_mhyp({
364 size => $plsize, name => $dat{name}, type => $dat{type}, files => $fc, mhods => $mhp
365 });
366 return $mhyp . $pl, $dat{curid};
367 }
368  
369  
370 # header for all files (like you use mk_mhlp for playlists)
371 sub mk_mhlt {
372 my ($hr) = @_;
373  
374 my $ret = "mhlt";
375 $ret .= pack("h8", _itop(92)); #Header size (static)
376 $ret .= pack("h8", _itop($hr->{songs})); #songs in this itunesdb
377 $ret .= pack("H160", "00"); #dummy space
378  
379 return $ret;
380 }
381  
382 # header for ALL playlists
383 sub mk_mhlp {
384 my ($hr) = @_;
385  
386 my $ret = "mhlp";
387 $ret .= pack("h8", _itop(92)); #Static header size
388 $ret .= pack("h8", _itop($hr->{playlists})); #playlists on iPod (including main!)
389 $ret .= pack("h160", "00"); #dummy space
390 return $ret;
391 }
392  
393 # Creates an header for a new playlist (child of mk_mhlp)
394 sub mk_mhyp {
395 my($hr) = @_;
396  
397 # We need to create a listview-layout and an mhod with the name. iTunes
398 # prefs for this PL & PL name (default PL has device name as PL name)
399 my $appnd = mk_mhod({stype=>"title", string=>$hr->{name}}).__dummy_listview();
400  
401 ##Child mhods calc..
402 ##We create 2 mhod's here.. mktunes may have created more mhods.. so we
403 ##have to adjust the childs here
404 my $cmh = 2+$hr->{mhods};
405  
406 my $ret .= "mhyp";
407 $ret .= pack("h8", _itop(108)); #type
408 $ret .= pack("h8", _itop($hr->{size}+108+(length($appnd)))); #size
409 $ret .= pack("h8", _itop($cmh)); #mhods
410 $ret .= pack("h8", _itop($hr->{files})); #songs in pl
411 $ret .= pack("h8", _itop($hr->{type})); # 1 = main .. 0=not main
412 $ret .= pack("H8", "00"); #?
413 $ret .= pack("H8", "00"); #?
414 $ret .= pack("H8", "00"); #?
415 $ret .= pack("H144", "00"); #dummy space
416  
417 return $ret.$appnd;
418 }
419  
420  
421 # header for new Playlist item (child if mk_mhyp)
422 sub mk_mhip {
423 my ($hr) = @_;
424 #sid = SongId
425 #plid = playlist order ID
426  
427 my $ret = "mhip";
428 $ret .= pack("h8", _itop(76));
429 $ret .= pack("h8", _itop(76));
430 $ret .= pack("h8", _itop($hr->{childs})); #Mhod childs !
431 $ret .= pack("H8", "00");
432 $ret .= pack("h8", _itop($hr->{plid})); #ORDER id
433 $ret .= pack("h8", _itop($hr->{sid})); #song id in playlist
434 $ret .= pack("H96", "00");
435 return $ret;
436 }
437  
438 #Convert utf8 (what we got from XML::Parser) to utf16 (ipod)
439 sub _ipod_string {
440 my $utf8 = shift;
441 my $utf16;
442 # We got utf8 from parser, the iPod likes utf16.., swapped..
443 if (UNIVERSAL::isa($utf8, 'Unicode::String')) {
444 $utf16 = $utf8->utf16;
445 }
446 else {
447 $utf16 = Unicode::String::utf8($utf8)->utf16;
448 }
449 $utf16 = Unicode::String::byteswap2($utf16);
450 return $utf16;
451 }
452  
453 #returns a (dummy) timestamp in MAC time format
454 sub _mactime {
455 my $x = 1234567890;
456 return sprintf("%08X", $x);
457 }
458  
459 #int to ipod
460 sub _itop {
461 my($in) = @_;
462 my($int) = $in =~ /(\d+)/;
463 return scalar(reverse(sprintf("%08X", $int )));
464 }
465  
466 #int to x86 ipodval (spl!!)
467 sub _x86itop {
468 my($in) = @_;
469 my($int) = $in =~ /(\d+)/;
470 return scalar((sprintf("%08X", $int )));
471 }
472  
473 #Create a dummy listview, this function could disappear in
474 #future, only meant to be used internal by this module, dont
475 #use it yourself..
476 sub __dummy_listview {
477 my($ret, $foobar);
478 $ret = "mhod"; #header
479 $ret .= pack("H8", reverse("18")); #size of header
480 $ret .= pack("H8", reverse("8802")); #$slen+40 - size of header+body
481 $ret .= pack("H8", reverse("64")); #type of the entry
482 $ret .= pack("H48", "00"); #?
483 $ret .= pack("H8", reverse("840001")); #? (Static?)
484 $ret .= pack("H8", reverse("01")); #?
485 $ret .= pack("H8", reverse("09")); #?
486 $ret .= pack("H8", reverse("00")); #?
487 $ret .= pack("H8",reverse("010025")); #static? (..or width of col?)
488 $ret .= pack("H8",reverse("00")); #how to sort
489 $ret .= pack("H16", "00");
490 $ret .= pack("H8", reverse("0200c8"));
491 $ret .= pack("H8", reverse("01"));
492 $ret .= pack("H16","00");
493 $ret .= pack("H8", reverse("0d003c"));
494 $ret .= pack("H24","00");
495 $ret .= pack("H8", reverse("04007d"));
496 $ret .= pack("H24", "00");
497 $ret .= pack("H8", reverse("03007d"));
498 $ret .= pack("H24", "00");
499 $ret .= pack("H8", reverse("080064"));
500 $ret .= pack("H24", "00");
501 $ret .= pack("H8", reverse("170064"));
502 $ret .= pack("H8", reverse("01"));
503 $ret .= pack("H16", "00");
504 $ret .= pack("H8", reverse("140050"));
505 $ret .= pack("H8", reverse("01"));
506 $ret .= pack("H16", "00");
507 $ret .= pack("H8", reverse("15007d"));
508 $ret .= pack("H8", reverse("01"));
509 $ret .= pack("H752", "00");
510 $ret .= pack("H8", reverse("65"));
511 $ret .= pack("H152", "00");
512  
513 # Every playlist has such an mhod, it tells iTunes (and other programs?) how the
514 # the playlist shall look (visible coloums.. etc..)
515 # But we are using always the same layout static.. we don't support this mhod type..
516 # But we write it (to make iTunes happy)
517 return $ret
518 }
519  
520 1;