1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
|
/*
*
*
* Copyright (c) 2004 Samuel Wood (sam.wood@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*
*/
// For more information on how all this stuff works, see:
// http://www.ipodlinux.org/ITunesDB
// iPodDB.cpp: implementation of the iPod classes.
//
//////////////////////////////////////////////////////////////////////
#pragma warning( disable : 4786)
#include "iPodDB.h"
#include <bfc/platform/types.h>
#include <assert.h>
#include <time.h>
#include <windows.h>
#include <tchar.h>
#include <math.h>
#include <vector>
/*
#ifdef ASSERT
#undef ASSERT
#define ASSERT(x) {}
#endif
*/
//#define IPODDB_PROFILER // Uncomment to enable profiler measurments
#ifdef IPODDB_PROFILER
/* profiler code from Foobar2000's PFC library:
*
* Copyright (c) 2001-2003, Peter Pawlowski
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class profiler_static
{
private:
const char * name;
__int64 total_time,num_called;
public:
profiler_static(const char * p_name)
{
name = p_name;
total_time = 0;
num_called = 0;
}
~profiler_static()
{
char blah[512] = {0};
char total_time_text[128] = {0};
char num_text[128] = {0};
_i64toa(total_time,total_time_text,10);
_i64toa(num_called,num_text,10);
_snprintf(blah, sizeof(blah), "profiler: %s - %s cycles (executed %s times)\n",name,total_time_text,num_text);
OutputDebugStringA(blah);
}
void add_time(__int64 delta) {total_time+=delta;num_called++;}
};
class profiler_local
{
private:
static __int64 get_timestamp();
__int64 start;
profiler_static * owner;
public:
profiler_local(profiler_static * p_owner)
{
owner = p_owner;
start = get_timestamp();
}
~profiler_local()
{
__int64 end = get_timestamp();
owner->add_time(end-start);
}
};
__declspec(naked) __int64 profiler_local::get_timestamp()
{
__asm
{
rdtsc
ret
}
}
#define profiler(name) \
static profiler_static profiler_static_##name(#name); \
profiler_local profiler_local_##name(&profiler_static_##name);
#endif
#ifdef _DEBUG
#define MYDEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__)
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
//allocations to be of _CLIENT_BLOCK type
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new MYDEBUG_NEW
#endif
//
// useful functions
//////////////////////////////////////////////////////////////////////
inline BOOL WINAPI IsCharSpaceW(wchar_t c) { return (c == L' ' || c == L'\t'); }
inline bool IsTheW(const wchar_t *str) { if (str && (str[0] == L't' || str[0] == L'T') && (str[1] == L'h' || str[1] == L'H') && (str[2] == L'e' || str[2] == L'E') && (str[3] == L' ')) return true; else return false; }
#define SKIP_THE_AND_WHITESPACE(x) { wchar_t *save##x=(wchar_t*)x; while (IsCharSpaceW(*x) && *x) x++; if (IsTheW(x)) x+=4; while (IsCharSpaceW(*x)) x++; if (!*x) x=save##x; }
///#define SKIP_THE_AND_WHITESPACE(x) { while (!iswalnum(*x) && *x) x++; if (!_wcsnicmp(x,L"the ",4)) x+=4; while (*x == L' ') x++; }
int STRCMP_NULLOK(const wchar_t *pa, const wchar_t *pb) {
if (!pa) pa=L"";
else SKIP_THE_AND_WHITESPACE(pa)
if (!pb) pb=L"";
else SKIP_THE_AND_WHITESPACE(pb)
return lstrcmpi(pa,pb);
}
#undef SKIP_THE_AND_WHITESPACE
// convert Macintosh timestamp to windows timestamp
time_t mactime_to_wintime (const unsigned long mactime)
{
if (mactime != 0) return (time_t)(mactime - 2082844800);
else return (time_t)mactime;
}
// convert windows timestamp to Macintosh timestamp
unsigned long wintime_to_mactime (const __time64_t time)
{
return (unsigned long)(time + 2082844800);
}
char * UTF16_to_UTF8(wchar_t * str)
{
const unsigned int tempstrLen = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
char * tempstr=(char *)malloc(tempstrLen + 1);
int ret=WideCharToMultiByte( CP_UTF8, 0, str, -1, tempstr, tempstrLen, NULL, NULL );
tempstr[tempstrLen]='\0';
if (!ret) DWORD bob=GetLastError();
return tempstr;
}
wchar_t* UTF8_to_UTF16(char *str)
{
const unsigned int tempstrLen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
wchar_t *tempstr = (wchar_t*)malloc((tempstrLen * 2) + 2);
MultiByteToWideChar(CP_UTF8, 0, str, -1, tempstr, tempstrLen);
tempstr[tempstrLen] = '\0';
return tempstr;
}
// get 2 bytes from data, reversed
static __forceinline uint16_t rev2(const uint8_t * data)
{
uint16_t ret;
ret = ((uint16_t) data[1]) << 8;
ret += ((uint16_t) data[0]);
return ret;
}
static __forceinline void rev2(const unsigned short number, unsigned char * data)
{
data[1] = (unsigned char)(number >> 8) & 0xff;
data[0] = (unsigned char)number & 0xff;
}
// get 4 bytes from data, reversed
static __forceinline uint32_t rev4(const uint8_t * data)
{
unsigned long ret;
ret = ((unsigned long) data[3]) << 24;
ret += ((unsigned long) data[2]) << 16;
ret += ((unsigned long) data[1]) << 8;
ret += ((unsigned long) data[0]);
return ret;
}
// get 4 bytes from data
static __forceinline uint32_t get4(const uint8_t * data)
{
unsigned long ret;
ret = ((unsigned long) data[0]) << 24;
ret += ((unsigned long) data[1]) << 16;
ret += ((unsigned long) data[2]) << 8;
ret += ((unsigned long) data[3]);
return ret;
}
// get 8 bytes from data
static __forceinline unsigned __int64 get8(const uint8_t * data)
{
unsigned __int64 ret;
ret = get4(data);
ret = ret << 32;
ret += get4(&data[4]);
return ret;
}
// reverse 8 bytes in place
static __forceinline unsigned __int64 rev8(uint64_t number)
{
unsigned __int64 ret;
ret = (number&0x00000000000000FF) << 56;
ret+= (number&0x000000000000FF00) << 40;
ret+= (number&0x0000000000FF0000) << 24;
ret+= (number&0x00000000FF000000) << 8;
ret+= (number&0x000000FF00000000) >> 8;
ret+= (number&0x0000FF0000000000) >> 24;
ret+= (number&0x00FF000000000000) >> 40;
ret+= (number&0xFF00000000000000) >> 56;
return ret;
}
//write 4 bytes reversed
static __forceinline void rev4(const unsigned long number, uint8_t * data)
{
data[3] = (uint8_t)(number >> 24) & 0xff;
data[2] = (uint8_t)(number >> 16) & 0xff;
data[1] = (uint8_t)(number >> 8) & 0xff;
data[0] = (uint8_t)number & 0xff;
}
//write 4 bytes normal
static __forceinline void put4(const unsigned long number, uint8_t * data)
{
data[0] = (uint8_t)(number >> 24) & 0xff;
data[1] = (uint8_t)(number >> 16) & 0xff;
data[2] = (uint8_t)(number >> 8) & 0xff;
data[3] = (uint8_t)number & 0xff;
}
// write 8 bytes normal
static __forceinline void put8(const unsigned __int64 number, uint8_t * data)
{
data[0] = (uint8_t)(number >> 56) & 0xff;
data[1] = (uint8_t)(number >> 48) & 0xff;
data[2] = (uint8_t)(number >> 40) & 0xff;
data[3] = (uint8_t)(number >> 32) & 0xff;
data[4] = (uint8_t)(number >> 24) & 0xff;
data[5] = (uint8_t)(number >> 16) & 0xff;
data[6] = (uint8_t)(number >> 8) & 0xff;
data[7] = (uint8_t)number & 0xff;
}
// get 3 bytes from data, reversed
static __forceinline unsigned long rev3(const uint8_t * data)
{
unsigned long ret = 0;
ret += ((unsigned long) data[2]) << 16;
ret += ((unsigned long) data[1]) << 8;
ret += ((unsigned long) data[0]);
return ret;
}
//write 3 bytes normal (used in iTunesSD)
static __forceinline void put3(const unsigned long number, uint8_t * data)
{
data[0] = (uint8_t)(number >> 16) & 0xff;
data[1] = (uint8_t)(number >> 8) & 0xff;
data[2] = (uint8_t)number & 0xff;
}
//write 3 bytes reversed
static __forceinline void rev3(const unsigned long number, uint8_t * data)
{
data[2] = (uint8_t)(number >> 16) & 0xff;
data[1] = (uint8_t)(number >> 8) & 0xff;
data[0] = (uint8_t)number & 0xff;
}
// pass data and ptr, updates ptr automatically (by reference)
static __forceinline void write_uint32_t(uint8_t *data, size_t &offset, uint32_t value)
{
rev4(value, &data[offset]);
offset+=4;
}
static __forceinline uint32_t read_uint32_t(const uint8_t *data, size_t &offset)
{
const uint8_t *ptr = &data[offset];
offset+=4;
return rev4(ptr);
}
static __forceinline uint32_t read_uint16_t(const uint8_t *data, size_t &offset)
{
const uint8_t *ptr = &data[offset];
offset+=2;
return rev2(ptr);
}
static unsigned __int64 Generate64BitID()
{
GUID tmp;
CoCreateGuid(&tmp);
unsigned __int64 one = tmp.Data1;
unsigned __int64 two = tmp.Data2;
unsigned __int64 three = tmp.Data3;
unsigned __int64 four = rand();
return(one << 32 | two << 16 | three | four);
}
// useful function to convert from UTF16 to chars
char * UTF16_to_char(wchar_t * str, int length)
{
char * tempstr=(char *)malloc(length/2+1);
int ret=WideCharToMultiByte( CP_MACCP, 0, str, length/2, tempstr, length/2, "x", NULL );
tempstr[length/2]='\0';
if (!ret) DWORD bob=GetLastError();
return tempstr;
}
// Case insensitive version of wcsstr
wchar_t *wcsistr (const wchar_t *s1, const wchar_t *s2)
{
wchar_t *cp = (wchar_t*) s1;
wchar_t *s, *t, *endp;
wchar_t l, r;
endp = (wchar_t*)s1 + ( lstrlen(s1) - lstrlen(s2)) ;
while (cp && *cp && (cp <= endp))
{
s = cp;
t = (wchar_t*)s2;
while (s && *s && t && *t)
{
l = towupper(*s);
r = towupper(*t);
if (l != r)
break;
s++, t++;
}
if (*t == 0)
return cp;
cp = CharNext(cp);
}
return NULL;
}
//////////////////////////////////////////////////////////////////////
// iPodObj - Base for all iPod classes
//////////////////////////////////////////////////////////////////////
iPodObj::iPodObj() :
size_head(0),
size_total(0)
{
}
iPodObj::~iPodObj()
{
}
//////////////////////////////////////////////////////////////////////
// iPod_mhbd - iTunes database class
//////////////////////////////////////////////////////////////////////
iPod_mhbd::iPod_mhbd() :
unk1(1),
dbversion(0x0c), // iTunes 4.2 = 0x09, 4.5 = 0x0a, 4.7 = 0x0b, 4.7.1 = 0x0c, 0x0d, 0x13
children(2),
id(0),
platform(2),
language('ne'), // byte-swapped 'en'
library_id(0),
timezone(0),
audio_language(0),
unk80(1),
unk84(15),
subtitle_language(0),
unk164(0),
unk166(0),
unk168(0)
{
// get timezone info
_tzset(); // this function call ensures that _timezone global var is valid
timezone = -_timezone;
id = Generate64BitID();
mhsdsongs = new iPod_mhsd(1);
mhsdplaylists = new iPod_mhsd(3);
mhsdsmartplaylists = new iPod_mhsd(5);
}
iPod_mhbd::~iPod_mhbd()
{
delete mhsdsongs;
delete mhsdplaylists;
}
long iPod_mhbd::parse(const uint8_t *data)
{
size_t ptr=0;
//check mhbd header
if (_strnicmp((char *)&data[ptr],"mhbd",4)) return -1;
ptr+=4;
// get sizes
size_head=read_uint32_t(data, ptr);
size_total=read_uint32_t(data, ptr);
//ASSERT(size_head == 0xbc);
// get unk's and numchildren
unk1=read_uint32_t(data, ptr);
dbversion=read_uint32_t(data, ptr);
children=read_uint32_t(data, ptr);
id=rev8(get8(&data[ptr]));
ptr+=8;
if(id == 0)
{
// Force the id to be a valid value.
// This may not always be the right thing to do, but I can't think of any reason why it wouldn't be ok...
id = Generate64BitID();
}
platform=read_uint16_t(data, ptr);
ptr = 0x46;
language = read_uint16_t(data, ptr);
library_id = rev8(get8(&data[ptr]));
ptr+=8;
unk80 = read_uint32_t(data, ptr);
unk84 = read_uint32_t(data, ptr);
ptr = 0xA0;
audio_language = read_uint16_t(data, ptr);
subtitle_language =read_uint16_t(data, ptr);
unk164 = read_uint16_t(data, ptr);
unk166 = read_uint16_t(data, ptr);
unk168 = read_uint16_t(data, ptr);
// timezone is at 0x6c, but we want to calculate this based on the computer timezone
// TODO: 4 byte field at 0xA0 that contains FFFFFFFF for the ipod shuffle I'm playing with
//if (children != 2) return -1;
//skip over nulls
ptr=size_head;
// get the mhsd's
bool parsedPlaylists = false;
for(unsigned int i=0; i<children; i++)
{
iPod_mhsd * mhsd = new iPod_mhsd(0);
long ret = mhsd->parse(&data[ptr]);
if(ret<0) return ret;
else ptr+=ret;
if(mhsd->index == 1)
{
delete mhsdsongs;
mhsdsongs = mhsd;
}
else if(mhsd->index == 3 && !parsedPlaylists)
{
delete mhsdplaylists;
mhsdplaylists = mhsd;
parsedPlaylists = true;
}
else if(mhsd->index == 2 && !parsedPlaylists)
{
delete mhsdplaylists;
mhsdplaylists = mhsd;
}
else if(mhsd->index == 5)
{
delete mhsdsmartplaylists;
mhsdsmartplaylists = mhsd;
}
else
{
delete mhsd;
}
}
return size_total;
}
long iPod_mhbd::write(unsigned char * data, const unsigned long datasize)
{
return write(data,datasize,NULL);
}
extern void GenerateHash(unsigned char *pFWID, unsigned char *pDataBase, long lSize, unsigned char *pHash);
long iPod_mhbd::write(unsigned char * data, const unsigned long datasize, unsigned char *fwid)
{
//const unsigned int headsize=0xbc; // for db version 0x19
const unsigned int headsize=188; // for db version 0x2A
// check for header size
if (headsize>datasize) return -1;
long ptr=0;
//write mhbd header
data[0]='m';data[1]='h';data[2]='b';data[3]='d';
ptr+=4;
// write sizes
rev4(headsize,&data[ptr]); // header size
ptr+=4;
rev4(0x00,&data[ptr]); // placeholder for total size (fill in later)
ptr+=4;
//write unks
rev4(unk1,&data[ptr]);
ptr+=4;
rev4(0x2a/*dbversion*/,&data[ptr]);
ptr+=4;
//write numchildren
//ASSERT (children == 2); // seen no other case in an iTunesDB yet
children = 4;
rev4(children,&data[ptr]);
ptr+=4;
// fill this in later (it's the db id, it has to be 0 for the hash generation)
put8(0,&data[ptr]);
ptr+=8;
rev2(2, &data[ptr]); // platform (2 == Windows)
ptr+=2;
// fill up the rest of the header with nulls
for (unsigned int i=ptr;i<headsize;i++)
data[i]=0;
ptr=headsize;
rev2(1, &data[0x30]);
rev2(language, &data[0x46]);
put8(library_id, &data[0x48]);
rev4(unk80, &data[0x50]);
rev4(unk84, &data[0x54]);
rev4(timezone, &data[0x6c]);
rev2(2, &data[0x70]);
rev2(audio_language, &data[0xA0]);
rev2(subtitle_language, &data[0xA2]);
rev2(unk164, &data[0xA4]);
rev2(unk166, &data[0xA6]);
rev2(unk168, &data[0xA8]);
long ret;
// write the mhla (album list)
iPod_mhsd mhsd_mhla(4);
iPod_mhla *album_list = mhsd_mhla.mhla;
iPod_mhlt::mhit_map_t::const_iterator begin = mhsdsongs->mhlt->mhit.begin();
iPod_mhlt::mhit_map_t::const_iterator end = mhsdsongs->mhlt->mhit.end();
for(iPod_mhlt::mhit_map_t::const_iterator it = begin; it != end; it++)
{
wchar_t * artist = L"";
wchar_t * album = L"";
iPod_mhit *m = static_cast<iPod_mhit*>((*it).second);
iPod_mhod *mhartist = m->FindString(MHOD_ARTIST);
iPod_mhod *mhalbum = m->FindString(MHOD_ALBUM);
if(mhartist && mhartist->str)
artist = mhartist->str;
if(mhalbum && mhalbum->str)
album = mhalbum->str;
m->album_id = mhsd_mhla.mhla->GetAlbumId(artist, album);
}
ret=mhsd_mhla.write(&data[ptr], datasize-ptr, 4);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
// write the mhsd's
ret=mhsdsongs->write(&data[ptr], datasize-ptr);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
ret=mhsdplaylists->write(&data[ptr], datasize-ptr, 3);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
ret=mhsdplaylists->write(&data[ptr], datasize-ptr, 2);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
if(mhsdsmartplaylists->mhlp_smart) {
ret=mhsdsmartplaylists->write(&data[ptr], datasize-ptr, 5);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
} else children--;
// fix the total size
rev4(ptr,&data[8]);
rev4(children,&data[20]);
if(fwid)
GenerateHash(fwid,data,ptr,data+0x58); // fuck you, gaydickian
put8(rev8(id),&data[0x18]); // put this back in -- it has to be 0 for the hash generation.
return ptr;
}
//////////////////////////////////////////////////////////////////////
// iPod_mhsd - Holds tracklists and playlists
//////////////////////////////////////////////////////////////////////
iPod_mhsd::iPod_mhsd() :
index(0),
mhlt(NULL),
mhlp(NULL),
mhlp_smart(NULL),
mhla(NULL)
{
}
iPod_mhsd::iPod_mhsd(int newindex) :
index(newindex),
mhlt(NULL),
mhlp(NULL),
mhlp_smart(NULL),
mhla(NULL)
{
switch(newindex)
{
case 1: mhlt=new iPod_mhlt(); break;
case 2:
case 3:
case 5: mhlp=new iPod_mhlp(); break;
case 4: mhla=new iPod_mhla(); break;
default: index=0;
}
}
iPod_mhsd::~iPod_mhsd()
{
delete mhlt;
delete mhlp;
delete mhla;
}
long iPod_mhsd::parse(const uint8_t *data)
{
unsigned long ptr=0;
//check mhsd header
if (_strnicmp((char *)&data[ptr],"mhsd",4)) return -1;
ptr+=4;
// get sizes
size_head=rev4(&data[ptr]);
ptr+=4;
size_total=rev4(&data[ptr]);
ptr+=4;
ASSERT(size_head == 0x60);
// get index number
index=rev4(&data[ptr]);
ptr+=4;
// skip null padding
ptr=size_head;
long ret;
// check to see if this is a holder for an mhlt or an mhlp
if (!_strnicmp((char *)&data[ptr],"mhlt",4))
{
if (mhlt==NULL)
{
mhlt=new iPod_mhlt();
//index=1;
}
ret=mhlt->parse(&data[ptr]);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
}
else if (!_strnicmp((char *)&data[ptr],"mhlp",4) && (index == 2 || index == 3))
{
if (mhlp==NULL)
{
mhlp=new iPod_mhlp();
if(index != 2) index=3;
}
ret=mhlp->parse(&data[ptr]);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
}
else if (!_strnicmp((char *)&data[ptr],"mhlp",4) && index == 5) // smart playlists
{
if (mhlp_smart==NULL)
mhlp_smart=new iPod_mhlp();
ret=mhlp_smart->parse(&data[ptr]);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
}
else {
}
//return -1;
//if (ptr != size_total) return -1;
return size_total;
}
long iPod_mhsd::write(unsigned char * data, const unsigned long datasize, int index)
{
const unsigned int headsize=0x60;
// check for header size
if (headsize>datasize) return -1;
long ptr=0;
//write mhsd header
data[0]='m';data[1]='h';data[2]='s';data[3]='d';
ptr+=4;
// write sizes
rev4(headsize,&data[ptr]); // header size
ptr+=4;
rev4(0x00,&data[ptr]); // placeholder for total size (fill in later)
ptr+=4;
// write index number
rev4(index,&data[ptr]);
ptr+=4;
// fill up the rest of the header with nulls
for (unsigned int i=ptr;i<headsize;i++)
data[i]=0;
ptr=headsize;
// write out the songs or the playlists, depending on index
long ret;
if (index==1) // mhlt
ret=mhlt->write(&data[ptr],datasize-ptr);
else if (index==2 || index==3) // mhlp
ret=mhlp->write(&data[ptr],datasize-ptr,index);
else if (index == 4) // mhla
ret=mhla->write(&data[ptr],datasize-ptr);
else if (index==5) // mhlp_smart
ret=mhlp_smart->write(&data[ptr],datasize-ptr,3);
else return -1;
ASSERT(ret>=0);
if (ret<0) return ret;
else ptr+=ret;
// fix the total size
rev4(ptr,&data[8]);
return ptr;
}
//////////////////////////////////////////////////////////////////////
// iPod_mhlt - TrackList class
//////////////////////////////////////////////////////////////////////
iPod_mhlt::iPod_mhlt() :
mhit(),
next_mhit_id(100)
{
}
iPod_mhlt::~iPod_mhlt()
{
// It is unnecessary (and slow) to clear the map, since the object is being destroyed anyway
ClearTracks(false);
}
long iPod_mhlt::parse(const uint8_t *data)
{
long ptr=0;
//check mhlt header
if (_strnicmp((char *)&data[ptr],"mhlt",4)) return -1;
ptr+=4;
// get size
size_head=rev4(&data[ptr]);
ptr+=4;
ASSERT(size_head == 0x5c);
// get num children (num songs on iPod)
const unsigned long children=rev4(&data[ptr]); // Only used locally - child count is obtained from the mhit list
ptr+=4;
//skip nulls
ptr=size_head;
long ret;
// get children one by one
for (unsigned long i=0;i<children;i++)
{
iPod_mhit *m = new iPod_mhit;
ret=m->parse(&data[ptr]);
ASSERT(ret >= 0);
if (ret<0)
{
delete m;
return ret;
}
ptr+=ret;
mhit.insert(mhit_value_t(m->id, m));
mhit_indexer.push_back(m->id);
}
if (!mhit.empty())
{
//InterlockedExchange(&next_mhit_id, mhit.back().first);
uint32_t id = mhit_indexer[mhit_indexer.size() - 1];
InterlockedExchange(&next_mhit_id, id);
}
return ptr;
}
uint32_t iPod_mhlt::GetNextID()
{
return (uint32_t)InterlockedIncrement(&next_mhit_id);
}
long iPod_mhlt::write(unsigned char * data, const unsigned long datasize)
{
const unsigned int headsize=0x5c;
// check for header size
if (headsize>datasize) return -1;
long ptr=0;
//write mhlt header
data[0]='m';data[1]='h';data[2]='l';data[3]='t';
ptr+=4;
// write size
rev4(headsize,&data[ptr]); // header size
ptr+=4;
// write numchildren (numsongs)
const unsigned long children = GetChildrenCount();
rev4(children,&data[ptr]);
ptr+=4;
// fill up the rest of the header with nulls
for (unsigned long i=ptr;i<headsize;i++)
data[i]=0;
ptr=headsize;
long ret;
// write children one by one
mhit_map_t::const_iterator begin = mhit.begin();
mhit_map_t::const_iterator end = mhit.end();
for(mhit_map_t::const_iterator it = begin; it != end; it++)
{
iPod_mhit *m = static_cast<iPod_mhit*>(it->second);
#ifdef _DEBUG
const unsigned int mapID = (*it).first;
ASSERT(mapID == m->id);
#endif
ret=m->write(&data[ptr],datasize-ptr);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
}
return ptr;
}
iPod_mhit * iPod_mhlt::NewTrack()
{
iPod_mhit *track = new iPod_mhit;
if (track != NULL)
{
track->addedtime = wintime_to_mactime(time(0));
track->id = GetNextID();
}
return track;
}
void iPod_mhlt::AddTrack(iPod_mhit *new_track)
{
mhit_indexer.push_back(new_track->id);
mhit.insert(mhit_value_t(new_track->id, new_track));
}
bool iPod_mhlt::DeleteTrack(const unsigned long index)
{
//unsigned int i=0;
//for(mhit_map_t::const_iterator it = mhit.begin(); it != mhit.end(); it++, i++)
//{
// if(i == index)
// {
// iPod_mhit *m = static_cast<iPod_mhit*>(it->second);
// return(DeleteTrackByID(m->id));
// }
//}
//return false;
if (index > mhit_indexer.size())
{
return false;
}
auto key = mhit_indexer[index];
auto it = mhit.find(key);
if (mhit.end() == it)
{
return false;
}
return DeleteTrackByID(it->first);
}
bool iPod_mhlt::DeleteTrackByID(const unsigned long id)
{
mhit_map_t::iterator it = mhit.find(id);
if(it != mhit.end())
{
iPod_mhit *m = static_cast<iPod_mhit*>(it->second);
mhit.erase(it);
// remove also from indexer!!
for (size_t n = 0; n < mhit_indexer.size(); ++n)
{
if (id == mhit_indexer[n])
{
mhit_indexer.erase(mhit_indexer.begin() + n);
break;
}
}
delete m;
return true;
}
return false;
}
iPod_mhit * iPod_mhlt::GetTrack(uint32_t index) const
{
//mhit_map_t::value_type value = mhit.at(index);
//return value.second;
if (index > mhit_indexer.size())
{
return nullptr;
}
uint32_t key = mhit_indexer[index];
auto it = mhit.find(key);
if (mhit.end() == it)
{
return nullptr;
}
return it->second;
}
iPod_mhit * iPod_mhlt::GetTrackByID(const unsigned long id)
{
mhit_map_t::const_iterator it = mhit.find(id);
if(it == mhit.end())
return NULL;
return static_cast<iPod_mhit*>(it->second);
}
bool iPod_mhlt::ClearTracks(const bool clearMap)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhlt_ClearTracks);
#endif
mhit_map_t::const_iterator begin = mhit.begin();
mhit_map_t::const_iterator end = mhit.end();
for(mhit_map_t::const_iterator it = begin; it != end; it++)
{
delete static_cast<iPod_mhit*>(it->second);
}
if (clearMap)
{
mhit.clear();
mhit_indexer.clear();
}
return true;
}
//////////////////////////////////////////////////////////////////////
// iPod_mhit - Holds info about a song
//////////////////////////////////////////////////////////////////////
iPod_mhit::iPod_mhit() :
id(0),
visible(1),
filetype(0),
vbr(0),
type(0),
compilation(0),
stars(0),
lastmodifiedtime(0),
size(0),
length(0),
tracknum(0),
totaltracks(0),
year(0),
bitrate(0),
samplerate(0),
samplerate_fixedpoint(0),
volume(0),
starttime(0),
stoptime(0),
soundcheck(0),
playcount(0),
playcount2(0),
lastplayedtime(0),
cdnum(0),
totalcds(0),
userID(0),
addedtime(0),
bookmarktime(0),
dbid(0),
BPM(0),
app_rating(0),
checked(0),
unk9(0),
artworkcount(0),
artworksize(0),
unk11(0),
samplerate2(0),
releasedtime(0),
unk14(0),
unk15(0),
unk16(0),
skipcount(0),
skippedtime(0),
hasArtwork(2), // iTunes 4.7.1 always seems to write 2 for unk19
skipShuffle(0),
rememberPosition(0),
unk19(0),
dbid2(0),
lyrics_flag(0),
movie_flag(0),
mark_unplayed(0),
unk20(0),
unk21(0),
pregap(0),
samplecount(0),
unk25(0),
postgap(0),
unk27(0),
mediatype(0),
seasonNumber(0),
episodeNumber(0),
unk31(0),
unk32(0),
unk33(0),
unk34(0),
unk35(0),
unk36(0),
unk37(0),
gaplessData(0),
unk39(0),
albumgapless(0),
trackgapless(0),
unk40(0),
unk41(0),
unk42(0),
unk43(0),
unk44(0),
unk45(0),
unk46(0),
album_id(0),
unk48(0),
unk49(0),
unk50(0),
unk51(0),
unk52(0),
unk53(0),
unk54(0),
unk55(0),
unk56(0),
mhii_link(0),
mhod()
{
// Create a highly randomized 64 bit value for the dbID
dbid = Generate64BitID();
dbid2 = dbid;
for(int i=0; i<25; i++) mhodcache[i]=NULL;
mhod.reserve(8);
}
iPod_mhit::~iPod_mhit()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhit_destructor);
#endif
const unsigned long count = GetChildrenCount();
for (unsigned long i=0;i<count;i++)
delete mhod[i];
}
long iPod_mhit::parse(const uint8_t *data)
{
size_t ptr=0;
unsigned long temp=0;
//check mhit header
if (_strnicmp((char *)&data[ptr],"mhit",4)) return -1;
ptr+=4;
// get sizes
size_head=read_uint32_t(data, ptr);
size_total=read_uint32_t(data, ptr);
ASSERT(size_head == 0x9c || size_head == 0xf4 || size_head == 0x148 || size_head == 0x184 || size_head == 0x248); // if this is false, this is a new mhit format
// get rest of data
unsigned long mhodnum=read_uint32_t(data, ptr); // Only used locally
id=read_uint32_t(data, ptr);
visible=read_uint32_t(data, ptr);
filetype=read_uint32_t(data, ptr);
vbr = data[ptr++];
type = data[ptr++];
compilation = data[ptr++];
stars = data[ptr++];
lastmodifiedtime=read_uint32_t(data, ptr);
size=read_uint32_t(data, ptr);
length=read_uint32_t(data, ptr);
tracknum=read_uint32_t(data, ptr);
totaltracks=read_uint32_t(data, ptr);
year=read_uint32_t(data, ptr);
bitrate=read_uint32_t(data, ptr);
temp=rev4(&data[ptr]);
ptr+=4;
samplerate = (uint16_t)(temp >> 16);
samplerate_fixedpoint = (uint16_t)(temp & 0x0000ffff);
volume=read_uint32_t(data, ptr);
starttime=read_uint32_t(data, ptr);
stoptime=read_uint32_t(data, ptr);
soundcheck=read_uint32_t(data, ptr);
playcount=read_uint32_t(data, ptr);
playcount2=read_uint32_t(data, ptr);
lastplayedtime=read_uint32_t(data, ptr);
cdnum=read_uint32_t(data, ptr);;
totalcds=read_uint32_t(data, ptr);
userID=read_uint32_t(data, ptr);
addedtime=read_uint32_t(data, ptr);
bookmarktime=read_uint32_t(data, ptr);
dbid=rev8(get8(&data[ptr]));
ptr+=8;
if(dbid == 0)
{
// Force the dbid to be a valid value.
// This may not always be the right thing to do, but I can't think of any reason why it wouldn't be ok...
dbid = Generate64BitID();
}
temp=rev4(&data[ptr]);
BPM=temp>>16;
app_rating=(temp&0xff00) >> 8;
checked = (uint8_t)(temp&0xff);
ptr+=4;
artworkcount=rev2(&data[ptr]);
ptr+=2;
unk9=rev2(&data[ptr]);
ptr+=2;
artworksize=read_uint32_t(data, ptr);
unk11=read_uint32_t(data, ptr);
memcpy(&samplerate2, &data[ptr], sizeof(float));
ptr+=4;
releasedtime=read_uint32_t(data, ptr);
unk14=read_uint32_t(data, ptr);
unk15=read_uint32_t(data, ptr);
unk16=read_uint32_t(data, ptr);
// Newly added as of dbversion 0x0c
if(size_head >= 0xf4)
{
skipcount=read_uint32_t(data, ptr);
skippedtime=read_uint32_t(data, ptr);
hasArtwork=data[ptr++];
skipShuffle=data[ptr++];
rememberPosition=data[ptr++];
unk19=data[ptr++];
dbid2=rev8(get8(&data[ptr]));
ptr+=8;
if(dbid2 == 0)
dbid2 = dbid;
lyrics_flag=data[ptr++];
movie_flag=data[ptr++];
mark_unplayed=data[ptr++];
unk20=data[ptr++];
unk21=read_uint32_t(data, ptr); // 180
pregap=read_uint32_t(data, ptr);
samplecount=rev8(get8(&data[ptr])); //sample count
ptr+=8;
unk25=read_uint32_t(data, ptr); // 196
postgap=read_uint32_t(data, ptr);
unk27=read_uint32_t(data, ptr);
mediatype=read_uint32_t(data, ptr);
seasonNumber=read_uint32_t(data, ptr);
episodeNumber=read_uint32_t(data, ptr);
unk31=read_uint32_t(data, ptr);
unk32=read_uint32_t(data, ptr);
unk33=read_uint32_t(data, ptr);
unk34=read_uint32_t(data, ptr);
unk35=read_uint32_t(data, ptr);
unk36=read_uint32_t(data, ptr);
}
if(size_head >= 0x148)
{ // dbversion 0x13
unk37=read_uint32_t(data, ptr);
gaplessData=read_uint32_t(data, ptr);
unk39=read_uint32_t(data, ptr);
trackgapless = read_uint16_t(data, ptr);
albumgapless = read_uint16_t(data, ptr);
unk40=read_uint32_t(data, ptr); // 260
unk41=read_uint32_t(data, ptr); // 264
unk42=read_uint32_t(data, ptr); // 268
unk43=read_uint32_t(data, ptr); // 272
unk44=read_uint32_t(data, ptr); // 276
unk45=read_uint32_t(data, ptr); // 280
unk46=read_uint32_t(data, ptr); // 284
album_id=read_uint32_t(data, ptr); // 288 - libgpod lists "album_id"
unk48=read_uint32_t(data, ptr); // 292 - libgpod lists first half of an id
unk49=read_uint32_t(data, ptr); // 296 - libgpod lists second half of an id
unk50=read_uint32_t(data, ptr); // 300 - libgpod lists file size
unk51=read_uint32_t(data, ptr); // 304
unk52=read_uint32_t(data, ptr); // 308 - libgpod mentions 8 bytes of 0x80
unk53=read_uint32_t(data, ptr); // 312 - libgpod mentions 8 bytes of 0x80
unk54=read_uint32_t(data, ptr); // 316
unk55=read_uint32_t(data, ptr); // 320
unk56=read_uint32_t(data, ptr); // 324
}
if(size_head >= 0x184)
{
ptr = 0x148; // line it up, just in case
ptr += 22; // dunno what the first 22 bytes are
album_id = read_uint16_t(data, ptr);
mhii_link = read_uint32_t(data, ptr);
}
#ifdef _DEBUG
// If these trigger an assertion, something in the database format has changed/been added
ASSERT(visible == 1);
ASSERT(unk11 == 0);
ASSERT(unk16 == 0);
// ASSERT(unk19 == 0);
// ASSERT(hasArtwork == 2); // iTunes always sets unk19 to 2, but older programs won't have set it
ASSERT(unk20 == 0);
ASSERT(unk21 == 0);
ASSERT(unk25 == 0);
// ASSERT(unk27 == 0);
//ASSERT(unk31 == 0);
ASSERT(unk32 == 0);
ASSERT(unk33 == 0);
ASSERT(unk34 == 0);
ASSERT(unk35 == 0);
ASSERT(unk36 == 0);
ASSERT(unk37 == 0);
ASSERT(unk39 == 0);
ASSERT(unk40 == 0);
ASSERT(unk41 == 0);
ASSERT(unk42 == 0);
ASSERT(unk43 == 0);
ASSERT(unk44 == 0);
ASSERT(unk45 == 0);
ASSERT(unk46 == 0);
// ASSERT(unk47 == 0);
//ASSERT(unk48 == 0);
// ASSERT(unk49 == 0);
ASSERT(unk50 == 0 || unk50 == size);
ASSERT(unk51 == 0);
// ASSERT(unk52 == 0);
ASSERT(unk53 == 0 || unk53 == 0x8080 || unk53 == 0x8081);
ASSERT(unk54 == 0);
ASSERT(unk55 == 0);
ASSERT(unk56 == 0);
#endif
// skip nulls
ptr=size_head;
long ret;
for (unsigned long i=0;i<mhodnum;i++)
{
iPod_mhod *m = new iPod_mhod;
ret=m->parse(&data[ptr]);
ASSERT(ret >= 0);
if (ret<0)
{
delete m;
return ret;
}
ptr+=ret;
mhod.push_back(m);
if(m->type <= 25 && m->type >= 1) mhodcache[m->type-1] = m;
}
return size_total;
}
long iPod_mhit::write(unsigned char * data, const unsigned long datasize)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhit_write);
#endif
//const unsigned int headsize=0x148; // was 0x9c in db version <= 0x0b
const unsigned int headsize=0x184; // db version 0x19
// check for header size
if (headsize>datasize) return -1;
size_t ptr=0;
//write mhlt header
data[0]='m';data[1]='h';data[2]='i';data[3]='t';
ptr+=4;
// write sizes
write_uint32_t(data, ptr, headsize); // header size
write_uint32_t(data, ptr, 0); // placeholder for total size (fill in later)
unsigned long temp, i;
// Remove all empty MHOD strings before continuing
for(i=0;i<GetChildrenCount();i++)
{
iPod_mhod *m = mhod[i];
ASSERT(m != NULL);
if(m == NULL)
continue;
if(m->type < 50 && m->length == 0)
{
//DeleteString(m->type);
//i = 0;
}
}
// write stuff out
unsigned long mhodnum = GetChildrenCount();
write_uint32_t(data, ptr, mhodnum);
write_uint32_t(data, ptr, id);
write_uint32_t(data, ptr, visible);
if(filetype == 0)
{
iPod_mhod *mhod = FindString(MHOD_LOCATION);
if(mhod)
{
filetype = GetFileTypeID(mhod->str);
}
}
write_uint32_t(data, ptr, filetype);
vbr = data[ptr++] = vbr;
type = data[ptr++] = type;
compilation = data[ptr++] = compilation;
stars = data[ptr++] = stars;
write_uint32_t(data, ptr, lastmodifiedtime);
write_uint32_t(data, ptr, size);
write_uint32_t(data, ptr, length);
write_uint32_t(data, ptr, tracknum);
write_uint32_t(data, ptr, totaltracks);
write_uint32_t(data, ptr, year);
write_uint32_t(data, ptr, bitrate);
temp = samplerate << 16 | samplerate_fixedpoint & 0x0000ffff;
rev4(temp,&data[ptr]);
ptr+=4;
write_uint32_t(data, ptr, volume);
write_uint32_t(data, ptr, starttime);
write_uint32_t(data, ptr, stoptime);
write_uint32_t(data, ptr, soundcheck);
write_uint32_t(data, ptr, playcount);
write_uint32_t(data, ptr, playcount2);
write_uint32_t(data, ptr, lastplayedtime);
write_uint32_t(data, ptr, cdnum);
write_uint32_t(data, ptr, totalcds);
write_uint32_t(data, ptr, userID);
write_uint32_t(data, ptr, addedtime);
write_uint32_t(data, ptr, bookmarktime);
put8(rev8(dbid),&data[ptr]);
ptr+=8;
temp = BPM << 16 | (app_rating & 0xff) << 8 | (checked & 0xff);
write_uint32_t(data, ptr, temp);
rev2(artworkcount, &data[ptr]);
ptr+=2;
rev2(unk9, &data[ptr]);
ptr+=2;
write_uint32_t(data, ptr, artworksize);
write_uint32_t(data, ptr, unk11);
// If samplerate2 is not set, base it off of samplerate
if(samplerate2 == 0)
{
// samplerate2 is the binary representation of the samplerate, as a 32 bit float
const float foo = (float)samplerate;
memcpy(&data[ptr], &foo, 4);
}
else
{
memcpy(&data[ptr], &samplerate2, 4);
}
ptr+=4;
rev4(releasedtime,&data[ptr]);
ptr+=4;
rev4(unk14,&data[ptr]);
ptr+=4;
rev4(unk15,&data[ptr]);
ptr+=4;
rev4(unk16,&data[ptr]);
ptr+=4;
// New data as of dbversion 0x0c
if(headsize >= 0xf4)
{
rev4(skipcount,&data[ptr]);
ptr+=4;
rev4(skippedtime,&data[ptr]);
ptr+=4;
data[ptr++]=hasArtwork;
data[ptr++]=skipShuffle;
data[ptr++]=rememberPosition;
data[ptr++]=unk19;
put8(rev8(dbid2),&data[ptr]);
ptr+=8;
data[ptr++]=lyrics_flag;
data[ptr++]=movie_flag;
data[ptr++]=mark_unplayed;
data[ptr++]=unk20;
rev4(unk21,&data[ptr]);
ptr+=4;
rev4(pregap,&data[ptr]);
ptr+=4;
put8(rev8(samplecount),&data[ptr]);
ptr+=8;
rev4(unk25,&data[ptr]);
ptr+=4;
rev4(postgap,&data[ptr]);
ptr+=4;
rev4(unk27,&data[ptr]);
ptr+=4;
rev4(mediatype,&data[ptr]);
ptr+=4;
rev4(seasonNumber,&data[ptr]);
ptr+=4;
rev4(episodeNumber,&data[ptr]);
ptr+=4;
rev4(unk31,&data[ptr]);
ptr+=4;
rev4(unk32,&data[ptr]);
ptr+=4;
rev4(unk33,&data[ptr]);
ptr+=4;
rev4(unk34,&data[ptr]);
ptr+=4;
rev4(unk35,&data[ptr]);
ptr+=4;
rev4(unk36,&data[ptr]);
ptr+=4;
}
if(headsize >= 0x148)
{
rev4(unk37,&data[ptr]); ptr+=4;
rev4(gaplessData,&data[ptr]); ptr+=4;
rev4(unk39,&data[ptr]); ptr+=4;
temp = albumgapless << 16 | (trackgapless & 0xffff);
rev4(temp, &data[ptr]); ptr+=4;
rev4(unk40,&data[ptr]); ptr+=4;
rev4(unk41,&data[ptr]); ptr+=4;
rev4(unk42,&data[ptr]); ptr+=4;
rev4(unk43,&data[ptr]); ptr+=4;
rev4(unk44,&data[ptr]); ptr+=4;
rev4(unk45,&data[ptr]); ptr+=4;
rev4(unk46,&data[ptr]); ptr+=4;
rev4(album_id,&data[ptr]); ptr+=4;
rev4(unk48,&data[ptr]); ptr+=4;
rev4(unk49,&data[ptr]); ptr+=4;
rev4(size,&data[ptr]); ptr+=4;
rev4(unk51,&data[ptr]); ptr+=4;
rev4(unk52,&data[ptr]); ptr+=4;
rev4(unk53,&data[ptr]); ptr+=4;
rev4(unk54,&data[ptr]); ptr+=4;
rev4(unk55,&data[ptr]); ptr+=4;
rev4(unk56,&data[ptr]); ptr+=4;
}
if (headsize >= 0x184)
{
ptr = 0x148; // line it up, just in case
memset(&data[ptr], 0, 22); // write a bunch of zeroes
ptr+=22;
rev2(album_id, &data[ptr]); ptr+=2;
rev4(mhii_link,&data[ptr]); ptr+=4;
memset(&data[ptr], 0, 32); // write a bunch of zeroes
ptr+=32;
}
ASSERT(ptr==headsize); // if this ain't true, I screwed up badly somewhere above
long ret;
for (i=0;i<mhodnum;i++)
{
ret=mhod[i]->write(&data[ptr],datasize-ptr);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
}
// fix the total size
rev4(ptr,&data[8]);
return ptr;
}
iPod_mhod * iPod_mhit::AddString(const int type)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhit_AddString);
#endif
iPod_mhod * m;
if (type)
{
m = FindString(type);
if (m != NULL)
{
return m;
}
}
m=new iPod_mhod;
if (m!=NULL && type) m->type=type;
mhod.push_back(m);
if(m->type <= 25 && m->type >= 1) mhodcache[m->type-1] = m;
return m;
}
iPod_mhod * iPod_mhit::FindString(const unsigned long type) const
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhit_FindString);
#endif
if(type <= 25 && type >= 1) return mhodcache[type-1];
const unsigned long children = GetChildrenCount();
for (unsigned long i=0;i<children;i++)
{
if (mhod[i]->type == type) return mhod[i];
}
return NULL;
}
unsigned long iPod_mhit::DeleteString(const unsigned long type)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhit_DeleteString);
#endif
if(type <= 25 && type >= 1) mhodcache[type-1] = NULL;
unsigned long count=0;
for (unsigned long i=0; i != GetChildrenCount(); i++)
{
if (mhod[i]->type == type)
{
iPod_mhod * m = mhod.at(i);
mhod.erase(mhod.begin() + i);
delete m;
i = i > 0 ? i - 1 : 0; // do this to ensure that it checks the new entry in position i next
count++;
}
}
return count;
}
unsigned int iPod_mhit::GetFileTypeID(const wchar_t *filename)
{
ASSERT(filename);
if(filename == NULL)
return(0);
// Incredibly, this is really the file extension as ASCII characters
// e.g. 0x4d = 'M', 0x50 = 'P', 0x33 = '3', 0x20 = '<space>'
if(wcsistr(filename, L".mp3") != NULL)
return FILETYPE_MP3;
else if(wcsistr(filename, L".m4a") != NULL)
return FILETYPE_M4A;
else if(wcsistr(filename, L".m4b") != NULL)
return(0x4d344220);
else if(wcsistr(filename, L".m4p") != NULL)
return(0x4d345020);
else if(wcsistr(filename, L".wav") != NULL)
return FILETYPE_WAV;
return(0);
}
iPod_mhit& iPod_mhit::operator=(const iPod_mhit& src)
{
Duplicate(&src,this);
return *this;
}
void iPod_mhit::Duplicate(const iPod_mhit *src, iPod_mhit *dst)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhit_Duplicate);
#endif
if(src == NULL || dst == NULL)
return;
dst->id = src->id;
dst->visible = src->visible;
dst->filetype = src->filetype;
dst->vbr = src->vbr;
dst->type = src->type;
dst->compilation = src->compilation;
dst->stars = src->stars;
dst->lastmodifiedtime = src->lastmodifiedtime;
dst->size = src->size;
dst->length = src->length;
dst->tracknum = src->tracknum;
dst->totaltracks = src->totaltracks;
dst->year = src->year;
dst->bitrate = src->bitrate;
dst->samplerate = src->samplerate;
dst->samplerate_fixedpoint = src->samplerate_fixedpoint;
dst->volume = src->volume;
dst->starttime = src->starttime;
dst->stoptime = src->stoptime;
dst->soundcheck = src->soundcheck;
dst->playcount = src->playcount;
dst->playcount2 = src->playcount2;
dst->lastplayedtime = src->lastplayedtime;
dst->cdnum = src->cdnum;
dst->totalcds = src->totalcds;
dst->userID = src->userID;
dst->addedtime = src->addedtime;
dst->bookmarktime = src->bookmarktime;
dst->dbid = src->dbid;
dst->BPM = src->BPM;
dst->app_rating = src->app_rating;
dst->checked = src->checked;
dst->unk9 = src->unk9;
dst->artworksize = src->artworksize;
dst->unk11 = src->unk11;
dst->samplerate2 = src->samplerate2;
dst->releasedtime = src->releasedtime;
dst->unk14 = src->unk14;
dst->unk15 = src->unk15;
dst->unk16 = src->unk16;
dst->skipcount = src->skipcount;
dst->skippedtime = src->skippedtime;
dst->hasArtwork = src->hasArtwork;
dst->skipShuffle = src->skipShuffle;
dst->rememberPosition = src->rememberPosition;
dst->unk19 = src->unk19;
dst->dbid2 = src->dbid2;
dst->lyrics_flag = src->lyrics_flag;
dst->movie_flag = src->movie_flag;
dst->mark_unplayed = src->mark_unplayed;
dst->unk20 = src->unk20;
dst->unk21 = src->unk21;
dst->pregap = src->pregap;
dst->samplecount = src->samplecount;
dst->unk25 = src->unk25;
dst->postgap = src->postgap;
dst->unk27 = src->unk27;
dst->mediatype = src->mediatype;
dst->seasonNumber = src->seasonNumber;
dst->episodeNumber = src->episodeNumber;
dst->unk31 = src->unk31;
dst->unk32 = src->unk32;
dst->unk33 = src->unk33;
dst->unk34 = src->unk34;
dst->unk35 = src->unk35;
dst->unk36 = src->unk36;
dst->unk37 = src->unk37;
dst->gaplessData = src->gaplessData;
dst->unk39 = src->unk39;
dst->albumgapless = src->albumgapless;
dst->trackgapless = src->trackgapless;
dst->unk40 = src->unk40;
dst->unk41 = src->unk41;
dst->unk42 = src->unk42;
dst->unk43 = src->unk43;
dst->unk44 = src->unk44;
dst->unk45 = src->unk45;
dst->unk46 = src->unk46;
dst->album_id = src->album_id;
dst->unk48 = src->unk48;
dst->unk49 = src->unk49;
dst->unk50 = src->unk50;
dst->unk51 = src->unk51;
dst->unk52 = src->unk52;
dst->unk53 = src->unk53;
dst->unk54 = src->unk54;
dst->unk55 = src->unk55;
dst->unk56 = src->unk56;
dst->mhii_link = src->mhii_link;
const unsigned int mhodSize = src->mhod.size();
for(unsigned int i=0; i<mhodSize; i++)
{
iPod_mhod *src_mhod = src->mhod[i];
if(src_mhod == NULL)
continue;
iPod_mhod *dst_mhod = dst->AddString(src_mhod->type);
if(dst_mhod)
dst_mhod->Duplicate(src_mhod, dst_mhod);
}
}
int iPod_mhit::GetEQSetting()
{
iPod_mhod *mhod = FindString(MHOD_EQSETTING);
if(mhod == NULL)
return(EQ_NONE);
ASSERT(lstrlen(mhod->str) == 9);
if(lstrlen(mhod->str) != 9)
return(EQ_NONE);
wchar_t strval[4] = {0};
lstrcpyn(strval, mhod->str + 3, 3);
int val = _wtoi(strval);
ASSERT(val >= EQ_ACOUSTIC && val <= EQ_VOCALBOOSTER);
return(val);
}
void iPod_mhit::SetEQSetting(int value)
{
DeleteString(MHOD_EQSETTING);
if(value < 0)
return;
ASSERT(value >= EQ_ACOUSTIC && value <= EQ_VOCALBOOSTER);
wchar_t strval[10] = {0};
_snwprintf(strval, 9, L"#!#%d#!#", value);
strval[9] = '\0';
iPod_mhod *mhod = AddString(MHOD_EQSETTING);
ASSERT(mhod);
if(mhod == NULL)
return;
mhod->SetString(strval);
}
//////////////////////////////////////////////////////////////////////
// iPod_mhod - Holds strings for a song or playlist, among other things
//////////////////////////////////////////////////////////////////////
iPod_mhod::iPod_mhod() :
type(0),
unk1(0),
unk2(0),
position(1),
length(0),
unk3(1),
unk4(0),
str(NULL),
binary(NULL),
liveupdate(1),
checkrules(0),
matchcheckedonly(0),
limitsort_opposite(0),
limittype(0),
limitsort(0),
limitvalue(0),
unk5(0),
rules_operator(SPLMATCH_AND),
parseSmartPlaylists(true)
{
}
iPod_mhod::~iPod_mhod()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhod_destructor);
#endif
if (str) delete [] str;
if (binary) delete [] binary;
const unsigned int size = rule.size();
for (unsigned int i=0;i<size;i++)
delete rule[i];
}
long iPod_mhod::parse(const uint8_t *data)
{
size_t ptr=0;
unsigned long i = 0;
//check mhod header
if (_strnicmp((char *)&data[ptr],"mhod",4)) return -1;
ptr+=4;
// get sizes
size_head=read_uint32_t(data, ptr);
size_total=read_uint32_t(data, ptr);
ASSERT(size_head == 0x18);
// get type
type=read_uint32_t(data, ptr);
// dunno what these are, but they are definitely common among all the types of mhods
// smartlist types prove that.
// they're usually zero though.. null padding for the MHOD header?
unk1=read_uint32_t(data, ptr);
unk2=read_uint32_t(data, ptr);
if (iPod_mhod::IsSimpleStringType(type))
{
// string types get parsed
// 1 == UTF-16, 2 == UTF-8
// TODO: handle UTF-8
encoding_type=read_uint32_t(data, ptr);
length=read_uint32_t(data, ptr);
unk3=read_uint32_t(data, ptr);
unk4=read_uint32_t(data, ptr);
str=new wchar_t[length + 1];
memcpy(str,&data[ptr],length);
str[length / 2] = '\0';
ptr+=length;
}
else if (type == MHOD_ENCLOSUREURL || type == MHOD_RSSFEEDURL)
{
// Apple makes life hard again! These are almost like regular/simple MHOD string types,
// except the string is UTF-8 encoded and there is no length or position fields
length = size_total - size_head;
ASSERT(length > 0);
if(length > 0)
{
char *tmp = new char[length + 1];
ASSERT(tmp);
if(tmp != NULL)
{
memcpy(tmp, &data[ptr], length);
tmp[length] = '\0';
wchar_t *tmpUTF = UTF8_to_UTF16(tmp);
unsigned int len = wcslen(tmpUTF);
str=new wchar_t[len + 1];
wcsncpy(str, tmpUTF, len);
str[len] = '\0';
free(tmpUTF);
}
ptr+=length;
}
}
else if (type==MHOD_SPLPREF)
{
if(parseSmartPlaylists)
{
liveupdate=data[ptr]; ptr++;
checkrules=data[ptr]; ptr++;
checklimits=data[ptr]; ptr++;
limittype=data[ptr]; ptr++;
limitsort=data[ptr]; ptr++;
ptr+=3;
limitvalue=read_uint32_t(data, ptr);
matchcheckedonly=data[ptr]; ptr++;
// if the opposite flag is on, set limitsort's high bit
limitsort_opposite=data[ptr]; ptr++;
if(limitsort_opposite)
limitsort += 0x80000000;
}
}
else if (type==MHOD_SPLDATA)
{
if(parseSmartPlaylists)
{
// strangely, SPL Data is the only thing in the file that *isn't* byte reversed.
// check for SLst header
if (_strnicmp((char *)&data[ptr],"SLst",4)) return -1;
ptr+=4;
unk5=get4(&data[ptr]); ptr+=4;
const unsigned int numrules=get4(&data[ptr]); ptr+=4;
rules_operator=get4(&data[ptr]); ptr+=4;
ptr+=120;
rule.reserve(numrules);
for (i=0;i<numrules;i++)
{
SPLRule * r = new SPLRule;
ASSERT(r);
if(r == NULL)
continue;
r->field=get4(&data[ptr]); ptr+=4;
r->action=get4(&data[ptr]); ptr+=4;
ptr+=44;
r->length=get4(&data[ptr]); ptr+=4;
#ifdef _DEBUG
switch(r->action)
{
case SPLACTION_IS_INT:
case SPLACTION_IS_GREATER_THAN:
case SPLACTION_IS_NOT_GREATER_THAN:
case SPLACTION_IS_LESS_THAN:
case SPLACTION_IS_NOT_LESS_THAN:
case SPLACTION_IS_IN_THE_RANGE:
case SPLACTION_IS_NOT_IN_THE_RANGE:
case SPLACTION_IS_IN_THE_LAST:
case SPLACTION_IS_STRING:
case SPLACTION_CONTAINS:
case SPLACTION_STARTS_WITH:
case SPLACTION_DOES_NOT_START_WITH:
case SPLACTION_ENDS_WITH:
case SPLACTION_DOES_NOT_END_WITH:
case SPLACTION_IS_NOT_INT:
case SPLACTION_IS_NOT_IN_THE_LAST:
case SPLACTION_IS_NOT:
case SPLACTION_DOES_NOT_CONTAIN:
case SPLACTION_BINARY_AND:
case SPLACTION_UNKNOWN2:
break;
default:
// New action!
//printf("New Action Discovered = %x\n",r->action);
ASSERT(0);
break;
}
#endif
const bool hasString = iPod_slst::GetFieldType(r->field) == iPod_slst::ftString;
if(hasString)
{
// For some unknown reason, smart playlist strings have UTF-16 characters that are byte swapped
unsigned char *c = (unsigned char*)r->string;
const unsigned len = min(r->length, SPL_MAXSTRINGLENGTH);
for(unsigned int i=0; i<len; i+=2)
{
*(c + i) = data[ptr + i + 1];
*(c + i + 1) = data[ptr + i];
}
ptr += r->length;
}
else
{
// from/to combos always seem to be 0x44 in length in all cases...
// fix this to be smarter if it turns out not to be the case
ASSERT(r->length == 0x44);
r->fromvalue=get8(&data[ptr]); ptr+=8;
r->fromdate=get8(&data[ptr]); ptr+=8;
r->fromunits=get8(&data[ptr]); ptr+=8;
r->tovalue=get8(&data[ptr]); ptr+=8;
r->todate=get8(&data[ptr]); ptr+=8;
r->tounits=get8(&data[ptr]); ptr+=8;
// SPLFIELD_PLAYLIST seems to use the unks here...
r->unk1=get4(&data[ptr]); ptr+=4;
r->unk2=get4(&data[ptr]); ptr+=4;
r->unk3=get4(&data[ptr]); ptr+=4;
r->unk4=get4(&data[ptr]); ptr+=4;
r->unk5=get4(&data[ptr]); ptr+=4;
}
rule.push_back(r);
}
}
}
else if(type == MHOD_PLAYLIST)
{
position=read_uint32_t(data, ptr);
// Skip to the end
ptr+=16;
}
else
{
// non string/smart playlist types get copied in.. with the header and such being ignored
binary=new unsigned char[size_total-size_head];
memcpy(binary,&data[ptr],size_total-size_head);
// in this case, we'll use the length field to store the length of the binary stuffs,
// since it's not being used for anything else in these entries.
// this helps in the writing phase of the process.
// note that if, for some reason, you decide to create a mhod for type 50+ from scratch,
// you need to set the length = the size of your binary space
length=size_total-size_head;
}
return size_total;
}
long iPod_mhod::write(unsigned char * data, const unsigned long datasize)
{
const unsigned long headsize=0x18;
// check for header size
if (headsize>datasize) return -1;
long ptr=0;
//write mhod header
data[0]='m';data[1]='h';data[2]='o';data[3]='d';
ptr+=4;
// write sizes
rev4(headsize,&data[ptr]); // header size
ptr+=4;
rev4(0x00,&data[ptr]); // placeholder for total size (fill in later)
ptr+=4;
// write stuff out
rev4(type,&data[ptr]);
ptr+=4;
rev4(unk1,&data[ptr]);
ptr+=4;
rev4(unk2,&data[ptr]);
ptr+=4;
if (iPod_mhod::IsSimpleStringType(type))
{
// check for string size
if (16+length+headsize>datasize) return -1;
rev4(position,&data[ptr]);
ptr+=4;
rev4(length,&data[ptr]);
ptr+=4;
rev4(unk3,&data[ptr]);
ptr+=4;
rev4(unk4,&data[ptr]);
ptr+=4;
const unsigned int len = length / 2;
for (unsigned int i=0;i<len;i++)
{
data[ptr]=str[i] & 0xff;
ptr++;
data[ptr]=(str[i] >> 8) & 0xff;
ptr++;
}
}
else if (type == MHOD_ENCLOSUREURL || type == MHOD_RSSFEEDURL)
{
// Convert the UTF-16 string back to UTF-8
char *utf8Str = UTF16_to_UTF8(str);
const unsigned int len = strlen(utf8Str);
if (16+len+headsize>datasize) { free(utf8Str); return -1; }
memcpy(data + ptr, utf8Str, len);
free(utf8Str);
ptr += len;
}
else if (type==MHOD_SPLPREF)
{
if (16+74 > datasize) return -1;
// write the type 50 mhod
data[ptr]=liveupdate; ptr++;
data[ptr]=checkrules; ptr++;
data[ptr]=checklimits; ptr++;
data[ptr]=(unsigned char)(limittype); ptr++;
data[ptr]=(unsigned char)((limitsort & 0x000000ff)); ptr++;
data[ptr]=0; ptr++;
data[ptr]=0; ptr++;
data[ptr]=0; ptr++;
rev4(limitvalue,&data[ptr]); ptr+=4;
data[ptr]=matchcheckedonly; ptr++;
// set the limitsort_opposite flag by checking the high bit of limitsort
data[ptr] = limitsort & 0x80000000 ? 1 : 0; ptr++;
// insert 58 nulls
memset(data + ptr, 0, 58); ptr += 58;
}
else if (type==MHOD_SPLDATA)
{
const unsigned int ruleCount = rule.size();
if (16+136+ (ruleCount*(124+515)) > datasize) return -1;
// put "SLst" header
data[ptr]='S';data[ptr+1]='L';data[ptr+2]='s';data[ptr+3]='t';
ptr+=4;
put4(unk5,&data[ptr]); ptr+=4;
put4(ruleCount,&data[ptr]); ptr+=4;
put4(rules_operator,&data[ptr]); ptr+=4;
memset(data + ptr, 0, 120); ptr+=120;
for (unsigned int i=0;i<ruleCount;i++)
{
SPLRule *r = rule[i];
ASSERT(r);
if(r == NULL)
continue;
put4(r->field,&data[ptr]); ptr+=4;
put4(r->action,&data[ptr]); ptr+=4;
memset(data + ptr, 0, 44); ptr+=44;
put4(r->length,&data[ptr]); ptr+=4;
const bool hasString = iPod_slst::GetFieldType(r->field) == iPod_slst::ftString;
if(hasString)
{
// Byte swap the characters
unsigned char *c = (unsigned char*)r->string;
for(unsigned int i=0; i<r->length; i+=2)
{
data[ptr + i] = *(c + i + 1);
data[ptr + i + 1] = *(c + i);
}
ptr += r->length;
}
else
{
put8(r->fromvalue,&data[ptr]); ptr+=8;
put8(r->fromdate,&data[ptr]); ptr+=8;
put8(r->fromunits,&data[ptr]); ptr+=8;
put8(r->tovalue,&data[ptr]); ptr+=8;
put8(r->todate,&data[ptr]); ptr+=8;
put8(r->tounits,&data[ptr]); ptr+=8;
put4(r->unk1,&data[ptr]); ptr+=4;
put4(r->unk2,&data[ptr]); ptr+=4;
put4(r->unk3,&data[ptr]); ptr+=4;
put4(r->unk4,&data[ptr]); ptr+=4;
put4(r->unk5,&data[ptr]); ptr+=4;
}
} // end for
}
else if(type == MHOD_PLAYLIST)
{
if (16+20 > datasize) return -1;
rev4(position,&data[ptr]); // position in playlist
ptr+=4;
rev4(0,&data[ptr]); // four nulls
ptr+=4;
rev4(0,&data[ptr]);
ptr+=4;
rev4(0,&data[ptr]);
ptr+=4;
rev4(0,&data[ptr]);
ptr+=4;
}
else // not a known type, use the binary
{
// check for binary size
if (length+headsize>datasize) return -1;
for (unsigned int i=0;i<length;i++)
{
data[ptr]=binary[i];
ptr++;
}
}
// fix the total size
rev4(ptr,&data[8]);
return ptr;
}
void iPod_mhod::SetString(const wchar_t *string)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhod_SetString);
#endif
if(!string)
return;
delete [] str;
length = 0;
unsigned int stringLen = min(wcslen(string), 512);
str = new wchar_t[stringLen + 1];
memset(str, 0, sizeof(wchar_t) * (stringLen + 1));
if(str)
{
wcsncpy(str, string, stringLen);
length = stringLen * 2;
}
}
bool iPod_mhod::IsSimpleStringType(const unsigned int type)
{
switch(type)
{
case MHOD_TITLE:
case MHOD_LOCATION:
case MHOD_ALBUM:
case MHOD_ARTIST:
case MHOD_GENRE:
case MHOD_FILETYPE:
case MHOD_EQSETTING:
case MHOD_COMMENT:
case MHOD_CATEGORY:
case MHOD_COMPOSER:
case MHOD_GROUPING:
case MHOD_DESCRIPTION:
case MHOD_SUBTITLE:
case MHOD_ALBUMARTIST:
case MHOD_ARTIST_SORT:
case MHOD_TITLE_SORT:
case MHOD_ALBUM_SORT:
case MHOD_ALBUMARTIST_SORT:
case MHOD_COMPOSER_SORT:
case MHOD_SHOW_SORT:
case MHOD_ALBUMLIST_ALBUM:
case MHOD_ALBUMLIST_ARTIST:
return(true);
}
return(false);
}
void iPod_mhod::Duplicate(iPod_mhod *src, iPod_mhod *dst)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhod_Duplicate);
#endif
if(src == NULL || dst == NULL)
return;
dst->type = src->type;
dst->unk1 = src->unk1;
dst->unk2 = src->unk2;
dst->position = src->position;
dst->length = src->length;
dst->unk3 = src->unk3;
dst->unk4 = src->unk4;
dst->liveupdate = src->liveupdate;
dst->checkrules = src->checkrules;
dst->checklimits = src->checklimits;
dst->limittype = src->limittype;
dst->limitsort = src->limitsort;
dst->limitvalue = src->limitvalue;
dst->matchcheckedonly = src->matchcheckedonly;
dst->limitsort_opposite = src->limitsort_opposite;
dst->unk5 = src->unk5;
dst->rules_operator = src->rules_operator;
if(src->str)
{
dst->SetString(src->str);
}
else if(src->binary)
{
dst->binary = new unsigned char[src->length];
if(dst->binary)
memcpy(dst->binary, src->binary, src->length);
}
const unsigned int ruleLen = src->rule.size();
for(unsigned int i=0; i<ruleLen; i++)
{
SPLRule *srcRule = src->rule[i];
if(srcRule)
{
SPLRule *dstRule = new SPLRule;
if(dstRule)
memcpy(dstRule, srcRule, sizeof(SPLRule));
dst->rule.push_back(dstRule);
}
}
}
//////////////////////////////////////////////////////////////////////
// iPod_mhlp - Holds playlists
//////////////////////////////////////////////////////////////////////
iPod_mhlp::iPod_mhlp() :
mhyp(),
beingDeleted(false)
{
// Always start off with an empty, hidden, default playlist
ASSERT(GetChildrenCount() == 0);
GetDefaultPlaylist();
}
iPod_mhlp::~iPod_mhlp()
{
// This is unnecessary (and slow) to clear the vector list,
// since the object is being destroyed anyway...
beingDeleted = true;
ClearPlaylists();
}
long iPod_mhlp::parse(const uint8_t *data)
{
long ptr=0;
//check mhlp header
if (_strnicmp((char *)&data[ptr],"mhlp",4)) return -1;
ptr+=4;
// get sizes
size_head=rev4(&data[ptr]);
ptr+=4;
const unsigned long children=rev4(&data[ptr]); // Only used locally - child count is obtained from the mhyp vector list
ptr+=4;
ASSERT(size_head == 0x5c);
// skip nulls
ptr=size_head;
mhyp.reserve(children); // pre allocate the space, for speed
ClearPlaylists();
long ret;
for (unsigned long i=0;i<children; i++)
{
iPod_mhyp *m = new iPod_mhyp;
ret=m->parse(&data[ptr]);
ASSERT(ret >= 0);
if (ret<0)
{
delete m;
return ret;
}
// If this is really a smart playlist, we need to parse it again as a smart playlist
if(m->FindString(MHOD_SPLPREF) != NULL)
{
delete m;
ptr+=ret;
continue;
m = new iPod_slst;
ASSERT(m);
ret = m->parse(&data[ptr]);
ASSERT(ret >= 0);
if(ret < 0)
{
delete m;
return ret;
}
}
ptr+=ret;
mhyp.push_back(m);
}
return ptr;
}
long iPod_mhlp::write(unsigned char * data, const unsigned long datasize, int index)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhlp_write);
#endif
const unsigned int headsize=0x5c;
// check for header size
if (headsize>datasize) return -1;
long ptr=0;
//write mhlp header
data[0]='m';data[1]='h';data[2]='l';data[3]='p';
ptr+=4;
// write sizes
rev4(headsize,&data[ptr]); // header size
ptr+=4;
// write num of children
const unsigned long children = GetChildrenCount();
rev4(children,&data[ptr]);
ptr+=4;
// fill up the rest of the header with nulls
unsigned int i;
for (i=ptr;i<headsize;i++)
data[i]=0;
ptr=headsize;
long ret;
for (i=0;i<children; i++)
{
ret=mhyp[i]->write(&data[ptr],datasize-ptr,index);
ASSERT(ret >= 0);
if (ret<0) return ret;
ptr+=ret;
}
return ptr;
}
iPod_mhyp * iPod_mhlp::AddPlaylist()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhlp_AddPlaylist);
#endif
iPod_mhyp * m = new iPod_mhyp;
ASSERT(m);
if (m != NULL)
{
mhyp.push_back(m);
return m;
}
else return NULL;
}
iPod_mhyp * iPod_mhlp::FindPlaylist(const unsigned __int64 playlistID)
{
const unsigned long count = GetChildrenCount();
for (unsigned long i=0; i<count; i++)
{
iPod_mhyp * m = GetPlaylist(i);
if (m->playlistID == playlistID)
return m;
}
return NULL;
}
// deletes the playlist at a position
bool iPod_mhlp::DeletePlaylist(const unsigned long pos)
{
if (GetChildrenCount() > pos)
{
iPod_mhyp *m = GetPlaylist(pos);
mhyp.erase(mhyp.begin() + pos);
delete m;
return true;
}
else return false;
}
bool iPod_mhlp::DeletePlaylistByID(const unsigned __int64 playlistID)
{
if(playlistID == 0)
return(false);
const unsigned int count = GetChildrenCount();
for(unsigned int i=0; i<count; i++)
{
iPod_mhyp *m = GetPlaylist(i);
ASSERT(m);
if(m == NULL)
continue;
if(m->playlistID == playlistID)
return(DeletePlaylist(i));
}
return(false);
}
iPod_mhyp * iPod_mhlp::GetDefaultPlaylist()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhlp_GetDefaultPlaylist);
#endif
if (!mhyp.empty())
return GetPlaylist(0);
else
{
// Create a new hidden playlist, and set a default title
iPod_mhyp * playlist = AddPlaylist();
ASSERT(playlist);
if(playlist)
{
playlist->hidden = 1;
iPod_mhod *mhod = playlist->AddString(MHOD_TITLE);
if(mhod)
mhod->SetString(L"iPod");
}
return playlist;
}
}
bool iPod_mhlp::ClearPlaylists(const bool createDefaultPlaylist)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhlp_ClearPlaylists);
#endif
const unsigned long count = GetChildrenCount();
for (unsigned long i=0;i<count;i++)
{
iPod_mhyp *m=GetPlaylist(i);
delete m;
}
if(!beingDeleted)
mhyp.clear();
// create the default playlist again, if it's gone
// XXX - Normally this is the right thing to do, but if we
// are about to parse the playlists from the iPod, we can't create
// the default playlist. Doing so will create two default/hidden
// playlists - this one and the one that will be created when
// the playlists are parsed!
if(createDefaultPlaylist)
GetDefaultPlaylist();
return true;
}
void iPod_mhlp::RemoveDeadPlaylistEntries(iPod_mhlt *mhlt)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhlp_RemoveDeadPlaylistEntries);
#endif
if(mhlt == NULL)
return;
const unsigned int playlistCount = GetChildrenCount();
for(unsigned int i=0; i<playlistCount; i++)
{
iPod_mhyp *m = mhyp[i];
ASSERT(m);
if(m == NULL)
continue;
std::vector<unsigned int> deleteList;
const unsigned int songCount = m->GetMhipChildrenCount();
for(unsigned int j=0; j<songCount; j++)
{
iPod_mhip *p = m->mhip.at(j);
ASSERT(p);
if(p == NULL)
continue;
if(mhlt->GetTrackByID(p->songindex) != NULL)
continue;
// Found a dead song
deleteList.push_back(p->songindex);
}
const unsigned int deleteCount = deleteList.size();
for(unsigned int k=0; k<deleteCount; k++)
{
const bool retval = m->DeletePlaylistEntryByID(deleteList[k]);
ASSERT(retval == true);
}
}
}
void iPod_mhlp::SortPlaylists() {
std::sort(mhyp.begin(),mhyp.end(),iPod_mhyp());
}
//////////////////////////////////////////////////////////////////////
// iPod_mhyp - A Playlist
//////////////////////////////////////////////////////////////////////
iPod_mhyp::iPod_mhyp() :
hidden(0),
timestamp(0),
unk3(0),
numStringMHODs(0),
podcastflag(0),
numLibraryMHODs(0),
mhod(),
mhip(),
mhit(NULL),
isSmartPlaylist(false),
isPopulated(true), // consider normal playlists to be populated always
writeLibraryMHODs(true)
{
timestamp = wintime_to_mactime(time(NULL));
// Create a highly randomized 64 bit value for the playlistID
playlistID = Generate64BitID();
}
iPod_mhyp::~iPod_mhyp()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhyp_destructor);
#endif
const unsigned long mhodCount = GetMhodChildrenCount();
const unsigned long mhipCount = GetMhipChildrenCount();
unsigned long i;
for (i=0;i<mhodCount;i++)
delete mhod[i];
for (i=0;i<mhipCount;i++)
delete mhip[i];
}
long iPod_mhyp::parse(const uint8_t *data)
{
size_t ptr=0;
//check mhyp header
if (_strnicmp((char *)&data[ptr],"mhyp",4)) return -1;
ptr+=4;
// get sizes
size_head=read_uint32_t(data, ptr);
size_total=read_uint32_t(data, ptr);
ASSERT(size_head == 0x6c || size_head == 0x8c);
const unsigned long mhodnum=read_uint32_t(data, ptr); // only used locally
const unsigned long numsongs=read_uint32_t(data, ptr); // only used locally
hidden=read_uint32_t(data, ptr);
timestamp=read_uint32_t(data, ptr);
playlistID = rev8(get8(&data[ptr]));
if(playlistID == 0)
{
// Force the playlistID to be a valid value.
// This may not always be the right thing to do, but I can't think of any reason why it wouldn't be ok...
playlistID = Generate64BitID();
}
ptr+=8;
unk3=read_uint32_t(data, ptr);
unsigned long temp = rev4(&data[ptr]);
numStringMHODs = temp && 0xFFFF;
podcastflag = (uint16_t)(temp >> 16);
ptr+=4;
numLibraryMHODs=read_uint32_t(data, ptr);
ptr=size_head;
long ret;
unsigned long i;
mhod.reserve(mhodnum); // pre allocate the space, for speed
mhip.reserve(numsongs);
for (i=0;i<mhodnum;i++)
{
iPod_mhod *m = new iPod_mhod;
// parseSmartPlaylists is an optimization for when dealing with smart playlists.
// Since the playlist has to be parsed in order to determine if it is a smart playlist,
// and if it is, parsed again as a smart playlist, this optimization prevents some duplicate parsing.
m->parseSmartPlaylists = isSmartPlaylist;
ret=m->parse(&data[ptr]);
ASSERT(ret >= 0);
if (ret<0)
{
delete m;
continue;
return ret;
}
ptr+=ret;
// Don't add Type 52 MHODs - if the file changes, the indexes are no longer valid
if(m->type == MHOD_LIBRARY || m->type == MHOD_LIBRARY_LETTER)
{
delete m;
continue;
}
// Reset the Type 52 MHOD count to zero
numLibraryMHODs = 0;
if(m->type == MHOD_PLAYLIST)
delete m; // fuck 'em!
else
mhod.push_back(m);
}
for (i=0;i<numsongs;i++)
{
iPod_mhip *m = new iPod_mhip;
ret=m->parse(&data[ptr]);
ASSERT(ret >= 0);
if (ret<0)
{
delete m;
return ret;
}
else
{
ptr+=ret;
}
mhip.push_back(m);
}
return ptr;
}
long iPod_mhyp::write(unsigned char * data, const unsigned long datasize, int index)
{
const unsigned int headsize=0x6c;
// check for header size
ASSERT(headsize <= datasize);
if (headsize>datasize) return -1;
size_t ptr=0;
// List of Type 52 MHODs to write
unsigned int indexType[] = { TYPE52_SONG_NAME, TYPE52_ARTIST, TYPE52_ALBUM, TYPE52_GENRE, TYPE52_COMPOSER };
const unsigned int indexTypeCount = sizeof(indexType) / sizeof(unsigned int);
//write mhyp header
data[0]='m';data[1]='h';data[2]='y';data[3]='p';
ptr+=4;
// write sizes
write_uint32_t(data, ptr, headsize);// header size
write_uint32_t(data, ptr, 0); // placeholder for total size (fill in later)
writeLibraryMHODs = true;
bool writeType52MHOD = writeLibraryMHODs && (hidden > 0 && mhit != NULL);
// fill in stuff
const unsigned long mhodnum = GetMhodChildrenCount();
numStringMHODs = (uint16_t)mhodnum;
if(writeType52MHOD)
{
write_uint32_t(data, ptr, mhodnum + indexTypeCount); // Include the extra MHOD Type 52s that aren't in the MHOD list
numLibraryMHODs = indexTypeCount;
}
else
{
write_uint32_t(data, ptr, mhodnum);
}
const unsigned long numsongs = GetMhipChildrenCount();
write_uint32_t(data, ptr, numsongs);
write_uint32_t(data, ptr, hidden);
write_uint32_t(data, ptr, timestamp);
put8(rev8(playlistID),&data[ptr]);
ptr+=8;
write_uint32_t(data, ptr, unk3);
unsigned long temp = numStringMHODs | podcastflag << 16;
rev4(temp,&data[ptr]);
ptr+=4;
write_uint32_t(data, ptr, numLibraryMHODs);
unsigned long i;
// fill up the rest of the header with nulls
for (i=ptr;i<headsize;i++)
data[i]=0;
ptr=headsize;
long ret;
for (i=0;i<mhodnum; i++)
{
ret=mhod[i]->write(&data[ptr],datasize-ptr);
ASSERT(ret >= 0);
if (ret<0) return ret;
else ptr+=ret;
}
// If this is the default hidden playlist, create the type 52 MHODs that are used to accelerate the browse menus
if(writeType52MHOD)
{
/*
header identifier | 4 | mhod
header length | 4 | size of the mhod header
total length | 4 | size of the header and all the index entries
type | 4 | the type indicator ( 52 )
unk1 | 4 | unknown (always zero)
unk2 | 4 | unknown (always zero)
index type | 4 | what this index is sorted on (see list above)
count | 4 | number of entries. Always the same as the number of entries in the playlist, which is the same as the number of songs on the iPod.
null padding | 40| lots of padding
-----
72 bytes (0x48)
index entries | 4 * count | The index entries themselves. This is an index into the mhit list, in order, starting from 0 for the first mhit.
*/
for(unsigned int i=0; i<indexTypeCount; i++)
{
unsigned int mhodType = 0;
const unsigned int mhod52Type = indexType[i];
// Map the index type to the MHOD type
switch(mhod52Type)
{
case TYPE52_SONG_NAME:
mhodType = MHOD_TITLE;
break;
case TYPE52_ALBUM:
mhodType = MHOD_ALBUM;
break;
case TYPE52_ARTIST:
mhodType = MHOD_ARTIST;
break;
case TYPE52_GENRE:
mhodType = MHOD_GENRE;
break;
case TYPE52_COMPOSER:
mhodType = MHOD_COMPOSER;
break;
default:
ASSERT(0); // unknown type
mhodType = 0;
}
if(mhodType == 0)
continue;
ASSERT(mhit->size() == numsongs);
if(mhit->size() != numsongs)
continue;
// Start writing the MHOD...
const unsigned long headsize = 0x18;
const unsigned long totalsize = 0x48 + (4 * numsongs);
//write mhod header
data[ptr++]='m';data[ptr++]='h';data[ptr++]='o';data[ptr++]='d';
// write sizes
write_uint32_t(data, ptr, headsize); // header size
write_uint32_t(data, ptr, totalsize); // total size = 72 * (4 * number of songs)
// This is type 52 MHOD
write_uint32_t(data, ptr, MHOD_LIBRARY);
// Unknowns (always zero)
unsigned long unk1 = 0, unk2 = 0;
write_uint32_t(data, ptr, unk1);
write_uint32_t(data, ptr, unk2);
// What kind of index is this?
write_uint32_t(data, ptr, mhod52Type);
// Song Count
write_uint32_t(data, ptr, numsongs);
// 40 NULLs
for(unsigned int j=0;j<40;j++)
data[ptr++]=0;
// Build the sort by string vector list
std::vector<indexMhit*> strList;
strList.reserve(numsongs);
unsigned int ki = 0;
/*
iPod_mhlt::mhit_map_t::const_iterator begin = mhit->begin();
iPod_mhlt::mhit_map_t::const_iterator end = mhit->end();
for(iPod_mhlt::mhit_map_t::const_iterator it = begin; it != end; it++)
{
iPod_mhit *m = static_cast<iPod_mhit*>((*it).second);
*/
int kl = mhip.size();
for(int k=0; k < kl; k++) {
iPod_mhit *m = mhit->find(mhip.at(k)->songindex)->second;
ASSERT(m != NULL);
if(m == NULL)
continue;
indexMhit *foo = new indexMhit;
foo->index = ki++;
iPod_mhod *d;
if(mhod52Type == TYPE52_COMPOSER) {
foo->track = 0;
foo->str[0] = L"";
foo->str[1] = L"";
d = m->FindString(MHOD_COMPOSER);
foo->str[2] = d?d->str:L"";
} else {
foo->track = (int)m->tracknum;
d = (mhod52Type >= TYPE52_GENRE)?m->FindString(MHOD_GENRE):NULL;
foo->str[0] = d?d->str:L"";
d = (mhod52Type >= TYPE52_ARTIST)?m->FindString(MHOD_ARTIST):NULL;
foo->str[1] = d?d->str:L"";
d = (mhod52Type >= TYPE52_ALBUM)?m->FindString(MHOD_ALBUM):NULL;
foo->str[2] = d?d->str:L"";
}
d = m->FindString(MHOD_TITLE);
foo->str[3] = d?d->str:L"";
strList.push_back(foo);
}
// Sort the list alphabetically
std::sort(strList.begin(), strList.end(), indexMhit());
// Write out the index entries
const unsigned indexCount = strList.size();
ASSERT(indexCount == numsongs);
for(unsigned int mi =0; mi<indexCount; mi++)
{
const unsigned int index = strList[mi]->index;
write_uint32_t(data, ptr, index);
}
// Free the list of indexMhits
for(unsigned int li=0;li<indexCount;li++)
{
delete strList[li];
}
}
}
int SongNum = 0;
for (i=0;i<numsongs;i++)
{
//FUCKO: use index here to write out podcast list differently
if(index == 3 || mhip[i]->podcastgroupflag == 0)
{
if(mhip[i]->podcastgroupflag == 0)
SongNum++;
unsigned long temp=0;
if(index == 2)
{
temp = mhip[i]->podcastgroupref;
mhip[i]->podcastgroupref = 0;
}
ret=mhip[i]->write(&data[ptr],datasize-ptr, SongNum);
if(index == 2)
mhip[i]->podcastgroupref=temp;
ASSERT(ret >= 0);
if (ret<0)
return ret;
else
ptr+=ret;
}
}
// fix the total size
rev4(ptr,&data[8]);
// fix number of songs written
if(index == 2) rev4(SongNum,&data[16]);
return ptr;
}
long iPod_mhyp::AddPlaylistEntry(iPod_mhip * entry, const unsigned long id)
{
entry = new iPod_mhip;
ASSERT(entry != NULL);
if (entry !=NULL)
{
entry->songindex=id;
entry->timestamp = wintime_to_mactime(time(NULL));
mhip.push_back(entry);
return GetMhipChildrenCount()-1;
}
else return -1;
}
long iPod_mhyp::FindPlaylistEntry(const unsigned long id) const
{
const unsigned long children = GetMhipChildrenCount();
for (unsigned long i=0;i<children;i++)
{
if (mhip.at(i)->songindex==id)
return i;
}
return -1;
}
bool iPod_mhyp::DeletePlaylistEntry(unsigned long pos)
{
if (GetMhipChildrenCount() >= pos)
{
iPod_mhip * m = GetPlaylistEntry(pos);
if (pos < mhip.size())
{
mhip.erase(mhip.begin() + pos);
}
delete m;
return true;
}
return false;
}
bool iPod_mhyp::DeletePlaylistEntryByID(unsigned long songindex)
{
// Search the list of mhips until the matching mhip(s) are found
bool retval = false;
unsigned int count = GetMhipChildrenCount();
for(unsigned int i=0; i<count; i++)
{
iPod_mhip *m = GetPlaylistEntry(i);
if(m->songindex == songindex)
{
DeletePlaylistEntry(i);
count = GetMhipChildrenCount();
i=0;
retval = true;
continue;
}
}
return(retval);
}
bool iPod_mhyp::ClearPlaylist()
{
const unsigned long count = GetMhipChildrenCount();
for (unsigned long i=0;i<count;i++)
{
iPod_mhip * m = GetPlaylistEntry(i);
delete m;
}
mhip.clear();
return true;
}
long iPod_mhyp::PopulatePlaylist(iPod_mhlt * tracks, int hidden_field)
{
ASSERT(tracks != NULL);
if(tracks == NULL)
return(-1);
const unsigned long trackCount = tracks->GetChildrenCount();
if(trackCount == 0)
return(0);
ClearPlaylist();
// Speed up getting the id as follows:
// Iterate the whole tracks->mhit map, storing the ids in a local vector
std::vector<unsigned long> ids;
ids.reserve(trackCount);
iPod_mhlt::mhit_map_t::const_iterator begin = tracks->mhit.begin();
iPod_mhlt::mhit_map_t::const_iterator end = tracks->mhit.end();
for(iPod_mhlt::mhit_map_t::const_iterator it = begin; it != end; it++)
ids.push_back(static_cast<unsigned long>((*it).first));
for (unsigned long i=0;i<trackCount;i++)
{
// Add the playlist entry locally rather than using
// AddPlaylistEntry() for speed optimization reasons
iPod_mhip *entry = new iPod_mhip;
ASSERT(entry != NULL);
if(entry)
{
entry->songindex = ids[i];
mhip.push_back(entry);
}
}
hidden=hidden_field;
return GetMhipChildrenCount();
}
iPod_mhod * iPod_mhyp::AddString(const int type)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhyp_AddString);
#endif
iPod_mhod * m;
if (type)
{
m = FindString(type);
if (m!=NULL) return m;
}
m=new iPod_mhod;
if (m!=NULL && type) m->type=type;
mhod.push_back(m);
return m;
}
iPod_mhod * iPod_mhyp::FindString(const unsigned long type)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhyp_FindString);
#endif
const unsigned long children = GetMhodChildrenCount();
for (unsigned long i=0;i<children;i++)
{
if (mhod[i]->type == type) return mhod[i];
}
return NULL;
}
unsigned long iPod_mhyp::DeleteString(const unsigned long type)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhyp_DeleteString);
#endif
unsigned long count=0;
for (unsigned long i=0; i != GetMhodChildrenCount(); i++)
{
if (mhod[i]->type == type)
{
iPod_mhod * m = mhod.at(i);
mhod.erase(mhod.begin() + i);
delete m;
i = i > 0 ? i - 1 : 0; // do this to ensure that it checks the new entry in position i next
count++;
}
}
return count;
}
void iPod_mhyp::SetPlaylistTitle(const wchar_t *string)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhyp_SetPlaylistTitle);
#endif
if(string == NULL)
return;
iPod_mhod *mhod = AddString(MHOD_TITLE);
ASSERT(mhod);
if(mhod)
mhod->SetString(string);
}
void iPod_mhyp::Duplicate(iPod_mhyp *src, iPod_mhyp *dst)
{
if(src == NULL || dst == NULL)
return;
dst->hidden = src->hidden;
dst->timestamp = src->timestamp;
dst->playlistID = src->playlistID;
dst->unk3 = src->unk3;
dst->numStringMHODs = src->numStringMHODs;
dst->podcastflag = src->podcastflag;
dst->numLibraryMHODs = src->numLibraryMHODs;
dst->isSmartPlaylist = src->isSmartPlaylist;
dst->isPopulated = src->isPopulated;
const unsigned int mhodCount = src->mhod.size();
const unsigned int mhipCount = src->mhip.size();
unsigned int i;
for(i=0; i<mhodCount; i++)
{
iPod_mhod *mhod = dst->AddString();
ASSERT(mhod);
if(mhod)
{
iPod_mhod *srcMHOD = src->mhod[i];
ASSERT(srcMHOD);
if(srcMHOD)
mhod->Duplicate(srcMHOD, mhod);
}
}
for(i=0; i<mhipCount; i++)
{
iPod_mhip *mhip = NULL;
if(dst->AddPlaylistEntry(mhip) >= 0 && mhip != NULL)
{
iPod_mhip *srcMHIP = src->mhip[i];
ASSERT(srcMHIP);
if(srcMHIP)
mhip->Duplicate(srcMHIP, mhip);
}
}
}
bool iPod_mhyp::operator()(iPod_mhyp*& one, iPod_mhyp*& two) {
if(one->hidden & 0xff) return true;
if(two->hidden & 0xff) return false;
wchar_t * a = one->FindString(MHOD_TITLE)->str;
wchar_t * b = two->FindString(MHOD_TITLE)->str;
return _wcsicmp(a?a:L"",b?b:L"") < 0;
}
//////////////////////////////////////////////////////////////////////
// iPod_mhip - Playlist Entry
//////////////////////////////////////////////////////////////////////
iPod_mhip::iPod_mhip() :
dataobjectcount(1),
podcastgroupflag(0),
groupid(0),
songindex(0),
timestamp(0),
podcastgroupref(0)
{
}
iPod_mhip::~iPod_mhip()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhip_destructor);
#endif
}
long iPod_mhip::parse(const uint8_t *data)
{
long ptr=0;
//check mhyp header
if (_strnicmp((char *)&data[ptr],"mhip",4)) return -1;
ptr+=4;
// get sizes
size_head=rev4(&data[ptr]);
ptr+=4;
size_total=rev4(&data[ptr]);
ptr+=4;
ASSERT(size_head == 0x4c);
// read in the useful info
dataobjectcount=rev4(&data[ptr]); //data object count
ptr+=4;
podcastgroupflag=rev4(&data[ptr]); //podcast group flag
ptr+=4;
groupid=rev4(&data[ptr]); // group id
ptr+=4;
songindex=rev4(&data[ptr]); //trackid
ptr+=4;
timestamp=rev4(&data[ptr]); // timestamp
ptr+=4;
podcastgroupref=rev4(&data[ptr]); // group ref
ptr+=4;
ptr=size_head;
// dump the mhod after the mhip, as it's just a position number in the playlist
// and useless to read in since we get them in order anyway
for(uint32_t i=0; i!=dataobjectcount; i++) {
iPod_mhod *m = new iPod_mhod;
long ret = m->parse(&data[ptr]);
ASSERT(ret >= 0);
if(ret<0) return ret;
ptr+=ret;
if(m->type == 100) delete m; //fuck 'em
else mhod.push_back(m);
}
return ptr;
}
long iPod_mhip::write(unsigned char * data, const unsigned long datasize, int entrynum)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhip_write);
#endif
long ptr=0;
const unsigned int headsize=0x4c;
// check for header size and mhod size
if (headsize+0x2c>datasize)
{
ASSERT(0);
return -1;
}
//write mhip header
data[0]='m';data[1]='h';data[2]='i';data[3]='p';
ptr+=4;
// write sizes
rev4(headsize,&data[ptr]); // header size
ptr+=4;
//rev4(headsize,&data[ptr]); // mhips have no children or subdata
ptr+=4;
// fill in stuff
rev4(mhod.size() + ((podcastgroupflag!=0)?0:1),&data[ptr]);
ptr+=4;
rev4(podcastgroupflag,&data[ptr]);
ptr+=4;
rev4(groupid,&data[ptr]);
ptr+=4;
rev4(songindex,&data[ptr]);
ptr+=4;
rev4(timestamp,&data[ptr]);
ptr+=4;
rev4(podcastgroupref,&data[ptr]);
ptr+=4;
// fill up the rest of the header with nulls
for (unsigned int i=ptr;i<headsize;i++)
data[i]=0;
ptr=headsize;
if(!podcastgroupflag) {
// create an faked up mhod type 100 for position info
// (required at this point, albeit seemingly useless.. type 100 mhods are weird)
data[ptr]='m';data[ptr+1]='h';data[ptr+2]='o';data[ptr+3]='d';
ptr+=4;
rev4(0x18,&data[ptr]); // header size
ptr+=4;
rev4(0x2c,&data[ptr]); // total size
ptr+=4;
rev4(100,&data[ptr]); // type
ptr+=4;
rev4(0,&data[ptr]); // two nulls
ptr+=4;
rev4(0,&data[ptr]);
ptr+=4;
rev4(entrynum,&data[ptr]); // position in playlist
ptr+=4;
rev4(0,&data[ptr]); // four nulls
ptr+=4;
rev4(0,&data[ptr]);
ptr+=4;
rev4(0,&data[ptr]);
ptr+=4;
rev4(0,&data[ptr]);
ptr+=4;
// the above code could be more optimized, but this is simpler to read, methinks
}
for(unsigned long i = 0; i < mhod.size(); i++) {
long ret = mhod[i]->write(&data[ptr],datasize-ptr);
ASSERT(ret >= 0);
if(ret<0) return ret;
else ptr+=ret;
}
rev4(ptr,&data[8]);
return ptr;
}
void iPod_mhip::Duplicate(iPod_mhip *src, iPod_mhip *dst)
{
if(src == NULL || dst == NULL)
return;
dst->dataobjectcount = src->dataobjectcount;
dst->podcastgroupflag = src->podcastgroupflag;
dst->groupid = src->groupid;
dst->songindex = src->songindex;
dst->timestamp = src->timestamp;
dst->podcastgroupref = src->podcastgroupref;
}
//////////////////////////////////////////////////////////////////////
// iPod_slst - Smart Playlist
//////////////////////////////////////////////////////////////////////
iPod_slst::iPod_slst() :
splPref(NULL),
splData(NULL)
{
isSmartPlaylist = true;
isPopulated = false; // smart playlists are not considered populated by default
Reset();
}
iPod_slst::~iPod_slst()
{
RemoveAllRules();
}
iPod_slst::FieldType iPod_slst::GetFieldType(const unsigned long field)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_slst_GetFieldType);
#endif
switch(field)
{
case SPLFIELD_SONG_NAME:
case SPLFIELD_ALBUM:
case SPLFIELD_ARTIST:
case SPLFIELD_GENRE:
case SPLFIELD_KIND:
case SPLFIELD_COMMENT:
case SPLFIELD_COMPOSER:
case SPLFIELD_GROUPING:
return(ftString);
case SPLFIELD_BITRATE:
case SPLFIELD_SAMPLE_RATE:
case SPLFIELD_YEAR:
case SPLFIELD_TRACKNUMBER:
case SPLFIELD_SIZE:
case SPLFIELD_PLAYCOUNT:
case SPLFIELD_DISC_NUMBER:
case SPLFIELD_BPM:
case SPLFIELD_RATING:
case SPLFIELD_TIME: // time is the length of the track in milliseconds
case SPLFIELD_VIDEO_KIND:
return(ftInt);
case SPLFIELD_COMPILATION:
return(ftBoolean);
case SPLFIELD_DATE_MODIFIED:
case SPLFIELD_DATE_ADDED:
case SPLFIELD_LAST_PLAYED:
return(ftDate);
case SPLFIELD_PLAYLIST:
return(ftPlaylist);
default:
// Unknown field type
ASSERT(0);
}
return(ftUnknown);
}
iPod_slst::ActionType iPod_slst::GetActionType(const unsigned long field, const unsigned long action)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_slst_GetActionType);
#endif
const FieldType fieldType = GetFieldType(field);
switch(fieldType)
{
case ftString:
switch(action)
{
case SPLACTION_IS_STRING:
case SPLACTION_IS_NOT:
case SPLACTION_CONTAINS:
case SPLACTION_DOES_NOT_CONTAIN:
case SPLACTION_STARTS_WITH:
case SPLACTION_DOES_NOT_START_WITH:
case SPLACTION_ENDS_WITH:
case SPLACTION_DOES_NOT_END_WITH:
return(atString);
case SPLACTION_IS_NOT_IN_THE_RANGE:
case SPLACTION_IS_INT:
case SPLACTION_IS_NOT_INT:
case SPLACTION_IS_GREATER_THAN:
case SPLACTION_IS_NOT_GREATER_THAN:
case SPLACTION_IS_LESS_THAN:
case SPLACTION_IS_NOT_LESS_THAN:
case SPLACTION_IS_IN_THE_RANGE:
case SPLACTION_IS_IN_THE_LAST:
case SPLACTION_IS_NOT_IN_THE_LAST:
return(atInvalid);
default:
// Unknown action type
ASSERT(0);
return(atUnknown);
}
break;
case ftInt:
switch(action)
{
case SPLACTION_IS_INT:
case SPLACTION_IS_NOT_INT:
case SPLACTION_IS_GREATER_THAN:
case SPLACTION_IS_NOT_GREATER_THAN:
case SPLACTION_IS_LESS_THAN:
case SPLACTION_IS_NOT_LESS_THAN:
return(atInt);
case SPLACTION_IS_NOT_IN_THE_RANGE:
case SPLACTION_IS_IN_THE_RANGE:
return(atRange);
case SPLACTION_IS_STRING:
case SPLACTION_CONTAINS:
case SPLACTION_STARTS_WITH:
case SPLACTION_DOES_NOT_START_WITH:
case SPLACTION_ENDS_WITH:
case SPLACTION_DOES_NOT_END_WITH:
case SPLACTION_IS_IN_THE_LAST:
case SPLACTION_IS_NOT_IN_THE_LAST:
case SPLACTION_IS_NOT:
case SPLACTION_DOES_NOT_CONTAIN:
return(atInvalid);
default:
// Unknown action type
ASSERT(0);
return(atUnknown);
}
break;
case ftBoolean:
return(atNone);
case ftDate:
switch(action)
{
case SPLACTION_IS_INT:
case SPLACTION_IS_NOT_INT:
case SPLACTION_IS_GREATER_THAN:
case SPLACTION_IS_NOT_GREATER_THAN:
case SPLACTION_IS_LESS_THAN:
case SPLACTION_IS_NOT_LESS_THAN:
return(atDate);
case SPLACTION_IS_IN_THE_LAST:
case SPLACTION_IS_NOT_IN_THE_LAST:
return(atInTheLast);
case SPLACTION_IS_IN_THE_RANGE:
case SPLACTION_IS_NOT_IN_THE_RANGE:
return(atRange);
case SPLACTION_IS_STRING:
case SPLACTION_CONTAINS:
case SPLACTION_STARTS_WITH:
case SPLACTION_DOES_NOT_START_WITH:
case SPLACTION_ENDS_WITH:
case SPLACTION_DOES_NOT_END_WITH:
case SPLACTION_IS_NOT:
case SPLACTION_DOES_NOT_CONTAIN:
return(atInvalid);
default:
// Unknown action type
ASSERT(0);
return(atUnknown);
}
break;
case ftPlaylist:
switch(action)
{
case SPLACTION_IS_INT:
case SPLACTION_IS_NOT_INT:
return (atPlaylist);
case SPLACTION_IS_GREATER_THAN:
case SPLACTION_IS_NOT_GREATER_THAN:
case SPLACTION_IS_LESS_THAN:
case SPLACTION_IS_NOT_LESS_THAN:
case SPLACTION_IS_IN_THE_LAST:
case SPLACTION_IS_NOT_IN_THE_LAST:
case SPLACTION_IS_IN_THE_RANGE:
case SPLACTION_IS_NOT_IN_THE_RANGE:
case SPLACTION_IS_STRING:
case SPLACTION_CONTAINS:
case SPLACTION_STARTS_WITH:
case SPLACTION_DOES_NOT_START_WITH:
case SPLACTION_ENDS_WITH:
case SPLACTION_DOES_NOT_END_WITH:
case SPLACTION_IS_NOT:
case SPLACTION_DOES_NOT_CONTAIN:
return (atInvalid);
default:
ASSERT(0);
return(atUnknown);
}
case ftUnknown:
// Unknown action type
ASSERT(0);
break;
}
return(atUnknown);
}
void iPod_slst::UpdateMHODPointers()
{
if(IsSmartPlaylist() == false)
return;
splPref = AddString(MHOD_SPLPREF);
splData = AddString(MHOD_SPLDATA);
}
void iPod_slst::SetPrefs(const bool liveupdate, const bool rules_enabled, const bool limits_enabled,
const unsigned long limitvalue, const unsigned long limittype, const unsigned long limitsort)
{
UpdateMHODPointers();
ASSERT(splPref);
if(splPref)
{
splPref->liveupdate = liveupdate ? 1 : 0;
splPref->checkrules = rules_enabled ? 1 : 0;
splPref->checklimits = limits_enabled ? 1 : 0;
splPref->matchcheckedonly = 0;
splPref->limitsort_opposite = limitsort & 0x80000000 ? 1 : 0;
splPref->limittype = limittype;
splPref->limitsort = limitsort & 0x000000ff;
splPref->limitvalue = limitvalue;
}
}
unsigned long iPod_slst::GetRuleCount()
{
UpdateMHODPointers();
ASSERT(splData);
if(splData == NULL)
return(0);
return(splData->rule.size());
}
int iPod_slst::AddRule(const unsigned long field,
const unsigned long action,
const wchar_t * string, // use string for string based rules
const unsigned __int64 value, // use value for single variable rules
const unsigned __int64 from, // use from and to for range based rules
const unsigned __int64 to,
const unsigned __int64 units) // use units for "In The Last" based rules
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_slst_AddRule);
#endif
UpdateMHODPointers();
ASSERT(splPref != NULL && splData != NULL);
if (splPref == NULL || splData == NULL) return -1;
// create a new rule
SPLRule *r = new SPLRule;
ASSERT(r);
if(r == NULL)
return(-1);
const FieldType ft = GetFieldType(field);
const ActionType at = GetActionType(field, action);
if(ft == ftUnknown || at == atUnknown)
{
ASSERT(0);
return(-1);
}
r->field = field;
r->action = action;
r->unk1 = 0;
r->unk2 = 0;
r->unk3 = 0;
r->unk4 = 0;
r->unk5 = 0;
if(ft == ftString)
{
// it's a string type (SetString() sets the length)
r->SetString(string);
}
else
{
// All non-string rules currently have a length of 68 bytes
r->length = 0x44;
/* Values based on ActionType:
* int: fromvalue = value, fromdate = 0, fromunits = 1, tovalue = value, todate = 0, tounits = 1
* playlist: same as int
* boolean: same as int, except fromvalue/tovalue are 1 if set, 0 if not set
* date: same as int
* range: same as int, except use from and to, instead of value
* in the last: fromvalue = 0x2dae2dae2dae2dae (SPLDATE_IDENTIFIER), fromdate = 0xffffffffffffffff - value, fromunits = seconds in period,
* tovalue = 0x2dae2dae2dae2dae (SPLDATE_IDENTIFIER), todate = 0xffffffffffffffff - value, tounits = seconds in period
*/
switch(at)
{
case atInt:
case atPlaylist:
case atDate:
r->fromvalue = value;
r->fromdate = 0;
r->fromunits = 1;
r->tovalue = value;
r->todate = 0;
r->tounits = 1;
break;
case atBoolean:
r->fromvalue = value > 0 ? 1 : 0;
r->fromdate = 0;
r->fromunits = 1;
r->tovalue = r->fromvalue;
r->todate = 0;
r->tounits = 1;
break;
case atRange:
r->fromvalue = from;
r->fromdate = 0;
r->fromunits = 1;
r->tovalue = to;
r->todate = 0;
r->tounits = 1;
break;
case atInTheLast:
r->fromvalue = SPLDATE_IDENTIFIER;
r->fromdate = ConvertNumToDateValue(value);
r->fromunits = units;
r->tovalue = SPLDATE_IDENTIFIER;
r->todate = ConvertNumToDateValue(value);
r->tounits = units;
break;
case atNone:
break;
default:
ASSERT(0);
break;
}
}
// set isPopulated to false, since we're modifying the rules
isPopulated = false;
// push the rule into the mhod
splData->rule.push_back(r);
return(splData->rule.size() - 1);
}
int iPod_slst::AddRule(const SPLRule &rule)
{
UpdateMHODPointers();
ASSERT(splPref != NULL && splData != NULL);
if (splPref == NULL || splData == NULL)
return -1;
SPLRule *r = new SPLRule;
ASSERT(r);
if(r == NULL)
return(-1);
r->action = rule.action;
r->field = rule.field;
r->fromdate = rule.fromdate;
r->fromunits = rule.fromunits;
r->fromvalue = rule.fromvalue;
r->length = rule.length;
r->SetString(rule.string);
r->todate = rule.todate;
r->tounits = rule.tounits;
r->tovalue = rule.tovalue;
r->unk1 = rule.unk1;
r->unk2 = rule.unk2;
r->unk3 = rule.unk3;
r->unk4 = rule.unk4;
r->unk5 = rule.unk5;
// set isPopulated to false, since we're modifying the rules
isPopulated = false;
// push the rule into the mhod
splData->rule.push_back(r);
return(splData->rule.size() - 1);
}
void iPod_slst::RemoveAllRules()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_slst_RemoveAllRules);
#endif
// Note: Don't update MHOD pointers here...if they don't already exist, there is no point in creating them
if(splData == NULL)
return;
//while(splData->rule.size() > 0)
//{
// // set isPopulated to false, since we're modifying the rules
// isPopulated = false;
// SPLRule *r = splData->rule[0];
// splData->rule.eraseindex(0);
// delete r;
//}
auto it = splData->rule.begin();
while ( it != splData->rule.end())
{
// set isPopulated to false, since we're modifying the rules
isPopulated = false;
SPLRule* r = *it;
it = splData->rule.erase(it);
delete r;
}
splData->rule.clear();
}
void iPod_slst::Reset()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_slst_Reset);
#endif
// Note: Don't update MHOD pointers here...if they don't already exist, there is no point in creating them
RemoveAllRules();
isPopulated = false;
if(splPref)
{
splPref->liveupdate = 1;
splPref->checkrules = 1;
splPref->checklimits = 0;
splPref->matchcheckedonly = 0;
splPref->limittype = LIMITTYPE_SONGS;
splPref->limitsort = LIMITSORT_RANDOM;
splPref->limitsort_opposite = 0;
splPref->limitvalue = 25;
}
if(splData)
{
splData->rules_operator = SPLMATCH_AND;
splData->unk5 = 0;
}
}
//////////////////////////////////////////////////////////////////////
// iPod_mhdp - Class for parsing the Play Counts file
//////////////////////////////////////////////////////////////////////
iPod_mhdp::iPod_mhdp()
{
children = 0;
entry = 0;
}
iPod_mhdp::~iPod_mhdp()
{
free(entry);
}
long iPod_mhdp::parse(const uint8_t *data)
{
long ptr=0;
//check mhdp header
if (_strnicmp((char *)&data[ptr],"mhdp",4)) return -1;
ptr+=4;
// get sizes
size_head=rev4(&data[ptr]);
ptr+=4;
entrysize=rev4(&data[ptr]);
ptr+=4;
if (entrysize != 0x10 && entrysize != 0x0c && entrysize != 0x14 && entrysize != 0x1c)
{
ASSERT(0);
return -1; // can't understand new versions of this file
}
children=rev4(&data[ptr]);
ptr+=4;
// skip dummy space
ptr=size_head;
unsigned long i;
entry = (PCEntry *)malloc(children * sizeof(PCEntry));
for (i=0;i<children;i++)
{
PCEntry &e = entry[i];
e.playcount=rev4(&data[ptr]);
ptr+=4;
e.lastplayedtime=rev4(&data[ptr]);
ptr+=4;
e.bookmarktime=rev4(&data[ptr]);
ptr+=4;
if (entrysize >= 0x10)
{
e.stars=rev4(&data[ptr]);
ptr+=4;
}
else
e.stars = 0;
if (entrysize >= 0x14)
{
e.unk1 = rev4(&data[ptr]);
ptr+=4;
}
else
e.unk1 = 0;
if (entrysize >= 0x1c)
{
e.skipcount = rev4(&data[ptr]);
ptr+=4;
e.skippedtime = rev4(&data[ptr]);
ptr+=4;
}
else
{
e.skipcount = 0;
e.skippedtime = 0;
}
}
return children;
}
//////////////////////////////////////////////////////////////////////
// iPod_mhpo - Class for parsing the OTGPlaylist file
//////////////////////////////////////////////////////////////////////
iPod_mhpo::iPod_mhpo() :
size_head(0),
unk1(0),
unk2(0)
{
idList = 0;
children = 0;
}
iPod_mhpo::~iPod_mhpo()
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhpo_destructor);
#endif
free(idList);
}
long iPod_mhpo::parse(const uint8_t *data)
{
long ptr=0;
//check mhdp header
if (_strnicmp((char *)&data[ptr],"mhpo",4)) return -1;
ptr+=4;
// get sizes
size_head=rev4(&data[ptr]);
ptr+=4;
unk1=rev4(&data[ptr]);
ptr+=4;
children=rev4(&data[ptr]); // Only used locally
ptr+=4;
unk2=rev4(&data[ptr]);
ptr+=4;
if (ptr!=size_head) return -1; // if this isn't true, I screwed up somewhere
unsigned long i;
idList = (uint32_t *)malloc(children * sizeof(uint32_t));
for (i=0;i<children;i++)
{
unsigned long temp;
temp=rev4(&data[ptr]);
ptr+=4;
idList[i]=temp;
}
return ptr;
}
long iPod_mhpo::write(unsigned char * data, const unsigned long datasize)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iPod_mhpo_write);
#endif
long ptr=0;
const unsigned int headsize=0x14;
// check for header size + child size
if (headsize+(GetChildrenCount()*4)>datasize) return -1;
//write mhpo header
data[0]='m';data[1]='h';data[2]='p';data[3]='o';
ptr+=4;
// write size
rev4(headsize,&data[ptr]); // header size
ptr+=4;
// fill in stuff
rev4(unk1,&data[ptr]);
ptr+=4;
const unsigned long children = GetChildrenCount();
rev4(children,&data[ptr]);
ptr+=4;
rev4(unk2,&data[ptr]);
ptr+=4;
for (unsigned long i=0;i<children;i++)
{
rev4(idList[i],&data[ptr]);
ptr+=4;
}
return ptr;
}
iPod_mhyp * iPod_mhpo::CreatePlaylistFromOTG(iPod_mhbd * iPodDB, wchar_t * name)
{
// create playlist
iPod_mhyp * myplaylist = iPodDB->mhsdplaylists->mhlp->AddPlaylist();
if (myplaylist == NULL)
return NULL;
// set name
iPod_mhod * playlistName = myplaylist->AddString(MHOD_TITLE);
playlistName->SetString(name);
unsigned long count = GetChildrenCount();
// for every track
for (unsigned long i=0;i<count;i++)
{
// find the track
iPod_mhit * track = iPodDB->mhsdsongs->mhlt->GetTrack(idList[i]);
// myplaylist->AddPlaylistEntry(iPod_mhip * entry, track->id);
// Add the playlist entry locally rather than using
// AddPlaylistEntry() for speed optimization reasons
iPod_mhip *entry = new iPod_mhip;
ASSERT(entry != NULL);
if(entry && track)
{
entry->songindex = track->id;
myplaylist->mhip.push_back(entry);
}
}
return myplaylist;
}
//////////////////////////////////////////////////////////////////////
// iPod_mqed - Class for parsing the EQ Presets file
//////////////////////////////////////////////////////////////////////
iPod_mqed::iPod_mqed() :
unk1(1),
unk2(1),
eqList(),
size_head(0x68)
{
}
iPod_mqed::~iPod_mqed()
{
const unsigned long count = GetChildrenCount();
for (unsigned long i=0;i<count;i++)
{
iPod_pqed * m=eqList[i];
delete m;
}
eqList.clear();
}
long iPod_mqed::parse(const uint8_t *data)
{
unsigned long ptr=0;
uint32_t i;
//check mqed header
if (_strnicmp((char *)&data[ptr],"mqed",4)) return -1;
ptr+=4;
size_head=rev4(&data[ptr]);
ptr+=4;
ASSERT(size_head == 0x68);
unk1=rev4(&data[ptr]);
ptr+=4;
unk2=rev4(&data[ptr]);
ptr+=4;
unsigned long numchildren=rev4(&data[ptr]);
ptr+=4;
unsigned long childsize=rev4(&data[ptr]);
ptr+=4;
ASSERT(childsize == 588);
if (childsize != 588) return -1;
// skip the nulls
ptr=size_head;
for (i=0;i<numchildren;i++)
{
iPod_pqed * e = new iPod_pqed;
long ret = e->parse(&data[ptr]);
if (ret < 0)
{
delete e;
return ret;
}
eqList.push_back(e);
ptr+=ret;
}
return ptr;
}
long iPod_mqed::write(unsigned char * data, const unsigned long datasize)
{
unsigned long ptr=0;
uint32_t i;
//write mqed header
data[0]='m';data[1]='q';data[2]='e';data[3]='d';
ptr+=4;
rev4(size_head,&data[ptr]);
ptr+=4;
rev4(unk1,&data[ptr]);
ptr+=4;
rev4(unk2,&data[ptr]);
ptr+=4;
rev4(GetChildrenCount(),&data[ptr]);
ptr+=4;
rev4(588,&data[ptr]);
ptr+=4;
// fill with nulls
while (ptr<size_head)
{
data[ptr]=0; ptr++;
}
// write the eq settings
for (i=0;i<GetChildrenCount();i++)
{
long ret=eqList[i]->write(&data[ptr],datasize-ptr);
if (ret <0) return -1;
ptr+=ret;
}
return ptr;
}
//////////////////////////////////////////////////////////////////////
// iPod_pqed - Class for parsing the EQ Entries
//////////////////////////////////////////////////////////////////////
iPod_pqed::iPod_pqed() :
name(NULL),
preamp(0)
{
int i;
for (i=0;i<10;i++)
eq[i]=0;
for (i=0;i<5;i++)
short_eq[i]=0;
}
iPod_pqed::~iPod_pqed()
{
if (name) delete [] name;
}
long iPod_pqed::parse(const uint8_t *data)
{
unsigned long ptr=0;
uint32_t i;
//check pqed header
if (_strnicmp((char *)&data[ptr],"pqed",4)) return -1;
ptr+=4;
// get string length
length=data[ptr]; ptr++;
length+=data[ptr]*256; ptr++;
name=new wchar_t[length + 1];
memcpy(name,&data[ptr],length);
name[length / 2] = '\0';
// skip the nulls
ptr=516;
preamp = rev4(&data[ptr]);
ptr+=4;
unsigned long numbands;
numbands = rev4(&data[ptr]);
ptr+=4;
ASSERT (numbands == 10);
if (numbands != 10) return -1;
for (i=0;i!=numbands;i++)
{
eq[i]=rev4(&data[ptr]);
ptr+=4;
}
numbands = rev4(&data[ptr]);
ptr+=4;
ASSERT (numbands == 5);
if (numbands != 5) return -1;
for (i=0;i!=numbands;i++)
{
short_eq[i]=rev4(&data[ptr]);
ptr+=4;
}
return ptr;
}
long iPod_pqed::write(unsigned char * data, const unsigned long datasize)
{
long ptr=0;
uint32_t i;
//write pqed header
data[0]='p';data[1]='q';data[2]='e';data[3]='d';
ptr+=4;
// write 2 byte string length
data[ptr++]=(uint8_t)(length & 0xff);
data[ptr++]=(uint8_t)((length >> 8) & 0xff);
for (i=0;i!=length;i++)
{
data[ptr++]=name[i] & 0xff;
data[ptr++]=(name[i] >> 8) & 0xff;
}
// fill rest with nulls
while (ptr<516)
{
data[ptr++]=0;
}
rev4(preamp,&data[ptr]);
ptr+=4;
rev4(10,&data[ptr]);
ptr+=4;
for (i=0;i<10;i++)
{
rev4(eq[i],&data[ptr]);
ptr+=4;
}
rev4(5,&data[ptr]);
ptr+=4;
for (i=0;i<5;i++)
{
rev4(short_eq[i],&data[ptr]);
ptr+=4;
}
return ptr;
}
iTunesStats::iTunesStats() :
mhlt(NULL)
{
entry = 0;
children = 0;
}
iTunesStats::~iTunesStats()
{
free(entry);
}
long iTunesStats::parse(const uint8_t *data)
{
long ptr=0;
children = rev3(&data[ptr]);
ptr+=3;
unk1 = rev3(&data[ptr]);
ptr+=3;
unsigned long i;
entry = (iTunesStatsEntry *)malloc(children*sizeof(iTunesStatsEntry));
for (i=0;i<children;i++)
{
iTunesStatsEntry &e = entry[i];
e.entry_size = rev3(&data[ptr]);
ptr+=3;
e.bookmarktime = rev3(&data[ptr]);
ptr+=3;
e.unk1 = rev3(&data[ptr]);
ptr+=3;
e.unk2 = rev3(&data[ptr]);
ptr+=3;
e.playcount = rev3(&data[ptr]);
ptr+=3;
e.skippedcount = rev3(&data[ptr]);
ptr+=3;
#ifdef _DEBUG
// If any of these trigger an assertion, something new is in the database format
ASSERT(e.entry_size == 0x12);
//ASSERT(e.unk1 == 0);
ASSERT(e.unk2 == 0);
#endif
}
return children;
}
// Unlike Play Counts, iTunesStats needs to be created by the application - apparently only for the bookmark time
long iTunesStats::write(unsigned char * data, const unsigned long datasize)
{
ASSERT(mhlt != NULL);
if(mhlt == NULL)
return(-1);
const unsigned int numentries = mhlt->GetChildrenCount();
const unsigned int total_size = 6 + (numentries * sizeof(iTunesStatsEntry)); // 6 bytes for the header
ASSERT(datasize >= total_size);
if(datasize < total_size)
return(-1);
long ptr=0;
rev3(numentries, &data[ptr]);
ptr+=3;
put3(unk1, &data[ptr]);
ptr+=3;
// Create a new iTunesStatsEntry for each song, only preserving the bookmark time
long ret = 0;
for(unsigned int i=0; i<numentries; i++)
{
iPod_mhit *mhit = mhlt->GetTrack(i);
ASSERT(mhit);
if(mhit == NULL)
return(-1);
rev3(0x12, &data[ptr]); // Entry size
ptr+=3;
rev3(mhit->bookmarktime / 256, &data[ptr]);
ptr+=3;
rev3(0, &data[ptr]); // iTunesStatsEntry.unk1
ptr+=3;
rev3(0, &data[ptr]); // iTunesStatsEntry.unk2
ptr+=3;
rev3(0, &data[ptr]); // Don't write out the playcount
ptr+=3;
rev3(0, &data[ptr]); // or the skipped count
ptr+=3;
}
return(ptr);
}
iTunesShuffle::iTunesShuffle() :
datasize(0)
{
numentries=0;
entry = 0;
}
iTunesShuffle::~iTunesShuffle()
{
free(entry);
}
long iTunesShuffle::parse(const uint8_t *data)
{
ASSERT(datasize > 0);
// iTunesShuffle is simply a list of reversed 3 byte indexes
ASSERT(datasize % 3 == 0);
if(datasize % 3 != 0)
return(-1);
free(entry);
long ptr = 0;
numentries = datasize / 3;
entry = (uint32_t *)malloc(numentries*sizeof(uint32_t));
for(unsigned int i=0; i<numentries; i++)
{
unsigned int value = rev3(&data[ptr]);
if(value == 0xffffff) // This can happen if iTunesSD::playflags doesn't have the shuffle bit set
value = 0;
entry[i]=value;
ptr+=3;
}
return(ptr);
}
long iTunesShuffle::write(unsigned char *data, const unsigned long datasize)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iTunesShuffle_write);
#endif
const unsigned int total_size = numentries * 3;
ASSERT(datasize >= total_size);
if(datasize < total_size)
return(-1);
long ptr = 0;
for(unsigned int i=0; i<numentries; i++)
{
rev3(entry[i], &data[ptr]);
ptr+=3;
}
return(ptr);
}
void iTunesShuffle::Randomize()
{
Randomize(numentries);
}
void iTunesShuffle::Randomize(const unsigned int numsongs)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iTunesShuffle_randomize);
#endif
if(numsongs == 0)
return;
if (numentries < numsongs)
{
free(entry);
entry = (uint32_t *)malloc(numsongs*sizeof(uint32_t));
}
numentries=numsongs;
for (uint32_t i=0;i!=numsongs;i++)
{
entry[i] = i;
}
std::random_shuffle(entry, entry+numsongs);
}
//////////////////////////////////////////////////////////////////////
// MHIA - Album Item
//////////////////////////////////////////////////////////////////////
iPod_mhia::iPod_mhia()
: unk1(0), albumid(0), type(2)
{
dbid = Generate64BitID();
}
iPod_mhia::~iPod_mhia()
{
for (auto obj : mhod)
{
delete obj;
}
mhod.clear();
}
long iPod_mhia::parse(const uint8_t *data)
{
return 0;
/*
long ptr = 0;
if (_strnicmp((char *)&data[ptr],"mhia",4)) return -1;
ptr+=4;
uint32_t size_head=rev4(&data[ptr]);
ptr+=4;
uint32_t size_total=rev4(&data[ptr]);
ptr+=4;
uint32_t num_children=rev4(&data[ptr]);
ptr+=4;
unk1 = rev2(&data[ptr]);
ptr+=2;
albumid = rev4(&data[ptr]);
ptr+=4;
dbid = rev8(get8(&data[ptr]));
ptr+=8;
type = rev4(&data[ptr]);
ptr+=4;
ptr = size_head; // skip nulls
for(uint32_t i = 0; i < num_children; i++)
{
iPod_mhod* m = new iPod_mhod();
long ret = m->parse(data + ptr);
if(ret == -1)
{
delete m;
return -1;
}
mhod.push_back(m);
ptr += ret;
}
return ptr;
*/
}
long iPod_mhia::write(unsigned char * data, const unsigned long datasize)
{
const unsigned int headsize=0x5C;
// check for header size
if (headsize>datasize) return -1;
long ptr=0;
//write mhla header
data[0]='m';data[1]='h';data[2]='i';data[3]='a';
ptr+=4;
rev4(headsize,&data[ptr]); // header size
ptr+=4;
rev4(-1,&data[ptr]); // total size (put this in later)
ptr+=4;
rev4((uint32_t)mhod.size(),&data[ptr]); // number of strings
ptr+=4;
rev2(unk1, &data[ptr]);
ptr+=2;
rev2(albumid, &data[ptr]);
ptr+=2;
put8(rev8(dbid), &data[ptr]);
ptr+=8;
rev4(type, &data[ptr]);
ptr+=4;
memset(&data[ptr], 0, 60); // a whole shitload of zeroes
ptr+=60;
for (size_t i=0;i!=mhod.size();i++)
{
long ret = mhod[i]->write(data + ptr, datasize - ptr);
if (ret == -1)
return -1;
ptr += ret;
}
// put in total size
rev4(ptr, &data[8]);
return ptr;
}
//////////////////////////////////////////////////////////////////////
// MHLA - Album List
//////////////////////////////////////////////////////////////////////
iPod_mhla::iPod_mhla() : nextAlbumId(400)
{
}
iPod_mhla::~iPod_mhla()
{
}
long iPod_mhla::parse(const uint8_t *data)
{
return 0;
/*
long ptr=0;
if (_strnicmp((char *)&data[ptr],"mhla",4)) return -1;
ptr+=4;
uint32_t size_head=rev4(&data[ptr]);
ptr+=4;
uint32_t num_children=rev4(&data[ptr]);
ptr+=4;
ptr = size_head; // skip nulls
for(uint32_t i = 0; i < num_children; i++)
{
iPod_mhia* m = new iPod_mhia();
long ret = m->parse(data + ptr);
if(ret == -1)
{
delete m;
return -1;
}
mhia.push_back(m);
ptr += ret;
}
return ptr;
*/
}
long iPod_mhla::write(unsigned char * data, const unsigned long datasize)
{
const unsigned int headsize=0x5C;
// check for header size
if (headsize>datasize) return -1;
long ptr=0;
//write mhla header
data[0]='m';data[1]='h';data[2]='l';data[3]='a';
ptr+=4;
rev4(headsize,&data[ptr]); // header size
ptr+=4;
rev4((uint32_t)albums.size(),&data[ptr]); // number of albums
ptr+=4;
memset(&data[ptr], 0, 80); // a whole shitload of zeroes
ptr+=80;
/*
for (size_t i=0;i!=mhia.size();i++)
{
long ret = mhia[i]->write(data + ptr, datasize - ptr);
if (ret == -1)
return -1;
ptr += ret;
}
*/
for(albums_map_t::iterator i = albums.begin(); i!=albums.end(); i++)
{
iPod_mhia mhia;
mhia.albumid = i->second;
iPod_mhod* artist = new iPod_mhod();
artist->SetString(i->first.artist);
artist->type = MHOD_ALBUMLIST_ARTIST;
mhia.mhod.push_back(artist);
iPod_mhod* album = new iPod_mhod();
album->SetString(i->first.album);
album->type = MHOD_ALBUMLIST_ALBUM;
mhia.mhod.push_back(album);
long ret = mhia.write(data + ptr, datasize - ptr);
if (ret == -1)
return -1;
ptr += ret;
}
return ptr;
}
uint16_t iPod_mhla::GetAlbumId(const wchar_t* artist, const wchar_t* album)
{
ArtistAlbumPair key(artist, album);
if(albums.find(key) == albums.end())
{
albums[key] = nextAlbumId;
return nextAlbumId++;
}
return albums[key];
}
void iPod_mhla::ClearAlbumsList()
{
nextAlbumId = 0;
albums.clear();
}
|