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
|
#include "iPodSD.h"
#include <math.h>
#include <assert.h>
// get 3 bytes from data (used in iTunesSD1)
static __forceinline unsigned long get3(const uint8_t * data)
{
unsigned long ret = 0;
ret += ((unsigned long) data[0]) << 16;
ret += ((unsigned long) data[1]) << 8;
ret += ((unsigned long) data[2]);
return ret;
}
//write 3 bytes normal (used in iTunesSD1)
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;
}
// pass data and ptr, updates ptr automatically (by reference)
static __forceinline void write_uint64_t(uint8_t *data, size_t &offset, uint64_t value)
{
memcpy(&data[offset], &value, 8);
offset+=8;
}
// pass data and ptr, updates ptr automatically (by reference)
static __forceinline void write_uint32_t(uint8_t *data, size_t &offset, uint32_t value)
{
memcpy(&data[offset], &value, 4);
offset+=4;
}
// pass data and ptr, updates ptr automatically (by reference)
static __forceinline void write_uint16_t(uint8_t *data, size_t &offset, uint16_t value)
{
memcpy(&data[offset], &value, 2);
offset+=2;
}
// pass data and ptr, updates ptr automatically (by reference)
static __forceinline void write_uint8_t(uint8_t *data, size_t &offset, uint8_t value)
{
data[offset++] = value;
}
// pass data and ptr, updates ptr automatically (by reference)
static __forceinline void write_header(uint8_t *data, size_t &offset, const char *header)
{
data[offset++] = header[0];
data[offset++] = header[1];
data[offset++] = header[2];
data[offset++] = header[3];
}
// Case insensitive version of wcsstr
static 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;
}
//////////////////////////////////////////////////////////////////////
// iTunesSD1 - Classes for dealing with the iPodShuffle
//////////////////////////////////////////////////////////////////////
iTunesSD1::iTunesSD1()
{
}
iTunesSD1::~iTunesSD1()
{
}
long iTunesSD1::write(const iPod_mhlt::mhit_map_t *songs, unsigned char * data, const unsigned long datasize)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iTunesSD_write);
#endif
const unsigned int numsongs = songs->size();
const unsigned int total_size = 18 + (numsongs * 558);
ASSERT(datasize >= total_size);
if(datasize < total_size)
return(-1);
long ptr=0;
put3(numsongs, &data[ptr]);
ptr+=3;
put3(0x010600, &data[ptr]);
ptr+=3;
put3(0x12, &data[ptr]);
ptr+=3;
put3(0, &data[ptr]);
ptr+=3;
put3(0, &data[ptr]);
ptr+=3;
put3(0, &data[ptr]);
ptr+=3;
iPod_mhlt::mhit_map_t::const_iterator begin = songs->begin();
iPod_mhlt::mhit_map_t::const_iterator end = songs->end();
for(iPod_mhlt::mhit_map_t::const_iterator it = begin; it != end; it++)
{
iPod_mhit *m = ((*it).second);
iTunesSD_Song song(m);
long ret = song.write(&data[ptr], datasize - ptr);
if (ret < 0)
return ret;
ptr += ret;
}
return(ptr);
}
iTunesSD_Song::iTunesSD_Song(const iPod_mhit *m) : size_total(0x22e), starttime(0), stoptime(0), volume(0x64), filetype(0), playflags(iTunesSD_Song::SHUFFLE)
{
memset(filename, 0, (SDSONG_FILENAME_LEN + 1) * sizeof(wchar_t));
iPod_mhod *mhod = m->FindString(MHOD_LOCATION);
ASSERT(mhod);
if(mhod)
{
// Convert from HFS format (:iPod_Control:Music:F00:filename) to quasi-FAT format (/iPod_Control/Music/F00/filename) filepaths
SetFilename(mhod->str);
wchar_t *w = filename;
while(w && *w != '\0')
{
if(*w == ':')
*w = '/';
w = CharNext(w);
}
SetStartTime(m->starttime);
SetStopTime(m->stoptime);
int volume = (int)m->volume;
// If Sound Check information is present, use that instead of volume
if(m->soundcheck != 0)
{
// This code converts SoundCheck back into a gain value, then into a -255 to 255 mhit::volume value
const double gain = -10.0 * log10(m->soundcheck / 1000.0);
volume = (int)(gain * 12.75); // XXX - this might not be the best way to convert the gain value...
}
if(volume < -255)
volume = -255;
else if(volume > 255)
volume = 255;
// Convert the volume value into a percentage for SetVolume
SetVolume((int)((double)volume / 2.55));
// To determine the filetype, first check the MHOD_FILETYPE type. If that isn't available, fallback to file extension
iPod_mhod *mtype = m->FindString(MHOD_FILETYPE);
if(mtype != NULL)
{
if(wcsistr(mtype->str, L"MPEG") != NULL || wcsistr(mtype->str, L"MP3") != NULL)
filetype = iTunesSD_Song::MP3;
else if(wcsistr(mtype->str, L"AAC") != NULL)
filetype = iTunesSD_Song::AAC;
else if(wcsistr(mtype->str, L"WAV") != NULL)
filetype = iTunesSD_Song::WAV;
}
if(filetype == 0)
{
if(wcsistr(mhod->str, L".mp3") != NULL)
filetype = iTunesSD_Song::MP3;
else if(wcsistr(mhod->str, L".m4a") != NULL || wcsistr(mhod->str, L".m4b") != NULL || wcsistr(mhod->str, L".m4p") != NULL)
filetype = iTunesSD_Song::AAC;
else if(wcsistr(mhod->str, L".wav") != NULL)
filetype = iTunesSD_Song::WAV;
}
ASSERT(filetype != 0);
if(filename == 0)
filetype = iTunesSD_Song::MP3; // Default to mp3
if(wcsistr(mhod->str, L".m4b") != NULL)
playflags = iTunesSD_Song::BOOKMARKABLE; // Only playback in normal mode
else
playflags = iTunesSD_Song::SHUFFLE; // Playable in normal/shuffle modes, but not bookmarkable
}
}
long iTunesSD_Song::write(unsigned char * data, const unsigned long datasize)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iTunesSD_Song_write);
#endif
long ptr=0;
ASSERT(size_total == 0x22e);
ASSERT(filetype != 0);
put3(size_total, &data[ptr]);
ptr+=3;
put3(0x005aa501, &data[ptr]);
ptr+=3;
put3(starttime, &data[ptr]);
ptr+=3;
put3(0, &data[ptr]);
ptr+=3;
put3(0, &data[ptr]);
ptr+=3;
put3(stoptime, &data[ptr]);
ptr+=3;
put3(0, &data[ptr]);
ptr+=3;
put3(0, &data[ptr]);
ptr+=3;
put3(volume, &data[ptr]);
ptr+=3;
put3(filetype, &data[ptr]);
ptr+=3;
put3(0x200, &data[ptr]);
ptr+=3;
const unsigned int bufSize = (SDSONG_FILENAME_LEN + 1) * sizeof(wchar_t);
memcpy(&data[ptr], filename, bufSize);
ptr+=bufSize;
put3(playflags, &data[ptr]);
ptr+=3;
ASSERT(size_total == ptr);
return(ptr);
}
void iTunesSD_Song::SetFilename(const wchar_t *filename)
{
#ifdef IPODDB_PROFILER
profiler(iPodDB__iTunesStats_SetFilename);
#endif
ASSERT(filename != NULL);
if(filename == NULL)
return;
if(filename)
{
lstrcpyn(this->filename, filename, SDSONG_FILENAME_LEN);
}
else
{
memset(this->filename, 0, SDSONG_FILENAME_LEN * sizeof(wchar_t));
}
}
// Accepts values from -100 to 100, with 0 meaning no volume change
void iTunesSD_Song::SetVolume(const int percent)
{
int p = percent;
if(p > 100)
p = 100;
else if(p < -100)
p = -100;
// Volume ranges from 0 (-100%) to 100 (0%) to 200 (100%)
volume = (unsigned int)(percent + 100);
}
/* Shadow DB version 2 */
long iTunesSD2::write(const iPod_mhlt *songs, const iPod_mhlp *playlists, unsigned char * data, const unsigned long datasize)
{
uint32_t numsongs = songs->GetChildrenCount();
uint32_t numplaylists = playlists->GetChildrenCount();
size_t offset=0;
size_t ptr=0;
if (datasize < 64)
return -1;
write_header(data, ptr, "bdhs");
write_uint32_t(data, ptr, 0x02000003); /* also have seen 0x02010001, perhaps a DB version number? */
write_uint32_t(data, ptr, 64); /* length of header */
write_uint32_t(data, ptr, numsongs);
write_uint32_t(data, ptr, numplaylists); /* number of playlists */
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint8_t(data, ptr, 0); /* volume limit */
write_uint8_t(data, ptr, 1); /* voiceover */
write_uint16_t(data, ptr, 0);
write_uint32_t(data, ptr, numsongs); /* TODO number of tracks w/o podcasts and audiobooks*/
write_uint32_t(data, ptr, 64); /* track header offset */
write_uint32_t(data, ptr, 64+20 + numsongs*4+iTunesSD2_Song::header_size*numsongs); /* playlist header offset */
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
offset = 64;
uint32_t hths_header_size = 20+numsongs*4;
if (datasize - ptr < hths_header_size)
return -1;
write_header(data, ptr, "hths");
write_uint32_t(data, ptr, hths_header_size); /* header length */
write_uint32_t(data, ptr, numsongs);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
offset += hths_header_size;
/* positions for each track */
for (size_t i=0;i<numsongs;i++)
{
write_uint32_t(data, ptr, offset + iTunesSD2_Song::header_size * i);
}
/* write tracks */
for (uint32_t i=0;i<numsongs;i++)
{
iPod_mhit *m = songs->GetTrack(i);
long ret = iTunesSD2_Song::write(m, &data[ptr], datasize - ptr);
if (ret < 0)
return ret;
ptr += ret;
offset += ret;
}
uint32_t podcast_playlist_count=0;
for (size_t i=0;i<numplaylists;i++)
{
iPod_mhyp *p = playlists->GetPlaylist(i);
if (p->podcastflag)
podcast_playlist_count++;
}
uint32_t hphs_header_size = 20 + numplaylists*4;
if (datasize - ptr < hphs_header_size)
return -1;
write_header(data, ptr, "hphs");
write_uint32_t(data, ptr, hphs_header_size); /* header length */
write_uint32_t(data, ptr, numplaylists);
write_uint16_t(data, ptr, 0);
write_uint16_t(data, ptr, numplaylists-podcast_playlist_count); /* non-podcast playlists */
write_uint16_t(data, ptr, 1); /* master playlists */
write_uint16_t(data, ptr, numplaylists); /* non-audiobook playlists */
offset += hphs_header_size;
/* write offsets for each track */
for (size_t i=0;i<numplaylists;i++)
{
iPod_mhyp *p = playlists->GetPlaylist(i);
write_uint32_t(data, ptr, offset);
offset += p->GetMhipChildrenCount()*4 + 44;
}
iPod_mhyp *master_playlist = playlists->GetPlaylist(0);
/* write playlists */
for (size_t i=0;i<numplaylists;i++)
{
iPod_mhyp *p = playlists->GetPlaylist(i);
long ret = iTunesSD2_Playlist::write(master_playlist, p, &data[ptr], datasize - ptr);
if (ret < 0)
return ret;
ptr += ret;
}
return ptr;
}
uint32_t iTunesSD2_Song::header_size = 372;
long iTunesSD2_Song::write(const iPod_mhit *mhit, unsigned char *data, const unsigned long datasize)
{
if (datasize < header_size)
return -1;
size_t ptr=0;
write_header(data, ptr, "rths");
write_uint32_t(data, ptr, header_size); /* length of header */
write_uint32_t(data, ptr, mhit->starttime); /* start time, in milliseconds */
write_uint32_t(data, ptr, mhit->stoptime); /* stop time, in milliseconds */
write_uint32_t(data, ptr, mhit->volume); /* volume */
switch(mhit->filetype)
{
case FILETYPE_WAV:
write_uint32_t(data, ptr, iTunesSD_Song::WAV); /* file type */
break;
case FILETYPE_M4A:
case 0x4d344220:
case 0x4d345020:
write_uint32_t(data, ptr, iTunesSD_Song::AAC); /* file type */
break;
case FILETYPE_MP3:
default:
write_uint32_t(data, ptr, iTunesSD_Song::MP3); /* file type */
break;
}
iPod_mhod *mhod = mhit->FindString(MHOD_LOCATION);
char filename[256] = {0};
int converted = WideCharToMultiByte(CP_UTF8, 0, mhod->str, -1, filename, 256, 0, 0);
for (int i=0;i<converted;i++)
{
if (filename[i] == ':')
filename[i] = '/';
}
memcpy(&data[ptr], filename, converted);
ptr+=converted;
memset(&data[ptr], 0, 256-converted);
ptr+=256-converted;
write_uint32_t(data, ptr, mhit->bookmarktime); /* bookmark time */
write_uint8_t(data, ptr, 0); /* skip flag */
write_uint8_t(data, ptr, mhit->rememberPosition); /* remember playback position */
write_uint8_t(data, ptr, 0); /* part of gapless album */
write_uint8_t(data, ptr, 0);
write_uint32_t(data, ptr, mhit->pregap); /* pre-gap */
write_uint32_t(data, ptr, mhit->postgap); /* post-gap */
write_uint64_t(data, ptr, mhit->samplecount); /* number of samples */
write_uint32_t(data, ptr, mhit->gaplessData); /* gapless data */
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, mhit->album_id); /* album ID */
write_uint16_t(data, ptr, mhit->tracknum); /* track number */
write_uint16_t(data, ptr, mhit->cdnum); /* disc number */
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint64_t(data, ptr, mhit->dbid); /* dbid */
write_uint32_t(data, ptr, 0); /* artist ID */
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
return ptr;
}
long iTunesSD2_Playlist::write(const iPod_mhyp *master_playlist, const iPod_mhyp *playlist, unsigned char * data, const unsigned long datasize)
{
size_t ptr=0;
uint32_t tracks = playlist->GetMhipChildrenCount();
uint32_t header_size = 44 + tracks*4;
if (datasize < header_size)
return -1;
write_header(data, ptr, "lphs");
write_uint32_t(data, ptr, header_size); /* header length */
write_uint32_t(data, ptr, tracks); /* number of tracks */
/* number of music tracks TODO: special handling for master playlist */
if (playlist->podcastflag)
write_uint32_t(data, ptr, 0);
else
write_uint32_t(data, ptr, tracks);
write_uint64_t(data, ptr, playlist->playlistID); /* playlist ID */
/* playlist type */
if (playlist->podcastflag)
write_uint32_t(data, ptr, 3); /* podcast */
else if (playlist->hidden)
write_uint32_t(data, ptr, 1); /* master playlist */
else
write_uint32_t(data, ptr, 2); /* normal */
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
write_uint32_t(data, ptr, 0);
if (master_playlist == playlist)
{
for (uint32_t i=0;i<tracks;i++)
{
write_uint32_t(data, ptr, i);
}
}
else
{
for (uint32_t i=0;i<tracks;i++)
{
iPod_mhip *item = playlist->GetPlaylistEntry(i);
uint32_t master_index = master_playlist->FindPlaylistEntry(item->songindex);
assert(master_index != -1);
write_uint32_t(data, ptr, master_index);
}
}
return ptr;
}
|