GNUpod/iTunesDBread.pm untested

filter: Statementstmt 0.0% 0 / 215 Branchbran 0.0% 0 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 292 CC 24 SCAR 35.0
CC
24
Cov
0%
get_pl
90.0
get_mhits
42.0
read_splpref
30.0
CRAP 33.3
1 #!/usr/bin/perl
2  
3 package Mac::iPod::GNUpod::iTunesDBread;
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'; # Useful throughout script (we deal with lots of intended undefs)
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 # Open the iTunesDB file..
36 sub open_itunesdb {
37 open(FILE, $_[0]);
38 }
39  
40 # Close the iTunesDB file..
41 sub close_itunesdb {
42 close(FILE);
43 }
44  
45 # Get a INT value
46 sub get_int {
47 my($start, $anz) = @_;
48 my $buffer = undef;
49  
50 # paranoia checks
51 $start = int($start);
52 $anz = int($anz);
53  
54 #seek to the given position
55 seek(FILE, $start, 0);
56  
57 #start reading
58 read(FILE, $buffer, $anz);
59 return shx2int($buffer);
60 }
61  
62 # Get a x86INT value
63 sub get_x86_int {
64 my($start, $anz) = @_;
65  
66 my($buffer, $xx, $xr) = undef;
67  
68 # paranoia checks
69 $start = int($start);
70 $anz = int($anz);
71  
72 #seek to the given position
73 seek(FILE, $start, 0);
74  
75 #start reading
76 read(FILE, $buffer, $anz);
77 foreach(split(//, $buffer)) {
78 $xx = sprintf("%02X", ord($_));
79 $xr .= $xx;
80 }
81 $xr = oct("0x".$xr);
82 return $xr;
83 }
84  
85 # Get all SPL items
86 sub read_spldata {
87 my($hr) = @_;
88  
89 my $diff = $hr->{start}+160;
90 my @ret = ();
91  
92 for(1..$hr->{htm}) {
93 my $field = get_int($diff+3, 1);
94 my $action= get_int($diff+7, 1);
95 my $slen = get_int($diff+55,1); #Whoa! This is true: string is limited to 0xfe (254) chars!! (iTunes4)
96 my $rs = undef; #ReturnSting
97  
98 #Fixme: this is ugly
99 if($field =~ /^(2|3|4|8|9|14|18)$/) { #Is a string type
100 my $string= get_string($diff+56, $slen);
101 #No byteswap here?? why???
102 $rs = Unicode::String::utf16($string)->utf8;
103 }
104  
105 else { #Is INT (Or range)
106 my $xfint = get_x86_int($diff+56+4,4);
107 my $xtint = get_x86_int($diff+56+28,4);
108 $rs = "$xfint:$xtint";
109 }
110 $diff += $slen+56;
111 push(@ret, {field=>$field,action=>$action,string=>$rs});
112 }
113 return \@ret;
114 }
115  
116 # Read SPLpref data
117 sub read_splpref {
118 my($hs) = @_;
119 my ($live, $chkrgx, $chklim, $mos);
120  
121 $live = 1 if get_int($hs->{start}+24,1);
122 $chkrgx = 1 if get_int($hs->{start}+25,1);
123 $chklim = 1 if get_int($hs->{start}+26,1);
124 my $item = get_int($hs->{start}+27,1);
125 my $sort = get_int($hs->{start}+28,1);
126 my $limit= get_int($hs->{start}+32,4);
127 $mos = 1 if get_int($hs->{start}+36,1);
128 return({
129 live=>$live,
130 value=>$limit,
131 iitem=>$item,
132 isort=>$sort,
133 mos=>$mos,
134 checkrule=>($chklim+($chkrgx*2))
135 });
136 }
137  
138 # Do a hexDump DEBUGGING ONLY
139 sub __hd {
140 open(KK,">/tmp/XLZ"); print KK $_[0]; close(KK);
141 system("hexdump -vC /tmp/XLZ");
142 }
143  
144 #get a SINGLE mhod entry:
145 # return+seek = new_mhod should be there
146 sub get_mhod {
147 my ($seek) = @_;
148  
149 my $id = get_string($seek, 4); #are we lost?
150 my $ml = get_int($seek+8, 4); #Length of this mhod
151 my $mty = get_int($seek+12, 4); #type number
152 my $xl = get_int($seek+28,4); #String length
153  
154 ## That's spl stuff, only to be used with 51 mhod's
155 my $htm = get_int($seek+35,1); #Only set for 51
156 my $anym= get_int($seek+39,1); #Only set for 51
157 my $spldata = undef; #dummy
158 my $splpref = undef; #dummy
159  
160 if($id eq "mhod") { #Seek was okay
161 my $foo = get_string($seek+($ml-$xl), $xl); #string of the entry
162 #$foo is now UTF16 (Swapped), but we need an utf8
163 $foo = Unicode::String::byteswap2($foo);
164 $foo = Unicode::String::utf16($foo)->utf8;
165  
166 ##Special handling for SPLs
167 if($mty == 51) { #Get data from spldata mhod
168 $foo = undef;
169 $spldata = read_spldata({start=>$seek, htm=>$htm});
170 }
171 elsif($mty == 50) { #Get prefs from splpref mhod
172 $foo = undef;
173 $splpref = read_splpref({start=>$seek, end=>$ml});
174 }
175 return({size=>$ml,string=>$foo,type=>$mty,spldata=>$spldata,splpref=>$splpref,matchrule=>$anym});
176  
177 }
178 else {
179 return({size => -1});
180 }
181 }
182  
183 # get an mhip entry
184 sub get_mhip {
185 my($pos) = @_;
186 my $oid = 0;
187 if(get_string($pos, 4) eq "mhip") {
188 my $oof = get_int($pos+4, 4);
189 my $mhods=get_int($pos+12,4);
190  
191 for(my $i=0;$i<$mhods;$i++) {
192 my $mhs = get_mhod($pos+$oof)->{size};
193 die "Fatal seek error in get_mhip, can't continue\n" if $mhs == -1;
194 $oid+=$mhs;
195 }
196  
197 my $plid = get_int($pos+5*4,4);
198 my $sid = get_int($pos+6*4, 4);
199 return({size=>($oid+$oof),sid=>$sid,plid=>$plid});
200 }
201  
202 #we are lost
203 return ({size=>-1});
204 }
205  
206 # Reads a string
207 sub get_string {
208 my ($start, $anz) = @_;
209 my($buffer) = undef;
210  
211 # paranoia
212 $start = int($start);
213 $anz = int($anz);
214 seek(FILE, $start, 0);
215  
216 #start reading
217 read(FILE, $buffer, $anz);
218 return $buffer;
219 }
220  
221 # Get a playlist (Should be called get_mhyp, but it does the whole playlist)
222 sub get_pl {
223 my($pos) = @_;
224  
225 my %ret_hash = ();
226 my @pldata = ();
227  
228 if(get_string($pos, 4) eq "mhyp") { #Ok, its an mhyp
229 $ret_hash{type}= get_int($pos+20, 4); #Is it a main playlist?
230 my $scount = get_int($pos+16, 4); #How many songs should we expect?
231 my $header_len = get_int($pos+4, 4); #Size of the header
232 my $mhyp_len = get_int($pos+8, 4); #Size of mhyp
233 my $mhods = get_int($pos+12,4); #How many mhods we have here
234  
235 #Its a MPL, do a fast skip
236 if($ret_hash{type}) {
237 return ($pos+$mhyp_len, {type=>1})
238 }
239  
240 $pos += $header_len; #set pos to start of first mhod
241 #We can now read the name of the Playlist
242 #Ehpod is buggy and writes the playlist name 2 times.. well catch both of them
243 #MusicMatch is also stupid and doesn't create a playlist mhod
244 #for the mainPlaylist
245 my ($oid, $plname, $itt) = undef;
246 for(my $i=0;$i<$mhods;$i++) {
247 my $mhh = get_mhod($pos);
248 if($mhh->{size} == -1) {
249 die "FATAL: Expected to find $mhods mhods, but I failed to get nr. $i. iTunesDBread.pm panic";
250 }
251 $pos+=$mhh->{size};
252 if($mhh->{type} == 1) {
253 $ret_hash{name} = $mhh->{string};
254 }
255 elsif(ref($mhh->{splpref}) eq "HASH") {
256 $ret_hash{splpref} = $mhh->{splpref};
257 }
258 elsif(ref($mhh->{spldata}) eq "ARRAY") {
259 $ret_hash{spldata} = $mhh->{spldata};
260 $ret_hash{matchrule}=$mhh->{matchrule};
261 }
262 }
263  
264 #Now get the items
265 for(my $i = 0; $i<$scount;$i++) {
266 my $mhih = get_mhip($pos);
267 if($mhih->{size} == -1) {
268 die "FATAL: Expected to find $scount songs, but I failed to get nr. $i. iTunesDBread.pm panic";
269 }
270 $pos += $mhih->{size};
271 push(@pldata, $mhih->{sid}) if $mhih->{sid};
272 }
273 $ret_hash{content} = \@pldata;
274 return ($pos, \%ret_hash);
275 }
276  
277 #Seek was wrong
278 return -1;
279 }
280  
281 # Get mhit + child mhods
282 sub get_mhits {
283 my ($sum) = @_;
284 if(get_string($sum, 4) eq "mhit") { #Ok, its a mhit
285  
286 my %ret = ();
287  
288 #Infos stored in mhit
289 $ret{id} = get_int($sum+16,4);
290 $ret{filesize} = get_int($sum+36,4);
291 $ret{time} = get_int($sum+40,4);
292 $ret{cdnum} = get_int($sum+92,4);
293 $ret{cds} = get_int($sum+96,4);
294 $ret{songnum} = get_int($sum+44,4);
295 $ret{songs} = get_int($sum+48,4);
296 $ret{year} = get_int($sum+52,4);
297 $ret{bitrate} = get_int($sum+56,4);
298 $ret{srate} = get_int($sum+62,2); #What is 60-61 ?!!
299 $ret{volume} = get_int($sum+64,4);
300 $ret{starttime}= get_int($sum+68,4);
301 $ret{stoptime} = get_int($sum+72,4);
302 $ret{playcount}= get_int($sum+80,4); #84 has also something to do with playcounts. (Like rating + prerating?)
303 $ret{lastplay} = get_int($sum+88,4);
304 $ret{addtime} = get_int($sum+104,4);
305 $ret{bookmark} = get_int($sum+108,4);
306 $ret{bpm} = get_int($sum+122,2);
307  
308 $ret{rating} = int((get_int($sum+28,4)-256)/oct('0x14000000'));
309 $ret{prerating}= int(get_int($sum+120,4) / oct('0x140000'));
310  
311 ####### We have to convert the 'volume' to percent...
312 ####### The iPod doesn't store the volume-value in percent..
313 #Minus value (-X%)
314 $ret{volume} -= oct("0xffffffff") if $ret{volume} > 255;
315  
316 #Convert it to percent
317 $ret{volume} = sprintf("%.0f",($ret{volume}/2.55));
318  
319 ## Paranoia check
320 if(abs($ret{volume}) > 100) {
321 $@ .= "Volume is $ret{volume} percent for song $ret{id}.. set to 0 percent";
322 $ret{volume} = 0;
323 }
324  
325 #Now get the mhods from this mhit
326 my $mhods = get_int($sum+12,4);
327 $sum += get_int($sum+4,4);
328  
329 for(my $i=0; $i < $mhods; $i++) {
330 my $mhh = get_mhod($sum);
331 if($mhh->{size} == -1) {
332 die "FATAL: Expected to find $mhods mhods, but I failed to get nr $i. iTunesDBread.pm panic";
333 }
334 $sum+=$mhh->{size};
335 my $xml_name = $mhod_array[$mhh->{type}];
336 if($xml_name) { #Has an xml name.. sounds interesting
337 $ret{$xml_name} = $mhh->{string};
338 }
339 else {
340 warn "iTunesDB.pm: found unhandled mhod type '$mhh->{type}'\n";
341 }
342 }
343  
344 return ($sum,\%ret); #black magic, returns next (possible?) start of the mhit
345 }
346
347 #Was no mhod
348 return -1;
349 }
350  
351 # Returns start of part1 (files) and part2 (playlists)
352 sub get_starts {
353 #Get start of first mhit:
354 my $mhbd_s = get_int(4,4);
355 my $pdi = get_int($mhbd_s+8,4); #Used to calculate start of playlist
356 my $mhsd_s = get_int($mhbd_s+4,4);
357 my $mhlt_s = get_int($mhbd_s+$mhsd_s+4,4);
358 my $pos = $mhbd_s+$mhsd_s+$mhlt_s; #pos is now the start of the first mhit (always 292?);
359  
360 #How many songs are on the iPod ?
361 my $sseek = $mhbd_s + $mhsd_s;
362 my $songs = get_int($sseek+8,4);
363  
364 #How many playlists should we find ?
365 $sseek = $mhbd_s + $pdi;
366 $sseek += get_int($sseek+4,4);
367 my $pls = get_int($sseek+8,4);
368 return({position=>$pos,pdi=>($pos+$pdi),songs=>$songs,playlists=>$pls});
369 }
370  
371 1;